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>
40 #include <linux/smp_lock.h>
42 static int drm_open_helper(struct inode *inode, struct file *filp,
43 struct drm_device * dev);
45 static int drm_setup(struct drm_device * dev)
52 if (dev->driver->firstopen) {
53 ret = dev->driver->firstopen(dev);
58 dev->magicfree.next = NULL;
60 /* prebuild the SAREA */
61 sareapage = max_t(unsigned, SAREA_MAX, PAGE_SIZE);
62 i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
66 atomic_set(&dev->ioctl_count, 0);
67 atomic_set(&dev->vma_count, 0);
69 atomic_set(&dev->buf_alloc, 0);
71 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
72 i = drm_dma_setup(dev);
77 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
78 atomic_set(&dev->counts[i], 0);
80 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
81 INIT_LIST_HEAD(&dev->magicfree);
83 dev->sigdata.lock = NULL;
84 init_waitqueue_head(&dev->lock.lock_queue);
86 dev->queue_reserved = 0;
88 dev->queuelist = NULL;
90 dev->context_flag = 0;
91 dev->interrupt_flag = 0;
93 dev->last_context = 0;
95 dev->last_checked = 0;
96 init_waitqueue_head(&dev->context_wait);
102 dev->buf_async = NULL;
103 init_waitqueue_head(&dev->buf_readers);
104 init_waitqueue_head(&dev->buf_writers);
109 * The kernel's context could be created here, but is now created
110 * in drm_dma_enqueue. This is more resource-efficient for
111 * hardware that does not do DMA, but may mean that
112 * drm_select_queue fails between the time the interrupt is
113 * initialized and the time the queues are initialized.
122 * \param inode device inode
123 * \param filp file pointer.
124 * \return zero on success or a negative number on failure.
126 * Searches the DRM device with the same minor number, calls open_helper(), and
127 * increments the device open count. If the open count was previous at zero,
128 * i.e., it's the first that the device is open, then calls setup().
130 int drm_open(struct inode *inode, struct file *filp)
132 struct drm_device *dev = NULL;
133 int minor_id = iminor(inode);
134 struct drm_minor *minor;
137 minor = idr_find(&drm_minors_idr, minor_id);
141 if (!(dev = 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 return drm_setup(dev);
152 spin_unlock(&dev->count_lock);
157 EXPORT_SYMBOL(drm_open);
160 * File \c open operation.
162 * \param inode device inode.
163 * \param filp file pointer.
165 * Puts the dev->fops corresponding to the device minor number into
166 * \p filp, call the \c open method, and restore the file operations.
168 int drm_stub_open(struct inode *inode, struct file *filp)
170 struct drm_device *dev = NULL;
171 struct drm_minor *minor;
172 int minor_id = iminor(inode);
174 const struct file_operations *old_fops;
178 /* BKL pushdown: note that nothing else serializes idr_find() */
180 minor = idr_find(&drm_minors_idr, minor_id);
184 if (!(dev = minor->dev))
187 old_fops = filp->f_op;
188 filp->f_op = fops_get(&dev->driver->fops);
189 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
190 fops_put(filp->f_op);
191 filp->f_op = fops_get(old_fops);
201 * Check whether DRI will run on this CPU.
203 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
205 static int drm_cpu_valid(void)
207 #if defined(__i386__)
208 if (boot_cpu_data.x86 == 3)
209 return 0; /* No cmpxchg on a 386 */
211 #if defined(__sparc__) && !defined(__sparc_v9__)
212 return 0; /* No cmpxchg before v9 sparc. */
218 * Called whenever a process opens /dev/drm.
220 * \param inode device inode.
221 * \param filp file pointer.
223 * \return zero on success or a negative number on failure.
225 * Creates and initializes a drm_file structure for the file private data in \p
226 * filp and add it into the double linked list in \p dev.
228 static int drm_open_helper(struct inode *inode, struct file *filp,
229 struct drm_device * dev)
231 int minor_id = iminor(inode);
232 struct drm_file *priv;
235 if (filp->f_flags & O_EXCL)
236 return -EBUSY; /* No exclusive opens */
237 if (!drm_cpu_valid())
240 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
242 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
246 memset(priv, 0, sizeof(*priv));
247 filp->private_data = priv;
249 priv->uid = current->euid;
250 priv->pid = task_pid_nr(current);
251 priv->minor = idr_find(&drm_minors_idr, minor_id);
252 priv->ioctl_count = 0;
253 /* for compatibility root is always authenticated */
254 priv->authenticated = capable(CAP_SYS_ADMIN);
255 priv->lock_count = 0;
257 INIT_LIST_HEAD(&priv->lhead);
259 if (dev->driver->open) {
260 ret = dev->driver->open(dev, priv);
265 mutex_lock(&dev->struct_mutex);
266 if (list_empty(&dev->filelist))
269 list_add(&priv->lhead, &dev->filelist);
270 mutex_unlock(&dev->struct_mutex);
277 struct pci_dev *pci_dev;
278 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
280 dev->hose = pci_dev->sysdata;
281 pci_dev_put(pci_dev);
284 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
286 dev->hose = b->sysdata;
293 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
294 filp->private_data = NULL;
299 int drm_fasync(int fd, struct file *filp, int on)
301 struct drm_file *priv = filp->private_data;
302 struct drm_device *dev = priv->minor->dev;
305 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
306 (long)old_encode_dev(priv->minor->device));
307 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
312 EXPORT_SYMBOL(drm_fasync);
317 * \param inode device inode
318 * \param file_priv DRM file private.
319 * \return zero on success or a negative number on failure.
321 * If the hardware lock is held then free it, and take it again for the kernel
322 * context since it's necessary to reclaim buffers. Unlink the file private
323 * data from its list and free it. Decreases the open count and if it reaches
324 * zero calls drm_lastclose().
326 int drm_release(struct inode *inode, struct file *filp)
328 struct drm_file *file_priv = filp->private_data;
329 struct drm_device *dev = file_priv->minor->dev;
334 DRM_DEBUG("open_count = %d\n", dev->open_count);
336 if (dev->driver->preclose)
337 dev->driver->preclose(dev, file_priv);
339 /* ========================================================
340 * Begin inline drm_release
343 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
344 task_pid_nr(current),
345 (long)old_encode_dev(file_priv->minor->device),
348 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
349 if (drm_i_have_hw_lock(dev, file_priv)) {
350 dev->driver->reclaim_buffers_locked(dev, file_priv);
352 unsigned long endtime = jiffies + 3 * DRM_HZ;
355 drm_idlelock_take(&dev->lock);
362 spin_lock_bh(&dev->lock.spinlock);
363 locked = dev->lock.idle_has_lock;
364 spin_unlock_bh(&dev->lock.spinlock);
368 } while (!time_after_eq(jiffies, endtime));
371 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
372 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
373 "\tI will go on reclaiming the buffers anyway.\n");
376 dev->driver->reclaim_buffers_locked(dev, file_priv);
377 drm_idlelock_release(&dev->lock);
381 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
383 drm_idlelock_take(&dev->lock);
384 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
385 drm_idlelock_release(&dev->lock);
389 if (drm_i_have_hw_lock(dev, file_priv)) {
390 DRM_DEBUG("File %p released, freeing lock for context %d\n",
391 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
393 drm_lock_free(&dev->lock,
394 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
398 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
399 !dev->driver->reclaim_buffers_locked) {
400 dev->driver->reclaim_buffers(dev, file_priv);
403 drm_fasync(-1, filp, 0);
405 mutex_lock(&dev->ctxlist_mutex);
406 if (!list_empty(&dev->ctxlist)) {
407 struct drm_ctx_list *pos, *n;
409 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
410 if (pos->tag == file_priv &&
411 pos->handle != DRM_KERNEL_CONTEXT) {
412 if (dev->driver->context_dtor)
413 dev->driver->context_dtor(dev,
416 drm_ctxbitmap_free(dev, pos->handle);
418 list_del(&pos->head);
419 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
424 mutex_unlock(&dev->ctxlist_mutex);
426 mutex_lock(&dev->struct_mutex);
427 if (file_priv->remove_auth_on_close == 1) {
428 struct drm_file *temp;
430 list_for_each_entry(temp, &dev->filelist, lhead)
431 temp->authenticated = 0;
433 list_del(&file_priv->lhead);
434 mutex_unlock(&dev->struct_mutex);
436 if (dev->driver->postclose)
437 dev->driver->postclose(dev, file_priv);
438 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
440 /* ========================================================
441 * End inline drm_release
444 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
445 spin_lock(&dev->count_lock);
446 if (!--dev->open_count) {
447 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
448 DRM_ERROR("Device busy: %d %d\n",
449 atomic_read(&dev->ioctl_count), dev->blocked);
450 spin_unlock(&dev->count_lock);
454 spin_unlock(&dev->count_lock);
456 return drm_lastclose(dev);
458 spin_unlock(&dev->count_lock);
464 EXPORT_SYMBOL(drm_release);
467 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
471 EXPORT_SYMBOL(drm_poll);