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).
28 #ifdef HAVE_XORG_CONFIG_H
29 #include <xorg-config.h>
34 #include "scrnintstr.h"
38 #include "xf86_OSlib.h"
39 #include "xf86OSpriv.h"
42 * This file contains the common part of the video memory mapping functions
46 * Get a piece of the ScrnInfoRec. At the moment, this is only used to hold
47 * the MTRR option information, but it is likely to be expanded if we do
48 * auto unmapping of memory at VT switch.
56 } MappingRec
, *MappingPtr
;
65 } VidMapRec
, *VidMapPtr
;
67 static int vidMapIndex
= -1;
69 #define VIDMAPPTR(p) ((VidMapPtr)((p)->privates[vidMapIndex].ptr))
71 static VidMemInfo vidMemInfo
= { FALSE
, };
72 static VidMapRec vidMapRec
= { 0, NULL
, TRUE
, X_DEFAULT
, FALSE
, NULL
};
75 getVidMapRec(int scrnIndex
)
80 if ((scrnIndex
< 0) || !(pScrn
= xf86Screens
[scrnIndex
]))
84 vidMapIndex
= xf86AllocateScrnInfoPrivateIndex();
86 if (VIDMAPPTR(pScrn
) != NULL
)
87 return VIDMAPPTR(pScrn
);
89 vp
= pScrn
->privates
[vidMapIndex
].ptr
= xnfcalloc(sizeof(VidMapRec
), 1);
90 vp
->mtrrEnabled
= TRUE
; /* default to enabled */
91 vp
->mtrrFrom
= X_DEFAULT
;
92 vp
->mtrrOptChecked
= FALSE
;
98 newMapping(VidMapPtr vp
)
100 vp
->mappings
= xnfrealloc(vp
->mappings
, sizeof(MappingPtr
) *
101 (vp
->numMappings
+ 1));
102 vp
->mappings
[vp
->numMappings
] = xnfcalloc(sizeof(MappingRec
), 1);
103 return vp
->mappings
[vp
->numMappings
++];
107 findMapping(VidMapPtr vp
, pointer vbase
, unsigned long size
)
111 for (i
= 0; i
< vp
->numMappings
; i
++) {
112 if (vp
->mappings
[i
]->virtBase
== vbase
&& vp
->mappings
[i
]->size
== size
)
113 return vp
->mappings
[i
];
119 removeMapping(VidMapPtr vp
, MappingPtr mp
)
123 for (i
= 0; i
< vp
->numMappings
; i
++) {
124 if (vp
->mappings
[i
] == mp
) {
126 free(vp
->mappings
[i
]);
129 vp
->mappings
[i
- 1] = vp
->mappings
[i
];
133 vp
->mappings
[vp
->numMappings
] = NULL
;
136 enum { OPTION_MTRR
};
138 static const OptionInfoRec opts
[] = {
139 {OPTION_MTRR
, "mtrr", OPTV_BOOLEAN
, {0}, FALSE
},
140 {-1, NULL
, OPTV_NONE
, {0}, FALSE
}
144 checkMtrrOption(VidMapPtr vp
)
146 if (!vp
->mtrrOptChecked
&& vp
->pScrn
&& vp
->pScrn
->options
!= NULL
) {
147 OptionInfoPtr options
;
149 options
= xnfalloc(sizeof(opts
));
150 (void) memcpy(options
, opts
, sizeof(opts
));
151 xf86ProcessOptions(vp
->pScrn
->scrnIndex
, vp
->pScrn
->options
, options
);
152 if (xf86GetOptValBool(options
, OPTION_MTRR
, &vp
->mtrrEnabled
))
153 vp
->mtrrFrom
= X_CONFIG
;
155 vp
->mtrrOptChecked
= TRUE
;
162 if (!vidMemInfo
.initialised
) {
163 memset(&vidMemInfo
, 0, sizeof(VidMemInfo
));
164 xf86OSInitVidMem(&vidMemInfo
);
169 xf86MapVidMem(int ScreenNum
, int Flags
, unsigned long Base
, unsigned long Size
)
171 pointer vbase
= NULL
;
175 if (((Flags
& VIDMEM_FRAMEBUFFER
) &&
176 (Flags
& (VIDMEM_MMIO
| VIDMEM_MMIO_32BIT
))))
177 FatalError("Mapping memory with more than one type\n");
180 if (!vidMemInfo
.initialised
|| !vidMemInfo
.mapMem
)
183 vbase
= vidMemInfo
.mapMem(ScreenNum
, Base
, Size
, Flags
);
185 if (!vbase
|| vbase
== (pointer
) -1)
188 vp
= getVidMapRec(ScreenNum
);
191 mp
->virtBase
= vbase
;
194 * Check the "mtrr" option even when MTRR isn't supported to avoid
195 * warnings about unrecognised options.
199 if (vp
->mtrrEnabled
&& vidMemInfo
.setWC
) {
200 if (Flags
& (VIDMEM_MMIO
| VIDMEM_MMIO_32BIT
))
202 vidMemInfo
.setWC(ScreenNum
, Base
, Size
, FALSE
, vp
->mtrrFrom
);
203 else if (Flags
& VIDMEM_FRAMEBUFFER
)
205 vidMemInfo
.setWC(ScreenNum
, Base
, Size
, TRUE
, vp
->mtrrFrom
);
211 xf86UnMapVidMem(int ScreenNum
, pointer Base
, unsigned long Size
)
216 if (!vidMemInfo
.initialised
|| !vidMemInfo
.unmapMem
) {
217 xf86DrvMsg(ScreenNum
, X_WARNING
,
218 "xf86UnMapVidMem() called before xf86MapVidMem()\n");
222 vp
= getVidMapRec(ScreenNum
);
223 mp
= findMapping(vp
, Base
, Size
);
225 xf86DrvMsg(ScreenNum
, X_WARNING
,
226 "xf86UnMapVidMem: cannot find region for [%p,0x%lx]\n",
230 if (vp
->mtrrEnabled
&& vidMemInfo
.undoWC
&& mp
)
231 vidMemInfo
.undoWC(ScreenNum
, mp
->mtrrInfo
);
233 vidMemInfo
.unmapMem(ScreenNum
, Base
, Size
);
234 removeMapping(vp
, mp
);
238 xf86CheckMTRR(int ScreenNum
)
240 VidMapPtr vp
= getVidMapRec(ScreenNum
);
243 * Check the "mtrr" option even when MTRR isn't supported to avoid
244 * warnings about unrecognised options.
248 if (vp
->mtrrEnabled
&& vidMemInfo
.setWC
)
255 xf86LinearVidMem(void)
258 return vidMemInfo
.linearSupported
;