fdt_machdep.c revision 1.5 1 /* $NetBSD: fdt_machdep.c,v 1.5 2017/06/06 00:28:05 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.5 2017/06/06 00:28:05 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
54 #include <uvm/uvm_extern.h>
55
56 #include <sys/conf.h>
57
58 #include <machine/db_machdep.h>
59 #include <ddb/db_sym.h>
60 #include <ddb/db_extern.h>
61
62 #include <machine/bootconfig.h>
63 #include <arm/armreg.h>
64 #include <arm/undefined.h>
65
66 #include <arm/arm32/machdep.h>
67
68 #include <evbarm/include/autoconf.h>
69 #include <evbarm/fdt/platform.h>
70
71 #include <arm/fdt/arm_fdtvar.h>
72
73 #ifndef FDT_MAX_BOOT_STRING
74 #define FDT_MAX_BOOT_STRING 1024
75 #endif
76
77 BootConfig bootconfig;
78 char bootargs[FDT_MAX_BOOT_STRING] = "";
79 char *boot_args = NULL;
80 u_int uboot_args[4] = { 0 }; /* filled in by xxx_start.S (not in bss) */
81
82 #include <libfdt.h>
83 #include <dev/fdt/fdtvar.h>
84 #define FDT_BUF_SIZE (128*1024)
85 static uint8_t fdt_data[FDT_BUF_SIZE];
86
87 extern char KERNEL_BASE_phys[];
88 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
89
90 static void fdt_update_stdout_path(void);
91 static void fdt_device_register(device_t, void *);
92 static void fdt_reset(void);
93 static void fdt_powerdown(void);
94
95 #ifdef PMAP_NEED_ALLOC_POOLPAGE
96 static struct boot_physmem bp_lowgig = {
97 .bp_pages = (KERNEL_VM_BASE - KERNEL_BASE) / NBPG,
98 .bp_freelist = VM_FREELIST_ISADMA,
99 .bp_flags = 0
100 };
101 #endif
102
103 #ifdef VERBOSE_INIT_ARM
104 static void
105 fdt_putchar(char c)
106 {
107 const struct arm_platform *plat = arm_fdt_platform();
108 if (plat && plat->early_putchar)
109 plat->early_putchar(c);
110 }
111
112 static void
113 fdt_putstr(const char *s)
114 {
115 for (const char *p = s; *p; p++)
116 fdt_putchar(*p);
117 }
118
119 static void
120 fdt_printn(u_int n, int base)
121 {
122 char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
123
124 p = buf;
125 do {
126 *p++ = hexdigits[n % base];
127 } while (n /= base);
128
129 do {
130 fdt_putchar(*--p);
131 } while (p > buf);
132 }
133 #define DPRINTF(...) printf(__VA_ARGS__)
134 #define DPRINT(x) fdt_putstr(x)
135 #define DPRINTN(x,b) fdt_printn((x), (b))
136 #else
137 #define DPRINTF(...)
138 #define DPRINT(x)
139 #define DPRINTN(x,b)
140 #endif
141
142 u_int
143 initarm(void *arg)
144 {
145 const struct arm_platform *plat;
146 uint64_t memory_addr, memory_size;
147 psize_t ram_size = 0;
148
149 /* Load FDT */
150 const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2];
151 int error = fdt_check_header(fdt_addr_r);
152 if (error == 0) {
153 error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
154 if (error != 0)
155 panic("fdt_move failed: %s", fdt_strerror(error));
156 fdtbus_set_data(fdt_data);
157 } else {
158 panic("fdt_check_header failed: %s", fdt_strerror(error));
159 }
160
161 /* Lookup platform specific backend */
162 plat = arm_fdt_platform();
163 if (plat == NULL)
164 panic("Kernel does not support this device");
165
166 /* Early console may be available, announce ourselves. */
167 DPRINT("FDT<");
168 DPRINTN((uintptr_t)fdt_addr_r, 16);
169 DPRINT(">");
170
171 DPRINT(" devmap");
172 pmap_devmap_register(plat->devmap());
173
174 DPRINT(" bootstrap");
175 plat->bootstrap();
176
177 /* Heads up ... Setup the CPU / MMU / TLB functions. */
178 DPRINT(" cpufunc");
179 if (set_cpufuncs())
180 panic("cpu not recognized!");
181
182 const int chosen = OF_finddevice("/chosen");
183 if (chosen >= 0)
184 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
185 boot_args = bootargs;
186
187 /*
188 * If stdout-path is specified on the command line, override the
189 * value in /chosen/stdout-path before initializing console.
190 */
191 fdt_update_stdout_path();
192
193 DPRINT(" consinit");
194 consinit();
195
196 DPRINTF(" ok\n");
197
198 DPRINTF("uboot: args %#x, %#x, %#x, %#x\n",
199 uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
200
201 cpu_reset_address = fdt_reset;
202 cpu_powerdown_address = fdt_powerdown;
203 evbarm_device_register = fdt_device_register;
204
205 /* Talk to the user */
206 DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
207
208 #ifdef BOOT_ARGS
209 char mi_bootargs[] = BOOT_ARGS;
210 parse_mi_bootargs(mi_bootargs);
211 #endif
212
213 DPRINTF("KERNEL_BASE=0x%x, "
214 "KERNEL_VM_BASE=0x%x, "
215 "KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
216 "KERNEL_BASE_VOFFSET=0x%x\n",
217 KERNEL_BASE,
218 KERNEL_VM_BASE,
219 KERNEL_VM_BASE - KERNEL_BASE,
220 KERNEL_BASE_VOFFSET);
221
222 const int memory = OF_finddevice("/memory");
223 if (fdtbus_get_reg64(memory, 0, &memory_addr, &memory_size) != 0)
224 panic("Cannot determine memory size");
225
226 #if !defined(_LP64)
227 /* Cannot map memory above 4GB */
228 if (memory_addr + memory_size > 0x100000000)
229 memory_size = 0x100000000 - memory_addr;
230 #endif
231
232 ram_size = (bus_size_t)memory_size;
233
234 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
235 const bool mapallmem_p = true;
236 #ifndef PMAP_NEED_ALLOC_POOLPAGE
237 if (ram_size > KERNEL_VM_BASE - KERNEL_BASE) {
238 DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
239 __func__, (unsigned long) (ram_size >> 20),
240 (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
241 ram_size = KERNEL_VM_BASE - KERNEL_BASE;
242 }
243 #endif
244 #else
245 const bool mapallmem_p = false;
246 #endif
247
248 /* Fake bootconfig structure for the benefit of pmap.c. */
249 bootconfig.dramblocks = 1;
250 bootconfig.dram[0].address = (bus_addr_t)memory_addr;
251 bootconfig.dram[0].pages = ram_size / PAGE_SIZE;
252
253 arm32_bootmem_init(bootconfig.dram[0].address, ram_size,
254 KERNEL_BASE_PHYS);
255 arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
256 plat->devmap(), mapallmem_p);
257
258 DPRINTF("bootargs: %s\n", bootargs);
259
260 parse_mi_bootargs(boot_args);
261
262 #ifdef PMAP_NEED_ALLOC_POOLPAGE
263 bp_lowgig.bp_start = memory_addr / NBPG;
264 if (atop(ram_size) > bp_lowgig.bp_pages) {
265 arm_poolpage_vmfreelist = bp_lowgig.bp_freelist;
266 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
267 &bp_lowgig, 1);
268 }
269 #endif
270
271 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
272
273 }
274
275 static void
276 fdt_update_stdout_path(void)
277 {
278 char *stdout_path, *ep;
279 int stdout_path_len;
280 char buf[256];
281
282 const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
283 if (chosen_off == -1)
284 return;
285
286 if (get_bootconf_option(boot_args, "stdout-path",
287 BOOTOPT_TYPE_STRING, &stdout_path) == 0)
288 return;
289
290 ep = strchr(stdout_path, ' ');
291 stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
292 if (stdout_path_len >= sizeof(buf))
293 return;
294
295 strncpy(buf, stdout_path, stdout_path_len);
296 buf[stdout_path_len] = '\0';
297 fdt_setprop(fdt_data, chosen_off, "stdout-path",
298 buf, stdout_path_len + 1);
299 }
300
301 void
302 consinit(void)
303 {
304 static bool initialized = false;
305 const struct arm_platform *plat = arm_fdt_platform();
306 const struct fdt_console *cons = fdtbus_get_console();
307 struct fdt_attach_args faa;
308 u_int uart_freq = 0;
309
310 if (initialized || cons == NULL)
311 return;
312
313 plat->init_attach_args(&faa);
314 faa.faa_phandle = fdtbus_get_stdout_phandle();
315
316 if (plat->uart_freq != NULL)
317 uart_freq = plat->uart_freq();
318
319 cons->consinit(&faa, uart_freq);
320
321 initialized = true;
322 }
323
324 void
325 delay(u_int us)
326 {
327 const struct arm_platform *plat = arm_fdt_platform();
328
329 plat->delay(us);
330 }
331
332 static void
333 fdt_device_register(device_t self, void *aux)
334 {
335 const struct arm_platform *plat = arm_fdt_platform();
336
337 if (plat && plat->device_register)
338 plat->device_register(self, aux);
339 }
340
341 static void
342 fdt_reset(void)
343 {
344 const struct arm_platform *plat = arm_fdt_platform();
345
346 fdtbus_power_reset();
347
348 if (plat && plat->reset)
349 plat->reset();
350 }
351
352 static void
353 fdt_powerdown(void)
354 {
355 fdtbus_power_poweroff();
356 }
357