machdep.c revision 1.14 1 /* $NetBSD: machdep.c,v 1.14 2003/07/15 01:37:37 lukem Exp $ */
2
3 /*
4 * Copyright 2001, 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
40 * Copyright (C) 1995, 1996 TooLs GmbH.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by TooLs GmbH.
54 * 4. The name of TooLs GmbH may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.14 2003/07/15 01:37:37 lukem Exp $");
71
72 #include "opt_compat_netbsd.h"
73 #include "opt_ddb.h"
74 #include "opt_ipkdb.h"
75
76 #include <sys/param.h>
77 #include <sys/buf.h>
78 #include <sys/exec.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/mount.h>
82 #include <sys/msgbuf.h>
83 #include <sys/proc.h>
84 #include <sys/reboot.h>
85 #include <sys/sa.h>
86 #include <sys/syscallargs.h>
87 #include <sys/syslog.h>
88 #include <sys/systm.h>
89 #include <sys/kernel.h>
90 #include <sys/user.h>
91 #include <sys/boot_flag.h>
92 #include <sys/properties.h>
93 #include <sys/ksyms.h>
94
95 #include <uvm/uvm_extern.h>
96
97 #include <net/netisr.h>
98
99 #include <machine/bus.h>
100 #include <machine/powerpc.h>
101 #include <machine/trap.h>
102 #include <machine/walnut.h>
103
104 #include <powerpc/spr.h>
105 #include <powerpc/ibm4xx/dcr405gp.h>
106 #include <machine/bus.h>
107
108 #include <dev/cons.h>
109
110 #include "ksyms.h"
111
112 #if defined(DDB)
113 #include <machine/db_machdep.h>
114 #include <ddb/db_extern.h>
115 #endif
116
117 /*
118 * Global variables used here and there
119 */
120 struct vm_map *exec_map = NULL;
121 struct vm_map *mb_map = NULL;
122 struct vm_map *phys_map = NULL;
123
124 /*
125 * This should probably be in autoconf! XXX
126 */
127 char cpu_model[80];
128 char machine[] = MACHINE; /* from <machine/param.h> */
129 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
130
131 extern struct user *proc0paddr;
132
133 char bootpath[256];
134 paddr_t msgbuf_paddr;
135 vaddr_t msgbuf_vaddr;
136
137 #if NKSYMS || defined(DDB) || defined(LKM)
138 void *startsym, *endsym;
139 #endif
140
141 int lcsplx(int);
142 void initppc(u_int, u_int, char *, void *);
143
144 static void dumpsys(void);
145 static void install_extint(void (*)(void));
146
147 #define MEMREGIONS 8
148 struct mem_region physmemr[MEMREGIONS]; /* Hard code memory */
149 struct mem_region availmemr[MEMREGIONS]; /* Who's supposed to set these up? */
150
151 struct board_cfg_data board_data;
152 struct propdb *board_info = NULL;
153
154 void
155 initppc(u_int startkernel, u_int endkernel, char *args, void *info_block)
156 {
157 extern int defaulttrap, defaultsize;
158 extern int sctrap, scsize;
159 extern int alitrap, alisize;
160 extern int dsitrap, dsisize;
161 extern int isitrap, isisize;
162 extern int mchktrap, mchksize;
163 extern int tlbimiss4xx, tlbim4size;
164 extern int tlbdmiss4xx, tlbdm4size;
165 extern int pitfitwdog, pitfitwdogsize;
166 extern int debugtrap, debugsize;
167 extern int errata51handler, errata51size;
168 #ifdef DDB
169 extern int ddblow, ddbsize;
170 #endif
171 #ifdef IPKDB
172 extern int ipkdblow, ipkdbsize;
173 #endif
174 int exc;
175 struct cpu_info * const ci = curcpu();
176
177 /* Disable all external interrupts */
178 mtdcr(DCR_UIC0_ER, 0);
179
180 /* Initialize cache info for memcpy, etc. */
181 cpu_probe_cache();
182
183 /* Save info block */
184 if (info_block == NULL)
185 /* XXX why isn't r3 set correctly?!?!? */
186 info_block = (void *)0x8e10;
187 memcpy(&board_data, info_block, sizeof(board_data));
188
189 memset(physmemr, 0, sizeof physmemr);
190 memset(availmemr, 0, sizeof availmemr);
191 physmemr[0].start = 0;
192 physmemr[0].size = board_data.mem_size & ~PGOFSET;
193 /* Lower memory reserved by eval board BIOS */
194 availmemr[0].start = startkernel;
195 availmemr[0].size = board_data.mem_size - availmemr[0].start;
196
197 /*
198 * Initialize lwp0 and current pcb and pmap pointers.
199 */
200 lwp0.l_cpu = ci;
201 lwp0.l_addr = proc0paddr;
202 memset(lwp0.l_addr, 0, sizeof *lwp0.l_addr);
203
204 curpcb = &proc0paddr->u_pcb;
205 curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
206
207 /*
208 * Set up trap vectors
209 */
210 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
211 switch (exc) {
212 default:
213 memcpy((void *)exc, &defaulttrap, (size_t)&defaultsize);
214 break;
215 case EXC_EXI:
216 /*
217 * This one is (potentially) installed during autoconf
218 */
219 break;
220 case EXC_SC:
221 memcpy((void *)EXC_SC, &sctrap, (size_t)&scsize);
222 break;
223 case EXC_ALI:
224 memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
225 break;
226 case EXC_DSI:
227 memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
228 break;
229 case EXC_ISI:
230 memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
231 break;
232 case EXC_MCHK:
233 memcpy((void *)EXC_MCHK, &mchktrap, (size_t)&mchksize);
234 break;
235 case EXC_ITMISS:
236 memcpy((void *)EXC_ITMISS, &tlbimiss4xx,
237 (size_t)&tlbim4size);
238 break;
239 case EXC_DTMISS:
240 memcpy((void *)EXC_DTMISS, &tlbdmiss4xx,
241 (size_t)&tlbdm4size);
242 break;
243 /*
244 * EXC_PIT, EXC_FIT, EXC_WDOG handlers
245 * are spaced by 0x10 bytes only..
246 */
247 case EXC_PIT:
248 memcpy((void *)EXC_PIT, &pitfitwdog,
249 (size_t)&pitfitwdogsize);
250 break;
251 case EXC_DEBUG:
252 memcpy((void *)EXC_DEBUG, &debugtrap,
253 (size_t)&debugsize);
254 break;
255 case EXC_DTMISS|EXC_ALI:
256 /* PPC405GP Rev D errata item 51 */
257 memcpy((void *)(EXC_DTMISS|EXC_ALI), &errata51handler,
258 (size_t)&errata51size);
259 break;
260 #if defined(DDB) || defined(IPKDB)
261 case EXC_PGM:
262 #if defined(DDB)
263 memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
264 #elif defined(IPKDB)
265 memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
266 #endif
267 #endif /* DDB | IPKDB */
268 break;
269 }
270
271 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
272 mtspr(SPR_EVPR, 0); /* Set Exception vector base */
273 consinit();
274
275 /* Handle trap instruction as PGM exception */
276 {
277 int dbcr0;
278 asm volatile("mfspr %0,%1":"=r"(dbcr0):"K"(SPR_DBCR0));
279 asm volatile("mtspr %0,%1"::"K"(SPR_DBCR0),"r"(dbcr0 & ~DBCR0_TDE));
280 }
281
282 /*
283 * external interrupt handler install
284 */
285 install_extint(ext_intr);
286
287 /*
288 * Now enable translation (and machine checks/recoverable interrupts).
289 */
290 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
291 : : "r"(0), "K"(PSL_IR|PSL_DR));
292 /* XXXX PSL_ME - With ME set kernel gets stuck... */
293
294 uvm_setpagesize();
295
296 /*
297 * Initialize pmap module.
298 */
299 pmap_bootstrap(startkernel, endkernel);
300
301 consinit();
302
303 #ifdef DEBUG
304 printf("Board config data:\n");
305 printf(" usr_config_ver = %s\n", board_data.usr_config_ver);
306 printf(" rom_sw_ver = %s\n", board_data.rom_sw_ver);
307 printf(" mem_size = %u\n", board_data.mem_size);
308 printf(" mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
309 board_data.mac_address_local[0], board_data.mac_address_local[1],
310 board_data.mac_address_local[2], board_data.mac_address_local[3],
311 board_data.mac_address_local[4], board_data.mac_address_local[5]);
312 printf(" mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
313 board_data.mac_address_pci[0], board_data.mac_address_pci[1],
314 board_data.mac_address_pci[2], board_data.mac_address_pci[3],
315 board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
316 printf(" processor_speed = %u\n", board_data.processor_speed);
317 printf(" plb_speed = %u\n", board_data.plb_speed);
318 printf(" pci_speed = %u\n", board_data.pci_speed);
319 #endif
320
321 #if NKSYMS || defined(DDB) || defined(LKM)
322 ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
323 #endif
324 #ifdef DDB
325 if (boothowto & RB_KDB)
326 Debugger();
327 #endif
328 #ifdef IPKDB
329 /*
330 * Now trap to IPKDB
331 */
332 ipkdb_init();
333 if (boothowto & RB_KDB)
334 ipkdb_connect(0);
335 #endif
336 fake_mapiodev = 0;
337 }
338
339 static void
340 install_extint(void (*handler)(void))
341 {
342 extern int extint, extsize;
343 extern u_long extint_call;
344 u_long offset = (u_long)handler - (u_long)&extint_call;
345 int msr;
346
347 #ifdef DIAGNOSTIC
348 if (offset > 0x1ffffff)
349 panic("install_extint: too far away");
350 #endif
351 asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
352 extint_call = (extint_call & 0xfc000003) | offset;
353 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
354 __syncicache((void *)&extint_call, sizeof extint_call);
355 __syncicache((void *)EXC_EXI, (int)&extsize);
356 asm volatile ("mtmsr %0" :: "r"(msr));
357 }
358
359 /*
360 * Machine dependent startup code.
361 */
362
363 char msgbuf[MSGBUFSIZE];
364
365 void
366 cpu_startup(void)
367 {
368 caddr_t v;
369 vaddr_t minaddr, maxaddr;
370 u_int sz, i, base, residual;
371 char pbuf[9];
372
373 /*
374 * Initialize error message buffer (at end of core).
375 */
376 #if 0 /* For some reason this fails... --Artem
377 * Besides, do we really have to put it at the end of core?
378 * Let's use static buffer for now
379 */
380 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE))))
381 panic("startup: no room for message buffer");
382 for (i = 0; i < btoc(MSGBUFSIZE); i++)
383 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
384 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
385 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
386 #else
387 initmsgbuf((caddr_t)msgbuf, round_page(MSGBUFSIZE));
388 #endif
389
390
391 printf("%s", version);
392 printf("Walnut PowerPC 405GP Evaluation Board\n");
393
394 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
395 printf("total memory = %s\n", pbuf);
396
397 /*
398 * Find out how much space we need, allocate it,
399 * and then give everything true virtual addresses.
400 */
401 sz = (u_int)allocsys(NULL, NULL);
402 if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
403 panic("startup: no room for tables");
404 if (allocsys(v, NULL) - v != sz)
405 panic("startup: table size inconsistency");
406
407 /*
408 * Now allocate buffers proper. They are different than the above
409 * in that they usually occupy more virtual memory than physical.
410 */
411 sz = MAXBSIZE * nbuf;
412 minaddr = 0;
413 if (uvm_map(kernel_map, (vaddr_t *)&minaddr, round_page(sz),
414 NULL, UVM_UNKNOWN_OFFSET, 0,
415 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
416 UVM_ADV_NORMAL, 0)) != 0)
417 panic("startup: cannot allocate VM for buffers");
418 buffers = (char *)minaddr;
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 curbuf = (vaddr_t)buffers + i * MAXBSIZE;
432 curbufsize = PAGE_SIZE * (i < residual ? base + 1 : base);
433
434 while (curbufsize) {
435 pg = uvm_pagealloc(NULL, 0, NULL, 0);
436 if (pg == NULL)
437 panic("cpu_startup: not enough memory for "
438 "buffer cache");
439 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
440 VM_PROT_READ | VM_PROT_WRITE);
441 curbuf += PAGE_SIZE;
442 curbufsize -= PAGE_SIZE;
443 }
444 }
445
446 /*
447 * Allocate a submap for exec arguments. This map effectively
448 * limits the number of processes exec'ing at any time.
449 */
450 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
451 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
452
453 /*
454 * Allocate a submap for physio
455 */
456 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
457 VM_PHYS_SIZE, 0, FALSE, NULL);
458
459 /*
460 * No need to allocate an mbuf cluster submap. Mbuf clusters
461 * are allocated via the pool allocator, and we use direct-mapped
462 * pool pages.
463 */
464
465 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
466 printf("avail memory = %s\n", pbuf);
467 format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
468 printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
469
470 /*
471 * Set up the buffers.
472 */
473 bufinit();
474
475 /*
476 * Set up the board properties database.
477 */
478 if (!(board_info = propdb_create("board info")))
479 panic("Cannot create board info database");
480
481 if (board_info_set("mem-size", &board_data.mem_size,
482 sizeof(&board_data.mem_size), PROP_CONST, 0))
483 panic("setting mem-size");
484 if (board_info_set("sip0-mac-addr", &board_data.mac_address_pci,
485 sizeof(&board_data.mac_address_pci), PROP_CONST, 0))
486 panic("setting sip0-mac-addr");
487 if (board_info_set("processor-frequency", &board_data.processor_speed,
488 sizeof(&board_data.processor_speed), PROP_CONST, 0))
489 panic("setting processor-frequency");
490 }
491
492
493 static void
494 dumpsys(void)
495 {
496
497 printf("dumpsys: TBD\n");
498 }
499
500 /*
501 * Soft networking interrupts.
502 */
503 void
504 softnet(void)
505 {
506 int isr;
507
508 isr = netisr;
509 netisr = 0;
510
511 #define DONETISR(bit, fn) do { \
512 if (isr & (1 << bit)) \
513 fn(); \
514 } while (0)
515
516 #include <net/netisr_dispatch.h>
517
518 #undef DONETISR
519
520 }
521
522 /*
523 * Soft tty interrupts.
524 */
525 #include "com.h"
526 void
527 softserial(void)
528 {
529 #if NCOM > 0
530 void comsoft(void); /* XXX from dev/ic/com.c */
531
532 comsoft();
533 #endif
534 }
535
536 /*
537 * Halt or reboot the machine after syncing/dumping according to howto.
538 */
539 void
540 cpu_reboot(int howto, char *what)
541 {
542 static int syncing;
543 static char str[256];
544 char *ap = str, *ap1 = ap;
545
546 boothowto = howto;
547 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
548 syncing = 1;
549 vfs_shutdown(); /* sync */
550 resettodr(); /* set wall clock */
551 }
552
553 splhigh();
554
555 if (!cold && (howto & RB_DUMP))
556 dumpsys();
557
558 doshutdownhooks();
559
560 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
561 /* Power off here if we know how...*/
562 }
563
564 if (howto & RB_HALT) {
565 printf("halted\n\n");
566
567 goto reboot; /* XXX for now... */
568
569 #ifdef DDB
570 printf("dropping to debugger\n");
571 while(1)
572 Debugger();
573 #endif
574 }
575
576 printf("rebooting\n\n");
577 if (what && *what) {
578 if (strlen(what) > sizeof str - 5)
579 printf("boot string too large, ignored\n");
580 else {
581 strcpy(str, what);
582 ap1 = ap = str + strlen(str);
583 *ap++ = ' ';
584 }
585 }
586 *ap++ = '-';
587 if (howto & RB_SINGLE)
588 *ap++ = 's';
589 if (howto & RB_KDB)
590 *ap++ = 'd';
591 *ap++ = 0;
592 if (ap[-2] == '-')
593 *ap1 = 0;
594
595 /* flush cache for msgbuf */
596 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
597
598 reboot:
599 ppc4xx_reset();
600
601 printf("ppc4xx_reset() failed!\n");
602 #ifdef DDB
603 while(1)
604 Debugger();
605 #else
606 while (1)
607 /* nothing */;
608 #endif
609 }
610
611 int
612 lcsplx(int ipl)
613 {
614
615 return spllower(ipl); /* XXX */
616 }
617
618 void
619 mem_regions(struct mem_region **mem, struct mem_region **avail)
620 {
621
622 *mem = physmemr;
623 *avail = availmemr;
624 }
625