First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / shared / vidmem.c
blob0b44116041555fb5d2b66ee81ef243665237d60b
1 /*
2 * Copyright (c) 1993-2003 by The XFree86 Project, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name of the copyright holder(s)
23 * and author(s) shall not be used in advertising or otherwise to promote
24 * the sale, use or other dealings in this Software without prior written
25 * authorization from the copyright holder(s) and author(s).
29 #ifdef HAVE_XORG_CONFIG_H
30 #include <xorg-config.h>
31 #endif
33 #include <X11/X.h>
34 #include "input.h"
35 #include "scrnintstr.h"
37 #include "xf86.h"
38 #include "xf86Priv.h"
39 #include "xf86_OSlib.h"
40 #include "xf86OSpriv.h"
43 * This file contains the common part of the video memory mapping functions
47 * Get a piece of the ScrnInfoRec. At the moment, this is only used to hold
48 * the MTRR option information, but it is likely to be expanded if we do
49 * auto unmapping of memory at VT switch.
53 typedef struct {
54 unsigned long physBase;
55 unsigned long size;
56 pointer virtBase;
57 pointer mtrrInfo;
58 int flags;
59 } MappingRec, *MappingPtr;
61 typedef struct {
62 int numMappings;
63 MappingPtr * mappings;
64 Bool mtrrEnabled;
65 MessageType mtrrFrom;
66 Bool mtrrOptChecked;
67 ScrnInfoPtr pScrn;
68 } VidMapRec, *VidMapPtr;
70 static int vidMapIndex = -1;
72 #define VIDMAPPTR(p) ((VidMapPtr)((p)->privates[vidMapIndex].ptr))
74 static VidMemInfo vidMemInfo = {FALSE, };
75 static VidMapRec vidMapRec = {0, NULL, TRUE, X_DEFAULT, FALSE, NULL};
77 static VidMapPtr
78 getVidMapRec(int scrnIndex)
80 VidMapPtr vp;
81 ScrnInfoPtr pScrn;
83 if ((scrnIndex < 0) ||
84 !(pScrn = xf86Screens[scrnIndex]))
85 return &vidMapRec;
87 if (vidMapIndex < 0)
88 vidMapIndex = xf86AllocateScrnInfoPrivateIndex();
90 if (VIDMAPPTR(pScrn) != NULL)
91 return VIDMAPPTR(pScrn);
93 vp = pScrn->privates[vidMapIndex].ptr = xnfcalloc(sizeof(VidMapRec), 1);
94 vp->mtrrEnabled = TRUE; /* default to enabled */
95 vp->mtrrFrom = X_DEFAULT;
96 vp->mtrrOptChecked = FALSE;
97 vp->pScrn = pScrn;
98 return vp;
101 static MappingPtr
102 newMapping(VidMapPtr vp)
104 vp->mappings = xnfrealloc(vp->mappings, sizeof(MappingPtr) *
105 (vp->numMappings + 1));
106 vp->mappings[vp->numMappings] = xnfcalloc(sizeof(MappingRec), 1);
107 return vp->mappings[vp->numMappings++];
110 static MappingPtr
111 findMapping(VidMapPtr vp, pointer vbase, unsigned long size)
113 int i;
115 for (i = 0; i < vp->numMappings; i++) {
116 if (vp->mappings[i]->virtBase == vbase &&
117 vp->mappings[i]->size == size)
118 return vp->mappings[i];
120 return NULL;
123 static void
124 removeMapping(VidMapPtr vp, MappingPtr mp)
126 int i, found = 0;
128 for (i = 0; i < vp->numMappings; i++) {
129 if (vp->mappings[i] == mp) {
130 found = 1;
131 xfree(vp->mappings[i]);
132 } else if (found) {
133 vp->mappings[i - 1] = vp->mappings[i];
136 vp->numMappings--;
137 vp->mappings[vp->numMappings] = NULL;
140 enum { OPTION_MTRR };
141 static const OptionInfoRec opts[] =
143 { OPTION_MTRR, "mtrr", OPTV_BOOLEAN, {0}, FALSE },
144 { -1, NULL, OPTV_NONE, {0}, FALSE }
147 static void
148 checkMtrrOption(VidMapPtr vp)
150 if (!vp->mtrrOptChecked && vp->pScrn && vp->pScrn->options != NULL) {
151 OptionInfoPtr options;
153 options = xnfalloc(sizeof(opts));
154 (void)memcpy(options, opts, sizeof(opts));
155 xf86ProcessOptions(vp->pScrn->scrnIndex, vp->pScrn->options,
156 options);
157 if (xf86GetOptValBool(options, OPTION_MTRR, &vp->mtrrEnabled))
158 vp->mtrrFrom = X_CONFIG;
159 xfree(options);
160 vp->mtrrOptChecked = TRUE;
164 void
165 xf86MakeNewMapping(int ScreenNum, int Flags, unsigned long Base, unsigned long Size, pointer Vbase)
167 VidMapPtr vp;
168 MappingPtr mp;
170 vp = getVidMapRec(ScreenNum);
171 mp = newMapping(vp);
172 mp->physBase = Base;
173 mp->size = Size;
174 mp->virtBase = Vbase;
175 mp->flags = Flags;
178 void
179 xf86InitVidMem(void)
181 if (!vidMemInfo.initialised) {
182 memset(&vidMemInfo, 0, sizeof(VidMemInfo));
183 xf86OSInitVidMem(&vidMemInfo);
187 _X_EXPORT pointer
188 xf86MapVidMem(int ScreenNum, int Flags, unsigned long Base, unsigned long Size)
190 pointer vbase = NULL;
191 VidMapPtr vp;
192 MappingPtr mp;
194 if (((Flags & VIDMEM_FRAMEBUFFER) &&
195 (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))))
196 FatalError("Mapping memory with more than one type\n");
198 xf86InitVidMem();
199 if (!vidMemInfo.initialised || !vidMemInfo.mapMem)
200 return NULL;
202 vbase = vidMemInfo.mapMem(ScreenNum, Base, Size, Flags);
204 if (!vbase || vbase == (pointer)-1)
205 return NULL;
207 vp = getVidMapRec(ScreenNum);
208 mp = newMapping(vp);
209 mp->physBase = Base;
210 mp->size = Size;
211 mp->virtBase = vbase;
212 mp->flags = Flags;
215 * Check the "mtrr" option even when MTRR isn't supported to avoid
216 * warnings about unrecognised options.
218 checkMtrrOption(vp);
220 if (vp->mtrrEnabled && vidMemInfo.setWC) {
221 if (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))
222 mp->mtrrInfo =
223 vidMemInfo.setWC(ScreenNum, Base, Size, FALSE,
224 vp->mtrrFrom);
225 else if (Flags & VIDMEM_FRAMEBUFFER)
226 mp->mtrrInfo =
227 vidMemInfo.setWC(ScreenNum, Base, Size, TRUE,
228 vp->mtrrFrom);
230 return vbase;
233 _X_EXPORT void
234 xf86UnMapVidMem(int ScreenNum, pointer Base, unsigned long Size)
236 VidMapPtr vp;
237 MappingPtr mp;
239 if (!vidMemInfo.initialised || !vidMemInfo.unmapMem) {
240 xf86DrvMsg(ScreenNum, X_WARNING,
241 "xf86UnMapVidMem() called before xf86MapVidMem()\n");
242 return;
245 vp = getVidMapRec(ScreenNum);
246 mp = findMapping(vp, Base, Size);
247 if (!mp) {
248 xf86DrvMsg(ScreenNum, X_WARNING,
249 "xf86UnMapVidMem: cannot find region for [%p,0x%lx]\n",
250 Base, Size);
251 return;
253 if (vp->mtrrEnabled && vidMemInfo.undoWC && mp)
254 vidMemInfo.undoWC(ScreenNum, mp->mtrrInfo);
256 vidMemInfo.unmapMem(ScreenNum, Base, Size);
257 removeMapping(vp, mp);
260 _X_EXPORT Bool
261 xf86CheckMTRR(int ScreenNum)
263 VidMapPtr vp = getVidMapRec(ScreenNum);
266 * Check the "mtrr" option even when MTRR isn't supported to avoid
267 * warnings about unrecognised options.
269 checkMtrrOption(vp);
271 if (vp->mtrrEnabled && vidMemInfo.setWC)
272 return TRUE;
274 return FALSE;
277 _X_EXPORT Bool
278 xf86LinearVidMem()
280 xf86InitVidMem();
281 return vidMemInfo.linearSupported;
284 _X_EXPORT void
285 xf86MapReadSideEffects(int ScreenNum, int Flags, pointer base,
286 unsigned long Size)
288 if (!(Flags & VIDMEM_READSIDEEFFECT))
289 return;
291 if (!vidMemInfo.initialised || !vidMemInfo.readSideEffects)
292 return;
294 vidMemInfo.readSideEffects(ScreenNum, base, Size);