xref: /netbsd-src/sys/arch/evbarm/fdt/fdt_machdep.c (revision 9fd8799cb5ceb66c69f2eb1a6d26a1d587ba1f1e)
1 /* $NetBSD: fdt_machdep.c,v 1.82 2020/11/28 22:16:23 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2015-2017 Jared McNeill <jmcneill@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.82 2020/11/28 22:16:23 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 "genfb.h"
42 #include "ukbd.h"
43 #include "wsdisplay.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/bus.h>
48 #include <sys/atomic.h>
49 #include <sys/cpu.h>
50 #include <sys/device.h>
51 #include <sys/endian.h>
52 #include <sys/exec.h>
53 #include <sys/kernel.h>
54 #include <sys/kmem.h>
55 #include <sys/ksyms.h>
56 #include <sys/msgbuf.h>
57 #include <sys/proc.h>
58 #include <sys/reboot.h>
59 #include <sys/termios.h>
60 #include <sys/bootblock.h>
61 #include <sys/disklabel.h>
62 #include <sys/vnode.h>
63 #include <sys/kauth.h>
64 #include <sys/fcntl.h>
65 #include <sys/uuid.h>
66 #include <sys/disk.h>
67 #include <sys/md5.h>
68 #include <sys/pserialize.h>
69 #include <sys/rnd.h>
70 #include <sys/rndsource.h>
71 
72 #include <net/if.h>
73 #include <net/if_dl.h>
74 
75 #include <dev/cons.h>
76 #include <uvm/uvm_extern.h>
77 
78 #include <sys/conf.h>
79 
80 #include <machine/db_machdep.h>
81 #include <ddb/db_sym.h>
82 #include <ddb/db_extern.h>
83 
84 #include <machine/bootconfig.h>
85 #include <arm/armreg.h>
86 
87 #include <arm/cpufunc.h>
88 
89 #include <evbarm/include/autoconf.h>
90 #include <evbarm/fdt/machdep.h>
91 #include <evbarm/fdt/platform.h>
92 #include <evbarm/fdt/fdt_memory.h>
93 
94 #include <arm/fdt/arm_fdtvar.h>
95 #include <dev/fdt/fdt_private.h>
96 
97 #ifdef EFI_RUNTIME
98 #include <arm/arm/efi_runtime.h>
99 #endif
100 
101 #if NWSDISPLAY > 0 && NGENFB > 0
102 #include <arm/fdt/arm_simplefb.h>
103 #endif
104 
105 #if NUKBD > 0
106 #include <dev/usb/ukbdvar.h>
107 #endif
108 #if NWSDISPLAY > 0
109 #include <dev/wscons/wsdisplayvar.h>
110 #endif
111 
112 #ifdef MEMORY_DISK_DYNAMIC
113 #include <dev/md.h>
114 #endif
115 
116 #ifndef FDT_MAX_BOOT_STRING
117 #define FDT_MAX_BOOT_STRING 1024
118 #endif
119 
120 BootConfig bootconfig;
121 char bootargs[FDT_MAX_BOOT_STRING] = "";
122 char *boot_args = NULL;
123 
124 /* filled in before cleaning bss. keep in .data */
125 u_long uboot_args[4] __attribute__((__section__(".data")));
126 const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
127 
128 static uint64_t initrd_start, initrd_end;
129 static uint64_t rndseed_start, rndseed_end; /* our on-disk seed */
130 static uint64_t efirng_start, efirng_end;   /* firmware's EFI RNG output */
131 
132 #include <libfdt.h>
133 #include <dev/fdt/fdtvar.h>
134 #define FDT_BUF_SIZE	(512*1024)
135 static uint8_t fdt_data[FDT_BUF_SIZE];
136 
137 extern char KERNEL_BASE_phys[];
138 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
139 
140 static void fdt_update_stdout_path(void);
141 static void fdt_device_register(device_t, void *);
142 static void fdt_device_register_post_config(device_t, void *);
143 static void fdt_cpu_rootconf(void);
144 static void fdt_reset(void);
145 static void fdt_powerdown(void);
146 
147 #if BYTE_ORDER == BIG_ENDIAN
148 static void fdt_update_fb_format(void);
149 #endif
150 
151 static void
152 earlyconsputc(dev_t dev, int c)
153 {
154 	uartputc(c);
155 }
156 
157 static int
158 earlyconsgetc(dev_t dev)
159 {
160 	return 0;
161 }
162 
163 static struct consdev earlycons = {
164 	.cn_putc = earlyconsputc,
165 	.cn_getc = earlyconsgetc,
166 	.cn_pollc = nullcnpollc,
167 };
168 
169 #ifdef VERBOSE_INIT_ARM
170 #define VPRINTF(...)	printf(__VA_ARGS__)
171 #else
172 #define VPRINTF(...)	__nothing
173 #endif
174 
175 /*
176  * Get all of physical memory, including holes.
177  */
178 static void
179 fdt_get_memory(uint64_t *pstart, uint64_t *pend)
180 {
181 	const int memory = OF_finddevice("/memory");
182 	uint64_t cur_addr, cur_size;
183 	int index;
184 
185 	/* Assume the first entry is the start of memory */
186 	if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0)
187 		panic("Cannot determine memory size");
188 
189 	*pstart = cur_addr;
190 	*pend = cur_addr + cur_size;
191 
192 	VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
193 	    0, *pstart, *pend - *pstart);
194 
195 	for (index = 1;
196 	     fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
197 	     index++) {
198 		VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
199 		    index, cur_addr, cur_size);
200 
201 		if (cur_addr + cur_size > *pend)
202 			*pend = cur_addr + cur_size;
203 	}
204 }
205 
206 void
207 fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
208 {
209 	fdt_memory_remove_range(addr, size);
210 }
211 
212 /*
213  * Exclude memory ranges from memory config from the device tree
214  */
215 static void
216 fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr)
217 {
218 	uint64_t lstart = 0, lend = 0;
219 	uint64_t addr, size;
220 	int index, error;
221 
222 	const int num = fdt_num_mem_rsv(fdtbus_get_data());
223 	for (index = 0; index <= num; index++) {
224 		error = fdt_get_mem_rsv(fdtbus_get_data(), index,
225 		    &addr, &size);
226 		if (error != 0)
227 			continue;
228 		if (lstart <= addr && addr <= lend) {
229 			size -= (lend - addr);
230 			addr = lend;
231 		}
232 		if (size == 0)
233 			continue;
234 		if (addr + size <= min_addr)
235 			continue;
236 		if (addr >= max_addr)
237 			continue;
238 		if (addr < min_addr) {
239 			size -= (min_addr - addr);
240 			addr = min_addr;
241 		}
242 		if (addr + size > max_addr)
243 			size = max_addr - addr;
244 		fdt_add_reserved_memory_range(addr, size);
245 		lstart = addr;
246 		lend = addr + size;
247 	}
248 }
249 
250 static void
251 fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
252 {
253 	BootConfig *bc = arg;
254 
255 	VPRINTF("  %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
256 	bc->dram[bc->dramblocks].address = m->start;
257 	bc->dram[bc->dramblocks].pages =
258 	    (m->end - m->start) / PAGE_SIZE;
259 	bc->dramblocks++;
260 }
261 
262 #define MAX_PHYSMEM 64
263 static int nfdt_physmem = 0;
264 static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
265 
266 static void
267 fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
268 {
269 	const paddr_t saddr = round_page(m->start);
270 	const paddr_t eaddr = trunc_page(m->end);
271 
272 	VPRINTF("  %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
273 	if (saddr >= eaddr) {
274 		VPRINTF(" skipped\n");
275 		return;
276 	}
277 	VPRINTF("\n");
278 
279 	struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
280 
281 	KASSERT(nfdt_physmem <= MAX_PHYSMEM);
282 
283 	bp->bp_start = atop(saddr);
284 	bp->bp_pages = atop(eaddr) - bp->bp_start;
285 	bp->bp_freelist = VM_FREELIST_DEFAULT;
286 
287 #ifdef PMAP_NEED_ALLOC_POOLPAGE
288 	const uint64_t memory_size = *(uint64_t *)arg;
289 	if (atop(memory_size) > bp->bp_pages) {
290 		arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
291 		bp->bp_freelist = VM_FREELIST_DIRECTMAP;
292 	}
293 #endif
294 }
295 
296 /*
297  * Define usable memory regions.
298  */
299 static void
300 fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
301 {
302 	const int memory = OF_finddevice("/memory");
303 	BootConfig *bc = &bootconfig;
304 	uint64_t addr, size;
305 	int index;
306 
307 	for (index = 0;
308 	     fdtbus_get_reg64(memory, index, &addr, &size) == 0;
309 	     index++) {
310 		if (addr >= mem_end || size == 0)
311 			continue;
312 		if (addr + size > mem_end)
313 			size = mem_end - addr;
314 
315 		fdt_memory_add_range(addr, size);
316 	}
317 
318 	fdt_add_reserved_memory(mem_start, mem_end);
319 
320 	const uint64_t initrd_size =
321 	    round_page(initrd_end) - trunc_page(initrd_start);
322 	if (initrd_size > 0)
323 		fdt_memory_remove_range(trunc_page(initrd_start), initrd_size);
324 
325 	const uint64_t rndseed_size =
326 	    round_page(rndseed_end) - trunc_page(rndseed_start);
327 	if (rndseed_size > 0)
328 		fdt_memory_remove_range(trunc_page(rndseed_start),
329 		    rndseed_size);
330 
331 	const uint64_t efirng_size =
332 	    round_page(efirng_end) - trunc_page(efirng_start);
333 	if (efirng_size > 0)
334 		fdt_memory_remove_range(trunc_page(efirng_start), efirng_size);
335 
336 	const int framebuffer = OF_finddevice("/chosen/framebuffer");
337 	if (framebuffer >= 0) {
338 		for (index = 0;
339 		     fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
340 		     index++) {
341 			fdt_add_reserved_memory_range(addr, size);
342 		}
343 	}
344 
345 	VPRINTF("Usable memory:\n");
346 	bc->dramblocks = 0;
347 	fdt_memory_foreach(fdt_add_dram_blocks, bc);
348 }
349 
350 static void
351 fdt_probe_range(const char *startname, const char *endname,
352     uint64_t *pstart, uint64_t *pend)
353 {
354 	int chosen, len;
355 	const void *start_data, *end_data;
356 
357 	*pstart = *pend = 0;
358 
359 	chosen = OF_finddevice("/chosen");
360 	if (chosen < 0)
361 		return;
362 
363 	start_data = fdtbus_get_prop(chosen, startname, &len);
364 	end_data = fdtbus_get_prop(chosen, endname, NULL);
365 	if (start_data == NULL || end_data == NULL)
366 		return;
367 
368 	switch (len) {
369 	case 4:
370 		*pstart = be32dec(start_data);
371 		*pend = be32dec(end_data);
372 		break;
373 	case 8:
374 		*pstart = be64dec(start_data);
375 		*pend = be64dec(end_data);
376 		break;
377 	default:
378 		printf("Unsupported len %d for /chosen `%s'\n",
379 		    len, startname);
380 		return;
381 	}
382 }
383 
384 static void *
385 fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize,
386     const char *purpose)
387 {
388 	const paddr_t startpa = trunc_page(start);
389 	const paddr_t endpa = round_page(end);
390 	paddr_t pa;
391 	vaddr_t va;
392 	void *ptr;
393 
394 	*psize = end - start;
395 	if (*psize == 0)
396 		return NULL;
397 
398 	const vaddr_t voff = start & PAGE_MASK;
399 
400 	va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY|UVM_KMF_NOWAIT);
401 	if (va == 0) {
402 		printf("Failed to allocate VA for %s\n", purpose);
403 		return NULL;
404 	}
405 	ptr = (void *)(va + voff);
406 
407 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
408 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
409 	pmap_update(pmap_kernel());
410 
411 	return ptr;
412 }
413 
414 static void
415 fdt_unmap_range(void *ptr, uint64_t size)
416 {
417 	const char *start = ptr, *end = start + size;
418 	const vaddr_t startva = trunc_page((vaddr_t)(uintptr_t)start);
419 	const vaddr_t endva = round_page((vaddr_t)(uintptr_t)end);
420 
421 	pmap_kremove(startva, endva - startva);
422 	pmap_update(pmap_kernel());
423 }
424 
425 static void
426 fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
427 {
428 	*pstart = *pend = 0;
429 
430 #ifdef MEMORY_DISK_DYNAMIC
431 	fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend);
432 #endif
433 }
434 
435 static void
436 fdt_setup_initrd(void)
437 {
438 #ifdef MEMORY_DISK_DYNAMIC
439 	void *md_start;
440 	uint64_t initrd_size;
441 
442 	md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size,
443 	    "initrd");
444 	if (md_start == NULL)
445 		return;
446 	md_root_setconf(md_start, initrd_size);
447 #endif
448 }
449 
450 static void
451 fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend)
452 {
453 
454 	fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end",
455 	    pstart, pend);
456 }
457 
458 static void
459 fdt_setup_rndseed(void)
460 {
461 	uint64_t rndseed_size;
462 	void *rndseed;
463 
464 	rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size,
465 	    "rndseed");
466 	if (rndseed == NULL)
467 		return;
468 	rnd_seed(rndseed, rndseed_size);
469 	fdt_unmap_range(rndseed, rndseed_size);
470 }
471 
472 static void
473 fdt_probe_efirng(uint64_t *pstart, uint64_t *pend)
474 {
475 
476 	fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end",
477 	    pstart, pend);
478 }
479 
480 static struct krndsource efirng_source;
481 
482 static void
483 fdt_setup_efirng(void)
484 {
485 	uint64_t efirng_size;
486 	void *efirng;
487 
488 	efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size,
489 	    "efirng");
490 	if (efirng == NULL)
491 		return;
492 
493 	rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG,
494 	    RND_FLAG_DEFAULT);
495 
496 	/*
497 	 * We don't really have specific information about the physical
498 	 * process underlying the data provided by the firmware via the
499 	 * EFI RNG API, so the entropy estimate here is heuristic.
500 	 * What efiboot provides us is up to 4096 bytes of data from
501 	 * the EFI RNG API, although in principle it may return short.
502 	 *
503 	 * The UEFI Specification (2.8 Errata A, February 2020[1]) says
504 	 *
505 	 *	When a Deterministic Random Bit Generator (DRBG) is
506 	 *	used on the output of a (raw) entropy source, its
507 	 *	security level must be at least 256 bits.
508 	 *
509 	 * It's not entirely clear whether `it' refers to the DRBG or
510 	 * the entropy source; if it refers to the DRBG, it's not
511 	 * entirely clear how ANSI X9.31 3DES, one of the options for
512 	 * DRBG in the UEFI spec, can provide a `256-bit security
513 	 * level' because it has only 232 bits of inputs (three 56-bit
514 	 * keys and one 64-bit block).  That said, even if it provides
515 	 * only 232 bits of entropy, that's enough to prevent all
516 	 * attacks and we probably get a few more bits from sampling
517 	 * the clock anyway.
518 	 *
519 	 * In the event we get raw samples, e.g. the bits sampled by a
520 	 * ring oscillator, we hope that the samples have at least half
521 	 * a bit of entropy per bit of data -- and efiboot tries to
522 	 * draw 4096 bytes to provide plenty of slop.  Hence we divide
523 	 * the total number of bits by two and clamp at 256.  There are
524 	 * ways this could go wrong, but on most machines it should
525 	 * behave reasonably.
526 	 *
527 	 * [1] https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_A_Feb14.pdf
528 	 */
529 	rnd_add_data(&efirng_source, efirng, efirng_size,
530 	    MIN(256, efirng_size*NBBY/2));
531 
532 	explicit_memset(efirng, 0, efirng_size);
533 	fdt_unmap_range(efirng, efirng_size);
534 }
535 
536 #ifdef EFI_RUNTIME
537 static void
538 fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type)
539 {
540 	int len;
541 
542 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
543 	if (chosen_off < 0)
544 		return;
545 
546 	const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len);
547 	if (map == NULL)
548 		return;
549 
550 	while (len >= 24) {
551 		const paddr_t pa = be64toh(map[0]);
552 		const vaddr_t va = be64toh(map[1]);
553 		const uint64_t sz = be64toh(map[2]);
554 		VPRINTF("%s: %s %lx-%lx (%lx-%lx)\n", __func__, prop, pa, pa+sz-1, va, va+sz-1);
555 		arm_efirt_md_map_range(va, pa, sz, type);
556 		map += 3;
557 		len -= 24;
558 	}
559 }
560 #endif
561 
562 vaddr_t
563 initarm(void *arg)
564 {
565 	const struct arm_platform *plat;
566 	uint64_t memory_start, memory_end;
567 
568 	/* set temporally to work printf()/panic() even before consinit() */
569 	cn_tab = &earlycons;
570 
571 	/* Load FDT */
572 	int error = fdt_check_header(fdt_addr_r);
573 	if (error != 0)
574 		panic("fdt_check_header failed: %s", fdt_strerror(error));
575 
576 	/* If the DTB is too big, try to pack it in place first. */
577 	if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
578 		(void)fdt_pack(__UNCONST(fdt_addr_r));
579 	error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
580 	if (error != 0)
581 		panic("fdt_move failed: %s", fdt_strerror(error));
582 
583 	fdtbus_init(fdt_data);
584 
585 	/* Lookup platform specific backend */
586 	plat = arm_fdt_platform();
587 	if (plat == NULL)
588 		panic("Kernel does not support this device");
589 
590 	/* Early console may be available, announce ourselves. */
591 	VPRINTF("FDT<%p>\n", fdt_addr_r);
592 
593 	const int chosen = OF_finddevice("/chosen");
594 	if (chosen >= 0)
595 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
596 	boot_args = bootargs;
597 
598 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
599 	VPRINTF("cpufunc\n");
600 	if (set_cpufuncs())
601 		panic("cpu not recognized!");
602 
603 	/*
604 	 * Memory is still identity/flat mapped this point so using ttbr for
605 	 * l1pt VA is fine
606 	 */
607 
608 	VPRINTF("devmap %p\n", plat->ap_devmap());
609 	extern char ARM_BOOTSTRAP_LxPT[];
610 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap());
611 
612 	VPRINTF("bootstrap\n");
613 	plat->ap_bootstrap();
614 
615 	/*
616 	 * If stdout-path is specified on the command line, override the
617 	 * value in /chosen/stdout-path before initializing console.
618 	 */
619 	VPRINTF("stdout\n");
620 	fdt_update_stdout_path();
621 
622 #if BYTE_ORDER == BIG_ENDIAN
623 	/*
624 	 * Most boards are configured to little-endian mode in initial, and
625 	 * switched to big-endian mode after kernel is loaded. In this case,
626 	 * framebuffer seems byte-swapped to CPU. Override FDT to let
627 	 * drivers know.
628 	 */
629 	VPRINTF("fb_format\n");
630 	fdt_update_fb_format();
631 #endif
632 
633 	/*
634 	 * Done making changes to the FDT.
635 	 */
636 	fdt_pack(fdt_data);
637 
638 	VPRINTF("consinit ");
639 	consinit();
640 	VPRINTF("ok\n");
641 
642 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
643 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
644 
645 	cpu_reset_address = fdt_reset;
646 	cpu_powerdown_address = fdt_powerdown;
647 	evbarm_device_register = fdt_device_register;
648 	evbarm_device_register_post_config = fdt_device_register_post_config;
649 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
650 
651 	/* Talk to the user */
652 	printf("NetBSD/evbarm (fdt) booting ...\n");
653 
654 #ifdef BOOT_ARGS
655 	char mi_bootargs[] = BOOT_ARGS;
656 	parse_mi_bootargs(mi_bootargs);
657 #endif
658 
659 	fdt_get_memory(&memory_start, &memory_end);
660 
661 #if !defined(_LP64)
662 	/* Cannot map memory above 4GB (remove last page as well) */
663 	const uint64_t memory_limit = 0x100000000ULL - PAGE_SIZE;
664 	if (memory_end > memory_limit)
665 		memory_end = memory_limit;
666 #endif
667 	uint64_t memory_size = memory_end - memory_start;
668 
669 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
670 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
671 
672 	/* Parse ramdisk info */
673 	fdt_probe_initrd(&initrd_start, &initrd_end);
674 
675 	/* Parse our on-disk rndseed and the firmware's RNG from EFI */
676 	fdt_probe_rndseed(&rndseed_start, &rndseed_end);
677 	fdt_probe_efirng(&efirng_start, &efirng_end);
678 
679 	/*
680 	 * Populate bootconfig structure for the benefit of dodumpsys
681 	 */
682 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
683 	fdt_build_bootconfig(memory_start, memory_end);
684 
685 #ifdef EFI_RUNTIME
686 	fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
687 	fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
688 	fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
689 #endif
690 
691 	/* Perform PT build and VM init */
692 	cpu_kernel_vm_init(memory_start, memory_size);
693 
694 	VPRINTF("bootargs: %s\n", bootargs);
695 
696 	parse_mi_bootargs(boot_args);
697 
698 	VPRINTF("Memory regions:\n");
699 	fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
700 
701 	vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
702 	     nfdt_physmem);
703 
704 	/*
705 	 * initarm_common flushes cache if required before AP start
706 	 */
707 	error = 0;
708 	if ((boothowto & RB_MD1) == 0) {
709 		VPRINTF("mpstart\n");
710 		if (plat->ap_mpstart)
711 			error = plat->ap_mpstart();
712 	}
713 
714 	if (error)
715 		return sp;
716 
717 	/*
718 	 * Now we have APs started the pages used for stacks and L1PT can
719 	 * be given to uvm
720 	 */
721 	extern char const __start__init_memory[];
722 	extern char const __stop__init_memory[] __weak;
723 
724 	if (__start__init_memory != __stop__init_memory) {
725 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
726 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
727 		const paddr_t spg = atop(spa);
728 		const paddr_t epg = atop(epa);
729 
730 		VPRINTF("         start %08lx  end %08lx... "
731 		    "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
732 
733 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
734 
735 	}
736 
737 	return sp;
738 }
739 
740 static void
741 fdt_update_stdout_path(void)
742 {
743 	char *stdout_path, *ep;
744 	int stdout_path_len;
745 	char buf[256];
746 
747 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
748 	if (chosen_off == -1)
749 		return;
750 
751 	if (get_bootconf_option(boot_args, "stdout-path",
752 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
753 		return;
754 
755 	ep = strchr(stdout_path, ' ');
756 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
757 	if (stdout_path_len >= sizeof(buf))
758 		return;
759 
760 	strncpy(buf, stdout_path, stdout_path_len);
761 	buf[stdout_path_len] = '\0';
762 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
763 	    buf, stdout_path_len + 1);
764 }
765 
766 void
767 consinit(void)
768 {
769 	static bool initialized = false;
770 	const struct arm_platform *plat = arm_fdt_platform();
771 	const struct fdt_console *cons = fdtbus_get_console();
772 	struct fdt_attach_args faa;
773 	u_int uart_freq = 0;
774 
775 	if (initialized || cons == NULL)
776 		return;
777 
778 	plat->ap_init_attach_args(&faa);
779 	faa.faa_phandle = fdtbus_get_stdout_phandle();
780 
781 	if (plat->ap_uart_freq != NULL)
782 		uart_freq = plat->ap_uart_freq();
783 
784 	cons->consinit(&faa, uart_freq);
785 
786 	initialized = true;
787 }
788 
789 void
790 cpu_startup_hook(void)
791 {
792 
793 	fdtbus_intr_init();
794 
795 	fdt_setup_rndseed();
796 	fdt_setup_efirng();
797 }
798 
799 void
800 delay(u_int us)
801 {
802 	const struct arm_platform *plat = arm_fdt_platform();
803 
804 	plat->ap_delay(us);
805 }
806 
807 static void
808 fdt_detect_root_device(device_t dev)
809 {
810 	struct mbr_sector mbr;
811 	uint8_t buf[DEV_BSIZE];
812 	uint8_t hash[16];
813 	const uint8_t *rhash;
814 	char rootarg[64];
815 	struct vnode *vp;
816 	MD5_CTX md5ctx;
817 	int error, len;
818 	size_t resid;
819 	u_int part;
820 
821 	const int chosen = OF_finddevice("/chosen");
822 	if (chosen < 0)
823 		return;
824 
825 	if (of_hasprop(chosen, "netbsd,mbr") &&
826 	    of_hasprop(chosen, "netbsd,partition")) {
827 
828 		/*
829 		 * The bootloader has passed in a partition index and MD5 hash
830 		 * of the MBR sector. Read the MBR of this device, calculate the
831 		 * hash, and compare it with the value passed in.
832 		 */
833 		rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
834 		if (rhash == NULL || len != 16)
835 			return;
836 		of_getprop_uint32(chosen, "netbsd,partition", &part);
837 		if (part >= MAXPARTITIONS)
838 			return;
839 
840 		vp = opendisk(dev);
841 		if (!vp)
842 			return;
843 		error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
844 		    0, NOCRED, &resid, NULL);
845 		VOP_CLOSE(vp, FREAD, NOCRED);
846 		vput(vp);
847 
848 		if (error != 0)
849 			return;
850 
851 		memcpy(&mbr, buf, sizeof(mbr));
852 		MD5Init(&md5ctx);
853 		MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
854 		MD5Final(hash, &md5ctx);
855 
856 		if (memcmp(rhash, hash, 16) != 0)
857 			return;
858 
859 		snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a');
860 		strcat(boot_args, rootarg);
861 	}
862 
863 	if (of_hasprop(chosen, "netbsd,gpt-guid")) {
864 		char guidbuf[UUID_STR_LEN];
865 		const struct uuid *guid = fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len);
866 		if (guid == NULL || len != 16)
867 			return;
868 
869 		uuid_snprintf(guidbuf, sizeof(guidbuf), guid);
870 		snprintf(rootarg, sizeof(rootarg), " root=wedge:%s", guidbuf);
871 		strcat(boot_args, rootarg);
872 	}
873 
874 	if (of_hasprop(chosen, "netbsd,gpt-label")) {
875 		const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label");
876 		if (label == NULL || *label == '\0')
877 			return;
878 
879 		device_t dv = dkwedge_find_by_wname(label);
880 		if (dv != NULL)
881 			booted_device = dv;
882 	}
883 
884 	if (of_hasprop(chosen, "netbsd,booted-mac-address")) {
885 		const uint8_t *macaddr = fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len);
886 		if (macaddr == NULL || len != 6)
887 			return;
888 		int s = pserialize_read_enter();
889 		struct ifnet *ifp;
890 		IFNET_READER_FOREACH(ifp) {
891 			if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) {
892 				device_t dv = device_find_by_xname(ifp->if_xname);
893 				if (dv != NULL)
894 					booted_device = dv;
895 				break;
896 			}
897 		}
898 		pserialize_read_exit(s);
899 	}
900 }
901 
902 static void
903 fdt_device_register(device_t self, void *aux)
904 {
905 	const struct arm_platform *plat = arm_fdt_platform();
906 
907 	if (device_is_a(self, "armfdt")) {
908 		fdt_setup_initrd();
909 
910 #if NWSDISPLAY > 0 && NGENFB > 0
911 		/*
912 		 * Setup framebuffer console, if present.
913 		 */
914 		arm_simplefb_preattach();
915 #endif
916 	}
917 
918 #if NWSDISPLAY > 0 && NGENFB > 0
919 	if (device_is_a(self, "genfb")) {
920 		prop_dictionary_t dict = device_properties(self);
921 		prop_dictionary_set_uint64(dict,
922 		    "simplefb-physaddr", arm_simplefb_physaddr());
923 	}
924 #endif
925 
926 	if (plat && plat->ap_device_register)
927 		plat->ap_device_register(self, aux);
928 }
929 
930 static void
931 fdt_device_register_post_config(device_t self, void *aux)
932 {
933 #if NUKBD > 0 && NWSDISPLAY > 0
934 	if (device_is_a(self, "wsdisplay")) {
935 		struct wsdisplay_softc *sc = device_private(self);
936 		if (wsdisplay_isconsole(sc))
937 			ukbd_cnattach();
938 	}
939 #endif
940 }
941 
942 static void
943 fdt_cpu_rootconf(void)
944 {
945 	device_t dev;
946 	deviter_t di;
947 	char *ptr;
948 
949 	for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
950 		if (device_class(dev) != DV_DISK)
951 			continue;
952 
953 		if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0)
954 			break;
955 
956 		if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd"))
957 			fdt_detect_root_device(dev);
958 	}
959 	deviter_release(&di);
960 }
961 
962 static void
963 fdt_reset(void)
964 {
965 	const struct arm_platform *plat = arm_fdt_platform();
966 
967 	fdtbus_power_reset();
968 
969 	if (plat && plat->ap_reset)
970 		plat->ap_reset();
971 }
972 
973 static void
974 fdt_powerdown(void)
975 {
976 	fdtbus_power_poweroff();
977 }
978 
979 #if BYTE_ORDER == BIG_ENDIAN
980 static void
981 fdt_update_fb_format(void)
982 {
983 	int off, len;
984 	const char *format, *replace;
985 
986 	off = fdt_path_offset(fdt_data, "/chosen");
987 	if (off < 0)
988 		return;
989 
990 	for (;;) {
991 		off = fdt_node_offset_by_compatible(fdt_data, off,
992 		    "simple-framebuffer");
993 		if (off < 0)
994 			return;
995 
996 		format = fdt_getprop(fdt_data, off, "format", &len);
997 		if (format == NULL)
998 			continue;
999 
1000 		replace = NULL;
1001 		if (strcmp(format, "a8b8g8r8") == 0)
1002 			replace = "r8g8b8a8";
1003 		else if (strcmp(format, "x8r8g8b8") == 0)
1004 			replace = "b8g8r8x8";
1005 		if (replace != NULL)
1006 			fdt_setprop(fdt_data, off, "format", replace,
1007 			    strlen(replace) + 1);
1008 	}
1009 }
1010 #endif
1011