machdep.c revision 1.15 1 /* $NetBSD: machdep.c,v 1.15 2011/01/17 07:32:54 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.15 2011/01/17 07:32:54 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
63 #include <uvm/uvm_extern.h>
64
65 #include <net/netisr.h>
66
67 #include <dev/cons.h>
68
69 #include <machine/bus.h>
70 #include <machine/powerpc.h>
71 #include <machine/trap.h>
72 #include <machine/pcb.h>
73
74 #include <powerpc/spr.h>
75 #include <powerpc/ibm4xx/spr.h>
76
77 #include <evbppc/virtex/dcr.h>
78 #include <evbppc/virtex/virtex.h>
79
80 #include "ksyms.h"
81
82 #if defined(DDB)
83 #include <machine/db_machdep.h>
84 #include <ddb/db_extern.h>
85 #endif
86
87 #if defined(KGDB)
88 #include <sys/kgdb.h>
89 #endif
90
91 /*
92 * Global variables used here and there
93 */
94 struct vm_map *phys_map = NULL;
95
96 /*
97 * This should probably be in autoconf! XXX
98 */
99 char cpu_model[80];
100 char machine[] = MACHINE; /* from <machine/param.h> */
101 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
102
103 char bootpath[256];
104 paddr_t msgbuf_paddr;
105 vaddr_t msgbuf_vaddr;
106
107 #if NKSYMS || defined(DDB) || defined(MODULAR)
108 void *startsym, *endsym;
109 #endif
110
111 int lcsplx(int);
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 /*
271 * trapcpy:
272 *
273 * Install a trap vector. We cannot use memcpy because the
274 * destination may be zero. Borrowed from Explora.
275 */
276 static void
277 trapcpy(int dest, void *src, size_t len)
278 {
279 uint32_t *dest_p = (void *)dest;
280 uint32_t *src_p = src;
281
282 while (len > 0) {
283 *dest_p++ = *src_p++;
284 len -= sizeof(uint32_t);
285 }
286 }
287
288 static void
289 install_extint(void (*handler)(void))
290 {
291 u_long offset = (u_long)handler - (u_long)&extint_call;
292
293 #ifdef DIAGNOSTIC
294 if (offset > 0x1ffffff)
295 panic("install_extint: too far away");
296 #endif
297
298 /* Patch branch target in EXC_EXI handler. */
299 extint_call = (extint_call & 0xfc000003) | offset;
300
301 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
302 __syncicache((void *)&extint_call, sizeof(extint_call));
303 __syncicache((void *)EXC_EXI, (int)&extsize);
304 }
305
306 /*
307 * Machine dependent startup code.
308 */
309
310 char msgbuf[MSGBUFSIZE];
311
312 void
313 cpu_startup(void)
314 {
315 /* For use by propdb. */
316 static u_int memsize = PHYSMEM * 1024 * 1024;
317 static u_int cpuspeed = CPUFREQ * 1000 * 1000;
318 prop_number_t pn;
319
320 vaddr_t minaddr, maxaddr;
321 char pbuf[9];
322
323 curcpu()->ci_khz = cpuspeed / 1000;
324
325 /* Initialize error message buffer. */
326 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
327
328 printf("%s%s", copyright, version);
329
330 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
331 printf("total memory = %s\n", pbuf);
332
333 minaddr = 0;
334 /*
335 * Allocate a submap for physio
336 */
337 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
338 VM_PHYS_SIZE, 0, false, NULL);
339
340 /*
341 * No need to allocate an mbuf cluster submap. Mbuf clusters
342 * are allocated via the pool allocator, and we use direct-mapped
343 * pool pages.
344 */
345
346 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
347 printf("avail memory = %s\n", pbuf);
348
349 /*
350 * Set up the board properties database.
351 */
352 board_info_init();
353
354 pn = prop_number_create_integer(memsize);
355 KASSERT(pn != NULL);
356 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
357 panic("setting mem-size");
358 prop_object_release(pn);
359
360 pn = prop_number_create_integer(cpuspeed);
361 KASSERT(pn != NULL);
362 if (prop_dictionary_set(board_properties, "processor-frequency",
363 pn) == false)
364 panic("setting processor-frequency");
365 prop_object_release(pn);
366
367 /*
368 * Now that we have VM, malloc()s are OK in bus_space.
369 */
370 bus_space_mallocok();
371 fake_mapiodev = 0;
372 }
373
374
375 static void
376 dumpsys(void)
377 {
378 printf("dumpsys: TBD\n");
379 }
380
381 /* Hook used by 405 pmap module. */
382 void
383 mem_regions(struct mem_region **mem, struct mem_region **avail)
384 {
385 *mem = physmemr;
386 *avail = availmemr;
387 }
388
389 /*
390 * Halt or reboot the machine after syncing/dumping according to howto.
391 */
392 void
393 cpu_reboot(int howto, char *what)
394 {
395 static int syncing;
396 static char str[256];
397 char *ap = str, *ap1 = ap;
398
399 boothowto = howto;
400 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
401 syncing = 1;
402 vfs_shutdown(); /* sync */
403 resettodr(); /* set wall clock */
404 }
405
406 splhigh();
407
408 if (!cold && (howto & RB_DUMP))
409 dumpsys();
410
411 doshutdownhooks();
412
413 pmf_system_shutdown(boothowto);
414
415 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
416 /* Power off here if we know how...*/
417 }
418
419 if (howto & RB_HALT) {
420 printf("halted\n\n");
421
422 goto reboot; /* XXX for now... */
423
424 #ifdef DDB
425 printf("dropping to debugger\n");
426 while(1)
427 Debugger();
428 #endif
429 #ifdef KGDB
430 printf("dropping to kgdb\n");
431 while(1)
432 kgdb_connect(1);
433 #endif
434 }
435
436 printf("rebooting\n\n");
437 if (what && *what) {
438 if (strlen(what) > sizeof str - 5)
439 printf("boot string too large, ignored\n");
440 else {
441 strcpy(str, what);
442 ap1 = ap = str + strlen(str);
443 *ap++ = ' ';
444 }
445 }
446 *ap++ = '-';
447 if (howto & RB_SINGLE)
448 *ap++ = 's';
449 if (howto & RB_KDB)
450 *ap++ = 'd';
451 *ap++ = 0;
452 if (ap[-2] == '-')
453 *ap1 = 0;
454
455 /* flush cache for msgbuf */
456 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
457
458 reboot:
459 ppc4xx_reset();
460
461 printf("ppc4xx_reset() failed!\n");
462 #ifdef DDB
463 while(1)
464 Debugger();
465 #endif
466 #ifdef KGDB
467 while(1)
468 kgdb_connect(1);
469 #else
470 while (1)
471 /* nothing */;
472 #endif
473 }
474
475 int
476 lcsplx(int ipl)
477 {
478 return spllower(ipl); /* XXX */
479 }
480