2 * Abstraction of the AGP GART interface.
4 * This version is for Linux and Free/Open/NetBSD.
6 * Copyright © 2000 VA Linux Systems, Inc.
7 * Copyright © 2001 The XFree86 Project, Inc.
10 #ifdef HAVE_XORG_CONFIG_H
11 #include <xorg-config.h>
17 #include "xf86_OSlib.h"
18 #include "xf86OSpriv.h"
21 #include <asm/ioctl.h>
22 #include <linux/agpgart.h>
23 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
24 #include <sys/ioctl.h>
25 #include <sys/agpio.h>
29 #define AGP_DEVICE "/dev/agpgart"
31 /* AGP page size is independent of the host page size. */
33 #define AGP_PAGE_SIZE 4096
35 #define AGPGART_MAJOR_VERSION 0
36 #define AGPGART_MINOR_VERSION 99
38 static int gartFd
= -1;
39 static int acquiredScreen
= -1;
40 static Bool initDone
= FALSE
;
43 * Close /dev/agpgart. This frees all associated memory allocated during
44 * this server generation.
47 xf86GARTCloseScreen(int screenNum
)
59 * Open /dev/agpgart. Keep it open until xf86GARTCloseScreen is called.
62 GARTInit(int screenNum
)
64 struct _agp_info agpinf
;
72 gartFd
= open(AGP_DEVICE
, O_RDWR
, 0);
77 xf86DrvMsg(screenNum
, X_ERROR
,
78 "GARTInit: Unable to open " AGP_DEVICE
" (%s)\n",
84 /* Check the kernel driver version. */
85 if (ioctl(gartFd
, AGPIOC_INFO
, &agpinf
) != 0) {
86 xf86DrvMsg(screenNum
, X_ERROR
,
87 "GARTInit: AGPIOC_INFO failed (%s)\n", strerror(errno
));
95 /* Per Dave Jones, every effort will be made to keep the
96 * agpgart interface backwards compatible, so allow all
100 #if (AGPGART_MAJOR_VERSION > 0) /* quiet compiler */
101 agpinf
.version
.major
< AGPGART_MAJOR_VERSION
||
103 (agpinf
.version
.major
== AGPGART_MAJOR_VERSION
&&
104 agpinf
.version
.minor
< AGPGART_MINOR_VERSION
)) {
105 xf86DrvMsg(screenNum
, X_ERROR
,
106 "GARTInit: Kernel agpgart driver version is not current"
107 " (%d.%d vs %d.%d)\n",
108 agpinf
.version
.major
, agpinf
.version
.minor
,
109 AGPGART_MAJOR_VERSION
, AGPGART_MINOR_VERSION
);
120 xf86AgpGARTSupported(void)
126 xf86GetAGPInfo(int screenNum
)
128 struct _agp_info agpinf
;
131 if (!GARTInit(screenNum
))
134 if ((info
= calloc(sizeof(AgpInfo
), 1)) == NULL
) {
135 xf86DrvMsg(screenNum
, X_ERROR
,
136 "xf86GetAGPInfo: Failed to allocate AgpInfo\n");
140 memset((char *) &agpinf
, 0, sizeof(agpinf
));
142 if (ioctl(gartFd
, AGPIOC_INFO
, &agpinf
) != 0) {
143 xf86DrvMsg(screenNum
, X_ERROR
,
144 "xf86GetAGPInfo: AGPIOC_INFO failed (%s)\n",
149 info
->bridgeId
= agpinf
.bridge_id
;
150 info
->agpMode
= agpinf
.agp_mode
;
151 info
->base
= agpinf
.aper_base
;
152 info
->size
= agpinf
.aper_size
;
153 info
->totalPages
= agpinf
.pg_total
;
154 info
->systemPages
= agpinf
.pg_system
;
155 info
->usedPages
= agpinf
.pg_used
;
157 xf86DrvMsg(screenNum
, X_INFO
, "Kernel reported %zu total, %zu used\n",
158 agpinf
.pg_total
, agpinf
.pg_used
);
164 * XXX If multiple screens can acquire the GART, should we have a reference
165 * count instead of using acquiredScreen?
169 xf86AcquireGART(int screenNum
)
171 if (screenNum
!= -1 && !GARTInit(screenNum
))
174 if (screenNum
== -1 || acquiredScreen
!= screenNum
) {
175 if (ioctl(gartFd
, AGPIOC_ACQUIRE
, 0) != 0) {
176 xf86DrvMsg(screenNum
, X_WARNING
,
177 "xf86AcquireGART: AGPIOC_ACQUIRE failed (%s)\n",
181 acquiredScreen
= screenNum
;
187 xf86ReleaseGART(int screenNum
)
189 if (screenNum
!= -1 && !GARTInit(screenNum
))
192 if (acquiredScreen
== screenNum
) {
194 * The FreeBSD agp driver removes allocations on release.
195 * The Linux driver doesn't. xf86ReleaseGART() is expected
196 * to give up access to the GART, but not to remove any
203 if (ioctl(gartFd
, AGPIOC_RELEASE
, 0) != 0) {
204 xf86DrvMsg(screenNum
, X_WARNING
,
205 "xf86ReleaseGART: AGPIOC_RELEASE failed (%s)\n",
217 xf86AllocateGARTMemory(int screenNum
, unsigned long size
, int type
,
218 unsigned long *physical
)
220 struct _agp_allocate alloc
;
224 * Allocates "size" bytes of GART memory (rounds up to the next
225 * page multiple) or type "type". A handle (key) for the allocated
226 * memory is returned. On error, the return value is -1.
229 if (!GARTInit(screenNum
) || acquiredScreen
!= screenNum
)
232 pages
= (size
/ AGP_PAGE_SIZE
);
233 if (size
% AGP_PAGE_SIZE
!= 0)
236 /* XXX check for pages == 0? */
238 alloc
.pg_count
= pages
;
241 if (ioctl(gartFd
, AGPIOC_ALLOCATE
, &alloc
) != 0) {
242 xf86DrvMsg(screenNum
, X_WARNING
, "xf86AllocateGARTMemory: "
243 "allocation of %d pages failed\n\t(%s)\n", pages
,
249 *physical
= alloc
.physical
;
255 xf86DeallocateGARTMemory(int screenNum
, int key
)
257 if (!GARTInit(screenNum
) || acquiredScreen
!= screenNum
)
260 if (acquiredScreen
!= screenNum
) {
261 xf86DrvMsg(screenNum
, X_ERROR
,
262 "xf86UnbindGARTMemory: AGP not acquired by this screen\n");
267 if (ioctl(gartFd
, AGPIOC_DEALLOCATE
, (int *) (uintptr_t) key
) != 0) {
269 if (ioctl(gartFd
, AGPIOC_DEALLOCATE
, &key
) != 0) {
271 xf86DrvMsg(screenNum
, X_WARNING
, "xf86DeAllocateGARTMemory: "
272 "deallocation gart memory with key %d failed\n\t(%s)\n",
273 key
, strerror(errno
));
280 /* Bind GART memory with "key" at "offset" */
282 xf86BindGARTMemory(int screenNum
, int key
, unsigned long offset
)
284 struct _agp_bind bind
;
287 if (!GARTInit(screenNum
) || acquiredScreen
!= screenNum
)
290 if (acquiredScreen
!= screenNum
) {
291 xf86DrvMsg(screenNum
, X_ERROR
,
292 "xf86BindGARTMemory: AGP not acquired by this screen\n");
296 if (offset
% AGP_PAGE_SIZE
!= 0) {
297 xf86DrvMsg(screenNum
, X_WARNING
, "xf86BindGARTMemory: "
298 "offset (0x%lx) is not page-aligned (%d)\n",
299 offset
, AGP_PAGE_SIZE
);
302 pageOffset
= offset
/ AGP_PAGE_SIZE
;
304 xf86DrvMsgVerb(screenNum
, X_INFO
, 3,
305 "xf86BindGARTMemory: bind key %d at 0x%08lx "
306 "(pgoffset %d)\n", key
, offset
, pageOffset
);
308 bind
.pg_start
= pageOffset
;
311 if (ioctl(gartFd
, AGPIOC_BIND
, &bind
) != 0) {
312 xf86DrvMsg(screenNum
, X_WARNING
, "xf86BindGARTMemory: "
313 "binding of gart memory with key %d\n"
314 "\tat offset 0x%lx failed (%s)\n",
315 key
, offset
, strerror(errno
));
322 /* Unbind GART memory with "key" */
324 xf86UnbindGARTMemory(int screenNum
, int key
)
326 struct _agp_unbind unbind
;
328 if (!GARTInit(screenNum
) || acquiredScreen
!= screenNum
)
331 if (acquiredScreen
!= screenNum
) {
332 xf86DrvMsg(screenNum
, X_ERROR
,
333 "xf86UnbindGARTMemory: AGP not acquired by this screen\n");
340 if (ioctl(gartFd
, AGPIOC_UNBIND
, &unbind
) != 0) {
341 xf86DrvMsg(screenNum
, X_WARNING
, "xf86UnbindGARTMemory: "
342 "unbinding of gart memory with key %d "
343 "failed (%s)\n", key
, strerror(errno
));
347 xf86DrvMsgVerb(screenNum
, X_INFO
, 3,
348 "xf86UnbindGARTMemory: unbind key %d\n", key
);
353 /* XXX Interface may change. */
355 xf86EnableAGP(int screenNum
, CARD32 mode
)
359 if (!GARTInit(screenNum
) || acquiredScreen
!= screenNum
)
362 setup
.agp_mode
= mode
;
363 if (ioctl(gartFd
, AGPIOC_SETUP
, &setup
) != 0) {
364 xf86DrvMsg(screenNum
, X_WARNING
, "xf86EnableAGP: "
365 "AGPIOC_SETUP with mode %ld failed (%s)\n",
366 (unsigned long) mode
, strerror(errno
));