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