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