]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpu/drm/drm_fops.c
drm: cleanup exit path for module unload
[net-next-2.6.git] / drivers / gpu / drm / drm_fops.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_fops.c
1da177e4 3 * File operations for DRM
b5e89ed5 4 *
1da177e4
LT
5 * \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"
d985c108 38#include "drm_sarea.h"
1da177e4 39#include <linux/poll.h>
70ffa16e 40#include <linux/smp_lock.h>
1da177e4 41
b5e89ed5 42static int drm_open_helper(struct inode *inode, struct file *filp,
84b1fd10 43 struct drm_device * dev);
c94f7029 44
84b1fd10 45static int drm_setup(struct drm_device * dev)
1da177e4 46{
d985c108 47 drm_local_map_t *map;
1da177e4
LT
48 int i;
49 int ret;
11d9c2fd 50 u32 sareapage;
1da177e4 51
22eae947
DA
52 if (dev->driver->firstopen) {
53 ret = dev->driver->firstopen(dev);
b5e89ed5 54 if (ret != 0)
1da177e4
LT
55 return ret;
56 }
57
1f4eccfd
TH
58 dev->magicfree.next = NULL;
59
d985c108 60 /* prebuild the SAREA */
4b560fde 61 sareapage = max_t(unsigned, SAREA_MAX, PAGE_SIZE);
11d9c2fd 62 i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
d985c108
DA
63 if (i != 0)
64 return i;
65
b5e89ed5
DA
66 atomic_set(&dev->ioctl_count, 0);
67 atomic_set(&dev->vma_count, 0);
1da177e4 68 dev->buf_use = 0;
b5e89ed5 69 atomic_set(&dev->buf_alloc, 0);
1da177e4 70
b5e89ed5
DA
71 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
72 i = drm_dma_setup(dev);
73 if (i < 0)
1da177e4
LT
74 return i;
75 }
76
3d77461e 77 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
b5e89ed5 78 atomic_set(&dev->counts[i], 0);
1da177e4 79
8669cbc5
TH
80 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
81 INIT_LIST_HEAD(&dev->magicfree);
1da177e4 82
11d9c2fd 83 dev->sigdata.lock = NULL;
b5e89ed5 84 init_waitqueue_head(&dev->lock.lock_queue);
1da177e4
LT
85 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;
b5e89ed5 96 init_waitqueue_head(&dev->context_wait);
1da177e4
LT
97 dev->if_version = 0;
98
99 dev->ctx_start = 0;
100 dev->lck_start = 0;
101
1da177e4 102 dev->buf_async = NULL;
b5e89ed5
DA
103 init_waitqueue_head(&dev->buf_readers);
104 init_waitqueue_head(&dev->buf_writers);
1da177e4 105
b5e89ed5 106 DRM_DEBUG("\n");
1da177e4
LT
107
108 /*
109 * The kernel's context could be created here, but is now created
b5e89ed5 110 * in drm_dma_enqueue. This is more resource-efficient for
1da177e4
LT
111 * 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 */
1da177e4
LT
115
116 return 0;
117}
118
119/**
120 * Open file.
b5e89ed5 121 *
1da177e4
LT
122 * \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 */
b5e89ed5 130int drm_open(struct inode *inode, struct file *filp)
1da177e4 131{
84b1fd10 132 struct drm_device *dev = NULL;
2c14f28b
DA
133 int minor_id = iminor(inode);
134 struct drm_minor *minor;
1da177e4
LT
135 int retcode = 0;
136
2c14f28b
DA
137 minor = idr_find(&drm_minors_idr, minor_id);
138 if (!minor)
1da177e4 139 return -ENODEV;
b5e89ed5 140
2c14f28b 141 if (!(dev = minor->dev))
1da177e4 142 return -ENODEV;
b5e89ed5
DA
143
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);
1da177e4 151 }
b5e89ed5 152 spin_unlock(&dev->count_lock);
1da177e4
LT
153 }
154
155 return retcode;
156}
157EXPORT_SYMBOL(drm_open);
158
d985c108
DA
159/**
160 * 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{
84b1fd10 170 struct drm_device *dev = NULL;
2c14f28b
DA
171 struct drm_minor *minor;
172 int minor_id = iminor(inode);
d985c108 173 int err = -ENODEV;
99ac48f5 174 const struct file_operations *old_fops;
d985c108
DA
175
176 DRM_DEBUG("\n");
177
70ffa16e
JC
178 /* BKL pushdown: note that nothing else serializes idr_find() */
179 lock_kernel();
2c14f28b
DA
180 minor = idr_find(&drm_minors_idr, minor_id);
181 if (!minor)
70ffa16e 182 goto out;
d985c108 183
2c14f28b 184 if (!(dev = minor->dev))
70ffa16e 185 goto out;
d985c108
DA
186
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
70ffa16e
JC
195out:
196 unlock_kernel();
d985c108
DA
197 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,
84b1fd10 229 struct drm_device * dev)
d985c108 230{
2c14f28b 231 int minor_id = iminor(inode);
84b1fd10 232 struct drm_file *priv;
d985c108
DA
233 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
2c14f28b 240 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
d985c108
DA
241
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;
6c340eac 248 priv->filp = filp;
2df68b43 249 priv->uid = current_euid();
ba25f9dc 250 priv->pid = task_pid_nr(current);
2c14f28b 251 priv->minor = idr_find(&drm_minors_idr, minor_id);
d985c108
DA
252 priv->ioctl_count = 0;
253 /* for compatibility root is always authenticated */
254 priv->authenticated = capable(CAP_SYS_ADMIN);
255 priv->lock_count = 0;
256
bd1b331f
DA
257 INIT_LIST_HEAD(&priv->lhead);
258
673a394b
EA
259 if (dev->driver->driver_features & DRIVER_GEM)
260 drm_gem_open(dev, priv);
261
d985c108
DA
262 if (dev->driver->open) {
263 ret = dev->driver->open(dev, priv);
264 if (ret < 0)
265 goto out_free;
266 }
267
30e2fb18 268 mutex_lock(&dev->struct_mutex);
bd1b331f 269 if (list_empty(&dev->filelist))
d985c108 270 priv->master = 1;
bd1b331f
DA
271
272 list_add(&priv->lhead, &dev->filelist);
30e2fb18 273 mutex_unlock(&dev->struct_mutex);
d985c108
DA
274
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{
84b1fd10 304 struct drm_file *priv = filp->private_data;
2c14f28b 305 struct drm_device *dev = priv->minor->dev;
d985c108
DA
306 int retcode;
307
308 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
2c14f28b 309 (long)old_encode_dev(priv->minor->device));
d985c108
DA
310 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
1da177e4
LT
317/**
318 * Release file.
319 *
320 * \param inode device inode
6c340eac 321 * \param file_priv DRM file private.
1da177e4
LT
322 * \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
22eae947 327 * zero calls drm_lastclose().
1da177e4 328 */
b5e89ed5 329int drm_release(struct inode *inode, struct file *filp)
1da177e4 330{
6c340eac 331 struct drm_file *file_priv = filp->private_data;
2c14f28b 332 struct drm_device *dev = file_priv->minor->dev;
1da177e4
LT
333 int retcode = 0;
334
335 lock_kernel();
1da177e4 336
b5e89ed5 337 DRM_DEBUG("open_count = %d\n", dev->open_count);
1da177e4 338
22eae947 339 if (dev->driver->preclose)
6c340eac 340 dev->driver->preclose(dev, file_priv);
1da177e4
LT
341
342 /* ========================================================
343 * Begin inline drm_release
344 */
345
b5e89ed5 346 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
ba25f9dc 347 task_pid_nr(current),
2c14f28b 348 (long)old_encode_dev(file_priv->minor->device),
b5e89ed5
DA
349 dev->open_count);
350
040ac320 351 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
c153f45f 352 if (drm_i_have_hw_lock(dev, file_priv)) {
6c340eac 353 dev->driver->reclaim_buffers_locked(dev, file_priv);
040ac320 354 } else {
b74e2082 355 unsigned long endtime = jiffies + 3 * DRM_HZ;
040ac320
TH
356 int locked = 0;
357
358 drm_idlelock_take(&dev->lock);
359
360 /*
361 * Wait for a while.
362 */
363
364 do{
f116cc56 365 spin_lock_bh(&dev->lock.spinlock);
040ac320 366 locked = dev->lock.idle_has_lock;
f116cc56 367 spin_unlock_bh(&dev->lock.spinlock);
040ac320
TH
368 if (locked)
369 break;
370 schedule();
b74e2082 371 } while (!time_after_eq(jiffies, endtime));
040ac320
TH
372
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");
1da177e4 377 }
040ac320 378
6c340eac 379 dev->driver->reclaim_buffers_locked(dev, file_priv);
040ac320 380 drm_idlelock_release(&dev->lock);
1da177e4
LT
381 }
382 }
b5e89ed5 383
040ac320
TH
384 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
385
386 drm_idlelock_take(&dev->lock);
6c340eac 387 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
040ac320
TH
388 drm_idlelock_release(&dev->lock);
389
390 }
391
c153f45f 392 if (drm_i_have_hw_lock(dev, file_priv)) {
040ac320
TH
393 DRM_DEBUG("File %p released, freeing lock for context %d\n",
394 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
395
396 drm_lock_free(&dev->lock,
397 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
398 }
399
400
22eae947
DA
401 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
402 !dev->driver->reclaim_buffers_locked) {
6c340eac 403 dev->driver->reclaim_buffers(dev, file_priv);
1da177e4
LT
404 }
405
673a394b
EA
406 if (dev->driver->driver_features & DRIVER_GEM)
407 drm_gem_release(dev, file_priv);
408
30e2fb18 409 mutex_lock(&dev->ctxlist_mutex);
bd1b331f 410 if (!list_empty(&dev->ctxlist)) {
55910517 411 struct drm_ctx_list *pos, *n;
1da177e4 412
bd1b331f 413 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
6c340eac 414 if (pos->tag == file_priv &&
b5e89ed5 415 pos->handle != DRM_KERNEL_CONTEXT) {
1da177e4 416 if (dev->driver->context_dtor)
b5e89ed5
DA
417 dev->driver->context_dtor(dev,
418 pos->handle);
1da177e4 419
b5e89ed5 420 drm_ctxbitmap_free(dev, pos->handle);
1da177e4 421
b5e89ed5
DA
422 list_del(&pos->head);
423 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
1da177e4
LT
424 --dev->ctx_count;
425 }
426 }
427 }
30e2fb18 428 mutex_unlock(&dev->ctxlist_mutex);
1da177e4 429
30e2fb18 430 mutex_lock(&dev->struct_mutex);
6c340eac 431 if (file_priv->remove_auth_on_close == 1) {
84b1fd10 432 struct drm_file *temp;
bd1b331f
DA
433
434 list_for_each_entry(temp, &dev->filelist, lhead)
1da177e4 435 temp->authenticated = 0;
1da177e4 436 }
6c340eac 437 list_del(&file_priv->lhead);
30e2fb18 438 mutex_unlock(&dev->struct_mutex);
b5e89ed5 439
22eae947 440 if (dev->driver->postclose)
6c340eac
EA
441 dev->driver->postclose(dev, file_priv);
442 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
1da177e4
LT
443
444 /* ========================================================
445 * End inline drm_release
446 */
447
b5e89ed5
DA
448 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
449 spin_lock(&dev->count_lock);
450 if (!--dev->open_count) {
451 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
452 DRM_ERROR("Device busy: %d %d\n",
453 atomic_read(&dev->ioctl_count), dev->blocked);
454 spin_unlock(&dev->count_lock);
1da177e4
LT
455 unlock_kernel();
456 return -EBUSY;
457 }
b5e89ed5 458 spin_unlock(&dev->count_lock);
1da177e4 459 unlock_kernel();
22eae947 460 return drm_lastclose(dev);
1da177e4 461 }
b5e89ed5 462 spin_unlock(&dev->count_lock);
1da177e4
LT
463
464 unlock_kernel();
465
466 return retcode;
467}
468EXPORT_SYMBOL(drm_release);
469
1da177e4
LT
470/** No-op. */
471unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
472{
473 return 0;
474}
b5e89ed5 475EXPORT_SYMBOL(drm_poll);