drm_file.c revision 1.1 1 /* $NetBSD: drm_file.c,v 1.1 2021/12/18 20:11:02 riastradh Exp $ */
2
3 /*
4 * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
5 * \author Daryll Strauss <daryll (at) valinux.com>
6 * \author Gareth Hughes <gareth (at) valinux.com>
7 */
8
9 /*
10 * Created: Mon Jan 4 08:58:31 1999 by faith (at) valinux.com
11 *
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: drm_file.c,v 1.1 2021/12/18 20:11:02 riastradh Exp $");
38
39 #include <linux/anon_inodes.h>
40 #include <linux/dma-fence.h>
41 #include <linux/file.h>
42 #include <linux/module.h>
43 #include <linux/pci.h>
44 #include <linux/poll.h>
45 #include <linux/slab.h>
46
47 #include <drm/drm_client.h>
48 #include <drm/drm_drv.h>
49 #include <drm/drm_file.h>
50 #include <drm/drm_print.h>
51
52 #include "drm_crtc_internal.h"
53 #include "drm_internal.h"
54 #include "drm_legacy.h"
55
56 /* from BKL pushdown */
57 DEFINE_MUTEX(drm_global_mutex);
58
59 /**
60 * DOC: file operations
61 *
62 * Drivers must define the file operations structure that forms the DRM
63 * userspace API entry point, even though most of those operations are
64 * implemented in the DRM core. The resulting &struct file_operations must be
65 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
66 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
67 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
68 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
69 * that require 32/64 bit compatibility support must provide their own
70 * &file_operations.compat_ioctl handler that processes private ioctls and calls
71 * drm_compat_ioctl() for core ioctls.
72 *
73 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
74 * events are a generic and extensible means to send asynchronous events to
75 * userspace through the file descriptor. They are used to send vblank event and
76 * page flip completions by the KMS API. But drivers can also use it for their
77 * own needs, e.g. to signal completion of rendering.
78 *
79 * For the driver-side event interface see drm_event_reserve_init() and
80 * drm_send_event() as the main starting points.
81 *
82 * The memory mapping implementation will vary depending on how the driver
83 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
84 * function, modern drivers should use one of the provided memory-manager
85 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
86 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
87 *
88 * No other file operations are supported by the DRM userspace API. Overall the
89 * following is an example &file_operations structure::
90 *
91 * static const example_drm_fops = {
92 * .owner = THIS_MODULE,
93 * .open = drm_open,
94 * .release = drm_release,
95 * .unlocked_ioctl = drm_ioctl,
96 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
97 * .poll = drm_poll,
98 * .read = drm_read,
99 * .llseek = no_llseek,
100 * .mmap = drm_gem_mmap,
101 * };
102 *
103 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
104 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
105 * simpler.
106 *
107 * The driver's &file_operations must be stored in &drm_driver.fops.
108 *
109 * For driver-private IOCTL handling see the more detailed discussion in
110 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
111 */
112
113 /**
114 * drm_file_alloc - allocate file context
115 * @minor: minor to allocate on
116 *
117 * This allocates a new DRM file context. It is not linked into any context and
118 * can be used by the caller freely. Note that the context keeps a pointer to
119 * @minor, so it must be freed before @minor is.
120 *
121 * RETURNS:
122 * Pointer to newly allocated context, ERR_PTR on failure.
123 */
124 struct drm_file *drm_file_alloc(struct drm_minor *minor)
125 {
126 struct drm_device *dev = minor->dev;
127 struct drm_file *file;
128 int ret;
129
130 file = kzalloc(sizeof(*file), GFP_KERNEL);
131 if (!file)
132 return ERR_PTR(-ENOMEM);
133
134 file->pid = get_pid(task_pid(current));
135 file->minor = minor;
136
137 /* for compatibility root is always authenticated */
138 file->authenticated = capable(CAP_SYS_ADMIN);
139
140 INIT_LIST_HEAD(&file->lhead);
141 INIT_LIST_HEAD(&file->fbs);
142 mutex_init(&file->fbs_lock);
143 INIT_LIST_HEAD(&file->blobs);
144 INIT_LIST_HEAD(&file->pending_event_list);
145 INIT_LIST_HEAD(&file->event_list);
146 init_waitqueue_head(&file->event_wait);
147 file->event_space = 4096; /* set aside 4k for event buffer */
148
149 mutex_init(&file->event_read_lock);
150
151 if (drm_core_check_feature(dev, DRIVER_GEM))
152 drm_gem_open(dev, file);
153
154 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
155 drm_syncobj_open(file);
156
157 drm_prime_init_file_private(&file->prime);
158
159 if (dev->driver->open) {
160 ret = dev->driver->open(dev, file);
161 if (ret < 0)
162 goto out_prime_destroy;
163 }
164
165 return file;
166
167 out_prime_destroy:
168 drm_prime_destroy_file_private(&file->prime);
169 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
170 drm_syncobj_release(file);
171 if (drm_core_check_feature(dev, DRIVER_GEM))
172 drm_gem_release(dev, file);
173 put_pid(file->pid);
174 kfree(file);
175
176 return ERR_PTR(ret);
177 }
178
179 static void drm_events_release(struct drm_file *file_priv)
180 {
181 struct drm_device *dev = file_priv->minor->dev;
182 struct drm_pending_event *e, *et;
183 unsigned long flags;
184
185 spin_lock_irqsave(&dev->event_lock, flags);
186
187 /* Unlink pending events */
188 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
189 pending_link) {
190 list_del(&e->pending_link);
191 e->file_priv = NULL;
192 }
193
194 /* Remove unconsumed events */
195 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
196 list_del(&e->link);
197 kfree(e);
198 }
199
200 spin_unlock_irqrestore(&dev->event_lock, flags);
201 }
202
203 /**
204 * drm_file_free - free file context
205 * @file: context to free, or NULL
206 *
207 * This destroys and deallocates a DRM file context previously allocated via
208 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
209 * before calling this.
210 *
211 * If NULL is passed, this is a no-op.
212 *
213 * RETURNS:
214 * 0 on success, or error code on failure.
215 */
216 void drm_file_free(struct drm_file *file)
217 {
218 struct drm_device *dev;
219
220 if (!file)
221 return;
222
223 dev = file->minor->dev;
224
225 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
226 task_pid_nr(current),
227 (long)old_encode_dev(file->minor->kdev->devt),
228 dev->open_count);
229
230 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
231 dev->driver->preclose)
232 dev->driver->preclose(dev, file);
233
234 if (drm_core_check_feature(dev, DRIVER_LEGACY))
235 drm_legacy_lock_release(dev, file->filp);
236
237 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
238 drm_legacy_reclaim_buffers(dev, file);
239
240 drm_events_release(file);
241
242 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
243 drm_fb_release(file);
244 drm_property_destroy_user_blobs(dev, file);
245 }
246
247 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
248 drm_syncobj_release(file);
249
250 if (drm_core_check_feature(dev, DRIVER_GEM))
251 drm_gem_release(dev, file);
252
253 drm_legacy_ctxbitmap_flush(dev, file);
254
255 if (drm_is_primary_client(file))
256 drm_master_release(file);
257
258 if (dev->driver->postclose)
259 dev->driver->postclose(dev, file);
260
261 drm_prime_destroy_file_private(&file->prime);
262
263 WARN_ON(!list_empty(&file->event_list));
264
265 put_pid(file->pid);
266 kfree(file);
267 }
268
269 static void drm_close_helper(struct file *filp)
270 {
271 struct drm_file *file_priv = filp->private_data;
272 struct drm_device *dev = file_priv->minor->dev;
273
274 mutex_lock(&dev->filelist_mutex);
275 list_del(&file_priv->lhead);
276 mutex_unlock(&dev->filelist_mutex);
277
278 drm_file_free(file_priv);
279 }
280
281 /*
282 * Check whether DRI will run on this CPU.
283 *
284 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
285 */
286 static int drm_cpu_valid(void)
287 {
288 #if defined(__sparc__) && !defined(__sparc_v9__)
289 return 0; /* No cmpxchg before v9 sparc. */
290 #endif
291 return 1;
292 }
293
294 /*
295 * Called whenever a process opens a drm node
296 *
297 * \param filp file pointer.
298 * \param minor acquired minor-object.
299 * \return zero on success or a negative number on failure.
300 *
301 * Creates and initializes a drm_file structure for the file private data in \p
302 * filp and add it into the double linked list in \p dev.
303 */
304 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
305 {
306 struct drm_device *dev = minor->dev;
307 struct drm_file *priv;
308 int ret;
309
310 if (filp->f_flags & O_EXCL)
311 return -EBUSY; /* No exclusive opens */
312 if (!drm_cpu_valid())
313 return -EINVAL;
314 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
315 return -EINVAL;
316
317 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
318
319 priv = drm_file_alloc(minor);
320 if (IS_ERR(priv))
321 return PTR_ERR(priv);
322
323 if (drm_is_primary_client(priv)) {
324 ret = drm_master_open(priv);
325 if (ret) {
326 drm_file_free(priv);
327 return ret;
328 }
329 }
330
331 filp->private_data = priv;
332 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
333 priv->filp = filp;
334
335 mutex_lock(&dev->filelist_mutex);
336 list_add(&priv->lhead, &dev->filelist);
337 mutex_unlock(&dev->filelist_mutex);
338
339 #ifdef __alpha__
340 /*
341 * Default the hose
342 */
343 if (!dev->hose) {
344 struct pci_dev *pci_dev;
345 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
346 if (pci_dev) {
347 dev->hose = pci_dev->sysdata;
348 pci_dev_put(pci_dev);
349 }
350 if (!dev->hose) {
351 struct pci_bus *b = list_entry(pci_root_buses.next,
352 struct pci_bus, node);
353 if (b)
354 dev->hose = b->sysdata;
355 }
356 }
357 #endif
358
359 return 0;
360 }
361
362 /**
363 * drm_open - open method for DRM file
364 * @inode: device inode
365 * @filp: file pointer.
366 *
367 * This function must be used by drivers as their &file_operations.open method.
368 * It looks up the correct DRM device and instantiates all the per-file
369 * resources for it. It also calls the &drm_driver.open driver callback.
370 *
371 * RETURNS:
372 *
373 * 0 on success or negative errno value on falure.
374 */
375 int drm_open(struct inode *inode, struct file *filp)
376 {
377 struct drm_device *dev;
378 struct drm_minor *minor;
379 int retcode;
380 int need_setup = 0;
381
382 minor = drm_minor_acquire(iminor(inode));
383 if (IS_ERR(minor))
384 return PTR_ERR(minor);
385
386 dev = minor->dev;
387 if (!dev->open_count++)
388 need_setup = 1;
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
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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
765 /**
766 * mock_drm_getfile - Create a new struct file for the drm device
767 * @minor: drm minor to wrap (e.g. #drm_device.primary)
768 * @flags: file creation mode (O_RDWR etc)
769 *
770 * This create a new struct file that wraps a DRM file context around a
771 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
772 * invoking userspace. The struct file may be operated on using its f_op
773 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
774 * to userspace facing functions as an internal/anonymous client.
775 *
776 * RETURNS:
777 * Pointer to newly created struct file, ERR_PTR on failure.
778 */
779 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
780 {
781 struct drm_device *dev = minor->dev;
782 struct drm_file *priv;
783 struct file *file;
784
785 priv = drm_file_alloc(minor);
786 if (IS_ERR(priv))
787 return ERR_CAST(priv);
788
789 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
790 if (IS_ERR(file)) {
791 drm_file_free(priv);
792 return file;
793 }
794
795 /* Everyone shares a single global address space */
796 file->f_mapping = dev->anon_inode->i_mapping;
797
798 drm_dev_get(dev);
799 priv->filp = file;
800
801 return file;
802 }
803 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);
804