machdep.c revision 1.65 1 /* $NetBSD: machdep.c,v 1.65 2001/06/02 18:09:18 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 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 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 pmap_update();
377 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
378
379 printf("%s", version);
380 identifycpu();
381
382 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
383 printf("total memory = %s\n", pbuf);
384
385 /*
386 * Find out how much space we need, allocate it,
387 * and then give everything true virtual addresses.
388 */
389 sz = (int)allocsys(NULL, NULL);
390 if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
391 panic("startup: no room for tables");
392 if (allocsys(v, NULL) - v != sz)
393 panic("startup: table size inconsistency");
394
395 /*
396 * Now allocate buffers proper. They are different than the above
397 * in that they usually occupy more virtual memory than physical.
398 */
399 sz = MAXBSIZE * nbuf;
400 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(sz),
401 NULL, UVM_UNKNOWN_OFFSET, 0,
402 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
403 UVM_ADV_NORMAL, 0)) != 0)
404 panic("startup: cannot allocate VM for buffers");
405 minaddr = (vaddr_t)buffers;
406 base = bufpages / nbuf;
407 residual = bufpages % nbuf;
408 if (base >= MAXBSIZE) {
409 /* Don't want to alloc more physical mem than ever needed */
410 base = MAXBSIZE;
411 residual = 0;
412 }
413 for (i = 0; i < nbuf; i++) {
414 vsize_t curbufsize;
415 vaddr_t curbuf;
416 struct vm_page *pg;
417
418 /*
419 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
420 * that MAXBSIZE space, we allocate and map (base+1) pages
421 * for the first "residual" buffers, and then we allocate
422 * "base" pages for the rest.
423 */
424 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
425 curbufsize = NBPG * ((i < residual) ? (base+1) : base);
426
427 while (curbufsize) {
428 pg = uvm_pagealloc(NULL, 0, NULL, 0);
429 if (pg == NULL)
430 panic("startup: not enough memory for "
431 "buffer cache");
432 pmap_enter(kernel_map->pmap, curbuf,
433 VM_PAGE_TO_PHYS(pg), VM_PROT_READ|VM_PROT_WRITE,
434 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
435 curbuf += PAGE_SIZE;
436 curbufsize -= PAGE_SIZE;
437 }
438 }
439 pmap_update();
440
441 /*
442 * Allocate a submap for exec arguments. This map effectively
443 * limits the number of processes exec'ing at any time.
444 */
445 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
446 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
447
448 /*
449 * Allocate a submap for physio
450 */
451 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
452 VM_PHYS_SIZE, 0, FALSE, NULL);
453
454 /*
455 * No need to allocate an mbuf cluster submap. Mbuf clusters
456 * are allocated via the pool allocator, and we use direct-mapped
457 * pool pages.
458 */
459
460 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
461 printf("avail memory = %s\n", pbuf);
462 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
463 printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
464
465 /*
466 * Set up the buffers.
467 */
468 bufinit();
469
470 /*
471 * For now, use soft spl handling.
472 */
473 {
474 extern struct machvec soft_machvec;
475
476 machine_interface = soft_machvec;
477 }
478
479 /*
480 * Now allow hardware interrupts.
481 */
482 {
483 int msr;
484
485 splhigh();
486 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
487 : "=r"(msr) : "K"((u_short)(PSL_EE|PSL_RI)));
488 }
489 }
490
491 /*
492 * consinit
493 * Initialize system console.
494 */
495 void
496 consinit()
497 {
498 static int initted;
499
500 if (initted)
501 return;
502 initted = 1;
503 cninit();
504 }
505
506 /*
507 * Crash dump handling.
508 */
509
510 void
511 dumpsys()
512 {
513 printf("dumpsys: TBD\n");
514 }
515
516 /*
517 * Soft networking interrupts.
518 */
519 void
520 softnet()
521 {
522 int isr = netisr;
523
524 netisr = 0;
525
526 #define DONETISR(bit, fn) do { \
527 if (isr & (1 << bit)) \
528 fn(); \
529 } while (0)
530
531 #include <net/netisr_dispatch.h>
532
533 #undef DONETISR
534 }
535
536 /*
537 * Stray interrupts.
538 */
539 void
540 strayintr(irq)
541 int irq;
542 {
543 log(LOG_ERR, "stray interrupt %d\n", irq);
544 }
545
546 /*
547 * Halt or reboot the machine after syncing/dumping according to howto.
548 */
549 void
550 cpu_reboot(howto, what)
551 int howto;
552 char *what;
553 {
554 static int syncing;
555 static char str[256];
556 char *ap = str, *ap1 = ap;
557
558 boothowto = howto;
559 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
560 syncing = 1;
561 vfs_shutdown(); /* sync */
562 resettodr(); /* set wall clock */
563 }
564 splhigh();
565 if (howto & RB_HALT) {
566 doshutdownhooks();
567 printf("halted\n\n");
568 ppc_exit();
569 }
570 if (!cold && (howto & RB_DUMP))
571 dumpsys();
572 doshutdownhooks();
573 printf("rebooting\n\n");
574 if (what && *what) {
575 if (strlen(what) > sizeof str - 5)
576 printf("boot string too large, ignored\n");
577 else {
578 strcpy(str, what);
579 ap1 = ap = str + strlen(str);
580 *ap++ = ' ';
581 }
582 }
583 *ap++ = '-';
584 if (howto & RB_SINGLE)
585 *ap++ = 's';
586 if (howto & RB_KDB)
587 *ap++ = 'd';
588 *ap++ = 0;
589 if (ap[-2] == '-')
590 *ap1 = 0;
591 ppc_boot(str);
592 }
593
594 /*
595 * OpenFirmware callback routine
596 */
597 void
598 callback(p)
599 void *p;
600 {
601 panic("callback"); /* for now XXX */
602 }
603
604 /*
605 * Perform an `splx()' for locore.
606 */
607 int
608 lcsplx(int ipl)
609 {
610
611 return (splx(ipl));
612 }
613
614 /*
615 * Initial Machine Interface.
616 */
617 static int
618 fake_spl()
619 {
620 int scratch;
621
622 asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
623 : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
624 return (-1);
625 }
626
627 static void
628 fake_setsoft()
629 {
630 /* Do nothing */
631 }
632
633 static int
634 fake_splx(new)
635 int new;
636 {
637 return (fake_spl());
638 }
639
640 static void
641 fake_clock_return(frame, nticks)
642 struct clockframe *frame;
643 int nticks;
644 {
645 /* Do nothing */
646 }
647
648 static void
649 fake_irq_establish(irq, level, handler, arg)
650 int irq, level;
651 void (*handler) __P((void *));
652 void *arg;
653 {
654 panic("fake_irq_establish");
655 }
656