Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
| 3 | * \author Daryll Strauss <daryll@valinux.com> |
| 4 | * \author Gareth Hughes <gareth@valinux.com> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com |
| 9 | * |
| 10 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
| 11 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 12 | * All Rights Reserved. |
| 13 | * |
| 14 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 15 | * copy of this software and associated documentation files (the "Software"), |
| 16 | * to deal in the Software without restriction, including without limitation |
| 17 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 18 | * and/or sell copies of the Software, and to permit persons to whom the |
| 19 | * Software is furnished to do so, subject to the following conditions: |
| 20 | * |
| 21 | * The above copyright notice and this permission notice (including the next |
| 22 | * paragraph) shall be included in all copies or substantial portions of the |
| 23 | * Software. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 28 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 29 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 30 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 31 | * OTHER DEALINGS IN THE SOFTWARE. |
| 32 | */ |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Paul Gortmaker | e0cd360 | 2011-08-30 11:04:30 -0400 | [diff] [blame] | 36 | #include <linux/module.h> |
Daniel Vetter | a8f8b1d | 2017-03-08 15:12:42 +0100 | [diff] [blame] | 37 | |
| 38 | #include <drm/drm_file.h> |
| 39 | #include <drm/drmP.h> |
| 40 | |
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" |
Daniel Vetter | 8106554 | 2016-06-21 10:54:13 +0200 | [diff] [blame] | 43 | #include "drm_crtc_internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
David Herrmann | 0d63988 | 2014-02-24 15:53:25 +0100 | [diff] [blame] | 45 | /* from BKL pushdown */ |
Arnd Bergmann | 5837471 | 2010-07-10 23:51:39 +0200 | [diff] [blame] | 46 | DEFINE_MUTEX(drm_global_mutex); |
| 47 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 48 | /** |
| 49 | * DOC: file operations |
| 50 | * |
| 51 | * Drivers must define the file operations structure that forms the DRM |
| 52 | * userspace API entry point, even though most of those operations are |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 53 | * implemented in the DRM core. The resulting &struct file_operations must be |
| 54 | * stored in the &drm_driver.fops field. The mandatory functions are drm_open(), |
Jani Nikula | 55edf41 | 2016-11-01 17:40:44 +0200 | [diff] [blame] | 55 | * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 56 | * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no |
| 57 | * need to sprinkle #ifdef into the code. Drivers which implement private ioctls |
| 58 | * that require 32/64 bit compatibility support must provide their own |
| 59 | * &file_operations.compat_ioctl handler that processes private ioctls and calls |
| 60 | * drm_compat_ioctl() for core ioctls. |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 61 | * |
| 62 | * In addition drm_read() and drm_poll() provide support for DRM events. DRM |
| 63 | * events are a generic and extensible means to send asynchronous events to |
| 64 | * userspace through the file descriptor. They are used to send vblank event and |
| 65 | * page flip completions by the KMS API. But drivers can also use it for their |
| 66 | * own needs, e.g. to signal completion of rendering. |
| 67 | * |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 68 | * For the driver-side event interface see drm_event_reserve_init() and |
| 69 | * drm_send_event() as the main starting points. |
| 70 | * |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 71 | * The memory mapping implementation will vary depending on how the driver |
| 72 | * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap() |
| 73 | * function, modern drivers should use one of the provided memory-manager |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 74 | * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and |
| 75 | * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap(). |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 76 | * |
| 77 | * No other file operations are supported by the DRM userspace API. Overall the |
Daniel Vetter | bb2eaba | 2017-05-31 11:20:45 +0200 | [diff] [blame] | 78 | * following is an example &file_operations structure:: |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 79 | * |
| 80 | * static const example_drm_fops = { |
| 81 | * .owner = THIS_MODULE, |
| 82 | * .open = drm_open, |
| 83 | * .release = drm_release, |
| 84 | * .unlocked_ioctl = drm_ioctl, |
Jani Nikula | 55edf41 | 2016-11-01 17:40:44 +0200 | [diff] [blame] | 85 | * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 86 | * .poll = drm_poll, |
| 87 | * .read = drm_read, |
| 88 | * .llseek = no_llseek, |
| 89 | * .mmap = drm_gem_mmap, |
| 90 | * }; |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 91 | * |
Daniel Vetter | f42e181 | 2017-03-08 15:12:57 +0100 | [diff] [blame] | 92 | * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for |
| 93 | * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this |
| 94 | * simpler. |
Daniel Vetter | bb2eaba | 2017-05-31 11:20:45 +0200 | [diff] [blame] | 95 | * |
| 96 | * The driver's &file_operations must be stored in &drm_driver.fops. |
| 97 | * |
| 98 | * For driver-private IOCTL handling see the more detailed discussion in |
| 99 | * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`. |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 100 | */ |
| 101 | |
Ilija Hadzic | 1dcc0ce | 2014-04-28 10:23:57 -0400 | [diff] [blame] | 102 | static int drm_open_helper(struct file *filp, struct drm_minor *minor); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 103 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 104 | static int drm_setup(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | int ret; |
| 107 | |
Daniel Vetter | 7d14bb6b | 2013-08-08 15:41:15 +0200 | [diff] [blame] | 108 | if (dev->driver->firstopen && |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 109 | drm_core_check_feature(dev, DRIVER_LEGACY)) { |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 110 | ret = dev->driver->firstopen(dev); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 111 | if (ret != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return ret; |
| 113 | } |
| 114 | |
Daniel Vetter | f336ab7 | 2013-08-08 15:41:35 +0200 | [diff] [blame] | 115 | ret = drm_legacy_dma_setup(dev); |
| 116 | if (ret < 0) |
| 117 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 120 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /** |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 125 | * drm_open - open method for DRM file |
| 126 | * @inode: device inode |
| 127 | * @filp: file pointer. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 128 | * |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 129 | * This function must be used by drivers as their &file_operations.open method. |
| 130 | * It looks up the correct DRM device and instantiates all the per-file |
| 131 | * resources for it. It also calls the &drm_driver.open driver callback. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 133 | * RETURNS: |
| 134 | * |
| 135 | * 0 on success or negative errno value on falure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 137 | int drm_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 139 | struct drm_device *dev; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 140 | struct drm_minor *minor; |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 141 | int retcode; |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 142 | int need_setup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 144 | minor = drm_minor_acquire(iminor(inode)); |
| 145 | if (IS_ERR(minor)) |
| 146 | return PTR_ERR(minor); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 147 | |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 148 | dev = minor->dev; |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 149 | if (!dev->open_count++) |
| 150 | need_setup = 1; |
Jesse Barnes | a2c0a97 | 2008-11-05 10:31:53 -0800 | [diff] [blame] | 151 | |
David Herrmann | 6796cb1 | 2014-01-03 14:24:19 +0100 | [diff] [blame] | 152 | /* share address_space across all char-devs of a single device */ |
| 153 | filp->f_mapping = dev->anon_inode->i_mapping; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 154 | |
Ilija Hadzic | 1dcc0ce | 2014-04-28 10:23:57 -0400 | [diff] [blame] | 155 | retcode = drm_open_helper(filp, minor); |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 156 | if (retcode) |
| 157 | goto err_undo; |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 158 | if (need_setup) { |
| 159 | retcode = drm_setup(dev); |
| 160 | if (retcode) |
| 161 | goto err_undo; |
| 162 | } |
| 163 | return 0; |
| 164 | |
| 165 | err_undo: |
Ilija Hadzic | fdb40a0 | 2012-10-29 17:35:01 +0000 | [diff] [blame] | 166 | dev->open_count--; |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 167 | drm_minor_release(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return retcode; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | EXPORT_SYMBOL(drm_open); |
| 171 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 172 | /* |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 173 | * Check whether DRI will run on this CPU. |
| 174 | * |
| 175 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. |
| 176 | */ |
| 177 | static int drm_cpu_valid(void) |
| 178 | { |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 179 | #if defined(__sparc__) && !defined(__sparc_v9__) |
| 180 | return 0; /* No cmpxchg before v9 sparc. */ |
| 181 | #endif |
| 182 | return 1; |
| 183 | } |
| 184 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 185 | /* |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 186 | * Called whenever a process opens /dev/drm. |
| 187 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 188 | * \param filp file pointer. |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 189 | * \param minor acquired minor-object. |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 190 | * \return zero on success or a negative number on failure. |
| 191 | * |
| 192 | * Creates and initializes a drm_file structure for the file private data in \p |
| 193 | * filp and add it into the double linked list in \p dev. |
| 194 | */ |
Ilija Hadzic | 1dcc0ce | 2014-04-28 10:23:57 -0400 | [diff] [blame] | 195 | static int drm_open_helper(struct file *filp, struct drm_minor *minor) |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 196 | { |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 197 | struct drm_device *dev = minor->dev; |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 198 | struct drm_file *priv; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 199 | int ret; |
| 200 | |
| 201 | if (filp->f_flags & O_EXCL) |
| 202 | return -EBUSY; /* No exclusive opens */ |
| 203 | if (!drm_cpu_valid()) |
| 204 | return -EINVAL; |
Dave Airlie | 13bb9cc | 2012-09-12 15:55:05 +1000 | [diff] [blame] | 205 | 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] | 206 | return -EINVAL; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 207 | |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 208 | 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] | 209 | |
Julia Lawall | 6ebc22e | 2010-05-13 21:58:56 +0200 | [diff] [blame] | 210 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 211 | if (!priv) |
| 212 | return -ENOMEM; |
| 213 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 214 | filp->private_data = priv; |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 215 | priv->filp = filp; |
Eric W. Biederman | 5fce5e0 | 2012-02-07 16:47:26 -0800 | [diff] [blame] | 216 | priv->pid = get_pid(task_pid(current)); |
David Herrmann | f4aede2 | 2014-01-29 10:18:02 +0100 | [diff] [blame] | 217 | priv->minor = minor; |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 218 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 219 | /* for compatibility root is always authenticated */ |
David Herrmann | 3cb01a9 | 2014-07-22 17:12:26 +0200 | [diff] [blame] | 220 | priv->authenticated = capable(CAP_SYS_ADMIN); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 221 | priv->lock_count = 0; |
| 222 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 223 | INIT_LIST_HEAD(&priv->lhead); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 224 | INIT_LIST_HEAD(&priv->fbs); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 225 | mutex_init(&priv->fbs_lock); |
Daniel Stone | e2f5d2e | 2015-05-22 13:34:51 +0100 | [diff] [blame] | 226 | INIT_LIST_HEAD(&priv->blobs); |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 227 | INIT_LIST_HEAD(&priv->pending_event_list); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 228 | INIT_LIST_HEAD(&priv->event_list); |
| 229 | init_waitqueue_head(&priv->event_wait); |
| 230 | priv->event_space = 4096; /* set aside 4k for event buffer */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 231 | |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 232 | mutex_init(&priv->event_read_lock); |
| 233 | |
Andrzej Hajda | 1bcecfa | 2014-09-30 16:49:56 +0200 | [diff] [blame] | 234 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 235 | drm_gem_open(dev, priv); |
| 236 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 237 | if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 238 | drm_syncobj_open(priv); |
| 239 | |
Dave Airlie | 3248877 | 2011-11-25 15:21:02 +0000 | [diff] [blame] | 240 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
| 241 | drm_prime_init_file_private(&priv->prime); |
| 242 | |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 243 | if (dev->driver->open) { |
| 244 | ret = dev->driver->open(dev, priv); |
| 245 | if (ret < 0) |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 246 | goto out_prime_destroy; |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 247 | } |
| 248 | |
Daniel Vetter | 2cbae7e | 2016-06-14 20:51:00 +0200 | [diff] [blame] | 249 | if (drm_is_primary_client(priv)) { |
| 250 | ret = drm_master_open(priv); |
Thomas Hellstrom | a0af2e5 | 2015-12-02 09:24:46 -0800 | [diff] [blame] | 251 | if (ret) |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 252 | goto out_close; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 253 | } |
| 254 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 255 | mutex_lock(&dev->filelist_mutex); |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 256 | list_add(&priv->lhead, &dev->filelist); |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 257 | mutex_unlock(&dev->filelist_mutex); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 258 | |
| 259 | #ifdef __alpha__ |
| 260 | /* |
| 261 | * Default the hose |
| 262 | */ |
| 263 | if (!dev->hose) { |
| 264 | struct pci_dev *pci_dev; |
| 265 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); |
| 266 | if (pci_dev) { |
| 267 | dev->hose = pci_dev->sysdata; |
| 268 | pci_dev_put(pci_dev); |
| 269 | } |
| 270 | if (!dev->hose) { |
Yijing Wang | 59c1ad3b | 2014-02-13 21:14:00 +0800 | [diff] [blame] | 271 | struct pci_bus *b = list_entry(pci_root_buses.next, |
| 272 | struct pci_bus, node); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 273 | if (b) |
| 274 | dev->hose = b->sysdata; |
| 275 | } |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | return 0; |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 280 | |
| 281 | out_close: |
| 282 | if (dev->driver->postclose) |
| 283 | dev->driver->postclose(dev, priv); |
| 284 | out_prime_destroy: |
| 285 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
| 286 | drm_prime_destroy_file_private(&priv->prime); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 287 | if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 288 | drm_syncobj_release(priv); |
Andrzej Hajda | 1bcecfa | 2014-09-30 16:49:56 +0200 | [diff] [blame] | 289 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 290 | drm_gem_release(dev, priv); |
Seung-Woo Kim | df9b6a9 | 2013-07-02 09:53:28 +0900 | [diff] [blame] | 291 | put_pid(priv->pid); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 292 | kfree(priv); |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 293 | filp->private_data = NULL; |
| 294 | return ret; |
| 295 | } |
| 296 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 297 | static void drm_events_release(struct drm_file *file_priv) |
| 298 | { |
| 299 | struct drm_device *dev = file_priv->minor->dev; |
| 300 | struct drm_pending_event *e, *et; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 301 | unsigned long flags; |
| 302 | |
| 303 | spin_lock_irqsave(&dev->event_lock, flags); |
| 304 | |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 305 | /* Unlink pending events */ |
| 306 | list_for_each_entry_safe(e, et, &file_priv->pending_event_list, |
| 307 | pending_link) { |
| 308 | list_del(&e->pending_link); |
| 309 | e->file_priv = NULL; |
| 310 | } |
| 311 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 312 | /* Remove unconsumed events */ |
YoungJun Cho | 1dda680 | 2013-10-29 20:30:26 +0900 | [diff] [blame] | 313 | list_for_each_entry_safe(e, et, &file_priv->event_list, link) { |
| 314 | list_del(&e->link); |
Gustavo Padovan | 1b47aaf | 2016-06-02 00:06:35 +0200 | [diff] [blame] | 315 | kfree(e); |
YoungJun Cho | 1dda680 | 2013-10-29 20:30:26 +0900 | [diff] [blame] | 316 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 317 | |
| 318 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 319 | } |
| 320 | |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 321 | static void drm_legacy_dev_reinit(struct drm_device *dev) |
| 322 | { |
Daniel Vetter | 68dfbeb | 2016-04-26 19:29:35 +0200 | [diff] [blame] | 323 | if (dev->irq_enabled) |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 324 | drm_irq_uninstall(dev); |
| 325 | |
| 326 | mutex_lock(&dev->struct_mutex); |
| 327 | |
Daniel Vetter | 366884b | 2016-04-26 19:29:34 +0200 | [diff] [blame] | 328 | drm_legacy_agp_clear(dev); |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 329 | |
| 330 | drm_legacy_sg_cleanup(dev); |
David Herrmann | 03decbe | 2014-08-29 12:12:29 +0200 | [diff] [blame] | 331 | drm_legacy_vma_flush(dev); |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 332 | drm_legacy_dma_takedown(dev); |
| 333 | |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 334 | mutex_unlock(&dev->struct_mutex); |
| 335 | |
Daniel Vetter | 68dfbeb | 2016-04-26 19:29:35 +0200 | [diff] [blame] | 336 | dev->sigdata.lock = NULL; |
| 337 | |
| 338 | dev->context_flag = 0; |
| 339 | dev->last_context = 0; |
| 340 | dev->if_version = 0; |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 341 | |
| 342 | DRM_DEBUG("lastclose completed\n"); |
Daniel Vetter | 68dfbeb | 2016-04-26 19:29:35 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Daniel Vetter | 68dfbeb | 2016-04-26 19:29:35 +0200 | [diff] [blame] | 345 | void drm_lastclose(struct drm_device * dev) |
| 346 | { |
| 347 | DRM_DEBUG("\n"); |
| 348 | |
| 349 | if (dev->driver->lastclose) |
| 350 | dev->driver->lastclose(dev); |
| 351 | DRM_DEBUG("driver lastclose completed\n"); |
| 352 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 353 | if (drm_core_check_feature(dev, DRIVER_LEGACY)) |
Daniel Vetter | 68dfbeb | 2016-04-26 19:29:35 +0200 | [diff] [blame] | 354 | drm_legacy_dev_reinit(dev); |
David Herrmann | 1c8887d | 2013-10-02 11:23:36 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 358 | * drm_release - release method for DRM file |
| 359 | * @inode: device inode |
| 360 | * @filp: file pointer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 362 | * This function must be used by drivers as their &file_operations.release |
| 363 | * method. It frees any resources associated with the open file, and calls the |
Daniel Vetter | 45c3d21 | 2017-05-08 10:26:33 +0200 | [diff] [blame] | 364 | * &drm_driver.postclose driver callback. If this is the last open file for the |
| 365 | * DRM device also proceeds to call the &drm_driver.lastclose driver callback. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | * |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 367 | * RETURNS: |
| 368 | * |
| 369 | * Always succeeds and returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 371 | int drm_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 373 | struct drm_file *file_priv = filp->private_data; |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 374 | struct drm_minor *minor = file_priv->minor; |
| 375 | struct drm_device *dev = minor->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Arnd Bergmann | 5837471 | 2010-07-10 23:51:39 +0200 | [diff] [blame] | 377 | mutex_lock(&drm_global_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 379 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 381 | mutex_lock(&dev->filelist_mutex); |
Chris Wilson | dff01de | 2014-07-24 14:23:10 +0100 | [diff] [blame] | 382 | list_del(&file_priv->lhead); |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 383 | mutex_unlock(&dev->filelist_mutex); |
| 384 | |
Daniel Vetter | 45c3d21 | 2017-05-08 10:26:33 +0200 | [diff] [blame] | 385 | if (drm_core_check_feature(dev, DRIVER_LEGACY) && |
| 386 | dev->driver->preclose) |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 387 | dev->driver->preclose(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | /* ======================================================== |
| 390 | * Begin inline drm_release |
| 391 | */ |
| 392 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 393 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 394 | task_pid_nr(current), |
David Herrmann | 5817878 | 2014-01-29 13:12:31 +0100 | [diff] [blame] | 395 | (long)old_encode_dev(file_priv->minor->kdev->devt), |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 396 | dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 398 | if (drm_core_check_feature(dev, DRIVER_LEGACY)) |
Daniel Vetter | 1a75a22 | 2016-06-14 20:50:57 +0200 | [diff] [blame] | 399 | drm_legacy_lock_release(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Daniel Vetter | 67cb4b4 | 2011-10-26 00:31:26 +0200 | [diff] [blame] | 401 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
Daniel Vetter | a266162 | 2014-09-11 07:41:51 +0200 | [diff] [blame] | 402 | drm_legacy_reclaim_buffers(dev, file_priv); |
Daniel Vetter | 67cb4b4 | 2011-10-26 00:31:26 +0200 | [diff] [blame] | 403 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 404 | drm_events_release(file_priv); |
| 405 | |
Daniel Stone | e2f5d2e | 2015-05-22 13:34:51 +0100 | [diff] [blame] | 406 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
Kristian Høgsberg | ea39f83 | 2009-02-12 14:37:56 -0500 | [diff] [blame] | 407 | drm_fb_release(file_priv); |
Daniel Stone | e2f5d2e | 2015-05-22 13:34:51 +0100 | [diff] [blame] | 408 | drm_property_destroy_user_blobs(dev, file_priv); |
| 409 | } |
Kristian Høgsberg | ea39f83 | 2009-02-12 14:37:56 -0500 | [diff] [blame] | 410 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 411 | if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 412 | drm_syncobj_release(file_priv); |
| 413 | |
Andrzej Hajda | 1bcecfa | 2014-09-30 16:49:56 +0200 | [diff] [blame] | 414 | if (drm_core_check_feature(dev, DRIVER_GEM)) |
Prathyush | 4e47e02 | 2012-04-14 17:22:13 +0530 | [diff] [blame] | 415 | drm_gem_release(dev, file_priv); |
| 416 | |
David Herrmann | e7b96070 | 2014-07-24 12:10:04 +0200 | [diff] [blame] | 417 | drm_legacy_ctxbitmap_flush(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Daniel Vetter | 14d71eb | 2016-06-14 20:51:01 +0200 | [diff] [blame] | 419 | if (drm_is_primary_client(file_priv)) |
| 420 | drm_master_release(file_priv); |
Thomas Hellstrom | c996fd0 | 2014-02-25 19:57:44 +0100 | [diff] [blame] | 421 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 422 | if (dev->driver->postclose) |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 423 | dev->driver->postclose(dev, file_priv); |
Dave Airlie | 3248877 | 2011-11-25 15:21:02 +0000 | [diff] [blame] | 424 | |
| 425 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
| 426 | drm_prime_destroy_file_private(&file_priv->prime); |
| 427 | |
Ville Syrjälä | ddde437 | 2014-08-06 14:02:50 +0300 | [diff] [blame] | 428 | WARN_ON(!list_empty(&file_priv->event_list)); |
| 429 | |
Eric W. Biederman | 5fce5e0 | 2012-02-07 16:47:26 -0800 | [diff] [blame] | 430 | put_pid(file_priv->pid); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 431 | kfree(file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | /* ======================================================== |
| 434 | * End inline drm_release |
| 435 | */ |
| 436 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 437 | if (!--dev->open_count) { |
Daniel Vetter | 68dfbeb | 2016-04-26 19:29:35 +0200 | [diff] [blame] | 438 | drm_lastclose(dev); |
Daniel Vetter | c07dcd6 | 2017-08-02 13:56:02 +0200 | [diff] [blame] | 439 | if (drm_dev_is_unplugged(dev)) |
Dave Airlie | 2c07a21 | 2012-02-20 14:18:07 +0000 | [diff] [blame] | 440 | drm_put_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
Arnd Bergmann | 5837471 | 2010-07-10 23:51:39 +0200 | [diff] [blame] | 442 | mutex_unlock(&drm_global_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
David Herrmann | 1616c52 | 2014-01-29 10:49:19 +0100 | [diff] [blame] | 444 | drm_minor_release(minor); |
| 445 | |
Daniel Vetter | 68dfbeb | 2016-04-26 19:29:35 +0200 | [diff] [blame] | 446 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | EXPORT_SYMBOL(drm_release); |
| 449 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 450 | /** |
| 451 | * drm_read - read method for DRM file |
| 452 | * @filp: file pointer |
| 453 | * @buffer: userspace destination pointer for the read |
| 454 | * @count: count in bytes to read |
| 455 | * @offset: offset to read |
| 456 | * |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 457 | * This function must be used by drivers as their &file_operations.read |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 458 | * method iff they use DRM events for asynchronous signalling to userspace. |
| 459 | * Since events are used by the KMS API for vblank and page flip completion this |
| 460 | * means all modern display drivers must use it. |
| 461 | * |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 462 | * @offset is ignored, DRM events are read like a pipe. Therefore drivers also |
| 463 | * must set the &file_operation.llseek to no_llseek(). Polling support is |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 464 | * provided by drm_poll(). |
| 465 | * |
| 466 | * This function will only ever read a full event. Therefore userspace must |
| 467 | * supply a big enough buffer to fit any event to ensure forward progress. Since |
| 468 | * the maximum event space is currently 4K it's recommended to just use that for |
| 469 | * safety. |
| 470 | * |
| 471 | * RETURNS: |
| 472 | * |
| 473 | * Number of bytes read (always aligned to full events, and can be 0) or a |
| 474 | * negative error code on failure. |
| 475 | */ |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 476 | ssize_t drm_read(struct file *filp, char __user *buffer, |
| 477 | size_t count, loff_t *offset) |
| 478 | { |
| 479 | struct drm_file *file_priv = filp->private_data; |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 480 | struct drm_device *dev = file_priv->minor->dev; |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 481 | ssize_t ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 482 | |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 483 | if (!access_ok(VERIFY_WRITE, buffer, count)) |
| 484 | return -EFAULT; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 485 | |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 486 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); |
| 487 | if (ret) |
| 488 | return ret; |
| 489 | |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 490 | for (;;) { |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 491 | struct drm_pending_event *e = NULL; |
| 492 | |
| 493 | spin_lock_irq(&dev->event_lock); |
| 494 | if (!list_empty(&file_priv->event_list)) { |
| 495 | e = list_first_entry(&file_priv->event_list, |
| 496 | struct drm_pending_event, link); |
| 497 | file_priv->event_space += e->event->length; |
| 498 | list_del(&e->link); |
| 499 | } |
| 500 | spin_unlock_irq(&dev->event_lock); |
| 501 | |
| 502 | if (e == NULL) { |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 503 | if (ret) |
| 504 | break; |
| 505 | |
| 506 | if (filp->f_flags & O_NONBLOCK) { |
| 507 | ret = -EAGAIN; |
| 508 | break; |
| 509 | } |
| 510 | |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 511 | mutex_unlock(&file_priv->event_read_lock); |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 512 | ret = wait_event_interruptible(file_priv->event_wait, |
| 513 | !list_empty(&file_priv->event_list)); |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 514 | if (ret >= 0) |
| 515 | ret = mutex_lock_interruptible(&file_priv->event_read_lock); |
| 516 | if (ret) |
| 517 | return ret; |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 518 | } else { |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 519 | unsigned length = e->event->length; |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 520 | |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 521 | if (length > count - ret) { |
| 522 | put_back_event: |
| 523 | spin_lock_irq(&dev->event_lock); |
| 524 | file_priv->event_space -= length; |
| 525 | list_add(&e->link, &file_priv->event_list); |
| 526 | spin_unlock_irq(&dev->event_lock); |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 527 | break; |
| 528 | } |
| 529 | |
Chris Wilson | 83eb64c | 2015-11-25 14:39:02 +0000 | [diff] [blame] | 530 | if (copy_to_user(buffer + ret, e->event, length)) { |
| 531 | if (ret == 0) |
| 532 | ret = -EFAULT; |
| 533 | goto put_back_event; |
| 534 | } |
| 535 | |
| 536 | ret += length; |
Gustavo Padovan | 1b47aaf | 2016-06-02 00:06:35 +0200 | [diff] [blame] | 537 | kfree(e); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 538 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 539 | } |
Chris Wilson | 9b2c0b7 | 2015-11-25 14:39:03 +0000 | [diff] [blame] | 540 | mutex_unlock(&file_priv->event_read_lock); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 541 | |
Chris Wilson | cdd1cf7 | 2014-12-04 21:03:25 +0000 | [diff] [blame] | 542 | return ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 543 | } |
| 544 | EXPORT_SYMBOL(drm_read); |
| 545 | |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 546 | /** |
| 547 | * drm_poll - poll method for DRM file |
| 548 | * @filp: file pointer |
| 549 | * @wait: poll waiter table |
| 550 | * |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 551 | * This function must be used by drivers as their &file_operations.read method |
| 552 | * iff they use DRM events for asynchronous signalling to userspace. Since |
| 553 | * events are used by the KMS API for vblank and page flip completion this means |
| 554 | * all modern display drivers must use it. |
Daniel Vetter | bcb877e | 2016-01-11 22:40:55 +0100 | [diff] [blame] | 555 | * |
| 556 | * See also drm_read(). |
| 557 | * |
| 558 | * RETURNS: |
| 559 | * |
| 560 | * Mask of POLL flags indicating the current status of the file. |
| 561 | */ |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 562 | __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 564 | struct drm_file *file_priv = filp->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 565 | __poll_t mask = 0; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 566 | |
| 567 | poll_wait(filp, &file_priv->event_wait, wait); |
| 568 | |
| 569 | if (!list_empty(&file_priv->event_list)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame^] | 570 | mask |= EPOLLIN | EPOLLRDNORM; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 571 | |
| 572 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 574 | EXPORT_SYMBOL(drm_poll); |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 575 | |
| 576 | /** |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 577 | * drm_event_reserve_init_locked - init a DRM event and reserve space for it |
| 578 | * @dev: DRM device |
| 579 | * @file_priv: DRM file private data |
| 580 | * @p: tracking structure for the pending event |
| 581 | * @e: actual event data to deliver to userspace |
| 582 | * |
| 583 | * This function prepares the passed in event for eventual delivery. If the event |
| 584 | * doesn't get delivered (because the IOCTL fails later on, before queuing up |
| 585 | * anything) then the even must be cancelled and freed using |
| 586 | * drm_event_cancel_free(). Successfully initialized events should be sent out |
| 587 | * using drm_send_event() or drm_send_event_locked() to signal completion of the |
| 588 | * asynchronous event to userspace. |
| 589 | * |
| 590 | * If callers embedded @p into a larger structure it must be allocated with |
| 591 | * kmalloc and @p must be the first member element. |
| 592 | * |
| 593 | * This is the locked version of drm_event_reserve_init() for callers which |
Daniel Vetter | ef40cbf9 | 2017-01-25 07:26:47 +0100 | [diff] [blame] | 594 | * already hold &drm_device.event_lock. |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 595 | * |
| 596 | * RETURNS: |
| 597 | * |
| 598 | * 0 on success or a negative error code on failure. |
| 599 | */ |
| 600 | int drm_event_reserve_init_locked(struct drm_device *dev, |
| 601 | struct drm_file *file_priv, |
| 602 | struct drm_pending_event *p, |
| 603 | struct drm_event *e) |
| 604 | { |
| 605 | if (file_priv->event_space < e->length) |
| 606 | return -ENOMEM; |
| 607 | |
| 608 | file_priv->event_space -= e->length; |
| 609 | |
| 610 | p->event = e; |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 611 | list_add(&p->pending_link, &file_priv->pending_event_list); |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 612 | p->file_priv = file_priv; |
| 613 | |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | EXPORT_SYMBOL(drm_event_reserve_init_locked); |
| 617 | |
| 618 | /** |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 619 | * drm_event_reserve_init - init a DRM event and reserve space for it |
| 620 | * @dev: DRM device |
| 621 | * @file_priv: DRM file private data |
| 622 | * @p: tracking structure for the pending event |
| 623 | * @e: actual event data to deliver to userspace |
| 624 | * |
| 625 | * This function prepares the passed in event for eventual delivery. If the event |
| 626 | * doesn't get delivered (because the IOCTL fails later on, before queuing up |
| 627 | * anything) then the even must be cancelled and freed using |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 628 | * drm_event_cancel_free(). Successfully initialized events should be sent out |
| 629 | * using drm_send_event() or drm_send_event_locked() to signal completion of the |
| 630 | * asynchronous event to userspace. |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 631 | * |
| 632 | * If callers embedded @p into a larger structure it must be allocated with |
| 633 | * kmalloc and @p must be the first member element. |
| 634 | * |
Daniel Vetter | ef40cbf9 | 2017-01-25 07:26:47 +0100 | [diff] [blame] | 635 | * Callers which already hold &drm_device.event_lock should use |
Thierry Reding | 20c9ca4f | 2016-12-15 12:36:02 +0100 | [diff] [blame] | 636 | * drm_event_reserve_init_locked() instead. |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 637 | * |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 638 | * RETURNS: |
| 639 | * |
| 640 | * 0 on success or a negative error code on failure. |
| 641 | */ |
| 642 | int drm_event_reserve_init(struct drm_device *dev, |
| 643 | struct drm_file *file_priv, |
| 644 | struct drm_pending_event *p, |
| 645 | struct drm_event *e) |
| 646 | { |
| 647 | unsigned long flags; |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 648 | int ret; |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 649 | |
| 650 | spin_lock_irqsave(&dev->event_lock, flags); |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 651 | ret = drm_event_reserve_init_locked(dev, file_priv, p, e); |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 652 | spin_unlock_irqrestore(&dev->event_lock, flags); |
Daniel Vetter | 4020b22 | 2016-01-28 12:01:04 +0100 | [diff] [blame] | 653 | |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 654 | return ret; |
| 655 | } |
| 656 | EXPORT_SYMBOL(drm_event_reserve_init); |
| 657 | |
| 658 | /** |
| 659 | * drm_event_cancel_free - free a DRM event and release it's space |
| 660 | * @dev: DRM device |
| 661 | * @p: tracking structure for the pending event |
| 662 | * |
| 663 | * This function frees the event @p initialized with drm_event_reserve_init() |
Daniel Vetter | b93658f | 2017-03-08 15:12:44 +0100 | [diff] [blame] | 664 | * and releases any allocated space. It is used to cancel an event when the |
| 665 | * nonblocking operation could not be submitted and needed to be aborted. |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 666 | */ |
| 667 | void drm_event_cancel_free(struct drm_device *dev, |
| 668 | struct drm_pending_event *p) |
| 669 | { |
| 670 | unsigned long flags; |
| 671 | spin_lock_irqsave(&dev->event_lock, flags); |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 672 | if (p->file_priv) { |
| 673 | p->file_priv->event_space += p->event->length; |
| 674 | list_del(&p->pending_link); |
| 675 | } |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 676 | spin_unlock_irqrestore(&dev->event_lock, flags); |
Gustavo Padovan | 838de39 | 2016-10-20 12:50:03 -0200 | [diff] [blame] | 677 | |
| 678 | if (p->fence) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 679 | dma_fence_put(p->fence); |
Gustavo Padovan | 838de39 | 2016-10-20 12:50:03 -0200 | [diff] [blame] | 680 | |
Gustavo Padovan | 1b47aaf | 2016-06-02 00:06:35 +0200 | [diff] [blame] | 681 | kfree(p); |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 682 | } |
| 683 | EXPORT_SYMBOL(drm_event_cancel_free); |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 684 | |
| 685 | /** |
| 686 | * drm_send_event_locked - send DRM event to file descriptor |
| 687 | * @dev: DRM device |
| 688 | * @e: DRM event to deliver |
| 689 | * |
| 690 | * This function sends the event @e, initialized with drm_event_reserve_init(), |
| 691 | * to its associated userspace DRM file. Callers must already hold |
Daniel Vetter | ef40cbf9 | 2017-01-25 07:26:47 +0100 | [diff] [blame] | 692 | * &drm_device.event_lock, see drm_send_event() for the unlocked version. |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 693 | * |
| 694 | * Note that the core will take care of unlinking and disarming events when the |
| 695 | * corresponding DRM file is closed. Drivers need not worry about whether the |
| 696 | * DRM file for this event still exists and can call this function upon |
| 697 | * completion of the asynchronous work unconditionally. |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 698 | */ |
| 699 | void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e) |
| 700 | { |
| 701 | assert_spin_locked(&dev->event_lock); |
| 702 | |
Daniel Vetter | 3b24f7d | 2016-06-08 14:19:00 +0200 | [diff] [blame] | 703 | if (e->completion) { |
Daniel Vetter | 3b24f7d | 2016-06-08 14:19:00 +0200 | [diff] [blame] | 704 | complete_all(e->completion); |
Daniel Vetter | 24835e4 | 2016-12-21 11:23:30 +0100 | [diff] [blame] | 705 | e->completion_release(e->completion); |
Daniel Vetter | 3b24f7d | 2016-06-08 14:19:00 +0200 | [diff] [blame] | 706 | e->completion = NULL; |
| 707 | } |
| 708 | |
Gustavo Padovan | 1b47aaf | 2016-06-02 00:06:35 +0200 | [diff] [blame] | 709 | if (e->fence) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 710 | dma_fence_signal(e->fence); |
| 711 | dma_fence_put(e->fence); |
Gustavo Padovan | 1b47aaf | 2016-06-02 00:06:35 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 714 | if (!e->file_priv) { |
Gustavo Padovan | 1b47aaf | 2016-06-02 00:06:35 +0200 | [diff] [blame] | 715 | kfree(e); |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 716 | return; |
| 717 | } |
| 718 | |
| 719 | list_del(&e->pending_link); |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 720 | list_add_tail(&e->link, |
| 721 | &e->file_priv->event_list); |
| 722 | wake_up_interruptible(&e->file_priv->event_wait); |
| 723 | } |
| 724 | EXPORT_SYMBOL(drm_send_event_locked); |
| 725 | |
| 726 | /** |
| 727 | * drm_send_event - send DRM event to file descriptor |
| 728 | * @dev: DRM device |
| 729 | * @e: DRM event to deliver |
| 730 | * |
| 731 | * This function sends the event @e, initialized with drm_event_reserve_init(), |
Daniel Vetter | ef40cbf9 | 2017-01-25 07:26:47 +0100 | [diff] [blame] | 732 | * to its associated userspace DRM file. This function acquires |
| 733 | * &drm_device.event_lock, see drm_send_event_locked() for callers which already |
| 734 | * hold this lock. |
Daniel Vetter | 681047b | 2016-01-25 22:16:43 +0100 | [diff] [blame] | 735 | * |
| 736 | * Note that the core will take care of unlinking and disarming events when the |
| 737 | * corresponding DRM file is closed. Drivers need not worry about whether the |
| 738 | * DRM file for this event still exists and can call this function upon |
| 739 | * completion of the asynchronous work unconditionally. |
Daniel Vetter | fb740cf | 2016-01-11 22:40:59 +0100 | [diff] [blame] | 740 | */ |
| 741 | void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) |
| 742 | { |
| 743 | unsigned long irqflags; |
| 744 | |
| 745 | spin_lock_irqsave(&dev->event_lock, irqflags); |
| 746 | drm_send_event_locked(dev, e); |
| 747 | spin_unlock_irqrestore(&dev->event_lock, irqflags); |
| 748 | } |
| 749 | EXPORT_SYMBOL(drm_send_event); |