Convert arm's physmem interface to MI code

The arm_physmem interface found in arm's MD code provides a convenient
set of routines for adding/excluding physical memory regions and
initializing important kernel globals such as Maxmem, realmem,
phys_avail[], and dump_avail[]. It is especially convenient for FDT
systems, since we can use FDT parsing functions and pass the result
directly to one of these physmem routines. This interface is already in
use on arm and arm64, and can be used to simplify this early
initialization on RISC-V as well.

This requires only a couple trivial changes:
  - Move arm_physmem_kernel_addr to arm/machdep.c. It is unused on arm64,
    and manipulated entirely in arm MD code.
  - Convert arm32_btop/arm64_btop to atop. This is equivalently defined
    on all architectures.
  - Drop the "arm" prefix.

Reviewed by:	manu, emaste ("looks reasonable")
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D24153
This commit is contained in:
mhorne 2020-04-19 00:12:30 +00:00
parent c8ac29f668
commit 9957fe2008
11 changed files with 52 additions and 67 deletions

View File

@ -66,6 +66,7 @@ __FBSDID("$FreeBSD$");
#include <sys/ktr.h>
#include <sys/linker.h>
#include <sys/msgbuf.h>
#include <sys/physmem.h>
#include <sys/reboot.h>
#include <sys/rwlock.h>
#include <sys/sched.h>
@ -83,7 +84,6 @@ __FBSDID("$FreeBSD$");
#include <machine/machdep.h>
#include <machine/metadata.h>
#include <machine/pcb.h>
#include <machine/physmem.h>
#include <machine/platform.h>
#include <machine/sysarch.h>
#include <machine/undefined.h>
@ -123,6 +123,9 @@ uint32_t cpu_reset_address = 0;
int cold = 1;
vm_offset_t vector_page;
/* The address at which the kernel was loaded. Set early in initarm(). */
vm_paddr_t arm_physmem_kernaddr;
int (*_arm_memcpy)(void *, void *, int, int) = NULL;
int (*_arm_bzero)(void *, int, int) = NULL;
int _min_memcpy_size = 0;
@ -160,7 +163,6 @@ static void *delay_arg;
#endif
struct kva_md_info kmi;
/*
* arm32_vector_init:
*
@ -237,7 +239,7 @@ cpu_startup(void *dummy)
(uintmax_t)arm32_ptob(vm_free_count()),
(uintmax_t)arm32_ptob(vm_free_count()) / mbyte);
if (bootverbose) {
arm_physmem_print_tables();
physmem_print_tables();
devmap_print_table();
}
@ -870,11 +872,11 @@ initarm(struct arm_boot_params *abp)
/* Grab physical memory regions information from device tree. */
if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, &memsize) != 0)
panic("Cannot get physical memory regions");
arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
physmem_hardware_regions(mem_regions, mem_regions_sz);
/* Grab reserved memory regions information from device tree. */
if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
physmem_exclude_regions(mem_regions, mem_regions_sz,
EXFLAG_NODUMP | EXFLAG_NOALLOC);
/* Platform-specific initialisation */
@ -1081,9 +1083,9 @@ initarm(struct arm_boot_params *abp)
*
* Prepare the list of physical memory available to the vm subsystem.
*/
arm_physmem_exclude_region(abp->abp_physaddr,
physmem_exclude_region(abp->abp_physaddr,
(virtual_avail - KERNVIRTADDR), EXFLAG_NOALLOC);
arm_physmem_init_kernel_globals();
physmem_init_kernel_globals();
init_param2(physmem);
dbg_monitor_init();
@ -1149,11 +1151,11 @@ initarm(struct arm_boot_params *abp)
if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,NULL) != 0)
panic("Cannot get physical memory regions");
}
arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
physmem_hardware_regions(mem_regions, mem_regions_sz);
/* Grab reserved memory regions information from device tree. */
if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
physmem_exclude_regions(mem_regions, mem_regions_sz,
EXFLAG_NODUMP | EXFLAG_NOALLOC);
/*
@ -1288,9 +1290,9 @@ initarm(struct arm_boot_params *abp)
*
* Prepare the list of physical memory available to the vm subsystem.
*/
arm_physmem_exclude_region(abp->abp_physaddr,
physmem_exclude_region(abp->abp_physaddr,
pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC);
arm_physmem_init_kernel_globals();
physmem_init_kernel_globals();
init_param2(physmem);
/* Init message buffer. */

View File

@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <sys/systm.h>
#include <sys/ctype.h>
#include <sys/linker.h>
#include <sys/physmem.h>
#include <sys/reboot.h>
#include <sys/sysctl.h>
#if defined(LINUX_BOOT_ABI)
@ -46,7 +47,6 @@ __FBSDID("$FreeBSD$");
#include <machine/cpu.h>
#include <machine/machdep.h>
#include <machine/metadata.h>
#include <machine/physmem.h>
#include <machine/vmparam.h> /* For KERNVIRTADDR */
#ifdef FDT
@ -228,7 +228,7 @@ linux_parse_boot_param(struct arm_boot_params *abp)
case ATAG_CORE:
break;
case ATAG_MEM:
arm_physmem_hardware_region(walker->u.tag_mem.start,
physmem_hardware_region(walker->u.tag_mem.start,
walker->u.tag_mem.size);
break;
case ATAG_INITRD2:

View File

@ -53,7 +53,6 @@ __FBSDID("$FreeBSD$");
#include <machine/debug_monitor.h>
#include <machine/smp.h>
#include <machine/pcb.h>
#include <machine/physmem.h>
#include <machine/intr.h>
#include <machine/vmparam.h>
#ifdef VFP

View File

@ -117,8 +117,6 @@ __FBSDID("$FreeBSD$");
#include <ddb/ddb.h>
#endif
#include <machine/physmem.h>
#include <vm/vm.h>
#include <vm/uma.h>
#include <vm/pmap.h>

View File

@ -42,6 +42,7 @@ extern uint32_t *vm_page_dump;
extern int vm_page_dump_size;
extern u_long elf_hwcap;
extern u_long elf_hwcap2;
extern vm_paddr_t arm_physmem_kernaddr;
extern int (*_arm_memcpy)(void *, void *, int, int);
extern int (*_arm_bzero)(void *, int, int);

View File

@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
#include <sys/linker.h>
#include <sys/msgbuf.h>
#include <sys/pcpu.h>
#include <sys/physmem.h>
#include <sys/proc.h>
#include <sys/ptrace.h>
#include <sys/reboot.h>
@ -82,8 +83,6 @@ __FBSDID("$FreeBSD$");
#include <machine/undefined.h>
#include <machine/vmparam.h>
#include <arm/include/physmem.h>
#ifdef VFP
#include <machine/vfp.h>
#endif
@ -855,7 +854,7 @@ exclude_efi_map_entry(struct efi_md *p)
*/
break;
default:
arm_physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
EXFLAG_NOALLOC);
}
}
@ -886,7 +885,7 @@ add_efi_map_entry(struct efi_md *p)
/*
* We're allowed to use any entry with these types.
*/
arm_physmem_hardware_region(p->md_phys,
physmem_hardware_region(p->md_phys,
p->md_pages * PAGE_SIZE);
break;
}
@ -1113,10 +1112,10 @@ initarm(struct arm64_bootparams *abp)
if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
NULL) != 0)
panic("Cannot get physical memory regions");
arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
physmem_hardware_regions(mem_regions, mem_regions_sz);
}
if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
physmem_exclude_regions(mem_regions, mem_regions_sz,
EXFLAG_NODUMP | EXFLAG_NOALLOC);
#endif
@ -1124,7 +1123,7 @@ initarm(struct arm64_bootparams *abp)
efifb = (struct efi_fb *)preload_search_info(kmdp,
MODINFO_METADATA | MODINFOMD_EFI_FB);
if (efifb != NULL)
arm_physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
EXFLAG_NOALLOC);
/* Set the pcpu data, this is needed by pmap_bootstrap */
@ -1153,7 +1152,7 @@ initarm(struct arm64_bootparams *abp)
/* Exclude entries neexed in teh DMAP region, but not phys_avail */
if (efihdr != NULL)
exclude_efi_map_entries(efihdr);
arm_physmem_init_kernel_globals();
physmem_init_kernel_globals();
devmap_bootstrap(0, NULL);
@ -1182,7 +1181,7 @@ initarm(struct arm64_bootparams *abp)
if (boothowto & RB_VERBOSE) {
print_efi_map_entries(efihdr);
arm_physmem_print_tables();
physmem_print_tables();
}
early_boot = 0;

View File

@ -119,6 +119,7 @@ __FBSDID("$FreeBSD$");
#include <sys/mman.h>
#include <sys/msgbuf.h>
#include <sys/mutex.h>
#include <sys/physmem.h>
#include <sys/proc.h>
#include <sys/rwlock.h>
#include <sys/sbuf.h>
@ -148,8 +149,6 @@ __FBSDID("$FreeBSD$");
#include <machine/md_var.h>
#include <machine/pcb.h>
#include <arm/include/physmem.h>
#define PMAP_ASSERT_STAGE1(pmap) MPASS((pmap)->pm_stage == PM_STAGE1)
#define NL0PG (PAGE_SIZE/(sizeof (pd_entry_t)))
@ -861,7 +860,7 @@ pmap_bootstrap(vm_offset_t l0pt, vm_offset_t l1pt, vm_paddr_t kernstart,
/* Assume the address we were loaded to is a valid physical address */
min_pa = KERNBASE - kern_delta;
physmap_idx = arm_physmem_avail(physmap, nitems(physmap));
physmap_idx = physmem_avail(physmap, nitems(physmap));
physmap_idx /= 2;
/*
@ -942,7 +941,7 @@ pmap_bootstrap(vm_offset_t l0pt, vm_offset_t l1pt, vm_paddr_t kernstart,
pa = pmap_early_vtophys(l1pt, freemempos);
arm_physmem_exclude_region(start_pa, pa - start_pa, EXFLAG_NOALLOC);
physmem_exclude_region(start_pa, pa - start_pa, EXFLAG_NOALLOC);
cpu_tlb_flushID();
}

View File

@ -58,7 +58,6 @@ arm/arm/mp_machdep.c optional smp
arm/arm/mpcore_timer.c optional mpcore_timer
arm/arm/nexus.c standard
arm/arm/ofw_machdep.c optional fdt
arm/arm/physmem.c standard
arm/arm/pl190.c optional pl190
arm/arm/pl310.c optional pl310
arm/arm/platform.c optional platform
@ -116,6 +115,7 @@ kern/msi_if.m optional intrng
kern/pic_if.m optional intrng
kern/subr_busdma_bufalloc.c standard
kern/subr_devmap.c standard
kern/subr_physmem.c standard
kern/subr_sfbuf.c standard
libkern/arm/aeabi_unwind.c standard
libkern/arm/divsi3.S standard

View File

@ -85,7 +85,6 @@ arm/arm/gic.c standard
arm/arm/gic_acpi.c optional acpi
arm/arm/gic_fdt.c optional fdt
arm/arm/pmu.c standard
arm/arm/physmem.c standard
arm/broadcom/bcm2835/bcm2835_audio.c optional sound vchiq fdt \
compile-with "${NORMAL_C} -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000 -I$S/contrib/vchiq"
arm/broadcom/bcm2835/bcm2835_bsc.c optional bcm2835_bsc fdt
@ -291,6 +290,7 @@ kern/msi_if.m optional intrng
kern/pic_if.m optional intrng
kern/subr_devmap.c standard
kern/subr_intr.c optional intrng
kern/subr_physmem.c standard
libkern/bcmp.c standard
libkern/memcmp.c standard \
compile-with "${NORMAL_C:N-fsanitize*}"

View File

@ -38,12 +38,12 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/physmem.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_page.h>
#include <vm/vm_phys.h>
#include <machine/md_var.h>
#include <arm/include/physmem.h>
/*
* These structures are used internally to keep track of regions of physical
@ -62,10 +62,8 @@ __FBSDID("$FreeBSD$");
#if defined(__arm__)
#define MAX_PHYS_ADDR 0xFFFFFFFFull
#define pm_btop(x) arm32_btop(x)
#elif defined(__aarch64__)
#elif defined(__aarch64__) || defined(__riscv)
#define MAX_PHYS_ADDR 0xFFFFFFFFFFFFFFFFull
#define pm_btop(x) arm64_btop(x)
#endif
struct region {
@ -87,9 +85,6 @@ static size_t excnt;
long realmem;
long Maxmem;
/* The address at which the kernel was loaded. Set early in initarm(). */
vm_paddr_t arm_physmem_kernaddr;
/*
* Print the contents of the physical and excluded region tables using the
* provided printf-like output function (which will be either printf or
@ -136,7 +131,7 @@ physmem_dump_tables(int (*prfunc)(const char *, ...))
* Print the contents of the static mapping table. Used for bootverbose.
*/
void
arm_physmem_print_tables(void)
physmem_print_tables(void)
{
physmem_dump_tables(printf);
@ -165,7 +160,7 @@ regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail,
for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) {
start = hwp->addr;
end = hwp->size + start;
totalmem += pm_btop((vm_offset_t)(end - start));
totalmem += atop((vm_offset_t)(end - start));
for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) {
/*
* If the excluded region does not match given flags,
@ -213,8 +208,7 @@ regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail,
avail[acnt++] = (vm_paddr_t)start;
avail[acnt++] = (vm_paddr_t)xstart;
}
availmem +=
pm_btop((vm_offset_t)(xstart - start));
availmem += atop((vm_offset_t)(xstart - start));
start = xend;
continue;
}
@ -239,7 +233,7 @@ regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail,
avail[acnt++] = (vm_paddr_t)start;
avail[acnt++] = (vm_paddr_t)end;
}
availmem += pm_btop((vm_offset_t)(end - start));
availmem += atop((vm_offset_t)(end - start));
}
if (acnt >= maxavail)
panic("Not enough space in the dump/phys_avail arrays");
@ -293,7 +287,7 @@ insert_region(struct region *regions, size_t rcnt, vm_paddr_t addr,
* Add a hardware memory region.
*/
void
arm_physmem_hardware_region(uint64_t pa, uint64_t sz)
physmem_hardware_region(uint64_t pa, uint64_t sz)
{
vm_offset_t adj;
@ -345,7 +339,7 @@ arm_physmem_hardware_region(uint64_t pa, uint64_t sz)
* Add an exclusion region.
*/
void
arm_physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
{
vm_offset_t adj;
@ -364,7 +358,7 @@ arm_physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
}
size_t
arm_physmem_avail(vm_paddr_t *avail, size_t maxavail)
physmem_avail(vm_paddr_t *avail, size_t maxavail)
{
return (regions_to_avail(avail, EXFLAG_NOALLOC, maxavail, NULL, NULL));
@ -380,7 +374,7 @@ arm_physmem_avail(vm_paddr_t *avail, size_t maxavail)
* last page of physical memory in the system.
*/
void
arm_physmem_init_kernel_globals(void)
physmem_init_kernel_globals(void)
{
size_t nextidx;
@ -403,4 +397,3 @@ DB_SHOW_COMMAND(physmem, db_show_physmem)
}
#endif /* DDB */

View File

@ -28,13 +28,8 @@
* $FreeBSD$
*/
#ifndef _MACHINE_PHYSMEM_H_
#define _MACHINE_PHYSMEM_H_
/*
* The physical address at which the kernel was loaded.
*/
extern vm_paddr_t arm_physmem_kernaddr;
#ifndef _SYS_PHYSMEM_H_
#define _SYS_PHYSMEM_H_
/*
* Routines to help configure physical ram.
@ -46,7 +41,7 @@ extern vm_paddr_t arm_physmem_kernaddr;
*
* After all early kernel init is done and it's time to configure all
* remainining non-excluded physical ram for use by other parts of the kernel,
* arm_physmem_init_kernel_globals() processes the hardware regions and
* physmem_init_kernel_globals() processes the hardware regions and
* exclusion regions to generate the global dump_avail and phys_avail arrays
* that communicate physical ram configuration to other parts of the kernel.
*/
@ -54,11 +49,11 @@ extern vm_paddr_t arm_physmem_kernaddr;
#define EXFLAG_NODUMP 0x01
#define EXFLAG_NOALLOC 0x02
void arm_physmem_hardware_region(uint64_t pa, uint64_t sz);
void arm_physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t flags);
size_t arm_physmem_avail(vm_paddr_t *avail, size_t maxavail);
void arm_physmem_init_kernel_globals(void);
void arm_physmem_print_tables(void);
void physmem_hardware_region(uint64_t pa, uint64_t sz);
void physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t flags);
size_t physmem_avail(vm_paddr_t *avail, size_t maxavail);
void physmem_init_kernel_globals(void);
void physmem_print_tables(void);
/*
* Convenience routines for FDT.
@ -69,20 +64,20 @@ void arm_physmem_print_tables(void);
#include <machine/ofw_machdep.h>
static inline void
arm_physmem_hardware_regions(struct mem_region * mrptr, int mrcount)
physmem_hardware_regions(struct mem_region * mrptr, int mrcount)
{
while (mrcount--) {
arm_physmem_hardware_region(mrptr->mr_start, mrptr->mr_size);
physmem_hardware_region(mrptr->mr_start, mrptr->mr_size);
++mrptr;
}
}
static inline void
arm_physmem_exclude_regions(struct mem_region * mrptr, int mrcount,
physmem_exclude_regions(struct mem_region * mrptr, int mrcount,
uint32_t exflags)
{
while (mrcount--) {
arm_physmem_exclude_region(mrptr->mr_start, mrptr->mr_size,
physmem_exclude_region(mrptr->mr_start, mrptr->mr_size,
exflags);
++mrptr;
}
@ -90,5 +85,4 @@ arm_physmem_exclude_regions(struct mem_region * mrptr, int mrcount,
#endif /* FDT */
#endif
#endif /* !_SYS_PHYSMEM_H_ */