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