Index: sys/conf/files.riscv =================================================================== --- sys/conf/files.riscv +++ sys/conf/files.riscv @@ -20,6 +20,7 @@ kern/subr_devmap.c standard kern/subr_dummy_vdso_tc.c standard kern/subr_intr.c standard +kern/subr_physmem.c standard libkern/bcmp.c standard libkern/bcopy.c standard libkern/ffs.c standard Index: sys/riscv/include/machdep.h =================================================================== --- sys/riscv/include/machdep.h +++ sys/riscv/include/machdep.h @@ -44,9 +44,6 @@ vm_offset_t dtbp_virt; /* Device tree blob virtual addr */ }; -extern vm_paddr_t physmap[]; -extern u_int physmap_idx; - vm_offset_t fake_preload_metadata(struct riscv_bootparams *rbp); void initriscv(struct riscv_bootparams *); Index: sys/riscv/riscv/machdep.c =================================================================== --- sys/riscv/riscv/machdep.c +++ sys/riscv/riscv/machdep.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -108,14 +109,9 @@ int early_boot = 1; int cold = 1; -long realmem = 0; -long Maxmem = 0; #define DTB_SIZE_MAX (1024 * 1024) -vm_paddr_t physmap[PHYS_AVAIL_ENTRIES]; -u_int physmap_idx; - struct kva_md_info kmi; int64_t dcache_line_size; /* The minimum D cache line size */ @@ -673,74 +669,6 @@ pcpup->pc_curpcb = thread0.td_pcb; } -static int -add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap, - u_int *physmap_idxp) -{ - u_int i, insert_idx, _physmap_idx; - - _physmap_idx = *physmap_idxp; - - if (length == 0) - return (1); - - /* - * Find insertion point while checking for overlap. Start off by - * assuming the new entry will be added to the end. - */ - insert_idx = _physmap_idx; - for (i = 0; i <= _physmap_idx; i += 2) { - if (base < physmap[i + 1]) { - if (base + length <= physmap[i]) { - insert_idx = i; - break; - } - if (boothowto & RB_VERBOSE) - printf( - "Overlapping memory regions, ignoring second region\n"); - return (1); - } - } - - /* See if we can prepend to the next entry. */ - if (insert_idx <= _physmap_idx && - base + length == physmap[insert_idx]) { - physmap[insert_idx] = base; - return (1); - } - - /* See if we can append to the previous entry. */ - if (insert_idx > 0 && base == physmap[insert_idx - 1]) { - physmap[insert_idx - 1] += length; - return (1); - } - - _physmap_idx += 2; - *physmap_idxp = _physmap_idx; - if (_physmap_idx == PHYS_AVAIL_ENTRIES) { - printf( - "Too many segments in the physical address map, giving up\n"); - return (0); - } - - /* - * Move the last 'N' entries down to make room for the new - * entry if needed. - */ - for (i = _physmap_idx; i > insert_idx; i -= 2) { - physmap[i] = physmap[i - 2]; - physmap[i + 1] = physmap[i - 1]; - } - - /* Insert the new entry. */ - physmap[insert_idx] = base; - physmap[insert_idx + 1] = base + length; - - printf("physmap[%d] = 0x%016lx\n", insert_idx, base); - printf("physmap[%d] = 0x%016lx\n", insert_idx + 1, base + length); - return (1); -} - #ifdef FDT static void try_load_dtb(caddr_t kmdp) @@ -864,7 +792,6 @@ vm_offset_t lastaddr; vm_size_t kernlen; caddr_t kmdp; - int i; TSRAW(&thread0, TS_ENTER, __func__, NULL); @@ -896,20 +823,11 @@ #ifdef FDT try_load_dtb(kmdp); -#endif - - /* Load the physical memory ranges */ - physmap_idx = 0; -#ifdef FDT /* Grab physical memory regions information from device tree. */ if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) panic("Cannot get physical memory regions"); - - for (i = 0; i < mem_regions_sz; i++) { - add_physmap_entry(mem_regions[i].mr_start, - mem_regions[i].mr_size, physmap, &physmap_idx); - } + physmem_hardware_regions(mem_regions, mem_regions_sz); #endif /* Do basic tuning, hz etc */ @@ -921,6 +839,8 @@ kernlen = (lastaddr - KERNBASE); pmap_bootstrap(rvbp->kern_l1pt, mem_regions[0].mr_start, kernlen); + physmem_init_kernel_globals(); + /* Establish static device mappings */ devmap_bootstrap(0, NULL); @@ -933,6 +853,9 @@ init_param2(physmem); kdb_init(); + if (boothowto & RB_VERBOSE) + physmem_print_tables(); + early_boot = 0; TSEXIT(); Index: sys/riscv/riscv/pmap.c =================================================================== --- sys/riscv/riscv/pmap.c +++ sys/riscv/riscv/pmap.c @@ -129,6 +129,7 @@ #include #include #include +#include #include #include #include @@ -231,6 +232,9 @@ CTASSERT((DMAP_MIN_ADDRESS & ~L1_OFFSET) == DMAP_MIN_ADDRESS); CTASSERT((DMAP_MAX_ADDRESS & ~L1_OFFSET) == DMAP_MAX_ADDRESS); +vm_paddr_t physmap[PHYS_AVAIL_ENTRIES]; +u_int physmap_idx; + static struct rwlock_padalign pvh_global_lock; static struct mtx_padalign allpmaps_lock; @@ -554,10 +558,10 @@ void pmap_bootstrap(vm_offset_t l1pt, vm_paddr_t kernstart, vm_size_t kernlen) { - u_int l1_slot, l2_slot, avail_slot, map_slot; + u_int l1_slot, l2_slot; vm_offset_t freemempos; vm_offset_t dpcpu, msgbufpv; - vm_paddr_t end, max_pa, min_pa, pa, start; + vm_paddr_t max_pa, min_pa, pa; pt_entry_t *l2p; int i; @@ -576,6 +580,9 @@ /* Assume the address we were loaded to is a valid physical address. */ min_pa = max_pa = kernstart; + physmap_idx = physmem_avail(physmap, nitems(physmap)); + physmap_idx /= 2; + /* * Find the minimum physical address. physmap is sorted, * but may contain empty ranges. @@ -641,46 +648,7 @@ pa = pmap_early_vtophys(l1pt, freemempos); - /* Initialize phys_avail and dump_avail. */ - for (avail_slot = map_slot = physmem = 0; map_slot < physmap_idx * 2; - map_slot += 2) { - start = physmap[map_slot]; - end = physmap[map_slot + 1]; - - if (start == end) - continue; - dump_avail[map_slot] = start; - dump_avail[map_slot + 1] = end; - realmem += atop((vm_offset_t)(end - start)); - - if (start >= kernstart && end <= pa) - continue; - - if (start < kernstart && end > kernstart) - end = kernstart; - else if (start < pa && end > pa) - start = pa; - phys_avail[avail_slot] = start; - phys_avail[avail_slot + 1] = end; - physmem += (end - start) >> PAGE_SHIFT; - avail_slot += 2; - - if (end != physmap[map_slot + 1] && end > pa) { - phys_avail[avail_slot] = pa; - phys_avail[avail_slot + 1] = physmap[map_slot + 1]; - physmem += (physmap[map_slot + 1] - pa) >> PAGE_SHIFT; - avail_slot += 2; - } - } - phys_avail[avail_slot] = 0; - phys_avail[avail_slot + 1] = 0; - - /* - * Maxmem isn't the "maximum memory", it's one larger than the - * highest page of the physical address space. It should be - * called something like "Maxphyspage". - */ - Maxmem = atop(phys_avail[avail_slot - 1]); + physmem_exclude_region(kernstart, pa - kernstart, EXFLAG_NOALLOC); } /*