arm32_machdep.c revision 1.41 1 /* $NetBSD: arm32_machdep.c,v 1.41 2003/12/13 12:07:41 rearnsha 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.41 2003/12/13 12:07:41 rearnsha 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/user.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
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 "opt_ipkdb.h"
71 #include "md.h"
72
73 struct vm_map *exec_map = NULL;
74 struct vm_map *mb_map = NULL;
75 struct vm_map *phys_map = NULL;
76
77 extern int physmem;
78
79 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
80 extern size_t md_root_size; /* Memory disc size */
81 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
82
83 pv_addr_t kernelstack;
84
85 /* the following is used externally (sysctl_hw) */
86 char machine[] = MACHINE; /* from <machine/param.h> */
87 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
88
89 /* Our exported CPU info; we can have only one. */
90 struct cpu_info cpu_info_store;
91
92 caddr_t msgbufaddr;
93 extern paddr_t msgbufphys;
94
95 int kernel_debug = 0;
96
97 struct user *proc0paddr;
98
99 /* exported variable to be filled in by the bootloaders */
100 char *booted_kernel;
101
102
103 /* Prototypes */
104
105 void data_abort_handler __P((trapframe_t *frame));
106 void prefetch_abort_handler __P((trapframe_t *frame));
107 extern void configure __P((void));
108
109 /*
110 * arm32_vector_init:
111 *
112 * Initialize the vector page, and select whether or not to
113 * relocate the vectors.
114 *
115 * NOTE: We expect the vector page to be mapped at its expected
116 * destination.
117 */
118 void
119 arm32_vector_init(vaddr_t va, int which)
120 {
121 extern unsigned int page0[], page0_data[];
122 unsigned int *vectors = (int *) va;
123 unsigned int *vectors_data = vectors + (page0_data - page0);
124 int vec;
125
126 /*
127 * Loop through the vectors we're taking over, and copy the
128 * vector's insn and data word.
129 */
130 for (vec = 0; vec < ARM_NVEC; vec++) {
131 if ((which & (1 << vec)) == 0) {
132 /* Don't want to take over this vector. */
133 continue;
134 }
135 vectors[vec] = page0[vec];
136 vectors_data[vec] = page0_data[vec];
137 }
138
139 /* Now sync the vectors. */
140 cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
141
142 vector_page = va;
143
144 if (va == ARM_VECTORS_HIGH) {
145 /*
146 * Assume the MD caller knows what it's doing here, and
147 * really does want the vector page relocated.
148 *
149 * Note: This has to be done here (and not just in
150 * cpu_setup()) because the vector page needs to be
151 * accessible *before* cpu_startup() is called.
152 * Think ddb(9) ...
153 *
154 * NOTE: If the CPU control register is not readable,
155 * this will totally fail! We'll just assume that
156 * any system that has high vector support has a
157 * readable CPU control register, for now. If we
158 * ever encounter one that does not, we'll have to
159 * rethink this.
160 */
161 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
162 }
163 }
164
165 /*
166 * Debug function just to park the CPU
167 */
168
169 void
170 halt()
171 {
172 while (1)
173 cpu_sleep(0);
174 }
175
176
177 /* Sync the discs and unmount the filesystems */
178
179 void
180 bootsync(void)
181 {
182 static int bootsyncdone = 0;
183
184 if (bootsyncdone) return;
185
186 bootsyncdone = 1;
187
188 /* Make sure we can still manage to do things */
189 if (GetCPSR() & I32_bit) {
190 /*
191 * If we get here then boot has been called without RB_NOSYNC
192 * and interrupts were disabled. This means the boot() call
193 * did not come from a user process e.g. shutdown, but must
194 * have come from somewhere in the kernel.
195 */
196 IRQenable;
197 printf("Warning IRQ's disabled during boot()\n");
198 }
199
200 vfs_shutdown();
201 }
202
203 /*
204 * void cpu_startup(void)
205 *
206 * Machine dependant startup code.
207 *
208 */
209 void
210 cpu_startup()
211 {
212 paddr_t minaddr;
213 paddr_t maxaddr;
214 caddr_t sysbase;
215 caddr_t size;
216 vsize_t bufsize;
217 u_int loop, base, residual;
218 char pbuf[9];
219
220 /* Set the cpu control register */
221 cpu_setup(boot_args);
222
223 /* Lock down zero page */
224 vector_page_setprot(VM_PROT_READ);
225
226 /*
227 * Give pmap a chance to set up a few more things now the vm
228 * is initialised
229 */
230 pmap_postinit();
231
232 /*
233 * Initialize error message buffer (at end of core).
234 */
235
236 /* msgbufphys was setup during the secondary boot strap */
237 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
238 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
239 msgbufphys + loop * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
240 pmap_update(pmap_kernel());
241 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
242
243 /*
244 * Identify ourselves for the msgbuf (everything printed earlier will
245 * not be buffered).
246 */
247 printf(version);
248
249 format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
250 printf("total memory = %s\n", pbuf);
251
252 /*
253 * Find out how much space we need, allocate it,
254 * and then give everything true virtual addresses.
255 */
256 size = allocsys(NULL, NULL);
257 sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
258 if (sysbase == 0)
259 panic(
260 "cpu_startup: no room for system tables; %d bytes required",
261 (u_int)size);
262 if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
263 panic("cpu_startup: system table size inconsistency");
264
265 /*
266 * Now allocate buffers proper. They are different than the above
267 * in that they usually occupy more virtual memory than physical.
268 */
269 bufsize = MAXBSIZE * nbuf;
270 if (uvm_map(kernel_map, (void *)&buffers, round_page(bufsize),
271 NULL, UVM_UNKNOWN_OFFSET, 0,
272 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
273 UVM_ADV_NORMAL, 0)) != 0)
274 panic("cpu_startup: cannot allocate UVM space for buffers");
275 minaddr = (vaddr_t)buffers;
276 if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
277 /* don't want to alloc more physical mem than needed */
278 bufpages = btoc(MAXBSIZE) * nbuf;
279 }
280
281 base = bufpages / nbuf;
282 residual = bufpages % nbuf;
283 for (loop = 0; loop < nbuf; ++loop) {
284 vsize_t curbufsize;
285 vaddr_t curbuf;
286 struct vm_page *pg;
287
288 /*
289 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
290 * that MAXBSIZE space, we allocate and map (base+1) pages
291 * for the first "residual" buffers, and then we allocate
292 * "base" pages for the rest.
293 */
294 curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
295 curbufsize = PAGE_SIZE * ((loop < residual) ? (base+1) : base);
296
297 while (curbufsize) {
298 pg = uvm_pagealloc(NULL, 0, NULL, 0);
299 if (pg == NULL)
300 panic("cpu_startup: not enough memory for buffer cache");
301 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
302 VM_PROT_READ|VM_PROT_WRITE);
303 curbuf += PAGE_SIZE;
304 curbufsize -= PAGE_SIZE;
305 }
306 }
307 pmap_update(pmap_kernel());
308
309 /*
310 * Allocate a submap for exec arguments. This map effectively
311 * limits the number of processes exec'ing at any time.
312 */
313 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
314 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
315
316 /*
317 * Allocate a submap for physio
318 */
319 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
320 VM_PHYS_SIZE, 0, FALSE, NULL);
321
322 /*
323 * Finally, allocate mbuf cluster submap.
324 */
325 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
326 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
327 FALSE, NULL);
328
329 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
330 printf("avail memory = %s\n", pbuf);
331 format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
332 printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
333
334 /*
335 * Set up buffers, so they can be used to read disk labels.
336 */
337 bufinit();
338
339 curpcb = &lwp0.l_addr->u_pcb;
340 curpcb->pcb_flags = 0;
341 curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
342 USPACE_UNDEF_STACK_TOP;
343 curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
344 USPACE_SVC_STACK_TOP;
345 pmap_set_pcb_pagedir(pmap_kernel(), curpcb);
346
347 curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
348 }
349
350 /*
351 * machine dependent system variables.
352 */
353 static int
354 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
355 {
356 struct sysctlnode node;
357
358 if (booted_device == NULL)
359 return (EOPNOTSUPP);
360
361 node = *rnode;
362 node.sysctl_data = booted_device->dv_xname;
363 node.sysctl_size = strlen(booted_device->dv_xname) + 1;
364 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
365 }
366
367 static int
368 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
369 {
370 struct sysctlnode node;
371
372 if (booted_kernel == NULL || booted_kernel[0] == '\0')
373 return (EOPNOTSUPP);
374
375 node = *rnode;
376 node.sysctl_data = booted_kernel;
377 node.sysctl_size = strlen(booted_kernel) + 1;
378 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
379 }
380
381 static int
382 sysctl_machdep_powersave(SYSCTLFN_ARGS)
383 {
384 struct sysctlnode node = *rnode;
385 int error, newval;
386
387 newval = cpu_do_powersave;
388 node.sysctl_data = &newval;
389 if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
390 node.sysctl_flags &= ~SYSCTL_READWRITE;
391 error = sysctl_lookup(SYSCTLFN_CALL(&node));
392 if (error || newp == NULL || newval == cpu_do_powersave)
393 return (error);
394
395 if (newval < 0 || newval > 1)
396 return (EINVAL);
397 cpu_do_powersave = newval;
398
399 return (0);
400 }
401
402 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
403 {
404
405 sysctl_createv(SYSCTL_PERMANENT,
406 CTLTYPE_NODE, "machdep", NULL,
407 NULL, 0, NULL, 0,
408 CTL_MACHDEP, CTL_EOL);
409
410 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
411 CTLTYPE_INT, "debug", NULL,
412 NULL, 0, &kernel_debug, 0,
413 CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
414 sysctl_createv(SYSCTL_PERMANENT,
415 CTLTYPE_STRING, "booted_device", NULL,
416 sysctl_machdep_booted_device, 0, NULL, 0,
417 CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
418 sysctl_createv(SYSCTL_PERMANENT,
419 CTLTYPE_STRING, "booted_kernel", NULL,
420 sysctl_machdep_booted_kernel, 0, NULL, 0,
421 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
422 sysctl_createv(SYSCTL_PERMANENT,
423 CTLTYPE_STRUCT, "console_device", NULL,
424 sysctl_consdev, 0, NULL, sizeof(dev_t),
425 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
426 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
427 CTLTYPE_INT, "powersave", NULL,
428 sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
429 CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
430 }
431
432 void
433 parse_mi_bootargs(args)
434 char *args;
435 {
436 int integer;
437
438 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
439 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
440 if (integer)
441 boothowto |= RB_SINGLE;
442 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
443 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
444 if (integer)
445 boothowto |= RB_KDB;
446 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
447 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
448 if (integer)
449 boothowto |= RB_ASKNAME;
450
451 #ifdef PMAP_DEBUG
452 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
453 pmap_debug_level = integer;
454 pmap_debug(pmap_debug_level);
455 }
456 #endif /* PMAP_DEBUG */
457
458 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
459 bufpages = integer;*/
460
461 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
462 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
463 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
464 md_root_size = integer;
465 md_root_size *= 1024;
466 if (md_root_size < 32*1024)
467 md_root_size = 32*1024;
468 if (md_root_size > 2048*1024)
469 md_root_size = 2048*1024;
470 }
471 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
472
473 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
474 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
475 if (integer)
476 boothowto |= AB_QUIET;
477 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
478 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
479 if (integer)
480 boothowto |= AB_VERBOSE;
481 }
482