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