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