blob: 4ce5318d14bc782812ce1d43486e902194b8f92b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Arnd Bergmann58374712010-07-10 23:51:39 +020042/* from BKL pushdown: note that nothing else serializes idr_find() */
43DEFINE_MUTEX(drm_global_mutex);
Ben Skeggse3461a22010-08-26 14:58:57 +100044EXPORT_SYMBOL(drm_global_mutex);
Arnd Bergmann58374712010-07-10 23:51:39 +020045
Dave Airlieb5e89ed2005-09-25 14:28:13 +100046static int drm_open_helper(struct inode *inode, struct file *filp,
David Herrmannf4aede22014-01-29 10:18:02 +010047 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;
86 struct address_space *old_mapping;
Ilija Hadzica8ec3a62013-04-02 09:56:10 -040087 struct address_space *old_imapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
David Herrmann1616c522014-01-29 10:49:19 +010089 minor = drm_minor_acquire(iminor(inode));
90 if (IS_ERR(minor))
91 return PTR_ERR(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092
David Herrmann1616c522014-01-29 10:49:19 +010093 dev = minor->dev;
94 if (drm_device_is_unplugged(dev)) {
95 retcode = -ENODEV;
96 goto err_release;
97 }
Dave Airlie2c07a212012-02-20 14:18:07 +000098
Ilija Hadzicfdb40a02012-10-29 17:35:01 +000099 if (!dev->open_count++)
100 need_setup = 1;
101 mutex_lock(&dev->struct_mutex);
Ilija Hadzica8ec3a62013-04-02 09:56:10 -0400102 old_imapping = inode->i_mapping;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000103 old_mapping = dev->dev_mapping;
104 if (old_mapping == NULL)
105 dev->dev_mapping = &inode->i_data;
106 /* ihold ensures nobody can remove inode with our i_data */
107 ihold(container_of(dev->dev_mapping, struct inode, i_data));
108 inode->i_mapping = dev->dev_mapping;
109 filp->f_mapping = dev->dev_mapping;
110 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800111
David Herrmannf4aede22014-01-29 10:18:02 +0100112 retcode = drm_open_helper(inode, filp, minor);
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000113 if (retcode)
114 goto err_undo;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000115 if (need_setup) {
116 retcode = drm_setup(dev);
117 if (retcode)
118 goto err_undo;
119 }
120 return 0;
121
122err_undo:
123 mutex_lock(&dev->struct_mutex);
Ilija Hadzica8ec3a62013-04-02 09:56:10 -0400124 filp->f_mapping = old_imapping;
125 inode->i_mapping = old_imapping;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000126 iput(container_of(dev->dev_mapping, struct inode, i_data));
127 dev->dev_mapping = old_mapping;
128 mutex_unlock(&dev->struct_mutex);
129 dev->open_count--;
David Herrmann1616c522014-01-29 10:49:19 +0100130err_release:
131 drm_minor_release(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return retcode;
133}
134EXPORT_SYMBOL(drm_open);
135
136/**
Dave Airlied985c102006-01-02 21:32:48 +1100137 * File \c open operation.
138 *
139 * \param inode device inode.
140 * \param filp file pointer.
141 *
142 * Puts the dev->fops corresponding to the device minor number into
143 * \p filp, call the \c open method, and restore the file operations.
144 */
145int drm_stub_open(struct inode *inode, struct file *filp)
146{
David Herrmann1616c522014-01-29 10:49:19 +0100147 struct drm_device *dev;
Dave Airlie2c14f282008-04-21 16:47:32 +1000148 struct drm_minor *minor;
Dave Airlied985c102006-01-02 21:32:48 +1100149 int err = -ENODEV;
Al Viroe84f9e52013-09-22 14:17:15 -0400150 const struct file_operations *new_fops;
Dave Airlied985c102006-01-02 21:32:48 +1100151
152 DRM_DEBUG("\n");
153
Arnd Bergmann58374712010-07-10 23:51:39 +0200154 mutex_lock(&drm_global_mutex);
David Herrmann1616c522014-01-29 10:49:19 +0100155 minor = drm_minor_acquire(iminor(inode));
156 if (IS_ERR(minor))
157 goto out_unlock;
Dave Airlied985c102006-01-02 21:32:48 +1100158
David Herrmann1616c522014-01-29 10:49:19 +0100159 dev = minor->dev;
Dave Airlie2c07a212012-02-20 14:18:07 +0000160 if (drm_device_is_unplugged(dev))
David Herrmann1616c522014-01-29 10:49:19 +0100161 goto out_release;
Dave Airlie2c07a212012-02-20 14:18:07 +0000162
Al Viroe84f9e52013-09-22 14:17:15 -0400163 new_fops = fops_get(dev->driver->fops);
164 if (!new_fops)
David Herrmann1616c522014-01-29 10:49:19 +0100165 goto out_release;
Dave Airlied985c102006-01-02 21:32:48 +1100166
Al Viroe84f9e52013-09-22 14:17:15 -0400167 replace_fops(filp, new_fops);
168 if (filp->f_op->open)
169 err = filp->f_op->open(inode, filp);
David Herrmann1616c522014-01-29 10:49:19 +0100170
171out_release:
172 drm_minor_release(minor);
173out_unlock:
Arnd Bergmann58374712010-07-10 23:51:39 +0200174 mutex_unlock(&drm_global_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100175 return err;
176}
177
178/**
179 * Check whether DRI will run on this CPU.
180 *
181 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
182 */
183static int drm_cpu_valid(void)
184{
185#if defined(__i386__)
186 if (boot_cpu_data.x86 == 3)
187 return 0; /* No cmpxchg on a 386 */
188#endif
189#if defined(__sparc__) && !defined(__sparc_v9__)
190 return 0; /* No cmpxchg before v9 sparc. */
191#endif
192 return 1;
193}
194
195/**
196 * Called whenever a process opens /dev/drm.
197 *
198 * \param inode device inode.
199 * \param filp file pointer.
David Herrmannf4aede22014-01-29 10:18:02 +0100200 * \param minor acquired minor-object.
Dave Airlied985c102006-01-02 21:32:48 +1100201 * \return zero on success or a negative number on failure.
202 *
203 * Creates and initializes a drm_file structure for the file private data in \p
204 * filp and add it into the double linked list in \p dev.
205 */
206static int drm_open_helper(struct inode *inode, struct file *filp,
David Herrmannf4aede22014-01-29 10:18:02 +0100207 struct drm_minor *minor)
Dave Airlied985c102006-01-02 21:32:48 +1100208{
David Herrmannf4aede22014-01-29 10:18:02 +0100209 struct drm_device *dev = minor->dev;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000210 struct drm_file *priv;
Dave Airlied985c102006-01-02 21:32:48 +1100211 int ret;
212
213 if (filp->f_flags & O_EXCL)
214 return -EBUSY; /* No exclusive opens */
215 if (!drm_cpu_valid())
216 return -EINVAL;
Dave Airlie13bb9cc2012-09-12 15:55:05 +1000217 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 +1000218 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100219
David Herrmannf4aede22014-01-29 10:18:02 +0100220 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
Dave Airlied985c102006-01-02 21:32:48 +1100221
Julia Lawall6ebc22e2010-05-13 21:58:56 +0200222 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Dave Airlied985c102006-01-02 21:32:48 +1100223 if (!priv)
224 return -ENOMEM;
225
Dave Airlied985c102006-01-02 21:32:48 +1100226 filp->private_data = priv;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000227 priv->filp = filp;
David Howells2df68b42008-09-02 11:03:14 +1000228 priv->uid = current_euid();
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800229 priv->pid = get_pid(task_pid(current));
David Herrmannf4aede22014-01-29 10:18:02 +0100230 priv->minor = minor;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900231
Dave Airlied985c102006-01-02 21:32:48 +1100232 /* for compatibility root is always authenticated */
Chris Wilson1020dc62013-10-29 08:55:57 +0000233 priv->always_authenticated = capable(CAP_SYS_ADMIN);
234 priv->authenticated = priv->always_authenticated;
Dave Airlied985c102006-01-02 21:32:48 +1100235 priv->lock_count = 0;
236
Dave Airliebd1b3312007-05-26 05:01:51 +1000237 INIT_LIST_HEAD(&priv->lhead);
Dave Airlief453ba02008-11-07 14:05:41 -0800238 INIT_LIST_HEAD(&priv->fbs);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100239 mutex_init(&priv->fbs_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000240 INIT_LIST_HEAD(&priv->event_list);
241 init_waitqueue_head(&priv->event_wait);
242 priv->event_space = 4096; /* set aside 4k for event buffer */
Dave Airliebd1b3312007-05-26 05:01:51 +1000243
Eric Anholt673a3942008-07-30 12:06:12 -0700244 if (dev->driver->driver_features & DRIVER_GEM)
245 drm_gem_open(dev, priv);
246
Dave Airlie32488772011-11-25 15:21:02 +0000247 if (drm_core_check_feature(dev, DRIVER_PRIME))
248 drm_prime_init_file_private(&priv->prime);
249
Dave Airlied985c102006-01-02 21:32:48 +1100250 if (dev->driver->open) {
251 ret = dev->driver->open(dev, priv);
252 if (ret < 0)
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900253 goto out_prime_destroy;
Dave Airlied985c102006-01-02 21:32:48 +1100254 }
255
David Herrmann17931262013-08-25 18:29:00 +0200256 /* if there is no current master make this fd it, but do not create
257 * any master object for render clients */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000258 mutex_lock(&dev->struct_mutex);
David Herrmann17931262013-08-25 18:29:00 +0200259 if (!priv->minor->master && !drm_is_render_client(priv)) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000260 /* create a new master */
261 priv->minor->master = drm_master_create(priv->minor);
262 if (!priv->minor->master) {
Dan Carpenterdba5ed02009-03-27 13:34:28 +0300263 mutex_unlock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000264 ret = -ENOMEM;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900265 goto out_close;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000266 }
267
268 priv->is_master = 1;
269 /* take another reference for the copy in the local file priv */
270 priv->master = drm_master_get(priv->minor->master);
271
272 priv->authenticated = 1;
273
274 mutex_unlock(&dev->struct_mutex);
275 if (dev->driver->master_create) {
276 ret = dev->driver->master_create(dev, priv->master);
277 if (ret) {
278 mutex_lock(&dev->struct_mutex);
279 /* drop both references if this fails */
280 drm_master_put(&priv->minor->master);
281 drm_master_put(&priv->master);
282 mutex_unlock(&dev->struct_mutex);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900283 goto out_close;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000284 }
285 }
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000286 mutex_lock(&dev->struct_mutex);
287 if (dev->driver->master_set) {
288 ret = dev->driver->master_set(dev, priv, true);
289 if (ret) {
290 /* drop both references if this fails */
291 drm_master_put(&priv->minor->master);
292 drm_master_put(&priv->master);
293 mutex_unlock(&dev->struct_mutex);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900294 goto out_close;
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000295 }
296 }
David Herrmann17931262013-08-25 18:29:00 +0200297 } else if (!drm_is_render_client(priv)) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000298 /* get a reference to the master */
299 priv->master = drm_master_get(priv->minor->master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000300 }
David Herrmann17931262013-08-25 18:29:00 +0200301 mutex_unlock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000302
303 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000304 list_add(&priv->lhead, &dev->filelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100305 mutex_unlock(&dev->struct_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100306
307#ifdef __alpha__
308 /*
309 * Default the hose
310 */
311 if (!dev->hose) {
312 struct pci_dev *pci_dev;
313 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
314 if (pci_dev) {
315 dev->hose = pci_dev->sysdata;
316 pci_dev_put(pci_dev);
317 }
318 if (!dev->hose) {
319 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
320 if (b)
321 dev->hose = b->sysdata;
322 }
323 }
324#endif
325
326 return 0;
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900327
328out_close:
329 if (dev->driver->postclose)
330 dev->driver->postclose(dev, priv);
331out_prime_destroy:
332 if (drm_core_check_feature(dev, DRIVER_PRIME))
333 drm_prime_destroy_file_private(&priv->prime);
334 if (dev->driver->driver_features & DRIVER_GEM)
335 drm_gem_release(dev, priv);
Seung-Woo Kimdf9b6a92013-07-02 09:53:28 +0900336 put_pid(priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700337 kfree(priv);
Dave Airlied985c102006-01-02 21:32:48 +1100338 filp->private_data = NULL;
339 return ret;
340}
341
Dave Airlie7c1c2872008-11-28 14:22:24 +1000342static void drm_master_release(struct drm_device *dev, struct file *filp)
343{
344 struct drm_file *file_priv = filp->private_data;
345
Dave Airlie7c1c2872008-11-28 14:22:24 +1000346 if (drm_i_have_hw_lock(dev, file_priv)) {
347 DRM_DEBUG("File %p released, freeing lock for context %d\n",
348 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
349 drm_lock_free(&file_priv->master->lock,
350 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
351 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000352}
353
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000354static void drm_events_release(struct drm_file *file_priv)
355{
356 struct drm_device *dev = file_priv->minor->dev;
357 struct drm_pending_event *e, *et;
358 struct drm_pending_vblank_event *v, *vt;
359 unsigned long flags;
360
361 spin_lock_irqsave(&dev->event_lock, flags);
362
363 /* Remove pending flips */
364 list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
365 if (v->base.file_priv == file_priv) {
366 list_del(&v->base.link);
367 drm_vblank_put(dev, v->pipe);
368 v->base.destroy(&v->base);
369 }
370
371 /* Remove unconsumed events */
YoungJun Cho1dda6802013-10-29 20:30:26 +0900372 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
373 list_del(&e->link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000374 e->destroy(e);
YoungJun Cho1dda6802013-10-29 20:30:26 +0900375 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000376
377 spin_unlock_irqrestore(&dev->event_lock, flags);
378}
379
Dave Airlied985c102006-01-02 21:32:48 +1100380/**
David Herrmann1c8887d2013-10-02 11:23:36 +0200381 * drm_legacy_dev_reinit
382 *
383 * Reinitializes a legacy/ums drm device in it's lastclose function.
384 */
385static void drm_legacy_dev_reinit(struct drm_device *dev)
386{
David Herrmann1c8887d2013-10-02 11:23:36 +0200387 if (drm_core_check_feature(dev, DRIVER_MODESET))
388 return;
389
David Herrmann1c8887d2013-10-02 11:23:36 +0200390 dev->sigdata.lock = NULL;
391
392 dev->context_flag = 0;
393 dev->last_context = 0;
394 dev->if_version = 0;
395}
396
397/**
398 * Take down the DRM device.
399 *
400 * \param dev DRM device structure.
401 *
402 * Frees every resource in \p dev.
403 *
404 * \sa drm_device
405 */
406int drm_lastclose(struct drm_device * dev)
407{
408 struct drm_vma_entry *vma, *vma_temp;
409
410 DRM_DEBUG("\n");
411
412 if (dev->driver->lastclose)
413 dev->driver->lastclose(dev);
414 DRM_DEBUG("driver lastclose completed\n");
415
416 if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
417 drm_irq_uninstall(dev);
418
419 mutex_lock(&dev->struct_mutex);
420
421 drm_agp_clear(dev);
422
423 drm_legacy_sg_cleanup(dev);
424
425 /* Clear vma list (only built for debugging) */
426 list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
427 list_del(&vma->head);
428 kfree(vma);
429 }
430
431 drm_legacy_dma_takedown(dev);
432
433 dev->dev_mapping = NULL;
434 mutex_unlock(&dev->struct_mutex);
435
436 drm_legacy_dev_reinit(dev);
437
438 DRM_DEBUG("lastclose completed\n");
439 return 0;
440}
441
442/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * Release file.
444 *
445 * \param inode device inode
Eric Anholt6c340ea2007-08-25 20:23:09 +1000446 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * \return zero on success or a negative number on failure.
448 *
449 * If the hardware lock is held then free it, and take it again for the kernel
450 * context since it's necessary to reclaim buffers. Unlink the file private
451 * data from its list and free it. Decreases the open count and if it reaches
Dave Airlie22eae942005-11-10 22:16:34 +1100452 * zero calls drm_lastclose().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000454int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000456 struct drm_file *file_priv = filp->private_data;
David Herrmann1616c522014-01-29 10:49:19 +0100457 struct drm_minor *minor = file_priv->minor;
458 struct drm_device *dev = minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int retcode = 0;
460
Arnd Bergmann58374712010-07-10 23:51:39 +0200461 mutex_lock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Dave Airlie22eae942005-11-10 22:16:34 +1100465 if (dev->driver->preclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000466 dev->driver->preclose(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* ========================================================
469 * Begin inline drm_release
470 */
471
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700473 task_pid_nr(current),
David Herrmann58178782014-01-29 13:12:31 +0100474 (long)old_encode_dev(file_priv->minor->kdev->devt),
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100477 /* Release any auth tokens that might point to this file_priv,
478 (do that under the drm_global_mutex) */
479 if (file_priv->magic)
480 (void) drm_remove_magic(file_priv->master, file_priv->magic);
481
Dave Airlie7c1c2872008-11-28 14:22:24 +1000482 /* if the master has gone away we can't do anything with the lock */
483 if (file_priv->minor->master)
484 drm_master_release(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Daniel Vetter67cb4b42011-10-26 00:31:26 +0200486 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
487 drm_core_reclaim_buffers(dev, file_priv);
488
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000489 drm_events_release(file_priv);
490
Kristian Høgsbergea39f832009-02-12 14:37:56 -0500491 if (dev->driver->driver_features & DRIVER_MODESET)
492 drm_fb_release(file_priv);
493
Prathyush4e47e022012-04-14 17:22:13 +0530494 if (dev->driver->driver_features & DRIVER_GEM)
495 drm_gem_release(dev, file_priv);
496
Dave Airliec21eb212013-09-20 08:32:59 +1000497 mutex_lock(&dev->ctxlist_mutex);
498 if (!list_empty(&dev->ctxlist)) {
499 struct drm_ctx_list *pos, *n;
500
501 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
502 if (pos->tag == file_priv &&
503 pos->handle != DRM_KERNEL_CONTEXT) {
504 if (dev->driver->context_dtor)
505 dev->driver->context_dtor(dev,
506 pos->handle);
507
508 drm_ctxbitmap_free(dev, pos->handle);
509
510 list_del(&pos->head);
511 kfree(pos);
Dave Airliec21eb212013-09-20 08:32:59 +1000512 }
513 }
514 }
515 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Dave Airlie30e2fb12006-02-02 19:37:46 +1100517 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000518
Dave Airlie7c1c2872008-11-28 14:22:24 +1000519 if (file_priv->is_master) {
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100520 struct drm_master *master = file_priv->master;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000521 struct drm_file *temp;
522 list_for_each_entry(temp, &dev->filelist, lhead) {
523 if ((temp->master == file_priv->master) &&
524 (temp != file_priv))
Chris Wilson1020dc62013-10-29 08:55:57 +0000525 temp->authenticated = temp->always_authenticated;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000526 }
527
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100528 /**
529 * Since the master is disappearing, so is the
530 * possibility to lock.
531 */
532
533 if (master->lock.hw_lock) {
534 if (dev->sigdata.lock == master->lock.hw_lock)
535 dev->sigdata.lock = NULL;
536 master->lock.hw_lock = NULL;
537 master->lock.file_priv = NULL;
538 wake_up_interruptible_all(&master->lock.lock_queue);
539 }
540
Dave Airlie7c1c2872008-11-28 14:22:24 +1000541 if (file_priv->minor->master == file_priv->master) {
542 /* drop the reference held my the minor */
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000543 if (dev->driver->master_drop)
544 dev->driver->master_drop(dev, file_priv, true);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000545 drm_master_put(&file_priv->minor->master);
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000548
Ilija Hadzic949c4a32012-05-15 16:40:10 -0400549 BUG_ON(dev->dev_mapping == NULL);
550 iput(container_of(dev->dev_mapping, struct inode, i_data));
551
Dave Airlie7c1c2872008-11-28 14:22:24 +1000552 /* drop the reference held my the file priv */
David Herrmann17931262013-08-25 18:29:00 +0200553 if (file_priv->master)
554 drm_master_put(&file_priv->master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000555 file_priv->is_master = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000556 list_del(&file_priv->lhead);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100557 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558
Dave Airlie22eae942005-11-10 22:16:34 +1100559 if (dev->driver->postclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000560 dev->driver->postclose(dev, file_priv);
Dave Airlie32488772011-11-25 15:21:02 +0000561
Daniel Vetter319c9332013-08-15 00:02:46 +0200562
Dave Airlie32488772011-11-25 15:21:02 +0000563 if (drm_core_check_feature(dev, DRIVER_PRIME))
564 drm_prime_destroy_file_private(&file_priv->prime);
565
Eric W. Biederman5fce5e02012-02-07 16:47:26 -0800566 put_pid(file_priv->pid);
Eric Anholt9a298b22009-03-24 12:23:04 -0700567 kfree(file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* ========================================================
570 * End inline drm_release
571 */
572
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 if (!--dev->open_count) {
Daniel Vetter43d13372013-12-11 11:35:08 +0100574 retcode = drm_lastclose(dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000575 if (drm_device_is_unplugged(dev))
576 drm_put_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Arnd Bergmann58374712010-07-10 23:51:39 +0200578 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David Herrmann1616c522014-01-29 10:49:19 +0100580 drm_minor_release(minor);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return retcode;
583}
584EXPORT_SYMBOL(drm_release);
585
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000586static bool
587drm_dequeue_event(struct drm_file *file_priv,
588 size_t total, size_t max, struct drm_pending_event **out)
589{
590 struct drm_device *dev = file_priv->minor->dev;
591 struct drm_pending_event *e;
592 unsigned long flags;
593 bool ret = false;
594
595 spin_lock_irqsave(&dev->event_lock, flags);
596
597 *out = NULL;
598 if (list_empty(&file_priv->event_list))
599 goto out;
600 e = list_first_entry(&file_priv->event_list,
601 struct drm_pending_event, link);
602 if (e->event->length + total > max)
603 goto out;
604
605 file_priv->event_space += e->event->length;
606 list_del(&e->link);
607 *out = e;
608 ret = true;
609
610out:
611 spin_unlock_irqrestore(&dev->event_lock, flags);
612 return ret;
613}
614
615ssize_t drm_read(struct file *filp, char __user *buffer,
616 size_t count, loff_t *offset)
617{
618 struct drm_file *file_priv = filp->private_data;
619 struct drm_pending_event *e;
620 size_t total;
621 ssize_t ret;
622
623 ret = wait_event_interruptible(file_priv->event_wait,
624 !list_empty(&file_priv->event_list));
625 if (ret < 0)
626 return ret;
627
628 total = 0;
629 while (drm_dequeue_event(file_priv, total, count, &e)) {
630 if (copy_to_user(buffer + total,
631 e->event, e->event->length)) {
632 total = -EFAULT;
633 break;
634 }
635
636 total += e->event->length;
637 e->destroy(e);
638 }
639
640 return total;
641}
642EXPORT_SYMBOL(drm_read);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
645{
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000646 struct drm_file *file_priv = filp->private_data;
647 unsigned int mask = 0;
648
649 poll_wait(filp, &file_priv->event_wait, wait);
650
651 if (!list_empty(&file_priv->event_list))
652 mask |= POLLIN | POLLRDNORM;
653
654 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656EXPORT_SYMBOL(drm_poll);