2 * \file drm_agpsupport.h
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 * Pointer to the drm_agp_t structure made available by the agpgart module.
42 const drm_agp_t *drm_agp = NULL;
45 * AGP information ioctl.
47 * \param inode device inode.
48 * \param filp file pointer.
50 * \param arg pointer to a (output) drm_agp_info structure.
51 * \return zero on success or a negative number on failure.
53 * Verifies the AGP device has been initialized and acquired and fills in the
54 * drm_agp_info structure with the information in drm_agp_head::agp_info.
56 int drm_agp_info(struct inode *inode, struct file *filp,
57 unsigned int cmd, unsigned long arg)
59 drm_file_t *priv = filp->private_data;
60 drm_device_t *dev = priv->dev;
64 if (!dev->agp || !dev->agp->acquired || !drm_agp->copy_info)
67 kern = &dev->agp->agp_info;
68 info.agp_version_major = kern->version.major;
69 info.agp_version_minor = kern->version.minor;
70 info.mode = kern->mode;
71 info.aperture_base = kern->aper_base;
72 info.aperture_size = kern->aper_size * 1024 * 1024;
73 info.memory_allowed = kern->max_memory << PAGE_SHIFT;
74 info.memory_used = kern->current_memory << PAGE_SHIFT;
75 info.id_vendor = kern->device->vendor;
76 info.id_device = kern->device->device;
78 if (copy_to_user((drm_agp_info_t __user *)arg, &info, sizeof(info)))
84 * Acquire the AGP device (ioctl).
86 * \param inode device inode.
87 * \param filp file pointer.
89 * \param arg user argument.
90 * \return zero on success or a negative number on failure.
92 * Verifies the AGP device hasn't been acquired before and calls
95 int drm_agp_acquire(struct inode *inode, struct file *filp,
96 unsigned int cmd, unsigned long arg)
98 drm_file_t *priv = filp->private_data;
99 drm_device_t *dev = priv->dev;
104 if (dev->agp->acquired)
106 if (!drm_agp->acquire)
109 if ( dev->agp->cant_use_aperture )
112 if ((retcode = drm_agp->acquire()))
114 dev->agp->acquired = 1;
119 * Release the AGP device (ioctl).
121 * \param inode device inode.
122 * \param filp file pointer.
123 * \param cmd command.
124 * \param arg user argument.
125 * \return zero on success or a negative number on failure.
127 * Verifies the AGP device has been acquired and calls drm_agp->release().
129 int drm_agp_release(struct inode *inode, struct file *filp,
130 unsigned int cmd, unsigned long arg)
132 drm_file_t *priv = filp->private_data;
133 drm_device_t *dev = priv->dev;
135 if (!dev->agp || !dev->agp->acquired || !drm_agp->release)
138 dev->agp->acquired = 0;
144 * Release the AGP device.
146 * Calls drm_agp->release().
148 void drm_agp_do_release(void)
150 if (drm_agp->release)
155 * Enable the AGP bus.
157 * \param inode device inode.
158 * \param filp file pointer.
159 * \param cmd command.
160 * \param arg pointer to a drm_agp_mode structure.
161 * \return zero on success or a negative number on failure.
163 * Verifies the AGP device has been acquired but not enabled, and calls
166 int drm_agp_enable(struct inode *inode, struct file *filp,
167 unsigned int cmd, unsigned long arg)
169 drm_file_t *priv = filp->private_data;
170 drm_device_t *dev = priv->dev;
173 if (!dev->agp || !dev->agp->acquired || !drm_agp->enable)
176 if (copy_from_user(&mode, (drm_agp_mode_t __user *)arg, sizeof(mode)))
179 dev->agp->mode = mode.mode;
180 drm_agp->enable(mode.mode);
181 dev->agp->base = dev->agp->agp_info.aper_base;
182 dev->agp->enabled = 1;
187 * Allocate AGP memory.
189 * \param inode device inode.
190 * \param filp file pointer.
191 * \param cmd command.
192 * \param arg pointer to a drm_agp_buffer structure.
193 * \return zero on success or a negative number on failure.
195 * Verifies the AGP device is present and has been acquired, allocates the
196 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
198 int drm_agp_alloc(struct inode *inode, struct file *filp,
199 unsigned int cmd, unsigned long arg)
201 drm_file_t *priv = filp->private_data;
202 drm_device_t *dev = priv->dev;
203 drm_agp_buffer_t request;
204 drm_agp_mem_t *entry;
208 drm_agp_buffer_t __user *argp = (void __user *)arg;
210 if (!dev->agp || !dev->agp->acquired)
212 if (copy_from_user(&request, argp, sizeof(request)))
214 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
217 memset(entry, 0, sizeof(*entry));
219 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
220 type = (u32) request.type;
222 if (!(memory = drm_alloc_agp(pages, type))) {
223 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
227 entry->handle = (unsigned long)memory->key + 1;
228 entry->memory = memory;
230 entry->pages = pages;
232 entry->next = dev->agp->memory;
233 if (dev->agp->memory)
234 dev->agp->memory->prev = entry;
235 dev->agp->memory = entry;
237 request.handle = entry->handle;
238 request.physical = memory->physical;
240 if (copy_to_user(argp, &request, sizeof(request))) {
241 dev->agp->memory = entry->next;
242 dev->agp->memory->prev = NULL;
243 drm_free_agp(memory, pages);
244 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
251 * Search for the AGP memory entry associated with a handle.
253 * \param dev DRM device structure.
254 * \param handle AGP memory handle.
255 * \return pointer to the drm_agp_mem structure associated with \p handle.
257 * Walks through drm_agp_head::memory until finding a matching handle.
259 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
260 unsigned long handle)
262 drm_agp_mem_t *entry;
264 for (entry = dev->agp->memory; entry; entry = entry->next) {
265 if (entry->handle == handle)
272 * Unbind AGP memory from the GATT (ioctl).
274 * \param inode device inode.
275 * \param filp file pointer.
276 * \param cmd command.
277 * \param arg pointer to a drm_agp_binding structure.
278 * \return zero on success or a negative number on failure.
280 * Verifies the AGP device is present and acquired, looks-up the AGP memory
281 * entry and passes it to the unbind_agp() function.
283 int drm_agp_unbind(struct inode *inode, struct file *filp,
284 unsigned int cmd, unsigned long arg)
286 drm_file_t *priv = filp->private_data;
287 drm_device_t *dev = priv->dev;
288 drm_agp_binding_t request;
289 drm_agp_mem_t *entry;
292 if (!dev->agp || !dev->agp->acquired)
294 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
296 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
300 ret = drm_unbind_agp(entry->memory);
307 * Bind AGP memory into the GATT (ioctl)
309 * \param inode device inode.
310 * \param filp file pointer.
311 * \param cmd command.
312 * \param arg pointer to a drm_agp_binding structure.
313 * \return zero on success or a negative number on failure.
315 * Verifies the AGP device is present and has been acquired and that no memory
316 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
317 * it to bind_agp() function.
319 int drm_agp_bind(struct inode *inode, struct file *filp,
320 unsigned int cmd, unsigned long arg)
322 drm_file_t *priv = filp->private_data;
323 drm_device_t *dev = priv->dev;
324 drm_agp_binding_t request;
325 drm_agp_mem_t *entry;
329 if (!dev->agp || !dev->agp->acquired || !drm_agp->bind_memory)
331 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
333 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
337 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
338 if ((retcode = drm_bind_agp(entry->memory, page)))
340 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
341 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
342 dev->agp->base, entry->bound);
347 * Free AGP memory (ioctl).
349 * \param inode device inode.
350 * \param filp file pointer.
351 * \param cmd command.
352 * \param arg pointer to a drm_agp_buffer structure.
353 * \return zero on success or a negative number on failure.
355 * Verifies the AGP device is present and has been acquired and looks up the
356 * AGP memory entry. If the memory it's currently bound, unbind it via
357 * unbind_agp(). Frees it via free_agp() as well as the entry itself
358 * and unlinks from the doubly linked list it's inserted in.
360 int drm_agp_free(struct inode *inode, struct file *filp,
361 unsigned int cmd, unsigned long arg)
363 drm_file_t *priv = filp->private_data;
364 drm_device_t *dev = priv->dev;
365 drm_agp_buffer_t request;
366 drm_agp_mem_t *entry;
368 if (!dev->agp || !dev->agp->acquired)
370 if (copy_from_user(&request, (drm_agp_buffer_t __user *)arg, sizeof(request)))
372 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
375 drm_unbind_agp(entry->memory);
378 entry->prev->next = entry->next;
380 dev->agp->memory = entry->next;
383 entry->next->prev = entry->prev;
385 drm_free_agp(entry->memory, entry->pages);
386 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
391 * Initialize the AGP resources.
393 * \return pointer to a drm_agp_head structure.
395 * Gets the drm_agp_t structure which is made available by the agpgart module
396 * via the inter_module_* functions. Creates and initializes a drm_agp_head
399 drm_agp_head_t *drm_agp_init(void)
401 drm_agp_head_t *head = NULL;
404 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
406 memset((void *)head, 0, sizeof(*head));
407 drm_agp->copy_info(&head->agp_info);
408 if (head->agp_info.chipset == NOT_SUPPORTED) {
409 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
413 #if LINUX_VERSION_CODE <= 0x020408
414 head->cant_use_aperture = 0;
415 head->page_mask = ~(0xfff);
417 head->cant_use_aperture = head->agp_info.cant_use_aperture;
418 head->page_mask = head->agp_info.page_mask;
424 /** Calls drm_agp->allocate_memory() */
425 DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type)
427 if (!drm_agp->allocate_memory)
429 return drm_agp->allocate_memory(pages, type);
432 /** Calls drm_agp->free_memory() */
433 int drm_agp_free_memory(DRM_AGP_MEM *handle)
435 if (!handle || !drm_agp->free_memory)
437 drm_agp->free_memory(handle);
441 /** Calls drm_agp->bind_memory() */
442 int drm_agp_bind_memory(DRM_AGP_MEM *handle, off_t start)
444 if (!handle || !drm_agp->bind_memory)
446 return drm_agp->bind_memory(handle, start);
449 /** Calls drm_agp->unbind_memory() */
450 int drm_agp_unbind_memory(DRM_AGP_MEM *handle)
452 if (!handle || !drm_agp->unbind_memory)
454 return drm_agp->unbind_memory(handle);
457 #endif /* __OS_HAS_AGP */