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