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