Merge tag 'selinux-pr-20200210' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.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/anon_inodes.h>
35 #include <linux/dma-fence.h>
36 #include <linux/file.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/poll.h>
40 #include <linux/slab.h>
41
42 #include <drm/drm_client.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_file.h>
45 #include <drm/drm_print.h>
46
47 #include "drm_crtc_internal.h"
48 #include "drm_internal.h"
49 #include "drm_legacy.h"
50
51 /* from BKL pushdown */
52 DEFINE_MUTEX(drm_global_mutex);
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  */
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
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  */
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
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  */
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 a drm node
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  */
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  */
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         /* share address_space across all char-devs of a single device */
386         filp->f_mapping = dev->anon_inode->i_mapping;
387
388         retcode = drm_open_helper(filp, minor);
389         if (retcode)
390                 goto err_undo;
391         if (need_setup) {
392                 retcode = drm_legacy_setup(dev);
393                 if (retcode) {
394                         drm_close_helper(filp);
395                         goto err_undo;
396                 }
397         }
398         return 0;
399
400 err_undo:
401         dev->open_count--;
402         drm_minor_release(minor);
403         return retcode;
404 }
405 EXPORT_SYMBOL(drm_open);
406
407 void drm_lastclose(struct drm_device * dev)
408 {
409         DRM_DEBUG("\n");
410
411         if (dev->driver->lastclose)
412                 dev->driver->lastclose(dev);
413         DRM_DEBUG("driver lastclose completed\n");
414
415         if (drm_core_check_feature(dev, DRIVER_LEGACY))
416                 drm_legacy_dev_reinit(dev);
417
418         drm_client_dev_restore(dev);
419 }
420
421 /**
422  * drm_release - release method for DRM file
423  * @inode: device inode
424  * @filp: file pointer.
425  *
426  * This function must be used by drivers as their &file_operations.release
427  * method. It frees any resources associated with the open file, and calls the
428  * &drm_driver.postclose driver callback. If this is the last open file for the
429  * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
430  *
431  * RETURNS:
432  *
433  * Always succeeds and returns 0.
434  */
435 int drm_release(struct inode *inode, struct file *filp)
436 {
437         struct drm_file *file_priv = filp->private_data;
438         struct drm_minor *minor = file_priv->minor;
439         struct drm_device *dev = minor->dev;
440
441         mutex_lock(&drm_global_mutex);
442
443         DRM_DEBUG("open_count = %d\n", dev->open_count);
444
445         drm_close_helper(filp);
446
447         if (!--dev->open_count)
448                 drm_lastclose(dev);
449
450         mutex_unlock(&drm_global_mutex);
451
452         drm_minor_release(minor);
453
454         return 0;
455 }
456 EXPORT_SYMBOL(drm_release);
457
458 /**
459  * drm_read - read method for DRM file
460  * @filp: file pointer
461  * @buffer: userspace destination pointer for the read
462  * @count: count in bytes to read
463  * @offset: offset to read
464  *
465  * This function must be used by drivers as their &file_operations.read
466  * method iff they use DRM events for asynchronous signalling to userspace.
467  * Since events are used by the KMS API for vblank and page flip completion this
468  * means all modern display drivers must use it.
469  *
470  * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
471  * must set the &file_operation.llseek to no_llseek(). Polling support is
472  * provided by drm_poll().
473  *
474  * This function will only ever read a full event. Therefore userspace must
475  * supply a big enough buffer to fit any event to ensure forward progress. Since
476  * the maximum event space is currently 4K it's recommended to just use that for
477  * safety.
478  *
479  * RETURNS:
480  *
481  * Number of bytes read (always aligned to full events, and can be 0) or a
482  * negative error code on failure.
483  */
484 ssize_t drm_read(struct file *filp, char __user *buffer,
485                  size_t count, loff_t *offset)
486 {
487         struct drm_file *file_priv = filp->private_data;
488         struct drm_device *dev = file_priv->minor->dev;
489         ssize_t ret;
490
491         if (!access_ok(buffer, count))
492                 return -EFAULT;
493
494         ret = mutex_lock_interruptible(&file_priv->event_read_lock);
495         if (ret)
496                 return ret;
497
498         for (;;) {
499                 struct drm_pending_event *e = NULL;
500
501                 spin_lock_irq(&dev->event_lock);
502                 if (!list_empty(&file_priv->event_list)) {
503                         e = list_first_entry(&file_priv->event_list,
504                                         struct drm_pending_event, link);
505                         file_priv->event_space += e->event->length;
506                         list_del(&e->link);
507                 }
508                 spin_unlock_irq(&dev->event_lock);
509
510                 if (e == NULL) {
511                         if (ret)
512                                 break;
513
514                         if (filp->f_flags & O_NONBLOCK) {
515                                 ret = -EAGAIN;
516                                 break;
517                         }
518
519                         mutex_unlock(&file_priv->event_read_lock);
520                         ret = wait_event_interruptible(file_priv->event_wait,
521                                                        !list_empty(&file_priv->event_list));
522                         if (ret >= 0)
523                                 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
524                         if (ret)
525                                 return ret;
526                 } else {
527                         unsigned length = e->event->length;
528
529                         if (length > count - ret) {
530 put_back_event:
531                                 spin_lock_irq(&dev->event_lock);
532                                 file_priv->event_space -= length;
533                                 list_add(&e->link, &file_priv->event_list);
534                                 spin_unlock_irq(&dev->event_lock);
535                                 wake_up_interruptible(&file_priv->event_wait);
536                                 break;
537                         }
538
539                         if (copy_to_user(buffer + ret, e->event, length)) {
540                                 if (ret == 0)
541                                         ret = -EFAULT;
542                                 goto put_back_event;
543                         }
544
545                         ret += length;
546                         kfree(e);
547                 }
548         }
549         mutex_unlock(&file_priv->event_read_lock);
550
551         return ret;
552 }
553 EXPORT_SYMBOL(drm_read);
554
555 /**
556  * drm_poll - poll method for DRM file
557  * @filp: file pointer
558  * @wait: poll waiter table
559  *
560  * This function must be used by drivers as their &file_operations.read method
561  * iff they use DRM events for asynchronous signalling to userspace.  Since
562  * events are used by the KMS API for vblank and page flip completion this means
563  * all modern display drivers must use it.
564  *
565  * See also drm_read().
566  *
567  * RETURNS:
568  *
569  * Mask of POLL flags indicating the current status of the file.
570  */
571 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
572 {
573         struct drm_file *file_priv = filp->private_data;
574         __poll_t mask = 0;
575
576         poll_wait(filp, &file_priv->event_wait, wait);
577
578         if (!list_empty(&file_priv->event_list))
579                 mask |= EPOLLIN | EPOLLRDNORM;
580
581         return mask;
582 }
583 EXPORT_SYMBOL(drm_poll);
584
585 /**
586  * drm_event_reserve_init_locked - init a DRM event and reserve space for it
587  * @dev: DRM device
588  * @file_priv: DRM file private data
589  * @p: tracking structure for the pending event
590  * @e: actual event data to deliver to userspace
591  *
592  * This function prepares the passed in event for eventual delivery. If the event
593  * doesn't get delivered (because the IOCTL fails later on, before queuing up
594  * anything) then the even must be cancelled and freed using
595  * drm_event_cancel_free(). Successfully initialized events should be sent out
596  * using drm_send_event() or drm_send_event_locked() to signal completion of the
597  * asynchronous event to userspace.
598  *
599  * If callers embedded @p into a larger structure it must be allocated with
600  * kmalloc and @p must be the first member element.
601  *
602  * This is the locked version of drm_event_reserve_init() for callers which
603  * already hold &drm_device.event_lock.
604  *
605  * RETURNS:
606  *
607  * 0 on success or a negative error code on failure.
608  */
609 int drm_event_reserve_init_locked(struct drm_device *dev,
610                                   struct drm_file *file_priv,
611                                   struct drm_pending_event *p,
612                                   struct drm_event *e)
613 {
614         if (file_priv->event_space < e->length)
615                 return -ENOMEM;
616
617         file_priv->event_space -= e->length;
618
619         p->event = e;
620         list_add(&p->pending_link, &file_priv->pending_event_list);
621         p->file_priv = file_priv;
622
623         return 0;
624 }
625 EXPORT_SYMBOL(drm_event_reserve_init_locked);
626
627 /**
628  * drm_event_reserve_init - init a DRM event and reserve space for it
629  * @dev: DRM device
630  * @file_priv: DRM file private data
631  * @p: tracking structure for the pending event
632  * @e: actual event data to deliver to userspace
633  *
634  * This function prepares the passed in event for eventual delivery. If the event
635  * doesn't get delivered (because the IOCTL fails later on, before queuing up
636  * anything) then the even must be cancelled and freed using
637  * drm_event_cancel_free(). Successfully initialized events should be sent out
638  * using drm_send_event() or drm_send_event_locked() to signal completion of the
639  * asynchronous event to userspace.
640  *
641  * If callers embedded @p into a larger structure it must be allocated with
642  * kmalloc and @p must be the first member element.
643  *
644  * Callers which already hold &drm_device.event_lock should use
645  * drm_event_reserve_init_locked() instead.
646  *
647  * RETURNS:
648  *
649  * 0 on success or a negative error code on failure.
650  */
651 int drm_event_reserve_init(struct drm_device *dev,
652                            struct drm_file *file_priv,
653                            struct drm_pending_event *p,
654                            struct drm_event *e)
655 {
656         unsigned long flags;
657         int ret;
658
659         spin_lock_irqsave(&dev->event_lock, flags);
660         ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
661         spin_unlock_irqrestore(&dev->event_lock, flags);
662
663         return ret;
664 }
665 EXPORT_SYMBOL(drm_event_reserve_init);
666
667 /**
668  * drm_event_cancel_free - free a DRM event and release its space
669  * @dev: DRM device
670  * @p: tracking structure for the pending event
671  *
672  * This function frees the event @p initialized with drm_event_reserve_init()
673  * and releases any allocated space. It is used to cancel an event when the
674  * nonblocking operation could not be submitted and needed to be aborted.
675  */
676 void drm_event_cancel_free(struct drm_device *dev,
677                            struct drm_pending_event *p)
678 {
679         unsigned long flags;
680         spin_lock_irqsave(&dev->event_lock, flags);
681         if (p->file_priv) {
682                 p->file_priv->event_space += p->event->length;
683                 list_del(&p->pending_link);
684         }
685         spin_unlock_irqrestore(&dev->event_lock, flags);
686
687         if (p->fence)
688                 dma_fence_put(p->fence);
689
690         kfree(p);
691 }
692 EXPORT_SYMBOL(drm_event_cancel_free);
693
694 /**
695  * drm_send_event_locked - send DRM event to file descriptor
696  * @dev: DRM device
697  * @e: DRM event to deliver
698  *
699  * This function sends the event @e, initialized with drm_event_reserve_init(),
700  * to its associated userspace DRM file. Callers must already hold
701  * &drm_device.event_lock, see drm_send_event() for the unlocked version.
702  *
703  * Note that the core will take care of unlinking and disarming events when the
704  * corresponding DRM file is closed. Drivers need not worry about whether the
705  * DRM file for this event still exists and can call this function upon
706  * completion of the asynchronous work unconditionally.
707  */
708 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
709 {
710         assert_spin_locked(&dev->event_lock);
711
712         if (e->completion) {
713                 complete_all(e->completion);
714                 e->completion_release(e->completion);
715                 e->completion = NULL;
716         }
717
718         if (e->fence) {
719                 dma_fence_signal(e->fence);
720                 dma_fence_put(e->fence);
721         }
722
723         if (!e->file_priv) {
724                 kfree(e);
725                 return;
726         }
727
728         list_del(&e->pending_link);
729         list_add_tail(&e->link,
730                       &e->file_priv->event_list);
731         wake_up_interruptible(&e->file_priv->event_wait);
732 }
733 EXPORT_SYMBOL(drm_send_event_locked);
734
735 /**
736  * drm_send_event - send DRM event to file descriptor
737  * @dev: DRM device
738  * @e: DRM event to deliver
739  *
740  * This function sends the event @e, initialized with drm_event_reserve_init(),
741  * to its associated userspace DRM file. This function acquires
742  * &drm_device.event_lock, see drm_send_event_locked() for callers which already
743  * hold this lock.
744  *
745  * Note that the core will take care of unlinking and disarming events when the
746  * corresponding DRM file is closed. Drivers need not worry about whether the
747  * DRM file for this event still exists and can call this function upon
748  * completion of the asynchronous work unconditionally.
749  */
750 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
751 {
752         unsigned long irqflags;
753
754         spin_lock_irqsave(&dev->event_lock, irqflags);
755         drm_send_event_locked(dev, e);
756         spin_unlock_irqrestore(&dev->event_lock, irqflags);
757 }
758 EXPORT_SYMBOL(drm_send_event);
759
760 /**
761  * mock_drm_getfile - Create a new struct file for the drm device
762  * @minor: drm minor to wrap (e.g. #drm_device.primary)
763  * @flags: file creation mode (O_RDWR etc)
764  *
765  * This create a new struct file that wraps a DRM file context around a
766  * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
767  * invoking userspace. The struct file may be operated on using its f_op
768  * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
769  * to userspace facing functions as an internal/anonymous client.
770  *
771  * RETURNS:
772  * Pointer to newly created struct file, ERR_PTR on failure.
773  */
774 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
775 {
776         struct drm_device *dev = minor->dev;
777         struct drm_file *priv;
778         struct file *file;
779
780         priv = drm_file_alloc(minor);
781         if (IS_ERR(priv))
782                 return ERR_CAST(priv);
783
784         file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
785         if (IS_ERR(file)) {
786                 drm_file_free(priv);
787                 return file;
788         }
789
790         /* Everyone shares a single global address space */
791         file->f_mapping = dev->anon_inode->i_mapping;
792
793         drm_dev_get(dev);
794         priv->filp = file;
795
796         return file;
797 }
798 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);