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