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 <linux/poll.h>
39 #include <linux/slab.h>
40 #include <linux/module.h>
42 /* from BKL pushdown: note that nothing else serializes idr_find() */
43 DEFINE_MUTEX(drm_global_mutex);
44 EXPORT_SYMBOL(drm_global_mutex);
46 static int drm_open_helper(struct inode *inode, struct file *filp,
47 struct drm_device * dev);
49 static int drm_setup(struct drm_device * dev)
54 if (dev->driver->firstopen) {
55 ret = dev->driver->firstopen(dev);
60 atomic_set(&dev->ioctl_count, 0);
61 atomic_set(&dev->vma_count, 0);
63 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
64 !drm_core_check_feature(dev, DRIVER_MODESET)) {
66 atomic_set(&dev->buf_alloc, 0);
68 i = drm_dma_setup(dev);
73 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
74 atomic_set(&dev->counts[i], 0);
76 dev->sigdata.lock = NULL;
79 dev->queue_reserved = 0;
81 dev->queuelist = NULL;
82 dev->context_flag = 0;
83 dev->interrupt_flag = 0;
85 dev->last_context = 0;
87 dev->last_checked = 0;
88 init_waitqueue_head(&dev->context_wait);
94 dev->buf_async = NULL;
95 init_waitqueue_head(&dev->buf_readers);
96 init_waitqueue_head(&dev->buf_writers);
101 * The kernel's context could be created here, but is now created
102 * in drm_dma_enqueue. This is more resource-efficient for
103 * hardware that does not do DMA, but may mean that
104 * drm_select_queue fails between the time the interrupt is
105 * initialized and the time the queues are initialized.
114 * \param inode device inode
115 * \param filp file pointer.
116 * \return zero on success or a negative number on failure.
118 * Searches the DRM device with the same minor number, calls open_helper(), and
119 * increments the device open count. If the open count was previous at zero,
120 * i.e., it's the first that the device is open, then calls setup().
122 int drm_open(struct inode *inode, struct file *filp)
124 struct drm_device *dev = NULL;
125 int minor_id = iminor(inode);
126 struct drm_minor *minor;
129 minor = idr_find(&drm_minors_idr, minor_id);
133 if (!(dev = minor->dev))
136 retcode = drm_open_helper(inode, filp, dev);
138 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
139 if (!dev->open_count++) {
140 retcode = drm_setup(dev);
146 mutex_lock(&dev->struct_mutex);
147 if (minor->type == DRM_MINOR_LEGACY) {
148 if (dev->dev_mapping == NULL)
149 dev->dev_mapping = inode->i_mapping;
150 else if (dev->dev_mapping != inode->i_mapping)
153 mutex_unlock(&dev->struct_mutex);
158 EXPORT_SYMBOL(drm_open);
161 * File \c open operation.
163 * \param inode device inode.
164 * \param filp file pointer.
166 * Puts the dev->fops corresponding to the device minor number into
167 * \p filp, call the \c open method, and restore the file operations.
169 int drm_stub_open(struct inode *inode, struct file *filp)
171 struct drm_device *dev = NULL;
172 struct drm_minor *minor;
173 int minor_id = iminor(inode);
175 const struct file_operations *old_fops;
179 mutex_lock(&drm_global_mutex);
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 == NULL) {
190 filp->f_op = old_fops;
193 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
194 fops_put(filp->f_op);
195 filp->f_op = fops_get(old_fops);
200 mutex_unlock(&drm_global_mutex);
205 * Check whether DRI will run on this CPU.
207 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
209 static int drm_cpu_valid(void)
211 #if defined(__i386__)
212 if (boot_cpu_data.x86 == 3)
213 return 0; /* No cmpxchg on a 386 */
215 #if defined(__sparc__) && !defined(__sparc_v9__)
216 return 0; /* No cmpxchg before v9 sparc. */
222 * drm_new_set_master - Allocate a new master object and become master for the
223 * associated master realm.
225 * @dev: The associated device.
226 * @fpriv: File private identifying the client.
228 * This function must be called with dev::struct_mutex held.
229 * Returns negative error code on failure. Zero on success.
231 int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
233 struct drm_master *old_master;
236 lockdep_assert_held_once(&dev->struct_mutex);
238 /* create a new master */
239 fpriv->minor->master = drm_master_create(fpriv->minor);
240 if (!fpriv->minor->master)
243 /* take another reference for the copy in the local file priv */
244 old_master = fpriv->master;
245 fpriv->master = drm_master_get(fpriv->minor->master);
247 if (dev->driver->master_create) {
248 mutex_unlock(&dev->struct_mutex);
249 ret = dev->driver->master_create(dev, fpriv->master);
250 mutex_lock(&dev->struct_mutex);
254 if (dev->driver->master_set) {
255 ret = dev->driver->master_set(dev, fpriv, true);
260 fpriv->is_master = 1;
261 fpriv->allowed_master = 1;
262 fpriv->authenticated = 1;
264 drm_master_put(&old_master);
269 /* drop both references and restore old master on failure */
270 drm_master_put(&fpriv->minor->master);
271 drm_master_put(&fpriv->master);
272 fpriv->master = old_master;
278 * Called whenever a process opens /dev/drm.
280 * \param inode device inode.
281 * \param filp file pointer.
283 * \return zero on success or a negative number on failure.
285 * Creates and initializes a drm_file structure for the file private data in \p
286 * filp and add it into the double linked list in \p dev.
288 static int drm_open_helper(struct inode *inode, struct file *filp,
289 struct drm_device * dev)
291 int minor_id = iminor(inode);
292 struct drm_file *priv;
295 if (filp->f_flags & O_EXCL)
296 return -EBUSY; /* No exclusive opens */
297 if (!drm_cpu_valid())
299 if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
302 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
304 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
308 filp->private_data = priv;
310 priv->uid = current_euid();
311 priv->pid = task_pid_nr(current);
312 priv->minor = idr_find(&drm_minors_idr, minor_id);
313 priv->ioctl_count = 0;
314 /* for compatibility root is always authenticated */
315 priv->authenticated = capable(CAP_SYS_ADMIN);
316 priv->lock_count = 0;
318 INIT_LIST_HEAD(&priv->lhead);
319 INIT_LIST_HEAD(&priv->fbs);
320 INIT_LIST_HEAD(&priv->event_list);
321 init_waitqueue_head(&priv->event_wait);
322 priv->event_space = 4096; /* set aside 4k for event buffer */
324 if (dev->driver->driver_features & DRIVER_GEM)
325 drm_gem_open(dev, priv);
327 if (dev->driver->open) {
328 ret = dev->driver->open(dev, priv);
334 /* if there is no current master make this fd it */
335 mutex_lock(&dev->struct_mutex);
336 if (!priv->minor->master) {
337 /* create a new master */
338 ret = drm_new_set_master(dev, priv);
339 mutex_unlock(&dev->struct_mutex);
343 /* get a reference to the master */
344 priv->master = drm_master_get(priv->minor->master);
345 mutex_unlock(&dev->struct_mutex);
348 mutex_lock(&dev->struct_mutex);
349 list_add(&priv->lhead, &dev->filelist);
350 mutex_unlock(&dev->struct_mutex);
357 struct pci_dev *pci_dev;
358 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
360 dev->hose = pci_dev->sysdata;
361 pci_dev_put(pci_dev);
364 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
366 dev->hose = b->sysdata;
374 filp->private_data = NULL;
379 int drm_fasync(int fd, struct file *filp, int on)
381 struct drm_file *priv = filp->private_data;
382 struct drm_device *dev = priv->minor->dev;
384 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
385 (long)old_encode_dev(priv->minor->device));
386 return fasync_helper(fd, filp, on, &dev->buf_async);
388 EXPORT_SYMBOL(drm_fasync);
391 * Reclaim locked buffers; note that this may be a bad idea if the current
392 * context doesn't have the hw lock...
394 static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
396 struct drm_file *file_priv = f->private_data;
398 if (drm_i_have_hw_lock(dev, file_priv)) {
399 dev->driver->reclaim_buffers_locked(dev, file_priv);
401 unsigned long _end = jiffies + 3 * DRM_HZ;
404 drm_idlelock_take(&file_priv->master->lock);
410 spin_lock_bh(&file_priv->master->lock.spinlock);
411 locked = file_priv->master->lock.idle_has_lock;
412 spin_unlock_bh(&file_priv->master->lock.spinlock);
416 } while (!time_after_eq(jiffies, _end));
419 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
420 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
421 "\tI will go on reclaiming the buffers anyway.\n");
424 dev->driver->reclaim_buffers_locked(dev, file_priv);
425 drm_idlelock_release(&file_priv->master->lock);
429 static void drm_master_release(struct drm_device *dev, struct file *filp)
431 struct drm_file *file_priv = filp->private_data;
433 if (dev->driver->reclaim_buffers_locked &&
434 file_priv->master->lock.hw_lock)
435 drm_reclaim_locked_buffers(dev, filp);
437 if (dev->driver->reclaim_buffers_idlelocked &&
438 file_priv->master->lock.hw_lock) {
439 drm_idlelock_take(&file_priv->master->lock);
440 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
441 drm_idlelock_release(&file_priv->master->lock);
445 if (drm_i_have_hw_lock(dev, file_priv)) {
446 DRM_DEBUG("File %p released, freeing lock for context %d\n",
447 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
448 drm_lock_free(&file_priv->master->lock,
449 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
452 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
453 !dev->driver->reclaim_buffers_locked) {
454 dev->driver->reclaim_buffers(dev, file_priv);
458 static void drm_events_release(struct drm_file *file_priv)
460 struct drm_device *dev = file_priv->minor->dev;
461 struct drm_pending_event *e, *et;
462 struct drm_pending_vblank_event *v, *vt;
465 spin_lock_irqsave(&dev->event_lock, flags);
467 /* Remove pending flips */
468 list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
469 if (v->base.file_priv == file_priv) {
470 list_del(&v->base.link);
471 drm_vblank_put(dev, v->pipe);
472 v->base.destroy(&v->base);
475 /* Remove unconsumed events */
476 list_for_each_entry_safe(e, et, &file_priv->event_list, link)
479 spin_unlock_irqrestore(&dev->event_lock, flags);
485 * \param inode device inode
486 * \param file_priv DRM file private.
487 * \return zero on success or a negative number on failure.
489 * If the hardware lock is held then free it, and take it again for the kernel
490 * context since it's necessary to reclaim buffers. Unlink the file private
491 * data from its list and free it. Decreases the open count and if it reaches
492 * zero calls drm_lastclose().
494 int drm_release(struct inode *inode, struct file *filp)
496 struct drm_file *file_priv = filp->private_data;
497 struct drm_device *dev = file_priv->minor->dev;
500 mutex_lock(&drm_global_mutex);
502 DRM_DEBUG("open_count = %d\n", dev->open_count);
504 if (dev->driver->preclose)
505 dev->driver->preclose(dev, file_priv);
507 /* ========================================================
508 * Begin inline drm_release
511 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
512 task_pid_nr(current),
513 (long)old_encode_dev(file_priv->minor->device),
516 /* Release any auth tokens that might point to this file_priv,
517 (do that under the drm_global_mutex) */
518 if (file_priv->magic)
519 (void) drm_remove_magic(file_priv->master, file_priv->magic);
521 /* if the master has gone away we can't do anything with the lock */
522 if (file_priv->minor->master)
523 drm_master_release(dev, filp);
525 drm_events_release(file_priv);
527 if (dev->driver->driver_features & DRIVER_GEM)
528 drm_gem_release(dev, file_priv);
530 if (dev->driver->driver_features & DRIVER_MODESET)
531 drm_fb_release(file_priv);
533 mutex_lock(&dev->ctxlist_mutex);
534 if (!list_empty(&dev->ctxlist)) {
535 struct drm_ctx_list *pos, *n;
537 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
538 if (pos->tag == file_priv &&
539 pos->handle != DRM_KERNEL_CONTEXT) {
540 if (dev->driver->context_dtor)
541 dev->driver->context_dtor(dev,
544 drm_ctxbitmap_free(dev, pos->handle);
546 list_del(&pos->head);
552 mutex_unlock(&dev->ctxlist_mutex);
554 mutex_lock(&dev->struct_mutex);
556 if (file_priv->is_master) {
557 struct drm_master *master = file_priv->master;
558 struct drm_file *temp;
559 list_for_each_entry(temp, &dev->filelist, lhead) {
560 if ((temp->master == file_priv->master) &&
562 temp->authenticated = 0;
566 * Since the master is disappearing, so is the
567 * possibility to lock.
570 if (master->lock.hw_lock) {
571 if (dev->sigdata.lock == master->lock.hw_lock)
572 dev->sigdata.lock = NULL;
573 master->lock.hw_lock = NULL;
574 master->lock.file_priv = NULL;
575 wake_up_interruptible_all(&master->lock.lock_queue);
578 if (file_priv->minor->master == file_priv->master) {
579 /* drop the reference held my the minor */
580 if (dev->driver->master_drop)
581 dev->driver->master_drop(dev, file_priv, true);
582 drm_master_put(&file_priv->minor->master);
586 /* drop the reference held my the file priv */
587 drm_master_put(&file_priv->master);
588 file_priv->is_master = 0;
589 list_del(&file_priv->lhead);
590 mutex_unlock(&dev->struct_mutex);
592 if (dev->driver->postclose)
593 dev->driver->postclose(dev, file_priv);
596 /* ========================================================
597 * End inline drm_release
600 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
601 if (!--dev->open_count) {
602 if (atomic_read(&dev->ioctl_count)) {
603 DRM_ERROR("Device busy: %d\n",
604 atomic_read(&dev->ioctl_count));
607 retcode = drm_lastclose(dev);
609 mutex_unlock(&drm_global_mutex);
613 EXPORT_SYMBOL(drm_release);
616 drm_dequeue_event(struct drm_file *file_priv,
617 size_t total, size_t max, struct drm_pending_event **out)
619 struct drm_device *dev = file_priv->minor->dev;
620 struct drm_pending_event *e;
624 spin_lock_irqsave(&dev->event_lock, flags);
627 if (list_empty(&file_priv->event_list))
629 e = list_first_entry(&file_priv->event_list,
630 struct drm_pending_event, link);
631 if (e->event->length + total > max)
634 file_priv->event_space += e->event->length;
640 spin_unlock_irqrestore(&dev->event_lock, flags);
644 ssize_t drm_read(struct file *filp, char __user *buffer,
645 size_t count, loff_t *offset)
647 struct drm_file *file_priv = filp->private_data;
648 struct drm_pending_event *e;
652 ret = wait_event_interruptible(file_priv->event_wait,
653 !list_empty(&file_priv->event_list));
658 while (drm_dequeue_event(file_priv, total, count, &e)) {
659 if (copy_to_user(buffer + total,
660 e->event, e->event->length)) {
665 total += e->event->length;
671 EXPORT_SYMBOL(drm_read);
673 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
675 struct drm_file *file_priv = filp->private_data;
676 unsigned int mask = 0;
678 poll_wait(filp, &file_priv->event_wait, wait);
680 if (!list_empty(&file_priv->event_list))
681 mask |= POLLIN | POLLRDNORM;
685 EXPORT_SYMBOL(drm_poll);