Home | History | Annotate | Line # | Download | only in common
dvma.c revision 1.2
      1 /*	$NetBSD: dvma.c,v 1.2 1998/10/12 21:17:28 pk Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Gordon W. Ross
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  * 4. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Gordon Ross
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * The easiest way to deal with the need for DVMA mappings is
     34  * to just map the entire third megabyte of RAM into DVMA space.
     35  * That way, dvma_mapin can just compute the DVMA alias address,
     36  * and dvma_mapout does nothing.  Note that this assumes all
     37  * standalone programs stay in the range SA_MIN_VA .. SA_MAX_VA
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sparc/sparc/asm.h>
     42 #include <machine/pte.h>
     43 #include <machine/ctlreg.h>
     44 
     45 #include <sparc/stand/common/promdev.h>
     46 
     47 #define	DVMA_BASE	0xFFF00000
     48 #define DVMA_MAPLEN	0xE0000	/* 1 MB - 128K (save MONSHORTSEG) */
     49 
     50 #define SA_MIN_VA	(RELOC - 0x40000)	/* XXX - magic constant */
     51 #define SA_MAX_VA	(SA_MIN_VA + DVMA_MAPLEN)
     52 
     53 #define	getsegmap(va)		(CPU_ISSUN4C \
     54 					? lduba(va, ASI_SEGMAP) \
     55 					: (lduha(va, ASI_SEGMAP)))
     56 #define	setsegmap(va, pmeg)	(CPU_ISSUN4C \
     57 					? stba(va, ASI_SEGMAP, pmeg) \
     58 					: stha(va, ASI_SEGMAP, pmeg))
     59 void
     60 dvma_init()
     61 {
     62 	register int segva, dmava;
     63 
     64 	dmava = DVMA_BASE;
     65 	for (segva = SA_MIN_VA; segva < SA_MAX_VA; segva += NBPSG) {
     66 		setsegmap(dmava, getsegmap(segva));
     67 		dmava += NBPSG;
     68 	}
     69 }
     70 
     71 /*
     72  * Convert a local address to a DVMA address.
     73  */
     74 char *
     75 dvma_mapin(char *addr, size_t len)
     76 {
     77 	register int va = (int)addr;
     78 
     79 	/* Make sure the address is in the DVMA map. */
     80 	if ((va < SA_MIN_VA) || (va >= SA_MAX_VA))
     81 		panic("dvma_mapin");
     82 
     83 	va += DVMA_BASE - SA_MIN_VA;
     84 
     85 	return ((char *)va);
     86 }
     87 
     88 /*
     89  * Convert a DVMA address to a local address.
     90  */
     91 char *
     92 dvma_mapout(char *addr, size_t len)
     93 {
     94 	int va = (int)addr;
     95 
     96 	/* Make sure the address is in the DVMA map. */
     97 	if ((va < DVMA_BASE) || (va >= (DVMA_BASE + DVMA_MAPLEN)))
     98 		panic("dvma_mapout");
     99 
    100 	va -= DVMA_BASE - SA_MIN_VA;
    101 
    102 	return ((char *)va);
    103 }
    104 
    105 extern char *alloc __P((int));
    106 
    107 char *
    108 dvma_alloc(int len)
    109 {
    110 	char *mem;
    111 
    112 	mem = alloc(len);
    113 	if (!mem)
    114 		return (mem);
    115 	return (dvma_mapin(mem, len));
    116 }
    117 
    118 extern void free(void *ptr, int len);
    119 void
    120 dvma_free(char *dvma, int len)
    121 {
    122 	char *mem;
    123 
    124 	mem = dvma_mapout(dvma, len);
    125 	if (mem)
    126 		free(mem, len);
    127 }
    128