blob: 6b5625e6611975735a1bd4c218edd3cfa9ec5e69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
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
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -040047static int drm_open_helper(struct file *filp, struct drm_minor *minor);
Dave Airliec94f7022005-07-07 21:03:38 +100048
Dave Airlie84b1fd12007-07-11 15:53:27 +100049static int drm_setup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int ret;
52
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +020053 if (dev->driver->firstopen &&
54 !drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie22eae942005-11-10 22:16:34 +110055 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100056 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return ret;
58 }
59
Daniel Vetterf336ab72013-08-08 15:41:35 +020060 ret = drm_legacy_dma_setup(dev);
61 if (ret < 0)
62 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67}
68
69/**
70 * Open file.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100071 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * \param inode device inode
73 * \param filp file pointer.
74 * \return zero on success or a negative number on failure.
75 *
76 * Searches the DRM device with the same minor number, calls open_helper(), and
77 * increments the device open count. If the open count was previous at zero,
78 * i.e., it's the first that the device is open, then calls setup().
79 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100080int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
David Herrmann1616c522014-01-29 10:49:19 +010082 struct drm_device *dev;
Dave Airlie2c14f282008-04-21 16:47:32 +100083 struct drm_minor *minor;
David Herrmann1616c522014-01-29 10:49:19 +010084 int retcode;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +000085 int need_setup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
David Herrmann1616c522014-01-29 10:49:19 +010087 minor = drm_minor_acquire(iminor(inode));
88 if (IS_ERR(minor))
89 return PTR_ERR(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090
David Herrmann1616c522014-01-29 10:49:19 +010091 dev = minor->dev;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +000092 if (!dev->open_count++)
93 need_setup = 1;
Jesse Barnesa2c0a972008-11-05 10:31:53 -080094
David Herrmann6796cb12014-01-03 14:24:19 +010095 /* share address_space across all char-devs of a single device */
96 filp->f_mapping = dev->anon_inode->i_mapping;
Dave Airlied985c102006-01-02 21:32:48 +110097
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -040098 retcode = drm_open_helper(filp, minor);
Ilija Hadzicfdb40a02012-10-29 17:35:01 +000099 if (retcode)
100 goto err_undo;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000101 if (need_setup) {
102 retcode = drm_setup(dev);
103 if (retcode)
104 goto err_undo;
105 }
106 return 0;
107
108err_undo:
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000109 dev->open_count--;
David Herrmann1616c522014-01-29 10:49:19 +0100110 drm_minor_release(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113EXPORT_SYMBOL(drm_open);
114
115/**
Dave Airlied985c102006-01-02 21:32:48 +1100116 * Check whether DRI will run on this CPU.
117 *
118 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
119 */
120static int drm_cpu_valid(void)
121{
Dave Airlied985c102006-01-02 21:32:48 +1100122#if defined(__sparc__) && !defined(__sparc_v9__)
123 return 0; /* No cmpxchg before v9 sparc. */
124#endif
125 return 1;
126}
127
128/**
Thomas Hellstroma0af2e52015-12-02 09:24:46 -0800129 * drm_new_set_master - Allocate a new master object and become master for the
130 * associated master realm.
131 *
132 * @dev: The associated device.
133 * @fpriv: File private identifying the client.
134 *
135 * This function must be called with dev::struct_mutex held.
136 * Returns negative error code on failure. Zero on success.
137 */
138int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
139{
140 struct drm_master *old_master;
141 int ret;
142
143 lockdep_assert_held_once(&dev->master_mutex);
144
145 /* create a new master */
146 fpriv->minor->master = drm_master_create(fpriv->minor);
147 if (!fpriv->minor->master)
148 return -ENOMEM;
149
150 /* take another reference for the copy in the local file priv */
151 old_master = fpriv->master;
152 fpriv->master = drm_master_get(fpriv->minor->master);
153
154 if (dev->driver->master_create) {
155 ret = dev->driver->master_create(dev, fpriv->master);
156 if (ret)
157 goto out_err;
158 }
159 if (dev->driver->master_set) {
160 ret = dev->driver->master_set(dev, fpriv, true);
161 if (ret)
162 goto out_err;
163 }
164
165 fpriv->is_master = 1;
166 fpriv->allowed_master = 1;
167 fpriv->authenticated = 1;
168 if (old_master)
169 drm_master_put(&old_master);
170
171 return 0;
172
173out_err:
174 /* drop both references and restore old master on failure */
175 drm_master_put(&fpriv->minor->master);
176 drm_master_put(&fpriv->master);
177 fpriv->master = old_master;
178
179 return ret;
180}
181
182/**
Dave Airlied985c102006-01-02 21:32:48 +1100183 * Called whenever a process opens /dev/drm.
184 *
Dave Airlied985c102006-01-02 21:32:48 +1100185 * \param filp file pointer.
David Herrmannf4aede22014-01-29 10:18:02 +0100186 * \param minor acquired minor-object.
Dave Airlied985c102006-01-02 21:32:48 +1100187 * \return zero on success or a negative number on failure.
188 *
189 * Creates and initializes a drm_file structure for the file private data in \p
190 * filp and add it into the double linked list in \p dev.
191 */
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400192static int drm_open_helper(struct file *filp, struct drm_minor *minor)
Dave Airlied985c102006-01-02 21:32:48 +1100193{
David Herrmannf4aede22014-01-29 10:18:02 +0100194 struct drm_device *dev = minor->dev;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000195 struct drm_file *priv;
Dave Airlied985c102006-01-02 21:32:48 +1100196 int ret;
197
198 if (filp->f_flags & O_EXCL)
199 return -EBUSY; /* No exclusive opens */
200 if (!drm_cpu_valid())
201 return -EINVAL;
Dave Airlie13bb9cc2012-09-12 15:55:05 +1000202 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 +1000203 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100204
David Herrmannf4aede22014-01-29 10:18:02 +0100205 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
Dave Airlied985c102006-01-02 21:32:48 +1100206
Julia Lawall6ebc22e2010-05-13 21:58:56 +0200207 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Dave Airlied985c102006-01-02 21:32:48 +1100208 if (!priv)
209 return -ENOMEM;
210
Dave Airlied985c102006-01-02 21:32:48 +1100211 filp->private_data = priv;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000212 priv->filp = filp;
David Howells2df68b42008-09-02 11:03:14 +1000213 priv->uid = current_euid();
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800214 priv->pid = get_pid(task_pid(current));
David Herrmannf4aede22014-01-29 10:18:02 +0100215 priv->minor = minor;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900216
Dave Airlied985c102006-01-02 21:32:48 +1100217 /* for compatibility root is always authenticated */
David Herrmann3cb01a92014-07-22 17:12:26 +0200218 priv->authenticated = capable(CAP_SYS_ADMIN);
Dave Airlied985c102006-01-02 21:32:48 +1100219 priv->lock_count = 0;
220
Dave Airliebd1b3312007-05-26 05:01:51 +1000221 INIT_LIST_HEAD(&priv->lhead);
Dave Airlief453ba02008-11-07 14:05:41 -0800222 INIT_LIST_HEAD(&priv->fbs);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100223 mutex_init(&priv->fbs_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100224 INIT_LIST_HEAD(&priv->blobs);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000225 INIT_LIST_HEAD(&priv->event_list);
226 init_waitqueue_head(&priv->event_wait);
227 priv->event_space = 4096; /* set aside 4k for event buffer */
Dave Airliebd1b3312007-05-26 05:01:51 +1000228
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
David Herrmann17931262013-08-25 18:29:00 +0200241 /* if there is no current master make this fd it, but do not create
242 * any master object for render clients */
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100243 mutex_lock(&dev->master_mutex);
Thomas Hellstrom43683052014-03-13 11:07:44 +0100244 if (drm_is_primary_client(priv) && !priv->minor->master) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000245 /* create a new master */
Thomas Hellstroma0af2e52015-12-02 09:24:46 -0800246 ret = drm_new_set_master(dev, priv);
247 if (ret)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900248 goto out_close;
Thomas Hellstrom43683052014-03-13 11:07:44 +0100249 } else if (drm_is_primary_client(priv)) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000250 /* get a reference to the master */
251 priv->master = drm_master_get(priv->minor->master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000252 }
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100253 mutex_unlock(&dev->master_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000254
255 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000256 list_add(&priv->lhead, &dev->filelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100257 mutex_unlock(&dev->struct_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:
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100282 mutex_unlock(&dev->master_mutex);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900283 if (dev->driver->postclose)
284 dev->driver->postclose(dev, priv);
285out_prime_destroy:
286 if (drm_core_check_feature(dev, DRIVER_PRIME))
287 drm_prime_destroy_file_private(&priv->prime);
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200288 if (drm_core_check_feature(dev, DRIVER_GEM))
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900289 drm_gem_release(dev, priv);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900290 put_pid(priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700291 kfree(priv);
Dave Airlied985c102006-01-02 21:32:48 +1100292 filp->private_data = NULL;
293 return ret;
294}
295
Dave Airlie7c1c2872008-11-28 14:22:24 +1000296static void drm_master_release(struct drm_device *dev, struct file *filp)
297{
298 struct drm_file *file_priv = filp->private_data;
299
David Herrmannbb6d8222014-08-29 12:12:46 +0200300 if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000301 DRM_DEBUG("File %p released, freeing lock for context %d\n",
302 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
David Herrmannbb6d8222014-08-29 12:12:46 +0200303 drm_legacy_lock_free(&file_priv->master->lock,
304 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
Dave Airlie7c1c2872008-11-28 14:22:24 +1000305 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000306}
307
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000308static void drm_events_release(struct drm_file *file_priv)
309{
310 struct drm_device *dev = file_priv->minor->dev;
311 struct drm_pending_event *e, *et;
312 struct drm_pending_vblank_event *v, *vt;
313 unsigned long flags;
314
315 spin_lock_irqsave(&dev->event_lock, flags);
316
317 /* Remove pending flips */
318 list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
319 if (v->base.file_priv == file_priv) {
320 list_del(&v->base.link);
321 drm_vblank_put(dev, v->pipe);
322 v->base.destroy(&v->base);
323 }
324
325 /* Remove unconsumed events */
YoungJun Cho1dda6802013-10-29 20:30:26 +0900326 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
327 list_del(&e->link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000328 e->destroy(e);
YoungJun Cho1dda6802013-10-29 20:30:26 +0900329 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000330
331 spin_unlock_irqrestore(&dev->event_lock, flags);
332}
333
Dave Airlied985c102006-01-02 21:32:48 +1100334/**
David Herrmann1c8887d2013-10-02 11:23:36 +0200335 * drm_legacy_dev_reinit
336 *
337 * Reinitializes a legacy/ums drm device in it's lastclose function.
338 */
339static void drm_legacy_dev_reinit(struct drm_device *dev)
340{
David Herrmann1c8887d2013-10-02 11:23:36 +0200341 if (drm_core_check_feature(dev, DRIVER_MODESET))
342 return;
343
David Herrmann1c8887d2013-10-02 11:23:36 +0200344 dev->sigdata.lock = NULL;
345
346 dev->context_flag = 0;
347 dev->last_context = 0;
348 dev->if_version = 0;
349}
350
351/**
352 * Take down the DRM device.
353 *
354 * \param dev DRM device structure.
355 *
356 * Frees every resource in \p dev.
357 *
358 * \sa drm_device
359 */
360int drm_lastclose(struct drm_device * dev)
361{
David Herrmann1c8887d2013-10-02 11:23:36 +0200362 DRM_DEBUG("\n");
363
364 if (dev->driver->lastclose)
365 dev->driver->lastclose(dev);
366 DRM_DEBUG("driver lastclose completed\n");
367
368 if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
369 drm_irq_uninstall(dev);
370
371 mutex_lock(&dev->struct_mutex);
372
373 drm_agp_clear(dev);
374
375 drm_legacy_sg_cleanup(dev);
David Herrmann03decbe2014-08-29 12:12:29 +0200376 drm_legacy_vma_flush(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200377 drm_legacy_dma_takedown(dev);
378
David Herrmann1c8887d2013-10-02 11:23:36 +0200379 mutex_unlock(&dev->struct_mutex);
380
381 drm_legacy_dev_reinit(dev);
382
383 DRM_DEBUG("lastclose completed\n");
384 return 0;
385}
386
387/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * Release file.
389 *
390 * \param inode device inode
Eric Anholt6c340ea2007-08-25 20:23:09 +1000391 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * \return zero on success or a negative number on failure.
393 *
394 * If the hardware lock is held then free it, and take it again for the kernel
395 * context since it's necessary to reclaim buffers. Unlink the file private
396 * data from its list and free it. Decreases the open count and if it reaches
Dave Airlie22eae942005-11-10 22:16:34 +1100397 * zero calls drm_lastclose().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000399int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000401 struct drm_file *file_priv = filp->private_data;
David Herrmann1616c522014-01-29 10:49:19 +0100402 struct drm_minor *minor = file_priv->minor;
403 struct drm_device *dev = minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int retcode = 0;
405
Arnd Bergmann58374712010-07-10 23:51:39 +0200406 mutex_lock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Chris Wilsondff01de2014-07-24 14:23:10 +0100410 mutex_lock(&dev->struct_mutex);
411 list_del(&file_priv->lhead);
David Herrmann32e7b942015-05-04 21:01:30 +0200412 if (file_priv->magic)
413 idr_remove(&file_priv->master->magic_map, file_priv->magic);
Chris Wilsondff01de2014-07-24 14:23:10 +0100414 mutex_unlock(&dev->struct_mutex);
415
Dave Airlie22eae942005-11-10 22:16:34 +1100416 if (dev->driver->preclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000417 dev->driver->preclose(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /* ========================================================
420 * Begin inline drm_release
421 */
422
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000423 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700424 task_pid_nr(current),
David Herrmann58178782014-01-29 13:12:31 +0100425 (long)old_encode_dev(file_priv->minor->kdev->devt),
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dave Airlie7c1c2872008-11-28 14:22:24 +1000428 /* if the master has gone away we can't do anything with the lock */
429 if (file_priv->minor->master)
430 drm_master_release(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200432 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
Daniel Vettera2661622014-09-11 07:41:51 +0200433 drm_legacy_reclaim_buffers(dev, file_priv);
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200434
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000435 drm_events_release(file_priv);
436
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100437 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500438 drm_fb_release(file_priv);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +0100439 drm_property_destroy_user_blobs(dev, file_priv);
440 }
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500441
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200442 if (drm_core_check_feature(dev, DRIVER_GEM))
Prathyush4e47e022012-04-14 17:22:13 +0530443 drm_gem_release(dev, file_priv);
444
David Herrmanne7b960702014-07-24 12:10:04 +0200445 drm_legacy_ctxbitmap_flush(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100447 mutex_lock(&dev->master_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000448
Dave Airlie7963e9d2014-08-08 07:30:53 +1000449 if (file_priv->is_master) {
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100450 struct drm_master *master = file_priv->master;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000451
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100452 /**
453 * Since the master is disappearing, so is the
454 * possibility to lock.
455 */
David Herrmann3cb01a92014-07-22 17:12:26 +0200456 mutex_lock(&dev->struct_mutex);
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100457 if (master->lock.hw_lock) {
458 if (dev->sigdata.lock == master->lock.hw_lock)
459 dev->sigdata.lock = NULL;
460 master->lock.hw_lock = NULL;
461 master->lock.file_priv = NULL;
462 wake_up_interruptible_all(&master->lock.lock_queue);
463 }
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100464 mutex_unlock(&dev->struct_mutex);
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100465
Dave Airlie7c1c2872008-11-28 14:22:24 +1000466 if (file_priv->minor->master == file_priv->master) {
467 /* drop the reference held my the minor */
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000468 if (dev->driver->master_drop)
469 dev->driver->master_drop(dev, file_priv, true);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000470 drm_master_put(&file_priv->minor->master);
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000473
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100474 /* drop the master reference held by the file priv */
David Herrmann17931262013-08-25 18:29:00 +0200475 if (file_priv->master)
476 drm_master_put(&file_priv->master);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000477 file_priv->is_master = 0;
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100478 mutex_unlock(&dev->master_mutex);
479
Dave Airlie22eae942005-11-10 22:16:34 +1100480 if (dev->driver->postclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000481 dev->driver->postclose(dev, file_priv);
Dave Airlie32488772011-11-25 15:21:02 +0000482
Daniel Vetter319c9332013-08-15 00:02:46 +0200483
Dave Airlie32488772011-11-25 15:21:02 +0000484 if (drm_core_check_feature(dev, DRIVER_PRIME))
485 drm_prime_destroy_file_private(&file_priv->prime);
486
Ville Syrjäläddde4372014-08-06 14:02:50 +0300487 WARN_ON(!list_empty(&file_priv->event_list));
488
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800489 put_pid(file_priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700490 kfree(file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* ========================================================
493 * End inline drm_release
494 */
495
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000496 if (!--dev->open_count) {
Daniel Vetter43d13372013-12-11 11:35:08 +0100497 retcode = drm_lastclose(dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000498 if (drm_device_is_unplugged(dev))
499 drm_put_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Arnd Bergmann58374712010-07-10 23:51:39 +0200501 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
David Herrmann1616c522014-01-29 10:49:19 +0100503 drm_minor_release(minor);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return retcode;
506}
507EXPORT_SYMBOL(drm_release);
508
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000509ssize_t drm_read(struct file *filp, char __user *buffer,
510 size_t count, loff_t *offset)
511{
512 struct drm_file *file_priv = filp->private_data;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000513 struct drm_device *dev = file_priv->minor->dev;
514 ssize_t ret = 0;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000515
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000516 if (!access_ok(VERIFY_WRITE, buffer, count))
517 return -EFAULT;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000518
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000519 spin_lock_irq(&dev->event_lock);
520 for (;;) {
521 if (list_empty(&file_priv->event_list)) {
522 if (ret)
523 break;
524
525 if (filp->f_flags & O_NONBLOCK) {
526 ret = -EAGAIN;
527 break;
528 }
529
530 spin_unlock_irq(&dev->event_lock);
531 ret = wait_event_interruptible(file_priv->event_wait,
532 !list_empty(&file_priv->event_list));
533 spin_lock_irq(&dev->event_lock);
534 if (ret < 0)
535 break;
536
537 ret = 0;
538 } else {
539 struct drm_pending_event *e;
540
541 e = list_first_entry(&file_priv->event_list,
542 struct drm_pending_event, link);
543 if (e->event->length + ret > count)
544 break;
545
546 if (__copy_to_user_inatomic(buffer + ret,
547 e->event, e->event->length)) {
548 if (ret == 0)
549 ret = -EFAULT;
550 break;
551 }
552
553 file_priv->event_space += e->event->length;
554 ret += e->event->length;
555 list_del(&e->link);
Takashi Iwaia0a0bde2014-12-04 11:56:42 +0100556 e->destroy(e);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000557 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000558 }
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000559 spin_unlock_irq(&dev->event_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000560
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000561 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000562}
563EXPORT_SYMBOL(drm_read);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
566{
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000567 struct drm_file *file_priv = filp->private_data;
568 unsigned int mask = 0;
569
570 poll_wait(filp, &file_priv->event_wait, wait);
571
572 if (!list_empty(&file_priv->event_list))
573 mask |= POLLIN | POLLRDNORM;
574
575 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577EXPORT_SYMBOL(drm_poll);