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