1 /*
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
34 #include <linux/dma-fence.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/poll.h>
38 #include <linux/slab.h>
39
40 #include <drm/drm_client.h>
41 #include <drm/drm_drv.h>
42 #include <drm/drm_file.h>
43 #include <drm/drm_print.h>
44
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
47 #include "drm_legacy.h"
48
49 /* from BKL pushdown */
50 DEFINE_MUTEX(drm_global_mutex);
51
52 #define MAX_DRM_OPEN_COUNT 128
53
54 /**
55 * DOC: file operations
56 *
57 * Drivers must define the file operations structure that forms the DRM
58 * userspace API entry point, even though most of those operations are
59 * implemented in the DRM core. The resulting &struct file_operations must be
60 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
61 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
62 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
63 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
64 * that require 32/64 bit compatibility support must provide their own
65 * &file_operations.compat_ioctl handler that processes private ioctls and calls
66 * drm_compat_ioctl() for core ioctls.
67 *
68 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
69 * events are a generic and extensible means to send asynchronous events to
70 * userspace through the file descriptor. They are used to send vblank event and
71 * page flip completions by the KMS API. But drivers can also use it for their
72 * own needs, e.g. to signal completion of rendering.
73 *
74 * For the driver-side event interface see drm_event_reserve_init() and
75 * drm_send_event() as the main starting points.
76 *
77 * The memory mapping implementation will vary depending on how the driver
78 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
79 * function, modern drivers should use one of the provided memory-manager
80 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
81 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
82 *
83 * No other file operations are supported by the DRM userspace API. Overall the
84 * following is an example &file_operations structure::
85 *
86 * static const example_drm_fops = {
87 * .owner = THIS_MODULE,
88 * .open = drm_open,
89 * .release = drm_release,
90 * .unlocked_ioctl = drm_ioctl,
91 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
92 * .poll = drm_poll,
93 * .read = drm_read,
94 * .llseek = no_llseek,
95 * .mmap = drm_gem_mmap,
96 * };
97 *
98 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
99 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
100 * simpler.
101 *
102 * The driver's &file_operations must be stored in &drm_driver.fops.
103 *
104 * For driver-private IOCTL handling see the more detailed discussion in
105 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
106 */
107
108 /**
109 * drm_file_alloc - allocate file context
110 * @minor: minor to allocate on
111 *
112 * This allocates a new DRM file context. It is not linked into any context and
113 * can be used by the caller freely. Note that the context keeps a pointer to
114 * @minor, so it must be freed before @minor is.
115 *
116 * RETURNS:
117 * Pointer to newly allocated context, ERR_PTR on failure.
118 */
drm_file_alloc(struct drm_minor * minor)119 struct drm_file *drm_file_alloc(struct drm_minor *minor)
120 {
121 struct drm_device *dev = minor->dev;
122 struct drm_file *file;
123 int ret;
124
125 file = kzalloc(sizeof(*file), GFP_KERNEL);
126 if (!file)
127 return ERR_PTR(-ENOMEM);
128
129 file->pid = get_pid(task_pid(current));
130 file->minor = minor;
131
132 /* for compatibility root is always authenticated */
133 file->authenticated = capable(CAP_SYS_ADMIN);
134
135 INIT_LIST_HEAD(&file->lhead);
136 INIT_LIST_HEAD(&file->fbs);
137 mutex_init(&file->fbs_lock);
138 INIT_LIST_HEAD(&file->blobs);
139 INIT_LIST_HEAD(&file->pending_event_list);
140 INIT_LIST_HEAD(&file->event_list);
141 init_waitqueue_head(&file->event_wait);
142 file->event_space = 4096; /* set aside 4k for event buffer */
143
144 mutex_init(&file->event_read_lock);
145
146 if (drm_core_check_feature(dev, DRIVER_GEM))
147 drm_gem_open(dev, file);
148
149 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
150 drm_syncobj_open(file);
151
152 drm_prime_init_file_private(&file->prime);
153
154 if (dev->driver->open) {
155 ret = dev->driver->open(dev, file);
156 if (ret < 0)
157 goto out_prime_destroy;
158 }
159
160 return file;
161
162 out_prime_destroy:
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
drm_events_release(struct drm_file * file_priv)174 static 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 */
drm_file_free(struct drm_file * file)211 void 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 drm_prime_destroy_file_private(&file->prime);
257
258 WARN_ON(!list_empty(&file->event_list));
259
260 put_pid(file->pid);
261 kfree(file);
262 }
263
drm_close_helper(struct file * filp)264 static void drm_close_helper(struct file *filp)
265 {
266 struct drm_file *file_priv = filp->private_data;
267 struct drm_device *dev = file_priv->minor->dev;
268
269 mutex_lock(&dev->filelist_mutex);
270 list_del(&file_priv->lhead);
271 mutex_unlock(&dev->filelist_mutex);
272
273 drm_file_free(file_priv);
274 }
275
276 /*
277 * Check whether DRI will run on this CPU.
278 *
279 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
280 */
drm_cpu_valid(void)281 static int drm_cpu_valid(void)
282 {
283 #if defined(__sparc__) && !defined(__sparc_v9__)
284 return 0; /* No cmpxchg before v9 sparc. */
285 #endif
286 return 1;
287 }
288
289 /*
290 * Called whenever a process opens /dev/drm.
291 *
292 * \param filp file pointer.
293 * \param minor acquired minor-object.
294 * \return zero on success or a negative number on failure.
295 *
296 * Creates and initializes a drm_file structure for the file private data in \p
297 * filp and add it into the double linked list in \p dev.
298 */
drm_open_helper(struct file * filp,struct drm_minor * minor)299 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
300 {
301 struct drm_device *dev = minor->dev;
302 struct drm_file *priv;
303 int ret;
304
305 if (filp->f_flags & O_EXCL)
306 return -EBUSY; /* No exclusive opens */
307 if (!drm_cpu_valid())
308 return -EINVAL;
309 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
310 return -EINVAL;
311
312 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
313
314 priv = drm_file_alloc(minor);
315 if (IS_ERR(priv))
316 return PTR_ERR(priv);
317
318 if (drm_is_primary_client(priv)) {
319 ret = drm_master_open(priv);
320 if (ret) {
321 drm_file_free(priv);
322 return ret;
323 }
324 }
325
326 filp->private_data = priv;
327 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
328 priv->filp = filp;
329
330 mutex_lock(&dev->filelist_mutex);
331 list_add(&priv->lhead, &dev->filelist);
332 mutex_unlock(&dev->filelist_mutex);
333
334 #ifdef __alpha__
335 /*
336 * Default the hose
337 */
338 if (!dev->hose) {
339 struct pci_dev *pci_dev;
340 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
341 if (pci_dev) {
342 dev->hose = pci_dev->sysdata;
343 pci_dev_put(pci_dev);
344 }
345 if (!dev->hose) {
346 struct pci_bus *b = list_entry(pci_root_buses.next,
347 struct pci_bus, node);
348 if (b)
349 dev->hose = b->sysdata;
350 }
351 }
352 #endif
353
354 return 0;
355 }
356
357 /**
358 * drm_open - open method for DRM file
359 * @inode: device inode
360 * @filp: file pointer.
361 *
362 * This function must be used by drivers as their &file_operations.open method.
363 * It looks up the correct DRM device and instantiates all the per-file
364 * resources for it. It also calls the &drm_driver.open driver callback.
365 *
366 * RETURNS:
367 *
368 * 0 on success or negative errno value on falure.
369 */
drm_open(struct inode * inode,struct file * filp)370 int drm_open(struct inode *inode, struct file *filp)
371 {
372 struct drm_device *dev;
373 struct drm_minor *minor;
374 int retcode;
375 int need_setup = 0;
376
377 minor = drm_minor_acquire(iminor(inode));
378 if (IS_ERR(minor))
379 return PTR_ERR(minor);
380
381 dev = minor->dev;
382 if (!dev->open_count++)
383 need_setup = 1;
384
385 if (dev->open_count >= MAX_DRM_OPEN_COUNT) {
386 retcode = -EPERM;
387 goto err_undo;
388 }
389
390 /* share address_space across all char-devs of a single device */
391 filp->f_mapping = dev->anon_inode->i_mapping;
392
393 retcode = drm_open_helper(filp, minor);
394 if (retcode)
395 goto err_undo;
396 if (need_setup) {
397 retcode = drm_legacy_setup(dev);
398 if (retcode) {
399 drm_close_helper(filp);
400 goto err_undo;
401 }
402 }
403 return 0;
404
405 err_undo:
406 dev->open_count--;
407 drm_minor_release(minor);
408 return retcode;
409 }
410 EXPORT_SYMBOL(drm_open);
411
drm_lastclose(struct drm_device * dev)412 void drm_lastclose(struct drm_device * dev)
413 {
414 DRM_DEBUG("\n");
415
416 if (dev->driver->lastclose)
417 dev->driver->lastclose(dev);
418 DRM_DEBUG("driver lastclose completed\n");
419
420 if (drm_core_check_feature(dev, DRIVER_LEGACY))
421 drm_legacy_dev_reinit(dev);
422
423 drm_client_dev_restore(dev);
424 }
425
426 /**
427 * drm_release - release method for DRM file
428 * @inode: device inode
429 * @filp: file pointer.
430 *
431 * This function must be used by drivers as their &file_operations.release
432 * method. It frees any resources associated with the open file, and calls the
433 * &drm_driver.postclose driver callback. If this is the last open file for the
434 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
435 *
436 * RETURNS:
437 *
438 * Always succeeds and returns 0.
439 */
drm_release(struct inode * inode,struct file * filp)440 int drm_release(struct inode *inode, struct file *filp)
441 {
442 struct drm_file *file_priv = filp->private_data;
443 struct drm_minor *minor = file_priv->minor;
444 struct drm_device *dev = minor->dev;
445
446 mutex_lock(&drm_global_mutex);
447
448 DRM_DEBUG("open_count = %d\n", dev->open_count);
449
450 drm_close_helper(filp);
451
452 if (!--dev->open_count)
453 drm_lastclose(dev);
454
455 mutex_unlock(&drm_global_mutex);
456
457 drm_minor_release(minor);
458
459 return 0;
460 }
461 EXPORT_SYMBOL(drm_release);
462
463 /**
464 * drm_read - read method for DRM file
465 * @filp: file pointer
466 * @buffer: userspace destination pointer for the read
467 * @count: count in bytes to read
468 * @offset: offset to read
469 *
470 * This function must be used by drivers as their &file_operations.read
471 * method iff they use DRM events for asynchronous signalling to userspace.
472 * Since events are used by the KMS API for vblank and page flip completion this
473 * means all modern display drivers must use it.
474 *
475 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
476 * must set the &file_operation.llseek to no_llseek(). Polling support is
477 * provided by drm_poll().
478 *
479 * This function will only ever read a full event. Therefore userspace must
480 * supply a big enough buffer to fit any event to ensure forward progress. Since
481 * the maximum event space is currently 4K it's recommended to just use that for
482 * safety.
483 *
484 * RETURNS:
485 *
486 * Number of bytes read (always aligned to full events, and can be 0) or a
487 * negative error code on failure.
488 */
drm_read(struct file * filp,char __user * buffer,size_t count,loff_t * offset)489 ssize_t drm_read(struct file *filp, char __user *buffer,
490 size_t count, loff_t *offset)
491 {
492 struct drm_file *file_priv = filp->private_data;
493 struct drm_device *dev = file_priv->minor->dev;
494 ssize_t ret;
495
496 if (!access_ok(buffer, count))
497 return -EFAULT;
498
499 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
500 if (ret)
501 return ret;
502
503 for (;;) {
504 struct drm_pending_event *e = NULL;
505
506 spin_lock_irq(&dev->event_lock);
507 if (!list_empty(&file_priv->event_list)) {
508 e = list_first_entry(&file_priv->event_list,
509 struct drm_pending_event, link);
510 file_priv->event_space += e->event->length;
511 list_del(&e->link);
512 }
513 spin_unlock_irq(&dev->event_lock);
514
515 if (e == NULL) {
516 if (ret)
517 break;
518
519 if (filp->f_flags & O_NONBLOCK) {
520 ret = -EAGAIN;
521 break;
522 }
523
524 mutex_unlock(&file_priv->event_read_lock);
525 ret = wait_event_interruptible(file_priv->event_wait,
526 !list_empty(&file_priv->event_list));
527 if (ret >= 0)
528 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
529 if (ret)
530 return ret;
531 } else {
532 unsigned length = e->event->length;
533
534 if (length > count - ret) {
535 put_back_event:
536 spin_lock_irq(&dev->event_lock);
537 file_priv->event_space -= length;
538 list_add(&e->link, &file_priv->event_list);
539 spin_unlock_irq(&dev->event_lock);
540 wake_up_interruptible(&file_priv->event_wait);
541 break;
542 }
543
544 if (copy_to_user(buffer + ret, e->event, length)) {
545 if (ret == 0)
546 ret = -EFAULT;
547 goto put_back_event;
548 }
549
550 ret += length;
551 kfree(e);
552 }
553 }
554 mutex_unlock(&file_priv->event_read_lock);
555
556 return ret;
557 }
558 EXPORT_SYMBOL(drm_read);
559
560 /**
561 * drm_poll - poll method for DRM file
562 * @filp: file pointer
563 * @wait: poll waiter table
564 *
565 * This function must be used by drivers as their &file_operations.read method
566 * iff they use DRM events for asynchronous signalling to userspace. Since
567 * events are used by the KMS API for vblank and page flip completion this means
568 * all modern display drivers must use it.
569 *
570 * See also drm_read().
571 *
572 * RETURNS:
573 *
574 * Mask of POLL flags indicating the current status of the file.
575 */
drm_poll(struct file * filp,struct poll_table_struct * wait)576 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
577 {
578 struct drm_file *file_priv = filp->private_data;
579 __poll_t mask = 0;
580
581 poll_wait(filp, &file_priv->event_wait, wait);
582
583 if (!list_empty(&file_priv->event_list))
584 mask |= EPOLLIN | EPOLLRDNORM;
585
586 return mask;
587 }
588 EXPORT_SYMBOL(drm_poll);
589
590 /**
591 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
592 * @dev: DRM device
593 * @file_priv: DRM file private data
594 * @p: tracking structure for the pending event
595 * @e: actual event data to deliver to userspace
596 *
597 * This function prepares the passed in event for eventual delivery. If the event
598 * doesn't get delivered (because the IOCTL fails later on, before queuing up
599 * anything) then the even must be cancelled and freed using
600 * drm_event_cancel_free(). Successfully initialized events should be sent out
601 * using drm_send_event() or drm_send_event_locked() to signal completion of the
602 * asynchronous event to userspace.
603 *
604 * If callers embedded @p into a larger structure it must be allocated with
605 * kmalloc and @p must be the first member element.
606 *
607 * This is the locked version of drm_event_reserve_init() for callers which
608 * already hold &drm_device.event_lock.
609 *
610 * RETURNS:
611 *
612 * 0 on success or a negative error code on failure.
613 */
drm_event_reserve_init_locked(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)614 int drm_event_reserve_init_locked(struct drm_device *dev,
615 struct drm_file *file_priv,
616 struct drm_pending_event *p,
617 struct drm_event *e)
618 {
619 if (file_priv->event_space < e->length)
620 return -ENOMEM;
621
622 file_priv->event_space -= e->length;
623
624 p->event = e;
625 list_add(&p->pending_link, &file_priv->pending_event_list);
626 p->file_priv = file_priv;
627
628 return 0;
629 }
630 EXPORT_SYMBOL(drm_event_reserve_init_locked);
631
632 /**
633 * drm_event_reserve_init - init a DRM event and reserve space for it
634 * @dev: DRM device
635 * @file_priv: DRM file private data
636 * @p: tracking structure for the pending event
637 * @e: actual event data to deliver to userspace
638 *
639 * This function prepares the passed in event for eventual delivery. If the event
640 * doesn't get delivered (because the IOCTL fails later on, before queuing up
641 * anything) then the even must be cancelled and freed using
642 * drm_event_cancel_free(). Successfully initialized events should be sent out
643 * using drm_send_event() or drm_send_event_locked() to signal completion of the
644 * asynchronous event to userspace.
645 *
646 * If callers embedded @p into a larger structure it must be allocated with
647 * kmalloc and @p must be the first member element.
648 *
649 * Callers which already hold &drm_device.event_lock should use
650 * drm_event_reserve_init_locked() instead.
651 *
652 * RETURNS:
653 *
654 * 0 on success or a negative error code on failure.
655 */
drm_event_reserve_init(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)656 int drm_event_reserve_init(struct drm_device *dev,
657 struct drm_file *file_priv,
658 struct drm_pending_event *p,
659 struct drm_event *e)
660 {
661 unsigned long flags;
662 int ret;
663
664 spin_lock_irqsave(&dev->event_lock, flags);
665 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
666 spin_unlock_irqrestore(&dev->event_lock, flags);
667
668 return ret;
669 }
670 EXPORT_SYMBOL(drm_event_reserve_init);
671
672 /**
673 * drm_event_cancel_free - free a DRM event and release its space
674 * @dev: DRM device
675 * @p: tracking structure for the pending event
676 *
677 * This function frees the event @p initialized with drm_event_reserve_init()
678 * and releases any allocated space. It is used to cancel an event when the
679 * nonblocking operation could not be submitted and needed to be aborted.
680 */
drm_event_cancel_free(struct drm_device * dev,struct drm_pending_event * p)681 void drm_event_cancel_free(struct drm_device *dev,
682 struct drm_pending_event *p)
683 {
684 unsigned long flags;
685 spin_lock_irqsave(&dev->event_lock, flags);
686 if (p->file_priv) {
687 p->file_priv->event_space += p->event->length;
688 list_del(&p->pending_link);
689 }
690 spin_unlock_irqrestore(&dev->event_lock, flags);
691
692 if (p->fence)
693 dma_fence_put(p->fence);
694
695 kfree(p);
696 }
697 EXPORT_SYMBOL(drm_event_cancel_free);
698
699 /**
700 * drm_send_event_locked - send DRM event to file descriptor
701 * @dev: DRM device
702 * @e: DRM event to deliver
703 *
704 * This function sends the event @e, initialized with drm_event_reserve_init(),
705 * to its associated userspace DRM file. Callers must already hold
706 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
707 *
708 * Note that the core will take care of unlinking and disarming events when the
709 * corresponding DRM file is closed. Drivers need not worry about whether the
710 * DRM file for this event still exists and can call this function upon
711 * completion of the asynchronous work unconditionally.
712 */
drm_send_event_locked(struct drm_device * dev,struct drm_pending_event * e)713 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
714 {
715 assert_spin_locked(&dev->event_lock);
716
717 if (e->completion) {
718 complete_all(e->completion);
719 e->completion_release(e->completion);
720 e->completion = NULL;
721 }
722
723 if (e->fence) {
724 dma_fence_signal(e->fence);
725 dma_fence_put(e->fence);
726 }
727
728 if (!e->file_priv) {
729 kfree(e);
730 return;
731 }
732
733 list_del(&e->pending_link);
734 list_add_tail(&e->link,
735 &e->file_priv->event_list);
736 wake_up_interruptible(&e->file_priv->event_wait);
737 }
738 EXPORT_SYMBOL(drm_send_event_locked);
739
740 /**
741 * drm_send_event - send DRM event to file descriptor
742 * @dev: DRM device
743 * @e: DRM event to deliver
744 *
745 * This function sends the event @e, initialized with drm_event_reserve_init(),
746 * to its associated userspace DRM file. This function acquires
747 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
748 * hold this lock.
749 *
750 * Note that the core will take care of unlinking and disarming events when the
751 * corresponding DRM file is closed. Drivers need not worry about whether the
752 * DRM file for this event still exists and can call this function upon
753 * completion of the asynchronous work unconditionally.
754 */
drm_send_event(struct drm_device * dev,struct drm_pending_event * e)755 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
756 {
757 unsigned long irqflags;
758
759 spin_lock_irqsave(&dev->event_lock, irqflags);
760 drm_send_event_locked(dev, e);
761 spin_unlock_irqrestore(&dev->event_lock, irqflags);
762 }
763 EXPORT_SYMBOL(drm_send_event);
764