arm32_machdep.c revision 1.33 1 /* $NetBSD: arm32_machdep.c,v 1.33 2003/05/21 18:04:42 thorpej 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 "opt_md.h"
45 #include "opt_pmap_debug.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/reboot.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52 #include <sys/kernel.h>
53 #include <sys/mbuf.h>
54 #include <sys/mount.h>
55 #include <sys/buf.h>
56 #include <sys/msgbuf.h>
57 #include <sys/device.h>
58 #include <uvm/uvm_extern.h>
59 #include <sys/sysctl.h>
60
61 #include <dev/cons.h>
62
63 #include <arm/arm32/katelib.h>
64 #include <arm/arm32/machdep.h>
65 #include <machine/bootconfig.h>
66
67 #include "opt_ipkdb.h"
68 #include "md.h"
69
70 struct vm_map *exec_map = NULL;
71 struct vm_map *mb_map = NULL;
72 struct vm_map *phys_map = NULL;
73
74 extern int physmem;
75
76 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
77 extern size_t md_root_size; /* Memory disc size */
78 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
79
80 pv_addr_t kernelstack;
81
82 /* the following is used externally (sysctl_hw) */
83 char machine[] = MACHINE; /* from <machine/param.h> */
84 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
85
86 /* Our exported CPU info; we can have only one. */
87 struct cpu_info cpu_info_store;
88
89 caddr_t msgbufaddr;
90 extern paddr_t msgbufphys;
91
92 int kernel_debug = 0;
93
94 struct user *proc0paddr;
95
96 /* exported variable to be filled in by the bootloaders */
97 char *booted_kernel;
98
99
100 /* Prototypes */
101
102 u_long strtoul __P((const char *s, char **ptr, int base));
103 void data_abort_handler __P((trapframe_t *frame));
104 void prefetch_abort_handler __P((trapframe_t *frame));
105 extern void configure __P((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()
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 int bootsyncdone = 0;
181
182 if (bootsyncdone) return;
183
184 bootsyncdone = 1;
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 dependant startup code.
205 *
206 */
207 void
208 cpu_startup()
209 {
210 paddr_t minaddr;
211 paddr_t maxaddr;
212 caddr_t sysbase;
213 caddr_t size;
214 vsize_t bufsize;
215 u_int loop, base, residual;
216 char pbuf[9];
217
218 /* Set the cpu control register */
219 cpu_setup(boot_args);
220
221 /* Lock down zero page */
222 vector_page_setprot(VM_PROT_READ);
223
224 /*
225 * Give pmap a chance to set up a few more things now the vm
226 * is initialised
227 */
228 pmap_postinit();
229
230 /*
231 * Initialize error message buffer (at end of core).
232 */
233
234 /* msgbufphys was setup during the secondary boot strap */
235 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
236 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
237 msgbufphys + loop * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
238 pmap_update(pmap_kernel());
239 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
240
241 /*
242 * Identify ourselves for the msgbuf (everything printed earlier will
243 * not be buffered).
244 */
245 printf(version);
246
247 format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
248 printf("total memory = %s\n", pbuf);
249
250 /*
251 * Find out how much space we need, allocate it,
252 * and then give everything true virtual addresses.
253 */
254 size = allocsys(NULL, NULL);
255 sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
256 if (sysbase == 0)
257 panic(
258 "cpu_startup: no room for system tables; %d bytes required",
259 (u_int)size);
260 if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
261 panic("cpu_startup: system table size inconsistency");
262
263 /*
264 * Now allocate buffers proper. They are different than the above
265 * in that they usually occupy more virtual memory than physical.
266 */
267 bufsize = MAXBSIZE * nbuf;
268 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(bufsize),
269 NULL, UVM_UNKNOWN_OFFSET, 0,
270 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
271 UVM_ADV_NORMAL, 0)) != 0)
272 panic("cpu_startup: cannot allocate UVM space for buffers");
273 minaddr = (vaddr_t)buffers;
274 if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
275 /* don't want to alloc more physical mem than needed */
276 bufpages = btoc(MAXBSIZE) * nbuf;
277 }
278
279 base = bufpages / nbuf;
280 residual = bufpages % nbuf;
281 for (loop = 0; loop < nbuf; ++loop) {
282 vsize_t curbufsize;
283 vaddr_t curbuf;
284 struct vm_page *pg;
285
286 /*
287 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
288 * that MAXBSIZE space, we allocate and map (base+1) pages
289 * for the first "residual" buffers, and then we allocate
290 * "base" pages for the rest.
291 */
292 curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
293 curbufsize = PAGE_SIZE * ((loop < residual) ? (base+1) : base);
294
295 while (curbufsize) {
296 pg = uvm_pagealloc(NULL, 0, NULL, 0);
297 if (pg == NULL)
298 panic("cpu_startup: not enough memory for buffer cache");
299 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
300 VM_PROT_READ|VM_PROT_WRITE);
301 curbuf += PAGE_SIZE;
302 curbufsize -= PAGE_SIZE;
303 }
304 }
305 pmap_update(pmap_kernel());
306
307 /*
308 * Allocate a submap for exec arguments. This map effectively
309 * limits the number of processes exec'ing at any time.
310 */
311 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
312 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
313
314 /*
315 * Allocate a submap for physio
316 */
317 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
318 VM_PHYS_SIZE, 0, FALSE, NULL);
319
320 /*
321 * Finally, allocate mbuf cluster submap.
322 */
323 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
324 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
325 FALSE, NULL);
326
327 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
328 printf("avail memory = %s\n", pbuf);
329 format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
330 printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
331
332 /*
333 * Set up buffers, so they can be used to read disk labels.
334 */
335 bufinit();
336
337 curpcb = &lwp0.l_addr->u_pcb;
338 curpcb->pcb_flags = 0;
339 curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
340 USPACE_UNDEF_STACK_TOP;
341 curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
342 USPACE_SVC_STACK_TOP;
343 pmap_set_pcb_pagedir(pmap_kernel(), curpcb);
344
345 curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
346 }
347
348 /*
349 * machine dependent system variables.
350 */
351
352 int
353 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
354 int *name;
355 u_int namelen;
356 void *oldp;
357 size_t *oldlenp;
358 void *newp;
359 size_t newlen;
360 struct proc *p;
361 {
362 /* all sysctl names at this level are terminal */
363 if (namelen != 1)
364 return (ENOTDIR); /* overloaded */
365
366 switch (name[0]) {
367 case CPU_DEBUG:
368 return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
369
370 case CPU_BOOTED_DEVICE:
371 if (booted_device != NULL)
372 return (sysctl_rdstring(oldp, oldlenp, newp,
373 booted_device->dv_xname));
374 return (EOPNOTSUPP);
375
376 case CPU_CONSDEV: {
377 dev_t consdev;
378 if (cn_tab != NULL)
379 consdev = cn_tab->cn_dev;
380 else
381 consdev = NODEV;
382 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
383 sizeof consdev));
384 }
385 case CPU_BOOTED_KERNEL: {
386 if (booted_kernel != NULL && booted_kernel[0] != '\0')
387 return sysctl_rdstring(oldp, oldlenp, newp,
388 booted_kernel);
389 return (EOPNOTSUPP);
390 }
391 case CPU_POWERSAVE: {
392 int error, newval;
393
394 newval = cpu_do_powersave;
395
396 if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
397 error = sysctl_rdint(oldp, oldlenp, newp, newval);
398 else
399 error = sysctl_int(oldp, oldlenp, newp, newlen,
400 &newval);
401 if (error || newval == cpu_do_powersave)
402 return (error);
403
404 if (newval < 0 || newval > 1)
405 return (EINVAL);
406
407 cpu_do_powersave = newval;
408 return (0);
409 }
410
411 default:
412 return (EOPNOTSUPP);
413 }
414 /* NOTREACHED */
415 }
416
417 void
418 parse_mi_bootargs(args)
419 char *args;
420 {
421 int integer;
422
423 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
424 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
425 if (integer)
426 boothowto |= RB_SINGLE;
427 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
428 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
429 if (integer)
430 boothowto |= RB_KDB;
431 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
432 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
433 if (integer)
434 boothowto |= RB_ASKNAME;
435
436 #ifdef PMAP_DEBUG
437 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
438 pmap_debug_level = integer;
439 pmap_debug(pmap_debug_level);
440 }
441 #endif /* PMAP_DEBUG */
442
443 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
444 bufpages = integer;*/
445
446 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
447 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
448 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
449 md_root_size = integer;
450 md_root_size *= 1024;
451 if (md_root_size < 32*1024)
452 md_root_size = 32*1024;
453 if (md_root_size > 2048*1024)
454 md_root_size = 2048*1024;
455 }
456 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
457
458 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
459 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
460 if (integer)
461 boothowto |= AB_QUIET;
462 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
463 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
464 if (integer)
465 boothowto |= AB_VERBOSE;
466 }
467