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