Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/arch/evbarm/fdt/fdt_machdep.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/arch/evbarm/fdt/fdt_machdep.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.13 retrieving revision 1.44 diff -u -p -r1.13 -r1.44 --- src/sys/arch/evbarm/fdt/fdt_machdep.c 2017/08/24 11:33:28 1.13 +++ src/sys/arch/evbarm/fdt/fdt_machdep.c 2018/10/18 09:01:54 1.44 @@ -1,4 +1,4 @@ -/* $NetBSD: fdt_machdep.c,v 1.13 2017/08/24 11:33:28 jmcneill Exp $ */ +/* $NetBSD: fdt_machdep.c,v 1.44 2018/10/18 09:01:54 skrll Exp $ */ /*- * Copyright (c) 2015-2017 Jared McNeill @@ -27,15 +27,19 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.13 2017/08/24 11:33:28 jmcneill Exp $"); +__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.44 2018/10/18 09:01:54 skrll Exp $"); #include "opt_machdep.h" +#include "opt_bootconfig.h" #include "opt_ddb.h" #include "opt_md.h" #include "opt_arm_debug.h" #include "opt_multiprocessor.h" #include "opt_cpuoptions.h" +#include "ukbd.h" +#include "wsdisplay.h" + #include #include #include @@ -51,7 +55,14 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep. #include #include #include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -62,15 +73,22 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep. #include #include -#include -#include +#include #include +#include #include #include +#if NUKBD > 0 +#include +#endif +#if NWSDISPLAY > 0 +#include +#endif + #ifdef MEMORY_DISK_DYNAMIC #include #endif @@ -82,7 +100,10 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep. BootConfig bootconfig; char bootargs[FDT_MAX_BOOT_STRING] = ""; char *boot_args = NULL; -u_int uboot_args[4] = { 0 }; /* filled in by xxx_start.S (not in bss) */ + +/* filled in before cleaning bss. keep in .data */ +u_long uboot_args[4] __attribute__((__section__(".data"))); +const uint8_t *fdt_addr_r __attribute__((__section__(".data"))); static char fdt_memory_ext_storage[EXTENT_FIXED_STORAGE_SIZE(DRAM_BLOCKS)]; static struct extent *fdt_memory_ext; @@ -91,7 +112,7 @@ static uint64_t initrd_start, initrd_end #include #include -#define FDT_BUF_SIZE (128*1024) +#define FDT_BUF_SIZE (512*1024) static uint8_t fdt_data[FDT_BUF_SIZE]; extern char KERNEL_BASE_phys[]; @@ -99,106 +120,111 @@ extern char KERNEL_BASE_phys[]; static void fdt_update_stdout_path(void); static void fdt_device_register(device_t, void *); +static void fdt_device_register_post_config(device_t, void *); +static void fdt_cpu_rootconf(void); static void fdt_reset(void); static void fdt_powerdown(void); -#ifdef PMAP_NEED_ALLOC_POOLPAGE -static struct boot_physmem bp_lowgig = { - .bp_pages = (KERNEL_VM_BASE - KERNEL_BASE) / NBPG, - .bp_freelist = VM_FREELIST_ISADMA, - .bp_flags = 0 +static dev_type_cnputc(earlyconsputc); +static dev_type_cngetc(earlyconsgetc); + +static struct consdev earlycons = { + .cn_putc = earlyconsputc, + .cn_getc = earlyconsgetc, + .cn_pollc = nullcnpollc, }; -#endif -#ifdef VERBOSE_INIT_ARM static void fdt_putchar(char c) { +#ifdef EARLYCONS const struct arm_platform *plat = arm_fdt_platform(); - if (plat && plat->early_putchar) - plat->early_putchar(c); + if (plat && plat->ap_early_putchar) { + plat->ap_early_putchar(c); + } else { + uartputc(c); + } +#endif } static void -fdt_putstr(const char *s) +earlyconsputc(dev_t dev, int c) { - for (const char *p = s; *p; p++) - fdt_putchar(*p); + fdt_putchar(c); } -static void -fdt_printn(u_int n, int base) +static int +earlyconsgetc(dev_t dev) { - char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */]; - - p = buf; - do { - *p++ = hexdigits[n % base]; - } while (n /= base); - - do { - fdt_putchar(*--p); - } while (p > buf); + return 0; /* XXX */ } -#define DPRINTF(...) printf(__VA_ARGS__) -#define DPRINT(x) fdt_putstr(x) -#define DPRINTN(x,b) fdt_printn((x), (b)) + +#ifdef VERBOSE_INIT_ARM +#define VPRINTF(...) printf(__VA_ARGS__) #else -#define DPRINTF(...) -#define DPRINT(x) -#define DPRINTN(x,b) +#define VPRINTF(...) __nothing #endif /* - * Get the first physically contiguous region of memory. + * ARM: Get the first physically contiguous region of memory. + * ARM64: Get all of physical memory, including holes. */ static void -fdt_get_memory(uint64_t *paddr, uint64_t *psize) +fdt_get_memory(uint64_t *pstart, uint64_t *pend) { const int memory = OF_finddevice("/memory"); uint64_t cur_addr, cur_size; int index; /* Assume the first entry is the start of memory */ - if (fdtbus_get_reg64(memory, 0, paddr, psize) != 0) + if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0) panic("Cannot determine memory size"); - DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", - 0, *paddr, *psize); + *pstart = cur_addr; + *pend = cur_addr + cur_size; + + VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", + 0, *pstart, *pend - *pstart); - /* If subsequent entries follow the previous one, append them. */ for (index = 1; fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0; index++) { - DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", + VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", index, cur_addr, cur_size); - if (*paddr + *psize == cur_addr) - *psize += cur_size; + +#ifdef __aarch64__ + if (cur_addr + cur_size > *pend) + *pend = cur_addr + cur_size; +#else + /* If subsequent entries follow the previous, append them. */ + if (*pend == cur_addr) + *pend = cur_addr + cur_size; +#endif } } -static void +void fdt_add_reserved_memory_range(uint64_t addr, uint64_t size) { - int error; - - addr = trunc_page(addr); - size = round_page(size); + uint64_t start = addr; + uint64_t end = addr + size; - error = extent_free(fdt_memory_ext, addr, size, EX_NOWAIT); + int error = extent_free(fdt_memory_ext, start, + end - start, EX_NOWAIT); if (error != 0) - printf("MEM ERROR: res %llx-%llx failed: %d\n", - addr, addr + size, error); + printf("MEM ERROR: res %" PRIx64 "-%" PRIx64 " failed: %d\n", + start, end, error); else - DPRINTF("MEM: res %llx-%llx\n", addr, addr + size); + VPRINTF("MEM: res %" PRIx64 "-%" PRIx64 "\n", start, end); } /* * Exclude memory ranges from memory config from the device tree */ static void -fdt_add_reserved_memory(uint64_t max_addr) +fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr) { + uint64_t lstart = 0, lend = 0; uint64_t addr, size; int index, error; @@ -206,13 +232,27 @@ fdt_add_reserved_memory(uint64_t max_add for (index = 0; index <= num; index++) { error = fdt_get_mem_rsv(fdtbus_get_data(), index, &addr, &size); - if (error != 0 || size == 0) + if (error != 0) + continue; + if (lstart <= addr && addr <= lend) { + size -= (lend - addr); + addr = lend; + } + if (size == 0) + continue; + if (addr + size <= min_addr) continue; if (addr >= max_addr) continue; + if (addr < min_addr) { + size -= (min_addr - addr); + addr = min_addr; + } if (addr + size > max_addr) size = max_addr - addr; fdt_add_reserved_memory_range(addr, size); + lstart = addr; + lend = addr + size; } } @@ -220,44 +260,43 @@ fdt_add_reserved_memory(uint64_t max_add * Define usable memory regions. */ static void -fdt_build_bootconfig(uint64_t mem_addr, uint64_t mem_size) +fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end) { const int memory = OF_finddevice("/memory"); - const uint64_t max_addr = mem_addr + mem_size; BootConfig *bc = &bootconfig; struct extent_region *er; uint64_t addr, size; int index, error; - - fdt_memory_ext = extent_create("FDT Memory", mem_addr, max_addr, + + fdt_memory_ext = extent_create("FDT Memory", mem_start, mem_end, fdt_memory_ext_storage, sizeof(fdt_memory_ext_storage), EX_EARLY); for (index = 0; fdtbus_get_reg64(memory, index, &addr, &size) == 0; index++) { - if (addr >= max_addr || size == 0) + if (addr >= mem_end || size == 0) continue; - if (addr + size > max_addr) - size = max_addr - addr; + if (addr + size > mem_end) + size = mem_end - addr; error = extent_alloc_region(fdt_memory_ext, addr, size, EX_NOWAIT); if (error != 0) - printf("MEM ERROR: add %llx-%llx failed: %d\n", - addr, size, error); - DPRINTF("MEM: add %llx-%llx\n", addr, size); + printf("MEM ERROR: add %" PRIx64 "-%" PRIx64 " failed: %d\n", + addr, addr + size, error); + VPRINTF("MEM: add %" PRIx64 "-%" PRIx64 "\n", addr, addr + size); } - fdt_add_reserved_memory(max_addr); + fdt_add_reserved_memory(mem_start, mem_end); const uint64_t initrd_size = initrd_end - initrd_start; if (initrd_size > 0) fdt_add_reserved_memory_range(initrd_start, initrd_size); - DPRINTF("Usable memory:\n"); + VPRINTF("Usable memory:\n"); bc->dramblocks = 0; LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) { - DPRINTF(" %lx - %lx\n", er->er_start, er->er_end); + VPRINTF(" %lx - %lx\n", er->er_start, er->er_end); bc->dram[bc->dramblocks].address = er->er_start; bc->dram[bc->dramblocks].pages = (er->er_end - er->er_start) / PAGE_SIZE; @@ -330,17 +369,24 @@ fdt_setup_initrd(void) #endif } +u_int initarm(void *arg); + u_int initarm(void *arg) { const struct arm_platform *plat; - uint64_t memory_addr, memory_size; + uint64_t memory_start, memory_end; + + /* set temporally to work printf()/panic() even before consinit() */ + cn_tab = &earlycons; /* Load FDT */ - const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2]; int error = fdt_check_header(fdt_addr_r); if (error == 0) { - error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data)); + /* If the DTB is too big, try to pack it in place first. */ + if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data)) + (void)fdt_pack(__UNCONST(fdt_addr_r)); + error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data)); if (error != 0) panic("fdt_move failed: %s", fdt_strerror(error)); fdtbus_set_data(fdt_data); @@ -354,108 +400,146 @@ initarm(void *arg) panic("Kernel does not support this device"); /* Early console may be available, announce ourselves. */ - DPRINT("FDT<"); - DPRINTN((uintptr_t)fdt_addr_r, 16); - DPRINT(">"); + VPRINTF("FDT<%p>\n", fdt_addr_r); const int chosen = OF_finddevice("/chosen"); if (chosen >= 0) OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs)); boot_args = bootargs; - DPRINT(" devmap"); - pmap_devmap_register(plat->devmap()); - - DPRINT(" bootstrap"); - plat->bootstrap(); - /* Heads up ... Setup the CPU / MMU / TLB functions. */ - DPRINT(" cpufunc"); + VPRINTF("cpufunc\n"); if (set_cpufuncs()) panic("cpu not recognized!"); /* + * Memory is still identity/flat mapped this point so using ttbr for + * l1pt VA is fine + */ + + VPRINTF("devmap\n"); + extern char ARM_BOOTSTRAP_LxPT[]; + pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap()); + + VPRINTF("bootstrap\n"); + plat->ap_bootstrap(); + + /* * If stdout-path is specified on the command line, override the * value in /chosen/stdout-path before initializing console. */ + VPRINTF("stdout\n"); fdt_update_stdout_path(); - DPRINT(" consinit"); - consinit(); + /* + * Done making changes to the FDT. + */ + fdt_pack(fdt_data); - DPRINTF(" ok\n"); + VPRINTF("consinit "); + consinit(); + VPRINTF("ok\n"); - DPRINTF("uboot: args %#x, %#x, %#x, %#x\n", + VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n", uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]); cpu_reset_address = fdt_reset; cpu_powerdown_address = fdt_powerdown; evbarm_device_register = fdt_device_register; + evbarm_device_register_post_config = fdt_device_register_post_config; + evbarm_cpu_rootconf = fdt_cpu_rootconf; /* Talk to the user */ - DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n"); + VPRINTF("\nNetBSD/evbarm (fdt) booting ...\n"); #ifdef BOOT_ARGS char mi_bootargs[] = BOOT_ARGS; parse_mi_bootargs(mi_bootargs); #endif - DPRINTF("KERNEL_BASE=0x%x, " - "KERNEL_VM_BASE=0x%x, " - "KERNEL_VM_BASE - KERNEL_BASE=0x%x, " - "KERNEL_BASE_VOFFSET=0x%x\n", - KERNEL_BASE, - KERNEL_VM_BASE, - KERNEL_VM_BASE - KERNEL_BASE, - KERNEL_BASE_VOFFSET); - - fdt_get_memory(&memory_addr, &memory_size); + fdt_get_memory(&memory_start, &memory_end); #if !defined(_LP64) /* Cannot map memory above 4GB */ - if (memory_addr + memory_size >= 0x100000000) - memory_size = 0x100000000 - memory_addr - PAGE_SIZE; -#endif + if (memory_end >= 0x100000000ULL) + memory_end = 0x100000000ULL - PAGE_SIZE; -#ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS - const bool mapallmem_p = true; -#ifndef PMAP_NEED_ALLOC_POOLPAGE - if (memory_size > KERNEL_VM_BASE - KERNEL_BASE) { - DPRINTF("%s: dropping RAM size from %luMB to %uMB\n", - __func__, (unsigned long) (memory_size >> 20), - (KERNEL_VM_BASE - KERNEL_BASE) >> 20); - memory_size = KERNEL_VM_BASE - KERNEL_BASE; - } -#endif -#else - const bool mapallmem_p = false; #endif + uint64_t memory_size = memory_end - memory_start; + + VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %" + PRIx64 ")\n", __func__, memory_start, memory_end, memory_size); /* Parse ramdisk info */ fdt_probe_initrd(&initrd_start, &initrd_end); - /* Populate bootconfig structure for the benefit of pmap.c. */ - fdt_build_bootconfig(memory_addr, memory_size); + /* + * Populate bootconfig structure for the benefit of + * dodumpsys + */ + VPRINTF("%s: fdt_build_bootconfig\n", __func__); + fdt_build_bootconfig(memory_start, memory_end); - arm32_bootmem_init(bootconfig.dram[0].address, memory_size, - KERNEL_BASE_PHYS); - arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0, - plat->devmap(), mapallmem_p); + /* Perform PT build and VM init */ + cpu_kernel_vm_init(memory_start, memory_size); - DPRINTF("bootargs: %s\n", bootargs); + VPRINTF("bootargs: %s\n", bootargs); parse_mi_bootargs(boot_args); + #define MAX_PHYSMEM 64 + static struct boot_physmem fdt_physmem[MAX_PHYSMEM]; + int nfdt_physmem = 0; + struct extent_region *er; + + VPRINTF("Memory regions :\n"); + LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) { + VPRINTF(" %lx - %lx\n", er->er_start, er->er_end); + struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++]; + + KASSERT(nfdt_physmem <= MAX_PHYSMEM); + + bp->bp_start = atop(round_page(er->er_start)); + bp->bp_pages = atop(trunc_page(er->er_end + 1)) - bp->bp_start; + bp->bp_freelist = VM_FREELIST_DEFAULT; + +#ifdef _LP64 + if (er->er_end > 0x100000000) + bp->bp_freelist = VM_FREELIST_HIGHMEM; +#endif + #ifdef PMAP_NEED_ALLOC_POOLPAGE - bp_lowgig.bp_start = memory_addr / NBPG; - if (atop(ram_size) > bp_lowgig.bp_pages) { - arm_poolpage_vmfreelist = bp_lowgig.bp_freelist; - return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, - &bp_lowgig, 1); - } + if (atop(memory_size) > bp->bp_pages) { + arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP; + bp->bp_freelist = VM_FREELIST_DIRECTMAP; + } #endif + } - return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0); + u_int sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem, + nfdt_physmem); + + VPRINTF("mpstart\n"); + if (plat->ap_mpstart) + plat->ap_mpstart(); + + /* + * Now we have APs started the pages used for stacks and L1PT can + * be given to uvm + */ + extern char __start__init_memory[], __stop__init_memory[]; + if (__start__init_memory != __stop__init_memory) { + const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory); + const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory); + const paddr_t spg = atop(spa); + const paddr_t epg = atop(epa); + + uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT); + + VPRINTF(" start %08lx end %08lx", ptoa(spa), ptoa(epa)); + } + + return sp; } static void @@ -496,11 +580,11 @@ consinit(void) if (initialized || cons == NULL) return; - plat->init_attach_args(&faa); + plat->ap_init_attach_args(&faa); faa.faa_phandle = fdtbus_get_stdout_phandle(); - if (plat->uart_freq != NULL) - uart_freq = plat->uart_freq(); + if (plat->ap_uart_freq != NULL) + uart_freq = plat->ap_uart_freq(); cons->consinit(&faa, uart_freq); @@ -512,7 +596,64 @@ delay(u_int us) { const struct arm_platform *plat = arm_fdt_platform(); - plat->delay(us); + plat->ap_delay(us); +} + +static void +fdt_detect_root_device(device_t dev) +{ + struct mbr_sector mbr; + uint8_t buf[DEV_BSIZE]; + uint8_t hash[16]; + const uint8_t *rhash; + char rootarg[32]; + struct vnode *vp; + MD5_CTX md5ctx; + int error, len; + size_t resid; + u_int part; + + const int chosen = OF_finddevice("/chosen"); + if (chosen < 0) + return; + + if (of_hasprop(chosen, "netbsd,mbr") && + of_hasprop(chosen, "netbsd,partition")) { + + /* + * The bootloader has passed in a partition index and MD5 hash + * of the MBR sector. Read the MBR of this device, calculate the + * hash, and compare it with the value passed in. + */ + rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len); + if (rhash == NULL || len != 16) + return; + of_getprop_uint32(chosen, "netbsd,partition", &part); + if (part >= MAXPARTITIONS) + return; + + vp = opendisk(dev); + if (!vp) + return; + error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE, + 0, NOCRED, &resid, NULL); + VOP_CLOSE(vp, FREAD, NOCRED); + vput(vp); + + if (error != 0) + return; + + memcpy(&mbr, buf, sizeof(mbr)); + MD5Init(&md5ctx); + MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr)); + MD5Final(hash, &md5ctx); + + if (memcmp(rhash, hash, 16) != 0) + return; + + snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a'); + strcat(boot_args, rootarg); + } } static void @@ -523,8 +664,40 @@ fdt_device_register(device_t self, void if (device_is_a(self, "armfdt")) fdt_setup_initrd(); - if (plat && plat->device_register) - plat->device_register(self, aux); + if (plat && plat->ap_device_register) + plat->ap_device_register(self, aux); +} + +static void +fdt_device_register_post_config(device_t self, void *aux) +{ +#if NUKBD > 0 && NWSDISPLAY > 0 + if (device_is_a(self, "wsdisplay")) { + struct wsdisplay_softc *sc = device_private(self); + if (wsdisplay_isconsole(sc)) + ukbd_cnattach(); + } +#endif +} + +static void +fdt_cpu_rootconf(void) +{ + device_t dev; + deviter_t di; + char *ptr; + + for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) { + if (device_class(dev) != DV_DISK) + continue; + + if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0) + break; + + if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd")) + fdt_detect_root_device(dev); + } + deviter_release(&di); } static void @@ -534,8 +707,8 @@ fdt_reset(void) fdtbus_power_reset(); - if (plat && plat->reset) - plat->reset(); + if (plat && plat->ap_reset) + plat->ap_reset(); } static void