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