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)
51 if (dev->driver->firstopen) {
52 ret = dev->driver->firstopen(dev);
57 dev->magicfree.next = NULL;
59 /* prebuild the SAREA */
60 sareapage = max(SAREA_MAX, PAGE_SIZE);
61 i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
65 atomic_set(&dev->ioctl_count, 0);
66 atomic_set(&dev->vma_count, 0);
68 atomic_set(&dev->buf_alloc, 0);
70 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
71 i = drm_dma_setup(dev);
76 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
77 atomic_set(&dev->counts[i], 0);
79 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
80 INIT_LIST_HEAD(&dev->magicfree);
82 dev->sigdata.lock = NULL;
83 init_waitqueue_head(&dev->lock.lock_queue);
85 dev->queue_reserved = 0;
87 dev->queuelist = NULL;
88 dev->context_flag = 0;
89 dev->interrupt_flag = 0;
91 dev->last_context = 0;
93 dev->last_checked = 0;
94 init_waitqueue_head(&dev->context_wait);
100 dev->buf_async = NULL;
101 init_waitqueue_head(&dev->buf_readers);
102 init_waitqueue_head(&dev->buf_writers);
107 * The kernel's context could be created here, but is now created
108 * in drm_dma_enqueue. This is more resource-efficient for
109 * hardware that does not do DMA, but may mean that
110 * drm_select_queue fails between the time the interrupt is
111 * initialized and the time the queues are initialized.
120 * \param inode device inode
121 * \param filp file pointer.
122 * \return zero on success or a negative number on failure.
124 * Searches the DRM device with the same minor number, calls open_helper(), and
125 * increments the device open count. If the open count was previous at zero,
126 * i.e., it's the first that the device is open, then calls setup().
128 int drm_open(struct inode *inode, struct file *filp)
130 struct drm_device *dev = NULL;
131 int minor_id = iminor(inode);
132 struct drm_minor *minor;
135 minor = idr_find(&drm_minors_idr, minor_id);
139 if (!(dev = minor->dev))
142 retcode = drm_open_helper(inode, filp, dev);
144 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
145 spin_lock(&dev->count_lock);
146 if (!dev->open_count++) {
147 spin_unlock(&dev->count_lock);
148 retcode = drm_setup(dev);
151 spin_unlock(&dev->count_lock);
155 mutex_lock(&dev->struct_mutex);
156 BUG_ON((dev->dev_mapping != NULL) &&
157 (dev->dev_mapping != inode->i_mapping));
158 if (dev->dev_mapping == NULL)
159 dev->dev_mapping = inode->i_mapping;
160 mutex_unlock(&dev->struct_mutex);
164 EXPORT_SYMBOL(drm_open);
167 * File \c open operation.
169 * \param inode device inode.
170 * \param filp file pointer.
172 * Puts the dev->fops corresponding to the device minor number into
173 * \p filp, call the \c open method, and restore the file operations.
175 int drm_stub_open(struct inode *inode, struct file *filp)
177 struct drm_device *dev = NULL;
178 struct drm_minor *minor;
179 int minor_id = iminor(inode);
181 const struct file_operations *old_fops;
185 minor = idr_find(&drm_minors_idr, minor_id);
189 if (!(dev = minor->dev))
192 old_fops = filp->f_op;
193 filp->f_op = fops_get(&dev->driver->fops);
194 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
195 fops_put(filp->f_op);
196 filp->f_op = fops_get(old_fops);
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 */
214 #if defined(__sparc__) && !defined(__sparc_v9__)
215 return 0; /* No cmpxchg before v9 sparc. */
221 * Called whenever a process opens /dev/drm.
223 * \param inode device inode.
224 * \param filp file pointer.
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;
239 if (filp->f_flags & O_EXCL)
240 return -EBUSY; /* No exclusive opens */
241 if (!drm_cpu_valid())
244 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor_id);
246 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
250 memset(priv, 0, sizeof(*priv));
251 filp->private_data = priv;
253 priv->uid = current_euid();
254 priv->pid = current->pid;
255 priv->minor = idr_find(&drm_minors_idr, minor_id);
256 priv->ioctl_count = 0;
257 /* for compatibility root is always authenticated */
258 priv->authenticated = capable(CAP_SYS_ADMIN);
259 priv->lock_count = 0;
261 INIT_LIST_HEAD(&priv->lhead);
262 INIT_LIST_HEAD(&priv->refd_objects);
264 for (i = 0; i < _DRM_NO_REF_TYPES; ++i) {
265 ret = drm_ht_create(&priv->refd_object_hash[i],
266 DRM_FILE_HASH_ORDER);
272 for (j = 0; j < i; ++j)
273 drm_ht_remove(&priv->refd_object_hash[j]);
277 if (dev->driver->driver_features & DRIVER_GEM)
278 drm_gem_open(dev, priv);
280 if (dev->driver->open) {
281 ret = dev->driver->open(dev, priv);
286 mutex_lock(&dev->struct_mutex);
287 if (list_empty(&dev->filelist))
290 list_add(&priv->lhead, &dev->filelist);
291 mutex_unlock(&dev->struct_mutex);
298 struct pci_dev *pci_dev;
299 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
301 dev->hose = pci_dev->sysdata;
302 pci_dev_put(pci_dev);
305 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
307 dev->hose = b->sysdata;
314 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
315 filp->private_data = NULL;
320 int drm_fasync(int fd, struct file *filp, int on)
322 struct drm_file *priv = filp->private_data;
323 struct drm_device *dev = priv->minor->dev;
326 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
327 (long)old_encode_dev(priv->minor->device));
328 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
333 EXPORT_SYMBOL(drm_fasync);
335 static void drm_object_release(struct file *filp)
337 struct drm_file *priv = filp->private_data;
338 struct list_head *head;
339 struct drm_ref_object *ref_object;
343 * Free leftover ref objects created by me. Note that we cannot use
344 * list_for_each() here, as the struct_mutex may be temporarily
345 * released by the remove_() functions, and thus the lists may be
347 * Also, a drm_remove_ref_object() will not remove it
348 * from the list unless its refcount is 1.
351 head = &priv->refd_objects;
352 while (head->next != head) {
353 ref_object = list_entry(head->next, struct drm_ref_object, list);
354 drm_remove_ref_object(priv, ref_object);
355 head = &priv->refd_objects;
358 for (i = 0; i < _DRM_NO_REF_TYPES; ++i)
359 drm_ht_remove(&priv->refd_object_hash[i]);
365 * \param inode device inode
366 * \param file_priv DRM file private.
367 * \return zero on success or a negative number on failure.
369 * If the hardware lock is held then free it, and take it again for the kernel
370 * context since it's necessary to reclaim buffers. Unlink the file private
371 * data from its list and free it. Decreases the open count and if it reaches
372 * zero calls drm_lastclose().
374 int drm_release(struct inode *inode, struct file *filp)
376 struct drm_file *file_priv = filp->private_data;
377 struct drm_device *dev = file_priv->minor->dev;
382 DRM_DEBUG("open_count = %d\n", dev->open_count);
384 if (dev->driver->preclose)
385 dev->driver->preclose(dev, file_priv);
387 /* ========================================================
388 * Begin inline drm_release
391 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
392 current->pid, (long)old_encode_dev(file_priv->minor->device),
395 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
396 if (drm_i_have_hw_lock(dev, file_priv)) {
397 dev->driver->reclaim_buffers_locked(dev, file_priv);
399 unsigned long _end=jiffies + 3*DRM_HZ;
402 drm_idlelock_take(&dev->lock);
409 spin_lock_bh(&dev->lock.spinlock);
410 locked = dev->lock.idle_has_lock;
411 spin_unlock_bh(&dev->lock.spinlock);
415 } while (!time_after_eq(jiffies, _end));
418 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
419 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
420 "\tI will go on reclaiming the buffers anyway.\n");
423 dev->driver->reclaim_buffers_locked(dev, file_priv);
424 drm_idlelock_release(&dev->lock);
428 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
430 drm_idlelock_take(&dev->lock);
431 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
432 drm_idlelock_release(&dev->lock);
436 if (drm_i_have_hw_lock(dev, file_priv)) {
437 DRM_DEBUG("File %p released, freeing lock for context %d\n",
438 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
440 drm_lock_free(&dev->lock,
441 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
445 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
446 !dev->driver->reclaim_buffers_locked) {
447 dev->driver->reclaim_buffers(dev, file_priv);
450 if (dev->driver->driver_features & DRIVER_GEM)
451 drm_gem_release(dev, file_priv);
453 drm_fasync(-1, filp, 0);
455 mutex_lock(&dev->ctxlist_mutex);
457 if (!list_empty(&dev->ctxlist)) {
458 struct drm_ctx_list *pos, *n;
460 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
461 if (pos->tag == file_priv &&
462 pos->handle != DRM_KERNEL_CONTEXT) {
463 if (dev->driver->context_dtor)
464 dev->driver->context_dtor(dev,
467 drm_ctxbitmap_free(dev, pos->handle);
469 list_del(&pos->head);
470 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
475 mutex_unlock(&dev->ctxlist_mutex);
477 mutex_lock(&dev->struct_mutex);
478 drm_object_release(filp);
479 if (file_priv->remove_auth_on_close == 1) {
480 struct drm_file *temp;
482 list_for_each_entry(temp, &dev->filelist, lhead)
483 temp->authenticated = 0;
485 list_del(&file_priv->lhead);
486 mutex_unlock(&dev->struct_mutex);
488 if (dev->driver->postclose)
489 dev->driver->postclose(dev, file_priv);
490 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
492 /* ========================================================
493 * End inline drm_release
496 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
497 spin_lock(&dev->count_lock);
498 if (!--dev->open_count) {
499 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
500 DRM_ERROR("Device busy: %d %d\n",
501 atomic_read(&dev->ioctl_count), dev->blocked);
502 spin_unlock(&dev->count_lock);
506 spin_unlock(&dev->count_lock);
508 return drm_lastclose(dev);
510 spin_unlock(&dev->count_lock);
516 EXPORT_SYMBOL(drm_release);
519 /* This is to deal with older X servers that believe 0 means data is
520 * available which is not the correct return for a poll function.
521 * This cannot be fixed until the Xserver is fixed. Xserver will need
522 * to set a newer interface version to avoid breaking older Xservers.
523 * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
524 * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
525 * to return the correct response.
527 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
529 /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
532 EXPORT_SYMBOL(drm_poll);