oea_machdep.c revision 1.65 1 /* $NetBSD: oea_machdep.c,v 1.65 2013/07/04 22:59:27 joerg Exp $ */
2
3 /*
4 * Copyright (C) 2002 Matt Thomas
5 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6 * Copyright (C) 1995, 1996 TooLs GmbH.
7 * All rights reserved.
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 by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: oea_machdep.c,v 1.65 2013/07/04 22:59:27 joerg Exp $");
37
38 #include "opt_ppcarch.h"
39 #include "opt_compat_netbsd.h"
40 #include "opt_ddb.h"
41 #include "opt_kgdb.h"
42 #include "opt_ipkdb.h"
43 #include "opt_multiprocessor.h"
44 #include "opt_altivec.h"
45
46 #include <sys/param.h>
47 #include <sys/buf.h>
48 #include <sys/boot_flag.h>
49 #include <sys/exec.h>
50 #include <sys/kernel.h>
51 #include <sys/mbuf.h>
52 #include <sys/mount.h>
53 #include <sys/msgbuf.h>
54 #include <sys/proc.h>
55 #include <sys/reboot.h>
56 #include <sys/syscallargs.h>
57 #include <sys/syslog.h>
58 #include <sys/systm.h>
59
60 #include <uvm/uvm_extern.h>
61
62 #ifdef DDB
63 #include <powerpc/db_machdep.h>
64 #include <ddb/db_extern.h>
65 #endif
66
67 #ifdef KGDB
68 #include <sys/kgdb.h>
69 #endif
70
71 #ifdef IPKDB
72 #include <ipkdb/ipkdb.h>
73 #endif
74
75 #include <machine/powerpc.h>
76
77 #include <powerpc/trap.h>
78 #include <powerpc/spr.h>
79 #include <powerpc/pte.h>
80 #include <powerpc/altivec.h>
81 #include <powerpc/pcb.h>
82
83 #include <powerpc/oea/bat.h>
84 #include <powerpc/oea/cpufeat.h>
85 #include <powerpc/oea/spr.h>
86 #include <powerpc/oea/sr_601.h>
87
88 char machine[] = MACHINE; /* from <machine/param.h> */
89 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
90
91 struct vm_map *phys_map = NULL;
92
93 /*
94 * Global variables used here and there
95 */
96 static void trap0(void *);
97
98 /* XXXSL: The battable is not initialized to non-zero for PPC_OEA64 and PPC_OEA64_BRIDGE */
99 struct bat battable[BAT_VA2IDX(0xffffffff)+1];
100
101 register_t iosrtable[16]; /* I/O segments, for kernel_pmap setup */
102 #ifndef MSGBUFADDR
103 paddr_t msgbuf_paddr;
104 #endif
105
106 extern int dsitrap_fix_dbat4[];
107 extern int dsitrap_fix_dbat5[];
108 extern int dsitrap_fix_dbat6[];
109 extern int dsitrap_fix_dbat7[];
110
111 void
112 oea_init(void (*handler)(void))
113 {
114 extern int trapcode[], trapsize[];
115 extern int sctrap[], scsize[];
116 extern int alitrap[], alisize[];
117 extern int dsitrap[], dsisize[];
118 extern int trapstart[], trapend[];
119 #ifdef PPC_OEA601
120 extern int dsi601trap[], dsi601size[];
121 #endif
122 extern int decrint[], decrsize[];
123 extern int tlbimiss[], tlbimsize[];
124 extern int tlbdlmiss[], tlbdlmsize[];
125 extern int tlbdsmiss[], tlbdsmsize[];
126 #if defined(DDB) || defined(KGDB)
127 extern int ddblow[], ddbsize[];
128 #endif
129 #ifdef IPKDB
130 extern int ipkdblow[], ipkdbsize[];
131 #endif
132 #ifdef ALTIVEC
133 register_t msr;
134 #endif
135 uintptr_t exc, exc_base;
136 #if defined(ALTIVEC) || defined(PPC_OEA)
137 register_t scratch;
138 #endif
139 unsigned int cpuvers;
140 size_t size;
141 struct cpu_info * const ci = &cpu_info[0];
142
143 #ifdef PPC_HIGH_VEC
144 exc_base = EXC_HIGHVEC;
145 #else
146 exc_base = 0;
147 #endif
148 KASSERT(mfspr(SPR_SPRG0) == (uintptr_t)ci);
149
150 cpuvers = mfpvr() >> 16;
151
152 /*
153 * Initialize proc0 and current pcb and pmap pointers.
154 */
155 (void) ci;
156 KASSERT(ci != NULL);
157 KASSERT(curcpu() == ci);
158 KASSERT(lwp0.l_cpu == ci);
159
160 curpcb = lwp_getpcb(&lwp0);
161 memset(curpcb, 0, sizeof(struct pcb));
162
163 #ifdef ALTIVEC
164 /*
165 * Initialize the vectors with NaNs
166 */
167 for (scratch = 0; scratch < 32; scratch++) {
168 curpcb->pcb_vr.vreg[scratch][0] = 0x7FFFDEAD;
169 curpcb->pcb_vr.vreg[scratch][1] = 0x7FFFDEAD;
170 curpcb->pcb_vr.vreg[scratch][2] = 0x7FFFDEAD;
171 curpcb->pcb_vr.vreg[scratch][3] = 0x7FFFDEAD;
172 }
173 #endif
174 curpm = curpcb->pcb_pm = pmap_kernel();
175
176 /*
177 * Cause a PGM trap if we branch to 0.
178 *
179 * XXX GCC4.1 complains about memset on address zero, so
180 * don't use the builtin.
181 */
182 #undef memset
183 memset(0, 0, 0x100);
184
185 /*
186 * Set up trap vectors. Don't assume vectors are on 0x100.
187 */
188 for (exc = exc_base; exc <= exc_base + EXC_LAST; exc += 0x100) {
189 switch (exc - exc_base) {
190 default:
191 size = (size_t)trapsize;
192 memcpy((void *)exc, trapcode, size);
193 break;
194 #if 0
195 case EXC_EXI:
196 /*
197 * This one is (potentially) installed during autoconf
198 */
199 break;
200 #endif
201 case EXC_SC:
202 size = (size_t)scsize;
203 memcpy((void *)exc, sctrap, size);
204 break;
205 case EXC_ALI:
206 size = (size_t)alisize;
207 memcpy((void *)exc, alitrap, size);
208 break;
209 case EXC_DSI:
210 #ifdef PPC_OEA601
211 if (cpuvers == MPC601) {
212 size = (size_t)dsi601size;
213 memcpy((void *)exc, dsi601trap, size);
214 break;
215 } else
216 #endif /* PPC_OEA601 */
217 if (oeacpufeat & OEACPU_NOBAT) {
218 size = (size_t)alisize;
219 memcpy((void *)exc, alitrap, size);
220 } else {
221 size = (size_t)dsisize;
222 memcpy((void *)exc, dsitrap, size);
223 }
224 break;
225 case EXC_DECR:
226 size = (size_t)decrsize;
227 memcpy((void *)exc, decrint, size);
228 break;
229 case EXC_IMISS:
230 size = (size_t)tlbimsize;
231 memcpy((void *)exc, tlbimiss, size);
232 break;
233 case EXC_DLMISS:
234 size = (size_t)tlbdlmsize;
235 memcpy((void *)exc, tlbdlmiss, size);
236 break;
237 case EXC_DSMISS:
238 size = (size_t)tlbdsmsize;
239 memcpy((void *)exc, tlbdsmiss, size);
240 break;
241 case EXC_PERF:
242 size = (size_t)trapsize;
243 memcpy((void *)exc, trapcode, size);
244 memcpy((void *)(exc_base + EXC_VEC), trapcode, size);
245 break;
246 #if defined(DDB) || defined(IPKDB) || defined(KGDB)
247 case EXC_RUNMODETRC:
248 #ifdef PPC_OEA601
249 if (cpuvers != MPC601) {
250 #endif
251 size = (size_t)trapsize;
252 memcpy((void *)exc, trapcode, size);
253 break;
254 #ifdef PPC_OEA601
255 }
256 /* FALLTHROUGH */
257 #endif
258 case EXC_PGM:
259 case EXC_TRC:
260 case EXC_BPT:
261 #if defined(DDB) || defined(KGDB)
262 size = (size_t)ddbsize;
263 memcpy((void *)exc, ddblow, size);
264 #if defined(IPKDB)
265 #error "cannot enable IPKDB with DDB or KGDB"
266 #endif
267 #else
268 size = (size_t)ipkdbsize;
269 memcpy((void *)exc, ipkdblow, size);
270 #endif
271 break;
272 #endif /* DDB || IPKDB || KGDB */
273 }
274 #if 0
275 exc += roundup(size, 32);
276 #endif
277 }
278
279 /*
280 * Install a branch absolute to trap0 to force a panic.
281 */
282 if ((uintptr_t)trap0 < 0x2000000) {
283 *(volatile uint32_t *) 0 = 0x7c6802a6;
284 *(volatile uint32_t *) 4 = 0x48000002 | (uintptr_t) trap0;
285 }
286
287 /*
288 * Get the cache sizes because install_extint calls __syncicache.
289 */
290 cpu_probe_cache();
291
292 #define MxSPR_MASK 0x7c1fffff
293 #define MFSPR_MQ 0x7c0002a6
294 #define MTSPR_MQ 0x7c0003a6
295 #define MTSPR_IBAT0L 0x7c1183a6
296 #define MTSPR_IBAT1L 0x7c1383a6
297 #define NOP 0x60000000
298 #define B 0x48000000
299 #define TLBSYNC 0x7c00046c
300 #define SYNC 0x7c0004ac
301
302 #ifdef ALTIVEC
303 #define MFSPR_VRSAVE 0x7c0042a6
304 #define MTSPR_VRSAVE 0x7c0043a6
305
306 /*
307 * Try to set the VEC bit in the MSR. If it doesn't get set, we are
308 * not on a AltiVec capable processor.
309 */
310 __asm volatile (
311 "mfmsr %0; oris %1,%0,%2@h; mtmsr %1; isync; "
312 "mfmsr %1; mtmsr %0; isync"
313 : "=r"(msr), "=r"(scratch)
314 : "J"(PSL_VEC));
315
316 /*
317 * If we aren't on an AltiVec capable processor, we need to zap any of
318 * the sequences we save/restore the VRSAVE SPR into NOPs.
319 */
320 if (scratch & PSL_VEC) {
321 cpu_altivec = 1;
322 } else {
323 int *ip = trapstart;
324
325 for (; ip < trapend; ip++) {
326 if ((ip[0] & MxSPR_MASK) == MFSPR_VRSAVE) {
327 ip[0] = NOP; /* mfspr */
328 ip[1] = NOP; /* stw */
329 } else if ((ip[0] & MxSPR_MASK) == MTSPR_VRSAVE) {
330 ip[-1] = NOP; /* lwz */
331 ip[0] = NOP; /* mtspr */
332 }
333 }
334 }
335 #endif
336
337 /* XXX It would seem like this code could be elided ifndef 601, but
338 * doing so breaks my power3 machine.
339 */
340 /*
341 * If we aren't on a MPC601 processor, we need to zap any of the
342 * sequences we save/restore the MQ SPR into NOPs, and skip over the
343 * sequences where we zap/restore BAT registers on kernel exit/entry.
344 */
345 if (cpuvers != MPC601) {
346 int *ip = trapstart;
347
348 for (; ip < trapend; ip++) {
349 if ((ip[0] & MxSPR_MASK) == MFSPR_MQ) {
350 ip[0] = NOP; /* mfspr */
351 ip[1] = NOP; /* stw */
352 } else if ((ip[0] & MxSPR_MASK) == MTSPR_MQ) {
353 ip[-1] = NOP; /* lwz */
354 ip[0] = NOP; /* mtspr */
355 } else if ((ip[0] & MxSPR_MASK) == MTSPR_IBAT0L) {
356 if ((ip[1] & MxSPR_MASK) == MTSPR_IBAT1L)
357 ip[-1] = B | 0x14; /* li */
358 else
359 ip[-4] = B | 0x24; /* lis */
360 }
361 }
362 }
363
364 /*
365 * Sync the changed instructions.
366 */
367 __syncicache((void *) trapstart,
368 (uintptr_t) trapend - (uintptr_t) trapstart);
369 __syncicache(dsitrap_fix_dbat4, 16);
370 __syncicache(dsitrap_fix_dbat7, 8);
371 #ifdef PPC_OEA601
372
373 /*
374 * If we are on a MPC601 processor, we need to zap any tlbsync
375 * instructions into sync. This differs from the above in
376 * examing all kernel text, as opposed to just the exception handling.
377 * We sync the icache on every instruction found since there are
378 * only very few of them.
379 */
380 if (cpuvers == MPC601) {
381 extern int kernel_text[], etext[];
382 int *ip;
383
384 for (ip = kernel_text; ip < etext; ip++)
385 if (*ip == TLBSYNC) {
386 *ip = SYNC;
387 __syncicache(ip, sizeof(*ip));
388 }
389 }
390 #endif /* PPC_OEA601 */
391
392 /*
393 * Configure a PSL user mask matching this processor.
394 */
395 cpu_psluserset = PSL_EE | PSL_PR | PSL_ME | PSL_IR | PSL_DR | PSL_RI;
396 cpu_pslusermod = PSL_FP | PSL_FE0 | PSL_FE1 | PSL_LE | PSL_SE | PSL_BE;
397 #ifdef PPC_OEA601
398 if (cpuvers == MPC601) {
399 cpu_psluserset &= PSL_601_MASK;
400 cpu_pslusermod &= PSL_601_MASK;
401 }
402 #endif
403 #ifdef ALTIVEC
404 if (cpu_altivec)
405 cpu_pslusermod |= PSL_VEC;
406 #endif
407 #ifdef PPC_HIGH_VEC
408 cpu_psluserset |= PSL_IP; /* XXX ok? */
409 #endif
410
411 /*
412 * external interrupt handler install
413 */
414 if (handler)
415 oea_install_extint(handler);
416
417 __syncicache((void *)exc_base, EXC_LAST + 0x100);
418
419 /*
420 * Now enable translation (and machine checks/recoverable interrupts).
421 */
422 #ifdef PPC_OEA
423 __asm volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
424 : "=r"(scratch)
425 : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
426 #endif
427
428 /*
429 * Let's take all the indirect calls via our stubs and patch
430 * them to be direct calls.
431 */
432 cpu_fixup_stubs();
433
434 KASSERT(curcpu() == ci);
435 }
436
437 #ifdef PPC_OEA601
438 void
439 mpc601_ioseg_add(paddr_t pa, register_t len)
440 {
441 const u_int i = pa >> ADDR_SR_SHFT;
442
443 if (len != BAT_BL_256M)
444 panic("mpc601_ioseg_add: len != 256M");
445
446 /*
447 * Translate into an I/O segment, load it, and stash away for use
448 * in pmap_bootstrap().
449 */
450 iosrtable[i] = SR601(SR601_Ks, SR601_BUID_MEMFORCED, 0, i);
451 __asm volatile ("mtsrin %0,%1"
452 :: "r"(iosrtable[i]),
453 "r"(pa));
454 }
455 #endif /* PPC_OEA601 */
456
457 #if defined (PPC_OEA) || defined (PPC_OEA64_BRIDGE)
458 #define DBAT_SET(n, batl, batu) \
459 do { \
460 mtspr(SPR_DBAT##n##L, (batl)); \
461 mtspr(SPR_DBAT##n##U, (batu)); \
462 } while (/*CONSTCOND*/ 0)
463 #define DBAT_RESET(n) DBAT_SET(n, 0, 0)
464 #define DBATU_GET(n) mfspr(SPR_DBAT##n##U)
465 #define IBAT_SET(n, batl, batu) \
466 do { \
467 mtspr(SPR_IBAT##n##L, (batl)); \
468 mtspr(SPR_IBAT##n##U, (batu)); \
469 } while (/*CONSTCOND*/ 0)
470 #define IBAT_RESET(n) IBAT_SET(n, 0, 0)
471
472 void
473 oea_iobat_add(paddr_t pa, register_t len)
474 {
475 static int z = 1;
476 const u_int n = BAT_BL_TO_SIZE(len) / BAT_BL_TO_SIZE(BAT_BL_8M);
477 const u_int i = BAT_VA2IDX(pa) & -n; /* in case pa was in the middle */
478 const int after_bat3 = (oeacpufeat & OEACPU_HIGHBAT) ? 4 : 8;
479
480 KASSERT(len >= BAT_BL_8M);
481
482 /*
483 * If the caller wanted a bigger BAT than the hardware supports,
484 * split it into smaller BATs.
485 */
486 if (len > BAT_BL_256M && (oeacpufeat & OEACPU_XBSEN) == 0) {
487 u_int xn = BAT_BL_TO_SIZE(len) >> 28;
488 while (xn-- > 0) {
489 oea_iobat_add(pa, BAT_BL_256M);
490 pa += 0x10000000;
491 }
492 return;
493 }
494
495 const register_t batl = BATL(pa, BAT_I|BAT_G, BAT_PP_RW);
496 const register_t batu = BATU(pa, len, BAT_Vs);
497
498 for (u_int j = 0; j < n; j++) {
499 battable[i + j].batl = batl;
500 battable[i + j].batu = batu;
501 }
502
503 /*
504 * Let's start loading the BAT registers.
505 */
506 switch (z) {
507 case 1:
508 DBAT_SET(1, batl, batu);
509 z = 2;
510 break;
511 case 2:
512 DBAT_SET(2, batl, batu);
513 z = 3;
514 break;
515 case 3:
516 DBAT_SET(3, batl, batu);
517 z = after_bat3; /* no highbat, skip to end */
518 break;
519 case 4:
520 DBAT_SET(4, batl, batu);
521 z = 5;
522 break;
523 case 5:
524 DBAT_SET(5, batl, batu);
525 z = 6;
526 break;
527 case 6:
528 DBAT_SET(6, batl, batu);
529 z = 7;
530 break;
531 case 7:
532 DBAT_SET(7, batl, batu);
533 z = 8;
534 break;
535 default:
536 break;
537 }
538 }
539
540 void
541 oea_iobat_remove(paddr_t pa)
542 {
543 const u_int i = BAT_VA2IDX(pa);
544
545 if (!BAT_VA_MATCH_P(battable[i].batu, pa) ||
546 !BAT_VALID_P(battable[i].batu, PSL_PR))
547 return;
548 const int n =
549 __SHIFTOUT(battable[i].batu, (BAT_XBL|BAT_BL) & ~BAT_BL_8M) + 1;
550 KASSERT((n & (n-1)) == 0); /* power of 2 */
551 KASSERT((i & (n-1)) == 0); /* multiple of n */
552
553 memset(&battable[i], 0, n*sizeof(battable[0]));
554
555 const int maxbat = oeacpufeat & OEACPU_HIGHBAT ? 8 : 4;
556 for (u_int k = 1 ; k < maxbat; k++) {
557 register_t batu;
558 switch (k) {
559 case 1:
560 batu = DBATU_GET(1);
561 if (BAT_VA_MATCH_P(batu, pa) &&
562 BAT_VALID_P(batu, PSL_PR))
563 DBAT_RESET(1);
564 break;
565 case 2:
566 batu = DBATU_GET(2);
567 if (BAT_VA_MATCH_P(batu, pa) &&
568 BAT_VALID_P(batu, PSL_PR))
569 DBAT_RESET(2);
570 break;
571 case 3:
572 batu = DBATU_GET(3);
573 if (BAT_VA_MATCH_P(batu, pa) &&
574 BAT_VALID_P(batu, PSL_PR))
575 DBAT_RESET(3);
576 break;
577 case 4:
578 batu = DBATU_GET(4);
579 if (BAT_VA_MATCH_P(batu, pa) &&
580 BAT_VALID_P(batu, PSL_PR))
581 DBAT_RESET(4);
582 break;
583 case 5:
584 batu = DBATU_GET(5);
585 if (BAT_VA_MATCH_P(batu, pa) &&
586 BAT_VALID_P(batu, PSL_PR))
587 DBAT_RESET(5);
588 break;
589 case 6:
590 batu = DBATU_GET(6);
591 if (BAT_VA_MATCH_P(batu, pa) &&
592 BAT_VALID_P(batu, PSL_PR))
593 DBAT_RESET(6);
594 break;
595 case 7:
596 batu = DBATU_GET(7);
597 if (BAT_VA_MATCH_P(batu, pa) &&
598 BAT_VALID_P(batu, PSL_PR))
599 DBAT_RESET(7);
600 break;
601 default:
602 break;
603 }
604 }
605 }
606
607 void
608 oea_batinit(paddr_t pa, ...)
609 {
610 struct mem_region *allmem, *availmem, *mp;
611 unsigned int cpuvers;
612 register_t msr = mfmsr();
613 va_list ap;
614
615 cpuvers = mfpvr() >> 16;
616
617 /*
618 * we need to call this before zapping BATs so OF calls work
619 */
620 mem_regions(&allmem, &availmem);
621
622 /*
623 * Initialize BAT registers to unmapped to not generate
624 * overlapping mappings below.
625 *
626 * The 601's implementation differs in the Valid bit being situated
627 * in the lower BAT register, and in being a unified BAT only whose
628 * four entries are accessed through the IBAT[0-3] SPRs.
629 *
630 * Also, while the 601 does distinguish between supervisor/user
631 * protection keys, it does _not_ distinguish between validity in
632 * supervisor/user mode.
633 */
634 if ((msr & (PSL_IR|PSL_DR)) == 0) {
635 #ifdef PPC_OEA601
636 if (cpuvers == MPC601) {
637 __asm volatile ("mtibatl 0,%0" :: "r"(0));
638 __asm volatile ("mtibatl 1,%0" :: "r"(0));
639 __asm volatile ("mtibatl 2,%0" :: "r"(0));
640 __asm volatile ("mtibatl 3,%0" :: "r"(0));
641 } else
642 #endif /* PPC_OEA601 */
643 {
644 DBAT_RESET(0); IBAT_RESET(0);
645 DBAT_RESET(1); IBAT_RESET(1);
646 DBAT_RESET(2); IBAT_RESET(2);
647 DBAT_RESET(3); IBAT_RESET(3);
648 if (oeacpufeat & OEACPU_HIGHBAT) {
649 DBAT_RESET(4); IBAT_RESET(4);
650 DBAT_RESET(5); IBAT_RESET(5);
651 DBAT_RESET(6); IBAT_RESET(6);
652 DBAT_RESET(7); IBAT_RESET(7);
653
654 /*
655 * Change the first instruction to branch to
656 * dsitrap_fix_dbat6
657 */
658 dsitrap_fix_dbat4[0] &= ~0xfffc;
659 dsitrap_fix_dbat4[0]
660 += (uintptr_t)dsitrap_fix_dbat6
661 - (uintptr_t)&dsitrap_fix_dbat4[0];
662
663 /*
664 * Change the second instruction to branch to
665 * dsitrap_fix_dbat5 if bit 30 (aka bit 1) is
666 * true.
667 */
668 dsitrap_fix_dbat4[1] = 0x419e0000
669 + (uintptr_t)dsitrap_fix_dbat5
670 - (uintptr_t)&dsitrap_fix_dbat4[1];
671
672 /*
673 * Change it to load dbat4 instead of dbat2
674 */
675 dsitrap_fix_dbat4[2] = 0x7fd88ba6;
676 dsitrap_fix_dbat4[3] = 0x7ff98ba6;
677
678 /*
679 * Change it to load dbat7 instead of dbat3
680 */
681 dsitrap_fix_dbat7[0] = 0x7fde8ba6;
682 dsitrap_fix_dbat7[1] = 0x7fff8ba6;
683 }
684 }
685 }
686
687 /*
688 * Set up BAT to map physical memory
689 */
690 #ifdef PPC_OEA601
691 if (cpuvers == MPC601) {
692 int i;
693
694 /*
695 * Set up battable to map the lowest 256 MB area.
696 * Map the lowest 32 MB area via BAT[0-3];
697 * BAT[01] are fixed, BAT[23] are floating.
698 */
699 for (i = 0; i < 32; i++) {
700 battable[i].batl = BATL601(i << 23,
701 BAT601_BSM_8M, BAT601_V);
702 battable[i].batu = BATU601(i << 23,
703 BAT601_M, BAT601_Ku, BAT601_PP_NONE);
704 }
705 __asm volatile ("mtibatu 0,%1; mtibatl 0,%0"
706 :: "r"(battable[0x00000000 >> 23].batl),
707 "r"(battable[0x00000000 >> 23].batu));
708 __asm volatile ("mtibatu 1,%1; mtibatl 1,%0"
709 :: "r"(battable[0x00800000 >> 23].batl),
710 "r"(battable[0x00800000 >> 23].batu));
711 __asm volatile ("mtibatu 2,%1; mtibatl 2,%0"
712 :: "r"(battable[0x01000000 >> 23].batl),
713 "r"(battable[0x01000000 >> 23].batu));
714 __asm volatile ("mtibatu 3,%1; mtibatl 3,%0"
715 :: "r"(battable[0x01800000 >> 23].batl),
716 "r"(battable[0x01800000 >> 23].batu));
717 }
718 #endif /* PPC_OEA601 */
719
720 /*
721 * Now setup other fixed bat registers
722 *
723 * Note that we still run in real mode, and the BAT
724 * registers were cleared above.
725 */
726
727 va_start(ap, pa);
728
729 /*
730 * Add any I/O BATs specificed;
731 * use I/O segments on the BAT-starved 601.
732 */
733 #ifdef PPC_OEA601
734 if (cpuvers == MPC601) {
735 while (pa != 0) {
736 register_t len = va_arg(ap, register_t);
737 mpc601_ioseg_add(pa, len);
738 pa = va_arg(ap, paddr_t);
739 }
740 } else
741 #endif
742 {
743 while (pa != 0) {
744 register_t len = va_arg(ap, register_t);
745 oea_iobat_add(pa, len);
746 pa = va_arg(ap, paddr_t);
747 }
748 }
749
750 va_end(ap);
751
752 /*
753 * Set up battable to map all RAM regions.
754 */
755 #ifdef PPC_OEA601
756 if (cpuvers == MPC601) {
757 for (mp = allmem; mp->size; mp++) {
758 paddr_t paddr = mp->start & 0xff800000;
759 paddr_t end = mp->start + mp->size;
760
761 do {
762 u_int ix = paddr >> 23;
763
764 battable[ix].batl =
765 BATL601(paddr, BAT601_BSM_8M, BAT601_V);
766 battable[ix].batu =
767 BATU601(paddr, BAT601_M, BAT601_Ku, BAT601_PP_NONE);
768 paddr += (1 << 23);
769 } while (paddr < end);
770 }
771 } else
772 #endif
773 {
774 const register_t bat_inc = BAT_IDX2VA(1);
775 for (mp = allmem; mp->size; mp++) {
776 paddr_t paddr = mp->start & -bat_inc;
777 paddr_t end = roundup2(mp->start + mp->size, bat_inc);
778
779 /*
780 * If the next entries are adjacent, merge them
781 * into this one
782 */
783 while (mp[1].size && end == (mp[1].start & -bat_inc)) {
784 mp++;
785 end = roundup2(mp->start + mp->size, bat_inc);
786 }
787
788 while (paddr < end) {
789 register_t bl = (oeacpufeat & OEACPU_XBSEN
790 ? BAT_BL_2G
791 : BAT_BL_256M);
792 psize_t size = BAT_BL_TO_SIZE(bl);
793 u_int n = BAT_VA2IDX(size);
794 u_int i = BAT_VA2IDX(paddr);
795
796 while ((paddr & (size - 1))
797 || paddr + size > end) {
798 size >>= 1;
799 bl = (bl >> 1) & (BAT_XBL|BAT_BL);
800 n >>= 1;
801 }
802
803 KASSERT(size >= bat_inc);
804 KASSERT(n >= 1);
805 KASSERT(bl >= BAT_BL_8M);
806
807 register_t batl = BATL(paddr, BAT_M, BAT_PP_RW);
808 register_t batu = BATU(paddr, bl, BAT_Vs);
809
810 for (; n-- > 0; i++) {
811 battable[i].batl = batl;
812 battable[i].batu = batu;
813 }
814 paddr += size;
815 }
816 }
817 /*
818 * Set up BAT0 to only map the lowest area.
819 */
820 __asm volatile ("mtibatl 0,%0; mtibatu 0,%1;"
821 "mtdbatl 0,%0; mtdbatu 0,%1;"
822 :: "r"(battable[0].batl), "r"(battable[0].batu));
823 }
824 }
825 #endif /* PPC_OEA || PPC_OEA64_BRIDGE */
826
827 void
828 oea_install_extint(void (*handler)(void))
829 {
830 extern int extint[], extsize[];
831 extern int extint_call[];
832 uintptr_t offset = (uintptr_t)handler - (uintptr_t)extint_call;
833 int omsr, msr;
834
835 #ifdef DIAGNOSTIC
836 if (offset > 0x1ffffff)
837 panic("install_extint: %p too far away (%#lx)", handler,
838 (unsigned long) offset);
839 #endif
840 __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
841 : "=r" (omsr), "=r" (msr)
842 : "K" ((u_short)~PSL_EE));
843 extint_call[0] = (extint_call[0] & 0xfc000003) | offset;
844 __syncicache((void *)extint_call, sizeof extint_call[0]);
845 #ifdef PPC_HIGH_VEC
846 memcpy((void *)(EXC_HIGHVEC + EXC_EXI), extint, (size_t)extsize);
847 __syncicache((void *)(EXC_HIGHVEC + EXC_EXI), (int)extsize);
848 #else
849 memcpy((void *)EXC_EXI, extint, (size_t)extsize);
850 __syncicache((void *)EXC_EXI, (int)extsize);
851 #endif
852 __asm volatile ("mtmsr %0" :: "r"(omsr));
853 }
854
855 /*
856 * Machine dependent startup code.
857 */
858 void
859 oea_startup(const char *model)
860 {
861 uintptr_t sz;
862 void *v;
863 vaddr_t minaddr, maxaddr;
864 char pbuf[9];
865
866 KASSERT(curcpu() != NULL);
867 KASSERT(lwp0.l_cpu != NULL);
868 KASSERT(curcpu()->ci_idepth == -1);
869
870 sz = round_page(MSGBUFSIZE);
871 #ifdef MSGBUFADDR
872 v = (void *) MSGBUFADDR;
873 #else
874 /*
875 * If the msgbuf is not in segment 0, allocate KVA for it and access
876 * it via mapped pages. [This prevents unneeded BAT switches.]
877 */
878 v = (void *) msgbuf_paddr;
879 if (msgbuf_paddr + sz > SEGMENT_LENGTH) {
880 u_int i;
881
882 minaddr = 0;
883 if (uvm_map(kernel_map, &minaddr, sz,
884 NULL, UVM_UNKNOWN_OFFSET, 0,
885 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE,
886 UVM_INH_NONE, UVM_ADV_NORMAL, 0)) != 0)
887 panic("startup: cannot allocate VM for msgbuf");
888 v = (void *)minaddr;
889 for (i = 0; i < sz; i += PAGE_SIZE) {
890 pmap_kenter_pa(minaddr + i, msgbuf_paddr + i,
891 VM_PROT_READ|VM_PROT_WRITE, 0);
892 }
893 pmap_update(pmap_kernel());
894 }
895 #endif
896 initmsgbuf(v, sz);
897
898 printf("%s%s", copyright, version);
899 if (model != NULL)
900 printf("Model: %s\n", model);
901 cpu_identify(NULL, 0);
902
903 format_bytes(pbuf, sizeof(pbuf), ctob((u_int)physmem));
904 printf("total memory = %s\n", pbuf);
905
906 /*
907 * Allocate away the pages that map to 0xDEA[CDE]xxxx. Do this after
908 * the bufpages are allocated in case they overlap since it's not
909 * fatal if we can't allocate these.
910 */
911 if (KERNEL_SR == 13 || KERNEL2_SR == 14) {
912 int error;
913 minaddr = 0xDEAC0000;
914 error = uvm_map(kernel_map, &minaddr, 0x30000,
915 NULL, UVM_UNKNOWN_OFFSET, 0,
916 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
917 UVM_ADV_NORMAL, UVM_FLAG_FIXED));
918 if (error != 0 || minaddr != 0xDEAC0000)
919 printf("oea_startup: failed to allocate DEAD "
920 "ZONE: error=%d\n", error);
921 }
922
923 minaddr = 0;
924
925 /*
926 * Allocate a submap for physio
927 */
928 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
929 VM_PHYS_SIZE, 0, false, NULL);
930
931 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
932 printf("avail memory = %s\n", pbuf);
933 }
934
935 /*
936 * Crash dump handling.
937 */
938
939 void
940 oea_dumpsys(void)
941 {
942 printf("dumpsys: TBD\n");
943 }
944
945 /*
946 * Convert kernel VA to physical address
947 */
948 paddr_t
949 kvtop(void *addr)
950 {
951 vaddr_t va;
952 paddr_t pa;
953 uintptr_t off;
954 extern char end[];
955
956 if (addr < (void *)end)
957 return (paddr_t)addr;
958
959 va = trunc_page((vaddr_t)addr);
960 off = (uintptr_t)addr - va;
961
962 if (pmap_extract(pmap_kernel(), va, &pa) == false) {
963 /*printf("kvtop: zero page frame (va=0x%x)\n", addr);*/
964 return (paddr_t)addr;
965 }
966
967 return(pa + off);
968 }
969
970 /*
971 * Allocate vm space and mapin the I/O address
972 */
973 void *
974 mapiodev(paddr_t pa, psize_t len, bool prefetchable)
975 {
976 paddr_t faddr;
977 vaddr_t taddr, va;
978 int off;
979
980 faddr = trunc_page(pa);
981 off = pa - faddr;
982 len = round_page(off + len);
983 va = taddr = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY);
984
985 if (va == 0)
986 return NULL;
987
988 for (; len > 0; len -= PAGE_SIZE) {
989 pmap_kenter_pa(taddr, faddr, VM_PROT_READ | VM_PROT_WRITE,
990 (prefetchable ? PMAP_MD_PREFETCHABLE : PMAP_NOCACHE));
991 faddr += PAGE_SIZE;
992 taddr += PAGE_SIZE;
993 }
994 pmap_update(pmap_kernel());
995 return (void *)(va + off);
996 }
997
998 void
999 unmapiodev(vaddr_t va, vsize_t len)
1000 {
1001 paddr_t faddr;
1002
1003 if (! va)
1004 return;
1005
1006 faddr = trunc_page(va);
1007 len = round_page(va - faddr + len);
1008
1009 pmap_kremove(faddr, len);
1010 pmap_update(pmap_kernel());
1011 uvm_km_free(kernel_map, faddr, len, UVM_KMF_VAONLY);
1012 }
1013
1014 void
1015 trap0(void *lr)
1016 {
1017 panic("call to null-ptr from %p", lr);
1018 }
1019