xref: /openbmc/linux/drivers/gpu/drm/drm_file.c (revision 16682588)
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 
drm_dev_needs_global_mutex(struct drm_device * dev)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  */
drm_file_alloc(struct drm_minor * minor)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 	rcu_assign_pointer(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(rcu_access_pointer(file->pid));
204 	kfree(file);
205 
206 	return ERR_PTR(ret);
207 }
208 
drm_events_release(struct drm_file * file_priv)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  */
drm_file_free(struct drm_file * file)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(rcu_access_pointer(file->pid));
295 	kfree(file);
296 }
297 
drm_close_helper(struct file * filp)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  */
drm_cpu_valid(void)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  */
drm_open_helper(struct file * filp,struct drm_minor * minor)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  */
drm_open(struct inode * inode,struct file * filp)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 
drm_lastclose(struct drm_device * dev)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  */
drm_release(struct inode * inode,struct file * filp)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 
drm_file_update_pid(struct drm_file * filp)508 void drm_file_update_pid(struct drm_file *filp)
509 {
510 	struct drm_device *dev;
511 	struct pid *pid, *old;
512 
513 	/*
514 	 * Master nodes need to keep the original ownership in order for
515 	 * drm_master_check_perm to keep working correctly. (See comment in
516 	 * drm_auth.c.)
517 	 */
518 	if (filp->was_master)
519 		return;
520 
521 	pid = task_tgid(current);
522 
523 	/*
524 	 * Quick unlocked check since the model is a single handover followed by
525 	 * exclusive repeated use.
526 	 */
527 	if (pid == rcu_access_pointer(filp->pid))
528 		return;
529 
530 	dev = filp->minor->dev;
531 	mutex_lock(&dev->filelist_mutex);
532 	get_pid(pid);
533 	old = rcu_replace_pointer(filp->pid, pid, 1);
534 	mutex_unlock(&dev->filelist_mutex);
535 
536 	synchronize_rcu();
537 	put_pid(old);
538 }
539 
540 /**
541  * drm_release_noglobal - release method for DRM file
542  * @inode: device inode
543  * @filp: file pointer.
544  *
545  * This function may be used by drivers as their &file_operations.release
546  * method. It frees any resources associated with the open file prior to taking
547  * the drm_global_mutex, which then calls the &drm_driver.postclose driver
548  * callback. If this is the last open file for the DRM device also proceeds to
549  * call the &drm_driver.lastclose driver callback.
550  *
551  * RETURNS:
552  *
553  * Always succeeds and returns 0.
554  */
drm_release_noglobal(struct inode * inode,struct file * filp)555 int drm_release_noglobal(struct inode *inode, struct file *filp)
556 {
557 	struct drm_file *file_priv = filp->private_data;
558 	struct drm_minor *minor = file_priv->minor;
559 	struct drm_device *dev = minor->dev;
560 
561 	drm_close_helper(filp);
562 
563 	if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
564 		drm_lastclose(dev);
565 		mutex_unlock(&drm_global_mutex);
566 	}
567 
568 	drm_minor_release(minor);
569 
570 	return 0;
571 }
572 EXPORT_SYMBOL(drm_release_noglobal);
573 
574 /**
575  * drm_read - read method for DRM file
576  * @filp: file pointer
577  * @buffer: userspace destination pointer for the read
578  * @count: count in bytes to read
579  * @offset: offset to read
580  *
581  * This function must be used by drivers as their &file_operations.read
582  * method if they use DRM events for asynchronous signalling to userspace.
583  * Since events are used by the KMS API for vblank and page flip completion this
584  * means all modern display drivers must use it.
585  *
586  * @offset is ignored, DRM events are read like a pipe. Polling support is
587  * provided by drm_poll().
588  *
589  * This function will only ever read a full event. Therefore userspace must
590  * supply a big enough buffer to fit any event to ensure forward progress. Since
591  * the maximum event space is currently 4K it's recommended to just use that for
592  * safety.
593  *
594  * RETURNS:
595  *
596  * Number of bytes read (always aligned to full events, and can be 0) or a
597  * negative error code on failure.
598  */
drm_read(struct file * filp,char __user * buffer,size_t count,loff_t * offset)599 ssize_t drm_read(struct file *filp, char __user *buffer,
600 		 size_t count, loff_t *offset)
601 {
602 	struct drm_file *file_priv = filp->private_data;
603 	struct drm_device *dev = file_priv->minor->dev;
604 	ssize_t ret;
605 
606 	ret = mutex_lock_interruptible(&file_priv->event_read_lock);
607 	if (ret)
608 		return ret;
609 
610 	for (;;) {
611 		struct drm_pending_event *e = NULL;
612 
613 		spin_lock_irq(&dev->event_lock);
614 		if (!list_empty(&file_priv->event_list)) {
615 			e = list_first_entry(&file_priv->event_list,
616 					struct drm_pending_event, link);
617 			file_priv->event_space += e->event->length;
618 			list_del(&e->link);
619 		}
620 		spin_unlock_irq(&dev->event_lock);
621 
622 		if (e == NULL) {
623 			if (ret)
624 				break;
625 
626 			if (filp->f_flags & O_NONBLOCK) {
627 				ret = -EAGAIN;
628 				break;
629 			}
630 
631 			mutex_unlock(&file_priv->event_read_lock);
632 			ret = wait_event_interruptible(file_priv->event_wait,
633 						       !list_empty(&file_priv->event_list));
634 			if (ret >= 0)
635 				ret = mutex_lock_interruptible(&file_priv->event_read_lock);
636 			if (ret)
637 				return ret;
638 		} else {
639 			unsigned length = e->event->length;
640 
641 			if (length > count - ret) {
642 put_back_event:
643 				spin_lock_irq(&dev->event_lock);
644 				file_priv->event_space -= length;
645 				list_add(&e->link, &file_priv->event_list);
646 				spin_unlock_irq(&dev->event_lock);
647 				wake_up_interruptible_poll(&file_priv->event_wait,
648 					EPOLLIN | EPOLLRDNORM);
649 				break;
650 			}
651 
652 			if (copy_to_user(buffer + ret, e->event, length)) {
653 				if (ret == 0)
654 					ret = -EFAULT;
655 				goto put_back_event;
656 			}
657 
658 			ret += length;
659 			kfree(e);
660 		}
661 	}
662 	mutex_unlock(&file_priv->event_read_lock);
663 
664 	return ret;
665 }
666 EXPORT_SYMBOL(drm_read);
667 
668 /**
669  * drm_poll - poll method for DRM file
670  * @filp: file pointer
671  * @wait: poll waiter table
672  *
673  * This function must be used by drivers as their &file_operations.read method
674  * if they use DRM events for asynchronous signalling to userspace.  Since
675  * events are used by the KMS API for vblank and page flip completion this means
676  * all modern display drivers must use it.
677  *
678  * See also drm_read().
679  *
680  * RETURNS:
681  *
682  * Mask of POLL flags indicating the current status of the file.
683  */
drm_poll(struct file * filp,struct poll_table_struct * wait)684 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
685 {
686 	struct drm_file *file_priv = filp->private_data;
687 	__poll_t mask = 0;
688 
689 	poll_wait(filp, &file_priv->event_wait, wait);
690 
691 	if (!list_empty(&file_priv->event_list))
692 		mask |= EPOLLIN | EPOLLRDNORM;
693 
694 	return mask;
695 }
696 EXPORT_SYMBOL(drm_poll);
697 
698 /**
699  * drm_event_reserve_init_locked - init a DRM event and reserve space for it
700  * @dev: DRM device
701  * @file_priv: DRM file private data
702  * @p: tracking structure for the pending event
703  * @e: actual event data to deliver to userspace
704  *
705  * This function prepares the passed in event for eventual delivery. If the event
706  * doesn't get delivered (because the IOCTL fails later on, before queuing up
707  * anything) then the even must be cancelled and freed using
708  * drm_event_cancel_free(). Successfully initialized events should be sent out
709  * using drm_send_event() or drm_send_event_locked() to signal completion of the
710  * asynchronous event to userspace.
711  *
712  * If callers embedded @p into a larger structure it must be allocated with
713  * kmalloc and @p must be the first member element.
714  *
715  * This is the locked version of drm_event_reserve_init() for callers which
716  * already hold &drm_device.event_lock.
717  *
718  * RETURNS:
719  *
720  * 0 on success or a negative error code on failure.
721  */
drm_event_reserve_init_locked(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)722 int drm_event_reserve_init_locked(struct drm_device *dev,
723 				  struct drm_file *file_priv,
724 				  struct drm_pending_event *p,
725 				  struct drm_event *e)
726 {
727 	if (file_priv->event_space < e->length)
728 		return -ENOMEM;
729 
730 	file_priv->event_space -= e->length;
731 
732 	p->event = e;
733 	list_add(&p->pending_link, &file_priv->pending_event_list);
734 	p->file_priv = file_priv;
735 
736 	return 0;
737 }
738 EXPORT_SYMBOL(drm_event_reserve_init_locked);
739 
740 /**
741  * drm_event_reserve_init - init a DRM event and reserve space for it
742  * @dev: DRM device
743  * @file_priv: DRM file private data
744  * @p: tracking structure for the pending event
745  * @e: actual event data to deliver to userspace
746  *
747  * This function prepares the passed in event for eventual delivery. If the event
748  * doesn't get delivered (because the IOCTL fails later on, before queuing up
749  * anything) then the even must be cancelled and freed using
750  * drm_event_cancel_free(). Successfully initialized events should be sent out
751  * using drm_send_event() or drm_send_event_locked() to signal completion of the
752  * asynchronous event to userspace.
753  *
754  * If callers embedded @p into a larger structure it must be allocated with
755  * kmalloc and @p must be the first member element.
756  *
757  * Callers which already hold &drm_device.event_lock should use
758  * drm_event_reserve_init_locked() instead.
759  *
760  * RETURNS:
761  *
762  * 0 on success or a negative error code on failure.
763  */
drm_event_reserve_init(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)764 int drm_event_reserve_init(struct drm_device *dev,
765 			   struct drm_file *file_priv,
766 			   struct drm_pending_event *p,
767 			   struct drm_event *e)
768 {
769 	unsigned long flags;
770 	int ret;
771 
772 	spin_lock_irqsave(&dev->event_lock, flags);
773 	ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
774 	spin_unlock_irqrestore(&dev->event_lock, flags);
775 
776 	return ret;
777 }
778 EXPORT_SYMBOL(drm_event_reserve_init);
779 
780 /**
781  * drm_event_cancel_free - free a DRM event and release its space
782  * @dev: DRM device
783  * @p: tracking structure for the pending event
784  *
785  * This function frees the event @p initialized with drm_event_reserve_init()
786  * and releases any allocated space. It is used to cancel an event when the
787  * nonblocking operation could not be submitted and needed to be aborted.
788  */
drm_event_cancel_free(struct drm_device * dev,struct drm_pending_event * p)789 void drm_event_cancel_free(struct drm_device *dev,
790 			   struct drm_pending_event *p)
791 {
792 	unsigned long flags;
793 
794 	spin_lock_irqsave(&dev->event_lock, flags);
795 	if (p->file_priv) {
796 		p->file_priv->event_space += p->event->length;
797 		list_del(&p->pending_link);
798 	}
799 	spin_unlock_irqrestore(&dev->event_lock, flags);
800 
801 	if (p->fence)
802 		dma_fence_put(p->fence);
803 
804 	kfree(p);
805 }
806 EXPORT_SYMBOL(drm_event_cancel_free);
807 
drm_send_event_helper(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)808 static void drm_send_event_helper(struct drm_device *dev,
809 			   struct drm_pending_event *e, ktime_t timestamp)
810 {
811 	assert_spin_locked(&dev->event_lock);
812 
813 	if (e->completion) {
814 		complete_all(e->completion);
815 		e->completion_release(e->completion);
816 		e->completion = NULL;
817 	}
818 
819 	if (e->fence) {
820 		if (timestamp)
821 			dma_fence_signal_timestamp(e->fence, timestamp);
822 		else
823 			dma_fence_signal(e->fence);
824 		dma_fence_put(e->fence);
825 	}
826 
827 	if (!e->file_priv) {
828 		kfree(e);
829 		return;
830 	}
831 
832 	list_del(&e->pending_link);
833 	list_add_tail(&e->link,
834 		      &e->file_priv->event_list);
835 	wake_up_interruptible_poll(&e->file_priv->event_wait,
836 		EPOLLIN | EPOLLRDNORM);
837 }
838 
839 /**
840  * drm_send_event_timestamp_locked - send DRM event to file descriptor
841  * @dev: DRM device
842  * @e: DRM event to deliver
843  * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
844  * time domain
845  *
846  * This function sends the event @e, initialized with drm_event_reserve_init(),
847  * to its associated userspace DRM file. Callers must already hold
848  * &drm_device.event_lock.
849  *
850  * Note that the core will take care of unlinking and disarming events when the
851  * corresponding DRM file is closed. Drivers need not worry about whether the
852  * DRM file for this event still exists and can call this function upon
853  * completion of the asynchronous work unconditionally.
854  */
drm_send_event_timestamp_locked(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)855 void drm_send_event_timestamp_locked(struct drm_device *dev,
856 				     struct drm_pending_event *e, ktime_t timestamp)
857 {
858 	drm_send_event_helper(dev, e, timestamp);
859 }
860 EXPORT_SYMBOL(drm_send_event_timestamp_locked);
861 
862 /**
863  * drm_send_event_locked - send DRM event to file descriptor
864  * @dev: DRM device
865  * @e: DRM event to deliver
866  *
867  * This function sends the event @e, initialized with drm_event_reserve_init(),
868  * to its associated userspace DRM file. Callers must already hold
869  * &drm_device.event_lock, see drm_send_event() for the unlocked version.
870  *
871  * Note that the core will take care of unlinking and disarming events when the
872  * corresponding DRM file is closed. Drivers need not worry about whether the
873  * DRM file for this event still exists and can call this function upon
874  * completion of the asynchronous work unconditionally.
875  */
drm_send_event_locked(struct drm_device * dev,struct drm_pending_event * e)876 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
877 {
878 	drm_send_event_helper(dev, e, 0);
879 }
880 EXPORT_SYMBOL(drm_send_event_locked);
881 
882 /**
883  * drm_send_event - send DRM event to file descriptor
884  * @dev: DRM device
885  * @e: DRM event to deliver
886  *
887  * This function sends the event @e, initialized with drm_event_reserve_init(),
888  * to its associated userspace DRM file. This function acquires
889  * &drm_device.event_lock, see drm_send_event_locked() for callers which already
890  * hold this lock.
891  *
892  * Note that the core will take care of unlinking and disarming events when the
893  * corresponding DRM file is closed. Drivers need not worry about whether the
894  * DRM file for this event still exists and can call this function upon
895  * completion of the asynchronous work unconditionally.
896  */
drm_send_event(struct drm_device * dev,struct drm_pending_event * e)897 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
898 {
899 	unsigned long irqflags;
900 
901 	spin_lock_irqsave(&dev->event_lock, irqflags);
902 	drm_send_event_helper(dev, e, 0);
903 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
904 }
905 EXPORT_SYMBOL(drm_send_event);
906 
print_size(struct drm_printer * p,const char * stat,const char * region,u64 sz)907 static void print_size(struct drm_printer *p, const char *stat,
908 		       const char *region, u64 sz)
909 {
910 	const char *units[] = {"", " KiB", " MiB"};
911 	unsigned u;
912 
913 	for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
914 		if (sz < SZ_1K)
915 			break;
916 		sz = div_u64(sz, SZ_1K);
917 	}
918 
919 	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
920 }
921 
922 /**
923  * drm_print_memory_stats - A helper to print memory stats
924  * @p: The printer to print output to
925  * @stats: The collected memory stats
926  * @supported_status: Bitmask of optional stats which are available
927  * @region: The memory region
928  *
929  */
drm_print_memory_stats(struct drm_printer * p,const struct drm_memory_stats * stats,enum drm_gem_object_status supported_status,const char * region)930 void drm_print_memory_stats(struct drm_printer *p,
931 			    const struct drm_memory_stats *stats,
932 			    enum drm_gem_object_status supported_status,
933 			    const char *region)
934 {
935 	print_size(p, "total", region, stats->private + stats->shared);
936 	print_size(p, "shared", region, stats->shared);
937 	print_size(p, "active", region, stats->active);
938 
939 	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
940 		print_size(p, "resident", region, stats->resident);
941 
942 	if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
943 		print_size(p, "purgeable", region, stats->purgeable);
944 }
945 EXPORT_SYMBOL(drm_print_memory_stats);
946 
947 /**
948  * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
949  * @p: the printer to print output to
950  * @file: the DRM file
951  *
952  * Helper to iterate over GEM objects with a handle allocated in the specified
953  * file.
954  */
drm_show_memory_stats(struct drm_printer * p,struct drm_file * file)955 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
956 {
957 	struct drm_gem_object *obj;
958 	struct drm_memory_stats status = {};
959 	enum drm_gem_object_status supported_status = 0;
960 	int id;
961 
962 	spin_lock(&file->table_lock);
963 	idr_for_each_entry (&file->object_idr, obj, id) {
964 		enum drm_gem_object_status s = 0;
965 
966 		if (obj->funcs && obj->funcs->status) {
967 			s = obj->funcs->status(obj);
968 			supported_status = DRM_GEM_OBJECT_RESIDENT |
969 					DRM_GEM_OBJECT_PURGEABLE;
970 		}
971 
972 		if (obj->handle_count > 1) {
973 			status.shared += obj->size;
974 		} else {
975 			status.private += obj->size;
976 		}
977 
978 		if (s & DRM_GEM_OBJECT_RESIDENT) {
979 			status.resident += obj->size;
980 		} else {
981 			/* If already purged or not yet backed by pages, don't
982 			 * count it as purgeable:
983 			 */
984 			s &= ~DRM_GEM_OBJECT_PURGEABLE;
985 		}
986 
987 		if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
988 			status.active += obj->size;
989 
990 			/* If still active, don't count as purgeable: */
991 			s &= ~DRM_GEM_OBJECT_PURGEABLE;
992 		}
993 
994 		if (s & DRM_GEM_OBJECT_PURGEABLE)
995 			status.purgeable += obj->size;
996 	}
997 	spin_unlock(&file->table_lock);
998 
999 	drm_print_memory_stats(p, &status, supported_status, "memory");
1000 }
1001 EXPORT_SYMBOL(drm_show_memory_stats);
1002 
1003 /**
1004  * drm_show_fdinfo - helper for drm file fops
1005  * @m: output stream
1006  * @f: the device file instance
1007  *
1008  * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
1009  * process using the GPU.  See also &drm_driver.show_fdinfo.
1010  *
1011  * For text output format description please see Documentation/gpu/drm-usage-stats.rst
1012  */
drm_show_fdinfo(struct seq_file * m,struct file * f)1013 void drm_show_fdinfo(struct seq_file *m, struct file *f)
1014 {
1015 	struct drm_file *file = f->private_data;
1016 	struct drm_device *dev = file->minor->dev;
1017 	struct drm_printer p = drm_seq_file_printer(m);
1018 
1019 	drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
1020 	drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
1021 
1022 	if (dev_is_pci(dev->dev)) {
1023 		struct pci_dev *pdev = to_pci_dev(dev->dev);
1024 
1025 		drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
1026 			   pci_domain_nr(pdev->bus), pdev->bus->number,
1027 			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1028 	}
1029 
1030 	if (dev->driver->show_fdinfo)
1031 		dev->driver->show_fdinfo(&p, file);
1032 }
1033 EXPORT_SYMBOL(drm_show_fdinfo);
1034 
1035 /**
1036  * mock_drm_getfile - Create a new struct file for the drm device
1037  * @minor: drm minor to wrap (e.g. #drm_device.primary)
1038  * @flags: file creation mode (O_RDWR etc)
1039  *
1040  * This create a new struct file that wraps a DRM file context around a
1041  * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
1042  * invoking userspace. The struct file may be operated on using its f_op
1043  * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
1044  * to userspace facing functions as an internal/anonymous client.
1045  *
1046  * RETURNS:
1047  * Pointer to newly created struct file, ERR_PTR on failure.
1048  */
mock_drm_getfile(struct drm_minor * minor,unsigned int flags)1049 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
1050 {
1051 	struct drm_device *dev = minor->dev;
1052 	struct drm_file *priv;
1053 	struct file *file;
1054 
1055 	priv = drm_file_alloc(minor);
1056 	if (IS_ERR(priv))
1057 		return ERR_CAST(priv);
1058 
1059 	file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
1060 	if (IS_ERR(file)) {
1061 		drm_file_free(priv);
1062 		return file;
1063 	}
1064 
1065 	/* Everyone shares a single global address space */
1066 	file->f_mapping = dev->anon_inode->i_mapping;
1067 
1068 	drm_dev_get(dev);
1069 	priv->filp = file;
1070 
1071 	return file;
1072 }
1073 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);
1074