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