arm32_machdep.c revision 1.82 1 /* $NetBSD: arm32_machdep.c,v 1.82 2012/08/16 18:22:39 matt Exp $ */
2
3 /*
4 * Copyright (c) 1994-1998 Mark Brinicombe.
5 * Copyright (c) 1994 Brini.
6 * All rights reserved.
7 *
8 * This code is derived from software written for Brini by Mark Brinicombe
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Mark Brinicombe
21 * for the NetBSD Project.
22 * 4. The name of the company nor the name of the author may be used to
23 * endorse or promote products derived from this software without specific
24 * prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * Machine dependent functions for kernel setup
39 *
40 * Created : 17/09/94
41 * Updated : 18/04/01 updated for new wscons
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: arm32_machdep.c,v 1.82 2012/08/16 18:22:39 matt Exp $");
46
47 #include "opt_modular.h"
48 #include "opt_md.h"
49 #include "opt_pmap_debug.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/reboot.h>
54 #include <sys/proc.h>
55 #include <sys/kauth.h>
56 #include <sys/kernel.h>
57 #include <sys/mbuf.h>
58 #include <sys/mount.h>
59 #include <sys/buf.h>
60 #include <sys/msgbuf.h>
61 #include <sys/device.h>
62 #include <sys/sysctl.h>
63 #include <sys/cpu.h>
64 #include <sys/module.h>
65
66 #include <uvm/uvm_extern.h>
67
68 #include <dev/cons.h>
69 #include <dev/mm.h>
70
71 #include <arm/arm32/katelib.h>
72 #include <arm/arm32/machdep.h>
73
74 #include <machine/bootconfig.h>
75 #include <machine/pcb.h>
76
77 void (*cpu_reset_address)(void); /* Used by locore */
78 paddr_t cpu_reset_address_paddr; /* Used by locore */
79
80 struct vm_map *phys_map = NULL;
81
82 #if defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
83 extern size_t md_root_size; /* Memory disc size */
84 #endif /* MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
85
86 pv_addr_t kernelstack;
87 pv_addr_t abtstack;
88 pv_addr_t fiqstack;
89 pv_addr_t irqstack;
90 pv_addr_t undstack;
91
92 void * msgbufaddr;
93 extern paddr_t msgbufphys;
94
95 int kernel_debug = 0;
96
97 /* exported variable to be filled in by the bootloaders */
98 char *booted_kernel;
99
100
101 /* Prototypes */
102
103 void data_abort_handler(trapframe_t *frame);
104 void prefetch_abort_handler(trapframe_t *frame);
105 extern void configure(void);
106
107 /*
108 * arm32_vector_init:
109 *
110 * Initialize the vector page, and select whether or not to
111 * relocate the vectors.
112 *
113 * NOTE: We expect the vector page to be mapped at its expected
114 * destination.
115 */
116 void
117 arm32_vector_init(vaddr_t va, int which)
118 {
119 extern unsigned int page0[], page0_data[];
120 unsigned int *vectors = (int *) va;
121 unsigned int *vectors_data = vectors + (page0_data - page0);
122 int vec;
123
124 /*
125 * Loop through the vectors we're taking over, and copy the
126 * vector's insn and data word.
127 */
128 for (vec = 0; vec < ARM_NVEC; vec++) {
129 if ((which & (1 << vec)) == 0) {
130 /* Don't want to take over this vector. */
131 continue;
132 }
133 vectors[vec] = page0[vec];
134 vectors_data[vec] = page0_data[vec];
135 }
136
137 /* Now sync the vectors. */
138 cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
139
140 vector_page = va;
141
142 if (va == ARM_VECTORS_HIGH) {
143 /*
144 * Assume the MD caller knows what it's doing here, and
145 * really does want the vector page relocated.
146 *
147 * Note: This has to be done here (and not just in
148 * cpu_setup()) because the vector page needs to be
149 * accessible *before* cpu_startup() is called.
150 * Think ddb(9) ...
151 *
152 * NOTE: If the CPU control register is not readable,
153 * this will totally fail! We'll just assume that
154 * any system that has high vector support has a
155 * readable CPU control register, for now. If we
156 * ever encounter one that does not, we'll have to
157 * rethink this.
158 */
159 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
160 }
161 }
162
163 /*
164 * Debug function just to park the CPU
165 */
166
167 void
168 halt(void)
169 {
170 while (1)
171 cpu_sleep(0);
172 }
173
174
175 /* Sync the discs and unmount the filesystems */
176
177 void
178 bootsync(void)
179 {
180 static bool bootsyncdone = false;
181
182 if (bootsyncdone) return;
183
184 bootsyncdone = true;
185
186 /* Make sure we can still manage to do things */
187 if (GetCPSR() & I32_bit) {
188 /*
189 * If we get here then boot has been called without RB_NOSYNC
190 * and interrupts were disabled. This means the boot() call
191 * did not come from a user process e.g. shutdown, but must
192 * have come from somewhere in the kernel.
193 */
194 IRQenable;
195 printf("Warning IRQ's disabled during boot()\n");
196 }
197
198 vfs_shutdown();
199 }
200
201 /*
202 * void cpu_startup(void)
203 *
204 * Machine dependent startup code.
205 *
206 */
207 void
208 cpu_startup(void)
209 {
210 vaddr_t minaddr;
211 vaddr_t maxaddr;
212 u_int loop;
213 char pbuf[9];
214
215 /* Set the CPU control register */
216 cpu_setup(boot_args);
217
218 /* Lock down zero page */
219 vector_page_setprot(VM_PROT_READ);
220
221 /*
222 * Give pmap a chance to set up a few more things now the vm
223 * is initialised
224 */
225 pmap_postinit();
226
227 /*
228 * Initialize error message buffer (at end of core).
229 */
230
231 /* msgbufphys was setup during the secondary boot strap */
232 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
233 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
234 msgbufphys + loop * PAGE_SIZE,
235 VM_PROT_READ|VM_PROT_WRITE, 0);
236 pmap_update(pmap_kernel());
237 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
238
239 /*
240 * Identify ourselves for the msgbuf (everything printed earlier will
241 * not be buffered).
242 */
243 printf("%s%s", copyright, version);
244
245 format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
246 printf("total memory = %s\n", pbuf);
247
248 minaddr = 0;
249
250 /*
251 * Allocate a submap for physio
252 */
253 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
254 VM_PHYS_SIZE, 0, false, NULL);
255
256 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
257 printf("avail memory = %s\n", pbuf);
258
259 struct lwp * const l = &lwp0;
260 struct pcb * const pcb = lwp_getpcb(l);
261 pcb->pcb_sp = uvm_lwp_getuarea(l) + USPACE_SVC_STACK_TOP;
262 lwp_settrapframe(l, (struct trapframe *)pcb->pcb_sp - 1);
263 }
264
265 /*
266 * machine dependent system variables.
267 */
268 static int
269 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
270 {
271 struct sysctlnode node;
272
273 if (booted_device == NULL)
274 return (EOPNOTSUPP);
275
276 node = *rnode;
277 node.sysctl_data = booted_device->dv_xname;
278 node.sysctl_size = strlen(booted_device->dv_xname) + 1;
279 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
280 }
281
282 static int
283 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
284 {
285 struct sysctlnode node;
286
287 if (booted_kernel == NULL || booted_kernel[0] == '\0')
288 return (EOPNOTSUPP);
289
290 node = *rnode;
291 node.sysctl_data = booted_kernel;
292 node.sysctl_size = strlen(booted_kernel) + 1;
293 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
294 }
295
296 static int
297 sysctl_machdep_powersave(SYSCTLFN_ARGS)
298 {
299 struct sysctlnode node = *rnode;
300 int error, newval;
301
302 newval = cpu_do_powersave;
303 node.sysctl_data = &newval;
304 if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
305 node.sysctl_flags &= ~CTLFLAG_READWRITE;
306 error = sysctl_lookup(SYSCTLFN_CALL(&node));
307 if (error || newp == NULL || newval == cpu_do_powersave)
308 return (error);
309
310 if (newval < 0 || newval > 1)
311 return (EINVAL);
312 cpu_do_powersave = newval;
313
314 return (0);
315 }
316
317 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
318 {
319
320 sysctl_createv(clog, 0, NULL, NULL,
321 CTLFLAG_PERMANENT,
322 CTLTYPE_NODE, "machdep", NULL,
323 NULL, 0, NULL, 0,
324 CTL_MACHDEP, CTL_EOL);
325
326 sysctl_createv(clog, 0, NULL, NULL,
327 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
328 CTLTYPE_INT, "debug", NULL,
329 NULL, 0, &kernel_debug, 0,
330 CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
331 sysctl_createv(clog, 0, NULL, NULL,
332 CTLFLAG_PERMANENT,
333 CTLTYPE_STRING, "booted_device", NULL,
334 sysctl_machdep_booted_device, 0, NULL, 0,
335 CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
336 sysctl_createv(clog, 0, NULL, NULL,
337 CTLFLAG_PERMANENT,
338 CTLTYPE_STRING, "booted_kernel", NULL,
339 sysctl_machdep_booted_kernel, 0, NULL, 0,
340 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
341 sysctl_createv(clog, 0, NULL, NULL,
342 CTLFLAG_PERMANENT,
343 CTLTYPE_STRUCT, "console_device", NULL,
344 sysctl_consdev, 0, NULL, sizeof(dev_t),
345 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
346 sysctl_createv(clog, 0, NULL, NULL,
347 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
348 CTLTYPE_INT, "powersave", NULL,
349 sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
350 CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
351 }
352
353 void
354 parse_mi_bootargs(char *args)
355 {
356 int integer;
357
358 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
359 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
360 if (integer)
361 boothowto |= RB_SINGLE;
362 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
363 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
364 if (integer)
365 boothowto |= RB_KDB;
366 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
367 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
368 if (integer)
369 boothowto |= RB_ASKNAME;
370
371 #ifdef PMAP_DEBUG
372 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
373 pmap_debug_level = integer;
374 pmap_debug(pmap_debug_level);
375 }
376 #endif /* PMAP_DEBUG */
377
378 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
379 bufpages = integer;*/
380
381 #if defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
382 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
383 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
384 md_root_size = integer;
385 md_root_size *= 1024;
386 if (md_root_size < 32*1024)
387 md_root_size = 32*1024;
388 if (md_root_size > 2048*1024)
389 md_root_size = 2048*1024;
390 }
391 #endif /* MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
392
393 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
394 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
395 if (integer)
396 boothowto |= AB_QUIET;
397 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
398 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
399 if (integer)
400 boothowto |= AB_VERBOSE;
401 }
402
403 #ifdef __HAVE_FAST_SOFTINTS
404 #if IPL_SOFTSERIAL != IPL_SOFTNET + 1
405 #error IPLs are screwed up
406 #elif IPL_SOFTNET != IPL_SOFTBIO + 1
407 #error IPLs are screwed up
408 #elif IPL_SOFTBIO != IPL_SOFTCLOCK + 1
409 #error IPLs are screwed up
410 #elif !(IPL_SOFTCLOCK > IPL_NONE)
411 #error IPLs are screwed up
412 #elif (IPL_NONE != 0)
413 #error IPLs are screwed up
414 #endif
415
416 #define SOFTINT2IPLMAP \
417 (((IPL_SOFTSERIAL - IPL_SOFTCLOCK) << (SOFTINT_SERIAL * 4)) | \
418 ((IPL_SOFTNET - IPL_SOFTCLOCK) << (SOFTINT_NET * 4)) | \
419 ((IPL_SOFTBIO - IPL_SOFTCLOCK) << (SOFTINT_BIO * 4)) | \
420 ((IPL_SOFTCLOCK - IPL_SOFTCLOCK) << (SOFTINT_CLOCK * 4)))
421 #define SOFTINT2IPL(l) ((SOFTINT2IPLMAP >> ((l) * 4)) & 0x0f)
422
423 /*
424 * This returns a mask of softint IPLs that be dispatch at <ipl>
425 * SOFTIPLMASK(IPL_NONE) = 0x0000000f
426 * SOFTIPLMASK(IPL_SOFTCLOCK) = 0x0000000e
427 * SOFTIPLMASK(IPL_SOFTBIO) = 0x0000000c
428 * SOFTIPLMASK(IPL_SOFTNET) = 0x00000008
429 * SOFTIPLMASK(IPL_SOFTSERIAL) = 0x00000000
430 */
431 #define SOFTIPLMASK(ipl) ((0x0f << (ipl)) & 0x0f)
432
433 void softint_switch(lwp_t *, int);
434
435 void
436 softint_trigger(uintptr_t mask)
437 {
438 curcpu()->ci_softints |= mask;
439 }
440
441 void
442 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
443 {
444 lwp_t ** lp = &curcpu()->ci_softlwps[level];
445 KASSERT(*lp == NULL || *lp == l);
446 *lp = l;
447 *machdep = 1 << SOFTINT2IPL(level);
448 KASSERT(level != SOFTINT_CLOCK || *machdep == (1 << (IPL_SOFTCLOCK - IPL_SOFTCLOCK)));
449 KASSERT(level != SOFTINT_BIO || *machdep == (1 << (IPL_SOFTBIO - IPL_SOFTCLOCK)));
450 KASSERT(level != SOFTINT_NET || *machdep == (1 << (IPL_SOFTNET - IPL_SOFTCLOCK)));
451 KASSERT(level != SOFTINT_SERIAL || *machdep == (1 << (IPL_SOFTSERIAL - IPL_SOFTCLOCK)));
452 }
453
454 void
455 dosoftints(void)
456 {
457 struct cpu_info * const ci = curcpu();
458 const int opl = ci->ci_cpl;
459 const uint32_t softiplmask = SOFTIPLMASK(opl);
460
461 splhigh();
462 for (;;) {
463 u_int softints = ci->ci_softints & softiplmask;
464 KASSERT((softints != 0) == ((ci->ci_softints >> opl) != 0));
465 KASSERT(opl == IPL_NONE || (softints & (1 << (opl - IPL_SOFTCLOCK))) == 0);
466 if (softints == 0) {
467 splx(opl);
468 return;
469 }
470 #define DOSOFTINT(n) \
471 if (ci->ci_softints & (1 << (IPL_SOFT ## n - IPL_SOFTCLOCK))) { \
472 ci->ci_softints &= \
473 ~(1 << (IPL_SOFT ## n - IPL_SOFTCLOCK)); \
474 softint_switch(ci->ci_softlwps[SOFTINT_ ## n], \
475 IPL_SOFT ## n); \
476 continue; \
477 }
478 DOSOFTINT(SERIAL);
479 DOSOFTINT(NET);
480 DOSOFTINT(BIO);
481 DOSOFTINT(CLOCK);
482 panic("dosoftints wtf (softints=%u?, ipl=%d)", softints, opl);
483 }
484 }
485 #endif /* __HAVE_FAST_SOFTINTS */
486
487 #ifdef MODULAR
488 /*
489 * Push any modules loaded by the boot loader.
490 */
491 void
492 module_init_md(void)
493 {
494 }
495 #endif /* MODULAR */
496
497 int
498 mm_md_physacc(paddr_t pa, vm_prot_t prot)
499 {
500
501 return (pa < ctob(physmem)) ? 0 : EFAULT;
502 }
503