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