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