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