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