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>
35
#include "scrnintstr.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.
54
unsigned long physBase;
59
} MappingRec, *MappingPtr;
63
MappingPtr * mappings;
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};
78
getVidMapRec(int scrnIndex)
83
if ((scrnIndex < 0) ||
84
!(pScrn = xf86Screens[scrnIndex]))
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;
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++];
111
findMapping(VidMapPtr vp, pointer vbase, unsigned long size)
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];
124
removeMapping(VidMapPtr vp, MappingPtr mp)
128
for (i = 0; i < vp->numMappings; i++) {
129
if (vp->mappings[i] == mp) {
131
xfree(vp->mappings[i]);
133
vp->mappings[i - 1] = vp->mappings[i];
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 }
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,
157
if (xf86GetOptValBool(options, OPTION_MTRR, &vp->mtrrEnabled))
158
vp->mtrrFrom = X_CONFIG;
160
vp->mtrrOptChecked = TRUE;
165
xf86MakeNewMapping(int ScreenNum, int Flags, unsigned long Base, unsigned long Size, pointer Vbase)
170
vp = getVidMapRec(ScreenNum);
174
mp->virtBase = Vbase;
181
if (!vidMemInfo.initialised) {
182
memset(&vidMemInfo, 0, sizeof(VidMemInfo));
183
xf86OSInitVidMem(&vidMemInfo);
188
xf86MapVidMem(int ScreenNum, int Flags, unsigned long Base, unsigned long Size)
190
pointer vbase = NULL;
194
if (((Flags & VIDMEM_FRAMEBUFFER) &&
195
(Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))))
196
FatalError("Mapping memory with more than one type\n");
199
if (!vidMemInfo.initialised || !vidMemInfo.mapMem)
202
vbase = vidMemInfo.mapMem(ScreenNum, Base, Size, Flags);
204
if (!vbase || vbase == (pointer)-1)
207
vp = getVidMapRec(ScreenNum);
211
mp->virtBase = vbase;
215
* Check the "mtrr" option even when MTRR isn't supported to avoid
216
* warnings about unrecognised options.
220
if (vp->mtrrEnabled && vidMemInfo.setWC) {
221
if (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))
223
vidMemInfo.setWC(ScreenNum, Base, Size, FALSE,
225
else if (Flags & VIDMEM_FRAMEBUFFER)
227
vidMemInfo.setWC(ScreenNum, Base, Size, TRUE,
234
xf86UnMapVidMem(int ScreenNum, pointer Base, unsigned long Size)
239
if (!vidMemInfo.initialised || !vidMemInfo.unmapMem) {
240
xf86DrvMsg(ScreenNum, X_WARNING,
241
"xf86UnMapVidMem() called before xf86MapVidMem()\n");
245
vp = getVidMapRec(ScreenNum);
246
mp = findMapping(vp, Base, Size);
248
xf86DrvMsg(ScreenNum, X_WARNING,
249
"xf86UnMapVidMem: cannot find region for [%p,0x%lx]\n",
253
if (vp->mtrrEnabled && vidMemInfo.undoWC && mp)
254
vidMemInfo.undoWC(ScreenNum, mp->mtrrInfo);
256
vidMemInfo.unmapMem(ScreenNum, Base, Size);
257
removeMapping(vp, mp);
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.
271
if (vp->mtrrEnabled && vidMemInfo.setWC)
278
xf86LinearVidMem(void)
281
return vidMemInfo.linearSupported;
285
xf86MapReadSideEffects(int ScreenNum, int Flags, pointer base,
288
if (!(Flags & VIDMEM_READSIDEEFFECT))
291
if (!vidMemInfo.initialised || !vidMemInfo.readSideEffects)
294
vidMemInfo.readSideEffects(ScreenNum, base, Size);