]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/drm_file.c
drm/nouveau/mmu/nv44: implement vmm on top of new base
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / drm_file.c
CommitLineData
bcb877e4 1/*
1da177e4
LT
2 * \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
1da177e4 34#include <linux/poll.h>
5a0e3ad6 35#include <linux/slab.h>
e0cd3608 36#include <linux/module.h>
a8f8b1d9
DV
37
38#include <drm/drm_file.h>
39#include <drm/drmP.h>
40
e7b96070 41#include "drm_legacy.h"
67d0ec4e 42#include "drm_internal.h"
81065548 43#include "drm_crtc_internal.h"
1da177e4 44
0d639883 45/* from BKL pushdown */
58374713
AB
46DEFINE_MUTEX(drm_global_mutex);
47
bcb877e4
DV
48/**
49 * DOC: file operations
50 *
51 * Drivers must define the file operations structure that forms the DRM
52 * userspace API entry point, even though most of those operations are
b93658f8
DV
53 * implemented in the DRM core. The resulting &struct file_operations must be
54 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
55edf41b 55 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
b93658f8
DV
56 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
57 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
58 * that require 32/64 bit compatibility support must provide their own
59 * &file_operations.compat_ioctl handler that processes private ioctls and calls
60 * drm_compat_ioctl() for core ioctls.
bcb877e4
DV
61 *
62 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
63 * events are a generic and extensible means to send asynchronous events to
64 * userspace through the file descriptor. They are used to send vblank event and
65 * page flip completions by the KMS API. But drivers can also use it for their
66 * own needs, e.g. to signal completion of rendering.
67 *
b93658f8
DV
68 * For the driver-side event interface see drm_event_reserve_init() and
69 * drm_send_event() as the main starting points.
70 *
bcb877e4
DV
71 * The memory mapping implementation will vary depending on how the driver
72 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
73 * function, modern drivers should use one of the provided memory-manager
b93658f8
DV
74 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
75 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
bcb877e4
DV
76 *
77 * No other file operations are supported by the DRM userspace API. Overall the
bb2eaba6 78 * following is an example &file_operations structure::
bcb877e4
DV
79 *
80 * static const example_drm_fops = {
81 * .owner = THIS_MODULE,
82 * .open = drm_open,
83 * .release = drm_release,
84 * .unlocked_ioctl = drm_ioctl,
55edf41b 85 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
bcb877e4
DV
86 * .poll = drm_poll,
87 * .read = drm_read,
88 * .llseek = no_llseek,
89 * .mmap = drm_gem_mmap,
90 * };
b93658f8 91 *
f42e1819
DV
92 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
93 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
94 * simpler.
bb2eaba6
DV
95 *
96 * The driver's &file_operations must be stored in &drm_driver.fops.
97 *
98 * For driver-private IOCTL handling see the more detailed discussion in
99 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
bcb877e4
DV
100 */
101
1dcc0ceb 102static int drm_open_helper(struct file *filp, struct drm_minor *minor);
c94f7029 103
84b1fd10 104static int drm_setup(struct drm_device * dev)
1da177e4 105{
1da177e4
LT
106 int ret;
107
7d14bb6b 108 if (dev->driver->firstopen &&
fa538645 109 drm_core_check_feature(dev, DRIVER_LEGACY)) {
22eae947 110 ret = dev->driver->firstopen(dev);
b5e89ed5 111 if (ret != 0)
1da177e4
LT
112 return ret;
113 }
114
f336ab76
DV
115 ret = drm_legacy_dma_setup(dev);
116 if (ret < 0)
117 return ret;
1da177e4 118
1da177e4 119
b5e89ed5 120 DRM_DEBUG("\n");
1da177e4
LT
121 return 0;
122}
123
124/**
bcb877e4
DV
125 * drm_open - open method for DRM file
126 * @inode: device inode
127 * @filp: file pointer.
b5e89ed5 128 *
b93658f8
DV
129 * This function must be used by drivers as their &file_operations.open method.
130 * It looks up the correct DRM device and instantiates all the per-file
131 * resources for it. It also calls the &drm_driver.open driver callback.
bcb877e4
DV
132 *
133 * RETURNS:
1da177e4 134 *
bcb877e4 135 * 0 on success or negative errno value on falure.
1da177e4 136 */
b5e89ed5 137int drm_open(struct inode *inode, struct file *filp)
1da177e4 138{
1616c525 139 struct drm_device *dev;
2c14f28b 140 struct drm_minor *minor;
1616c525 141 int retcode;
fdb40a08 142 int need_setup = 0;
b5e89ed5 143
1616c525
DH
144 minor = drm_minor_acquire(iminor(inode));
145 if (IS_ERR(minor))
146 return PTR_ERR(minor);
2c07a21d 147
1616c525 148 dev = minor->dev;
fdb40a08
IH
149 if (!dev->open_count++)
150 need_setup = 1;
fdb40a08 151
6796cb16
DH
152 /* share address_space across all char-devs of a single device */
153 filp->f_mapping = dev->anon_inode->i_mapping;
fdb40a08 154
1dcc0ceb 155 retcode = drm_open_helper(filp, minor);
fdb40a08
IH
156 if (retcode)
157 goto err_undo;
fdb40a08
IH
158 if (need_setup) {
159 retcode = drm_setup(dev);
160 if (retcode)
161 goto err_undo;
f453ba04 162 }
fdb40a08 163 return 0;
a2c0a97b 164
fdb40a08 165err_undo:
fdb40a08 166 dev->open_count--;
1616c525 167 drm_minor_release(minor);
1da177e4
LT
168 return retcode;
169}
170EXPORT_SYMBOL(drm_open);
171
bcb877e4 172/*
d985c108
DA
173 * Check whether DRI will run on this CPU.
174 *
175 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
176 */
177static int drm_cpu_valid(void)
178{
d985c108
DA
179#if defined(__sparc__) && !defined(__sparc_v9__)
180 return 0; /* No cmpxchg before v9 sparc. */
181#endif
182 return 1;
183}
184
bcb877e4 185/*
d985c108
DA
186 * Called whenever a process opens /dev/drm.
187 *
d985c108 188 * \param filp file pointer.
f4aede2e 189 * \param minor acquired minor-object.
d985c108
DA
190 * \return zero on success or a negative number on failure.
191 *
192 * Creates and initializes a drm_file structure for the file private data in \p
193 * filp and add it into the double linked list in \p dev.
194 */
1dcc0ceb 195static int drm_open_helper(struct file *filp, struct drm_minor *minor)
d985c108 196{
f4aede2e 197 struct drm_device *dev = minor->dev;
84b1fd10 198 struct drm_file *priv;
d985c108
DA
199 int ret;
200
201 if (filp->f_flags & O_EXCL)
202 return -EBUSY; /* No exclusive opens */
203 if (!drm_cpu_valid())
204 return -EINVAL;
13bb9cc8 205 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
5bcf719b 206 return -EINVAL;
d985c108 207
f4aede2e 208 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
d985c108 209
6ebc22e6 210 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
d985c108
DA
211 if (!priv)
212 return -ENOMEM;
213
d985c108 214 filp->private_data = priv;
6c340eac 215 priv->filp = filp;
5fce5e0b 216 priv->pid = get_pid(task_pid(current));
f4aede2e 217 priv->minor = minor;
df9b6a9c 218
d985c108 219 /* for compatibility root is always authenticated */
3cb01a98 220 priv->authenticated = capable(CAP_SYS_ADMIN);
d985c108
DA
221 priv->lock_count = 0;
222
bd1b331f 223 INIT_LIST_HEAD(&priv->lhead);
f453ba04 224 INIT_LIST_HEAD(&priv->fbs);
4b096ac1 225 mutex_init(&priv->fbs_lock);
e2f5d2ea 226 INIT_LIST_HEAD(&priv->blobs);
681047b4 227 INIT_LIST_HEAD(&priv->pending_event_list);
c9a9c5e0
KH
228 INIT_LIST_HEAD(&priv->event_list);
229 init_waitqueue_head(&priv->event_wait);
230 priv->event_space = 4096; /* set aside 4k for event buffer */
bd1b331f 231
9b2c0b7f
CW
232 mutex_init(&priv->event_read_lock);
233
1bcecfac 234 if (drm_core_check_feature(dev, DRIVER_GEM))
673a394b
EA
235 drm_gem_open(dev, priv);
236
e9083420
DA
237 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
238 drm_syncobj_open(priv);
239
3248877e
DA
240 if (drm_core_check_feature(dev, DRIVER_PRIME))
241 drm_prime_init_file_private(&priv->prime);
242
d985c108
DA
243 if (dev->driver->open) {
244 ret = dev->driver->open(dev, priv);
245 if (ret < 0)
df9b6a9c 246 goto out_prime_destroy;
d985c108
DA
247 }
248
2cbae7e6
DV
249 if (drm_is_primary_client(priv)) {
250 ret = drm_master_open(priv);
a0af2e53 251 if (ret)
df9b6a9c 252 goto out_close;
7c1c2871 253 }
bd1b331f 254
1d2ac403 255 mutex_lock(&dev->filelist_mutex);
bd1b331f 256 list_add(&priv->lhead, &dev->filelist);
1d2ac403 257 mutex_unlock(&dev->filelist_mutex);
d985c108
DA
258
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) {
59c1ad3b
YW
271 struct pci_bus *b = list_entry(pci_root_buses.next,
272 struct pci_bus, node);
d985c108
DA
273 if (b)
274 dev->hose = b->sysdata;
275 }
276 }
277#endif
278
279 return 0;
df9b6a9c
SWK
280
281out_close:
282 if (dev->driver->postclose)
283 dev->driver->postclose(dev, priv);
284out_prime_destroy:
285 if (drm_core_check_feature(dev, DRIVER_PRIME))
286 drm_prime_destroy_file_private(&priv->prime);
e9083420
DA
287 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
288 drm_syncobj_release(priv);
1bcecfac 289 if (drm_core_check_feature(dev, DRIVER_GEM))
df9b6a9c 290 drm_gem_release(dev, priv);
df9b6a9c 291 put_pid(priv->pid);
9a298b2a 292 kfree(priv);
d985c108
DA
293 filp->private_data = NULL;
294 return ret;
295}
296
c9a9c5e0
KH
297static void drm_events_release(struct drm_file *file_priv)
298{
299 struct drm_device *dev = file_priv->minor->dev;
300 struct drm_pending_event *e, *et;
c9a9c5e0
KH
301 unsigned long flags;
302
303 spin_lock_irqsave(&dev->event_lock, flags);
304
681047b4
DV
305 /* Unlink pending events */
306 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
307 pending_link) {
308 list_del(&e->pending_link);
309 e->file_priv = NULL;
310 }
311
c9a9c5e0 312 /* Remove unconsumed events */
1dda6805
YC
313 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
314 list_del(&e->link);
1b47aaf9 315 kfree(e);
1dda6805 316 }
c9a9c5e0
KH
317
318 spin_unlock_irqrestore(&dev->event_lock, flags);
319}
320
1c8887dd
DH
321static void drm_legacy_dev_reinit(struct drm_device *dev)
322{
68dfbeba
DV
323 if (dev->irq_enabled)
324 drm_irq_uninstall(dev);
325
326 mutex_lock(&dev->struct_mutex);
327
328 drm_legacy_agp_clear(dev);
329
330 drm_legacy_sg_cleanup(dev);
331 drm_legacy_vma_flush(dev);
332 drm_legacy_dma_takedown(dev);
333
334 mutex_unlock(&dev->struct_mutex);
1c8887dd 335
1c8887dd
DH
336 dev->sigdata.lock = NULL;
337
338 dev->context_flag = 0;
339 dev->last_context = 0;
340 dev->if_version = 0;
68dfbeba
DV
341
342 DRM_DEBUG("lastclose completed\n");
1c8887dd
DH
343}
344
68dfbeba 345void drm_lastclose(struct drm_device * dev)
1c8887dd 346{
1c8887dd
DH
347 DRM_DEBUG("\n");
348
349 if (dev->driver->lastclose)
350 dev->driver->lastclose(dev);
351 DRM_DEBUG("driver lastclose completed\n");
352
fa538645 353 if (drm_core_check_feature(dev, DRIVER_LEGACY))
68dfbeba 354 drm_legacy_dev_reinit(dev);
1c8887dd
DH
355}
356
1da177e4 357/**
bcb877e4
DV
358 * drm_release - release method for DRM file
359 * @inode: device inode
360 * @filp: file pointer.
1da177e4 361 *
b93658f8
DV
362 * This function must be used by drivers as their &file_operations.release
363 * method. It frees any resources associated with the open file, and calls the
45c3d213
DV
364 * &drm_driver.postclose driver callback. If this is the last open file for the
365 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
1da177e4 366 *
bcb877e4
DV
367 * RETURNS:
368 *
369 * Always succeeds and returns 0.
1da177e4 370 */
b5e89ed5 371int drm_release(struct inode *inode, struct file *filp)
1da177e4 372{
6c340eac 373 struct drm_file *file_priv = filp->private_data;
1616c525
DH
374 struct drm_minor *minor = file_priv->minor;
375 struct drm_device *dev = minor->dev;
1da177e4 376
58374713 377 mutex_lock(&drm_global_mutex);
1da177e4 378
b5e89ed5 379 DRM_DEBUG("open_count = %d\n", dev->open_count);
1da177e4 380
1d2ac403 381 mutex_lock(&dev->filelist_mutex);
dff01de1 382 list_del(&file_priv->lhead);
1d2ac403
DV
383 mutex_unlock(&dev->filelist_mutex);
384
45c3d213
DV
385 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
386 dev->driver->preclose)
6c340eac 387 dev->driver->preclose(dev, file_priv);
1da177e4
LT
388
389 /* ========================================================
390 * Begin inline drm_release
391 */
392
b5e89ed5 393 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
ba25f9dc 394 task_pid_nr(current),
5817878c 395 (long)old_encode_dev(file_priv->minor->kdev->devt),
b5e89ed5
DA
396 dev->open_count);
397
fa538645 398 if (drm_core_check_feature(dev, DRIVER_LEGACY))
1a75a222 399 drm_legacy_lock_release(dev, filp);
1da177e4 400
67cb4b4d 401 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
a266162a 402 drm_legacy_reclaim_buffers(dev, file_priv);
67cb4b4d 403
c9a9c5e0
KH
404 drm_events_release(file_priv);
405
e2f5d2ea 406 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
ea39f835 407 drm_fb_release(file_priv);
e2f5d2ea
DS
408 drm_property_destroy_user_blobs(dev, file_priv);
409 }
ea39f835 410
e9083420
DA
411 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
412 drm_syncobj_release(file_priv);
413
1bcecfac 414 if (drm_core_check_feature(dev, DRIVER_GEM))
4e47e02d
P
415 drm_gem_release(dev, file_priv);
416
e7b96070 417 drm_legacy_ctxbitmap_flush(dev, file_priv);
1da177e4 418
14d71ebd
DV
419 if (drm_is_primary_client(file_priv))
420 drm_master_release(file_priv);
c996fd0b 421
22eae947 422 if (dev->driver->postclose)
6c340eac 423 dev->driver->postclose(dev, file_priv);
3248877e
DA
424
425 if (drm_core_check_feature(dev, DRIVER_PRIME))
426 drm_prime_destroy_file_private(&file_priv->prime);
427
ddde4371
VS
428 WARN_ON(!list_empty(&file_priv->event_list));
429
5fce5e0b 430 put_pid(file_priv->pid);
9a298b2a 431 kfree(file_priv);
1da177e4
LT
432
433 /* ========================================================
434 * End inline drm_release
435 */
436
b5e89ed5 437 if (!--dev->open_count) {
68dfbeba 438 drm_lastclose(dev);
c07dcd61 439 if (drm_dev_is_unplugged(dev))
2c07a21d 440 drm_put_dev(dev);
1da177e4 441 }
58374713 442 mutex_unlock(&drm_global_mutex);
1da177e4 443
1616c525
DH
444 drm_minor_release(minor);
445
68dfbeba 446 return 0;
1da177e4
LT
447}
448EXPORT_SYMBOL(drm_release);
449
bcb877e4
DV
450/**
451 * drm_read - read method for DRM file
452 * @filp: file pointer
453 * @buffer: userspace destination pointer for the read
454 * @count: count in bytes to read
455 * @offset: offset to read
456 *
b93658f8 457 * This function must be used by drivers as their &file_operations.read
bcb877e4
DV
458 * method iff they use DRM events for asynchronous signalling to userspace.
459 * Since events are used by the KMS API for vblank and page flip completion this
460 * means all modern display drivers must use it.
461 *
b93658f8
DV
462 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
463 * must set the &file_operation.llseek to no_llseek(). Polling support is
bcb877e4
DV
464 * provided by drm_poll().
465 *
466 * This function will only ever read a full event. Therefore userspace must
467 * supply a big enough buffer to fit any event to ensure forward progress. Since
468 * the maximum event space is currently 4K it's recommended to just use that for
469 * safety.
470 *
471 * RETURNS:
472 *
473 * Number of bytes read (always aligned to full events, and can be 0) or a
474 * negative error code on failure.
475 */
cdd1cf79
CW
476ssize_t drm_read(struct file *filp, char __user *buffer,
477 size_t count, loff_t *offset)
c9a9c5e0 478{
cdd1cf79 479 struct drm_file *file_priv = filp->private_data;
c9a9c5e0 480 struct drm_device *dev = file_priv->minor->dev;
9b2c0b7f 481 ssize_t ret;
c9a9c5e0 482
cdd1cf79
CW
483 if (!access_ok(VERIFY_WRITE, buffer, count))
484 return -EFAULT;
c9a9c5e0 485
9b2c0b7f
CW
486 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
487 if (ret)
488 return ret;
489
cdd1cf79 490 for (;;) {
83eb64c8
CW
491 struct drm_pending_event *e = NULL;
492
493 spin_lock_irq(&dev->event_lock);
494 if (!list_empty(&file_priv->event_list)) {
495 e = list_first_entry(&file_priv->event_list,
496 struct drm_pending_event, link);
497 file_priv->event_space += e->event->length;
498 list_del(&e->link);
499 }
500 spin_unlock_irq(&dev->event_lock);
501
502 if (e == NULL) {
cdd1cf79
CW
503 if (ret)
504 break;
c9a9c5e0 505
cdd1cf79
CW
506 if (filp->f_flags & O_NONBLOCK) {
507 ret = -EAGAIN;
508 break;
509 }
c9a9c5e0 510
9b2c0b7f 511 mutex_unlock(&file_priv->event_read_lock);
cdd1cf79
CW
512 ret = wait_event_interruptible(file_priv->event_wait,
513 !list_empty(&file_priv->event_list));
9b2c0b7f
CW
514 if (ret >= 0)
515 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
516 if (ret)
517 return ret;
cdd1cf79 518 } else {
83eb64c8
CW
519 unsigned length = e->event->length;
520
521 if (length > count - ret) {
522put_back_event:
523 spin_lock_irq(&dev->event_lock);
524 file_priv->event_space -= length;
525 list_add(&e->link, &file_priv->event_list);
526 spin_unlock_irq(&dev->event_lock);
cdd1cf79 527 break;
83eb64c8 528 }
cdd1cf79 529
83eb64c8 530 if (copy_to_user(buffer + ret, e->event, length)) {
cdd1cf79
CW
531 if (ret == 0)
532 ret = -EFAULT;
83eb64c8 533 goto put_back_event;
cdd1cf79 534 }
c9a9c5e0 535
83eb64c8 536 ret += length;
1b47aaf9 537 kfree(e);
c9a9c5e0 538 }
c9a9c5e0 539 }
9b2c0b7f 540 mutex_unlock(&file_priv->event_read_lock);
c9a9c5e0 541
cdd1cf79 542 return ret;
c9a9c5e0
KH
543}
544EXPORT_SYMBOL(drm_read);
545
bcb877e4
DV
546/**
547 * drm_poll - poll method for DRM file
548 * @filp: file pointer
549 * @wait: poll waiter table
550 *
b93658f8
DV
551 * This function must be used by drivers as their &file_operations.read method
552 * iff they use DRM events for asynchronous signalling to userspace. Since
553 * events are used by the KMS API for vblank and page flip completion this means
554 * all modern display drivers must use it.
bcb877e4
DV
555 *
556 * See also drm_read().
557 *
558 * RETURNS:
559 *
560 * Mask of POLL flags indicating the current status of the file.
561 */
1da177e4
LT
562unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
563{
c9a9c5e0
KH
564 struct drm_file *file_priv = filp->private_data;
565 unsigned int mask = 0;
566
567 poll_wait(filp, &file_priv->event_wait, wait);
568
569 if (!list_empty(&file_priv->event_list))
570 mask |= POLLIN | POLLRDNORM;
571
572 return mask;
1da177e4 573}
b5e89ed5 574EXPORT_SYMBOL(drm_poll);
2dd500f1
DV
575
576/**
4020b220 577 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
2dd500f1
DV
578 * @dev: DRM device
579 * @file_priv: DRM file private data
580 * @p: tracking structure for the pending event
581 * @e: actual event data to deliver to userspace
582 *
583 * This function prepares the passed in event for eventual delivery. If the event
584 * doesn't get delivered (because the IOCTL fails later on, before queuing up
585 * anything) then the even must be cancelled and freed using
fb740cf2
DV
586 * drm_event_cancel_free(). Successfully initialized events should be sent out
587 * using drm_send_event() or drm_send_event_locked() to signal completion of the
588 * asynchronous event to userspace.
2dd500f1
DV
589 *
590 * If callers embedded @p into a larger structure it must be allocated with
591 * kmalloc and @p must be the first member element.
592 *
4020b220 593 * This is the locked version of drm_event_reserve_init() for callers which
ef40cbf9 594 * already hold &drm_device.event_lock.
4020b220 595 *
2dd500f1
DV
596 * RETURNS:
597 *
598 * 0 on success or a negative error code on failure.
599 */
4020b220
DV
600int drm_event_reserve_init_locked(struct drm_device *dev,
601 struct drm_file *file_priv,
602 struct drm_pending_event *p,
603 struct drm_event *e)
2dd500f1 604{
4020b220
DV
605 if (file_priv->event_space < e->length)
606 return -ENOMEM;
2dd500f1
DV
607
608 file_priv->event_space -= e->length;
609
610 p->event = e;
681047b4 611 list_add(&p->pending_link, &file_priv->pending_event_list);
2dd500f1
DV
612 p->file_priv = file_priv;
613
4020b220
DV
614 return 0;
615}
616EXPORT_SYMBOL(drm_event_reserve_init_locked);
617
618/**
619 * drm_event_reserve_init - init a DRM event and reserve space for it
620 * @dev: DRM device
621 * @file_priv: DRM file private data
622 * @p: tracking structure for the pending event
623 * @e: actual event data to deliver to userspace
624 *
625 * This function prepares the passed in event for eventual delivery. If the event
626 * doesn't get delivered (because the IOCTL fails later on, before queuing up
627 * anything) then the even must be cancelled and freed using
628 * drm_event_cancel_free(). Successfully initialized events should be sent out
629 * using drm_send_event() or drm_send_event_locked() to signal completion of the
630 * asynchronous event to userspace.
631 *
632 * If callers embedded @p into a larger structure it must be allocated with
633 * kmalloc and @p must be the first member element.
634 *
ef40cbf9 635 * Callers which already hold &drm_device.event_lock should use
20c9ca4f 636 * drm_event_reserve_init_locked() instead.
4020b220
DV
637 *
638 * RETURNS:
639 *
640 * 0 on success or a negative error code on failure.
641 */
642int drm_event_reserve_init(struct drm_device *dev,
643 struct drm_file *file_priv,
644 struct drm_pending_event *p,
645 struct drm_event *e)
646{
647 unsigned long flags;
648 int ret;
649
650 spin_lock_irqsave(&dev->event_lock, flags);
651 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
2dd500f1 652 spin_unlock_irqrestore(&dev->event_lock, flags);
4020b220 653
2dd500f1
DV
654 return ret;
655}
656EXPORT_SYMBOL(drm_event_reserve_init);
657
658/**
659 * drm_event_cancel_free - free a DRM event and release it's space
660 * @dev: DRM device
661 * @p: tracking structure for the pending event
662 *
663 * This function frees the event @p initialized with drm_event_reserve_init()
b93658f8
DV
664 * and releases any allocated space. It is used to cancel an event when the
665 * nonblocking operation could not be submitted and needed to be aborted.
2dd500f1
DV
666 */
667void drm_event_cancel_free(struct drm_device *dev,
668 struct drm_pending_event *p)
669{
670 unsigned long flags;
671 spin_lock_irqsave(&dev->event_lock, flags);
681047b4
DV
672 if (p->file_priv) {
673 p->file_priv->event_space += p->event->length;
674 list_del(&p->pending_link);
675 }
2dd500f1 676 spin_unlock_irqrestore(&dev->event_lock, flags);
838de39f
GP
677
678 if (p->fence)
f54d1867 679 dma_fence_put(p->fence);
838de39f 680
1b47aaf9 681 kfree(p);
2dd500f1
DV
682}
683EXPORT_SYMBOL(drm_event_cancel_free);
fb740cf2
DV
684
685/**
686 * drm_send_event_locked - send DRM event to file descriptor
687 * @dev: DRM device
688 * @e: DRM event to deliver
689 *
690 * This function sends the event @e, initialized with drm_event_reserve_init(),
691 * to its associated userspace DRM file. Callers must already hold
ef40cbf9 692 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
681047b4
DV
693 *
694 * Note that the core will take care of unlinking and disarming events when the
695 * corresponding DRM file is closed. Drivers need not worry about whether the
696 * DRM file for this event still exists and can call this function upon
697 * completion of the asynchronous work unconditionally.
fb740cf2
DV
698 */
699void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
700{
701 assert_spin_locked(&dev->event_lock);
702
3b24f7d6 703 if (e->completion) {
3b24f7d6 704 complete_all(e->completion);
24835e44 705 e->completion_release(e->completion);
3b24f7d6
DV
706 e->completion = NULL;
707 }
708
1b47aaf9 709 if (e->fence) {
f54d1867
CW
710 dma_fence_signal(e->fence);
711 dma_fence_put(e->fence);
1b47aaf9
GP
712 }
713
681047b4 714 if (!e->file_priv) {
1b47aaf9 715 kfree(e);
681047b4
DV
716 return;
717 }
718
719 list_del(&e->pending_link);
fb740cf2
DV
720 list_add_tail(&e->link,
721 &e->file_priv->event_list);
722 wake_up_interruptible(&e->file_priv->event_wait);
723}
724EXPORT_SYMBOL(drm_send_event_locked);
725
726/**
727 * drm_send_event - send DRM event to file descriptor
728 * @dev: DRM device
729 * @e: DRM event to deliver
730 *
731 * This function sends the event @e, initialized with drm_event_reserve_init(),
ef40cbf9
DV
732 * to its associated userspace DRM file. This function acquires
733 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
734 * hold this lock.
681047b4
DV
735 *
736 * Note that the core will take care of unlinking and disarming events when the
737 * corresponding DRM file is closed. Drivers need not worry about whether the
738 * DRM file for this event still exists and can call this function upon
739 * completion of the asynchronous work unconditionally.
fb740cf2
DV
740 */
741void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
742{
743 unsigned long irqflags;
744
745 spin_lock_irqsave(&dev->event_lock, irqflags);
746 drm_send_event_locked(dev, e);
747 spin_unlock_irqrestore(&dev->event_lock, irqflags);
748}
749EXPORT_SYMBOL(drm_send_event);