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