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