blob: b1838a41ad4359782432df8bbc7e71425fc74ee8 [file] [log] [blame]
Daniel Vetterbcb877e2016-01-11 22:40:55 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * \author Rickard E. (Rik) Faith <faith@valinux.com>
3 * \author Daryll Strauss <daryll@valinux.com>
4 * \author Gareth Hughes <gareth@valinux.com>
5 */
6
7/*
8 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
9 *
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
Daniel Vettera8f8b1d2017-03-08 15:12:42 +010037
Noralf Trønnesc76f0f72018-07-03 18:03:47 +020038#include <drm/drm_client.h>
Daniel Vettera8f8b1d2017-03-08 15:12:42 +010039#include <drm/drm_file.h>
40#include <drm/drmP.h>
41
David Herrmanne7b960702014-07-24 12:10:04 +020042#include "drm_legacy.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020043#include "drm_internal.h"
Daniel Vetter81065542016-06-21 10:54:13 +020044#include "drm_crtc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
David Herrmann0d639882014-02-24 15:53:25 +010046/* from BKL pushdown */
Arnd Bergmann58374712010-07-10 23:51:39 +020047DEFINE_MUTEX(drm_global_mutex);
48
Daniel Vetterbcb877e2016-01-11 22:40:55 +010049/**
50 * DOC: file operations
51 *
52 * Drivers must define the file operations structure that forms the DRM
53 * userspace API entry point, even though most of those operations are
Daniel Vetterb93658f2017-03-08 15:12:44 +010054 * implemented in the DRM core. The resulting &struct file_operations must be
55 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
Jani Nikula55edf412016-11-01 17:40:44 +020056 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
Daniel Vetterb93658f2017-03-08 15:12:44 +010057 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
58 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
59 * that require 32/64 bit compatibility support must provide their own
60 * &file_operations.compat_ioctl handler that processes private ioctls and calls
61 * drm_compat_ioctl() for core ioctls.
Daniel Vetterbcb877e2016-01-11 22:40:55 +010062 *
63 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
64 * events are a generic and extensible means to send asynchronous events to
65 * userspace through the file descriptor. They are used to send vblank event and
66 * page flip completions by the KMS API. But drivers can also use it for their
67 * own needs, e.g. to signal completion of rendering.
68 *
Daniel Vetterb93658f2017-03-08 15:12:44 +010069 * For the driver-side event interface see drm_event_reserve_init() and
70 * drm_send_event() as the main starting points.
71 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +010072 * The memory mapping implementation will vary depending on how the driver
73 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
74 * function, modern drivers should use one of the provided memory-manager
Daniel Vetterb93658f2017-03-08 15:12:44 +010075 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
76 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
Daniel Vetterbcb877e2016-01-11 22:40:55 +010077 *
78 * No other file operations are supported by the DRM userspace API. Overall the
Daniel Vetterbb2eaba2017-05-31 11:20:45 +020079 * following is an example &file_operations structure::
Daniel Vetterbcb877e2016-01-11 22:40:55 +010080 *
81 * static const example_drm_fops = {
82 * .owner = THIS_MODULE,
83 * .open = drm_open,
84 * .release = drm_release,
85 * .unlocked_ioctl = drm_ioctl,
Jani Nikula55edf412016-11-01 17:40:44 +020086 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
Daniel Vetterbcb877e2016-01-11 22:40:55 +010087 * .poll = drm_poll,
88 * .read = drm_read,
89 * .llseek = no_llseek,
90 * .mmap = drm_gem_mmap,
91 * };
Daniel Vetterb93658f2017-03-08 15:12:44 +010092 *
Daniel Vetterf42e1812017-03-08 15:12:57 +010093 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
94 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
95 * simpler.
Daniel Vetterbb2eaba2017-05-31 11:20:45 +020096 *
97 * The driver's &file_operations must be stored in &drm_driver.fops.
98 *
99 * For driver-private IOCTL handling see the more detailed discussion in
100 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100101 */
102
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400103static int drm_open_helper(struct file *filp, struct drm_minor *minor);
Dave Airliec94f7022005-07-07 21:03:38 +1000104
David Herrmann15720422018-06-18 16:17:28 +0200105/**
106 * drm_file_alloc - allocate file context
107 * @minor: minor to allocate on
108 *
109 * This allocates a new DRM file context. It is not linked into any context and
110 * can be used by the caller freely. Note that the context keeps a pointer to
111 * @minor, so it must be freed before @minor is.
112 *
113 * RETURNS:
114 * Pointer to newly allocated context, ERR_PTR on failure.
115 */
116struct drm_file *drm_file_alloc(struct drm_minor *minor)
117{
118 struct drm_device *dev = minor->dev;
119 struct drm_file *file;
120 int ret;
121
122 file = kzalloc(sizeof(*file), GFP_KERNEL);
123 if (!file)
124 return ERR_PTR(-ENOMEM);
125
126 file->pid = get_pid(task_pid(current));
127 file->minor = minor;
128
129 /* for compatibility root is always authenticated */
130 file->authenticated = capable(CAP_SYS_ADMIN);
131 file->lock_count = 0;
132
133 INIT_LIST_HEAD(&file->lhead);
134 INIT_LIST_HEAD(&file->fbs);
135 mutex_init(&file->fbs_lock);
136 INIT_LIST_HEAD(&file->blobs);
137 INIT_LIST_HEAD(&file->pending_event_list);
138 INIT_LIST_HEAD(&file->event_list);
139 init_waitqueue_head(&file->event_wait);
140 file->event_space = 4096; /* set aside 4k for event buffer */
141
142 mutex_init(&file->event_read_lock);
143
144 if (drm_core_check_feature(dev, DRIVER_GEM))
145 drm_gem_open(dev, file);
146
147 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
148 drm_syncobj_open(file);
149
150 if (drm_core_check_feature(dev, DRIVER_PRIME))
151 drm_prime_init_file_private(&file->prime);
152
153 if (dev->driver->open) {
154 ret = dev->driver->open(dev, file);
155 if (ret < 0)
156 goto out_prime_destroy;
157 }
158
David Herrmann15720422018-06-18 16:17:28 +0200159 return file;
160
David Herrmann15720422018-06-18 16:17:28 +0200161out_prime_destroy:
162 if (drm_core_check_feature(dev, DRIVER_PRIME))
163 drm_prime_destroy_file_private(&file->prime);
164 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
165 drm_syncobj_release(file);
166 if (drm_core_check_feature(dev, DRIVER_GEM))
167 drm_gem_release(dev, file);
168 put_pid(file->pid);
169 kfree(file);
170
171 return ERR_PTR(ret);
172}
173
174static void drm_events_release(struct drm_file *file_priv)
175{
176 struct drm_device *dev = file_priv->minor->dev;
177 struct drm_pending_event *e, *et;
178 unsigned long flags;
179
180 spin_lock_irqsave(&dev->event_lock, flags);
181
182 /* Unlink pending events */
183 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
184 pending_link) {
185 list_del(&e->pending_link);
186 e->file_priv = NULL;
187 }
188
189 /* Remove unconsumed events */
190 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
191 list_del(&e->link);
192 kfree(e);
193 }
194
195 spin_unlock_irqrestore(&dev->event_lock, flags);
196}
197
198/**
199 * drm_file_free - free file context
200 * @file: context to free, or NULL
201 *
202 * This destroys and deallocates a DRM file context previously allocated via
203 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
204 * before calling this.
205 *
206 * If NULL is passed, this is a no-op.
207 *
208 * RETURNS:
209 * 0 on success, or error code on failure.
210 */
211void drm_file_free(struct drm_file *file)
212{
213 struct drm_device *dev;
214
215 if (!file)
216 return;
217
218 dev = file->minor->dev;
219
220 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
221 task_pid_nr(current),
222 (long)old_encode_dev(file->minor->kdev->devt),
223 dev->open_count);
224
225 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
226 dev->driver->preclose)
227 dev->driver->preclose(dev, file);
228
229 if (drm_core_check_feature(dev, DRIVER_LEGACY))
230 drm_legacy_lock_release(dev, file->filp);
231
232 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
233 drm_legacy_reclaim_buffers(dev, file);
234
235 drm_events_release(file);
236
237 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
238 drm_fb_release(file);
239 drm_property_destroy_user_blobs(dev, file);
240 }
241
242 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
243 drm_syncobj_release(file);
244
245 if (drm_core_check_feature(dev, DRIVER_GEM))
246 drm_gem_release(dev, file);
247
248 drm_legacy_ctxbitmap_flush(dev, file);
249
250 if (drm_is_primary_client(file))
251 drm_master_release(file);
252
253 if (dev->driver->postclose)
254 dev->driver->postclose(dev, file);
255
256 if (drm_core_check_feature(dev, DRIVER_PRIME))
257 drm_prime_destroy_file_private(&file->prime);
258
259 WARN_ON(!list_empty(&file->event_list));
260
261 put_pid(file->pid);
262 kfree(file);
263}
264
Dave Airlie84b1fd12007-07-11 15:53:27 +1000265static int drm_setup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int ret;
268
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +0200269 if (dev->driver->firstopen &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200270 drm_core_check_feature(dev, DRIVER_LEGACY)) {
Dave Airlie22eae942005-11-10 22:16:34 +1100271 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return ret;
274 }
275
Daniel Vetterf336ab72013-08-08 15:41:35 +0200276 ret = drm_legacy_dma_setup(dev);
277 if (ret < 0)
278 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
284
285/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100286 * drm_open - open method for DRM file
287 * @inode: device inode
288 * @filp: file pointer.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100290 * This function must be used by drivers as their &file_operations.open method.
291 * It looks up the correct DRM device and instantiates all the per-file
292 * resources for it. It also calls the &drm_driver.open driver callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100294 * RETURNS:
295 *
296 * 0 on success or negative errno value on falure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000298int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
David Herrmann1616c522014-01-29 10:49:19 +0100300 struct drm_device *dev;
Dave Airlie2c14f282008-04-21 16:47:32 +1000301 struct drm_minor *minor;
David Herrmann1616c522014-01-29 10:49:19 +0100302 int retcode;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000303 int need_setup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
David Herrmann1616c522014-01-29 10:49:19 +0100305 minor = drm_minor_acquire(iminor(inode));
306 if (IS_ERR(minor))
307 return PTR_ERR(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308
David Herrmann1616c522014-01-29 10:49:19 +0100309 dev = minor->dev;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000310 if (!dev->open_count++)
311 need_setup = 1;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800312
David Herrmann6796cb12014-01-03 14:24:19 +0100313 /* share address_space across all char-devs of a single device */
314 filp->f_mapping = dev->anon_inode->i_mapping;
Dave Airlied985c102006-01-02 21:32:48 +1100315
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400316 retcode = drm_open_helper(filp, minor);
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000317 if (retcode)
318 goto err_undo;
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000319 if (need_setup) {
320 retcode = drm_setup(dev);
321 if (retcode)
322 goto err_undo;
323 }
324 return 0;
325
326err_undo:
Ilija Hadzicfdb40a02012-10-29 17:35:01 +0000327 dev->open_count--;
David Herrmann1616c522014-01-29 10:49:19 +0100328 drm_minor_release(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331EXPORT_SYMBOL(drm_open);
332
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100333/*
Dave Airlied985c102006-01-02 21:32:48 +1100334 * Check whether DRI will run on this CPU.
335 *
336 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
337 */
338static int drm_cpu_valid(void)
339{
Dave Airlied985c102006-01-02 21:32:48 +1100340#if defined(__sparc__) && !defined(__sparc_v9__)
341 return 0; /* No cmpxchg before v9 sparc. */
342#endif
343 return 1;
344}
345
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100346/*
Dave Airlied985c102006-01-02 21:32:48 +1100347 * Called whenever a process opens /dev/drm.
348 *
Dave Airlied985c102006-01-02 21:32:48 +1100349 * \param filp file pointer.
David Herrmannf4aede22014-01-29 10:18:02 +0100350 * \param minor acquired minor-object.
Dave Airlied985c102006-01-02 21:32:48 +1100351 * \return zero on success or a negative number on failure.
352 *
353 * Creates and initializes a drm_file structure for the file private data in \p
354 * filp and add it into the double linked list in \p dev.
355 */
Ilija Hadzic1dcc0ce2014-04-28 10:23:57 -0400356static int drm_open_helper(struct file *filp, struct drm_minor *minor)
Dave Airlied985c102006-01-02 21:32:48 +1100357{
David Herrmannf4aede22014-01-29 10:18:02 +0100358 struct drm_device *dev = minor->dev;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000359 struct drm_file *priv;
Noralf Trønnes7eeaeb92018-06-18 16:17:29 +0200360 int ret;
Dave Airlied985c102006-01-02 21:32:48 +1100361
362 if (filp->f_flags & O_EXCL)
363 return -EBUSY; /* No exclusive opens */
364 if (!drm_cpu_valid())
365 return -EINVAL;
Dave Airlie13bb9cc2012-09-12 15:55:05 +1000366 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 +1000367 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100368
David Herrmannf4aede22014-01-29 10:18:02 +0100369 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
Dave Airlied985c102006-01-02 21:32:48 +1100370
David Herrmann15720422018-06-18 16:17:28 +0200371 priv = drm_file_alloc(minor);
372 if (IS_ERR(priv))
373 return PTR_ERR(priv);
Dave Airlied985c102006-01-02 21:32:48 +1100374
Noralf Trønnes7eeaeb92018-06-18 16:17:29 +0200375 if (drm_is_primary_client(priv)) {
376 ret = drm_master_open(priv);
377 if (ret) {
378 drm_file_free(priv);
379 return ret;
380 }
381 }
382
Dave Airlied985c102006-01-02 21:32:48 +1100383 filp->private_data = priv;
Dave Airlie76ef6b22018-05-15 13:38:15 +1000384 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000385 priv->filp = filp;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000386
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200387 mutex_lock(&dev->filelist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000388 list_add(&priv->lhead, &dev->filelist);
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200389 mutex_unlock(&dev->filelist_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100390
391#ifdef __alpha__
392 /*
393 * Default the hose
394 */
395 if (!dev->hose) {
396 struct pci_dev *pci_dev;
397 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
398 if (pci_dev) {
399 dev->hose = pci_dev->sysdata;
400 pci_dev_put(pci_dev);
401 }
402 if (!dev->hose) {
Yijing Wang59c1ad3b2014-02-13 21:14:00 +0800403 struct pci_bus *b = list_entry(pci_root_buses.next,
404 struct pci_bus, node);
Dave Airlied985c102006-01-02 21:32:48 +1100405 if (b)
406 dev->hose = b->sysdata;
407 }
408 }
409#endif
410
411 return 0;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000412}
413
David Herrmann1c8887d2013-10-02 11:23:36 +0200414static void drm_legacy_dev_reinit(struct drm_device *dev)
415{
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200416 if (dev->irq_enabled)
David Herrmann1c8887d2013-10-02 11:23:36 +0200417 drm_irq_uninstall(dev);
418
419 mutex_lock(&dev->struct_mutex);
420
Daniel Vetter366884b2016-04-26 19:29:34 +0200421 drm_legacy_agp_clear(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200422
423 drm_legacy_sg_cleanup(dev);
David Herrmann03decbe2014-08-29 12:12:29 +0200424 drm_legacy_vma_flush(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200425 drm_legacy_dma_takedown(dev);
426
David Herrmann1c8887d2013-10-02 11:23:36 +0200427 mutex_unlock(&dev->struct_mutex);
428
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200429 dev->sigdata.lock = NULL;
430
431 dev->context_flag = 0;
432 dev->last_context = 0;
433 dev->if_version = 0;
David Herrmann1c8887d2013-10-02 11:23:36 +0200434
435 DRM_DEBUG("lastclose completed\n");
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200436}
437
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200438void drm_lastclose(struct drm_device * dev)
439{
440 DRM_DEBUG("\n");
441
442 if (dev->driver->lastclose)
443 dev->driver->lastclose(dev);
444 DRM_DEBUG("driver lastclose completed\n");
445
Daniel Vetterfa538642016-08-03 21:11:10 +0200446 if (drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200447 drm_legacy_dev_reinit(dev);
Noralf Trønnesc76f0f72018-07-03 18:03:47 +0200448
449 drm_client_dev_restore(dev);
David Herrmann1c8887d2013-10-02 11:23:36 +0200450}
451
452/**
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100453 * drm_release - release method for DRM file
454 * @inode: device inode
455 * @filp: file pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100457 * This function must be used by drivers as their &file_operations.release
458 * method. It frees any resources associated with the open file, and calls the
Daniel Vetter45c3d212017-05-08 10:26:33 +0200459 * &drm_driver.postclose driver callback. If this is the last open file for the
460 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100462 * RETURNS:
463 *
464 * Always succeeds and returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000468 struct drm_file *file_priv = filp->private_data;
David Herrmann1616c522014-01-29 10:49:19 +0100469 struct drm_minor *minor = file_priv->minor;
470 struct drm_device *dev = minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Arnd Bergmann58374712010-07-10 23:51:39 +0200472 mutex_lock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200476 mutex_lock(&dev->filelist_mutex);
Chris Wilsondff01de2014-07-24 14:23:10 +0100477 list_del(&file_priv->lhead);
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200478 mutex_unlock(&dev->filelist_mutex);
479
David Herrmann15720422018-06-18 16:17:28 +0200480 drm_file_free(file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482 if (!--dev->open_count) {
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200483 drm_lastclose(dev);
Daniel Vetterc07dcd62017-08-02 13:56:02 +0200484 if (drm_dev_is_unplugged(dev))
Dave Airlie2c07a212012-02-20 14:18:07 +0000485 drm_put_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Arnd Bergmann58374712010-07-10 23:51:39 +0200487 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David Herrmann1616c522014-01-29 10:49:19 +0100489 drm_minor_release(minor);
490
Daniel Vetter68dfbeb2016-04-26 19:29:35 +0200491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493EXPORT_SYMBOL(drm_release);
494
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100495/**
496 * drm_read - read method for DRM file
497 * @filp: file pointer
498 * @buffer: userspace destination pointer for the read
499 * @count: count in bytes to read
500 * @offset: offset to read
501 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100502 * This function must be used by drivers as their &file_operations.read
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100503 * method iff they use DRM events for asynchronous signalling to userspace.
504 * Since events are used by the KMS API for vblank and page flip completion this
505 * means all modern display drivers must use it.
506 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100507 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
508 * must set the &file_operation.llseek to no_llseek(). Polling support is
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100509 * provided by drm_poll().
510 *
511 * This function will only ever read a full event. Therefore userspace must
512 * supply a big enough buffer to fit any event to ensure forward progress. Since
513 * the maximum event space is currently 4K it's recommended to just use that for
514 * safety.
515 *
516 * RETURNS:
517 *
518 * Number of bytes read (always aligned to full events, and can be 0) or a
519 * negative error code on failure.
520 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000521ssize_t drm_read(struct file *filp, char __user *buffer,
522 size_t count, loff_t *offset)
523{
524 struct drm_file *file_priv = filp->private_data;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000525 struct drm_device *dev = file_priv->minor->dev;
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000526 ssize_t ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000527
Linus Torvalds96d4f262019-01-03 18:57:57 -0800528 if (!access_ok(buffer, count))
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000529 return -EFAULT;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000530
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000531 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
532 if (ret)
533 return ret;
534
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000535 for (;;) {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000536 struct drm_pending_event *e = NULL;
537
538 spin_lock_irq(&dev->event_lock);
539 if (!list_empty(&file_priv->event_list)) {
540 e = list_first_entry(&file_priv->event_list,
541 struct drm_pending_event, link);
542 file_priv->event_space += e->event->length;
543 list_del(&e->link);
544 }
545 spin_unlock_irq(&dev->event_lock);
546
547 if (e == NULL) {
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000548 if (ret)
549 break;
550
551 if (filp->f_flags & O_NONBLOCK) {
552 ret = -EAGAIN;
553 break;
554 }
555
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000556 mutex_unlock(&file_priv->event_read_lock);
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000557 ret = wait_event_interruptible(file_priv->event_wait,
558 !list_empty(&file_priv->event_list));
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000559 if (ret >= 0)
560 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
561 if (ret)
562 return ret;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000563 } else {
Chris Wilson83eb64c2015-11-25 14:39:02 +0000564 unsigned length = e->event->length;
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000565
Chris Wilson83eb64c2015-11-25 14:39:02 +0000566 if (length > count - ret) {
567put_back_event:
568 spin_lock_irq(&dev->event_lock);
569 file_priv->event_space -= length;
570 list_add(&e->link, &file_priv->event_list);
571 spin_unlock_irq(&dev->event_lock);
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000572 break;
573 }
574
Chris Wilson83eb64c2015-11-25 14:39:02 +0000575 if (copy_to_user(buffer + ret, e->event, length)) {
576 if (ret == 0)
577 ret = -EFAULT;
578 goto put_back_event;
579 }
580
581 ret += length;
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200582 kfree(e);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000583 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000584 }
Chris Wilson9b2c0b72015-11-25 14:39:03 +0000585 mutex_unlock(&file_priv->event_read_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000586
Chris Wilsoncdd1cf72014-12-04 21:03:25 +0000587 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000588}
589EXPORT_SYMBOL(drm_read);
590
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100591/**
592 * drm_poll - poll method for DRM file
593 * @filp: file pointer
594 * @wait: poll waiter table
595 *
Daniel Vetterb93658f2017-03-08 15:12:44 +0100596 * This function must be used by drivers as their &file_operations.read method
597 * iff they use DRM events for asynchronous signalling to userspace. Since
598 * events are used by the KMS API for vblank and page flip completion this means
599 * all modern display drivers must use it.
Daniel Vetterbcb877e2016-01-11 22:40:55 +0100600 *
601 * See also drm_read().
602 *
603 * RETURNS:
604 *
605 * Mask of POLL flags indicating the current status of the file.
606 */
Al Viroafc9a422017-07-03 06:39:46 -0400607__poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000609 struct drm_file *file_priv = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400610 __poll_t mask = 0;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000611
612 poll_wait(filp, &file_priv->event_wait, wait);
613
614 if (!list_empty(&file_priv->event_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800615 mask |= EPOLLIN | EPOLLRDNORM;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000616
617 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619EXPORT_SYMBOL(drm_poll);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100620
621/**
Daniel Vetter4020b222016-01-28 12:01:04 +0100622 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
623 * @dev: DRM device
624 * @file_priv: DRM file private data
625 * @p: tracking structure for the pending event
626 * @e: actual event data to deliver to userspace
627 *
628 * This function prepares the passed in event for eventual delivery. If the event
629 * doesn't get delivered (because the IOCTL fails later on, before queuing up
630 * anything) then the even must be cancelled and freed using
631 * drm_event_cancel_free(). Successfully initialized events should be sent out
632 * using drm_send_event() or drm_send_event_locked() to signal completion of the
633 * asynchronous event to userspace.
634 *
635 * If callers embedded @p into a larger structure it must be allocated with
636 * kmalloc and @p must be the first member element.
637 *
638 * This is the locked version of drm_event_reserve_init() for callers which
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100639 * already hold &drm_device.event_lock.
Daniel Vetter4020b222016-01-28 12:01:04 +0100640 *
641 * RETURNS:
642 *
643 * 0 on success or a negative error code on failure.
644 */
645int drm_event_reserve_init_locked(struct drm_device *dev,
646 struct drm_file *file_priv,
647 struct drm_pending_event *p,
648 struct drm_event *e)
649{
650 if (file_priv->event_space < e->length)
651 return -ENOMEM;
652
653 file_priv->event_space -= e->length;
654
655 p->event = e;
Daniel Vetter681047b2016-01-25 22:16:43 +0100656 list_add(&p->pending_link, &file_priv->pending_event_list);
Daniel Vetter4020b222016-01-28 12:01:04 +0100657 p->file_priv = file_priv;
658
Daniel Vetter4020b222016-01-28 12:01:04 +0100659 return 0;
660}
661EXPORT_SYMBOL(drm_event_reserve_init_locked);
662
663/**
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100664 * drm_event_reserve_init - init a DRM event and reserve space for it
665 * @dev: DRM device
666 * @file_priv: DRM file private data
667 * @p: tracking structure for the pending event
668 * @e: actual event data to deliver to userspace
669 *
670 * This function prepares the passed in event for eventual delivery. If the event
671 * doesn't get delivered (because the IOCTL fails later on, before queuing up
672 * anything) then the even must be cancelled and freed using
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100673 * drm_event_cancel_free(). Successfully initialized events should be sent out
674 * using drm_send_event() or drm_send_event_locked() to signal completion of the
675 * asynchronous event to userspace.
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100676 *
677 * If callers embedded @p into a larger structure it must be allocated with
678 * kmalloc and @p must be the first member element.
679 *
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100680 * Callers which already hold &drm_device.event_lock should use
Thierry Reding20c9ca4f2016-12-15 12:36:02 +0100681 * drm_event_reserve_init_locked() instead.
Daniel Vetter4020b222016-01-28 12:01:04 +0100682 *
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100683 * RETURNS:
684 *
685 * 0 on success or a negative error code on failure.
686 */
687int drm_event_reserve_init(struct drm_device *dev,
688 struct drm_file *file_priv,
689 struct drm_pending_event *p,
690 struct drm_event *e)
691{
692 unsigned long flags;
Daniel Vetter4020b222016-01-28 12:01:04 +0100693 int ret;
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100694
695 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100696 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100697 spin_unlock_irqrestore(&dev->event_lock, flags);
Daniel Vetter4020b222016-01-28 12:01:04 +0100698
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100699 return ret;
700}
701EXPORT_SYMBOL(drm_event_reserve_init);
702
703/**
Matt Roper1e55a532019-02-01 17:23:26 -0800704 * drm_event_cancel_free - free a DRM event and release its space
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100705 * @dev: DRM device
706 * @p: tracking structure for the pending event
707 *
708 * This function frees the event @p initialized with drm_event_reserve_init()
Daniel Vetterb93658f2017-03-08 15:12:44 +0100709 * and releases any allocated space. It is used to cancel an event when the
710 * nonblocking operation could not be submitted and needed to be aborted.
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100711 */
712void drm_event_cancel_free(struct drm_device *dev,
713 struct drm_pending_event *p)
714{
715 unsigned long flags;
716 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter681047b2016-01-25 22:16:43 +0100717 if (p->file_priv) {
718 p->file_priv->event_space += p->event->length;
719 list_del(&p->pending_link);
720 }
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100721 spin_unlock_irqrestore(&dev->event_lock, flags);
Gustavo Padovan838de392016-10-20 12:50:03 -0200722
723 if (p->fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100724 dma_fence_put(p->fence);
Gustavo Padovan838de392016-10-20 12:50:03 -0200725
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200726 kfree(p);
Daniel Vetter2dd500f2016-01-11 22:40:56 +0100727}
728EXPORT_SYMBOL(drm_event_cancel_free);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100729
730/**
731 * drm_send_event_locked - send DRM event to file descriptor
732 * @dev: DRM device
733 * @e: DRM event to deliver
734 *
735 * This function sends the event @e, initialized with drm_event_reserve_init(),
736 * to its associated userspace DRM file. Callers must already hold
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100737 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
Daniel Vetter681047b2016-01-25 22:16:43 +0100738 *
739 * Note that the core will take care of unlinking and disarming events when the
740 * corresponding DRM file is closed. Drivers need not worry about whether the
741 * DRM file for this event still exists and can call this function upon
742 * completion of the asynchronous work unconditionally.
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100743 */
744void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
745{
746 assert_spin_locked(&dev->event_lock);
747
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200748 if (e->completion) {
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200749 complete_all(e->completion);
Daniel Vetter24835e42016-12-21 11:23:30 +0100750 e->completion_release(e->completion);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200751 e->completion = NULL;
752 }
753
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200754 if (e->fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100755 dma_fence_signal(e->fence);
756 dma_fence_put(e->fence);
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200757 }
758
Daniel Vetter681047b2016-01-25 22:16:43 +0100759 if (!e->file_priv) {
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +0200760 kfree(e);
Daniel Vetter681047b2016-01-25 22:16:43 +0100761 return;
762 }
763
764 list_del(&e->pending_link);
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100765 list_add_tail(&e->link,
766 &e->file_priv->event_list);
767 wake_up_interruptible(&e->file_priv->event_wait);
768}
769EXPORT_SYMBOL(drm_send_event_locked);
770
771/**
772 * drm_send_event - send DRM event to file descriptor
773 * @dev: DRM device
774 * @e: DRM event to deliver
775 *
776 * This function sends the event @e, initialized with drm_event_reserve_init(),
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100777 * to its associated userspace DRM file. This function acquires
778 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
779 * hold this lock.
Daniel Vetter681047b2016-01-25 22:16:43 +0100780 *
781 * Note that the core will take care of unlinking and disarming events when the
782 * corresponding DRM file is closed. Drivers need not worry about whether the
783 * DRM file for this event still exists and can call this function upon
784 * completion of the asynchronous work unconditionally.
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100785 */
786void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
787{
788 unsigned long irqflags;
789
790 spin_lock_irqsave(&dev->event_lock, irqflags);
791 drm_send_event_locked(dev, e);
792 spin_unlock_irqrestore(&dev->event_lock, irqflags);
793}
794EXPORT_SYMBOL(drm_send_event);