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