machdep.c revision 1.60 1 /* $NetBSD: machdep.c,v 1.60 2000/11/16 01:56:05 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 #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)) != KERN_SUCCESS)
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 * Set set up registers on exec.
506 */
507 void
508 setregs(p, pack, stack)
509 struct proc *p;
510 struct exec_package *pack;
511 u_long stack;
512 {
513 struct trapframe *tf = trapframe(p);
514 struct ps_strings arginfo;
515
516 bzero(tf, sizeof *tf);
517 tf->fixreg[1] = -roundup(-stack + 8, 16);
518
519 /*
520 * XXX Machine-independent code has already copied arguments and
521 * XXX environment to userland. Get them back here.
522 */
523 (void)copyin((char *)PS_STRINGS, &arginfo, sizeof (arginfo));
524
525 /*
526 * Set up arguments for _start():
527 * _start(argc, argv, envp, obj, cleanup, ps_strings);
528 *
529 * Notes:
530 * - obj and cleanup are the auxilliary and termination
531 * vectors. They are fixed up by ld.elf_so.
532 * - ps_strings is a NetBSD extention, and will be
533 * ignored by executables which are strictly
534 * compliant with the SVR4 ABI.
535 *
536 * XXX We have to set both regs and retval here due to different
537 * XXX calling convention in trap.c and init_main.c.
538 */
539 tf->fixreg[3] = arginfo.ps_nargvstr;
540 tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
541 tf->fixreg[5] = (register_t)arginfo.ps_envstr;
542 tf->fixreg[6] = 0; /* auxillary vector */
543 tf->fixreg[7] = 0; /* termination vector */
544 tf->fixreg[8] = (register_t)PS_STRINGS; /* NetBSD extension */
545
546 tf->srr0 = pack->ep_entry;
547 tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
548 p->p_addr->u_pcb.pcb_flags = 0;
549 }
550
551 /*
552 * Machine dependent system variables.
553 */
554 int
555 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
556 int *name;
557 u_int namelen;
558 void *oldp;
559 size_t *oldlenp;
560 void *newp;
561 size_t newlen;
562 struct proc *p;
563 {
564 /* all sysctl names at this level are terminal */
565 if (namelen != 1)
566 return (ENOTDIR);
567
568 switch (name[0]) {
569 case CPU_CACHELINE:
570 return sysctl_rdint(oldp, oldlenp, newp, CACHELINESIZE);
571 default:
572 return (EOPNOTSUPP);
573 }
574 }
575
576 /*
577 * Crash dump handling.
578 */
579 u_long dumpmag = 0x8fca0101; /* magic number */
580 int dumpsize = 0; /* size of dump in pages */
581 long dumplo = -1; /* blocks */
582
583 void
584 dumpsys()
585 {
586 printf("dumpsys: TBD\n");
587 }
588
589 /*
590 * Soft networking interrupts.
591 */
592 void
593 softnet()
594 {
595 int isr = netisr;
596
597 netisr = 0;
598
599 #define DONETISR(bit, fn) do { \
600 if (isr & (1 << bit)) \
601 fn(); \
602 } while (0)
603
604 #include <net/netisr_dispatch.h>
605
606 #undef DONETISR
607 }
608
609 /*
610 * Stray interrupts.
611 */
612 void
613 strayintr(irq)
614 int irq;
615 {
616 log(LOG_ERR, "stray interrupt %d\n", irq);
617 }
618
619 /*
620 * Halt or reboot the machine after syncing/dumping according to howto.
621 */
622 void
623 cpu_reboot(howto, what)
624 int howto;
625 char *what;
626 {
627 static int syncing;
628 static char str[256];
629 char *ap = str, *ap1 = ap;
630
631 boothowto = howto;
632 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
633 syncing = 1;
634 vfs_shutdown(); /* sync */
635 resettodr(); /* set wall clock */
636 }
637 splhigh();
638 if (howto & RB_HALT) {
639 doshutdownhooks();
640 printf("halted\n\n");
641 ppc_exit();
642 }
643 if (!cold && (howto & RB_DUMP))
644 dumpsys();
645 doshutdownhooks();
646 printf("rebooting\n\n");
647 if (what && *what) {
648 if (strlen(what) > sizeof str - 5)
649 printf("boot string too large, ignored\n");
650 else {
651 strcpy(str, what);
652 ap1 = ap = str + strlen(str);
653 *ap++ = ' ';
654 }
655 }
656 *ap++ = '-';
657 if (howto & RB_SINGLE)
658 *ap++ = 's';
659 if (howto & RB_KDB)
660 *ap++ = 'd';
661 *ap++ = 0;
662 if (ap[-2] == '-')
663 *ap1 = 0;
664 ppc_boot(str);
665 }
666
667 /*
668 * OpenFirmware callback routine
669 */
670 void
671 callback(p)
672 void *p;
673 {
674 panic("callback"); /* for now XXX */
675 }
676
677 /*
678 * Perform an `splx()' for locore.
679 */
680 int
681 lcsplx(int ipl)
682 {
683
684 return (splx(ipl));
685 }
686
687 /*
688 * Initial Machine Interface.
689 */
690 static int
691 fake_spl()
692 {
693 int scratch;
694
695 asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
696 : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
697 return (-1);
698 }
699
700 static void
701 fake_setsoft()
702 {
703 /* Do nothing */
704 }
705
706 static int
707 fake_splx(new)
708 int new;
709 {
710 return (fake_spl());
711 }
712
713 static void
714 fake_clock_return(frame, nticks)
715 struct clockframe *frame;
716 int nticks;
717 {
718 /* Do nothing */
719 }
720
721 static void
722 fake_irq_establish(irq, level, handler, arg)
723 int irq, level;
724 void (*handler) __P((void *));
725 void *arg;
726 {
727 panic("fake_irq_establish");
728 }
729