blob: afe8c53e5aad98183f23f279740c11ea46e10cbc [file] [log] [blame]
Daniel Vetterbcb877e2016-01-11 22:40:55 +01001/*
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_fops.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * File operations for DRM
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
8 */
9
10/*
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
12 *
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice (including the next
25 * paragraph) shall be included in all copies or substantial portions of the
26 * Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
35 */
36
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040040#include <linux/module.h>
David Herrmanne7b960702014-07-24 12:10:04 +020041#include "drm_legacy.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020042#include "drm_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Herrmann0d639882014-02-24 15:53:25 +010044/* from BKL pushdown */
Arnd Bergmann58374712010-07-10 23:51:39 +020045DEFINE_MUTEX(drm_global_mutex);
46
Daniel Vetterbcb877e2016-01-11 22:40:55 +010047/**
48 * DOC: file operations
49 *
50 * Drivers must define the file operations structure that forms the DRM
51 * userspace API entry point, even though most of those operations are
52 * implemented in the DRM core. The mandatory functions are drm_open(),
53 * drm_read(), drm_ioctl() and drm_compat_ioctl if CONFIG_COMPAT is enabled.
54 * Drivers which implement private ioctls that require 32/64 bit compatibility
55 * support must provided their onw .compat_ioctl() handler that processes
56 * private ioctls and calls drm_compat_ioctl() for core ioctls.
57 *
58 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
59 * events are a generic and extensible means to send asynchronous events to
60 * userspace through the file descriptor. They are used to send vblank event and
61 * page flip completions by the KMS API. But drivers can also use it for their
62 * own needs, e.g. to signal completion of rendering.
63 *
64 * The memory mapping implementation will vary depending on how the driver
65 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
66 * function, modern drivers should use one of the provided memory-manager
67 * specific implementations. For GEM-based drivers this is drm_gem_mmap().
68 *
69 * No other file operations are supported by the DRM userspace API. Overall the
70 * following is an example #file_operations structure:
71 *
72 * static const example_drm_fops = {
73 * .owner = THIS_MODULE,
74 * .open = drm_open,
75 * .release = drm_release,
76 * .unlocked_ioctl = drm_ioctl,
77 * #ifdef CONFIG_COMPAT
78 * .compat_ioctl = drm_compat_ioctl,
79 * #endif
80 * .poll = drm_poll,
81 * .read = drm_read,
82 * .llseek = no_llseek,
83 * .mmap = drm_gem_mmap,
84 * };
85 */
86
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -040087static int drm_open_helper(struct file *filp, struct drm_minor *minor);
Dave Airliec94f7022005-07-07 21:03:38 +100088
Dave Airlie84b1fd12007-07-11 15:53:27 +100089static int drm_setup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int ret;
92
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +020093 if (dev->driver->firstopen &&
94 !drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie22eae942005-11-10 22:16:34 +110095 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100096 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return ret;
98 }
99
Daniel Vetterf336ab72013-08-08 15:41:35 +0200100 ret = drm_legacy_dma_setup(dev);
101 if (ret < 0)
102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107}
108
109/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100110 * drm_open - open method for DRM file
111 * @inode: device inode
112 * @filp: file pointer.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000113 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100114 * This function must be used by drivers as their .open() #file_operations
115 * method. It looks up the correct DRM device and instantiates all the per-file
116 * resources for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100118 * RETURNS:
119 *
120 * 0 on success or negative errno value on falure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000122int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
David Herrmann1616c522014-01-29 10:49:19 +0100124 struct drm_device *dev;
Dave Airlie2c14f282008-04-21 16:47:32 +1000125 struct drm_minor *minor;
David Herrmann1616c522014-01-29 10:49:19 +0100126 int retcode;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000127 int need_setup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
David Herrmann1616c522014-01-29 10:49:19 +0100129 minor = drm_minor_acquire(iminor(inode));
130 if (IS_ERR(minor))
131 return PTR_ERR(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132
David Herrmann1616c522014-01-29 10:49:19 +0100133 dev = minor->dev;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000134 if (!dev->open_count++)
135 need_setup = 1;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800136
David Herrmann6796cb12014-01-03 14:24:19 +0100137 /* share address_space across all char-devs of a single device */
138 filp->f_mapping = dev->anon_inode->i_mapping;
Dave Airlied985c102006-01-02 21:32:48 +1100139
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400140 retcode = drm_open_helper(filp, minor);
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000141 if (retcode)
142 goto err_undo;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000143 if (need_setup) {
144 retcode = drm_setup(dev);
145 if (retcode)
146 goto err_undo;
147 }
148 return 0;
149
150err_undo:
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000151 dev->open_count--;
David Herrmann1616c522014-01-29 10:49:19 +0100152 drm_minor_release(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155EXPORT_SYMBOL(drm_open);
156
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100157/*
Dave Airlied985c102006-01-02 21:32:48 +1100158 * Check whether DRI will run on this CPU.
159 *
160 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
161 */
162static int drm_cpu_valid(void)
163{
Dave Airlied985c102006-01-02 21:32:48 +1100164#if defined(__sparc__) && !defined(__sparc_v9__)
165 return 0; /* No cmpxchg before v9 sparc. */
166#endif
167 return 1;
168}
169
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100170/*
Thomas Hellstroma0af2e52015-12-02 09:24:46 -0800171 * drm_new_set_master - Allocate a new master object and become master for the
172 * associated master realm.
173 *
174 * @dev: The associated device.
175 * @fpriv: File private identifying the client.
176 *
177 * This function must be called with dev::struct_mutex held.
178 * Returns negative error code on failure. Zero on success.
179 */
180int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
181{
182 struct drm_master *old_master;
183 int ret;
184
185 lockdep_assert_held_once(&dev->master_mutex);
186
187 /* create a new master */
188 fpriv->minor->master = drm_master_create(fpriv->minor);
189 if (!fpriv->minor->master)
190 return -ENOMEM;
191
192 /* take another reference for the copy in the local file priv */
193 old_master = fpriv->master;
194 fpriv->master = drm_master_get(fpriv->minor->master);
195
196 if (dev->driver->master_create) {
197 ret = dev->driver->master_create(dev, fpriv->master);
198 if (ret)
199 goto out_err;
200 }
201 if (dev->driver->master_set) {
202 ret = dev->driver->master_set(dev, fpriv, true);
203 if (ret)
204 goto out_err;
205 }
206
207 fpriv->is_master = 1;
208 fpriv->allowed_master = 1;
209 fpriv->authenticated = 1;
210 if (old_master)
211 drm_master_put(&old_master);
212
213 return 0;
214
215out_err:
216 /* drop both references and restore old master on failure */
217 drm_master_put(&fpriv->minor->master);
218 drm_master_put(&fpriv->master);
219 fpriv->master = old_master;
220
221 return ret;
222}
223
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100224/*
Dave Airlied985c102006-01-02 21:32:48 +1100225 * Called whenever a process opens /dev/drm.
226 *
Dave Airlied985c102006-01-02 21:32:48 +1100227 * \param filp file pointer.
David Herrmannf4aede22014-01-29 10:18:02 +0100228 * \param minor acquired minor-object.
Dave Airlied985c102006-01-02 21:32:48 +1100229 * \return zero on success or a negative number on failure.
230 *
231 * Creates and initializes a drm_file structure for the file private data in \p
232 * filp and add it into the double linked list in \p dev.
233 */
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400234static int drm_open_helper(struct file *filp, struct drm_minor *minor)
Dave Airlied985c102006-01-02 21:32:48 +1100235{
David Herrmannf4aede22014-01-29 10:18:02 +0100236 struct drm_device *dev = minor->dev;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000237 struct drm_file *priv;
Dave Airlied985c102006-01-02 21:32:48 +1100238 int ret;
239
240 if (filp->f_flags & O_EXCL)
241 return -EBUSY; /* No exclusive opens */
242 if (!drm_cpu_valid())
243 return -EINVAL;
Dave Airlie13bb9cc2012-09-12 15:55:05 +1000244 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 +1000245 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100246
David Herrmannf4aede22014-01-29 10:18:02 +0100247 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
Dave Airlied985c102006-01-02 21:32:48 +1100248
Julia Lawall6ebc22e2010-05-13 21:58:56 +0200249 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Dave Airlied985c102006-01-02 21:32:48 +1100250 if (!priv)
251 return -ENOMEM;
252
Dave Airlied985c102006-01-02 21:32:48 +1100253 filp->private_data = priv;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000254 priv->filp = filp;
David Howells2df68b42008-09-02 11:03:14 +1000255 priv->uid = current_euid();
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800256 priv->pid = get_pid(task_pid(current));
David Herrmannf4aede22014-01-29 10:18:02 +0100257 priv->minor = minor;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900258
Dave Airlied985c102006-01-02 21:32:48 +1100259 /* for compatibility root is always authenticated */
David Herrmann3cb01a92014-07-22 17:12:26 +0200260 priv->authenticated = capable(CAP_SYS_ADMIN);
Dave Airlied985c102006-01-02 21:32:48 +1100261 priv->lock_count = 0;
262
Dave Airliebd1b3312007-05-26 05:01:51 +1000263 INIT_LIST_HEAD(&priv->lhead);
Dave Airlief453ba02008-11-07 14:05:41 -0800264 INIT_LIST_HEAD(&priv->fbs);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100265 mutex_init(&priv->fbs_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100266 INIT_LIST_HEAD(&priv->blobs);
Daniel Vetter681047b2016-01-25 22:16:43 +0100267 INIT_LIST_HEAD(&priv->pending_event_list);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000268 INIT_LIST_HEAD(&priv->event_list);
269 init_waitqueue_head(&priv->event_wait);
270 priv->event_space = 4096; /* set aside 4k for event buffer */
Dave Airliebd1b3312007-05-26 05:01:51 +1000271
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000272 mutex_init(&priv->event_read_lock);
273
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200274 if (drm_core_check_feature(dev, DRIVER_GEM))
Eric Anholt673a3942008-07-30 12:06:12 -0700275 drm_gem_open(dev, priv);
276
Dave Airlie32488772011-11-25 15:21:02 +0000277 if (drm_core_check_feature(dev, DRIVER_PRIME))
278 drm_prime_init_file_private(&priv->prime);
279
Dave Airlied985c102006-01-02 21:32:48 +1100280 if (dev->driver->open) {
281 ret = dev->driver->open(dev, priv);
282 if (ret < 0)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900283 goto out_prime_destroy;
Dave Airlied985c102006-01-02 21:32:48 +1100284 }
285
David Herrmann17931262013-08-25 18:29:00 +0200286 /* if there is no current master make this fd it, but do not create
287 * any master object for render clients */
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100288 mutex_lock(&dev->master_mutex);
Thomas Hellstrom43683052014-03-13 11:07:44 +0100289 if (drm_is_primary_client(priv) && !priv->minor->master) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000290 /* create a new master */
Thomas Hellstroma0af2e52015-12-02 09:24:46 -0800291 ret = drm_new_set_master(dev, priv);
292 if (ret)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900293 goto out_close;
Thomas Hellstrom43683052014-03-13 11:07:44 +0100294 } else if (drm_is_primary_client(priv)) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000295 /* get a reference to the master */
296 priv->master = drm_master_get(priv->minor->master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000297 }
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100298 mutex_unlock(&dev->master_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000299
300 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000301 list_add(&priv->lhead, &dev->filelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100302 mutex_unlock(&dev->struct_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100303
304#ifdef __alpha__
305 /*
306 * Default the hose
307 */
308 if (!dev->hose) {
309 struct pci_dev *pci_dev;
310 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
311 if (pci_dev) {
312 dev->hose = pci_dev->sysdata;
313 pci_dev_put(pci_dev);
314 }
315 if (!dev->hose) {
Yijing Wang59c1ad3b2014-02-13 21:14:00 +0800316 struct pci_bus *b = list_entry(pci_root_buses.next,
317 struct pci_bus, node);
Dave Airlied985c102006-01-02 21:32:48 +1100318 if (b)
319 dev->hose = b->sysdata;
320 }
321 }
322#endif
323
324 return 0;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900325
326out_close:
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100327 mutex_unlock(&dev->master_mutex);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900328 if (dev->driver->postclose)
329 dev->driver->postclose(dev, priv);
330out_prime_destroy:
331 if (drm_core_check_feature(dev, DRIVER_PRIME))
332 drm_prime_destroy_file_private(&priv->prime);
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200333 if (drm_core_check_feature(dev, DRIVER_GEM))
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900334 drm_gem_release(dev, priv);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900335 put_pid(priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700336 kfree(priv);
Dave Airlied985c102006-01-02 21:32:48 +1100337 filp->private_data = NULL;
338 return ret;
339}
340
Dave Airlie7c1c2872008-11-28 14:22:24 +1000341static void drm_master_release(struct drm_device *dev, struct file *filp)
342{
343 struct drm_file *file_priv = filp->private_data;
344
David Herrmannbb6d8222014-08-29 12:12:46 +0200345 if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000346 DRM_DEBUG("File %p released, freeing lock for context %d\n",
347 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
David Herrmannbb6d8222014-08-29 12:12:46 +0200348 drm_legacy_lock_free(&file_priv->master->lock,
349 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
Dave Airlie7c1c2872008-11-28 14:22:24 +1000350 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000351}
352
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000353static void drm_events_release(struct drm_file *file_priv)
354{
355 struct drm_device *dev = file_priv->minor->dev;
356 struct drm_pending_event *e, *et;
357 struct drm_pending_vblank_event *v, *vt;
358 unsigned long flags;
359
360 spin_lock_irqsave(&dev->event_lock, flags);
361
362 /* Remove pending flips */
363 list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
364 if (v->base.file_priv == file_priv) {
365 list_del(&v->base.link);
366 drm_vblank_put(dev, v->pipe);
367 v->base.destroy(&v->base);
368 }
369
Daniel Vetter681047b2016-01-25 22:16:43 +0100370 /* Unlink pending events */
371 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
372 pending_link) {
373 list_del(&e->pending_link);
374 e->file_priv = NULL;
375 }
376
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000377 /* Remove unconsumed events */
YoungJun Cho1dda6802013-10-29 20:30:26 +0900378 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
379 list_del(&e->link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000380 e->destroy(e);
YoungJun Cho1dda6802013-10-29 20:30:26 +0900381 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000382
383 spin_unlock_irqrestore(&dev->event_lock, flags);
384}
385
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100386/*
David Herrmann1c8887d2013-10-02 11:23:36 +0200387 * drm_legacy_dev_reinit
388 *
389 * Reinitializes a legacy/ums drm device in it's lastclose function.
390 */
391static void drm_legacy_dev_reinit(struct drm_device *dev)
392{
David Herrmann1c8887d2013-10-02 11:23:36 +0200393 if (drm_core_check_feature(dev, DRIVER_MODESET))
394 return;
395
David Herrmann1c8887d2013-10-02 11:23:36 +0200396 dev->sigdata.lock = NULL;
397
398 dev->context_flag = 0;
399 dev->last_context = 0;
400 dev->if_version = 0;
401}
402
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100403/*
David Herrmann1c8887d2013-10-02 11:23:36 +0200404 * Take down the DRM device.
405 *
406 * \param dev DRM device structure.
407 *
408 * Frees every resource in \p dev.
409 *
410 * \sa drm_device
411 */
412int drm_lastclose(struct drm_device * dev)
413{
David Herrmann1c8887d2013-10-02 11:23:36 +0200414 DRM_DEBUG("\n");
415
416 if (dev->driver->lastclose)
417 dev->driver->lastclose(dev);
418 DRM_DEBUG("driver lastclose completed\n");
419
420 if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
421 drm_irq_uninstall(dev);
422
423 mutex_lock(&dev->struct_mutex);
424
425 drm_agp_clear(dev);
426
427 drm_legacy_sg_cleanup(dev);
David Herrmann03decbe2014-08-29 12:12:29 +0200428 drm_legacy_vma_flush(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200429 drm_legacy_dma_takedown(dev);
430
David Herrmann1c8887d2013-10-02 11:23:36 +0200431 mutex_unlock(&dev->struct_mutex);
432
433 drm_legacy_dev_reinit(dev);
434
435 DRM_DEBUG("lastclose completed\n");
436 return 0;
437}
438
439/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100440 * drm_release - release method for DRM file
441 * @inode: device inode
442 * @filp: file pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100444 * This function must be used by drivers as their .release() #file_operations
445 * method. It frees any resources associated with the open file, and if this is
446 * the last open file for the DRM device also proceeds to call drm_lastclose().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100448 * RETURNS:
449 *
450 * Always succeeds and returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000454 struct drm_file *file_priv = filp->private_data;
David Herrmann1616c522014-01-29 10:49:19 +0100455 struct drm_minor *minor = file_priv->minor;
456 struct drm_device *dev = minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int retcode = 0;
458
Arnd Bergmann58374712010-07-10 23:51:39 +0200459 mutex_lock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Chris Wilsondff01de2014-07-24 14:23:10 +0100463 mutex_lock(&dev->struct_mutex);
464 list_del(&file_priv->lhead);
David Herrmann32e7b942015-05-04 21:01:30 +0200465 if (file_priv->magic)
466 idr_remove(&file_priv->master->magic_map, file_priv->magic);
Chris Wilsondff01de2014-07-24 14:23:10 +0100467 mutex_unlock(&dev->struct_mutex);
468
Dave Airlie22eae942005-11-10 22:16:34 +1100469 if (dev->driver->preclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000470 dev->driver->preclose(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* ========================================================
473 * Begin inline drm_release
474 */
475
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700477 task_pid_nr(current),
David Herrmann58178782014-01-29 13:12:31 +0100478 (long)old_encode_dev(file_priv->minor->kdev->devt),
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Dave Airlie7c1c2872008-11-28 14:22:24 +1000481 /* if the master has gone away we can't do anything with the lock */
482 if (file_priv->minor->master)
483 drm_master_release(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200485 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Daniel Vettera2661622014-09-11 07:41:51 +0200486 drm_legacy_reclaim_buffers(dev, file_priv);
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200487
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000488 drm_events_release(file_priv);
489
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100490 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500491 drm_fb_release(file_priv);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100492 drm_property_destroy_user_blobs(dev, file_priv);
493 }
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500494
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200495 if (drm_core_check_feature(dev, DRIVER_GEM))
Prathyush4e47e022012-04-14 17:22:13 +0530496 drm_gem_release(dev, file_priv);
497
David Herrmanne7b960702014-07-24 12:10:04 +0200498 drm_legacy_ctxbitmap_flush(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100500 mutex_lock(&dev->master_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000501
Dave Airlie7963e9d2014-08-08 07:30:53 +1000502 if (file_priv->is_master) {
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100503 struct drm_master *master = file_priv->master;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000504
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100505 /*
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100506 * Since the master is disappearing, so is the
507 * possibility to lock.
508 */
David Herrmann3cb01a92014-07-22 17:12:26 +0200509 mutex_lock(&dev->struct_mutex);
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100510 if (master->lock.hw_lock) {
511 if (dev->sigdata.lock == master->lock.hw_lock)
512 dev->sigdata.lock = NULL;
513 master->lock.hw_lock = NULL;
514 master->lock.file_priv = NULL;
515 wake_up_interruptible_all(&master->lock.lock_queue);
516 }
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100517 mutex_unlock(&dev->struct_mutex);
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100518
Dave Airlie7c1c2872008-11-28 14:22:24 +1000519 if (file_priv->minor->master == file_priv->master) {
520 /* drop the reference held my the minor */
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000521 if (dev->driver->master_drop)
522 dev->driver->master_drop(dev, file_priv, true);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000523 drm_master_put(&file_priv->minor->master);
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000526
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100527 /* drop the master reference held by the file priv */
David Herrmann17931262013-08-25 18:29:00 +0200528 if (file_priv->master)
529 drm_master_put(&file_priv->master);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000530 file_priv->is_master = 0;
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100531 mutex_unlock(&dev->master_mutex);
532
Dave Airlie22eae942005-11-10 22:16:34 +1100533 if (dev->driver->postclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000534 dev->driver->postclose(dev, file_priv);
Dave Airlie32488772011-11-25 15:21:02 +0000535
Daniel Vetter319c9332013-08-15 00:02:46 +0200536
Dave Airlie32488772011-11-25 15:21:02 +0000537 if (drm_core_check_feature(dev, DRIVER_PRIME))
538 drm_prime_destroy_file_private(&file_priv->prime);
539
Ville Syrjäläddde4372014-08-06 14:02:50 +0300540 WARN_ON(!list_empty(&file_priv->event_list));
541
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800542 put_pid(file_priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700543 kfree(file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* ========================================================
546 * End inline drm_release
547 */
548
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 if (!--dev->open_count) {
Daniel Vetter43d13372013-12-11 11:35:08 +0100550 retcode = drm_lastclose(dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000551 if (drm_device_is_unplugged(dev))
552 drm_put_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Arnd Bergmann58374712010-07-10 23:51:39 +0200554 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
David Herrmann1616c522014-01-29 10:49:19 +0100556 drm_minor_release(minor);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return retcode;
559}
560EXPORT_SYMBOL(drm_release);
561
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100562/**
563 * drm_read - read method for DRM file
564 * @filp: file pointer
565 * @buffer: userspace destination pointer for the read
566 * @count: count in bytes to read
567 * @offset: offset to read
568 *
569 * This function must be used by drivers as their .read() #file_operations
570 * method iff they use DRM events for asynchronous signalling to userspace.
571 * Since events are used by the KMS API for vblank and page flip completion this
572 * means all modern display drivers must use it.
573 *
574 * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
575 * must set the .llseek() #file_operation to no_llseek(). Polling support is
576 * provided by drm_poll().
577 *
578 * This function will only ever read a full event. Therefore userspace must
579 * supply a big enough buffer to fit any event to ensure forward progress. Since
580 * the maximum event space is currently 4K it's recommended to just use that for
581 * safety.
582 *
583 * RETURNS:
584 *
585 * Number of bytes read (always aligned to full events, and can be 0) or a
586 * negative error code on failure.
587 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000588ssize_t drm_read(struct file *filp, char __user *buffer,
589 size_t count, loff_t *offset)
590{
591 struct drm_file *file_priv = filp->private_data;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000592 struct drm_device *dev = file_priv->minor->dev;
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000593 ssize_t ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000594
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000595 if (!access_ok(VERIFY_WRITE, buffer, count))
596 return -EFAULT;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000597
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000598 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
599 if (ret)
600 return ret;
601
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000602 for (;;) {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000603 struct drm_pending_event *e = NULL;
604
605 spin_lock_irq(&dev->event_lock);
606 if (!list_empty(&file_priv->event_list)) {
607 e = list_first_entry(&file_priv->event_list,
608 struct drm_pending_event, link);
609 file_priv->event_space += e->event->length;
610 list_del(&e->link);
611 }
612 spin_unlock_irq(&dev->event_lock);
613
614 if (e == NULL) {
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000615 if (ret)
616 break;
617
618 if (filp->f_flags & O_NONBLOCK) {
619 ret = -EAGAIN;
620 break;
621 }
622
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000623 mutex_unlock(&file_priv->event_read_lock);
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000624 ret = wait_event_interruptible(file_priv->event_wait,
625 !list_empty(&file_priv->event_list));
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000626 if (ret >= 0)
627 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
628 if (ret)
629 return ret;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000630 } else {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000631 unsigned length = e->event->length;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000632
Chris Wilson83eb64c2015-11-25 14:39:02 +0000633 if (length > count - ret) {
634put_back_event:
635 spin_lock_irq(&dev->event_lock);
636 file_priv->event_space -= length;
637 list_add(&e->link, &file_priv->event_list);
638 spin_unlock_irq(&dev->event_lock);
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000639 break;
640 }
641
Chris Wilson83eb64c2015-11-25 14:39:02 +0000642 if (copy_to_user(buffer + ret, e->event, length)) {
643 if (ret == 0)
644 ret = -EFAULT;
645 goto put_back_event;
646 }
647
648 ret += length;
Takashi Iwaia0a0bde2014-12-04 11:56:42 +0100649 e->destroy(e);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000650 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000651 }
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000652 mutex_unlock(&file_priv->event_read_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000653
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000654 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000655}
656EXPORT_SYMBOL(drm_read);
657
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100658/**
659 * drm_poll - poll method for DRM file
660 * @filp: file pointer
661 * @wait: poll waiter table
662 *
663 * This function must be used by drivers as their .read() #file_operations
664 * method iff they use DRM events for asynchronous signalling to userspace.
665 * Since events are used by the KMS API for vblank and page flip completion this
666 * means all modern display drivers must use it.
667 *
668 * See also drm_read().
669 *
670 * RETURNS:
671 *
672 * Mask of POLL flags indicating the current status of the file.
673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
675{
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000676 struct drm_file *file_priv = filp->private_data;
677 unsigned int mask = 0;
678
679 poll_wait(filp, &file_priv->event_wait, wait);
680
681 if (!list_empty(&file_priv->event_list))
682 mask |= POLLIN | POLLRDNORM;
683
684 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686EXPORT_SYMBOL(drm_poll);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100687
688/**
Daniel Vetter4020b222016-01-28 12:01:04 +0100689 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
690 * @dev: DRM device
691 * @file_priv: DRM file private data
692 * @p: tracking structure for the pending event
693 * @e: actual event data to deliver to userspace
694 *
695 * This function prepares the passed in event for eventual delivery. If the event
696 * doesn't get delivered (because the IOCTL fails later on, before queuing up
697 * anything) then the even must be cancelled and freed using
698 * drm_event_cancel_free(). Successfully initialized events should be sent out
699 * using drm_send_event() or drm_send_event_locked() to signal completion of the
700 * asynchronous event to userspace.
701 *
702 * If callers embedded @p into a larger structure it must be allocated with
703 * kmalloc and @p must be the first member element.
704 *
705 * This is the locked version of drm_event_reserve_init() for callers which
706 * already hold dev->event_lock.
707 *
708 * RETURNS:
709 *
710 * 0 on success or a negative error code on failure.
711 */
712int drm_event_reserve_init_locked(struct drm_device *dev,
713 struct drm_file *file_priv,
714 struct drm_pending_event *p,
715 struct drm_event *e)
716{
717 if (file_priv->event_space < e->length)
718 return -ENOMEM;
719
720 file_priv->event_space -= e->length;
721
722 p->event = e;
Daniel Vetter681047b2016-01-25 22:16:43 +0100723 list_add(&p->pending_link, &file_priv->pending_event_list);
Daniel Vetter4020b222016-01-28 12:01:04 +0100724 p->file_priv = file_priv;
725
726 /* we *could* pass this in as arg, but everyone uses kfree: */
727 p->destroy = (void (*) (struct drm_pending_event *)) kfree;
728
729 return 0;
730}
731EXPORT_SYMBOL(drm_event_reserve_init_locked);
732
733/**
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100734 * drm_event_reserve_init - init a DRM event and reserve space for it
735 * @dev: DRM device
736 * @file_priv: DRM file private data
737 * @p: tracking structure for the pending event
738 * @e: actual event data to deliver to userspace
739 *
740 * This function prepares the passed in event for eventual delivery. If the event
741 * doesn't get delivered (because the IOCTL fails later on, before queuing up
742 * anything) then the even must be cancelled and freed using
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100743 * drm_event_cancel_free(). Successfully initialized events should be sent out
744 * using drm_send_event() or drm_send_event_locked() to signal completion of the
745 * asynchronous event to userspace.
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100746 *
747 * If callers embedded @p into a larger structure it must be allocated with
748 * kmalloc and @p must be the first member element.
749 *
Daniel Vetter4020b222016-01-28 12:01:04 +0100750 * Callers which already hold dev->event_lock should use
751 * drm_event_reserve_init() instead.
752 *
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100753 * RETURNS:
754 *
755 * 0 on success or a negative error code on failure.
756 */
757int drm_event_reserve_init(struct drm_device *dev,
758 struct drm_file *file_priv,
759 struct drm_pending_event *p,
760 struct drm_event *e)
761{
762 unsigned long flags;
Daniel Vetter4020b222016-01-28 12:01:04 +0100763 int ret;
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100764
765 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100766 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100767 spin_unlock_irqrestore(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100768
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100769 return ret;
770}
771EXPORT_SYMBOL(drm_event_reserve_init);
772
773/**
774 * drm_event_cancel_free - free a DRM event and release it's space
775 * @dev: DRM device
776 * @p: tracking structure for the pending event
777 *
778 * This function frees the event @p initialized with drm_event_reserve_init()
779 * and releases any allocated space.
780 */
781void drm_event_cancel_free(struct drm_device *dev,
782 struct drm_pending_event *p)
783{
784 unsigned long flags;
785 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter681047b2016-01-25 22:16:43 +0100786 if (p->file_priv) {
787 p->file_priv->event_space += p->event->length;
788 list_del(&p->pending_link);
789 }
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100790 spin_unlock_irqrestore(&dev->event_lock, flags);
791 p->destroy(p);
792}
793EXPORT_SYMBOL(drm_event_cancel_free);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100794
795/**
796 * drm_send_event_locked - send DRM event to file descriptor
797 * @dev: DRM device
798 * @e: DRM event to deliver
799 *
800 * This function sends the event @e, initialized with drm_event_reserve_init(),
801 * to its associated userspace DRM file. Callers must already hold
802 * dev->event_lock, see drm_send_event() for the unlocked version.
Daniel Vetter681047b2016-01-25 22:16:43 +0100803 *
804 * Note that the core will take care of unlinking and disarming events when the
805 * corresponding DRM file is closed. Drivers need not worry about whether the
806 * DRM file for this event still exists and can call this function upon
807 * completion of the asynchronous work unconditionally.
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100808 */
809void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
810{
811 assert_spin_locked(&dev->event_lock);
812
Daniel Vetter681047b2016-01-25 22:16:43 +0100813 if (!e->file_priv) {
814 e->destroy(e);
815 return;
816 }
817
818 list_del(&e->pending_link);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100819 list_add_tail(&e->link,
820 &e->file_priv->event_list);
821 wake_up_interruptible(&e->file_priv->event_wait);
822}
823EXPORT_SYMBOL(drm_send_event_locked);
824
825/**
826 * drm_send_event - send DRM event to file descriptor
827 * @dev: DRM device
828 * @e: DRM event to deliver
829 *
830 * This function sends the event @e, initialized with drm_event_reserve_init(),
831 * to its associated userspace DRM file. This function acquires dev->event_lock,
832 * see drm_send_event_locked() for callers which already hold this lock.
Daniel Vetter681047b2016-01-25 22:16:43 +0100833 *
834 * Note that the core will take care of unlinking and disarming events when the
835 * corresponding DRM file is closed. Drivers need not worry about whether the
836 * DRM file for this event still exists and can call this function upon
837 * completion of the asynchronous work unconditionally.
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100838 */
839void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
840{
841 unsigned long irqflags;
842
843 spin_lock_irqsave(&dev->event_lock, irqflags);
844 drm_send_event_locked(dev, e);
845 spin_unlock_irqrestore(&dev->event_lock, irqflags);
846}
847EXPORT_SYMBOL(drm_send_event);