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