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