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_client.h>
39 #include <drm/drm_file.h>
42 #include "drm_legacy.h"
43 #include "drm_internal.h"
44 #include "drm_crtc_internal.h"
46 /* from BKL pushdown */
47 DEFINE_MUTEX(drm_global_mutex);
50 * DOC: file operations
52 * Drivers must define the file operations structure that forms the DRM
53 * userspace API entry point, even though most of those operations are
54 * implemented in the DRM core. The resulting &struct file_operations must be
55 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
56 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
57 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
58 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
59 * that require 32/64 bit compatibility support must provide their own
60 * &file_operations.compat_ioctl handler that processes private ioctls and calls
61 * drm_compat_ioctl() for core ioctls.
63 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
64 * events are a generic and extensible means to send asynchronous events to
65 * userspace through the file descriptor. They are used to send vblank event and
66 * page flip completions by the KMS API. But drivers can also use it for their
67 * own needs, e.g. to signal completion of rendering.
69 * For the driver-side event interface see drm_event_reserve_init() and
70 * drm_send_event() as the main starting points.
72 * The memory mapping implementation will vary depending on how the driver
73 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
74 * function, modern drivers should use one of the provided memory-manager
75 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
76 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
78 * No other file operations are supported by the DRM userspace API. Overall the
79 * following is an example &file_operations structure::
81 * static const example_drm_fops = {
82 * .owner = THIS_MODULE,
84 * .release = drm_release,
85 * .unlocked_ioctl = drm_ioctl,
86 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
89 * .llseek = no_llseek,
90 * .mmap = drm_gem_mmap,
93 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
94 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
97 * The driver's &file_operations must be stored in &drm_driver.fops.
99 * For driver-private IOCTL handling see the more detailed discussion in
100 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
103 static int drm_open_helper(struct file *filp, struct drm_minor *minor);
106 * drm_file_alloc - allocate file context
107 * @minor: minor to allocate on
109 * This allocates a new DRM file context. It is not linked into any context and
110 * can be used by the caller freely. Note that the context keeps a pointer to
111 * @minor, so it must be freed before @minor is.
114 * Pointer to newly allocated context, ERR_PTR on failure.
116 struct drm_file *drm_file_alloc(struct drm_minor *minor)
118 struct drm_device *dev = minor->dev;
119 struct drm_file *file;
122 file = kzalloc(sizeof(*file), GFP_KERNEL);
124 return ERR_PTR(-ENOMEM);
126 file->pid = get_pid(task_pid(current));
129 /* for compatibility root is always authenticated */
130 file->authenticated = capable(CAP_SYS_ADMIN);
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 void drm_close_helper(struct file *filp)
266 struct drm_file *file_priv = filp->private_data;
267 struct drm_device *dev = file_priv->minor->dev;
269 mutex_lock(&dev->filelist_mutex);
270 list_del(&file_priv->lhead);
271 mutex_unlock(&dev->filelist_mutex);
273 drm_file_free(file_priv);
276 static int drm_setup(struct drm_device * dev)
280 if (dev->driver->firstopen &&
281 drm_core_check_feature(dev, DRIVER_LEGACY)) {
282 ret = dev->driver->firstopen(dev);
287 ret = drm_legacy_dma_setup(dev);
297 * drm_open - open method for DRM file
298 * @inode: device inode
299 * @filp: file pointer.
301 * This function must be used by drivers as their &file_operations.open method.
302 * It looks up the correct DRM device and instantiates all the per-file
303 * resources for it. It also calls the &drm_driver.open driver callback.
307 * 0 on success or negative errno value on falure.
309 int drm_open(struct inode *inode, struct file *filp)
311 struct drm_device *dev;
312 struct drm_minor *minor;
316 minor = drm_minor_acquire(iminor(inode));
318 return PTR_ERR(minor);
321 if (!dev->open_count++)
324 /* share address_space across all char-devs of a single device */
325 filp->f_mapping = dev->anon_inode->i_mapping;
327 retcode = drm_open_helper(filp, minor);
331 retcode = drm_setup(dev);
333 drm_close_helper(filp);
341 drm_minor_release(minor);
344 EXPORT_SYMBOL(drm_open);
347 * Check whether DRI will run on this CPU.
349 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
351 static int drm_cpu_valid(void)
353 #if defined(__sparc__) && !defined(__sparc_v9__)
354 return 0; /* No cmpxchg before v9 sparc. */
360 * Called whenever a process opens /dev/drm.
362 * \param filp file pointer.
363 * \param minor acquired minor-object.
364 * \return zero on success or a negative number on failure.
366 * Creates and initializes a drm_file structure for the file private data in \p
367 * filp and add it into the double linked list in \p dev.
369 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
371 struct drm_device *dev = minor->dev;
372 struct drm_file *priv;
375 if (filp->f_flags & O_EXCL)
376 return -EBUSY; /* No exclusive opens */
377 if (!drm_cpu_valid())
379 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
382 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
384 priv = drm_file_alloc(minor);
386 return PTR_ERR(priv);
388 if (drm_is_primary_client(priv)) {
389 ret = drm_master_open(priv);
396 filp->private_data = priv;
397 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
400 mutex_lock(&dev->filelist_mutex);
401 list_add(&priv->lhead, &dev->filelist);
402 mutex_unlock(&dev->filelist_mutex);
409 struct pci_dev *pci_dev;
410 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
412 dev->hose = pci_dev->sysdata;
413 pci_dev_put(pci_dev);
416 struct pci_bus *b = list_entry(pci_root_buses.next,
417 struct pci_bus, node);
419 dev->hose = b->sysdata;
427 void drm_lastclose(struct drm_device * dev)
431 if (dev->driver->lastclose)
432 dev->driver->lastclose(dev);
433 DRM_DEBUG("driver lastclose completed\n");
435 if (drm_core_check_feature(dev, DRIVER_LEGACY))
436 drm_legacy_dev_reinit(dev);
438 drm_client_dev_restore(dev);
442 * drm_release - release method for DRM file
443 * @inode: device inode
444 * @filp: file pointer.
446 * This function must be used by drivers as their &file_operations.release
447 * method. It frees any resources associated with the open file, and calls the
448 * &drm_driver.postclose driver callback. If this is the last open file for the
449 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
453 * Always succeeds and returns 0.
455 int drm_release(struct inode *inode, struct file *filp)
457 struct drm_file *file_priv = filp->private_data;
458 struct drm_minor *minor = file_priv->minor;
459 struct drm_device *dev = minor->dev;
461 mutex_lock(&drm_global_mutex);
463 DRM_DEBUG("open_count = %d\n", dev->open_count);
465 drm_close_helper(filp);
467 if (!--dev->open_count)
470 mutex_unlock(&drm_global_mutex);
472 drm_minor_release(minor);
476 EXPORT_SYMBOL(drm_release);
479 * drm_read - read method for DRM file
480 * @filp: file pointer
481 * @buffer: userspace destination pointer for the read
482 * @count: count in bytes to read
483 * @offset: offset to read
485 * This function must be used by drivers as their &file_operations.read
486 * method iff they use DRM events for asynchronous signalling to userspace.
487 * Since events are used by the KMS API for vblank and page flip completion this
488 * means all modern display drivers must use it.
490 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
491 * must set the &file_operation.llseek to no_llseek(). Polling support is
492 * provided by drm_poll().
494 * This function will only ever read a full event. Therefore userspace must
495 * supply a big enough buffer to fit any event to ensure forward progress. Since
496 * the maximum event space is currently 4K it's recommended to just use that for
501 * Number of bytes read (always aligned to full events, and can be 0) or a
502 * negative error code on failure.
504 ssize_t drm_read(struct file *filp, char __user *buffer,
505 size_t count, loff_t *offset)
507 struct drm_file *file_priv = filp->private_data;
508 struct drm_device *dev = file_priv->minor->dev;
511 if (!access_ok(buffer, count))
514 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
519 struct drm_pending_event *e = NULL;
521 spin_lock_irq(&dev->event_lock);
522 if (!list_empty(&file_priv->event_list)) {
523 e = list_first_entry(&file_priv->event_list,
524 struct drm_pending_event, link);
525 file_priv->event_space += e->event->length;
528 spin_unlock_irq(&dev->event_lock);
534 if (filp->f_flags & O_NONBLOCK) {
539 mutex_unlock(&file_priv->event_read_lock);
540 ret = wait_event_interruptible(file_priv->event_wait,
541 !list_empty(&file_priv->event_list));
543 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
547 unsigned length = e->event->length;
549 if (length > count - ret) {
551 spin_lock_irq(&dev->event_lock);
552 file_priv->event_space -= length;
553 list_add(&e->link, &file_priv->event_list);
554 spin_unlock_irq(&dev->event_lock);
555 wake_up_interruptible(&file_priv->event_wait);
559 if (copy_to_user(buffer + ret, e->event, length)) {
569 mutex_unlock(&file_priv->event_read_lock);
573 EXPORT_SYMBOL(drm_read);
576 * drm_poll - poll method for DRM file
577 * @filp: file pointer
578 * @wait: poll waiter table
580 * This function must be used by drivers as their &file_operations.read method
581 * iff they use DRM events for asynchronous signalling to userspace. Since
582 * events are used by the KMS API for vblank and page flip completion this means
583 * all modern display drivers must use it.
585 * See also drm_read().
589 * Mask of POLL flags indicating the current status of the file.
591 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
593 struct drm_file *file_priv = filp->private_data;
596 poll_wait(filp, &file_priv->event_wait, wait);
598 if (!list_empty(&file_priv->event_list))
599 mask |= EPOLLIN | EPOLLRDNORM;
603 EXPORT_SYMBOL(drm_poll);
606 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
608 * @file_priv: DRM file private data
609 * @p: tracking structure for the pending event
610 * @e: actual event data to deliver to userspace
612 * This function prepares the passed in event for eventual delivery. If the event
613 * doesn't get delivered (because the IOCTL fails later on, before queuing up
614 * anything) then the even must be cancelled and freed using
615 * drm_event_cancel_free(). Successfully initialized events should be sent out
616 * using drm_send_event() or drm_send_event_locked() to signal completion of the
617 * asynchronous event to userspace.
619 * If callers embedded @p into a larger structure it must be allocated with
620 * kmalloc and @p must be the first member element.
622 * This is the locked version of drm_event_reserve_init() for callers which
623 * already hold &drm_device.event_lock.
627 * 0 on success or a negative error code on failure.
629 int drm_event_reserve_init_locked(struct drm_device *dev,
630 struct drm_file *file_priv,
631 struct drm_pending_event *p,
634 if (file_priv->event_space < e->length)
637 file_priv->event_space -= e->length;
640 list_add(&p->pending_link, &file_priv->pending_event_list);
641 p->file_priv = file_priv;
645 EXPORT_SYMBOL(drm_event_reserve_init_locked);
648 * drm_event_reserve_init - init a DRM event and reserve space for it
650 * @file_priv: DRM file private data
651 * @p: tracking structure for the pending event
652 * @e: actual event data to deliver to userspace
654 * This function prepares the passed in event for eventual delivery. If the event
655 * doesn't get delivered (because the IOCTL fails later on, before queuing up
656 * anything) then the even must be cancelled and freed using
657 * drm_event_cancel_free(). Successfully initialized events should be sent out
658 * using drm_send_event() or drm_send_event_locked() to signal completion of the
659 * asynchronous event to userspace.
661 * If callers embedded @p into a larger structure it must be allocated with
662 * kmalloc and @p must be the first member element.
664 * Callers which already hold &drm_device.event_lock should use
665 * drm_event_reserve_init_locked() instead.
669 * 0 on success or a negative error code on failure.
671 int drm_event_reserve_init(struct drm_device *dev,
672 struct drm_file *file_priv,
673 struct drm_pending_event *p,
679 spin_lock_irqsave(&dev->event_lock, flags);
680 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
681 spin_unlock_irqrestore(&dev->event_lock, flags);
685 EXPORT_SYMBOL(drm_event_reserve_init);
688 * drm_event_cancel_free - free a DRM event and release its space
690 * @p: tracking structure for the pending event
692 * This function frees the event @p initialized with drm_event_reserve_init()
693 * and releases any allocated space. It is used to cancel an event when the
694 * nonblocking operation could not be submitted and needed to be aborted.
696 void drm_event_cancel_free(struct drm_device *dev,
697 struct drm_pending_event *p)
700 spin_lock_irqsave(&dev->event_lock, flags);
702 p->file_priv->event_space += p->event->length;
703 list_del(&p->pending_link);
705 spin_unlock_irqrestore(&dev->event_lock, flags);
708 dma_fence_put(p->fence);
712 EXPORT_SYMBOL(drm_event_cancel_free);
715 * drm_send_event_locked - send DRM event to file descriptor
717 * @e: DRM event to deliver
719 * This function sends the event @e, initialized with drm_event_reserve_init(),
720 * to its associated userspace DRM file. Callers must already hold
721 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
723 * Note that the core will take care of unlinking and disarming events when the
724 * corresponding DRM file is closed. Drivers need not worry about whether the
725 * DRM file for this event still exists and can call this function upon
726 * completion of the asynchronous work unconditionally.
728 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
730 assert_spin_locked(&dev->event_lock);
733 complete_all(e->completion);
734 e->completion_release(e->completion);
735 e->completion = NULL;
739 dma_fence_signal(e->fence);
740 dma_fence_put(e->fence);
748 list_del(&e->pending_link);
749 list_add_tail(&e->link,
750 &e->file_priv->event_list);
751 wake_up_interruptible(&e->file_priv->event_wait);
753 EXPORT_SYMBOL(drm_send_event_locked);
756 * drm_send_event - send DRM event to file descriptor
758 * @e: DRM event to deliver
760 * This function sends the event @e, initialized with drm_event_reserve_init(),
761 * to its associated userspace DRM file. This function acquires
762 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
765 * Note that the core will take care of unlinking and disarming events when the
766 * corresponding DRM file is closed. Drivers need not worry about whether the
767 * DRM file for this event still exists and can call this function upon
768 * completion of the asynchronous work unconditionally.
770 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
772 unsigned long irqflags;
774 spin_lock_irqsave(&dev->event_lock, irqflags);
775 drm_send_event_locked(dev, e);
776 spin_unlock_irqrestore(&dev->event_lock, irqflags);
778 EXPORT_SYMBOL(drm_send_event);