Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 37 | #include <drm/drmP.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Paul Gortmaker | e0cd360 | 2011-08-30 11:04:30 -0400 | [diff] [blame] | 40 | #include <linux/module.h> |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 41 | #include "drm_legacy.h" |
Daniel Vetter | 67d0ec4 | 2014-09-10 12:43:53 +0200 | [diff] [blame] | 42 | #include "drm_internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
David Herrmann | 0d63988 | 2014-02-24 15:53:25 +0100 | [diff] [blame] | 44 | /* from BKL pushdown */ |
Arnd Bergmann | 5837471 | 2010-07-10 23:51:39 +0200 | [diff] [blame] | 45 | DEFINE_MUTEX(drm_global_mutex); |
| 46 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 47 | /** |
| 48 | * DOC: file operations |
| 49 | * |
| 50 | * Drivers must define the file operations structure that forms the DRM |
| 51 | * userspace API entry point, even though most of those operations are |
| 52 | * implemented in the DRM core. The mandatory functions are drm_open(), |
| 53 | * drm_read(), drm_ioctl() and drm_compat_ioctl if CONFIG_COMPAT is enabled. |
| 54 | * Drivers which implement private ioctls that require 32/64 bit compatibility |
| 55 | * support must provided their onw .compat_ioctl() handler that processes |
| 56 | * private ioctls and calls drm_compat_ioctl() for core ioctls. |
| 57 | * |
| 58 | * In addition drm_read() and drm_poll() provide support for DRM events. DRM |
| 59 | * events are a generic and extensible means to send asynchronous events to |
| 60 | * userspace through the file descriptor. They are used to send vblank event and |
| 61 | * page flip completions by the KMS API. But drivers can also use it for their |
| 62 | * own needs, e.g. to signal completion of rendering. |
| 63 | * |
| 64 | * The memory mapping implementation will vary depending on how the driver |
| 65 | * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap() |
| 66 | * function, modern drivers should use one of the provided memory-manager |
| 67 | * specific implementations. For GEM-based drivers this is drm_gem_mmap(). |
| 68 | * |
| 69 | * No other file operations are supported by the DRM userspace API. Overall the |
| 70 | * following is an example #file_operations structure: |
| 71 | * |
| 72 | * static const example_drm_fops = { |
| 73 | * .owner = THIS_MODULE, |
| 74 | * .open = drm_open, |
| 75 | * .release = drm_release, |
| 76 | * .unlocked_ioctl = drm_ioctl, |
| 77 | * #ifdef CONFIG_COMPAT |
| 78 | * .compat_ioctl = drm_compat_ioctl, |
| 79 | * #endif |
| 80 | * .poll = drm_poll, |
| 81 | * .read = drm_read, |
| 82 | * .llseek = no_llseek, |
| 83 | * .mmap = drm_gem_mmap, |
| 84 | * }; |
| 85 | */ |
| 86 | |
Ilija Hadzic | 1dcc0ce | 2014-04-28 10:23:57 -0400 | [diff] [blame] | 87 | static int drm_open_helper(struct file *filp, struct drm_minor *minor); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 88 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 89 | static int drm_setup(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | int ret; |
| 92 | |
Daniel Vetter | 7d14bb6b | 2013-08-08 15:41:15 +0200 | [diff] [blame] | 93 | if (dev->driver->firstopen && |
| 94 | !drm_core_check_feature(dev, DRIVER_MODESET)) { |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 95 | ret = dev->driver->firstopen(dev); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 96 | if (ret != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return ret; |
| 98 | } |
| 99 | |
Daniel Vetter | f336ab7 | 2013-08-08 15:41:35 +0200 | [diff] [blame] | 100 | ret = drm_legacy_dma_setup(dev); |
| 101 | if (ret < 0) |
| 102 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 105 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /** |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 110 | * drm_open - open method for DRM file |
| 111 | * @inode: device inode |
| 112 | * @filp: file pointer. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 113 | * |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 114 | * This function must be used by drivers as their .open() #file_operations |
| 115 | * method. It looks up the correct DRM device and instantiates all the per-file |
| 116 | * resources for it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 118 | * RETURNS: |
| 119 | * |
| 120 | * 0 on success or negative errno value on falure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 122 | int drm_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 124 | struct drm_device *dev; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 125 | struct drm_minor *minor; |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 126 | int retcode; |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 127 | int need_setup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 129 | minor = drm_minor_acquire(iminor(inode)); |
| 130 | if (IS_ERR(minor)) |
| 131 | return PTR_ERR(minor); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 132 | |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 133 | dev = minor->dev; |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 134 | if (!dev->open_count++) |
| 135 | need_setup = 1; |
Jesse Barnes | a2c0a97 | 2008-11-05 10:31:53 -0800 | [diff] [blame] | 136 | |
David Herrmann | 6796cb1 | 2014-01-03 14:24:19 +0100 | [diff] [blame] | 137 | /* share address_space across all char-devs of a single device */ |
| 138 | filp->f_mapping = dev->anon_inode->i_mapping; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 139 | |
Ilija Hadzic | 1dcc0ce | 2014-04-28 10:23:57 -0400 | [diff] [blame] | 140 | retcode = drm_open_helper(filp, minor); |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 141 | if (retcode) |
| 142 | goto err_undo; |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 143 | if (need_setup) { |
| 144 | retcode = drm_setup(dev); |
| 145 | if (retcode) |
| 146 | goto err_undo; |
| 147 | } |
| 148 | return 0; |
| 149 | |
| 150 | err_undo: |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 151 | dev->open_count--; |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 152 | drm_minor_release(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return retcode; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | EXPORT_SYMBOL(drm_open); |
| 156 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 157 | /* |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 158 | * Check whether DRI will run on this CPU. |
| 159 | * |
| 160 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. |
| 161 | */ |
| 162 | static int drm_cpu_valid(void) |
| 163 | { |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 164 | #if defined(__sparc__) && !defined(__sparc_v9__) |
| 165 | return 0; /* No cmpxchg before v9 sparc. */ |
| 166 | #endif |
| 167 | return 1; |
| 168 | } |
| 169 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 170 | /* |
Thomas Hellstrom | a0af2e5 | 2015-12-02 09:24:46 -0800 | [diff] [blame] | 171 | * drm_new_set_master - Allocate a new master object and become master for the |
| 172 | * associated master realm. |
| 173 | * |
| 174 | * @dev: The associated device. |
| 175 | * @fpriv: File private identifying the client. |
| 176 | * |
| 177 | * This function must be called with dev::struct_mutex held. |
| 178 | * Returns negative error code on failure. Zero on success. |
| 179 | */ |
| 180 | int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv) |
| 181 | { |
| 182 | struct drm_master *old_master; |
| 183 | int ret; |
| 184 | |
| 185 | lockdep_assert_held_once(&dev->master_mutex); |
| 186 | |
| 187 | /* create a new master */ |
| 188 | fpriv->minor->master = drm_master_create(fpriv->minor); |
| 189 | if (!fpriv->minor->master) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | /* take another reference for the copy in the local file priv */ |
| 193 | old_master = fpriv->master; |
| 194 | fpriv->master = drm_master_get(fpriv->minor->master); |
| 195 | |
| 196 | if (dev->driver->master_create) { |
| 197 | ret = dev->driver->master_create(dev, fpriv->master); |
| 198 | if (ret) |
| 199 | goto out_err; |
| 200 | } |
| 201 | if (dev->driver->master_set) { |
| 202 | ret = dev->driver->master_set(dev, fpriv, true); |
| 203 | if (ret) |
| 204 | goto out_err; |
| 205 | } |
| 206 | |
| 207 | fpriv->is_master = 1; |
| 208 | fpriv->allowed_master = 1; |
| 209 | fpriv->authenticated = 1; |
| 210 | if (old_master) |
| 211 | drm_master_put(&old_master); |
| 212 | |
| 213 | return 0; |
| 214 | |
| 215 | out_err: |
| 216 | /* drop both references and restore old master on failure */ |
| 217 | drm_master_put(&fpriv->minor->master); |
| 218 | drm_master_put(&fpriv->master); |
| 219 | fpriv->master = old_master; |
| 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 224 | /* |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 225 | * Called whenever a process opens /dev/drm. |
| 226 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 227 | * \param filp file pointer. |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 228 | * \param minor acquired minor-object. |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 229 | * \return zero on success or a negative number on failure. |
| 230 | * |
| 231 | * Creates and initializes a drm_file structure for the file private data in \p |
| 232 | * filp and add it into the double linked list in \p dev. |
| 233 | */ |
Ilija Hadzic | 1dcc0ce | 2014-04-28 10:23:57 -0400 | [diff] [blame] | 234 | static int drm_open_helper(struct file *filp, struct drm_minor *minor) |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 235 | { |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 236 | struct drm_device *dev = minor->dev; |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 237 | struct drm_file *priv; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 238 | int ret; |
| 239 | |
| 240 | if (filp->f_flags & O_EXCL) |
| 241 | return -EBUSY; /* No exclusive opens */ |
| 242 | if (!drm_cpu_valid()) |
| 243 | return -EINVAL; |
Dave Airlie | 13bb9cc | 2012-09-12 15:55:05 +1000 | [diff] [blame] | 244 | if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF) |
Dave Airlie | 5bcf719 | 2010-12-07 09:20:40 +1000 | [diff] [blame] | 245 | return -EINVAL; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 246 | |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 247 | DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 248 | |
Julia Lawall | 6ebc22e | 2010-05-13 21:58:56 +0200 | [diff] [blame] | 249 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 250 | if (!priv) |
| 251 | return -ENOMEM; |
| 252 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 253 | filp->private_data = priv; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 254 | priv->filp = filp; |
David Howells | 2df68b4 | 2008-09-02 11:03:14 +1000 | [diff] [blame] | 255 | priv->uid = current_euid(); |
Eric W. Biederman | 5fce5e0 | 2012-02-07 16:47:26 -0800 | [diff] [blame] | 256 | priv->pid = get_pid(task_pid(current)); |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 257 | priv->minor = minor; |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 258 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 259 | /* for compatibility root is always authenticated */ |
David Herrmann | 3cb01a9 | 2014-07-22 17:12:26 +0200 | [diff] [blame] | 260 | priv->authenticated = capable(CAP_SYS_ADMIN); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 261 | priv->lock_count = 0; |
| 262 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 263 | INIT_LIST_HEAD(&priv->lhead); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 264 | INIT_LIST_HEAD(&priv->fbs); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 265 | mutex_init(&priv->fbs_lock); |
Daniel Stone | e2f5d2e | 2015-05-22 13:34:51 +0100 | [diff] [blame] | 266 | INIT_LIST_HEAD(&priv->blobs); |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 267 | INIT_LIST_HEAD(&priv->pending_event_list); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 268 | INIT_LIST_HEAD(&priv->event_list); |
| 269 | init_waitqueue_head(&priv->event_wait); |
| 270 | priv->event_space = 4096; /* set aside 4k for event buffer */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 271 | |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 272 | mutex_init(&priv->event_read_lock); |
| 273 | |
Andrzej Hajda | 1bcecfa | 2014-09-30 16:49:56 +0200 | [diff] [blame] | 274 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 275 | drm_gem_open(dev, priv); |
| 276 | |
Dave Airlie | 3248877 | 2011-11-25 15:21:02 +0000 | [diff] [blame] | 277 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
| 278 | drm_prime_init_file_private(&priv->prime); |
| 279 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 280 | if (dev->driver->open) { |
| 281 | ret = dev->driver->open(dev, priv); |
| 282 | if (ret < 0) |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 283 | goto out_prime_destroy; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 284 | } |
| 285 | |
David Herrmann | 1793126 | 2013-08-25 18:29:00 +0200 | [diff] [blame] | 286 | /* if there is no current master make this fd it, but do not create |
| 287 | * any master object for render clients */ |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 288 | mutex_lock(&dev->master_mutex); |
Thomas Hellstrom | 4368305 | 2014-03-13 11:07:44 +0100 | [diff] [blame] | 289 | if (drm_is_primary_client(priv) && !priv->minor->master) { |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 290 | /* create a new master */ |
Thomas Hellstrom | a0af2e5 | 2015-12-02 09:24:46 -0800 | [diff] [blame] | 291 | ret = drm_new_set_master(dev, priv); |
| 292 | if (ret) |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 293 | goto out_close; |
Thomas Hellstrom | 4368305 | 2014-03-13 11:07:44 +0100 | [diff] [blame] | 294 | } else if (drm_is_primary_client(priv)) { |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 295 | /* get a reference to the master */ |
| 296 | priv->master = drm_master_get(priv->minor->master); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 297 | } |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 298 | mutex_unlock(&dev->master_mutex); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 299 | |
| 300 | mutex_lock(&dev->struct_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 301 | list_add(&priv->lhead, &dev->filelist); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 302 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 303 | |
| 304 | #ifdef __alpha__ |
| 305 | /* |
| 306 | * Default the hose |
| 307 | */ |
| 308 | if (!dev->hose) { |
| 309 | struct pci_dev *pci_dev; |
| 310 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); |
| 311 | if (pci_dev) { |
| 312 | dev->hose = pci_dev->sysdata; |
| 313 | pci_dev_put(pci_dev); |
| 314 | } |
| 315 | if (!dev->hose) { |
Yijing Wang | 59c1ad3b | 2014-02-13 21:14:00 +0800 | [diff] [blame] | 316 | struct pci_bus *b = list_entry(pci_root_buses.next, |
| 317 | struct pci_bus, node); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 318 | if (b) |
| 319 | dev->hose = b->sysdata; |
| 320 | } |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | return 0; |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 325 | |
| 326 | out_close: |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 327 | mutex_unlock(&dev->master_mutex); |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 328 | if (dev->driver->postclose) |
| 329 | dev->driver->postclose(dev, priv); |
| 330 | out_prime_destroy: |
| 331 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
| 332 | drm_prime_destroy_file_private(&priv->prime); |
Andrzej Hajda | 1bcecfa | 2014-09-30 16:49:56 +0200 | [diff] [blame] | 333 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 334 | drm_gem_release(dev, priv); |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 335 | put_pid(priv->pid); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 336 | kfree(priv); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 337 | filp->private_data = NULL; |
| 338 | return ret; |
| 339 | } |
| 340 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 341 | static void drm_master_release(struct drm_device *dev, struct file *filp) |
| 342 | { |
| 343 | struct drm_file *file_priv = filp->private_data; |
| 344 | |
David Herrmann | bb6d822 | 2014-08-29 12:12:46 +0200 | [diff] [blame] | 345 | if (drm_legacy_i_have_hw_lock(dev, file_priv)) { |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 346 | DRM_DEBUG("File %p released, freeing lock for context %d\n", |
| 347 | filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); |
David Herrmann | bb6d822 | 2014-08-29 12:12:46 +0200 | [diff] [blame] | 348 | drm_legacy_lock_free(&file_priv->master->lock, |
| 349 | _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 350 | } |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 351 | } |
| 352 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 353 | static void drm_events_release(struct drm_file *file_priv) |
| 354 | { |
| 355 | struct drm_device *dev = file_priv->minor->dev; |
| 356 | struct drm_pending_event *e, *et; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 357 | unsigned long flags; |
| 358 | |
| 359 | spin_lock_irqsave(&dev->event_lock, flags); |
| 360 | |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 361 | /* Unlink pending events */ |
| 362 | list_for_each_entry_safe(e, et, &file_priv->pending_event_list, |
| 363 | pending_link) { |
| 364 | list_del(&e->pending_link); |
| 365 | e->file_priv = NULL; |
| 366 | } |
| 367 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 368 | /* Remove unconsumed events */ |
YoungJun Cho | 1dda680 | 2013-10-29 20:30:26 +0900 | [diff] [blame] | 369 | list_for_each_entry_safe(e, et, &file_priv->event_list, link) { |
| 370 | list_del(&e->link); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 371 | e->destroy(e); |
YoungJun Cho | 1dda680 | 2013-10-29 20:30:26 +0900 | [diff] [blame] | 372 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 373 | |
| 374 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 375 | } |
| 376 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 377 | /* |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 378 | * drm_legacy_dev_reinit |
| 379 | * |
| 380 | * Reinitializes a legacy/ums drm device in it's lastclose function. |
| 381 | */ |
| 382 | static void drm_legacy_dev_reinit(struct drm_device *dev) |
| 383 | { |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 384 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 385 | return; |
| 386 | |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 387 | dev->sigdata.lock = NULL; |
| 388 | |
| 389 | dev->context_flag = 0; |
| 390 | dev->last_context = 0; |
| 391 | dev->if_version = 0; |
| 392 | } |
| 393 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 394 | /* |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 395 | * Take down the DRM device. |
| 396 | * |
| 397 | * \param dev DRM device structure. |
| 398 | * |
| 399 | * Frees every resource in \p dev. |
| 400 | * |
| 401 | * \sa drm_device |
| 402 | */ |
| 403 | int drm_lastclose(struct drm_device * dev) |
| 404 | { |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 405 | DRM_DEBUG("\n"); |
| 406 | |
| 407 | if (dev->driver->lastclose) |
| 408 | dev->driver->lastclose(dev); |
| 409 | DRM_DEBUG("driver lastclose completed\n"); |
| 410 | |
| 411 | if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET)) |
| 412 | drm_irq_uninstall(dev); |
| 413 | |
| 414 | mutex_lock(&dev->struct_mutex); |
| 415 | |
| 416 | drm_agp_clear(dev); |
| 417 | |
| 418 | drm_legacy_sg_cleanup(dev); |
David Herrmann | 03decbe | 2014-08-29 12:12:29 +0200 | [diff] [blame] | 419 | drm_legacy_vma_flush(dev); |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 420 | drm_legacy_dma_takedown(dev); |
| 421 | |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 422 | mutex_unlock(&dev->struct_mutex); |
| 423 | |
| 424 | drm_legacy_dev_reinit(dev); |
| 425 | |
| 426 | DRM_DEBUG("lastclose completed\n"); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /** |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 431 | * drm_release - release method for DRM file |
| 432 | * @inode: device inode |
| 433 | * @filp: file pointer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | * |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 435 | * This function must be used by drivers as their .release() #file_operations |
| 436 | * method. It frees any resources associated with the open file, and if this is |
| 437 | * the last open file for the DRM device also proceeds to call drm_lastclose(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | * |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 439 | * RETURNS: |
| 440 | * |
| 441 | * Always succeeds and returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 443 | int drm_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 445 | struct drm_file *file_priv = filp->private_data; |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 446 | struct drm_minor *minor = file_priv->minor; |
| 447 | struct drm_device *dev = minor->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | int retcode = 0; |
| 449 | |
Arnd Bergmann | 5837471 | 2010-07-10 23:51:39 +0200 | [diff] [blame] | 450 | mutex_lock(&drm_global_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 452 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Chris Wilson | dff01de | 2014-07-24 14:23:10 +0100 | [diff] [blame] | 454 | mutex_lock(&dev->struct_mutex); |
| 455 | list_del(&file_priv->lhead); |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 456 | if (file_priv->magic) |
| 457 | idr_remove(&file_priv->master->magic_map, file_priv->magic); |
Chris Wilson | dff01de | 2014-07-24 14:23:10 +0100 | [diff] [blame] | 458 | mutex_unlock(&dev->struct_mutex); |
| 459 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 460 | if (dev->driver->preclose) |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 461 | dev->driver->preclose(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | /* ======================================================== |
| 464 | * Begin inline drm_release |
| 465 | */ |
| 466 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 467 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 468 | task_pid_nr(current), |
David Herrmann | 5817878 | 2014-01-29 13:12:31 +0100 | [diff] [blame] | 469 | (long)old_encode_dev(file_priv->minor->kdev->devt), |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 470 | dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 472 | /* if the master has gone away we can't do anything with the lock */ |
| 473 | if (file_priv->minor->master) |
| 474 | drm_master_release(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
Daniel Vetter | 67cb4b4 | 2011-10-26 00:31:26 +0200 | [diff] [blame] | 476 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
Daniel Vetter | a266162 | 2014-09-11 07:41:51 +0200 | [diff] [blame] | 477 | drm_legacy_reclaim_buffers(dev, file_priv); |
Daniel Vetter | 67cb4b4 | 2011-10-26 00:31:26 +0200 | [diff] [blame] | 478 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 479 | drm_events_release(file_priv); |
| 480 | |
Daniel Stone | e2f5d2e | 2015-05-22 13:34:51 +0100 | [diff] [blame] | 481 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
Kristian Høgsberg | ea39f83 | 2009-02-12 14:37:56 -0500 | [diff] [blame] | 482 | drm_fb_release(file_priv); |
Daniel Stone | e2f5d2e | 2015-05-22 13:34:51 +0100 | [diff] [blame] | 483 | drm_property_destroy_user_blobs(dev, file_priv); |
| 484 | } |
Kristian Høgsberg | ea39f83 | 2009-02-12 14:37:56 -0500 | [diff] [blame] | 485 | |
Andrzej Hajda | 1bcecfa | 2014-09-30 16:49:56 +0200 | [diff] [blame] | 486 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
Prathyush | 4e47e02 | 2012-04-14 17:22:13 +0530 | [diff] [blame] | 487 | drm_gem_release(dev, file_priv); |
| 488 | |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 489 | drm_legacy_ctxbitmap_flush(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 491 | mutex_lock(&dev->master_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 492 | |
Dave Airlie | 7963e9d | 2014-08-08 07:30:53 +1000 | [diff] [blame] | 493 | if (file_priv->is_master) { |
Thomas Hellstrom | fda714c | 2009-03-02 11:10:56 +0100 | [diff] [blame] | 494 | struct drm_master *master = file_priv->master; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 495 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 496 | /* |
Thomas Hellstrom | fda714c | 2009-03-02 11:10:56 +0100 | [diff] [blame] | 497 | * Since the master is disappearing, so is the |
| 498 | * possibility to lock. |
| 499 | */ |
David Herrmann | 3cb01a9 | 2014-07-22 17:12:26 +0200 | [diff] [blame] | 500 | mutex_lock(&dev->struct_mutex); |
Thomas Hellstrom | fda714c | 2009-03-02 11:10:56 +0100 | [diff] [blame] | 501 | if (master->lock.hw_lock) { |
| 502 | if (dev->sigdata.lock == master->lock.hw_lock) |
| 503 | dev->sigdata.lock = NULL; |
| 504 | master->lock.hw_lock = NULL; |
| 505 | master->lock.file_priv = NULL; |
| 506 | wake_up_interruptible_all(&master->lock.lock_queue); |
| 507 | } |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 508 | mutex_unlock(&dev->struct_mutex); |
Thomas Hellstrom | fda714c | 2009-03-02 11:10:56 +0100 | [diff] [blame] | 509 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 510 | if (file_priv->minor->master == file_priv->master) { |
| 511 | /* drop the reference held my the minor */ |
Thomas Hellstrom | 862302f | 2009-12-02 18:15:25 +0000 | [diff] [blame] | 512 | if (dev->driver->master_drop) |
| 513 | dev->driver->master_drop(dev, file_priv, true); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 514 | drm_master_put(&file_priv->minor->master); |
| 515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 517 | |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 518 | /* drop the master reference held by the file priv */ |
David Herrmann | 1793126 | 2013-08-25 18:29:00 +0200 | [diff] [blame] | 519 | if (file_priv->master) |
| 520 | drm_master_put(&file_priv->master); |
Dave Airlie | 7963e9d | 2014-08-08 07:30:53 +1000 | [diff] [blame] | 521 | file_priv->is_master = 0; |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 522 | mutex_unlock(&dev->master_mutex); |
| 523 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 524 | if (dev->driver->postclose) |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 525 | dev->driver->postclose(dev, file_priv); |
Dave Airlie | 3248877 | 2011-11-25 15:21:02 +0000 | [diff] [blame] | 526 | |
Daniel Vetter | 319c933 | 2013-08-15 00:02:46 +0200 | [diff] [blame] | 527 | |
Dave Airlie | 3248877 | 2011-11-25 15:21:02 +0000 | [diff] [blame] | 528 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
| 529 | drm_prime_destroy_file_private(&file_priv->prime); |
| 530 | |
Ville Syrjälä | ddde437 | 2014-08-06 14:02:50 +0300 | [diff] [blame] | 531 | WARN_ON(!list_empty(&file_priv->event_list)); |
| 532 | |
Eric W. Biederman | 5fce5e0 | 2012-02-07 16:47:26 -0800 | [diff] [blame] | 533 | put_pid(file_priv->pid); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 534 | kfree(file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | /* ======================================================== |
| 537 | * End inline drm_release |
| 538 | */ |
| 539 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 540 | if (!--dev->open_count) { |
Daniel Vetter | 43d1337 | 2013-12-11 11:35:08 +0100 | [diff] [blame] | 541 | retcode = drm_lastclose(dev); |
Dave Airlie | 2c07a21 | 2012-02-20 14:18:07 +0000 | [diff] [blame] | 542 | if (drm_device_is_unplugged(dev)) |
| 543 | drm_put_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
Arnd Bergmann | 5837471 | 2010-07-10 23:51:39 +0200 | [diff] [blame] | 545 | mutex_unlock(&drm_global_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 547 | drm_minor_release(minor); |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return retcode; |
| 550 | } |
| 551 | EXPORT_SYMBOL(drm_release); |
| 552 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 553 | /** |
| 554 | * drm_read - read method for DRM file |
| 555 | * @filp: file pointer |
| 556 | * @buffer: userspace destination pointer for the read |
| 557 | * @count: count in bytes to read |
| 558 | * @offset: offset to read |
| 559 | * |
| 560 | * This function must be used by drivers as their .read() #file_operations |
| 561 | * method iff they use DRM events for asynchronous signalling to userspace. |
| 562 | * Since events are used by the KMS API for vblank and page flip completion this |
| 563 | * means all modern display drivers must use it. |
| 564 | * |
| 565 | * @offset is ignore, DRM events are read like a pipe. Therefore drivers also |
| 566 | * must set the .llseek() #file_operation to no_llseek(). Polling support is |
| 567 | * provided by drm_poll(). |
| 568 | * |
| 569 | * This function will only ever read a full event. Therefore userspace must |
| 570 | * supply a big enough buffer to fit any event to ensure forward progress. Since |
| 571 | * the maximum event space is currently 4K it's recommended to just use that for |
| 572 | * safety. |
| 573 | * |
| 574 | * RETURNS: |
| 575 | * |
| 576 | * Number of bytes read (always aligned to full events, and can be 0) or a |
| 577 | * negative error code on failure. |
| 578 | */ |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 579 | ssize_t drm_read(struct file *filp, char __user *buffer, |
| 580 | size_t count, loff_t *offset) |
| 581 | { |
| 582 | struct drm_file *file_priv = filp->private_data; |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 583 | struct drm_device *dev = file_priv->minor->dev; |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 584 | ssize_t ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 585 | |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 586 | if (!access_ok(VERIFY_WRITE, buffer, count)) |
| 587 | return -EFAULT; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 588 | |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 589 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); |
| 590 | if (ret) |
| 591 | return ret; |
| 592 | |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 593 | for (;;) { |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 594 | struct drm_pending_event *e = NULL; |
| 595 | |
| 596 | spin_lock_irq(&dev->event_lock); |
| 597 | if (!list_empty(&file_priv->event_list)) { |
| 598 | e = list_first_entry(&file_priv->event_list, |
| 599 | struct drm_pending_event, link); |
| 600 | file_priv->event_space += e->event->length; |
| 601 | list_del(&e->link); |
| 602 | } |
| 603 | spin_unlock_irq(&dev->event_lock); |
| 604 | |
| 605 | if (e == NULL) { |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 606 | if (ret) |
| 607 | break; |
| 608 | |
| 609 | if (filp->f_flags & O_NONBLOCK) { |
| 610 | ret = -EAGAIN; |
| 611 | break; |
| 612 | } |
| 613 | |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 614 | mutex_unlock(&file_priv->event_read_lock); |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 615 | ret = wait_event_interruptible(file_priv->event_wait, |
| 616 | !list_empty(&file_priv->event_list)); |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 617 | if (ret >= 0) |
| 618 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); |
| 619 | if (ret) |
| 620 | return ret; |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 621 | } else { |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 622 | unsigned length = e->event->length; |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 623 | |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 624 | if (length > count - ret) { |
| 625 | put_back_event: |
| 626 | spin_lock_irq(&dev->event_lock); |
| 627 | file_priv->event_space -= length; |
| 628 | list_add(&e->link, &file_priv->event_list); |
| 629 | spin_unlock_irq(&dev->event_lock); |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 630 | break; |
| 631 | } |
| 632 | |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 633 | if (copy_to_user(buffer + ret, e->event, length)) { |
| 634 | if (ret == 0) |
| 635 | ret = -EFAULT; |
| 636 | goto put_back_event; |
| 637 | } |
| 638 | |
| 639 | ret += length; |
Takashi Iwai | a0a0bde | 2014-12-04 11:56:42 +0100 | [diff] [blame] | 640 | e->destroy(e); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 641 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 642 | } |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 643 | mutex_unlock(&file_priv->event_read_lock); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 644 | |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 645 | return ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 646 | } |
| 647 | EXPORT_SYMBOL(drm_read); |
| 648 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 649 | /** |
| 650 | * drm_poll - poll method for DRM file |
| 651 | * @filp: file pointer |
| 652 | * @wait: poll waiter table |
| 653 | * |
| 654 | * This function must be used by drivers as their .read() #file_operations |
| 655 | * method iff they use DRM events for asynchronous signalling to userspace. |
| 656 | * Since events are used by the KMS API for vblank and page flip completion this |
| 657 | * means all modern display drivers must use it. |
| 658 | * |
| 659 | * See also drm_read(). |
| 660 | * |
| 661 | * RETURNS: |
| 662 | * |
| 663 | * Mask of POLL flags indicating the current status of the file. |
| 664 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait) |
| 666 | { |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 667 | struct drm_file *file_priv = filp->private_data; |
| 668 | unsigned int mask = 0; |
| 669 | |
| 670 | poll_wait(filp, &file_priv->event_wait, wait); |
| 671 | |
| 672 | if (!list_empty(&file_priv->event_list)) |
| 673 | mask |= POLLIN | POLLRDNORM; |
| 674 | |
| 675 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 677 | EXPORT_SYMBOL(drm_poll); |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 678 | |
| 679 | /** |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 680 | * drm_event_reserve_init_locked - init a DRM event and reserve space for it |
| 681 | * @dev: DRM device |
| 682 | * @file_priv: DRM file private data |
| 683 | * @p: tracking structure for the pending event |
| 684 | * @e: actual event data to deliver to userspace |
| 685 | * |
| 686 | * This function prepares the passed in event for eventual delivery. If the event |
| 687 | * doesn't get delivered (because the IOCTL fails later on, before queuing up |
| 688 | * anything) then the even must be cancelled and freed using |
| 689 | * drm_event_cancel_free(). Successfully initialized events should be sent out |
| 690 | * using drm_send_event() or drm_send_event_locked() to signal completion of the |
| 691 | * asynchronous event to userspace. |
| 692 | * |
| 693 | * If callers embedded @p into a larger structure it must be allocated with |
| 694 | * kmalloc and @p must be the first member element. |
| 695 | * |
| 696 | * This is the locked version of drm_event_reserve_init() for callers which |
| 697 | * already hold dev->event_lock. |
| 698 | * |
| 699 | * RETURNS: |
| 700 | * |
| 701 | * 0 on success or a negative error code on failure. |
| 702 | */ |
| 703 | int drm_event_reserve_init_locked(struct drm_device *dev, |
| 704 | struct drm_file *file_priv, |
| 705 | struct drm_pending_event *p, |
| 706 | struct drm_event *e) |
| 707 | { |
| 708 | if (file_priv->event_space < e->length) |
| 709 | return -ENOMEM; |
| 710 | |
| 711 | file_priv->event_space -= e->length; |
| 712 | |
| 713 | p->event = e; |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 714 | list_add(&p->pending_link, &file_priv->pending_event_list); |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 715 | p->file_priv = file_priv; |
| 716 | |
| 717 | /* we *could* pass this in as arg, but everyone uses kfree: */ |
| 718 | p->destroy = (void (*) (struct drm_pending_event *)) kfree; |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | EXPORT_SYMBOL(drm_event_reserve_init_locked); |
| 723 | |
| 724 | /** |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 725 | * drm_event_reserve_init - init a DRM event and reserve space for it |
| 726 | * @dev: DRM device |
| 727 | * @file_priv: DRM file private data |
| 728 | * @p: tracking structure for the pending event |
| 729 | * @e: actual event data to deliver to userspace |
| 730 | * |
| 731 | * This function prepares the passed in event for eventual delivery. If the event |
| 732 | * doesn't get delivered (because the IOCTL fails later on, before queuing up |
| 733 | * anything) then the even must be cancelled and freed using |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 734 | * drm_event_cancel_free(). Successfully initialized events should be sent out |
| 735 | * using drm_send_event() or drm_send_event_locked() to signal completion of the |
| 736 | * asynchronous event to userspace. |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 737 | * |
| 738 | * If callers embedded @p into a larger structure it must be allocated with |
| 739 | * kmalloc and @p must be the first member element. |
| 740 | * |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 741 | * Callers which already hold dev->event_lock should use |
| 742 | * drm_event_reserve_init() instead. |
| 743 | * |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 744 | * RETURNS: |
| 745 | * |
| 746 | * 0 on success or a negative error code on failure. |
| 747 | */ |
| 748 | int drm_event_reserve_init(struct drm_device *dev, |
| 749 | struct drm_file *file_priv, |
| 750 | struct drm_pending_event *p, |
| 751 | struct drm_event *e) |
| 752 | { |
| 753 | unsigned long flags; |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 754 | int ret; |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 755 | |
| 756 | spin_lock_irqsave(&dev->event_lock, flags); |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 757 | ret = drm_event_reserve_init_locked(dev, file_priv, p, e); |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 758 | spin_unlock_irqrestore(&dev->event_lock, flags); |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 759 | |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | EXPORT_SYMBOL(drm_event_reserve_init); |
| 763 | |
| 764 | /** |
| 765 | * drm_event_cancel_free - free a DRM event and release it's space |
| 766 | * @dev: DRM device |
| 767 | * @p: tracking structure for the pending event |
| 768 | * |
| 769 | * This function frees the event @p initialized with drm_event_reserve_init() |
| 770 | * and releases any allocated space. |
| 771 | */ |
| 772 | void drm_event_cancel_free(struct drm_device *dev, |
| 773 | struct drm_pending_event *p) |
| 774 | { |
| 775 | unsigned long flags; |
| 776 | spin_lock_irqsave(&dev->event_lock, flags); |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 777 | if (p->file_priv) { |
| 778 | p->file_priv->event_space += p->event->length; |
| 779 | list_del(&p->pending_link); |
| 780 | } |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 781 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 782 | p->destroy(p); |
| 783 | } |
| 784 | EXPORT_SYMBOL(drm_event_cancel_free); |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 785 | |
| 786 | /** |
| 787 | * drm_send_event_locked - send DRM event to file descriptor |
| 788 | * @dev: DRM device |
| 789 | * @e: DRM event to deliver |
| 790 | * |
| 791 | * This function sends the event @e, initialized with drm_event_reserve_init(), |
| 792 | * to its associated userspace DRM file. Callers must already hold |
| 793 | * dev->event_lock, see drm_send_event() for the unlocked version. |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 794 | * |
| 795 | * Note that the core will take care of unlinking and disarming events when the |
| 796 | * corresponding DRM file is closed. Drivers need not worry about whether the |
| 797 | * DRM file for this event still exists and can call this function upon |
| 798 | * completion of the asynchronous work unconditionally. |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 799 | */ |
| 800 | void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e) |
| 801 | { |
| 802 | assert_spin_locked(&dev->event_lock); |
| 803 | |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 804 | if (!e->file_priv) { |
| 805 | e->destroy(e); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | list_del(&e->pending_link); |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 810 | list_add_tail(&e->link, |
| 811 | &e->file_priv->event_list); |
| 812 | wake_up_interruptible(&e->file_priv->event_wait); |
| 813 | } |
| 814 | EXPORT_SYMBOL(drm_send_event_locked); |
| 815 | |
| 816 | /** |
| 817 | * drm_send_event - send DRM event to file descriptor |
| 818 | * @dev: DRM device |
| 819 | * @e: DRM event to deliver |
| 820 | * |
| 821 | * This function sends the event @e, initialized with drm_event_reserve_init(), |
| 822 | * to its associated userspace DRM file. This function acquires dev->event_lock, |
| 823 | * see drm_send_event_locked() for callers which already hold this lock. |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 824 | * |
| 825 | * Note that the core will take care of unlinking and disarming events when the |
| 826 | * corresponding DRM file is closed. Drivers need not worry about whether the |
| 827 | * DRM file for this event still exists and can call this function upon |
| 828 | * completion of the asynchronous work unconditionally. |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 829 | */ |
| 830 | void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) |
| 831 | { |
| 832 | unsigned long irqflags; |
| 833 | |
| 834 | spin_lock_irqsave(&dev->event_lock, irqflags); |
| 835 | drm_send_event_locked(dev, e); |
| 836 | spin_unlock_irqrestore(&dev->event_lock, irqflags); |
| 837 | } |
| 838 | EXPORT_SYMBOL(drm_send_event); |