blob: 6d7a69abdc628adcdd10f555f90d48e44a238477 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlied985c102006-01-02 21:32:48 +11002 * \file drm_agpsupport.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * DRM support for AGP/GART backend
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
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:
20 *
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.
24 *
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.
32 */
33
34#include "drmP.h"
35#include <linux/module.h>
36
37#if __OS_HAS_AGP
38
39/**
Dave Airlie7ab98402005-07-10 16:56:52 +100040 * Get AGP information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 *
42 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100043 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * \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.
47 *
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.
50 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100051int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 DRM_AGP_KERN *kern;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 if (!dev->agp || !dev->agp->acquired)
56 return -EINVAL;
57
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 kern = &dev->agp->agp_info;
Dave Airlie7ab98402005-07-10 16:56:52 +100059 info->agp_version_major = kern->version.major;
60 info->agp_version_minor = kern->version.minor;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dave Airlie7ab98402005-07-10 16:56:52 +100069 return 0;
70}
Dave Airlieb5e89ed2005-09-25 14:28:13 +100071
Dave Airlie7ab98402005-07-10 16:56:52 +100072EXPORT_SYMBOL(drm_agp_info);
73
Eric Anholt6c340ea2007-08-25 20:23:09 +100074int drm_agp_info_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075 unsigned int cmd, unsigned long arg)
Dave Airlie7ab98402005-07-10 16:56:52 +100076{
Eric Anholt6c340ea2007-08-25 20:23:09 +100077 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +100078 struct drm_agp_info info;
Dave Airlie7ab98402005-07-10 16:56:52 +100079 int err;
80
81 err = drm_agp_info(dev, &info);
82 if (err)
83 return err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084
Dave Airliec60ce622007-07-11 15:27:12 +100085 if (copy_to_user((struct drm_agp_info __user *) arg, &info, sizeof(info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -EFAULT;
87 return 0;
88}
89
90/**
Dave Airlie7ab98402005-07-10 16:56:52 +100091 * Acquire the AGP device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Dave Airlied985c102006-01-02 21:32:48 +110093 * \param dev DRM device that is to acquire AGP.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100094 * \return zero on success or a negative number on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
96 * Verifies the AGP device hasn't been acquired before and calls
Dave Airlie7ab98402005-07-10 16:56:52 +100097 * \c agp_backend_acquire.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100099int drm_agp_acquire(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 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;
109}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110
Dave Airlie7ab98402005-07-10 16:56:52 +1000111EXPORT_SYMBOL(drm_agp_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/**
Dave Airlie7ab98402005-07-10 16:56:52 +1000114 * Acquire the AGP device (ioctl).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
116 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000117 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * \param cmd command.
119 * \param arg user argument.
120 * \return zero on success or a negative number on failure.
121 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000122 * Verifies the AGP device hasn't been acquired before and calls
123 * \c agp_backend_acquire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000125int drm_agp_acquire_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlie7ab98402005-07-10 16:56:52 +1000126 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000128 return drm_agp_acquire((struct drm_device *) file_priv->head->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/**
132 * Release the AGP device.
133 *
Dave Airlied985c102006-01-02 21:32:48 +1100134 * \param dev DRM device that is to release AGP.
Dave Airlie7ab98402005-07-10 16:56:52 +1000135 * \return zero on success or a negative number on failure.
136 *
137 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000139int drm_agp_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Dave Airlie7ab98402005-07-10 16:56:52 +1000141 if (!dev->agp || !dev->agp->acquired)
142 return -EINVAL;
143 agp_backend_release(dev->agp->bridge);
144 dev->agp->acquired = 0;
145 return 0;
146}
147EXPORT_SYMBOL(drm_agp_release);
148
Eric Anholt6c340ea2007-08-25 20:23:09 +1000149int drm_agp_release_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlie7ab98402005-07-10 16:56:52 +1000150 unsigned int cmd, unsigned long arg)
151{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000152 struct drm_device *dev = file_priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153
Dave Airlie7ab98402005-07-10 16:56:52 +1000154 return drm_agp_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/**
158 * Enable the AGP bus.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000160 * \param dev DRM device that has previously acquired AGP.
161 * \param mode Requested AGP mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * \return zero on success or a negative number on failure.
163 *
164 * Verifies the AGP device has been acquired but not enabled, and calls
Dave Airlie7ab98402005-07-10 16:56:52 +1000165 * \c agp_enable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000167int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (!dev->agp || !dev->agp->acquired)
170 return -EINVAL;
171
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 dev->agp->mode = mode.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 agp_enable(dev->agp->bridge, mode.mode);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 dev->agp->base = dev->agp->agp_info.aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dev->agp->enabled = 1;
176 return 0;
177}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178
Dave Airlie7ab98402005-07-10 16:56:52 +1000179EXPORT_SYMBOL(drm_agp_enable);
180
Eric Anholt6c340ea2007-08-25 20:23:09 +1000181int drm_agp_enable_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182 unsigned int cmd, unsigned long arg)
Dave Airlie7ab98402005-07-10 16:56:52 +1000183{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000184 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000185 struct drm_agp_mode mode;
Dave Airlie7ab98402005-07-10 16:56:52 +1000186
Dave Airliec60ce622007-07-11 15:27:12 +1000187 if (copy_from_user(&mode, (struct drm_agp_mode __user *) arg, sizeof(mode)))
Dave Airlie7ab98402005-07-10 16:56:52 +1000188 return -EFAULT;
189
190 return drm_agp_enable(dev, mode);
191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/**
194 * Allocate AGP memory.
195 *
196 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000197 * \param file_priv file private pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * \param cmd command.
199 * \param arg pointer to a drm_agp_buffer structure.
200 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * Verifies the AGP device is present and has been acquired, allocates the
203 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
204 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000205int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Dave Airlie55910512007-07-11 16:53:40 +1000207 struct drm_agp_mem *entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 DRM_AGP_MEM *memory;
209 unsigned long pages;
210 u32 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (!dev->agp || !dev->agp->acquired)
213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
215 return -ENOMEM;
216
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 memset(entry, 0, sizeof(*entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Dave Airlieefa58392005-11-11 22:33:39 +1100219 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
220 type = (u32) request->type;
Dave Airlieb5d499c2005-07-10 18:17:42 +1000221 if (!(memory = drm_alloc_agp(dev, pages, type))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
223 return -ENOMEM;
224 }
225
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 entry->handle = (unsigned long)memory->key + 1;
227 entry->memory = memory;
228 entry->bound = 0;
229 entry->pages = pages;
Dave Airliebd1b3312007-05-26 05:01:51 +1000230 list_add(&entry->head, &dev->agp->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Dave Airlieefa58392005-11-11 22:33:39 +1100232 request->handle = entry->handle;
233 request->physical = memory->physical;
234
235 return 0;
236}
237EXPORT_SYMBOL(drm_agp_alloc);
238
Eric Anholt6c340ea2007-08-25 20:23:09 +1000239int drm_agp_alloc_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlieefa58392005-11-11 22:33:39 +1100240 unsigned int cmd, unsigned long arg)
241{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000242 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000243 struct drm_agp_buffer request;
244 struct drm_agp_buffer __user *argp = (void __user *)arg;
Dave Airlieefa58392005-11-11 22:33:39 +1100245 int err;
246
247 if (copy_from_user(&request, argp, sizeof(request)))
248 return -EFAULT;
249
250 err = drm_agp_alloc(dev, &request);
251 if (err)
252 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (copy_to_user(argp, &request, sizeof(request))) {
Dave Airlie55910512007-07-11 16:53:40 +1000255 struct drm_agp_mem *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +1000256 list_for_each_entry(entry, &dev->agp->memory, head) {
257 if (entry->handle == request.handle)
258 break;
259 }
260 list_del(&entry->head);
Dave Airlieefa58392005-11-11 22:33:39 +1100261 drm_free_agp(entry->memory, entry->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
263 return -EFAULT;
264 }
Dave Airlieefa58392005-11-11 22:33:39 +1100265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267}
268
269/**
270 * Search for the AGP memory entry associated with a handle.
271 *
272 * \param dev DRM device structure.
273 * \param handle AGP memory handle.
274 * \return pointer to the drm_agp_mem structure associated with \p handle.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * Walks through drm_agp_head::memory until finding a matching handle.
277 */
Dave Airlie55910512007-07-11 16:53:40 +1000278static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 unsigned long handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Dave Airlie55910512007-07-11 16:53:40 +1000281 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dave Airliebd1b3312007-05-26 05:01:51 +1000283 list_for_each_entry(entry, &dev->agp->memory, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (entry->handle == handle)
285 return entry;
286 }
287 return NULL;
288}
289
290/**
291 * Unbind AGP memory from the GATT (ioctl).
292 *
293 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000294 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * \param cmd command.
296 * \param arg pointer to a drm_agp_binding structure.
297 * \return zero on success or a negative number on failure.
298 *
299 * Verifies the AGP device is present and acquired, looks-up the AGP memory
300 * entry and passes it to the unbind_agp() function.
301 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000302int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Dave Airlie55910512007-07-11 16:53:40 +1000304 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int ret;
306
307 if (!dev->agp || !dev->agp->acquired)
308 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100309 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -EINVAL;
311 if (!entry->bound)
312 return -EINVAL;
313 ret = drm_unbind_agp(entry->memory);
314 if (ret == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315 entry->bound = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return ret;
317}
Dave Airlieefa58392005-11-11 22:33:39 +1100318EXPORT_SYMBOL(drm_agp_unbind);
319
Eric Anholt6c340ea2007-08-25 20:23:09 +1000320int drm_agp_unbind_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlieefa58392005-11-11 22:33:39 +1100321 unsigned int cmd, unsigned long arg)
322{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000323 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000324 struct drm_agp_binding request;
Dave Airlieefa58392005-11-11 22:33:39 +1100325
326 if (copy_from_user
Dave Airliec60ce622007-07-11 15:27:12 +1000327 (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
Dave Airlieefa58392005-11-11 22:33:39 +1100328 return -EFAULT;
329
330 return drm_agp_unbind(dev, &request);
331}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333/**
334 * Bind AGP memory into the GATT (ioctl)
335 *
336 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000337 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * \param cmd command.
339 * \param arg pointer to a drm_agp_binding structure.
340 * \return zero on success or a negative number on failure.
341 *
342 * Verifies the AGP device is present and has been acquired and that no memory
343 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
344 * it to bind_agp() function.
345 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000346int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Dave Airlie55910512007-07-11 16:53:40 +1000348 struct drm_agp_mem *entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 int retcode;
350 int page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!dev->agp || !dev->agp->acquired)
353 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100354 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EINVAL;
356 if (entry->bound)
357 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100358 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if ((retcode = drm_bind_agp(entry->memory, page)))
360 return retcode;
361 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
362 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
363 dev->agp->base, entry->bound);
364 return 0;
365}
Dave Airlieefa58392005-11-11 22:33:39 +1100366EXPORT_SYMBOL(drm_agp_bind);
367
Eric Anholt6c340ea2007-08-25 20:23:09 +1000368int drm_agp_bind_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlieefa58392005-11-11 22:33:39 +1100369 unsigned int cmd, unsigned long arg)
370{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000371 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000372 struct drm_agp_binding request;
Dave Airlieefa58392005-11-11 22:33:39 +1100373
374 if (copy_from_user
Dave Airliec60ce622007-07-11 15:27:12 +1000375 (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
Dave Airlieefa58392005-11-11 22:33:39 +1100376 return -EFAULT;
377
378 return drm_agp_bind(dev, &request);
379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381/**
382 * Free AGP memory (ioctl).
383 *
384 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000385 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * \param cmd command.
387 * \param arg pointer to a drm_agp_buffer structure.
388 * \return zero on success or a negative number on failure.
389 *
390 * Verifies the AGP device is present and has been acquired and looks up the
391 * AGP memory entry. If the memory it's currently bound, unbind it via
392 * unbind_agp(). Frees it via free_agp() as well as the entry itself
393 * and unlinks from the doubly linked list it's inserted in.
394 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000395int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Dave Airlie55910512007-07-11 16:53:40 +1000397 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (!dev->agp || !dev->agp->acquired)
400 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100401 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EINVAL;
403 if (entry->bound)
404 drm_unbind_agp(entry->memory);
405
Dave Airliebd1b3312007-05-26 05:01:51 +1000406 list_del(&entry->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 drm_free_agp(entry->memory, entry->pages);
409 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
410 return 0;
411}
Dave Airlieefa58392005-11-11 22:33:39 +1100412EXPORT_SYMBOL(drm_agp_free);
413
Eric Anholt6c340ea2007-08-25 20:23:09 +1000414int drm_agp_free_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airlieefa58392005-11-11 22:33:39 +1100415 unsigned int cmd, unsigned long arg)
416{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000417 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000418 struct drm_agp_buffer request;
Dave Airlieefa58392005-11-11 22:33:39 +1100419
420 if (copy_from_user
Dave Airliec60ce622007-07-11 15:27:12 +1000421 (&request, (struct drm_agp_buffer __user *) arg, sizeof(request)))
Dave Airlieefa58392005-11-11 22:33:39 +1100422 return -EFAULT;
423
424 return drm_agp_free(dev, &request);
425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427/**
428 * Initialize the AGP resources.
429 *
430 * \return pointer to a drm_agp_head structure.
431 *
Dave Airlied985c102006-01-02 21:32:48 +1100432 * Gets the drm_agp_t structure which is made available by the agpgart module
433 * via the inter_module_* functions. Creates and initializes a drm_agp_head
434 * structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Dave Airlie55910512007-07-11 16:53:40 +1000436struct drm_agp_head *drm_agp_init(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Dave Airlie55910512007-07-11 16:53:40 +1000438 struct drm_agp_head *head = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
441 return NULL;
442 memset((void *)head, 0, sizeof(*head));
443 head->bridge = agp_find_bridge(dev->pdev);
444 if (!head->bridge) {
445 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
446 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
447 return NULL;
448 }
449 agp_copy_info(head->bridge, &head->agp_info);
450 agp_backend_release(head->bridge);
451 } else {
452 agp_copy_info(head->bridge, &head->agp_info);
453 }
454 if (head->agp_info.chipset == NOT_SUPPORTED) {
455 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
456 return NULL;
457 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000458 INIT_LIST_HEAD(&head->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 head->cant_use_aperture = head->agp_info.cant_use_aperture;
460 head->page_mask = head->agp_info.page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 return head;
463}
464
465/** Calls agp_allocate_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge,
467 size_t pages, u32 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 return agp_allocate_memory(bridge, pages, type);
470}
471
472/** Calls agp_free_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473int drm_agp_free_memory(DRM_AGP_MEM * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 if (!handle)
476 return 0;
477 agp_free_memory(handle);
478 return 1;
479}
480
481/** Calls agp_bind_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 if (!handle)
485 return -EINVAL;
486 return agp_bind_memory(handle, start);
487}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/** Calls agp_unbind_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 if (!handle)
493 return -EINVAL;
494 return agp_unbind_memory(handle);
495}
496
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497#endif /* __OS_HAS_AGP */