machdep.c revision 1.68 1 /* $NetBSD: machdep.c,v 1.68 2001/08/26 02:47:41 matt Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "opt_compat_netbsd.h"
35 #include "opt_ddb.h"
36
37 #include <sys/param.h>
38 #include <sys/buf.h>
39 #include <sys/exec.h>
40 #include <sys/malloc.h>
41 #include <sys/map.h>
42 #include <sys/mbuf.h>
43 #include <sys/mount.h>
44 #include <sys/msgbuf.h>
45 #include <sys/proc.h>
46 #include <sys/reboot.h>
47 #include <sys/syscallargs.h>
48 #include <sys/syslog.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/user.h>
52 #include <sys/boot_flag.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <net/netisr.h>
57
58 #include <machine/autoconf.h>
59 #include <machine/bat.h>
60 #include <machine/pmap.h>
61 #include <machine/powerpc.h>
62 #include <machine/trap.h>
63
64 #include <dev/cons.h>
65
66 /*
67 * Global variables used here and there
68 */
69 struct vm_map *exec_map = NULL;
70 struct vm_map *mb_map = NULL;
71 struct vm_map *phys_map = NULL;
72
73 struct pcb *curpcb;
74 struct pmap *curpm;
75 struct proc *fpuproc;
76
77 extern struct user *proc0paddr;
78
79 struct bat battable[16];
80
81 int astpending;
82
83 char *bootpath;
84
85 paddr_t msgbuf_paddr;
86 vaddr_t msgbuf_vaddr;
87
88 int lcsplx(int); /* called from locore.S */
89
90 static int fake_spl __P((void));
91 static int fake_splx __P((int));
92 static void fake_setsoft __P((void));
93 static void fake_clock_return __P((struct clockframe *, int));
94 static void fake_irq_establish __P((int, int, void (*)(void *), void *));
95
96 struct machvec machine_interface = {
97 fake_spl,
98 fake_spl,
99 fake_spl,
100 fake_spl,
101 fake_spl,
102 fake_spl,
103 fake_spl,
104 fake_spl,
105 fake_spl,
106 fake_spl,
107 fake_splx,
108 fake_setsoft,
109 fake_setsoft,
110 fake_clock_return,
111 fake_irq_establish,
112 };
113
114 void
115 initppc(startkernel, endkernel, args)
116 u_int startkernel, endkernel;
117 char *args;
118 {
119 extern int trapcode, trapsize;
120 extern int alitrap, alisize;
121 extern int dsitrap, dsisize;
122 extern int isitrap, isisize;
123 extern int decrint, decrsize;
124 extern int tlbimiss, tlbimsize;
125 extern int tlbdlmiss, tlbdlmsize;
126 extern int tlbdsmiss, tlbdsmsize;
127 #ifdef DDB
128 extern int ddblow, ddbsize;
129 /* extern void *startsym, *endsym; */
130 #endif
131 #ifdef IPKDB
132 extern int ipkdblow, ipkdbsize;
133 #endif
134 int exc, scratch;
135
136 proc0.p_addr = proc0paddr;
137 memset(proc0.p_addr, 0, sizeof *proc0.p_addr);
138
139 curpcb = &proc0paddr->u_pcb;
140
141 curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
142
143 /*
144 * i386 port says, that this shouldn't be here,
145 * but I really think the console should be initialized
146 * as early as possible.
147 */
148 consinit();
149
150 #ifdef __notyet__ /* Needs some rethinking regarding real/virtual OFW */
151 OF_set_callback(callback);
152 #endif
153 /*
154 * Initialize BAT registers to unmapped to not generate
155 * overlapping mappings below.
156 */
157 asm volatile ("mtibatu 0,%0" :: "r"(0));
158 asm volatile ("mtibatu 1,%0" :: "r"(0));
159 asm volatile ("mtibatu 2,%0" :: "r"(0));
160 asm volatile ("mtibatu 3,%0" :: "r"(0));
161 asm volatile ("mtdbatu 0,%0" :: "r"(0));
162 asm volatile ("mtdbatu 1,%0" :: "r"(0));
163 asm volatile ("mtdbatu 2,%0" :: "r"(0));
164 asm volatile ("mtdbatu 3,%0" :: "r"(0));
165
166 /*
167 * Set up initial BAT table to only map the lowest 256 MB area
168 */
169 battable[0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
170 battable[0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
171
172 /*
173 * Now setup fixed bat registers
174 *
175 * Note that we still run in real mode, and the BAT
176 * registers were cleared above.
177 */
178 /* IBAT0 used for initial 256 MB segment */
179 asm volatile ("mtibatl 0,%0; mtibatu 0,%1"
180 :: "r"(battable[0].batl), "r"(battable[0].batu));
181 /* DBAT0 used similar */
182 asm volatile ("mtdbatl 0,%0; mtdbatu 0,%1"
183 :: "r"(battable[0].batl), "r"(battable[0].batu));
184
185 /*
186 * Set up trap vectors
187 */
188 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
189 switch (exc) {
190 default:
191 memcpy((void *)exc, &trapcode, (size_t)&trapsize);
192 break;
193 case EXC_EXI:
194 /*
195 * This one is (potentially) installed during autoconf
196 */
197 break;
198 case EXC_ALI:
199 memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
200 break;
201 case EXC_DSI:
202 memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
203 break;
204 case EXC_ISI:
205 memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
206 break;
207 case EXC_DECR:
208 memcpy((void *)EXC_DECR, &decrint, (size_t)&decrsize);
209 break;
210 case EXC_IMISS:
211 memcpy((void *)EXC_IMISS, &tlbimiss, (size_t)&tlbimsize);
212 break;
213 case EXC_DLMISS:
214 memcpy((void *)EXC_DLMISS, &tlbdlmiss, (size_t)&tlbdlmsize);
215 break;
216 case EXC_DSMISS:
217 memcpy((void *)EXC_DSMISS, &tlbdsmiss, (size_t)&tlbdsmsize);
218 break;
219 #if defined(DDB) || defined(IPKDB)
220 case EXC_PGM:
221 case EXC_TRC:
222 case EXC_BPT:
223 #if defined(DDB)
224 memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
225 #else
226 memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
227 #endif
228 break;
229 #endif /* DDB || IPKDB */
230 }
231
232 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
233
234 /*
235 * Now enable translation (and machine checks/recoverable interrupts).
236 */
237 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
238 : "=r"(scratch) : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
239
240 /*
241 * Parse arg string.
242 */
243 bootpath = args;
244 while (*++args && *args != ' ');
245 if (*args) {
246 for(*args++ = 0; *args; args++)
247 BOOT_FLAG(*args, boothowto);
248 }
249
250 #ifdef DDB
251 /* ddb_init((int)(endsym - startsym), startsym, endsym); */
252 #endif
253 #ifdef IPKDB
254 /*
255 * Now trap to IPKDB
256 */
257 ipkdb_init();
258 if (boothowto & RB_KDB)
259 ipkdb_connect(0);
260 #endif
261
262 /*
263 * Set the page size.
264 */
265 uvm_setpagesize();
266
267 /*
268 * Initialize pmap module.
269 */
270 pmap_bootstrap(startkernel, endkernel);
271 }
272
273 /*
274 * This should probably be in autoconf! XXX
275 */
276 int cpu;
277 char machine[] = MACHINE; /* from <machine/param.h> */
278 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
279
280 void
281 install_extint(handler)
282 void (*handler) __P((void));
283 {
284 extern int extint, extsize;
285 extern u_long extint_call;
286 u_long offset = (u_long)handler - (u_long)&extint_call;
287 int omsr, msr;
288
289 #ifdef DIAGNOSTIC
290 if (offset > 0x1ffffff)
291 panic("install_extint: too far away");
292 #endif
293 asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
294 : "=r"(omsr), "=r"(msr) : "K"((u_short)~PSL_EE));
295 extint_call = (extint_call & 0xfc000003) | offset;
296 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
297 __syncicache((void *)&extint_call, sizeof extint_call);
298 __syncicache((void *)EXC_EXI, (int)&extsize);
299 asm volatile ("mtmsr %0" :: "r"(omsr));
300 }
301
302 /*
303 * Machine dependent startup code.
304 */
305 void
306 cpu_startup()
307 {
308 int sz, i;
309 caddr_t v;
310 paddr_t minaddr, maxaddr;
311 int base, residual;
312 char pbuf[9];
313
314 proc0.p_addr = proc0paddr;
315 v = (caddr_t)proc0paddr + USPACE;
316
317 /*
318 * Initialize error message buffer (at end of core).
319 */
320 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE))))
321 panic("startup: no room for message buffer");
322 for (i = 0; i < btoc(MSGBUFSIZE); i++)
323 pmap_enter(pmap_kernel(), msgbuf_vaddr + i * NBPG,
324 msgbuf_paddr + i * NBPG, VM_PROT_READ|VM_PROT_WRITE,
325 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
326 pmap_update();
327 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
328
329 printf("%s", version);
330 cpu_identify(NULL, 0);
331
332 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
333 printf("total memory = %s\n", pbuf);
334
335 /*
336 * Find out how much space we need, allocate it,
337 * and then give everything true virtual addresses.
338 */
339 sz = (int)allocsys(NULL, NULL);
340 if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
341 panic("startup: no room for tables");
342 if (allocsys(v, NULL) - v != sz)
343 panic("startup: table size inconsistency");
344
345 /*
346 * Now allocate buffers proper. They are different than the above
347 * in that they usually occupy more virtual memory than physical.
348 */
349 sz = MAXBSIZE * nbuf;
350 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(sz),
351 NULL, UVM_UNKNOWN_OFFSET, 0,
352 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
353 UVM_ADV_NORMAL, 0)) != 0)
354 panic("startup: cannot allocate VM for buffers");
355 minaddr = (vaddr_t)buffers;
356 base = bufpages / nbuf;
357 residual = bufpages % nbuf;
358 if (base >= MAXBSIZE) {
359 /* Don't want to alloc more physical mem than ever needed */
360 base = MAXBSIZE;
361 residual = 0;
362 }
363 for (i = 0; i < nbuf; i++) {
364 vsize_t curbufsize;
365 vaddr_t curbuf;
366 struct vm_page *pg;
367
368 /*
369 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
370 * that MAXBSIZE space, we allocate and map (base+1) pages
371 * for the first "residual" buffers, and then we allocate
372 * "base" pages for the rest.
373 */
374 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
375 curbufsize = NBPG * ((i < residual) ? (base+1) : base);
376
377 while (curbufsize) {
378 pg = uvm_pagealloc(NULL, 0, NULL, 0);
379 if (pg == NULL)
380 panic("startup: not enough memory for "
381 "buffer cache");
382 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
383 VM_PROT_READ | VM_PROT_WRITE);
384 curbuf += PAGE_SIZE;
385 curbufsize -= PAGE_SIZE;
386 }
387 }
388 pmap_update();
389
390 /*
391 * Allocate a submap for exec arguments. This map effectively
392 * limits the number of processes exec'ing at any time.
393 */
394 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
395 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
396
397 /*
398 * Allocate a submap for physio
399 */
400 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
401 VM_PHYS_SIZE, 0, FALSE, NULL);
402
403 /*
404 * No need to allocate an mbuf cluster submap. Mbuf clusters
405 * are allocated via the pool allocator, and we use direct-mapped
406 * pool pages.
407 */
408
409 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
410 printf("avail memory = %s\n", pbuf);
411 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
412 printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
413
414 /*
415 * Set up the buffers.
416 */
417 bufinit();
418
419 /*
420 * For now, use soft spl handling.
421 */
422 {
423 extern struct machvec soft_machvec;
424
425 machine_interface = soft_machvec;
426 }
427
428 /*
429 * Now allow hardware interrupts.
430 */
431 {
432 int msr;
433
434 splhigh();
435 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
436 : "=r"(msr) : "K"((u_short)(PSL_EE|PSL_RI)));
437 }
438 }
439
440 /*
441 * consinit
442 * Initialize system console.
443 */
444 void
445 consinit()
446 {
447 static int initted;
448
449 if (initted)
450 return;
451 initted = 1;
452 cninit();
453 }
454
455 /*
456 * Crash dump handling.
457 */
458
459 void
460 dumpsys()
461 {
462 printf("dumpsys: TBD\n");
463 }
464
465 /*
466 * Soft networking interrupts.
467 */
468 void
469 softnet()
470 {
471 int isr = netisr;
472
473 netisr = 0;
474
475 #define DONETISR(bit, fn) do { \
476 if (isr & (1 << bit)) \
477 fn(); \
478 } while (0)
479
480 #include <net/netisr_dispatch.h>
481
482 #undef DONETISR
483 }
484
485 /*
486 * Stray interrupts.
487 */
488 void
489 strayintr(irq)
490 int irq;
491 {
492 log(LOG_ERR, "stray interrupt %d\n", irq);
493 }
494
495 /*
496 * Halt or reboot the machine after syncing/dumping according to howto.
497 */
498 void
499 cpu_reboot(howto, what)
500 int howto;
501 char *what;
502 {
503 static int syncing;
504 static char str[256];
505 char *ap = str, *ap1 = ap;
506
507 boothowto = howto;
508 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
509 syncing = 1;
510 vfs_shutdown(); /* sync */
511 resettodr(); /* set wall clock */
512 }
513 splhigh();
514 if (howto & RB_HALT) {
515 doshutdownhooks();
516 printf("halted\n\n");
517 ppc_exit();
518 }
519 if (!cold && (howto & RB_DUMP))
520 dumpsys();
521 doshutdownhooks();
522 printf("rebooting\n\n");
523 if (what && *what) {
524 if (strlen(what) > sizeof str - 5)
525 printf("boot string too large, ignored\n");
526 else {
527 strcpy(str, what);
528 ap1 = ap = str + strlen(str);
529 *ap++ = ' ';
530 }
531 }
532 *ap++ = '-';
533 if (howto & RB_SINGLE)
534 *ap++ = 's';
535 if (howto & RB_KDB)
536 *ap++ = 'd';
537 *ap++ = 0;
538 if (ap[-2] == '-')
539 *ap1 = 0;
540 ppc_boot(str);
541 }
542
543 #ifdef notyet
544 /*
545 * OpenFirmware callback routine
546 */
547 void
548 callback(p)
549 void *p;
550 {
551 panic("callback"); /* for now XXX */
552 }
553 #endif
554
555 /*
556 * Perform an `splx()' for locore.
557 */
558 int
559 lcsplx(int ipl)
560 {
561
562 return (splx(ipl));
563 }
564
565 /*
566 * Initial Machine Interface.
567 */
568 static int
569 fake_spl()
570 {
571 int scratch;
572
573 asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
574 : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
575 return (-1);
576 }
577
578 static void
579 fake_setsoft()
580 {
581 /* Do nothing */
582 }
583
584 static int
585 fake_splx(new)
586 int new;
587 {
588 return (fake_spl());
589 }
590
591 static void
592 fake_clock_return(frame, nticks)
593 struct clockframe *frame;
594 int nticks;
595 {
596 /* Do nothing */
597 }
598
599 static void
600 fake_irq_establish(irq, level, handler, arg)
601 int irq, level;
602 void (*handler) __P((void *));
603 void *arg;
604 {
605 panic("fake_irq_establish");
606 }
607