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