Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 2 | * \file drm_agpsupport.h |
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 | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 51 | int drm_agp_info(drm_device_t * dev, drm_agp_info_t * 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 | { |
| 77 | drm_file_t *priv = filp->private_data; |
| 78 | drm_device_t *dev = priv->head->dev; |
| 79 | drm_agp_info_t info; |
| 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 | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 86 | if (copy_to_user((drm_agp_info_t __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 | 7ab9840 | 2005-07-10 16:56:52 +1000 | [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 | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 100 | int drm_agp_acquire(drm_device_t * 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 | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 129 | drm_file_t *priv = filp->private_data; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 130 | |
| 131 | return drm_agp_acquire((drm_device_t *) 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 | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 137 | * \param dev DRM device that is to release AGP |
| 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 | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 142 | int drm_agp_release(drm_device_t * 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 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 150 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 151 | EXPORT_SYMBOL(drm_agp_release); |
| 152 | |
| 153 | int drm_agp_release_ioctl(struct inode *inode, struct file *filp, |
| 154 | unsigned int cmd, unsigned long arg) |
| 155 | { |
| 156 | drm_file_t *priv = filp->private_data; |
| 157 | drm_device_t *dev = priv->head->dev; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 158 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 159 | return drm_agp_release(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Enable the AGP bus. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 164 | * |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 165 | * \param dev DRM device that has previously acquired AGP. |
| 166 | * \param mode Requested AGP mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | * \return zero on success or a negative number on failure. |
| 168 | * |
| 169 | * Verifies the AGP device has been acquired but not enabled, and calls |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 170 | * \c agp_enable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 172 | int drm_agp_enable(drm_device_t * dev, drm_agp_mode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | if (!dev->agp || !dev->agp->acquired) |
| 175 | return -EINVAL; |
| 176 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 177 | dev->agp->mode = mode.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | agp_enable(dev->agp->bridge, mode.mode); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 179 | dev->agp->base = dev->agp->agp_info.aper_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | dev->agp->enabled = 1; |
| 181 | return 0; |
| 182 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 183 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 184 | EXPORT_SYMBOL(drm_agp_enable); |
| 185 | |
| 186 | int drm_agp_enable_ioctl(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 187 | unsigned int cmd, unsigned long arg) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 188 | { |
| 189 | drm_file_t *priv = filp->private_data; |
| 190 | drm_device_t *dev = priv->head->dev; |
| 191 | drm_agp_mode_t mode; |
| 192 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 193 | if (copy_from_user(&mode, (drm_agp_mode_t __user *) arg, sizeof(mode))) |
| 194 | return -EFAULT; |
| 195 | |
| 196 | return drm_agp_enable(dev, mode); |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Allocate AGP memory. |
| 201 | * |
| 202 | * \param inode device inode. |
| 203 | * \param filp file pointer. |
| 204 | * \param cmd command. |
| 205 | * \param arg pointer to a drm_agp_buffer structure. |
| 206 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 207 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | * Verifies the AGP device is present and has been acquired, allocates the |
| 209 | * memory via alloc_agp() and creates a drm_agp_mem entry for it. |
| 210 | */ |
| 211 | int drm_agp_alloc(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 212 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 214 | drm_file_t *priv = filp->private_data; |
| 215 | drm_device_t *dev = priv->head->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | drm_agp_buffer_t request; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 217 | drm_agp_mem_t *entry; |
| 218 | DRM_AGP_MEM *memory; |
| 219 | unsigned long pages; |
| 220 | u32 type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | drm_agp_buffer_t __user *argp = (void __user *)arg; |
| 222 | |
| 223 | if (!dev->agp || !dev->agp->acquired) |
| 224 | return -EINVAL; |
| 225 | if (copy_from_user(&request, argp, sizeof(request))) |
| 226 | return -EFAULT; |
| 227 | if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS))) |
| 228 | return -ENOMEM; |
| 229 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 230 | memset(entry, 0, sizeof(*entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE; |
| 233 | type = (u32) request.type; |
| 234 | |
Dave Airlie | b5d499c | 2005-07-10 18:17:42 +1000 | [diff] [blame] | 235 | if (!(memory = drm_alloc_agp(dev, pages, type))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 240 | entry->handle = (unsigned long)memory->key + 1; |
| 241 | entry->memory = memory; |
| 242 | entry->bound = 0; |
| 243 | entry->pages = pages; |
| 244 | entry->prev = NULL; |
| 245 | entry->next = dev->agp->memory; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (dev->agp->memory) |
| 247 | dev->agp->memory->prev = entry; |
| 248 | dev->agp->memory = entry; |
| 249 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 250 | request.handle = entry->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | request.physical = memory->physical; |
| 252 | |
| 253 | if (copy_to_user(argp, &request, sizeof(request))) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 254 | dev->agp->memory = entry->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | dev->agp->memory->prev = NULL; |
| 256 | drm_free_agp(memory, pages); |
| 257 | drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); |
| 258 | return -EFAULT; |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Search for the AGP memory entry associated with a handle. |
| 265 | * |
| 266 | * \param dev DRM device structure. |
| 267 | * \param handle AGP memory handle. |
| 268 | * \return pointer to the drm_agp_mem structure associated with \p handle. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 269 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * Walks through drm_agp_head::memory until finding a matching handle. |
| 271 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 272 | static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t * dev, |
| 273 | unsigned long handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | drm_agp_mem_t *entry; |
| 276 | |
| 277 | for (entry = dev->agp->memory; entry; entry = entry->next) { |
| 278 | if (entry->handle == handle) |
| 279 | return entry; |
| 280 | } |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Unbind AGP memory from the GATT (ioctl). |
| 286 | * |
| 287 | * \param inode device inode. |
| 288 | * \param filp file pointer. |
| 289 | * \param cmd command. |
| 290 | * \param arg pointer to a drm_agp_binding structure. |
| 291 | * \return zero on success or a negative number on failure. |
| 292 | * |
| 293 | * Verifies the AGP device is present and acquired, looks-up the AGP memory |
| 294 | * entry and passes it to the unbind_agp() function. |
| 295 | */ |
| 296 | int drm_agp_unbind(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 297 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 299 | drm_file_t *priv = filp->private_data; |
| 300 | drm_device_t *dev = priv->head->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | drm_agp_binding_t request; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 302 | drm_agp_mem_t *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | int ret; |
| 304 | |
| 305 | if (!dev->agp || !dev->agp->acquired) |
| 306 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 307 | if (copy_from_user |
| 308 | (&request, (drm_agp_binding_t __user *) arg, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return -EFAULT; |
| 310 | if (!(entry = drm_agp_lookup_entry(dev, request.handle))) |
| 311 | return -EINVAL; |
| 312 | if (!entry->bound) |
| 313 | return -EINVAL; |
| 314 | ret = drm_unbind_agp(entry->memory); |
| 315 | if (ret == 0) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 316 | entry->bound = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Bind AGP memory into the GATT (ioctl) |
| 322 | * |
| 323 | * \param inode device inode. |
| 324 | * \param filp file pointer. |
| 325 | * \param cmd command. |
| 326 | * \param arg pointer to a drm_agp_binding structure. |
| 327 | * \return zero on success or a negative number on failure. |
| 328 | * |
| 329 | * Verifies the AGP device is present and has been acquired and that no memory |
| 330 | * is currently bound into the GATT. Looks-up the AGP memory entry and passes |
| 331 | * it to bind_agp() function. |
| 332 | */ |
| 333 | int drm_agp_bind(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 334 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 336 | drm_file_t *priv = filp->private_data; |
| 337 | drm_device_t *dev = priv->head->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | drm_agp_binding_t request; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 339 | drm_agp_mem_t *entry; |
| 340 | int retcode; |
| 341 | int page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | if (!dev->agp || !dev->agp->acquired) |
| 344 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 345 | if (copy_from_user |
| 346 | (&request, (drm_agp_binding_t __user *) arg, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return -EFAULT; |
| 348 | if (!(entry = drm_agp_lookup_entry(dev, request.handle))) |
| 349 | return -EINVAL; |
| 350 | if (entry->bound) |
| 351 | return -EINVAL; |
| 352 | page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE; |
| 353 | if ((retcode = drm_bind_agp(entry->memory, page))) |
| 354 | return retcode; |
| 355 | entry->bound = dev->agp->base + (page << PAGE_SHIFT); |
| 356 | DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n", |
| 357 | dev->agp->base, entry->bound); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Free AGP memory (ioctl). |
| 363 | * |
| 364 | * \param inode device inode. |
| 365 | * \param filp file pointer. |
| 366 | * \param cmd command. |
| 367 | * \param arg pointer to a drm_agp_buffer structure. |
| 368 | * \return zero on success or a negative number on failure. |
| 369 | * |
| 370 | * Verifies the AGP device is present and has been acquired and looks up the |
| 371 | * AGP memory entry. If the memory it's currently bound, unbind it via |
| 372 | * unbind_agp(). Frees it via free_agp() as well as the entry itself |
| 373 | * and unlinks from the doubly linked list it's inserted in. |
| 374 | */ |
| 375 | int drm_agp_free(struct inode *inode, struct file *filp, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 376 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 378 | drm_file_t *priv = filp->private_data; |
| 379 | drm_device_t *dev = priv->head->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | drm_agp_buffer_t request; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 381 | drm_agp_mem_t *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | if (!dev->agp || !dev->agp->acquired) |
| 384 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 385 | if (copy_from_user |
| 386 | (&request, (drm_agp_buffer_t __user *) arg, sizeof(request))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return -EFAULT; |
| 388 | if (!(entry = drm_agp_lookup_entry(dev, request.handle))) |
| 389 | return -EINVAL; |
| 390 | if (entry->bound) |
| 391 | drm_unbind_agp(entry->memory); |
| 392 | |
| 393 | if (entry->prev) |
| 394 | entry->prev->next = entry->next; |
| 395 | else |
| 396 | dev->agp->memory = entry->next; |
| 397 | |
| 398 | if (entry->next) |
| 399 | entry->next->prev = entry->prev; |
| 400 | |
| 401 | drm_free_agp(entry->memory, entry->pages); |
| 402 | drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Initialize the AGP resources. |
| 408 | * |
| 409 | * \return pointer to a drm_agp_head structure. |
| 410 | * |
| 411 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 412 | drm_agp_head_t *drm_agp_init(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 414 | drm_agp_head_t *head = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS))) |
| 417 | return NULL; |
| 418 | memset((void *)head, 0, sizeof(*head)); |
| 419 | head->bridge = agp_find_bridge(dev->pdev); |
| 420 | if (!head->bridge) { |
| 421 | if (!(head->bridge = agp_backend_acquire(dev->pdev))) { |
| 422 | drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS); |
| 423 | return NULL; |
| 424 | } |
| 425 | agp_copy_info(head->bridge, &head->agp_info); |
| 426 | agp_backend_release(head->bridge); |
| 427 | } else { |
| 428 | agp_copy_info(head->bridge, &head->agp_info); |
| 429 | } |
| 430 | if (head->agp_info.chipset == NOT_SUPPORTED) { |
| 431 | drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS); |
| 432 | return NULL; |
| 433 | } |
| 434 | head->memory = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | head->cant_use_aperture = head->agp_info.cant_use_aperture; |
| 436 | head->page_mask = head->agp_info.page_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | return head; |
| 439 | } |
| 440 | |
| 441 | /** Calls agp_allocate_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 442 | DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge, |
| 443 | size_t pages, u32 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | return agp_allocate_memory(bridge, pages, type); |
| 446 | } |
| 447 | |
| 448 | /** Calls agp_free_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 449 | int drm_agp_free_memory(DRM_AGP_MEM * handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
| 451 | if (!handle) |
| 452 | return 0; |
| 453 | agp_free_memory(handle); |
| 454 | return 1; |
| 455 | } |
| 456 | |
| 457 | /** Calls agp_bind_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 458 | int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | if (!handle) |
| 461 | return -EINVAL; |
| 462 | return agp_bind_memory(handle, start); |
| 463 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 464 | |
Dave Airlie | d84f76d | 2005-07-10 17:04:22 +1000 | [diff] [blame] | 465 | EXPORT_SYMBOL(drm_agp_bind_memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | /** Calls agp_unbind_memory() */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 468 | int drm_agp_unbind_memory(DRM_AGP_MEM * handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | if (!handle) |
| 471 | return -EINVAL; |
| 472 | return agp_unbind_memory(handle); |
| 473 | } |
| 474 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 475 | #endif /* __OS_HAS_AGP */ |