Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.75
      1 /* $NetBSD: fdt_machdep.c,v 1.75 2020/10/10 15:25:30 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.75 2020/10/10 15:25:30 jmcneill Exp $");
     31 
     32 #include "opt_machdep.h"
     33 #include "opt_bootconfig.h"
     34 #include "opt_ddb.h"
     35 #include "opt_md.h"
     36 #include "opt_arm_debug.h"
     37 #include "opt_multiprocessor.h"
     38 #include "opt_cpuoptions.h"
     39 #include "opt_efi.h"
     40 
     41 #include "genfb.h"
     42 #include "ukbd.h"
     43 #include "wsdisplay.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/bus.h>
     48 #include <sys/atomic.h>
     49 #include <sys/cpu.h>
     50 #include <sys/device.h>
     51 #include <sys/exec.h>
     52 #include <sys/kernel.h>
     53 #include <sys/kmem.h>
     54 #include <sys/ksyms.h>
     55 #include <sys/msgbuf.h>
     56 #include <sys/proc.h>
     57 #include <sys/reboot.h>
     58 #include <sys/termios.h>
     59 #include <sys/bootblock.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/vnode.h>
     62 #include <sys/kauth.h>
     63 #include <sys/fcntl.h>
     64 #include <sys/uuid.h>
     65 #include <sys/disk.h>
     66 #include <sys/md5.h>
     67 #include <sys/pserialize.h>
     68 #include <sys/rnd.h>
     69 #include <sys/rndsource.h>
     70 
     71 #include <net/if.h>
     72 #include <net/if_dl.h>
     73 
     74 #include <dev/cons.h>
     75 #include <uvm/uvm_extern.h>
     76 
     77 #include <sys/conf.h>
     78 
     79 #include <machine/db_machdep.h>
     80 #include <ddb/db_sym.h>
     81 #include <ddb/db_extern.h>
     82 
     83 #include <machine/bootconfig.h>
     84 #include <arm/armreg.h>
     85 
     86 #include <arm/cpufunc.h>
     87 
     88 #include <evbarm/include/autoconf.h>
     89 #include <evbarm/fdt/machdep.h>
     90 #include <evbarm/fdt/platform.h>
     91 #include <evbarm/fdt/fdt_memory.h>
     92 
     93 #include <arm/fdt/arm_fdtvar.h>
     94 #include <dev/fdt/fdt_private.h>
     95 
     96 #ifdef EFI_RUNTIME
     97 #include <arm/arm/efi_runtime.h>
     98 #endif
     99 
    100 #if NWSDISPLAY > 0 && NGENFB > 0
    101 #include <arm/fdt/arm_simplefb.h>
    102 #endif
    103 
    104 #if NUKBD > 0
    105 #include <dev/usb/ukbdvar.h>
    106 #endif
    107 #if NWSDISPLAY > 0
    108 #include <dev/wscons/wsdisplayvar.h>
    109 #endif
    110 
    111 #ifdef MEMORY_DISK_DYNAMIC
    112 #include <dev/md.h>
    113 #endif
    114 
    115 #ifndef FDT_MAX_BOOT_STRING
    116 #define FDT_MAX_BOOT_STRING 1024
    117 #endif
    118 
    119 BootConfig bootconfig;
    120 char bootargs[FDT_MAX_BOOT_STRING] = "";
    121 char *boot_args = NULL;
    122 
    123 /* filled in before cleaning bss. keep in .data */
    124 u_long uboot_args[4] __attribute__((__section__(".data")));
    125 const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
    126 
    127 static uint64_t initrd_start, initrd_end;
    128 static uint64_t rndseed_start, rndseed_end; /* our on-disk seed */
    129 static uint64_t efirng_start, efirng_end;   /* firmware's EFI RNG output */
    130 
    131 #include <libfdt.h>
    132 #include <dev/fdt/fdtvar.h>
    133 #define FDT_BUF_SIZE	(512*1024)
    134 static uint8_t fdt_data[FDT_BUF_SIZE];
    135 
    136 extern char KERNEL_BASE_phys[];
    137 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
    138 
    139 static void fdt_update_stdout_path(void);
    140 static void fdt_device_register(device_t, void *);
    141 static void fdt_device_register_post_config(device_t, void *);
    142 static void fdt_cpu_rootconf(void);
    143 static void fdt_reset(void);
    144 static void fdt_powerdown(void);
    145 
    146 static void
    147 earlyconsputc(dev_t dev, int c)
    148 {
    149 	uartputc(c);
    150 }
    151 
    152 static int
    153 earlyconsgetc(dev_t dev)
    154 {
    155 	return 0;
    156 }
    157 
    158 static struct consdev earlycons = {
    159 	.cn_putc = earlyconsputc,
    160 	.cn_getc = earlyconsgetc,
    161 	.cn_pollc = nullcnpollc,
    162 };
    163 
    164 #ifdef VERBOSE_INIT_ARM
    165 #define VPRINTF(...)	printf(__VA_ARGS__)
    166 #else
    167 #define VPRINTF(...)	__nothing
    168 #endif
    169 
    170 /*
    171  * Get all of physical memory, including holes.
    172  */
    173 static void
    174 fdt_get_memory(uint64_t *pstart, uint64_t *pend)
    175 {
    176 	const int memory = OF_finddevice("/memory");
    177 	uint64_t cur_addr, cur_size;
    178 	int index;
    179 
    180 	/* Assume the first entry is the start of memory */
    181 	if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0)
    182 		panic("Cannot determine memory size");
    183 
    184 	*pstart = cur_addr;
    185 	*pend = cur_addr + cur_size;
    186 
    187 	VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    188 	    0, *pstart, *pend - *pstart);
    189 
    190 	for (index = 1;
    191 	     fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
    192 	     index++) {
    193 		VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    194 		    index, cur_addr, cur_size);
    195 
    196 		if (cur_addr + cur_size > *pend)
    197 			*pend = cur_addr + cur_size;
    198 	}
    199 }
    200 
    201 void
    202 fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
    203 {
    204 	fdt_memory_remove_range(addr, size);
    205 }
    206 
    207 /*
    208  * Exclude memory ranges from memory config from the device tree
    209  */
    210 static void
    211 fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr)
    212 {
    213 	uint64_t lstart = 0, lend = 0;
    214 	uint64_t addr, size;
    215 	int index, error;
    216 
    217 	const int num = fdt_num_mem_rsv(fdtbus_get_data());
    218 	for (index = 0; index <= num; index++) {
    219 		error = fdt_get_mem_rsv(fdtbus_get_data(), index,
    220 		    &addr, &size);
    221 		if (error != 0)
    222 			continue;
    223 		if (lstart <= addr && addr <= lend) {
    224 			size -= (lend - addr);
    225 			addr = lend;
    226 		}
    227 		if (size == 0)
    228 			continue;
    229 		if (addr + size <= min_addr)
    230 			continue;
    231 		if (addr >= max_addr)
    232 			continue;
    233 		if (addr < min_addr) {
    234 			size -= (min_addr - addr);
    235 			addr = min_addr;
    236 		}
    237 		if (addr + size > max_addr)
    238 			size = max_addr - addr;
    239 		fdt_add_reserved_memory_range(addr, size);
    240 		lstart = addr;
    241 		lend = addr + size;
    242 	}
    243 }
    244 
    245 static void
    246 fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
    247 {
    248 	BootConfig *bc = arg;
    249 
    250 	VPRINTF("  %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
    251 	bc->dram[bc->dramblocks].address = m->start;
    252 	bc->dram[bc->dramblocks].pages =
    253 	    (m->end - m->start) / PAGE_SIZE;
    254 	bc->dramblocks++;
    255 }
    256 
    257 #define MAX_PHYSMEM 64
    258 static int nfdt_physmem = 0;
    259 static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
    260 
    261 static void
    262 fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
    263 {
    264 	const paddr_t saddr = round_page(m->start);
    265 	const paddr_t eaddr = trunc_page(m->end);
    266 
    267 	VPRINTF("  %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
    268 	if (saddr >= eaddr) {
    269 		VPRINTF(" skipped\n");
    270 		return;
    271 	}
    272 	VPRINTF("\n");
    273 
    274 	struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
    275 
    276 	KASSERT(nfdt_physmem <= MAX_PHYSMEM);
    277 
    278 	bp->bp_start = atop(saddr);
    279 	bp->bp_pages = atop(eaddr) - bp->bp_start;
    280 	bp->bp_freelist = VM_FREELIST_DEFAULT;
    281 
    282 #ifdef PMAP_NEED_ALLOC_POOLPAGE
    283 	const uint64_t memory_size = *(uint64_t *)arg;
    284 	if (atop(memory_size) > bp->bp_pages) {
    285 		arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
    286 		bp->bp_freelist = VM_FREELIST_DIRECTMAP;
    287 	}
    288 #endif
    289 }
    290 
    291 /*
    292  * Define usable memory regions.
    293  */
    294 static void
    295 fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
    296 {
    297 	const int memory = OF_finddevice("/memory");
    298 	BootConfig *bc = &bootconfig;
    299 	uint64_t addr, size;
    300 	int index;
    301 
    302 	for (index = 0;
    303 	     fdtbus_get_reg64(memory, index, &addr, &size) == 0;
    304 	     index++) {
    305 		if (addr >= mem_end || size == 0)
    306 			continue;
    307 		if (addr + size > mem_end)
    308 			size = mem_end - addr;
    309 
    310 		fdt_memory_add_range(addr, size);
    311 	}
    312 
    313 	fdt_add_reserved_memory(mem_start, mem_end);
    314 
    315 	const uint64_t initrd_size =
    316 	    round_page(initrd_end) - trunc_page(initrd_start);
    317 	if (initrd_size > 0)
    318 		fdt_memory_remove_range(trunc_page(initrd_start), initrd_size);
    319 
    320 	const uint64_t rndseed_size =
    321 	    round_page(rndseed_end) - trunc_page(rndseed_start);
    322 	if (rndseed_size > 0)
    323 		fdt_memory_remove_range(trunc_page(rndseed_start),
    324 		    rndseed_size);
    325 
    326 	const uint64_t efirng_size =
    327 	    round_page(efirng_end) - trunc_page(efirng_start);
    328 	if (efirng_size > 0)
    329 		fdt_memory_remove_range(trunc_page(efirng_start), efirng_size);
    330 
    331 	const int framebuffer = OF_finddevice("/chosen/framebuffer");
    332 	if (framebuffer >= 0) {
    333 		for (index = 0;
    334 		     fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
    335 		     index++) {
    336 			fdt_add_reserved_memory_range(addr, size);
    337 		}
    338 	}
    339 
    340 	VPRINTF("Usable memory:\n");
    341 	bc->dramblocks = 0;
    342 	fdt_memory_foreach(fdt_add_dram_blocks, bc);
    343 }
    344 
    345 static void
    346 fdt_probe_range(const char *startname, const char *endname,
    347     uint64_t *pstart, uint64_t *pend)
    348 {
    349 	int chosen, len;
    350 	const void *start_data, *end_data;
    351 
    352 	*pstart = *pend = 0;
    353 
    354 	chosen = OF_finddevice("/chosen");
    355 	if (chosen < 0)
    356 		return;
    357 
    358 	start_data = fdtbus_get_prop(chosen, startname, &len);
    359 	end_data = fdtbus_get_prop(chosen, endname, NULL);
    360 	if (start_data == NULL || end_data == NULL)
    361 		return;
    362 
    363 	switch (len) {
    364 	case 4:
    365 		*pstart = be32dec(start_data);
    366 		*pend = be32dec(end_data);
    367 		break;
    368 	case 8:
    369 		*pstart = be64dec(start_data);
    370 		*pend = be64dec(end_data);
    371 		break;
    372 	default:
    373 		printf("Unsupported len %d for /chosen `%s'\n",
    374 		    len, startname);
    375 		return;
    376 	}
    377 }
    378 
    379 static void *
    380 fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize,
    381     const char *purpose)
    382 {
    383 	const paddr_t startpa = trunc_page(start);
    384 	const paddr_t endpa = round_page(end);
    385 	paddr_t pa;
    386 	vaddr_t va;
    387 	void *ptr;
    388 
    389 	*psize = end - start;
    390 	if (*psize == 0)
    391 		return NULL;
    392 
    393 	const vaddr_t voff = start & PAGE_MASK;
    394 
    395 	va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY|UVM_KMF_NOWAIT);
    396 	if (va == 0) {
    397 		printf("Failed to allocate VA for %s\n", purpose);
    398 		return NULL;
    399 	}
    400 	ptr = (void *)(va + voff);
    401 
    402 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
    403 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
    404 	pmap_update(pmap_kernel());
    405 
    406 	return ptr;
    407 }
    408 
    409 static void
    410 fdt_unmap_range(void *ptr, uint64_t size)
    411 {
    412 	const char *start = ptr, *end = start + size;
    413 	const vaddr_t startva = trunc_page((vaddr_t)(uintptr_t)start);
    414 	const vaddr_t endva = round_page((vaddr_t)(uintptr_t)end);
    415 
    416 	pmap_kremove(startva, endva - startva);
    417 	pmap_update(pmap_kernel());
    418 }
    419 
    420 static void
    421 fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
    422 {
    423 	*pstart = *pend = 0;
    424 
    425 #ifdef MEMORY_DISK_DYNAMIC
    426 	fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend);
    427 #endif
    428 }
    429 
    430 static void
    431 fdt_setup_initrd(void)
    432 {
    433 #ifdef MEMORY_DISK_DYNAMIC
    434 	void *md_start;
    435 	uint64_t initrd_size;
    436 
    437 	md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size,
    438 	    "initrd");
    439 	if (md_start == NULL)
    440 		return;
    441 	md_root_setconf(md_start, initrd_size);
    442 #endif
    443 }
    444 
    445 static void
    446 fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend)
    447 {
    448 
    449 	fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end",
    450 	    pstart, pend);
    451 }
    452 
    453 static void
    454 fdt_setup_rndseed(void)
    455 {
    456 	uint64_t rndseed_size;
    457 	void *rndseed;
    458 
    459 	rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size,
    460 	    "rndseed");
    461 	if (rndseed == NULL)
    462 		return;
    463 	rnd_seed(rndseed, rndseed_size);
    464 	fdt_unmap_range(rndseed, rndseed_size);
    465 }
    466 
    467 static void
    468 fdt_probe_efirng(uint64_t *pstart, uint64_t *pend)
    469 {
    470 
    471 	fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end",
    472 	    pstart, pend);
    473 }
    474 
    475 static struct krndsource efirng_source;
    476 
    477 static void
    478 fdt_setup_efirng(void)
    479 {
    480 	uint64_t efirng_size;
    481 	void *efirng;
    482 
    483 	efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size,
    484 	    "efirng");
    485 	if (efirng == NULL)
    486 		return;
    487 
    488 	rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG,
    489 	    RND_FLAG_DEFAULT);
    490 	rnd_add_data(&efirng_source, efirng, efirng_size, 0);
    491 	explicit_memset(efirng, 0, efirng_size);
    492 	fdt_unmap_range(efirng, efirng_size);
    493 }
    494 
    495 #ifdef EFI_RUNTIME
    496 static void
    497 fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type)
    498 {
    499 	int len;
    500 
    501 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    502 	if (chosen_off < 0)
    503 		return;
    504 
    505 	const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len);
    506 	if (map == NULL)
    507 		return;
    508 
    509 	while (len >= 24) {
    510 		const paddr_t pa = be64toh(map[0]);
    511 		const vaddr_t va = be64toh(map[1]);
    512 		const uint64_t sz = be64toh(map[2]);
    513 		VPRINTF("%s: %s %lx-%lx (%lx-%lx)\n", __func__, prop, pa, pa+sz-1, va, va+sz-1);
    514 		arm_efirt_md_map_range(va, pa, sz, type);
    515 		map += 3;
    516 		len -= 24;
    517 	}
    518 }
    519 #endif
    520 
    521 vaddr_t
    522 initarm(void *arg)
    523 {
    524 	const struct arm_platform *plat;
    525 	uint64_t memory_start, memory_end;
    526 
    527 	/* set temporally to work printf()/panic() even before consinit() */
    528 	cn_tab = &earlycons;
    529 
    530 	/* Load FDT */
    531 	int error = fdt_check_header(fdt_addr_r);
    532 	if (error == 0) {
    533 		/* If the DTB is too big, try to pack it in place first. */
    534 		if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
    535 			(void)fdt_pack(__UNCONST(fdt_addr_r));
    536 		error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
    537 		if (error != 0)
    538 			panic("fdt_move failed: %s", fdt_strerror(error));
    539 		fdtbus_init(fdt_data);
    540 	} else {
    541 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    542 	}
    543 
    544 	/* Lookup platform specific backend */
    545 	plat = arm_fdt_platform();
    546 	if (plat == NULL)
    547 		panic("Kernel does not support this device");
    548 
    549 	/* Early console may be available, announce ourselves. */
    550 	VPRINTF("FDT<%p>\n", fdt_addr_r);
    551 
    552 	const int chosen = OF_finddevice("/chosen");
    553 	if (chosen >= 0)
    554 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    555 	boot_args = bootargs;
    556 
    557 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    558 	VPRINTF("cpufunc\n");
    559 	if (set_cpufuncs())
    560 		panic("cpu not recognized!");
    561 
    562 	/*
    563 	 * Memory is still identity/flat mapped this point so using ttbr for
    564 	 * l1pt VA is fine
    565 	 */
    566 
    567 	VPRINTF("devmap\n");
    568 	extern char ARM_BOOTSTRAP_LxPT[];
    569 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap());
    570 
    571 	VPRINTF("bootstrap\n");
    572 	plat->ap_bootstrap();
    573 
    574 	/*
    575 	 * If stdout-path is specified on the command line, override the
    576 	 * value in /chosen/stdout-path before initializing console.
    577 	 */
    578 	VPRINTF("stdout\n");
    579 	fdt_update_stdout_path();
    580 
    581 	/*
    582 	 * Done making changes to the FDT.
    583 	 */
    584 	fdt_pack(fdt_data);
    585 
    586 	VPRINTF("consinit ");
    587 	consinit();
    588 	VPRINTF("ok\n");
    589 
    590 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
    591 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    592 
    593 	cpu_reset_address = fdt_reset;
    594 	cpu_powerdown_address = fdt_powerdown;
    595 	evbarm_device_register = fdt_device_register;
    596 	evbarm_device_register_post_config = fdt_device_register_post_config;
    597 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
    598 
    599 	/* Talk to the user */
    600 	printf("NetBSD/evbarm (fdt) booting ...\n");
    601 
    602 #ifdef BOOT_ARGS
    603 	char mi_bootargs[] = BOOT_ARGS;
    604 	parse_mi_bootargs(mi_bootargs);
    605 #endif
    606 
    607 	fdt_get_memory(&memory_start, &memory_end);
    608 
    609 #if !defined(_LP64)
    610 	/* Cannot map memory above 4GB */
    611 	if (memory_end >= 0x100000000ULL)
    612 		memory_end = 0x100000000ULL - PAGE_SIZE;
    613 
    614 #endif
    615 	uint64_t memory_size = memory_end - memory_start;
    616 
    617 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
    618 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
    619 
    620 	/* Parse ramdisk info */
    621 	fdt_probe_initrd(&initrd_start, &initrd_end);
    622 
    623 	/* Parse our on-disk rndseed and the firmware's RNG from EFI */
    624 	fdt_probe_rndseed(&rndseed_start, &rndseed_end);
    625 	fdt_probe_efirng(&efirng_start, &efirng_end);
    626 
    627 	/*
    628 	 * Populate bootconfig structure for the benefit of
    629 	 * dodumpsys
    630 	 */
    631 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
    632 	fdt_build_bootconfig(memory_start, memory_end);
    633 
    634 #ifdef EFI_RUNTIME
    635 	fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
    636 	fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
    637 	fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
    638 #endif
    639 
    640 	/* Perform PT build and VM init */
    641 	cpu_kernel_vm_init(memory_start, memory_size);
    642 
    643 	VPRINTF("bootargs: %s\n", bootargs);
    644 
    645 	parse_mi_bootargs(boot_args);
    646 
    647 	VPRINTF("Memory regions:\n");
    648 	fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
    649 
    650 	vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
    651 	     nfdt_physmem);
    652 
    653 	/*
    654 	 * initarm_common flushes cache if required before AP start
    655 	 */
    656 	error = 0;
    657 	if ((boothowto & RB_MD1) == 0) {
    658 		VPRINTF("mpstart\n");
    659 		if (plat->ap_mpstart)
    660 			error = plat->ap_mpstart();
    661 	}
    662 
    663 	if (error)
    664 		return sp;
    665 
    666 	/*
    667 	 * Now we have APs started the pages used for stacks and L1PT can
    668 	 * be given to uvm
    669 	 */
    670 	extern char const __start__init_memory[];
    671 	extern char const __stop__init_memory[] __weak;
    672 
    673 	if (__start__init_memory != __stop__init_memory) {
    674 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
    675 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
    676 		const paddr_t spg = atop(spa);
    677 		const paddr_t epg = atop(epa);
    678 
    679 		VPRINTF("         start %08lx  end %08lx... "
    680 		    "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
    681 
    682 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
    683 
    684 	}
    685 
    686 	return sp;
    687 }
    688 
    689 static void
    690 fdt_update_stdout_path(void)
    691 {
    692 	char *stdout_path, *ep;
    693 	int stdout_path_len;
    694 	char buf[256];
    695 
    696 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    697 	if (chosen_off == -1)
    698 		return;
    699 
    700 	if (get_bootconf_option(boot_args, "stdout-path",
    701 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
    702 		return;
    703 
    704 	ep = strchr(stdout_path, ' ');
    705 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
    706 	if (stdout_path_len >= sizeof(buf))
    707 		return;
    708 
    709 	strncpy(buf, stdout_path, stdout_path_len);
    710 	buf[stdout_path_len] = '\0';
    711 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
    712 	    buf, stdout_path_len + 1);
    713 }
    714 
    715 void
    716 consinit(void)
    717 {
    718 	static bool initialized = false;
    719 	const struct arm_platform *plat = arm_fdt_platform();
    720 	const struct fdt_console *cons = fdtbus_get_console();
    721 	struct fdt_attach_args faa;
    722 	u_int uart_freq = 0;
    723 
    724 	if (initialized || cons == NULL)
    725 		return;
    726 
    727 	plat->ap_init_attach_args(&faa);
    728 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    729 
    730 	if (plat->ap_uart_freq != NULL)
    731 		uart_freq = plat->ap_uart_freq();
    732 
    733 	cons->consinit(&faa, uart_freq);
    734 
    735 	initialized = true;
    736 }
    737 
    738 void
    739 cpu_startup_hook(void)
    740 {
    741 
    742 	fdtbus_intr_init();
    743 
    744 	fdt_setup_rndseed();
    745 	fdt_setup_efirng();
    746 }
    747 
    748 void
    749 delay(u_int us)
    750 {
    751 	const struct arm_platform *plat = arm_fdt_platform();
    752 
    753 	plat->ap_delay(us);
    754 }
    755 
    756 static void
    757 fdt_detect_root_device(device_t dev)
    758 {
    759 	struct mbr_sector mbr;
    760 	uint8_t buf[DEV_BSIZE];
    761 	uint8_t hash[16];
    762 	const uint8_t *rhash;
    763 	char rootarg[64];
    764 	struct vnode *vp;
    765 	MD5_CTX md5ctx;
    766 	int error, len;
    767 	size_t resid;
    768 	u_int part;
    769 
    770 	const int chosen = OF_finddevice("/chosen");
    771 	if (chosen < 0)
    772 		return;
    773 
    774 	if (of_hasprop(chosen, "netbsd,mbr") &&
    775 	    of_hasprop(chosen, "netbsd,partition")) {
    776 
    777 		/*
    778 		 * The bootloader has passed in a partition index and MD5 hash
    779 		 * of the MBR sector. Read the MBR of this device, calculate the
    780 		 * hash, and compare it with the value passed in.
    781 		 */
    782 		rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
    783 		if (rhash == NULL || len != 16)
    784 			return;
    785 		of_getprop_uint32(chosen, "netbsd,partition", &part);
    786 		if (part >= MAXPARTITIONS)
    787 			return;
    788 
    789 		vp = opendisk(dev);
    790 		if (!vp)
    791 			return;
    792 		error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
    793 		    0, NOCRED, &resid, NULL);
    794 		VOP_CLOSE(vp, FREAD, NOCRED);
    795 		vput(vp);
    796 
    797 		if (error != 0)
    798 			return;
    799 
    800 		memcpy(&mbr, buf, sizeof(mbr));
    801 		MD5Init(&md5ctx);
    802 		MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
    803 		MD5Final(hash, &md5ctx);
    804 
    805 		if (memcmp(rhash, hash, 16) != 0)
    806 			return;
    807 
    808 		snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a');
    809 		strcat(boot_args, rootarg);
    810 	}
    811 
    812 	if (of_hasprop(chosen, "netbsd,gpt-guid")) {
    813 		char guidbuf[UUID_STR_LEN];
    814 		const struct uuid *guid = fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len);
    815 		if (guid == NULL || len != 16)
    816 			return;
    817 
    818 		uuid_snprintf(guidbuf, sizeof(guidbuf), guid);
    819 		snprintf(rootarg, sizeof(rootarg), " root=wedge:%s", guidbuf);
    820 		strcat(boot_args, rootarg);
    821 	}
    822 
    823 	if (of_hasprop(chosen, "netbsd,gpt-label")) {
    824 		const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label");
    825 		if (label == NULL || *label == '\0')
    826 			return;
    827 
    828 		device_t dv = dkwedge_find_by_wname(label);
    829 		if (dv != NULL)
    830 			booted_device = dv;
    831 	}
    832 
    833 	if (of_hasprop(chosen, "netbsd,booted-mac-address")) {
    834 		const uint8_t *macaddr = fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len);
    835 		if (macaddr == NULL || len != 6)
    836 			return;
    837 		int s = pserialize_read_enter();
    838 		struct ifnet *ifp;
    839 		IFNET_READER_FOREACH(ifp) {
    840 			if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) {
    841 				device_t dv = device_find_by_xname(ifp->if_xname);
    842 				if (dv != NULL)
    843 					booted_device = dv;
    844 				break;
    845 			}
    846 		}
    847 		pserialize_read_exit(s);
    848 	}
    849 }
    850 
    851 static void
    852 fdt_device_register(device_t self, void *aux)
    853 {
    854 	const struct arm_platform *plat = arm_fdt_platform();
    855 
    856 	if (device_is_a(self, "armfdt")) {
    857 		fdt_setup_initrd();
    858 
    859 #if NWSDISPLAY > 0 && NGENFB > 0
    860 		/*
    861 		 * Setup framebuffer console, if present.
    862 		 */
    863 		arm_simplefb_preattach();
    864 #endif
    865 	}
    866 
    867 	if (plat && plat->ap_device_register)
    868 		plat->ap_device_register(self, aux);
    869 }
    870 
    871 static void
    872 fdt_device_register_post_config(device_t self, void *aux)
    873 {
    874 #if NUKBD > 0 && NWSDISPLAY > 0
    875 	if (device_is_a(self, "wsdisplay")) {
    876 		struct wsdisplay_softc *sc = device_private(self);
    877 		if (wsdisplay_isconsole(sc))
    878 			ukbd_cnattach();
    879 	}
    880 #endif
    881 }
    882 
    883 static void
    884 fdt_cpu_rootconf(void)
    885 {
    886 	device_t dev;
    887 	deviter_t di;
    888 	char *ptr;
    889 
    890 	for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
    891 		if (device_class(dev) != DV_DISK)
    892 			continue;
    893 
    894 		if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0)
    895 			break;
    896 
    897 		if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd"))
    898 			fdt_detect_root_device(dev);
    899 	}
    900 	deviter_release(&di);
    901 }
    902 
    903 static void
    904 fdt_reset(void)
    905 {
    906 	const struct arm_platform *plat = arm_fdt_platform();
    907 
    908 	fdtbus_power_reset();
    909 
    910 	if (plat && plat->ap_reset)
    911 		plat->ap_reset();
    912 }
    913 
    914 static void
    915 fdt_powerdown(void)
    916 {
    917 	fdtbus_power_poweroff();
    918 }
    919