machdep.c revision 1.9.2.4 1 /* $NetBSD: machdep.c,v 1.9.2.4 2005/01/24 08:34:12 skrll 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.9.2.4 2005/01/24 08:34:12 skrll 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
153 void
154 initppc(u_int startkernel, u_int endkernel, char *args, void *info_block)
155 {
156 extern int defaulttrap, defaultsize;
157 extern int sctrap, scsize;
158 extern int alitrap, alisize;
159 extern int dsitrap, dsisize;
160 extern int isitrap, isisize;
161 extern int mchktrap, mchksize;
162 extern int tlbimiss4xx, tlbim4size;
163 extern int tlbdmiss4xx, tlbdm4size;
164 extern int pitfitwdog, pitfitwdogsize;
165 extern int debugtrap, debugsize;
166 extern int errata51handler, errata51size;
167 #ifdef DDB
168 extern int ddblow, ddbsize;
169 #endif
170 #ifdef IPKDB
171 extern int ipkdblow, ipkdbsize;
172 #endif
173 int exc, dbcr0;
174 struct cpu_info * const ci = curcpu();
175
176 /* Disable all external interrupts */
177 mtdcr(DCR_UIC0_ER, 0);
178
179 /* Initialize cache info for memcpy, etc. */
180 cpu_probe_cache();
181
182 /* Save info block */
183 memcpy(&board_data, info_block, sizeof(board_data));
184
185 memset(physmemr, 0, sizeof physmemr);
186 memset(availmemr, 0, sizeof availmemr);
187 physmemr[0].start = 0;
188 physmemr[0].size = board_data.mem_size & ~PGOFSET;
189 /* Lower memory reserved by eval board BIOS */
190 availmemr[0].start = startkernel;
191 availmemr[0].size = board_data.mem_size - availmemr[0].start;
192
193 /*
194 * Initialize lwp0 and current pcb and pmap pointers.
195 */
196 lwp0.l_cpu = ci;
197 lwp0.l_addr = proc0paddr;
198 memset(lwp0.l_addr, 0, sizeof *lwp0.l_addr);
199
200 curpcb = &proc0paddr->u_pcb;
201 curpcb->pcb_pm = pmap_kernel();
202
203 /*
204 * Set up trap vectors
205 */
206 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
207 switch (exc) {
208 default:
209 memcpy((void *)exc, &defaulttrap, (size_t)&defaultsize);
210 break;
211 case EXC_EXI:
212 /*
213 * This one is (potentially) installed during autoconf
214 */
215 break;
216 case EXC_SC:
217 memcpy((void *)EXC_SC, &sctrap, (size_t)&scsize);
218 break;
219 case EXC_ALI:
220 memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
221 break;
222 case EXC_DSI:
223 memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
224 break;
225 case EXC_ISI:
226 memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
227 break;
228 case EXC_MCHK:
229 memcpy((void *)EXC_MCHK, &mchktrap, (size_t)&mchksize);
230 break;
231 case EXC_ITMISS:
232 memcpy((void *)EXC_ITMISS, &tlbimiss4xx,
233 (size_t)&tlbim4size);
234 break;
235 case EXC_DTMISS:
236 memcpy((void *)EXC_DTMISS, &tlbdmiss4xx,
237 (size_t)&tlbdm4size);
238 break;
239 /*
240 * EXC_PIT, EXC_FIT, EXC_WDOG handlers
241 * are spaced by 0x10 bytes only..
242 */
243 case EXC_PIT:
244 memcpy((void *)EXC_PIT, &pitfitwdog,
245 (size_t)&pitfitwdogsize);
246 break;
247 case EXC_DEBUG:
248 memcpy((void *)EXC_DEBUG, &debugtrap,
249 (size_t)&debugsize);
250 break;
251 case EXC_DTMISS|EXC_ALI:
252 /* PPC405GP Rev D errata item 51 */
253 memcpy((void *)(EXC_DTMISS|EXC_ALI), &errata51handler,
254 (size_t)&errata51size);
255 break;
256 #if defined(DDB) || defined(IPKDB)
257 case EXC_PGM:
258 #if defined(DDB)
259 memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
260 #elif defined(IPKDB)
261 memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
262 #endif
263 #endif /* DDB | IPKDB */
264 break;
265 }
266
267 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
268 mtspr(SPR_EVPR, 0); /* Set Exception vector base */
269 consinit();
270
271 /* Handle trap instruction as PGM exception */
272 asm volatile("mfspr %0,%1":"=r"(dbcr0):"K"(SPR_DBCR0));
273 asm volatile("mtspr %0,%1"::"K"(SPR_DBCR0),"r"(dbcr0 & ~DBCR0_TDE));
274
275 /*
276 * external interrupt handler install
277 */
278 install_extint(ext_intr);
279
280 /*
281 * Now enable translation (and machine checks/recoverable interrupts).
282 */
283 asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
284 : : "r"(0), "K"(PSL_IR|PSL_DR));
285 /* XXXX PSL_ME - With ME set kernel gets stuck... */
286
287 uvm_setpagesize();
288
289 /*
290 * Initialize pmap module.
291 */
292 pmap_bootstrap(startkernel, endkernel);
293
294 #ifdef DEBUG
295 printf("Board config data:\n");
296 printf(" usr_config_ver = %s\n", board_data.usr_config_ver);
297 printf(" rom_sw_ver = %s\n", board_data.rom_sw_ver);
298 printf(" mem_size = %u\n", board_data.mem_size);
299 printf(" mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
300 board_data.mac_address_local[0], board_data.mac_address_local[1],
301 board_data.mac_address_local[2], board_data.mac_address_local[3],
302 board_data.mac_address_local[4], board_data.mac_address_local[5]);
303 printf(" mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
304 board_data.mac_address_pci[0], board_data.mac_address_pci[1],
305 board_data.mac_address_pci[2], board_data.mac_address_pci[3],
306 board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
307 printf(" processor_speed = %u\n", board_data.processor_speed);
308 printf(" plb_speed = %u\n", board_data.plb_speed);
309 printf(" pci_speed = %u\n", board_data.pci_speed);
310 #endif
311
312 #if NKSYMS || defined(DDB) || defined(LKM)
313 ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
314 #endif
315 #ifdef DDB
316 if (boothowto & RB_KDB)
317 Debugger();
318 #endif
319 #ifdef IPKDB
320 /*
321 * Now trap to IPKDB
322 */
323 ipkdb_init();
324 if (boothowto & RB_KDB)
325 ipkdb_connect(0);
326 #endif
327 }
328
329 static void
330 install_extint(void (*handler)(void))
331 {
332 extern int extint, extsize;
333 extern u_long extint_call;
334 u_long offset = (u_long)handler - (u_long)&extint_call;
335 int msr;
336
337 #ifdef DIAGNOSTIC
338 if (offset > 0x1ffffff)
339 panic("install_extint: too far away");
340 #endif
341 asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
342 extint_call = (extint_call & 0xfc000003) | offset;
343 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
344 __syncicache((void *)&extint_call, sizeof extint_call);
345 __syncicache((void *)EXC_EXI, (int)&extsize);
346 asm volatile ("mtmsr %0" :: "r"(msr));
347 }
348
349 /*
350 * Machine dependent startup code.
351 */
352
353 char msgbuf[MSGBUFSIZE];
354
355 void
356 cpu_startup(void)
357 {
358 vaddr_t minaddr, maxaddr;
359 char pbuf[9];
360
361 /*
362 * Initialize error message buffer (at end of core).
363 */
364 #if 0 /* For some reason this fails... --Artem
365 * Besides, do we really have to put it at the end of core?
366 * Let's use static buffer for now
367 */
368 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE))))
369 panic("startup: no room for message buffer");
370 for (i = 0; i < btoc(MSGBUFSIZE); i++)
371 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
372 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
373 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
374 #else
375 initmsgbuf((caddr_t)msgbuf, round_page(MSGBUFSIZE));
376 #endif
377
378 printf("%s", version);
379 printf("Walnut PowerPC 405GP Evaluation Board\n");
380
381 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
382 printf("total memory = %s\n", pbuf);
383
384 minaddr = 0;
385 /*
386 * Allocate a submap for exec arguments. This map effectively
387 * limits the number of processes exec'ing at any time.
388 */
389 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
390 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
391
392 /*
393 * Allocate a submap for physio
394 */
395 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
396 VM_PHYS_SIZE, 0, FALSE, NULL);
397
398 /*
399 * No need to allocate an mbuf cluster submap. Mbuf clusters
400 * are allocated via the pool allocator, and we use direct-mapped
401 * pool pages.
402 */
403
404 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
405 printf("avail memory = %s\n", pbuf);
406
407 /*
408 * Set up the board properties database.
409 */
410 if (!(board_info = propdb_create("board info")))
411 panic("Cannot create board info database");
412
413 if (board_info_set("mem-size", &board_data.mem_size,
414 sizeof(&board_data.mem_size), PROP_CONST, 0))
415 panic("setting mem-size");
416 if (board_info_set("sip0-mac-addr", &board_data.mac_address_pci,
417 sizeof(&board_data.mac_address_pci), PROP_CONST, 0))
418 panic("setting sip0-mac-addr");
419 if (board_info_set("processor-frequency", &board_data.processor_speed,
420 sizeof(&board_data.processor_speed), PROP_CONST, 0))
421 panic("setting processor-frequency");
422
423 /*
424 * Now that we have VM, malloc()s are OK in bus_space.
425 */
426 bus_space_mallocok();
427 fake_mapiodev = 0;
428 }
429
430
431 static void
432 dumpsys(void)
433 {
434
435 printf("dumpsys: TBD\n");
436 }
437
438 /*
439 * Soft networking interrupts.
440 */
441 void
442 softnet(void)
443 {
444 int isr;
445
446 isr = netisr;
447 netisr = 0;
448
449 #define DONETISR(bit, fn) do { \
450 if (isr & (1 << bit)) \
451 fn(); \
452 } while (0)
453
454 #include <net/netisr_dispatch.h>
455
456 #undef DONETISR
457
458 }
459
460 /*
461 * Soft tty interrupts.
462 */
463 #include "com.h"
464 void
465 softserial(void)
466 {
467 #if NCOM > 0
468 void comsoft(void); /* XXX from dev/ic/com.c */
469
470 comsoft();
471 #endif
472 }
473
474 /*
475 * Halt or reboot the machine after syncing/dumping according to howto.
476 */
477 void
478 cpu_reboot(int howto, char *what)
479 {
480 static int syncing;
481 static char str[256];
482 char *ap = str, *ap1 = ap;
483
484 boothowto = howto;
485 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
486 syncing = 1;
487 vfs_shutdown(); /* sync */
488 resettodr(); /* set wall clock */
489 }
490
491 splhigh();
492
493 if (!cold && (howto & RB_DUMP))
494 dumpsys();
495
496 doshutdownhooks();
497
498 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
499 /* Power off here if we know how...*/
500 }
501
502 if (howto & RB_HALT) {
503 printf("halted\n\n");
504
505 goto reboot; /* XXX for now... */
506
507 #ifdef DDB
508 printf("dropping to debugger\n");
509 while(1)
510 Debugger();
511 #endif
512 }
513
514 printf("rebooting\n\n");
515 if (what && *what) {
516 if (strlen(what) > sizeof str - 5)
517 printf("boot string too large, ignored\n");
518 else {
519 strcpy(str, what);
520 ap1 = ap = str + strlen(str);
521 *ap++ = ' ';
522 }
523 }
524 *ap++ = '-';
525 if (howto & RB_SINGLE)
526 *ap++ = 's';
527 if (howto & RB_KDB)
528 *ap++ = 'd';
529 *ap++ = 0;
530 if (ap[-2] == '-')
531 *ap1 = 0;
532
533 /* flush cache for msgbuf */
534 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
535
536 reboot:
537 ppc4xx_reset();
538
539 printf("ppc4xx_reset() failed!\n");
540 #ifdef DDB
541 while(1)
542 Debugger();
543 #else
544 while (1)
545 /* nothing */;
546 #endif
547 }
548
549 int
550 lcsplx(int ipl)
551 {
552
553 return spllower(ipl); /* XXX */
554 }
555
556 void
557 mem_regions(struct mem_region **mem, struct mem_region **avail)
558 {
559
560 *mem = physmemr;
561 *avail = availmemr;
562 }
563