1 /* $NetBSD: dvma.c,v 1.5 2000/02/15 14:09:59 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 to just 34 * map the entire megabyte of RAM where we are loaded into DVMA space. 35 * That way, dvma_mapin can just compute the DVMA alias address, and 36 * dvma_mapout does nothing. Note that this assumes all standalone 37 * programs stay in the range `base_va' .. `base_va + DVMA_MAPLEN' 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 <lib/libsa/stand.h> 46 #include <sparc/stand/common/promdev.h> 47 48 #define DVMA_BASE 0xFFF00000 49 #define DVMA_MAPLEN 0xE0000 /* 1 MB - 128K (save MONSHORTSEG) */ 50 51 static int base_va; 52 53 /* 54 * This module is only used on sun4, so: 55 */ 56 #define getsegmap(va) (lduha(va, ASI_SEGMAP)) 57 #define setsegmap(va, pmeg) do stha(va, ASI_SEGMAP, pmeg); while(0) 58 59 void 60 dvma_init() 61 { 62 int segva, dmava; 63 extern int start; 64 65 /* Align our address base with the DVMA segment */ 66 base_va = segva = ((int)&start) & DVMA_BASE; 67 68 /* Then double-map the DVMA adresses */ 69 for (dmava = DVMA_BASE; dmava < DVMA_BASE + DVMA_MAPLEN; ) { 70 setsegmap(dmava, getsegmap(segva)); 71 segva += NBPSG; 72 dmava += NBPSG; 73 } 74 } 75 76 /* 77 * Convert a local address to a DVMA address. 78 */ 79 char * 80 dvma_mapin(addr, len) 81 char *addr; 82 size_t len; 83 { 84 int va = (int)addr; 85 86 va -= base_va; 87 88 #ifndef BOOTXX 89 /* Make sure the address is in the DVMA map. */ 90 if (va < 0 || va >= DVMA_MAPLEN) 91 panic("dvma_mapin"); 92 #endif 93 94 va += DVMA_BASE; 95 96 return ((char *)va); 97 } 98 99 /* 100 * Convert a DVMA address to a local address. 101 */ 102 char * 103 dvma_mapout(addr, len) 104 char *addr; 105 size_t len; 106 { 107 int va = (int)addr; 108 109 va -= DVMA_BASE; 110 111 #ifndef BOOTXX 112 /* Make sure the address is in the DVMA map. */ 113 if (va < 0 || va >= DVMA_MAPLEN) 114 panic("dvma_mapout"); 115 #endif 116 117 va += base_va; 118 119 return ((char *)va); 120 } 121 122 char * 123 dvma_alloc(len) 124 int len; 125 { 126 char *mem; 127 128 mem = alloc(len); 129 if (mem == NULL) 130 return (mem); 131 return (dvma_mapin(mem, len)); 132 } 133 134 void 135 dvma_free(dvma, len) 136 char *dvma; 137 int len; 138 { 139 char *mem; 140 141 mem = dvma_mapout(dvma, len); 142 if (mem != NULL) 143 free(mem, len); 144 } 145