blob: 354f0e3674bfff3c5b586c7b5599bcf965c6922c [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.
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.
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
74int drm_agp_info_ioctl(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075 unsigned int cmd, unsigned long arg)
Dave Airlie7ab98402005-07-10 16:56:52 +100076{
Dave Airlie84b1fd12007-07-11 15:53:27 +100077 struct drm_file *priv = filp->private_data;
78 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +100079 struct drm_agp_info info;
Dave Airlie7ab98402005-07-10 16:56:52 +100080 int err;
81
82 err = drm_agp_info(dev, &info);
83 if (err)
84 return err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085
Dave Airliec60ce622007-07-11 15:27:12 +100086 if (copy_to_user((struct drm_agp_info __user *) arg, &info, sizeof(info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -EFAULT;
88 return 0;
89}
90
91/**
Dave Airlie7ab98402005-07-10 16:56:52 +100092 * Acquire the AGP device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Dave Airlied985c102006-01-02 21:32:48 +110094 * \param dev DRM device that is to acquire AGP.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 * \return zero on success or a negative number on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
97 * Verifies the AGP device hasn't been acquired before and calls
Dave Airlie7ab98402005-07-10 16:56:52 +100098 * \c agp_backend_acquire.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000100int drm_agp_acquire(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!dev->agp)
103 return -ENODEV;
104 if (dev->agp->acquired)
105 return -EBUSY;
106 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
107 return -ENODEV;
108 dev->agp->acquired = 1;
109 return 0;
110}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111
Dave Airlie7ab98402005-07-10 16:56:52 +1000112EXPORT_SYMBOL(drm_agp_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/**
Dave Airlie7ab98402005-07-10 16:56:52 +1000115 * Acquire the AGP device (ioctl).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
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.
122 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000123 * Verifies the AGP device hasn't been acquired before and calls
124 * \c agp_backend_acquire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000126int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp,
127 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000129 struct drm_file *priv = filp->private_data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130
Dave Airlie84b1fd12007-07-11 15:53:27 +1000131 return drm_agp_acquire((struct drm_device *) priv->head->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/**
135 * Release the AGP device.
136 *
Dave Airlied985c102006-01-02 21:32:48 +1100137 * \param dev DRM device that is to release AGP.
Dave Airlie7ab98402005-07-10 16:56:52 +1000138 * \return zero on success or a negative number on failure.
139 *
140 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000142int drm_agp_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Dave Airlie7ab98402005-07-10 16:56:52 +1000144 if (!dev->agp || !dev->agp->acquired)
145 return -EINVAL;
146 agp_backend_release(dev->agp->bridge);
147 dev->agp->acquired = 0;
148 return 0;
149}
150EXPORT_SYMBOL(drm_agp_release);
151
152int drm_agp_release_ioctl(struct inode *inode, struct file *filp,
153 unsigned int cmd, unsigned long arg)
154{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000155 struct drm_file *priv = filp->private_data;
156 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157
Dave Airlie7ab98402005-07-10 16:56:52 +1000158 return drm_agp_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/**
162 * Enable the AGP bus.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000164 * \param dev DRM device that has previously acquired AGP.
165 * \param mode Requested AGP mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * \return zero on success or a negative number on failure.
167 *
168 * Verifies the AGP device has been acquired but not enabled, and calls
Dave Airlie7ab98402005-07-10 16:56:52 +1000169 * \c agp_enable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000171int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (!dev->agp || !dev->agp->acquired)
174 return -EINVAL;
175
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 dev->agp->mode = mode.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 agp_enable(dev->agp->bridge, mode.mode);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 dev->agp->base = dev->agp->agp_info.aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dev->agp->enabled = 1;
180 return 0;
181}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182
Dave Airlie7ab98402005-07-10 16:56:52 +1000183EXPORT_SYMBOL(drm_agp_enable);
184
185int drm_agp_enable_ioctl(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 unsigned int cmd, unsigned long arg)
Dave Airlie7ab98402005-07-10 16:56:52 +1000187{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000188 struct drm_file *priv = filp->private_data;
189 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000190 struct drm_agp_mode mode;
Dave Airlie7ab98402005-07-10 16:56:52 +1000191
Dave Airliec60ce622007-07-11 15:27:12 +1000192 if (copy_from_user(&mode, (struct drm_agp_mode __user *) arg, sizeof(mode)))
Dave Airlie7ab98402005-07-10 16:56:52 +1000193 return -EFAULT;
194
195 return drm_agp_enable(dev, mode);
196}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/**
199 * Allocate AGP memory.
200 *
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.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * 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.
209 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000210int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Dave Airlie55910512007-07-11 16:53:40 +1000212 struct drm_agp_mem *entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 DRM_AGP_MEM *memory;
214 unsigned long pages;
215 u32 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (!dev->agp || !dev->agp->acquired)
218 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
220 return -ENOMEM;
221
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 memset(entry, 0, sizeof(*entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Dave Airlieefa58392005-11-11 22:33:39 +1100224 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
225 type = (u32) request->type;
Dave Airlieb5d499c2005-07-10 18:17:42 +1000226 if (!(memory = drm_alloc_agp(dev, pages, type))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
228 return -ENOMEM;
229 }
230
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 entry->handle = (unsigned long)memory->key + 1;
232 entry->memory = memory;
233 entry->bound = 0;
234 entry->pages = pages;
Dave Airliebd1b3312007-05-26 05:01:51 +1000235 list_add(&entry->head, &dev->agp->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Dave Airlieefa58392005-11-11 22:33:39 +1100237 request->handle = entry->handle;
238 request->physical = memory->physical;
239
240 return 0;
241}
242EXPORT_SYMBOL(drm_agp_alloc);
243
244int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp,
245 unsigned int cmd, unsigned long arg)
246{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000247 struct drm_file *priv = filp->private_data;
248 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000249 struct drm_agp_buffer request;
250 struct drm_agp_buffer __user *argp = (void __user *)arg;
Dave Airlieefa58392005-11-11 22:33:39 +1100251 int err;
252
253 if (copy_from_user(&request, argp, sizeof(request)))
254 return -EFAULT;
255
256 err = drm_agp_alloc(dev, &request);
257 if (err)
258 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (copy_to_user(argp, &request, sizeof(request))) {
Dave Airlie55910512007-07-11 16:53:40 +1000261 struct drm_agp_mem *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +1000262 list_for_each_entry(entry, &dev->agp->memory, head) {
263 if (entry->handle == request.handle)
264 break;
265 }
266 list_del(&entry->head);
Dave Airlieefa58392005-11-11 22:33:39 +1100267 drm_free_agp(entry->memory, entry->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
269 return -EFAULT;
270 }
Dave Airlieefa58392005-11-11 22:33:39 +1100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273}
274
275/**
276 * Search for the AGP memory entry associated with a handle.
277 *
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.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * Walks through drm_agp_head::memory until finding a matching handle.
283 */
Dave Airlie55910512007-07-11 16:53:40 +1000284static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 unsigned long handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Dave Airlie55910512007-07-11 16:53:40 +1000287 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dave Airliebd1b3312007-05-26 05:01:51 +1000289 list_for_each_entry(entry, &dev->agp->memory, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (entry->handle == handle)
291 return entry;
292 }
293 return NULL;
294}
295
296/**
297 * Unbind AGP memory from the GATT (ioctl).
298 *
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.
304 *
305 * Verifies the AGP device is present and acquired, looks-up the AGP memory
306 * entry and passes it to the unbind_agp() function.
307 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000308int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Dave Airlie55910512007-07-11 16:53:40 +1000310 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int ret;
312
313 if (!dev->agp || !dev->agp->acquired)
314 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100315 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EINVAL;
317 if (!entry->bound)
318 return -EINVAL;
319 ret = drm_unbind_agp(entry->memory);
320 if (ret == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 entry->bound = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return ret;
323}
Dave Airlieefa58392005-11-11 22:33:39 +1100324EXPORT_SYMBOL(drm_agp_unbind);
325
326int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp,
327 unsigned int cmd, unsigned long arg)
328{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000329 struct drm_file *priv = filp->private_data;
330 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000331 struct drm_agp_binding request;
Dave Airlieefa58392005-11-11 22:33:39 +1100332
333 if (copy_from_user
Dave Airliec60ce622007-07-11 15:27:12 +1000334 (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
Dave Airlieefa58392005-11-11 22:33:39 +1100335 return -EFAULT;
336
337 return drm_agp_unbind(dev, &request);
338}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/**
341 * Bind AGP memory into the GATT (ioctl)
342 *
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.
348 *
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.
352 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000353int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Dave Airlie55910512007-07-11 16:53:40 +1000355 struct drm_agp_mem *entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356 int retcode;
357 int page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (!dev->agp || !dev->agp->acquired)
360 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100361 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return -EINVAL;
363 if (entry->bound)
364 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100365 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if ((retcode = drm_bind_agp(entry->memory, page)))
367 return retcode;
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);
371 return 0;
372}
Dave Airlieefa58392005-11-11 22:33:39 +1100373EXPORT_SYMBOL(drm_agp_bind);
374
375int drm_agp_bind_ioctl(struct inode *inode, struct file *filp,
376 unsigned int cmd, unsigned long arg)
377{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000378 struct drm_file *priv = filp->private_data;
379 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000380 struct drm_agp_binding request;
Dave Airlieefa58392005-11-11 22:33:39 +1100381
382 if (copy_from_user
Dave Airliec60ce622007-07-11 15:27:12 +1000383 (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
Dave Airlieefa58392005-11-11 22:33:39 +1100384 return -EFAULT;
385
386 return drm_agp_bind(dev, &request);
387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389/**
390 * Free AGP memory (ioctl).
391 *
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.
397 *
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.
402 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000403int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Dave Airlie55910512007-07-11 16:53:40 +1000405 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (!dev->agp || !dev->agp->acquired)
408 return -EINVAL;
Dave Airlieefa58392005-11-11 22:33:39 +1100409 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -EINVAL;
411 if (entry->bound)
412 drm_unbind_agp(entry->memory);
413
Dave Airliebd1b3312007-05-26 05:01:51 +1000414 list_del(&entry->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 drm_free_agp(entry->memory, entry->pages);
417 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
418 return 0;
419}
Dave Airlieefa58392005-11-11 22:33:39 +1100420EXPORT_SYMBOL(drm_agp_free);
421
422int drm_agp_free_ioctl(struct inode *inode, struct file *filp,
423 unsigned int cmd, unsigned long arg)
424{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000425 struct drm_file *priv = filp->private_data;
426 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000427 struct drm_agp_buffer request;
Dave Airlieefa58392005-11-11 22:33:39 +1100428
429 if (copy_from_user
Dave Airliec60ce622007-07-11 15:27:12 +1000430 (&request, (struct drm_agp_buffer __user *) arg, sizeof(request)))
Dave Airlieefa58392005-11-11 22:33:39 +1100431 return -EFAULT;
432
433 return drm_agp_free(dev, &request);
434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436/**
437 * Initialize the AGP resources.
438 *
439 * \return pointer to a drm_agp_head structure.
440 *
Dave Airlied985c102006-01-02 21:32:48 +1100441 * 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
443 * structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
Dave Airlie55910512007-07-11 16:53:40 +1000445struct drm_agp_head *drm_agp_init(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Dave Airlie55910512007-07-11 16:53:40 +1000447 struct drm_agp_head *head = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
450 return NULL;
451 memset((void *)head, 0, sizeof(*head));
452 head->bridge = agp_find_bridge(dev->pdev);
453 if (!head->bridge) {
454 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
455 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
456 return NULL;
457 }
458 agp_copy_info(head->bridge, &head->agp_info);
459 agp_backend_release(head->bridge);
460 } else {
461 agp_copy_info(head->bridge, &head->agp_info);
462 }
463 if (head->agp_info.chipset == NOT_SUPPORTED) {
464 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
465 return NULL;
466 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000467 INIT_LIST_HEAD(&head->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 head->cant_use_aperture = head->agp_info.cant_use_aperture;
469 head->page_mask = head->agp_info.page_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 return head;
472}
473
474/** Calls agp_allocate_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge,
476 size_t pages, u32 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 return agp_allocate_memory(bridge, pages, type);
479}
480
481/** Calls agp_free_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482int drm_agp_free_memory(DRM_AGP_MEM * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 if (!handle)
485 return 0;
486 agp_free_memory(handle);
487 return 1;
488}
489
490/** Calls agp_bind_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000491int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 if (!handle)
494 return -EINVAL;
495 return agp_bind_memory(handle, start);
496}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/** Calls agp_unbind_memory() */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 if (!handle)
502 return -EINVAL;
503 return agp_unbind_memory(handle);
504}
505
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506#endif /* __OS_HAS_AGP */