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