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