Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 2 | * \file drm_agpsupport.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * DRM support for AGP/GART backend |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * \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 Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 40 | * Get AGP information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * |
| 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 Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 51 | int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 53 | DRM_AGP_KERN *kern; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | if (!dev->agp || !dev->agp->acquired) |
| 56 | return -EINVAL; |
| 57 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 58 | kern = &dev->agp->agp_info; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 59 | info->agp_version_major = kern->version.major; |
| 60 | info->agp_version_minor = kern->version.minor; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 71 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 72 | EXPORT_SYMBOL(drm_agp_info); |
| 73 | |
| 74 | int drm_agp_info_ioctl(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 75 | unsigned int cmd, unsigned long arg) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 76 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 77 | struct drm_file *priv = filp->private_data; |
| 78 | struct drm_device *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 79 | struct drm_agp_info info; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 80 | int err; |
| 81 | |
| 82 | err = drm_agp_info(dev, &info); |
| 83 | if (err) |
| 84 | return err; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 85 | |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 86 | if (copy_to_user((struct drm_agp_info __user *) arg, &info, sizeof(info))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return -EFAULT; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /** |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 92 | * Acquire the AGP device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 94 | * \param dev DRM device that is to acquire AGP. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 95 | * \return zero on success or a negative number on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | * |
| 97 | * Verifies the AGP device hasn't been acquired before and calls |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 98 | * \c agp_backend_acquire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 100 | int drm_agp_acquire(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | 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 Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 111 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 112 | EXPORT_SYMBOL(drm_agp_acquire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 115 | * Acquire the AGP device (ioctl). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | * |
| 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 Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 123 | * Verifies the AGP device hasn't been acquired before and calls |
| 124 | * \c agp_backend_acquire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | */ |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 126 | int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp, |
| 127 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 129 | struct drm_file *priv = filp->private_data; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 130 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 131 | return drm_agp_acquire((struct drm_device *) priv->head->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Release the AGP device. |
| 136 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 137 | * \param dev DRM device that is to release AGP. |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 138 | * \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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 142 | int drm_agp_release(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 144 | 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 | } |
| 150 | EXPORT_SYMBOL(drm_agp_release); |
| 151 | |
| 152 | int drm_agp_release_ioctl(struct inode *inode, struct file *filp, |
| 153 | unsigned int cmd, unsigned long arg) |
| 154 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 155 | struct drm_file *priv = filp->private_data; |
| 156 | struct drm_device *dev = priv->head->dev; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 157 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 158 | return drm_agp_release(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Enable the AGP bus. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 163 | * |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 164 | * \param dev DRM device that has previously acquired AGP. |
| 165 | * \param mode Requested AGP mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | * \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 Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 169 | * \c agp_enable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 171 | int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (!dev->agp || !dev->agp->acquired) |
| 174 | return -EINVAL; |
| 175 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 176 | dev->agp->mode = mode.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | agp_enable(dev->agp->bridge, mode.mode); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 178 | dev->agp->base = dev->agp->agp_info.aper_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | dev->agp->enabled = 1; |
| 180 | return 0; |
| 181 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 182 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 183 | EXPORT_SYMBOL(drm_agp_enable); |
| 184 | |
| 185 | int drm_agp_enable_ioctl(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 186 | unsigned int cmd, unsigned long arg) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 187 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 188 | struct drm_file *priv = filp->private_data; |
| 189 | struct drm_device *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 190 | struct drm_agp_mode mode; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 191 | |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 192 | if (copy_from_user(&mode, (struct drm_agp_mode __user *) arg, sizeof(mode))) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 193 | return -EFAULT; |
| 194 | |
| 195 | return drm_agp_enable(dev, mode); |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 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 Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 206 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 209 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 210 | int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 212 | struct drm_agp_mem *entry; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 213 | DRM_AGP_MEM *memory; |
| 214 | unsigned long pages; |
| 215 | u32 type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | if (!dev->agp || !dev->agp->acquired) |
| 218 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS))) |
| 220 | return -ENOMEM; |
| 221 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 222 | memset(entry, 0, sizeof(*entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 224 | pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE; |
| 225 | type = (u32) request->type; |
Dave Airlie | b5d499c | 2005-07-10 18:17:42 +1000 | [diff] [blame] | 226 | if (!(memory = drm_alloc_agp(dev, pages, type))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); |
| 228 | return -ENOMEM; |
| 229 | } |
| 230 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 231 | entry->handle = (unsigned long)memory->key + 1; |
| 232 | entry->memory = memory; |
| 233 | entry->bound = 0; |
| 234 | entry->pages = pages; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 235 | list_add(&entry->head, &dev->agp->memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 237 | request->handle = entry->handle; |
| 238 | request->physical = memory->physical; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | EXPORT_SYMBOL(drm_agp_alloc); |
| 243 | |
| 244 | int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp, |
| 245 | unsigned int cmd, unsigned long arg) |
| 246 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 247 | struct drm_file *priv = filp->private_data; |
| 248 | struct drm_device *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 249 | struct drm_agp_buffer request; |
| 250 | struct drm_agp_buffer __user *argp = (void __user *)arg; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 251 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | if (copy_to_user(argp, &request, sizeof(request))) { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 261 | struct drm_agp_mem *entry; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 262 | list_for_each_entry(entry, &dev->agp->memory, head) { |
| 263 | if (entry->handle == request.handle) |
| 264 | break; |
| 265 | } |
| 266 | list_del(&entry->head); |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 267 | drm_free_agp(entry->memory, entry->pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); |
| 269 | return -EFAULT; |
| 270 | } |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | 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 Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 281 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | * Walks through drm_agp_head::memory until finding a matching handle. |
| 283 | */ |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 284 | static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 285 | unsigned long handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 287 | struct drm_agp_mem *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 289 | list_for_each_entry(entry, &dev->agp->memory, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 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 Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 308 | int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 310 | struct drm_agp_mem *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | int ret; |
| 312 | |
| 313 | if (!dev->agp || !dev->agp->acquired) |
| 314 | return -EINVAL; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 315 | if (!(entry = drm_agp_lookup_entry(dev, request->handle))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return -EINVAL; |
| 317 | if (!entry->bound) |
| 318 | return -EINVAL; |
| 319 | ret = drm_unbind_agp(entry->memory); |
| 320 | if (ret == 0) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 321 | entry->bound = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | return ret; |
| 323 | } |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 324 | EXPORT_SYMBOL(drm_agp_unbind); |
| 325 | |
| 326 | int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp, |
| 327 | unsigned int cmd, unsigned long arg) |
| 328 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 329 | struct drm_file *priv = filp->private_data; |
| 330 | struct drm_device *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 331 | struct drm_agp_binding request; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 332 | |
| 333 | if (copy_from_user |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 334 | (&request, (struct drm_agp_binding __user *) arg, sizeof(request))) |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 335 | return -EFAULT; |
| 336 | |
| 337 | return drm_agp_unbind(dev, &request); |
| 338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 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 Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 353 | int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 355 | struct drm_agp_mem *entry; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 356 | int retcode; |
| 357 | int page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | if (!dev->agp || !dev->agp->acquired) |
| 360 | return -EINVAL; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 361 | if (!(entry = drm_agp_lookup_entry(dev, request->handle))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return -EINVAL; |
| 363 | if (entry->bound) |
| 364 | return -EINVAL; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 365 | page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | 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 Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 373 | EXPORT_SYMBOL(drm_agp_bind); |
| 374 | |
| 375 | int drm_agp_bind_ioctl(struct inode *inode, struct file *filp, |
| 376 | unsigned int cmd, unsigned long arg) |
| 377 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 378 | struct drm_file *priv = filp->private_data; |
| 379 | struct drm_device *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 380 | struct drm_agp_binding request; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 381 | |
| 382 | if (copy_from_user |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 383 | (&request, (struct drm_agp_binding __user *) arg, sizeof(request))) |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 384 | return -EFAULT; |
| 385 | |
| 386 | return drm_agp_bind(dev, &request); |
| 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 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 Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 403 | int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 405 | struct drm_agp_mem *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | if (!dev->agp || !dev->agp->acquired) |
| 408 | return -EINVAL; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 409 | if (!(entry = drm_agp_lookup_entry(dev, request->handle))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | return -EINVAL; |
| 411 | if (entry->bound) |
| 412 | drm_unbind_agp(entry->memory); |
| 413 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 414 | list_del(&entry->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | drm_free_agp(entry->memory, entry->pages); |
| 417 | drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); |
| 418 | return 0; |
| 419 | } |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 420 | EXPORT_SYMBOL(drm_agp_free); |
| 421 | |
| 422 | int drm_agp_free_ioctl(struct inode *inode, struct file *filp, |
| 423 | unsigned int cmd, unsigned long arg) |
| 424 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 425 | struct drm_file *priv = filp->private_data; |
| 426 | struct drm_device *dev = priv->head->dev; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 427 | struct drm_agp_buffer request; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 428 | |
| 429 | if (copy_from_user |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 430 | (&request, (struct drm_agp_buffer __user *) arg, sizeof(request))) |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 431 | return -EFAULT; |
| 432 | |
| 433 | return drm_agp_free(dev, &request); |
| 434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * Initialize the AGP resources. |
| 438 | * |
| 439 | * \return pointer to a drm_agp_head structure. |
| 440 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 441 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | */ |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 445 | struct drm_agp_head *drm_agp_init(struct drm_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 447 | struct drm_agp_head *head = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 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 Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 467 | INIT_LIST_HEAD(&head->memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | head->cant_use_aperture = head->agp_info.cant_use_aperture; |
| 469 | head->page_mask = head->agp_info.page_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | return head; |
| 472 | } |
| 473 | |
| 474 | /** Calls agp_allocate_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 475 | DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge, |
| 476 | size_t pages, u32 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | return agp_allocate_memory(bridge, pages, type); |
| 479 | } |
| 480 | |
| 481 | /** Calls agp_free_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 482 | int drm_agp_free_memory(DRM_AGP_MEM * handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
| 484 | if (!handle) |
| 485 | return 0; |
| 486 | agp_free_memory(handle); |
| 487 | return 1; |
| 488 | } |
| 489 | |
| 490 | /** Calls agp_bind_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 491 | int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | if (!handle) |
| 494 | return -EINVAL; |
| 495 | return agp_bind_memory(handle, start); |
| 496 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | /** Calls agp_unbind_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 499 | int drm_agp_unbind_memory(DRM_AGP_MEM * handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
| 501 | if (!handle) |
| 502 | return -EINVAL; |
| 503 | return agp_unbind_memory(handle); |
| 504 | } |
| 505 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 506 | #endif /* __OS_HAS_AGP */ |