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