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