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 * AGP information ioctl.
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 inode *inode, struct file *filp,
52 unsigned int cmd, unsigned long arg)
54 drm_file_t *priv = filp->private_data;
55 drm_device_t *dev = priv->head->dev;
59 if (!dev->agp || !dev->agp->acquired)
62 kern = &dev->agp->agp_info;
63 info.agp_version_major = kern->version.major;
64 info.agp_version_minor = kern->version.minor;
65 info.mode = kern->mode;
66 info.aperture_base = kern->aper_base;
67 info.aperture_size = kern->aper_size * 1024 * 1024;
68 info.memory_allowed = kern->max_memory << PAGE_SHIFT;
69 info.memory_used = kern->current_memory << PAGE_SHIFT;
70 info.id_vendor = kern->device->vendor;
71 info.id_device = kern->device->device;
73 if (copy_to_user((drm_agp_info_t __user *) arg, &info, sizeof(info)))
79 * Acquire the AGP device (ioctl).
81 * \param inode device inode.
82 * \param filp file pointer.
84 * \param arg user argument.
85 * \return zero on success or a negative number on failure.
87 * Verifies the AGP device hasn't been acquired before and calls
88 * agp_backend_acquire().
90 int drm_agp_acquire(struct inode *inode, struct file *filp,
91 unsigned int cmd, unsigned long arg)
93 drm_file_t *priv = filp->private_data;
94 drm_device_t *dev = priv->head->dev;
95 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
101 if (dev->agp->acquired)
104 if (dev->agp->cant_use_aperture)
107 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
108 if ((retcode = agp_backend_acquire()))
111 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
115 dev->agp->acquired = 1;
120 * Release the AGP device (ioctl).
122 * \param inode device inode.
123 * \param filp file pointer.
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 has been acquired and calls agp_backend_release().
130 int drm_agp_release(struct inode *inode, struct file *filp,
131 unsigned int cmd, unsigned long arg)
133 drm_file_t *priv = filp->private_data;
134 drm_device_t *dev = priv->head->dev;
136 if (!dev->agp || !dev->agp->acquired)
138 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
139 agp_backend_release();
141 agp_backend_release(dev->agp->bridge);
143 dev->agp->acquired = 0;
149 * Release the AGP device.
151 * Calls agp_backend_release().
153 void drm_agp_do_release(drm_device_t *dev)
155 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
156 agp_backend_release();
158 agp_backend_release(dev->agp->bridge);
163 * Enable the AGP bus.
165 * \param inode device inode.
166 * \param filp file pointer.
167 * \param cmd command.
168 * \param arg pointer to a drm_agp_mode structure.
169 * \return zero on success or a negative number on failure.
171 * Verifies the AGP device has been acquired but not enabled, and calls
174 int drm_agp_enable(struct inode *inode, struct file *filp,
175 unsigned int cmd, unsigned long arg)
177 drm_file_t *priv = filp->private_data;
178 drm_device_t *dev = priv->head->dev;
181 if (!dev->agp || !dev->agp->acquired)
184 if (copy_from_user(&mode, (drm_agp_mode_t __user *) arg, sizeof(mode)))
187 dev->agp->mode = mode.mode;
188 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
189 agp_enable(mode.mode);
191 agp_enable(dev->agp->bridge, mode.mode);
193 dev->agp->base = dev->agp->agp_info.aper_base;
194 dev->agp->enabled = 1;
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 inode *inode, struct file *filp,
211 unsigned int cmd, unsigned long arg)
213 drm_file_t *priv = filp->private_data;
214 drm_device_t *dev = priv->head->dev;
215 drm_agp_buffer_t request;
216 drm_agp_mem_t *entry;
220 drm_agp_buffer_t __user *argp = (void __user *)arg;
222 if (!dev->agp || !dev->agp->acquired)
224 if (copy_from_user(&request, argp, sizeof(request)))
226 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
229 memset(entry, 0, sizeof(*entry));
231 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
232 type = (u32) request.type;
233 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
234 if (!(memory = drm_alloc_agp(pages, type))) {
235 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
239 if (!(memory = drm_alloc_agp(dev->agp->bridge, pages, type))) {
240 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
245 entry->handle = (unsigned long)memory->key + 1;
246 entry->memory = memory;
248 entry->pages = pages;
250 entry->next = dev->agp->memory;
251 if (dev->agp->memory)
252 dev->agp->memory->prev = entry;
253 dev->agp->memory = entry;
255 request.handle = entry->handle;
256 request.physical = memory->physical;
258 if (copy_to_user(argp, &request, sizeof(request))) {
259 dev->agp->memory = entry->next;
260 dev->agp->memory->prev = NULL;
261 drm_free_agp(memory, pages);
262 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
269 * Search for the AGP memory entry associated with a handle.
271 * \param dev DRM device structure.
272 * \param handle AGP memory handle.
273 * \return pointer to the drm_agp_mem structure associated with \p handle.
275 * Walks through drm_agp_head::memory until finding a matching handle.
277 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t * dev,
278 unsigned long handle)
280 drm_agp_mem_t *entry;
282 for (entry = dev->agp->memory; entry; entry = entry->next) {
283 if (entry->handle == handle)
290 * Unbind AGP memory from the GATT (ioctl).
292 * \param inode device inode.
293 * \param filp file pointer.
294 * \param cmd command.
295 * \param arg pointer to a drm_agp_binding structure.
296 * \return zero on success or a negative number on failure.
298 * Verifies the AGP device is present and acquired, looks-up the AGP memory
299 * entry and passes it to the unbind_agp() function.
301 int drm_agp_unbind(struct inode *inode, struct file *filp,
302 unsigned int cmd, unsigned long arg)
304 drm_file_t *priv = filp->private_data;
305 drm_device_t *dev = priv->head->dev;
306 drm_agp_binding_t request;
307 drm_agp_mem_t *entry;
310 if (!dev->agp || !dev->agp->acquired)
313 (&request, (drm_agp_binding_t __user *) arg, sizeof(request)))
315 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
319 ret = drm_unbind_agp(entry->memory);
326 * Bind AGP memory into the GATT (ioctl)
328 * \param inode device inode.
329 * \param filp file pointer.
330 * \param cmd command.
331 * \param arg pointer to a drm_agp_binding structure.
332 * \return zero on success or a negative number on failure.
334 * Verifies the AGP device is present and has been acquired and that no memory
335 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
336 * it to bind_agp() function.
338 int drm_agp_bind(struct inode *inode, struct file *filp,
339 unsigned int cmd, unsigned long arg)
341 drm_file_t *priv = filp->private_data;
342 drm_device_t *dev = priv->head->dev;
343 drm_agp_binding_t request;
344 drm_agp_mem_t *entry;
348 if (!dev->agp || !dev->agp->acquired)
351 (&request, (drm_agp_binding_t __user *) arg, sizeof(request)))
353 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
357 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
358 if ((retcode = drm_bind_agp(entry->memory, page)))
360 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
361 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
362 dev->agp->base, entry->bound);
367 * Free AGP memory (ioctl).
369 * \param inode device inode.
370 * \param filp file pointer.
371 * \param cmd command.
372 * \param arg pointer to a drm_agp_buffer structure.
373 * \return zero on success or a negative number on failure.
375 * Verifies the AGP device is present and has been acquired and looks up the
376 * AGP memory entry. If the memory it's currently bound, unbind it via
377 * unbind_agp(). Frees it via free_agp() as well as the entry itself
378 * and unlinks from the doubly linked list it's inserted in.
380 int drm_agp_free(struct inode *inode, struct file *filp,
381 unsigned int cmd, unsigned long arg)
383 drm_file_t *priv = filp->private_data;
384 drm_device_t *dev = priv->head->dev;
385 drm_agp_buffer_t request;
386 drm_agp_mem_t *entry;
388 if (!dev->agp || !dev->agp->acquired)
391 (&request, (drm_agp_buffer_t __user *) arg, sizeof(request)))
393 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
396 drm_unbind_agp(entry->memory);
399 entry->prev->next = entry->next;
401 dev->agp->memory = entry->next;
404 entry->next->prev = entry->prev;
406 drm_free_agp(entry->memory, entry->pages);
407 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
412 * Initialize the AGP resources.
414 * \return pointer to a drm_agp_head structure.
416 * Gets the drm_agp_t structure which is made available by the agpgart module
417 * via the inter_module_* functions. Creates and initializes a drm_agp_head
420 drm_agp_head_t *drm_agp_init(drm_device_t *dev)
422 drm_agp_head_t *head = NULL;
424 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
426 memset((void *)head, 0, sizeof(*head));
428 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
429 agp_copy_info(&head->agp_info);
431 head->bridge = agp_find_bridge(dev->pdev);
433 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
434 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
437 agp_copy_info(head->bridge, &head->agp_info);
438 agp_backend_release(head->bridge);
440 agp_copy_info(head->bridge, &head->agp_info);
443 if (head->agp_info.chipset == NOT_SUPPORTED) {
444 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
448 head->cant_use_aperture = head->agp_info.cant_use_aperture;
449 head->page_mask = head->agp_info.page_mask;
453 /** Calls agp_allocate_memory() */
454 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
455 DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type)
457 return agp_allocate_memory(pages, type);
460 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type)
462 return agp_allocate_memory(bridge, pages, type);
466 /** Calls agp_free_memory() */
467 int drm_agp_free_memory(DRM_AGP_MEM * handle)
471 agp_free_memory(handle);
475 /** Calls agp_bind_memory() */
476 int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
480 return agp_bind_memory(handle, start);
483 /** Calls agp_unbind_memory() */
484 int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
488 return agp_unbind_memory(handle);
491 #endif /* __OS_HAS_AGP */