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