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