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