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