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