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