xref: /freebsd/sys/dev/drm2/drm_fops.c (revision c9130a46bca272492b4f29429ed340504b8eced7)
1592ffb21SWarner Losh /**
2592ffb21SWarner Losh  * \file drm_fops.c
3592ffb21SWarner Losh  * File operations for DRM
4592ffb21SWarner Losh  *
5592ffb21SWarner Losh  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6592ffb21SWarner Losh  * \author Daryll Strauss <daryll@valinux.com>
7592ffb21SWarner Losh  * \author Gareth Hughes <gareth@valinux.com>
8592ffb21SWarner Losh  */
9592ffb21SWarner Losh 
10592ffb21SWarner Losh /*
11592ffb21SWarner Losh  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
12592ffb21SWarner Losh  *
13592ffb21SWarner Losh  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14592ffb21SWarner Losh  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15592ffb21SWarner Losh  * All Rights Reserved.
16592ffb21SWarner Losh  *
17592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
18592ffb21SWarner Losh  * copy of this software and associated documentation files (the "Software"),
19592ffb21SWarner Losh  * to deal in the Software without restriction, including without limitation
20592ffb21SWarner Losh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21592ffb21SWarner Losh  * and/or sell copies of the Software, and to permit persons to whom the
22592ffb21SWarner Losh  * Software is furnished to do so, subject to the following conditions:
23592ffb21SWarner Losh  *
24592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the next
25592ffb21SWarner Losh  * paragraph) shall be included in all copies or substantial portions of the
26592ffb21SWarner Losh  * Software.
27592ffb21SWarner Losh  *
28592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31592ffb21SWarner Losh  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32592ffb21SWarner Losh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33592ffb21SWarner Losh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34592ffb21SWarner Losh  * OTHER DEALINGS IN THE SOFTWARE.
35592ffb21SWarner Losh  */
36592ffb21SWarner Losh 
37592ffb21SWarner Losh #include <sys/cdefs.h>
38592ffb21SWarner Losh #include <dev/drm2/drmP.h>
39592ffb21SWarner Losh 
40592ffb21SWarner Losh static int drm_open_helper(struct cdev *kdev, int flags, int fmt,
41592ffb21SWarner Losh 			   DRM_STRUCTPROC *p, struct drm_device *dev);
42592ffb21SWarner Losh 
drm_setup(struct drm_device * dev)43592ffb21SWarner Losh static int drm_setup(struct drm_device * dev)
44592ffb21SWarner Losh {
45592ffb21SWarner Losh 	int i;
46592ffb21SWarner Losh 	int ret;
47592ffb21SWarner Losh 
48592ffb21SWarner Losh 	if (dev->driver->firstopen) {
49592ffb21SWarner Losh 		ret = dev->driver->firstopen(dev);
50592ffb21SWarner Losh 		if (ret != 0)
51592ffb21SWarner Losh 			return ret;
52592ffb21SWarner Losh 	}
53592ffb21SWarner Losh 
54592ffb21SWarner Losh 	atomic_set(&dev->ioctl_count, 0);
55592ffb21SWarner Losh 	atomic_set(&dev->vma_count, 0);
56592ffb21SWarner Losh 
57592ffb21SWarner Losh 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
58592ffb21SWarner Losh 	    !drm_core_check_feature(dev, DRIVER_MODESET)) {
59592ffb21SWarner Losh 		dev->buf_use = 0;
60592ffb21SWarner Losh 		atomic_set(&dev->buf_alloc, 0);
61592ffb21SWarner Losh 
62592ffb21SWarner Losh 		i = drm_dma_setup(dev);
63592ffb21SWarner Losh 		if (i < 0)
64592ffb21SWarner Losh 			return i;
65592ffb21SWarner Losh 	}
66592ffb21SWarner Losh 
67592ffb21SWarner Losh 	/*
68592ffb21SWarner Losh 	 * FIXME Linux<->FreeBSD: counter incremented in drm_open() and
69592ffb21SWarner Losh 	 * reset to 0 here.
70592ffb21SWarner Losh 	 */
71592ffb21SWarner Losh #if 0
72592ffb21SWarner Losh 	for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
73592ffb21SWarner Losh 		atomic_set(&dev->counts[i], 0);
74592ffb21SWarner Losh #endif
75592ffb21SWarner Losh 
76592ffb21SWarner Losh 	dev->sigdata.lock = NULL;
77592ffb21SWarner Losh 
78592ffb21SWarner Losh 	dev->context_flag = 0;
79592ffb21SWarner Losh 	dev->interrupt_flag = 0;
80592ffb21SWarner Losh 	dev->dma_flag = 0;
81592ffb21SWarner Losh 	dev->last_context = 0;
82592ffb21SWarner Losh 	dev->last_switch = 0;
83592ffb21SWarner Losh 	dev->last_checked = 0;
84592ffb21SWarner Losh 	DRM_INIT_WAITQUEUE(&dev->context_wait);
85592ffb21SWarner Losh 	dev->if_version = 0;
86592ffb21SWarner Losh 
87592ffb21SWarner Losh #ifdef FREEBSD_NOTYET
88592ffb21SWarner Losh 	dev->ctx_start = 0;
89592ffb21SWarner Losh 	dev->lck_start = 0;
90592ffb21SWarner Losh 
91592ffb21SWarner Losh 	dev->buf_async = NULL;
92592ffb21SWarner Losh 	DRM_INIT_WAITQUEUE(&dev->buf_readers);
93592ffb21SWarner Losh 	DRM_INIT_WAITQUEUE(&dev->buf_writers);
94592ffb21SWarner Losh #endif /* FREEBSD_NOTYET */
95592ffb21SWarner Losh 
96592ffb21SWarner Losh 	DRM_DEBUG("\n");
97592ffb21SWarner Losh 
98592ffb21SWarner Losh 	/*
99592ffb21SWarner Losh 	 * The kernel's context could be created here, but is now created
100592ffb21SWarner Losh 	 * in drm_dma_enqueue.  This is more resource-efficient for
101592ffb21SWarner Losh 	 * hardware that does not do DMA, but may mean that
102592ffb21SWarner Losh 	 * drm_select_queue fails between the time the interrupt is
103592ffb21SWarner Losh 	 * initialized and the time the queues are initialized.
104592ffb21SWarner Losh 	 */
105592ffb21SWarner Losh 
106592ffb21SWarner Losh 	return 0;
107592ffb21SWarner Losh }
108592ffb21SWarner Losh 
109592ffb21SWarner Losh /**
110592ffb21SWarner Losh  * Open file.
111592ffb21SWarner Losh  *
112592ffb21SWarner Losh  * \param inode device inode
113592ffb21SWarner Losh  * \param filp file pointer.
114592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
115592ffb21SWarner Losh  *
116592ffb21SWarner Losh  * Searches the DRM device with the same minor number, calls open_helper(), and
117592ffb21SWarner Losh  * increments the device open count. If the open count was previous at zero,
118592ffb21SWarner Losh  * i.e., it's the first that the device is open, then calls setup().
119592ffb21SWarner Losh  */
drm_open(struct cdev * kdev,int flags,int fmt,DRM_STRUCTPROC * p)120592ffb21SWarner Losh int drm_open(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p)
121592ffb21SWarner Losh {
122592ffb21SWarner Losh 	struct drm_device *dev = NULL;
123592ffb21SWarner Losh 	struct drm_minor *minor;
124592ffb21SWarner Losh 	int retcode = 0;
125592ffb21SWarner Losh 	int need_setup = 0;
126592ffb21SWarner Losh 
127592ffb21SWarner Losh 	minor = kdev->si_drv1;
128592ffb21SWarner Losh 	if (!minor)
129592ffb21SWarner Losh 		return ENODEV;
130592ffb21SWarner Losh 
131592ffb21SWarner Losh 	if (!(dev = minor->dev))
132592ffb21SWarner Losh 		return ENODEV;
133592ffb21SWarner Losh 
134592ffb21SWarner Losh 	sx_xlock(&drm_global_mutex);
135592ffb21SWarner Losh 
136592ffb21SWarner Losh 	/*
137592ffb21SWarner Losh 	 * FIXME Linux<->FreeBSD: On Linux, counter updated outside
138592ffb21SWarner Losh 	 * global mutex.
139592ffb21SWarner Losh 	 */
140592ffb21SWarner Losh 	if (!dev->open_count++)
141592ffb21SWarner Losh 		need_setup = 1;
142592ffb21SWarner Losh 
143592ffb21SWarner Losh 	retcode = drm_open_helper(kdev, flags, fmt, p, dev);
144592ffb21SWarner Losh 	if (retcode) {
145592ffb21SWarner Losh 		sx_xunlock(&drm_global_mutex);
146592ffb21SWarner Losh 		return (-retcode);
147592ffb21SWarner Losh 	}
148592ffb21SWarner Losh 	atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
149592ffb21SWarner Losh 	if (need_setup) {
150592ffb21SWarner Losh 		retcode = drm_setup(dev);
151592ffb21SWarner Losh 		if (retcode)
152592ffb21SWarner Losh 			goto err_undo;
153592ffb21SWarner Losh 	}
154592ffb21SWarner Losh 	sx_xunlock(&drm_global_mutex);
155592ffb21SWarner Losh 	return 0;
156592ffb21SWarner Losh 
157592ffb21SWarner Losh err_undo:
158592ffb21SWarner Losh 	device_unbusy(dev->dev);
159592ffb21SWarner Losh 	dev->open_count--;
160592ffb21SWarner Losh 	sx_xunlock(&drm_global_mutex);
161592ffb21SWarner Losh 	return -retcode;
162592ffb21SWarner Losh }
163592ffb21SWarner Losh EXPORT_SYMBOL(drm_open);
164592ffb21SWarner Losh 
165592ffb21SWarner Losh /**
166592ffb21SWarner Losh  * Called whenever a process opens /dev/drm.
167592ffb21SWarner Losh  *
168592ffb21SWarner Losh  * \param inode device inode.
169592ffb21SWarner Losh  * \param filp file pointer.
170592ffb21SWarner Losh  * \param dev device.
171592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
172592ffb21SWarner Losh  *
173592ffb21SWarner Losh  * Creates and initializes a drm_file structure for the file private data in \p
174592ffb21SWarner Losh  * filp and add it into the double linked list in \p dev.
175592ffb21SWarner Losh  */
drm_open_helper(struct cdev * kdev,int flags,int fmt,DRM_STRUCTPROC * p,struct drm_device * dev)176592ffb21SWarner Losh static int drm_open_helper(struct cdev *kdev, int flags, int fmt,
177592ffb21SWarner Losh 			   DRM_STRUCTPROC *p, struct drm_device *dev)
178592ffb21SWarner Losh {
179592ffb21SWarner Losh 	struct drm_file *priv;
180592ffb21SWarner Losh 	int ret;
181592ffb21SWarner Losh 
182592ffb21SWarner Losh 	if (flags & O_EXCL)
183592ffb21SWarner Losh 		return -EBUSY;	/* No exclusive opens */
184592ffb21SWarner Losh 	if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
185592ffb21SWarner Losh 		return -EINVAL;
186592ffb21SWarner Losh 
187592ffb21SWarner Losh 	DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
188592ffb21SWarner Losh 
189592ffb21SWarner Losh 	priv = malloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
190592ffb21SWarner Losh 	if (!priv)
191592ffb21SWarner Losh 		return -ENOMEM;
192592ffb21SWarner Losh 
193592ffb21SWarner Losh 	priv->uid = p->td_ucred->cr_svuid;
194592ffb21SWarner Losh 	priv->pid = p->td_proc->p_pid;
195592ffb21SWarner Losh 	priv->minor = kdev->si_drv1;
196592ffb21SWarner Losh 	priv->ioctl_count = 0;
197592ffb21SWarner Losh 	/* for compatibility root is always authenticated */
198592ffb21SWarner Losh 	priv->authenticated = DRM_SUSER(p);
199592ffb21SWarner Losh 	priv->lock_count = 0;
200592ffb21SWarner Losh 
201592ffb21SWarner Losh 	INIT_LIST_HEAD(&priv->lhead);
202592ffb21SWarner Losh 	INIT_LIST_HEAD(&priv->fbs);
203592ffb21SWarner Losh 	INIT_LIST_HEAD(&priv->event_list);
204592ffb21SWarner Losh 	priv->event_space = 4096; /* set aside 4k for event buffer */
205592ffb21SWarner Losh 
206592ffb21SWarner Losh 	if (dev->driver->driver_features & DRIVER_GEM)
207592ffb21SWarner Losh 		drm_gem_open(dev, priv);
208592ffb21SWarner Losh 
209592ffb21SWarner Losh #ifdef FREEBSD_NOTYET
210592ffb21SWarner Losh 	if (drm_core_check_feature(dev, DRIVER_PRIME))
211592ffb21SWarner Losh 		drm_prime_init_file_private(&priv->prime);
212592ffb21SWarner Losh #endif /* FREEBSD_NOTYET */
213592ffb21SWarner Losh 
214592ffb21SWarner Losh 	if (dev->driver->open) {
215592ffb21SWarner Losh 		ret = dev->driver->open(dev, priv);
216592ffb21SWarner Losh 		if (ret < 0)
217592ffb21SWarner Losh 			goto out_free;
218592ffb21SWarner Losh 	}
219592ffb21SWarner Losh 
220592ffb21SWarner Losh 
221592ffb21SWarner Losh 	/* if there is no current master make this fd it */
222592ffb21SWarner Losh 	DRM_LOCK(dev);
223592ffb21SWarner Losh 	if (!priv->minor->master) {
224592ffb21SWarner Losh 		/* create a new master */
225592ffb21SWarner Losh 		priv->minor->master = drm_master_create(priv->minor);
226592ffb21SWarner Losh 		if (!priv->minor->master) {
227592ffb21SWarner Losh 			DRM_UNLOCK(dev);
228592ffb21SWarner Losh 			ret = -ENOMEM;
229592ffb21SWarner Losh 			goto out_free;
230592ffb21SWarner Losh 		}
231592ffb21SWarner Losh 
232592ffb21SWarner Losh 		priv->is_master = 1;
233592ffb21SWarner Losh 		/* take another reference for the copy in the local file priv */
234592ffb21SWarner Losh 		priv->master = drm_master_get(priv->minor->master);
235592ffb21SWarner Losh 
236592ffb21SWarner Losh 		priv->authenticated = 1;
237592ffb21SWarner Losh 
238592ffb21SWarner Losh 		DRM_UNLOCK(dev);
239592ffb21SWarner Losh 		if (dev->driver->master_create) {
240592ffb21SWarner Losh 			ret = dev->driver->master_create(dev, priv->master);
241592ffb21SWarner Losh 			if (ret) {
242592ffb21SWarner Losh 				DRM_LOCK(dev);
243592ffb21SWarner Losh 				/* drop both references if this fails */
244592ffb21SWarner Losh 				drm_master_put(&priv->minor->master);
245592ffb21SWarner Losh 				drm_master_put(&priv->master);
246592ffb21SWarner Losh 				DRM_UNLOCK(dev);
247592ffb21SWarner Losh 				goto out_free;
248592ffb21SWarner Losh 			}
249592ffb21SWarner Losh 		}
250592ffb21SWarner Losh 		DRM_LOCK(dev);
251592ffb21SWarner Losh 		if (dev->driver->master_set) {
252592ffb21SWarner Losh 			ret = dev->driver->master_set(dev, priv, true);
253592ffb21SWarner Losh 			if (ret) {
254592ffb21SWarner Losh 				/* drop both references if this fails */
255592ffb21SWarner Losh 				drm_master_put(&priv->minor->master);
256592ffb21SWarner Losh 				drm_master_put(&priv->master);
257592ffb21SWarner Losh 				DRM_UNLOCK(dev);
258592ffb21SWarner Losh 				goto out_free;
259592ffb21SWarner Losh 			}
260592ffb21SWarner Losh 		}
261592ffb21SWarner Losh 		DRM_UNLOCK(dev);
262592ffb21SWarner Losh 	} else {
263592ffb21SWarner Losh 		/* get a reference to the master */
264592ffb21SWarner Losh 		priv->master = drm_master_get(priv->minor->master);
265592ffb21SWarner Losh 		DRM_UNLOCK(dev);
266592ffb21SWarner Losh 	}
267592ffb21SWarner Losh 
268592ffb21SWarner Losh 	DRM_LOCK(dev);
269592ffb21SWarner Losh 	list_add(&priv->lhead, &dev->filelist);
270592ffb21SWarner Losh 	DRM_UNLOCK(dev);
271592ffb21SWarner Losh 
272592ffb21SWarner Losh 	device_busy(dev->dev);
273592ffb21SWarner Losh 
274592ffb21SWarner Losh 	ret = devfs_set_cdevpriv(priv, drm_release);
275592ffb21SWarner Losh 	if (ret != 0)
276592ffb21SWarner Losh 		drm_release(priv);
277592ffb21SWarner Losh 
278592ffb21SWarner Losh 	return ret;
279592ffb21SWarner Losh       out_free:
280592ffb21SWarner Losh 	free(priv, DRM_MEM_FILES);
281592ffb21SWarner Losh 	return ret;
282592ffb21SWarner Losh }
283592ffb21SWarner Losh 
drm_master_release(struct drm_device * dev,struct drm_file * file_priv)284592ffb21SWarner Losh static void drm_master_release(struct drm_device *dev, struct drm_file *file_priv)
285592ffb21SWarner Losh {
286592ffb21SWarner Losh 
287592ffb21SWarner Losh 	if (drm_i_have_hw_lock(dev, file_priv)) {
288592ffb21SWarner Losh 		DRM_DEBUG("File %p released, freeing lock for context %d\n",
289592ffb21SWarner Losh 			  file_priv, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
290592ffb21SWarner Losh 		drm_lock_free(&file_priv->master->lock,
291592ffb21SWarner Losh 			      _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
292592ffb21SWarner Losh 	}
293592ffb21SWarner Losh }
294592ffb21SWarner Losh 
drm_events_release(struct drm_file * file_priv)295592ffb21SWarner Losh static void drm_events_release(struct drm_file *file_priv)
296592ffb21SWarner Losh {
297592ffb21SWarner Losh 	struct drm_device *dev = file_priv->minor->dev;
298592ffb21SWarner Losh 	struct drm_pending_event *e, *et;
299592ffb21SWarner Losh 	struct drm_pending_vblank_event *v, *vt;
300592ffb21SWarner Losh 	unsigned long flags;
301592ffb21SWarner Losh 
302592ffb21SWarner Losh 	DRM_SPINLOCK_IRQSAVE(&dev->event_lock, flags);
303592ffb21SWarner Losh 
304592ffb21SWarner Losh 	/* Remove pending flips */
305592ffb21SWarner Losh 	list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
306592ffb21SWarner Losh 		if (v->base.file_priv == file_priv) {
307592ffb21SWarner Losh 			list_del(&v->base.link);
308592ffb21SWarner Losh 			drm_vblank_put(dev, v->pipe);
309592ffb21SWarner Losh 			v->base.destroy(&v->base);
310592ffb21SWarner Losh 		}
311592ffb21SWarner Losh 
312592ffb21SWarner Losh 	/* Remove unconsumed events */
313592ffb21SWarner Losh 	list_for_each_entry_safe(e, et, &file_priv->event_list, link)
314592ffb21SWarner Losh 		e->destroy(e);
315592ffb21SWarner Losh 
316592ffb21SWarner Losh 	DRM_SPINUNLOCK_IRQRESTORE(&dev->event_lock, flags);
317592ffb21SWarner Losh }
318592ffb21SWarner Losh 
319592ffb21SWarner Losh /**
320592ffb21SWarner Losh  * Release file.
321592ffb21SWarner Losh  *
322592ffb21SWarner Losh  * \param inode device inode
323592ffb21SWarner Losh  * \param file_priv DRM file private.
324592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
325592ffb21SWarner Losh  *
326592ffb21SWarner Losh  * If the hardware lock is held then free it, and take it again for the kernel
327592ffb21SWarner Losh  * context since it's necessary to reclaim buffers. Unlink the file private
328592ffb21SWarner Losh  * data from its list and free it. Decreases the open count and if it reaches
329592ffb21SWarner Losh  * zero calls drm_lastclose().
330592ffb21SWarner Losh  */
drm_release(void * data)331592ffb21SWarner Losh void drm_release(void *data)
332592ffb21SWarner Losh {
333592ffb21SWarner Losh 	struct drm_file *file_priv = data;
334592ffb21SWarner Losh 	struct drm_device *dev = file_priv->minor->dev;
335592ffb21SWarner Losh 
336592ffb21SWarner Losh 	sx_xlock(&drm_global_mutex);
337592ffb21SWarner Losh 
338592ffb21SWarner Losh 	DRM_DEBUG("open_count = %d\n", dev->open_count);
339592ffb21SWarner Losh 
340592ffb21SWarner Losh 	if (dev->driver->preclose)
341592ffb21SWarner Losh 		dev->driver->preclose(dev, file_priv);
342592ffb21SWarner Losh 
343592ffb21SWarner Losh 	/* ========================================================
344592ffb21SWarner Losh 	 * Begin inline drm_release
345592ffb21SWarner Losh 	 */
346592ffb21SWarner Losh 
347592ffb21SWarner Losh 	DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
348592ffb21SWarner Losh 		  DRM_CURRENTPID,
349592ffb21SWarner Losh 		  (long)file_priv->minor->device,
350592ffb21SWarner Losh 		  dev->open_count);
351592ffb21SWarner Losh 
352592ffb21SWarner Losh 	/* Release any auth tokens that might point to this file_priv,
353592ffb21SWarner Losh 	   (do that under the drm_global_mutex) */
354592ffb21SWarner Losh 	if (file_priv->magic)
355592ffb21SWarner Losh 		(void) drm_remove_magic(file_priv->master, file_priv->magic);
356592ffb21SWarner Losh 
357592ffb21SWarner Losh 	/* if the master has gone away we can't do anything with the lock */
358592ffb21SWarner Losh 	if (file_priv->minor->master)
359592ffb21SWarner Losh 		drm_master_release(dev, file_priv);
360592ffb21SWarner Losh 
361592ffb21SWarner Losh 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
362592ffb21SWarner Losh 		drm_core_reclaim_buffers(dev, file_priv);
363592ffb21SWarner Losh 
364592ffb21SWarner Losh 	drm_events_release(file_priv);
365592ffb21SWarner Losh 
366592ffb21SWarner Losh 	seldrain(&file_priv->event_poll);
367592ffb21SWarner Losh 
368592ffb21SWarner Losh 	if (dev->driver->driver_features & DRIVER_MODESET)
369592ffb21SWarner Losh 		drm_fb_release(file_priv);
370592ffb21SWarner Losh 
371592ffb21SWarner Losh 	if (dev->driver->driver_features & DRIVER_GEM)
372592ffb21SWarner Losh 		drm_gem_release(dev, file_priv);
373592ffb21SWarner Losh 
374592ffb21SWarner Losh #ifdef FREEBSD_NOTYET
375592ffb21SWarner Losh 	mutex_lock(&dev->ctxlist_mutex);
376592ffb21SWarner Losh 	if (!list_empty(&dev->ctxlist)) {
377592ffb21SWarner Losh 		struct drm_ctx_list *pos, *n;
378592ffb21SWarner Losh 
379592ffb21SWarner Losh 		list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
380592ffb21SWarner Losh 			if (pos->tag == file_priv &&
381592ffb21SWarner Losh 			    pos->handle != DRM_KERNEL_CONTEXT) {
382592ffb21SWarner Losh 				if (dev->driver->context_dtor)
383592ffb21SWarner Losh 					dev->driver->context_dtor(dev,
384592ffb21SWarner Losh 								  pos->handle);
385592ffb21SWarner Losh 
386592ffb21SWarner Losh 				drm_ctxbitmap_free(dev, pos->handle);
387592ffb21SWarner Losh 
388592ffb21SWarner Losh 				list_del(&pos->head);
389592ffb21SWarner Losh 				kfree(pos);
390592ffb21SWarner Losh 				--dev->ctx_count;
391592ffb21SWarner Losh 			}
392592ffb21SWarner Losh 		}
393592ffb21SWarner Losh 	}
394592ffb21SWarner Losh 	mutex_unlock(&dev->ctxlist_mutex);
395592ffb21SWarner Losh #endif /* FREEBSD_NOTYET */
396592ffb21SWarner Losh 
397592ffb21SWarner Losh 	DRM_LOCK(dev);
398592ffb21SWarner Losh 
399592ffb21SWarner Losh 	if (file_priv->is_master) {
400592ffb21SWarner Losh 		struct drm_master *master = file_priv->master;
401592ffb21SWarner Losh 		struct drm_file *temp;
402592ffb21SWarner Losh 		list_for_each_entry(temp, &dev->filelist, lhead) {
403592ffb21SWarner Losh 			if ((temp->master == file_priv->master) &&
404592ffb21SWarner Losh 			    (temp != file_priv))
405592ffb21SWarner Losh 				temp->authenticated = 0;
406592ffb21SWarner Losh 		}
407592ffb21SWarner Losh 
408592ffb21SWarner Losh 		/**
409592ffb21SWarner Losh 		 * Since the master is disappearing, so is the
410592ffb21SWarner Losh 		 * possibility to lock.
411592ffb21SWarner Losh 		 */
412592ffb21SWarner Losh 
413592ffb21SWarner Losh 		if (master->lock.hw_lock) {
414592ffb21SWarner Losh 			if (dev->sigdata.lock == master->lock.hw_lock)
415592ffb21SWarner Losh 				dev->sigdata.lock = NULL;
416592ffb21SWarner Losh 			master->lock.hw_lock = NULL;
417592ffb21SWarner Losh 			master->lock.file_priv = NULL;
418592ffb21SWarner Losh 			DRM_WAKEUP_INT(&master->lock.lock_queue);
419592ffb21SWarner Losh 		}
420592ffb21SWarner Losh 
421592ffb21SWarner Losh 		if (file_priv->minor->master == file_priv->master) {
422592ffb21SWarner Losh 			/* drop the reference held my the minor */
423592ffb21SWarner Losh 			if (dev->driver->master_drop)
424592ffb21SWarner Losh 				dev->driver->master_drop(dev, file_priv, true);
425592ffb21SWarner Losh 			drm_master_put(&file_priv->minor->master);
426592ffb21SWarner Losh 		}
427592ffb21SWarner Losh 	}
428592ffb21SWarner Losh 
429592ffb21SWarner Losh 	/* drop the reference held my the file priv */
430592ffb21SWarner Losh 	drm_master_put(&file_priv->master);
431592ffb21SWarner Losh 	file_priv->is_master = 0;
432592ffb21SWarner Losh 	list_del(&file_priv->lhead);
433592ffb21SWarner Losh 	DRM_UNLOCK(dev);
434592ffb21SWarner Losh 
435592ffb21SWarner Losh 	if (dev->driver->postclose)
436592ffb21SWarner Losh 		dev->driver->postclose(dev, file_priv);
437592ffb21SWarner Losh 
438592ffb21SWarner Losh #ifdef FREEBSD_NOTYET
439592ffb21SWarner Losh 	if (drm_core_check_feature(dev, DRIVER_PRIME))
440592ffb21SWarner Losh 		drm_prime_destroy_file_private(&file_priv->prime);
441592ffb21SWarner Losh #endif /* FREEBSD_NOTYET */
442592ffb21SWarner Losh 
443592ffb21SWarner Losh 	free(file_priv, DRM_MEM_FILES);
444592ffb21SWarner Losh 
445592ffb21SWarner Losh 	/* ========================================================
446592ffb21SWarner Losh 	 * End inline drm_release
447592ffb21SWarner Losh 	 */
448592ffb21SWarner Losh 
449592ffb21SWarner Losh 	atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
450592ffb21SWarner Losh 	device_unbusy(dev->dev);
451592ffb21SWarner Losh 	if (!--dev->open_count) {
452592ffb21SWarner Losh 		if (atomic_read(&dev->ioctl_count)) {
453592ffb21SWarner Losh 			DRM_ERROR("Device busy: %d\n",
454592ffb21SWarner Losh 				  atomic_read(&dev->ioctl_count));
455592ffb21SWarner Losh 		} else
456592ffb21SWarner Losh 			drm_lastclose(dev);
457592ffb21SWarner Losh 	}
458592ffb21SWarner Losh 	sx_xunlock(&drm_global_mutex);
459592ffb21SWarner Losh }
460592ffb21SWarner Losh EXPORT_SYMBOL(drm_release);
461592ffb21SWarner Losh 
462592ffb21SWarner Losh static bool
drm_dequeue_event(struct drm_file * file_priv,struct uio * uio,struct drm_pending_event ** out)463592ffb21SWarner Losh drm_dequeue_event(struct drm_file *file_priv, struct uio *uio,
464592ffb21SWarner Losh     struct drm_pending_event **out)
465592ffb21SWarner Losh {
466592ffb21SWarner Losh 	struct drm_pending_event *e;
467592ffb21SWarner Losh 	bool ret = false;
468592ffb21SWarner Losh 
469592ffb21SWarner Losh 	/* Already locked in drm_read(). */
470592ffb21SWarner Losh 	/* DRM_SPINLOCK_IRQSAVE(&dev->event_lock, flags); */
471592ffb21SWarner Losh 
472592ffb21SWarner Losh 	*out = NULL;
473592ffb21SWarner Losh 	if (list_empty(&file_priv->event_list))
474592ffb21SWarner Losh 		goto out;
475592ffb21SWarner Losh 	e = list_first_entry(&file_priv->event_list,
476592ffb21SWarner Losh 			     struct drm_pending_event, link);
477592ffb21SWarner Losh 	if (e->event->length > uio->uio_resid)
478592ffb21SWarner Losh 		goto out;
479592ffb21SWarner Losh 
480592ffb21SWarner Losh 	file_priv->event_space += e->event->length;
481592ffb21SWarner Losh 	list_del(&e->link);
482592ffb21SWarner Losh 	*out = e;
483592ffb21SWarner Losh 	ret = true;
484592ffb21SWarner Losh 
485592ffb21SWarner Losh out:
486592ffb21SWarner Losh 	/* DRM_SPINUNLOCK_IRQRESTORE(&dev->event_lock, flags); */
487592ffb21SWarner Losh 	return ret;
488592ffb21SWarner Losh }
489592ffb21SWarner Losh 
490592ffb21SWarner Losh int
drm_read(struct cdev * kdev,struct uio * uio,int ioflag)491592ffb21SWarner Losh drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
492592ffb21SWarner Losh {
493592ffb21SWarner Losh 	struct drm_file *file_priv;
494592ffb21SWarner Losh 	struct drm_device *dev;
495592ffb21SWarner Losh 	struct drm_pending_event *e;
496592ffb21SWarner Losh 	ssize_t error;
497592ffb21SWarner Losh 
498592ffb21SWarner Losh 	error = devfs_get_cdevpriv((void **)&file_priv);
499592ffb21SWarner Losh 	if (error != 0) {
500592ffb21SWarner Losh 		DRM_ERROR("can't find authenticator\n");
501592ffb21SWarner Losh 		return (EINVAL);
502592ffb21SWarner Losh 	}
503592ffb21SWarner Losh 
504592ffb21SWarner Losh 	dev = drm_get_device_from_kdev(kdev);
505592ffb21SWarner Losh 	mtx_lock(&dev->event_lock);
506592ffb21SWarner Losh 	while (list_empty(&file_priv->event_list)) {
507592ffb21SWarner Losh 		if ((ioflag & O_NONBLOCK) != 0) {
508592ffb21SWarner Losh 			error = EAGAIN;
509592ffb21SWarner Losh 			goto out;
510592ffb21SWarner Losh 		}
511592ffb21SWarner Losh 		error = msleep(&file_priv->event_space, &dev->event_lock,
512592ffb21SWarner Losh 	           PCATCH, "drmrea", 0);
513592ffb21SWarner Losh 	       if (error != 0)
514592ffb21SWarner Losh 		       goto out;
515592ffb21SWarner Losh 	}
516592ffb21SWarner Losh 
517592ffb21SWarner Losh 	while (drm_dequeue_event(file_priv, uio, &e)) {
518592ffb21SWarner Losh 		mtx_unlock(&dev->event_lock);
519592ffb21SWarner Losh 		error = uiomove(e->event, e->event->length, uio);
520592ffb21SWarner Losh 		CTR3(KTR_DRM, "drm_event_dequeued %d %d %d", curproc->p_pid,
521592ffb21SWarner Losh 		    e->event->type, e->event->length);
522592ffb21SWarner Losh 
523592ffb21SWarner Losh 		e->destroy(e);
524592ffb21SWarner Losh 		if (error != 0)
525592ffb21SWarner Losh 			return (error);
526592ffb21SWarner Losh 		mtx_lock(&dev->event_lock);
527592ffb21SWarner Losh 	}
528592ffb21SWarner Losh 
529592ffb21SWarner Losh out:
530592ffb21SWarner Losh 	mtx_unlock(&dev->event_lock);
531592ffb21SWarner Losh 	return (error);
532592ffb21SWarner Losh }
533592ffb21SWarner Losh EXPORT_SYMBOL(drm_read);
534592ffb21SWarner Losh 
535592ffb21SWarner Losh void
drm_event_wakeup(struct drm_pending_event * e)536592ffb21SWarner Losh drm_event_wakeup(struct drm_pending_event *e)
537592ffb21SWarner Losh {
538592ffb21SWarner Losh 	struct drm_file *file_priv;
539*c9130a46SMateusz Guzik 	struct drm_device *dev __diagused;
540592ffb21SWarner Losh 
541592ffb21SWarner Losh 	file_priv = e->file_priv;
542592ffb21SWarner Losh 	dev = file_priv->minor->dev;
543592ffb21SWarner Losh 	mtx_assert(&dev->event_lock, MA_OWNED);
544592ffb21SWarner Losh 
545592ffb21SWarner Losh 	wakeup(&file_priv->event_space);
546592ffb21SWarner Losh 	selwakeup(&file_priv->event_poll);
547592ffb21SWarner Losh }
548592ffb21SWarner Losh 
549592ffb21SWarner Losh int
drm_poll(struct cdev * kdev,int events,struct thread * td)550592ffb21SWarner Losh drm_poll(struct cdev *kdev, int events, struct thread *td)
551592ffb21SWarner Losh {
552592ffb21SWarner Losh 	struct drm_file *file_priv;
553592ffb21SWarner Losh 	struct drm_device *dev;
554592ffb21SWarner Losh 	int error, revents;
555592ffb21SWarner Losh 
556592ffb21SWarner Losh 	error = devfs_get_cdevpriv((void **)&file_priv);
557592ffb21SWarner Losh 	if (error != 0) {
558592ffb21SWarner Losh 		DRM_ERROR("can't find authenticator\n");
559592ffb21SWarner Losh 		return (EINVAL);
560592ffb21SWarner Losh 	}
561592ffb21SWarner Losh 
562592ffb21SWarner Losh 	dev = drm_get_device_from_kdev(kdev);
563592ffb21SWarner Losh 
564592ffb21SWarner Losh 	revents = 0;
565592ffb21SWarner Losh 	mtx_lock(&dev->event_lock);
566592ffb21SWarner Losh 	if ((events & (POLLIN | POLLRDNORM)) != 0) {
567592ffb21SWarner Losh 		if (list_empty(&file_priv->event_list)) {
568592ffb21SWarner Losh 			CTR0(KTR_DRM, "drm_poll empty list");
569592ffb21SWarner Losh 			selrecord(td, &file_priv->event_poll);
570592ffb21SWarner Losh 		} else {
571592ffb21SWarner Losh 			revents |= events & (POLLIN | POLLRDNORM);
572592ffb21SWarner Losh 			CTR1(KTR_DRM, "drm_poll revents %x", revents);
573592ffb21SWarner Losh 		}
574592ffb21SWarner Losh 	}
575592ffb21SWarner Losh 	mtx_unlock(&dev->event_lock);
576592ffb21SWarner Losh 	return (revents);
577592ffb21SWarner Losh }
578592ffb21SWarner Losh EXPORT_SYMBOL(drm_poll);
579592ffb21SWarner Losh 
580592ffb21SWarner Losh int
drm_mmap_single(struct cdev * kdev,vm_ooffset_t * offset,vm_size_t size,struct vm_object ** obj_res,int nprot)581592ffb21SWarner Losh drm_mmap_single(struct cdev *kdev, vm_ooffset_t *offset, vm_size_t size,
582592ffb21SWarner Losh     struct vm_object **obj_res, int nprot)
583592ffb21SWarner Losh {
584592ffb21SWarner Losh 	struct drm_device *dev;
585592ffb21SWarner Losh 
586592ffb21SWarner Losh 	dev = drm_get_device_from_kdev(kdev);
587592ffb21SWarner Losh 	if (dev->drm_ttm_bdev != NULL) {
588592ffb21SWarner Losh 		return (-ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
589592ffb21SWarner Losh 		    obj_res, nprot));
590592ffb21SWarner Losh 	} else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
591592ffb21SWarner Losh 		return (-drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
592592ffb21SWarner Losh 	} else {
593592ffb21SWarner Losh 		return (ENODEV);
594592ffb21SWarner Losh 	}
595592ffb21SWarner Losh }
596