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