machdep.c revision 1.49 1 /* $NetBSD: machdep.c,v 1.49 2011/06/12 03:42:42 mrg 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.49 2011/06/12 03:42:42 mrg Exp $");
71
72 #include "opt_compat_netbsd.h"
73 #include "opt_ddb.h"
74 #include "opt_ipkdb.h"
75 #include "opt_modular.h"
76
77 #include <sys/param.h>
78 #include <sys/buf.h>
79 #include <sys/exec.h>
80 #include <sys/malloc.h>
81 #include <sys/mbuf.h>
82 #include <sys/mount.h>
83 #include <sys/msgbuf.h>
84 #include <sys/proc.h>
85 #include <sys/reboot.h>
86 #include <sys/syscallargs.h>
87 #include <sys/syslog.h>
88 #include <sys/systm.h>
89 #include <sys/kernel.h>
90 #include <sys/boot_flag.h>
91 #include <sys/ksyms.h>
92 #include <sys/device.h>
93 #include <sys/module.h>
94
95 #include <uvm/uvm_extern.h>
96
97 #include <net/netisr.h>
98
99 #include <prop/proplib.h>
100
101 #include <machine/bus.h>
102 #include <machine/powerpc.h>
103 #include <machine/trap.h>
104 #include <machine/walnut.h>
105 #include <machine/pcb.h>
106
107 #include <powerpc/spr.h>
108 #include <powerpc/ibm4xx/spr.h>
109 #include <powerpc/ibm4xx/dcr4xx.h>
110
111 #include <dev/cons.h>
112 #include <dev/pci/pcivar.h>
113 #include <dev/pci/pciconf.h>
114
115 #include "ksyms.h"
116
117 #if defined(DDB)
118 #include <machine/db_machdep.h>
119 #include <ddb/db_extern.h>
120 #endif
121
122
123 #define TLB_PG_SIZE (16*1024*1024)
124
125 /*
126 * Global variables used here and there
127 */
128 struct vm_map *phys_map = NULL;
129
130 /*
131 * This should probably be in autoconf! XXX
132 */
133 char cpu_model[80];
134 char machine[] = MACHINE; /* from <machine/param.h> */
135 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
136
137 char bootpath[256];
138 paddr_t msgbuf_paddr;
139 vaddr_t msgbuf_vaddr;
140
141 #if NKSYMS || defined(DDB) || defined(MODULAR)
142 void *startsym, *endsym;
143 #endif
144
145 int lcsplx(int);
146 void initppc(u_int, u_int, char *, void *);
147
148 static void dumpsys(void);
149 static void install_extint(void (*)(void));
150
151 #define MEMREGIONS 8
152 struct mem_region physmemr[MEMREGIONS]; /* Hard code memory */
153 struct mem_region availmemr[MEMREGIONS]; /* Who's supposed to set these up? */
154
155 struct board_cfg_data board_data;
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 vaddr_t va;
178 int exc, dbcr0;
179 struct cpu_info * const ci = curcpu();
180
181 /* Disable all external interrupts */
182 mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
183
184 /* Initialize cache info for memcpy, etc. */
185 cpu_probe_cache();
186
187 /* Save info block */
188 memcpy(&board_data, info_block, sizeof(board_data));
189
190 memset(physmemr, 0, sizeof physmemr);
191 memset(availmemr, 0, sizeof availmemr);
192 physmemr[0].start = 0;
193 physmemr[0].size = board_data.mem_size & ~PGOFSET;
194 /* Lower memory reserved by eval board BIOS */
195 availmemr[0].start = startkernel;
196 availmemr[0].size = board_data.mem_size - availmemr[0].start;
197
198 /* Linear map kernel memory */
199 for (va = 0; va < endkernel; va += TLB_PG_SIZE)
200 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
201
202 /* Map console after physmem (see pmap_tlbmiss()) */
203 ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE),
204 TLB_PG_SIZE, TLB_I | TLB_G);
205
206 /*
207 * Initialize lwp0 and current pcb and pmap pointers.
208 */
209 lwp0.l_cpu = ci;
210
211 curpcb = lwp_getpcb(&lwp0);
212 memset(curpcb, 0, sizeof(struct pcb));
213 curpcb->pcb_pm = pmap_kernel();
214
215 /*
216 * Set up trap vectors
217 */
218 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
219 switch (exc) {
220 default:
221 memcpy((void *)exc, &defaulttrap, (size_t)&defaultsize);
222 break;
223 case EXC_EXI:
224 /*
225 * This one is (potentially) installed during autoconf
226 */
227 break;
228 case EXC_SC:
229 memcpy((void *)EXC_SC, &sctrap, (size_t)&scsize);
230 break;
231 case EXC_ALI:
232 memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
233 break;
234 case EXC_DSI:
235 memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
236 break;
237 case EXC_ISI:
238 memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
239 break;
240 case EXC_MCHK:
241 memcpy((void *)EXC_MCHK, &mchktrap, (size_t)&mchksize);
242 break;
243 case EXC_ITMISS:
244 memcpy((void *)EXC_ITMISS, &tlbimiss4xx,
245 (size_t)&tlbim4size);
246 break;
247 case EXC_DTMISS:
248 memcpy((void *)EXC_DTMISS, &tlbdmiss4xx,
249 (size_t)&tlbdm4size);
250 break;
251 /*
252 * EXC_PIT, EXC_FIT, EXC_WDOG handlers
253 * are spaced by 0x10 bytes only..
254 */
255 case EXC_PIT:
256 memcpy((void *)EXC_PIT, &pitfitwdog,
257 (size_t)&pitfitwdogsize);
258 break;
259 case EXC_DEBUG:
260 memcpy((void *)EXC_DEBUG, &debugtrap,
261 (size_t)&debugsize);
262 break;
263 case EXC_DTMISS|EXC_ALI:
264 /* PPC405GP Rev D errata item 51 */
265 memcpy((void *)(EXC_DTMISS|EXC_ALI), &errata51handler,
266 (size_t)&errata51size);
267 break;
268 #if defined(DDB) || defined(IPKDB)
269 case EXC_PGM:
270 #if defined(DDB)
271 memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
272 #elif defined(IPKDB)
273 memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
274 #endif
275 #endif /* DDB | IPKDB */
276 break;
277 }
278
279 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
280 mtspr(SPR_EVPR, 0); /* Set Exception vector base */
281
282 consinit();
283
284 /* Handle trap instruction as PGM exception */
285 __asm volatile("mfspr %0,%1":"=r"(dbcr0):"K"(SPR_DBCR0));
286 __asm volatile("mtspr %0,%1"::"K"(SPR_DBCR0),"r"(dbcr0 & ~DBCR0_TDE));
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 #ifdef DEBUG
308 printf("Board config data:\n");
309 printf(" usr_config_ver = %s\n", board_data.usr_config_ver);
310 printf(" rom_sw_ver = %s\n", board_data.rom_sw_ver);
311 printf(" mem_size = %u\n", board_data.mem_size);
312 printf(" mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
313 board_data.mac_address_local[0], board_data.mac_address_local[1],
314 board_data.mac_address_local[2], board_data.mac_address_local[3],
315 board_data.mac_address_local[4], board_data.mac_address_local[5]);
316 printf(" mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
317 board_data.mac_address_pci[0], board_data.mac_address_pci[1],
318 board_data.mac_address_pci[2], board_data.mac_address_pci[3],
319 board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
320 printf(" processor_speed = %u\n", board_data.processor_speed);
321 printf(" plb_speed = %u\n", board_data.plb_speed);
322 printf(" pci_speed = %u\n", board_data.pci_speed);
323 #endif
324
325 #if NKSYMS || defined(DDB) || defined(MODULAR)
326 ksyms_addsyms_elf((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
327 #endif
328 #ifdef DDB
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
341 /*
342 * Look for the ibm4xx modules in the right place.
343 */
344 module_machine = module_machine_ibm4xx;
345 }
346
347 static void
348 install_extint(void (*handler)(void))
349 {
350 extern int extint, extsize;
351 extern u_long extint_call;
352 u_long offset = (u_long)handler - (u_long)&extint_call;
353 int msr;
354
355 #ifdef DIAGNOSTIC
356 if (offset > 0x1ffffff)
357 panic("install_extint: too far away");
358 #endif
359 __asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
360 extint_call = (extint_call & 0xfc000003) | offset;
361 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
362 __syncicache((void *)&extint_call, sizeof extint_call);
363 __syncicache((void *)EXC_EXI, (int)&extsize);
364 __asm volatile ("mtmsr %0" :: "r"(msr));
365 }
366
367 /*
368 * Machine dependent startup code.
369 */
370
371 char msgbuf[MSGBUFSIZE];
372
373 void
374 cpu_startup(void)
375 {
376 vaddr_t minaddr, maxaddr;
377 prop_number_t pn;
378 prop_data_t pd;
379 char pbuf[9];
380
381 /*
382 * Initialize error message buffer (at end of core).
383 */
384 #if 0 /* For some reason this fails... --Artem
385 * Besides, do we really have to put it at the end of core?
386 * Let's use static buffer for now
387 */
388 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
389 UVM_KMF_VAONLY)))
390 panic("startup: no room for message buffer");
391 for (i = 0; i < btoc(MSGBUFSIZE); i++)
392 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
393 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
394 initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
395 #else
396 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
397 #endif
398
399 printf("%s%s", copyright, version);
400 printf("Walnut PowerPC 405GP Evaluation Board\n");
401
402 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
403 printf("total memory = %s\n", pbuf);
404
405 minaddr = 0;
406 /*
407 * Allocate a submap for physio
408 */
409 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
410 VM_PHYS_SIZE, 0, false, NULL);
411
412 /*
413 * No need to allocate an mbuf cluster submap. Mbuf clusters
414 * are allocated via the pool allocator, and we use direct-mapped
415 * pool pages.
416 */
417
418 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
419 printf("avail memory = %s\n", pbuf);
420
421 /*
422 * Set up the board properties dictionary.
423 */
424 board_properties = prop_dictionary_create();
425 KASSERT(board_properties != NULL);
426
427 pn = prop_number_create_integer(board_data.mem_size);
428 KASSERT(pn != NULL);
429 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
430 panic("setting mem-size");
431 prop_object_release(pn);
432
433 pd = prop_data_create_data_nocopy(board_data.mac_address_local,
434 sizeof(board_data.mac_address_local));
435 KASSERT(pd != NULL);
436 if (prop_dictionary_set(board_properties, "emac0-mac-addr",
437 pd) == false)
438 panic("setting emac0-mac-addr");
439 prop_object_release(pd);
440
441 pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
442 sizeof(board_data.mac_address_pci));
443 KASSERT(pd != NULL);
444 if (prop_dictionary_set(board_properties, "sip0-mac-addr",
445 pd) == false)
446 panic("setting sip0-mac-addr");
447 prop_object_release(pd);
448
449 pn = prop_number_create_integer(board_data.processor_speed);
450 KASSERT(pn != NULL);
451 if (prop_dictionary_set(board_properties, "processor-frequency",
452 pn) == false)
453 panic("setting processor-frequency");
454 prop_object_release(pn);
455
456 /*
457 * Now that we have VM, malloc()s are OK in bus_space.
458 */
459 bus_space_mallocok();
460 fake_mapiodev = 0;
461 }
462
463
464 static void
465 dumpsys(void)
466 {
467
468 printf("dumpsys: TBD\n");
469 }
470
471 /*
472 * Halt or reboot the machine after syncing/dumping according to howto.
473 */
474 void
475 cpu_reboot(int howto, char *what)
476 {
477 static int syncing;
478 static char str[256];
479 char *ap = str, *ap1 = ap;
480
481 boothowto = howto;
482 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
483 syncing = 1;
484 vfs_shutdown(); /* sync */
485 resettodr(); /* set wall clock */
486 }
487
488 splhigh();
489
490 if (!cold && (howto & RB_DUMP))
491 dumpsys();
492
493 doshutdownhooks();
494
495 pmf_system_shutdown(boothowto);
496
497 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
498 /* Power off here if we know how...*/
499 }
500
501 if (howto & RB_HALT) {
502 printf("halted\n\n");
503
504 goto reboot; /* XXX for now... */
505
506 #ifdef DDB
507 printf("dropping to debugger\n");
508 while(1)
509 Debugger();
510 #endif
511 }
512
513 printf("rebooting\n\n");
514 if (what && *what) {
515 if (strlen(what) > sizeof str - 5)
516 printf("boot string too large, ignored\n");
517 else {
518 strcpy(str, what);
519 ap1 = ap = str + strlen(str);
520 *ap++ = ' ';
521 }
522 }
523 *ap++ = '-';
524 if (howto & RB_SINGLE)
525 *ap++ = 's';
526 if (howto & RB_KDB)
527 *ap++ = 'd';
528 *ap++ = 0;
529 if (ap[-2] == '-')
530 *ap1 = 0;
531
532 /* flush cache for msgbuf */
533 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
534
535 reboot:
536 ppc4xx_reset();
537
538 printf("ppc4xx_reset() failed!\n");
539 #ifdef DDB
540 while(1)
541 Debugger();
542 #else
543 while (1)
544 /* nothing */;
545 #endif
546 }
547
548 int
549 lcsplx(int ipl)
550 {
551
552 return spllower(ipl); /* XXX */
553 }
554
555 void
556 mem_regions(struct mem_region **mem, struct mem_region **avail)
557 {
558
559 *mem = physmemr;
560 *avail = availmemr;
561 }
562
563
564 int
565 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
566 {
567
568 /*
569 * Bus number is irrelevant. Configuration Mechanism 1 is in
570 * use, can have devices 0-32 (i.e. the `normal' range).
571 */
572 return 5;
573 }
574
575 int
576 pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
577 {
578 int pin = pa->pa_intrpin;
579 int dev = pa->pa_device;
580
581 if (pin == 0)
582 /* No IRQ used. */
583 goto bad;
584
585 if (pin > 4) {
586 printf("pci_intr_map: bad interrupt pin %d\n", pin);
587 goto bad;
588 }
589
590 /*
591 * We need to map the interrupt pin to the interrupt bit in the UIC
592 * associated with it. This is highly machine-dependent.
593 */
594 switch(dev) {
595 case 1:
596 case 2:
597 case 3:
598 case 4:
599 *ihp = 27 + dev;
600 break;
601 default:
602 printf("Hmm.. PCI device %d should not exist on this board\n",
603 dev);
604 goto bad;
605 }
606 return 0;
607
608 bad:
609 *ihp = -1;
610 return 1;
611 }
612
613 void
614 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
615 int swiz, int *iline)
616 {
617
618 if (bus == 0)
619 switch(dev) {
620 case 1:
621 case 2:
622 case 3:
623 case 4:
624 *iline = 31 - dev;
625 }
626 else
627 *iline = 20 + ((swiz + dev + 1) & 3);
628 }
629