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(struct drm_device *dev, struct drm_agp_info *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 struct drm_file *priv = filp->private_data;
78 struct drm_device *dev = priv->head->dev;
79 struct drm_agp_info info;
82 err = drm_agp_info(dev, &info);
86 if (copy_to_user((struct drm_agp_info __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(struct drm_device * 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 struct drm_file *priv = filp->private_data;
131 return drm_agp_acquire((struct drm_device *) 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(struct drm_device * 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 struct drm_file *priv = filp->private_data;
156 struct drm_device *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(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->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 struct drm_file *priv = filp->private_data;
189 struct drm_device *dev = priv->head->dev;
190 struct drm_agp_mode mode;
192 if (copy_from_user(&mode, (struct drm_agp_mode __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(struct drm_device *dev, struct drm_agp_buffer *request)
212 struct drm_agp_mem *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;
235 list_add(&entry->head, &dev->agp->memory);
237 request->handle = entry->handle;
238 request->physical = memory->physical;
242 EXPORT_SYMBOL(drm_agp_alloc);
244 int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp,
245 unsigned int cmd, unsigned long arg)
247 struct drm_file *priv = filp->private_data;
248 struct drm_device *dev = priv->head->dev;
249 struct drm_agp_buffer request;
250 struct drm_agp_buffer __user *argp = (void __user *)arg;
253 if (copy_from_user(&request, argp, sizeof(request)))
256 err = drm_agp_alloc(dev, &request);
260 if (copy_to_user(argp, &request, sizeof(request))) {
261 struct drm_agp_mem *entry;
262 list_for_each_entry(entry, &dev->agp->memory, head) {
263 if (entry->handle == request.handle)
266 list_del(&entry->head);
267 drm_free_agp(entry->memory, entry->pages);
268 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
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,
285 unsigned long handle)
287 struct drm_agp_mem *entry;
289 list_for_each_entry(entry, &dev->agp->memory, head) {
290 if (entry->handle == handle)
297 * Unbind AGP memory from the GATT (ioctl).
299 * \param inode device inode.
300 * \param filp file pointer.
301 * \param cmd command.
302 * \param arg pointer to a drm_agp_binding structure.
303 * \return zero on success or a negative number on failure.
305 * Verifies the AGP device is present and acquired, looks-up the AGP memory
306 * entry and passes it to the unbind_agp() function.
308 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
310 struct drm_agp_mem *entry;
313 if (!dev->agp || !dev->agp->acquired)
315 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
319 ret = drm_unbind_agp(entry->memory);
324 EXPORT_SYMBOL(drm_agp_unbind);
326 int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp,
327 unsigned int cmd, unsigned long arg)
329 struct drm_file *priv = filp->private_data;
330 struct drm_device *dev = priv->head->dev;
331 struct drm_agp_binding request;
334 (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
337 return drm_agp_unbind(dev, &request);
341 * Bind AGP memory into the GATT (ioctl)
343 * \param inode device inode.
344 * \param filp file pointer.
345 * \param cmd command.
346 * \param arg pointer to a drm_agp_binding structure.
347 * \return zero on success or a negative number on failure.
349 * Verifies the AGP device is present and has been acquired and that no memory
350 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
351 * it to bind_agp() function.
353 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
355 struct drm_agp_mem *entry;
359 if (!dev->agp || !dev->agp->acquired)
361 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
365 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
366 if ((retcode = drm_bind_agp(entry->memory, page)))
368 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
369 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
370 dev->agp->base, entry->bound);
373 EXPORT_SYMBOL(drm_agp_bind);
375 int drm_agp_bind_ioctl(struct inode *inode, struct file *filp,
376 unsigned int cmd, unsigned long arg)
378 struct drm_file *priv = filp->private_data;
379 struct drm_device *dev = priv->head->dev;
380 struct drm_agp_binding request;
383 (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
386 return drm_agp_bind(dev, &request);
390 * Free AGP memory (ioctl).
392 * \param inode device inode.
393 * \param filp file pointer.
394 * \param cmd command.
395 * \param arg pointer to a drm_agp_buffer structure.
396 * \return zero on success or a negative number on failure.
398 * Verifies the AGP device is present and has been acquired and looks up the
399 * AGP memory entry. If the memory it's currently bound, unbind it via
400 * unbind_agp(). Frees it via free_agp() as well as the entry itself
401 * and unlinks from the doubly linked list it's inserted in.
403 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
405 struct drm_agp_mem *entry;
407 if (!dev->agp || !dev->agp->acquired)
409 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
412 drm_unbind_agp(entry->memory);
414 list_del(&entry->head);
416 drm_free_agp(entry->memory, entry->pages);
417 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
420 EXPORT_SYMBOL(drm_agp_free);
422 int drm_agp_free_ioctl(struct inode *inode, struct file *filp,
423 unsigned int cmd, unsigned long arg)
425 struct drm_file *priv = filp->private_data;
426 struct drm_device *dev = priv->head->dev;
427 struct drm_agp_buffer request;
430 (&request, (struct drm_agp_buffer __user *) arg, sizeof(request)))
433 return drm_agp_free(dev, &request);
437 * Initialize the AGP resources.
439 * \return pointer to a drm_agp_head structure.
441 * Gets the drm_agp_t structure which is made available by the agpgart module
442 * via the inter_module_* functions. Creates and initializes a drm_agp_head
445 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
447 struct drm_agp_head *head = NULL;
449 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
451 memset((void *)head, 0, sizeof(*head));
452 head->bridge = agp_find_bridge(dev->pdev);
454 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
455 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
458 agp_copy_info(head->bridge, &head->agp_info);
459 agp_backend_release(head->bridge);
461 agp_copy_info(head->bridge, &head->agp_info);
463 if (head->agp_info.chipset == NOT_SUPPORTED) {
464 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
467 INIT_LIST_HEAD(&head->memory);
468 head->cant_use_aperture = head->agp_info.cant_use_aperture;
469 head->page_mask = head->agp_info.page_mask;
474 /** Calls agp_allocate_memory() */
475 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge,
476 size_t pages, u32 type)
478 return agp_allocate_memory(bridge, pages, type);
481 /** Calls agp_free_memory() */
482 int drm_agp_free_memory(DRM_AGP_MEM * handle)
486 agp_free_memory(handle);
490 /** Calls agp_bind_memory() */
491 int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
495 return agp_bind_memory(handle, start);
498 /** Calls agp_unbind_memory() */
499 int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
503 return agp_unbind_memory(handle);
506 #endif /* __OS_HAS_AGP */