machdep.c revision 1.17 1 /* $NetBSD: machdep.c,v 1.17 2011/06/15 05:50:49 matt Exp $ */
2
3 /*
4 * Copyright (c) 2006 Jachym Holecek
5 * All rights reserved.
6 *
7 * Written for DFC Design, s.r.o.
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 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Based on Walnut and Explora.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.17 2011/06/15 05:50:49 matt Exp $");
38
39 #include "opt_compat_netbsd.h"
40 #include "opt_ddb.h"
41 #include "opt_ipkdb.h"
42 #include "opt_modular.h"
43 #include "opt_virtex.h"
44 #include "opt_kgdb.h"
45
46 #include <sys/param.h>
47 #include <sys/buf.h>
48 #include <sys/exec.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mount.h>
52 #include <sys/msgbuf.h>
53 #include <sys/proc.h>
54 #include <sys/reboot.h>
55 #include <sys/syscallargs.h>
56 #include <sys/syslog.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/boot_flag.h>
60 #include <sys/ksyms.h>
61 #include <sys/device.h>
62 #include <sys/module.h>
63
64 #include <uvm/uvm_extern.h>
65
66 #include <net/netisr.h>
67
68 #include <dev/cons.h>
69
70 #include <machine/bus.h>
71 #include <machine/powerpc.h>
72 #include <machine/trap.h>
73 #include <machine/pcb.h>
74
75 #include <powerpc/spr.h>
76 #include <powerpc/ibm4xx/spr.h>
77
78 #include <evbppc/virtex/dcr.h>
79 #include <evbppc/virtex/virtex.h>
80
81 #include "ksyms.h"
82
83 #if defined(DDB)
84 #include <machine/db_machdep.h>
85 #include <ddb/db_extern.h>
86 #endif
87
88 #if defined(KGDB)
89 #include <sys/kgdb.h>
90 #endif
91
92 /*
93 * Global variables used here and there
94 */
95 struct vm_map *phys_map = NULL;
96
97 /*
98 * This should probably be in autoconf! XXX
99 */
100 char cpu_model[80];
101 char machine[] = MACHINE; /* from <machine/param.h> */
102 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
103
104 char bootpath[256];
105 paddr_t msgbuf_paddr;
106 vaddr_t msgbuf_vaddr;
107
108 #if NKSYMS || defined(DDB) || defined(MODULAR)
109 void *startsym, *endsym;
110 #endif
111
112 void initppc(u_int, u_int);
113
114 static void dumpsys(void);
115 static void install_extint(void (*)(void));
116 static void trapcpy(int, void *, size_t);
117
118
119 /* These two live in powerpc/powerpc/trap_subr.S */
120 extern int extint, extsize;
121 extern u_long extint_call;
122
123 extern int defaulttrap, defaultsize;
124 extern int sctrap, scsize;
125 extern int alitrap, alisize;
126 extern int dsitrap, dsisize;
127 extern int isitrap, isisize;
128 extern int mchktrap, mchksize;
129 extern int tlbimiss4xx, tlbim4size;
130 extern int tlbdmiss4xx, tlbdm4size;
131 extern int pitfitwdog, pitfitwdogsize;
132 extern int debugtrap, debugsize;
133 extern int errata51handler, errata51size;
134 #ifdef DDB
135 extern int ddblow, ddbsize;
136 #endif
137 #ifdef IPKDB
138 extern int ipkdblow, ipkdbsize;
139 #endif
140
141 /* BSS segment start & end. */
142 extern char edata[], end[];
143
144 /* One region holds all memory, the other is terminator expected by 405 pmap. */
145 #define MEMREGIONS 2
146 struct mem_region physmemr[MEMREGIONS];
147 struct mem_region availmemr[MEMREGIONS];
148
149 static struct {
150 int vector;
151 void *addr;
152 void *size;
153 } traps[] = {
154 /* EXC_EXI handled by install_extint(). */
155 { EXC_SC, &sctrap, &scsize },
156 { EXC_ALI, &alitrap, &alisize },
157 { EXC_DSI, &dsitrap, &dsisize },
158 { EXC_ISI, &isitrap, &isisize },
159 { EXC_MCHK, &mchktrap, &mchksize },
160 { EXC_ITMISS, &tlbimiss4xx, &tlbim4size },
161 { EXC_DTMISS, &tlbdmiss4xx, &tlbdm4size },
162
163 /* EXC_{PIT, FIT, WDOG} handlers are spaced by 0x10 bytes only.. */
164 { EXC_PIT, &pitfitwdog, &pitfitwdogsize },
165 { EXC_DEBUG, &debugtrap, &debugsize },
166
167 /* PPC405GP Rev D errata item 51 */
168 { EXC_DTMISS|EXC_ALI, &errata51handler, &errata51size },
169
170 #if defined(DDB)
171 { EXC_PGM, &ddblow, &ddbsize },
172 #elif defined(IPKDB)
173 { EXC_PGM, &ipkdblow, &ipkdbsize },
174 #endif
175 };
176
177 /* Maximum TLB page size. */
178 #define TLB_PG_SIZE (16*1024*1024)
179
180 void
181 initppc(u_int startkernel, u_int endkernel)
182 {
183 struct cpu_info * const ci = curcpu();
184 u_int addr, memsize;
185 int exc, dbcr0, i;
186
187 /* Initialize cache info for memcpy, memset, etc. */
188 cpu_probe_cache();
189
190 memset(physmemr, 0, sizeof(physmemr)); /* [1].size = 0 */
191 memset(availmemr, 0, sizeof(availmemr)); /* [1].size = 0 */
192
193 memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET;
194
195 physmemr[0].start = 0;
196 physmemr[0].size = memsize;
197
198 availmemr[0].start = startkernel;
199 availmemr[0].size = memsize - availmemr[0].start;
200
201 /* Map kernel memory linearly. */
202 for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE)
203 ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX);
204
205 /* Give design-specific code a hint for reserved mappings. */
206 virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
207 physmemr, availmemr);
208
209 lwp0.l_cpu = ci;
210
211 curpcb = lwp_getpcb(&lwp0);
212 memset(curpcb, 0, sizeof(struct pcb));
213 curpcb->pcb_pm = pmap_kernel();
214
215 for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
216 trapcpy(exc, &defaulttrap, (size_t)&defaultsize);
217
218 for (i = 0; i < __arraycount(traps); i++)
219 trapcpy(traps[i].vector, traps[i].addr, (size_t)traps[i].size);
220
221 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
222
223 /* set exception vector base */
224 mtspr(SPR_EVPR, 0);
225
226 /* handle trap instruction as PGM exception */
227 dbcr0 = mfspr(SPR_DBCR0) & ~DBCR0_TDE;
228 mtspr(SPR_DBCR0, dbcr0);
229
230 install_extint(ext_intr);
231
232 /* enable translation */
233 __asm volatile (
234 " mfmsr %0 \n"
235 " ori %0,%0,%1 \n"
236 " mtmsr %0 \n"
237 " sync \n"
238 " isync \n"
239 : : "r"(0), "K"(PSL_IR | PSL_DR | PSL_ME));
240
241 /* now that we enabled MMU, we can map console */
242 consinit();
243
244 uvm_setpagesize();
245 pmap_bootstrap(startkernel, endkernel);
246
247 #if NKSYMS || defined(DDB) || defined(MODULAR)
248 ksyms_addsyms_elf((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
249 #endif
250 #ifdef DDB
251 if (boothowto & RB_KDB)
252 Debugger();
253 #endif
254 #ifdef IPKDB
255 /*
256 * Now trap to IPKDB
257 */
258 ipkdb_init();
259 if (boothowto & RB_KDB)
260 ipkdb_connect(0);
261 #endif
262 #ifdef KGDB
263 /*
264 * Now trap to KGDB
265 */
266 kgdb_connect(1);
267 #endif /* KGDB */
268
269 /*
270 * Look for the ibm4xx modules in the right place.
271 */
272 module_machine = module_machine_ibm4xx;
273 }
274
275 /*
276 * trapcpy:
277 *
278 * Install a trap vector. We cannot use memcpy because the
279 * destination may be zero. Borrowed from Explora.
280 */
281 static void
282 trapcpy(int dest, void *src, size_t len)
283 {
284 uint32_t *dest_p = (void *)dest;
285 uint32_t *src_p = src;
286
287 while (len > 0) {
288 *dest_p++ = *src_p++;
289 len -= sizeof(uint32_t);
290 }
291 }
292
293 static void
294 install_extint(void (*handler)(void))
295 {
296 u_long offset = (u_long)handler - (u_long)&extint_call;
297
298 #ifdef DIAGNOSTIC
299 if (offset > 0x1ffffff)
300 panic("install_extint: too far away");
301 #endif
302
303 /* Patch branch target in EXC_EXI handler. */
304 extint_call = (extint_call & 0xfc000003) | offset;
305
306 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
307 __syncicache((void *)&extint_call, sizeof(extint_call));
308 __syncicache((void *)EXC_EXI, (int)&extsize);
309 }
310
311 /*
312 * Machine dependent startup code.
313 */
314
315 char msgbuf[MSGBUFSIZE];
316
317 void
318 cpu_startup(void)
319 {
320 /* For use by propdb. */
321 static u_int memsize = PHYSMEM * 1024 * 1024;
322 static u_int cpuspeed = CPUFREQ * 1000 * 1000;
323 prop_number_t pn;
324
325 vaddr_t minaddr, maxaddr;
326 char pbuf[9];
327
328 curcpu()->ci_khz = cpuspeed / 1000;
329
330 /* Initialize error message buffer. */
331 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
332
333 printf("%s%s", copyright, version);
334
335 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
336 printf("total memory = %s\n", pbuf);
337
338 minaddr = 0;
339 /*
340 * Allocate a submap for physio
341 */
342 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
343 VM_PHYS_SIZE, 0, false, NULL);
344
345 /*
346 * No need to allocate an mbuf cluster submap. Mbuf clusters
347 * are allocated via the pool allocator, and we use direct-mapped
348 * pool pages.
349 */
350
351 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
352 printf("avail memory = %s\n", pbuf);
353
354 /*
355 * Set up the board properties database.
356 */
357 board_info_init();
358
359 pn = prop_number_create_integer(memsize);
360 KASSERT(pn != NULL);
361 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
362 panic("setting mem-size");
363 prop_object_release(pn);
364
365 pn = prop_number_create_integer(cpuspeed);
366 KASSERT(pn != NULL);
367 if (prop_dictionary_set(board_properties, "processor-frequency",
368 pn) == false)
369 panic("setting processor-frequency");
370 prop_object_release(pn);
371
372 /*
373 * Now that we have VM, malloc()s are OK in bus_space.
374 */
375 bus_space_mallocok();
376 fake_mapiodev = 0;
377 }
378
379
380 static void
381 dumpsys(void)
382 {
383 printf("dumpsys: TBD\n");
384 }
385
386 /* Hook used by 405 pmap module. */
387 void
388 mem_regions(struct mem_region **mem, struct mem_region **avail)
389 {
390 *mem = physmemr;
391 *avail = availmemr;
392 }
393
394 /*
395 * Halt or reboot the machine after syncing/dumping according to howto.
396 */
397 void
398 cpu_reboot(int howto, char *what)
399 {
400 static int syncing;
401 static char str[256];
402 char *ap = str, *ap1 = ap;
403
404 boothowto = howto;
405 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
406 syncing = 1;
407 vfs_shutdown(); /* sync */
408 resettodr(); /* set wall clock */
409 }
410
411 splhigh();
412
413 if (!cold && (howto & RB_DUMP))
414 dumpsys();
415
416 doshutdownhooks();
417
418 pmf_system_shutdown(boothowto);
419
420 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
421 /* Power off here if we know how...*/
422 }
423
424 if (howto & RB_HALT) {
425 printf("halted\n\n");
426
427 goto reboot; /* XXX for now... */
428
429 #ifdef DDB
430 printf("dropping to debugger\n");
431 while(1)
432 Debugger();
433 #endif
434 #ifdef KGDB
435 printf("dropping to kgdb\n");
436 while(1)
437 kgdb_connect(1);
438 #endif
439 }
440
441 printf("rebooting\n\n");
442 if (what && *what) {
443 if (strlen(what) > sizeof str - 5)
444 printf("boot string too large, ignored\n");
445 else {
446 strcpy(str, what);
447 ap1 = ap = str + strlen(str);
448 *ap++ = ' ';
449 }
450 }
451 *ap++ = '-';
452 if (howto & RB_SINGLE)
453 *ap++ = 's';
454 if (howto & RB_KDB)
455 *ap++ = 'd';
456 *ap++ = 0;
457 if (ap[-2] == '-')
458 *ap1 = 0;
459
460 /* flush cache for msgbuf */
461 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
462
463 reboot:
464 ppc4xx_reset();
465
466 printf("ppc4xx_reset() failed!\n");
467 #ifdef DDB
468 while(1)
469 Debugger();
470 #endif
471 #ifdef KGDB
472 while(1)
473 kgdb_connect(1);
474 #else
475 while (1)
476 /* nothing */;
477 #endif
478 }
479