dvma.c revision 1.10 1 /* $NetBSD: dvma.c,v 1.10 2003/11/10 08:51:52 wiz 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;
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(addr, len)
88 char *addr;
89 size_t len;
90 {
91 int va = (int)addr;
92
93 va -= base_va;
94
95 #ifndef BOOTXX
96 /* Make sure the address is in the DVMA map. */
97 if (va < 0 || va >= DVMA_MAPLEN)
98 panic("dvma_mapin: va %x (DMA base %x)", va+base_va, base_va);
99 #endif
100
101 va += DVMA_BASE;
102
103 return ((char *)va);
104 }
105
106 /*
107 * Convert a DVMA address to a local address.
108 */
109 char *
110 dvma_mapout(addr, len)
111 char *addr;
112 size_t len;
113 {
114 int va = (int)addr;
115
116 va -= DVMA_BASE;
117
118 #ifndef BOOTXX
119 /* Make sure the address is in the DVMA map. */
120 if (va < 0 || va >= DVMA_MAPLEN)
121 panic("dvma_mapout: va %x (DMA base %x)", va+base_va, base_va);
122 #endif
123
124 va += base_va;
125
126 return ((char *)va);
127 }
128
129 char *
130 dvma_alloc(len)
131 int len;
132 {
133 char *mem;
134
135 mem = alloc(len);
136 if (mem == NULL)
137 return (mem);
138 return (dvma_mapin(mem, len));
139 }
140
141 void
142 dvma_free(dvma, len)
143 char *dvma;
144 int len;
145 {
146 char *mem;
147
148 mem = dvma_mapout(dvma, len);
149 if (mem != NULL)
150 free(mem, len);
151 }
152