2 * \file drm_agpsupport.c
3 * DRM support for AGP/GART backend
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
35 #include <linux/module.h>
36 #include "drm_legacy.h"
40 #include <dev/agp/agpreg.h>
41 #include <bus/pci/pcireg.h>
44 * Get AGP information.
46 * \param inode device inode.
47 * \param file_priv DRM file private.
49 * \param arg pointer to a (output) drm_agp_info structure.
50 * \return zero on success or a negative number on failure.
52 * Verifies the AGP device has been initialized and acquired and fills in the
53 * drm_agp_info structure with the information in drm_agp_head::agp_info.
55 int drm_agp_info(struct drm_device
*dev
, struct drm_agp_info
*info
)
57 struct agp_info
*kern
;
59 if (!dev
->agp
|| !dev
->agp
->acquired
)
62 kern
= &dev
->agp
->agp_info
;
63 agp_get_info(dev
->agp
->agpdev
, kern
);
64 info
->agp_version_major
= 1;
65 info
->agp_version_minor
= 0;
66 info
->mode
= kern
->ai_mode
;
67 info
->aperture_base
= kern
->ai_aperture_base
;
68 info
->aperture_size
= kern
->ai_aperture_size
;
69 info
->memory_allowed
= kern
->ai_memory_allowed
;
70 info
->memory_used
= kern
->ai_memory_used
;
71 info
->id_vendor
= kern
->ai_devid
& 0xffff;
72 info
->id_device
= kern
->ai_devid
>> 16;
77 EXPORT_SYMBOL(drm_agp_info
);
79 int drm_agp_info_ioctl(struct drm_device
*dev
, void *data
,
80 struct drm_file
*file_priv
)
82 struct drm_agp_info info
;
85 err
= drm_agp_info(dev
, &info
);
89 *(struct drm_agp_info
*) data
= info
;
94 * Acquire the AGP device.
96 * \param dev DRM device that is to acquire AGP.
97 * \return zero on success or a negative number on failure.
99 * Verifies the AGP device hasn't been acquired before and calls
100 * \c agp_backend_acquire.
102 int drm_agp_acquire(struct drm_device
* dev
)
106 if (!dev
->agp
|| dev
->agp
->acquired
)
109 retcode
= -agp_acquire(dev
->agp
->agpdev
);
113 dev
->agp
->acquired
= 1;
117 EXPORT_SYMBOL(drm_agp_acquire
);
120 * Acquire the AGP device (ioctl).
122 * \param inode device inode.
123 * \param file_priv DRM file private.
124 * \param cmd command.
125 * \param arg user argument.
126 * \return zero on success or a negative number on failure.
128 * Verifies the AGP device hasn't been acquired before and calls
129 * \c agp_backend_acquire.
131 int drm_agp_acquire_ioctl(struct drm_device
*dev
, void *data
,
132 struct drm_file
*file_priv
)
134 return drm_agp_acquire(dev
);
138 * Release the AGP device.
140 * \param dev DRM device that is to release AGP.
141 * \return zero on success or a negative number on failure.
143 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
145 int drm_agp_release(struct drm_device
* dev
)
147 if (!dev
->agp
|| !dev
->agp
->acquired
)
149 agp_release(dev
->agp
->agpdev
);
150 dev
->agp
->acquired
= 0;
153 EXPORT_SYMBOL(drm_agp_release
);
155 int drm_agp_release_ioctl(struct drm_device
*dev
, void *data
,
156 struct drm_file
*file_priv
)
158 return drm_agp_release(dev
);
162 * Enable the AGP bus.
164 * \param dev DRM device that has previously acquired AGP.
165 * \param mode Requested AGP mode.
166 * \return zero on success or a negative number on failure.
168 * Verifies the AGP device has been acquired but not enabled, and calls
171 int drm_agp_enable(struct drm_device
* dev
, struct drm_agp_mode mode
)
173 if (!dev
->agp
|| !dev
->agp
->acquired
)
176 dev
->agp
->mode
= mode
.mode
;
177 agp_enable(dev
->agp
->agpdev
, mode
.mode
);
178 dev
->agp
->enabled
= 1;
182 EXPORT_SYMBOL(drm_agp_enable
);
184 int drm_agp_enable_ioctl(struct drm_device
*dev
, void *data
,
185 struct drm_file
*file_priv
)
187 struct drm_agp_mode mode
;
189 mode
= *(struct drm_agp_mode
*) data
;
191 return drm_agp_enable(dev
, mode
);
194 static void *drm_agp_allocate_memory(size_t pages
, u32 type
)
198 agpdev
= agp_find_device();
202 return agp_alloc_memory(agpdev
, type
, pages
<< AGP_PAGE_SHIFT
);
206 * Allocate AGP memory.
208 * \param inode device inode.
209 * \param file_priv file private pointer.
210 * \param cmd command.
211 * \param arg pointer to a drm_agp_buffer structure.
212 * \return zero on success or a negative number on failure.
214 * Verifies the AGP device is present and has been acquired, allocates the
215 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
217 int drm_agp_alloc(struct drm_device
*dev
, struct drm_agp_buffer
*request
)
219 struct drm_agp_mem
*entry
;
223 struct agp_memory_info info
;
225 if (!dev
->agp
|| !dev
->agp
->acquired
)
228 entry
= kmalloc(sizeof(*entry
), M_DRM
, M_WAITOK
| M_NULLOK
| M_ZERO
);
232 pages
= (request
->size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
233 type
= (u_int32_t
) request
->type
;
235 handle
= drm_agp_allocate_memory(pages
, type
);
236 if (handle
== NULL
) {
237 drm_free(entry
, M_DRM
);
241 entry
->handle
= handle
;
243 entry
->pages
= pages
;
245 entry
->next
= dev
->agp
->memory
;
246 if (dev
->agp
->memory
)
247 dev
->agp
->memory
->prev
= entry
;
248 dev
->agp
->memory
= entry
;
250 agp_memory_info(dev
->agp
->agpdev
, entry
->handle
, &info
);
252 request
->handle
= (unsigned long) entry
->handle
;
253 request
->physical
= info
.ami_physical
;
257 EXPORT_SYMBOL(drm_agp_alloc
);
260 int drm_agp_alloc_ioctl(struct drm_device
*dev
, void *data
,
261 struct drm_file
*file_priv
)
263 struct drm_agp_buffer request
;
266 request
= *(struct drm_agp_buffer
*) data
;
268 retcode
= drm_agp_alloc(dev
, &request
);
270 *(struct drm_agp_buffer
*) data
= request
;
276 * Search for the AGP memory entry associated with a handle.
278 * \param dev DRM device structure.
279 * \param handle AGP memory handle.
280 * \return pointer to the drm_agp_mem structure associated with \p handle.
282 * Walks through drm_agp_head::memory until finding a matching handle.
284 static struct drm_agp_mem
*drm_agp_lookup_entry(struct drm_device
* dev
,
287 struct drm_agp_mem
*entry
;
289 for (entry
= dev
->agp
->memory
; entry
; entry
= entry
->next
) {
290 if (entry
->handle
== handle
)
296 static int drm_agp_unbind_memory(void *handle
)
300 agpdev
= agp_find_device();
301 if (!agpdev
|| !handle
)
304 return -agp_unbind_memory(agpdev
, handle
);
308 * Unbind AGP memory from the GATT (ioctl).
310 * \param inode device inode.
311 * \param file_priv DRM file private.
312 * \param cmd command.
313 * \param arg pointer to a drm_agp_binding structure.
314 * \return zero on success or a negative number on failure.
316 * Verifies the AGP device is present and acquired, looks-up the AGP memory
317 * entry and passes it to the unbind_agp() function.
319 int drm_agp_unbind(struct drm_device
*dev
, struct drm_agp_binding
*request
)
321 struct drm_agp_mem
*entry
;
324 if (!dev
->agp
|| !dev
->agp
->acquired
)
326 entry
= drm_agp_lookup_entry(dev
, (void *)request
->handle
);
327 if (entry
== NULL
|| !entry
->bound
)
329 ret
= drm_agp_unbind_memory(entry
->handle
);
334 EXPORT_SYMBOL(drm_agp_unbind
);
337 int drm_agp_unbind_ioctl(struct drm_device
*dev
, void *data
,
338 struct drm_file
*file_priv
)
340 struct drm_agp_binding request
;
342 request
= *(struct drm_agp_binding
*) data
;
344 return drm_agp_unbind(dev
, &request
);
347 static int drm_agp_bind_memory(void *handle
, off_t start
)
351 agpdev
= agp_find_device();
352 if (!agpdev
|| !handle
)
355 return -agp_bind_memory(agpdev
, handle
, start
* PAGE_SIZE
);
359 * Bind AGP memory into the GATT (ioctl)
361 * \param inode device inode.
362 * \param file_priv DRM file private.
363 * \param cmd command.
364 * \param arg pointer to a drm_agp_binding structure.
365 * \return zero on success or a negative number on failure.
367 * Verifies the AGP device is present and has been acquired and that no memory
368 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
369 * it to bind_agp() function.
371 int drm_agp_bind(struct drm_device
*dev
, struct drm_agp_binding
*request
)
373 struct drm_agp_mem
*entry
;
377 if (!dev
->agp
|| !dev
->agp
->acquired
)
380 DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE
);
382 entry
= drm_agp_lookup_entry(dev
, (void *)request
->handle
);
383 if (entry
== NULL
|| entry
->bound
)
386 page
= (request
->offset
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
388 retcode
= drm_agp_bind_memory(entry
->handle
, page
);
390 entry
->bound
= dev
->agp
->base
+ (page
<< PAGE_SHIFT
);
394 EXPORT_SYMBOL(drm_agp_bind
);
397 int drm_agp_bind_ioctl(struct drm_device
*dev
, void *data
,
398 struct drm_file
*file_priv
)
400 struct drm_agp_binding request
;
402 request
= *(struct drm_agp_binding
*) data
;
404 return drm_agp_bind(dev
, &request
);
407 static int drm_agp_free_memory(void *handle
)
411 agpdev
= agp_find_device();
412 if (!agpdev
|| !handle
)
415 agp_free_memory(agpdev
, handle
);
420 * Free AGP memory (ioctl).
422 * \param inode device inode.
423 * \param file_priv DRM file private.
424 * \param cmd command.
425 * \param arg pointer to a drm_agp_buffer structure.
426 * \return zero on success or a negative number on failure.
428 * Verifies the AGP device is present and has been acquired and looks up the
429 * AGP memory entry. If the memory it's currently bound, unbind it via
430 * unbind_agp(). Frees it via free_agp() as well as the entry itself
431 * and unlinks from the doubly linked list it's inserted in.
433 int drm_agp_free(struct drm_device
*dev
, struct drm_agp_buffer
*request
)
435 struct drm_agp_mem
*entry
;
437 if (!dev
->agp
|| !dev
->agp
->acquired
)
439 entry
= drm_agp_lookup_entry(dev
, (void*)request
->handle
);
444 entry
->prev
->next
= entry
->next
;
446 dev
->agp
->memory
= entry
->next
;
448 entry
->next
->prev
= entry
->prev
;
451 drm_agp_unbind_memory(entry
->handle
);
453 drm_agp_free_memory(entry
->handle
);
457 EXPORT_SYMBOL(drm_agp_free
);
461 int drm_agp_free_ioctl(struct drm_device
*dev
, void *data
,
462 struct drm_file
*file_priv
)
464 struct drm_agp_buffer request
;
466 request
= *(struct drm_agp_buffer
*) data
;
468 return drm_agp_free(dev
, &request
);
472 * Initialize the AGP resources.
474 * \return pointer to a drm_agp_head structure.
476 * Gets the drm_agp_t structure which is made available by the agpgart module
477 * via the inter_module_* functions. Creates and initializes a drm_agp_head
480 * Note that final cleanup of the kmalloced structure is directly done in
481 * drm_pci_agp_destroy.
483 struct drm_agp_head
*drm_agp_init(struct drm_device
*dev
)
486 struct drm_agp_head
*head
= NULL
;
487 int agp_available
= 1;
489 agpdev
= agp_find_device();
493 DRM_DEBUG("agp_available = %d\n", agp_available
);
496 head
= kmalloc(sizeof(*head
), M_DRM
,
497 M_WAITOK
| M_NULLOK
| M_ZERO
);
500 head
->agpdev
= agpdev
;
501 agp_get_info(agpdev
, &head
->agp_info
);
502 head
->base
= head
->agp_info
.ai_aperture_base
;
504 DRM_INFO("AGP at 0x%08lx %dMB\n",
505 (long)head
->agp_info
.ai_aperture_base
,
506 (int)(head
->agp_info
.ai_aperture_size
>> 20));
512 * drm_agp_clear - Clear AGP resource list
515 * Iterate over all AGP resources and remove them. But keep the AGP head
516 * intact so it can still be used. It is safe to call this if AGP is disabled or
517 * was already removed.
519 * If DRIVER_MODESET is active, nothing is done to protect the modesetting
520 * resources from getting destroyed. Drivers are responsible of cleaning them up
521 * during device shutdown.
523 void drm_agp_clear(struct drm_device
*dev
)
525 struct drm_agp_mem
*entry
, *nexte
;
529 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
532 /* Remove AGP resources, but leave dev->agp intact until
533 * drm_unload is called.
535 for (entry
= dev
->agp
->memory
; entry
; entry
= nexte
) {
538 drm_agp_unbind_memory(entry
->handle
);
539 drm_agp_free_memory(entry
->handle
);
542 dev
->agp
->memory
= NULL
;
544 if (dev
->agp
->acquired
)
545 drm_agp_release(dev
);
547 dev
->agp
->acquired
= 0;
548 dev
->agp
->enabled
= 0;
551 #endif /* __OS_HAS_AGP */