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