machdep.c revision 1.50 1 /* $NetBSD: machdep.c,v 1.50 2011/06/15 05:50:49 matt 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.50 2011/06/15 05:50:49 matt 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 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
156 void
157 initppc(u_int startkernel, u_int endkernel, char *args, void *info_block)
158 {
159 extern int defaulttrap, defaultsize;
160 extern int sctrap, scsize;
161 extern int alitrap, alisize;
162 extern int dsitrap, dsisize;
163 extern int isitrap, isisize;
164 extern int mchktrap, mchksize;
165 extern int tlbimiss4xx, tlbim4size;
166 extern int tlbdmiss4xx, tlbdm4size;
167 extern int pitfitwdog, pitfitwdogsize;
168 extern int debugtrap, debugsize;
169 extern int errata51handler, errata51size;
170 #ifdef DDB
171 extern int ddblow, ddbsize;
172 #endif
173 #ifdef IPKDB
174 extern int ipkdblow, ipkdbsize;
175 #endif
176 vaddr_t va;
177 int exc, dbcr0;
178 struct cpu_info * const ci = curcpu();
179
180 /* Disable all external interrupts */
181 mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
182
183 /* Initialize cache info for memcpy, etc. */
184 cpu_probe_cache();
185
186 /* Save info block */
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 /* Linear map kernel memory */
198 for (va = 0; va < endkernel; va += TLB_PG_SIZE)
199 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
200
201 /* Map console after physmem (see pmap_tlbmiss()) */
202 ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE),
203 TLB_PG_SIZE, TLB_I | TLB_G);
204
205 /*
206 * Initialize lwp0 and current pcb and pmap pointers.
207 */
208 lwp0.l_cpu = ci;
209
210 curpcb = lwp_getpcb(&lwp0);
211 memset(curpcb, 0, sizeof(struct pcb));
212 curpcb->pcb_pm = pmap_kernel();
213
214 /*
215 * Set up trap vectors
216 */
217 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
218 switch (exc) {
219 default:
220 memcpy((void *)exc, &defaulttrap, (size_t)&defaultsize);
221 break;
222 case EXC_EXI:
223 /*
224 * This one is (potentially) installed during autoconf
225 */
226 break;
227 case EXC_SC:
228 memcpy((void *)EXC_SC, &sctrap, (size_t)&scsize);
229 break;
230 case EXC_ALI:
231 memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
232 break;
233 case EXC_DSI:
234 memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
235 break;
236 case EXC_ISI:
237 memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
238 break;
239 case EXC_MCHK:
240 memcpy((void *)EXC_MCHK, &mchktrap, (size_t)&mchksize);
241 break;
242 case EXC_ITMISS:
243 memcpy((void *)EXC_ITMISS, &tlbimiss4xx,
244 (size_t)&tlbim4size);
245 break;
246 case EXC_DTMISS:
247 memcpy((void *)EXC_DTMISS, &tlbdmiss4xx,
248 (size_t)&tlbdm4size);
249 break;
250 /*
251 * EXC_PIT, EXC_FIT, EXC_WDOG handlers
252 * are spaced by 0x10 bytes only..
253 */
254 case EXC_PIT:
255 memcpy((void *)EXC_PIT, &pitfitwdog,
256 (size_t)&pitfitwdogsize);
257 break;
258 case EXC_DEBUG:
259 memcpy((void *)EXC_DEBUG, &debugtrap,
260 (size_t)&debugsize);
261 break;
262 case EXC_DTMISS|EXC_ALI:
263 /* PPC405GP Rev D errata item 51 */
264 memcpy((void *)(EXC_DTMISS|EXC_ALI), &errata51handler,
265 (size_t)&errata51size);
266 break;
267 #if defined(DDB) || defined(IPKDB)
268 case EXC_PGM:
269 #if defined(DDB)
270 memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
271 #elif defined(IPKDB)
272 memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
273 #endif
274 #endif /* DDB | IPKDB */
275 break;
276 }
277
278 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
279 mtspr(SPR_EVPR, 0); /* Set Exception vector base */
280
281 consinit();
282
283 /* Handle trap instruction as PGM exception */
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 * external interrupt handler install
289 */
290 install_extint(ext_intr);
291
292 /*
293 * Now enable translation (and machine checks/recoverable interrupts).
294 */
295 __asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
296 : : "r"(0), "K"(PSL_IR|PSL_DR));
297 /* XXXX PSL_ME - With ME set kernel gets stuck... */
298
299 uvm_setpagesize();
300
301 /*
302 * Initialize pmap module.
303 */
304 pmap_bootstrap(startkernel, endkernel);
305
306 #ifdef DEBUG
307 printf("Board config data:\n");
308 printf(" usr_config_ver = %s\n", board_data.usr_config_ver);
309 printf(" rom_sw_ver = %s\n", board_data.rom_sw_ver);
310 printf(" mem_size = %u\n", board_data.mem_size);
311 printf(" mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
312 board_data.mac_address_local[0], board_data.mac_address_local[1],
313 board_data.mac_address_local[2], board_data.mac_address_local[3],
314 board_data.mac_address_local[4], board_data.mac_address_local[5]);
315 printf(" mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
316 board_data.mac_address_pci[0], board_data.mac_address_pci[1],
317 board_data.mac_address_pci[2], board_data.mac_address_pci[3],
318 board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
319 printf(" processor_speed = %u\n", board_data.processor_speed);
320 printf(" plb_speed = %u\n", board_data.plb_speed);
321 printf(" pci_speed = %u\n", board_data.pci_speed);
322 #endif
323
324 #if NKSYMS || defined(DDB) || defined(MODULAR)
325 ksyms_addsyms_elf((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
326 #endif
327 #ifdef DDB
328 if (boothowto & RB_KDB)
329 Debugger();
330 #endif
331 #ifdef IPKDB
332 /*
333 * Now trap to IPKDB
334 */
335 ipkdb_init();
336 if (boothowto & RB_KDB)
337 ipkdb_connect(0);
338 #endif
339
340 /*
341 * Look for the ibm4xx modules in the right place.
342 */
343 module_machine = module_machine_ibm4xx;
344 }
345
346 static void
347 install_extint(void (*handler)(void))
348 {
349 extern int extint, extsize;
350 extern u_long extint_call;
351 u_long offset = (u_long)handler - (u_long)&extint_call;
352 int msr;
353
354 #ifdef DIAGNOSTIC
355 if (offset > 0x1ffffff)
356 panic("install_extint: too far away");
357 #endif
358 __asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
359 extint_call = (extint_call & 0xfc000003) | offset;
360 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
361 __syncicache((void *)&extint_call, sizeof extint_call);
362 __syncicache((void *)EXC_EXI, (int)&extsize);
363 __asm volatile ("mtmsr %0" :: "r"(msr));
364 }
365
366 /*
367 * Machine dependent startup code.
368 */
369
370 char msgbuf[MSGBUFSIZE];
371
372 void
373 cpu_startup(void)
374 {
375 vaddr_t minaddr, maxaddr;
376 prop_number_t pn;
377 prop_data_t pd;
378 char pbuf[9];
379
380 /*
381 * Initialize error message buffer (at end of core).
382 */
383 #if 0 /* For some reason this fails... --Artem
384 * Besides, do we really have to put it at the end of core?
385 * Let's use static buffer for now
386 */
387 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
388 UVM_KMF_VAONLY)))
389 panic("startup: no room for message buffer");
390 for (i = 0; i < btoc(MSGBUFSIZE); i++)
391 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
392 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
393 initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
394 #else
395 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
396 #endif
397
398 printf("%s%s", copyright, version);
399 printf("Walnut PowerPC 405GP Evaluation Board\n");
400
401 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
402 printf("total memory = %s\n", pbuf);
403
404 minaddr = 0;
405 /*
406 * Allocate a submap for physio
407 */
408 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
409 VM_PHYS_SIZE, 0, false, NULL);
410
411 /*
412 * No need to allocate an mbuf cluster submap. Mbuf clusters
413 * are allocated via the pool allocator, and we use direct-mapped
414 * pool pages.
415 */
416
417 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
418 printf("avail memory = %s\n", pbuf);
419
420 /*
421 * Set up the board properties dictionary.
422 */
423 board_properties = prop_dictionary_create();
424 KASSERT(board_properties != NULL);
425
426 pn = prop_number_create_integer(board_data.mem_size);
427 KASSERT(pn != NULL);
428 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
429 panic("setting mem-size");
430 prop_object_release(pn);
431
432 pd = prop_data_create_data_nocopy(board_data.mac_address_local,
433 sizeof(board_data.mac_address_local));
434 KASSERT(pd != NULL);
435 if (prop_dictionary_set(board_properties, "emac0-mac-addr",
436 pd) == false)
437 panic("setting emac0-mac-addr");
438 prop_object_release(pd);
439
440 pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
441 sizeof(board_data.mac_address_pci));
442 KASSERT(pd != NULL);
443 if (prop_dictionary_set(board_properties, "sip0-mac-addr",
444 pd) == false)
445 panic("setting sip0-mac-addr");
446 prop_object_release(pd);
447
448 pn = prop_number_create_integer(board_data.processor_speed);
449 KASSERT(pn != NULL);
450 if (prop_dictionary_set(board_properties, "processor-frequency",
451 pn) == false)
452 panic("setting processor-frequency");
453 prop_object_release(pn);
454
455 /*
456 * Now that we have VM, malloc()s are OK in bus_space.
457 */
458 bus_space_mallocok();
459 fake_mapiodev = 0;
460 }
461
462
463 static void
464 dumpsys(void)
465 {
466
467 printf("dumpsys: TBD\n");
468 }
469
470 /*
471 * Halt or reboot the machine after syncing/dumping according to howto.
472 */
473 void
474 cpu_reboot(int howto, char *what)
475 {
476 static int syncing;
477 static char str[256];
478 char *ap = str, *ap1 = ap;
479
480 boothowto = howto;
481 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
482 syncing = 1;
483 vfs_shutdown(); /* sync */
484 resettodr(); /* set wall clock */
485 }
486
487 splhigh();
488
489 if (!cold && (howto & RB_DUMP))
490 dumpsys();
491
492 doshutdownhooks();
493
494 pmf_system_shutdown(boothowto);
495
496 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
497 /* Power off here if we know how...*/
498 }
499
500 if (howto & RB_HALT) {
501 printf("halted\n\n");
502
503 goto reboot; /* XXX for now... */
504
505 #ifdef DDB
506 printf("dropping to debugger\n");
507 while(1)
508 Debugger();
509 #endif
510 }
511
512 printf("rebooting\n\n");
513 if (what && *what) {
514 if (strlen(what) > sizeof str - 5)
515 printf("boot string too large, ignored\n");
516 else {
517 strcpy(str, what);
518 ap1 = ap = str + strlen(str);
519 *ap++ = ' ';
520 }
521 }
522 *ap++ = '-';
523 if (howto & RB_SINGLE)
524 *ap++ = 's';
525 if (howto & RB_KDB)
526 *ap++ = 'd';
527 *ap++ = 0;
528 if (ap[-2] == '-')
529 *ap1 = 0;
530
531 /* flush cache for msgbuf */
532 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
533
534 reboot:
535 ppc4xx_reset();
536
537 printf("ppc4xx_reset() failed!\n");
538 #ifdef DDB
539 while(1)
540 Debugger();
541 #else
542 while (1)
543 /* nothing */;
544 #endif
545 }
546
547 void
548 mem_regions(struct mem_region **mem, struct mem_region **avail)
549 {
550
551 *mem = physmemr;
552 *avail = availmemr;
553 }
554
555
556 int
557 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
558 {
559
560 /*
561 * Bus number is irrelevant. Configuration Mechanism 1 is in
562 * use, can have devices 0-32 (i.e. the `normal' range).
563 */
564 return 5;
565 }
566
567 int
568 pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
569 {
570 int pin = pa->pa_intrpin;
571 int dev = pa->pa_device;
572
573 if (pin == 0)
574 /* No IRQ used. */
575 goto bad;
576
577 if (pin > 4) {
578 printf("pci_intr_map: bad interrupt pin %d\n", pin);
579 goto bad;
580 }
581
582 /*
583 * We need to map the interrupt pin to the interrupt bit in the UIC
584 * associated with it. This is highly machine-dependent.
585 */
586 switch(dev) {
587 case 1:
588 case 2:
589 case 3:
590 case 4:
591 *ihp = 27 + dev;
592 break;
593 default:
594 printf("Hmm.. PCI device %d should not exist on this board\n",
595 dev);
596 goto bad;
597 }
598 return 0;
599
600 bad:
601 *ihp = -1;
602 return 1;
603 }
604
605 void
606 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
607 int swiz, int *iline)
608 {
609
610 if (bus == 0)
611 switch(dev) {
612 case 1:
613 case 2:
614 case 3:
615 case 4:
616 *iline = 31 - dev;
617 }
618 else
619 *iline = 20 + ((swiz + dev + 1) & 3);
620 }
621