fdt_machdep.c revision 1.13 1 /* $NetBSD: fdt_machdep.c,v 1.13 2017/08/24 11:33:28 jmcneill 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.13 2017/08/24 11:33:28 jmcneill 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 <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/atomic.h>
43 #include <sys/cpu.h>
44 #include <sys/device.h>
45 #include <sys/exec.h>
46 #include <sys/kernel.h>
47 #include <sys/kmem.h>
48 #include <sys/ksyms.h>
49 #include <sys/msgbuf.h>
50 #include <sys/proc.h>
51 #include <sys/reboot.h>
52 #include <sys/termios.h>
53 #include <sys/extent.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #include <sys/conf.h>
58
59 #include <machine/db_machdep.h>
60 #include <ddb/db_sym.h>
61 #include <ddb/db_extern.h>
62
63 #include <machine/bootconfig.h>
64 #include <arm/armreg.h>
65 #include <arm/undefined.h>
66
67 #include <arm/arm32/machdep.h>
68
69 #include <evbarm/include/autoconf.h>
70 #include <evbarm/fdt/platform.h>
71
72 #include <arm/fdt/arm_fdtvar.h>
73
74 #ifdef MEMORY_DISK_DYNAMIC
75 #include <dev/md.h>
76 #endif
77
78 #ifndef FDT_MAX_BOOT_STRING
79 #define FDT_MAX_BOOT_STRING 1024
80 #endif
81
82 BootConfig bootconfig;
83 char bootargs[FDT_MAX_BOOT_STRING] = "";
84 char *boot_args = NULL;
85 u_int uboot_args[4] = { 0 }; /* filled in by xxx_start.S (not in bss) */
86
87 static char fdt_memory_ext_storage[EXTENT_FIXED_STORAGE_SIZE(DRAM_BLOCKS)];
88 static struct extent *fdt_memory_ext;
89
90 static uint64_t initrd_start, initrd_end;
91
92 #include <libfdt.h>
93 #include <dev/fdt/fdtvar.h>
94 #define FDT_BUF_SIZE (128*1024)
95 static uint8_t fdt_data[FDT_BUF_SIZE];
96
97 extern char KERNEL_BASE_phys[];
98 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
99
100 static void fdt_update_stdout_path(void);
101 static void fdt_device_register(device_t, void *);
102 static void fdt_reset(void);
103 static void fdt_powerdown(void);
104
105 #ifdef PMAP_NEED_ALLOC_POOLPAGE
106 static struct boot_physmem bp_lowgig = {
107 .bp_pages = (KERNEL_VM_BASE - KERNEL_BASE) / NBPG,
108 .bp_freelist = VM_FREELIST_ISADMA,
109 .bp_flags = 0
110 };
111 #endif
112
113 #ifdef VERBOSE_INIT_ARM
114 static void
115 fdt_putchar(char c)
116 {
117 const struct arm_platform *plat = arm_fdt_platform();
118 if (plat && plat->early_putchar)
119 plat->early_putchar(c);
120 }
121
122 static void
123 fdt_putstr(const char *s)
124 {
125 for (const char *p = s; *p; p++)
126 fdt_putchar(*p);
127 }
128
129 static void
130 fdt_printn(u_int n, int base)
131 {
132 char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
133
134 p = buf;
135 do {
136 *p++ = hexdigits[n % base];
137 } while (n /= base);
138
139 do {
140 fdt_putchar(*--p);
141 } while (p > buf);
142 }
143 #define DPRINTF(...) printf(__VA_ARGS__)
144 #define DPRINT(x) fdt_putstr(x)
145 #define DPRINTN(x,b) fdt_printn((x), (b))
146 #else
147 #define DPRINTF(...)
148 #define DPRINT(x)
149 #define DPRINTN(x,b)
150 #endif
151
152 /*
153 * Get the first physically contiguous region of memory.
154 */
155 static void
156 fdt_get_memory(uint64_t *paddr, uint64_t *psize)
157 {
158 const int memory = OF_finddevice("/memory");
159 uint64_t cur_addr, cur_size;
160 int index;
161
162 /* Assume the first entry is the start of memory */
163 if (fdtbus_get_reg64(memory, 0, paddr, psize) != 0)
164 panic("Cannot determine memory size");
165
166 DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
167 0, *paddr, *psize);
168
169 /* If subsequent entries follow the previous one, append them. */
170 for (index = 1;
171 fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
172 index++) {
173 DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
174 index, cur_addr, cur_size);
175 if (*paddr + *psize == cur_addr)
176 *psize += cur_size;
177 }
178 }
179
180 static void
181 fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
182 {
183 int error;
184
185 addr = trunc_page(addr);
186 size = round_page(size);
187
188 error = extent_free(fdt_memory_ext, addr, size, EX_NOWAIT);
189 if (error != 0)
190 printf("MEM ERROR: res %llx-%llx failed: %d\n",
191 addr, addr + size, error);
192 else
193 DPRINTF("MEM: res %llx-%llx\n", addr, addr + size);
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, size, error);
248 DPRINTF("MEM: add %llx-%llx\n", 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 const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2];
341 int error = fdt_check_header(fdt_addr_r);
342 if (error == 0) {
343 error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
344 if (error != 0)
345 panic("fdt_move failed: %s", fdt_strerror(error));
346 fdtbus_set_data(fdt_data);
347 } else {
348 panic("fdt_check_header failed: %s", fdt_strerror(error));
349 }
350
351 /* Lookup platform specific backend */
352 plat = arm_fdt_platform();
353 if (plat == NULL)
354 panic("Kernel does not support this device");
355
356 /* Early console may be available, announce ourselves. */
357 DPRINT("FDT<");
358 DPRINTN((uintptr_t)fdt_addr_r, 16);
359 DPRINT(">");
360
361 const int chosen = OF_finddevice("/chosen");
362 if (chosen >= 0)
363 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
364 boot_args = bootargs;
365
366 DPRINT(" devmap");
367 pmap_devmap_register(plat->devmap());
368
369 DPRINT(" bootstrap");
370 plat->bootstrap();
371
372 /* Heads up ... Setup the CPU / MMU / TLB functions. */
373 DPRINT(" cpufunc");
374 if (set_cpufuncs())
375 panic("cpu not recognized!");
376
377 /*
378 * If stdout-path is specified on the command line, override the
379 * value in /chosen/stdout-path before initializing console.
380 */
381 fdt_update_stdout_path();
382
383 DPRINT(" consinit");
384 consinit();
385
386 DPRINTF(" ok\n");
387
388 DPRINTF("uboot: args %#x, %#x, %#x, %#x\n",
389 uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
390
391 cpu_reset_address = fdt_reset;
392 cpu_powerdown_address = fdt_powerdown;
393 evbarm_device_register = fdt_device_register;
394
395 /* Talk to the user */
396 DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
397
398 #ifdef BOOT_ARGS
399 char mi_bootargs[] = BOOT_ARGS;
400 parse_mi_bootargs(mi_bootargs);
401 #endif
402
403 DPRINTF("KERNEL_BASE=0x%x, "
404 "KERNEL_VM_BASE=0x%x, "
405 "KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
406 "KERNEL_BASE_VOFFSET=0x%x\n",
407 KERNEL_BASE,
408 KERNEL_VM_BASE,
409 KERNEL_VM_BASE - KERNEL_BASE,
410 KERNEL_BASE_VOFFSET);
411
412 fdt_get_memory(&memory_addr, &memory_size);
413
414 #if !defined(_LP64)
415 /* Cannot map memory above 4GB */
416 if (memory_addr + memory_size >= 0x100000000)
417 memory_size = 0x100000000 - memory_addr - PAGE_SIZE;
418 #endif
419
420 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
421 const bool mapallmem_p = true;
422 #ifndef PMAP_NEED_ALLOC_POOLPAGE
423 if (memory_size > KERNEL_VM_BASE - KERNEL_BASE) {
424 DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
425 __func__, (unsigned long) (memory_size >> 20),
426 (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
427 memory_size = KERNEL_VM_BASE - KERNEL_BASE;
428 }
429 #endif
430 #else
431 const bool mapallmem_p = false;
432 #endif
433
434 /* Parse ramdisk info */
435 fdt_probe_initrd(&initrd_start, &initrd_end);
436
437 /* Populate bootconfig structure for the benefit of pmap.c. */
438 fdt_build_bootconfig(memory_addr, memory_size);
439
440 arm32_bootmem_init(bootconfig.dram[0].address, memory_size,
441 KERNEL_BASE_PHYS);
442 arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
443 plat->devmap(), mapallmem_p);
444
445 DPRINTF("bootargs: %s\n", bootargs);
446
447 parse_mi_bootargs(boot_args);
448
449 #ifdef PMAP_NEED_ALLOC_POOLPAGE
450 bp_lowgig.bp_start = memory_addr / NBPG;
451 if (atop(ram_size) > bp_lowgig.bp_pages) {
452 arm_poolpage_vmfreelist = bp_lowgig.bp_freelist;
453 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
454 &bp_lowgig, 1);
455 }
456 #endif
457
458 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
459 }
460
461 static void
462 fdt_update_stdout_path(void)
463 {
464 char *stdout_path, *ep;
465 int stdout_path_len;
466 char buf[256];
467
468 const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
469 if (chosen_off == -1)
470 return;
471
472 if (get_bootconf_option(boot_args, "stdout-path",
473 BOOTOPT_TYPE_STRING, &stdout_path) == 0)
474 return;
475
476 ep = strchr(stdout_path, ' ');
477 stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
478 if (stdout_path_len >= sizeof(buf))
479 return;
480
481 strncpy(buf, stdout_path, stdout_path_len);
482 buf[stdout_path_len] = '\0';
483 fdt_setprop(fdt_data, chosen_off, "stdout-path",
484 buf, stdout_path_len + 1);
485 }
486
487 void
488 consinit(void)
489 {
490 static bool initialized = false;
491 const struct arm_platform *plat = arm_fdt_platform();
492 const struct fdt_console *cons = fdtbus_get_console();
493 struct fdt_attach_args faa;
494 u_int uart_freq = 0;
495
496 if (initialized || cons == NULL)
497 return;
498
499 plat->init_attach_args(&faa);
500 faa.faa_phandle = fdtbus_get_stdout_phandle();
501
502 if (plat->uart_freq != NULL)
503 uart_freq = plat->uart_freq();
504
505 cons->consinit(&faa, uart_freq);
506
507 initialized = true;
508 }
509
510 void
511 delay(u_int us)
512 {
513 const struct arm_platform *plat = arm_fdt_platform();
514
515 plat->delay(us);
516 }
517
518 static void
519 fdt_device_register(device_t self, void *aux)
520 {
521 const struct arm_platform *plat = arm_fdt_platform();
522
523 if (device_is_a(self, "armfdt"))
524 fdt_setup_initrd();
525
526 if (plat && plat->device_register)
527 plat->device_register(self, aux);
528 }
529
530 static void
531 fdt_reset(void)
532 {
533 const struct arm_platform *plat = arm_fdt_platform();
534
535 fdtbus_power_reset();
536
537 if (plat && plat->reset)
538 plat->reset();
539 }
540
541 static void
542 fdt_powerdown(void)
543 {
544 fdtbus_power_poweroff();
545 }
546