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