machdep.c revision 1.63 1 /* $NetBSD: machdep.c,v 1.63 2001/03/15 06:10:46 chs 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/bat.h>
59 #include <machine/pmap.h>
60 #include <machine/powerpc.h>
61 #include <machine/trap.h>
62
63 /* Our exported CPU info; we can have only one. */
64 struct cpu_info cpu_info_store;
65
66 /*
67 * Global variables used here and there
68 */
69 vm_map_t exec_map = NULL;
70 vm_map_t mb_map = NULL;
71 vm_map_t 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 int phandle, qhandle;
120 char name[32];
121 struct machvec *mp;
122 extern trapcode, trapsize;
123 extern alitrap, alisize;
124 extern dsitrap, dsisize;
125 extern isitrap, isisize;
126 extern decrint, decrsize;
127 extern tlbimiss, tlbimsize;
128 extern tlbdlmiss, tlbdlmsize;
129 extern tlbdsmiss, tlbdsmsize;
130 #ifdef DDB
131 extern ddblow, ddbsize;
132 extern void *startsym, *endsym;
133 #endif
134 #ifdef IPKDB
135 extern ipkdblow, ipkdbsize;
136 #endif
137 extern void consinit __P((void));
138 extern void callback __P((void *));
139 int exc, scratch;
140
141 proc0.p_addr = proc0paddr;
142 bzero(proc0.p_addr, sizeof *proc0.p_addr);
143
144 curpcb = &proc0paddr->u_pcb;
145
146 curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
147
148 /*
149 * i386 port says, that this shouldn't be here,
150 * but I really think the console should be initialized
151 * as early as possible.
152 */
153 consinit();
154
155 #ifdef __notyet__ /* Needs some rethinking regarding real/virtual OFW */
156 OF_set_callback(callback);
157 #endif
158 /*
159 * Initialize BAT registers to unmapped to not generate
160 * overlapping mappings below.
161 */
162 asm volatile ("mtibatu 0,%0" :: "r"(0));
163 asm volatile ("mtibatu 1,%0" :: "r"(0));
164 asm volatile ("mtibatu 2,%0" :: "r"(0));
165 asm volatile ("mtibatu 3,%0" :: "r"(0));
166 asm volatile ("mtdbatu 0,%0" :: "r"(0));
167 asm volatile ("mtdbatu 1,%0" :: "r"(0));
168 asm volatile ("mtdbatu 2,%0" :: "r"(0));
169 asm volatile ("mtdbatu 3,%0" :: "r"(0));
170
171 /*
172 * Set up initial BAT table to only map the lowest 256 MB area
173 */
174 battable[0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
175 battable[0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
176
177 /*
178 * Now setup fixed bat registers
179 *
180 * Note that we still run in real mode, and the BAT
181 * registers were cleared above.
182 */
183 /* IBAT0 used for initial 256 MB segment */
184 asm volatile ("mtibatl 0,%0; mtibatu 0,%1"
185 :: "r"(battable[0].batl), "r"(battable[0].batu));
186 /* DBAT0 used similar */
187 asm volatile ("mtdbatl 0,%0; mtdbatu 0,%1"
188 :: "r"(battable[0].batl), "r"(battable[0].batu));
189
190 /*
191 * Set up trap vectors
192 */
193 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
194 switch (exc) {
195 default:
196 bcopy(&trapcode, (void *)exc, (size_t)&trapsize);
197 break;
198 case EXC_EXI:
199 /*
200 * This one is (potentially) installed during autoconf
201 */
202 break;
203 case EXC_ALI:
204 bcopy(&alitrap, (void *)EXC_ALI, (size_t)&alisize);
205 break;
206 case EXC_DSI:
207 bcopy(&dsitrap, (void *)EXC_DSI, (size_t)&dsisize);
208 break;
209 case EXC_ISI:
210 bcopy(&isitrap, (void *)EXC_ISI, (size_t)&isisize);
211 break;
212 case EXC_DECR:
213 bcopy(&decrint, (void *)EXC_DECR, (size_t)&decrsize);
214 break;
215 case EXC_IMISS:
216 bcopy(&tlbimiss, (void *)EXC_IMISS, (size_t)&tlbimsize);
217 break;
218 case EXC_DLMISS:
219 bcopy(&tlbdlmiss, (void *)EXC_DLMISS, (size_t)&tlbdlmsize);
220 break;
221 case EXC_DSMISS:
222 bcopy(&tlbdsmiss, (void *)EXC_DSMISS, (size_t)&tlbdsmsize);
223 break;
224 #if defined(DDB) || defined(IPKDB)
225 case EXC_PGM:
226 case EXC_TRC:
227 case EXC_BPT:
228 #if defined(DDB)
229 bcopy(&ddblow, (void *)exc, (size_t)&ddbsize);
230 #else
231 bcopy(&ipkdblow, (void *)exc, (size_t)&ipkdbsize);
232 #endif
233 break;
234 #endif /* DDB || IPKDB */
235 }
236
237 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
238
239 /*
240 * Now enable translation (and machine checks/recoverable interrupts).
241 */
242 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
243 : "=r"(scratch) : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
244
245 /*
246 * Parse arg string.
247 */
248 bootpath = args;
249 while (*++args && *args != ' ');
250 if (*args) {
251 for(*args++ = 0; *args; args++)
252 BOOT_FLAG(*args, boothowto);
253 }
254
255 #ifdef DDB
256 /* ddb_init((int)(endsym - startsym), startsym, endsym); */
257 #endif
258 #ifdef IPKDB
259 /*
260 * Now trap to IPKDB
261 */
262 ipkdb_init();
263 if (boothowto & RB_KDB)
264 ipkdb_connect(0);
265 #endif
266
267 /*
268 * Set the page size.
269 */
270 uvm_setpagesize();
271
272 /*
273 * Initialize pmap module.
274 */
275 pmap_bootstrap(startkernel, endkernel);
276 }
277
278 /*
279 * This should probably be in autoconf! XXX
280 */
281 int cpu;
282 char cpu_model[80];
283 char machine[] = MACHINE; /* from <machine/param.h> */
284 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
285
286 void
287 identifycpu()
288 {
289 int phandle, pvr;
290 char name[32];
291
292 /*
293 * Find cpu type (Do it by OpenFirmware?)
294 */
295 asm ("mfpvr %0" : "=r"(pvr));
296 cpu = pvr >> 16;
297 switch (cpu) {
298 case 1:
299 sprintf(cpu_model, "601");
300 break;
301 case 3:
302 sprintf(cpu_model, "603");
303 break;
304 case 4:
305 sprintf(cpu_model, "604");
306 break;
307 case 5:
308 sprintf(cpu_model, "602");
309 break;
310 case 6:
311 sprintf(cpu_model, "603e");
312 break;
313 case 7:
314 sprintf(cpu_model, "603ev");
315 break;
316 case 9:
317 sprintf(cpu_model, "604ev");
318 break;
319 case 20:
320 sprintf(cpu_model, "620");
321 break;
322 default:
323 sprintf(cpu_model, "Version %x", cpu);
324 break;
325 }
326 sprintf(cpu_model + strlen(cpu_model), " (Revision %x)", pvr & 0xffff);
327 printf("CPU: %s\n", cpu_model);
328 }
329
330 void
331 install_extint(handler)
332 void (*handler) __P((void));
333 {
334 extern extint, extsize;
335 extern u_long extint_call;
336 u_long offset = (u_long)handler - (u_long)&extint_call;
337 int omsr, msr;
338
339 #ifdef DIAGNOSTIC
340 if (offset > 0x1ffffff)
341 panic("install_extint: too far away");
342 #endif
343 asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
344 : "=r"(omsr), "=r"(msr) : "K"((u_short)~PSL_EE));
345 extint_call = (extint_call & 0xfc000003) | offset;
346 bcopy(&extint, (void *)EXC_EXI, (size_t)&extsize);
347 __syncicache((void *)&extint_call, sizeof extint_call);
348 __syncicache((void *)EXC_EXI, (int)&extsize);
349 asm volatile ("mtmsr %0" :: "r"(omsr));
350 }
351
352 /*
353 * Machine dependent startup code.
354 */
355 void
356 cpu_startup()
357 {
358 int sz, i;
359 caddr_t v;
360 paddr_t minaddr, maxaddr;
361 int base, residual;
362 char pbuf[9];
363
364 proc0.p_addr = proc0paddr;
365 v = (caddr_t)proc0paddr + USPACE;
366
367 /*
368 * Initialize error message buffer (at end of core).
369 */
370 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE))))
371 panic("startup: no room for message buffer");
372 for (i = 0; i < btoc(MSGBUFSIZE); i++)
373 pmap_enter(pmap_kernel(), msgbuf_vaddr + i * NBPG,
374 msgbuf_paddr + i * NBPG, VM_PROT_READ|VM_PROT_WRITE,
375 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
376 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
377
378 printf("%s", version);
379 identifycpu();
380
381 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
382 printf("total memory = %s\n", pbuf);
383
384 /*
385 * Find out how much space we need, allocate it,
386 * and then give everything true virtual addresses.
387 */
388 sz = (int)allocsys(NULL, NULL);
389 if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
390 panic("startup: no room for tables");
391 if (allocsys(v, NULL) - v != sz)
392 panic("startup: table size inconsistency");
393
394 /*
395 * Now allocate buffers proper. They are different than the above
396 * in that they usually occupy more virtual memory than physical.
397 */
398 sz = MAXBSIZE * nbuf;
399 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(sz),
400 NULL, UVM_UNKNOWN_OFFSET, 0,
401 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
402 UVM_ADV_NORMAL, 0)) != 0)
403 panic("startup: cannot allocate VM for buffers");
404 minaddr = (vaddr_t)buffers;
405 base = bufpages / nbuf;
406 residual = bufpages % nbuf;
407 if (base >= MAXBSIZE) {
408 /* Don't want to alloc more physical mem than ever needed */
409 base = MAXBSIZE;
410 residual = 0;
411 }
412 for (i = 0; i < nbuf; i++) {
413 vsize_t curbufsize;
414 vaddr_t curbuf;
415 struct vm_page *pg;
416
417 /*
418 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
419 * that MAXBSIZE space, we allocate and map (base+1) pages
420 * for the first "residual" buffers, and then we allocate
421 * "base" pages for the rest.
422 */
423 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
424 curbufsize = NBPG * ((i < residual) ? (base+1) : base);
425
426 while (curbufsize) {
427 pg = uvm_pagealloc(NULL, 0, NULL, 0);
428 if (pg == NULL)
429 panic("startup: not enough memory for "
430 "buffer cache");
431 pmap_enter(kernel_map->pmap, curbuf,
432 VM_PAGE_TO_PHYS(pg), VM_PROT_READ|VM_PROT_WRITE,
433 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
434 curbuf += PAGE_SIZE;
435 curbufsize -= PAGE_SIZE;
436 }
437 }
438
439 /*
440 * Allocate a submap for exec arguments. This map effectively
441 * limits the number of processes exec'ing at any time.
442 */
443 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
444 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
445
446 /*
447 * Allocate a submap for physio
448 */
449 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
450 VM_PHYS_SIZE, 0, FALSE, NULL);
451
452 /*
453 * No need to allocate an mbuf cluster submap. Mbuf clusters
454 * are allocated via the pool allocator, and we use direct-mapped
455 * pool pages.
456 */
457
458 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
459 printf("avail memory = %s\n", pbuf);
460 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
461 printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
462
463 /*
464 * Set up the buffers.
465 */
466 bufinit();
467
468 /*
469 * For now, use soft spl handling.
470 */
471 {
472 extern struct machvec soft_machvec;
473
474 machine_interface = soft_machvec;
475 }
476
477 /*
478 * Now allow hardware interrupts.
479 */
480 {
481 int msr;
482
483 splhigh();
484 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
485 : "=r"(msr) : "K"((u_short)(PSL_EE|PSL_RI)));
486 }
487 }
488
489 /*
490 * consinit
491 * Initialize system console.
492 */
493 void
494 consinit()
495 {
496 static int initted;
497
498 if (initted)
499 return;
500 initted = 1;
501 cninit();
502 }
503
504 /*
505 * Crash dump handling.
506 */
507
508 void
509 dumpsys()
510 {
511 printf("dumpsys: TBD\n");
512 }
513
514 /*
515 * Soft networking interrupts.
516 */
517 void
518 softnet()
519 {
520 int isr = netisr;
521
522 netisr = 0;
523
524 #define DONETISR(bit, fn) do { \
525 if (isr & (1 << bit)) \
526 fn(); \
527 } while (0)
528
529 #include <net/netisr_dispatch.h>
530
531 #undef DONETISR
532 }
533
534 /*
535 * Stray interrupts.
536 */
537 void
538 strayintr(irq)
539 int irq;
540 {
541 log(LOG_ERR, "stray interrupt %d\n", irq);
542 }
543
544 /*
545 * Halt or reboot the machine after syncing/dumping according to howto.
546 */
547 void
548 cpu_reboot(howto, what)
549 int howto;
550 char *what;
551 {
552 static int syncing;
553 static char str[256];
554 char *ap = str, *ap1 = ap;
555
556 boothowto = howto;
557 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
558 syncing = 1;
559 vfs_shutdown(); /* sync */
560 resettodr(); /* set wall clock */
561 }
562 splhigh();
563 if (howto & RB_HALT) {
564 doshutdownhooks();
565 printf("halted\n\n");
566 ppc_exit();
567 }
568 if (!cold && (howto & RB_DUMP))
569 dumpsys();
570 doshutdownhooks();
571 printf("rebooting\n\n");
572 if (what && *what) {
573 if (strlen(what) > sizeof str - 5)
574 printf("boot string too large, ignored\n");
575 else {
576 strcpy(str, what);
577 ap1 = ap = str + strlen(str);
578 *ap++ = ' ';
579 }
580 }
581 *ap++ = '-';
582 if (howto & RB_SINGLE)
583 *ap++ = 's';
584 if (howto & RB_KDB)
585 *ap++ = 'd';
586 *ap++ = 0;
587 if (ap[-2] == '-')
588 *ap1 = 0;
589 ppc_boot(str);
590 }
591
592 /*
593 * OpenFirmware callback routine
594 */
595 void
596 callback(p)
597 void *p;
598 {
599 panic("callback"); /* for now XXX */
600 }
601
602 /*
603 * Perform an `splx()' for locore.
604 */
605 int
606 lcsplx(int ipl)
607 {
608
609 return (splx(ipl));
610 }
611
612 /*
613 * Initial Machine Interface.
614 */
615 static int
616 fake_spl()
617 {
618 int scratch;
619
620 asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
621 : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
622 return (-1);
623 }
624
625 static void
626 fake_setsoft()
627 {
628 /* Do nothing */
629 }
630
631 static int
632 fake_splx(new)
633 int new;
634 {
635 return (fake_spl());
636 }
637
638 static void
639 fake_clock_return(frame, nticks)
640 struct clockframe *frame;
641 int nticks;
642 {
643 /* Do nothing */
644 }
645
646 static void
647 fake_irq_establish(irq, level, handler, arg)
648 int irq, level;
649 void (*handler) __P((void *));
650 void *arg;
651 {
652 panic("fake_irq_establish");
653 }
654