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