initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / gpu / drm / drm_fops.c
blob519161e7e721d5461879766a61853f6c6d5bbfc2
1 /**
2 * \file drm_fops.c
3 * File operations for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
8 */
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
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:
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.
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.
37 #include "drmP.h"
38 #include <linux/poll.h>
39 #include <linux/smp_lock.h>
41 static int drm_open_helper(struct inode *inode, struct file *filp,
42 struct drm_device * dev);
44 static int drm_setup(struct drm_device * dev)
46 int i;
47 int ret;
49 if (dev->driver->firstopen) {
50 ret = dev->driver->firstopen(dev);
51 if (ret != 0)
52 return ret;
55 atomic_set(&dev->ioctl_count, 0);
56 atomic_set(&dev->vma_count, 0);
58 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
59 !drm_core_check_feature(dev, DRIVER_MODESET)) {
60 dev->buf_use = 0;
61 atomic_set(&dev->buf_alloc, 0);
63 i = drm_dma_setup(dev);
64 if (i < 0)
65 return i;
68 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
69 atomic_set(&dev->counts[i], 0);
71 dev->sigdata.lock = NULL;
73 dev->queue_count = 0;
74 dev->queue_reserved = 0;
75 dev->queue_slots = 0;
76 dev->queuelist = NULL;
77 dev->context_flag = 0;
78 dev->interrupt_flag = 0;
79 dev->dma_flag = 0;
80 dev->last_context = 0;
81 dev->last_switch = 0;
82 dev->last_checked = 0;
83 init_waitqueue_head(&dev->context_wait);
84 dev->if_version = 0;
86 dev->ctx_start = 0;
87 dev->lck_start = 0;
89 dev->buf_async = NULL;
90 init_waitqueue_head(&dev->buf_readers);
91 init_waitqueue_head(&dev->buf_writers);
93 DRM_DEBUG("\n");
96 * The kernel's context could be created here, but is now created
97 * in drm_dma_enqueue. This is more resource-efficient for
98 * hardware that does not do DMA, but may mean that
99 * drm_select_queue fails between the time the interrupt is
100 * initialized and the time the queues are initialized.
103 return 0;
107 * Open file.
109 * \param inode device inode
110 * \param filp file pointer.
111 * \return zero on success or a negative number on failure.
113 * Searches the DRM device with the same minor number, calls open_helper(), and
114 * increments the device open count. If the open count was previous at zero,
115 * i.e., it's the first that the device is open, then calls setup().
117 int drm_open(struct inode *inode, struct file *filp)
119 struct drm_device *dev = NULL;
120 int minor_id = iminor(inode);
121 struct drm_minor *minor;
122 int retcode = 0;
124 minor = idr_find(&drm_minors_idr, minor_id);
125 if (!minor)
126 return -ENODEV;
128 if (!(dev = minor->dev))
129 return -ENODEV;
131 retcode = drm_open_helper(inode, filp, dev);
132 if (!retcode) {
133 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
134 spin_lock(&dev->count_lock);
135 if (!dev->open_count++) {
136 spin_unlock(&dev->count_lock);
137 retcode = drm_setup(dev);
138 goto out;
140 spin_unlock(&dev->count_lock);
142 out:
143 if (!retcode) {
144 mutex_lock(&dev->struct_mutex);
145 if (minor->type == DRM_MINOR_LEGACY) {
146 if (dev->dev_mapping == NULL)
147 dev->dev_mapping = inode->i_mapping;
148 else if (dev->dev_mapping != inode->i_mapping)
149 retcode = -ENODEV;
151 mutex_unlock(&dev->struct_mutex);
154 return retcode;
156 EXPORT_SYMBOL(drm_open);
159 * File \c open operation.
161 * \param inode device inode.
162 * \param filp file pointer.
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.
167 int drm_stub_open(struct inode *inode, struct file *filp)
169 struct drm_device *dev = NULL;
170 struct drm_minor *minor;
171 int minor_id = iminor(inode);
172 int err = -ENODEV;
173 const struct file_operations *old_fops;
175 DRM_DEBUG("\n");
177 /* BKL pushdown: note that nothing else serializes idr_find() */
178 lock_kernel();
179 minor = idr_find(&drm_minors_idr, minor_id);
180 if (!minor)
181 goto out;
183 if (!(dev = minor->dev))
184 goto out;
186 old_fops = filp->f_op;
187 filp->f_op = fops_get(&dev->driver->fops);
188 if (filp->f_op == NULL) {
189 filp->f_op = old_fops;
190 goto out;
192 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
193 fops_put(filp->f_op);
194 filp->f_op = fops_get(old_fops);
196 fops_put(old_fops);
198 out:
199 unlock_kernel();
200 return err;
204 * Check whether DRI will run on this CPU.
206 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
208 static int drm_cpu_valid(void)
210 #if defined(__i386__)
211 if (boot_cpu_data.x86 == 3)
212 return 0; /* No cmpxchg on a 386 */
213 #endif
214 #if defined(__sparc__) && !defined(__sparc_v9__)
215 return 0; /* No cmpxchg before v9 sparc. */
216 #endif
217 return 1;
221 * Called whenever a process opens /dev/drm.
223 * \param inode device inode.
224 * \param filp file pointer.
225 * \param dev device.
226 * \return zero on success or a negative number on failure.
228 * Creates and initializes a drm_file structure for the file private data in \p
229 * filp and add it into the double linked list in \p dev.
231 static int drm_open_helper(struct inode *inode, struct file *filp,
232 struct drm_device * dev)
234 int minor_id = iminor(inode);
235 struct drm_file *priv;
236 int ret;
238 if (filp->f_flags & O_EXCL)
239 return -EBUSY; /* No exclusive opens */
240 if (!drm_cpu_valid())
241 return -EINVAL;
243 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
245 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
246 if (!priv)
247 return -ENOMEM;
249 memset(priv, 0, sizeof(*priv));
250 filp->private_data = priv;
251 priv->filp = filp;
252 priv->uid = current_euid();
253 priv->pid = task_pid_nr(current);
254 priv->minor = idr_find(&drm_minors_idr, minor_id);
255 priv->ioctl_count = 0;
256 /* for compatibility root is always authenticated */
257 priv->authenticated = capable(CAP_SYS_ADMIN);
258 priv->lock_count = 0;
260 INIT_LIST_HEAD(&priv->lhead);
261 INIT_LIST_HEAD(&priv->fbs);
263 if (dev->driver->driver_features & DRIVER_GEM)
264 drm_gem_open(dev, priv);
266 if (dev->driver->open) {
267 ret = dev->driver->open(dev, priv);
268 if (ret < 0)
269 goto out_free;
273 /* if there is no current master make this fd it */
274 mutex_lock(&dev->struct_mutex);
275 if (!priv->minor->master) {
276 /* create a new master */
277 priv->minor->master = drm_master_create(priv->minor);
278 if (!priv->minor->master) {
279 mutex_unlock(&dev->struct_mutex);
280 ret = -ENOMEM;
281 goto out_free;
284 priv->is_master = 1;
285 /* take another reference for the copy in the local file priv */
286 priv->master = drm_master_get(priv->minor->master);
288 priv->authenticated = 1;
290 mutex_unlock(&dev->struct_mutex);
291 if (dev->driver->master_create) {
292 ret = dev->driver->master_create(dev, priv->master);
293 if (ret) {
294 mutex_lock(&dev->struct_mutex);
295 /* drop both references if this fails */
296 drm_master_put(&priv->minor->master);
297 drm_master_put(&priv->master);
298 mutex_unlock(&dev->struct_mutex);
299 goto out_free;
302 } else {
303 /* get a reference to the master */
304 priv->master = drm_master_get(priv->minor->master);
305 mutex_unlock(&dev->struct_mutex);
308 mutex_lock(&dev->struct_mutex);
309 list_add(&priv->lhead, &dev->filelist);
310 mutex_unlock(&dev->struct_mutex);
312 #ifdef __alpha__
314 * Default the hose
316 if (!dev->hose) {
317 struct pci_dev *pci_dev;
318 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
319 if (pci_dev) {
320 dev->hose = pci_dev->sysdata;
321 pci_dev_put(pci_dev);
323 if (!dev->hose) {
324 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
325 if (b)
326 dev->hose = b->sysdata;
329 #endif
331 return 0;
332 out_free:
333 kfree(priv);
334 filp->private_data = NULL;
335 return ret;
338 /** No-op. */
339 int drm_fasync(int fd, struct file *filp, int on)
341 struct drm_file *priv = filp->private_data;
342 struct drm_device *dev = priv->minor->dev;
344 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
345 (long)old_encode_dev(priv->minor->device));
346 return fasync_helper(fd, filp, on, &dev->buf_async);
348 EXPORT_SYMBOL(drm_fasync);
351 * Reclaim locked buffers; note that this may be a bad idea if the current
352 * context doesn't have the hw lock...
354 static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
356 struct drm_file *file_priv = f->private_data;
358 if (drm_i_have_hw_lock(dev, file_priv)) {
359 dev->driver->reclaim_buffers_locked(dev, file_priv);
360 } else {
361 unsigned long _end = jiffies + 3 * DRM_HZ;
362 int locked = 0;
364 drm_idlelock_take(&file_priv->master->lock);
367 * Wait for a while.
369 do {
370 spin_lock_bh(&file_priv->master->lock.spinlock);
371 locked = file_priv->master->lock.idle_has_lock;
372 spin_unlock_bh(&file_priv->master->lock.spinlock);
373 if (locked)
374 break;
375 schedule();
376 } while (!time_after_eq(jiffies, _end));
378 if (!locked) {
379 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
380 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
381 "\tI will go on reclaiming the buffers anyway.\n");
384 dev->driver->reclaim_buffers_locked(dev, file_priv);
385 drm_idlelock_release(&file_priv->master->lock);
389 static void drm_master_release(struct drm_device *dev, struct file *filp)
391 struct drm_file *file_priv = filp->private_data;
393 if (dev->driver->reclaim_buffers_locked &&
394 file_priv->master->lock.hw_lock)
395 drm_reclaim_locked_buffers(dev, filp);
397 if (dev->driver->reclaim_buffers_idlelocked &&
398 file_priv->master->lock.hw_lock) {
399 drm_idlelock_take(&file_priv->master->lock);
400 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
401 drm_idlelock_release(&file_priv->master->lock);
405 if (drm_i_have_hw_lock(dev, file_priv)) {
406 DRM_DEBUG("File %p released, freeing lock for context %d\n",
407 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
408 drm_lock_free(&file_priv->master->lock,
409 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
412 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
413 !dev->driver->reclaim_buffers_locked) {
414 dev->driver->reclaim_buffers(dev, file_priv);
419 * Release file.
421 * \param inode device inode
422 * \param file_priv DRM file private.
423 * \return zero on success or a negative number on failure.
425 * If the hardware lock is held then free it, and take it again for the kernel
426 * context since it's necessary to reclaim buffers. Unlink the file private
427 * data from its list and free it. Decreases the open count and if it reaches
428 * zero calls drm_lastclose().
430 int drm_release(struct inode *inode, struct file *filp)
432 struct drm_file *file_priv = filp->private_data;
433 struct drm_device *dev = file_priv->minor->dev;
434 int retcode = 0;
436 lock_kernel();
438 DRM_DEBUG("open_count = %d\n", dev->open_count);
440 if (dev->driver->preclose)
441 dev->driver->preclose(dev, file_priv);
443 /* ========================================================
444 * Begin inline drm_release
447 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
448 task_pid_nr(current),
449 (long)old_encode_dev(file_priv->minor->device),
450 dev->open_count);
452 /* Release any auth tokens that might point to this file_priv,
453 (do that under the drm_global_mutex) */
454 if (file_priv->magic)
455 (void) drm_remove_magic(file_priv->master, file_priv->magic);
457 /* if the master has gone away we can't do anything with the lock */
458 if (file_priv->minor->master)
459 drm_master_release(dev, filp);
461 if (dev->driver->driver_features & DRIVER_GEM)
462 drm_gem_release(dev, file_priv);
464 if (dev->driver->driver_features & DRIVER_MODESET)
465 drm_fb_release(file_priv);
467 mutex_lock(&dev->ctxlist_mutex);
468 if (!list_empty(&dev->ctxlist)) {
469 struct drm_ctx_list *pos, *n;
471 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
472 if (pos->tag == file_priv &&
473 pos->handle != DRM_KERNEL_CONTEXT) {
474 if (dev->driver->context_dtor)
475 dev->driver->context_dtor(dev,
476 pos->handle);
478 drm_ctxbitmap_free(dev, pos->handle);
480 list_del(&pos->head);
481 kfree(pos);
482 --dev->ctx_count;
486 mutex_unlock(&dev->ctxlist_mutex);
488 mutex_lock(&dev->struct_mutex);
490 if (file_priv->is_master) {
491 struct drm_master *master = file_priv->master;
492 struct drm_file *temp;
493 list_for_each_entry(temp, &dev->filelist, lhead) {
494 if ((temp->master == file_priv->master) &&
495 (temp != file_priv))
496 temp->authenticated = 0;
500 * Since the master is disappearing, so is the
501 * possibility to lock.
504 if (master->lock.hw_lock) {
505 if (dev->sigdata.lock == master->lock.hw_lock)
506 dev->sigdata.lock = NULL;
507 master->lock.hw_lock = NULL;
508 master->lock.file_priv = NULL;
509 wake_up_interruptible_all(&master->lock.lock_queue);
512 if (file_priv->minor->master == file_priv->master) {
513 /* drop the reference held my the minor */
514 drm_master_put(&file_priv->minor->master);
518 /* drop the reference held my the file priv */
519 drm_master_put(&file_priv->master);
520 file_priv->is_master = 0;
521 list_del(&file_priv->lhead);
522 mutex_unlock(&dev->struct_mutex);
524 if (dev->driver->postclose)
525 dev->driver->postclose(dev, file_priv);
526 kfree(file_priv);
528 /* ========================================================
529 * End inline drm_release
532 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
533 spin_lock(&dev->count_lock);
534 if (!--dev->open_count) {
535 if (atomic_read(&dev->ioctl_count)) {
536 DRM_ERROR("Device busy: %d\n",
537 atomic_read(&dev->ioctl_count));
538 spin_unlock(&dev->count_lock);
539 unlock_kernel();
540 return -EBUSY;
542 spin_unlock(&dev->count_lock);
543 unlock_kernel();
544 return drm_lastclose(dev);
546 spin_unlock(&dev->count_lock);
548 unlock_kernel();
550 return retcode;
552 EXPORT_SYMBOL(drm_release);
554 /** No-op. */
555 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
557 return 0;
559 EXPORT_SYMBOL(drm_poll);