dvma.c revision 1.1 1 /* $NetBSD: dvma.c,v 1.1 1997/06/01 03:39:33 mrg 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 <machine/pte.h>
42 #include <machine/ctlreg.h>
43
44 #include <sparc/stand/common/promdev.h>
45
46 #define DVMA_BASE 0xFFF00000
47 #define DVMA_MAPLEN 0xE0000 /* 1 MB - 128K (save MONSHORTSEG) */
48
49 #define SA_MIN_VA (RELOC - 0x40000) /* XXX - magic constant */
50 #define SA_MAX_VA (SA_MIN_VA + DVMA_MAPLEN)
51
52 void
53 dvma_init()
54 {
55 register int segva, dmava;
56
57 dmava = DVMA_BASE;
58 for (segva = SA_MIN_VA; segva < SA_MAX_VA; segva += NBPSG) {
59 setsegmap(dmava, getsegmap(segva));
60 dmava += NBPSG;
61 }
62 }
63
64 /*
65 * Convert a local address to a DVMA address.
66 */
67 char *
68 dvma_mapin(char *addr, size_t len)
69 {
70 register int va = (int)addr;
71
72 /* Make sure the address is in the DVMA map. */
73 if ((va < SA_MIN_VA) || (va >= SA_MAX_VA))
74 panic("dvma_mapin");
75
76 va += DVMA_BASE - SA_MIN_VA;
77
78 return ((char *)va);
79 }
80
81 /*
82 * Convert a DVMA address to a local address.
83 */
84 char *
85 dvma_mapout(char *addr, size_t len)
86 {
87 int va = (int)addr;
88
89 /* Make sure the address is in the DVMA map. */
90 if ((va < DVMA_BASE) || (va >= (DVMA_BASE + DVMA_MAPLEN)))
91 panic("dvma_mapout");
92
93 va -= DVMA_BASE - SA_MIN_VA;
94
95 return ((char *)va);
96 }
97
98 extern char *alloc __P((int));
99
100 char *
101 dvma_alloc(int len)
102 {
103 char *mem;
104
105 mem = alloc(len);
106 if (!mem)
107 return (mem);
108 return (dvma_mapin(mem, len));
109 }
110
111 extern void free(void *ptr, int len);
112 void
113 dvma_free(char *dvma, int len)
114 {
115 char *mem;
116
117 mem = dvma_mapout(dvma, len);
118 if (mem)
119 free(mem, len);
120 }
121