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