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.58 retrieving revision 1.85 diff -u -p -r1.58 -r1.85 --- src/sys/arch/evbarm/fdt/fdt_machdep.c 2019/01/31 13:26:21 1.58 +++ src/sys/arch/evbarm/fdt/fdt_machdep.c 2020/12/18 07:34:31 1.85 @@ -1,4 +1,4 @@ -/* $NetBSD: fdt_machdep.c,v 1.58 2019/01/31 13:26:21 skrll Exp $ */ +/* $NetBSD: fdt_machdep.c,v 1.85 2020/12/18 07:34:31 skrll Exp $ */ /*- * Copyright (c) 2015-2017 Jared McNeill @@ -27,17 +27,18 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.58 2019/01/31 13:26:21 skrll Exp $"); +__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.85 2020/12/18 07:34:31 skrll Exp $"); -#include "opt_machdep.h" +#include "opt_arm_debug.h" #include "opt_bootconfig.h" +#include "opt_cpuoptions.h" #include "opt_ddb.h" +#include "opt_efi.h" +#include "opt_machdep.h" #include "opt_md.h" -#include "opt_arm_debug.h" #include "opt_multiprocessor.h" -#include "opt_cpuoptions.h" -#include "opt_efi.h" +#include "genfb.h" #include "ukbd.h" #include "wsdisplay.h" @@ -47,6 +48,7 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep. #include #include #include +#include #include #include #include @@ -64,6 +66,8 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep. #include #include #include +#include +#include #include #include @@ -85,14 +89,19 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep. #include #include #include -#include #include +#include +#include #ifdef EFI_RUNTIME #include #endif +#if NWSDISPLAY > 0 && NGENFB > 0 +#include +#endif + #if NUKBD > 0 #include #endif @@ -117,6 +126,8 @@ u_long uboot_args[4] __attribute__((__se const uint8_t *fdt_addr_r __attribute__((__section__(".data"))); static uint64_t initrd_start, initrd_end; +static uint64_t rndseed_start, rndseed_end; /* our on-disk seed */ +static uint64_t efirng_start, efirng_end; /* firmware's EFI RNG output */ #include #include @@ -133,6 +144,10 @@ static void fdt_cpu_rootconf(void); static void fdt_reset(void); static void fdt_powerdown(void); +#if BYTE_ORDER == BIG_ENDIAN +static void fdt_update_fb_format(void); +#endif + static void earlyconsputc(dev_t dev, int c) { @@ -157,88 +172,6 @@ static struct consdev earlycons = { #define VPRINTF(...) __nothing #endif -/* - * ARM: Get the first physically contiguous region of memory. - * ARM64: Get all of physical memory, including holes. - */ -static void -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, &cur_addr, &cur_size) != 0) - panic("Cannot determine memory size"); - - *pstart = cur_addr; - *pend = cur_addr + cur_size; - - VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", - 0, *pstart, *pend - *pstart); - - for (index = 1; - fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0; - index++) { - VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", - index, cur_addr, 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 - } -} - -void -fdt_add_reserved_memory_range(uint64_t addr, uint64_t size) -{ - fdt_memory_remove_range(addr, size); -} - -/* - * Exclude memory ranges from memory config from the device tree - */ -static void -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; - - const int num = fdt_num_mem_rsv(fdtbus_get_data()); - for (index = 0; index <= num; index++) { - error = fdt_get_mem_rsv(fdtbus_get_data(), index, - &addr, &size); - 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; - } -} - static void fdt_add_dram_blocks(const struct fdt_memory *m, void *arg) { @@ -258,21 +191,24 @@ static struct boot_physmem fdt_physmem[M static void fdt_add_boot_physmem(const struct fdt_memory *m, void *arg) { - struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++]; + const paddr_t saddr = round_page(m->start); + const paddr_t eaddr = trunc_page(m->end); - VPRINTF(" %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1); + VPRINTF(" %" PRIx64 " - %" PRIx64, m->start, m->end - 1); + if (saddr >= eaddr) { + VPRINTF(" skipped\n"); + return; + } + VPRINTF("\n"); + + struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++]; KASSERT(nfdt_physmem <= MAX_PHYSMEM); - bp->bp_start = atop(round_page(m->start)); - bp->bp_pages = atop(trunc_page(m->end)) - bp->bp_start; + bp->bp_start = atop(saddr); + bp->bp_pages = atop(eaddr) - bp->bp_start; bp->bp_freelist = VM_FREELIST_DEFAULT; -#ifdef _LP64 - if (m->end > 0x100000000) - bp->bp_freelist = VM_FREELIST_HIGHMEM; -#endif - #ifdef PMAP_NEED_ALLOC_POOLPAGE const uint64_t memory_size = *(uint64_t *)arg; if (atop(memory_size) > bp->bp_pages) { @@ -282,40 +218,51 @@ fdt_add_boot_physmem(const struct fdt_me #endif } + +static void +fdt_print_memory(const struct fdt_memory *m, void *arg) +{ + + VPRINTF("FDT /memory @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", + m->start, m->end - m->start); +} + + /* * Define usable memory regions. */ static void fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end) { - const int memory = OF_finddevice("/memory"); BootConfig *bc = &bootconfig; + uint64_t addr, size; int index; - for (index = 0; - fdtbus_get_reg64(memory, index, &addr, &size) == 0; - index++) { - if (addr >= mem_end || size == 0) - continue; - if (addr + size > mem_end) - size = mem_end - addr; + fdt_memory_remove_reserved(mem_start, mem_end); - fdt_memory_add_range(addr, size); - } - - fdt_add_reserved_memory(mem_start, mem_end); - - const uint64_t initrd_size = initrd_end - initrd_start; + const uint64_t initrd_size = + round_page(initrd_end) - trunc_page(initrd_start); if (initrd_size > 0) - fdt_memory_remove_range(initrd_start, initrd_size); + fdt_memory_remove_range(trunc_page(initrd_start), initrd_size); + + const uint64_t rndseed_size = + round_page(rndseed_end) - trunc_page(rndseed_start); + if (rndseed_size > 0) + fdt_memory_remove_range(trunc_page(rndseed_start), + rndseed_size); + + const uint64_t efirng_size = + round_page(efirng_end) - trunc_page(efirng_start); + if (efirng_size > 0) + fdt_memory_remove_range(trunc_page(efirng_start), efirng_size); const int framebuffer = OF_finddevice("/chosen/framebuffer"); if (framebuffer >= 0) { for (index = 0; fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0; index++) { - fdt_add_reserved_memory_range(addr, size); + fdt_memory_remove_range(addr, size); } } @@ -325,20 +272,20 @@ fdt_build_bootconfig(uint64_t mem_start, } static void -fdt_probe_initrd(uint64_t *pstart, uint64_t *pend) +fdt_probe_range(const char *startname, const char *endname, + uint64_t *pstart, uint64_t *pend) { + int chosen, len; + const void *start_data, *end_data; + *pstart = *pend = 0; -#ifdef MEMORY_DISK_DYNAMIC - const int chosen = OF_finddevice("/chosen"); + chosen = OF_finddevice("/chosen"); if (chosen < 0) return; - int len; - const void *start_data = fdtbus_get_prop(chosen, - "linux,initrd-start", &len); - const void *end_data = fdtbus_get_prop(chosen, - "linux,initrd-end", NULL); + start_data = fdtbus_get_prop(chosen, startname, &len); + end_data = fdtbus_get_prop(chosen, endname, NULL); if (start_data == NULL || end_data == NULL) return; @@ -352,43 +299,164 @@ fdt_probe_initrd(uint64_t *pstart, uint6 *pend = be64dec(end_data); break; default: - printf("Unsupported len %d for /chosen/initrd-start\n", len); + printf("Unsupported len %d for /chosen `%s'\n", + len, startname); return; } -#endif } -static void -fdt_setup_initrd(void) +static void * +fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize, + const char *purpose) { -#ifdef MEMORY_DISK_DYNAMIC - const uint64_t initrd_size = initrd_end - initrd_start; - paddr_t startpa = trunc_page(initrd_start); - paddr_t endpa = round_page(initrd_end); + const paddr_t startpa = trunc_page(start); + const paddr_t endpa = round_page(end); paddr_t pa; vaddr_t va; - void *md_start; + void *ptr; - if (initrd_size == 0) - return; + *psize = end - start; + if (*psize == 0) + return NULL; - va = uvm_km_alloc(kernel_map, initrd_size, 0, - UVM_KMF_VAONLY | UVM_KMF_NOWAIT); + const vaddr_t voff = start & PAGE_MASK; + + va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY|UVM_KMF_NOWAIT); if (va == 0) { - printf("Failed to allocate VA for initrd\n"); - return; + printf("Failed to allocate VA for %s\n", purpose); + return NULL; } - - md_start = (void *)va; + ptr = (void *)(va + voff); for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0); pmap_update(pmap_kernel()); + return ptr; +} + +static void +fdt_unmap_range(void *ptr, uint64_t size) +{ + const char *start = ptr, *end = start + size; + const vaddr_t startva = trunc_page((vaddr_t)(uintptr_t)start); + const vaddr_t endva = round_page((vaddr_t)(uintptr_t)end); + + pmap_kremove(startva, endva - startva); + pmap_update(pmap_kernel()); +} + +static void +fdt_probe_initrd(uint64_t *pstart, uint64_t *pend) +{ + *pstart = *pend = 0; + +#ifdef MEMORY_DISK_DYNAMIC + fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend); +#endif +} + +static void +fdt_setup_initrd(void) +{ +#ifdef MEMORY_DISK_DYNAMIC + void *md_start; + uint64_t initrd_size; + + md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size, + "initrd"); + if (md_start == NULL) + return; md_root_setconf(md_start, initrd_size); #endif } +static void +fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend) +{ + + fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end", + pstart, pend); +} + +static void +fdt_setup_rndseed(void) +{ + uint64_t rndseed_size; + void *rndseed; + + rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size, + "rndseed"); + if (rndseed == NULL) + return; + rnd_seed(rndseed, rndseed_size); + fdt_unmap_range(rndseed, rndseed_size); +} + +static void +fdt_probe_efirng(uint64_t *pstart, uint64_t *pend) +{ + + fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end", + pstart, pend); +} + +static struct krndsource efirng_source; + +static void +fdt_setup_efirng(void) +{ + uint64_t efirng_size; + void *efirng; + + efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size, + "efirng"); + if (efirng == NULL) + return; + + rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG, + RND_FLAG_DEFAULT); + + /* + * We don't really have specific information about the physical + * process underlying the data provided by the firmware via the + * EFI RNG API, so the entropy estimate here is heuristic. + * What efiboot provides us is up to 4096 bytes of data from + * the EFI RNG API, although in principle it may return short. + * + * The UEFI Specification (2.8 Errata A, February 2020[1]) says + * + * When a Deterministic Random Bit Generator (DRBG) is + * used on the output of a (raw) entropy source, its + * security level must be at least 256 bits. + * + * It's not entirely clear whether `it' refers to the DRBG or + * the entropy source; if it refers to the DRBG, it's not + * entirely clear how ANSI X9.31 3DES, one of the options for + * DRBG in the UEFI spec, can provide a `256-bit security + * level' because it has only 232 bits of inputs (three 56-bit + * keys and one 64-bit block). That said, even if it provides + * only 232 bits of entropy, that's enough to prevent all + * attacks and we probably get a few more bits from sampling + * the clock anyway. + * + * In the event we get raw samples, e.g. the bits sampled by a + * ring oscillator, we hope that the samples have at least half + * a bit of entropy per bit of data -- and efiboot tries to + * draw 4096 bytes to provide plenty of slop. Hence we divide + * the total number of bits by two and clamp at 256. There are + * ways this could go wrong, but on most machines it should + * behave reasonably. + * + * [1] https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_A_Feb14.pdf + */ + rnd_add_data(&efirng_source, efirng, efirng_size, + MIN(256, efirng_size*NBBY/2)); + + explicit_memset(efirng, 0, efirng_size); + fdt_unmap_range(efirng, efirng_size); +} + #ifdef EFI_RUNTIME static void fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type) @@ -406,8 +474,10 @@ fdt_map_efi_runtime(const char *prop, en while (len >= 24) { const paddr_t pa = be64toh(map[0]); const vaddr_t va = be64toh(map[1]); - const uint64_t sz = be64toh(map[2]); - VPRINTF("%s: %s %lx-%lx (%lx-%lx)\n", __func__, prop, pa, pa+sz-1, va, va+sz-1); + const size_t sz = be64toh(map[2]); + VPRINTF("%s: %s %" PRIxPADDR "-%" PRIxVADDR "(%" PRIxVADDR + "-%" PRIxVSIZE "\n", __func__, prop, pa, pa + sz - 1, + va, va + sz - 1); arm_efirt_md_map_range(va, pa, sz, type); map += 3; len -= 24; @@ -415,9 +485,7 @@ fdt_map_efi_runtime(const char *prop, en } #endif -u_int initarm(void *arg); - -u_int +vaddr_t initarm(void *arg) { const struct arm_platform *plat; @@ -428,17 +496,18 @@ initarm(void *arg) /* Load FDT */ int error = fdt_check_header(fdt_addr_r); - if (error == 0) { - /* 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); - } else { + if (error != 0) panic("fdt_check_header failed: %s", fdt_strerror(error)); - } + + /* 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_init(fdt_data); /* Lookup platform specific backend */ plat = arm_fdt_platform(); @@ -463,7 +532,7 @@ initarm(void *arg) * l1pt VA is fine */ - VPRINTF("devmap\n"); + VPRINTF("devmap %p\n", plat->ap_devmap()); extern char ARM_BOOTSTRAP_LxPT[]; pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap()); @@ -477,6 +546,17 @@ initarm(void *arg) VPRINTF("stdout\n"); fdt_update_stdout_path(); +#if BYTE_ORDER == BIG_ENDIAN + /* + * Most boards are configured to little-endian mode in initial, and + * switched to big-endian mode after kernel is loaded. In this case, + * framebuffer seems byte-swapped to CPU. Override FDT to let + * drivers know. + */ + VPRINTF("fb_format\n"); + fdt_update_fb_format(); +#endif + /* * Done making changes to the FDT. */ @@ -503,13 +583,17 @@ initarm(void *arg) parse_mi_bootargs(mi_bootargs); #endif - fdt_get_memory(&memory_start, &memory_end); + fdt_memory_get(&memory_start, &memory_end); -#if !defined(_LP64) - /* Cannot map memory above 4GB */ - if (memory_end >= 0x100000000ULL) - memory_end = 0x100000000ULL - PAGE_SIZE; + fdt_memory_foreach(fdt_print_memory, NULL); +#if !defined(_LP64) + /* Cannot map memory above 4GB (remove last page as well) */ + const uint64_t memory_limit = 0x100000000ULL - PAGE_SIZE; + if (memory_end > memory_limit) { + fdt_memory_remove_range(memory_limit , memory_end); + memory_end = memory_limit; + } #endif uint64_t memory_size = memory_end - memory_start; @@ -519,9 +603,12 @@ initarm(void *arg) /* Parse ramdisk info */ fdt_probe_initrd(&initrd_start, &initrd_end); + /* Parse our on-disk rndseed and the firmware's RNG from EFI */ + fdt_probe_rndseed(&rndseed_start, &rndseed_end); + fdt_probe_efirng(&efirng_start, &efirng_end); + /* - * Populate bootconfig structure for the benefit of - * dodumpsys + * Populate bootconfig structure for the benefit of dodumpsys */ VPRINTF("%s: fdt_build_bootconfig\n", __func__); fdt_build_bootconfig(memory_start, memory_end); @@ -542,9 +629,12 @@ initarm(void *arg) VPRINTF("Memory regions:\n"); fdt_memory_foreach(fdt_add_boot_physmem, &memory_size); - u_int sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem, + vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem, nfdt_physmem); + /* + * initarm_common flushes cache if required before AP start + */ error = 0; if ((boothowto & RB_MD1) == 0) { VPRINTF("mpstart\n"); @@ -554,6 +644,7 @@ initarm(void *arg) if (error) return sp; + /* * Now we have APs started the pages used for stacks and L1PT can * be given to uvm @@ -567,9 +658,11 @@ initarm(void *arg) const paddr_t spg = atop(spa); const paddr_t epg = atop(epa); + VPRINTF(" start %08lx end %08lx... " + "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT); + uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT); - VPRINTF(" start %08lx end %08lx", ptoa(spa), ptoa(epa)); } return sp; @@ -625,6 +718,16 @@ consinit(void) } void +cpu_startup_hook(void) +{ + + fdtbus_intr_init(); + + fdt_setup_rndseed(); + fdt_setup_efirng(); +} + +void delay(u_int us) { const struct arm_platform *plat = arm_fdt_platform(); @@ -732,9 +835,25 @@ fdt_device_register(device_t self, void { const struct arm_platform *plat = arm_fdt_platform(); - if (device_is_a(self, "armfdt")) + if (device_is_a(self, "armfdt")) { fdt_setup_initrd(); +#if NWSDISPLAY > 0 && NGENFB > 0 + /* + * Setup framebuffer console, if present. + */ + arm_simplefb_preattach(); +#endif + } + +#if NWSDISPLAY > 0 && NGENFB > 0 + if (device_is_a(self, "genfb")) { + prop_dictionary_t dict = device_properties(self); + prop_dictionary_set_uint64(dict, + "simplefb-physaddr", arm_simplefb_physaddr()); + } +#endif + if (plat && plat->ap_device_register) plat->ap_device_register(self, aux); } @@ -787,3 +906,36 @@ fdt_powerdown(void) { fdtbus_power_poweroff(); } + +#if BYTE_ORDER == BIG_ENDIAN +static void +fdt_update_fb_format(void) +{ + int off, len; + const char *format, *replace; + + off = fdt_path_offset(fdt_data, "/chosen"); + if (off < 0) + return; + + for (;;) { + off = fdt_node_offset_by_compatible(fdt_data, off, + "simple-framebuffer"); + if (off < 0) + return; + + format = fdt_getprop(fdt_data, off, "format", &len); + if (format == NULL) + continue; + + replace = NULL; + if (strcmp(format, "a8b8g8r8") == 0) + replace = "r8g8b8a8"; + else if (strcmp(format, "x8r8g8b8") == 0) + replace = "b8g8r8x8"; + if (replace != NULL) + fdt_setprop(fdt_data, off, "format", replace, + strlen(replace) + 1); + } +} +#endif