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