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;
78 dev->context_flag = 0;
79 dev->last_context = 0;
82 dev->buf_async = NULL;
87 * The kernel's context could be created here, but is now created
88 * in drm_dma_enqueue. This is more resource-efficient for
89 * hardware that does not do DMA, but may mean that
90 * drm_select_queue fails between the time the interrupt is
91 * initialized and the time the queues are initialized.
100 * \param inode device inode
101 * \param filp file pointer.
102 * \return zero on success or a negative number on failure.
104 * Searches the DRM device with the same minor number, calls open_helper(), and
105 * increments the device open count. If the open count was previous at zero,
106 * i.e., it's the first that the device is open, then calls setup().
108 int drm_open(struct inode *inode, struct file *filp)
110 struct drm_device *dev = NULL;
111 int minor_id = iminor(inode);
112 struct drm_minor *minor;
115 struct address_space *old_mapping;
116 struct address_space *old_imapping;
118 minor = idr_find(&drm_minors_idr, minor_id);
122 if (!(dev = minor->dev))
125 if (drm_device_is_unplugged(dev))
128 if (!dev->open_count++)
130 mutex_lock(&dev->struct_mutex);
131 old_imapping = inode->i_mapping;
132 old_mapping = dev->dev_mapping;
133 if (old_mapping == NULL)
134 dev->dev_mapping = &inode->i_data;
135 /* ihold ensures nobody can remove inode with our i_data */
136 ihold(container_of(dev->dev_mapping, struct inode, i_data));
137 inode->i_mapping = dev->dev_mapping;
138 filp->f_mapping = dev->dev_mapping;
139 mutex_unlock(&dev->struct_mutex);
141 retcode = drm_open_helper(inode, filp, dev);
144 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
146 retcode = drm_setup(dev);
153 mutex_lock(&dev->struct_mutex);
154 filp->f_mapping = old_imapping;
155 inode->i_mapping = old_imapping;
156 iput(container_of(dev->dev_mapping, struct inode, i_data));
157 dev->dev_mapping = old_mapping;
158 mutex_unlock(&dev->struct_mutex);
162 EXPORT_SYMBOL(drm_open);
165 * File \c open operation.
167 * \param inode device inode.
168 * \param filp file pointer.
170 * Puts the dev->fops corresponding to the device minor number into
171 * \p filp, call the \c open method, and restore the file operations.
173 int drm_stub_open(struct inode *inode, struct file *filp)
175 struct drm_device *dev = NULL;
176 struct drm_minor *minor;
177 int minor_id = iminor(inode);
179 const struct file_operations *old_fops;
183 mutex_lock(&drm_global_mutex);
184 minor = idr_find(&drm_minors_idr, minor_id);
188 if (!(dev = minor->dev))
191 if (drm_device_is_unplugged(dev))
194 old_fops = filp->f_op;
195 filp->f_op = fops_get(dev->driver->fops);
196 if (filp->f_op == NULL) {
197 filp->f_op = old_fops;
200 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
201 fops_put(filp->f_op);
202 filp->f_op = fops_get(old_fops);
207 mutex_unlock(&drm_global_mutex);
212 * Check whether DRI will run on this CPU.
214 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
216 static int drm_cpu_valid(void)
218 #if defined(__i386__)
219 if (boot_cpu_data.x86 == 3)
220 return 0; /* No cmpxchg on a 386 */
222 #if defined(__sparc__) && !defined(__sparc_v9__)
223 return 0; /* No cmpxchg before v9 sparc. */
229 * Called whenever a process opens /dev/drm.
231 * \param inode device inode.
232 * \param filp file pointer.
234 * \return zero on success or a negative number on failure.
236 * Creates and initializes a drm_file structure for the file private data in \p
237 * filp and add it into the double linked list in \p dev.
239 static int drm_open_helper(struct inode *inode, struct file *filp,
240 struct drm_device * dev)
242 int minor_id = iminor(inode);
243 struct drm_file *priv;
246 if (filp->f_flags & O_EXCL)
247 return -EBUSY; /* No exclusive opens */
248 if (!drm_cpu_valid())
250 if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
253 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
255 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
259 filp->private_data = priv;
261 priv->uid = current_euid();
262 priv->pid = get_pid(task_pid(current));
263 priv->minor = idr_find(&drm_minors_idr, minor_id);
269 priv->ioctl_count = 0;
270 /* for compatibility root is always authenticated */
271 priv->authenticated = capable(CAP_SYS_ADMIN);
272 priv->lock_count = 0;
274 INIT_LIST_HEAD(&priv->lhead);
275 INIT_LIST_HEAD(&priv->fbs);
276 mutex_init(&priv->fbs_lock);
277 INIT_LIST_HEAD(&priv->event_list);
278 init_waitqueue_head(&priv->event_wait);
279 priv->event_space = 4096; /* set aside 4k for event buffer */
281 if (dev->driver->driver_features & DRIVER_GEM)
282 drm_gem_open(dev, priv);
284 if (drm_core_check_feature(dev, DRIVER_PRIME))
285 drm_prime_init_file_private(&priv->prime);
287 if (dev->driver->open) {
288 ret = dev->driver->open(dev, priv);
290 goto out_prime_destroy;
294 /* if there is no current master make this fd it */
295 mutex_lock(&dev->struct_mutex);
296 if (!priv->minor->master) {
297 /* create a new master */
298 priv->minor->master = drm_master_create(priv->minor);
299 if (!priv->minor->master) {
300 mutex_unlock(&dev->struct_mutex);
306 /* take another reference for the copy in the local file priv */
307 priv->master = drm_master_get(priv->minor->master);
309 priv->authenticated = 1;
311 mutex_unlock(&dev->struct_mutex);
312 if (dev->driver->master_create) {
313 ret = dev->driver->master_create(dev, priv->master);
315 mutex_lock(&dev->struct_mutex);
316 /* drop both references if this fails */
317 drm_master_put(&priv->minor->master);
318 drm_master_put(&priv->master);
319 mutex_unlock(&dev->struct_mutex);
323 mutex_lock(&dev->struct_mutex);
324 if (dev->driver->master_set) {
325 ret = dev->driver->master_set(dev, priv, true);
327 /* drop both references if this fails */
328 drm_master_put(&priv->minor->master);
329 drm_master_put(&priv->master);
330 mutex_unlock(&dev->struct_mutex);
334 mutex_unlock(&dev->struct_mutex);
336 /* get a reference to the master */
337 priv->master = drm_master_get(priv->minor->master);
338 mutex_unlock(&dev->struct_mutex);
341 mutex_lock(&dev->struct_mutex);
342 list_add(&priv->lhead, &dev->filelist);
343 mutex_unlock(&dev->struct_mutex);
350 struct pci_dev *pci_dev;
351 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
353 dev->hose = pci_dev->sysdata;
354 pci_dev_put(pci_dev);
357 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
359 dev->hose = b->sysdata;
367 if (dev->driver->postclose)
368 dev->driver->postclose(dev, priv);
370 if (drm_core_check_feature(dev, DRIVER_PRIME))
371 drm_prime_destroy_file_private(&priv->prime);
372 if (dev->driver->driver_features & DRIVER_GEM)
373 drm_gem_release(dev, priv);
377 filp->private_data = NULL;
382 int drm_fasync(int fd, struct file *filp, int on)
384 struct drm_file *priv = filp->private_data;
385 struct drm_device *dev = priv->minor->dev;
387 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
388 (long)old_encode_dev(priv->minor->device));
389 return fasync_helper(fd, filp, on, &dev->buf_async);
391 EXPORT_SYMBOL(drm_fasync);
393 static void drm_master_release(struct drm_device *dev, struct file *filp)
395 struct drm_file *file_priv = filp->private_data;
397 if (drm_i_have_hw_lock(dev, file_priv)) {
398 DRM_DEBUG("File %p released, freeing lock for context %d\n",
399 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
400 drm_lock_free(&file_priv->master->lock,
401 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
405 static void drm_events_release(struct drm_file *file_priv)
407 struct drm_device *dev = file_priv->minor->dev;
408 struct drm_pending_event *e, *et;
409 struct drm_pending_vblank_event *v, *vt;
412 spin_lock_irqsave(&dev->event_lock, flags);
414 /* Remove pending flips */
415 list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
416 if (v->base.file_priv == file_priv) {
417 list_del(&v->base.link);
418 drm_vblank_put(dev, v->pipe);
419 v->base.destroy(&v->base);
422 /* Remove unconsumed events */
423 list_for_each_entry_safe(e, et, &file_priv->event_list, link)
426 spin_unlock_irqrestore(&dev->event_lock, flags);
432 * \param inode device inode
433 * \param file_priv DRM file private.
434 * \return zero on success or a negative number on failure.
436 * If the hardware lock is held then free it, and take it again for the kernel
437 * context since it's necessary to reclaim buffers. Unlink the file private
438 * data from its list and free it. Decreases the open count and if it reaches
439 * zero calls drm_lastclose().
441 int drm_release(struct inode *inode, struct file *filp)
443 struct drm_file *file_priv = filp->private_data;
444 struct drm_device *dev = file_priv->minor->dev;
447 mutex_lock(&drm_global_mutex);
449 DRM_DEBUG("open_count = %d\n", dev->open_count);
451 if (dev->driver->preclose)
452 dev->driver->preclose(dev, file_priv);
454 /* ========================================================
455 * Begin inline drm_release
458 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
459 task_pid_nr(current),
460 (long)old_encode_dev(file_priv->minor->device),
463 /* Release any auth tokens that might point to this file_priv,
464 (do that under the drm_global_mutex) */
465 if (file_priv->magic)
466 (void) drm_remove_magic(file_priv->master, file_priv->magic);
468 /* if the master has gone away we can't do anything with the lock */
469 if (file_priv->minor->master)
470 drm_master_release(dev, filp);
472 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
473 drm_core_reclaim_buffers(dev, file_priv);
475 drm_events_release(file_priv);
477 if (dev->driver->driver_features & DRIVER_MODESET)
478 drm_fb_release(file_priv);
480 if (dev->driver->driver_features & DRIVER_GEM)
481 drm_gem_release(dev, file_priv);
483 mutex_lock(&dev->ctxlist_mutex);
484 if (!list_empty(&dev->ctxlist)) {
485 struct drm_ctx_list *pos, *n;
487 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
488 if (pos->tag == file_priv &&
489 pos->handle != DRM_KERNEL_CONTEXT) {
490 if (dev->driver->context_dtor)
491 dev->driver->context_dtor(dev,
494 drm_ctxbitmap_free(dev, pos->handle);
496 list_del(&pos->head);
502 mutex_unlock(&dev->ctxlist_mutex);
504 mutex_lock(&dev->struct_mutex);
506 if (file_priv->is_master) {
507 struct drm_master *master = file_priv->master;
508 struct drm_file *temp;
509 list_for_each_entry(temp, &dev->filelist, lhead) {
510 if ((temp->master == file_priv->master) &&
512 temp->authenticated = 0;
516 * Since the master is disappearing, so is the
517 * possibility to lock.
520 if (master->lock.hw_lock) {
521 if (dev->sigdata.lock == master->lock.hw_lock)
522 dev->sigdata.lock = NULL;
523 master->lock.hw_lock = NULL;
524 master->lock.file_priv = NULL;
525 wake_up_interruptible_all(&master->lock.lock_queue);
528 if (file_priv->minor->master == file_priv->master) {
529 /* drop the reference held my the minor */
530 if (dev->driver->master_drop)
531 dev->driver->master_drop(dev, file_priv, true);
532 drm_master_put(&file_priv->minor->master);
536 BUG_ON(dev->dev_mapping == NULL);
537 iput(container_of(dev->dev_mapping, struct inode, i_data));
539 /* drop the reference held my the file priv */
540 drm_master_put(&file_priv->master);
541 file_priv->is_master = 0;
542 list_del(&file_priv->lhead);
543 mutex_unlock(&dev->struct_mutex);
545 if (dev->driver->postclose)
546 dev->driver->postclose(dev, file_priv);
548 if (drm_core_check_feature(dev, DRIVER_PRIME))
549 drm_prime_destroy_file_private(&file_priv->prime);
551 put_pid(file_priv->pid);
554 /* ========================================================
555 * End inline drm_release
558 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
559 if (!--dev->open_count) {
560 if (atomic_read(&dev->ioctl_count)) {
561 DRM_ERROR("Device busy: %d\n",
562 atomic_read(&dev->ioctl_count));
565 retcode = drm_lastclose(dev);
566 if (drm_device_is_unplugged(dev))
569 mutex_unlock(&drm_global_mutex);
573 EXPORT_SYMBOL(drm_release);
576 drm_dequeue_event(struct drm_file *file_priv,
577 size_t total, size_t max, struct drm_pending_event **out)
579 struct drm_device *dev = file_priv->minor->dev;
580 struct drm_pending_event *e;
584 spin_lock_irqsave(&dev->event_lock, flags);
587 if (list_empty(&file_priv->event_list))
589 e = list_first_entry(&file_priv->event_list,
590 struct drm_pending_event, link);
591 if (e->event->length + total > max)
594 file_priv->event_space += e->event->length;
600 spin_unlock_irqrestore(&dev->event_lock, flags);
604 ssize_t drm_read(struct file *filp, char __user *buffer,
605 size_t count, loff_t *offset)
607 struct drm_file *file_priv = filp->private_data;
608 struct drm_pending_event *e;
612 ret = wait_event_interruptible(file_priv->event_wait,
613 !list_empty(&file_priv->event_list));
618 while (drm_dequeue_event(file_priv, total, count, &e)) {
619 if (copy_to_user(buffer + total,
620 e->event, e->event->length)) {
625 total += e->event->length;
631 EXPORT_SYMBOL(drm_read);
633 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
635 struct drm_file *file_priv = filp->private_data;
636 unsigned int mask = 0;
638 poll_wait(filp, &file_priv->event_wait, wait);
640 if (!list_empty(&file_priv->event_list))
641 mask |= POLLIN | POLLRDNORM;
645 EXPORT_SYMBOL(drm_poll);