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_fops.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * File operations for DRM |
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 Daryll Strauss <daryll@valinux.com> |
| 7 | * \author Gareth Hughes <gareth@valinux.com> |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com |
| 12 | * |
| 13 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
| 14 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 15 | * All Rights Reserved. |
| 16 | * |
| 17 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 18 | * copy of this software and associated documentation files (the "Software"), |
| 19 | * to deal in the Software without restriction, including without limitation |
| 20 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 21 | * and/or sell copies of the Software, and to permit persons to whom the |
| 22 | * Software is furnished to do so, subject to the following conditions: |
| 23 | * |
| 24 | * The above copyright notice and this permission notice (including the next |
| 25 | * paragraph) shall be included in all copies or substantial portions of the |
| 26 | * Software. |
| 27 | * |
| 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 29 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 30 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 31 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 32 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 33 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 34 | * OTHER DEALINGS IN THE SOFTWARE. |
| 35 | */ |
| 36 | |
| 37 | #include "drmP.h" |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 38 | #include "drm_sarea.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/poll.h> |
| 40 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 41 | static int drm_open_helper(struct inode *inode, struct file *filp, |
| 42 | drm_device_t * dev); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 43 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 44 | static int drm_setup(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 46 | drm_local_map_t *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | int i; |
| 48 | int ret; |
| 49 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 50 | if (dev->driver->firstopen) { |
| 51 | ret = dev->driver->firstopen(dev); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 52 | if (ret != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return ret; |
| 54 | } |
| 55 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 56 | /* prebuild the SAREA */ |
| 57 | i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM, _DRM_CONTAINS_LOCK, &map); |
| 58 | if (i != 0) |
| 59 | return i; |
| 60 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 61 | atomic_set(&dev->ioctl_count, 0); |
| 62 | atomic_set(&dev->vma_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | dev->buf_use = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 64 | atomic_set(&dev->buf_alloc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 66 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) { |
| 67 | i = drm_dma_setup(dev); |
| 68 | if (i < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return i; |
| 70 | } |
| 71 | |
Dave Airlie | 3d77461 | 2006-08-07 20:07:43 +1000 | [diff] [blame] | 72 | for (i = 0; i < ARRAY_SIZE(dev->counts); i++) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 73 | atomic_set(&dev->counts[i], 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Thomas Hellstrom | 8669cbc | 2006-08-07 22:22:10 +1000 | [diff] [blame^] | 75 | drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER); |
| 76 | INIT_LIST_HEAD(&dev->magicfree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 78 | dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist), DRM_MEM_CTXLIST); |
| 79 | if (dev->ctxlist == NULL) |
| 80 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | memset(dev->ctxlist, 0, sizeof(*dev->ctxlist)); |
| 82 | INIT_LIST_HEAD(&dev->ctxlist->head); |
| 83 | |
| 84 | dev->vmalist = NULL; |
| 85 | dev->sigdata.lock = dev->lock.hw_lock = NULL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 86 | init_waitqueue_head(&dev->lock.lock_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | dev->queue_count = 0; |
| 88 | dev->queue_reserved = 0; |
| 89 | dev->queue_slots = 0; |
| 90 | dev->queuelist = NULL; |
| 91 | dev->irq_enabled = 0; |
| 92 | dev->context_flag = 0; |
| 93 | dev->interrupt_flag = 0; |
| 94 | dev->dma_flag = 0; |
| 95 | dev->last_context = 0; |
| 96 | dev->last_switch = 0; |
| 97 | dev->last_checked = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 98 | init_waitqueue_head(&dev->context_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | dev->if_version = 0; |
| 100 | |
| 101 | dev->ctx_start = 0; |
| 102 | dev->lck_start = 0; |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | dev->buf_async = NULL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 105 | init_waitqueue_head(&dev->buf_readers); |
| 106 | init_waitqueue_head(&dev->buf_writers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 108 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * The kernel's context could be created here, but is now created |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 112 | * in drm_dma_enqueue. This is more resource-efficient for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * hardware that does not do DMA, but may mean that |
| 114 | * drm_select_queue fails between the time the interrupt is |
| 115 | * initialized and the time the queues are initialized. |
| 116 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Open file. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 123 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * \param inode device inode |
| 125 | * \param filp file pointer. |
| 126 | * \return zero on success or a negative number on failure. |
| 127 | * |
| 128 | * Searches the DRM device with the same minor number, calls open_helper(), and |
| 129 | * increments the device open count. If the open count was previous at zero, |
| 130 | * i.e., it's the first that the device is open, then calls setup(). |
| 131 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 132 | int drm_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | drm_device_t *dev = NULL; |
| 135 | int minor = iminor(inode); |
| 136 | int retcode = 0; |
| 137 | |
| 138 | if (!((minor >= 0) && (minor < drm_cards_limit))) |
| 139 | return -ENODEV; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (!drm_heads[minor]) |
| 142 | return -ENODEV; |
| 143 | |
| 144 | if (!(dev = drm_heads[minor]->dev)) |
| 145 | return -ENODEV; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 146 | |
| 147 | retcode = drm_open_helper(inode, filp, dev); |
| 148 | if (!retcode) { |
| 149 | atomic_inc(&dev->counts[_DRM_STAT_OPENS]); |
| 150 | spin_lock(&dev->count_lock); |
| 151 | if (!dev->open_count++) { |
| 152 | spin_unlock(&dev->count_lock); |
| 153 | return drm_setup(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 155 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return retcode; |
| 159 | } |
| 160 | EXPORT_SYMBOL(drm_open); |
| 161 | |
| 162 | /** |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 163 | * File \c open operation. |
| 164 | * |
| 165 | * \param inode device inode. |
| 166 | * \param filp file pointer. |
| 167 | * |
| 168 | * Puts the dev->fops corresponding to the device minor number into |
| 169 | * \p filp, call the \c open method, and restore the file operations. |
| 170 | */ |
| 171 | int drm_stub_open(struct inode *inode, struct file *filp) |
| 172 | { |
| 173 | drm_device_t *dev = NULL; |
| 174 | int minor = iminor(inode); |
| 175 | int err = -ENODEV; |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 176 | const struct file_operations *old_fops; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 177 | |
| 178 | DRM_DEBUG("\n"); |
| 179 | |
| 180 | if (!((minor >= 0) && (minor < drm_cards_limit))) |
| 181 | return -ENODEV; |
| 182 | |
| 183 | if (!drm_heads[minor]) |
| 184 | return -ENODEV; |
| 185 | |
| 186 | if (!(dev = drm_heads[minor]->dev)) |
| 187 | return -ENODEV; |
| 188 | |
| 189 | old_fops = filp->f_op; |
| 190 | filp->f_op = fops_get(&dev->driver->fops); |
| 191 | if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) { |
| 192 | fops_put(filp->f_op); |
| 193 | filp->f_op = fops_get(old_fops); |
| 194 | } |
| 195 | fops_put(old_fops); |
| 196 | |
| 197 | return err; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Check whether DRI will run on this CPU. |
| 202 | * |
| 203 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. |
| 204 | */ |
| 205 | static int drm_cpu_valid(void) |
| 206 | { |
| 207 | #if defined(__i386__) |
| 208 | if (boot_cpu_data.x86 == 3) |
| 209 | return 0; /* No cmpxchg on a 386 */ |
| 210 | #endif |
| 211 | #if defined(__sparc__) && !defined(__sparc_v9__) |
| 212 | return 0; /* No cmpxchg before v9 sparc. */ |
| 213 | #endif |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Called whenever a process opens /dev/drm. |
| 219 | * |
| 220 | * \param inode device inode. |
| 221 | * \param filp file pointer. |
| 222 | * \param dev device. |
| 223 | * \return zero on success or a negative number on failure. |
| 224 | * |
| 225 | * Creates and initializes a drm_file structure for the file private data in \p |
| 226 | * filp and add it into the double linked list in \p dev. |
| 227 | */ |
| 228 | static int drm_open_helper(struct inode *inode, struct file *filp, |
| 229 | drm_device_t * dev) |
| 230 | { |
| 231 | int minor = iminor(inode); |
| 232 | drm_file_t *priv; |
| 233 | int ret; |
| 234 | |
| 235 | if (filp->f_flags & O_EXCL) |
| 236 | return -EBUSY; /* No exclusive opens */ |
| 237 | if (!drm_cpu_valid()) |
| 238 | return -EINVAL; |
| 239 | |
| 240 | DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor); |
| 241 | |
| 242 | priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES); |
| 243 | if (!priv) |
| 244 | return -ENOMEM; |
| 245 | |
| 246 | memset(priv, 0, sizeof(*priv)); |
| 247 | filp->private_data = priv; |
| 248 | priv->uid = current->euid; |
| 249 | priv->pid = current->pid; |
| 250 | priv->minor = minor; |
| 251 | priv->head = drm_heads[minor]; |
| 252 | priv->ioctl_count = 0; |
| 253 | /* for compatibility root is always authenticated */ |
| 254 | priv->authenticated = capable(CAP_SYS_ADMIN); |
| 255 | priv->lock_count = 0; |
| 256 | |
| 257 | if (dev->driver->open) { |
| 258 | ret = dev->driver->open(dev, priv); |
| 259 | if (ret < 0) |
| 260 | goto out_free; |
| 261 | } |
| 262 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 263 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 264 | if (!dev->file_last) { |
| 265 | priv->next = NULL; |
| 266 | priv->prev = NULL; |
| 267 | dev->file_first = priv; |
| 268 | dev->file_last = priv; |
| 269 | /* first opener automatically becomes master */ |
| 270 | priv->master = 1; |
| 271 | } else { |
| 272 | priv->next = NULL; |
| 273 | priv->prev = dev->file_last; |
| 274 | dev->file_last->next = priv; |
| 275 | dev->file_last = priv; |
| 276 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 277 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 278 | |
| 279 | #ifdef __alpha__ |
| 280 | /* |
| 281 | * Default the hose |
| 282 | */ |
| 283 | if (!dev->hose) { |
| 284 | struct pci_dev *pci_dev; |
| 285 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); |
| 286 | if (pci_dev) { |
| 287 | dev->hose = pci_dev->sysdata; |
| 288 | pci_dev_put(pci_dev); |
| 289 | } |
| 290 | if (!dev->hose) { |
| 291 | struct pci_bus *b = pci_bus_b(pci_root_buses.next); |
| 292 | if (b) |
| 293 | dev->hose = b->sysdata; |
| 294 | } |
| 295 | } |
| 296 | #endif |
| 297 | |
| 298 | return 0; |
| 299 | out_free: |
| 300 | drm_free(priv, sizeof(*priv), DRM_MEM_FILES); |
| 301 | filp->private_data = NULL; |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | /** No-op. */ |
| 306 | int drm_fasync(int fd, struct file *filp, int on) |
| 307 | { |
| 308 | drm_file_t *priv = filp->private_data; |
| 309 | drm_device_t *dev = priv->head->dev; |
| 310 | int retcode; |
| 311 | |
| 312 | DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, |
| 313 | (long)old_encode_dev(priv->head->device)); |
| 314 | retcode = fasync_helper(fd, filp, on, &dev->buf_async); |
| 315 | if (retcode < 0) |
| 316 | return retcode; |
| 317 | return 0; |
| 318 | } |
| 319 | EXPORT_SYMBOL(drm_fasync); |
| 320 | |
| 321 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | * Release file. |
| 323 | * |
| 324 | * \param inode device inode |
| 325 | * \param filp file pointer. |
| 326 | * \return zero on success or a negative number on failure. |
| 327 | * |
| 328 | * If the hardware lock is held then free it, and take it again for the kernel |
| 329 | * context since it's necessary to reclaim buffers. Unlink the file private |
| 330 | * data from its list and free it. Decreases the open count and if it reaches |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 331 | * zero calls drm_lastclose(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 333 | int drm_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | drm_file_t *priv = filp->private_data; |
| 336 | drm_device_t *dev; |
| 337 | int retcode = 0; |
| 338 | |
| 339 | lock_kernel(); |
| 340 | dev = priv->head->dev; |
| 341 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 342 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 344 | if (dev->driver->preclose) |
| 345 | dev->driver->preclose(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | /* ======================================================== |
| 348 | * Begin inline drm_release |
| 349 | */ |
| 350 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 351 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
| 352 | current->pid, (long)old_encode_dev(priv->head->device), |
| 353 | dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 355 | if (priv->lock_count && dev->lock.hw_lock && |
| 356 | _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) && |
| 357 | dev->lock.filp == filp) { |
| 358 | DRM_DEBUG("File %p released, freeing lock for context %d\n", |
| 359 | filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); |
| 360 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 361 | if (dev->driver->reclaim_buffers_locked) |
| 362 | dev->driver->reclaim_buffers_locked(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 364 | drm_lock_free(dev, &dev->lock.hw_lock->lock, |
| 365 | _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 367 | /* FIXME: may require heavy-handed reset of |
| 368 | hardware at this point, possibly |
| 369 | processed via a callback to the X |
| 370 | server. */ |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 371 | } else if (dev->driver->reclaim_buffers_locked && priv->lock_count |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 372 | && dev->lock.hw_lock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* The lock is required to reclaim buffers */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 374 | DECLARE_WAITQUEUE(entry, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 376 | add_wait_queue(&dev->lock.lock_queue, &entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | for (;;) { |
| 378 | __set_current_state(TASK_INTERRUPTIBLE); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 379 | if (!dev->lock.hw_lock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* Device has been unregistered */ |
| 381 | retcode = -EINTR; |
| 382 | break; |
| 383 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 384 | if (drm_lock_take(&dev->lock.hw_lock->lock, |
| 385 | DRM_KERNEL_CONTEXT)) { |
| 386 | dev->lock.filp = filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | dev->lock.lock_time = jiffies; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 388 | atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | break; /* Got lock */ |
| 390 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 391 | /* Contention */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | schedule(); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 393 | if (signal_pending(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | retcode = -ERESTARTSYS; |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | __set_current_state(TASK_RUNNING); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 399 | remove_wait_queue(&dev->lock.lock_queue, &entry); |
| 400 | if (!retcode) { |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 401 | dev->driver->reclaim_buffers_locked(dev, filp); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 402 | drm_lock_free(dev, &dev->lock.hw_lock->lock, |
| 403 | DRM_KERNEL_CONTEXT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 406 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 407 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && |
| 408 | !dev->driver->reclaim_buffers_locked) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | dev->driver->reclaim_buffers(dev, filp); |
| 410 | } |
| 411 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 412 | drm_fasync(-1, filp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 414 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 415 | if (dev->ctxlist && (!list_empty(&dev->ctxlist->head))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | drm_ctx_list_t *pos, *n; |
| 417 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 418 | list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) { |
| 419 | if (pos->tag == priv && |
| 420 | pos->handle != DRM_KERNEL_CONTEXT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | if (dev->driver->context_dtor) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 422 | dev->driver->context_dtor(dev, |
| 423 | pos->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 425 | drm_ctxbitmap_free(dev, pos->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 427 | list_del(&pos->head); |
| 428 | drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | --dev->ctx_count; |
| 430 | } |
| 431 | } |
| 432 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 433 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 435 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 436 | if (priv->remove_auth_on_close == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | drm_file_t *temp = dev->file_first; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 438 | while (temp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | temp->authenticated = 0; |
| 440 | temp = temp->next; |
| 441 | } |
| 442 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 443 | if (priv->prev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | priv->prev->next = priv->next; |
| 445 | } else { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 446 | dev->file_first = priv->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 448 | if (priv->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | priv->next->prev = priv->prev; |
| 450 | } else { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 451 | dev->file_last = priv->prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 453 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 454 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 455 | if (dev->driver->postclose) |
| 456 | dev->driver->postclose(dev, priv); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 457 | drm_free(priv, sizeof(*priv), DRM_MEM_FILES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | /* ======================================================== |
| 460 | * End inline drm_release |
| 461 | */ |
| 462 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 463 | atomic_inc(&dev->counts[_DRM_STAT_CLOSES]); |
| 464 | spin_lock(&dev->count_lock); |
| 465 | if (!--dev->open_count) { |
| 466 | if (atomic_read(&dev->ioctl_count) || dev->blocked) { |
| 467 | DRM_ERROR("Device busy: %d %d\n", |
| 468 | atomic_read(&dev->ioctl_count), dev->blocked); |
| 469 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | unlock_kernel(); |
| 471 | return -EBUSY; |
| 472 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 473 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | unlock_kernel(); |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 475 | return drm_lastclose(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 477 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | unlock_kernel(); |
| 480 | |
| 481 | return retcode; |
| 482 | } |
| 483 | EXPORT_SYMBOL(drm_release); |
| 484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | /** No-op. */ |
| 486 | unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait) |
| 487 | { |
| 488 | return 0; |
| 489 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 490 | EXPORT_SYMBOL(drm_poll); |