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,
44 static int drm_setup(drm_device_t * 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;
89 dev->context_flag = 0;
90 dev->interrupt_flag = 0;
92 dev->last_context = 0;
94 dev->last_checked = 0;
95 init_waitqueue_head(&dev->context_wait);
101 dev->buf_async = NULL;
102 init_waitqueue_head(&dev->buf_readers);
103 init_waitqueue_head(&dev->buf_writers);
108 * The kernel's context could be created here, but is now created
109 * in drm_dma_enqueue. This is more resource-efficient for
110 * hardware that does not do DMA, but may mean that
111 * drm_select_queue fails between the time the interrupt is
112 * initialized and the time the queues are initialized.
121 * \param inode device inode
122 * \param filp file pointer.
123 * \return zero on success or a negative number on failure.
125 * Searches the DRM device with the same minor number, calls open_helper(), and
126 * increments the device open count. If the open count was previous at zero,
127 * i.e., it's the first that the device is open, then calls setup().
129 int drm_open(struct inode *inode, struct file *filp)
131 drm_device_t *dev = NULL;
132 int minor = iminor(inode);
135 if (!((minor >= 0) && (minor < drm_cards_limit)))
138 if (!drm_heads[minor])
141 if (!(dev = drm_heads[minor]->dev))
144 retcode = drm_open_helper(inode, filp, dev);
146 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
147 spin_lock(&dev->count_lock);
148 if (!dev->open_count++) {
149 spin_unlock(&dev->count_lock);
150 retcode = drm_setup(dev);
153 spin_unlock(&dev->count_lock);
157 mutex_lock(&dev->struct_mutex);
158 BUG_ON((dev->dev_mapping != NULL) &&
159 (dev->dev_mapping != inode->i_mapping));
160 if (dev->dev_mapping == NULL)
161 dev->dev_mapping = inode->i_mapping;
162 mutex_unlock(&dev->struct_mutex);
166 EXPORT_SYMBOL(drm_open);
169 * File \c open operation.
171 * \param inode device inode.
172 * \param filp file pointer.
174 * Puts the dev->fops corresponding to the device minor number into
175 * \p filp, call the \c open method, and restore the file operations.
177 int drm_stub_open(struct inode *inode, struct file *filp)
179 drm_device_t *dev = NULL;
180 int minor = iminor(inode);
182 const struct file_operations *old_fops;
186 if (!((minor >= 0) && (minor < drm_cards_limit)))
189 if (!drm_heads[minor])
192 if (!(dev = drm_heads[minor]->dev))
195 old_fops = filp->f_op;
196 filp->f_op = fops_get(&dev->driver->fops);
197 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
198 fops_put(filp->f_op);
199 filp->f_op = fops_get(old_fops);
207 * Check whether DRI will run on this CPU.
209 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
211 static int drm_cpu_valid(void)
213 #if defined(__i386__)
214 if (boot_cpu_data.x86 == 3)
215 return 0; /* No cmpxchg on a 386 */
217 #if defined(__sparc__) && !defined(__sparc_v9__)
218 return 0; /* No cmpxchg before v9 sparc. */
224 * Called whenever a process opens /dev/drm.
226 * \param inode device inode.
227 * \param filp file pointer.
229 * \return zero on success or a negative number on failure.
231 * Creates and initializes a drm_file structure for the file private data in \p
232 * filp and add it into the double linked list in \p dev.
234 static int drm_open_helper(struct inode *inode, struct file *filp,
237 int minor = iminor(inode);
242 if (filp->f_flags & O_EXCL)
243 return -EBUSY; /* No exclusive opens */
244 if (!drm_cpu_valid())
247 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
249 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
253 memset(priv, 0, sizeof(*priv));
254 filp->private_data = priv;
255 priv->uid = current->euid;
256 priv->pid = current->pid;
258 priv->head = drm_heads[minor];
259 priv->ioctl_count = 0;
260 /* for compatibility root is always authenticated */
261 priv->authenticated = capable(CAP_SYS_ADMIN);
262 priv->lock_count = 0;
264 INIT_LIST_HEAD(&priv->lhead);
265 INIT_LIST_HEAD(&priv->user_objects);
266 INIT_LIST_HEAD(&priv->refd_objects);
268 for (i=0; i<_DRM_NO_REF_TYPES; ++i) {
269 ret = drm_ht_create(&priv->refd_object_hash[i], DRM_FILE_HASH_ORDER);
276 drm_ht_remove(&priv->refd_object_hash[j]);
281 if (dev->driver->open) {
282 ret = dev->driver->open(dev, priv);
287 mutex_lock(&dev->struct_mutex);
288 if (list_empty(&dev->filelist))
291 list_add(&priv->lhead, &dev->filelist);
292 mutex_unlock(&dev->struct_mutex);
299 struct pci_dev *pci_dev;
300 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
302 dev->hose = pci_dev->sysdata;
303 pci_dev_put(pci_dev);
306 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
308 dev->hose = b->sysdata;
315 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
316 filp->private_data = NULL;
321 int drm_fasync(int fd, struct file *filp, int on)
323 drm_file_t *priv = filp->private_data;
324 drm_device_t *dev = priv->head->dev;
327 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
328 (long)old_encode_dev(priv->head->device));
329 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
334 EXPORT_SYMBOL(drm_fasync);
336 static void drm_object_release(struct file *filp) {
338 drm_file_t *priv = filp->private_data;
339 struct list_head *head;
340 drm_user_object_t *user_object;
341 drm_ref_object_t *ref_object;
345 * Free leftover ref objects created by me. Note that we cannot use
346 * list_for_each() here, as the struct_mutex may be temporarily released
347 * by the remove_() functions, and thus the lists may be altered.
348 * Also, a drm_remove_ref_object() will not remove it
349 * from the list unless its refcount is 1.
352 head = &priv->refd_objects;
353 while (head->next != head) {
354 ref_object = list_entry(head->next, drm_ref_object_t, list);
355 drm_remove_ref_object(priv, ref_object);
356 head = &priv->refd_objects;
360 * Free leftover user objects created by me.
363 head = &priv->user_objects;
364 while (head->next != head) {
365 user_object = list_entry(head->next, drm_user_object_t, list);
366 drm_remove_user_object(priv, user_object);
367 head = &priv->user_objects;
370 for(i=0; i<_DRM_NO_REF_TYPES; ++i) {
371 drm_ht_remove(&priv->refd_object_hash[i]);
378 * \param inode device inode
379 * \param filp file pointer.
380 * \return zero on success or a negative number on failure.
382 * If the hardware lock is held then free it, and take it again for the kernel
383 * context since it's necessary to reclaim buffers. Unlink the file private
384 * data from its list and free it. Decreases the open count and if it reaches
385 * zero calls drm_lastclose().
387 int drm_release(struct inode *inode, struct file *filp)
389 drm_file_t *priv = filp->private_data;
394 dev = priv->head->dev;
396 DRM_DEBUG("open_count = %d\n", dev->open_count);
398 if (dev->driver->preclose)
399 dev->driver->preclose(dev, filp);
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(priv->head->device),
409 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
410 if (drm_i_have_hw_lock(filp)) {
411 dev->driver->reclaim_buffers_locked(dev, filp);
413 unsigned long _end=jiffies + 3*DRM_HZ;
416 drm_idlelock_take(&dev->lock);
423 spin_lock(&dev->lock.spinlock);
424 locked = dev->lock.idle_has_lock;
425 spin_unlock(&dev->lock.spinlock);
429 } while (!time_after_eq(jiffies, _end));
432 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
433 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
434 "\tI will go on reclaiming the buffers anyway.\n");
437 dev->driver->reclaim_buffers_locked(dev, filp);
438 drm_idlelock_release(&dev->lock);
442 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
444 drm_idlelock_take(&dev->lock);
445 dev->driver->reclaim_buffers_idlelocked(dev, filp);
446 drm_idlelock_release(&dev->lock);
450 if (drm_i_have_hw_lock(filp)) {
451 DRM_DEBUG("File %p released, freeing lock for context %d\n",
452 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
454 drm_lock_free(&dev->lock,
455 _DRM_LOCKING_CONTEXT(dev->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, filp);
464 drm_fasync(-1, filp, 0);
466 mutex_lock(&dev->ctxlist_mutex);
468 if (!list_empty(&dev->ctxlist)) {
469 drm_ctx_list_t *pos, *n;
471 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
472 if (pos->tag == priv &&
473 pos->handle != DRM_KERNEL_CONTEXT) {
474 if (dev->driver->context_dtor)
475 dev->driver->context_dtor(dev,
478 drm_ctxbitmap_free(dev, pos->handle);
480 list_del(&pos->head);
481 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
486 mutex_unlock(&dev->ctxlist_mutex);
488 mutex_lock(&dev->struct_mutex);
489 drm_object_release(filp);
490 if (priv->remove_auth_on_close == 1) {
493 list_for_each_entry(temp, &dev->filelist, lhead)
494 temp->authenticated = 0;
496 list_del(&priv->lhead);
497 mutex_unlock(&dev->struct_mutex);
499 if (dev->driver->postclose)
500 dev->driver->postclose(dev, priv);
501 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
503 /* ========================================================
504 * End inline drm_release
507 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
508 spin_lock(&dev->count_lock);
509 if (!--dev->open_count) {
510 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
511 DRM_ERROR("Device busy: %d %d\n",
512 atomic_read(&dev->ioctl_count), dev->blocked);
513 spin_unlock(&dev->count_lock);
517 spin_unlock(&dev->count_lock);
519 return drm_lastclose(dev);
521 spin_unlock(&dev->count_lock);
527 EXPORT_SYMBOL(drm_release);
530 /* This is to deal with older X servers that believe 0 means data is
531 * available which is not the correct return for a poll function.
532 * This cannot be fixed until the Xserver is fixed. Xserver will need
533 * to set a newer interface version to avoid breaking older Xservers.
534 * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
535 * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
536 * to return the correct response.
538 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
540 /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
543 EXPORT_SYMBOL(drm_poll);