1 /* $NetBSD: dvma.c,v 1.14 2006/07/13 20:03:34 uwe 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(void) 61 { 62 u_int segva, dmava; 63 int nseg; 64 extern int start; 65 66 /* 67 * Align our address base with the DVMA segment. 68 * Allocate one DVMA segment to cover the stack, which 69 * grows downward from `start'. 70 */ 71 dmava = DVMA_BASE; 72 base_va = segva = (((int)&start) & -NBPSG) - NBPSG; 73 74 /* Then double-map the DVMA addresses */ 75 nseg = (DVMA_MAPLEN + NBPSG - 1) >> SGSHIFT; 76 while (nseg-- > 0) { 77 setsegmap(dmava, getsegmap(segva)); 78 segva += NBPSG; 79 dmava += NBPSG; 80 } 81 } 82 83 /* 84 * Convert a local address to a DVMA address. 85 */ 86 char * 87 dvma_mapin(char *addr, size_t len) 88 { 89 int va = (int)addr; 90 91 va -= base_va; 92 93 #ifndef BOOTXX 94 /* Make sure the address is in the DVMA map. */ 95 if (va < 0 || va >= DVMA_MAPLEN) 96 panic("dvma_mapin: va %x (DMA base %x)", va+base_va, base_va); 97 #endif 98 99 va += DVMA_BASE; 100 101 return ((char *)va); 102 } 103 104 /* 105 * Convert a DVMA address to a local address. 106 */ 107 char * 108 dvma_mapout(char *addr, size_t len) 109 { 110 int va = (int)addr; 111 112 va -= DVMA_BASE; 113 114 #ifndef BOOTXX 115 /* Make sure the address is in the DVMA map. */ 116 if (va < 0 || va >= DVMA_MAPLEN) 117 panic("dvma_mapout: va %x (DMA base %x)", va+base_va, base_va); 118 #endif 119 120 va += base_va; 121 122 return ((char *)va); 123 } 124 125 char * 126 dvma_alloc(int len) 127 { 128 char *mem; 129 130 mem = alloc(len); 131 if (mem == NULL) 132 return (mem); 133 return (dvma_mapin(mem, len)); 134 } 135 136 void 137 dvma_free(char *dvma, int len) 138 { 139 char *mem; 140 141 mem = dvma_mapout(dvma, len); 142 if (mem != NULL) 143 dealloc(mem, len); 144 } 145