fdt_machdep.c revision 1.104 1 /* $NetBSD: fdt_machdep.c,v 1.104 2023/07/10 07:00:12 rin 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.104 2023/07/10 07:00:12 rin 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_multiprocessor.h"
39
40 #include "genfb.h"
41 #include "ukbd.h"
42 #include "wsdisplay.h"
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46
47 #include <sys/atomic.h>
48 #include <sys/bootblock.h>
49 #include <sys/bus.h>
50 #include <sys/conf.h>
51 #include <sys/cpu.h>
52 #include <sys/device.h>
53 #include <sys/disk.h>
54 #include <sys/disklabel.h>
55 #include <sys/endian.h>
56 #include <sys/exec.h>
57 #include <sys/fcntl.h>
58 #include <sys/kauth.h>
59 #include <sys/kernel.h>
60 #include <sys/kmem.h>
61 #include <sys/ksyms.h>
62 #include <sys/md5.h>
63 #include <sys/msgbuf.h>
64 #include <sys/proc.h>
65 #include <sys/pserialize.h>
66 #include <sys/reboot.h>
67 #include <sys/systm.h>
68 #include <sys/termios.h>
69 #include <sys/vnode.h>
70 #include <sys/uuid.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 <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
91 #include <arm/fdt/arm_fdtvar.h>
92
93 #include <dev/fdt/fdtvar.h>
94 #include <dev/fdt/fdt_boot.h>
95 #include <dev/fdt/fdt_private.h>
96 #include <dev/fdt/fdt_memory.h>
97
98 #ifdef EFI_RUNTIME
99 #include <arm/arm/efi_runtime.h>
100 #endif
101
102 #if NWSDISPLAY > 0 && NGENFB > 0
103 #include <arm/fdt/arm_simplefb.h>
104 #endif
105
106 #if NUKBD > 0
107 #include <dev/usb/ukbdvar.h>
108 #endif
109 #if NWSDISPLAY > 0
110 #include <dev/wscons/wsdisplayvar.h>
111 #endif
112
113 #ifndef FDT_MAX_BOOT_STRING
114 #define FDT_MAX_BOOT_STRING 1024
115 #endif
116
117 BootConfig bootconfig;
118 char bootargs[FDT_MAX_BOOT_STRING] = "";
119 char *boot_args = NULL;
120
121 /* filled in before cleaning bss. keep in .data */
122 u_long uboot_args[4] __attribute__((__section__(".data")));
123 const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
124
125 #include <libfdt.h>
126 #include <dev/fdt/fdtvar.h>
127 #define FDT_BUF_SIZE (512*1024)
128 static uint8_t fdt_data[FDT_BUF_SIZE];
129
130 extern char KERNEL_BASE_phys[];
131 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
132
133 static void fdt_device_register(device_t, void *);
134 static void fdt_device_register_post_config(device_t, void *);
135 static void fdt_cpu_rootconf(void);
136 static void fdt_reset(void);
137 static void fdt_powerdown(void);
138
139 #if BYTE_ORDER == BIG_ENDIAN
140 static void fdt_update_fb_format(void);
141 #endif
142
143 static void
144 earlyconsputc(dev_t dev, int c)
145 {
146 uartputc(c);
147 }
148
149 static int
150 earlyconsgetc(dev_t dev)
151 {
152 return -1;
153 }
154
155 static struct consdev earlycons = {
156 .cn_putc = earlyconsputc,
157 .cn_getc = earlyconsgetc,
158 .cn_pollc = nullcnpollc,
159 };
160
161 #ifdef VERBOSE_INIT_ARM
162 #define VPRINTF(...) printf(__VA_ARGS__)
163 #else
164 #define VPRINTF(...) __nothing
165 #endif
166
167 static void
168 fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
169 {
170 BootConfig *bc = arg;
171
172 VPRINTF(" %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
173 bc->dram[bc->dramblocks].address = m->start;
174 bc->dram[bc->dramblocks].pages =
175 (m->end - m->start) / PAGE_SIZE;
176 bc->dramblocks++;
177 }
178
179 static int nfdt_physmem = 0;
180 static struct boot_physmem fdt_physmem[FDT_MEMORY_RANGES];
181
182 static void
183 fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
184 {
185 const paddr_t saddr = round_page(m->start);
186 const paddr_t eaddr = trunc_page(m->end);
187
188 VPRINTF(" %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
189 if (saddr >= eaddr) {
190 VPRINTF(" skipped\n");
191 return;
192 }
193 VPRINTF("\n");
194
195 struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
196
197 KASSERT(nfdt_physmem <= FDT_MEMORY_RANGES);
198
199 bp->bp_start = atop(saddr);
200 bp->bp_pages = atop(eaddr) - bp->bp_start;
201 bp->bp_freelist = VM_FREELIST_DEFAULT;
202
203 #ifdef PMAP_NEED_ALLOC_POOLPAGE
204 const uint64_t memory_size = *(uint64_t *)arg;
205 if (atop(memory_size) > bp->bp_pages) {
206 arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
207 bp->bp_freelist = VM_FREELIST_DIRECTMAP;
208 }
209 #endif
210 }
211
212
213 static void
214 fdt_print_memory(const struct fdt_memory *m, void *arg)
215 {
216
217 VPRINTF("FDT /memory @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
218 m->start, m->end - m->start);
219 }
220
221
222 /*
223 * Define usable memory regions.
224 */
225 static void
226 fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
227 {
228 BootConfig *bc = &bootconfig;
229
230 uint64_t addr, size;
231 int index;
232
233 /* Reserve pages for ramdisk, rndseed, and firmware's RNG */
234 fdt_reserve_initrd();
235 fdt_reserve_rndseed();
236 fdt_reserve_efirng();
237
238 const int framebuffer = OF_finddevice("/chosen/framebuffer");
239 if (framebuffer >= 0) {
240 for (index = 0;
241 fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
242 index++) {
243 fdt_memory_remove_range(addr, size);
244 }
245 }
246
247 VPRINTF("Usable memory:\n");
248 bc->dramblocks = 0;
249 fdt_memory_foreach(fdt_add_dram_blocks, bc);
250 }
251
252
253 vaddr_t
254 initarm(void *arg)
255 {
256 const struct fdt_platform *plat;
257 uint64_t memory_start, memory_end;
258
259 /* set temporally to work printf()/panic() even before consinit() */
260 cn_tab = &earlycons;
261
262 /* Load FDT */
263 int error = fdt_check_header(fdt_addr_r);
264 if (error != 0)
265 panic("fdt_check_header failed: %s", fdt_strerror(error));
266
267 /* If the DTB is too big, try to pack it in place first. */
268 if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
269 (void)fdt_pack(__UNCONST(fdt_addr_r));
270
271 error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
272 if (error != 0)
273 panic("fdt_move failed: %s", fdt_strerror(error));
274
275 fdtbus_init(fdt_data);
276
277 /* Lookup platform specific backend */
278 plat = fdt_platform_find();
279 if (plat == NULL)
280 panic("Kernel does not support this device");
281
282 /* Early console may be available, announce ourselves. */
283 VPRINTF("FDT<%p>\n", fdt_addr_r);
284
285 const int chosen = OF_finddevice("/chosen");
286 if (chosen >= 0)
287 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
288 boot_args = bootargs;
289
290 /* Heads up ... Setup the CPU / MMU / TLB functions. */
291 VPRINTF("cpufunc\n");
292 if (set_cpufuncs())
293 panic("cpu not recognized!");
294
295 /*
296 * Memory is still identity/flat mapped this point so using ttbr for
297 * l1pt VA is fine
298 */
299
300 VPRINTF("devmap %p\n", plat->fp_devmap());
301 extern char ARM_BOOTSTRAP_LxPT[];
302 pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->fp_devmap());
303
304 VPRINTF("bootstrap\n");
305 plat->fp_bootstrap();
306
307 /*
308 * If stdout-path is specified on the command line, override the
309 * value in /chosen/stdout-path before initializing console.
310 */
311 VPRINTF("stdout\n");
312 fdt_update_stdout_path(fdt_data, boot_args);
313
314 #if BYTE_ORDER == BIG_ENDIAN
315 /*
316 * Most boards are configured to little-endian mode initially, and
317 * switched to big-endian mode after kernel is loaded. In this case,
318 * framebuffer seems byte-swapped to CPU. Override FDT to let
319 * drivers know.
320 */
321 VPRINTF("fb_format\n");
322 fdt_update_fb_format();
323 #endif
324
325 /*
326 * Done making changes to the FDT.
327 */
328 fdt_pack(fdt_data);
329
330 VPRINTF("consinit ");
331 consinit();
332 VPRINTF("ok\n");
333
334 VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
335 uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
336
337 cpu_reset_address = fdt_reset;
338 cpu_powerdown_address = fdt_powerdown;
339 evbarm_device_register = fdt_device_register;
340 evbarm_device_register_post_config = fdt_device_register_post_config;
341 evbarm_cpu_rootconf = fdt_cpu_rootconf;
342
343 /* Talk to the user */
344 printf("NetBSD/evbarm (fdt) booting ...\n");
345
346 #ifdef BOOT_ARGS
347 char mi_bootargs[] = BOOT_ARGS;
348 parse_mi_bootargs(mi_bootargs);
349 #endif
350
351 fdt_memory_get(&memory_start, &memory_end);
352
353 fdt_memory_foreach(fdt_print_memory, NULL);
354
355 #if !defined(_LP64)
356 /* Cannot map memory above 4GB (remove last page as well) */
357 const uint64_t memory_limit = 0x100000000ULL - PAGE_SIZE;
358 if (memory_end > memory_limit) {
359 fdt_memory_remove_range(memory_limit , memory_end);
360 memory_end = memory_limit;
361 }
362 #endif
363 uint64_t memory_size = memory_end - memory_start;
364
365 VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
366 PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
367
368 /* Parse ramdisk info */
369 fdt_probe_initrd();
370
371 /* Parse our on-disk rndseed and the firmware's RNG from EFI */
372 fdt_probe_rndseed();
373 fdt_probe_efirng();
374
375 fdt_memory_remove_reserved(memory_start, memory_end);
376
377 /*
378 * Populate bootconfig structure for the benefit of dodumpsys
379 */
380 VPRINTF("%s: fdt_build_bootconfig\n", __func__);
381 fdt_build_bootconfig(memory_start, memory_end);
382
383 /* Perform PT build and VM init */
384 cpu_kernel_vm_init(memory_start, memory_size);
385
386 VPRINTF("bootargs: %s\n", bootargs);
387
388 parse_mi_bootargs(boot_args);
389
390 VPRINTF("Memory regions:\n");
391
392 /* Populate fdt_physmem / nfdt_physmem for initarm_common */
393 fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
394
395 vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
396 nfdt_physmem);
397
398 /*
399 * initarm_common flushes cache if required before AP start
400 */
401 error = 0;
402 if ((boothowto & RB_MD1) == 0) {
403 VPRINTF("mpstart\n");
404 if (plat->fp_mpstart)
405 error = plat->fp_mpstart();
406 }
407
408 if (error)
409 return sp;
410
411 /*
412 * Now we have APs started the pages used for stacks and L1PT can
413 * be given to uvm
414 */
415 extern char const __start__init_memory[];
416 extern char const __stop__init_memory[] __weak;
417
418 if (__start__init_memory != __stop__init_memory) {
419 const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
420 const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
421 const paddr_t spg = atop(spa);
422 const paddr_t epg = atop(epa);
423
424 VPRINTF(" start %08lx end %08lx... "
425 "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
426
427 uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
428 }
429
430 return sp;
431 }
432
433 void
434 consinit(void)
435 {
436 static bool initialized = false;
437 const struct fdt_platform *plat = fdt_platform_find();
438 const struct fdt_console *cons = fdtbus_get_console();
439 struct fdt_attach_args faa;
440 u_int uart_freq = 0;
441
442 if (initialized || cons == NULL)
443 return;
444
445 plat->fp_init_attach_args(&faa);
446 faa.faa_phandle = fdtbus_get_stdout_phandle();
447
448 if (plat->fp_uart_freq != NULL)
449 uart_freq = plat->fp_uart_freq();
450
451 cons->consinit(&faa, uart_freq);
452
453 initialized = true;
454 }
455
456 void
457 cpu_startup_hook(void)
458 {
459 #ifdef EFI_RUNTIME
460 fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
461 fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
462 fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
463 #endif
464
465 fdtbus_intr_init();
466
467 fdt_setup_rndseed();
468 fdt_setup_efirng();
469 }
470
471 void
472 delay(u_int us)
473 {
474 const struct fdt_platform *plat = fdt_platform_find();
475
476 plat->fp_delay(us);
477 }
478
479 static void
480 fdt_detect_root_device(device_t dev)
481 {
482 int error, len;
483
484 const int chosen = OF_finddevice("/chosen");
485 if (chosen < 0)
486 return;
487
488 if (of_hasprop(chosen, "netbsd,mbr") &&
489 of_hasprop(chosen, "netbsd,partition")) {
490 struct mbr_sector mbr;
491 uint8_t buf[DEV_BSIZE];
492 uint8_t hash[16];
493 const uint8_t *rhash;
494 struct vnode *vp;
495 MD5_CTX md5ctx;
496 size_t resid;
497 u_int part;
498
499 /*
500 * The bootloader has passed in a partition index and MD5 hash
501 * of the MBR sector. Read the MBR of this device, calculate the
502 * hash, and compare it with the value passed in.
503 */
504 rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
505 if (rhash == NULL || len != 16)
506 return;
507 of_getprop_uint32(chosen, "netbsd,partition", &part);
508 if (part >= MAXPARTITIONS)
509 return;
510
511 vp = opendisk(dev);
512 if (!vp)
513 return;
514 error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
515 IO_NODELOCKED, NOCRED, &resid, NULL);
516 VOP_CLOSE(vp, FREAD, NOCRED);
517 vput(vp);
518
519 if (error != 0)
520 return;
521
522 memcpy(&mbr, buf, sizeof(mbr));
523 MD5Init(&md5ctx);
524 MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
525 MD5Final(hash, &md5ctx);
526
527 if (memcmp(rhash, hash, 16) == 0) {
528 booted_device = dev;
529 booted_partition = part;
530 }
531
532 return;
533 }
534
535 if (of_hasprop(chosen, "netbsd,gpt-guid")) {
536 const struct uuid *guid =
537 fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len);
538
539 if (guid == NULL || len != 16)
540 return;
541
542 char guidstr[UUID_STR_LEN];
543 uuid_snprintf(guidstr, sizeof(guidstr), guid);
544
545 device_t dv = dkwedge_find_by_wname(guidstr);
546 if (dv != NULL)
547 booted_device = dv;
548
549 return;
550 }
551
552 if (of_hasprop(chosen, "netbsd,gpt-label")) {
553 const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label");
554 if (label == NULL || *label == '\0')
555 return;
556
557 device_t dv = dkwedge_find_by_wname(label);
558 if (dv != NULL)
559 booted_device = dv;
560
561 return;
562 }
563
564 if (of_hasprop(chosen, "netbsd,booted-mac-address")) {
565 const uint8_t *macaddr =
566 fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len);
567 struct ifnet *ifp;
568
569 if (macaddr == NULL || len != 6)
570 return;
571
572 int s = pserialize_read_enter();
573 IFNET_READER_FOREACH(ifp) {
574 if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) {
575 device_t dv = device_find_by_xname(ifp->if_xname);
576 if (dv != NULL)
577 booted_device = dv;
578 break;
579 }
580 }
581 pserialize_read_exit(s);
582
583 return;
584 }
585 }
586
587 static void
588 fdt_device_register(device_t self, void *aux)
589 {
590 const struct fdt_platform *plat = fdt_platform_find();
591
592 if (device_is_a(self, "armfdt")) {
593 fdt_setup_initrd();
594
595 #if NWSDISPLAY > 0 && NGENFB > 0
596 /*
597 * Setup framebuffer console, if present.
598 */
599 arm_simplefb_preattach();
600 #endif
601 }
602
603 #if NWSDISPLAY > 0 && NGENFB > 0
604 if (device_is_a(self, "genfb")) {
605 prop_dictionary_t dict = device_properties(self);
606 prop_dictionary_set_uint64(dict,
607 "simplefb-physaddr", arm_simplefb_physaddr());
608 }
609 #endif
610
611 if (plat && plat->fp_device_register)
612 plat->fp_device_register(self, aux);
613 }
614
615 static void
616 fdt_device_register_post_config(device_t self, void *aux)
617 {
618 #if NUKBD > 0 && NWSDISPLAY > 0
619 if (device_is_a(self, "wsdisplay")) {
620 struct wsdisplay_softc *sc = device_private(self);
621 if (wsdisplay_isconsole(sc))
622 ukbd_cnattach();
623 }
624 #endif
625 }
626
627 static void
628 fdt_cpu_rootconf(void)
629 {
630 device_t dev;
631 deviter_t di;
632
633 if (booted_device != NULL)
634 return;
635
636 for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
637 if (device_class(dev) != DV_DISK)
638 continue;
639
640 fdt_detect_root_device(dev);
641
642 if (booted_device != NULL)
643 break;
644 }
645 deviter_release(&di);
646 }
647
648 static void
649 fdt_reset(void)
650 {
651 const struct fdt_platform *plat = fdt_platform_find();
652
653 fdtbus_power_reset();
654
655 if (plat && plat->fp_reset)
656 plat->fp_reset();
657 }
658
659 static void
660 fdt_powerdown(void)
661 {
662 fdtbus_power_poweroff();
663 }
664
665 #if BYTE_ORDER == BIG_ENDIAN
666 static void
667 fdt_update_fb_format(void)
668 {
669 int off, len;
670 const char *format, *replace;
671
672 off = fdt_path_offset(fdt_data, "/chosen");
673 if (off < 0)
674 return;
675
676 for (;;) {
677 off = fdt_node_offset_by_compatible(fdt_data, off,
678 "simple-framebuffer");
679 if (off < 0)
680 return;
681
682 format = fdt_getprop(fdt_data, off, "format", &len);
683 if (format == NULL)
684 continue;
685
686 replace = NULL;
687 if (strcmp(format, "a8b8g8r8") == 0)
688 replace = "r8g8b8a8";
689 else if (strcmp(format, "x8r8g8b8") == 0)
690 replace = "b8g8r8x8";
691 if (replace != NULL)
692 fdt_setprop(fdt_data, off, "format", replace,
693 strlen(replace) + 1);
694 }
695 }
696 #endif
697