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->driver_features & DRIVER_GEM)
266 drm_gem_open(dev, priv);
268 if (dev->driver->open) {
269 ret = dev->driver->open(dev, priv);
275 /* if there is no current master make this fd it */
276 mutex_lock(&dev->struct_mutex);
277 if (!priv->minor->master) {
278 priv->minor->master = drm_get_master(priv->minor);
279 if (!priv->minor->master) {
285 priv->master = priv->minor->master;
287 priv->authenticated = 1;
288 mutex_unlock(&dev->struct_mutex);
289 if (dev->driver->master_create) {
290 ret = dev->driver->master_create(dev, priv->master);
292 drm_put_master(priv->minor->master);
293 priv->minor->master = priv->master = NULL;
294 mutex_unlock(&dev->struct_mutex);
299 priv->master = priv->minor->master;
300 mutex_unlock(&dev->struct_mutex);
303 mutex_lock(&dev->struct_mutex);
304 list_add(&priv->lhead, &dev->filelist);
305 mutex_unlock(&dev->struct_mutex);
312 struct pci_dev *pci_dev;
313 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
315 dev->hose = pci_dev->sysdata;
316 pci_dev_put(pci_dev);
319 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
321 dev->hose = b->sysdata;
328 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
329 filp->private_data = NULL;
334 int drm_fasync(int fd, struct file *filp, int on)
336 struct drm_file *priv = filp->private_data;
337 struct drm_device *dev = priv->minor->dev;
340 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
341 (long)old_encode_dev(priv->minor->device));
342 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
347 EXPORT_SYMBOL(drm_fasync);
349 static void drm_object_release(struct file *filp)
351 struct drm_file *priv = filp->private_data;
352 struct list_head *head;
353 struct drm_ref_object *ref_object;
357 * Free leftover ref objects created by me. Note that we cannot use
358 * list_for_each() here, as the struct_mutex may be temporarily
359 * released by the remove_() functions, and thus the lists may be
361 * Also, a drm_remove_ref_object() will not remove it
362 * from the list unless its refcount is 1.
365 head = &priv->refd_objects;
366 while (head->next != head) {
367 ref_object = list_entry(head->next, struct drm_ref_object, list);
368 drm_remove_ref_object(priv, ref_object);
369 head = &priv->refd_objects;
372 for (i = 0; i < _DRM_NO_REF_TYPES; ++i)
373 drm_ht_remove(&priv->refd_object_hash[i]);
379 * \param inode device inode
380 * \param file_priv DRM file private.
381 * \return zero on success or a negative number on failure.
383 * If the hardware lock is held then free it, and take it again for the kernel
384 * context since it's necessary to reclaim buffers. Unlink the file private
385 * data from its list and free it. Decreases the open count and if it reaches
386 * zero calls drm_lastclose().
388 int drm_release(struct inode *inode, struct file *filp)
390 struct drm_file *file_priv = filp->private_data;
391 struct drm_device *dev = file_priv->minor->dev;
396 DRM_DEBUG("open_count = %d\n", dev->open_count);
398 if (dev->driver->preclose)
399 dev->driver->preclose(dev, file_priv);
401 /* ========================================================
402 * Begin inline drm_release
405 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
406 current->pid, (long)old_encode_dev(file_priv->minor->device),
409 /* if the master has gone away we can't do anything with the lock */
410 if (file_priv->minor->master) {
411 if (dev->driver->reclaim_buffers_locked && file_priv->master->lock.hw_lock) {
412 if (drm_i_have_hw_lock(dev, file_priv)) {
413 dev->driver->reclaim_buffers_locked(dev, file_priv);
415 unsigned long _end=jiffies + 3*DRM_HZ;
418 drm_idlelock_take(&file_priv->master->lock);
425 spin_lock_bh(&file_priv->master->lock.spinlock);
426 locked = file_priv->master->lock.idle_has_lock;
427 spin_unlock_bh(&file_priv->master->lock.spinlock);
431 } while (!time_after_eq(jiffies, _end));
434 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
435 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
436 "\tI will go on reclaiming the buffers anyway.\n");
439 dev->driver->reclaim_buffers_locked(dev, file_priv);
440 drm_idlelock_release(&file_priv->master->lock);
444 if (dev->driver->reclaim_buffers_idlelocked && file_priv->master->lock.hw_lock) {
446 drm_idlelock_take(&file_priv->master->lock);
447 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
448 drm_idlelock_release(&file_priv->master->lock);
453 if (drm_i_have_hw_lock(dev, file_priv)) {
454 DRM_DEBUG("File %p released, freeing lock for context %d\n",
455 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
457 drm_lock_free(&file_priv->master->lock,
458 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
462 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
463 !dev->driver->reclaim_buffers_locked) {
464 dev->driver->reclaim_buffers(dev, file_priv);
468 if (dev->driver->driver_features & DRIVER_GEM)
469 drm_gem_release(dev, file_priv);
471 drm_fasync(-1, filp, 0);
473 mutex_lock(&dev->ctxlist_mutex);
475 if (!list_empty(&dev->ctxlist)) {
476 struct drm_ctx_list *pos, *n;
478 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
479 if (pos->tag == file_priv &&
480 pos->handle != DRM_KERNEL_CONTEXT) {
481 if (dev->driver->context_dtor)
482 dev->driver->context_dtor(dev,
485 drm_ctxbitmap_free(dev, pos->handle);
487 list_del(&pos->head);
488 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
493 mutex_unlock(&dev->ctxlist_mutex);
495 if (drm_core_check_feature(dev, DRIVER_MODESET))
496 drm_fb_release(filp);
498 if (file_priv->is_master) {
499 struct drm_file *temp;
500 list_for_each_entry(temp, &dev->filelist, lhead) {
501 if ((temp->master == file_priv->master) &&
503 temp->authenticated = 0;
506 if (file_priv->minor->master == file_priv->master)
507 file_priv->minor->master = NULL;
508 drm_put_master(file_priv->master);
511 file_priv->master = NULL;
512 file_priv->is_master = 0;
514 mutex_lock(&dev->struct_mutex);
515 drm_object_release(filp);
517 list_del(&file_priv->lhead);
519 mutex_unlock(&dev->struct_mutex);
521 if (dev->driver->postclose)
522 dev->driver->postclose(dev, file_priv);
523 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
525 /* ========================================================
526 * End inline drm_release
529 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
530 spin_lock(&dev->count_lock);
531 if (!--dev->open_count) {
532 if (atomic_read(&dev->ioctl_count)) {
533 DRM_ERROR("Device busy: %d\n",
534 atomic_read(&dev->ioctl_count));
535 spin_unlock(&dev->count_lock);
539 spin_unlock(&dev->count_lock);
541 return drm_lastclose(dev);
543 spin_unlock(&dev->count_lock);
549 EXPORT_SYMBOL(drm_release);
552 /* This is to deal with older X servers that believe 0 means data is
553 * available which is not the correct return for a poll function.
554 * This cannot be fixed until the Xserver is fixed. Xserver will need
555 * to set a newer interface version to avoid breaking older Xservers.
556 * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
557 * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
558 * to return the correct response.
560 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
562 /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
565 EXPORT_SYMBOL(drm_poll);