xref: /dragonfly/sys/dev/drm/drm_file.c (revision 78973132)
1a85cb24fSFrançois Tigeot /*
2a85cb24fSFrançois Tigeot  * \author Rickard E. (Rik) Faith <faith@valinux.com>
3a85cb24fSFrançois Tigeot  * \author Daryll Strauss <daryll@valinux.com>
4a85cb24fSFrançois Tigeot  * \author Gareth Hughes <gareth@valinux.com>
5a85cb24fSFrançois Tigeot  */
6a85cb24fSFrançois Tigeot 
7a85cb24fSFrançois Tigeot /*
8a85cb24fSFrançois Tigeot  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
9a85cb24fSFrançois Tigeot  *
10a85cb24fSFrançois Tigeot  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11a85cb24fSFrançois Tigeot  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12a85cb24fSFrançois Tigeot  * All Rights Reserved.
13a85cb24fSFrançois Tigeot  *
14a85cb24fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
15a85cb24fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
16a85cb24fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
17a85cb24fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18a85cb24fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
19a85cb24fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
20a85cb24fSFrançois Tigeot  *
21a85cb24fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
22a85cb24fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
23a85cb24fSFrançois Tigeot  * Software.
24a85cb24fSFrançois Tigeot  *
25a85cb24fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26a85cb24fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27a85cb24fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28a85cb24fSFrançois Tigeot  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29a85cb24fSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30a85cb24fSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31a85cb24fSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
32a85cb24fSFrançois Tigeot  */
33a85cb24fSFrançois Tigeot 
34a85cb24fSFrançois Tigeot #include <sys/types.h>
35a85cb24fSFrançois Tigeot #include <sys/uio.h>	/* must come first to avoid kfree() macros issues */
36a85cb24fSFrançois Tigeot 
37a85cb24fSFrançois Tigeot #include <linux/poll.h>
38a85cb24fSFrançois Tigeot #include <linux/slab.h>
39a85cb24fSFrançois Tigeot #include <linux/module.h>
40a85cb24fSFrançois Tigeot 
41a85cb24fSFrançois Tigeot #include <drm/drm_file.h>
42a85cb24fSFrançois Tigeot #include <drm/drmP.h>
43a85cb24fSFrançois Tigeot 
44a85cb24fSFrançois Tigeot #include "drm_legacy.h"
45a85cb24fSFrançois Tigeot #include "drm_internal.h"
46a85cb24fSFrançois Tigeot #include "drm_crtc_internal.h"
47a85cb24fSFrançois Tigeot 
48*78973132SSergey Zigachev static void drm_events_release(struct drm_file *file_priv);
49*78973132SSergey Zigachev 
50a85cb24fSFrançois Tigeot /* from BKL pushdown */
51a85cb24fSFrançois Tigeot DEFINE_MUTEX(drm_global_mutex);
52a85cb24fSFrançois Tigeot 
53a85cb24fSFrançois Tigeot /**
54a85cb24fSFrançois Tigeot  * DOC: file operations
55a85cb24fSFrançois Tigeot  *
56a85cb24fSFrançois Tigeot  * Drivers must define the file operations structure that forms the DRM
57a85cb24fSFrançois Tigeot  * userspace API entry point, even though most of those operations are
583f2dd94aSFrançois Tigeot  * implemented in the DRM core. The resulting &struct file_operations must be
593f2dd94aSFrançois Tigeot  * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
60a85cb24fSFrançois Tigeot  * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
613f2dd94aSFrançois Tigeot  * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
623f2dd94aSFrançois Tigeot  * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
633f2dd94aSFrançois Tigeot  * that require 32/64 bit compatibility support must provide their own
643f2dd94aSFrançois Tigeot  * &file_operations.compat_ioctl handler that processes private ioctls and calls
653f2dd94aSFrançois Tigeot  * drm_compat_ioctl() for core ioctls.
66a85cb24fSFrançois Tigeot  *
67a85cb24fSFrançois Tigeot  * In addition drm_read() and drm_poll() provide support for DRM events. DRM
68a85cb24fSFrançois Tigeot  * events are a generic and extensible means to send asynchronous events to
69a85cb24fSFrançois Tigeot  * userspace through the file descriptor. They are used to send vblank event and
70a85cb24fSFrançois Tigeot  * page flip completions by the KMS API. But drivers can also use it for their
71a85cb24fSFrançois Tigeot  * own needs, e.g. to signal completion of rendering.
72a85cb24fSFrançois Tigeot  *
733f2dd94aSFrançois Tigeot  * For the driver-side event interface see drm_event_reserve_init() and
743f2dd94aSFrançois Tigeot  * drm_send_event() as the main starting points.
753f2dd94aSFrançois Tigeot  *
76a85cb24fSFrançois Tigeot  * The memory mapping implementation will vary depending on how the driver
77a85cb24fSFrançois Tigeot  * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
78a85cb24fSFrançois Tigeot  * function, modern drivers should use one of the provided memory-manager
793f2dd94aSFrançois Tigeot  * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
803f2dd94aSFrançois Tigeot  * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
81a85cb24fSFrançois Tigeot  *
82a85cb24fSFrançois Tigeot  * No other file operations are supported by the DRM userspace API. Overall the
833f2dd94aSFrançois Tigeot  * following is an example &file_operations structure::
84a85cb24fSFrançois Tigeot  *
85a85cb24fSFrançois Tigeot  *     static const example_drm_fops = {
86a85cb24fSFrançois Tigeot  *             .owner = THIS_MODULE,
87a85cb24fSFrançois Tigeot  *             .open = drm_open,
88a85cb24fSFrançois Tigeot  *             .release = drm_release,
89a85cb24fSFrançois Tigeot  *             .unlocked_ioctl = drm_ioctl,
90a85cb24fSFrançois Tigeot  *             .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
91a85cb24fSFrançois Tigeot  *             .poll = drm_poll,
92a85cb24fSFrançois Tigeot  *             .read = drm_read,
93a85cb24fSFrançois Tigeot  *             .llseek = no_llseek,
94a85cb24fSFrançois Tigeot  *             .mmap = drm_gem_mmap,
95a85cb24fSFrançois Tigeot  *     };
963f2dd94aSFrançois Tigeot  *
973f2dd94aSFrançois Tigeot  * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
983f2dd94aSFrançois Tigeot  * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
993f2dd94aSFrançois Tigeot  * simpler.
1003f2dd94aSFrançois Tigeot  *
1013f2dd94aSFrançois Tigeot  * The driver's &file_operations must be stored in &drm_driver.fops.
1023f2dd94aSFrançois Tigeot  *
1033f2dd94aSFrançois Tigeot  * For driver-private IOCTL handling see the more detailed discussion in
1043f2dd94aSFrançois Tigeot  * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
105a85cb24fSFrançois Tigeot  */
106a85cb24fSFrançois Tigeot 
1073f2dd94aSFrançois Tigeot static int drm_open_helper(struct cdev *kdev, int flags,
1083f2dd94aSFrançois Tigeot 			   struct file *filp, struct drm_minor *minor);
109a85cb24fSFrançois Tigeot 
drm_setup(struct drm_device * dev)110a85cb24fSFrançois Tigeot static int drm_setup(struct drm_device * dev)
111a85cb24fSFrançois Tigeot {
112a85cb24fSFrançois Tigeot 	int ret;
113a85cb24fSFrançois Tigeot 
114a85cb24fSFrançois Tigeot 	if (dev->driver->firstopen &&
115a85cb24fSFrançois Tigeot 	    drm_core_check_feature(dev, DRIVER_LEGACY)) {
116a85cb24fSFrançois Tigeot 		ret = dev->driver->firstopen(dev);
117a85cb24fSFrançois Tigeot 		if (ret != 0)
118a85cb24fSFrançois Tigeot 			return ret;
119a85cb24fSFrançois Tigeot 	}
120a85cb24fSFrançois Tigeot 
121a85cb24fSFrançois Tigeot 	ret = drm_legacy_dma_setup(dev);
122a85cb24fSFrançois Tigeot 	if (ret < 0)
123a85cb24fSFrançois Tigeot 		return ret;
124a85cb24fSFrançois Tigeot 
125a85cb24fSFrançois Tigeot 
126a85cb24fSFrançois Tigeot 	DRM_DEBUG("\n");
127a85cb24fSFrançois Tigeot 	return 0;
128a85cb24fSFrançois Tigeot }
129a85cb24fSFrançois Tigeot 
130a85cb24fSFrançois Tigeot /**
131a85cb24fSFrançois Tigeot  * drm_open - open method for DRM file
132a85cb24fSFrançois Tigeot  * @inode: device inode
133a85cb24fSFrançois Tigeot  * @filp: file pointer.
134a85cb24fSFrançois Tigeot  *
1353f2dd94aSFrançois Tigeot  * This function must be used by drivers as their &file_operations.open method.
1363f2dd94aSFrançois Tigeot  * It looks up the correct DRM device and instantiates all the per-file
1373f2dd94aSFrançois Tigeot  * resources for it. It also calls the &drm_driver.open driver callback.
138a85cb24fSFrançois Tigeot  *
139a85cb24fSFrançois Tigeot  * RETURNS:
140a85cb24fSFrançois Tigeot  *
141a85cb24fSFrançois Tigeot  * 0 on success or negative errno value on falure.
142a85cb24fSFrançois Tigeot  */
143a85cb24fSFrançois Tigeot // drm_open() is a file_operations function, not a dev_ops function
144a85cb24fSFrançois Tigeot // int drm_open(struct inode *inode, struct file *filp)
drm_open(struct dev_open_args * ap)145a85cb24fSFrançois Tigeot int drm_open(struct dev_open_args *ap)
146a85cb24fSFrançois Tigeot {
1473f2dd94aSFrançois Tigeot #ifdef __DragonFly__
1485bd45597SMatthew Dillon 	struct file *filp = ap->a_fpp ? *ap->a_fpp : NULL;
149a85cb24fSFrançois Tigeot 	struct inode *inode = filp->f_data;	/* A Linux inode is a Unix vnode */
150a85cb24fSFrançois Tigeot 	struct cdev *kdev = ap->a_head.a_dev;
151a85cb24fSFrançois Tigeot 	int flags = ap->a_oflags;
1523f2dd94aSFrançois Tigeot #endif
1533f2dd94aSFrançois Tigeot 	struct drm_device *dev;
1543f2dd94aSFrançois Tigeot 	struct drm_minor *minor;
155a85cb24fSFrançois Tigeot 	int retcode;
156a85cb24fSFrançois Tigeot 	int need_setup = 0;
157a85cb24fSFrançois Tigeot 
158a85cb24fSFrançois Tigeot 	minor = drm_minor_acquire(iminor(inode));
1593f2dd94aSFrançois Tigeot 	if (IS_ERR(minor))
1603f2dd94aSFrançois Tigeot 		return PTR_ERR(minor);
1613f2dd94aSFrançois Tigeot 
1623f2dd94aSFrançois Tigeot 	dev = minor->dev;
163a85cb24fSFrançois Tigeot 	if (!dev->open_count++)
164a85cb24fSFrançois Tigeot 		need_setup = 1;
165a85cb24fSFrançois Tigeot 
166a85cb24fSFrançois Tigeot 	/* share address_space across all char-devs of a single device */
167a85cb24fSFrançois Tigeot #if 0
168a85cb24fSFrançois Tigeot 	filp->f_mapping = dev->anon_inode->i_mapping;
169a85cb24fSFrançois Tigeot #endif
170a85cb24fSFrançois Tigeot 
1715bd45597SMatthew Dillon #ifdef __DragonFly__
1725bd45597SMatthew Dillon 	retcode = drm_open_helper(kdev, flags, filp, minor);
1735bd45597SMatthew Dillon #else
1743f2dd94aSFrançois Tigeot 	retcode = drm_open_helper(kdev, flags, ap->a_fp, minor);
1755bd45597SMatthew Dillon #endif
1763f2dd94aSFrançois Tigeot 	if (retcode)
1773f2dd94aSFrançois Tigeot 		goto err_undo;
178a85cb24fSFrançois Tigeot 	if (need_setup) {
179a85cb24fSFrançois Tigeot 		retcode = drm_setup(dev);
180a85cb24fSFrançois Tigeot 		if (retcode)
181a85cb24fSFrançois Tigeot 			goto err_undo;
182a85cb24fSFrançois Tigeot 	}
1833f2dd94aSFrançois Tigeot #ifdef __DragonFly__
1843f2dd94aSFrançois Tigeot 	device_busy(dev->dev->bsddev);
1853f2dd94aSFrançois Tigeot #endif
186a85cb24fSFrançois Tigeot 	return 0;
187a85cb24fSFrançois Tigeot 
188a85cb24fSFrançois Tigeot err_undo:
189a85cb24fSFrançois Tigeot 	dev->open_count--;
190a85cb24fSFrançois Tigeot 	drm_minor_release(minor);
191a85cb24fSFrançois Tigeot 	return retcode;
192a85cb24fSFrançois Tigeot }
193a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_open);
194a85cb24fSFrançois Tigeot 
195a85cb24fSFrançois Tigeot /*
196*78973132SSergey Zigachev  * close() function
197*78973132SSergey Zigachev  */
198*78973132SSergey Zigachev int
drm_close(struct dev_close_args * ap)199*78973132SSergey Zigachev drm_close(struct dev_close_args *ap)
200*78973132SSergey Zigachev {
201*78973132SSergey Zigachev #ifdef __DragonFly__
202*78973132SSergey Zigachev 	struct file *filp = ap->a_fp;
203*78973132SSergey Zigachev 	struct inode *inode = filp->f_data;	/* A Linux inode is a Unix vnode */
204*78973132SSergey Zigachev #endif
205*78973132SSergey Zigachev 	struct drm_file *file_priv = filp->private_data;
206*78973132SSergey Zigachev 	struct drm_minor *minor = drm_minor_acquire(iminor(inode));
207*78973132SSergey Zigachev 	struct drm_device *dev = minor->dev;
208*78973132SSergey Zigachev 
209*78973132SSergey Zigachev 	mutex_lock(&drm_global_mutex);
210*78973132SSergey Zigachev 
211*78973132SSergey Zigachev 	DRM_DEBUG("open_count = %d\n", dev->open_count);
212*78973132SSergey Zigachev 
213*78973132SSergey Zigachev 	mutex_lock(&dev->filelist_mutex);
214*78973132SSergey Zigachev 	list_del(&file_priv->lhead);
215*78973132SSergey Zigachev 	mutex_unlock(&dev->filelist_mutex);
216*78973132SSergey Zigachev 
217*78973132SSergey Zigachev 	if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
218*78973132SSergey Zigachev 	    dev->driver->preclose)
219*78973132SSergey Zigachev 		dev->driver->preclose(dev, file_priv);
220*78973132SSergey Zigachev 
221*78973132SSergey Zigachev 	/* ========================================================
222*78973132SSergey Zigachev 	 * Begin inline drm_release
223*78973132SSergey Zigachev 	 */
224*78973132SSergey Zigachev 
225*78973132SSergey Zigachev 	DRM_DEBUG("pid = %d, device = 0x%p, open_count = %d\n",
226*78973132SSergey Zigachev 		  curproc->p_pid,
227*78973132SSergey Zigachev 		  dev,
228*78973132SSergey Zigachev 		  dev->open_count);
229*78973132SSergey Zigachev 
230*78973132SSergey Zigachev 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
231*78973132SSergey Zigachev 		drm_legacy_lock_release(dev, filp);
232*78973132SSergey Zigachev 
233*78973132SSergey Zigachev 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
234*78973132SSergey Zigachev 		drm_legacy_reclaim_buffers(dev, file_priv);
235*78973132SSergey Zigachev 
236*78973132SSergey Zigachev 	drm_events_release(file_priv);
237*78973132SSergey Zigachev 
238*78973132SSergey Zigachev 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
239*78973132SSergey Zigachev 		drm_fb_release(file_priv);
240*78973132SSergey Zigachev 		drm_property_destroy_user_blobs(dev, file_priv);
241*78973132SSergey Zigachev 	}
242*78973132SSergey Zigachev 
243*78973132SSergey Zigachev 	if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
244*78973132SSergey Zigachev 		drm_syncobj_release(file_priv);
245*78973132SSergey Zigachev 
246*78973132SSergey Zigachev 	if (drm_core_check_feature(dev, DRIVER_GEM))
247*78973132SSergey Zigachev 		drm_gem_release(dev, file_priv);
248*78973132SSergey Zigachev 
249*78973132SSergey Zigachev 	drm_legacy_ctxbitmap_flush(dev, file_priv);
250*78973132SSergey Zigachev 
251*78973132SSergey Zigachev 	if (drm_is_primary_client(file_priv))
252*78973132SSergey Zigachev 		drm_master_release(file_priv);
253*78973132SSergey Zigachev 
254*78973132SSergey Zigachev 	if (dev->driver->postclose)
255*78973132SSergey Zigachev 		dev->driver->postclose(dev, file_priv);
256*78973132SSergey Zigachev 
257*78973132SSergey Zigachev 	if (drm_core_check_feature(dev, DRIVER_PRIME))
258*78973132SSergey Zigachev 		drm_prime_destroy_file_private(&file_priv->prime);
259*78973132SSergey Zigachev 
260*78973132SSergey Zigachev 	WARN_ON(!list_empty(&file_priv->event_list));
261*78973132SSergey Zigachev 
262*78973132SSergey Zigachev 	put_pid(file_priv->pid);
263*78973132SSergey Zigachev 	kfree(file_priv);
264*78973132SSergey Zigachev 
265*78973132SSergey Zigachev 	/* ========================================================
266*78973132SSergey Zigachev 	 * End inline drm_release
267*78973132SSergey Zigachev 	 */
268*78973132SSergey Zigachev 
269*78973132SSergey Zigachev 	if (!--dev->open_count) {
270*78973132SSergey Zigachev 		drm_lastclose(dev);
271*78973132SSergey Zigachev #if 0	/* XXX: drm_put_dev() not implemented */
272*78973132SSergey Zigachev 		if (drm_dev_is_unplugged(dev))
273*78973132SSergey Zigachev 			drm_put_dev(dev);
274*78973132SSergey Zigachev #endif
275*78973132SSergey Zigachev 	}
276*78973132SSergey Zigachev 	mutex_unlock(&drm_global_mutex);
277*78973132SSergey Zigachev 
278*78973132SSergey Zigachev 	drm_minor_release(minor);
279*78973132SSergey Zigachev 
280*78973132SSergey Zigachev 	return 0;
281*78973132SSergey Zigachev }
282*78973132SSergey Zigachev EXPORT_SYMBOL(drm_close);
283*78973132SSergey Zigachev 
284*78973132SSergey Zigachev /*
285a85cb24fSFrançois Tigeot  * Check whether DRI will run on this CPU.
286a85cb24fSFrançois Tigeot  *
287a85cb24fSFrançois Tigeot  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
288a85cb24fSFrançois Tigeot  */
drm_cpu_valid(void)2893f2dd94aSFrançois Tigeot static int drm_cpu_valid(void)
2903f2dd94aSFrançois Tigeot {
2913f2dd94aSFrançois Tigeot #if defined(__sparc__) && !defined(__sparc_v9__)
2923f2dd94aSFrançois Tigeot 	return 0;		/* No cmpxchg before v9 sparc. */
2933f2dd94aSFrançois Tigeot #endif
2943f2dd94aSFrançois Tigeot 	return 1;
2953f2dd94aSFrançois Tigeot }
296a85cb24fSFrançois Tigeot 
297a85cb24fSFrançois Tigeot /*
298a85cb24fSFrançois Tigeot  * Called whenever a process opens /dev/drm.
299a85cb24fSFrançois Tigeot  *
300a85cb24fSFrançois Tigeot  * \param filp file pointer.
301a85cb24fSFrançois Tigeot  * \param minor acquired minor-object.
302a85cb24fSFrançois Tigeot  * \return zero on success or a negative number on failure.
303a85cb24fSFrançois Tigeot  *
304a85cb24fSFrançois Tigeot  * Creates and initializes a drm_file structure for the file private data in \p
305a85cb24fSFrançois Tigeot  * filp and add it into the double linked list in \p dev.
306a85cb24fSFrançois Tigeot  */
drm_open_helper(struct cdev * kdev,int flags,struct file * filp,struct drm_minor * minor)3073f2dd94aSFrançois Tigeot static int drm_open_helper(struct cdev *kdev, int flags,
3083f2dd94aSFrançois Tigeot 		    struct file *filp, struct drm_minor *minor)
309a85cb24fSFrançois Tigeot {
3103f2dd94aSFrançois Tigeot 	struct drm_device *dev = minor->dev;
311a85cb24fSFrançois Tigeot 	struct drm_file *priv;
312a85cb24fSFrançois Tigeot 	int ret;
313a85cb24fSFrançois Tigeot 
314a85cb24fSFrançois Tigeot 	if (flags & O_EXCL)
3153f2dd94aSFrançois Tigeot 		return -EBUSY;	/* No exclusive opens */
3163f2dd94aSFrançois Tigeot 	if (!drm_cpu_valid())
3173f2dd94aSFrançois Tigeot 		return -EINVAL;
318a85cb24fSFrançois Tigeot 
3193f2dd94aSFrançois Tigeot 	DRM_DEBUG("pid = %d, minor = %d\n", DRM_CURRENTPID, minor->index);
320a85cb24fSFrançois Tigeot 
321a85cb24fSFrançois Tigeot 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
322a85cb24fSFrançois Tigeot 	if (!priv)
323a85cb24fSFrançois Tigeot 		return -ENOMEM;
324a85cb24fSFrançois Tigeot 
325a85cb24fSFrançois Tigeot 	filp->private_data = priv;
326a85cb24fSFrançois Tigeot 	priv->filp = filp;
3273f2dd94aSFrançois Tigeot 	priv->pid = curproc->p_pid;
328a85cb24fSFrançois Tigeot 	priv->minor = minor;
329a85cb24fSFrançois Tigeot 	priv->dev = dev;
330a85cb24fSFrançois Tigeot 
331a85cb24fSFrançois Tigeot 	/* for compatibility root is always authenticated */
332a85cb24fSFrançois Tigeot 	priv->authenticated = capable(CAP_SYS_ADMIN);
333a85cb24fSFrançois Tigeot 	priv->lock_count = 0;
334a85cb24fSFrançois Tigeot 
335a85cb24fSFrançois Tigeot 	INIT_LIST_HEAD(&priv->lhead);
336a85cb24fSFrançois Tigeot 	INIT_LIST_HEAD(&priv->fbs);
337a85cb24fSFrançois Tigeot 	lockinit(&priv->fbs_lock, "dpfl", 0, LK_CANRECURSE);
338a85cb24fSFrançois Tigeot 	INIT_LIST_HEAD(&priv->blobs);
339a85cb24fSFrançois Tigeot 	INIT_LIST_HEAD(&priv->pending_event_list);
340a85cb24fSFrançois Tigeot 	INIT_LIST_HEAD(&priv->event_list);
341a85cb24fSFrançois Tigeot 	init_waitqueue_head(&priv->event_wait);
342a85cb24fSFrançois Tigeot 	priv->event_space = 4096; /* set aside 4k for event buffer */
343a85cb24fSFrançois Tigeot 
344a85cb24fSFrançois Tigeot 	lockinit(&priv->event_read_lock, "dperl", 0, LK_CANRECURSE);
345a85cb24fSFrançois Tigeot 
346a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_GEM))
347a85cb24fSFrançois Tigeot 		drm_gem_open(dev, priv);
348a85cb24fSFrançois Tigeot 
3493f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
3503f2dd94aSFrançois Tigeot 		drm_syncobj_open(priv);
3513f2dd94aSFrançois Tigeot 
3523f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_PRIME))
3533f2dd94aSFrançois Tigeot 		drm_prime_init_file_private(&priv->prime);
3543f2dd94aSFrançois Tigeot 
355a85cb24fSFrançois Tigeot 	if (dev->driver->open) {
356a85cb24fSFrançois Tigeot 		/* shared code returns -errno */
357a85cb24fSFrançois Tigeot 		ret = -dev->driver->open(dev, priv);
3583f2dd94aSFrançois Tigeot 		if (ret != 0)
3593f2dd94aSFrançois Tigeot 			goto out_prime_destroy;
360a85cb24fSFrançois Tigeot 	}
361a85cb24fSFrançois Tigeot 
362a85cb24fSFrançois Tigeot #ifdef __DragonFly__
3633f2dd94aSFrançois Tigeot 	kdev->si_drv1 = dev;
364a85cb24fSFrançois Tigeot #endif
365a85cb24fSFrançois Tigeot 
3663f2dd94aSFrançois Tigeot 	if (drm_is_primary_client(priv)) {
3673f2dd94aSFrançois Tigeot 		ret = drm_master_open(priv);
3683f2dd94aSFrançois Tigeot 		if (ret)
3693f2dd94aSFrançois Tigeot 			goto out_close;
3703f2dd94aSFrançois Tigeot 	}
3713f2dd94aSFrançois Tigeot 
372a85cb24fSFrançois Tigeot 	mutex_lock(&dev->filelist_mutex);
373a85cb24fSFrançois Tigeot 	list_add(&priv->lhead, &dev->filelist);
374a85cb24fSFrançois Tigeot 	mutex_unlock(&dev->filelist_mutex);
375a85cb24fSFrançois Tigeot 
3763f2dd94aSFrançois Tigeot #ifdef __alpha__
3773f2dd94aSFrançois Tigeot 	/*
3783f2dd94aSFrançois Tigeot 	 * Default the hose
3793f2dd94aSFrançois Tigeot 	 */
3803f2dd94aSFrançois Tigeot 	if (!dev->hose) {
3813f2dd94aSFrançois Tigeot 		struct pci_dev *pci_dev;
3823f2dd94aSFrançois Tigeot 		pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
3833f2dd94aSFrançois Tigeot 		if (pci_dev) {
3843f2dd94aSFrançois Tigeot 			dev->hose = pci_dev->sysdata;
3853f2dd94aSFrançois Tigeot 			pci_dev_put(pci_dev);
3863f2dd94aSFrançois Tigeot 		}
3873f2dd94aSFrançois Tigeot 		if (!dev->hose) {
3883f2dd94aSFrançois Tigeot 			struct pci_bus *b = list_entry(pci_root_buses.next,
3893f2dd94aSFrançois Tigeot 				struct pci_bus, node);
3903f2dd94aSFrançois Tigeot 			if (b)
3913f2dd94aSFrançois Tigeot 				dev->hose = b->sysdata;
3923f2dd94aSFrançois Tigeot 		}
3933f2dd94aSFrançois Tigeot 	}
394a85cb24fSFrançois Tigeot #endif
3953f2dd94aSFrançois Tigeot 
3963f2dd94aSFrançois Tigeot 	return 0;
3973f2dd94aSFrançois Tigeot 
3983f2dd94aSFrançois Tigeot out_close:
399a85cb24fSFrançois Tigeot 	if (dev->driver->postclose)
400a85cb24fSFrançois Tigeot 		dev->driver->postclose(dev, priv);
4013f2dd94aSFrançois Tigeot out_prime_destroy:
4023f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_PRIME))
4033f2dd94aSFrançois Tigeot 		drm_prime_destroy_file_private(&priv->prime);
4043f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
4053f2dd94aSFrançois Tigeot 		drm_syncobj_release(priv);
406a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_GEM))
407a85cb24fSFrançois Tigeot 		drm_gem_release(dev, priv);
408a85cb24fSFrançois Tigeot 	put_pid(priv->pid);
409a85cb24fSFrançois Tigeot 	kfree(priv);
410a85cb24fSFrançois Tigeot 	filp->private_data = NULL;
411a85cb24fSFrançois Tigeot 	return ret;
412a85cb24fSFrançois Tigeot }
413a85cb24fSFrançois Tigeot 
drm_events_release(struct drm_file * file_priv)414a85cb24fSFrançois Tigeot static void drm_events_release(struct drm_file *file_priv)
415a85cb24fSFrançois Tigeot {
416a85cb24fSFrançois Tigeot 	struct drm_device *dev = file_priv->minor->dev;
417a85cb24fSFrançois Tigeot 	struct drm_pending_event *e, *et;
418a85cb24fSFrançois Tigeot 	unsigned long flags;
419a85cb24fSFrançois Tigeot 
420a85cb24fSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, flags);
421a85cb24fSFrançois Tigeot 
422a85cb24fSFrançois Tigeot 	/* Unlink pending events */
423a85cb24fSFrançois Tigeot 	list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
424a85cb24fSFrançois Tigeot 				 pending_link) {
425a85cb24fSFrançois Tigeot 		list_del(&e->pending_link);
426a85cb24fSFrançois Tigeot 		e->file_priv = NULL;
427a85cb24fSFrançois Tigeot 	}
428a85cb24fSFrançois Tigeot 
429a85cb24fSFrançois Tigeot 	/* Remove unconsumed events */
430a85cb24fSFrançois Tigeot 	list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
431a85cb24fSFrançois Tigeot 		list_del(&e->link);
432a85cb24fSFrançois Tigeot 		kfree(e);
433a85cb24fSFrançois Tigeot 	}
434a85cb24fSFrançois Tigeot 
435a85cb24fSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, flags);
436a85cb24fSFrançois Tigeot }
437a85cb24fSFrançois Tigeot 
drm_legacy_dev_reinit(struct drm_device * dev)438a85cb24fSFrançois Tigeot static void drm_legacy_dev_reinit(struct drm_device *dev)
439a85cb24fSFrançois Tigeot {
440a85cb24fSFrançois Tigeot 	if (dev->irq_enabled)
441a85cb24fSFrançois Tigeot 		drm_irq_uninstall(dev);
442a85cb24fSFrançois Tigeot 
443a85cb24fSFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
444a85cb24fSFrançois Tigeot 
445a85cb24fSFrançois Tigeot 	drm_legacy_agp_clear(dev);
446a85cb24fSFrançois Tigeot 
447a85cb24fSFrançois Tigeot 	drm_legacy_sg_cleanup(dev);
448a85cb24fSFrançois Tigeot 	drm_legacy_vma_flush(dev);
449a85cb24fSFrançois Tigeot 	drm_legacy_dma_takedown(dev);
450a85cb24fSFrançois Tigeot 
451a85cb24fSFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
452a85cb24fSFrançois Tigeot 
453a85cb24fSFrançois Tigeot 	dev->sigdata.lock = NULL;
454a85cb24fSFrançois Tigeot 
455a85cb24fSFrançois Tigeot 	dev->context_flag = 0;
456a85cb24fSFrançois Tigeot 	dev->last_context = 0;
457a85cb24fSFrançois Tigeot 	dev->if_version = 0;
458a85cb24fSFrançois Tigeot 
459a85cb24fSFrançois Tigeot 	DRM_DEBUG("lastclose completed\n");
460a85cb24fSFrançois Tigeot }
461a85cb24fSFrançois Tigeot 
drm_lastclose(struct drm_device * dev)462a85cb24fSFrançois Tigeot void drm_lastclose(struct drm_device * dev)
463a85cb24fSFrançois Tigeot {
464a85cb24fSFrançois Tigeot 	DRM_DEBUG("\n");
465a85cb24fSFrançois Tigeot 
466a85cb24fSFrançois Tigeot 	if (dev->driver->lastclose)
467a85cb24fSFrançois Tigeot 		dev->driver->lastclose(dev);
468a85cb24fSFrançois Tigeot 	DRM_DEBUG("driver lastclose completed\n");
469a85cb24fSFrançois Tigeot 
470a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
471a85cb24fSFrançois Tigeot 		drm_legacy_dev_reinit(dev);
472a85cb24fSFrançois Tigeot }
473a85cb24fSFrançois Tigeot 
474a85cb24fSFrançois Tigeot /**
475a85cb24fSFrançois Tigeot  * drm_release - release method for DRM file
476a85cb24fSFrançois Tigeot  * @inode: device inode
477a85cb24fSFrançois Tigeot  * @filp: file pointer.
478a85cb24fSFrançois Tigeot  *
4793f2dd94aSFrançois Tigeot  * This function must be used by drivers as their &file_operations.release
4803f2dd94aSFrançois Tigeot  * method. It frees any resources associated with the open file, and calls the
4813f2dd94aSFrançois Tigeot  * &drm_driver.postclose driver callback. If this is the last open file for the
4823f2dd94aSFrançois Tigeot  * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
483a85cb24fSFrançois Tigeot  *
484a85cb24fSFrançois Tigeot  * RETURNS:
485a85cb24fSFrançois Tigeot  *
486a85cb24fSFrançois Tigeot  * Always succeeds and returns 0.
487a85cb24fSFrançois Tigeot  */
drm_release(struct inode * inode,struct file * filp)488a85cb24fSFrançois Tigeot int drm_release(struct inode *inode, struct file *filp)
489a85cb24fSFrançois Tigeot {
490a85cb24fSFrançois Tigeot 	struct drm_file *file_priv = filp->private_data;
491a85cb24fSFrançois Tigeot 	struct drm_minor *minor = file_priv->minor;
492a85cb24fSFrançois Tigeot 	struct drm_device *dev = minor->dev;
493a85cb24fSFrançois Tigeot 
494a85cb24fSFrançois Tigeot #ifdef __DragonFly__
495a85cb24fSFrançois Tigeot 	/* dev is not correctly set yet */
496a85cb24fSFrançois Tigeot 	return 0;
497a85cb24fSFrançois Tigeot #endif
498a85cb24fSFrançois Tigeot 
499a85cb24fSFrançois Tigeot 	mutex_lock(&drm_global_mutex);
500a85cb24fSFrançois Tigeot 
501a85cb24fSFrançois Tigeot 	DRM_DEBUG("open_count = %d\n", dev->open_count);
502a85cb24fSFrançois Tigeot 
503a85cb24fSFrançois Tigeot 	mutex_lock(&dev->filelist_mutex);
504a85cb24fSFrançois Tigeot 	list_del(&file_priv->lhead);
505a85cb24fSFrançois Tigeot 	mutex_unlock(&dev->filelist_mutex);
506a85cb24fSFrançois Tigeot 
5073f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
5083f2dd94aSFrançois Tigeot 	    dev->driver->preclose)
509a85cb24fSFrançois Tigeot 		dev->driver->preclose(dev, file_priv);
510a85cb24fSFrançois Tigeot 
511a85cb24fSFrançois Tigeot 	/* ========================================================
512a85cb24fSFrançois Tigeot 	 * Begin inline drm_release
513a85cb24fSFrançois Tigeot 	 */
514a85cb24fSFrançois Tigeot 
5153f2dd94aSFrançois Tigeot 	DRM_DEBUG("pid = %d, device = 0x%p, open_count = %d\n",
5163f2dd94aSFrançois Tigeot 		  curproc->p_pid,
5173f2dd94aSFrançois Tigeot 		  dev,
5183f2dd94aSFrançois Tigeot 		  dev->open_count);
519a85cb24fSFrançois Tigeot 
520a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
521a85cb24fSFrançois Tigeot 		drm_legacy_lock_release(dev, filp);
522a85cb24fSFrançois Tigeot 
523a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
524a85cb24fSFrançois Tigeot 		drm_legacy_reclaim_buffers(dev, file_priv);
525a85cb24fSFrançois Tigeot 
526a85cb24fSFrançois Tigeot 	drm_events_release(file_priv);
527a85cb24fSFrançois Tigeot 
528a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
529a85cb24fSFrançois Tigeot 		drm_fb_release(file_priv);
530a85cb24fSFrançois Tigeot 		drm_property_destroy_user_blobs(dev, file_priv);
531a85cb24fSFrançois Tigeot 	}
532a85cb24fSFrançois Tigeot 
5333f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
5343f2dd94aSFrançois Tigeot 		drm_syncobj_release(file_priv);
5353f2dd94aSFrançois Tigeot 
536a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_GEM))
537a85cb24fSFrançois Tigeot 		drm_gem_release(dev, file_priv);
538a85cb24fSFrançois Tigeot 
539a85cb24fSFrançois Tigeot 	drm_legacy_ctxbitmap_flush(dev, file_priv);
540a85cb24fSFrançois Tigeot 
541a85cb24fSFrançois Tigeot 	if (drm_is_primary_client(file_priv))
542a85cb24fSFrançois Tigeot 		drm_master_release(file_priv);
543a85cb24fSFrançois Tigeot 
544a85cb24fSFrançois Tigeot 	if (dev->driver->postclose)
545a85cb24fSFrançois Tigeot 		dev->driver->postclose(dev, file_priv);
546a85cb24fSFrançois Tigeot 
547a85cb24fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_PRIME))
548a85cb24fSFrançois Tigeot 		drm_prime_destroy_file_private(&file_priv->prime);
549a85cb24fSFrançois Tigeot 
550a85cb24fSFrançois Tigeot 	WARN_ON(!list_empty(&file_priv->event_list));
551a85cb24fSFrançois Tigeot 
552a85cb24fSFrançois Tigeot 	put_pid(file_priv->pid);
553a85cb24fSFrançois Tigeot 	kfree(file_priv);
554a85cb24fSFrançois Tigeot 
555a85cb24fSFrançois Tigeot 	/* ========================================================
556a85cb24fSFrançois Tigeot 	 * End inline drm_release
557a85cb24fSFrançois Tigeot 	 */
558a85cb24fSFrançois Tigeot 
559a85cb24fSFrançois Tigeot 	if (!--dev->open_count) {
560a85cb24fSFrançois Tigeot 		drm_lastclose(dev);
561a85cb24fSFrançois Tigeot #if 0	/* XXX: drm_put_dev() not implemented */
5623f2dd94aSFrançois Tigeot 		if (drm_dev_is_unplugged(dev))
563a85cb24fSFrançois Tigeot 			drm_put_dev(dev);
564a85cb24fSFrançois Tigeot #endif
565a85cb24fSFrançois Tigeot 	}
566a85cb24fSFrançois Tigeot 	mutex_unlock(&drm_global_mutex);
567a85cb24fSFrançois Tigeot 
568a85cb24fSFrançois Tigeot 	drm_minor_release(minor);
569a85cb24fSFrançois Tigeot 
570a85cb24fSFrançois Tigeot 	return 0;
571a85cb24fSFrançois Tigeot }
572a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_release);
573a85cb24fSFrançois Tigeot 
574a85cb24fSFrançois Tigeot /**
575a85cb24fSFrançois Tigeot  * drm_read - read method for DRM file
576a85cb24fSFrançois Tigeot  * @filp: file pointer
577a85cb24fSFrançois Tigeot  * @buffer: userspace destination pointer for the read
578a85cb24fSFrançois Tigeot  * @count: count in bytes to read
579a85cb24fSFrançois Tigeot  * @offset: offset to read
580a85cb24fSFrançois Tigeot  *
5813f2dd94aSFrançois Tigeot  * This function must be used by drivers as their &file_operations.read
582a85cb24fSFrançois Tigeot  * method iff they use DRM events for asynchronous signalling to userspace.
583a85cb24fSFrançois Tigeot  * Since events are used by the KMS API for vblank and page flip completion this
584a85cb24fSFrançois Tigeot  * means all modern display drivers must use it.
585a85cb24fSFrançois Tigeot  *
5863f2dd94aSFrançois Tigeot  * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
5873f2dd94aSFrançois Tigeot  * must set the &file_operation.llseek to no_llseek(). Polling support is
588a85cb24fSFrançois Tigeot  * provided by drm_poll().
589a85cb24fSFrançois Tigeot  *
590a85cb24fSFrançois Tigeot  * This function will only ever read a full event. Therefore userspace must
591a85cb24fSFrançois Tigeot  * supply a big enough buffer to fit any event to ensure forward progress. Since
592a85cb24fSFrançois Tigeot  * the maximum event space is currently 4K it's recommended to just use that for
593a85cb24fSFrançois Tigeot  * safety.
594a85cb24fSFrançois Tigeot  *
595a85cb24fSFrançois Tigeot  * RETURNS:
596a85cb24fSFrançois Tigeot  *
597a85cb24fSFrançois Tigeot  * Number of bytes read (always aligned to full events, and can be 0) or a
598a85cb24fSFrançois Tigeot  * negative error code on failure.
599a85cb24fSFrançois Tigeot  */
600a85cb24fSFrançois Tigeot /*
601a85cb24fSFrançois Tigeot ssize_t drm_read(struct file *filp, char __user *buffer,
602a85cb24fSFrançois Tigeot 		 size_t count, loff_t *offset)
603a85cb24fSFrançois Tigeot */
drm_read(struct dev_read_args * ap)604a85cb24fSFrançois Tigeot int drm_read(struct dev_read_args *ap)
605a85cb24fSFrançois Tigeot {
606a85cb24fSFrançois Tigeot 	struct file *filp = ap->a_fp;
607a85cb24fSFrançois Tigeot 	struct uio *uio = ap->a_uio;
608a85cb24fSFrançois Tigeot 	size_t count = uio->uio_resid;
609a85cb24fSFrançois Tigeot 	struct drm_file *file_priv = filp->private_data;
6103f2dd94aSFrançois Tigeot 	struct drm_device *dev = file_priv->minor->dev;
611a85cb24fSFrançois Tigeot 	int ret = 0;	/* drm_read() returns int in DragonFly */
612a85cb24fSFrançois Tigeot 
613a85cb24fSFrançois Tigeot 	ret = mutex_lock_interruptible(&file_priv->event_read_lock);
614a85cb24fSFrançois Tigeot 	if (ret)
615a85cb24fSFrançois Tigeot 		return ret;
616a85cb24fSFrançois Tigeot 
617a85cb24fSFrançois Tigeot 	for (;;) {
618a85cb24fSFrançois Tigeot 		struct drm_pending_event *e = NULL;
619a85cb24fSFrançois Tigeot 
620a85cb24fSFrançois Tigeot 		spin_lock_irq(&dev->event_lock);
621a85cb24fSFrançois Tigeot 		if (!list_empty(&file_priv->event_list)) {
622a85cb24fSFrançois Tigeot 			e = list_first_entry(&file_priv->event_list,
623a85cb24fSFrançois Tigeot 					struct drm_pending_event, link);
624a85cb24fSFrançois Tigeot 			file_priv->event_space += e->event->length;
625a85cb24fSFrançois Tigeot 			list_del(&e->link);
626a85cb24fSFrançois Tigeot 		}
627a85cb24fSFrançois Tigeot 		spin_unlock_irq(&dev->event_lock);
628a85cb24fSFrançois Tigeot 
629a85cb24fSFrançois Tigeot 		if (e == NULL) {
630a85cb24fSFrançois Tigeot 			if (ret) {
631a85cb24fSFrançois Tigeot 				ret = 0;	/* DragonFly expects a zero return value on success */
632a85cb24fSFrançois Tigeot 				break;
633a85cb24fSFrançois Tigeot 			}
634a85cb24fSFrançois Tigeot 
635a85cb24fSFrançois Tigeot 			if (filp->f_flag & O_NONBLOCK) {
636a85cb24fSFrançois Tigeot 				ret = -EAGAIN;
637a85cb24fSFrançois Tigeot 				break;
638a85cb24fSFrançois Tigeot 			}
639a85cb24fSFrançois Tigeot 
640a85cb24fSFrançois Tigeot 			mutex_unlock(&file_priv->event_read_lock);
641a85cb24fSFrançois Tigeot 			ret = wait_event_interruptible(file_priv->event_wait,
642a85cb24fSFrançois Tigeot 						       !list_empty(&file_priv->event_list));
643a85cb24fSFrançois Tigeot 			if (ret >= 0)
644a85cb24fSFrançois Tigeot 				ret = mutex_lock_interruptible(&file_priv->event_read_lock);
645a85cb24fSFrançois Tigeot 			if (ret)
646a85cb24fSFrançois Tigeot 				return ret;
647a85cb24fSFrançois Tigeot 		} else {
648a85cb24fSFrançois Tigeot 			unsigned length = e->event->length;
649a85cb24fSFrançois Tigeot 
650a85cb24fSFrançois Tigeot 			if (length > count - ret) {
651a85cb24fSFrançois Tigeot put_back_event:
652a85cb24fSFrançois Tigeot 				spin_lock_irq(&dev->event_lock);
653a85cb24fSFrançois Tigeot 				file_priv->event_space -= length;
654a85cb24fSFrançois Tigeot 				list_add(&e->link, &file_priv->event_list);
655a85cb24fSFrançois Tigeot 				spin_unlock_irq(&dev->event_lock);
656a85cb24fSFrançois Tigeot 				break;
657a85cb24fSFrançois Tigeot 			}
658a85cb24fSFrançois Tigeot 
659a85cb24fSFrançois Tigeot 			if (uiomove((caddr_t)e->event, length, uio)) {
660a85cb24fSFrançois Tigeot 				if (ret == 0)
661a85cb24fSFrançois Tigeot 					ret = -EFAULT;
662a85cb24fSFrançois Tigeot 				goto put_back_event;
663a85cb24fSFrançois Tigeot 			}
664a85cb24fSFrançois Tigeot 
665a85cb24fSFrançois Tigeot 			ret += length;
666a85cb24fSFrançois Tigeot 			kfree(e);
667a85cb24fSFrançois Tigeot 		}
668a85cb24fSFrançois Tigeot 	}
669a85cb24fSFrançois Tigeot 	mutex_unlock(&file_priv->event_read_lock);
670a85cb24fSFrançois Tigeot 
671a85cb24fSFrançois Tigeot 	return ret;
672a85cb24fSFrançois Tigeot }
673a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_read);
674a85cb24fSFrançois Tigeot 
675a85cb24fSFrançois Tigeot /**
676a85cb24fSFrançois Tigeot  * drm_poll - poll method for DRM file
677a85cb24fSFrançois Tigeot  * @filp: file pointer
678a85cb24fSFrançois Tigeot  * @wait: poll waiter table
679a85cb24fSFrançois Tigeot  *
6803f2dd94aSFrançois Tigeot  * This function must be used by drivers as their &file_operations.read method
6813f2dd94aSFrançois Tigeot  * iff they use DRM events for asynchronous signalling to userspace.  Since
6823f2dd94aSFrançois Tigeot  * events are used by the KMS API for vblank and page flip completion this means
6833f2dd94aSFrançois Tigeot  * all modern display drivers must use it.
684a85cb24fSFrançois Tigeot  *
685a85cb24fSFrançois Tigeot  * See also drm_read().
686a85cb24fSFrançois Tigeot  *
687a85cb24fSFrançois Tigeot  * RETURNS:
688a85cb24fSFrançois Tigeot  *
689a85cb24fSFrançois Tigeot  * Mask of POLL flags indicating the current status of the file.
690a85cb24fSFrançois Tigeot  */
691a85cb24fSFrançois Tigeot static int
drmfilt(struct knote * kn,long hint)692a85cb24fSFrançois Tigeot drmfilt(struct knote *kn, long hint)
693a85cb24fSFrançois Tigeot {
694a85cb24fSFrançois Tigeot 	struct drm_file *file_priv = (struct drm_file *)kn->kn_hook;
695a85cb24fSFrançois Tigeot 	int ready = 0;
696a85cb24fSFrançois Tigeot 
697a85cb24fSFrançois Tigeot //	poll_wait(filp, &file_priv->event_wait, wait);
698a85cb24fSFrançois Tigeot 
699a85cb24fSFrançois Tigeot 	if (!list_empty(&file_priv->event_list))
700a85cb24fSFrançois Tigeot 		ready = 1;
701a85cb24fSFrançois Tigeot 
702a85cb24fSFrançois Tigeot 	return (ready);
703a85cb24fSFrançois Tigeot }
704a85cb24fSFrançois Tigeot 
705a85cb24fSFrançois Tigeot static void
drmfilt_detach(struct knote * kn)706a85cb24fSFrançois Tigeot drmfilt_detach(struct knote *kn)
707a85cb24fSFrançois Tigeot {
708a85cb24fSFrançois Tigeot 	struct drm_file *file_priv;
709a85cb24fSFrançois Tigeot 	struct klist *klist;
710a85cb24fSFrançois Tigeot 
711a85cb24fSFrançois Tigeot 	file_priv = (struct drm_file *)kn->kn_hook;
712a85cb24fSFrançois Tigeot 
713a85cb24fSFrançois Tigeot 	klist = &file_priv->dkq.ki_note;
714a85cb24fSFrançois Tigeot 	knote_remove(klist, kn);
715a85cb24fSFrançois Tigeot }
716a85cb24fSFrançois Tigeot 
717a85cb24fSFrançois Tigeot static struct filterops drmfiltops =
718a85cb24fSFrançois Tigeot         { FILTEROP_MPSAFE | FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
719a85cb24fSFrançois Tigeot 
720a85cb24fSFrançois Tigeot int
drm_kqfilter(struct dev_kqfilter_args * ap)721a85cb24fSFrançois Tigeot drm_kqfilter(struct dev_kqfilter_args *ap)
722a85cb24fSFrançois Tigeot {
723a85cb24fSFrançois Tigeot 	struct file *filp = ap->a_fp;
724a85cb24fSFrançois Tigeot 	struct drm_file *file_priv = filp->private_data;
725a85cb24fSFrançois Tigeot 	struct knote *kn = ap->a_kn;
726a85cb24fSFrançois Tigeot 	struct klist *klist;
727a85cb24fSFrançois Tigeot 
728a85cb24fSFrançois Tigeot 	ap->a_result = 0;
729a85cb24fSFrançois Tigeot 
730a85cb24fSFrançois Tigeot 	switch (kn->kn_filter) {
731a85cb24fSFrançois Tigeot 	case EVFILT_READ:
732a85cb24fSFrançois Tigeot 	case EVFILT_WRITE:
733a85cb24fSFrançois Tigeot 		kn->kn_fop = &drmfiltops;
734a85cb24fSFrançois Tigeot 		kn->kn_hook = (caddr_t)file_priv;
735a85cb24fSFrançois Tigeot 		break;
736a85cb24fSFrançois Tigeot 	default:
737a85cb24fSFrançois Tigeot 		ap->a_result = EOPNOTSUPP;
738a85cb24fSFrançois Tigeot 		return (0);
739a85cb24fSFrançois Tigeot 	}
740a85cb24fSFrançois Tigeot 
741a85cb24fSFrançois Tigeot 	klist = &file_priv->dkq.ki_note;
742a85cb24fSFrançois Tigeot 	knote_insert(klist, kn);
743a85cb24fSFrançois Tigeot 
744a85cb24fSFrançois Tigeot 	return (0);
745a85cb24fSFrançois Tigeot }
746a85cb24fSFrançois Tigeot 
747a85cb24fSFrançois Tigeot /**
748a85cb24fSFrançois Tigeot  * drm_event_reserve_init_locked - init a DRM event and reserve space for it
749a85cb24fSFrançois Tigeot  * @dev: DRM device
750a85cb24fSFrançois Tigeot  * @file_priv: DRM file private data
751a85cb24fSFrançois Tigeot  * @p: tracking structure for the pending event
752a85cb24fSFrançois Tigeot  * @e: actual event data to deliver to userspace
753a85cb24fSFrançois Tigeot  *
754a85cb24fSFrançois Tigeot  * This function prepares the passed in event for eventual delivery. If the event
755a85cb24fSFrançois Tigeot  * doesn't get delivered (because the IOCTL fails later on, before queuing up
756a85cb24fSFrançois Tigeot  * anything) then the even must be cancelled and freed using
757a85cb24fSFrançois Tigeot  * drm_event_cancel_free(). Successfully initialized events should be sent out
758a85cb24fSFrançois Tigeot  * using drm_send_event() or drm_send_event_locked() to signal completion of the
759a85cb24fSFrançois Tigeot  * asynchronous event to userspace.
760a85cb24fSFrançois Tigeot  *
761a85cb24fSFrançois Tigeot  * If callers embedded @p into a larger structure it must be allocated with
762a85cb24fSFrançois Tigeot  * kmalloc and @p must be the first member element.
763a85cb24fSFrançois Tigeot  *
764a85cb24fSFrançois Tigeot  * This is the locked version of drm_event_reserve_init() for callers which
765a85cb24fSFrançois Tigeot  * already hold &drm_device.event_lock.
766a85cb24fSFrançois Tigeot  *
767a85cb24fSFrançois Tigeot  * RETURNS:
768a85cb24fSFrançois Tigeot  *
769a85cb24fSFrançois Tigeot  * 0 on success or a negative error code on failure.
770a85cb24fSFrançois Tigeot  */
drm_event_reserve_init_locked(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)771a85cb24fSFrançois Tigeot int drm_event_reserve_init_locked(struct drm_device *dev,
772a85cb24fSFrançois Tigeot 				  struct drm_file *file_priv,
773a85cb24fSFrançois Tigeot 				  struct drm_pending_event *p,
774a85cb24fSFrançois Tigeot 				  struct drm_event *e)
775a85cb24fSFrançois Tigeot {
776a85cb24fSFrançois Tigeot 	if (file_priv->event_space < e->length)
777a85cb24fSFrançois Tigeot 		return -ENOMEM;
778a85cb24fSFrançois Tigeot 
779a85cb24fSFrançois Tigeot 	file_priv->event_space -= e->length;
780a85cb24fSFrançois Tigeot 
781a85cb24fSFrançois Tigeot 	p->event = e;
782a85cb24fSFrançois Tigeot 	list_add(&p->pending_link, &file_priv->pending_event_list);
783a85cb24fSFrançois Tigeot 	p->file_priv = file_priv;
784a85cb24fSFrançois Tigeot 
785a85cb24fSFrançois Tigeot 	return 0;
786a85cb24fSFrançois Tigeot }
787a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_event_reserve_init_locked);
788a85cb24fSFrançois Tigeot 
789a85cb24fSFrançois Tigeot /**
790a85cb24fSFrançois Tigeot  * drm_event_reserve_init - init a DRM event and reserve space for it
791a85cb24fSFrançois Tigeot  * @dev: DRM device
792a85cb24fSFrançois Tigeot  * @file_priv: DRM file private data
793a85cb24fSFrançois Tigeot  * @p: tracking structure for the pending event
794a85cb24fSFrançois Tigeot  * @e: actual event data to deliver to userspace
795a85cb24fSFrançois Tigeot  *
796a85cb24fSFrançois Tigeot  * This function prepares the passed in event for eventual delivery. If the event
797a85cb24fSFrançois Tigeot  * doesn't get delivered (because the IOCTL fails later on, before queuing up
798a85cb24fSFrançois Tigeot  * anything) then the even must be cancelled and freed using
799a85cb24fSFrançois Tigeot  * drm_event_cancel_free(). Successfully initialized events should be sent out
800a85cb24fSFrançois Tigeot  * using drm_send_event() or drm_send_event_locked() to signal completion of the
801a85cb24fSFrançois Tigeot  * asynchronous event to userspace.
802a85cb24fSFrançois Tigeot  *
803a85cb24fSFrançois Tigeot  * If callers embedded @p into a larger structure it must be allocated with
804a85cb24fSFrançois Tigeot  * kmalloc and @p must be the first member element.
805a85cb24fSFrançois Tigeot  *
806a85cb24fSFrançois Tigeot  * Callers which already hold &drm_device.event_lock should use
807a85cb24fSFrançois Tigeot  * drm_event_reserve_init_locked() instead.
808a85cb24fSFrançois Tigeot  *
809a85cb24fSFrançois Tigeot  * RETURNS:
810a85cb24fSFrançois Tigeot  *
811a85cb24fSFrançois Tigeot  * 0 on success or a negative error code on failure.
812a85cb24fSFrançois Tigeot  */
drm_event_reserve_init(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)813a85cb24fSFrançois Tigeot int drm_event_reserve_init(struct drm_device *dev,
814a85cb24fSFrançois Tigeot 			   struct drm_file *file_priv,
815a85cb24fSFrançois Tigeot 			   struct drm_pending_event *p,
816a85cb24fSFrançois Tigeot 			   struct drm_event *e)
817a85cb24fSFrançois Tigeot {
818a85cb24fSFrançois Tigeot 	unsigned long flags;
819a85cb24fSFrançois Tigeot 	int ret;
820a85cb24fSFrançois Tigeot 
821a85cb24fSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, flags);
822a85cb24fSFrançois Tigeot 	ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
823a85cb24fSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, flags);
824a85cb24fSFrançois Tigeot 
825a85cb24fSFrançois Tigeot 	return ret;
826a85cb24fSFrançois Tigeot }
827a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_event_reserve_init);
828a85cb24fSFrançois Tigeot 
829a85cb24fSFrançois Tigeot /**
830a85cb24fSFrançois Tigeot  * drm_event_cancel_free - free a DRM event and release it's space
831a85cb24fSFrançois Tigeot  * @dev: DRM device
832a85cb24fSFrançois Tigeot  * @p: tracking structure for the pending event
833a85cb24fSFrançois Tigeot  *
834a85cb24fSFrançois Tigeot  * This function frees the event @p initialized with drm_event_reserve_init()
8353f2dd94aSFrançois Tigeot  * and releases any allocated space. It is used to cancel an event when the
8363f2dd94aSFrançois Tigeot  * nonblocking operation could not be submitted and needed to be aborted.
837a85cb24fSFrançois Tigeot  */
drm_event_cancel_free(struct drm_device * dev,struct drm_pending_event * p)838a85cb24fSFrançois Tigeot void drm_event_cancel_free(struct drm_device *dev,
839a85cb24fSFrançois Tigeot 			   struct drm_pending_event *p)
840a85cb24fSFrançois Tigeot {
841a85cb24fSFrançois Tigeot 	unsigned long flags;
842a85cb24fSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, flags);
843a85cb24fSFrançois Tigeot 	if (p->file_priv) {
844a85cb24fSFrançois Tigeot 		p->file_priv->event_space += p->event->length;
845a85cb24fSFrançois Tigeot 		list_del(&p->pending_link);
846a85cb24fSFrançois Tigeot 	}
847a85cb24fSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, flags);
8483f2dd94aSFrançois Tigeot 
8493f2dd94aSFrançois Tigeot 	if (p->fence)
8503f2dd94aSFrançois Tigeot 		dma_fence_put(p->fence);
8513f2dd94aSFrançois Tigeot 
852a85cb24fSFrançois Tigeot 	kfree(p);
853a85cb24fSFrançois Tigeot }
854a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_event_cancel_free);
855a85cb24fSFrançois Tigeot 
856a85cb24fSFrançois Tigeot /**
857a85cb24fSFrançois Tigeot  * drm_send_event_locked - send DRM event to file descriptor
858a85cb24fSFrançois Tigeot  * @dev: DRM device
859a85cb24fSFrançois Tigeot  * @e: DRM event to deliver
860a85cb24fSFrançois Tigeot  *
861a85cb24fSFrançois Tigeot  * This function sends the event @e, initialized with drm_event_reserve_init(),
862a85cb24fSFrançois Tigeot  * to its associated userspace DRM file. Callers must already hold
863a85cb24fSFrançois Tigeot  * &drm_device.event_lock, see drm_send_event() for the unlocked version.
864a85cb24fSFrançois Tigeot  *
865a85cb24fSFrançois Tigeot  * Note that the core will take care of unlinking and disarming events when the
866a85cb24fSFrançois Tigeot  * corresponding DRM file is closed. Drivers need not worry about whether the
867a85cb24fSFrançois Tigeot  * DRM file for this event still exists and can call this function upon
868a85cb24fSFrançois Tigeot  * completion of the asynchronous work unconditionally.
869a85cb24fSFrançois Tigeot  */
drm_send_event_locked(struct drm_device * dev,struct drm_pending_event * e)870a85cb24fSFrançois Tigeot void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
871a85cb24fSFrançois Tigeot {
872a85cb24fSFrançois Tigeot 	assert_spin_locked(&dev->event_lock);
873a85cb24fSFrançois Tigeot 
874a85cb24fSFrançois Tigeot 	if (e->completion) {
875a85cb24fSFrançois Tigeot 		complete_all(e->completion);
876a85cb24fSFrançois Tigeot 		e->completion_release(e->completion);
877a85cb24fSFrançois Tigeot 		e->completion = NULL;
878a85cb24fSFrançois Tigeot 	}
879a85cb24fSFrançois Tigeot 
880a85cb24fSFrançois Tigeot 	if (e->fence) {
881a85cb24fSFrançois Tigeot 		dma_fence_signal(e->fence);
882a85cb24fSFrançois Tigeot 		dma_fence_put(e->fence);
883a85cb24fSFrançois Tigeot 	}
884a85cb24fSFrançois Tigeot 
885a85cb24fSFrançois Tigeot 	if (!e->file_priv) {
886a85cb24fSFrançois Tigeot 		kfree(e);
887a85cb24fSFrançois Tigeot 		return;
888a85cb24fSFrançois Tigeot 	}
889a85cb24fSFrançois Tigeot 
890a85cb24fSFrançois Tigeot 	list_del(&e->pending_link);
891a85cb24fSFrançois Tigeot 	list_add_tail(&e->link,
892a85cb24fSFrançois Tigeot 		      &e->file_priv->event_list);
893a85cb24fSFrançois Tigeot 	wake_up_interruptible(&e->file_priv->event_wait);
894a85cb24fSFrançois Tigeot #ifdef __DragonFly__
895a85cb24fSFrançois Tigeot 	KNOTE(&e->file_priv->dkq.ki_note, 0);
896a85cb24fSFrançois Tigeot #endif
897a85cb24fSFrançois Tigeot }
898a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_send_event_locked);
899a85cb24fSFrançois Tigeot 
900a85cb24fSFrançois Tigeot /**
901a85cb24fSFrançois Tigeot  * drm_send_event - send DRM event to file descriptor
902a85cb24fSFrançois Tigeot  * @dev: DRM device
903a85cb24fSFrançois Tigeot  * @e: DRM event to deliver
904a85cb24fSFrançois Tigeot  *
905a85cb24fSFrançois Tigeot  * This function sends the event @e, initialized with drm_event_reserve_init(),
906a85cb24fSFrançois Tigeot  * to its associated userspace DRM file. This function acquires
907a85cb24fSFrançois Tigeot  * &drm_device.event_lock, see drm_send_event_locked() for callers which already
908a85cb24fSFrançois Tigeot  * hold this lock.
909a85cb24fSFrançois Tigeot  *
910a85cb24fSFrançois Tigeot  * Note that the core will take care of unlinking and disarming events when the
911a85cb24fSFrançois Tigeot  * corresponding DRM file is closed. Drivers need not worry about whether the
912a85cb24fSFrançois Tigeot  * DRM file for this event still exists and can call this function upon
913a85cb24fSFrançois Tigeot  * completion of the asynchronous work unconditionally.
914a85cb24fSFrançois Tigeot  */
drm_send_event(struct drm_device * dev,struct drm_pending_event * e)915a85cb24fSFrançois Tigeot void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
916a85cb24fSFrançois Tigeot {
917a85cb24fSFrançois Tigeot 	unsigned long irqflags;
918a85cb24fSFrançois Tigeot 
919a85cb24fSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, irqflags);
920a85cb24fSFrançois Tigeot 	drm_send_event_locked(dev, e);
921a85cb24fSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
922a85cb24fSFrançois Tigeot }
923a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_send_event);
924