blob: e394799979a6eabce5c91a7a6b87773dd4b45186 [file] [log] [blame]
Daniel Vetterbcb877e2016-01-11 22:40:55 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * \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 Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
Daniel Vettera8f8b1d2017-03-08 15:12:42 +010037
38#include <drm/drm_file.h>
39#include <drm/drmP.h>
40
David Herrmanne7b960702014-07-24 12:10:04 +020041#include "drm_legacy.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020042#include "drm_internal.h"
Daniel Vetter81065542016-06-21 10:54:13 +020043#include "drm_crtc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
David Herrmann0d639882014-02-24 15:53:25 +010045/* from BKL pushdown */
Arnd Bergmann58374712010-07-10 23:51:39 +020046DEFINE_MUTEX(drm_global_mutex);
47
Daniel Vetterbcb877e2016-01-11 22:40:55 +010048/**
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 Vetterb93658f2017-03-08 15:12:44 +010053 * 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 Nikula55edf412016-11-01 17:40:44 +020055 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
Daniel Vetterb93658f2017-03-08 15:12:44 +010056 * 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 Vetterbcb877e2016-01-11 22:40:55 +010061 *
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 Vetterb93658f2017-03-08 15:12:44 +010068 * For the driver-side event interface see drm_event_reserve_init() and
69 * drm_send_event() as the main starting points.
70 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +010071 * 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 Vetterb93658f2017-03-08 15:12:44 +010074 * 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 Vetterbcb877e2016-01-11 22:40:55 +010076 *
77 * No other file operations are supported by the DRM userspace API. Overall the
Daniel Vetterbb2eaba2017-05-31 11:20:45 +020078 * following is an example &file_operations structure::
Daniel Vetterbcb877e2016-01-11 22:40:55 +010079 *
80 * static const example_drm_fops = {
81 * .owner = THIS_MODULE,
82 * .open = drm_open,
83 * .release = drm_release,
84 * .unlocked_ioctl = drm_ioctl,
Jani Nikula55edf412016-11-01 17:40:44 +020085 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
Daniel Vetterbcb877e2016-01-11 22:40:55 +010086 * .poll = drm_poll,
87 * .read = drm_read,
88 * .llseek = no_llseek,
89 * .mmap = drm_gem_mmap,
90 * };
Daniel Vetterb93658f2017-03-08 15:12:44 +010091 *
Daniel Vetterf42e1812017-03-08 15:12:57 +010092 * 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 Vetterbb2eaba2017-05-31 11:20:45 +020095 *
96 * The driver's &file_operations must be stored in &drm_driver.fops.
97 *
98 * For driver-private IOCTL handling see the more detailed discussion in
99 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100100 */
101
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400102static int drm_open_helper(struct file *filp, struct drm_minor *minor);
Dave Airliec94f7022005-07-07 21:03:38 +1000103
Dave Airlie84b1fd12007-07-11 15:53:27 +1000104static int drm_setup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int ret;
107
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +0200108 if (dev->driver->firstopen &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200109 drm_core_check_feature(dev, DRIVER_LEGACY)) {
Dave Airlie22eae942005-11-10 22:16:34 +1100110 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ret;
113 }
114
Daniel Vetterf336ab72013-08-08 15:41:35 +0200115 ret = drm_legacy_dma_setup(dev);
116 if (ret < 0)
117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122}
123
124/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100125 * drm_open - open method for DRM file
126 * @inode: device inode
127 * @filp: file pointer.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100129 * This function must be used by drivers as their &file_operations.open method.
130 * It looks up the correct DRM device and instantiates all the per-file
131 * resources for it. It also calls the &drm_driver.open driver callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100133 * RETURNS:
134 *
135 * 0 on success or negative errno value on falure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000137int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
David Herrmann1616c522014-01-29 10:49:19 +0100139 struct drm_device *dev;
Dave Airlie2c14f282008-04-21 16:47:32 +1000140 struct drm_minor *minor;
David Herrmann1616c522014-01-29 10:49:19 +0100141 int retcode;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000142 int need_setup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
David Herrmann1616c522014-01-29 10:49:19 +0100144 minor = drm_minor_acquire(iminor(inode));
145 if (IS_ERR(minor))
146 return PTR_ERR(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147
David Herrmann1616c522014-01-29 10:49:19 +0100148 dev = minor->dev;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000149 if (!dev->open_count++)
150 need_setup = 1;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800151
David Herrmann6796cb12014-01-03 14:24:19 +0100152 /* share address_space across all char-devs of a single device */
153 filp->f_mapping = dev->anon_inode->i_mapping;
Dave Airlied985c102006-01-02 21:32:48 +1100154
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400155 retcode = drm_open_helper(filp, minor);
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000156 if (retcode)
157 goto err_undo;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000158 if (need_setup) {
159 retcode = drm_setup(dev);
160 if (retcode)
161 goto err_undo;
162 }
163 return 0;
164
165err_undo:
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000166 dev->open_count--;
David Herrmann1616c522014-01-29 10:49:19 +0100167 drm_minor_release(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170EXPORT_SYMBOL(drm_open);
171
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100172/*
Dave Airlied985c102006-01-02 21:32:48 +1100173 * Check whether DRI will run on this CPU.
174 *
175 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
176 */
177static int drm_cpu_valid(void)
178{
Dave Airlied985c102006-01-02 21:32:48 +1100179#if defined(__sparc__) && !defined(__sparc_v9__)
180 return 0; /* No cmpxchg before v9 sparc. */
181#endif
182 return 1;
183}
184
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100185/*
Dave Airlied985c102006-01-02 21:32:48 +1100186 * Called whenever a process opens /dev/drm.
187 *
Dave Airlied985c102006-01-02 21:32:48 +1100188 * \param filp file pointer.
David Herrmannf4aede22014-01-29 10:18:02 +0100189 * \param minor acquired minor-object.
Dave Airlied985c102006-01-02 21:32:48 +1100190 * \return zero on success or a negative number on failure.
191 *
192 * Creates and initializes a drm_file structure for the file private data in \p
193 * filp and add it into the double linked list in \p dev.
194 */
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400195static int drm_open_helper(struct file *filp, struct drm_minor *minor)
Dave Airlied985c102006-01-02 21:32:48 +1100196{
David Herrmannf4aede22014-01-29 10:18:02 +0100197 struct drm_device *dev = minor->dev;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000198 struct drm_file *priv;
Dave Airlied985c102006-01-02 21:32:48 +1100199 int ret;
200
201 if (filp->f_flags & O_EXCL)
202 return -EBUSY; /* No exclusive opens */
203 if (!drm_cpu_valid())
204 return -EINVAL;
Dave Airlie13bb9cc2012-09-12 15:55:05 +1000205 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
Dave Airlie5bcf7192010-12-07 09:20:40 +1000206 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100207
David Herrmannf4aede22014-01-29 10:18:02 +0100208 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
Dave Airlied985c102006-01-02 21:32:48 +1100209
Julia Lawall6ebc22e2010-05-13 21:58:56 +0200210 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Dave Airlied985c102006-01-02 21:32:48 +1100211 if (!priv)
212 return -ENOMEM;
213
Dave Airlied985c102006-01-02 21:32:48 +1100214 filp->private_data = priv;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000215 priv->filp = filp;
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800216 priv->pid = get_pid(task_pid(current));
David Herrmannf4aede22014-01-29 10:18:02 +0100217 priv->minor = minor;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900218
Dave Airlied985c102006-01-02 21:32:48 +1100219 /* for compatibility root is always authenticated */
David Herrmann3cb01a92014-07-22 17:12:26 +0200220 priv->authenticated = capable(CAP_SYS_ADMIN);
Dave Airlied985c102006-01-02 21:32:48 +1100221 priv->lock_count = 0;
222
Dave Airliebd1b3312007-05-26 05:01:51 +1000223 INIT_LIST_HEAD(&priv->lhead);
Dave Airlief453ba02008-11-07 14:05:41 -0800224 INIT_LIST_HEAD(&priv->fbs);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100225 mutex_init(&priv->fbs_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100226 INIT_LIST_HEAD(&priv->blobs);
Daniel Vetter681047b2016-01-25 22:16:43 +0100227 INIT_LIST_HEAD(&priv->pending_event_list);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000228 INIT_LIST_HEAD(&priv->event_list);
229 init_waitqueue_head(&priv->event_wait);
230 priv->event_space = 4096; /* set aside 4k for event buffer */
Dave Airliebd1b3312007-05-26 05:01:51 +1000231
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000232 mutex_init(&priv->event_read_lock);
233
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200234 if (drm_core_check_feature(dev, DRIVER_GEM))
Eric Anholt673a3942008-07-30 12:06:12 -0700235 drm_gem_open(dev, priv);
236
Dave Airliee9083422017-04-04 13:26:24 +1000237 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
238 drm_syncobj_open(priv);
239
Dave Airlie32488772011-11-25 15:21:02 +0000240 if (drm_core_check_feature(dev, DRIVER_PRIME))
241 drm_prime_init_file_private(&priv->prime);
242
Dave Airlied985c102006-01-02 21:32:48 +1100243 if (dev->driver->open) {
244 ret = dev->driver->open(dev, priv);
245 if (ret < 0)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900246 goto out_prime_destroy;
Dave Airlied985c102006-01-02 21:32:48 +1100247 }
248
Daniel Vetter2cbae7e2016-06-14 20:51:00 +0200249 if (drm_is_primary_client(priv)) {
250 ret = drm_master_open(priv);
Thomas Hellstroma0af2e52015-12-02 09:24:46 -0800251 if (ret)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900252 goto out_close;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000253 }
254
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200255 mutex_lock(&dev->filelist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000256 list_add(&priv->lhead, &dev->filelist);
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200257 mutex_unlock(&dev->filelist_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100258
259#ifdef __alpha__
260 /*
261 * Default the hose
262 */
263 if (!dev->hose) {
264 struct pci_dev *pci_dev;
265 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
266 if (pci_dev) {
267 dev->hose = pci_dev->sysdata;
268 pci_dev_put(pci_dev);
269 }
270 if (!dev->hose) {
Yijing Wang59c1ad3b2014-02-13 21:14:00 +0800271 struct pci_bus *b = list_entry(pci_root_buses.next,
272 struct pci_bus, node);
Dave Airlied985c102006-01-02 21:32:48 +1100273 if (b)
274 dev->hose = b->sysdata;
275 }
276 }
277#endif
278
279 return 0;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900280
281out_close:
282 if (dev->driver->postclose)
283 dev->driver->postclose(dev, priv);
284out_prime_destroy:
285 if (drm_core_check_feature(dev, DRIVER_PRIME))
286 drm_prime_destroy_file_private(&priv->prime);
Dave Airliee9083422017-04-04 13:26:24 +1000287 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
288 drm_syncobj_release(priv);
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200289 if (drm_core_check_feature(dev, DRIVER_GEM))
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900290 drm_gem_release(dev, priv);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900291 put_pid(priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700292 kfree(priv);
Dave Airlied985c102006-01-02 21:32:48 +1100293 filp->private_data = NULL;
294 return ret;
295}
296
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000297static void drm_events_release(struct drm_file *file_priv)
298{
299 struct drm_device *dev = file_priv->minor->dev;
300 struct drm_pending_event *e, *et;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000301 unsigned long flags;
302
303 spin_lock_irqsave(&dev->event_lock, flags);
304
Daniel Vetter681047b2016-01-25 22:16:43 +0100305 /* Unlink pending events */
306 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
307 pending_link) {
308 list_del(&e->pending_link);
309 e->file_priv = NULL;
310 }
311
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000312 /* Remove unconsumed events */
YoungJun Cho1dda6802013-10-29 20:30:26 +0900313 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
314 list_del(&e->link);
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200315 kfree(e);
YoungJun Cho1dda6802013-10-29 20:30:26 +0900316 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000317
318 spin_unlock_irqrestore(&dev->event_lock, flags);
319}
320
David Herrmann1c8887d2013-10-02 11:23:36 +0200321static void drm_legacy_dev_reinit(struct drm_device *dev)
322{
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200323 if (dev->irq_enabled)
David Herrmann1c8887d2013-10-02 11:23:36 +0200324 drm_irq_uninstall(dev);
325
326 mutex_lock(&dev->struct_mutex);
327
Daniel Vetter366884b2016-04-26 19:29:34 +0200328 drm_legacy_agp_clear(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200329
330 drm_legacy_sg_cleanup(dev);
David Herrmann03decbe2014-08-29 12:12:29 +0200331 drm_legacy_vma_flush(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200332 drm_legacy_dma_takedown(dev);
333
David Herrmann1c8887d2013-10-02 11:23:36 +0200334 mutex_unlock(&dev->struct_mutex);
335
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200336 dev->sigdata.lock = NULL;
337
338 dev->context_flag = 0;
339 dev->last_context = 0;
340 dev->if_version = 0;
David Herrmann1c8887d2013-10-02 11:23:36 +0200341
342 DRM_DEBUG("lastclose completed\n");
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200343}
344
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200345void drm_lastclose(struct drm_device * dev)
346{
347 DRM_DEBUG("\n");
348
349 if (dev->driver->lastclose)
350 dev->driver->lastclose(dev);
351 DRM_DEBUG("driver lastclose completed\n");
352
Daniel Vetterfa538642016-08-03 21:11:10 +0200353 if (drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200354 drm_legacy_dev_reinit(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200355}
356
357/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100358 * drm_release - release method for DRM file
359 * @inode: device inode
360 * @filp: file pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100362 * This function must be used by drivers as their &file_operations.release
363 * method. It frees any resources associated with the open file, and calls the
Daniel Vetter45c3d212017-05-08 10:26:33 +0200364 * &drm_driver.postclose driver callback. If this is the last open file for the
365 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100367 * RETURNS:
368 *
369 * Always succeeds and returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000373 struct drm_file *file_priv = filp->private_data;
David Herrmann1616c522014-01-29 10:49:19 +0100374 struct drm_minor *minor = file_priv->minor;
375 struct drm_device *dev = minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Arnd Bergmann58374712010-07-10 23:51:39 +0200377 mutex_lock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200381 mutex_lock(&dev->filelist_mutex);
Chris Wilsondff01de2014-07-24 14:23:10 +0100382 list_del(&file_priv->lhead);
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200383 mutex_unlock(&dev->filelist_mutex);
384
Daniel Vetter45c3d212017-05-08 10:26:33 +0200385 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
386 dev->driver->preclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000387 dev->driver->preclose(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* ========================================================
390 * Begin inline drm_release
391 */
392
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700394 task_pid_nr(current),
David Herrmann58178782014-01-29 13:12:31 +0100395 (long)old_encode_dev(file_priv->minor->kdev->devt),
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Daniel Vetterfa538642016-08-03 21:11:10 +0200398 if (drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter1a75a222016-06-14 20:50:57 +0200399 drm_legacy_lock_release(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200401 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Daniel Vettera2661622014-09-11 07:41:51 +0200402 drm_legacy_reclaim_buffers(dev, file_priv);
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200403
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000404 drm_events_release(file_priv);
405
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100406 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500407 drm_fb_release(file_priv);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100408 drm_property_destroy_user_blobs(dev, file_priv);
409 }
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500410
Dave Airliee9083422017-04-04 13:26:24 +1000411 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
412 drm_syncobj_release(file_priv);
413
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200414 if (drm_core_check_feature(dev, DRIVER_GEM))
Prathyush4e47e022012-04-14 17:22:13 +0530415 drm_gem_release(dev, file_priv);
416
David Herrmanne7b960702014-07-24 12:10:04 +0200417 drm_legacy_ctxbitmap_flush(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200419 if (drm_is_primary_client(file_priv))
420 drm_master_release(file_priv);
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100421
Dave Airlie22eae942005-11-10 22:16:34 +1100422 if (dev->driver->postclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000423 dev->driver->postclose(dev, file_priv);
Dave Airlie32488772011-11-25 15:21:02 +0000424
425 if (drm_core_check_feature(dev, DRIVER_PRIME))
426 drm_prime_destroy_file_private(&file_priv->prime);
427
Ville Syrjäläddde4372014-08-06 14:02:50 +0300428 WARN_ON(!list_empty(&file_priv->event_list));
429
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800430 put_pid(file_priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700431 kfree(file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* ========================================================
434 * End inline drm_release
435 */
436
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437 if (!--dev->open_count) {
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200438 drm_lastclose(dev);
Daniel Vetterc07dcd62017-08-02 13:56:02 +0200439 if (drm_dev_is_unplugged(dev))
Dave Airlie2c07a212012-02-20 14:18:07 +0000440 drm_put_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Arnd Bergmann58374712010-07-10 23:51:39 +0200442 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David Herrmann1616c522014-01-29 10:49:19 +0100444 drm_minor_release(minor);
445
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448EXPORT_SYMBOL(drm_release);
449
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100450/**
451 * drm_read - read method for DRM file
452 * @filp: file pointer
453 * @buffer: userspace destination pointer for the read
454 * @count: count in bytes to read
455 * @offset: offset to read
456 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100457 * This function must be used by drivers as their &file_operations.read
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100458 * method iff they use DRM events for asynchronous signalling to userspace.
459 * Since events are used by the KMS API for vblank and page flip completion this
460 * means all modern display drivers must use it.
461 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100462 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
463 * must set the &file_operation.llseek to no_llseek(). Polling support is
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100464 * provided by drm_poll().
465 *
466 * This function will only ever read a full event. Therefore userspace must
467 * supply a big enough buffer to fit any event to ensure forward progress. Since
468 * the maximum event space is currently 4K it's recommended to just use that for
469 * safety.
470 *
471 * RETURNS:
472 *
473 * Number of bytes read (always aligned to full events, and can be 0) or a
474 * negative error code on failure.
475 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000476ssize_t drm_read(struct file *filp, char __user *buffer,
477 size_t count, loff_t *offset)
478{
479 struct drm_file *file_priv = filp->private_data;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000480 struct drm_device *dev = file_priv->minor->dev;
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000481 ssize_t ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000482
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000483 if (!access_ok(VERIFY_WRITE, buffer, count))
484 return -EFAULT;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000485
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000486 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
487 if (ret)
488 return ret;
489
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000490 for (;;) {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000491 struct drm_pending_event *e = NULL;
492
493 spin_lock_irq(&dev->event_lock);
494 if (!list_empty(&file_priv->event_list)) {
495 e = list_first_entry(&file_priv->event_list,
496 struct drm_pending_event, link);
497 file_priv->event_space += e->event->length;
498 list_del(&e->link);
499 }
500 spin_unlock_irq(&dev->event_lock);
501
502 if (e == NULL) {
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000503 if (ret)
504 break;
505
506 if (filp->f_flags & O_NONBLOCK) {
507 ret = -EAGAIN;
508 break;
509 }
510
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000511 mutex_unlock(&file_priv->event_read_lock);
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000512 ret = wait_event_interruptible(file_priv->event_wait,
513 !list_empty(&file_priv->event_list));
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000514 if (ret >= 0)
515 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
516 if (ret)
517 return ret;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000518 } else {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000519 unsigned length = e->event->length;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000520
Chris Wilson83eb64c2015-11-25 14:39:02 +0000521 if (length > count - ret) {
522put_back_event:
523 spin_lock_irq(&dev->event_lock);
524 file_priv->event_space -= length;
525 list_add(&e->link, &file_priv->event_list);
526 spin_unlock_irq(&dev->event_lock);
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000527 break;
528 }
529
Chris Wilson83eb64c2015-11-25 14:39:02 +0000530 if (copy_to_user(buffer + ret, e->event, length)) {
531 if (ret == 0)
532 ret = -EFAULT;
533 goto put_back_event;
534 }
535
536 ret += length;
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200537 kfree(e);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000538 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000539 }
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000540 mutex_unlock(&file_priv->event_read_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000541
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000542 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000543}
544EXPORT_SYMBOL(drm_read);
545
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100546/**
547 * drm_poll - poll method for DRM file
548 * @filp: file pointer
549 * @wait: poll waiter table
550 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100551 * This function must be used by drivers as their &file_operations.read method
552 * iff they use DRM events for asynchronous signalling to userspace. Since
553 * events are used by the KMS API for vblank and page flip completion this means
554 * all modern display drivers must use it.
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100555 *
556 * See also drm_read().
557 *
558 * RETURNS:
559 *
560 * Mask of POLL flags indicating the current status of the file.
561 */
Al Viroafc9a422017-07-03 06:39:46 -0400562__poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000564 struct drm_file *file_priv = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400565 __poll_t mask = 0;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000566
567 poll_wait(filp, &file_priv->event_wait, wait);
568
569 if (!list_empty(&file_priv->event_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800570 mask |= EPOLLIN | EPOLLRDNORM;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000571
572 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574EXPORT_SYMBOL(drm_poll);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100575
576/**
Daniel Vetter4020b222016-01-28 12:01:04 +0100577 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
578 * @dev: DRM device
579 * @file_priv: DRM file private data
580 * @p: tracking structure for the pending event
581 * @e: actual event data to deliver to userspace
582 *
583 * This function prepares the passed in event for eventual delivery. If the event
584 * doesn't get delivered (because the IOCTL fails later on, before queuing up
585 * anything) then the even must be cancelled and freed using
586 * drm_event_cancel_free(). Successfully initialized events should be sent out
587 * using drm_send_event() or drm_send_event_locked() to signal completion of the
588 * asynchronous event to userspace.
589 *
590 * If callers embedded @p into a larger structure it must be allocated with
591 * kmalloc and @p must be the first member element.
592 *
593 * This is the locked version of drm_event_reserve_init() for callers which
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100594 * already hold &drm_device.event_lock.
Daniel Vetter4020b222016-01-28 12:01:04 +0100595 *
596 * RETURNS:
597 *
598 * 0 on success or a negative error code on failure.
599 */
600int drm_event_reserve_init_locked(struct drm_device *dev,
601 struct drm_file *file_priv,
602 struct drm_pending_event *p,
603 struct drm_event *e)
604{
605 if (file_priv->event_space < e->length)
606 return -ENOMEM;
607
608 file_priv->event_space -= e->length;
609
610 p->event = e;
Daniel Vetter681047b2016-01-25 22:16:43 +0100611 list_add(&p->pending_link, &file_priv->pending_event_list);
Daniel Vetter4020b222016-01-28 12:01:04 +0100612 p->file_priv = file_priv;
613
Daniel Vetter4020b222016-01-28 12:01:04 +0100614 return 0;
615}
616EXPORT_SYMBOL(drm_event_reserve_init_locked);
617
618/**
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100619 * drm_event_reserve_init - init a DRM event and reserve space for it
620 * @dev: DRM device
621 * @file_priv: DRM file private data
622 * @p: tracking structure for the pending event
623 * @e: actual event data to deliver to userspace
624 *
625 * This function prepares the passed in event for eventual delivery. If the event
626 * doesn't get delivered (because the IOCTL fails later on, before queuing up
627 * anything) then the even must be cancelled and freed using
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100628 * drm_event_cancel_free(). Successfully initialized events should be sent out
629 * using drm_send_event() or drm_send_event_locked() to signal completion of the
630 * asynchronous event to userspace.
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100631 *
632 * If callers embedded @p into a larger structure it must be allocated with
633 * kmalloc and @p must be the first member element.
634 *
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100635 * Callers which already hold &drm_device.event_lock should use
Thierry Reding20c9ca4f2016-12-15 12:36:02 +0100636 * drm_event_reserve_init_locked() instead.
Daniel Vetter4020b222016-01-28 12:01:04 +0100637 *
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100638 * RETURNS:
639 *
640 * 0 on success or a negative error code on failure.
641 */
642int drm_event_reserve_init(struct drm_device *dev,
643 struct drm_file *file_priv,
644 struct drm_pending_event *p,
645 struct drm_event *e)
646{
647 unsigned long flags;
Daniel Vetter4020b222016-01-28 12:01:04 +0100648 int ret;
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100649
650 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100651 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100652 spin_unlock_irqrestore(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100653
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100654 return ret;
655}
656EXPORT_SYMBOL(drm_event_reserve_init);
657
658/**
659 * drm_event_cancel_free - free a DRM event and release it's space
660 * @dev: DRM device
661 * @p: tracking structure for the pending event
662 *
663 * This function frees the event @p initialized with drm_event_reserve_init()
Daniel Vetterb93658f2017-03-08 15:12:44 +0100664 * and releases any allocated space. It is used to cancel an event when the
665 * nonblocking operation could not be submitted and needed to be aborted.
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100666 */
667void drm_event_cancel_free(struct drm_device *dev,
668 struct drm_pending_event *p)
669{
670 unsigned long flags;
671 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter681047b2016-01-25 22:16:43 +0100672 if (p->file_priv) {
673 p->file_priv->event_space += p->event->length;
674 list_del(&p->pending_link);
675 }
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100676 spin_unlock_irqrestore(&dev->event_lock, flags);
Gustavo Padovan838de392016-10-20 12:50:03 -0200677
678 if (p->fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100679 dma_fence_put(p->fence);
Gustavo Padovan838de392016-10-20 12:50:03 -0200680
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200681 kfree(p);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100682}
683EXPORT_SYMBOL(drm_event_cancel_free);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100684
685/**
686 * drm_send_event_locked - send DRM event to file descriptor
687 * @dev: DRM device
688 * @e: DRM event to deliver
689 *
690 * This function sends the event @e, initialized with drm_event_reserve_init(),
691 * to its associated userspace DRM file. Callers must already hold
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100692 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
Daniel Vetter681047b2016-01-25 22:16:43 +0100693 *
694 * Note that the core will take care of unlinking and disarming events when the
695 * corresponding DRM file is closed. Drivers need not worry about whether the
696 * DRM file for this event still exists and can call this function upon
697 * completion of the asynchronous work unconditionally.
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100698 */
699void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
700{
701 assert_spin_locked(&dev->event_lock);
702
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200703 if (e->completion) {
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200704 complete_all(e->completion);
Daniel Vetter24835e42016-12-21 11:23:30 +0100705 e->completion_release(e->completion);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200706 e->completion = NULL;
707 }
708
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200709 if (e->fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100710 dma_fence_signal(e->fence);
711 dma_fence_put(e->fence);
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200712 }
713
Daniel Vetter681047b2016-01-25 22:16:43 +0100714 if (!e->file_priv) {
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200715 kfree(e);
Daniel Vetter681047b2016-01-25 22:16:43 +0100716 return;
717 }
718
719 list_del(&e->pending_link);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100720 list_add_tail(&e->link,
721 &e->file_priv->event_list);
722 wake_up_interruptible(&e->file_priv->event_wait);
723}
724EXPORT_SYMBOL(drm_send_event_locked);
725
726/**
727 * drm_send_event - send DRM event to file descriptor
728 * @dev: DRM device
729 * @e: DRM event to deliver
730 *
731 * This function sends the event @e, initialized with drm_event_reserve_init(),
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100732 * to its associated userspace DRM file. This function acquires
733 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
734 * hold this lock.
Daniel Vetter681047b2016-01-25 22:16:43 +0100735 *
736 * Note that the core will take care of unlinking and disarming events when the
737 * corresponding DRM file is closed. Drivers need not worry about whether the
738 * DRM file for this event still exists and can call this function upon
739 * completion of the asynchronous work unconditionally.
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100740 */
741void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
742{
743 unsigned long irqflags;
744
745 spin_lock_irqsave(&dev->event_lock, irqflags);
746 drm_send_event_locked(dev, e);
747 spin_unlock_irqrestore(&dev->event_lock, irqflags);
748}
749EXPORT_SYMBOL(drm_send_event);