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>
40 * Get AGP information.
42 * \param inode device inode.
43 * \param filp file pointer.
45 * \param arg pointer to a (output) drm_agp_info structure.
46 * \return zero on success or a negative number on failure.
48 * Verifies the AGP device has been initialized and acquired and fills in the
49 * drm_agp_info structure with the information in drm_agp_head::agp_info.
51 int drm_agp_info(drm_device_t
* dev
, drm_agp_info_t
* info
)
55 if (!dev
->agp
|| !dev
->agp
->acquired
)
58 kern
= &dev
->agp
->agp_info
;
59 info
->agp_version_major
= kern
->version
.major
;
60 info
->agp_version_minor
= kern
->version
.minor
;
61 info
->mode
= kern
->mode
;
62 info
->aperture_base
= kern
->aper_base
;
63 info
->aperture_size
= kern
->aper_size
* 1024 * 1024;
64 info
->memory_allowed
= kern
->max_memory
<< PAGE_SHIFT
;
65 info
->memory_used
= kern
->current_memory
<< PAGE_SHIFT
;
66 info
->id_vendor
= kern
->device
->vendor
;
67 info
->id_device
= kern
->device
->device
;
72 EXPORT_SYMBOL(drm_agp_info
);
74 int drm_agp_info_ioctl(struct inode
*inode
, struct file
*filp
,
75 unsigned int cmd
, unsigned long arg
)
77 drm_file_t
*priv
= filp
->private_data
;
78 drm_device_t
*dev
= priv
->head
->dev
;
82 err
= drm_agp_info(dev
, &info
);
86 if (copy_to_user((drm_agp_info_t __user
*) arg
, &info
, sizeof(info
)))
92 * Acquire the AGP device.
94 * \param dev DRM device that is to acquire AGP.
95 * \return zero on success or a negative number on failure.
97 * Verifies the AGP device hasn't been acquired before and calls
98 * \c agp_backend_acquire.
100 int drm_agp_acquire(drm_device_t
* dev
)
104 if (dev
->agp
->acquired
)
106 if (!(dev
->agp
->bridge
= agp_backend_acquire(dev
->pdev
)))
108 dev
->agp
->acquired
= 1;
112 EXPORT_SYMBOL(drm_agp_acquire
);
115 * Acquire the AGP device (ioctl).
117 * \param inode device inode.
118 * \param filp file pointer.
119 * \param cmd command.
120 * \param arg user argument.
121 * \return zero on success or a negative number on failure.
123 * Verifies the AGP device hasn't been acquired before and calls
124 * \c agp_backend_acquire.
126 int drm_agp_acquire_ioctl(struct inode
*inode
, struct file
*filp
,
127 unsigned int cmd
, unsigned long arg
)
129 drm_file_t
*priv
= filp
->private_data
;
131 return drm_agp_acquire((drm_device_t
*) priv
->head
->dev
);
135 * Release the AGP device.
137 * \param dev DRM device that is to release AGP.
138 * \return zero on success or a negative number on failure.
140 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
142 int drm_agp_release(drm_device_t
* dev
)
144 if (!dev
->agp
|| !dev
->agp
->acquired
)
146 agp_backend_release(dev
->agp
->bridge
);
147 dev
->agp
->acquired
= 0;
150 EXPORT_SYMBOL(drm_agp_release
);
152 int drm_agp_release_ioctl(struct inode
*inode
, struct file
*filp
,
153 unsigned int cmd
, unsigned long arg
)
155 drm_file_t
*priv
= filp
->private_data
;
156 drm_device_t
*dev
= priv
->head
->dev
;
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(drm_device_t
* dev
, drm_agp_mode_t mode
)
173 if (!dev
->agp
|| !dev
->agp
->acquired
)
176 dev
->agp
->mode
= mode
.mode
;
177 agp_enable(dev
->agp
->bridge
, mode
.mode
);
178 dev
->agp
->base
= dev
->agp
->agp_info
.aper_base
;
179 dev
->agp
->enabled
= 1;
183 EXPORT_SYMBOL(drm_agp_enable
);
185 int drm_agp_enable_ioctl(struct inode
*inode
, struct file
*filp
,
186 unsigned int cmd
, unsigned long arg
)
188 drm_file_t
*priv
= filp
->private_data
;
189 drm_device_t
*dev
= priv
->head
->dev
;
192 if (copy_from_user(&mode
, (drm_agp_mode_t __user
*) arg
, sizeof(mode
)))
195 return drm_agp_enable(dev
, mode
);
199 * Allocate AGP memory.
201 * \param inode device inode.
202 * \param filp file pointer.
203 * \param cmd command.
204 * \param arg pointer to a drm_agp_buffer structure.
205 * \return zero on success or a negative number on failure.
207 * Verifies the AGP device is present and has been acquired, allocates the
208 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
210 int drm_agp_alloc(drm_device_t
*dev
, drm_agp_buffer_t
*request
)
212 drm_agp_mem_t
*entry
;
217 if (!dev
->agp
|| !dev
->agp
->acquired
)
219 if (!(entry
= drm_alloc(sizeof(*entry
), DRM_MEM_AGPLISTS
)))
222 memset(entry
, 0, sizeof(*entry
));
224 pages
= (request
->size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
225 type
= (u32
) request
->type
;
226 if (!(memory
= drm_alloc_agp(dev
, pages
, type
))) {
227 drm_free(entry
, sizeof(*entry
), DRM_MEM_AGPLISTS
);
231 entry
->handle
= (unsigned long)memory
->key
+ 1;
232 entry
->memory
= memory
;
234 entry
->pages
= pages
;
236 entry
->next
= dev
->agp
->memory
;
237 if (dev
->agp
->memory
)
238 dev
->agp
->memory
->prev
= entry
;
239 dev
->agp
->memory
= entry
;
241 request
->handle
= entry
->handle
;
242 request
->physical
= memory
->physical
;
246 EXPORT_SYMBOL(drm_agp_alloc
);
248 int drm_agp_alloc_ioctl(struct inode
*inode
, struct file
*filp
,
249 unsigned int cmd
, unsigned long arg
)
251 drm_file_t
*priv
= filp
->private_data
;
252 drm_device_t
*dev
= priv
->head
->dev
;
253 drm_agp_buffer_t request
;
254 drm_agp_buffer_t __user
*argp
= (void __user
*)arg
;
257 if (copy_from_user(&request
, argp
, sizeof(request
)))
260 err
= drm_agp_alloc(dev
, &request
);
264 if (copy_to_user(argp
, &request
, sizeof(request
))) {
265 drm_agp_mem_t
*entry
= dev
->agp
->memory
;
267 dev
->agp
->memory
= entry
->next
;
268 dev
->agp
->memory
->prev
= NULL
;
269 drm_free_agp(entry
->memory
, entry
->pages
);
270 drm_free(entry
, sizeof(*entry
), DRM_MEM_AGPLISTS
);
278 * Search for the AGP memory entry associated with a handle.
280 * \param dev DRM device structure.
281 * \param handle AGP memory handle.
282 * \return pointer to the drm_agp_mem structure associated with \p handle.
284 * Walks through drm_agp_head::memory until finding a matching handle.
286 static drm_agp_mem_t
*drm_agp_lookup_entry(drm_device_t
* dev
,
287 unsigned long handle
)
289 drm_agp_mem_t
*entry
;
291 for (entry
= dev
->agp
->memory
; entry
; entry
= entry
->next
) {
292 if (entry
->handle
== handle
)
299 * Unbind AGP memory from the GATT (ioctl).
301 * \param inode device inode.
302 * \param filp file pointer.
303 * \param cmd command.
304 * \param arg pointer to a drm_agp_binding structure.
305 * \return zero on success or a negative number on failure.
307 * Verifies the AGP device is present and acquired, looks-up the AGP memory
308 * entry and passes it to the unbind_agp() function.
310 int drm_agp_unbind(drm_device_t
*dev
, drm_agp_binding_t
*request
)
312 drm_agp_mem_t
*entry
;
315 if (!dev
->agp
|| !dev
->agp
->acquired
)
317 if (!(entry
= drm_agp_lookup_entry(dev
, request
->handle
)))
321 ret
= drm_unbind_agp(entry
->memory
);
326 EXPORT_SYMBOL(drm_agp_unbind
);
328 int drm_agp_unbind_ioctl(struct inode
*inode
, struct file
*filp
,
329 unsigned int cmd
, unsigned long arg
)
331 drm_file_t
*priv
= filp
->private_data
;
332 drm_device_t
*dev
= priv
->head
->dev
;
333 drm_agp_binding_t request
;
336 (&request
, (drm_agp_binding_t __user
*) arg
, sizeof(request
)))
339 return drm_agp_unbind(dev
, &request
);
343 * Bind AGP memory into the GATT (ioctl)
345 * \param inode device inode.
346 * \param filp file pointer.
347 * \param cmd command.
348 * \param arg pointer to a drm_agp_binding structure.
349 * \return zero on success or a negative number on failure.
351 * Verifies the AGP device is present and has been acquired and that no memory
352 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
353 * it to bind_agp() function.
355 int drm_agp_bind(drm_device_t
*dev
, drm_agp_binding_t
*request
)
357 drm_agp_mem_t
*entry
;
361 if (!dev
->agp
|| !dev
->agp
->acquired
)
363 if (!(entry
= drm_agp_lookup_entry(dev
, request
->handle
)))
367 page
= (request
->offset
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
368 if ((retcode
= drm_bind_agp(entry
->memory
, page
)))
370 entry
->bound
= dev
->agp
->base
+ (page
<< PAGE_SHIFT
);
371 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
372 dev
->agp
->base
, entry
->bound
);
375 EXPORT_SYMBOL(drm_agp_bind
);
377 int drm_agp_bind_ioctl(struct inode
*inode
, struct file
*filp
,
378 unsigned int cmd
, unsigned long arg
)
380 drm_file_t
*priv
= filp
->private_data
;
381 drm_device_t
*dev
= priv
->head
->dev
;
382 drm_agp_binding_t request
;
385 (&request
, (drm_agp_binding_t __user
*) arg
, sizeof(request
)))
388 return drm_agp_bind(dev
, &request
);
392 * Free AGP memory (ioctl).
394 * \param inode device inode.
395 * \param filp file pointer.
396 * \param cmd command.
397 * \param arg pointer to a drm_agp_buffer structure.
398 * \return zero on success or a negative number on failure.
400 * Verifies the AGP device is present and has been acquired and looks up the
401 * AGP memory entry. If the memory it's currently bound, unbind it via
402 * unbind_agp(). Frees it via free_agp() as well as the entry itself
403 * and unlinks from the doubly linked list it's inserted in.
405 int drm_agp_free(drm_device_t
*dev
, drm_agp_buffer_t
*request
)
407 drm_agp_mem_t
*entry
;
409 if (!dev
->agp
|| !dev
->agp
->acquired
)
411 if (!(entry
= drm_agp_lookup_entry(dev
, request
->handle
)))
414 drm_unbind_agp(entry
->memory
);
417 entry
->prev
->next
= entry
->next
;
419 dev
->agp
->memory
= entry
->next
;
422 entry
->next
->prev
= entry
->prev
;
424 drm_free_agp(entry
->memory
, entry
->pages
);
425 drm_free(entry
, sizeof(*entry
), DRM_MEM_AGPLISTS
);
428 EXPORT_SYMBOL(drm_agp_free
);
430 int drm_agp_free_ioctl(struct inode
*inode
, struct file
*filp
,
431 unsigned int cmd
, unsigned long arg
)
433 drm_file_t
*priv
= filp
->private_data
;
434 drm_device_t
*dev
= priv
->head
->dev
;
435 drm_agp_buffer_t request
;
438 (&request
, (drm_agp_buffer_t __user
*) arg
, sizeof(request
)))
441 return drm_agp_free(dev
, &request
);
445 * Initialize the AGP resources.
447 * \return pointer to a drm_agp_head structure.
449 * Gets the drm_agp_t structure which is made available by the agpgart module
450 * via the inter_module_* functions. Creates and initializes a drm_agp_head
453 drm_agp_head_t
*drm_agp_init(drm_device_t
* dev
)
455 drm_agp_head_t
*head
= NULL
;
457 if (!(head
= drm_alloc(sizeof(*head
), DRM_MEM_AGPLISTS
)))
459 memset((void *)head
, 0, sizeof(*head
));
460 head
->bridge
= agp_find_bridge(dev
->pdev
);
462 if (!(head
->bridge
= agp_backend_acquire(dev
->pdev
))) {
463 drm_free(head
, sizeof(*head
), DRM_MEM_AGPLISTS
);
466 agp_copy_info(head
->bridge
, &head
->agp_info
);
467 agp_backend_release(head
->bridge
);
469 agp_copy_info(head
->bridge
, &head
->agp_info
);
471 if (head
->agp_info
.chipset
== NOT_SUPPORTED
) {
472 drm_free(head
, sizeof(*head
), DRM_MEM_AGPLISTS
);
476 head
->cant_use_aperture
= head
->agp_info
.cant_use_aperture
;
477 head
->page_mask
= head
->agp_info
.page_mask
;
482 /** Calls agp_allocate_memory() */
483 DRM_AGP_MEM
*drm_agp_allocate_memory(struct agp_bridge_data
* bridge
,
484 size_t pages
, u32 type
)
486 return agp_allocate_memory(bridge
, pages
, type
);
489 /** Calls agp_free_memory() */
490 int drm_agp_free_memory(DRM_AGP_MEM
* handle
)
494 agp_free_memory(handle
);
498 /** Calls agp_bind_memory() */
499 int drm_agp_bind_memory(DRM_AGP_MEM
* handle
, off_t start
)
503 return agp_bind_memory(handle
, start
);
506 /** Calls agp_unbind_memory() */
507 int drm_agp_unbind_memory(DRM_AGP_MEM
* handle
)
511 return agp_unbind_memory(handle
);
514 #endif /* __OS_HAS_AGP */