blob: 68f0da801ed82b579efc5fcc3a94c6b755536f1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_fops.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * File operations for DRM
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
8 */
9
10/*
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
12 *
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice (including the next
25 * paragraph) shall be included in all copies or substantial portions of the
26 * Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
35 */
36
37#include "drmP.h"
Dave Airlied985c102006-01-02 21:32:48 +110038#include "drm_sarea.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/poll.h>
40
Dave Airlieb5e89ed2005-09-25 14:28:13 +100041static int drm_open_helper(struct inode *inode, struct file *filp,
Dave Airlie84b1fd12007-07-11 15:53:27 +100042 struct drm_device * dev);
Dave Airliec94f7022005-07-07 21:03:38 +100043
Dave Airlie84b1fd12007-07-11 15:53:27 +100044static int drm_setup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Dave Airlied985c102006-01-02 21:32:48 +110046 drm_local_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int i;
48 int ret;
Dave Airlie11d9c2f2007-02-18 17:13:39 +110049 u32 sareapage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Dave Airlie22eae942005-11-10 22:16:34 +110051 if (dev->driver->firstopen) {
52 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return ret;
55 }
56
Thomas Hellstrom1f4eccf2006-08-18 16:37:10 +100057 dev->magicfree.next = NULL;
58
Dave Airlied985c102006-01-02 21:32:48 +110059 /* prebuild the SAREA */
Andrew Morton4b560fd2007-03-19 09:08:21 +110060 sareapage = max_t(unsigned, SAREA_MAX, PAGE_SIZE);
Dave Airlie11d9c2f2007-02-18 17:13:39 +110061 i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
Dave Airlied985c102006-01-02 21:32:48 +110062 if (i != 0)
63 return i;
64
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 atomic_set(&dev->ioctl_count, 0);
66 atomic_set(&dev->vma_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 dev->buf_use = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 atomic_set(&dev->buf_alloc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dave Airlieb5e89ed2005-09-25 14:28:13 +100070 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
71 i = drm_dma_setup(dev);
72 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return i;
74 }
75
Dave Airlie3d774612006-08-07 20:07:43 +100076 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 atomic_set(&dev->counts[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100079 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
80 INIT_LIST_HEAD(&dev->magicfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Dave Airlie11d9c2f2007-02-18 17:13:39 +110082 dev->sigdata.lock = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083 init_waitqueue_head(&dev->lock.lock_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 dev->queue_count = 0;
85 dev->queue_reserved = 0;
86 dev->queue_slots = 0;
87 dev->queuelist = NULL;
88 dev->irq_enabled = 0;
89 dev->context_flag = 0;
90 dev->interrupt_flag = 0;
91 dev->dma_flag = 0;
92 dev->last_context = 0;
93 dev->last_switch = 0;
94 dev->last_checked = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 init_waitqueue_head(&dev->context_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 dev->if_version = 0;
97
98 dev->ctx_start = 0;
99 dev->lck_start = 0;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dev->buf_async = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 init_waitqueue_head(&dev->buf_readers);
103 init_waitqueue_head(&dev->buf_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /*
108 * The kernel's context could be created here, but is now created
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 * in drm_dma_enqueue. This is more resource-efficient for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * hardware that does not do DMA, but may mean that
111 * drm_select_queue fails between the time the interrupt is
112 * initialized and the time the queues are initialized.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return 0;
116}
117
118/**
119 * Open file.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * \param inode device inode
122 * \param filp file pointer.
123 * \return zero on success or a negative number on failure.
124 *
125 * Searches the DRM device with the same minor number, calls open_helper(), and
126 * increments the device open count. If the open count was previous at zero,
127 * i.e., it's the first that the device is open, then calls setup().
128 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000131 struct drm_device *dev = NULL;
Dave Airlie2c14f282008-04-21 16:47:32 +1000132 int minor_id = iminor(inode);
133 struct drm_minor *minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int retcode = 0;
135
Dave Airlie2c14f282008-04-21 16:47:32 +1000136 minor = idr_find(&drm_minors_idr, minor_id);
137 if (!minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139
Dave Airlie2c14f282008-04-21 16:47:32 +1000140 if (!(dev = minor->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142
143 retcode = drm_open_helper(inode, filp, dev);
144 if (!retcode) {
145 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
146 spin_lock(&dev->count_lock);
147 if (!dev->open_count++) {
148 spin_unlock(&dev->count_lock);
149 return drm_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
154 return retcode;
155}
156EXPORT_SYMBOL(drm_open);
157
158/**
Dave Airlied985c102006-01-02 21:32:48 +1100159 * File \c open operation.
160 *
161 * \param inode device inode.
162 * \param filp file pointer.
163 *
164 * Puts the dev->fops corresponding to the device minor number into
165 * \p filp, call the \c open method, and restore the file operations.
166 */
167int drm_stub_open(struct inode *inode, struct file *filp)
168{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000169 struct drm_device *dev = NULL;
Dave Airlie2c14f282008-04-21 16:47:32 +1000170 struct drm_minor *minor;
171 int minor_id = iminor(inode);
Dave Airlied985c102006-01-02 21:32:48 +1100172 int err = -ENODEV;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800173 const struct file_operations *old_fops;
Dave Airlied985c102006-01-02 21:32:48 +1100174
175 DRM_DEBUG("\n");
176
Dave Airlie2c14f282008-04-21 16:47:32 +1000177 minor = idr_find(&drm_minors_idr, minor_id);
178 if (!minor)
Dave Airlied985c102006-01-02 21:32:48 +1100179 return -ENODEV;
180
Dave Airlie2c14f282008-04-21 16:47:32 +1000181 if (!(dev = minor->dev))
Dave Airlied985c102006-01-02 21:32:48 +1100182 return -ENODEV;
183
184 old_fops = filp->f_op;
185 filp->f_op = fops_get(&dev->driver->fops);
186 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
187 fops_put(filp->f_op);
188 filp->f_op = fops_get(old_fops);
189 }
190 fops_put(old_fops);
191
192 return err;
193}
194
195/**
196 * Check whether DRI will run on this CPU.
197 *
198 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
199 */
200static int drm_cpu_valid(void)
201{
202#if defined(__i386__)
203 if (boot_cpu_data.x86 == 3)
204 return 0; /* No cmpxchg on a 386 */
205#endif
206#if defined(__sparc__) && !defined(__sparc_v9__)
207 return 0; /* No cmpxchg before v9 sparc. */
208#endif
209 return 1;
210}
211
212/**
213 * Called whenever a process opens /dev/drm.
214 *
215 * \param inode device inode.
216 * \param filp file pointer.
217 * \param dev device.
218 * \return zero on success or a negative number on failure.
219 *
220 * Creates and initializes a drm_file structure for the file private data in \p
221 * filp and add it into the double linked list in \p dev.
222 */
223static int drm_open_helper(struct inode *inode, struct file *filp,
Dave Airlie84b1fd12007-07-11 15:53:27 +1000224 struct drm_device * dev)
Dave Airlied985c102006-01-02 21:32:48 +1100225{
Dave Airlie2c14f282008-04-21 16:47:32 +1000226 int minor_id = iminor(inode);
Dave Airlie84b1fd12007-07-11 15:53:27 +1000227 struct drm_file *priv;
Dave Airlied985c102006-01-02 21:32:48 +1100228 int ret;
229
230 if (filp->f_flags & O_EXCL)
231 return -EBUSY; /* No exclusive opens */
232 if (!drm_cpu_valid())
233 return -EINVAL;
234
Dave Airlie2c14f282008-04-21 16:47:32 +1000235 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
Dave Airlied985c102006-01-02 21:32:48 +1100236
237 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
238 if (!priv)
239 return -ENOMEM;
240
241 memset(priv, 0, sizeof(*priv));
242 filp->private_data = priv;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000243 priv->filp = filp;
Dave Airlied985c102006-01-02 21:32:48 +1100244 priv->uid = current->euid;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700245 priv->pid = task_pid_nr(current);
Dave Airlie2c14f282008-04-21 16:47:32 +1000246 priv->minor = idr_find(&drm_minors_idr, minor_id);
Dave Airlied985c102006-01-02 21:32:48 +1100247 priv->ioctl_count = 0;
248 /* for compatibility root is always authenticated */
249 priv->authenticated = capable(CAP_SYS_ADMIN);
250 priv->lock_count = 0;
251
Dave Airliebd1b3312007-05-26 05:01:51 +1000252 INIT_LIST_HEAD(&priv->lhead);
253
Dave Airlied985c102006-01-02 21:32:48 +1100254 if (dev->driver->open) {
255 ret = dev->driver->open(dev, priv);
256 if (ret < 0)
257 goto out_free;
258 }
259
Dave Airlie30e2fb12006-02-02 19:37:46 +1100260 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000261 if (list_empty(&dev->filelist))
Dave Airlied985c102006-01-02 21:32:48 +1100262 priv->master = 1;
Dave Airliebd1b3312007-05-26 05:01:51 +1000263
264 list_add(&priv->lhead, &dev->filelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100265 mutex_unlock(&dev->struct_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100266
267#ifdef __alpha__
268 /*
269 * Default the hose
270 */
271 if (!dev->hose) {
272 struct pci_dev *pci_dev;
273 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
274 if (pci_dev) {
275 dev->hose = pci_dev->sysdata;
276 pci_dev_put(pci_dev);
277 }
278 if (!dev->hose) {
279 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
280 if (b)
281 dev->hose = b->sysdata;
282 }
283 }
284#endif
285
286 return 0;
287 out_free:
288 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
289 filp->private_data = NULL;
290 return ret;
291}
292
293/** No-op. */
294int drm_fasync(int fd, struct file *filp, int on)
295{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000296 struct drm_file *priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000297 struct drm_device *dev = priv->minor->dev;
Dave Airlied985c102006-01-02 21:32:48 +1100298 int retcode;
299
300 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
Dave Airlie2c14f282008-04-21 16:47:32 +1000301 (long)old_encode_dev(priv->minor->device));
Dave Airlied985c102006-01-02 21:32:48 +1100302 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
303 if (retcode < 0)
304 return retcode;
305 return 0;
306}
307EXPORT_SYMBOL(drm_fasync);
308
309/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * Release file.
311 *
312 * \param inode device inode
Eric Anholt6c340ea2007-08-25 20:23:09 +1000313 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * \return zero on success or a negative number on failure.
315 *
316 * If the hardware lock is held then free it, and take it again for the kernel
317 * context since it's necessary to reclaim buffers. Unlink the file private
318 * data from its list and free it. Decreases the open count and if it reaches
Dave Airlie22eae942005-11-10 22:16:34 +1100319 * zero calls drm_lastclose().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000323 struct drm_file *file_priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000324 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int retcode = 0;
Mike Isely9df58082008-03-13 15:30:35 -0500326 unsigned long irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dave Airlie22eae942005-11-10 22:16:34 +1100332 if (dev->driver->preclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000333 dev->driver->preclose(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* ========================================================
336 * Begin inline drm_release
337 */
338
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700340 task_pid_nr(current),
Dave Airlie2c14f282008-04-21 16:47:32 +1000341 (long)old_encode_dev(file_priv->minor->device),
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100344 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
Eric Anholtc153f452007-09-03 12:06:45 +1000345 if (drm_i_have_hw_lock(dev, file_priv)) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000346 dev->driver->reclaim_buffers_locked(dev, file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100347 } else {
Dave Airlieb74e2082008-04-26 18:21:28 +1000348 unsigned long endtime = jiffies + 3 * DRM_HZ;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100349 int locked = 0;
350
351 drm_idlelock_take(&dev->lock);
352
353 /*
354 * Wait for a while.
355 */
356
357 do{
Mike Isely9df58082008-03-13 15:30:35 -0500358 spin_lock_irqsave(&dev->lock.spinlock,
359 irqflags);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100360 locked = dev->lock.idle_has_lock;
Mike Isely9df58082008-03-13 15:30:35 -0500361 spin_unlock_irqrestore(&dev->lock.spinlock,
362 irqflags);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100363 if (locked)
364 break;
365 schedule();
Dave Airlieb74e2082008-04-26 18:21:28 +1000366 } while (!time_after_eq(jiffies, endtime));
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100367
368 if (!locked) {
369 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
370 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
371 "\tI will go on reclaiming the buffers anyway.\n");
372 }
373
Eric Anholt6c340ea2007-08-25 20:23:09 +1000374 dev->driver->reclaim_buffers_locked(dev, file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100375 drm_idlelock_release(&dev->lock);
376 }
377 }
378
379 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
380
381 drm_idlelock_take(&dev->lock);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000382 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100383 drm_idlelock_release(&dev->lock);
384
385 }
386
Eric Anholtc153f452007-09-03 12:06:45 +1000387 if (drm_i_have_hw_lock(dev, file_priv)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 DRM_DEBUG("File %p released, freeing lock for context %d\n",
389 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
390
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100391 drm_lock_free(&dev->lock,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100395
Dave Airlie22eae942005-11-10 22:16:34 +1100396 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
397 !dev->driver->reclaim_buffers_locked) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000398 dev->driver->reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401 drm_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Dave Airlie30e2fb12006-02-02 19:37:46 +1100403 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000404 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000405 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dave Airliebd1b3312007-05-26 05:01:51 +1000407 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000408 if (pos->tag == file_priv &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409 pos->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (dev->driver->context_dtor)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 dev->driver->context_dtor(dev,
412 pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 drm_ctxbitmap_free(dev, pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 list_del(&pos->head);
417 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 --dev->ctx_count;
419 }
420 }
421 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100422 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Dave Airlie30e2fb12006-02-02 19:37:46 +1100424 mutex_lock(&dev->struct_mutex);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000425 if (file_priv->remove_auth_on_close == 1) {
Dave Airlie84b1fd12007-07-11 15:53:27 +1000426 struct drm_file *temp;
Dave Airliebd1b3312007-05-26 05:01:51 +1000427
428 list_for_each_entry(temp, &dev->filelist, lhead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 temp->authenticated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000431 list_del(&file_priv->lhead);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100432 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000433
Dave Airlie22eae942005-11-10 22:16:34 +1100434 if (dev->driver->postclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000435 dev->driver->postclose(dev, file_priv);
436 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* ========================================================
439 * End inline drm_release
440 */
441
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
443 spin_lock(&dev->count_lock);
444 if (!--dev->open_count) {
445 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
446 DRM_ERROR("Device busy: %d %d\n",
447 atomic_read(&dev->ioctl_count), dev->blocked);
448 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 unlock_kernel();
450 return -EBUSY;
451 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 unlock_kernel();
Dave Airlie22eae942005-11-10 22:16:34 +1100454 return drm_lastclose(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 unlock_kernel();
459
460 return retcode;
461}
462EXPORT_SYMBOL(drm_release);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/** No-op. */
465unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
466{
467 return 0;
468}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000469EXPORT_SYMBOL(drm_poll);