Annotation of src/sys/arch/evbarm/fdt/fdt_machdep.c, Revision 1.71
1.71 ! riastrad 1: /* $NetBSD: fdt_machdep.c,v 1.70 2020/05/14 19:24:35 riastradh Exp $ */
1.1 jmcneill 2:
3: /*-
4: * Copyright (c) 2015-2017 Jared McNeill <jmcneill@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>
1.71 ! riastrad 30: __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.70 2020/05/14 19:24:35 riastradh Exp $");
1.1 jmcneill 31:
32: #include "opt_machdep.h"
1.21 ryo 33: #include "opt_bootconfig.h"
1.1 jmcneill 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"
1.46 jmcneill 39: #include "opt_efi.h"
1.1 jmcneill 40:
1.14 jmcneill 41: #include "ukbd.h"
1.39 bouyer 42: #include "wsdisplay.h"
1.14 jmcneill 43:
1.1 jmcneill 44: #include <sys/param.h>
45: #include <sys/systm.h>
46: #include <sys/bus.h>
47: #include <sys/atomic.h>
48: #include <sys/cpu.h>
49: #include <sys/device.h>
50: #include <sys/exec.h>
51: #include <sys/kernel.h>
52: #include <sys/kmem.h>
53: #include <sys/ksyms.h>
54: #include <sys/msgbuf.h>
55: #include <sys/proc.h>
56: #include <sys/reboot.h>
57: #include <sys/termios.h>
1.34 jmcneill 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>
1.53 jmcneill 63: #include <sys/uuid.h>
64: #include <sys/disk.h>
1.34 jmcneill 65: #include <sys/md5.h>
1.55 jmcneill 66: #include <sys/pserialize.h>
1.65 riastrad 67: #include <sys/rnd.h>
1.69 riastrad 68: #include <sys/rndsource.h>
1.55 jmcneill 69:
70: #include <net/if.h>
71: #include <net/if_dl.h>
1.1 jmcneill 72:
1.23 ryo 73: #include <dev/cons.h>
1.1 jmcneill 74: #include <uvm/uvm_extern.h>
75:
76: #include <sys/conf.h>
77:
78: #include <machine/db_machdep.h>
79: #include <ddb/db_sym.h>
80: #include <ddb/db_extern.h>
81:
82: #include <machine/bootconfig.h>
83: #include <arm/armreg.h>
84:
1.21 ryo 85: #include <arm/cpufunc.h>
1.1 jmcneill 86:
87: #include <evbarm/include/autoconf.h>
1.30 skrll 88: #include <evbarm/fdt/machdep.h>
1.1 jmcneill 89: #include <evbarm/fdt/platform.h>
1.49 jmcneill 90: #include <evbarm/fdt/fdt_memory.h>
1.1 jmcneill 91:
92: #include <arm/fdt/arm_fdtvar.h>
1.68 skrll 93: #include <dev/fdt/fdt_private.h>
1.1 jmcneill 94:
1.46 jmcneill 95: #ifdef EFI_RUNTIME
96: #include <arm/arm/efi_runtime.h>
97: #endif
98:
1.14 jmcneill 99: #if NUKBD > 0
100: #include <dev/usb/ukbdvar.h>
101: #endif
1.39 bouyer 102: #if NWSDISPLAY > 0
103: #include <dev/wscons/wsdisplayvar.h>
104: #endif
1.14 jmcneill 105:
1.8 jmcneill 106: #ifdef MEMORY_DISK_DYNAMIC
107: #include <dev/md.h>
108: #endif
109:
1.1 jmcneill 110: #ifndef FDT_MAX_BOOT_STRING
111: #define FDT_MAX_BOOT_STRING 1024
112: #endif
113:
114: BootConfig bootconfig;
115: char bootargs[FDT_MAX_BOOT_STRING] = "";
116: char *boot_args = NULL;
1.26 christos 117:
118: /* filled in before cleaning bss. keep in .data */
1.27 christos 119: u_long uboot_args[4] __attribute__((__section__(".data")));
1.28 skrll 120: const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
1.1 jmcneill 121:
1.8 jmcneill 122: static uint64_t initrd_start, initrd_end;
1.69 riastrad 123: static uint64_t rndseed_start, rndseed_end; /* our on-disk seed */
124: static uint64_t efirng_start, efirng_end; /* firmware's EFI RNG output */
1.8 jmcneill 125:
1.1 jmcneill 126: #include <libfdt.h>
127: #include <dev/fdt/fdtvar.h>
1.40 jmcneill 128: #define FDT_BUF_SIZE (512*1024)
1.1 jmcneill 129: static uint8_t fdt_data[FDT_BUF_SIZE];
130:
131: extern char KERNEL_BASE_phys[];
132: #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
133:
1.5 jmcneill 134: static void fdt_update_stdout_path(void);
1.1 jmcneill 135: static void fdt_device_register(device_t, void *);
1.39 bouyer 136: static void fdt_device_register_post_config(device_t, void *);
1.34 jmcneill 137: static void fdt_cpu_rootconf(void);
1.1 jmcneill 138: static void fdt_reset(void);
139: static void fdt_powerdown(void);
140:
141: static void
1.23 ryo 142: earlyconsputc(dev_t dev, int c)
1.1 jmcneill 143: {
1.48 skrll 144: uartputc(c);
1.1 jmcneill 145: }
146:
1.23 ryo 147: static int
148: earlyconsgetc(dev_t dev)
1.1 jmcneill 149: {
1.54 skrll 150: return 0;
1.23 ryo 151: }
1.1 jmcneill 152:
1.54 skrll 153: static struct consdev earlycons = {
154: .cn_putc = earlyconsputc,
155: .cn_getc = earlyconsgetc,
156: .cn_pollc = nullcnpollc,
157: };
158:
1.23 ryo 159: #ifdef VERBOSE_INIT_ARM
1.29 skrll 160: #define VPRINTF(...) printf(__VA_ARGS__)
1.1 jmcneill 161: #else
1.43 skrll 162: #define VPRINTF(...) __nothing
1.1 jmcneill 163: #endif
164:
1.7 jmcneill 165: /*
1.61 jmcneill 166: * Get all of physical memory, including holes.
1.7 jmcneill 167: */
168: static void
1.21 ryo 169: fdt_get_memory(uint64_t *pstart, uint64_t *pend)
1.7 jmcneill 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 */
1.21 ryo 176: if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0)
1.7 jmcneill 177: panic("Cannot determine memory size");
178:
1.21 ryo 179: *pstart = cur_addr;
180: *pend = cur_addr + cur_size;
181:
1.29 skrll 182: VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
1.21 ryo 183: 0, *pstart, *pend - *pstart);
1.7 jmcneill 184:
185: for (index = 1;
186: fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
187: index++) {
1.29 skrll 188: VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
1.7 jmcneill 189: index, cur_addr, cur_size);
1.21 ryo 190:
191: if (cur_addr + cur_size > *pend)
192: *pend = cur_addr + cur_size;
1.7 jmcneill 193: }
194: }
195:
1.16 skrll 196: void
1.8 jmcneill 197: fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
198: {
1.49 jmcneill 199: fdt_memory_remove_range(addr, size);
1.8 jmcneill 200: }
201:
202: /*
1.11 jmcneill 203: * Exclude memory ranges from memory config from the device tree
1.8 jmcneill 204: */
205: static void
1.33 jmcneill 206: fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr)
1.8 jmcneill 207: {
1.44 skrll 208: uint64_t lstart = 0, lend = 0;
1.8 jmcneill 209: uint64_t addr, size;
1.11 jmcneill 210: int index, error;
1.8 jmcneill 211:
1.11 jmcneill 212: const int num = fdt_num_mem_rsv(fdtbus_get_data());
213: for (index = 0; index <= num; index++) {
214: error = fdt_get_mem_rsv(fdtbus_get_data(), index,
215: &addr, &size);
1.44 skrll 216: if (error != 0)
217: continue;
218: if (lstart <= addr && addr <= lend) {
219: size -= (lend - addr);
220: addr = lend;
221: }
222: if (size == 0)
1.11 jmcneill 223: continue;
1.33 jmcneill 224: if (addr + size <= min_addr)
225: continue;
1.8 jmcneill 226: if (addr >= max_addr)
227: continue;
1.33 jmcneill 228: if (addr < min_addr) {
229: size -= (min_addr - addr);
230: addr = min_addr;
231: }
1.8 jmcneill 232: if (addr + size > max_addr)
233: size = max_addr - addr;
234: fdt_add_reserved_memory_range(addr, size);
1.44 skrll 235: lstart = addr;
236: lend = addr + size;
1.8 jmcneill 237: }
238: }
239:
1.49 jmcneill 240: static void
241: fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
242: {
243: BootConfig *bc = arg;
244:
1.52 jmcneill 245: VPRINTF(" %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
1.49 jmcneill 246: bc->dram[bc->dramblocks].address = m->start;
247: bc->dram[bc->dramblocks].pages =
248: (m->end - m->start) / PAGE_SIZE;
249: bc->dramblocks++;
250: }
251:
252: #define MAX_PHYSMEM 64
253: static int nfdt_physmem = 0;
254: static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
255:
256: static void
257: fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
258: {
1.62 skrll 259: const paddr_t saddr = round_page(m->start);
260: const paddr_t eaddr = trunc_page(m->end);
261:
262: VPRINTF(" %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
263: if (saddr >= eaddr) {
264: VPRINTF(" skipped\n");
265: return;
266: }
267: VPRINTF("\n");
268:
1.49 jmcneill 269: struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
270:
271: KASSERT(nfdt_physmem <= MAX_PHYSMEM);
272:
1.62 skrll 273: bp->bp_start = atop(saddr);
274: bp->bp_pages = atop(eaddr) - bp->bp_start;
1.49 jmcneill 275: bp->bp_freelist = VM_FREELIST_DEFAULT;
276:
277: #ifdef PMAP_NEED_ALLOC_POOLPAGE
278: const uint64_t memory_size = *(uint64_t *)arg;
279: if (atop(memory_size) > bp->bp_pages) {
280: arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
281: bp->bp_freelist = VM_FREELIST_DIRECTMAP;
282: }
283: #endif
284: }
285:
1.8 jmcneill 286: /*
287: * Define usable memory regions.
288: */
289: static void
1.21 ryo 290: fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
1.8 jmcneill 291: {
292: const int memory = OF_finddevice("/memory");
293: BootConfig *bc = &bootconfig;
294: uint64_t addr, size;
1.49 jmcneill 295: int index;
1.8 jmcneill 296:
297: for (index = 0;
298: fdtbus_get_reg64(memory, index, &addr, &size) == 0;
299: index++) {
1.21 ryo 300: if (addr >= mem_end || size == 0)
1.8 jmcneill 301: continue;
1.21 ryo 302: if (addr + size > mem_end)
303: size = mem_end - addr;
1.8 jmcneill 304:
1.49 jmcneill 305: fdt_memory_add_range(addr, size);
1.8 jmcneill 306: }
307:
1.33 jmcneill 308: fdt_add_reserved_memory(mem_start, mem_end);
1.8 jmcneill 309:
310: const uint64_t initrd_size = initrd_end - initrd_start;
311: if (initrd_size > 0)
1.49 jmcneill 312: fdt_memory_remove_range(initrd_start, initrd_size);
1.8 jmcneill 313:
1.65 riastrad 314: const uint64_t rndseed_size = rndseed_end - rndseed_start;
315: if (rndseed_size > 0)
316: fdt_memory_remove_range(rndseed_start, rndseed_size);
317:
1.69 riastrad 318: const uint64_t efirng_size = efirng_end - efirng_start;
319: if (efirng_size > 0)
320: fdt_memory_remove_range(efirng_start, efirng_size);
321:
1.47 jmcneill 322: const int framebuffer = OF_finddevice("/chosen/framebuffer");
323: if (framebuffer >= 0) {
324: for (index = 0;
325: fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
326: index++) {
327: fdt_add_reserved_memory_range(addr, size);
328: }
329: }
330:
1.29 skrll 331: VPRINTF("Usable memory:\n");
1.8 jmcneill 332: bc->dramblocks = 0;
1.49 jmcneill 333: fdt_memory_foreach(fdt_add_dram_blocks, bc);
1.8 jmcneill 334: }
335:
336: static void
1.70 riastrad 337: fdt_probe_range(const char *startname, const char *endname,
338: uint64_t *pstart, uint64_t *pend)
1.8 jmcneill 339: {
1.70 riastrad 340: int chosen, len;
341: const void *start_data, *end_data;
342:
1.8 jmcneill 343: *pstart = *pend = 0;
344:
1.70 riastrad 345: chosen = OF_finddevice("/chosen");
1.8 jmcneill 346: if (chosen < 0)
347: return;
348:
1.70 riastrad 349: start_data = fdtbus_get_prop(chosen, startname, &len);
350: end_data = fdtbus_get_prop(chosen, endname, NULL);
1.8 jmcneill 351: if (start_data == NULL || end_data == NULL)
352: return;
353:
354: switch (len) {
355: case 4:
356: *pstart = be32dec(start_data);
357: *pend = be32dec(end_data);
358: break;
359: case 8:
360: *pstart = be64dec(start_data);
361: *pend = be64dec(end_data);
362: break;
363: default:
1.70 riastrad 364: printf("Unsupported len %d for /chosen `%s'\n",
365: len, startname);
1.8 jmcneill 366: return;
367: }
368: }
369:
1.70 riastrad 370: static void *
371: fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize,
372: const char *purpose)
1.8 jmcneill 373: {
1.70 riastrad 374: const paddr_t startpa = trunc_page(start);
375: const paddr_t endpa = round_page(end);
1.8 jmcneill 376: paddr_t pa;
377: vaddr_t va;
1.70 riastrad 378: void *ptr;
1.8 jmcneill 379:
1.70 riastrad 380: *psize = end - start;
381: if (*psize == 0)
382: return NULL;
1.8 jmcneill 383:
1.70 riastrad 384: va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY|UVM_KMF_NOWAIT);
1.8 jmcneill 385: if (va == 0) {
1.70 riastrad 386: printf("Failed to allocate VA for %s\n", purpose);
387: return NULL;
1.8 jmcneill 388: }
1.70 riastrad 389: ptr = (void *)(va + (start & (PAGE_SIZE-1)));
1.8 jmcneill 390:
391: for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
392: pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
393: pmap_update(pmap_kernel());
394:
1.70 riastrad 395: return ptr;
396: }
397:
398: static void
1.71 ! riastrad 399: fdt_unmap_range(void *ptr, uint64_t size)
! 400: {
! 401: const char *start = ptr, *end = start + size;
! 402: const vaddr_t startva = trunc_page((vaddr_t)(uintptr_t)start);
! 403: const vaddr_t endva = round_page((vaddr_t)(uintptr_t)end);
! 404:
! 405: pmap_kremove(startva, endva - startva);
! 406: pmap_update(pmap_kernel());
! 407: }
! 408:
! 409: static void
1.70 riastrad 410: fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
411: {
412: *pstart = *pend = 0;
413:
414: #ifdef MEMORY_DISK_DYNAMIC
415: fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend);
1.8 jmcneill 416: #endif
417: }
418:
1.65 riastrad 419: static void
1.70 riastrad 420: fdt_setup_initrd(void)
1.65 riastrad 421: {
1.70 riastrad 422: #ifdef MEMORY_DISK_DYNAMIC
423: void *md_start;
424: uint64_t initrd_size;
1.65 riastrad 425:
1.70 riastrad 426: md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size,
427: "initrd");
428: if (md_start == NULL)
1.65 riastrad 429: return;
1.70 riastrad 430: md_root_setconf(md_start, initrd_size);
431: #endif
432: }
1.65 riastrad 433:
1.70 riastrad 434: static void
435: fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend)
436: {
1.65 riastrad 437:
1.70 riastrad 438: fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end",
439: pstart, pend);
1.65 riastrad 440: }
441:
442: static void
443: fdt_setup_rndseed(void)
444: {
1.70 riastrad 445: uint64_t rndseed_size;
1.65 riastrad 446: void *rndseed;
447:
1.70 riastrad 448: rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size,
449: "rndseed");
450: if (rndseed == NULL)
1.65 riastrad 451: return;
452: rnd_seed(rndseed, rndseed_size);
1.71 ! riastrad 453: fdt_unmap_range(rndseed, rndseed_size);
1.65 riastrad 454: }
455:
1.69 riastrad 456: static void
457: fdt_probe_efirng(uint64_t *pstart, uint64_t *pend)
458: {
459:
1.70 riastrad 460: fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end",
461: pstart, pend);
1.69 riastrad 462: }
463:
464: static struct krndsource efirng_source;
465:
466: static void
467: fdt_setup_efirng(void)
468: {
1.70 riastrad 469: uint64_t efirng_size;
1.69 riastrad 470: void *efirng;
471:
1.70 riastrad 472: efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size,
473: "efirng");
474: if (efirng == NULL)
1.69 riastrad 475: return;
476:
477: rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG,
478: RND_FLAG_DEFAULT);
479: rnd_add_data(&efirng_source, efirng, efirng_size, 0);
480: explicit_memset(efirng, 0, efirng_size);
1.71 ! riastrad 481: fdt_unmap_range(efirng, efirng_size);
1.69 riastrad 482: }
483:
1.46 jmcneill 484: #ifdef EFI_RUNTIME
485: static void
1.51 jmcneill 486: fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type)
1.46 jmcneill 487: {
488: int len;
489:
490: const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
491: if (chosen_off < 0)
492: return;
493:
494: const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len);
495: if (map == NULL)
496: return;
497:
498: while (len >= 24) {
499: const paddr_t pa = be64toh(map[0]);
500: const vaddr_t va = be64toh(map[1]);
501: const uint64_t sz = be64toh(map[2]);
1.51 jmcneill 502: VPRINTF("%s: %s %lx-%lx (%lx-%lx)\n", __func__, prop, pa, pa+sz-1, va, va+sz-1);
503: arm_efirt_md_map_range(va, pa, sz, type);
1.46 jmcneill 504: map += 3;
505: len -= 24;
506: }
507: }
508: #endif
509:
1.64 skrll 510: vaddr_t
1.1 jmcneill 511: initarm(void *arg)
512: {
513: const struct arm_platform *plat;
1.21 ryo 514: uint64_t memory_start, memory_end;
1.1 jmcneill 515:
1.23 ryo 516: /* set temporally to work printf()/panic() even before consinit() */
517: cn_tab = &earlycons;
518:
1.1 jmcneill 519: /* Load FDT */
520: int error = fdt_check_header(fdt_addr_r);
521: if (error == 0) {
1.41 jmcneill 522: /* If the DTB is too big, try to pack it in place first. */
523: if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
524: (void)fdt_pack(__UNCONST(fdt_addr_r));
1.38 jmcneill 525: error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
1.1 jmcneill 526: if (error != 0)
527: panic("fdt_move failed: %s", fdt_strerror(error));
1.67 thorpej 528: fdtbus_init(fdt_data);
1.1 jmcneill 529: } else {
530: panic("fdt_check_header failed: %s", fdt_strerror(error));
531: }
532:
533: /* Lookup platform specific backend */
534: plat = arm_fdt_platform();
535: if (plat == NULL)
536: panic("Kernel does not support this device");
537:
538: /* Early console may be available, announce ourselves. */
1.29 skrll 539: VPRINTF("FDT<%p>\n", fdt_addr_r);
1.1 jmcneill 540:
1.6 jmcneill 541: const int chosen = OF_finddevice("/chosen");
542: if (chosen >= 0)
543: OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
544: boot_args = bootargs;
545:
1.1 jmcneill 546: /* Heads up ... Setup the CPU / MMU / TLB functions. */
1.29 skrll 547: VPRINTF("cpufunc\n");
1.1 jmcneill 548: if (set_cpufuncs())
549: panic("cpu not recognized!");
550:
1.44 skrll 551: /*
552: * Memory is still identity/flat mapped this point so using ttbr for
553: * l1pt VA is fine
554: */
555:
556: VPRINTF("devmap\n");
557: extern char ARM_BOOTSTRAP_LxPT[];
558: pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap());
559:
1.29 skrll 560: VPRINTF("bootstrap\n");
1.32 skrll 561: plat->ap_bootstrap();
1.16 skrll 562:
1.5 jmcneill 563: /*
564: * If stdout-path is specified on the command line, override the
565: * value in /chosen/stdout-path before initializing console.
566: */
1.44 skrll 567: VPRINTF("stdout\n");
1.5 jmcneill 568: fdt_update_stdout_path();
569:
1.38 jmcneill 570: /*
571: * Done making changes to the FDT.
572: */
573: fdt_pack(fdt_data);
574:
1.29 skrll 575: VPRINTF("consinit ");
1.1 jmcneill 576: consinit();
1.29 skrll 577: VPRINTF("ok\n");
1.1 jmcneill 578:
1.29 skrll 579: VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
1.1 jmcneill 580: uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
581:
582: cpu_reset_address = fdt_reset;
583: cpu_powerdown_address = fdt_powerdown;
584: evbarm_device_register = fdt_device_register;
1.39 bouyer 585: evbarm_device_register_post_config = fdt_device_register_post_config;
1.34 jmcneill 586: evbarm_cpu_rootconf = fdt_cpu_rootconf;
1.1 jmcneill 587:
588: /* Talk to the user */
1.45 skrll 589: printf("NetBSD/evbarm (fdt) booting ...\n");
1.1 jmcneill 590:
591: #ifdef BOOT_ARGS
592: char mi_bootargs[] = BOOT_ARGS;
593: parse_mi_bootargs(mi_bootargs);
594: #endif
595:
1.21 ryo 596: fdt_get_memory(&memory_start, &memory_end);
1.1 jmcneill 597:
598: #if !defined(_LP64)
599: /* Cannot map memory above 4GB */
1.21 ryo 600: if (memory_end >= 0x100000000ULL)
601: memory_end = 0x100000000ULL - PAGE_SIZE;
602:
1.31 skrll 603: #endif
1.21 ryo 604: uint64_t memory_size = memory_end - memory_start;
1.1 jmcneill 605:
1.44 skrll 606: VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
607: PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
608:
1.8 jmcneill 609: /* Parse ramdisk info */
610: fdt_probe_initrd(&initrd_start, &initrd_end);
611:
1.69 riastrad 612: /* Parse our on-disk rndseed and the firmware's RNG from EFI */
1.65 riastrad 613: fdt_probe_rndseed(&rndseed_start, &rndseed_end);
1.69 riastrad 614: fdt_probe_efirng(&efirng_start, &efirng_end);
1.65 riastrad 615:
1.16 skrll 616: /*
617: * Populate bootconfig structure for the benefit of
618: * dodumpsys
619: */
1.44 skrll 620: VPRINTF("%s: fdt_build_bootconfig\n", __func__);
1.21 ryo 621: fdt_build_bootconfig(memory_start, memory_end);
622:
1.46 jmcneill 623: #ifdef EFI_RUNTIME
1.51 jmcneill 624: fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
625: fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
626: fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
1.46 jmcneill 627: #endif
628:
1.31 skrll 629: /* Perform PT build and VM init */
630: cpu_kernel_vm_init(memory_start, memory_size);
1.1 jmcneill 631:
1.29 skrll 632: VPRINTF("bootargs: %s\n", bootargs);
1.1 jmcneill 633:
634: parse_mi_bootargs(boot_args);
635:
1.49 jmcneill 636: VPRINTF("Memory regions:\n");
637: fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
1.1 jmcneill 638:
1.64 skrll 639: vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
1.16 skrll 640: nfdt_physmem);
1.44 skrll 641:
1.59 skrll 642: /*
1.60 skrll 643: * initarm_common flushes cache if required before AP start
1.59 skrll 644: */
1.58 skrll 645: error = 0;
1.56 ryo 646: if ((boothowto & RB_MD1) == 0) {
647: VPRINTF("mpstart\n");
648: if (plat->ap_mpstart)
1.58 skrll 649: error = plat->ap_mpstart();
1.56 ryo 650: }
1.44 skrll 651:
1.58 skrll 652: if (error)
653: return sp;
1.44 skrll 654: /*
655: * Now we have APs started the pages used for stacks and L1PT can
656: * be given to uvm
657: */
1.57 skrll 658: extern char const __start__init_memory[];
659: extern char const __stop__init_memory[] __weak;
660:
1.44 skrll 661: if (__start__init_memory != __stop__init_memory) {
662: const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
663: const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
664: const paddr_t spg = atop(spa);
665: const paddr_t epg = atop(epa);
666:
1.63 skrll 667: VPRINTF(" start %08lx end %08lx... "
668: "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
669:
1.44 skrll 670: uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
671:
672: }
673:
674: return sp;
1.1 jmcneill 675: }
676:
1.5 jmcneill 677: static void
678: fdt_update_stdout_path(void)
679: {
680: char *stdout_path, *ep;
681: int stdout_path_len;
682: char buf[256];
683:
684: const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
685: if (chosen_off == -1)
686: return;
687:
688: if (get_bootconf_option(boot_args, "stdout-path",
689: BOOTOPT_TYPE_STRING, &stdout_path) == 0)
690: return;
691:
692: ep = strchr(stdout_path, ' ');
693: stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
694: if (stdout_path_len >= sizeof(buf))
695: return;
696:
697: strncpy(buf, stdout_path, stdout_path_len);
698: buf[stdout_path_len] = '\0';
699: fdt_setprop(fdt_data, chosen_off, "stdout-path",
700: buf, stdout_path_len + 1);
701: }
702:
1.1 jmcneill 703: void
704: consinit(void)
705: {
706: static bool initialized = false;
707: const struct arm_platform *plat = arm_fdt_platform();
708: const struct fdt_console *cons = fdtbus_get_console();
709: struct fdt_attach_args faa;
1.4 jmcneill 710: u_int uart_freq = 0;
1.1 jmcneill 711:
712: if (initialized || cons == NULL)
713: return;
714:
1.32 skrll 715: plat->ap_init_attach_args(&faa);
1.1 jmcneill 716: faa.faa_phandle = fdtbus_get_stdout_phandle();
717:
1.32 skrll 718: if (plat->ap_uart_freq != NULL)
719: uart_freq = plat->ap_uart_freq();
1.4 jmcneill 720:
721: cons->consinit(&faa, uart_freq);
1.1 jmcneill 722:
723: initialized = true;
724: }
725:
1.3 jmcneill 726: void
1.65 riastrad 727: cpu_startup_hook(void)
728: {
729:
1.68 skrll 730: fdtbus_intr_init();
731:
1.65 riastrad 732: fdt_setup_rndseed();
1.69 riastrad 733: fdt_setup_efirng();
1.65 riastrad 734: }
735:
736: void
1.3 jmcneill 737: delay(u_int us)
738: {
739: const struct arm_platform *plat = arm_fdt_platform();
740:
1.32 skrll 741: plat->ap_delay(us);
1.3 jmcneill 742: }
743:
1.1 jmcneill 744: static void
1.34 jmcneill 745: fdt_detect_root_device(device_t dev)
746: {
747: struct mbr_sector mbr;
748: uint8_t buf[DEV_BSIZE];
749: uint8_t hash[16];
750: const uint8_t *rhash;
1.53 jmcneill 751: char rootarg[64];
1.34 jmcneill 752: struct vnode *vp;
753: MD5_CTX md5ctx;
754: int error, len;
755: size_t resid;
756: u_int part;
757:
758: const int chosen = OF_finddevice("/chosen");
759: if (chosen < 0)
760: return;
761:
762: if (of_hasprop(chosen, "netbsd,mbr") &&
763: of_hasprop(chosen, "netbsd,partition")) {
764:
765: /*
766: * The bootloader has passed in a partition index and MD5 hash
767: * of the MBR sector. Read the MBR of this device, calculate the
768: * hash, and compare it with the value passed in.
769: */
770: rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
771: if (rhash == NULL || len != 16)
772: return;
773: of_getprop_uint32(chosen, "netbsd,partition", &part);
774: if (part >= MAXPARTITIONS)
775: return;
776:
777: vp = opendisk(dev);
778: if (!vp)
779: return;
780: error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
781: 0, NOCRED, &resid, NULL);
782: VOP_CLOSE(vp, FREAD, NOCRED);
783: vput(vp);
784:
785: if (error != 0)
786: return;
787:
788: memcpy(&mbr, buf, sizeof(mbr));
789: MD5Init(&md5ctx);
790: MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
791: MD5Final(hash, &md5ctx);
792:
793: if (memcmp(rhash, hash, 16) != 0)
794: return;
795:
796: snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a');
797: strcat(boot_args, rootarg);
798: }
1.53 jmcneill 799:
800: if (of_hasprop(chosen, "netbsd,gpt-guid")) {
801: char guidbuf[UUID_STR_LEN];
802: const struct uuid *guid = fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len);
803: if (guid == NULL || len != 16)
804: return;
805:
806: uuid_snprintf(guidbuf, sizeof(guidbuf), guid);
807: snprintf(rootarg, sizeof(rootarg), " root=wedge:%s", guidbuf);
808: strcat(boot_args, rootarg);
809: }
810:
811: if (of_hasprop(chosen, "netbsd,gpt-label")) {
812: const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label");
813: if (label == NULL || *label == '\0')
814: return;
815:
816: device_t dv = dkwedge_find_by_wname(label);
817: if (dv != NULL)
818: booted_device = dv;
819: }
1.55 jmcneill 820:
821: if (of_hasprop(chosen, "netbsd,booted-mac-address")) {
822: const uint8_t *macaddr = fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len);
823: if (macaddr == NULL || len != 6)
824: return;
825: int s = pserialize_read_enter();
826: struct ifnet *ifp;
827: IFNET_READER_FOREACH(ifp) {
828: if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) {
829: device_t dv = device_find_by_xname(ifp->if_xname);
830: if (dv != NULL)
831: booted_device = dv;
832: break;
833: }
834: }
835: pserialize_read_exit(s);
836: }
1.34 jmcneill 837: }
838:
839: static void
1.1 jmcneill 840: fdt_device_register(device_t self, void *aux)
841: {
842: const struct arm_platform *plat = arm_fdt_platform();
843:
1.8 jmcneill 844: if (device_is_a(self, "armfdt"))
845: fdt_setup_initrd();
846:
1.32 skrll 847: if (plat && plat->ap_device_register)
848: plat->ap_device_register(self, aux);
1.1 jmcneill 849: }
850:
851: static void
1.39 bouyer 852: fdt_device_register_post_config(device_t self, void *aux)
853: {
854: #if NUKBD > 0 && NWSDISPLAY > 0
855: if (device_is_a(self, "wsdisplay")) {
856: struct wsdisplay_softc *sc = device_private(self);
857: if (wsdisplay_isconsole(sc))
858: ukbd_cnattach();
859: }
860: #endif
861: }
862:
863: static void
1.34 jmcneill 864: fdt_cpu_rootconf(void)
865: {
866: device_t dev;
867: deviter_t di;
868: char *ptr;
869:
870: for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
871: if (device_class(dev) != DV_DISK)
872: continue;
873:
874: if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0)
875: break;
876:
1.36 jakllsch 877: if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd"))
1.34 jmcneill 878: fdt_detect_root_device(dev);
879: }
880: deviter_release(&di);
881: }
882:
883: static void
1.1 jmcneill 884: fdt_reset(void)
885: {
886: const struct arm_platform *plat = arm_fdt_platform();
887:
888: fdtbus_power_reset();
889:
1.32 skrll 890: if (plat && plat->ap_reset)
891: plat->ap_reset();
1.1 jmcneill 892: }
893:
894: static void
895: fdt_powerdown(void)
896: {
897: fdtbus_power_poweroff();
898: }
CVSweb <webmaster@jp.NetBSD.org>