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