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