blob: 0d46627663b1e93e0d50729b41162ca32c5af1a0 [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>
Jonathan Corbet70ffa162008-05-15 11:34:16 -060040#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Airlieb5e89ed2005-09-25 14:28:13 +100042static int drm_open_helper(struct inode *inode, struct file *filp,
Dave Airlie84b1fd12007-07-11 15:53:27 +100043 struct drm_device * dev);
Dave Airliec94f7022005-07-07 21:03:38 +100044
Dave Airlie84b1fd12007-07-11 15:53:27 +100045static int drm_setup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Dave Airlied985c102006-01-02 21:32:48 +110047 drm_local_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int i;
49 int ret;
Dave Airlie11d9c2f2007-02-18 17:13:39 +110050 u32 sareapage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Dave Airlie22eae942005-11-10 22:16:34 +110052 if (dev->driver->firstopen) {
53 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100054 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return ret;
56 }
57
Thomas Hellstrom1f4eccf2006-08-18 16:37:10 +100058 dev->magicfree.next = NULL;
59
Dave Airlied985c102006-01-02 21:32:48 +110060 /* prebuild the SAREA */
Andrew Morton4b560fd2007-03-19 09:08:21 +110061 sareapage = max_t(unsigned, SAREA_MAX, PAGE_SIZE);
Dave Airlie11d9c2f2007-02-18 17:13:39 +110062 i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
Dave Airlied985c102006-01-02 21:32:48 +110063 if (i != 0)
64 return i;
65
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066 atomic_set(&dev->ioctl_count, 0);
67 atomic_set(&dev->vma_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 dev->buf_use = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100069 atomic_set(&dev->buf_alloc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Dave Airlieb5e89ed2005-09-25 14:28:13 +100071 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
72 i = drm_dma_setup(dev);
73 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return i;
75 }
76
Dave Airlie3d774612006-08-07 20:07:43 +100077 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 atomic_set(&dev->counts[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100080 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
81 INIT_LIST_HEAD(&dev->magicfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Dave Airlie11d9c2f2007-02-18 17:13:39 +110083 dev->sigdata.lock = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084 init_waitqueue_head(&dev->lock.lock_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 dev->queue_count = 0;
86 dev->queue_reserved = 0;
87 dev->queue_slots = 0;
88 dev->queuelist = NULL;
89 dev->irq_enabled = 0;
90 dev->context_flag = 0;
91 dev->interrupt_flag = 0;
92 dev->dma_flag = 0;
93 dev->last_context = 0;
94 dev->last_switch = 0;
95 dev->last_checked = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100096 init_waitqueue_head(&dev->context_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 dev->if_version = 0;
98
99 dev->ctx_start = 0;
100 dev->lck_start = 0;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dev->buf_async = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 init_waitqueue_head(&dev->buf_readers);
104 init_waitqueue_head(&dev->buf_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /*
109 * The kernel's context could be created here, but is now created
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 * in drm_dma_enqueue. This is more resource-efficient for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * hardware that does not do DMA, but may mean that
112 * drm_select_queue fails between the time the interrupt is
113 * initialized and the time the queues are initialized.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 return 0;
117}
118
119/**
120 * Open file.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * \param inode device inode
123 * \param filp file pointer.
124 * \return zero on success or a negative number on failure.
125 *
126 * Searches the DRM device with the same minor number, calls open_helper(), and
127 * increments the device open count. If the open count was previous at zero,
128 * i.e., it's the first that the device is open, then calls setup().
129 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000132 struct drm_device *dev = NULL;
Dave Airlie2c14f282008-04-21 16:47:32 +1000133 int minor_id = iminor(inode);
134 struct drm_minor *minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int retcode = 0;
136
Dave Airlie2c14f282008-04-21 16:47:32 +1000137 minor = idr_find(&drm_minors_idr, minor_id);
138 if (!minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140
Dave Airlie2c14f282008-04-21 16:47:32 +1000141 if (!(dev = minor->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143
144 retcode = drm_open_helper(inode, filp, dev);
145 if (!retcode) {
146 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
147 spin_lock(&dev->count_lock);
148 if (!dev->open_count++) {
149 spin_unlock(&dev->count_lock);
150 return drm_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 return retcode;
156}
157EXPORT_SYMBOL(drm_open);
158
159/**
Dave Airlied985c102006-01-02 21:32:48 +1100160 * File \c open operation.
161 *
162 * \param inode device inode.
163 * \param filp file pointer.
164 *
165 * Puts the dev->fops corresponding to the device minor number into
166 * \p filp, call the \c open method, and restore the file operations.
167 */
168int drm_stub_open(struct inode *inode, struct file *filp)
169{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000170 struct drm_device *dev = NULL;
Dave Airlie2c14f282008-04-21 16:47:32 +1000171 struct drm_minor *minor;
172 int minor_id = iminor(inode);
Dave Airlied985c102006-01-02 21:32:48 +1100173 int err = -ENODEV;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800174 const struct file_operations *old_fops;
Dave Airlied985c102006-01-02 21:32:48 +1100175
176 DRM_DEBUG("\n");
177
Jonathan Corbet70ffa162008-05-15 11:34:16 -0600178 /* BKL pushdown: note that nothing else serializes idr_find() */
179 lock_kernel();
Dave Airlie2c14f282008-04-21 16:47:32 +1000180 minor = idr_find(&drm_minors_idr, minor_id);
181 if (!minor)
Jonathan Corbet70ffa162008-05-15 11:34:16 -0600182 goto out;
Dave Airlied985c102006-01-02 21:32:48 +1100183
Dave Airlie2c14f282008-04-21 16:47:32 +1000184 if (!(dev = minor->dev))
Jonathan Corbet70ffa162008-05-15 11:34:16 -0600185 goto out;
Dave Airlied985c102006-01-02 21:32:48 +1100186
187 old_fops = filp->f_op;
188 filp->f_op = fops_get(&dev->driver->fops);
189 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
190 fops_put(filp->f_op);
191 filp->f_op = fops_get(old_fops);
192 }
193 fops_put(old_fops);
194
Jonathan Corbet70ffa162008-05-15 11:34:16 -0600195out:
196 unlock_kernel();
Dave Airlied985c102006-01-02 21:32:48 +1100197 return err;
198}
199
200/**
201 * Check whether DRI will run on this CPU.
202 *
203 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
204 */
205static int drm_cpu_valid(void)
206{
207#if defined(__i386__)
208 if (boot_cpu_data.x86 == 3)
209 return 0; /* No cmpxchg on a 386 */
210#endif
211#if defined(__sparc__) && !defined(__sparc_v9__)
212 return 0; /* No cmpxchg before v9 sparc. */
213#endif
214 return 1;
215}
216
217/**
218 * Called whenever a process opens /dev/drm.
219 *
220 * \param inode device inode.
221 * \param filp file pointer.
222 * \param dev device.
223 * \return zero on success or a negative number on failure.
224 *
225 * Creates and initializes a drm_file structure for the file private data in \p
226 * filp and add it into the double linked list in \p dev.
227 */
228static int drm_open_helper(struct inode *inode, struct file *filp,
Dave Airlie84b1fd12007-07-11 15:53:27 +1000229 struct drm_device * dev)
Dave Airlied985c102006-01-02 21:32:48 +1100230{
Dave Airlie2c14f282008-04-21 16:47:32 +1000231 int minor_id = iminor(inode);
Dave Airlie84b1fd12007-07-11 15:53:27 +1000232 struct drm_file *priv;
Dave Airlied985c102006-01-02 21:32:48 +1100233 int ret;
234
235 if (filp->f_flags & O_EXCL)
236 return -EBUSY; /* No exclusive opens */
237 if (!drm_cpu_valid())
238 return -EINVAL;
239
Dave Airlie2c14f282008-04-21 16:47:32 +1000240 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
Dave Airlied985c102006-01-02 21:32:48 +1100241
242 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
243 if (!priv)
244 return -ENOMEM;
245
246 memset(priv, 0, sizeof(*priv));
247 filp->private_data = priv;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000248 priv->filp = filp;
David Howells2df68b42008-09-02 11:03:14 +1000249 priv->uid = current_euid();
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700250 priv->pid = task_pid_nr(current);
Dave Airlie2c14f282008-04-21 16:47:32 +1000251 priv->minor = idr_find(&drm_minors_idr, minor_id);
Dave Airlied985c102006-01-02 21:32:48 +1100252 priv->ioctl_count = 0;
253 /* for compatibility root is always authenticated */
254 priv->authenticated = capable(CAP_SYS_ADMIN);
255 priv->lock_count = 0;
256
Dave Airliebd1b3312007-05-26 05:01:51 +1000257 INIT_LIST_HEAD(&priv->lhead);
258
Eric Anholt673a3942008-07-30 12:06:12 -0700259 if (dev->driver->driver_features & DRIVER_GEM)
260 drm_gem_open(dev, priv);
261
Dave Airlied985c102006-01-02 21:32:48 +1100262 if (dev->driver->open) {
263 ret = dev->driver->open(dev, priv);
264 if (ret < 0)
265 goto out_free;
266 }
267
Dave Airlie30e2fb12006-02-02 19:37:46 +1100268 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000269 if (list_empty(&dev->filelist))
Dave Airlied985c102006-01-02 21:32:48 +1100270 priv->master = 1;
Dave Airliebd1b3312007-05-26 05:01:51 +1000271
272 list_add(&priv->lhead, &dev->filelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100273 mutex_unlock(&dev->struct_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100274
275#ifdef __alpha__
276 /*
277 * Default the hose
278 */
279 if (!dev->hose) {
280 struct pci_dev *pci_dev;
281 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
282 if (pci_dev) {
283 dev->hose = pci_dev->sysdata;
284 pci_dev_put(pci_dev);
285 }
286 if (!dev->hose) {
287 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
288 if (b)
289 dev->hose = b->sysdata;
290 }
291 }
292#endif
293
294 return 0;
295 out_free:
296 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
297 filp->private_data = NULL;
298 return ret;
299}
300
301/** No-op. */
302int drm_fasync(int fd, struct file *filp, int on)
303{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000304 struct drm_file *priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000305 struct drm_device *dev = priv->minor->dev;
Dave Airlied985c102006-01-02 21:32:48 +1100306 int retcode;
307
308 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
Dave Airlie2c14f282008-04-21 16:47:32 +1000309 (long)old_encode_dev(priv->minor->device));
Dave Airlied985c102006-01-02 21:32:48 +1100310 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
311 if (retcode < 0)
312 return retcode;
313 return 0;
314}
315EXPORT_SYMBOL(drm_fasync);
316
317/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * Release file.
319 *
320 * \param inode device inode
Eric Anholt6c340ea2007-08-25 20:23:09 +1000321 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * \return zero on success or a negative number on failure.
323 *
324 * If the hardware lock is held then free it, and take it again for the kernel
325 * context since it's necessary to reclaim buffers. Unlink the file private
326 * data from its list and free it. Decreases the open count and if it reaches
Dave Airlie22eae942005-11-10 22:16:34 +1100327 * zero calls drm_lastclose().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000331 struct drm_file *file_priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000332 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int retcode = 0;
334
335 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Airlie22eae942005-11-10 22:16:34 +1100339 if (dev->driver->preclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000340 dev->driver->preclose(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* ========================================================
343 * Begin inline drm_release
344 */
345
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700347 task_pid_nr(current),
Dave Airlie2c14f282008-04-21 16:47:32 +1000348 (long)old_encode_dev(file_priv->minor->device),
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100351 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
Eric Anholtc153f452007-09-03 12:06:45 +1000352 if (drm_i_have_hw_lock(dev, file_priv)) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000353 dev->driver->reclaim_buffers_locked(dev, file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100354 } else {
Dave Airlieb74e2082008-04-26 18:21:28 +1000355 unsigned long endtime = jiffies + 3 * DRM_HZ;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100356 int locked = 0;
357
358 drm_idlelock_take(&dev->lock);
359
360 /*
361 * Wait for a while.
362 */
363
364 do{
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000365 spin_lock_bh(&dev->lock.spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100366 locked = dev->lock.idle_has_lock;
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000367 spin_unlock_bh(&dev->lock.spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100368 if (locked)
369 break;
370 schedule();
Dave Airlieb74e2082008-04-26 18:21:28 +1000371 } while (!time_after_eq(jiffies, endtime));
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100372
373 if (!locked) {
374 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
375 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
376 "\tI will go on reclaiming the buffers anyway.\n");
377 }
378
Eric Anholt6c340ea2007-08-25 20:23:09 +1000379 dev->driver->reclaim_buffers_locked(dev, file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100380 drm_idlelock_release(&dev->lock);
381 }
382 }
383
384 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
385
386 drm_idlelock_take(&dev->lock);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000387 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100388 drm_idlelock_release(&dev->lock);
389
390 }
391
Eric Anholtc153f452007-09-03 12:06:45 +1000392 if (drm_i_have_hw_lock(dev, file_priv)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 DRM_DEBUG("File %p released, freeing lock for context %d\n",
394 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
395
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100396 drm_lock_free(&dev->lock,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000399
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100400
Dave Airlie22eae942005-11-10 22:16:34 +1100401 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
402 !dev->driver->reclaim_buffers_locked) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000403 dev->driver->reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Eric Anholt673a3942008-07-30 12:06:12 -0700406 if (dev->driver->driver_features & DRIVER_GEM)
407 drm_gem_release(dev, file_priv);
408
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409 drm_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dave Airlie30e2fb12006-02-02 19:37:46 +1100411 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000412 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000413 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Dave Airliebd1b3312007-05-26 05:01:51 +1000415 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000416 if (pos->tag == file_priv &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 pos->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (dev->driver->context_dtor)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 dev->driver->context_dtor(dev,
420 pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 drm_ctxbitmap_free(dev, pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000424 list_del(&pos->head);
425 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 --dev->ctx_count;
427 }
428 }
429 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100430 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Dave Airlie30e2fb12006-02-02 19:37:46 +1100432 mutex_lock(&dev->struct_mutex);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000433 if (file_priv->remove_auth_on_close == 1) {
Dave Airlie84b1fd12007-07-11 15:53:27 +1000434 struct drm_file *temp;
Dave Airliebd1b3312007-05-26 05:01:51 +1000435
436 list_for_each_entry(temp, &dev->filelist, lhead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 temp->authenticated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000439 list_del(&file_priv->lhead);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100440 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441
Dave Airlie22eae942005-11-10 22:16:34 +1100442 if (dev->driver->postclose)
Eric Anholt6c340ea2007-08-25 20:23:09 +1000443 dev->driver->postclose(dev, file_priv);
444 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* ========================================================
447 * End inline drm_release
448 */
449
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000450 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
451 spin_lock(&dev->count_lock);
452 if (!--dev->open_count) {
453 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
454 DRM_ERROR("Device busy: %d %d\n",
455 atomic_read(&dev->ioctl_count), dev->blocked);
456 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 unlock_kernel();
458 return -EBUSY;
459 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000460 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 unlock_kernel();
Dave Airlie22eae942005-11-10 22:16:34 +1100462 return drm_lastclose(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 unlock_kernel();
467
468 return retcode;
469}
470EXPORT_SYMBOL(drm_release);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/** No-op. */
473unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
474{
475 return 0;
476}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477EXPORT_SYMBOL(drm_poll);