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->open) {
278 ret = dev->driver->open(dev, priv);
283 mutex_lock(&dev->struct_mutex);
284 if (list_empty(&dev->filelist))
287 list_add(&priv->lhead, &dev->filelist);
288 mutex_unlock(&dev->struct_mutex);
295 struct pci_dev *pci_dev;
296 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
298 dev->hose = pci_dev->sysdata;
299 pci_dev_put(pci_dev);
302 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
304 dev->hose = b->sysdata;
311 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
312 filp->private_data = NULL;
317 int drm_fasync(int fd, struct file *filp, int on)
319 struct drm_file *priv = filp->private_data;
320 struct drm_device *dev = priv->minor->dev;
323 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
324 (long)old_encode_dev(priv->minor->device));
325 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
330 EXPORT_SYMBOL(drm_fasync);
332 static void drm_object_release(struct file *filp)
334 struct drm_file *priv = filp->private_data;
335 struct list_head *head;
336 struct drm_ref_object *ref_object;
340 * Free leftover ref objects created by me. Note that we cannot use
341 * list_for_each() here, as the struct_mutex may be temporarily
342 * released by the remove_() functions, and thus the lists may be
344 * Also, a drm_remove_ref_object() will not remove it
345 * from the list unless its refcount is 1.
348 head = &priv->refd_objects;
349 while (head->next != head) {
350 ref_object = list_entry(head->next, struct drm_ref_object, list);
351 drm_remove_ref_object(priv, ref_object);
352 head = &priv->refd_objects;
355 for (i = 0; i < _DRM_NO_REF_TYPES; ++i)
356 drm_ht_remove(&priv->refd_object_hash[i]);
362 * \param inode device inode
363 * \param file_priv DRM file private.
364 * \return zero on success or a negative number on failure.
366 * If the hardware lock is held then free it, and take it again for the kernel
367 * context since it's necessary to reclaim buffers. Unlink the file private
368 * data from its list and free it. Decreases the open count and if it reaches
369 * zero calls drm_lastclose().
371 int drm_release(struct inode *inode, struct file *filp)
373 struct drm_file *file_priv = filp->private_data;
374 struct drm_device *dev = file_priv->minor->dev;
379 DRM_DEBUG("open_count = %d\n", dev->open_count);
381 if (dev->driver->preclose)
382 dev->driver->preclose(dev, file_priv);
384 /* ========================================================
385 * Begin inline drm_release
388 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
389 current->pid, (long)old_encode_dev(file_priv->minor->device),
392 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
393 if (drm_i_have_hw_lock(dev, file_priv)) {
394 dev->driver->reclaim_buffers_locked(dev, file_priv);
396 unsigned long _end=jiffies + 3*DRM_HZ;
399 drm_idlelock_take(&dev->lock);
406 spin_lock_bh(&dev->lock.spinlock);
407 locked = dev->lock.idle_has_lock;
408 spin_unlock_bh(&dev->lock.spinlock);
412 } while (!time_after_eq(jiffies, _end));
415 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
416 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
417 "\tI will go on reclaiming the buffers anyway.\n");
420 dev->driver->reclaim_buffers_locked(dev, file_priv);
421 drm_idlelock_release(&dev->lock);
425 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
427 drm_idlelock_take(&dev->lock);
428 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
429 drm_idlelock_release(&dev->lock);
433 if (drm_i_have_hw_lock(dev, file_priv)) {
434 DRM_DEBUG("File %p released, freeing lock for context %d\n",
435 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
437 drm_lock_free(&dev->lock,
438 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
442 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
443 !dev->driver->reclaim_buffers_locked) {
444 dev->driver->reclaim_buffers(dev, file_priv);
447 drm_fasync(-1, filp, 0);
449 mutex_lock(&dev->ctxlist_mutex);
451 if (!list_empty(&dev->ctxlist)) {
452 struct drm_ctx_list *pos, *n;
454 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
455 if (pos->tag == file_priv &&
456 pos->handle != DRM_KERNEL_CONTEXT) {
457 if (dev->driver->context_dtor)
458 dev->driver->context_dtor(dev,
461 drm_ctxbitmap_free(dev, pos->handle);
463 list_del(&pos->head);
464 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
469 mutex_unlock(&dev->ctxlist_mutex);
471 mutex_lock(&dev->struct_mutex);
472 drm_object_release(filp);
473 if (file_priv->remove_auth_on_close == 1) {
474 struct drm_file *temp;
476 list_for_each_entry(temp, &dev->filelist, lhead)
477 temp->authenticated = 0;
479 list_del(&file_priv->lhead);
480 mutex_unlock(&dev->struct_mutex);
482 if (dev->driver->postclose)
483 dev->driver->postclose(dev, file_priv);
484 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
486 /* ========================================================
487 * End inline drm_release
490 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
491 spin_lock(&dev->count_lock);
492 if (!--dev->open_count) {
493 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
494 DRM_ERROR("Device busy: %d %d\n",
495 atomic_read(&dev->ioctl_count), dev->blocked);
496 spin_unlock(&dev->count_lock);
500 spin_unlock(&dev->count_lock);
502 return drm_lastclose(dev);
504 spin_unlock(&dev->count_lock);
510 EXPORT_SYMBOL(drm_release);
513 /* This is to deal with older X servers that believe 0 means data is
514 * available which is not the correct return for a poll function.
515 * This cannot be fixed until the Xserver is fixed. Xserver will need
516 * to set a newer interface version to avoid breaking older Xservers.
517 * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
518 * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
519 * to return the correct response.
521 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
523 /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
526 EXPORT_SYMBOL(drm_poll);