machdep.c revision 1.41 1 /* $NetBSD: machdep.c,v 1.41 2009/11/07 07:27:43 cegger 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.41 2009/11/07 07:27:43 cegger 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 extern struct user *proc0paddr;
135
136 char bootpath[256];
137 paddr_t msgbuf_paddr;
138 vaddr_t msgbuf_vaddr;
139
140 #if NKSYMS || defined(DDB) || defined(MODULAR)
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
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_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 lwp0.l_addr = proc0paddr;
210 memset(lwp0.l_addr, 0, sizeof *lwp0.l_addr);
211
212 curpcb = &proc0paddr->u_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 static void
343 install_extint(void (*handler)(void))
344 {
345 extern int extint, extsize;
346 extern u_long extint_call;
347 u_long offset = (u_long)handler - (u_long)&extint_call;
348 int msr;
349
350 #ifdef DIAGNOSTIC
351 if (offset > 0x1ffffff)
352 panic("install_extint: too far away");
353 #endif
354 __asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
355 extint_call = (extint_call & 0xfc000003) | offset;
356 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
357 __syncicache((void *)&extint_call, sizeof extint_call);
358 __syncicache((void *)EXC_EXI, (int)&extsize);
359 __asm volatile ("mtmsr %0" :: "r"(msr));
360 }
361
362 /*
363 * Machine dependent startup code.
364 */
365
366 char msgbuf[MSGBUFSIZE];
367
368 void
369 cpu_startup(void)
370 {
371 vaddr_t minaddr, maxaddr;
372 prop_number_t pn;
373 prop_data_t pd;
374 char pbuf[9];
375
376 /*
377 * Initialize error message buffer (at end of core).
378 */
379 #if 0 /* For some reason this fails... --Artem
380 * Besides, do we really have to put it at the end of core?
381 * Let's use static buffer for now
382 */
383 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
384 UVM_KMF_VAONLY)))
385 panic("startup: no room for message buffer");
386 for (i = 0; i < btoc(MSGBUFSIZE); i++)
387 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
388 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
389 initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
390 #else
391 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
392 #endif
393
394 printf("%s%s", copyright, version);
395 printf("Walnut PowerPC 405GP Evaluation Board\n");
396
397 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
398 printf("total memory = %s\n", pbuf);
399
400 minaddr = 0;
401 /*
402 * Allocate a submap for physio
403 */
404 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
405 VM_PHYS_SIZE, 0, false, NULL);
406
407 /*
408 * No need to allocate an mbuf cluster submap. Mbuf clusters
409 * are allocated via the pool allocator, and we use direct-mapped
410 * pool pages.
411 */
412
413 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
414 printf("avail memory = %s\n", pbuf);
415
416 /*
417 * Set up the board properties dictionary.
418 */
419 board_properties = prop_dictionary_create();
420 KASSERT(board_properties != NULL);
421
422 pn = prop_number_create_integer(board_data.mem_size);
423 KASSERT(pn != NULL);
424 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
425 panic("setting mem-size");
426 prop_object_release(pn);
427
428 pd = prop_data_create_data_nocopy(board_data.mac_address_local,
429 sizeof(board_data.mac_address_local));
430 KASSERT(pd != NULL);
431 if (prop_dictionary_set(board_properties, "emac0-mac-addr",
432 pd) == false)
433 panic("setting emac0-mac-addr");
434 prop_object_release(pd);
435
436 pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
437 sizeof(board_data.mac_address_pci));
438 KASSERT(pd != NULL);
439 if (prop_dictionary_set(board_properties, "sip0-mac-addr",
440 pd) == false)
441 panic("setting sip0-mac-addr");
442 prop_object_release(pd);
443
444 pn = prop_number_create_integer(board_data.processor_speed);
445 KASSERT(pn != NULL);
446 if (prop_dictionary_set(board_properties, "processor-frequency",
447 pn) == false)
448 panic("setting processor-frequency");
449 prop_object_release(pn);
450
451 /*
452 * Now that we have VM, malloc()s are OK in bus_space.
453 */
454 bus_space_mallocok();
455 fake_mapiodev = 0;
456 }
457
458
459 static void
460 dumpsys(void)
461 {
462
463 printf("dumpsys: TBD\n");
464 }
465
466 /*
467 * Halt or reboot the machine after syncing/dumping according to howto.
468 */
469 void
470 cpu_reboot(int howto, char *what)
471 {
472 static int syncing;
473 static char str[256];
474 char *ap = str, *ap1 = ap;
475
476 boothowto = howto;
477 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
478 syncing = 1;
479 vfs_shutdown(); /* sync */
480 resettodr(); /* set wall clock */
481 }
482
483 splhigh();
484
485 if (!cold && (howto & RB_DUMP))
486 dumpsys();
487
488 doshutdownhooks();
489
490 pmf_system_shutdown(boothowto);
491
492 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
493 /* Power off here if we know how...*/
494 }
495
496 if (howto & RB_HALT) {
497 printf("halted\n\n");
498
499 goto reboot; /* XXX for now... */
500
501 #ifdef DDB
502 printf("dropping to debugger\n");
503 while(1)
504 Debugger();
505 #endif
506 }
507
508 printf("rebooting\n\n");
509 if (what && *what) {
510 if (strlen(what) > sizeof str - 5)
511 printf("boot string too large, ignored\n");
512 else {
513 strcpy(str, what);
514 ap1 = ap = str + strlen(str);
515 *ap++ = ' ';
516 }
517 }
518 *ap++ = '-';
519 if (howto & RB_SINGLE)
520 *ap++ = 's';
521 if (howto & RB_KDB)
522 *ap++ = 'd';
523 *ap++ = 0;
524 if (ap[-2] == '-')
525 *ap1 = 0;
526
527 /* flush cache for msgbuf */
528 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
529
530 reboot:
531 ppc4xx_reset();
532
533 printf("ppc4xx_reset() failed!\n");
534 #ifdef DDB
535 while(1)
536 Debugger();
537 #else
538 while (1)
539 /* nothing */;
540 #endif
541 }
542
543 int
544 lcsplx(int ipl)
545 {
546
547 return spllower(ipl); /* XXX */
548 }
549
550 void
551 mem_regions(struct mem_region **mem, struct mem_region **avail)
552 {
553
554 *mem = physmemr;
555 *avail = availmemr;
556 }
557