summaryrefslogtreecommitdiff
path: root/hw/xfree86/os-support/shared/vidmem.c
blob: 4d775eb52f2463da07eade2696c9772d19a011d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (c) 1993-2003 by The XFree86 Project, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the copyright holder(s)
 * and author(s) shall not be used in advertising or otherwise to promote
 * the sale, use or other dealings in this Software without prior written
 * authorization from the copyright holder(s) and author(s).
 */


#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#ifdef __UNIXOS2__
# define I_NEED_OS2_H
#endif
#include <X11/X.h>
#include "input.h"
#include "scrnintstr.h"

#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#include "xf86OSpriv.h"

/*
 * This file contains the common part of the video memory mapping functions
 */

/*
 * Get a piece of the ScrnInfoRec.  At the moment, this is only used to hold
 * the MTRR option information, but it is likely to be expanded if we do
 * auto unmapping of memory at VT switch.
 *
 */

typedef struct {
	unsigned long 	physBase;
	unsigned long 	size;
	pointer		virtBase;
	pointer 	mtrrInfo;
	int		flags;
} MappingRec, *MappingPtr;
	
typedef struct {
	int		numMappings;
	MappingPtr *	mappings;
	Bool		mtrrEnabled;
	MessageType	mtrrFrom;
	Bool		mtrrOptChecked;
	ScrnInfoPtr	pScrn;
} VidMapRec, *VidMapPtr;

static int vidMapIndex = -1;

#define VIDMAPPTR(p) ((VidMapPtr)((p)->privates[vidMapIndex].ptr))

static VidMemInfo vidMemInfo = {FALSE, };
static VidMapRec  vidMapRec  = {0, NULL, TRUE, X_DEFAULT, FALSE, NULL};

static VidMapPtr
getVidMapRec(int scrnIndex)
{
	VidMapPtr vp;
	ScrnInfoPtr pScrn;

	if ((scrnIndex < 0) ||
	    !(pScrn = xf86Screens[scrnIndex]))
		return &vidMapRec;

	if (vidMapIndex < 0)
		vidMapIndex = xf86AllocateScrnInfoPrivateIndex();

	if (VIDMAPPTR(pScrn) != NULL)
		return VIDMAPPTR(pScrn);

	vp = pScrn->privates[vidMapIndex].ptr = xnfcalloc(sizeof(VidMapRec), 1);
	vp->mtrrEnabled = TRUE;	/* default to enabled */
	vp->mtrrFrom = X_DEFAULT;
	vp->mtrrOptChecked = FALSE;
	vp->pScrn = pScrn;
	return vp;
}

static MappingPtr
newMapping(VidMapPtr vp)
{
	vp->mappings = xnfrealloc(vp->mappings, sizeof(MappingPtr) *
				  (vp->numMappings + 1));
	vp->mappings[vp->numMappings] = xnfcalloc(sizeof(MappingRec), 1);
	return vp->mappings[vp->numMappings++];
}

static MappingPtr
findMapping(VidMapPtr vp, pointer vbase, unsigned long size)
{
	int i;

	for (i = 0; i < vp->numMappings; i++) {
		if (vp->mappings[i]->virtBase == vbase &&
		    vp->mappings[i]->size == size)
			return vp->mappings[i];
	}
	return NULL;
}

static void
removeMapping(VidMapPtr vp, MappingPtr mp)
{
	int i, found = 0;

	for (i = 0; i < vp->numMappings; i++) {
		if (vp->mappings[i] == mp) {
			found = 1;
			xfree(vp->mappings[i]);
		} else if (found) {
			vp->mappings[i - 1] = vp->mappings[i];
		}
	}
	vp->numMappings--;
	vp->mappings[vp->numMappings] = NULL;
}

enum { OPTION_MTRR };
static const OptionInfoRec opts[] =
{
	{ OPTION_MTRR, "mtrr", OPTV_BOOLEAN, {0}, FALSE },
	{ -1, NULL, OPTV_NONE, {0}, FALSE }
};

static void
checkMtrrOption(VidMapPtr vp)
{
	if (!vp->mtrrOptChecked && vp->pScrn && vp->pScrn->options != NULL) {
		OptionInfoPtr options;

		options = xnfalloc(sizeof(opts));
		(void)memcpy(options, opts, sizeof(opts));
		xf86ProcessOptions(vp->pScrn->scrnIndex, vp->pScrn->options,
					options);
		if (xf86GetOptValBool(options, OPTION_MTRR, &vp->mtrrEnabled))
			vp->mtrrFrom = X_CONFIG;
		xfree(options);
		vp->mtrrOptChecked = TRUE;
	}
}

void
xf86MakeNewMapping(int ScreenNum, int Flags, unsigned long Base, unsigned long Size, pointer Vbase)
{
	VidMapPtr vp;
	MappingPtr mp;

	vp = getVidMapRec(ScreenNum);
	mp = newMapping(vp);
	mp->physBase = Base;
	mp->size = Size;
	mp->virtBase = Vbase;
	mp->flags = Flags;
}

void
xf86InitVidMem(void)
{
	if (!vidMemInfo.initialised) {
		memset(&vidMemInfo, 0, sizeof(VidMemInfo));
		xf86OSInitVidMem(&vidMemInfo);
	}
}

_X_EXPORT pointer
xf86MapVidMem(int ScreenNum, int Flags, unsigned long Base, unsigned long Size)
{
	pointer vbase = NULL;
	VidMapPtr vp;
	MappingPtr mp;

	if (((Flags & VIDMEM_FRAMEBUFFER) &&
	     (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))))
	    FatalError("Mapping memory with more than one type\n");
	    
	xf86InitVidMem();
	if (!vidMemInfo.initialised || !vidMemInfo.mapMem)
		return NULL;

	vbase = vidMemInfo.mapMem(ScreenNum, Base, Size, Flags);

	if (!vbase || vbase == (pointer)-1)
		return NULL;

	vp = getVidMapRec(ScreenNum);
	mp = newMapping(vp);
	mp->physBase = Base;
	mp->size = Size;
	mp->virtBase = vbase;
	mp->flags = Flags;

	/*
	 * Check the "mtrr" option even when MTRR isn't supported to avoid
	 * warnings about unrecognised options.
	 */
	checkMtrrOption(vp);

	if (vp->mtrrEnabled && vidMemInfo.setWC) {
		if (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))
			mp->mtrrInfo =
				vidMemInfo.setWC(ScreenNum, Base, Size, FALSE,
						 vp->mtrrFrom);
		else if (Flags & VIDMEM_FRAMEBUFFER)
			mp->mtrrInfo =
				vidMemInfo.setWC(ScreenNum, Base, Size, TRUE,
						 vp->mtrrFrom);
	}
	return vbase;
}

_X_EXPORT void
xf86UnMapVidMem(int ScreenNum, pointer Base, unsigned long Size)
{
	VidMapPtr vp;
	MappingPtr mp;

	if (!vidMemInfo.initialised || !vidMemInfo.unmapMem) {
		xf86DrvMsg(ScreenNum, X_WARNING,
		  "xf86UnMapVidMem() called before xf86MapVidMem()\n");
		return;
	}

	vp = getVidMapRec(ScreenNum);
	mp = findMapping(vp, Base, Size);
	if (!mp) {
		xf86DrvMsg(ScreenNum, X_WARNING,
		  "xf86UnMapVidMem: cannot find region for [%p,0x%lx]\n",
		  Base, Size);
		return;
	}
	if (vp->mtrrEnabled && vidMemInfo.undoWC && mp)
		vidMemInfo.undoWC(ScreenNum, mp->mtrrInfo);

	vidMemInfo.unmapMem(ScreenNum, Base, Size);
	removeMapping(vp, mp);
}

_X_EXPORT Bool
xf86CheckMTRR(int ScreenNum)
{
	VidMapPtr vp = getVidMapRec(ScreenNum);

	/*
	 * Check the "mtrr" option even when MTRR isn't supported to avoid
	 * warnings about unrecognised options.
	 */
	checkMtrrOption(vp);

	if (vp->mtrrEnabled && vidMemInfo.setWC)
		return TRUE;
		
	return FALSE;
}

_X_EXPORT Bool
xf86LinearVidMem()
{
	xf86InitVidMem();
	return vidMemInfo.linearSupported;
}

_X_EXPORT void
xf86MapReadSideEffects(int ScreenNum, int Flags, pointer base,
			unsigned long Size)
{
	if (!(Flags & VIDMEM_READSIDEEFFECT))
		return;

	if (!vidMemInfo.initialised || !vidMemInfo.readSideEffects)
		return;

	vidMemInfo.readSideEffects(ScreenNum, base, Size);
}