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