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