Home | History | Annotate | Line # | Download | only in libsa
sun2.c revision 1.2
      1 /*	$NetBSD: sun2.c,v 1.2 2001/11/30 16:00:27 fredette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross and Matthew Fredette.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Standalone functions specific to the Sun2.
     41  */
     42 
     43 /* Need to avoid conflicts on these: */
     44 #define get_pte sun2_get_pte
     45 #define set_pte sun2_set_pte
     46 #define get_segmap sun2_get_segmap
     47 #define set_segmap sun2_set_segmap
     48 
     49 #include <sys/param.h>
     50 #include <machine/idprom.h>
     51 #include <machine/mon.h>
     52 
     53 #include <arch/sun2/include/pte.h>
     54 #include <arch/sun2/sun2/control.h>
     55 #ifdef notyet
     56 #include <arch/sun3/sun3/vme.h>
     57 #else
     58 #define VME16_BASE MBIO_BASE
     59 #define VME16_MASK MBIO_MASK
     60 #endif
     61 #include <arch/sun2/sun2/mbmem.h>
     62 #include <arch/sun2/sun2/mbio.h>
     63 
     64 #include <stand.h>
     65 
     66 #include "libsa.h"
     67 #include "dvma.h"
     68 #include "saio.h"	/* enum MAPTYPES */
     69 
     70 #define OBIO_MASK 0xFFFFFF
     71 
     72 u_int	get_pte __P((vaddr_t va));
     73 void	set_pte __P((vaddr_t va, u_int pte));
     74 char *	dvma2_alloc  __P((int len));
     75 void	dvma2_free  __P((char *dvma, int len));
     76 char *	dvma2_mapin  __P((char *pkt, int len));
     77 void	dvma2_mapout  __P((char *dmabuf, int len));
     78 char *	dev2_mapin  __P((int type, u_long addr, int len));
     79 
     80 struct mapinfo {
     81 	int maptype;
     82 	int pgtype;
     83 	u_int base;
     84 	u_int mask;
     85 };
     86 
     87 #ifdef	notyet
     88 struct mapinfo
     89 sun2_mapinfo[MAP__NTYPES] = {
     90 	/* On-board memory, I/O */
     91 	{ MAP_MAINMEM,   PGT_OBMEM,   0,          ~0 },
     92 	{ MAP_OBIO,      PGT_OBIO,    0,          OBIO_MASK },
     93 	/* Multibus memory, I/O */
     94 	{ MAP_MBMEM,     PGT_MBMEM, MBMEM_BASE, MBMEM_MASK },
     95 	{ MAP_MBIO,      PGT_MBIO,  MBIO_BASE, MBIO_MASK },
     96 	/* VME A16 */
     97 	{ MAP_VME16A16D, PGT_VME_D16, VME16_BASE, VME16_MASK },
     98 	{ MAP_VME16A32D, 0, 0, 0 },
     99 	/* VME A24 */
    100 	{ MAP_VME24A16D, 0, 0, 0 },
    101 	{ MAP_VME24A32D, 0, 0, 0 },
    102 	/* VME A32 */
    103 	{ MAP_VME32A16D, 0, 0, 0 },
    104 	{ MAP_VME32A32D, 0, 0, 0 },
    105 };
    106 #endif
    107 
    108 /* The virtual address we will use for PROM device mappings. */
    109 int sun2_devmap = SUN3_MONSHORTSEG;
    110 
    111 char *
    112 dev2_mapin(maptype, physaddr, length)
    113 	int maptype;
    114 	u_long physaddr;
    115 	int length;
    116 {
    117 #ifdef	notyet
    118 	u_int i, pa, pte, pgva, va;
    119 
    120 	if ((sun2_devmap + length) > SUN3_MONSHORTPAGE)
    121 		panic("dev2_mapin: length=%d\n", length);
    122 
    123 	for (i = 0; i < MAP__NTYPES; i++)
    124 		if (sun2_mapinfo[i].maptype == maptype)
    125 			goto found;
    126 	panic("dev2_mapin: bad maptype");
    127 found:
    128 
    129 	if (physaddr & ~(sun2_mapinfo[i].mask))
    130 		panic("dev2_mapin: bad address");
    131 	pa = sun2_mapinfo[i].base += physaddr;
    132 
    133 	pte = PA_PGNUM(pa) | PG_PERM |
    134 		sun2_mapinfo[i].pgtype;
    135 
    136 	va = pgva = sun2_devmap;
    137 	do {
    138 		set_pte(pgva, pte);
    139 		pgva += NBPG;
    140 		pte += 1;
    141 		length -= NBPG;
    142 	} while (length > 0);
    143 	sun2_devmap = pgva;
    144 	va += (physaddr & PGOFSET);
    145 
    146 #ifdef	DEBUG_PROM
    147 	if (debug)
    148 		printf("dev2_mapin: va=0x%x pte=0x%x\n",
    149 			   va, get_pte(va));
    150 #endif
    151 	return ((char*)va);
    152 #else
    153 	panic("dev2_mapin");
    154 	return(NULL);
    155 #endif
    156 }
    157 
    158 /*****************************************************************
    159  * DVMA support
    160  */
    161 
    162 /*
    163  * The easiest way to deal with the need for DVMA mappings is to
    164  * create a DVMA alias mapping of the entire address range used by
    165  * the boot program.  That way, dvma_mapin can just compute the
    166  * DVMA alias address, and dvma_mapout does nothing.
    167  *
    168  * Note that this assumes that standalone programs will do I/O
    169  * operations only within range (SA_MIN_VA .. SA_MAX_VA) checked.
    170  */
    171 
    172 #define DVMA_BASE 0x00f00000
    173 #define DVMA_MAPLEN  0x38000	/* 256K - 32K (save MONSHORTSEG) */
    174 
    175 #define SA_MIN_VA	0x220000
    176 #define SA_MAX_VA	(SA_MIN_VA + DVMA_MAPLEN)
    177 
    178 /* This points to the end of the free DVMA space. */
    179 u_int dvma2_end = DVMA_BASE + DVMA_MAPLEN;
    180 
    181 void
    182 dvma2_init()
    183 {
    184 	int segva, dmava, sme;
    185 
    186 	segva = SA_MIN_VA;
    187 	dmava = DVMA_BASE;
    188 
    189 	while (segva < SA_MAX_VA) {
    190 		sme = get_segmap(segva);
    191 		set_segmap(dmava, sme);
    192 		segva += NBSG;
    193 		dmava += NBSG;
    194 	}
    195 }
    196 
    197 /* Convert a local address to a DVMA address. */
    198 char *
    199 dvma2_mapin(char *addr, int len)
    200 {
    201 	int va = (int)addr;
    202 
    203 	/* Make sure the address is in the DVMA map. */
    204 	if ((va < SA_MIN_VA) || (va >= SA_MAX_VA))
    205 		panic("dvma2_mapin: 0x%x outside 0x%x..0x%x\n",
    206 		    va, SA_MIN_VA, SA_MAX_VA);
    207 
    208 	va -= SA_MIN_VA;
    209 	va += DVMA_BASE;
    210 
    211 	return ((char *) va);
    212 }
    213 
    214 /* Destroy a DVMA address alias. */
    215 void
    216 dvma2_mapout(char *addr, int len)
    217 {
    218 	int va = (int)addr;
    219 
    220 	/* Make sure the address is in the DVMA map. */
    221 	if ((va < DVMA_BASE) || (va >= (DVMA_BASE + DVMA_MAPLEN)))
    222 		panic("dvma2_mapout");
    223 }
    224 
    225 char *
    226 dvma2_alloc(int len)
    227 {
    228 	len = m68k_round_page(len);
    229 	dvma2_end -= len;
    230 	return((char*)dvma2_end);
    231 }
    232 
    233 void
    234 dvma2_free(char *dvma, int len)
    235 {
    236 	/* not worth the trouble */
    237 }
    238 
    239 /*****************************************************************
    240  * Control space stuff...
    241  */
    242 
    243 u_int
    244 get_pte(va)
    245 	vaddr_t va;
    246 {
    247 	u_int pte;
    248 
    249 	pte = get_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va));
    250 	if (pte & PG_VALID) {
    251 		/*
    252 		 * This clears bit 30 (the kernel readable bit, which
    253 		 * should always be set), bit 28 (which should always
    254 		 * be set) and bit 26 (the user writable bit, which we
    255 		 * always have tracking the kernel writable bit).  In
    256 		 * the protection, this leaves bit 29 (the kernel
    257 		 * writable bit) and bit 27 (the user readable bit).
    258 		 * See pte2.h for more about this hack.
    259 		 */
    260 		pte &= ~(0x54000000);
    261 		/*
    262 		 * Flip bit 27 (the user readable bit) to become bit
    263 		 * 27 (the PG_SYSTEM bit).
    264 		 */
    265 		pte ^= (PG_SYSTEM);
    266 	}
    267 	return (pte);
    268 }
    269 
    270 void
    271 set_pte(va, pte)
    272 	vaddr_t va;
    273 	u_int pte;
    274 {
    275 	if (pte & PG_VALID) {
    276 		/* Clear bit 26 (the user writable bit).  */
    277 		pte &= (~0x04000000);
    278 		/*
    279 		 * Flip bit 27 (the PG_SYSTEM bit) to become bit 27
    280 		 * (the user readable bit).
    281 		 */
    282 		pte ^= (PG_SYSTEM);
    283 		/*
    284 		 * Always set bits 30 (the kernel readable bit) and
    285 		 * bit 28, and set bit 26 (the user writable bit) iff
    286 		 * bit 29 (the kernel writable bit) is set *and* bit
    287 		 * 27 (the user readable bit) is set.  This latter bit
    288 		 * of logic is expressed in the bizarre second term
    289 		 * below, chosen because it needs no branches.
    290 		 */
    291 #if (PG_WRITE >> 2) != PG_SYSTEM
    292 #error	"PG_WRITE and PG_SYSTEM definitions don't match!"
    293 #endif
    294 		pte |= 0x50000000
    295 		    | ((((pte & PG_WRITE) >> 2) & pte) >> 1);
    296 	}
    297 	set_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va), pte);
    298 }
    299 
    300 int
    301 get_segmap(va)
    302 	vaddr_t va;
    303 {
    304 	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
    305 	return (get_control_byte(va));
    306 }
    307 
    308 void
    309 set_segmap(va, sme)
    310 	vaddr_t va;
    311 	int sme;
    312 {
    313 	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
    314 	set_control_byte(va, sme);
    315 }
    316 
    317 /*
    318  * Copy the IDPROM contents into the passed buffer.
    319  * The caller (idprom.c) will do the checksum.
    320  */
    321 void
    322 sun2_getidprom(u_char *dst)
    323 {
    324 	vaddr_t src;	/* control space address */
    325 	int len, x;
    326 
    327 	src = IDPROM_BASE;
    328 	len = sizeof(struct idprom);
    329 	do {
    330 		x = get_control_byte(src);
    331 		src += NBPG;
    332 		*dst++ = x;
    333 	} while (--len > 0);
    334 }
    335 
    336 /*****************************************************************
    337  * Init our function pointers, etc.
    338  */
    339 
    340 void
    341 sun2_init()
    342 {
    343 	vaddr_t va;
    344 	u_int pte;
    345 	u_int pte_zero;
    346 
    347 	/* Set the function pointers. */
    348 	dev_mapin_p   = dev2_mapin;
    349 	dvma_alloc_p  = dvma2_alloc;
    350 	dvma_free_p   = dvma2_free;
    351 	dvma_mapin_p  = dvma2_mapin;
    352 	dvma_mapout_p = dvma2_mapout;
    353 
    354 	/* Prepare DVMA segment. */
    355 	dvma2_init();
    356 
    357 	/*
    358 	 * XXX fredette - the PROM in my Sun 2/120 doesn't map a whole
    359 	 * lot of main memory contiguously from zero, only
    360 	 * 768KB. (0x0c0000).  This probably has something to do with
    361 	 * the PROM using physical addresses 0x0c0000 - 0x0fffff for
    362 	 * its DVMA space.  We'll need more contiguous memory to bring
    363 	 * in the kernel, so we make sure that the first 1.25MB
    364 	 * (0x140000) of main memory is mapped contiguously.  This value
    365 	 * was chosen because my 2/120 PROM maps mbmem space at VA
    366 	 * 0x140000, and I don't want to mess with it, and 1.25MB will
    367 	 * be enough for my early kernels. We use the PTE for page 0 as
    368 	 * a template.
    369 	 */
    370 #define MAINMEM_MAP_BYTES (0x140000)
    371 	pte_zero = (get_pte(0) & ~PG_FRAME);
    372 	for(va = NBPG; va < MAINMEM_MAP_BYTES; va += NBPG) {
    373 		pte = pte_zero | PA_PGNUM(va);
    374 		set_pte(va, pte);
    375 	}
    376 }
    377