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