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