drm: make drm_alloc_agp take a dev arg.
[linux-2.6/verdex.git] / drivers / char / drm / drm_agpsupport.c
blobffb4acaefe847cfd5394ad23a4252b6788648a7b
1 /**
2 * \file drm_agpsupport.h
3 * DRM support for AGP/GART backend
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
9 /*
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
23 * Software.
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.
34 #include "drmP.h"
35 #include <linux/module.h>
37 #if __OS_HAS_AGP
39 /**
40 * Get AGP information.
42 * \param inode device inode.
43 * \param filp file pointer.
44 * \param cmd command.
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)
53 DRM_AGP_KERN *kern;
55 if (!dev->agp || !dev->agp->acquired)
56 return -EINVAL;
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;
69 return 0;
71 EXPORT_SYMBOL(drm_agp_info);
73 int drm_agp_info_ioctl(struct inode *inode, struct file *filp,
74 unsigned int cmd, unsigned long arg)
76 drm_file_t *priv = filp->private_data;
77 drm_device_t *dev = priv->head->dev;
78 drm_agp_info_t info;
79 int err;
81 err = drm_agp_info(dev, &info);
82 if (err)
83 return err;
85 if (copy_to_user((drm_agp_info_t __user *) arg, &info, sizeof(info)))
86 return -EFAULT;
87 return 0;
90 /**
91 * Acquire the AGP device.
93 * \param dev DRM device that is to acquire AGP
94 * \return zero on success or a negative number on failure.
96 * Verifies the AGP device hasn't been acquired before and calls
97 * \c agp_backend_acquire.
99 int drm_agp_acquire(drm_device_t *dev)
101 if (!dev->agp)
102 return -ENODEV;
103 if (dev->agp->acquired)
104 return -EBUSY;
105 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
106 return -ENODEV;
107 dev->agp->acquired = 1;
108 return 0;
110 EXPORT_SYMBOL(drm_agp_acquire);
113 * Acquire the AGP device (ioctl).
115 * \param inode device inode.
116 * \param filp file pointer.
117 * \param cmd command.
118 * \param arg user argument.
119 * \return zero on success or a negative number on failure.
121 * Verifies the AGP device hasn't been acquired before and calls
122 * \c agp_backend_acquire.
124 int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp,
125 unsigned int cmd, unsigned long arg)
127 drm_file_t *priv = filp->private_data;
129 return drm_agp_acquire( (drm_device_t *) priv->head->dev );
133 * Release the AGP device.
135 * \param dev DRM device that is to release AGP
136 * \return zero on success or a negative number on failure.
138 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
140 int drm_agp_release(drm_device_t *dev)
142 if (!dev->agp || !dev->agp->acquired)
143 return -EINVAL;
144 agp_backend_release(dev->agp->bridge);
145 dev->agp->acquired = 0;
146 return 0;
148 EXPORT_SYMBOL(drm_agp_release);
150 int drm_agp_release_ioctl(struct inode *inode, struct file *filp,
151 unsigned int cmd, unsigned long arg)
153 drm_file_t *priv = filp->private_data;
154 drm_device_t *dev = priv->head->dev;
156 return drm_agp_release(dev);
160 * Enable the AGP bus.
162 * \param dev DRM device that has previously acquired AGP.
163 * \param mode Requested AGP mode.
164 * \return zero on success or a negative number on failure.
166 * Verifies the AGP device has been acquired but not enabled, and calls
167 * \c agp_enable.
169 int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode)
171 if (!dev->agp || !dev->agp->acquired)
172 return -EINVAL;
174 dev->agp->mode = mode.mode;
175 agp_enable(dev->agp->bridge, mode.mode);
176 dev->agp->base = dev->agp->agp_info.aper_base;
177 dev->agp->enabled = 1;
178 return 0;
180 EXPORT_SYMBOL(drm_agp_enable);
182 int drm_agp_enable_ioctl(struct inode *inode, struct file *filp,
183 unsigned int cmd, unsigned long arg)
185 drm_file_t *priv = filp->private_data;
186 drm_device_t *dev = priv->head->dev;
187 drm_agp_mode_t mode;
190 if (copy_from_user(&mode, (drm_agp_mode_t __user *) arg, sizeof(mode)))
191 return -EFAULT;
193 return drm_agp_enable(dev, mode);
197 * Allocate AGP memory.
199 * \param inode device inode.
200 * \param filp file pointer.
201 * \param cmd command.
202 * \param arg pointer to a drm_agp_buffer structure.
203 * \return zero on success or a negative number on failure.
205 * Verifies the AGP device is present and has been acquired, allocates the
206 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
208 int drm_agp_alloc(struct inode *inode, struct file *filp,
209 unsigned int cmd, unsigned long arg)
211 drm_file_t *priv = filp->private_data;
212 drm_device_t *dev = priv->head->dev;
213 drm_agp_buffer_t request;
214 drm_agp_mem_t *entry;
215 DRM_AGP_MEM *memory;
216 unsigned long pages;
217 u32 type;
218 drm_agp_buffer_t __user *argp = (void __user *)arg;
220 if (!dev->agp || !dev->agp->acquired)
221 return -EINVAL;
222 if (copy_from_user(&request, argp, sizeof(request)))
223 return -EFAULT;
224 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
225 return -ENOMEM;
227 memset(entry, 0, sizeof(*entry));
229 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
230 type = (u32) request.type;
232 if (!(memory = drm_alloc_agp(dev, pages, type))) {
233 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
234 return -ENOMEM;
237 entry->handle = (unsigned long)memory->key + 1;
238 entry->memory = memory;
239 entry->bound = 0;
240 entry->pages = pages;
241 entry->prev = NULL;
242 entry->next = dev->agp->memory;
243 if (dev->agp->memory)
244 dev->agp->memory->prev = entry;
245 dev->agp->memory = entry;
247 request.handle = entry->handle;
248 request.physical = memory->physical;
250 if (copy_to_user(argp, &request, sizeof(request))) {
251 dev->agp->memory = entry->next;
252 dev->agp->memory->prev = NULL;
253 drm_free_agp(memory, pages);
254 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
255 return -EFAULT;
257 return 0;
261 * Search for the AGP memory entry associated with a handle.
263 * \param dev DRM device structure.
264 * \param handle AGP memory handle.
265 * \return pointer to the drm_agp_mem structure associated with \p handle.
267 * Walks through drm_agp_head::memory until finding a matching handle.
269 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
270 unsigned long handle)
272 drm_agp_mem_t *entry;
274 for (entry = dev->agp->memory; entry; entry = entry->next) {
275 if (entry->handle == handle)
276 return entry;
278 return NULL;
282 * Unbind AGP memory from the GATT (ioctl).
284 * \param inode device inode.
285 * \param filp file pointer.
286 * \param cmd command.
287 * \param arg pointer to a drm_agp_binding structure.
288 * \return zero on success or a negative number on failure.
290 * Verifies the AGP device is present and acquired, looks-up the AGP memory
291 * entry and passes it to the unbind_agp() function.
293 int drm_agp_unbind(struct inode *inode, struct file *filp,
294 unsigned int cmd, unsigned long arg)
296 drm_file_t *priv = filp->private_data;
297 drm_device_t *dev = priv->head->dev;
298 drm_agp_binding_t request;
299 drm_agp_mem_t *entry;
300 int ret;
302 if (!dev->agp || !dev->agp->acquired)
303 return -EINVAL;
304 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
305 return -EFAULT;
306 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
307 return -EINVAL;
308 if (!entry->bound)
309 return -EINVAL;
310 ret = drm_unbind_agp(entry->memory);
311 if (ret == 0)
312 entry->bound = 0;
313 return ret;
317 * Bind AGP memory into the GATT (ioctl)
319 * \param inode device inode.
320 * \param filp file pointer.
321 * \param cmd command.
322 * \param arg pointer to a drm_agp_binding structure.
323 * \return zero on success or a negative number on failure.
325 * Verifies the AGP device is present and has been acquired and that no memory
326 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
327 * it to bind_agp() function.
329 int drm_agp_bind(struct inode *inode, struct file *filp,
330 unsigned int cmd, unsigned long arg)
332 drm_file_t *priv = filp->private_data;
333 drm_device_t *dev = priv->head->dev;
334 drm_agp_binding_t request;
335 drm_agp_mem_t *entry;
336 int retcode;
337 int page;
339 if (!dev->agp || !dev->agp->acquired)
340 return -EINVAL;
341 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
342 return -EFAULT;
343 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
344 return -EINVAL;
345 if (entry->bound)
346 return -EINVAL;
347 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
348 if ((retcode = drm_bind_agp(entry->memory, page)))
349 return retcode;
350 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
351 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
352 dev->agp->base, entry->bound);
353 return 0;
357 * Free AGP memory (ioctl).
359 * \param inode device inode.
360 * \param filp file pointer.
361 * \param cmd command.
362 * \param arg pointer to a drm_agp_buffer structure.
363 * \return zero on success or a negative number on failure.
365 * Verifies the AGP device is present and has been acquired and looks up the
366 * AGP memory entry. If the memory it's currently bound, unbind it via
367 * unbind_agp(). Frees it via free_agp() as well as the entry itself
368 * and unlinks from the doubly linked list it's inserted in.
370 int drm_agp_free(struct inode *inode, struct file *filp,
371 unsigned int cmd, unsigned long arg)
373 drm_file_t *priv = filp->private_data;
374 drm_device_t *dev = priv->head->dev;
375 drm_agp_buffer_t request;
376 drm_agp_mem_t *entry;
378 if (!dev->agp || !dev->agp->acquired)
379 return -EINVAL;
380 if (copy_from_user(&request, (drm_agp_buffer_t __user *)arg, sizeof(request)))
381 return -EFAULT;
382 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
383 return -EINVAL;
384 if (entry->bound)
385 drm_unbind_agp(entry->memory);
387 if (entry->prev)
388 entry->prev->next = entry->next;
389 else
390 dev->agp->memory = entry->next;
392 if (entry->next)
393 entry->next->prev = entry->prev;
395 drm_free_agp(entry->memory, entry->pages);
396 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
397 return 0;
401 * Initialize the AGP resources.
403 * \return pointer to a drm_agp_head structure.
406 drm_agp_head_t *drm_agp_init(drm_device_t *dev)
408 drm_agp_head_t *head = NULL;
410 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
411 return NULL;
412 memset((void *)head, 0, sizeof(*head));
413 head->bridge = agp_find_bridge(dev->pdev);
414 if (!head->bridge) {
415 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
416 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
417 return NULL;
419 agp_copy_info(head->bridge, &head->agp_info);
420 agp_backend_release(head->bridge);
421 } else {
422 agp_copy_info(head->bridge, &head->agp_info);
424 if (head->agp_info.chipset == NOT_SUPPORTED) {
425 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
426 return NULL;
428 head->memory = NULL;
429 #if LINUX_VERSION_CODE <= 0x020408
430 head->cant_use_aperture = 0;
431 head->page_mask = ~(0xfff);
432 #else
433 head->cant_use_aperture = head->agp_info.cant_use_aperture;
434 head->page_mask = head->agp_info.page_mask;
435 #endif
437 return head;
440 /** Calls agp_allocate_memory() */
441 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type)
443 return agp_allocate_memory(bridge, pages, type);
446 /** Calls agp_free_memory() */
447 int drm_agp_free_memory(DRM_AGP_MEM *handle)
449 if (!handle)
450 return 0;
451 agp_free_memory(handle);
452 return 1;
455 /** Calls agp_bind_memory() */
456 int drm_agp_bind_memory(DRM_AGP_MEM *handle, off_t start)
458 if (!handle)
459 return -EINVAL;
460 return agp_bind_memory(handle, start);
462 EXPORT_SYMBOL(drm_agp_bind_memory);
464 /** Calls agp_unbind_memory() */
465 int drm_agp_unbind_memory(DRM_AGP_MEM *handle)
467 if (!handle)
468 return -EINVAL;
469 return agp_unbind_memory(handle);
472 #endif /* __OS_HAS_AGP */