10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <drm/drm_file.h>
41 #include "drm_legacy.h"
42 #include "drm_internal.h"
43 #include "drm_crtc_internal.h"
45 /* from BKL pushdown */
46 DEFINE_MUTEX(drm_global_mutex);
49 * DOC: file operations
51 * Drivers must define the file operations structure that forms the DRM
52 * userspace API entry point, even though most of those operations are
53 * implemented in the DRM core. The resulting &struct file_operations must be
54 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
55 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
56 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
57 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
58 * that require 32/64 bit compatibility support must provide their own
59 * &file_operations.compat_ioctl handler that processes private ioctls and calls
60 * drm_compat_ioctl() for core ioctls.
62 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
63 * events are a generic and extensible means to send asynchronous events to
64 * userspace through the file descriptor. They are used to send vblank event and
65 * page flip completions by the KMS API. But drivers can also use it for their
66 * own needs, e.g. to signal completion of rendering.
68 * For the driver-side event interface see drm_event_reserve_init() and
69 * drm_send_event() as the main starting points.
71 * The memory mapping implementation will vary depending on how the driver
72 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
73 * function, modern drivers should use one of the provided memory-manager
74 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
75 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
77 * No other file operations are supported by the DRM userspace API. Overall the
78 * following is an example &file_operations structure::
80 * static const example_drm_fops = {
81 * .owner = THIS_MODULE,
83 * .release = drm_release,
84 * .unlocked_ioctl = drm_ioctl,
85 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
88 * .llseek = no_llseek,
89 * .mmap = drm_gem_mmap,
92 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
93 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
96 * The driver's &file_operations must be stored in &drm_driver.fops.
98 * For driver-private IOCTL handling see the more detailed discussion in
99 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
102 static int drm_open_helper(struct file *filp, struct drm_minor *minor);
105 * drm_file_alloc - allocate file context
106 * @minor: minor to allocate on
108 * This allocates a new DRM file context. It is not linked into any context and
109 * can be used by the caller freely. Note that the context keeps a pointer to
110 * @minor, so it must be freed before @minor is.
113 * Pointer to newly allocated context, ERR_PTR on failure.
115 struct drm_file *drm_file_alloc(struct drm_minor *minor)
117 struct drm_device *dev = minor->dev;
118 struct drm_file *file;
121 file = kzalloc(sizeof(*file), GFP_KERNEL);
123 return ERR_PTR(-ENOMEM);
125 file->pid = get_pid(task_pid(current));
128 /* for compatibility root is always authenticated */
129 file->authenticated = capable(CAP_SYS_ADMIN);
130 file->lock_count = 0;
132 INIT_LIST_HEAD(&file->lhead);
133 INIT_LIST_HEAD(&file->fbs);
134 mutex_init(&file->fbs_lock);
135 INIT_LIST_HEAD(&file->blobs);
136 INIT_LIST_HEAD(&file->pending_event_list);
137 INIT_LIST_HEAD(&file->event_list);
138 init_waitqueue_head(&file->event_wait);
139 file->event_space = 4096; /* set aside 4k for event buffer */
141 mutex_init(&file->event_read_lock);
143 if (drm_core_check_feature(dev, DRIVER_GEM))
144 drm_gem_open(dev, file);
146 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
147 drm_syncobj_open(file);
149 if (drm_core_check_feature(dev, DRIVER_PRIME))
150 drm_prime_init_file_private(&file->prime);
152 if (dev->driver->open) {
153 ret = dev->driver->open(dev, file);
155 goto out_prime_destroy;
161 if (drm_core_check_feature(dev, DRIVER_PRIME))
162 drm_prime_destroy_file_private(&file->prime);
163 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
164 drm_syncobj_release(file);
165 if (drm_core_check_feature(dev, DRIVER_GEM))
166 drm_gem_release(dev, file);
173 static void drm_events_release(struct drm_file *file_priv)
175 struct drm_device *dev = file_priv->minor->dev;
176 struct drm_pending_event *e, *et;
179 spin_lock_irqsave(&dev->event_lock, flags);
181 /* Unlink pending events */
182 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
184 list_del(&e->pending_link);
188 /* Remove unconsumed events */
189 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
194 spin_unlock_irqrestore(&dev->event_lock, flags);
198 * drm_file_free - free file context
199 * @file: context to free, or NULL
201 * This destroys and deallocates a DRM file context previously allocated via
202 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
203 * before calling this.
205 * If NULL is passed, this is a no-op.
208 * 0 on success, or error code on failure.
210 void drm_file_free(struct drm_file *file)
212 struct drm_device *dev;
217 dev = file->minor->dev;
219 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
220 task_pid_nr(current),
221 (long)old_encode_dev(file->minor->kdev->devt),
224 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
225 dev->driver->preclose)
226 dev->driver->preclose(dev, file);
228 if (drm_core_check_feature(dev, DRIVER_LEGACY))
229 drm_legacy_lock_release(dev, file->filp);
231 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
232 drm_legacy_reclaim_buffers(dev, file);
234 drm_events_release(file);
236 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
237 drm_fb_release(file);
238 drm_property_destroy_user_blobs(dev, file);
241 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
242 drm_syncobj_release(file);
244 if (drm_core_check_feature(dev, DRIVER_GEM))
245 drm_gem_release(dev, file);
247 drm_legacy_ctxbitmap_flush(dev, file);
249 if (drm_is_primary_client(file))
250 drm_master_release(file);
252 if (dev->driver->postclose)
253 dev->driver->postclose(dev, file);
255 if (drm_core_check_feature(dev, DRIVER_PRIME))
256 drm_prime_destroy_file_private(&file->prime);
258 WARN_ON(!list_empty(&file->event_list));
264 static int drm_setup(struct drm_device * dev)
268 if (dev->driver->firstopen &&
269 drm_core_check_feature(dev, DRIVER_LEGACY)) {
270 ret = dev->driver->firstopen(dev);
275 ret = drm_legacy_dma_setup(dev);
285 * drm_open - open method for DRM file
286 * @inode: device inode
287 * @filp: file pointer.
289 * This function must be used by drivers as their &file_operations.open method.
290 * It looks up the correct DRM device and instantiates all the per-file
291 * resources for it. It also calls the &drm_driver.open driver callback.
295 * 0 on success or negative errno value on falure.
297 int drm_open(struct inode *inode, struct file *filp)
299 struct drm_device *dev;
300 struct drm_minor *minor;
304 minor = drm_minor_acquire(iminor(inode));
306 return PTR_ERR(minor);
309 if (!dev->open_count++)
312 /* share address_space across all char-devs of a single device */
313 filp->f_mapping = dev->anon_inode->i_mapping;
315 retcode = drm_open_helper(filp, minor);
319 retcode = drm_setup(dev);
327 drm_minor_release(minor);
330 EXPORT_SYMBOL(drm_open);
333 * Check whether DRI will run on this CPU.
335 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
337 static int drm_cpu_valid(void)
339 #if defined(__sparc__) && !defined(__sparc_v9__)
340 return 0; /* No cmpxchg before v9 sparc. */
346 * Called whenever a process opens /dev/drm.
348 * \param filp file pointer.
349 * \param minor acquired minor-object.
350 * \return zero on success or a negative number on failure.
352 * Creates and initializes a drm_file structure for the file private data in \p
353 * filp and add it into the double linked list in \p dev.
355 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
357 struct drm_device *dev = minor->dev;
358 struct drm_file *priv;
361 if (filp->f_flags & O_EXCL)
362 return -EBUSY; /* No exclusive opens */
363 if (!drm_cpu_valid())
365 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
368 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
370 priv = drm_file_alloc(minor);
372 return PTR_ERR(priv);
374 if (drm_is_primary_client(priv)) {
375 ret = drm_master_open(priv);
382 filp->private_data = priv;
383 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
386 mutex_lock(&dev->filelist_mutex);
387 list_add(&priv->lhead, &dev->filelist);
388 mutex_unlock(&dev->filelist_mutex);
395 struct pci_dev *pci_dev;
396 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
398 dev->hose = pci_dev->sysdata;
399 pci_dev_put(pci_dev);
402 struct pci_bus *b = list_entry(pci_root_buses.next,
403 struct pci_bus, node);
405 dev->hose = b->sysdata;
413 static void drm_legacy_dev_reinit(struct drm_device *dev)
415 if (dev->irq_enabled)
416 drm_irq_uninstall(dev);
418 mutex_lock(&dev->struct_mutex);
420 drm_legacy_agp_clear(dev);
422 drm_legacy_sg_cleanup(dev);
423 drm_legacy_vma_flush(dev);
424 drm_legacy_dma_takedown(dev);
426 mutex_unlock(&dev->struct_mutex);
428 dev->sigdata.lock = NULL;
430 dev->context_flag = 0;
431 dev->last_context = 0;
434 DRM_DEBUG("lastclose completed\n");
437 void drm_lastclose(struct drm_device * dev)
441 if (dev->driver->lastclose)
442 dev->driver->lastclose(dev);
443 DRM_DEBUG("driver lastclose completed\n");
445 if (drm_core_check_feature(dev, DRIVER_LEGACY))
446 drm_legacy_dev_reinit(dev);
450 * drm_release - release method for DRM file
451 * @inode: device inode
452 * @filp: file pointer.
454 * This function must be used by drivers as their &file_operations.release
455 * method. It frees any resources associated with the open file, and calls the
456 * &drm_driver.postclose driver callback. If this is the last open file for the
457 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
461 * Always succeeds and returns 0.
463 int drm_release(struct inode *inode, struct file *filp)
465 struct drm_file *file_priv = filp->private_data;
466 struct drm_minor *minor = file_priv->minor;
467 struct drm_device *dev = minor->dev;
469 mutex_lock(&drm_global_mutex);
471 DRM_DEBUG("open_count = %d\n", dev->open_count);
473 mutex_lock(&dev->filelist_mutex);
474 list_del(&file_priv->lhead);
475 mutex_unlock(&dev->filelist_mutex);
477 drm_file_free(file_priv);
479 if (!--dev->open_count) {
481 if (drm_dev_is_unplugged(dev))
484 mutex_unlock(&drm_global_mutex);
486 drm_minor_release(minor);
490 EXPORT_SYMBOL(drm_release);
493 * drm_read - read method for DRM file
494 * @filp: file pointer
495 * @buffer: userspace destination pointer for the read
496 * @count: count in bytes to read
497 * @offset: offset to read
499 * This function must be used by drivers as their &file_operations.read
500 * method iff they use DRM events for asynchronous signalling to userspace.
501 * Since events are used by the KMS API for vblank and page flip completion this
502 * means all modern display drivers must use it.
504 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
505 * must set the &file_operation.llseek to no_llseek(). Polling support is
506 * provided by drm_poll().
508 * This function will only ever read a full event. Therefore userspace must
509 * supply a big enough buffer to fit any event to ensure forward progress. Since
510 * the maximum event space is currently 4K it's recommended to just use that for
515 * Number of bytes read (always aligned to full events, and can be 0) or a
516 * negative error code on failure.
518 ssize_t drm_read(struct file *filp, char __user *buffer,
519 size_t count, loff_t *offset)
521 struct drm_file *file_priv = filp->private_data;
522 struct drm_device *dev = file_priv->minor->dev;
525 if (!access_ok(VERIFY_WRITE, buffer, count))
528 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
533 struct drm_pending_event *e = NULL;
535 spin_lock_irq(&dev->event_lock);
536 if (!list_empty(&file_priv->event_list)) {
537 e = list_first_entry(&file_priv->event_list,
538 struct drm_pending_event, link);
539 file_priv->event_space += e->event->length;
542 spin_unlock_irq(&dev->event_lock);
548 if (filp->f_flags & O_NONBLOCK) {
553 mutex_unlock(&file_priv->event_read_lock);
554 ret = wait_event_interruptible(file_priv->event_wait,
555 !list_empty(&file_priv->event_list));
557 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
561 unsigned length = e->event->length;
563 if (length > count - ret) {
565 spin_lock_irq(&dev->event_lock);
566 file_priv->event_space -= length;
567 list_add(&e->link, &file_priv->event_list);
568 spin_unlock_irq(&dev->event_lock);
572 if (copy_to_user(buffer + ret, e->event, length)) {
582 mutex_unlock(&file_priv->event_read_lock);
586 EXPORT_SYMBOL(drm_read);
589 * drm_poll - poll method for DRM file
590 * @filp: file pointer
591 * @wait: poll waiter table
593 * This function must be used by drivers as their &file_operations.read method
594 * iff they use DRM events for asynchronous signalling to userspace. Since
595 * events are used by the KMS API for vblank and page flip completion this means
596 * all modern display drivers must use it.
598 * See also drm_read().
602 * Mask of POLL flags indicating the current status of the file.
604 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
606 struct drm_file *file_priv = filp->private_data;
609 poll_wait(filp, &file_priv->event_wait, wait);
611 if (!list_empty(&file_priv->event_list))
612 mask |= EPOLLIN | EPOLLRDNORM;
616 EXPORT_SYMBOL(drm_poll);
619 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
621 * @file_priv: DRM file private data
622 * @p: tracking structure for the pending event
623 * @e: actual event data to deliver to userspace
625 * This function prepares the passed in event for eventual delivery. If the event
626 * doesn't get delivered (because the IOCTL fails later on, before queuing up
627 * anything) then the even must be cancelled and freed using
628 * drm_event_cancel_free(). Successfully initialized events should be sent out
629 * using drm_send_event() or drm_send_event_locked() to signal completion of the
630 * asynchronous event to userspace.
632 * If callers embedded @p into a larger structure it must be allocated with
633 * kmalloc and @p must be the first member element.
635 * This is the locked version of drm_event_reserve_init() for callers which
636 * already hold &drm_device.event_lock.
640 * 0 on success or a negative error code on failure.
642 int drm_event_reserve_init_locked(struct drm_device *dev,
643 struct drm_file *file_priv,
644 struct drm_pending_event *p,
647 if (file_priv->event_space < e->length)
650 file_priv->event_space -= e->length;
653 list_add(&p->pending_link, &file_priv->pending_event_list);
654 p->file_priv = file_priv;
658 EXPORT_SYMBOL(drm_event_reserve_init_locked);
661 * drm_event_reserve_init - init a DRM event and reserve space for it
663 * @file_priv: DRM file private data
664 * @p: tracking structure for the pending event
665 * @e: actual event data to deliver to userspace
667 * This function prepares the passed in event for eventual delivery. If the event
668 * doesn't get delivered (because the IOCTL fails later on, before queuing up
669 * anything) then the even must be cancelled and freed using
670 * drm_event_cancel_free(). Successfully initialized events should be sent out
671 * using drm_send_event() or drm_send_event_locked() to signal completion of the
672 * asynchronous event to userspace.
674 * If callers embedded @p into a larger structure it must be allocated with
675 * kmalloc and @p must be the first member element.
677 * Callers which already hold &drm_device.event_lock should use
678 * drm_event_reserve_init_locked() instead.
682 * 0 on success or a negative error code on failure.
684 int drm_event_reserve_init(struct drm_device *dev,
685 struct drm_file *file_priv,
686 struct drm_pending_event *p,
692 spin_lock_irqsave(&dev->event_lock, flags);
693 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
694 spin_unlock_irqrestore(&dev->event_lock, flags);
698 EXPORT_SYMBOL(drm_event_reserve_init);
701 * drm_event_cancel_free - free a DRM event and release it's space
703 * @p: tracking structure for the pending event
705 * This function frees the event @p initialized with drm_event_reserve_init()
706 * and releases any allocated space. It is used to cancel an event when the
707 * nonblocking operation could not be submitted and needed to be aborted.
709 void drm_event_cancel_free(struct drm_device *dev,
710 struct drm_pending_event *p)
713 spin_lock_irqsave(&dev->event_lock, flags);
715 p->file_priv->event_space += p->event->length;
716 list_del(&p->pending_link);
718 spin_unlock_irqrestore(&dev->event_lock, flags);
721 dma_fence_put(p->fence);
725 EXPORT_SYMBOL(drm_event_cancel_free);
728 * drm_send_event_locked - send DRM event to file descriptor
730 * @e: DRM event to deliver
732 * This function sends the event @e, initialized with drm_event_reserve_init(),
733 * to its associated userspace DRM file. Callers must already hold
734 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
736 * Note that the core will take care of unlinking and disarming events when the
737 * corresponding DRM file is closed. Drivers need not worry about whether the
738 * DRM file for this event still exists and can call this function upon
739 * completion of the asynchronous work unconditionally.
741 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
743 assert_spin_locked(&dev->event_lock);
746 complete_all(e->completion);
747 e->completion_release(e->completion);
748 e->completion = NULL;
752 dma_fence_signal(e->fence);
753 dma_fence_put(e->fence);
761 list_del(&e->pending_link);
762 list_add_tail(&e->link,
763 &e->file_priv->event_list);
764 wake_up_interruptible(&e->file_priv->event_wait);
766 EXPORT_SYMBOL(drm_send_event_locked);
769 * drm_send_event - send DRM event to file descriptor
771 * @e: DRM event to deliver
773 * This function sends the event @e, initialized with drm_event_reserve_init(),
774 * to its associated userspace DRM file. This function acquires
775 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
778 * Note that the core will take care of unlinking and disarming events when the
779 * corresponding DRM file is closed. Drivers need not worry about whether the
780 * DRM file for this event still exists and can call this function upon
781 * completion of the asynchronous work unconditionally.
783 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
785 unsigned long irqflags;
787 spin_lock_irqsave(&dev->event_lock, irqflags);
788 drm_send_event_locked(dev, e);
789 spin_unlock_irqrestore(&dev->event_lock, irqflags);
791 EXPORT_SYMBOL(drm_send_event);