machdep.c revision 1.30 1 /* $NetBSD: machdep.c,v 1.30 2006/10/16 18:14:37 kiyohara 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.30 2006/10/16 18:14:37 kiyohara 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/sa.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
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 #include <machine/bus.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 *exec_map = NULL;
125 struct vm_map *mb_map = NULL;
126 struct vm_map *phys_map = NULL;
127
128 /*
129 * This should probably be in autoconf! XXX
130 */
131 char cpu_model[80];
132 char machine[] = MACHINE; /* from <machine/param.h> */
133 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
134
135 extern struct user *proc0paddr;
136
137 char bootpath[256];
138 paddr_t msgbuf_paddr;
139 vaddr_t msgbuf_vaddr;
140
141 #if NKSYMS || defined(DDB) || defined(LKM)
142 void *startsym, *endsym;
143 #endif
144
145 int lcsplx(int);
146 void initppc(u_int, u_int, char *, void *);
147
148 static void dumpsys(void);
149 static void install_extint(void (*)(void));
150
151 #define MEMREGIONS 8
152 struct mem_region physmemr[MEMREGIONS]; /* Hard code memory */
153 struct mem_region availmemr[MEMREGIONS]; /* Who's supposed to set these up? */
154
155 struct board_cfg_data board_data;
156
157 void
158 initppc(u_int startkernel, u_int endkernel, char *args, void *info_block)
159 {
160 extern int defaulttrap, defaultsize;
161 extern int sctrap, scsize;
162 extern int alitrap, alisize;
163 extern int dsitrap, dsisize;
164 extern int isitrap, isisize;
165 extern int mchktrap, mchksize;
166 extern int tlbimiss4xx, tlbim4size;
167 extern int tlbdmiss4xx, tlbdm4size;
168 extern int pitfitwdog, pitfitwdogsize;
169 extern int debugtrap, debugsize;
170 extern int errata51handler, errata51size;
171 #ifdef DDB
172 extern int ddblow, ddbsize;
173 #endif
174 #ifdef IPKDB
175 extern int ipkdblow, ipkdbsize;
176 #endif
177 vaddr_t va;
178 int exc, dbcr0;
179 struct cpu_info * const ci = curcpu();
180
181 /* Disable all external interrupts */
182 mtdcr(DCR_UIC0_ER, 0);
183
184 /* Initialize cache info for memcpy, etc. */
185 cpu_probe_cache();
186
187 /* Save info block */
188 memcpy(&board_data, info_block, sizeof(board_data));
189
190 memset(physmemr, 0, sizeof physmemr);
191 memset(availmemr, 0, sizeof availmemr);
192 physmemr[0].start = 0;
193 physmemr[0].size = board_data.mem_size & ~PGOFSET;
194 /* Lower memory reserved by eval board BIOS */
195 availmemr[0].start = startkernel;
196 availmemr[0].size = board_data.mem_size - availmemr[0].start;
197
198 /* Linear map whole physmem */
199 for (va = 0; va < board_data.mem_size; va += TLB_PG_SIZE)
200 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
201
202 /* Map console just after RAM */
203 ppc4xx_tlb_reserve(0xef000000, va, 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(LKM)
326 ksyms_init((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);
389 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
390 #else
391 initmsgbuf((caddr_t)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 exec arguments. This map effectively
403 * limits the number of processes exec'ing at any time.
404 */
405 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
406 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
407
408 /*
409 * Allocate a submap for physio
410 */
411 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
412 VM_PHYS_SIZE, 0, FALSE, NULL);
413
414 /*
415 * No need to allocate an mbuf cluster submap. Mbuf clusters
416 * are allocated via the pool allocator, and we use direct-mapped
417 * pool pages.
418 */
419
420 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
421 printf("avail memory = %s\n", pbuf);
422
423 /*
424 * Set up the board properties dictionary.
425 */
426 board_properties = prop_dictionary_create();
427 KASSERT(board_properties != NULL);
428
429 pn = prop_number_create_integer(board_data.mem_size);
430 KASSERT(pn != NULL);
431 if (prop_dictionary_set(board_properties, "mem-size", pn) == FALSE)
432 panic("setting mem-size");
433 prop_object_release(pn);
434
435 pd = prop_data_create_data_nocopy(board_data.mac_address_local,
436 sizeof(board_data.mac_address_local));
437 KASSERT(pd != NULL);
438 if (prop_dictionary_set(board_properties, "emac0-mac-addr",
439 pd) == FALSE)
440 panic("setting emac0-mac-addr");
441 prop_object_release(pd);
442
443 pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
444 sizeof(board_data.mac_address_pci));
445 KASSERT(pd != NULL);
446 if (prop_dictionary_set(board_properties, "sip0-mac-addr",
447 pd) == FALSE)
448 panic("setting sip0-mac-addr");
449 prop_object_release(pd);
450
451 pn = prop_number_create_integer(board_data.processor_speed);
452 KASSERT(pn != NULL);
453 if (prop_dictionary_set(board_properties, "processor-frequency",
454 pn) == FALSE)
455 panic("setting processor-frequency");
456 prop_object_release(pn);
457
458 /*
459 * Now that we have VM, malloc()s are OK in bus_space.
460 */
461 bus_space_mallocok();
462 fake_mapiodev = 0;
463 }
464
465
466 static void
467 dumpsys(void)
468 {
469
470 printf("dumpsys: TBD\n");
471 }
472
473 /*
474 * Halt or reboot the machine after syncing/dumping according to howto.
475 */
476 void
477 cpu_reboot(int howto, char *what)
478 {
479 static int syncing;
480 static char str[256];
481 char *ap = str, *ap1 = ap;
482
483 boothowto = howto;
484 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
485 syncing = 1;
486 vfs_shutdown(); /* sync */
487 resettodr(); /* set wall clock */
488 }
489
490 splhigh();
491
492 if (!cold && (howto & RB_DUMP))
493 dumpsys();
494
495 doshutdownhooks();
496
497 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
498 /* Power off here if we know how...*/
499 }
500
501 if (howto & RB_HALT) {
502 printf("halted\n\n");
503
504 goto reboot; /* XXX for now... */
505
506 #ifdef DDB
507 printf("dropping to debugger\n");
508 while(1)
509 Debugger();
510 #endif
511 }
512
513 printf("rebooting\n\n");
514 if (what && *what) {
515 if (strlen(what) > sizeof str - 5)
516 printf("boot string too large, ignored\n");
517 else {
518 strcpy(str, what);
519 ap1 = ap = str + strlen(str);
520 *ap++ = ' ';
521 }
522 }
523 *ap++ = '-';
524 if (howto & RB_SINGLE)
525 *ap++ = 's';
526 if (howto & RB_KDB)
527 *ap++ = 'd';
528 *ap++ = 0;
529 if (ap[-2] == '-')
530 *ap1 = 0;
531
532 /* flush cache for msgbuf */
533 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
534
535 reboot:
536 ppc4xx_reset();
537
538 printf("ppc4xx_reset() failed!\n");
539 #ifdef DDB
540 while(1)
541 Debugger();
542 #else
543 while (1)
544 /* nothing */;
545 #endif
546 }
547
548 int
549 lcsplx(int ipl)
550 {
551
552 return spllower(ipl); /* XXX */
553 }
554
555 void
556 mem_regions(struct mem_region **mem, struct mem_region **avail)
557 {
558
559 *mem = physmemr;
560 *avail = availmemr;
561 }
562