dvma.c revision 1.3.2.2 1 /* $NetBSD: dvma.c,v 1.3.2.2 2000/07/23 16:13:49 he 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 u_int segva, dmava;
63 int nseg;
64 extern int start asm("start");
65
66 /* Align our address base with the DVMA segment */
67 dmava = DVMA_BASE;
68 base_va = segva = (((int)&start) - 0x40000) & DVMA_BASE;
69
70 /* Then double-map the DVMA adresses */
71 nseg = (DVMA_MAPLEN + NBPSG - 1) >> SGSHIFT;
72 while (nseg-- > 0) {
73 setsegmap(dmava, getsegmap(segva));
74 segva += NBPSG;
75 dmava += NBPSG;
76 }
77 }
78
79 /*
80 * Convert a local address to a DVMA address.
81 */
82 char *
83 dvma_mapin(addr, len)
84 char *addr;
85 size_t len;
86 {
87 int va = (int)addr;
88
89 va -= base_va;
90
91 #ifndef BOOTXX
92 /* Make sure the address is in the DVMA map. */
93 if (va < 0 || va >= DVMA_MAPLEN)
94 panic("dvma_mapin");
95 #endif
96
97 va += DVMA_BASE;
98
99 return ((char *)va);
100 }
101
102 /*
103 * Convert a DVMA address to a local address.
104 */
105 char *
106 dvma_mapout(addr, len)
107 char *addr;
108 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");
118 #endif
119
120 va += base_va;
121
122 return ((char *)va);
123 }
124
125 char *
126 dvma_alloc(len)
127 int len;
128 {
129 char *mem;
130
131 mem = alloc(len);
132 if (mem == NULL)
133 return (mem);
134 return (dvma_mapin(mem, len));
135 }
136
137 void
138 dvma_free(dvma, len)
139 char *dvma;
140 int len;
141 {
142 char *mem;
143
144 mem = dvma_mapout(dvma, len);
145 if (mem != NULL)
146 free(mem, len);
147 }
148