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