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