fdt_machdep.c revision 1.28 1 /* $NetBSD: fdt_machdep.c,v 1.28 2018/08/03 07:44:31 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.28 2018/08/03 07:44:31 skrll 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
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>
45 #include <sys/atomic.h>
46 #include <sys/cpu.h>
47 #include <sys/device.h>
48 #include <sys/exec.h>
49 #include <sys/kernel.h>
50 #include <sys/kmem.h>
51 #include <sys/ksyms.h>
52 #include <sys/msgbuf.h>
53 #include <sys/proc.h>
54 #include <sys/reboot.h>
55 #include <sys/termios.h>
56 #include <sys/extent.h>
57
58 #include <dev/cons.h>
59 #include <uvm/uvm_extern.h>
60
61 #include <sys/conf.h>
62
63 #include <machine/db_machdep.h>
64 #include <ddb/db_sym.h>
65 #include <ddb/db_extern.h>
66
67 #include <machine/bootconfig.h>
68 #include <arm/armreg.h>
69
70 #include <arm/cpufunc.h>
71 #ifdef __aarch64__
72 #include <aarch64/machdep.h>
73 #else
74 #include <arm/arm32/machdep.h>
75 #endif
76
77
78 #include <evbarm/include/autoconf.h>
79 #include <evbarm/fdt/platform.h>
80
81 #include <arm/fdt/arm_fdtvar.h>
82
83 #if NUKBD > 0
84 #include <dev/usb/ukbdvar.h>
85 #endif
86
87 #ifdef MEMORY_DISK_DYNAMIC
88 #include <dev/md.h>
89 #endif
90
91 #ifndef FDT_MAX_BOOT_STRING
92 #define FDT_MAX_BOOT_STRING 1024
93 #endif
94
95 BootConfig bootconfig;
96 char bootargs[FDT_MAX_BOOT_STRING] = "";
97 char *boot_args = NULL;
98
99 /* filled in before cleaning bss. keep in .data */
100 u_long uboot_args[4] __attribute__((__section__(".data")));
101 const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
102
103 static char fdt_memory_ext_storage[EXTENT_FIXED_STORAGE_SIZE(DRAM_BLOCKS)];
104 static struct extent *fdt_memory_ext;
105
106 static uint64_t initrd_start, initrd_end;
107
108 #include <libfdt.h>
109 #include <dev/fdt/fdtvar.h>
110 #define FDT_BUF_SIZE (256*1024)
111 static uint8_t fdt_data[FDT_BUF_SIZE];
112
113 extern char KERNEL_BASE_phys[];
114 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
115
116 static void fdt_update_stdout_path(void);
117 static void fdt_device_register(device_t, void *);
118 static void fdt_reset(void);
119 static void fdt_powerdown(void);
120
121 static dev_type_cnputc(earlyconsputc);
122 static dev_type_cngetc(earlyconsgetc);
123
124 static struct consdev earlycons = {
125 .cn_putc = earlyconsputc,
126 .cn_getc = earlyconsgetc,
127 .cn_pollc = nullcnpollc,
128 };
129
130 static void
131 fdt_putchar(char c)
132 {
133 const struct arm_platform *plat = arm_fdt_platform();
134 if (plat && plat->early_putchar) {
135 plat->early_putchar(c);
136 }
137 #ifdef EARLYCONS
138 else {
139 #define PLATFORM_EARLY_PUTCHAR ___CONCAT(EARLYCONS, _platform_early_putchar)
140 void PLATFORM_EARLY_PUTCHAR(char);
141 PLATFORM_EARLY_PUTCHAR(c);
142 }
143 #endif
144 }
145
146 static void
147 earlyconsputc(dev_t dev, int c)
148 {
149 fdt_putchar(c);
150 }
151
152 static int
153 earlyconsgetc(dev_t dev)
154 {
155 return 0; /* XXX */
156 }
157
158 #ifdef VERBOSE_INIT_ARM
159 #define DPRINTF(...) printf(__VA_ARGS__)
160 #else
161 #define DPRINTF(...)
162 #endif
163
164 /*
165 * ARM: Get the first physically contiguous region of memory.
166 * ARM64: 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 DPRINTF("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 DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
189 index, cur_addr, cur_size);
190
191 #ifdef __aarch64__
192 if (cur_addr + cur_size > *pend)
193 *pend = cur_addr + cur_size;
194 #else
195 /* If subsequent entries follow the previous, append them. */
196 if (*pend == cur_addr)
197 *pend = cur_addr + cur_size;
198 #endif
199 }
200 }
201
202 void
203 fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
204 {
205 uint64_t start = trunc_page(addr);
206 uint64_t end = round_page(addr + size);
207
208 int error = extent_free(fdt_memory_ext, start,
209 end - start, EX_NOWAIT);
210 if (error != 0)
211 printf("MEM ERROR: res %" PRIx64 "-%" PRIx64 " failed: %d\n",
212 start, end, error);
213 else
214 DPRINTF("MEM: res %" PRIx64 "-%" PRIx64 "\n", start, end);
215 }
216
217 /*
218 * Exclude memory ranges from memory config from the device tree
219 */
220 static void
221 fdt_add_reserved_memory(uint64_t max_addr)
222 {
223 uint64_t addr, size;
224 int index, error;
225
226 const int num = fdt_num_mem_rsv(fdtbus_get_data());
227 for (index = 0; index <= num; index++) {
228 error = fdt_get_mem_rsv(fdtbus_get_data(), index,
229 &addr, &size);
230 if (error != 0 || size == 0)
231 continue;
232 if (addr >= max_addr)
233 continue;
234 if (addr + size > max_addr)
235 size = max_addr - addr;
236 fdt_add_reserved_memory_range(addr, size);
237 }
238 }
239
240 /*
241 * Define usable memory regions.
242 */
243 static void
244 fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
245 {
246 const int memory = OF_finddevice("/memory");
247 BootConfig *bc = &bootconfig;
248 struct extent_region *er;
249 uint64_t addr, size;
250 int index, error;
251
252 fdt_memory_ext = extent_create("FDT Memory", mem_start, mem_end,
253 fdt_memory_ext_storage, sizeof(fdt_memory_ext_storage), EX_EARLY);
254
255 for (index = 0;
256 fdtbus_get_reg64(memory, index, &addr, &size) == 0;
257 index++) {
258 if (addr >= mem_end || size == 0)
259 continue;
260 if (addr + size > mem_end)
261 size = mem_end - addr;
262
263 error = extent_alloc_region(fdt_memory_ext, addr, size,
264 EX_NOWAIT);
265 if (error != 0)
266 printf("MEM ERROR: add %" PRIx64 "-%" PRIx64 " failed: %d\n",
267 addr, addr + size, error);
268 DPRINTF("MEM: add %" PRIx64 "-%" PRIx64 "\n", addr, addr + size);
269 }
270
271 fdt_add_reserved_memory(mem_end);
272
273 const uint64_t initrd_size = initrd_end - initrd_start;
274 if (initrd_size > 0)
275 fdt_add_reserved_memory_range(initrd_start, initrd_size);
276
277 DPRINTF("Usable memory:\n");
278 bc->dramblocks = 0;
279 LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
280 DPRINTF(" %lx - %lx\n", er->er_start, er->er_end);
281 bc->dram[bc->dramblocks].address = er->er_start;
282 bc->dram[bc->dramblocks].pages =
283 (er->er_end - er->er_start) / PAGE_SIZE;
284 bc->dramblocks++;
285 }
286 }
287
288 static void
289 fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
290 {
291 *pstart = *pend = 0;
292
293 #ifdef MEMORY_DISK_DYNAMIC
294 const int chosen = OF_finddevice("/chosen");
295 if (chosen < 0)
296 return;
297
298 int len;
299 const void *start_data = fdtbus_get_prop(chosen,
300 "linux,initrd-start", &len);
301 const void *end_data = fdtbus_get_prop(chosen,
302 "linux,initrd-end", NULL);
303 if (start_data == NULL || end_data == NULL)
304 return;
305
306 switch (len) {
307 case 4:
308 *pstart = be32dec(start_data);
309 *pend = be32dec(end_data);
310 break;
311 case 8:
312 *pstart = be64dec(start_data);
313 *pend = be64dec(end_data);
314 break;
315 default:
316 printf("Unsupported len %d for /chosen/initrd-start\n", len);
317 return;
318 }
319 #endif
320 }
321
322 static void
323 fdt_setup_initrd(void)
324 {
325 #ifdef MEMORY_DISK_DYNAMIC
326 const uint64_t initrd_size = initrd_end - initrd_start;
327 paddr_t startpa = trunc_page(initrd_start);
328 paddr_t endpa = round_page(initrd_end);
329 paddr_t pa;
330 vaddr_t va;
331 void *md_start;
332
333 if (initrd_size == 0)
334 return;
335
336 va = uvm_km_alloc(kernel_map, initrd_size, 0,
337 UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
338 if (va == 0) {
339 printf("Failed to allocate VA for initrd\n");
340 return;
341 }
342
343 md_start = (void *)va;
344
345 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
346 pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
347 pmap_update(pmap_kernel());
348
349 md_root_setconf(md_start, initrd_size);
350 #endif
351 }
352
353 u_int initarm(void *arg);
354
355 u_int
356 initarm(void *arg)
357 {
358 const struct arm_platform *plat;
359 uint64_t memory_start, memory_end;
360
361 /* set temporally to work printf()/panic() even before consinit() */
362 cn_tab = &earlycons;
363
364 /* Load FDT */
365 int error = fdt_check_header(fdt_addr_r);
366 if (error == 0) {
367 error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
368 if (error != 0)
369 panic("fdt_move failed: %s", fdt_strerror(error));
370 fdtbus_set_data(fdt_data);
371 } else {
372 panic("fdt_check_header failed: %s", fdt_strerror(error));
373 }
374
375 /* Lookup platform specific backend */
376 plat = arm_fdt_platform();
377 if (plat == NULL)
378 panic("Kernel does not support this device");
379
380 /* Early console may be available, announce ourselves. */
381 DPRINTF("FDT<%p>\n", fdt_addr_r);
382
383 const int chosen = OF_finddevice("/chosen");
384 if (chosen >= 0)
385 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
386 boot_args = bootargs;
387
388 DPRINTF("devmap\n");
389 pmap_devmap_register(plat->devmap());
390 #ifdef __aarch64__
391 pmap_devmap_bootstrap(plat->devmap());
392 #endif
393
394 /* Heads up ... Setup the CPU / MMU / TLB functions. */
395 DPRINTF("cpufunc\n");
396 if (set_cpufuncs())
397 panic("cpu not recognized!");
398
399 DPRINTF("bootstrap\n");
400 plat->bootstrap();
401
402 /*
403 * If stdout-path is specified on the command line, override the
404 * value in /chosen/stdout-path before initializing console.
405 */
406 fdt_update_stdout_path();
407
408 DPRINTF("consinit ");
409 consinit();
410 DPRINTF("ok\n");
411
412 DPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
413 uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
414
415 cpu_reset_address = fdt_reset;
416 cpu_powerdown_address = fdt_powerdown;
417 evbarm_device_register = fdt_device_register;
418
419 /* Talk to the user */
420 DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
421
422 #ifdef BOOT_ARGS
423 char mi_bootargs[] = BOOT_ARGS;
424 parse_mi_bootargs(mi_bootargs);
425 #endif
426
427 #ifndef __aarch64__
428 DPRINTF("KERNEL_BASE=0x%x, "
429 "KERNEL_VM_BASE=0x%x, "
430 "KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
431 "KERNEL_BASE_VOFFSET=0x%x\n",
432 KERNEL_BASE,
433 KERNEL_VM_BASE,
434 KERNEL_VM_BASE - KERNEL_BASE,
435 KERNEL_BASE_VOFFSET);
436 #endif
437
438 fdt_get_memory(&memory_start, &memory_end);
439
440 #if !defined(_LP64)
441 /* Cannot map memory above 4GB */
442 if (memory_end >= 0x100000000ULL)
443 memory_end = 0x100000000ULL - PAGE_SIZE;
444
445 uint64_t memory_size = memory_end - memory_start;
446 #endif
447
448 #ifndef __aarch64__
449 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
450 const bool mapallmem_p = true;
451 #ifndef PMAP_NEED_ALLOC_POOLPAGE
452 if (memory_size > KERNEL_VM_BASE - KERNEL_BASE) {
453 DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
454 __func__, (unsigned long) (memory_size >> 20),
455 (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
456 memory_size = KERNEL_VM_BASE - KERNEL_BASE;
457 }
458 #endif
459 #else
460 const bool mapallmem_p = false;
461 #endif
462 #endif
463
464 /* Parse ramdisk info */
465 fdt_probe_initrd(&initrd_start, &initrd_end);
466
467 /*
468 * Populate bootconfig structure for the benefit of
469 * dodumpsys
470 */
471 fdt_build_bootconfig(memory_start, memory_end);
472
473 #ifdef __aarch64__
474 extern char __kernel_text[];
475 extern char _end[];
476
477 vaddr_t kernstart = trunc_page((vaddr_t)__kernel_text);
478 vaddr_t kernend = round_page((vaddr_t)_end);
479
480 paddr_t kernstart_phys = KERN_VTOPHYS(kernstart);
481 paddr_t kernend_phys = KERN_VTOPHYS(kernend);
482
483 DPRINTF("%s: kernel phys start %lx end %lx\n", __func__, kernstart_phys, kernend_phys);
484
485 fdt_add_reserved_memory_range(kernstart_phys,
486 kernend_phys - kernstart_phys);
487 #else
488 arm32_bootmem_init(memory_start, memory_size, KERNEL_BASE_PHYS);
489 arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
490 plat->devmap(), mapallmem_p);
491 #endif
492
493 DPRINTF("bootargs: %s\n", bootargs);
494
495 parse_mi_bootargs(boot_args);
496
497 #define MAX_PHYSMEM 16
498 static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
499 int nfdt_physmem = 0;
500 struct extent_region *er;
501
502 LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
503 DPRINTF(" %lx - %lx\n", er->er_start, er->er_end);
504 struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
505
506 KASSERT(nfdt_physmem <= MAX_PHYSMEM);
507 bp->bp_start = atop(er->er_start);
508 bp->bp_pages = atop(er->er_end - er->er_start);
509 bp->bp_freelist = VM_FREELIST_DEFAULT;
510
511 #ifdef _LP64
512 if (er->er_end > 0x100000000)
513 bp->bp_freelist = VM_FREELIST_HIGHMEM;
514 #endif
515
516 #ifdef PMAP_NEED_ALLOC_POOLPAGE
517 if (atop(memory_size) > bp->bp_pages) {
518 arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
519 bp->bp_freelist = VM_FREELIST_DIRECTMAP;
520 }
521 #endif
522 }
523
524 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
525 nfdt_physmem);
526 }
527
528 static void
529 fdt_update_stdout_path(void)
530 {
531 char *stdout_path, *ep;
532 int stdout_path_len;
533 char buf[256];
534
535 const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
536 if (chosen_off == -1)
537 return;
538
539 if (get_bootconf_option(boot_args, "stdout-path",
540 BOOTOPT_TYPE_STRING, &stdout_path) == 0)
541 return;
542
543 ep = strchr(stdout_path, ' ');
544 stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
545 if (stdout_path_len >= sizeof(buf))
546 return;
547
548 strncpy(buf, stdout_path, stdout_path_len);
549 buf[stdout_path_len] = '\0';
550 fdt_setprop(fdt_data, chosen_off, "stdout-path",
551 buf, stdout_path_len + 1);
552 }
553
554 void
555 consinit(void)
556 {
557 static bool initialized = false;
558 const struct arm_platform *plat = arm_fdt_platform();
559 const struct fdt_console *cons = fdtbus_get_console();
560 struct fdt_attach_args faa;
561 u_int uart_freq = 0;
562
563 if (initialized || cons == NULL)
564 return;
565
566 plat->init_attach_args(&faa);
567 faa.faa_phandle = fdtbus_get_stdout_phandle();
568
569 if (plat->uart_freq != NULL)
570 uart_freq = plat->uart_freq();
571
572 cons->consinit(&faa, uart_freq);
573
574 #if NUKBD > 0
575 ukbd_cnattach(); /* allow USB keyboard to become console */
576 #endif
577
578 initialized = true;
579 }
580
581 void
582 delay(u_int us)
583 {
584 const struct arm_platform *plat = arm_fdt_platform();
585
586 plat->delay(us);
587 }
588
589 static void
590 fdt_device_register(device_t self, void *aux)
591 {
592 const struct arm_platform *plat = arm_fdt_platform();
593
594 if (device_is_a(self, "armfdt"))
595 fdt_setup_initrd();
596
597 if (plat && plat->device_register)
598 plat->device_register(self, aux);
599 }
600
601 static void
602 fdt_reset(void)
603 {
604 const struct arm_platform *plat = arm_fdt_platform();
605
606 fdtbus_power_reset();
607
608 if (plat && plat->reset)
609 plat->reset();
610 }
611
612 static void
613 fdt_powerdown(void)
614 {
615 fdtbus_power_poweroff();
616 }
617