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> |
Jonathan Corbet | 70ffa16 | 2008-05-15 11:34:16 -0600 | [diff] [blame] | 40 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 42 | static int drm_open_helper(struct inode *inode, struct file *filp, |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 43 | struct drm_device * dev); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 44 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 45 | static int drm_setup(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 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 | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 56 | atomic_set(&dev->ioctl_count, 0); |
| 57 | atomic_set(&dev->vma_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | dev->buf_use = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 59 | atomic_set(&dev->buf_alloc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 61 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) { |
| 62 | i = drm_dma_setup(dev); |
| 63 | if (i < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return i; |
| 65 | } |
| 66 | |
Dave Airlie | 3d77461 | 2006-08-07 20:07:43 +1000 | [diff] [blame] | 67 | for (i = 0; i < ARRAY_SIZE(dev->counts); i++) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 68 | atomic_set(&dev->counts[i], 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Dave Airlie | 11d9c2f | 2007-02-18 17:13:39 +1100 | [diff] [blame] | 70 | dev->sigdata.lock = NULL; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame^] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | dev->queue_count = 0; |
| 73 | dev->queue_reserved = 0; |
| 74 | dev->queue_slots = 0; |
| 75 | dev->queuelist = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | dev->context_flag = 0; |
| 77 | dev->interrupt_flag = 0; |
| 78 | dev->dma_flag = 0; |
| 79 | dev->last_context = 0; |
| 80 | dev->last_switch = 0; |
| 81 | dev->last_checked = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 82 | init_waitqueue_head(&dev->context_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | dev->if_version = 0; |
| 84 | |
| 85 | dev->ctx_start = 0; |
| 86 | dev->lck_start = 0; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | dev->buf_async = NULL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 89 | init_waitqueue_head(&dev->buf_readers); |
| 90 | init_waitqueue_head(&dev->buf_writers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 92 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * The kernel's context could be created here, but is now created |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 96 | * in drm_dma_enqueue. This is more resource-efficient for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * hardware that does not do DMA, but may mean that |
| 98 | * drm_select_queue fails between the time the interrupt is |
| 99 | * initialized and the time the queues are initialized. |
| 100 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Open file. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 107 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | * \param inode device inode |
| 109 | * \param filp file pointer. |
| 110 | * \return zero on success or a negative number on failure. |
| 111 | * |
| 112 | * Searches the DRM device with the same minor number, calls open_helper(), and |
| 113 | * increments the device open count. If the open count was previous at zero, |
| 114 | * i.e., it's the first that the device is open, then calls setup(). |
| 115 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 116 | int drm_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 118 | struct drm_device *dev = NULL; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 119 | int minor_id = iminor(inode); |
| 120 | struct drm_minor *minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | int retcode = 0; |
| 122 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 123 | minor = idr_find(&drm_minors_idr, minor_id); |
| 124 | if (!minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return -ENODEV; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 126 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 127 | if (!(dev = minor->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return -ENODEV; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 129 | |
| 130 | retcode = drm_open_helper(inode, filp, dev); |
| 131 | if (!retcode) { |
| 132 | atomic_inc(&dev->counts[_DRM_STAT_OPENS]); |
| 133 | spin_lock(&dev->count_lock); |
| 134 | if (!dev->open_count++) { |
| 135 | spin_unlock(&dev->count_lock); |
| 136 | return drm_setup(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 138 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return retcode; |
| 142 | } |
| 143 | EXPORT_SYMBOL(drm_open); |
| 144 | |
| 145 | /** |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 146 | * File \c open operation. |
| 147 | * |
| 148 | * \param inode device inode. |
| 149 | * \param filp file pointer. |
| 150 | * |
| 151 | * Puts the dev->fops corresponding to the device minor number into |
| 152 | * \p filp, call the \c open method, and restore the file operations. |
| 153 | */ |
| 154 | int drm_stub_open(struct inode *inode, struct file *filp) |
| 155 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 156 | struct drm_device *dev = NULL; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 157 | struct drm_minor *minor; |
| 158 | int minor_id = iminor(inode); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 159 | int err = -ENODEV; |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 160 | const struct file_operations *old_fops; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 161 | |
| 162 | DRM_DEBUG("\n"); |
| 163 | |
Jonathan Corbet | 70ffa16 | 2008-05-15 11:34:16 -0600 | [diff] [blame] | 164 | /* BKL pushdown: note that nothing else serializes idr_find() */ |
| 165 | lock_kernel(); |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 166 | minor = idr_find(&drm_minors_idr, minor_id); |
| 167 | if (!minor) |
Jonathan Corbet | 70ffa16 | 2008-05-15 11:34:16 -0600 | [diff] [blame] | 168 | goto out; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 169 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 170 | if (!(dev = minor->dev)) |
Jonathan Corbet | 70ffa16 | 2008-05-15 11:34:16 -0600 | [diff] [blame] | 171 | goto out; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 172 | |
| 173 | old_fops = filp->f_op; |
| 174 | filp->f_op = fops_get(&dev->driver->fops); |
| 175 | if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) { |
| 176 | fops_put(filp->f_op); |
| 177 | filp->f_op = fops_get(old_fops); |
| 178 | } |
| 179 | fops_put(old_fops); |
| 180 | |
Jonathan Corbet | 70ffa16 | 2008-05-15 11:34:16 -0600 | [diff] [blame] | 181 | out: |
| 182 | unlock_kernel(); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 183 | return err; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Check whether DRI will run on this CPU. |
| 188 | * |
| 189 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. |
| 190 | */ |
| 191 | static int drm_cpu_valid(void) |
| 192 | { |
| 193 | #if defined(__i386__) |
| 194 | if (boot_cpu_data.x86 == 3) |
| 195 | return 0; /* No cmpxchg on a 386 */ |
| 196 | #endif |
| 197 | #if defined(__sparc__) && !defined(__sparc_v9__) |
| 198 | return 0; /* No cmpxchg before v9 sparc. */ |
| 199 | #endif |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Called whenever a process opens /dev/drm. |
| 205 | * |
| 206 | * \param inode device inode. |
| 207 | * \param filp file pointer. |
| 208 | * \param dev device. |
| 209 | * \return zero on success or a negative number on failure. |
| 210 | * |
| 211 | * Creates and initializes a drm_file structure for the file private data in \p |
| 212 | * filp and add it into the double linked list in \p dev. |
| 213 | */ |
| 214 | static int drm_open_helper(struct inode *inode, struct file *filp, |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 215 | struct drm_device * dev) |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 216 | { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 217 | int minor_id = iminor(inode); |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 218 | struct drm_file *priv; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 219 | int ret; |
| 220 | |
| 221 | if (filp->f_flags & O_EXCL) |
| 222 | return -EBUSY; /* No exclusive opens */ |
| 223 | if (!drm_cpu_valid()) |
| 224 | return -EINVAL; |
| 225 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 226 | DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 227 | |
| 228 | priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES); |
| 229 | if (!priv) |
| 230 | return -ENOMEM; |
| 231 | |
| 232 | memset(priv, 0, sizeof(*priv)); |
| 233 | filp->private_data = priv; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 234 | priv->filp = filp; |
David Howells | 2df68b4 | 2008-09-02 11:03:14 +1000 | [diff] [blame] | 235 | priv->uid = current_euid(); |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 236 | priv->pid = task_pid_nr(current); |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 237 | priv->minor = idr_find(&drm_minors_idr, minor_id); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 238 | priv->ioctl_count = 0; |
| 239 | /* for compatibility root is always authenticated */ |
| 240 | priv->authenticated = capable(CAP_SYS_ADMIN); |
| 241 | priv->lock_count = 0; |
| 242 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 243 | INIT_LIST_HEAD(&priv->lhead); |
| 244 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 245 | if (dev->driver->driver_features & DRIVER_GEM) |
| 246 | drm_gem_open(dev, priv); |
| 247 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 248 | if (dev->driver->open) { |
| 249 | ret = dev->driver->open(dev, priv); |
| 250 | if (ret < 0) |
| 251 | goto out_free; |
| 252 | } |
| 253 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 254 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame^] | 255 | /* if there is no current master make this fd it */ |
| 256 | mutex_lock(&dev->struct_mutex); |
| 257 | if (!priv->minor->master) { |
| 258 | /* create a new master */ |
| 259 | priv->minor->master = drm_master_create(priv->minor); |
| 260 | if (!priv->minor->master) { |
| 261 | ret = -ENOMEM; |
| 262 | goto out_free; |
| 263 | } |
| 264 | |
| 265 | priv->is_master = 1; |
| 266 | /* take another reference for the copy in the local file priv */ |
| 267 | priv->master = drm_master_get(priv->minor->master); |
| 268 | |
| 269 | priv->authenticated = 1; |
| 270 | |
| 271 | mutex_unlock(&dev->struct_mutex); |
| 272 | if (dev->driver->master_create) { |
| 273 | ret = dev->driver->master_create(dev, priv->master); |
| 274 | if (ret) { |
| 275 | mutex_lock(&dev->struct_mutex); |
| 276 | /* drop both references if this fails */ |
| 277 | drm_master_put(&priv->minor->master); |
| 278 | drm_master_put(&priv->master); |
| 279 | mutex_unlock(&dev->struct_mutex); |
| 280 | goto out_free; |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | /* get a reference to the master */ |
| 285 | priv->master = drm_master_get(priv->minor->master); |
| 286 | mutex_unlock(&dev->struct_mutex); |
| 287 | } |
| 288 | |
| 289 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 290 | list_add(&priv->lhead, &dev->filelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 291 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 292 | |
| 293 | #ifdef __alpha__ |
| 294 | /* |
| 295 | * Default the hose |
| 296 | */ |
| 297 | if (!dev->hose) { |
| 298 | struct pci_dev *pci_dev; |
| 299 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); |
| 300 | if (pci_dev) { |
| 301 | dev->hose = pci_dev->sysdata; |
| 302 | pci_dev_put(pci_dev); |
| 303 | } |
| 304 | if (!dev->hose) { |
| 305 | struct pci_bus *b = pci_bus_b(pci_root_buses.next); |
| 306 | if (b) |
| 307 | dev->hose = b->sysdata; |
| 308 | } |
| 309 | } |
| 310 | #endif |
| 311 | |
| 312 | return 0; |
| 313 | out_free: |
| 314 | drm_free(priv, sizeof(*priv), DRM_MEM_FILES); |
| 315 | filp->private_data = NULL; |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | /** No-op. */ |
| 320 | int drm_fasync(int fd, struct file *filp, int on) |
| 321 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 322 | struct drm_file *priv = filp->private_data; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 323 | struct drm_device *dev = priv->minor->dev; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 324 | int retcode; |
| 325 | |
| 326 | DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 327 | (long)old_encode_dev(priv->minor->device)); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 328 | retcode = fasync_helper(fd, filp, on, &dev->buf_async); |
| 329 | if (retcode < 0) |
| 330 | return retcode; |
| 331 | return 0; |
| 332 | } |
| 333 | EXPORT_SYMBOL(drm_fasync); |
| 334 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame^] | 335 | /* |
| 336 | * Reclaim locked buffers; note that this may be a bad idea if the current |
| 337 | * context doesn't have the hw lock... |
| 338 | */ |
| 339 | static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f) |
| 340 | { |
| 341 | struct drm_file *file_priv = f->private_data; |
| 342 | |
| 343 | if (drm_i_have_hw_lock(dev, file_priv)) { |
| 344 | dev->driver->reclaim_buffers_locked(dev, file_priv); |
| 345 | } else { |
| 346 | unsigned long _end = jiffies + 3 * DRM_HZ; |
| 347 | int locked = 0; |
| 348 | |
| 349 | drm_idlelock_take(&file_priv->master->lock); |
| 350 | |
| 351 | /* |
| 352 | * Wait for a while. |
| 353 | */ |
| 354 | do { |
| 355 | spin_lock_bh(&file_priv->master->lock.spinlock); |
| 356 | locked = file_priv->master->lock.idle_has_lock; |
| 357 | spin_unlock_bh(&file_priv->master->lock.spinlock); |
| 358 | if (locked) |
| 359 | break; |
| 360 | schedule(); |
| 361 | } while (!time_after_eq(jiffies, _end)); |
| 362 | |
| 363 | if (!locked) { |
| 364 | DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n" |
| 365 | "\tdriver to use reclaim_buffers_idlelocked() instead.\n" |
| 366 | "\tI will go on reclaiming the buffers anyway.\n"); |
| 367 | } |
| 368 | |
| 369 | dev->driver->reclaim_buffers_locked(dev, file_priv); |
| 370 | drm_idlelock_release(&file_priv->master->lock); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static void drm_master_release(struct drm_device *dev, struct file *filp) |
| 375 | { |
| 376 | struct drm_file *file_priv = filp->private_data; |
| 377 | |
| 378 | if (dev->driver->reclaim_buffers_locked && |
| 379 | file_priv->master->lock.hw_lock) |
| 380 | drm_reclaim_locked_buffers(dev, filp); |
| 381 | |
| 382 | if (dev->driver->reclaim_buffers_idlelocked && |
| 383 | file_priv->master->lock.hw_lock) { |
| 384 | drm_idlelock_take(&file_priv->master->lock); |
| 385 | dev->driver->reclaim_buffers_idlelocked(dev, file_priv); |
| 386 | drm_idlelock_release(&file_priv->master->lock); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | if (drm_i_have_hw_lock(dev, file_priv)) { |
| 391 | DRM_DEBUG("File %p released, freeing lock for context %d\n", |
| 392 | filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); |
| 393 | drm_lock_free(&file_priv->master->lock, |
| 394 | _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); |
| 395 | } |
| 396 | |
| 397 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && |
| 398 | !dev->driver->reclaim_buffers_locked) { |
| 399 | dev->driver->reclaim_buffers(dev, file_priv); |
| 400 | } |
| 401 | } |
| 402 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 403 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | * Release file. |
| 405 | * |
| 406 | * \param inode device inode |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 407 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | * \return zero on success or a negative number on failure. |
| 409 | * |
| 410 | * If the hardware lock is held then free it, and take it again for the kernel |
| 411 | * context since it's necessary to reclaim buffers. Unlink the file private |
| 412 | * 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] | 413 | * zero calls drm_lastclose(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 415 | int drm_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 417 | struct drm_file *file_priv = filp->private_data; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 418 | struct drm_device *dev = file_priv->minor->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | int retcode = 0; |
| 420 | |
| 421 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 423 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 425 | if (dev->driver->preclose) |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 426 | dev->driver->preclose(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | /* ======================================================== |
| 429 | * Begin inline drm_release |
| 430 | */ |
| 431 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 432 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 433 | task_pid_nr(current), |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 434 | (long)old_encode_dev(file_priv->minor->device), |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 435 | dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame^] | 437 | /* if the master has gone away we can't do anything with the lock */ |
| 438 | if (file_priv->minor->master) |
| 439 | drm_master_release(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 441 | if (dev->driver->driver_features & DRIVER_GEM) |
| 442 | drm_gem_release(dev, file_priv); |
| 443 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 444 | mutex_lock(&dev->ctxlist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 445 | if (!list_empty(&dev->ctxlist)) { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 446 | struct drm_ctx_list *pos, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 448 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 449 | if (pos->tag == file_priv && |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 450 | pos->handle != DRM_KERNEL_CONTEXT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (dev->driver->context_dtor) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 452 | dev->driver->context_dtor(dev, |
| 453 | pos->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 455 | drm_ctxbitmap_free(dev, pos->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 457 | list_del(&pos->head); |
| 458 | drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | --dev->ctx_count; |
| 460 | } |
| 461 | } |
| 462 | } |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 463 | mutex_unlock(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 465 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 466 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame^] | 467 | if (file_priv->is_master) { |
| 468 | struct drm_file *temp; |
| 469 | list_for_each_entry(temp, &dev->filelist, lhead) { |
| 470 | if ((temp->master == file_priv->master) && |
| 471 | (temp != file_priv)) |
| 472 | temp->authenticated = 0; |
| 473 | } |
| 474 | |
| 475 | if (file_priv->minor->master == file_priv->master) { |
| 476 | /* drop the reference held my the minor */ |
| 477 | drm_master_put(&file_priv->minor->master); |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame^] | 480 | |
| 481 | /* drop the reference held my the file priv */ |
| 482 | drm_master_put(&file_priv->master); |
| 483 | file_priv->is_master = 0; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 484 | list_del(&file_priv->lhead); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 485 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 486 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 487 | if (dev->driver->postclose) |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 488 | dev->driver->postclose(dev, file_priv); |
| 489 | drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
| 491 | /* ======================================================== |
| 492 | * End inline drm_release |
| 493 | */ |
| 494 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 495 | atomic_inc(&dev->counts[_DRM_STAT_CLOSES]); |
| 496 | spin_lock(&dev->count_lock); |
| 497 | if (!--dev->open_count) { |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame^] | 498 | if (atomic_read(&dev->ioctl_count)) { |
| 499 | DRM_ERROR("Device busy: %d\n", |
| 500 | atomic_read(&dev->ioctl_count)); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 501 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | unlock_kernel(); |
| 503 | return -EBUSY; |
| 504 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 505 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | unlock_kernel(); |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 507 | return drm_lastclose(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 509 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
| 511 | unlock_kernel(); |
| 512 | |
| 513 | return retcode; |
| 514 | } |
| 515 | EXPORT_SYMBOL(drm_release); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /** No-op. */ |
| 518 | unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait) |
| 519 | { |
| 520 | return 0; |
| 521 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 522 | EXPORT_SYMBOL(drm_poll); |