arm32_machdep.c revision 1.72 1 /* $NetBSD: arm32_machdep.c,v 1.72 2010/01/18 23:04:30 jmmv 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 dependant 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.72 2010/01/18 23:04:30 jmmv 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/kernel.h>
56 #include <sys/mbuf.h>
57 #include <sys/mount.h>
58 #include <sys/buf.h>
59 #include <sys/msgbuf.h>
60 #include <sys/device.h>
61 #include <uvm/uvm_extern.h>
62 #include <sys/sysctl.h>
63 #include <sys/cpu.h>
64 #include <sys/module.h>
65
66 #include <dev/cons.h>
67
68 #include <arm/arm32/katelib.h>
69 #include <arm/arm32/machdep.h>
70 #include <machine/bootconfig.h>
71
72 #include "md.h"
73
74 struct vm_map *mb_map = NULL;
75 struct vm_map *phys_map = NULL;
76
77 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
78 extern size_t md_root_size; /* Memory disc size */
79 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
80
81 pv_addr_t kernelstack;
82
83 void * msgbufaddr;
84 extern paddr_t msgbufphys;
85
86 int kernel_debug = 0;
87
88 /* exported variable to be filled in by the bootloaders */
89 char *booted_kernel;
90
91
92 /* Prototypes */
93
94 void data_abort_handler(trapframe_t *frame);
95 void prefetch_abort_handler(trapframe_t *frame);
96 extern void configure(void);
97
98 /*
99 * arm32_vector_init:
100 *
101 * Initialize the vector page, and select whether or not to
102 * relocate the vectors.
103 *
104 * NOTE: We expect the vector page to be mapped at its expected
105 * destination.
106 */
107 void
108 arm32_vector_init(vaddr_t va, int which)
109 {
110 extern unsigned int page0[], page0_data[];
111 unsigned int *vectors = (int *) va;
112 unsigned int *vectors_data = vectors + (page0_data - page0);
113 int vec;
114
115 /*
116 * Loop through the vectors we're taking over, and copy the
117 * vector's insn and data word.
118 */
119 for (vec = 0; vec < ARM_NVEC; vec++) {
120 if ((which & (1 << vec)) == 0) {
121 /* Don't want to take over this vector. */
122 continue;
123 }
124 vectors[vec] = page0[vec];
125 vectors_data[vec] = page0_data[vec];
126 }
127
128 /* Now sync the vectors. */
129 cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
130
131 vector_page = va;
132
133 if (va == ARM_VECTORS_HIGH) {
134 /*
135 * Assume the MD caller knows what it's doing here, and
136 * really does want the vector page relocated.
137 *
138 * Note: This has to be done here (and not just in
139 * cpu_setup()) because the vector page needs to be
140 * accessible *before* cpu_startup() is called.
141 * Think ddb(9) ...
142 *
143 * NOTE: If the CPU control register is not readable,
144 * this will totally fail! We'll just assume that
145 * any system that has high vector support has a
146 * readable CPU control register, for now. If we
147 * ever encounter one that does not, we'll have to
148 * rethink this.
149 */
150 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
151 }
152 }
153
154 /*
155 * Debug function just to park the CPU
156 */
157
158 void
159 halt(void)
160 {
161 while (1)
162 cpu_sleep(0);
163 }
164
165
166 /* Sync the discs and unmount the filesystems */
167
168 void
169 bootsync(void)
170 {
171 static bool bootsyncdone = false;
172
173 if (bootsyncdone) return;
174
175 bootsyncdone = true;
176
177 /* Make sure we can still manage to do things */
178 if (GetCPSR() & I32_bit) {
179 /*
180 * If we get here then boot has been called without RB_NOSYNC
181 * and interrupts were disabled. This means the boot() call
182 * did not come from a user process e.g. shutdown, but must
183 * have come from somewhere in the kernel.
184 */
185 IRQenable;
186 printf("Warning IRQ's disabled during boot()\n");
187 }
188
189 vfs_shutdown();
190 }
191
192 /*
193 * void cpu_startup(void)
194 *
195 * Machine dependant startup code.
196 *
197 */
198 void
199 cpu_startup(void)
200 {
201 vaddr_t minaddr;
202 vaddr_t maxaddr;
203 u_int loop;
204 char pbuf[9];
205
206 /* Set the CPU control register */
207 cpu_setup(boot_args);
208
209 /* Lock down zero page */
210 vector_page_setprot(VM_PROT_READ);
211
212 /*
213 * Give pmap a chance to set up a few more things now the vm
214 * is initialised
215 */
216 pmap_postinit();
217
218 /*
219 * Initialize error message buffer (at end of core).
220 */
221
222 /* msgbufphys was setup during the secondary boot strap */
223 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
224 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
225 msgbufphys + loop * PAGE_SIZE,
226 VM_PROT_READ|VM_PROT_WRITE, 0);
227 pmap_update(pmap_kernel());
228 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
229
230 /*
231 * Identify ourselves for the msgbuf (everything printed earlier will
232 * not be buffered).
233 */
234 printf("%s%s", copyright, version);
235
236 format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
237 printf("total memory = %s\n", pbuf);
238
239 minaddr = 0;
240
241 /*
242 * Allocate a submap for physio
243 */
244 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
245 VM_PHYS_SIZE, 0, false, NULL);
246
247 /*
248 * Finally, allocate mbuf cluster submap.
249 */
250 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
251 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
252 false, NULL);
253
254 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
255 printf("avail memory = %s\n", pbuf);
256
257 curpcb = lwp_getpcb(&lwp0);
258 curpcb->pcb_flags = 0;
259 curpcb->pcb_un.un_32.pcb32_sp =
260 uvm_lwp_getuarea(&lwp0) + USPACE_SVC_STACK_TOP;
261 curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
262 }
263
264 /*
265 * machine dependent system variables.
266 */
267 static int
268 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
269 {
270 struct sysctlnode node;
271
272 if (booted_device == NULL)
273 return (EOPNOTSUPP);
274
275 node = *rnode;
276 node.sysctl_data = booted_device->dv_xname;
277 node.sysctl_size = strlen(booted_device->dv_xname) + 1;
278 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
279 }
280
281 static int
282 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
283 {
284 struct sysctlnode node;
285
286 if (booted_kernel == NULL || booted_kernel[0] == '\0')
287 return (EOPNOTSUPP);
288
289 node = *rnode;
290 node.sysctl_data = booted_kernel;
291 node.sysctl_size = strlen(booted_kernel) + 1;
292 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
293 }
294
295 static int
296 sysctl_machdep_powersave(SYSCTLFN_ARGS)
297 {
298 struct sysctlnode node = *rnode;
299 int error, newval;
300
301 newval = cpu_do_powersave;
302 node.sysctl_data = &newval;
303 if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
304 node.sysctl_flags &= ~CTLFLAG_READWRITE;
305 error = sysctl_lookup(SYSCTLFN_CALL(&node));
306 if (error || newp == NULL || newval == cpu_do_powersave)
307 return (error);
308
309 if (newval < 0 || newval > 1)
310 return (EINVAL);
311 cpu_do_powersave = newval;
312
313 return (0);
314 }
315
316 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
317 {
318
319 sysctl_createv(clog, 0, NULL, NULL,
320 CTLFLAG_PERMANENT,
321 CTLTYPE_NODE, "machdep", NULL,
322 NULL, 0, NULL, 0,
323 CTL_MACHDEP, CTL_EOL);
324
325 sysctl_createv(clog, 0, NULL, NULL,
326 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
327 CTLTYPE_INT, "debug", NULL,
328 NULL, 0, &kernel_debug, 0,
329 CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
330 sysctl_createv(clog, 0, NULL, NULL,
331 CTLFLAG_PERMANENT,
332 CTLTYPE_STRING, "booted_device", NULL,
333 sysctl_machdep_booted_device, 0, NULL, 0,
334 CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
335 sysctl_createv(clog, 0, NULL, NULL,
336 CTLFLAG_PERMANENT,
337 CTLTYPE_STRING, "booted_kernel", NULL,
338 sysctl_machdep_booted_kernel, 0, NULL, 0,
339 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
340 sysctl_createv(clog, 0, NULL, NULL,
341 CTLFLAG_PERMANENT,
342 CTLTYPE_STRUCT, "console_device", NULL,
343 sysctl_consdev, 0, NULL, sizeof(dev_t),
344 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
345 sysctl_createv(clog, 0, NULL, NULL,
346 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
347 CTLTYPE_INT, "powersave", NULL,
348 sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
349 CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
350 }
351
352 void
353 parse_mi_bootargs(char *args)
354 {
355 int integer;
356
357 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
358 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
359 if (integer)
360 boothowto |= RB_SINGLE;
361 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
362 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
363 if (integer)
364 boothowto |= RB_KDB;
365 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
366 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
367 if (integer)
368 boothowto |= RB_ASKNAME;
369
370 #ifdef PMAP_DEBUG
371 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
372 pmap_debug_level = integer;
373 pmap_debug(pmap_debug_level);
374 }
375 #endif /* PMAP_DEBUG */
376
377 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
378 bufpages = integer;*/
379
380 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
381 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
382 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
383 md_root_size = integer;
384 md_root_size *= 1024;
385 if (md_root_size < 32*1024)
386 md_root_size = 32*1024;
387 if (md_root_size > 2048*1024)
388 md_root_size = 2048*1024;
389 }
390 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
391
392 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
393 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
394 if (integer)
395 boothowto |= AB_QUIET;
396 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
397 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
398 if (integer)
399 boothowto |= AB_VERBOSE;
400 }
401
402 #ifdef __HAVE_FAST_SOFTINTS
403 #if IPL_SOFTSERIAL != IPL_SOFTNET + 1
404 #error IPLs are screwed up
405 #elif IPL_SOFTNET != IPL_SOFTBIO + 1
406 #error IPLs are screwed up
407 #elif IPL_SOFTBIO != IPL_SOFTCLOCK + 1
408 #error IPLs are screwed up
409 #elif !(IPL_SOFTCLOCK > IPL_NONE)
410 #error IPLs are screwed up
411 #elif (IPL_NONE != 0)
412 #error IPLs are screwed up
413 #endif
414
415 #define SOFTINT2IPLMAP \
416 (((IPL_SOFTSERIAL - IPL_SOFTCLOCK) << (SOFTINT_SERIAL * 4)) | \
417 ((IPL_SOFTNET - IPL_SOFTCLOCK) << (SOFTINT_NET * 4)) | \
418 ((IPL_SOFTBIO - IPL_SOFTCLOCK) << (SOFTINT_BIO * 4)) | \
419 ((IPL_SOFTCLOCK - IPL_SOFTCLOCK) << (SOFTINT_CLOCK * 4)))
420 #define SOFTINT2IPL(l) ((SOFTINT2IPLMAP >> ((l) * 4)) & 0x0f)
421
422 /*
423 * This returns a mask of softint IPLs that be dispatch at <ipl>
424 * SOFTIPLMASK(IPL_NONE) = 0x0000000f
425 * SOFTIPLMASK(IPL_SOFTCLOCK) = 0x0000000e
426 * SOFTIPLMASK(IPL_SOFTBIO) = 0x0000000c
427 * SOFTIPLMASK(IPL_SOFTNET) = 0x00000008
428 * SOFTIPLMASK(IPL_SOFTSERIAL) = 0x00000000
429 */
430 #define SOFTIPLMASK(ipl) (0x0f << (ipl))
431
432 void softint_switch(lwp_t *, int);
433
434 void
435 softint_trigger(uintptr_t mask)
436 {
437 curcpu()->ci_softints |= mask;
438 }
439
440 void
441 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
442 {
443 lwp_t ** lp = &curcpu()->ci_softlwps[level];
444 KASSERT(*lp == NULL || *lp == l);
445 *lp = l;
446 *machdep = 1 << SOFTINT2IPL(level);
447 KASSERT(level != SOFTINT_CLOCK || *machdep == (1 << (IPL_SOFTCLOCK - IPL_SOFTCLOCK)));
448 KASSERT(level != SOFTINT_BIO || *machdep == (1 << (IPL_SOFTBIO - IPL_SOFTCLOCK)));
449 KASSERT(level != SOFTINT_NET || *machdep == (1 << (IPL_SOFTNET - IPL_SOFTCLOCK)));
450 KASSERT(level != SOFTINT_SERIAL || *machdep == (1 << (IPL_SOFTSERIAL - IPL_SOFTCLOCK)));
451 }
452
453 void
454 dosoftints(void)
455 {
456 struct cpu_info * const ci = curcpu();
457 const int opl = ci->ci_cpl;
458 const uint32_t softiplmask = SOFTIPLMASK(opl);
459
460 for (;;) {
461 u_int softints = ci->ci_softints & softiplmask;
462 KASSERT((softints != 0) == ((ci->ci_softints >> opl) != 0));
463 if (softints == 0)
464 return;
465 ci->ci_cpl = IPL_HIGH;
466 #define DOSOFTINT(n) \
467 if (softints & (1 << (IPL_SOFT ## n - IPL_SOFTCLOCK))) { \
468 ci->ci_softints &= \
469 ~(1 << (IPL_SOFT ## n - IPL_SOFTCLOCK)); \
470 softint_switch(ci->ci_softlwps[SOFTINT_ ## n], \
471 IPL_SOFT ## n); \
472 ci->ci_cpl = opl; \
473 continue; \
474 }
475 DOSOFTINT(SERIAL);
476 DOSOFTINT(NET);
477 DOSOFTINT(BIO);
478 DOSOFTINT(CLOCK);
479 panic("dosoftints wtf (softints=%u?, ipl=%d)", softints, opl);
480 }
481 }
482 #endif /* __HAVE_FAST_SOFTINTS */
483
484 #ifdef MODULAR
485 /*
486 * Push any modules loaded by the boot loader.
487 */
488 void
489 module_init_md(void)
490 {
491 }
492 #endif /* MODULAR */
493