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