summaryrefslogtreecommitdiff
path: root/graphics/utah-glx/files/patch-ag
blob: f73242728f07b0639aa4499e4345bdb6795b2375 (plain) (blame)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
--- servGL/hwglx/common/hw_mtrr.c.orig	Mon Oct  9 21:42:30 2000
+++ servGL/hwglx/common/hw_mtrr.c	Mon Oct  9 21:42:55 2000
@@ -1,4 +1,3 @@
-
 #include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
@@ -7,13 +6,15 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <sys/ioctl.h>
 
 #include "glx_symbols.h"
 #include "hwlog.h"
 
-
 #ifndef NO_MTRR
 
+#ifdef linux
+
 #define MTRR_NEED_STRINGS
 #include <errno.h>
 #include <asm/mtrr.h>
@@ -237,6 +238,339 @@
 		       sentry.base, sentry.size );
 	}
 }
+
+#endif 
+
+#if defined(__FreeBSD__) || defined(__NetBSD__)
+
+#ifndef __NetBSD__
+#include <sys/memrange.h>
+#else
+#include "memrange.h"
+#endif
+#define X_MTRR_ID "XFree86"
+
+typedef struct x_RangeRec {
+	struct mem_range_desc	mrd;
+	int			wasWC;
+	struct x_RangeRec *	next;
+} RangeRec, *RangePtr;
+
+static RangePtr savedRangeList = NULL;
+static int      devMemFd = -1;
+
+/*
+ * getAllRanges returns the full list of memory ranges with attributes set.
+ */
+
+static struct mem_range_desc *
+getAllRanges(int *nmr)
+{
+	struct mem_range_desc *mrd;
+	struct mem_range_op mro;
+
+	/*
+	 * Find how many ranges there are.  If this fails, then the kernel
+	 * probably doesn't have MTRR *support.
+	 */
+	mro.mo_arg[0] = 0;
+	if (ioctl(devMemFd, MEMRANGE_GET, &mro))
+		return NULL;
+	*nmr = mro.mo_arg[0];
+	mrd = (struct mem_range_desc *)xalloc(*nmr * sizeof(struct mem_range_desc));
+	mro.mo_arg[0] = *nmr;
+	mro.mo_desc = mrd;
+	if (ioctl(devMemFd, MEMRANGE_GET, &mro)) {
+		xfree(mrd);
+		return NULL;
+	}
+	return mrd;
+}
+
+static void
+freeRangeList(RangePtr range)
+{
+	RangePtr rp;
+
+	while (range) {
+		rp = range;
+		range = rp->next;
+		xfree(rp);
+	}
+}
+
+static RangePtr
+dupRangeList(RangePtr list)
+{
+	RangePtr new = NULL, rp, p;
+
+	rp = list;
+	while (rp) {
+		p = (RangePtr)xalloc(sizeof(RangeRec));
+		*p = *rp;
+		p->next = new;
+		new = p;
+		rp = rp->next;
+	}
+	return new;
+}
+
+static RangePtr
+sortRangeList(RangePtr list)
+{
+	RangePtr rp1, rp2, copy, sorted = NULL, minp, prev, minprev;
+	unsigned long minBase;
+
+	/* Sort by base address */
+	rp1 = copy = dupRangeList(list);
+	while (rp1) {
+		minBase = rp1->mrd.mr_base;
+		minp = rp1;
+		minprev = NULL;
+		prev = rp1;
+		rp2 = rp1->next;
+		while (rp2) {
+			if (rp2->mrd.mr_base < minBase) {
+				minBase = rp2->mrd.mr_base;
+				minp = rp2;
+				minprev = prev;
+			}
+			prev = rp2;
+			rp2 = rp2->next;
+		}
+		if (minprev) {
+			minprev->next = minp->next;
+			rp1 = copy;
+		} else {
+			rp1 = minp->next;
+		}
+		minp->next = sorted;
+		sorted = minp;
+	}
+	return sorted;
+}
+
+/*
+ * findRanges returns a list of ranges that overlap the specified range.
+ */
+
+static void
+findRanges(unsigned long base, unsigned long size, RangePtr *ucp, RangePtr *wcp)
+{
+	struct mem_range_desc *mrd;
+	int nmr, i;
+	RangePtr rp, *p;
+	
+	if (!(mrd = getAllRanges(&nmr)))
+		return;
+
+	for (i = 0; i < nmr; i++) {
+		if ((mrd[i].mr_flags & MDF_ACTIVE) &&
+		    mrd[i].mr_base < base + size &&
+		    mrd[i].mr_base + mrd[i].mr_len > base) {
+			if (mrd[i].mr_flags & MDF_WRITECOMBINE)
+				p = wcp;
+			else if (mrd[i].mr_flags & MDF_UNCACHEABLE)
+				p = ucp;
+			else
+				continue;
+			rp = (RangePtr)xalloc(sizeof(RangeRec));
+			rp->mrd = mrd[i];
+			rp->next = *p;
+			*p = rp;
+		}
+	}
+	xfree(mrd);
+}
+
+/*
+ * This checks if the existing overlapping ranges fully cover the requested
+ * range.  Is this overkill?
+ */
+
+static int
+fullCoverage(unsigned long base, unsigned long size, RangePtr overlap)
+{
+	RangePtr rp1, sorted = NULL;
+	unsigned long end;
+
+	sorted = sortRangeList(overlap);
+	/* Look for gaps */
+	rp1 = sorted;
+	end = base + size;
+	while (rp1) {
+		if (rp1->mrd.mr_base > base) {
+			freeRangeList(sorted);
+			return FALSE;
+		} else {
+			base = rp1->mrd.mr_base + rp1->mrd.mr_len;
+		}
+		if (base >= end) {
+			freeRangeList(sorted);
+			return 1;
+		}
+		rp1 = rp1->next;
+	}
+	freeRangeList(sorted);
+	return 0;
+}
+
+static RangePtr
+addWC(unsigned long base, unsigned long size)
+{
+	RangePtr uc = NULL, wc = NULL, retlist = NULL;
+	struct mem_range_desc mrd;
+	struct mem_range_op mro;
+
+	findRanges(base, size, &uc, &wc);
+
+	/* See of the full range is already WC */
+	if (!uc && fullCoverage(base, size, wc)) {
+		hwMsg(1, "Write-combining range (0x%lx,0x%lx) was already set\n",
+		    base, size);
+		return NULL;
+	}
+
+	/* Otherwise, try to add the new range */
+	mrd.mr_base = base;
+	mrd.mr_len = size;
+	strcpy(mrd.mr_owner, X_MTRR_ID);
+	mrd.mr_flags = MDF_WRITECOMBINE;
+	mro.mo_desc = &mrd;
+	mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+	if (ioctl(devMemFd, MEMRANGE_SET, &mro)) {
+		hwError("Failed to set write-combining range "
+			"(0x%lx,0x%lx)\n", base, size);
+		return NULL;
+	} else {
+	  hwMsg(1, "Write-combining range (0x%lx,0x%lx)\n", base, size);
+		retlist = (RangePtr)xalloc(sizeof(RangeRec));
+		retlist->mrd = mrd;
+		retlist->wasWC = FALSE;
+		retlist->next = NULL;
+		return retlist;
+	}
+}
+
+static void
+undoWC(RangePtr  rp)
+{
+	struct mem_range_op mro;
+	int failed;
+
+	while (rp) {
+		hwMsg(1,"Undo for (0x%lx,0x%lx), %d\n",
+			(unsigned long)rp->mrd.mr_base,
+			(unsigned long)rp->mrd.mr_len, rp->wasWC);
+		failed = FALSE;
+		if (rp->wasWC) {
+			mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+			rp->mrd.mr_flags = MDF_WRITECOMBINE;
+			strcpy(rp->mrd.mr_owner, "unknown");
+		} else {
+			mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
+		}
+		mro.mo_desc = &rp->mrd;
+
+		if (ioctl(devMemFd, MEMRANGE_SET, &mro)) {
+			if (!rp->wasWC) {
+				mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+				rp->mrd.mr_flags = MDF_UNCACHEABLE;
+				strcpy(rp->mrd.mr_owner, "unknown");
+				if (ioctl(devMemFd, MEMRANGE_SET, &mro))
+					failed = TRUE;
+			} else
+				failed = TRUE;
+		}
+		if (failed) {
+		        hwError("Failed to restore MTRR range (0x%lx,0x%lx)\n",
+				(unsigned long)rp->mrd.mr_base,
+				(unsigned long)rp->mrd.mr_len);
+		}
+		rp = rp->next;
+	}
+}
+
+static int
+cleanMTRR()
+{
+	struct mem_range_desc *mrd;
+	struct mem_range_op mro;
+	int nmr, i;
+
+	/* This shouldn't happen */
+	if (devMemFd < 0)
+		return FALSE;
+
+	if (!(mrd = getAllRanges(&nmr)))
+		return FALSE;
+
+	for (i = 0; i < nmr; i++) {
+		if (strcmp(mrd[i].mr_owner, X_MTRR_ID) == 0 &&
+		    (mrd[i].mr_flags & MDF_ACTIVE)) {
+			hwMsg( 1,"Clean for (0x%lx,0x%lx)\n",
+				(unsigned long)mrd[i].mr_base,
+				(unsigned long)mrd[i].mr_len);
+			if (mrd[i].mr_flags & MDF_FIXACTIVE) {
+				mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+				mrd[i].mr_flags = MDF_UNCACHEABLE;
+			} else {
+				mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
+			}
+			mro.mo_desc = mrd + i;
+			ioctl(devMemFd, MEMRANGE_SET, &mro);
+		}
+	}
+	xfree(mrd);
+	return TRUE;
+}
+
+static void CloseMTRR( void ) 
+{
+        undoWC(savedRangeList);
+        close(devMemFd);
+}
+
+static void OpenMTRR( void )
+{
+	if ( ( devMemFd = open( "/dev/mem", O_RDWR, 0 ) ) == -1 )
+	{
+		hwError( "Error opening /dev/mem: %s\n", strerror( errno ) );
+		hwError( "MTRR not enabled\n" );
+		return;
+	}
+
+	if ( !cleanMTRR() ) {
+        	close(devMemFd);
+	        devMemFd = -1;
+	        return;
+	}
+
+	atexit( CloseMTRR );
+}
+
+void SetWriteCombining( long physical, int bytes ) 
+{
+        RangePtr rp;
+
+        if ( devMemFd < 0 ) {
+	  OpenMTRR();
+	}
+
+	if ( devMemFd < 0 ) {
+		return;
+	}
+
+	rp = addWC(physical, bytes);
+
+	if ( NULL != rp ) {
+	        rp->next = savedRangeList;
+        	savedRangeList = rp;
+	}
+}
+
+
+#endif /* end of BSD MTRR support */
 
 /*
  * FlushWriteCombining