Merge tag 'for-linus-4.14c-rc7-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / drm_file.c
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/poll.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37
38 #include <drm/drm_file.h>
39 #include <drm/drmP.h>
40
41 #include "drm_legacy.h"
42 #include "drm_internal.h"
43 #include "drm_crtc_internal.h"
44
45 /* from BKL pushdown */
46 DEFINE_MUTEX(drm_global_mutex);
47
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
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(),
55  * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
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.
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  *
68  * For the driver-side event interface see drm_event_reserve_init() and
69  * drm_send_event() as the main starting points.
70  *
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
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().
76  *
77  * No other file operations are supported by the DRM userspace API. Overall the
78  * following is an example &file_operations structure::
79  *
80  *     static const example_drm_fops = {
81  *             .owner = THIS_MODULE,
82  *             .open = drm_open,
83  *             .release = drm_release,
84  *             .unlocked_ioctl = drm_ioctl,
85  *             .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
86  *             .poll = drm_poll,
87  *             .read = drm_read,
88  *             .llseek = no_llseek,
89  *             .mmap = drm_gem_mmap,
90  *     };
91  *
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.
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>`.
100  */
101
102 static int drm_open_helper(struct file *filp, struct drm_minor *minor);
103
104 static int drm_setup(struct drm_device * dev)
105 {
106         int ret;
107
108         if (dev->driver->firstopen &&
109             drm_core_check_feature(dev, DRIVER_LEGACY)) {
110                 ret = dev->driver->firstopen(dev);
111                 if (ret != 0)
112                         return ret;
113         }
114
115         ret = drm_legacy_dma_setup(dev);
116         if (ret < 0)
117                 return ret;
118
119
120         DRM_DEBUG("\n");
121         return 0;
122 }
123
124 /**
125  * drm_open - open method for DRM file
126  * @inode: device inode
127  * @filp: file pointer.
128  *
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.
132  *
133  * RETURNS:
134  *
135  * 0 on success or negative errno value on falure.
136  */
137 int drm_open(struct inode *inode, struct file *filp)
138 {
139         struct drm_device *dev;
140         struct drm_minor *minor;
141         int retcode;
142         int need_setup = 0;
143
144         minor = drm_minor_acquire(iminor(inode));
145         if (IS_ERR(minor))
146                 return PTR_ERR(minor);
147
148         dev = minor->dev;
149         if (!dev->open_count++)
150                 need_setup = 1;
151
152         /* share address_space across all char-devs of a single device */
153         filp->f_mapping = dev->anon_inode->i_mapping;
154
155         retcode = drm_open_helper(filp, minor);
156         if (retcode)
157                 goto err_undo;
158         if (need_setup) {
159                 retcode = drm_setup(dev);
160                 if (retcode)
161                         goto err_undo;
162         }
163         return 0;
164
165 err_undo:
166         dev->open_count--;
167         drm_minor_release(minor);
168         return retcode;
169 }
170 EXPORT_SYMBOL(drm_open);
171
172 /*
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  */
177 static int drm_cpu_valid(void)
178 {
179 #if defined(__sparc__) && !defined(__sparc_v9__)
180         return 0;               /* No cmpxchg before v9 sparc. */
181 #endif
182         return 1;
183 }
184
185 /*
186  * Called whenever a process opens /dev/drm.
187  *
188  * \param filp file pointer.
189  * \param minor acquired minor-object.
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  */
195 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
196 {
197         struct drm_device *dev = minor->dev;
198         struct drm_file *priv;
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;
205         if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
206                 return -EINVAL;
207
208         DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
209
210         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
211         if (!priv)
212                 return -ENOMEM;
213
214         filp->private_data = priv;
215         priv->filp = filp;
216         priv->pid = get_pid(task_pid(current));
217         priv->minor = minor;
218
219         /* for compatibility root is always authenticated */
220         priv->authenticated = capable(CAP_SYS_ADMIN);
221         priv->lock_count = 0;
222
223         INIT_LIST_HEAD(&priv->lhead);
224         INIT_LIST_HEAD(&priv->fbs);
225         mutex_init(&priv->fbs_lock);
226         INIT_LIST_HEAD(&priv->blobs);
227         INIT_LIST_HEAD(&priv->pending_event_list);
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 */
231
232         mutex_init(&priv->event_read_lock);
233
234         if (drm_core_check_feature(dev, DRIVER_GEM))
235                 drm_gem_open(dev, priv);
236
237         if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
238                 drm_syncobj_open(priv);
239
240         if (drm_core_check_feature(dev, DRIVER_PRIME))
241                 drm_prime_init_file_private(&priv->prime);
242
243         if (dev->driver->open) {
244                 ret = dev->driver->open(dev, priv);
245                 if (ret < 0)
246                         goto out_prime_destroy;
247         }
248
249         if (drm_is_primary_client(priv)) {
250                 ret = drm_master_open(priv);
251                 if (ret)
252                         goto out_close;
253         }
254
255         mutex_lock(&dev->filelist_mutex);
256         list_add(&priv->lhead, &dev->filelist);
257         mutex_unlock(&dev->filelist_mutex);
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) {
271                         struct pci_bus *b = list_entry(pci_root_buses.next,
272                                 struct pci_bus, node);
273                         if (b)
274                                 dev->hose = b->sysdata;
275                 }
276         }
277 #endif
278
279         return 0;
280
281 out_close:
282         if (dev->driver->postclose)
283                 dev->driver->postclose(dev, priv);
284 out_prime_destroy:
285         if (drm_core_check_feature(dev, DRIVER_PRIME))
286                 drm_prime_destroy_file_private(&priv->prime);
287         if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
288                 drm_syncobj_release(priv);
289         if (drm_core_check_feature(dev, DRIVER_GEM))
290                 drm_gem_release(dev, priv);
291         put_pid(priv->pid);
292         kfree(priv);
293         filp->private_data = NULL;
294         return ret;
295 }
296
297 static 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;
301         unsigned long flags;
302
303         spin_lock_irqsave(&dev->event_lock, flags);
304
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
312         /* Remove unconsumed events */
313         list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
314                 list_del(&e->link);
315                 kfree(e);
316         }
317
318         spin_unlock_irqrestore(&dev->event_lock, flags);
319 }
320
321 static void drm_legacy_dev_reinit(struct drm_device *dev)
322 {
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);
335
336         dev->sigdata.lock = NULL;
337
338         dev->context_flag = 0;
339         dev->last_context = 0;
340         dev->if_version = 0;
341
342         DRM_DEBUG("lastclose completed\n");
343 }
344
345 void drm_lastclose(struct drm_device * dev)
346 {
347         DRM_DEBUG("\n");
348
349         if (dev->driver->lastclose)
350                 dev->driver->lastclose(dev);
351         DRM_DEBUG("driver lastclose completed\n");
352
353         if (drm_core_check_feature(dev, DRIVER_LEGACY))
354                 drm_legacy_dev_reinit(dev);
355 }
356
357 /**
358  * drm_release - release 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.release
363  * method. It frees any resources associated with the open file, and calls the
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.
366  *
367  * RETURNS:
368  *
369  * Always succeeds and returns 0.
370  */
371 int drm_release(struct inode *inode, struct file *filp)
372 {
373         struct drm_file *file_priv = filp->private_data;
374         struct drm_minor *minor = file_priv->minor;
375         struct drm_device *dev = minor->dev;
376
377         mutex_lock(&drm_global_mutex);
378
379         DRM_DEBUG("open_count = %d\n", dev->open_count);
380
381         mutex_lock(&dev->filelist_mutex);
382         list_del(&file_priv->lhead);
383         mutex_unlock(&dev->filelist_mutex);
384
385         if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
386             dev->driver->preclose)
387                 dev->driver->preclose(dev, file_priv);
388
389         /* ========================================================
390          * Begin inline drm_release
391          */
392
393         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
394                   task_pid_nr(current),
395                   (long)old_encode_dev(file_priv->minor->kdev->devt),
396                   dev->open_count);
397
398         if (drm_core_check_feature(dev, DRIVER_LEGACY))
399                 drm_legacy_lock_release(dev, filp);
400
401         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
402                 drm_legacy_reclaim_buffers(dev, file_priv);
403
404         drm_events_release(file_priv);
405
406         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
407                 drm_fb_release(file_priv);
408                 drm_property_destroy_user_blobs(dev, file_priv);
409         }
410
411         if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
412                 drm_syncobj_release(file_priv);
413
414         if (drm_core_check_feature(dev, DRIVER_GEM))
415                 drm_gem_release(dev, file_priv);
416
417         drm_legacy_ctxbitmap_flush(dev, file_priv);
418
419         if (drm_is_primary_client(file_priv))
420                 drm_master_release(file_priv);
421
422         if (dev->driver->postclose)
423                 dev->driver->postclose(dev, file_priv);
424
425         if (drm_core_check_feature(dev, DRIVER_PRIME))
426                 drm_prime_destroy_file_private(&file_priv->prime);
427
428         WARN_ON(!list_empty(&file_priv->event_list));
429
430         put_pid(file_priv->pid);
431         kfree(file_priv);
432
433         /* ========================================================
434          * End inline drm_release
435          */
436
437         if (!--dev->open_count) {
438                 drm_lastclose(dev);
439                 if (drm_dev_is_unplugged(dev))
440                         drm_put_dev(dev);
441         }
442         mutex_unlock(&drm_global_mutex);
443
444         drm_minor_release(minor);
445
446         return 0;
447 }
448 EXPORT_SYMBOL(drm_release);
449
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  *
457  * This function must be used by drivers as their &file_operations.read
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  *
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
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  */
476 ssize_t drm_read(struct file *filp, char __user *buffer,
477                  size_t count, loff_t *offset)
478 {
479         struct drm_file *file_priv = filp->private_data;
480         struct drm_device *dev = file_priv->minor->dev;
481         ssize_t ret;
482
483         if (!access_ok(VERIFY_WRITE, buffer, count))
484                 return -EFAULT;
485
486         ret = mutex_lock_interruptible(&file_priv->event_read_lock);
487         if (ret)
488                 return ret;
489
490         for (;;) {
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) {
503                         if (ret)
504                                 break;
505
506                         if (filp->f_flags & O_NONBLOCK) {
507                                 ret = -EAGAIN;
508                                 break;
509                         }
510
511                         mutex_unlock(&file_priv->event_read_lock);
512                         ret = wait_event_interruptible(file_priv->event_wait,
513                                                        !list_empty(&file_priv->event_list));
514                         if (ret >= 0)
515                                 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
516                         if (ret)
517                                 return ret;
518                 } else {
519                         unsigned length = e->event->length;
520
521                         if (length > count - ret) {
522 put_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);
527                                 break;
528                         }
529
530                         if (copy_to_user(buffer + ret, e->event, length)) {
531                                 if (ret == 0)
532                                         ret = -EFAULT;
533                                 goto put_back_event;
534                         }
535
536                         ret += length;
537                         kfree(e);
538                 }
539         }
540         mutex_unlock(&file_priv->event_read_lock);
541
542         return ret;
543 }
544 EXPORT_SYMBOL(drm_read);
545
546 /**
547  * drm_poll - poll method for DRM file
548  * @filp: file pointer
549  * @wait: poll waiter table
550  *
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.
555  *
556  * See also drm_read().
557  *
558  * RETURNS:
559  *
560  * Mask of POLL flags indicating the current status of the file.
561  */
562 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
563 {
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;
573 }
574 EXPORT_SYMBOL(drm_poll);
575
576 /**
577  * drm_event_reserve_init_locked - init a DRM event and reserve space for it
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
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.
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  *
593  * This is the locked version of drm_event_reserve_init() for callers which
594  * already hold &drm_device.event_lock.
595  *
596  * RETURNS:
597  *
598  * 0 on success or a negative error code on failure.
599  */
600 int 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)
604 {
605         if (file_priv->event_space < e->length)
606                 return -ENOMEM;
607
608         file_priv->event_space -= e->length;
609
610         p->event = e;
611         list_add(&p->pending_link, &file_priv->pending_event_list);
612         p->file_priv = file_priv;
613
614         return 0;
615 }
616 EXPORT_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  *
635  * Callers which already hold &drm_device.event_lock should use
636  * drm_event_reserve_init_locked() instead.
637  *
638  * RETURNS:
639  *
640  * 0 on success or a negative error code on failure.
641  */
642 int 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);
652         spin_unlock_irqrestore(&dev->event_lock, flags);
653
654         return ret;
655 }
656 EXPORT_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()
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.
666  */
667 void 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);
672         if (p->file_priv) {
673                 p->file_priv->event_space += p->event->length;
674                 list_del(&p->pending_link);
675         }
676         spin_unlock_irqrestore(&dev->event_lock, flags);
677
678         if (p->fence)
679                 dma_fence_put(p->fence);
680
681         kfree(p);
682 }
683 EXPORT_SYMBOL(drm_event_cancel_free);
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
692  * &drm_device.event_lock, see drm_send_event() for the unlocked version.
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.
698  */
699 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
700 {
701         assert_spin_locked(&dev->event_lock);
702
703         if (e->completion) {
704                 complete_all(e->completion);
705                 e->completion_release(e->completion);
706                 e->completion = NULL;
707         }
708
709         if (e->fence) {
710                 dma_fence_signal(e->fence);
711                 dma_fence_put(e->fence);
712         }
713
714         if (!e->file_priv) {
715                 kfree(e);
716                 return;
717         }
718
719         list_del(&e->pending_link);
720         list_add_tail(&e->link,
721                       &e->file_priv->event_list);
722         wake_up_interruptible(&e->file_priv->event_wait);
723 }
724 EXPORT_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(),
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.
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.
740  */
741 void 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 }
749 EXPORT_SYMBOL(drm_send_event);