xref: /openbsd-src/sys/dev/pci/drm/drm_file.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
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 		if (drm_dev_is_unplugged(dev))
493 			drm_put_dev(dev);
494 	}
495 	mutex_unlock(&drm_global_mutex);
496 
497 	drm_minor_release(minor);
498 
499 	return 0;
500 }
501 EXPORT_SYMBOL(drm_release);
502 
503 /**
504  * drm_read - read method for DRM file
505  * @filp: file pointer
506  * @buffer: userspace destination pointer for the read
507  * @count: count in bytes to read
508  * @offset: offset to read
509  *
510  * This function must be used by drivers as their &file_operations.read
511  * method iff they use DRM events for asynchronous signalling to userspace.
512  * Since events are used by the KMS API for vblank and page flip completion this
513  * means all modern display drivers must use it.
514  *
515  * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
516  * must set the &file_operation.llseek to no_llseek(). Polling support is
517  * provided by drm_poll().
518  *
519  * This function will only ever read a full event. Therefore userspace must
520  * supply a big enough buffer to fit any event to ensure forward progress. Since
521  * the maximum event space is currently 4K it's recommended to just use that for
522  * safety.
523  *
524  * RETURNS:
525  *
526  * Number of bytes read (always aligned to full events, and can be 0) or a
527  * negative error code on failure.
528  */
529 ssize_t drm_read(struct file *filp, char __user *buffer,
530 		 size_t count, loff_t *offset)
531 {
532 	struct drm_file *file_priv = filp->private_data;
533 	struct drm_device *dev = file_priv->minor->dev;
534 	ssize_t ret;
535 
536 	if (!access_ok(VERIFY_WRITE, buffer, count))
537 		return -EFAULT;
538 
539 	ret = mutex_lock_interruptible(&file_priv->event_read_lock);
540 	if (ret)
541 		return ret;
542 
543 	for (;;) {
544 		struct drm_pending_event *e = NULL;
545 
546 		spin_lock_irq(&dev->event_lock);
547 		if (!list_empty(&file_priv->event_list)) {
548 			e = list_first_entry(&file_priv->event_list,
549 					struct drm_pending_event, link);
550 			file_priv->event_space += e->event->length;
551 			list_del(&e->link);
552 		}
553 		spin_unlock_irq(&dev->event_lock);
554 
555 		if (e == NULL) {
556 			if (ret)
557 				break;
558 
559 			if (filp->f_flags & O_NONBLOCK) {
560 				ret = -EAGAIN;
561 				break;
562 			}
563 
564 			mutex_unlock(&file_priv->event_read_lock);
565 			ret = wait_event_interruptible(file_priv->event_wait,
566 						       !list_empty(&file_priv->event_list));
567 			if (ret >= 0)
568 				ret = mutex_lock_interruptible(&file_priv->event_read_lock);
569 			if (ret)
570 				return ret;
571 		} else {
572 			unsigned length = e->event->length;
573 
574 			if (length > count - ret) {
575 put_back_event:
576 				spin_lock_irq(&dev->event_lock);
577 				file_priv->event_space -= length;
578 				list_add(&e->link, &file_priv->event_list);
579 				spin_unlock_irq(&dev->event_lock);
580 				break;
581 			}
582 
583 			if (copy_to_user(buffer + ret, e->event, length)) {
584 				if (ret == 0)
585 					ret = -EFAULT;
586 				goto put_back_event;
587 			}
588 
589 			ret += length;
590 			kfree(e);
591 		}
592 	}
593 	mutex_unlock(&file_priv->event_read_lock);
594 
595 	return ret;
596 }
597 EXPORT_SYMBOL(drm_read);
598 
599 /**
600  * drm_poll - poll method for DRM file
601  * @filp: file pointer
602  * @wait: poll waiter table
603  *
604  * This function must be used by drivers as their &file_operations.read method
605  * iff they use DRM events for asynchronous signalling to userspace.  Since
606  * events are used by the KMS API for vblank and page flip completion this means
607  * all modern display drivers must use it.
608  *
609  * See also drm_read().
610  *
611  * RETURNS:
612  *
613  * Mask of POLL flags indicating the current status of the file.
614  */
615 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
616 {
617 	struct drm_file *file_priv = filp->private_data;
618 	__poll_t mask = 0;
619 
620 	poll_wait(filp, &file_priv->event_wait, wait);
621 
622 	if (!list_empty(&file_priv->event_list))
623 		mask |= EPOLLIN | EPOLLRDNORM;
624 
625 	return mask;
626 }
627 EXPORT_SYMBOL(drm_poll);
628 
629 #endif
630 
631 /**
632  * drm_event_reserve_init_locked - init a DRM event and reserve space for it
633  * @dev: DRM device
634  * @file_priv: DRM file private data
635  * @p: tracking structure for the pending event
636  * @e: actual event data to deliver to userspace
637  *
638  * This function prepares the passed in event for eventual delivery. If the event
639  * doesn't get delivered (because the IOCTL fails later on, before queuing up
640  * anything) then the even must be cancelled and freed using
641  * drm_event_cancel_free(). Successfully initialized events should be sent out
642  * using drm_send_event() or drm_send_event_locked() to signal completion of the
643  * asynchronous event to userspace.
644  *
645  * If callers embedded @p into a larger structure it must be allocated with
646  * kmalloc and @p must be the first member element.
647  *
648  * This is the locked version of drm_event_reserve_init() for callers which
649  * already hold &drm_device.event_lock.
650  *
651  * RETURNS:
652  *
653  * 0 on success or a negative error code on failure.
654  */
655 int drm_event_reserve_init_locked(struct drm_device *dev,
656 				  struct drm_file *file_priv,
657 				  struct drm_pending_event *p,
658 				  struct drm_event *e)
659 {
660 	if (file_priv->event_space < e->length)
661 		return -ENOMEM;
662 
663 	file_priv->event_space -= e->length;
664 
665 	p->event = e;
666 	list_add(&p->pending_link, &file_priv->pending_event_list);
667 	p->file_priv = file_priv;
668 
669 	return 0;
670 }
671 EXPORT_SYMBOL(drm_event_reserve_init_locked);
672 
673 /**
674  * drm_event_reserve_init - init a DRM event and reserve space for it
675  * @dev: DRM device
676  * @file_priv: DRM file private data
677  * @p: tracking structure for the pending event
678  * @e: actual event data to deliver to userspace
679  *
680  * This function prepares the passed in event for eventual delivery. If the event
681  * doesn't get delivered (because the IOCTL fails later on, before queuing up
682  * anything) then the even must be cancelled and freed using
683  * drm_event_cancel_free(). Successfully initialized events should be sent out
684  * using drm_send_event() or drm_send_event_locked() to signal completion of the
685  * asynchronous event to userspace.
686  *
687  * If callers embedded @p into a larger structure it must be allocated with
688  * kmalloc and @p must be the first member element.
689  *
690  * Callers which already hold &drm_device.event_lock should use
691  * drm_event_reserve_init_locked() instead.
692  *
693  * RETURNS:
694  *
695  * 0 on success or a negative error code on failure.
696  */
697 int drm_event_reserve_init(struct drm_device *dev,
698 			   struct drm_file *file_priv,
699 			   struct drm_pending_event *p,
700 			   struct drm_event *e)
701 {
702 	unsigned long flags;
703 	int ret;
704 
705 	spin_lock_irqsave(&dev->event_lock, flags);
706 	ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
707 	spin_unlock_irqrestore(&dev->event_lock, flags);
708 
709 	return ret;
710 }
711 EXPORT_SYMBOL(drm_event_reserve_init);
712 
713 /**
714  * drm_event_cancel_free - free a DRM event and release it's space
715  * @dev: DRM device
716  * @p: tracking structure for the pending event
717  *
718  * This function frees the event @p initialized with drm_event_reserve_init()
719  * and releases any allocated space. It is used to cancel an event when the
720  * nonblocking operation could not be submitted and needed to be aborted.
721  */
722 void drm_event_cancel_free(struct drm_device *dev,
723 			   struct drm_pending_event *p)
724 {
725 	unsigned long flags;
726 	spin_lock_irqsave(&dev->event_lock, flags);
727 	if (p->file_priv) {
728 		p->file_priv->event_space += p->event->length;
729 		list_del(&p->pending_link);
730 	}
731 	spin_unlock_irqrestore(&dev->event_lock, flags);
732 
733 	if (p->fence)
734 		dma_fence_put(p->fence);
735 
736 	kfree(p);
737 }
738 EXPORT_SYMBOL(drm_event_cancel_free);
739 
740 /**
741  * drm_send_event_locked - send DRM event to file descriptor
742  * @dev: DRM device
743  * @e: DRM event to deliver
744  *
745  * This function sends the event @e, initialized with drm_event_reserve_init(),
746  * to its associated userspace DRM file. Callers must already hold
747  * &drm_device.event_lock, see drm_send_event() for the unlocked version.
748  *
749  * Note that the core will take care of unlinking and disarming events when the
750  * corresponding DRM file is closed. Drivers need not worry about whether the
751  * DRM file for this event still exists and can call this function upon
752  * completion of the asynchronous work unconditionally.
753  */
754 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
755 {
756 	assert_spin_locked(&dev->event_lock);
757 
758 	if (e->completion) {
759 		complete_all(e->completion);
760 		e->completion_release(e->completion);
761 		e->completion = NULL;
762 	}
763 
764 	if (e->fence) {
765 		dma_fence_signal(e->fence);
766 		dma_fence_put(e->fence);
767 	}
768 
769 	if (!e->file_priv) {
770 		kfree(e);
771 		return;
772 	}
773 
774 	list_del(&e->pending_link);
775 	list_add_tail(&e->link,
776 		      &e->file_priv->event_list);
777 	wake_up_interruptible(&e->file_priv->event_wait);
778 #ifdef __OpenBSD__
779 	selwakeup(&e->file_priv->rsel);
780 #endif
781 }
782 EXPORT_SYMBOL(drm_send_event_locked);
783 
784 /**
785  * drm_send_event - send DRM event to file descriptor
786  * @dev: DRM device
787  * @e: DRM event to deliver
788  *
789  * This function sends the event @e, initialized with drm_event_reserve_init(),
790  * to its associated userspace DRM file. This function acquires
791  * &drm_device.event_lock, see drm_send_event_locked() for callers which already
792  * hold this lock.
793  *
794  * Note that the core will take care of unlinking and disarming events when the
795  * corresponding DRM file is closed. Drivers need not worry about whether the
796  * DRM file for this event still exists and can call this function upon
797  * completion of the asynchronous work unconditionally.
798  */
799 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
800 {
801 	unsigned long irqflags;
802 
803 	spin_lock_irqsave(&dev->event_lock, irqflags);
804 	drm_send_event_locked(dev, e);
805 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
806 }
807 EXPORT_SYMBOL(drm_send_event);
808