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