xref: /openbsd/sys/dev/pci/drm/drm_file.c (revision 9ae055aa)
17f4dd379Sjsg /*
27f4dd379Sjsg  * \author Rickard E. (Rik) Faith <faith@valinux.com>
37f4dd379Sjsg  * \author Daryll Strauss <daryll@valinux.com>
47f4dd379Sjsg  * \author Gareth Hughes <gareth@valinux.com>
57f4dd379Sjsg  */
67f4dd379Sjsg 
77f4dd379Sjsg /*
87f4dd379Sjsg  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
97f4dd379Sjsg  *
107f4dd379Sjsg  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
117f4dd379Sjsg  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
127f4dd379Sjsg  * All Rights Reserved.
137f4dd379Sjsg  *
147f4dd379Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
157f4dd379Sjsg  * copy of this software and associated documentation files (the "Software"),
167f4dd379Sjsg  * to deal in the Software without restriction, including without limitation
177f4dd379Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
187f4dd379Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
197f4dd379Sjsg  * Software is furnished to do so, subject to the following conditions:
207f4dd379Sjsg  *
217f4dd379Sjsg  * The above copyright notice and this permission notice (including the next
227f4dd379Sjsg  * paragraph) shall be included in all copies or substantial portions of the
237f4dd379Sjsg  * Software.
247f4dd379Sjsg  *
257f4dd379Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
267f4dd379Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
277f4dd379Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
287f4dd379Sjsg  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
297f4dd379Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
307f4dd379Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
317f4dd379Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
327f4dd379Sjsg  */
337f4dd379Sjsg 
34c349dbc7Sjsg #include <linux/anon_inodes.h>
35c349dbc7Sjsg #include <linux/dma-fence.h>
36c349dbc7Sjsg #include <linux/file.h>
37c349dbc7Sjsg #include <linux/module.h>
38c349dbc7Sjsg #include <linux/pci.h>
397f4dd379Sjsg #include <linux/poll.h>
407f4dd379Sjsg #include <linux/slab.h>
417f4dd379Sjsg 
427f4dd379Sjsg #include <drm/drm_client.h>
43c349dbc7Sjsg #include <drm/drm_drv.h>
447f4dd379Sjsg #include <drm/drm_file.h>
45f005ef32Sjsg #include <drm/drm_gem.h>
46c349dbc7Sjsg #include <drm/drm_print.h>
477f4dd379Sjsg 
487f4dd379Sjsg #include "drm_crtc_internal.h"
49c349dbc7Sjsg #include "drm_internal.h"
50c349dbc7Sjsg #include "drm_legacy.h"
517f4dd379Sjsg 
527f4dd379Sjsg /* from BKL pushdown */
537f4dd379Sjsg DEFINE_MUTEX(drm_global_mutex);
54c349dbc7Sjsg 
drm_dev_needs_global_mutex(struct drm_device * dev)55c349dbc7Sjsg bool drm_dev_needs_global_mutex(struct drm_device *dev)
56c349dbc7Sjsg {
57c349dbc7Sjsg 	/*
58c349dbc7Sjsg 	 * Legacy drivers rely on all kinds of BKL locking semantics, don't
59c349dbc7Sjsg 	 * bother. They also still need BKL locking for their ioctls, so better
60c349dbc7Sjsg 	 * safe than sorry.
61c349dbc7Sjsg 	 */
62c349dbc7Sjsg 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
63c349dbc7Sjsg 		return true;
64c349dbc7Sjsg 
65c349dbc7Sjsg 	/*
66c349dbc7Sjsg 	 * The deprecated ->load callback must be called after the driver is
67c349dbc7Sjsg 	 * already registered. This means such drivers rely on the BKL to make
68c349dbc7Sjsg 	 * sure an open can't proceed until the driver is actually fully set up.
69c349dbc7Sjsg 	 * Similar hilarity holds for the unload callback.
70c349dbc7Sjsg 	 */
71c349dbc7Sjsg 	if (dev->driver->load || dev->driver->unload)
72c349dbc7Sjsg 		return true;
73c349dbc7Sjsg 
74c349dbc7Sjsg 	/*
75c349dbc7Sjsg 	 * Drivers with the lastclose callback assume that it's synchronized
76c349dbc7Sjsg 	 * against concurrent opens, which again needs the BKL. The proper fix
77c349dbc7Sjsg 	 * is to use the drm_client infrastructure with proper locking for each
78c349dbc7Sjsg 	 * client.
79c349dbc7Sjsg 	 */
80c349dbc7Sjsg 	if (dev->driver->lastclose)
81c349dbc7Sjsg 		return true;
82c349dbc7Sjsg 
83c349dbc7Sjsg 	return false;
84c349dbc7Sjsg }
857f4dd379Sjsg 
867f4dd379Sjsg /**
877f4dd379Sjsg  * DOC: file operations
887f4dd379Sjsg  *
897f4dd379Sjsg  * Drivers must define the file operations structure that forms the DRM
907f4dd379Sjsg  * userspace API entry point, even though most of those operations are
917f4dd379Sjsg  * implemented in the DRM core. The resulting &struct file_operations must be
927f4dd379Sjsg  * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
937f4dd379Sjsg  * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
947f4dd379Sjsg  * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
957f4dd379Sjsg  * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
967f4dd379Sjsg  * that require 32/64 bit compatibility support must provide their own
977f4dd379Sjsg  * &file_operations.compat_ioctl handler that processes private ioctls and calls
987f4dd379Sjsg  * drm_compat_ioctl() for core ioctls.
997f4dd379Sjsg  *
1007f4dd379Sjsg  * In addition drm_read() and drm_poll() provide support for DRM events. DRM
1017f4dd379Sjsg  * events are a generic and extensible means to send asynchronous events to
1027f4dd379Sjsg  * userspace through the file descriptor. They are used to send vblank event and
1037f4dd379Sjsg  * page flip completions by the KMS API. But drivers can also use it for their
1047f4dd379Sjsg  * own needs, e.g. to signal completion of rendering.
1057f4dd379Sjsg  *
1067f4dd379Sjsg  * For the driver-side event interface see drm_event_reserve_init() and
1077f4dd379Sjsg  * drm_send_event() as the main starting points.
1087f4dd379Sjsg  *
1097f4dd379Sjsg  * The memory mapping implementation will vary depending on how the driver
1107f4dd379Sjsg  * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
1117f4dd379Sjsg  * function, modern drivers should use one of the provided memory-manager
1125ca02815Sjsg  * specific implementations. For GEM-based drivers this is drm_gem_mmap().
1137f4dd379Sjsg  *
1147f4dd379Sjsg  * No other file operations are supported by the DRM userspace API. Overall the
1157f4dd379Sjsg  * following is an example &file_operations structure::
1167f4dd379Sjsg  *
1177f4dd379Sjsg  *     static const example_drm_fops = {
1187f4dd379Sjsg  *             .owner = THIS_MODULE,
1197f4dd379Sjsg  *             .open = drm_open,
1207f4dd379Sjsg  *             .release = drm_release,
1217f4dd379Sjsg  *             .unlocked_ioctl = drm_ioctl,
1227f4dd379Sjsg  *             .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
1237f4dd379Sjsg  *             .poll = drm_poll,
1247f4dd379Sjsg  *             .read = drm_read,
1257f4dd379Sjsg  *             .llseek = no_llseek,
1267f4dd379Sjsg  *             .mmap = drm_gem_mmap,
1277f4dd379Sjsg  *     };
1287f4dd379Sjsg  *
1297f4dd379Sjsg  * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
1301bb76ff1Sjsg  * DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS() macro to make this
1317f4dd379Sjsg  * simpler.
1327f4dd379Sjsg  *
1337f4dd379Sjsg  * The driver's &file_operations must be stored in &drm_driver.fops.
1347f4dd379Sjsg  *
1357f4dd379Sjsg  * For driver-private IOCTL handling see the more detailed discussion in
1367f4dd379Sjsg  * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
1377f4dd379Sjsg  */
1387f4dd379Sjsg 
1397f4dd379Sjsg /**
1407f4dd379Sjsg  * drm_file_alloc - allocate file context
1417f4dd379Sjsg  * @minor: minor to allocate on
1427f4dd379Sjsg  *
1437f4dd379Sjsg  * This allocates a new DRM file context. It is not linked into any context and
1447f4dd379Sjsg  * can be used by the caller freely. Note that the context keeps a pointer to
1457f4dd379Sjsg  * @minor, so it must be freed before @minor is.
1467f4dd379Sjsg  *
1477f4dd379Sjsg  * RETURNS:
1487f4dd379Sjsg  * Pointer to newly allocated context, ERR_PTR on failure.
1497f4dd379Sjsg  */
drm_file_alloc(struct drm_minor * minor)1507f4dd379Sjsg struct drm_file *drm_file_alloc(struct drm_minor *minor)
1517f4dd379Sjsg {
152f005ef32Sjsg 	static atomic64_t ident = ATOMIC64_INIT(0);
1537f4dd379Sjsg 	struct drm_device *dev = minor->dev;
1547f4dd379Sjsg 	struct drm_file *file;
1557f4dd379Sjsg 	int ret;
1567f4dd379Sjsg 
1577f4dd379Sjsg 	file = kzalloc(sizeof(*file), GFP_KERNEL);
1587f4dd379Sjsg 	if (!file)
1597f4dd379Sjsg 		return ERR_PTR(-ENOMEM);
1607f4dd379Sjsg 
161f005ef32Sjsg 	/* Get a unique identifier for fdinfo: */
162f005ef32Sjsg 	file->client_id = atomic64_inc_return(&ident);
163c349dbc7Sjsg #ifdef __linux__
164f005ef32Sjsg 	rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
165c349dbc7Sjsg #endif
1667f4dd379Sjsg 	file->minor = minor;
1677f4dd379Sjsg 
1687f4dd379Sjsg 	/* for compatibility root is always authenticated */
1697f4dd379Sjsg 	file->authenticated = capable(CAP_SYS_ADMIN);
1707f4dd379Sjsg 
1717f4dd379Sjsg 	INIT_LIST_HEAD(&file->lhead);
1727f4dd379Sjsg 	INIT_LIST_HEAD(&file->fbs);
173c349dbc7Sjsg 	rw_init(&file->fbs_lock, "fbslk");
1747f4dd379Sjsg 	INIT_LIST_HEAD(&file->blobs);
1757f4dd379Sjsg 	INIT_LIST_HEAD(&file->pending_event_list);
1767f4dd379Sjsg 	INIT_LIST_HEAD(&file->event_list);
1777f4dd379Sjsg 	init_waitqueue_head(&file->event_wait);
1787f4dd379Sjsg 	file->event_space = 4096; /* set aside 4k for event buffer */
1797f4dd379Sjsg 
180ebdefde7Sjsg 	mtx_init(&file->master_lookup_lock, IPL_NONE);
181c349dbc7Sjsg 	rw_init(&file->event_read_lock, "evread");
1827f4dd379Sjsg 
1837f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_GEM))
1847f4dd379Sjsg 		drm_gem_open(dev, file);
1857f4dd379Sjsg 
1867f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1877f4dd379Sjsg 		drm_syncobj_open(file);
1887f4dd379Sjsg 
1897f4dd379Sjsg 	drm_prime_init_file_private(&file->prime);
1907f4dd379Sjsg 
1917f4dd379Sjsg 	if (dev->driver->open) {
1927f4dd379Sjsg 		ret = dev->driver->open(dev, file);
1937f4dd379Sjsg 		if (ret < 0)
1947f4dd379Sjsg 			goto out_prime_destroy;
1957f4dd379Sjsg 	}
1967f4dd379Sjsg 
1977f4dd379Sjsg 	return file;
1987f4dd379Sjsg 
1997f4dd379Sjsg out_prime_destroy:
2007f4dd379Sjsg 	drm_prime_destroy_file_private(&file->prime);
2017f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
2027f4dd379Sjsg 		drm_syncobj_release(file);
2037f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_GEM))
2047f4dd379Sjsg 		drm_gem_release(dev, file);
205f005ef32Sjsg 	put_pid(rcu_access_pointer(file->pid));
2067f4dd379Sjsg 	kfree(file);
2077f4dd379Sjsg 
2087f4dd379Sjsg 	return ERR_PTR(ret);
2097f4dd379Sjsg }
2107f4dd379Sjsg 
drm_events_release(struct drm_file * file_priv)211c349dbc7Sjsg static void drm_events_release(struct drm_file *file_priv)
2127f4dd379Sjsg {
213c349dbc7Sjsg 	struct drm_device *dev = file_priv->minor->dev;
2147f4dd379Sjsg 	struct drm_pending_event *e, *et;
2157f4dd379Sjsg 	unsigned long flags;
2167f4dd379Sjsg 
2177f4dd379Sjsg 	spin_lock_irqsave(&dev->event_lock, flags);
2187f4dd379Sjsg 
2197f4dd379Sjsg 	/* Unlink pending events */
2207f4dd379Sjsg 	list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
2217f4dd379Sjsg 				 pending_link) {
2227f4dd379Sjsg 		list_del(&e->pending_link);
2237f4dd379Sjsg 		e->file_priv = NULL;
2247f4dd379Sjsg 	}
2257f4dd379Sjsg 
2267f4dd379Sjsg 	/* Remove unconsumed events */
2277f4dd379Sjsg 	list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
2287f4dd379Sjsg 		list_del(&e->link);
2297f4dd379Sjsg 		kfree(e);
2307f4dd379Sjsg 	}
2317f4dd379Sjsg 
2327f4dd379Sjsg 	spin_unlock_irqrestore(&dev->event_lock, flags);
2337f4dd379Sjsg }
2347f4dd379Sjsg 
2357f4dd379Sjsg /**
2367f4dd379Sjsg  * drm_file_free - free file context
2377f4dd379Sjsg  * @file: context to free, or NULL
2387f4dd379Sjsg  *
2397f4dd379Sjsg  * This destroys and deallocates a DRM file context previously allocated via
2407f4dd379Sjsg  * drm_file_alloc(). The caller must make sure to unlink it from any contexts
2417f4dd379Sjsg  * before calling this.
2427f4dd379Sjsg  *
2437f4dd379Sjsg  * If NULL is passed, this is a no-op.
2447f4dd379Sjsg  */
drm_file_free(struct drm_file * file)2457f4dd379Sjsg void drm_file_free(struct drm_file *file)
2467f4dd379Sjsg {
2477f4dd379Sjsg 	struct drm_device *dev;
2487f4dd379Sjsg 
2497f4dd379Sjsg 	if (!file)
2507f4dd379Sjsg 		return;
2517f4dd379Sjsg 
2527f4dd379Sjsg 	dev = file->minor->dev;
2537f4dd379Sjsg 
254c349dbc7Sjsg #ifdef __linux__
255f005ef32Sjsg 	drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n",
256ad8b1aafSjsg 		     current->comm, task_pid_nr(current),
2577f4dd379Sjsg 		     (long)old_encode_dev(file->minor->kdev->devt),
258c349dbc7Sjsg 		     atomic_read(&dev->open_count));
259c349dbc7Sjsg #else
260f005ef32Sjsg 	drm_dbg_core(dev, "pid=%d, dev=0x%lx, open_count=%d\n",
261c349dbc7Sjsg 		     curproc->p_p->ps_pid, (long)&dev->dev,
262c349dbc7Sjsg 		     atomic_read(&dev->open_count));
263c349dbc7Sjsg #endif
2647f4dd379Sjsg 
2655ca02815Sjsg #ifdef CONFIG_DRM_LEGACY
2667f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
2677f4dd379Sjsg 	    dev->driver->preclose)
2687f4dd379Sjsg 		dev->driver->preclose(dev, file);
2695ca02815Sjsg #endif
2707f4dd379Sjsg 
2717f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
2727f4dd379Sjsg 		drm_legacy_lock_release(dev, file->filp);
2737f4dd379Sjsg 
2747f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
2757f4dd379Sjsg 		drm_legacy_reclaim_buffers(dev, file);
2767f4dd379Sjsg 
2777f4dd379Sjsg 	drm_events_release(file);
2787f4dd379Sjsg 
2797f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
2807f4dd379Sjsg 		drm_fb_release(file);
2817f4dd379Sjsg 		drm_property_destroy_user_blobs(dev, file);
2827f4dd379Sjsg 	}
2837f4dd379Sjsg 
2847f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
2857f4dd379Sjsg 		drm_syncobj_release(file);
2867f4dd379Sjsg 
2877f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_GEM))
2887f4dd379Sjsg 		drm_gem_release(dev, file);
2897f4dd379Sjsg 
2907f4dd379Sjsg 	drm_legacy_ctxbitmap_flush(dev, file);
2917f4dd379Sjsg 
2927f4dd379Sjsg 	if (drm_is_primary_client(file))
2937f4dd379Sjsg 		drm_master_release(file);
2947f4dd379Sjsg 
2957f4dd379Sjsg 	if (dev->driver->postclose)
2967f4dd379Sjsg 		dev->driver->postclose(dev, file);
2977f4dd379Sjsg 
2987f4dd379Sjsg 	drm_prime_destroy_file_private(&file->prime);
2997f4dd379Sjsg 
3007f4dd379Sjsg 	WARN_ON(!list_empty(&file->event_list));
3017f4dd379Sjsg 
302f005ef32Sjsg 	put_pid(rcu_access_pointer(file->pid));
3037f4dd379Sjsg 	kfree(file);
3047f4dd379Sjsg }
3057f4dd379Sjsg 
306c349dbc7Sjsg #ifdef __linux__
307c349dbc7Sjsg 
drm_close_helper(struct file * filp)308c349dbc7Sjsg static void drm_close_helper(struct file *filp)
3097f4dd379Sjsg {
310c349dbc7Sjsg 	struct drm_file *file_priv = filp->private_data;
311c349dbc7Sjsg 	struct drm_device *dev = file_priv->minor->dev;
3127f4dd379Sjsg 
313c349dbc7Sjsg 	mutex_lock(&dev->filelist_mutex);
314c349dbc7Sjsg 	list_del(&file_priv->lhead);
315c349dbc7Sjsg 	mutex_unlock(&dev->filelist_mutex);
316c349dbc7Sjsg 
317c349dbc7Sjsg 	drm_file_free(file_priv);
3187f4dd379Sjsg }
3197f4dd379Sjsg 
3207f4dd379Sjsg /*
3217f4dd379Sjsg  * Check whether DRI will run on this CPU.
3227f4dd379Sjsg  *
3237f4dd379Sjsg  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
3247f4dd379Sjsg  */
drm_cpu_valid(void)3257f4dd379Sjsg static int drm_cpu_valid(void)
3267f4dd379Sjsg {
3277f4dd379Sjsg #if defined(__sparc__) && !defined(__sparc_v9__)
3287f4dd379Sjsg 	return 0;		/* No cmpxchg before v9 sparc. */
3297f4dd379Sjsg #endif
3307f4dd379Sjsg 	return 1;
3317f4dd379Sjsg }
3327f4dd379Sjsg 
3337f4dd379Sjsg /*
334c349dbc7Sjsg  * Called whenever a process opens a drm node
3357f4dd379Sjsg  *
3367f4dd379Sjsg  * \param filp file pointer.
3377f4dd379Sjsg  * \param minor acquired minor-object.
3387f4dd379Sjsg  * \return zero on success or a negative number on failure.
3397f4dd379Sjsg  *
3407f4dd379Sjsg  * Creates and initializes a drm_file structure for the file private data in \p
3417f4dd379Sjsg  * filp and add it into the double linked list in \p dev.
3427f4dd379Sjsg  */
drm_open_helper(struct file * filp,struct drm_minor * minor)343f005ef32Sjsg int drm_open_helper(struct file *filp, struct drm_minor *minor)
3447f4dd379Sjsg {
3457f4dd379Sjsg 	struct drm_device *dev = minor->dev;
3467f4dd379Sjsg 	struct drm_file *priv;
3477f4dd379Sjsg 	int ret;
3487f4dd379Sjsg 
3497f4dd379Sjsg 	if (filp->f_flags & O_EXCL)
3507f4dd379Sjsg 		return -EBUSY;	/* No exclusive opens */
3517f4dd379Sjsg 	if (!drm_cpu_valid())
3527f4dd379Sjsg 		return -EINVAL;
353ad8b1aafSjsg 	if (dev->switch_power_state != DRM_SWITCH_POWER_ON &&
354ad8b1aafSjsg 	    dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
3557f4dd379Sjsg 		return -EINVAL;
3567f4dd379Sjsg 
357f005ef32Sjsg 	drm_dbg_core(dev, "comm=\"%s\", pid=%d, minor=%d\n",
358f005ef32Sjsg 		     current->comm, task_pid_nr(current), minor->index);
3597f4dd379Sjsg 
3607f4dd379Sjsg 	priv = drm_file_alloc(minor);
3617f4dd379Sjsg 	if (IS_ERR(priv))
3627f4dd379Sjsg 		return PTR_ERR(priv);
3637f4dd379Sjsg 
3647f4dd379Sjsg 	if (drm_is_primary_client(priv)) {
3657f4dd379Sjsg 		ret = drm_master_open(priv);
3667f4dd379Sjsg 		if (ret) {
3677f4dd379Sjsg 			drm_file_free(priv);
3687f4dd379Sjsg 			return ret;
3697f4dd379Sjsg 		}
3707f4dd379Sjsg 	}
3717f4dd379Sjsg 
3727f4dd379Sjsg 	filp->private_data = priv;
3737f4dd379Sjsg 	filp->f_mode |= FMODE_UNSIGNED_OFFSET;
3747f4dd379Sjsg 	priv->filp = filp;
3757f4dd379Sjsg 
3767f4dd379Sjsg 	mutex_lock(&dev->filelist_mutex);
3777f4dd379Sjsg 	list_add(&priv->lhead, &dev->filelist);
3787f4dd379Sjsg 	mutex_unlock(&dev->filelist_mutex);
3797f4dd379Sjsg 
3805ca02815Sjsg #ifdef CONFIG_DRM_LEGACY
3817f4dd379Sjsg #ifdef __alpha__
3827f4dd379Sjsg 	/*
3837f4dd379Sjsg 	 * Default the hose
3847f4dd379Sjsg 	 */
3857f4dd379Sjsg 	if (!dev->hose) {
3867f4dd379Sjsg 		struct pci_dev *pci_dev;
387ad8b1aafSjsg 
3887f4dd379Sjsg 		pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
3897f4dd379Sjsg 		if (pci_dev) {
3907f4dd379Sjsg 			dev->hose = pci_dev->sysdata;
3917f4dd379Sjsg 			pci_dev_put(pci_dev);
3927f4dd379Sjsg 		}
3937f4dd379Sjsg 		if (!dev->hose) {
3947f4dd379Sjsg 			struct pci_bus *b = list_entry(pci_root_buses.next,
3957f4dd379Sjsg 				struct pci_bus, node);
3967f4dd379Sjsg 			if (b)
3977f4dd379Sjsg 				dev->hose = b->sysdata;
3987f4dd379Sjsg 		}
3997f4dd379Sjsg 	}
4007f4dd379Sjsg #endif
4015ca02815Sjsg #endif
4027f4dd379Sjsg 
4037f4dd379Sjsg 	return 0;
4047f4dd379Sjsg }
405f005ef32Sjsg 
406c349dbc7Sjsg #endif /* __linux__ */
4077f4dd379Sjsg 
408c349dbc7Sjsg /**
409c349dbc7Sjsg  * drm_open - open method for DRM file
410c349dbc7Sjsg  * @inode: device inode
411c349dbc7Sjsg  * @filp: file pointer.
412c349dbc7Sjsg  *
413c349dbc7Sjsg  * This function must be used by drivers as their &file_operations.open method.
414c349dbc7Sjsg  * It looks up the correct DRM device and instantiates all the per-file
415c349dbc7Sjsg  * resources for it. It also calls the &drm_driver.open driver callback.
416c349dbc7Sjsg  *
417c349dbc7Sjsg  * RETURNS:
418c349dbc7Sjsg  *
4195ca02815Sjsg  * 0 on success or negative errno value on failure.
420c349dbc7Sjsg  */
421c349dbc7Sjsg #ifdef __linux__
drm_open(struct inode * inode,struct file * filp)422c349dbc7Sjsg int drm_open(struct inode *inode, struct file *filp)
4237f4dd379Sjsg {
424c349dbc7Sjsg 	struct drm_device *dev;
425c349dbc7Sjsg 	struct drm_minor *minor;
426c349dbc7Sjsg 	int retcode;
427c349dbc7Sjsg 	int need_setup = 0;
4287f4dd379Sjsg 
429*9ae055aaSjsg 	minor = drm_minor_acquire(&drm_minors_xa, iminor(inode));
430c349dbc7Sjsg 	if (IS_ERR(minor))
431c349dbc7Sjsg 		return PTR_ERR(minor);
4327f4dd379Sjsg 
433c349dbc7Sjsg 	dev = minor->dev;
434c349dbc7Sjsg 	if (drm_dev_needs_global_mutex(dev))
435c349dbc7Sjsg 		mutex_lock(&drm_global_mutex);
4367f4dd379Sjsg 
437c349dbc7Sjsg 	if (!atomic_fetch_inc(&dev->open_count))
438c349dbc7Sjsg 		need_setup = 1;
4397f4dd379Sjsg 
440c349dbc7Sjsg 	/* share address_space across all char-devs of a single device */
441c349dbc7Sjsg 	filp->f_mapping = dev->anon_inode->i_mapping;
4427f4dd379Sjsg 
443c349dbc7Sjsg 	retcode = drm_open_helper(filp, minor);
444c349dbc7Sjsg 	if (retcode)
445c349dbc7Sjsg 		goto err_undo;
446c349dbc7Sjsg 	if (need_setup) {
447c349dbc7Sjsg 		retcode = drm_legacy_setup(dev);
448c349dbc7Sjsg 		if (retcode) {
449c349dbc7Sjsg 			drm_close_helper(filp);
450c349dbc7Sjsg 			goto err_undo;
4517f4dd379Sjsg 		}
452c349dbc7Sjsg 	}
453c349dbc7Sjsg 
454c349dbc7Sjsg 	if (drm_dev_needs_global_mutex(dev))
455c349dbc7Sjsg 		mutex_unlock(&drm_global_mutex);
456c349dbc7Sjsg 
457c349dbc7Sjsg 	return 0;
458c349dbc7Sjsg 
459c349dbc7Sjsg err_undo:
460c349dbc7Sjsg 	atomic_dec(&dev->open_count);
461c349dbc7Sjsg 	if (drm_dev_needs_global_mutex(dev))
462c349dbc7Sjsg 		mutex_unlock(&drm_global_mutex);
463c349dbc7Sjsg 	drm_minor_release(minor);
464c349dbc7Sjsg 	return retcode;
465c349dbc7Sjsg }
466c349dbc7Sjsg EXPORT_SYMBOL(drm_open);
467c349dbc7Sjsg #endif
4687f4dd379Sjsg 
drm_lastclose(struct drm_device * dev)4697f4dd379Sjsg void drm_lastclose(struct drm_device * dev)
4707f4dd379Sjsg {
471f005ef32Sjsg 	drm_dbg_core(dev, "\n");
4727f4dd379Sjsg 
4737f4dd379Sjsg 	if (dev->driver->lastclose)
4747f4dd379Sjsg 		dev->driver->lastclose(dev);
475f005ef32Sjsg 	drm_dbg_core(dev, "driver lastclose completed\n");
4767f4dd379Sjsg 
4777f4dd379Sjsg 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
4787f4dd379Sjsg 		drm_legacy_dev_reinit(dev);
4797f4dd379Sjsg 
4807f4dd379Sjsg 	drm_client_dev_restore(dev);
4817f4dd379Sjsg }
4827f4dd379Sjsg 
4837f4dd379Sjsg /**
4847f4dd379Sjsg  * drm_release - release method for DRM file
4857f4dd379Sjsg  * @inode: device inode
4867f4dd379Sjsg  * @filp: file pointer.
4877f4dd379Sjsg  *
4887f4dd379Sjsg  * This function must be used by drivers as their &file_operations.release
4897f4dd379Sjsg  * method. It frees any resources associated with the open file, and calls the
4907f4dd379Sjsg  * &drm_driver.postclose driver callback. If this is the last open file for the
4917f4dd379Sjsg  * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
4927f4dd379Sjsg  *
4937f4dd379Sjsg  * RETURNS:
4947f4dd379Sjsg  *
4957f4dd379Sjsg  * Always succeeds and returns 0.
4967f4dd379Sjsg  */
drm_release(struct inode * inode,struct file * filp)4977f4dd379Sjsg int drm_release(struct inode *inode, struct file *filp)
4987f4dd379Sjsg {
499c349dbc7Sjsg 	STUB();
500c349dbc7Sjsg 	return -ENOSYS;
501c349dbc7Sjsg #ifdef notyet
5027f4dd379Sjsg 	struct drm_file *file_priv = filp->private_data;
5037f4dd379Sjsg 	struct drm_minor *minor = file_priv->minor;
5047f4dd379Sjsg 	struct drm_device *dev = minor->dev;
5057f4dd379Sjsg 
506c349dbc7Sjsg 	if (drm_dev_needs_global_mutex(dev))
5077f4dd379Sjsg 		mutex_lock(&drm_global_mutex);
5087f4dd379Sjsg 
509f005ef32Sjsg 	drm_dbg_core(dev, "open_count = %d\n", atomic_read(&dev->open_count));
5107f4dd379Sjsg 
511c349dbc7Sjsg 	drm_close_helper(filp);
5127f4dd379Sjsg 
513c349dbc7Sjsg 	if (atomic_dec_and_test(&dev->open_count))
5147f4dd379Sjsg 		drm_lastclose(dev);
5157c4f8494Sjsg 
516c349dbc7Sjsg 	if (drm_dev_needs_global_mutex(dev))
5177f4dd379Sjsg 		mutex_unlock(&drm_global_mutex);
5187f4dd379Sjsg 
5197f4dd379Sjsg 	drm_minor_release(minor);
5207f4dd379Sjsg 
5217f4dd379Sjsg 	return 0;
522c349dbc7Sjsg #endif
5237f4dd379Sjsg }
5247f4dd379Sjsg EXPORT_SYMBOL(drm_release);
5257f4dd379Sjsg 
drm_file_update_pid(struct drm_file * filp)526f005ef32Sjsg void drm_file_update_pid(struct drm_file *filp)
527f005ef32Sjsg {
528f005ef32Sjsg #ifdef notyet
529f005ef32Sjsg 	struct drm_device *dev;
530f005ef32Sjsg 	struct pid *pid, *old;
531f005ef32Sjsg #endif
532f005ef32Sjsg 
533f005ef32Sjsg 	/*
534f005ef32Sjsg 	 * Master nodes need to keep the original ownership in order for
535f005ef32Sjsg 	 * drm_master_check_perm to keep working correctly. (See comment in
536f005ef32Sjsg 	 * drm_auth.c.)
537f005ef32Sjsg 	 */
538f005ef32Sjsg 	if (filp->was_master)
539f005ef32Sjsg 		return;
540f005ef32Sjsg 
541f005ef32Sjsg 	STUB();
542f005ef32Sjsg #ifdef notyet
543f005ef32Sjsg 	pid = task_tgid(current);
544f005ef32Sjsg 
545f005ef32Sjsg 	/*
546f005ef32Sjsg 	 * Quick unlocked check since the model is a single handover followed by
547f005ef32Sjsg 	 * exclusive repeated use.
548f005ef32Sjsg 	 */
549f005ef32Sjsg 	if (pid == rcu_access_pointer(filp->pid))
550f005ef32Sjsg 		return;
551f005ef32Sjsg 
552f005ef32Sjsg 	dev = filp->minor->dev;
553f005ef32Sjsg 	mutex_lock(&dev->filelist_mutex);
55468796300Sjsg 	get_pid(pid);
555f005ef32Sjsg 	old = rcu_replace_pointer(filp->pid, pid, 1);
556f005ef32Sjsg 	mutex_unlock(&dev->filelist_mutex);
557f005ef32Sjsg 
558f005ef32Sjsg 	synchronize_rcu();
559f005ef32Sjsg 	put_pid(old);
560f005ef32Sjsg #endif
561f005ef32Sjsg }
562f005ef32Sjsg 
5637f4dd379Sjsg /**
564c349dbc7Sjsg  * drm_release_noglobal - release method for DRM file
565c349dbc7Sjsg  * @inode: device inode
566c349dbc7Sjsg  * @filp: file pointer.
567c349dbc7Sjsg  *
568c349dbc7Sjsg  * This function may be used by drivers as their &file_operations.release
569c349dbc7Sjsg  * method. It frees any resources associated with the open file prior to taking
570c349dbc7Sjsg  * the drm_global_mutex, which then calls the &drm_driver.postclose driver
571c349dbc7Sjsg  * callback. If this is the last open file for the DRM device also proceeds to
572c349dbc7Sjsg  * call the &drm_driver.lastclose driver callback.
573c349dbc7Sjsg  *
574c349dbc7Sjsg  * RETURNS:
575c349dbc7Sjsg  *
576c349dbc7Sjsg  * Always succeeds and returns 0.
577c349dbc7Sjsg  */
drm_release_noglobal(struct inode * inode,struct file * filp)578c349dbc7Sjsg int drm_release_noglobal(struct inode *inode, struct file *filp)
579c349dbc7Sjsg {
580c349dbc7Sjsg 	STUB();
581c349dbc7Sjsg 	return -ENOSYS;
582c349dbc7Sjsg #ifdef notyet
583c349dbc7Sjsg 	struct drm_file *file_priv = filp->private_data;
584c349dbc7Sjsg 	struct drm_minor *minor = file_priv->minor;
585c349dbc7Sjsg 	struct drm_device *dev = minor->dev;
586c349dbc7Sjsg 
587c349dbc7Sjsg 	drm_close_helper(filp);
588c349dbc7Sjsg 
589c349dbc7Sjsg 	if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
590c349dbc7Sjsg 		drm_lastclose(dev);
591c349dbc7Sjsg 		mutex_unlock(&drm_global_mutex);
592c349dbc7Sjsg 	}
593c349dbc7Sjsg 
594c349dbc7Sjsg 	drm_minor_release(minor);
595c349dbc7Sjsg 
596c349dbc7Sjsg 	return 0;
597c349dbc7Sjsg #endif
598c349dbc7Sjsg }
599c349dbc7Sjsg EXPORT_SYMBOL(drm_release_noglobal);
600c349dbc7Sjsg 
601c349dbc7Sjsg /**
6027f4dd379Sjsg  * drm_read - read method for DRM file
6037f4dd379Sjsg  * @filp: file pointer
6047f4dd379Sjsg  * @buffer: userspace destination pointer for the read
6057f4dd379Sjsg  * @count: count in bytes to read
6067f4dd379Sjsg  * @offset: offset to read
6077f4dd379Sjsg  *
6087f4dd379Sjsg  * This function must be used by drivers as their &file_operations.read
6095ca02815Sjsg  * method if they use DRM events for asynchronous signalling to userspace.
6107f4dd379Sjsg  * Since events are used by the KMS API for vblank and page flip completion this
6117f4dd379Sjsg  * means all modern display drivers must use it.
6127f4dd379Sjsg  *
6131bb76ff1Sjsg  * @offset is ignored, DRM events are read like a pipe. Polling support is
6147f4dd379Sjsg  * provided by drm_poll().
6157f4dd379Sjsg  *
6167f4dd379Sjsg  * This function will only ever read a full event. Therefore userspace must
6177f4dd379Sjsg  * supply a big enough buffer to fit any event to ensure forward progress. Since
6187f4dd379Sjsg  * the maximum event space is currently 4K it's recommended to just use that for
6197f4dd379Sjsg  * safety.
6207f4dd379Sjsg  *
6217f4dd379Sjsg  * RETURNS:
6227f4dd379Sjsg  *
6237f4dd379Sjsg  * Number of bytes read (always aligned to full events, and can be 0) or a
6247f4dd379Sjsg  * negative error code on failure.
6257f4dd379Sjsg  */
drm_read(struct file * filp,char __user * buffer,size_t count,loff_t * offset)6267f4dd379Sjsg ssize_t drm_read(struct file *filp, char __user *buffer,
6277f4dd379Sjsg 		 size_t count, loff_t *offset)
6287f4dd379Sjsg {
629c349dbc7Sjsg 	STUB();
630c349dbc7Sjsg 	return -ENOSYS;
631c349dbc7Sjsg #ifdef notyet
6327f4dd379Sjsg 	struct drm_file *file_priv = filp->private_data;
6337f4dd379Sjsg 	struct drm_device *dev = file_priv->minor->dev;
6347f4dd379Sjsg 	ssize_t ret;
6357f4dd379Sjsg 
6367f4dd379Sjsg 	ret = mutex_lock_interruptible(&file_priv->event_read_lock);
6377f4dd379Sjsg 	if (ret)
6387f4dd379Sjsg 		return ret;
6397f4dd379Sjsg 
6407f4dd379Sjsg 	for (;;) {
6417f4dd379Sjsg 		struct drm_pending_event *e = NULL;
6427f4dd379Sjsg 
6437f4dd379Sjsg 		spin_lock_irq(&dev->event_lock);
6447f4dd379Sjsg 		if (!list_empty(&file_priv->event_list)) {
6457f4dd379Sjsg 			e = list_first_entry(&file_priv->event_list,
6467f4dd379Sjsg 					struct drm_pending_event, link);
6477f4dd379Sjsg 			file_priv->event_space += e->event->length;
6487f4dd379Sjsg 			list_del(&e->link);
6497f4dd379Sjsg 		}
6507f4dd379Sjsg 		spin_unlock_irq(&dev->event_lock);
6517f4dd379Sjsg 
6527f4dd379Sjsg 		if (e == NULL) {
6537f4dd379Sjsg 			if (ret)
6547f4dd379Sjsg 				break;
6557f4dd379Sjsg 
6567f4dd379Sjsg 			if (filp->f_flags & O_NONBLOCK) {
6577f4dd379Sjsg 				ret = -EAGAIN;
6587f4dd379Sjsg 				break;
6597f4dd379Sjsg 			}
6607f4dd379Sjsg 
6617f4dd379Sjsg 			mutex_unlock(&file_priv->event_read_lock);
6627f4dd379Sjsg 			ret = wait_event_interruptible(file_priv->event_wait,
6637f4dd379Sjsg 						       !list_empty(&file_priv->event_list));
6647f4dd379Sjsg 			if (ret >= 0)
6657f4dd379Sjsg 				ret = mutex_lock_interruptible(&file_priv->event_read_lock);
6667f4dd379Sjsg 			if (ret)
6677f4dd379Sjsg 				return ret;
6687f4dd379Sjsg 		} else {
6697f4dd379Sjsg 			unsigned length = e->event->length;
6707f4dd379Sjsg 
6717f4dd379Sjsg 			if (length > count - ret) {
6727f4dd379Sjsg put_back_event:
6737f4dd379Sjsg 				spin_lock_irq(&dev->event_lock);
6747f4dd379Sjsg 				file_priv->event_space -= length;
6757f4dd379Sjsg 				list_add(&e->link, &file_priv->event_list);
6767f4dd379Sjsg 				spin_unlock_irq(&dev->event_lock);
677ad8b1aafSjsg 				wake_up_interruptible_poll(&file_priv->event_wait,
678ad8b1aafSjsg 					EPOLLIN | EPOLLRDNORM);
6797f4dd379Sjsg 				break;
6807f4dd379Sjsg 			}
6817f4dd379Sjsg 
6827f4dd379Sjsg 			if (copy_to_user(buffer + ret, e->event, length)) {
6837f4dd379Sjsg 				if (ret == 0)
6847f4dd379Sjsg 					ret = -EFAULT;
6857f4dd379Sjsg 				goto put_back_event;
6867f4dd379Sjsg 			}
6877f4dd379Sjsg 
6887f4dd379Sjsg 			ret += length;
6897f4dd379Sjsg 			kfree(e);
6907f4dd379Sjsg 		}
6917f4dd379Sjsg 	}
6927f4dd379Sjsg 	mutex_unlock(&file_priv->event_read_lock);
6937f4dd379Sjsg 
6947f4dd379Sjsg 	return ret;
695c349dbc7Sjsg #endif
6967f4dd379Sjsg }
6977f4dd379Sjsg EXPORT_SYMBOL(drm_read);
6987f4dd379Sjsg 
699c349dbc7Sjsg #ifdef notyet
7007f4dd379Sjsg /**
7017f4dd379Sjsg  * drm_poll - poll method for DRM file
7027f4dd379Sjsg  * @filp: file pointer
7037f4dd379Sjsg  * @wait: poll waiter table
7047f4dd379Sjsg  *
7057f4dd379Sjsg  * This function must be used by drivers as their &file_operations.read method
7065ca02815Sjsg  * if they use DRM events for asynchronous signalling to userspace.  Since
7077f4dd379Sjsg  * events are used by the KMS API for vblank and page flip completion this means
7087f4dd379Sjsg  * all modern display drivers must use it.
7097f4dd379Sjsg  *
7107f4dd379Sjsg  * See also drm_read().
7117f4dd379Sjsg  *
7127f4dd379Sjsg  * RETURNS:
7137f4dd379Sjsg  *
7147f4dd379Sjsg  * Mask of POLL flags indicating the current status of the file.
7157f4dd379Sjsg  */
drm_poll(struct file * filp,struct poll_table_struct * wait)7167f4dd379Sjsg __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
7177f4dd379Sjsg {
7187f4dd379Sjsg 	struct drm_file *file_priv = filp->private_data;
7197f4dd379Sjsg 	__poll_t mask = 0;
7207f4dd379Sjsg 
7217f4dd379Sjsg 	poll_wait(filp, &file_priv->event_wait, wait);
7227f4dd379Sjsg 
7237f4dd379Sjsg 	if (!list_empty(&file_priv->event_list))
7247f4dd379Sjsg 		mask |= EPOLLIN | EPOLLRDNORM;
7257f4dd379Sjsg 
7267f4dd379Sjsg 	return mask;
7277f4dd379Sjsg }
7287f4dd379Sjsg EXPORT_SYMBOL(drm_poll);
7297f4dd379Sjsg #endif
7307f4dd379Sjsg 
7317f4dd379Sjsg /**
7327f4dd379Sjsg  * drm_event_reserve_init_locked - init a DRM event and reserve space for it
7337f4dd379Sjsg  * @dev: DRM device
7347f4dd379Sjsg  * @file_priv: DRM file private data
7357f4dd379Sjsg  * @p: tracking structure for the pending event
7367f4dd379Sjsg  * @e: actual event data to deliver to userspace
7377f4dd379Sjsg  *
7387f4dd379Sjsg  * This function prepares the passed in event for eventual delivery. If the event
7397f4dd379Sjsg  * doesn't get delivered (because the IOCTL fails later on, before queuing up
7407f4dd379Sjsg  * anything) then the even must be cancelled and freed using
7417f4dd379Sjsg  * drm_event_cancel_free(). Successfully initialized events should be sent out
7427f4dd379Sjsg  * using drm_send_event() or drm_send_event_locked() to signal completion of the
7437f4dd379Sjsg  * asynchronous event to userspace.
7447f4dd379Sjsg  *
7457f4dd379Sjsg  * If callers embedded @p into a larger structure it must be allocated with
7467f4dd379Sjsg  * kmalloc and @p must be the first member element.
7477f4dd379Sjsg  *
7487f4dd379Sjsg  * This is the locked version of drm_event_reserve_init() for callers which
7497f4dd379Sjsg  * already hold &drm_device.event_lock.
7507f4dd379Sjsg  *
7517f4dd379Sjsg  * RETURNS:
7527f4dd379Sjsg  *
7537f4dd379Sjsg  * 0 on success or a negative error code on failure.
7547f4dd379Sjsg  */
drm_event_reserve_init_locked(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)7557f4dd379Sjsg int drm_event_reserve_init_locked(struct drm_device *dev,
7567f4dd379Sjsg 				  struct drm_file *file_priv,
7577f4dd379Sjsg 				  struct drm_pending_event *p,
7587f4dd379Sjsg 				  struct drm_event *e)
7597f4dd379Sjsg {
7607f4dd379Sjsg 	if (file_priv->event_space < e->length)
7617f4dd379Sjsg 		return -ENOMEM;
7627f4dd379Sjsg 
7637f4dd379Sjsg 	file_priv->event_space -= e->length;
7647f4dd379Sjsg 
7657f4dd379Sjsg 	p->event = e;
7667f4dd379Sjsg 	list_add(&p->pending_link, &file_priv->pending_event_list);
7677f4dd379Sjsg 	p->file_priv = file_priv;
7687f4dd379Sjsg 
7697f4dd379Sjsg 	return 0;
7707f4dd379Sjsg }
7717f4dd379Sjsg EXPORT_SYMBOL(drm_event_reserve_init_locked);
7727f4dd379Sjsg 
7737f4dd379Sjsg /**
7747f4dd379Sjsg  * drm_event_reserve_init - init a DRM event and reserve space for it
7757f4dd379Sjsg  * @dev: DRM device
7767f4dd379Sjsg  * @file_priv: DRM file private data
7777f4dd379Sjsg  * @p: tracking structure for the pending event
7787f4dd379Sjsg  * @e: actual event data to deliver to userspace
7797f4dd379Sjsg  *
7807f4dd379Sjsg  * This function prepares the passed in event for eventual delivery. If the event
7817f4dd379Sjsg  * doesn't get delivered (because the IOCTL fails later on, before queuing up
7827f4dd379Sjsg  * anything) then the even must be cancelled and freed using
7837f4dd379Sjsg  * drm_event_cancel_free(). Successfully initialized events should be sent out
7847f4dd379Sjsg  * using drm_send_event() or drm_send_event_locked() to signal completion of the
7857f4dd379Sjsg  * asynchronous event to userspace.
7867f4dd379Sjsg  *
7877f4dd379Sjsg  * If callers embedded @p into a larger structure it must be allocated with
7887f4dd379Sjsg  * kmalloc and @p must be the first member element.
7897f4dd379Sjsg  *
7907f4dd379Sjsg  * Callers which already hold &drm_device.event_lock should use
7917f4dd379Sjsg  * drm_event_reserve_init_locked() instead.
7927f4dd379Sjsg  *
7937f4dd379Sjsg  * RETURNS:
7947f4dd379Sjsg  *
7957f4dd379Sjsg  * 0 on success or a negative error code on failure.
7967f4dd379Sjsg  */
drm_event_reserve_init(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)7977f4dd379Sjsg int drm_event_reserve_init(struct drm_device *dev,
7987f4dd379Sjsg 			   struct drm_file *file_priv,
7997f4dd379Sjsg 			   struct drm_pending_event *p,
8007f4dd379Sjsg 			   struct drm_event *e)
8017f4dd379Sjsg {
8027f4dd379Sjsg 	unsigned long flags;
8037f4dd379Sjsg 	int ret;
8047f4dd379Sjsg 
8057f4dd379Sjsg 	spin_lock_irqsave(&dev->event_lock, flags);
8067f4dd379Sjsg 	ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
8077f4dd379Sjsg 	spin_unlock_irqrestore(&dev->event_lock, flags);
8087f4dd379Sjsg 
8097f4dd379Sjsg 	return ret;
8107f4dd379Sjsg }
8117f4dd379Sjsg EXPORT_SYMBOL(drm_event_reserve_init);
8127f4dd379Sjsg 
8137f4dd379Sjsg /**
814c349dbc7Sjsg  * drm_event_cancel_free - free a DRM event and release its space
8157f4dd379Sjsg  * @dev: DRM device
8167f4dd379Sjsg  * @p: tracking structure for the pending event
8177f4dd379Sjsg  *
8187f4dd379Sjsg  * This function frees the event @p initialized with drm_event_reserve_init()
8197f4dd379Sjsg  * and releases any allocated space. It is used to cancel an event when the
8207f4dd379Sjsg  * nonblocking operation could not be submitted and needed to be aborted.
8217f4dd379Sjsg  */
drm_event_cancel_free(struct drm_device * dev,struct drm_pending_event * p)8227f4dd379Sjsg void drm_event_cancel_free(struct drm_device *dev,
8237f4dd379Sjsg 			   struct drm_pending_event *p)
8247f4dd379Sjsg {
8257f4dd379Sjsg 	unsigned long flags;
826ad8b1aafSjsg 
8277f4dd379Sjsg 	spin_lock_irqsave(&dev->event_lock, flags);
8287f4dd379Sjsg 	if (p->file_priv) {
8297f4dd379Sjsg 		p->file_priv->event_space += p->event->length;
8307f4dd379Sjsg 		list_del(&p->pending_link);
8317f4dd379Sjsg 	}
8327f4dd379Sjsg 	spin_unlock_irqrestore(&dev->event_lock, flags);
8337f4dd379Sjsg 
8347f4dd379Sjsg 	if (p->fence)
8357f4dd379Sjsg 		dma_fence_put(p->fence);
8367f4dd379Sjsg 
8377f4dd379Sjsg 	kfree(p);
8387f4dd379Sjsg }
8397f4dd379Sjsg EXPORT_SYMBOL(drm_event_cancel_free);
8407f4dd379Sjsg 
drm_send_event_helper(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)8415ca02815Sjsg static void drm_send_event_helper(struct drm_device *dev,
8425ca02815Sjsg 			   struct drm_pending_event *e, ktime_t timestamp)
8437f4dd379Sjsg {
8447f4dd379Sjsg 	assert_spin_locked(&dev->event_lock);
8457f4dd379Sjsg 
8467f4dd379Sjsg 	if (e->completion) {
8477f4dd379Sjsg 		complete_all(e->completion);
8487f4dd379Sjsg 		e->completion_release(e->completion);
8497f4dd379Sjsg 		e->completion = NULL;
8507f4dd379Sjsg 	}
8517f4dd379Sjsg 
8527f4dd379Sjsg 	if (e->fence) {
8535ca02815Sjsg 		if (timestamp)
8545ca02815Sjsg 			dma_fence_signal_timestamp(e->fence, timestamp);
8555ca02815Sjsg 		else
8567f4dd379Sjsg 			dma_fence_signal(e->fence);
8577f4dd379Sjsg 		dma_fence_put(e->fence);
8587f4dd379Sjsg 	}
8597f4dd379Sjsg 
8607f4dd379Sjsg 	if (!e->file_priv) {
8617f4dd379Sjsg 		kfree(e);
8627f4dd379Sjsg 		return;
8637f4dd379Sjsg 	}
8647f4dd379Sjsg 
8657f4dd379Sjsg 	list_del(&e->pending_link);
8667f4dd379Sjsg 	list_add_tail(&e->link,
8677f4dd379Sjsg 		      &e->file_priv->event_list);
868ad8b1aafSjsg 	wake_up_interruptible_poll(&e->file_priv->event_wait,
869ad8b1aafSjsg 		EPOLLIN | EPOLLRDNORM);
8707f4dd379Sjsg #ifdef __OpenBSD__
8717f4dd379Sjsg 	selwakeup(&e->file_priv->rsel);
8727f4dd379Sjsg #endif
8737f4dd379Sjsg }
8745ca02815Sjsg 
8755ca02815Sjsg /**
8765ca02815Sjsg  * drm_send_event_timestamp_locked - send DRM event to file descriptor
8775ca02815Sjsg  * @dev: DRM device
8785ca02815Sjsg  * @e: DRM event to deliver
8795ca02815Sjsg  * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
8805ca02815Sjsg  * time domain
8815ca02815Sjsg  *
8825ca02815Sjsg  * This function sends the event @e, initialized with drm_event_reserve_init(),
8835ca02815Sjsg  * to its associated userspace DRM file. Callers must already hold
8845ca02815Sjsg  * &drm_device.event_lock.
8855ca02815Sjsg  *
8865ca02815Sjsg  * Note that the core will take care of unlinking and disarming events when the
8875ca02815Sjsg  * corresponding DRM file is closed. Drivers need not worry about whether the
8885ca02815Sjsg  * DRM file for this event still exists and can call this function upon
8895ca02815Sjsg  * completion of the asynchronous work unconditionally.
8905ca02815Sjsg  */
drm_send_event_timestamp_locked(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)8915ca02815Sjsg void drm_send_event_timestamp_locked(struct drm_device *dev,
8925ca02815Sjsg 				     struct drm_pending_event *e, ktime_t timestamp)
8935ca02815Sjsg {
8945ca02815Sjsg 	drm_send_event_helper(dev, e, timestamp);
8955ca02815Sjsg }
8965ca02815Sjsg EXPORT_SYMBOL(drm_send_event_timestamp_locked);
8975ca02815Sjsg 
8985ca02815Sjsg /**
8995ca02815Sjsg  * drm_send_event_locked - send DRM event to file descriptor
9005ca02815Sjsg  * @dev: DRM device
9015ca02815Sjsg  * @e: DRM event to deliver
9025ca02815Sjsg  *
9035ca02815Sjsg  * This function sends the event @e, initialized with drm_event_reserve_init(),
9045ca02815Sjsg  * to its associated userspace DRM file. Callers must already hold
9055ca02815Sjsg  * &drm_device.event_lock, see drm_send_event() for the unlocked version.
9065ca02815Sjsg  *
9075ca02815Sjsg  * Note that the core will take care of unlinking and disarming events when the
9085ca02815Sjsg  * corresponding DRM file is closed. Drivers need not worry about whether the
9095ca02815Sjsg  * DRM file for this event still exists and can call this function upon
9105ca02815Sjsg  * completion of the asynchronous work unconditionally.
9115ca02815Sjsg  */
drm_send_event_locked(struct drm_device * dev,struct drm_pending_event * e)9125ca02815Sjsg void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
9135ca02815Sjsg {
9145ca02815Sjsg 	drm_send_event_helper(dev, e, 0);
9155ca02815Sjsg }
9167f4dd379Sjsg EXPORT_SYMBOL(drm_send_event_locked);
9177f4dd379Sjsg 
9187f4dd379Sjsg /**
9197f4dd379Sjsg  * drm_send_event - send DRM event to file descriptor
9207f4dd379Sjsg  * @dev: DRM device
9217f4dd379Sjsg  * @e: DRM event to deliver
9227f4dd379Sjsg  *
9237f4dd379Sjsg  * This function sends the event @e, initialized with drm_event_reserve_init(),
9247f4dd379Sjsg  * to its associated userspace DRM file. This function acquires
9257f4dd379Sjsg  * &drm_device.event_lock, see drm_send_event_locked() for callers which already
9267f4dd379Sjsg  * hold this lock.
9277f4dd379Sjsg  *
9287f4dd379Sjsg  * Note that the core will take care of unlinking and disarming events when the
9297f4dd379Sjsg  * corresponding DRM file is closed. Drivers need not worry about whether the
9307f4dd379Sjsg  * DRM file for this event still exists and can call this function upon
9317f4dd379Sjsg  * completion of the asynchronous work unconditionally.
9327f4dd379Sjsg  */
drm_send_event(struct drm_device * dev,struct drm_pending_event * e)9337f4dd379Sjsg void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
9347f4dd379Sjsg {
9357f4dd379Sjsg 	unsigned long irqflags;
9367f4dd379Sjsg 
9377f4dd379Sjsg 	spin_lock_irqsave(&dev->event_lock, irqflags);
9385ca02815Sjsg 	drm_send_event_helper(dev, e, 0);
9397f4dd379Sjsg 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
9407f4dd379Sjsg }
9417f4dd379Sjsg EXPORT_SYMBOL(drm_send_event);
942c349dbc7Sjsg 
print_size(struct drm_printer * p,const char * stat,const char * region,u64 sz)943f005ef32Sjsg static void print_size(struct drm_printer *p, const char *stat,
944f005ef32Sjsg 		       const char *region, u64 sz)
945f005ef32Sjsg {
946f005ef32Sjsg 	const char *units[] = {"", " KiB", " MiB"};
947f005ef32Sjsg 	unsigned u;
948f005ef32Sjsg 
949f005ef32Sjsg 	for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
950f005ef32Sjsg 		if (sz < SZ_1K)
951f005ef32Sjsg 			break;
952f005ef32Sjsg 		sz = div_u64(sz, SZ_1K);
953f005ef32Sjsg 	}
954f005ef32Sjsg 
955f005ef32Sjsg 	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
956f005ef32Sjsg }
957f005ef32Sjsg 
958f005ef32Sjsg /**
959f005ef32Sjsg  * drm_print_memory_stats - A helper to print memory stats
960f005ef32Sjsg  * @p: The printer to print output to
961f005ef32Sjsg  * @stats: The collected memory stats
962f005ef32Sjsg  * @supported_status: Bitmask of optional stats which are available
963f005ef32Sjsg  * @region: The memory region
964f005ef32Sjsg  *
965f005ef32Sjsg  */
drm_print_memory_stats(struct drm_printer * p,const struct drm_memory_stats * stats,enum drm_gem_object_status supported_status,const char * region)966f005ef32Sjsg void drm_print_memory_stats(struct drm_printer *p,
967f005ef32Sjsg 			    const struct drm_memory_stats *stats,
968f005ef32Sjsg 			    enum drm_gem_object_status supported_status,
969f005ef32Sjsg 			    const char *region)
970f005ef32Sjsg {
971f005ef32Sjsg 	print_size(p, "total", region, stats->private + stats->shared);
972f005ef32Sjsg 	print_size(p, "shared", region, stats->shared);
973f005ef32Sjsg 	print_size(p, "active", region, stats->active);
974f005ef32Sjsg 
975f005ef32Sjsg 	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
976f005ef32Sjsg 		print_size(p, "resident", region, stats->resident);
977f005ef32Sjsg 
978f005ef32Sjsg 	if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
979f005ef32Sjsg 		print_size(p, "purgeable", region, stats->purgeable);
980f005ef32Sjsg }
981f005ef32Sjsg EXPORT_SYMBOL(drm_print_memory_stats);
982f005ef32Sjsg 
983f005ef32Sjsg /**
984f005ef32Sjsg  * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
985f005ef32Sjsg  * @p: the printer to print output to
986f005ef32Sjsg  * @file: the DRM file
987f005ef32Sjsg  *
988f005ef32Sjsg  * Helper to iterate over GEM objects with a handle allocated in the specified
989f005ef32Sjsg  * file.
990f005ef32Sjsg  */
drm_show_memory_stats(struct drm_printer * p,struct drm_file * file)991f005ef32Sjsg void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
992f005ef32Sjsg {
993f005ef32Sjsg 	struct drm_gem_object *obj;
994f005ef32Sjsg 	struct drm_memory_stats status = {};
995e6c6eae8Sjsg 	enum drm_gem_object_status supported_status = 0;
996f005ef32Sjsg 	int id;
997f005ef32Sjsg 
998f005ef32Sjsg 	spin_lock(&file->table_lock);
999f005ef32Sjsg 	idr_for_each_entry (&file->object_idr, obj, id) {
1000f005ef32Sjsg 		enum drm_gem_object_status s = 0;
1001f005ef32Sjsg 
1002f005ef32Sjsg 		if (obj->funcs && obj->funcs->status) {
1003f005ef32Sjsg 			s = obj->funcs->status(obj);
1004f005ef32Sjsg 			supported_status = DRM_GEM_OBJECT_RESIDENT |
1005f005ef32Sjsg 					DRM_GEM_OBJECT_PURGEABLE;
1006f005ef32Sjsg 		}
1007f005ef32Sjsg 
1008f005ef32Sjsg 		if (obj->handle_count > 1) {
1009f005ef32Sjsg 			status.shared += obj->size;
1010f005ef32Sjsg 		} else {
1011f005ef32Sjsg 			status.private += obj->size;
1012f005ef32Sjsg 		}
1013f005ef32Sjsg 
1014f005ef32Sjsg 		if (s & DRM_GEM_OBJECT_RESIDENT) {
1015f005ef32Sjsg 			status.resident += obj->size;
1016f005ef32Sjsg 		} else {
1017f005ef32Sjsg 			/* If already purged or not yet backed by pages, don't
1018f005ef32Sjsg 			 * count it as purgeable:
1019f005ef32Sjsg 			 */
1020f005ef32Sjsg 			s &= ~DRM_GEM_OBJECT_PURGEABLE;
1021f005ef32Sjsg 		}
1022f005ef32Sjsg 
1023f005ef32Sjsg 		if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
1024f005ef32Sjsg 			status.active += obj->size;
1025f005ef32Sjsg 
1026f005ef32Sjsg 			/* If still active, don't count as purgeable: */
1027f005ef32Sjsg 			s &= ~DRM_GEM_OBJECT_PURGEABLE;
1028f005ef32Sjsg 		}
1029f005ef32Sjsg 
1030f005ef32Sjsg 		if (s & DRM_GEM_OBJECT_PURGEABLE)
1031f005ef32Sjsg 			status.purgeable += obj->size;
1032f005ef32Sjsg 	}
1033f005ef32Sjsg 	spin_unlock(&file->table_lock);
1034f005ef32Sjsg 
1035f005ef32Sjsg 	drm_print_memory_stats(p, &status, supported_status, "memory");
1036f005ef32Sjsg }
1037f005ef32Sjsg EXPORT_SYMBOL(drm_show_memory_stats);
1038f005ef32Sjsg 
1039f005ef32Sjsg /**
1040f005ef32Sjsg  * drm_show_fdinfo - helper for drm file fops
1041f005ef32Sjsg  * @m: output stream
1042f005ef32Sjsg  * @f: the device file instance
1043f005ef32Sjsg  *
1044f005ef32Sjsg  * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
1045f005ef32Sjsg  * process using the GPU.  See also &drm_driver.show_fdinfo.
1046f005ef32Sjsg  *
1047f005ef32Sjsg  * For text output format description please see Documentation/gpu/drm-usage-stats.rst
1048f005ef32Sjsg  */
drm_show_fdinfo(struct seq_file * m,struct file * f)1049f005ef32Sjsg void drm_show_fdinfo(struct seq_file *m, struct file *f)
1050f005ef32Sjsg {
1051f005ef32Sjsg 	STUB();
1052f005ef32Sjsg #ifdef notyet
1053f005ef32Sjsg 	struct drm_file *file = f->private_data;
1054f005ef32Sjsg 	struct drm_device *dev = file->minor->dev;
1055f005ef32Sjsg 	struct drm_printer p = drm_seq_file_printer(m);
1056f005ef32Sjsg 
1057f005ef32Sjsg 	drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
1058f005ef32Sjsg 	drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
1059f005ef32Sjsg 
1060f005ef32Sjsg 	if (dev_is_pci(dev->dev)) {
1061f005ef32Sjsg 		struct pci_dev *pdev = to_pci_dev(dev->dev);
1062f005ef32Sjsg 
1063f005ef32Sjsg 		drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
1064f005ef32Sjsg 			   pci_domain_nr(pdev->bus), pdev->bus->number,
1065f005ef32Sjsg 			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1066f005ef32Sjsg 	}
1067f005ef32Sjsg 
1068f005ef32Sjsg 	if (dev->driver->show_fdinfo)
1069f005ef32Sjsg 		dev->driver->show_fdinfo(&p, file);
1070f005ef32Sjsg #endif
1071f005ef32Sjsg }
1072f005ef32Sjsg EXPORT_SYMBOL(drm_show_fdinfo);
1073f005ef32Sjsg 
1074c349dbc7Sjsg /**
1075c349dbc7Sjsg  * mock_drm_getfile - Create a new struct file for the drm device
1076c349dbc7Sjsg  * @minor: drm minor to wrap (e.g. #drm_device.primary)
1077c349dbc7Sjsg  * @flags: file creation mode (O_RDWR etc)
1078c349dbc7Sjsg  *
1079c349dbc7Sjsg  * This create a new struct file that wraps a DRM file context around a
1080c349dbc7Sjsg  * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
1081c349dbc7Sjsg  * invoking userspace. The struct file may be operated on using its f_op
1082c349dbc7Sjsg  * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
1083c349dbc7Sjsg  * to userspace facing functions as an internal/anonymous client.
1084c349dbc7Sjsg  *
1085c349dbc7Sjsg  * RETURNS:
1086c349dbc7Sjsg  * Pointer to newly created struct file, ERR_PTR on failure.
1087c349dbc7Sjsg  */
mock_drm_getfile(struct drm_minor * minor,unsigned int flags)1088c349dbc7Sjsg struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
1089c349dbc7Sjsg {
1090c349dbc7Sjsg 	STUB();
1091c349dbc7Sjsg 	return ERR_PTR(-ENOSYS);
1092c349dbc7Sjsg #ifdef notyet
1093c349dbc7Sjsg 	struct drm_device *dev = minor->dev;
1094c349dbc7Sjsg 	struct drm_file *priv;
1095c349dbc7Sjsg 	struct file *file;
1096c349dbc7Sjsg 
1097c349dbc7Sjsg 	priv = drm_file_alloc(minor);
1098c349dbc7Sjsg 	if (IS_ERR(priv))
1099c349dbc7Sjsg 		return ERR_CAST(priv);
1100c349dbc7Sjsg 
1101c349dbc7Sjsg 	file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
1102c349dbc7Sjsg 	if (IS_ERR(file)) {
1103c349dbc7Sjsg 		drm_file_free(priv);
1104c349dbc7Sjsg 		return file;
1105c349dbc7Sjsg 	}
1106c349dbc7Sjsg 
1107c349dbc7Sjsg 	/* Everyone shares a single global address space */
1108c349dbc7Sjsg 	file->f_mapping = dev->anon_inode->i_mapping;
1109c349dbc7Sjsg 
1110c349dbc7Sjsg 	drm_dev_get(dev);
1111c349dbc7Sjsg 	priv->filp = file;
1112c349dbc7Sjsg 
1113c349dbc7Sjsg 	return file;
1114c349dbc7Sjsg #endif
1115c349dbc7Sjsg }
1116c349dbc7Sjsg EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);
1117