machdep.c revision 1.71 1 /* $NetBSD: machdep.c,v 1.71 2001/10/22 23:01:17 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 <dev/ofw/openfirm.h>
59
60 #include <machine/autoconf.h>
61 #include <machine/bat.h>
62 #include <machine/pmap.h>
63 #include <machine/powerpc.h>
64 #include <machine/trap.h>
65
66 #include <machine/platform.h>
67
68 #include <dev/cons.h>
69
70 /*
71 * Global variables used here and there
72 */
73 struct vm_map *exec_map = NULL;
74 struct vm_map *mb_map = NULL;
75 struct vm_map *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 int lcsplx(int); /* called from locore.S */
93
94 static int fake_spl __P((void));
95 static int fake_splx __P((int));
96 static void fake_setsoft __P((void));
97 static void fake_clock_return __P((struct clockframe *, int));
98 static void fake_irq_establish __P((int, int, void (*)(void *), void *));
99
100 struct machvec machine_interface = {
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_spl,
110 fake_spl,
111 fake_splx,
112 fake_setsoft,
113 fake_setsoft,
114 fake_clock_return,
115 fake_irq_establish,
116 };
117
118 void ofppc_bootstrap_console(void);
119
120 void
121 initppc(startkernel, endkernel, args)
122 u_int startkernel, endkernel;
123 char *args;
124 {
125 extern int trapcode, trapsize;
126 extern int alitrap, alisize;
127 extern int dsitrap, dsisize;
128 extern int isitrap, isisize;
129 extern int decrint, decrsize;
130 extern int tlbimiss, tlbimsize;
131 extern int tlbdlmiss, tlbdlmsize;
132 extern int tlbdsmiss, tlbdsmsize;
133 #ifdef DDB
134 extern int ddblow, ddbsize;
135 /* extern void *startsym, *endsym; */
136 #endif
137 #ifdef IPKDB
138 extern int ipkdblow, ipkdbsize;
139 #endif
140 int exc, scratch;
141
142 proc0.p_addr = proc0paddr;
143 memset(proc0.p_addr, 0, sizeof *proc0.p_addr);
144
145 curpcb = &proc0paddr->u_pcb;
146
147 curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
148
149 /* Initialize the bootstrap console. */
150 ofppc_bootstrap_console();
151
152 /* Initialize the platform structure. */
153 platform_init();
154
155 /*
156 * Now that we know what platform we're running on, initialize
157 * the console.
158 */
159 (*platform.cons_init)();
160
161 #ifdef __notyet__ /* Needs some rethinking regarding real/virtual OFW */
162 OF_set_callback(callback);
163 #endif
164
165 /*
166 * Initialize BAT registers to unmapped to not generate
167 * overlapping mappings below.
168 */
169 asm volatile ("mtibatu 0,%0" :: "r"(0));
170 asm volatile ("mtibatu 1,%0" :: "r"(0));
171 asm volatile ("mtibatu 2,%0" :: "r"(0));
172 asm volatile ("mtibatu 3,%0" :: "r"(0));
173 asm volatile ("mtdbatu 0,%0" :: "r"(0));
174 asm volatile ("mtdbatu 1,%0" :: "r"(0));
175 asm volatile ("mtdbatu 2,%0" :: "r"(0));
176 asm volatile ("mtdbatu 3,%0" :: "r"(0));
177
178 /*
179 * Set up initial BAT table to only map the lowest 256 MB area
180 */
181 battable[0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
182 battable[0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
183
184 /*
185 * Now setup fixed bat registers
186 *
187 * Note that we still run in real mode, and the BAT
188 * registers were cleared above.
189 */
190 /* IBAT0 used for initial 256 MB segment */
191 asm volatile ("mtibatl 0,%0; mtibatu 0,%1"
192 :: "r"(battable[0].batl), "r"(battable[0].batu));
193 /* DBAT0 used similar */
194 asm volatile ("mtdbatl 0,%0; mtdbatu 0,%1"
195 :: "r"(battable[0].batl), "r"(battable[0].batu));
196
197 /*
198 * Set up trap vectors
199 */
200 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
201 switch (exc) {
202 default:
203 memcpy((void *)exc, &trapcode, (size_t)&trapsize);
204 break;
205 case EXC_EXI:
206 /*
207 * This one is (potentially) installed during autoconf
208 */
209 break;
210 case EXC_ALI:
211 memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
212 break;
213 case EXC_DSI:
214 memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
215 break;
216 case EXC_ISI:
217 memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
218 break;
219 case EXC_DECR:
220 memcpy((void *)EXC_DECR, &decrint, (size_t)&decrsize);
221 break;
222 case EXC_IMISS:
223 memcpy((void *)EXC_IMISS, &tlbimiss, (size_t)&tlbimsize);
224 break;
225 case EXC_DLMISS:
226 memcpy((void *)EXC_DLMISS, &tlbdlmiss, (size_t)&tlbdlmsize);
227 break;
228 case EXC_DSMISS:
229 memcpy((void *)EXC_DSMISS, &tlbdsmiss, (size_t)&tlbdsmsize);
230 break;
231 #if defined(DDB) || defined(IPKDB)
232 case EXC_PGM:
233 case EXC_TRC:
234 case EXC_BPT:
235 #if defined(DDB)
236 memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
237 #else
238 memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
239 #endif
240 break;
241 #endif /* DDB || IPKDB */
242 }
243
244 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
245
246 /*
247 * Now enable translation (and machine checks/recoverable interrupts).
248 */
249 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
250 : "=r"(scratch) : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
251
252 /*
253 * Parse arg string.
254 */
255 bootpath = args;
256 while (*++args && *args != ' ');
257 if (*args) {
258 for(*args++ = 0; *args; args++)
259 BOOT_FLAG(*args, boothowto);
260 }
261
262 #ifdef DDB
263 /* ddb_init((int)(endsym - startsym), startsym, endsym); */
264 #endif
265 #ifdef IPKDB
266 /*
267 * Now trap to IPKDB
268 */
269 ipkdb_init();
270 if (boothowto & RB_KDB)
271 ipkdb_connect(0);
272 #endif
273
274 /*
275 * Set the page size.
276 */
277 uvm_setpagesize();
278
279 /*
280 * Initialize pmap module.
281 */
282 pmap_bootstrap(startkernel, endkernel);
283 }
284
285 /*
286 * This should probably be in autoconf! XXX
287 */
288 int cpu;
289 char machine[] = MACHINE; /* from <machine/param.h> */
290 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
291
292 void
293 install_extint(handler)
294 void (*handler) __P((void));
295 {
296 extern int extint, extsize;
297 extern u_long extint_call;
298 u_long offset = (u_long)handler - (u_long)&extint_call;
299 int omsr, msr;
300
301 #ifdef DIAGNOSTIC
302 if (offset > 0x1ffffff)
303 panic("install_extint: too far away");
304 #endif
305 asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
306 : "=r"(omsr), "=r"(msr) : "K"((u_short)~PSL_EE));
307 extint_call = (extint_call & 0xfc000003) | offset;
308 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
309 __syncicache((void *)&extint_call, sizeof extint_call);
310 __syncicache((void *)EXC_EXI, (int)&extsize);
311 asm volatile ("mtmsr %0" :: "r"(omsr));
312 }
313
314 /*
315 * Machine dependent startup code.
316 */
317 void
318 cpu_startup()
319 {
320 int sz, i;
321 caddr_t v;
322 paddr_t minaddr, maxaddr;
323 int base, residual;
324 char pbuf[9];
325
326 proc0.p_addr = proc0paddr;
327 v = (caddr_t)proc0paddr + USPACE;
328
329 /*
330 * Initialize error message buffer (at end of core).
331 */
332 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE))))
333 panic("startup: no room for message buffer");
334 for (i = 0; i < btoc(MSGBUFSIZE); i++)
335 pmap_enter(pmap_kernel(), msgbuf_vaddr + i * NBPG,
336 msgbuf_paddr + i * NBPG, VM_PROT_READ|VM_PROT_WRITE,
337 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
338 pmap_update(pmap_kernel());
339 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
340
341 printf("%s", version);
342 cpu_identify(NULL, 0);
343
344 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
345 printf("total memory = %s\n", pbuf);
346
347 /*
348 * Find out how much space we need, allocate it,
349 * and then give everything true virtual addresses.
350 */
351 sz = (int)allocsys(NULL, NULL);
352 if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
353 panic("startup: no room for tables");
354 if (allocsys(v, NULL) - v != sz)
355 panic("startup: table size inconsistency");
356
357 /*
358 * Now allocate buffers proper. They are different than the above
359 * in that they usually occupy more virtual memory than physical.
360 */
361 sz = MAXBSIZE * nbuf;
362 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(sz),
363 NULL, UVM_UNKNOWN_OFFSET, 0,
364 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
365 UVM_ADV_NORMAL, 0)) != 0)
366 panic("startup: cannot allocate VM for buffers");
367 minaddr = (vaddr_t)buffers;
368 base = bufpages / nbuf;
369 residual = bufpages % nbuf;
370 if (base >= MAXBSIZE) {
371 /* Don't want to alloc more physical mem than ever needed */
372 base = MAXBSIZE;
373 residual = 0;
374 }
375 for (i = 0; i < nbuf; i++) {
376 vsize_t curbufsize;
377 vaddr_t curbuf;
378 struct vm_page *pg;
379
380 /*
381 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
382 * that MAXBSIZE space, we allocate and map (base+1) pages
383 * for the first "residual" buffers, and then we allocate
384 * "base" pages for the rest.
385 */
386 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
387 curbufsize = NBPG * ((i < residual) ? (base+1) : base);
388
389 while (curbufsize) {
390 pg = uvm_pagealloc(NULL, 0, NULL, 0);
391 if (pg == NULL)
392 panic("startup: not enough memory for "
393 "buffer cache");
394 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
395 VM_PROT_READ | VM_PROT_WRITE);
396 curbuf += PAGE_SIZE;
397 curbufsize -= PAGE_SIZE;
398 }
399 }
400 pmap_update(kernel_map->pmap);
401
402 /*
403 * Allocate a submap for exec arguments. This map effectively
404 * limits the number of processes exec'ing at any time.
405 */
406 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
407 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
408
409 /*
410 * Allocate a submap for physio
411 */
412 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
413 VM_PHYS_SIZE, 0, FALSE, NULL);
414
415 /*
416 * No need to allocate an mbuf cluster submap. Mbuf clusters
417 * are allocated via the pool allocator, and we use direct-mapped
418 * pool pages.
419 */
420
421 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
422 printf("avail memory = %s\n", pbuf);
423 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
424 printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
425
426 /*
427 * Set up the buffers.
428 */
429 bufinit();
430
431 /*
432 * Now allow hardware interrupts.
433 */
434 {
435 int msr;
436
437 splhigh();
438 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
439 : "=r"(msr) : "K"((u_short)(PSL_EE|PSL_RI)));
440 }
441 }
442
443 void
444 consinit()
445 {
446
447 /* Nothing to do; console is already initialized. */
448 }
449
450 int ofppc_cngetc(dev_t);
451 void ofppc_cnputc(dev_t, int);
452
453 struct consdev ofppc_bootcons = {
454 NULL, NULL, ofppc_cngetc, ofppc_cnputc, nullcnpollc, NULL,
455 makedev(0,0), 1,
456 };
457
458 int ofppc_stdin_ihandle, ofppc_stdout_ihandle;
459 int ofppc_stdin_phandle, ofppc_stdout_phandle;
460
461 void
462 ofppc_bootstrap_console(void)
463 {
464 int chosen;
465 char data[4];
466
467 chosen = OF_finddevice("/chosen");
468
469 if (OF_getprop(chosen, "stdin", data, sizeof(data)) != sizeof(int))
470 goto nocons;
471 ofppc_stdin_ihandle = of_decode_int(data);
472 ofppc_stdin_phandle = OF_instance_to_package(ofppc_stdin_ihandle);
473
474 if (OF_getprop(chosen, "stdout", data, sizeof(data)) != sizeof(int))
475 goto nocons;
476 ofppc_stdout_ihandle = of_decode_int(data);
477 ofppc_stdout_phandle = OF_instance_to_package(ofppc_stdout_ihandle);
478
479 cn_tab = &ofppc_bootcons;
480
481 nocons:
482 return;
483 }
484
485 int
486 ofppc_cngetc(dev_t dev)
487 {
488 u_char ch = '\0';
489 int l;
490
491 while ((l = OF_read(ofppc_stdin_ihandle, &ch, 1)) != 1)
492 if (l != -2 && l != 0)
493 return (-1);
494
495 return (ch);
496 }
497
498 void
499 ofppc_cnputc(dev_t dev, int c)
500 {
501 char ch = c;
502
503 OF_write(ofppc_stdout_ihandle, &ch, 1);
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 #ifdef notyet
595 /*
596 * OpenFirmware callback routine
597 */
598 void
599 callback(p)
600 void *p;
601 {
602 panic("callback"); /* for now XXX */
603 }
604 #endif
605
606 /*
607 * Perform an `splx()' for locore.
608 */
609 int
610 lcsplx(int ipl)
611 {
612
613 return (splx(ipl));
614 }
615
616 /*
617 * Initial Machine Interface.
618 */
619 static int
620 fake_spl()
621 {
622 int scratch;
623
624 asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
625 : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
626 return (-1);
627 }
628
629 static void
630 fake_setsoft()
631 {
632 /* Do nothing */
633 }
634
635 static int
636 fake_splx(new)
637 int new;
638 {
639 return (fake_spl());
640 }
641
642 static void
643 fake_clock_return(frame, nticks)
644 struct clockframe *frame;
645 int nticks;
646 {
647 /* Do nothing */
648 }
649
650 static void
651 fake_irq_establish(irq, level, handler, arg)
652 int irq, level;
653 void (*handler) __P((void *));
654 void *arg;
655 {
656 panic("fake_irq_establish");
657 }
658