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_t(unsigned, 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 struct drm_device *dev = NULL;
132 int minor_id = iminor(inode);
133 struct drm_minor *minor;
136 minor = idr_find(&drm_minors_idr, minor_id);
140 if (!(dev = minor->dev))
143 retcode = drm_open_helper(inode, filp, dev);
145 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
146 spin_lock(&dev->count_lock);
147 if (!dev->open_count++) {
148 spin_unlock(&dev->count_lock);
149 return drm_setup(dev);
151 spin_unlock(&dev->count_lock);
156 EXPORT_SYMBOL(drm_open);
159 * File \c open operation.
161 * \param inode device inode.
162 * \param filp file pointer.
164 * Puts the dev->fops corresponding to the device minor number into
165 * \p filp, call the \c open method, and restore the file operations.
167 int drm_stub_open(struct inode *inode, struct file *filp)
169 struct drm_device *dev = NULL;
170 struct drm_minor *minor;
171 int minor_id = iminor(inode);
173 const struct file_operations *old_fops;
177 minor = idr_find(&drm_minors_idr, minor_id);
181 if (!(dev = minor->dev))
184 old_fops = filp->f_op;
185 filp->f_op = fops_get(&dev->driver->fops);
186 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
187 fops_put(filp->f_op);
188 filp->f_op = fops_get(old_fops);
196 * Check whether DRI will run on this CPU.
198 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
200 static int drm_cpu_valid(void)
202 #if defined(__i386__)
203 if (boot_cpu_data.x86 == 3)
204 return 0; /* No cmpxchg on a 386 */
206 #if defined(__sparc__) && !defined(__sparc_v9__)
207 return 0; /* No cmpxchg before v9 sparc. */
213 * Called whenever a process opens /dev/drm.
215 * \param inode device inode.
216 * \param filp file pointer.
218 * \return zero on success or a negative number on failure.
220 * Creates and initializes a drm_file structure for the file private data in \p
221 * filp and add it into the double linked list in \p dev.
223 static int drm_open_helper(struct inode *inode, struct file *filp,
224 struct drm_device * dev)
226 int minor_id = iminor(inode);
227 struct drm_file *priv;
230 if (filp->f_flags & O_EXCL)
231 return -EBUSY; /* No exclusive opens */
232 if (!drm_cpu_valid())
235 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
237 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
241 memset(priv, 0, sizeof(*priv));
242 filp->private_data = priv;
244 priv->uid = current->euid;
245 priv->pid = task_pid_nr(current);
246 priv->minor = idr_find(&drm_minors_idr, minor_id);
247 priv->ioctl_count = 0;
248 /* for compatibility root is always authenticated */
249 priv->authenticated = capable(CAP_SYS_ADMIN);
250 priv->lock_count = 0;
252 INIT_LIST_HEAD(&priv->lhead);
254 if (dev->driver->open) {
255 ret = dev->driver->open(dev, priv);
260 mutex_lock(&dev->struct_mutex);
261 if (list_empty(&dev->filelist))
264 list_add(&priv->lhead, &dev->filelist);
265 mutex_unlock(&dev->struct_mutex);
272 struct pci_dev *pci_dev;
273 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
275 dev->hose = pci_dev->sysdata;
276 pci_dev_put(pci_dev);
279 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
281 dev->hose = b->sysdata;
288 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
289 filp->private_data = NULL;
294 int drm_fasync(int fd, struct file *filp, int on)
296 struct drm_file *priv = filp->private_data;
297 struct drm_device *dev = priv->minor->dev;
300 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
301 (long)old_encode_dev(priv->minor->device));
302 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
307 EXPORT_SYMBOL(drm_fasync);
312 * \param inode device inode
313 * \param file_priv DRM file private.
314 * \return zero on success or a negative number on failure.
316 * If the hardware lock is held then free it, and take it again for the kernel
317 * context since it's necessary to reclaim buffers. Unlink the file private
318 * data from its list and free it. Decreases the open count and if it reaches
319 * zero calls drm_lastclose().
321 int drm_release(struct inode *inode, struct file *filp)
323 struct drm_file *file_priv = filp->private_data;
324 struct drm_device *dev = file_priv->minor->dev;
329 DRM_DEBUG("open_count = %d\n", dev->open_count);
331 if (dev->driver->preclose)
332 dev->driver->preclose(dev, file_priv);
334 /* ========================================================
335 * Begin inline drm_release
338 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
339 task_pid_nr(current),
340 (long)old_encode_dev(file_priv->minor->device),
343 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
344 if (drm_i_have_hw_lock(dev, file_priv)) {
345 dev->driver->reclaim_buffers_locked(dev, file_priv);
347 unsigned long endtime = jiffies + 3 * DRM_HZ;
350 drm_idlelock_take(&dev->lock);
357 spin_lock_bh(&dev->lock.spinlock);
358 locked = dev->lock.idle_has_lock;
359 spin_unlock_bh(&dev->lock.spinlock);
363 } while (!time_after_eq(jiffies, endtime));
366 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
367 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
368 "\tI will go on reclaiming the buffers anyway.\n");
371 dev->driver->reclaim_buffers_locked(dev, file_priv);
372 drm_idlelock_release(&dev->lock);
376 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
378 drm_idlelock_take(&dev->lock);
379 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
380 drm_idlelock_release(&dev->lock);
384 if (drm_i_have_hw_lock(dev, file_priv)) {
385 DRM_DEBUG("File %p released, freeing lock for context %d\n",
386 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
388 drm_lock_free(&dev->lock,
389 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
393 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
394 !dev->driver->reclaim_buffers_locked) {
395 dev->driver->reclaim_buffers(dev, file_priv);
398 drm_fasync(-1, filp, 0);
400 mutex_lock(&dev->ctxlist_mutex);
401 if (!list_empty(&dev->ctxlist)) {
402 struct drm_ctx_list *pos, *n;
404 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
405 if (pos->tag == file_priv &&
406 pos->handle != DRM_KERNEL_CONTEXT) {
407 if (dev->driver->context_dtor)
408 dev->driver->context_dtor(dev,
411 drm_ctxbitmap_free(dev, pos->handle);
413 list_del(&pos->head);
414 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
419 mutex_unlock(&dev->ctxlist_mutex);
421 mutex_lock(&dev->struct_mutex);
422 if (file_priv->remove_auth_on_close == 1) {
423 struct drm_file *temp;
425 list_for_each_entry(temp, &dev->filelist, lhead)
426 temp->authenticated = 0;
428 list_del(&file_priv->lhead);
429 mutex_unlock(&dev->struct_mutex);
431 if (dev->driver->postclose)
432 dev->driver->postclose(dev, file_priv);
433 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
435 /* ========================================================
436 * End inline drm_release
439 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
440 spin_lock(&dev->count_lock);
441 if (!--dev->open_count) {
442 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
443 DRM_ERROR("Device busy: %d %d\n",
444 atomic_read(&dev->ioctl_count), dev->blocked);
445 spin_unlock(&dev->count_lock);
449 spin_unlock(&dev->count_lock);
451 return drm_lastclose(dev);
453 spin_unlock(&dev->count_lock);
459 EXPORT_SYMBOL(drm_release);
462 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
466 EXPORT_SYMBOL(drm_poll);