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
|
/*
* Abstraction of the AGP GART interface.
*
* This version is for both Linux and FreeBSD.
*
* Copyright © 2000-2001 Nokia Home Communications
* Copyright © 2000 VA Linux Systems, Inc.
All rights reserved.
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, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, provided that the above
copyright notice(s) and this permission notice appear in all copies of
the Software and that both the above copyright notice(s) and this
permission notice appear in supporting documentation.
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
OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Except as contained in this notice, the name of a copyright holder
shall not be used in advertising or otherwise to promote the sale, use
or other dealings in this Software without prior written authorization
of the copyright holder.
*/
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_agp.c,v 3.4 2000/08/28 18:12:56 dawes Exp $ */
/*
* Author: Pontus Lidman <pontus.lidman@nokia.com> (adaption to KDrive) and others
*/
#include "X.h"
#include "Xdefs.h"
#include "Xmd.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "agp.h"
#if defined(linux)
#include <asm/ioctl.h>
#include "../../xfree86/os-support/linux/agpgart.h"
#elif defined(__FreeBSD__)
#include <sys/ioctl.h>
#include <sys/agpio.h>
#endif
#ifndef AGP_DEVICE
#define AGP_DEVICE "/dev/agpgart"
#endif
/* AGP page size is independent of the host page size. */
#ifndef AGP_PAGE_SIZE
#define AGP_PAGE_SIZE 4096
#endif
#define AGPGART_MAJOR_VERSION 0
#define AGPGART_MINOR_VERSION 99
static int gartFd = -1;
static int acquiredScreen = -1;
/*
* Open /dev/agpgart. Keep it open until server exit.
*/
static Bool
GARTInit()
{
static Bool initDone = FALSE;
struct _agp_info agpinf;
if (initDone)
return (gartFd != -1);
initDone = TRUE;
if (gartFd == -1)
gartFd = open(AGP_DEVICE, O_RDWR, 0);
else
return FALSE;
if (gartFd == -1) {
fprintf(stderr, "Unable to open " AGP_DEVICE " (%s)\n",
strerror(errno));
return FALSE;
}
KdAcquireGART(-1);
/* Check the kernel driver version. */
if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) {
fprintf(stderr, "GARTInit: AGPIOC_INFO failed (%s)\n",
strerror(errno));
close(gartFd);
gartFd = -1;
return FALSE;
}
KdReleaseGART(-1);
#if defined(linux)
/* Per Dave Jones, every effort will be made to keep the
* agpgart interface backwards compatible, so allow all
* future versions.
*/
if (
#if (AGPGART_MAJOR_VERSION > 0) /* quiet compiler */
agpinf.version.major < AGPGART_MAJOR_VERSION ||
#endif
(agpinf.version.major == AGPGART_MAJOR_VERSION &&
agpinf.version.minor < AGPGART_MINOR_VERSION)) {
fprintf(stderr,
"Kernel agpgart driver version is not current"
" (%d.%d vs %d.%d)\n",
agpinf.version.major, agpinf.version.minor,
AGPGART_MAJOR_VERSION, AGPGART_MINOR_VERSION);
close(gartFd);
gartFd = -1;
return FALSE;
}
#endif
return TRUE;
}
Bool
KdAgpGARTSupported()
{
return GARTInit();
}
AgpInfoPtr
KdGetAGPInfo(int screenNum)
{
struct _agp_info agpinf;
AgpInfoPtr info;
if (!GARTInit())
return NULL;
if ((info = calloc(sizeof(AgpInfo), 1)) == NULL) {
fprintf(stderr, "Failed to allocate AgpInfo\n");
return NULL;
}
if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) {
fprintf(stderr,
"xf86GetAGPInfo: AGPIOC_INFO failed (%s)\n",
strerror(errno));
return NULL;
}
info->bridgeId = agpinf.bridge_id;
info->agpMode = agpinf.agp_mode;
info->base = agpinf.aper_base;
info->size = agpinf.aper_size;
info->totalPages = agpinf.pg_total;
info->systemPages = agpinf.pg_system;
info->usedPages = agpinf.pg_used;
return info;
}
/*
* XXX If multiple screens can acquire the GART, should we have a reference
* count instead of using acquiredScreen?
*/
Bool
KdAcquireGART(int screenNum)
{
if (screenNum != -1 && !GARTInit())
return FALSE;
if (screenNum == -1 || acquiredScreen != screenNum) {
if (ioctl(gartFd, AGPIOC_ACQUIRE, 0) != 0) {
fprintf(stderr,
"AGPIOC_ACQUIRE failed (%s)\n",
strerror(errno));
return FALSE;
}
acquiredScreen = screenNum;
}
return TRUE;
}
Bool
KdReleaseGART(int screenNum)
{
if (screenNum != -1 && !GARTInit())
return FALSE;
if (acquiredScreen == screenNum) {
if (ioctl(gartFd, AGPIOC_RELEASE, 0) != 0) {
fprintf(stderr,
"AGPIOC_RELEASE failed (%s)\n",
strerror(errno));
return FALSE;
}
acquiredScreen = -1;
return TRUE;
}
return FALSE;
}
int
KdAllocateGARTMemory(int screenNum, unsigned long size, int type,
unsigned long *physical)
{
struct _agp_allocate alloc;
int pages;
/*
* Allocates "size" bytes of GART memory (rounds up to the next
* page multiple) or type "type". A handle (key) for the allocated
* memory is returned. On error, the return value is -1.
*/
if (!GARTInit() || acquiredScreen != screenNum)
return -1;
pages = (size / AGP_PAGE_SIZE);
if (size % AGP_PAGE_SIZE != 0)
pages++;
/* XXX check for pages == 0? */
alloc.pg_count = pages;
alloc.type = type;
if (ioctl(gartFd, AGPIOC_ALLOCATE, &alloc) != 0) {
fprintf(stderr, "KdAllocateGARTMemory: "
"allocation of %d pages failed\n\t(%s)\n", pages,
strerror(errno));
return -1;
}
if (physical)
*physical = alloc.physical;
return alloc.key;
}
/* Bind GART memory with "key" at "offset" */
Bool
KdBindGARTMemory(int screenNum, int key, unsigned long offset)
{
struct _agp_bind bind;
int pageOffset;
if (!GARTInit() || acquiredScreen != screenNum)
return FALSE;
if (acquiredScreen != screenNum) {
fprintf(stderr,
"AGP not acquired by this screen\n");
return FALSE;
}
if (offset % AGP_PAGE_SIZE != 0) {
fprintf(stderr, "KdBindGARTMemory: "
"offset (0x%lx) is not page-aligned (%d)\n",
offset, AGP_PAGE_SIZE);
return FALSE;
}
pageOffset = offset / AGP_PAGE_SIZE;
bind.pg_start = pageOffset;
bind.key = key;
if (ioctl(gartFd, AGPIOC_BIND, &bind) != 0) {
fprintf(stderr, "KdBindGARTMemory: "
"binding of gart memory with key %d\n"
"\tat offset 0x%lx failed (%s)\n",
key, offset, strerror(errno));
return FALSE;
}
return TRUE;
}
/* Unbind GART memory with "key" */
Bool
KdUnbindGARTMemory(int screenNum, int key)
{
struct _agp_unbind unbind;
if (!GARTInit() || acquiredScreen != screenNum)
return FALSE;
if (acquiredScreen != screenNum) {
fprintf(stderr,
"AGP not acquired by this screen\n");
return FALSE;
}
unbind.priority = 0;
unbind.key = key;
if (ioctl(gartFd, AGPIOC_UNBIND, &unbind) != 0) {
fprintf(stderr, "KdUnbindGARTMemory: "
"unbinding of gart memory with key %d "
"failed (%s)\n", key, strerror(errno));
return FALSE;
}
return TRUE;
}
/* XXX Interface may change. */
Bool
KdEnableAGP(int screenNum, CARD32 mode)
{
agp_setup setup;
if (!GARTInit() || acquiredScreen != screenNum)
return FALSE;
setup.agp_mode = mode;
if (ioctl(gartFd, AGPIOC_SETUP, &setup) != 0) {
fprintf(stderr, "KdEnableAGP: "
"AGPIOC_SETUP with mode %ld failed (%s)\n",
mode, strerror(errno));
return FALSE;
}
return TRUE;
}
|