fdt_machdep.c revision 1.1 1 /* $NetBSD: fdt_machdep.c,v 1.1 2017/05/30 10:27:53 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.1 2017/05/30 10:27:53 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_device_register(device_t, void *);
91 static void fdt_reset(void);
92 static void fdt_powerdown(void);
93
94 #ifdef PMAP_NEED_ALLOC_POOLPAGE
95 static struct boot_physmem bp_lowgig = {
96 .bp_pages = (KERNEL_VM_BASE - KERNEL_BASE) / NBPG,
97 .bp_freelist = VM_FREELIST_ISADMA,
98 .bp_flags = 0
99 };
100 #endif
101
102 #ifdef VERBOSE_INIT_ARM
103 static void
104 fdt_putchar(char c)
105 {
106 const struct arm_platform *plat = arm_fdt_platform();
107 if (plat && plat->early_purchar)
108 plat->early_putchar(c);
109 }
110
111 static void
112 fdt_putstr(const char *s)
113 {
114 for (const char *p = s; *p; p++)
115 fdt_putchar(*p);
116 }
117
118 static void
119 fdt_printn(u_int n, int base)
120 {
121 char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
122
123 p = buf;
124 do {
125 *p++ = hexdigits[n % base];
126 } while (n /= base);
127
128 do {
129 fdt_putchar(*--p);
130 } while (p > buf);
131 }
132 #define DPRINTF(...) printf(__VA_ARGS__)
133 #define DPRINT(x) fdt_putstr(x)
134 #define DPRINTN(x,b) fdt_printn((x), (b))
135 #else
136 #define DPRINTF(...)
137 #define DPRINT(x)
138 #define DPRINTN(x,b)
139 #endif
140
141 u_int
142 initarm(void *arg)
143 {
144 const struct arm_platform *plat;
145 uint64_t memory_addr, memory_size;
146 psize_t ram_size = 0;
147
148 /* Load FDT */
149 const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2];
150 int error = fdt_check_header(fdt_addr_r);
151 if (error == 0) {
152 error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
153 if (error != 0)
154 panic("fdt_move failed: %s", fdt_strerror(error));
155 fdtbus_set_data(fdt_data);
156 } else {
157 panic("fdt_check_header failed: %s", fdt_strerror(error));
158 }
159
160 /* Lookup platform specific backend */
161 plat = arm_fdt_platform();
162 if (plat == NULL)
163 panic("Kernel does not support this device");
164
165 /* Early console may be available, announce ourselves. */
166 DPRINT("NetBSD FDT init");
167
168 DPRINT(" devmap");
169 pmap_devmap_register(plat->devmap());
170
171 DPRINT(" bootstrap");
172 plat->bootstrap();
173
174 /* Heads up ... Setup the CPU / MMU / TLB functions. */
175 DPRINT(" cpufunc");
176 if (set_cpufuncs())
177 panic("cpu not recognized!");
178
179 DPRINT(" consinit");
180 consinit();
181
182 DPRINTF(" ok\n");
183
184 DPRINTF("uboot: args %#x, %#x, %#x, %#x\n",
185 uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
186
187 cpu_reset_address = fdt_reset;
188 cpu_powerdown_address = fdt_powerdown;
189 evbarm_device_register = fdt_device_register;
190
191 /* Talk to the user */
192 DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
193
194 #ifdef BOOT_ARGS
195 char mi_bootargs[] = BOOT_ARGS;
196 parse_mi_bootargs(mi_bootargs);
197 #endif
198
199 DPRINTF("KERNEL_BASE=0x%x, "
200 "KERNEL_VM_BASE=0x%x, "
201 "KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
202 "KERNEL_BASE_VOFFSET=0x%x\n",
203 KERNEL_BASE,
204 KERNEL_VM_BASE,
205 KERNEL_VM_BASE - KERNEL_BASE,
206 KERNEL_BASE_VOFFSET);
207
208 const int memory = OF_finddevice("/memory");
209 if (fdtbus_get_reg64(memory, 0, &memory_addr, &memory_size) != 0)
210 panic("Cannot determine memory size");
211
212 #if !defined(_LP64)
213 /* Cannot map memory above 4GB */
214 if (memory_addr + memory_size > 0x100000000)
215 memory_size = 0x100000000 - memory_addr;
216 #endif
217
218 ram_size = (bus_size_t)memory_size;
219
220 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
221 const bool mapallmem_p = true;
222 #ifndef PMAP_NEED_ALLOC_POOLPAGE
223 if (ram_size > KERNEL_VM_BASE - KERNEL_BASE) {
224 DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
225 __func__, (unsigned long) (ram_size >> 20),
226 (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
227 ram_size = KERNEL_VM_BASE - KERNEL_BASE;
228 }
229 #endif
230 #else
231 const bool mapallmem_p = false;
232 #endif
233
234 /* Fake bootconfig structure for the benefit of pmap.c. */
235 bootconfig.dramblocks = 1;
236 bootconfig.dram[0].address = (bus_addr_t)memory_addr;
237 bootconfig.dram[0].pages = ram_size / PAGE_SIZE;
238
239 arm32_bootmem_init(bootconfig.dram[0].address, ram_size,
240 KERNEL_BASE_PHYS);
241 arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
242 plat->devmap(), mapallmem_p);
243
244 const int chosen = OF_finddevice("/chosen");
245 if (chosen >= 0)
246 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
247
248 DPRINTF("bootargs: %s\n", bootargs);
249
250 boot_args = bootargs;
251 parse_mi_bootargs(boot_args);
252
253 #ifdef PMAP_NEED_ALLOC_POOLPAGE
254 bp_lowgig.bp_start = memory_addr / NBPG;
255 if (atop(ram_size) > bp_lowgig.bp_pages) {
256 arm_poolpage_vmfreelist = bp_lowgig.bp_freelist;
257 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
258 &bp_lowgig, 1);
259 }
260 #endif
261
262 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
263
264 }
265
266 void
267 consinit(void)
268 {
269 static bool initialized = false;
270 const struct arm_platform *plat = arm_fdt_platform();
271 const struct fdt_console *cons = fdtbus_get_console();
272 struct fdt_attach_args faa;
273
274 if (initialized || cons == NULL)
275 return;
276
277 plat->init_attach_args(&faa);
278 faa.faa_phandle = fdtbus_get_stdout_phandle();
279
280 cons->consinit(&faa);
281
282 initialized = true;
283 }
284
285 static void
286 fdt_device_register(device_t self, void *aux)
287 {
288 const struct arm_platform *plat = arm_fdt_platform();
289
290 if (plat && plat->device_register)
291 plat->device_register(self, aux);
292 }
293
294 static void
295 fdt_reset(void)
296 {
297 const struct arm_platform *plat = arm_fdt_platform();
298
299 fdtbus_power_reset();
300
301 if (plat && plat->reset)
302 plat->reset();
303 }
304
305 static void
306 fdt_powerdown(void)
307 {
308 fdtbus_power_poweroff();
309 }
310