arm32_machdep.c revision 1.14 1 /* $NetBSD: arm32_machdep.c,v 1.14 2002/02/20 02:32:57 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 "opt_mdsize.h"
69 #include "md.h"
70
71 struct vm_map *exec_map = NULL;
72 struct vm_map *mb_map = NULL;
73 struct vm_map *phys_map = NULL;
74
75 extern int physmem;
76
77 #ifndef PMAP_STATIC_L1S
78 extern int max_processes;
79 #endif /* !PMAP_STATIC_L1S */
80 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MINIROOTSIZE)
81 extern u_int memory_disc_size; /* Memory disc size */
82 #endif /* NMD && MEMORY_DISK_HOOKS && !MINIROOTSIZE */
83
84 pv_addr_t systempage;
85 pv_addr_t kernelstack;
86
87 /* the following is used externally (sysctl_hw) */
88 char machine[] = MACHINE; /* from <machine/param.h> */
89 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
90
91 /* Our exported CPU info; we can have only one. */
92 struct cpu_info cpu_info_store;
93
94 extern pt_entry_t msgbufpte;
95 caddr_t msgbufaddr;
96 extern paddr_t msgbufphys;
97
98 int kernel_debug = 0;
99
100 struct user *proc0paddr;
101
102 /* exported variable to be filled in by the bootloaders */
103 char *booted_kernel;
104
105
106 /* Prototypes */
107
108 u_long strtoul __P((const char *s, char **ptr, int base));
109 void data_abort_handler __P((trapframe_t *frame));
110 void prefetch_abort_handler __P((trapframe_t *frame));
111 void zero_page_readonly __P((void));
112 void zero_page_readwrite __P((void));
113 extern void configure __P((void));
114
115 /*
116 * Debug function just to park the CPU
117 */
118
119 void
120 halt()
121 {
122 while (1)
123 cpu_sleep(0);
124 }
125
126
127 /* Sync the discs and unmount the filesystems */
128
129 void
130 bootsync(void)
131 {
132 static int bootsyncdone = 0;
133
134 if (bootsyncdone) return;
135
136 bootsyncdone = 1;
137
138 /* Make sure we can still manage to do things */
139 if (GetCPSR() & I32_bit) {
140 /*
141 * If we get here then boot has been called without RB_NOSYNC
142 * and interrupts were disabled. This means the boot() call
143 * did not come from a user process e.g. shutdown, but must
144 * have come from somewhere in the kernel.
145 */
146 IRQenable;
147 printf("Warning IRQ's disabled during boot()\n");
148 }
149
150 vfs_shutdown();
151 }
152
153 /*
154 * A few functions that are used to help construct the page tables
155 * during the bootstrap process.
156 */
157
158 void
159 map_pagetable(pagetable, va, pa)
160 vaddr_t pagetable;
161 vaddr_t va;
162 paddr_t pa;
163 {
164 #ifdef DIAGNOSTIC
165 if ((pa & 0xc00) != 0)
166 panic("pagetables should be group allocated on pageboundry");
167 #endif /* DIAGNOSTIC */
168
169 ((u_int *)pagetable)[(va >> PDSHIFT) + 0] =
170 L1_PTE((pa & PG_FRAME) + 0x000);
171 ((u_int *)pagetable)[(va >> PDSHIFT) + 1] =
172 L1_PTE((pa & PG_FRAME) + 0x400);
173 ((u_int *)pagetable)[(va >> PDSHIFT) + 2] =
174 L1_PTE((pa & PG_FRAME) + 0x800);
175 ((u_int *)pagetable)[(va >> PDSHIFT) + 3] =
176 L1_PTE((pa & PG_FRAME) + 0xc00);
177 }
178
179 /* cats kernels have a 2nd l2 pt, so the range is bigger hence the 0x7ff etc */
180 vsize_t
181 map_chunk(pd, pt, va, pa, size, acc, flg)
182 vaddr_t pd;
183 vaddr_t pt;
184 vaddr_t va;
185 paddr_t pa;
186 vsize_t size;
187 u_int acc;
188 u_int flg;
189 {
190 pd_entry_t *l1pt = (pd_entry_t *)pd;
191 pt_entry_t *l2pt = (pt_entry_t *)pt;
192 vsize_t remain;
193 u_int loop;
194
195 remain = (size + (NBPG - 1)) & ~(NBPG - 1);
196 #ifdef VERBOSE_INIT_ARM
197 printf("map_chunk: pa=%lx va=%lx sz=%lx rem=%lx acc=%x flg=%x\n",
198 pa, va, size, remain, acc, flg);
199 printf("map_chunk: ");
200 #endif
201 size = remain;
202
203 while (remain > 0) {
204 /* Can we do a section mapping ? */
205 if (l1pt && !((pa | va) & (L1_SEC_SIZE - 1))
206 && remain >= L1_SEC_SIZE) {
207 #ifdef VERBOSE_INIT_ARM
208 printf("S");
209 #endif
210 l1pt[(va >> PDSHIFT)] = L1_SECPTE(pa, acc, flg);
211 va += L1_SEC_SIZE;
212 pa += L1_SEC_SIZE;
213 remain -= L1_SEC_SIZE;
214 } else
215 /* Can we do a large page mapping ? */
216 if (!((pa | va) & (L2_LPAGE_SIZE - 1))
217 && (remain >= L2_LPAGE_SIZE)) {
218 #ifdef VERBOSE_INIT_ARM
219 printf("L");
220 #endif
221 for (loop = 0; loop < 16; ++loop)
222 #ifndef cats
223 l2pt[((va >> PGSHIFT) & 0x3f0) + loop] =
224 L2_LPTE(pa, acc, flg);
225 #else
226 l2pt[((va >> PGSHIFT) & 0x7f0) + loop] =
227 L2_LPTE(pa, acc, flg);
228 #endif
229 va += L2_LPAGE_SIZE;
230 pa += L2_LPAGE_SIZE;
231 remain -= L2_LPAGE_SIZE;
232 } else
233 /* All we can do is a small page mapping */
234 {
235 #ifdef VERBOSE_INIT_ARM
236 printf("P");
237 #endif
238 #ifndef cats
239 l2pt[((va >> PGSHIFT) & 0x3ff)] = L2_SPTE(pa, acc, flg);
240 #else
241 l2pt[((va >> PGSHIFT) & 0x7ff)] = L2_SPTE(pa, acc, flg);
242 #endif
243 va += NBPG;
244 pa += NBPG;
245 remain -= NBPG;
246 }
247 }
248 #ifdef VERBOSE_INIT_ARM
249 printf("\n");
250 #endif
251 return(size);
252 }
253
254 /*
255 * void cpu_startup(void)
256 *
257 * Machine dependant startup code.
258 *
259 */
260
261 void
262 cpu_startup()
263 {
264 int loop;
265 paddr_t minaddr;
266 paddr_t maxaddr;
267 caddr_t sysbase;
268 caddr_t size;
269 vsize_t bufsize;
270 int base, residual;
271 char pbuf[9];
272
273 proc0paddr = (struct user *)kernelstack.pv_va;
274 proc0.p_addr = proc0paddr;
275
276 /* Set the cpu control register */
277 cpu_setup(boot_args);
278
279 /* All domains MUST be clients, permissions are VERY important */
280 cpu_domains(DOMAIN_CLIENT);
281
282 /* Lock down zero page */
283 zero_page_readonly();
284
285 /*
286 * Give pmap a chance to set up a few more things now the vm
287 * is initialised
288 */
289 pmap_postinit();
290
291 /*
292 * Initialize error message buffer (at end of core).
293 */
294
295 /* msgbufphys was setup during the secondary boot strap */
296 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
297 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * NBPG,
298 msgbufphys + loop * NBPG, VM_PROT_READ|VM_PROT_WRITE);
299 pmap_update(pmap_kernel());
300 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
301
302 /*
303 * Identify ourselves for the msgbuf (everything printed earlier will
304 * not be buffered).
305 */
306 printf(version);
307
308 format_bytes(pbuf, sizeof(pbuf), arm_page_to_byte(physmem));
309 printf("total memory = %s\n", pbuf);
310
311 /*
312 * Find out how much space we need, allocate it,
313 * and then give everything true virtual addresses.
314 */
315 size = allocsys(NULL, NULL);
316 sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
317 if (sysbase == 0)
318 panic(
319 "cpu_startup: no room for system tables; %d bytes required",
320 (u_int)size);
321 if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
322 panic("cpu_startup: system table size inconsistency");
323
324 /*
325 * Now allocate buffers proper. They are different than the above
326 * in that they usually occupy more virtual memory than physical.
327 */
328 bufsize = MAXBSIZE * nbuf;
329 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(bufsize),
330 NULL, UVM_UNKNOWN_OFFSET, 0,
331 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
332 UVM_ADV_NORMAL, 0)) != 0)
333 panic("cpu_startup: cannot allocate UVM space for buffers");
334 minaddr = (vaddr_t)buffers;
335 if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
336 /* don't want to alloc more physical mem than needed */
337 bufpages = btoc(MAXBSIZE) * nbuf;
338 }
339
340 base = bufpages / nbuf;
341 residual = bufpages % nbuf;
342 for (loop = 0; loop < nbuf; ++loop) {
343 vsize_t curbufsize;
344 vaddr_t curbuf;
345 struct vm_page *pg;
346
347 /*
348 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
349 * that MAXBSIZE space, we allocate and map (base+1) pages
350 * for the first "residual" buffers, and then we allocate
351 * "base" pages for the rest.
352 */
353 curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
354 curbufsize = NBPG * ((loop < residual) ? (base+1) : base);
355
356 while (curbufsize) {
357 pg = uvm_pagealloc(NULL, 0, NULL, 0);
358 if (pg == NULL)
359 panic("cpu_startup: not enough memory for buffer cache");
360 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
361 VM_PROT_READ|VM_PROT_WRITE);
362 curbuf += PAGE_SIZE;
363 curbufsize -= PAGE_SIZE;
364 }
365 }
366 pmap_update(pmap_kernel());
367
368 /*
369 * Allocate a submap for exec arguments. This map effectively
370 * limits the number of processes exec'ing at any time.
371 */
372 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
373 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
374
375 /*
376 * Allocate a submap for physio
377 */
378 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
379 VM_PHYS_SIZE, 0, FALSE, NULL);
380
381 /*
382 * Finally, allocate mbuf cluster submap.
383 */
384 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
385 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
386 FALSE, NULL);
387
388 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
389 printf("avail memory = %s\n", pbuf);
390 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
391 printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
392
393 /*
394 * Set up buffers, so they can be used to read disk labels.
395 */
396 bufinit();
397
398 curpcb = &proc0.p_addr->u_pcb;
399 curpcb->pcb_flags = 0;
400 curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)proc0.p_addr +
401 USPACE_UNDEF_STACK_TOP;
402 curpcb->pcb_un.un_32.pcb32_sp = (u_int)proc0.p_addr +
403 USPACE_SVC_STACK_TOP;
404 (void) pmap_extract(pmap_kernel(), (vaddr_t)(pmap_kernel())->pm_pdir,
405 (paddr_t *)&curpcb->pcb_pagedir);
406
407 curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
408 }
409
410 /*
411 * Modify the current mapping for zero page to make it read only
412 *
413 * This routine is only used until things start forking. Then new
414 * system pages are mapped read only in pmap_enter().
415 */
416
417 void
418 zero_page_readonly()
419 {
420 WriteWord(PROCESS_PAGE_TBLS_BASE + 0,
421 L2_PTE((systempage.pv_pa & PG_FRAME), AP_KR));
422 cpu_tlb_flushID_SE(0x00000000);
423 }
424
425
426 /*
427 * Modify the current mapping for zero page to make it read/write
428 *
429 * This routine is only used until things start forking. Then system
430 * pages belonging to user processes are never made writable.
431 */
432
433 void
434 zero_page_readwrite()
435 {
436 WriteWord(PROCESS_PAGE_TBLS_BASE + 0,
437 L2_PTE((systempage.pv_pa & PG_FRAME), AP_KRW));
438 cpu_tlb_flushID_SE(0x00000000);
439 }
440
441
442 /*
443 * machine dependent system variables.
444 */
445
446 int
447 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
448 int *name;
449 u_int namelen;
450 void *oldp;
451 size_t *oldlenp;
452 void *newp;
453 size_t newlen;
454 struct proc *p;
455 {
456 /* all sysctl names at this level are terminal */
457 if (namelen != 1)
458 return (ENOTDIR); /* overloaded */
459
460 switch (name[0]) {
461 case CPU_DEBUG:
462 return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
463
464 case CPU_BOOTED_DEVICE:
465 if (booted_device != NULL)
466 return (sysctl_rdstring(oldp, oldlenp, newp,
467 booted_device->dv_xname));
468 return (EOPNOTSUPP);
469
470 case CPU_CONSDEV: {
471 dev_t consdev;
472 if (cn_tab != NULL)
473 consdev = cn_tab->cn_dev;
474 else
475 consdev = NODEV;
476 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
477 sizeof consdev));
478 }
479 case CPU_BOOTED_KERNEL: {
480 if (booted_kernel != NULL && booted_kernel[0] != '\0')
481 return sysctl_rdstring(oldp, oldlenp, newp,
482 booted_kernel);
483 return (EOPNOTSUPP);
484 }
485
486 default:
487 return (EOPNOTSUPP);
488 }
489 /* NOTREACHED */
490 }
491
492 void
493 parse_mi_bootargs(args)
494 char *args;
495 {
496 int integer;
497
498 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
499 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
500 if (integer)
501 boothowto |= RB_SINGLE;
502 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
503 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
504 if (integer)
505 boothowto |= RB_KDB;
506 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
507 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
508 if (integer)
509 boothowto |= RB_ASKNAME;
510
511 #ifdef PMAP_DEBUG
512 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
513 pmap_debug_level = integer;
514 pmap_debug(pmap_debug_level);
515 }
516 #endif /* PMAP_DEBUG */
517
518 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
519 bufpages = integer;*/
520
521 #ifndef PMAP_STATIC_L1S
522 if (get_bootconf_option(args, "maxproc", BOOTOPT_TYPE_INT, &integer)) {
523 max_processes = integer;
524 if (max_processes < 16)
525 max_processes = 16;
526 /* Limit is PDSIZE * (max_processes + 1) <= 4MB */
527 if (max_processes > 255)
528 max_processes = 255;
529 }
530 #endif /* !PMAP_STATUC_L1S */
531 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MINIROOTSIZE)
532 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
533 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
534 memory_disc_size = integer;
535 memory_disc_size *= 1024;
536 if (memory_disc_size < 32*1024)
537 memory_disc_size = 32*1024;
538 if (memory_disc_size > 2048*1024)
539 memory_disc_size = 2048*1024;
540 }
541 #endif /* NMD && MEMORY_DISK_HOOKS && !MINIROOTSIZE */
542
543 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
544 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
545 if (integer)
546 boothowto |= AB_QUIET;
547 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
548 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
549 if (integer)
550 boothowto |= AB_VERBOSE;
551 }
552
553 /* End of machdep.c */
554