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