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