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>
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
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.
38 #include "drm_sarea.h"
39 #include <linux/poll.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)
49 if (dev->driver->firstopen) {
50 ret = dev->driver->firstopen(dev);
55 atomic_set(&dev->ioctl_count, 0);
56 atomic_set(&dev->vma_count, 0);
58 atomic_set(&dev->buf_alloc, 0);
60 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
61 i = drm_dma_setup(dev);
66 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
67 atomic_set(&dev->counts[i], 0);
69 dev->sigdata.lock = NULL;
72 dev->queue_reserved = 0;
74 dev->queuelist = NULL;
75 dev->context_flag = 0;
76 dev->interrupt_flag = 0;
78 dev->last_context = 0;
80 dev->last_checked = 0;
81 init_waitqueue_head(&dev->context_wait);
87 dev->buf_async = NULL;
88 init_waitqueue_head(&dev->buf_readers);
89 init_waitqueue_head(&dev->buf_writers);
94 * The kernel's context could be created here, but is now created
95 * in drm_dma_enqueue. This is more resource-efficient for
96 * hardware that does not do DMA, but may mean that
97 * drm_select_queue fails between the time the interrupt is
98 * initialized and the time the queues are initialized.
107 * \param inode device inode
108 * \param filp file pointer.
109 * \return zero on success or a negative number on failure.
111 * Searches the DRM device with the same minor number, calls open_helper(), and
112 * increments the device open count. If the open count was previous at zero,
113 * i.e., it's the first that the device is open, then calls setup().
115 int drm_open(struct inode *inode, struct file *filp)
117 struct drm_device *dev = NULL;
118 int minor_id = iminor(inode);
119 struct drm_minor *minor;
122 minor = idr_find(&drm_minors_idr, minor_id);
126 if (!(dev = minor->dev))
129 retcode = drm_open_helper(inode, filp, dev);
131 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
132 spin_lock(&dev->count_lock);
133 if (!dev->open_count++) {
134 spin_unlock(&dev->count_lock);
135 retcode = drm_setup(dev);
138 spin_unlock(&dev->count_lock);
142 mutex_lock(&dev->struct_mutex);
143 BUG_ON((dev->dev_mapping != NULL) &&
144 (dev->dev_mapping != inode->i_mapping));
145 if (dev->dev_mapping == NULL)
146 dev->dev_mapping = inode->i_mapping;
147 mutex_unlock(&dev->struct_mutex);
151 EXPORT_SYMBOL(drm_open);
154 * File \c open operation.
156 * \param inode device inode.
157 * \param filp file pointer.
159 * Puts the dev->fops corresponding to the device minor number into
160 * \p filp, call the \c open method, and restore the file operations.
162 int drm_stub_open(struct inode *inode, struct file *filp)
164 struct drm_device *dev = NULL;
165 struct drm_minor *minor;
166 int minor_id = iminor(inode);
168 const struct file_operations *old_fops;
172 minor = idr_find(&drm_minors_idr, minor_id);
176 if (!(dev = minor->dev))
179 old_fops = filp->f_op;
180 filp->f_op = fops_get(&dev->driver->fops);
181 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
182 fops_put(filp->f_op);
183 filp->f_op = fops_get(old_fops);
191 * Check whether DRI will run on this CPU.
193 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
195 static int drm_cpu_valid(void)
197 #if defined(__i386__)
198 if (boot_cpu_data.x86 == 3)
199 return 0; /* No cmpxchg on a 386 */
201 #if defined(__sparc__) && !defined(__sparc_v9__)
202 return 0; /* No cmpxchg before v9 sparc. */
208 * Called whenever a process opens /dev/drm.
210 * \param inode device inode.
211 * \param filp file pointer.
213 * \return zero on success or a negative number on failure.
215 * Creates and initializes a drm_file structure for the file private data in \p
216 * filp and add it into the double linked list in \p dev.
218 static int drm_open_helper(struct inode *inode, struct file *filp,
219 struct drm_device * dev)
221 int minor_id = iminor(inode);
222 struct drm_file *priv;
226 if (filp->f_flags & O_EXCL)
227 return -EBUSY; /* No exclusive opens */
228 if (!drm_cpu_valid())
231 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor_id);
233 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
237 memset(priv, 0, sizeof(*priv));
238 filp->private_data = priv;
240 priv->uid = current->euid;
241 priv->pid = current->pid;
242 priv->minor = idr_find(&drm_minors_idr, minor_id);
243 priv->ioctl_count = 0;
244 /* for compatibility root is always authenticated */
245 priv->authenticated = capable(CAP_SYS_ADMIN);
246 priv->lock_count = 0;
248 INIT_LIST_HEAD(&priv->lhead);
249 INIT_LIST_HEAD(&priv->refd_objects);
250 INIT_LIST_HEAD(&priv->fbs);
252 for (i = 0; i < _DRM_NO_REF_TYPES; ++i) {
253 ret = drm_ht_create(&priv->refd_object_hash[i],
254 DRM_FILE_HASH_ORDER);
260 for (j = 0; j < i; ++j)
261 drm_ht_remove(&priv->refd_object_hash[j]);
265 if (dev->driver->open) {
266 ret = dev->driver->open(dev, priv);
272 /* if there is no current master make this fd it */
273 mutex_lock(&dev->struct_mutex);
274 if (!priv->minor->master) {
275 priv->minor->master = drm_get_master(priv->minor);
276 if (!priv->minor->master) {
282 priv->master = priv->minor->master;
284 priv->authenticated = 1;
285 mutex_unlock(&dev->struct_mutex);
286 if (dev->driver->master_create) {
287 ret = dev->driver->master_create(dev, priv->master);
289 drm_put_master(priv->minor->master);
290 priv->minor->master = priv->master = NULL;
291 mutex_unlock(&dev->struct_mutex);
296 priv->master = priv->minor->master;
297 mutex_unlock(&dev->struct_mutex);
300 mutex_lock(&dev->struct_mutex);
301 list_add(&priv->lhead, &dev->filelist);
302 mutex_unlock(&dev->struct_mutex);
309 struct pci_dev *pci_dev;
310 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
312 dev->hose = pci_dev->sysdata;
313 pci_dev_put(pci_dev);
316 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
318 dev->hose = b->sysdata;
325 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
326 filp->private_data = NULL;
331 int drm_fasync(int fd, struct file *filp, int on)
333 struct drm_file *priv = filp->private_data;
334 struct drm_device *dev = priv->minor->dev;
337 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
338 (long)old_encode_dev(priv->minor->device));
339 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
344 EXPORT_SYMBOL(drm_fasync);
346 static void drm_object_release(struct file *filp)
348 struct drm_file *priv = filp->private_data;
349 struct list_head *head;
350 struct drm_ref_object *ref_object;
354 * Free leftover ref objects created by me. Note that we cannot use
355 * list_for_each() here, as the struct_mutex may be temporarily
356 * released by the remove_() functions, and thus the lists may be
358 * Also, a drm_remove_ref_object() will not remove it
359 * from the list unless its refcount is 1.
362 head = &priv->refd_objects;
363 while (head->next != head) {
364 ref_object = list_entry(head->next, struct drm_ref_object, list);
365 drm_remove_ref_object(priv, ref_object);
366 head = &priv->refd_objects;
369 for (i = 0; i < _DRM_NO_REF_TYPES; ++i)
370 drm_ht_remove(&priv->refd_object_hash[i]);
376 * \param inode device inode
377 * \param file_priv DRM file private.
378 * \return zero on success or a negative number on failure.
380 * If the hardware lock is held then free it, and take it again for the kernel
381 * context since it's necessary to reclaim buffers. Unlink the file private
382 * data from its list and free it. Decreases the open count and if it reaches
383 * zero calls drm_lastclose().
385 int drm_release(struct inode *inode, struct file *filp)
387 struct drm_file *file_priv = filp->private_data;
388 struct drm_device *dev = file_priv->minor->dev;
393 DRM_DEBUG("open_count = %d\n", dev->open_count);
395 if (dev->driver->preclose)
396 dev->driver->preclose(dev, file_priv);
398 /* ========================================================
399 * Begin inline drm_release
402 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
403 current->pid, (long)old_encode_dev(file_priv->minor->device),
406 /* if the master has gone away we can't do anything with the lock */
407 if (file_priv->minor->master) {
408 if (dev->driver->reclaim_buffers_locked && file_priv->master->lock.hw_lock) {
409 if (drm_i_have_hw_lock(dev, file_priv)) {
410 dev->driver->reclaim_buffers_locked(dev, file_priv);
412 unsigned long _end=jiffies + 3*DRM_HZ;
415 drm_idlelock_take(&file_priv->master->lock);
422 spin_lock_bh(&file_priv->master->lock.spinlock);
423 locked = file_priv->master->lock.idle_has_lock;
424 spin_unlock_bh(&file_priv->master->lock.spinlock);
428 } while (!time_after_eq(jiffies, _end));
431 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
432 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
433 "\tI will go on reclaiming the buffers anyway.\n");
436 dev->driver->reclaim_buffers_locked(dev, file_priv);
437 drm_idlelock_release(&file_priv->master->lock);
441 if (dev->driver->reclaim_buffers_idlelocked && file_priv->master->lock.hw_lock) {
443 drm_idlelock_take(&file_priv->master->lock);
444 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
445 drm_idlelock_release(&file_priv->master->lock);
450 if (drm_i_have_hw_lock(dev, file_priv)) {
451 DRM_DEBUG("File %p released, freeing lock for context %d\n",
452 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
454 drm_lock_free(&file_priv->master->lock,
455 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
459 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
460 !dev->driver->reclaim_buffers_locked) {
461 dev->driver->reclaim_buffers(dev, file_priv);
465 drm_fasync(-1, filp, 0);
467 mutex_lock(&dev->ctxlist_mutex);
469 if (!list_empty(&dev->ctxlist)) {
470 struct drm_ctx_list *pos, *n;
472 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
473 if (pos->tag == file_priv &&
474 pos->handle != DRM_KERNEL_CONTEXT) {
475 if (dev->driver->context_dtor)
476 dev->driver->context_dtor(dev,
479 drm_ctxbitmap_free(dev, pos->handle);
481 list_del(&pos->head);
482 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
487 mutex_unlock(&dev->ctxlist_mutex);
489 if (drm_core_check_feature(dev, DRIVER_MODESET))
490 drm_fb_release(filp);
492 if (file_priv->is_master) {
493 struct drm_file *temp;
494 list_for_each_entry(temp, &dev->filelist, lhead) {
495 if ((temp->master == file_priv->master) &&
497 temp->authenticated = 0;
500 if (file_priv->minor->master == file_priv->master)
501 file_priv->minor->master = NULL;
502 drm_put_master(file_priv->master);
505 file_priv->master = NULL;
506 file_priv->is_master = 0;
508 mutex_lock(&dev->struct_mutex);
509 drm_object_release(filp);
511 list_del(&file_priv->lhead);
513 mutex_unlock(&dev->struct_mutex);
515 if (dev->driver->postclose)
516 dev->driver->postclose(dev, file_priv);
517 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
519 /* ========================================================
520 * End inline drm_release
523 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
524 spin_lock(&dev->count_lock);
525 if (!--dev->open_count) {
526 if (atomic_read(&dev->ioctl_count)) {
527 DRM_ERROR("Device busy: %d\n",
528 atomic_read(&dev->ioctl_count));
529 spin_unlock(&dev->count_lock);
533 spin_unlock(&dev->count_lock);
535 return drm_lastclose(dev);
537 spin_unlock(&dev->count_lock);
543 EXPORT_SYMBOL(drm_release);
546 /* This is to deal with older X servers that believe 0 means data is
547 * available which is not the correct return for a poll function.
548 * This cannot be fixed until the Xserver is fixed. Xserver will need
549 * to set a newer interface version to avoid breaking older Xservers.
550 * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
551 * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
552 * to return the correct response.
554 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
556 /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
559 EXPORT_SYMBOL(drm_poll);