machdep.c revision 1.73 1 /* $NetBSD: machdep.c,v 1.73 2001/10/29 19:04:26 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/db_machdep.h>
59 #include <ddb/db_extern.h>
60
61 #include <dev/ofw/openfirm.h>
62
63 #include <machine/autoconf.h>
64 #include <machine/bat.h>
65 #include <machine/pmap.h>
66 #include <machine/powerpc.h>
67 #include <machine/trap.h>
68
69 #include <machine/platform.h>
70
71 #include <dev/cons.h>
72
73 /*
74 * Global variables used here and there
75 */
76 struct vm_map *exec_map = NULL;
77 struct vm_map *mb_map = NULL;
78 struct vm_map *phys_map = NULL;
79
80 struct pcb *curpcb;
81 struct pmap *curpm;
82 struct proc *fpuproc;
83
84 extern struct user *proc0paddr;
85
86 struct bat battable[16];
87
88 int astpending;
89
90 char *bootpath;
91
92 paddr_t msgbuf_paddr;
93 vaddr_t msgbuf_vaddr;
94
95 int lcsplx(int); /* called from locore.S */
96
97 static int fake_spl __P((int));
98 static void fake_splx __P((int));
99 static void fake_setsoft __P((int));
100 static void fake_clock_return __P((struct clockframe *, int));
101 static void *fake_intr_establish __P((int, int, int, int (*)(void *), void *));
102 static void fake_intr_disestablish __P((void *));
103
104 struct machvec machine_interface = {
105 fake_spl,
106 fake_spl,
107 fake_splx,
108 fake_setsoft,
109 fake_clock_return,
110 fake_intr_establish,
111 fake_intr_disestablish,
112 };
113
114 void ofppc_bootstrap_console(void);
115
116 void
117 initppc(startkernel, endkernel, args)
118 u_int startkernel, endkernel;
119 char *args;
120 {
121 extern int trapcode, trapsize;
122 extern int alitrap, alisize;
123 extern int dsitrap, dsisize;
124 extern int isitrap, isisize;
125 extern int decrint, decrsize;
126 extern int tlbimiss, tlbimsize;
127 extern int tlbdlmiss, tlbdlmsize;
128 extern int tlbdsmiss, tlbdsmsize;
129 #ifdef DDB
130 extern int ddblow, ddbsize;
131 extern void *startsym, *endsym;
132 #endif
133 #ifdef IPKDB
134 extern int ipkdblow, ipkdbsize;
135 #endif
136 int exc, scratch;
137
138 /* Initialize the bootstrap console. */
139 ofppc_bootstrap_console();
140
141 /*
142 * Initialize BAT registers to unmapped to not generate
143 * overlapping mappings below.
144 */
145 asm volatile ("mtibatu 0,%0" :: "r"(0));
146 asm volatile ("mtibatu 1,%0" :: "r"(0));
147 asm volatile ("mtibatu 2,%0" :: "r"(0));
148 asm volatile ("mtibatu 3,%0" :: "r"(0));
149 asm volatile ("mtdbatu 0,%0" :: "r"(0));
150 asm volatile ("mtdbatu 1,%0" :: "r"(0));
151 asm volatile ("mtdbatu 2,%0" :: "r"(0));
152 asm volatile ("mtdbatu 3,%0" :: "r"(0));
153
154 /*
155 * Set up initial BAT table to only map the lowest 256 MB area
156 */
157 battable[0].batl = BATL(0x00000000, BAT_M, BAT_PP_RW);
158 battable[0].batu = BATU(0x00000000, BAT_BL_256M, BAT_Vs);
159
160 /*
161 * Now setup fixed bat registers
162 *
163 * Note that we still run in real mode, and the BAT
164 * registers were cleared above.
165 */
166 /* IBAT0 used for initial 256 MB segment */
167 asm volatile ("mtibatl 0,%0; mtibatu 0,%1"
168 :: "r"(battable[0].batl), "r"(battable[0].batu));
169 /* DBAT0 used similar */
170 asm volatile ("mtdbatl 0,%0; mtdbatu 0,%1"
171 :: "r"(battable[0].batl), "r"(battable[0].batu));
172
173 /*
174 * Initialize the platform structure. This may add entries
175 * to the BAT table.
176 */
177 platform_init();
178
179 proc0.p_addr = proc0paddr;
180 memset(proc0.p_addr, 0, sizeof *proc0.p_addr);
181
182 curpcb = &proc0paddr->u_pcb;
183
184 curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
185
186 #ifdef __notyet__ /* Needs some rethinking regarding real/virtual OFW */
187 OF_set_callback(callback);
188 #endif
189
190 /*
191 * Set up trap vectors
192 */
193 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
194 switch (exc) {
195 default:
196 memcpy((void *)exc, &trapcode, (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 memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
205 break;
206 case EXC_DSI:
207 memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
208 break;
209 case EXC_ISI:
210 memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
211 break;
212 case EXC_DECR:
213 memcpy((void *)EXC_DECR, &decrint, (size_t)&decrsize);
214 break;
215 case EXC_IMISS:
216 memcpy((void *)EXC_IMISS, &tlbimiss, (size_t)&tlbimsize);
217 break;
218 case EXC_DLMISS:
219 memcpy((void *)EXC_DLMISS, &tlbdlmiss, (size_t)&tlbdlmsize);
220 break;
221 case EXC_DSMISS:
222 memcpy((void *)EXC_DSMISS, &tlbdsmiss, (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 memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
230 #else
231 memcpy((void *)exc, &ipkdblow, (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 * Now that translation is enabled (and we can access bus space),
247 * initialize the console.
248 */
249 (*platform.cons_init)();
250
251 /*
252 * Parse arg string.
253 */
254 bootpath = args;
255 while (*++args && *args != ' ');
256 if (*args) {
257 for(*args++ = 0; *args; args++)
258 BOOT_FLAG(*args, boothowto);
259 }
260
261 /*
262 * Set the page size.
263 */
264 uvm_setpagesize();
265
266 /*
267 * Initialize pmap module.
268 */
269 pmap_bootstrap(startkernel, endkernel);
270
271 #ifdef DDB
272 ddb_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
273 if (boothowto & RB_KDB)
274 Debugger();
275 #endif
276 #ifdef IPKDB
277 /*
278 * Now trap to IPKDB
279 */
280 ipkdb_init();
281 if (boothowto & RB_KDB)
282 ipkdb_connect(0);
283 #endif
284 }
285
286 /*
287 * This should probably be in autoconf! XXX
288 */
289 int cpu;
290 char machine[] = MACHINE; /* from <machine/param.h> */
291 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
292
293 void
294 install_extint(handler)
295 void (*handler) __P((void));
296 {
297 extern int extint, extsize;
298 extern u_long extint_call;
299 u_long offset = (u_long)handler - (u_long)&extint_call;
300 int omsr, msr;
301
302 #ifdef DIAGNOSTIC
303 if (offset > 0x1ffffff)
304 panic("install_extint: too far away");
305 #endif
306 asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
307 : "=r"(omsr), "=r"(msr) : "K"((u_short)~PSL_EE));
308 extint_call = (extint_call & 0xfc000003) | offset;
309 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
310 __syncicache((void *)&extint_call, sizeof extint_call);
311 __syncicache((void *)EXC_EXI, (int)&extsize);
312 asm volatile ("mtmsr %0" :: "r"(omsr));
313 }
314
315 /*
316 * Machine dependent startup code.
317 */
318 void
319 cpu_startup()
320 {
321 int sz, i;
322 caddr_t v;
323 paddr_t minaddr, maxaddr;
324 int base, residual;
325 char pbuf[9];
326
327 proc0.p_addr = proc0paddr;
328 v = (caddr_t)proc0paddr + USPACE;
329
330 /*
331 * Initialize error message buffer (at end of core).
332 */
333 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE))))
334 panic("startup: no room for message buffer");
335 for (i = 0; i < btoc(MSGBUFSIZE); i++)
336 pmap_enter(pmap_kernel(), msgbuf_vaddr + i * NBPG,
337 msgbuf_paddr + i * NBPG, VM_PROT_READ|VM_PROT_WRITE,
338 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
339 pmap_update(pmap_kernel());
340 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
341
342 printf("%s", version);
343 cpu_identify(NULL, 0);
344
345 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
346 printf("total memory = %s\n", pbuf);
347
348 /*
349 * Find out how much space we need, allocate it,
350 * and then give everything true virtual addresses.
351 */
352 sz = (int)allocsys(NULL, NULL);
353 if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
354 panic("startup: no room for tables");
355 if (allocsys(v, NULL) - v != sz)
356 panic("startup: table size inconsistency");
357
358 /*
359 * Now allocate buffers proper. They are different than the above
360 * in that they usually occupy more virtual memory than physical.
361 */
362 sz = MAXBSIZE * nbuf;
363 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(sz),
364 NULL, UVM_UNKNOWN_OFFSET, 0,
365 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
366 UVM_ADV_NORMAL, 0)) != 0)
367 panic("startup: cannot allocate VM for buffers");
368 minaddr = (vaddr_t)buffers;
369 base = bufpages / nbuf;
370 residual = bufpages % nbuf;
371 if (base >= MAXBSIZE) {
372 /* Don't want to alloc more physical mem than ever needed */
373 base = MAXBSIZE;
374 residual = 0;
375 }
376 for (i = 0; i < nbuf; i++) {
377 vsize_t curbufsize;
378 vaddr_t curbuf;
379 struct vm_page *pg;
380
381 /*
382 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
383 * that MAXBSIZE space, we allocate and map (base+1) pages
384 * for the first "residual" buffers, and then we allocate
385 * "base" pages for the rest.
386 */
387 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
388 curbufsize = NBPG * ((i < residual) ? (base+1) : base);
389
390 while (curbufsize) {
391 pg = uvm_pagealloc(NULL, 0, NULL, 0);
392 if (pg == NULL)
393 panic("startup: not enough memory for "
394 "buffer cache");
395 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
396 VM_PROT_READ | VM_PROT_WRITE);
397 curbuf += PAGE_SIZE;
398 curbufsize -= PAGE_SIZE;
399 }
400 }
401 pmap_update(kernel_map->pmap);
402
403 /*
404 * Allocate a submap for exec arguments. This map effectively
405 * limits the number of processes exec'ing at any time.
406 */
407 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
408 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
409
410 /*
411 * Allocate a submap for physio
412 */
413 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
414 VM_PHYS_SIZE, 0, FALSE, NULL);
415
416 /*
417 * No need to allocate an mbuf cluster submap. Mbuf clusters
418 * are allocated via the pool allocator, and we use direct-mapped
419 * pool pages.
420 */
421
422 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
423 printf("avail memory = %s\n", pbuf);
424 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
425 printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
426
427 /*
428 * Set up the buffers.
429 */
430 bufinit();
431
432 /*
433 * Now allow hardware interrupts.
434 */
435 {
436 int msr;
437
438 splhigh();
439 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
440 : "=r"(msr) : "K"((u_short)(PSL_EE|PSL_RI)));
441 }
442 }
443
444 void
445 consinit()
446 {
447
448 /* Nothing to do; console is already initialized. */
449 }
450
451 int ofppc_cngetc(dev_t);
452 void ofppc_cnputc(dev_t, int);
453
454 struct consdev ofppc_bootcons = {
455 NULL, NULL, ofppc_cngetc, ofppc_cnputc, nullcnpollc, NULL,
456 makedev(0,0), 1,
457 };
458
459 int ofppc_stdin_ihandle, ofppc_stdout_ihandle;
460 int ofppc_stdin_phandle, ofppc_stdout_phandle;
461
462 void
463 ofppc_bootstrap_console(void)
464 {
465 int chosen;
466 char data[4];
467
468 chosen = OF_finddevice("/chosen");
469
470 if (OF_getprop(chosen, "stdin", data, sizeof(data)) != sizeof(int))
471 goto nocons;
472 ofppc_stdin_ihandle = of_decode_int(data);
473 ofppc_stdin_phandle = OF_instance_to_package(ofppc_stdin_ihandle);
474
475 if (OF_getprop(chosen, "stdout", data, sizeof(data)) != sizeof(int))
476 goto nocons;
477 ofppc_stdout_ihandle = of_decode_int(data);
478 ofppc_stdout_phandle = OF_instance_to_package(ofppc_stdout_ihandle);
479
480 cn_tab = &ofppc_bootcons;
481
482 nocons:
483 return;
484 }
485
486 int
487 ofppc_cngetc(dev_t dev)
488 {
489 u_char ch = '\0';
490 int l;
491
492 while ((l = OF_read(ofppc_stdin_ihandle, &ch, 1)) != 1)
493 if (l != -2 && l != 0)
494 return (-1);
495
496 return (ch);
497 }
498
499 void
500 ofppc_cnputc(dev_t dev, int c)
501 {
502 char ch = c;
503
504 OF_write(ofppc_stdout_ihandle, &ch, 1);
505 }
506
507 /*
508 * Crash dump handling.
509 */
510
511 void
512 dumpsys()
513 {
514 printf("dumpsys: TBD\n");
515 }
516
517 /*
518 * Soft networking interrupts.
519 */
520 void
521 softnet()
522 {
523 int isr = netisr;
524
525 netisr = 0;
526
527 #define DONETISR(bit, fn) do { \
528 if (isr & (1 << bit)) \
529 fn(); \
530 } while (0)
531
532 #include <net/netisr_dispatch.h>
533
534 #undef DONETISR
535 }
536
537 /*
538 * Stray interrupts.
539 */
540 void
541 strayintr(irq)
542 int irq;
543 {
544 log(LOG_ERR, "stray interrupt %d\n", irq);
545 }
546
547 /*
548 * Halt or reboot the machine after syncing/dumping according to howto.
549 */
550 void
551 cpu_reboot(howto, what)
552 int howto;
553 char *what;
554 {
555 static int syncing;
556 static char str[256];
557 char *ap = str, *ap1 = ap;
558
559 boothowto = howto;
560 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
561 syncing = 1;
562 vfs_shutdown(); /* sync */
563 resettodr(); /* set wall clock */
564 }
565 splhigh();
566 if (howto & RB_HALT) {
567 doshutdownhooks();
568 printf("halted\n\n");
569 ppc_exit();
570 }
571 if (!cold && (howto & RB_DUMP))
572 dumpsys();
573 doshutdownhooks();
574 printf("rebooting\n\n");
575 if (what && *what) {
576 if (strlen(what) > sizeof str - 5)
577 printf("boot string too large, ignored\n");
578 else {
579 strcpy(str, what);
580 ap1 = ap = str + strlen(str);
581 *ap++ = ' ';
582 }
583 }
584 *ap++ = '-';
585 if (howto & RB_SINGLE)
586 *ap++ = 's';
587 if (howto & RB_KDB)
588 *ap++ = 'd';
589 *ap++ = 0;
590 if (ap[-2] == '-')
591 *ap1 = 0;
592 ppc_boot(str);
593 }
594
595 #ifdef notyet
596 /*
597 * OpenFirmware callback routine
598 */
599 void
600 callback(p)
601 void *p;
602 {
603 panic("callback"); /* for now XXX */
604 }
605 #endif
606
607 /*
608 * Perform an `splx()' for locore.
609 */
610 int
611 lcsplx(int ipl)
612 {
613
614 return (_spllower(ipl));
615 }
616
617 /*
618 * Initial Machine Interface.
619 */
620 static int
621 fake_spl(int new)
622 {
623 int scratch;
624
625 asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
626 : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
627 return (-1);
628 }
629
630 static void
631 fake_setsoft(int ipl)
632 {
633 /* Do nothing */
634 }
635
636 static void
637 fake_splx(new)
638 int new;
639 {
640
641 (void) fake_spl(0);
642 }
643
644 static void
645 fake_clock_return(frame, nticks)
646 struct clockframe *frame;
647 int nticks;
648 {
649 /* Do nothing */
650 }
651
652 static void *
653 fake_intr_establish(irq, level, ist, handler, arg)
654 int irq, level, ist;
655 int (*handler) __P((void *));
656 void *arg;
657 {
658
659 panic("fake_intr_establish");
660 }
661
662 static void
663 fake_intr_disestablish(cookie)
664 void *cookie;
665 {
666
667 panic("fake_intr_disestablish");
668 }
669