blob: caad93dab54bb3c72c9105af2025220aea47e1b1 [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 Vetterda5335b2016-05-31 22:55:13 +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 Vetterbcb877e2016-01-11 22:40:55 +010095 */
96
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -040097static int drm_open_helper(struct file *filp, struct drm_minor *minor);
Dave Airliec94f7022005-07-07 21:03:38 +100098
Dave Airlie84b1fd12007-07-11 15:53:27 +100099static int drm_setup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int ret;
102
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +0200103 if (dev->driver->firstopen &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200104 drm_core_check_feature(dev, DRIVER_LEGACY)) {
Dave Airlie22eae942005-11-10 22:16:34 +1100105 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return ret;
108 }
109
Daniel Vetterf336ab72013-08-08 15:41:35 +0200110 ret = drm_legacy_dma_setup(dev);
111 if (ret < 0)
112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000115 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118
119/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100120 * drm_open - open method for DRM file
121 * @inode: device inode
122 * @filp: file pointer.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100124 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700127 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100128 * RETURNS:
129 *
130 * 0 on success or negative errno value on falure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
David Herrmann1616c522014-01-29 10:49:19 +0100134 struct drm_device *dev;
Dave Airlie2c14f282008-04-21 16:47:32 +1000135 struct drm_minor *minor;
David Herrmann1616c522014-01-29 10:49:19 +0100136 int retcode;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000137 int need_setup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
David Herrmann1616c522014-01-29 10:49:19 +0100139 minor = drm_minor_acquire(iminor(inode));
140 if (IS_ERR(minor))
141 return PTR_ERR(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142
David Herrmann1616c522014-01-29 10:49:19 +0100143 dev = minor->dev;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000144 if (!dev->open_count++)
145 need_setup = 1;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800146
David Herrmann6796cb12014-01-03 14:24:19 +0100147 /* share address_space across all char-devs of a single device */
148 filp->f_mapping = dev->anon_inode->i_mapping;
Dave Airlied985c102006-01-02 21:32:48 +1100149
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400150 retcode = drm_open_helper(filp, minor);
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000151 if (retcode)
152 goto err_undo;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000153 if (need_setup) {
154 retcode = drm_setup(dev);
155 if (retcode)
156 goto err_undo;
157 }
158 return 0;
159
160err_undo:
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000161 dev->open_count--;
David Herrmann1616c522014-01-29 10:49:19 +0100162 drm_minor_release(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165EXPORT_SYMBOL(drm_open);
166
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100167/*
Dave Airlied985c102006-01-02 21:32:48 +1100168 * 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 */
172static int drm_cpu_valid(void)
173{
Dave Airlied985c102006-01-02 21:32:48 +1100174#if defined(__sparc__) && !defined(__sparc_v9__)
175 return 0; /* No cmpxchg before v9 sparc. */
176#endif
177 return 1;
178}
179
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100180/*
Dave Airlied985c102006-01-02 21:32:48 +1100181 * Called whenever a process opens /dev/drm.
182 *
Dave Airlied985c102006-01-02 21:32:48 +1100183 * \param filp file pointer.
David Herrmannf4aede22014-01-29 10:18:02 +0100184 * \param minor acquired minor-object.
Dave Airlied985c102006-01-02 21:32:48 +1100185 * \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 Hadzic1dcc0ce2014-04-28 10:23:57 -0400190static int drm_open_helper(struct file *filp, struct drm_minor *minor)
Dave Airlied985c102006-01-02 21:32:48 +1100191{
David Herrmannf4aede22014-01-29 10:18:02 +0100192 struct drm_device *dev = minor->dev;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000193 struct drm_file *priv;
Dave Airlied985c102006-01-02 21:32:48 +1100194 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 Airlie13bb9cc2012-09-12 15:55:05 +1000200 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 +1000201 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100202
David Herrmannf4aede22014-01-29 10:18:02 +0100203 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
Dave Airlied985c102006-01-02 21:32:48 +1100204
Julia Lawall6ebc22e2010-05-13 21:58:56 +0200205 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Dave Airlied985c102006-01-02 21:32:48 +1100206 if (!priv)
207 return -ENOMEM;
208
Dave Airlied985c102006-01-02 21:32:48 +1100209 filp->private_data = priv;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000210 priv->filp = filp;
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800211 priv->pid = get_pid(task_pid(current));
David Herrmannf4aede22014-01-29 10:18:02 +0100212 priv->minor = minor;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900213
Dave Airlied985c102006-01-02 21:32:48 +1100214 /* for compatibility root is always authenticated */
David Herrmann3cb01a92014-07-22 17:12:26 +0200215 priv->authenticated = capable(CAP_SYS_ADMIN);
Dave Airlied985c102006-01-02 21:32:48 +1100216 priv->lock_count = 0;
217
Dave Airliebd1b3312007-05-26 05:01:51 +1000218 INIT_LIST_HEAD(&priv->lhead);
Dave Airlief453ba02008-11-07 14:05:41 -0800219 INIT_LIST_HEAD(&priv->fbs);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100220 mutex_init(&priv->fbs_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100221 INIT_LIST_HEAD(&priv->blobs);
Daniel Vetter681047b2016-01-25 22:16:43 +0100222 INIT_LIST_HEAD(&priv->pending_event_list);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000223 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 Airliebd1b3312007-05-26 05:01:51 +1000226
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000227 mutex_init(&priv->event_read_lock);
228
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200229 if (drm_core_check_feature(dev, DRIVER_GEM))
Eric Anholt673a3942008-07-30 12:06:12 -0700230 drm_gem_open(dev, priv);
231
Dave Airlie32488772011-11-25 15:21:02 +0000232 if (drm_core_check_feature(dev, DRIVER_PRIME))
233 drm_prime_init_file_private(&priv->prime);
234
Dave Airlied985c102006-01-02 21:32:48 +1100235 if (dev->driver->open) {
236 ret = dev->driver->open(dev, priv);
237 if (ret < 0)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900238 goto out_prime_destroy;
Dave Airlied985c102006-01-02 21:32:48 +1100239 }
240
Daniel Vetter2cbae7e2016-06-14 20:51:00 +0200241 if (drm_is_primary_client(priv)) {
242 ret = drm_master_open(priv);
Thomas Hellstroma0af2e52015-12-02 09:24:46 -0800243 if (ret)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900244 goto out_close;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000245 }
246
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200247 mutex_lock(&dev->filelist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000248 list_add(&priv->lhead, &dev->filelist);
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200249 mutex_unlock(&dev->filelist_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100250
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 Wang59c1ad3b2014-02-13 21:14:00 +0800263 struct pci_bus *b = list_entry(pci_root_buses.next,
264 struct pci_bus, node);
Dave Airlied985c102006-01-02 21:32:48 +1100265 if (b)
266 dev->hose = b->sysdata;
267 }
268 }
269#endif
270
271 return 0;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900272
273out_close:
274 if (dev->driver->postclose)
275 dev->driver->postclose(dev, priv);
276out_prime_destroy:
277 if (drm_core_check_feature(dev, DRIVER_PRIME))
278 drm_prime_destroy_file_private(&priv->prime);
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200279 if (drm_core_check_feature(dev, DRIVER_GEM))
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900280 drm_gem_release(dev, priv);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900281 put_pid(priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700282 kfree(priv);
Dave Airlied985c102006-01-02 21:32:48 +1100283 filp->private_data = NULL;
284 return ret;
285}
286
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000287static 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øgsbergc9a9c5e2009-09-12 04:33:34 +1000291 unsigned long flags;
292
293 spin_lock_irqsave(&dev->event_lock, flags);
294
Daniel Vetter681047b2016-01-25 22:16:43 +0100295 /* 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øgsbergc9a9c5e2009-09-12 04:33:34 +1000302 /* Remove unconsumed events */
YoungJun Cho1dda6802013-10-29 20:30:26 +0900303 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
304 list_del(&e->link);
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200305 kfree(e);
YoungJun Cho1dda6802013-10-29 20:30:26 +0900306 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000307
308 spin_unlock_irqrestore(&dev->event_lock, flags);
309}
310
David Herrmann1c8887d2013-10-02 11:23:36 +0200311static void drm_legacy_dev_reinit(struct drm_device *dev)
312{
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200313 if (dev->irq_enabled)
David Herrmann1c8887d2013-10-02 11:23:36 +0200314 drm_irq_uninstall(dev);
315
316 mutex_lock(&dev->struct_mutex);
317
Daniel Vetter366884b2016-04-26 19:29:34 +0200318 drm_legacy_agp_clear(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200319
320 drm_legacy_sg_cleanup(dev);
David Herrmann03decbe2014-08-29 12:12:29 +0200321 drm_legacy_vma_flush(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200322 drm_legacy_dma_takedown(dev);
323
David Herrmann1c8887d2013-10-02 11:23:36 +0200324 mutex_unlock(&dev->struct_mutex);
325
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200326 dev->sigdata.lock = NULL;
327
328 dev->context_flag = 0;
329 dev->last_context = 0;
330 dev->if_version = 0;
David Herrmann1c8887d2013-10-02 11:23:36 +0200331
332 DRM_DEBUG("lastclose completed\n");
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200333}
334
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200335void 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 Vetterfa538642016-08-03 21:11:10 +0200343 if (drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200344 drm_legacy_dev_reinit(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200345}
346
347/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100348 * drm_release - release method for DRM file
349 * @inode: device inode
350 * @filp: file pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100352 * 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 Vetter45c3d212017-05-08 10:26:33 +0200354 * &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 Torvalds1da177e2005-04-16 15:20:36 -0700356 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100357 * RETURNS:
358 *
359 * Always succeeds and returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000363 struct drm_file *file_priv = filp->private_data;
David Herrmann1616c522014-01-29 10:49:19 +0100364 struct drm_minor *minor = file_priv->minor;
365 struct drm_device *dev = minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Arnd Bergmann58374712010-07-10 23:51:39 +0200367 mutex_lock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000369 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200371 mutex_lock(&dev->filelist_mutex);
Chris Wilsondff01de2014-07-24 14:23:10 +0100372 list_del(&file_priv->lhead);
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200373 mutex_unlock(&dev->filelist_mutex);
374
Daniel Vetter45c3d212017-05-08 10:26:33 +0200375 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
376 dev->driver->preclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000377 dev->driver->preclose(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* ========================================================
380 * Begin inline drm_release
381 */
382
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700384 task_pid_nr(current),
David Herrmann58178782014-01-29 13:12:31 +0100385 (long)old_encode_dev(file_priv->minor->kdev->devt),
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000386 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Daniel Vetterfa538642016-08-03 21:11:10 +0200388 if (drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter1a75a222016-06-14 20:50:57 +0200389 drm_legacy_lock_release(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200391 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Daniel Vettera2661622014-09-11 07:41:51 +0200392 drm_legacy_reclaim_buffers(dev, file_priv);
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200393
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000394 drm_events_release(file_priv);
395
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100396 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500397 drm_fb_release(file_priv);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100398 drm_property_destroy_user_blobs(dev, file_priv);
399 }
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500400
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200401 if (drm_core_check_feature(dev, DRIVER_GEM))
Prathyush4e47e022012-04-14 17:22:13 +0530402 drm_gem_release(dev, file_priv);
403
David Herrmanne7b960702014-07-24 12:10:04 +0200404 drm_legacy_ctxbitmap_flush(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200406 if (drm_is_primary_client(file_priv))
407 drm_master_release(file_priv);
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100408
Dave Airlie22eae942005-11-10 22:16:34 +1100409 if (dev->driver->postclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000410 dev->driver->postclose(dev, file_priv);
Dave Airlie32488772011-11-25 15:21:02 +0000411
412 if (drm_core_check_feature(dev, DRIVER_PRIME))
413 drm_prime_destroy_file_private(&file_priv->prime);
414
Ville Syrjäläddde4372014-08-06 14:02:50 +0300415 WARN_ON(!list_empty(&file_priv->event_list));
416
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800417 put_pid(file_priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700418 kfree(file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* ========================================================
421 * End inline drm_release
422 */
423
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000424 if (!--dev->open_count) {
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200425 drm_lastclose(dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000426 if (drm_device_is_unplugged(dev))
427 drm_put_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Arnd Bergmann58374712010-07-10 23:51:39 +0200429 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David Herrmann1616c522014-01-29 10:49:19 +0100431 drm_minor_release(minor);
432
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435EXPORT_SYMBOL(drm_release);
436
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100437/**
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 Vetterb93658f2017-03-08 15:12:44 +0100444 * This function must be used by drivers as their &file_operations.read
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100445 * 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 Vetterb93658f2017-03-08 15:12:44 +0100449 * @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 Vetterbcb877e2016-01-11 22:40:55 +0100451 * 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øgsbergc9a9c5e2009-09-12 04:33:34 +1000463ssize_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 Wilsoncdd1cf72014-12-04 21:03:25 +0000467 struct drm_device *dev = file_priv->minor->dev;
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000468 ssize_t ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000469
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000470 if (!access_ok(VERIFY_WRITE, buffer, count))
471 return -EFAULT;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000472
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000473 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
474 if (ret)
475 return ret;
476
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000477 for (;;) {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000478 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 Wilsoncdd1cf72014-12-04 21:03:25 +0000490 if (ret)
491 break;
492
493 if (filp->f_flags & O_NONBLOCK) {
494 ret = -EAGAIN;
495 break;
496 }
497
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000498 mutex_unlock(&file_priv->event_read_lock);
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000499 ret = wait_event_interruptible(file_priv->event_wait,
500 !list_empty(&file_priv->event_list));
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000501 if (ret >= 0)
502 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
503 if (ret)
504 return ret;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000505 } else {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000506 unsigned length = e->event->length;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000507
Chris Wilson83eb64c2015-11-25 14:39:02 +0000508 if (length > count - ret) {
509put_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 Wilsoncdd1cf72014-12-04 21:03:25 +0000514 break;
515 }
516
Chris Wilson83eb64c2015-11-25 14:39:02 +0000517 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 Padovan1b47aaf2016-06-02 00:06:35 +0200524 kfree(e);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000525 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000526 }
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000527 mutex_unlock(&file_priv->event_read_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000528
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000529 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000530}
531EXPORT_SYMBOL(drm_read);
532
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100533/**
534 * drm_poll - poll method for DRM file
535 * @filp: file pointer
536 * @wait: poll waiter table
537 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100538 * 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 Vetterbcb877e2016-01-11 22:40:55 +0100542 *
543 * See also drm_read().
544 *
545 * RETURNS:
546 *
547 * Mask of POLL flags indicating the current status of the file.
548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
550{
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000551 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 Torvalds1da177e2005-04-16 15:20:36 -0700560}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561EXPORT_SYMBOL(drm_poll);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100562
563/**
Daniel Vetter4020b222016-01-28 12:01:04 +0100564 * 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 Vetteref40cbf92017-01-25 07:26:47 +0100581 * already hold &drm_device.event_lock.
Daniel Vetter4020b222016-01-28 12:01:04 +0100582 *
583 * RETURNS:
584 *
585 * 0 on success or a negative error code on failure.
586 */
587int 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 Vetter681047b2016-01-25 22:16:43 +0100598 list_add(&p->pending_link, &file_priv->pending_event_list);
Daniel Vetter4020b222016-01-28 12:01:04 +0100599 p->file_priv = file_priv;
600
Daniel Vetter4020b222016-01-28 12:01:04 +0100601 return 0;
602}
603EXPORT_SYMBOL(drm_event_reserve_init_locked);
604
605/**
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100606 * 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 Vetterfb740cf2016-01-11 22:40:59 +0100615 * 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 Vetter2dd500f2016-01-11 22:40:56 +0100618 *
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 Vetteref40cbf92017-01-25 07:26:47 +0100622 * Callers which already hold &drm_device.event_lock should use
Thierry Reding20c9ca4f2016-12-15 12:36:02 +0100623 * drm_event_reserve_init_locked() instead.
Daniel Vetter4020b222016-01-28 12:01:04 +0100624 *
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100625 * RETURNS:
626 *
627 * 0 on success or a negative error code on failure.
628 */
629int 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 Vetter4020b222016-01-28 12:01:04 +0100635 int ret;
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100636
637 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100638 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100639 spin_unlock_irqrestore(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100640
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100641 return ret;
642}
643EXPORT_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 Vetterb93658f2017-03-08 15:12:44 +0100651 * 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 Vetter2dd500f2016-01-11 22:40:56 +0100653 */
654void 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 Vetter681047b2016-01-25 22:16:43 +0100659 if (p->file_priv) {
660 p->file_priv->event_space += p->event->length;
661 list_del(&p->pending_link);
662 }
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100663 spin_unlock_irqrestore(&dev->event_lock, flags);
Gustavo Padovan838de392016-10-20 12:50:03 -0200664
665 if (p->fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100666 dma_fence_put(p->fence);
Gustavo Padovan838de392016-10-20 12:50:03 -0200667
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200668 kfree(p);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100669}
670EXPORT_SYMBOL(drm_event_cancel_free);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100671
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 Vetteref40cbf92017-01-25 07:26:47 +0100679 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
Daniel Vetter681047b2016-01-25 22:16:43 +0100680 *
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 Vetterfb740cf2016-01-11 22:40:59 +0100685 */
686void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
687{
688 assert_spin_locked(&dev->event_lock);
689
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200690 if (e->completion) {
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200691 complete_all(e->completion);
Daniel Vetter24835e42016-12-21 11:23:30 +0100692 e->completion_release(e->completion);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200693 e->completion = NULL;
694 }
695
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200696 if (e->fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100697 dma_fence_signal(e->fence);
698 dma_fence_put(e->fence);
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200699 }
700
Daniel Vetter681047b2016-01-25 22:16:43 +0100701 if (!e->file_priv) {
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200702 kfree(e);
Daniel Vetter681047b2016-01-25 22:16:43 +0100703 return;
704 }
705
706 list_del(&e->pending_link);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100707 list_add_tail(&e->link,
708 &e->file_priv->event_list);
709 wake_up_interruptible(&e->file_priv->event_wait);
710}
711EXPORT_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 Vetteref40cbf92017-01-25 07:26:47 +0100719 * 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 Vetter681047b2016-01-25 22:16:43 +0100722 *
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 Vetterfb740cf2016-01-11 22:40:59 +0100727 */
728void 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}
736EXPORT_SYMBOL(drm_send_event);