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/dma-fence.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/poll.h>
38 #include <linux/slab.h>
40 #include <drm/drm_client.h>
41 #include <drm/drm_drv.h>
42 #include <drm/drm_file.h>
43 #include <drm/drm_print.h>
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
47 #include "drm_legacy.h"
49 /* from BKL pushdown */
50 DEFINE_MUTEX(drm_global_mutex);
53 * DOC: file operations
55 * Drivers must define the file operations structure that forms the DRM
56 * userspace API entry point, even though most of those operations are
57 * implemented in the DRM core. The resulting &struct file_operations must be
58 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
59 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
60 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
61 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
62 * that require 32/64 bit compatibility support must provide their own
63 * &file_operations.compat_ioctl handler that processes private ioctls and calls
64 * drm_compat_ioctl() for core ioctls.
66 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
67 * events are a generic and extensible means to send asynchronous events to
68 * userspace through the file descriptor. They are used to send vblank event and
69 * page flip completions by the KMS API. But drivers can also use it for their
70 * own needs, e.g. to signal completion of rendering.
72 * For the driver-side event interface see drm_event_reserve_init() and
73 * drm_send_event() as the main starting points.
75 * The memory mapping implementation will vary depending on how the driver
76 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
77 * function, modern drivers should use one of the provided memory-manager
78 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
79 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
81 * No other file operations are supported by the DRM userspace API. Overall the
82 * following is an example &file_operations structure::
84 * static const example_drm_fops = {
85 * .owner = THIS_MODULE,
87 * .release = drm_release,
88 * .unlocked_ioctl = drm_ioctl,
89 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
92 * .llseek = no_llseek,
93 * .mmap = drm_gem_mmap,
96 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
97 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
100 * The driver's &file_operations must be stored in &drm_driver.fops.
102 * For driver-private IOCTL handling see the more detailed discussion in
103 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
107 * drm_file_alloc - allocate file context
108 * @minor: minor to allocate on
110 * This allocates a new DRM file context. It is not linked into any context and
111 * can be used by the caller freely. Note that the context keeps a pointer to
112 * @minor, so it must be freed before @minor is.
115 * Pointer to newly allocated context, ERR_PTR on failure.
117 struct drm_file *drm_file_alloc(struct drm_minor *minor)
119 struct drm_device *dev = minor->dev;
120 struct drm_file *file;
123 file = kzalloc(sizeof(*file), GFP_KERNEL);
125 return ERR_PTR(-ENOMEM);
127 file->pid = get_pid(task_pid(current));
130 /* for compatibility root is always authenticated */
131 file->authenticated = capable(CAP_SYS_ADMIN);
133 INIT_LIST_HEAD(&file->lhead);
134 INIT_LIST_HEAD(&file->fbs);
135 mutex_init(&file->fbs_lock);
136 INIT_LIST_HEAD(&file->blobs);
137 INIT_LIST_HEAD(&file->pending_event_list);
138 INIT_LIST_HEAD(&file->event_list);
139 init_waitqueue_head(&file->event_wait);
140 file->event_space = 4096; /* set aside 4k for event buffer */
142 mutex_init(&file->event_read_lock);
144 if (drm_core_check_feature(dev, DRIVER_GEM))
145 drm_gem_open(dev, file);
147 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
148 drm_syncobj_open(file);
150 if (drm_core_check_feature(dev, DRIVER_PRIME))
151 drm_prime_init_file_private(&file->prime);
153 if (dev->driver->open) {
154 ret = dev->driver->open(dev, file);
156 goto out_prime_destroy;
162 if (drm_core_check_feature(dev, DRIVER_PRIME))
163 drm_prime_destroy_file_private(&file->prime);
164 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
165 drm_syncobj_release(file);
166 if (drm_core_check_feature(dev, DRIVER_GEM))
167 drm_gem_release(dev, file);
174 static void drm_events_release(struct drm_file *file_priv)
176 struct drm_device *dev = file_priv->minor->dev;
177 struct drm_pending_event *e, *et;
180 spin_lock_irqsave(&dev->event_lock, flags);
182 /* Unlink pending events */
183 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
185 list_del(&e->pending_link);
189 /* Remove unconsumed events */
190 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
195 spin_unlock_irqrestore(&dev->event_lock, flags);
199 * drm_file_free - free file context
200 * @file: context to free, or NULL
202 * This destroys and deallocates a DRM file context previously allocated via
203 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
204 * before calling this.
206 * If NULL is passed, this is a no-op.
209 * 0 on success, or error code on failure.
211 void drm_file_free(struct drm_file *file)
213 struct drm_device *dev;
218 dev = file->minor->dev;
220 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
221 task_pid_nr(current),
222 (long)old_encode_dev(file->minor->kdev->devt),
225 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
226 dev->driver->preclose)
227 dev->driver->preclose(dev, file);
229 if (drm_core_check_feature(dev, DRIVER_LEGACY))
230 drm_legacy_lock_release(dev, file->filp);
232 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
233 drm_legacy_reclaim_buffers(dev, file);
235 drm_events_release(file);
237 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
238 drm_fb_release(file);
239 drm_property_destroy_user_blobs(dev, file);
242 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
243 drm_syncobj_release(file);
245 if (drm_core_check_feature(dev, DRIVER_GEM))
246 drm_gem_release(dev, file);
248 drm_legacy_ctxbitmap_flush(dev, file);
250 if (drm_is_primary_client(file))
251 drm_master_release(file);
253 if (dev->driver->postclose)
254 dev->driver->postclose(dev, file);
256 if (drm_core_check_feature(dev, DRIVER_PRIME))
257 drm_prime_destroy_file_private(&file->prime);
259 WARN_ON(!list_empty(&file->event_list));
265 static void drm_close_helper(struct file *filp)
267 struct drm_file *file_priv = filp->private_data;
268 struct drm_device *dev = file_priv->minor->dev;
270 mutex_lock(&dev->filelist_mutex);
271 list_del(&file_priv->lhead);
272 mutex_unlock(&dev->filelist_mutex);
274 drm_file_free(file_priv);
278 * Check whether DRI will run on this CPU.
280 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
282 static int drm_cpu_valid(void)
284 #if defined(__sparc__) && !defined(__sparc_v9__)
285 return 0; /* No cmpxchg before v9 sparc. */
291 * Called whenever a process opens /dev/drm.
293 * \param filp file pointer.
294 * \param minor acquired minor-object.
295 * \return zero on success or a negative number on failure.
297 * Creates and initializes a drm_file structure for the file private data in \p
298 * filp and add it into the double linked list in \p dev.
300 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
302 struct drm_device *dev = minor->dev;
303 struct drm_file *priv;
306 if (filp->f_flags & O_EXCL)
307 return -EBUSY; /* No exclusive opens */
308 if (!drm_cpu_valid())
310 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
313 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
315 priv = drm_file_alloc(minor);
317 return PTR_ERR(priv);
319 if (drm_is_primary_client(priv)) {
320 ret = drm_master_open(priv);
327 filp->private_data = priv;
328 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
331 mutex_lock(&dev->filelist_mutex);
332 list_add(&priv->lhead, &dev->filelist);
333 mutex_unlock(&dev->filelist_mutex);
340 struct pci_dev *pci_dev;
341 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
343 dev->hose = pci_dev->sysdata;
344 pci_dev_put(pci_dev);
347 struct pci_bus *b = list_entry(pci_root_buses.next,
348 struct pci_bus, node);
350 dev->hose = b->sysdata;
359 * drm_open - open method for DRM file
360 * @inode: device inode
361 * @filp: file pointer.
363 * This function must be used by drivers as their &file_operations.open method.
364 * It looks up the correct DRM device and instantiates all the per-file
365 * resources for it. It also calls the &drm_driver.open driver callback.
369 * 0 on success or negative errno value on falure.
371 int drm_open(struct inode *inode, struct file *filp)
373 struct drm_device *dev;
374 struct drm_minor *minor;
378 minor = drm_minor_acquire(iminor(inode));
380 return PTR_ERR(minor);
383 if (!dev->open_count++)
386 /* share address_space across all char-devs of a single device */
387 filp->f_mapping = dev->anon_inode->i_mapping;
389 retcode = drm_open_helper(filp, minor);
393 retcode = drm_legacy_setup(dev);
395 drm_close_helper(filp);
403 drm_minor_release(minor);
406 EXPORT_SYMBOL(drm_open);
408 void drm_lastclose(struct drm_device * dev)
412 if (dev->driver->lastclose)
413 dev->driver->lastclose(dev);
414 DRM_DEBUG("driver lastclose completed\n");
416 if (drm_core_check_feature(dev, DRIVER_LEGACY))
417 drm_legacy_dev_reinit(dev);
419 drm_client_dev_restore(dev);
423 * drm_release - release method for DRM file
424 * @inode: device inode
425 * @filp: file pointer.
427 * This function must be used by drivers as their &file_operations.release
428 * method. It frees any resources associated with the open file, and calls the
429 * &drm_driver.postclose driver callback. If this is the last open file for the
430 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
434 * Always succeeds and returns 0.
436 int drm_release(struct inode *inode, struct file *filp)
438 struct drm_file *file_priv = filp->private_data;
439 struct drm_minor *minor = file_priv->minor;
440 struct drm_device *dev = minor->dev;
442 mutex_lock(&drm_global_mutex);
444 DRM_DEBUG("open_count = %d\n", dev->open_count);
446 drm_close_helper(filp);
448 if (!--dev->open_count)
451 mutex_unlock(&drm_global_mutex);
453 drm_minor_release(minor);
457 EXPORT_SYMBOL(drm_release);
460 * drm_read - read method for DRM file
461 * @filp: file pointer
462 * @buffer: userspace destination pointer for the read
463 * @count: count in bytes to read
464 * @offset: offset to read
466 * This function must be used by drivers as their &file_operations.read
467 * method iff they use DRM events for asynchronous signalling to userspace.
468 * Since events are used by the KMS API for vblank and page flip completion this
469 * means all modern display drivers must use it.
471 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
472 * must set the &file_operation.llseek to no_llseek(). Polling support is
473 * provided by drm_poll().
475 * This function will only ever read a full event. Therefore userspace must
476 * supply a big enough buffer to fit any event to ensure forward progress. Since
477 * the maximum event space is currently 4K it's recommended to just use that for
482 * Number of bytes read (always aligned to full events, and can be 0) or a
483 * negative error code on failure.
485 ssize_t drm_read(struct file *filp, char __user *buffer,
486 size_t count, loff_t *offset)
488 struct drm_file *file_priv = filp->private_data;
489 struct drm_device *dev = file_priv->minor->dev;
492 if (!access_ok(buffer, count))
495 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
500 struct drm_pending_event *e = NULL;
502 spin_lock_irq(&dev->event_lock);
503 if (!list_empty(&file_priv->event_list)) {
504 e = list_first_entry(&file_priv->event_list,
505 struct drm_pending_event, link);
506 file_priv->event_space += e->event->length;
509 spin_unlock_irq(&dev->event_lock);
515 if (filp->f_flags & O_NONBLOCK) {
520 mutex_unlock(&file_priv->event_read_lock);
521 ret = wait_event_interruptible(file_priv->event_wait,
522 !list_empty(&file_priv->event_list));
524 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
528 unsigned length = e->event->length;
530 if (length > count - ret) {
532 spin_lock_irq(&dev->event_lock);
533 file_priv->event_space -= length;
534 list_add(&e->link, &file_priv->event_list);
535 spin_unlock_irq(&dev->event_lock);
536 wake_up_interruptible(&file_priv->event_wait);
540 if (copy_to_user(buffer + ret, e->event, length)) {
550 mutex_unlock(&file_priv->event_read_lock);
554 EXPORT_SYMBOL(drm_read);
557 * drm_poll - poll method for DRM file
558 * @filp: file pointer
559 * @wait: poll waiter table
561 * This function must be used by drivers as their &file_operations.read method
562 * iff they use DRM events for asynchronous signalling to userspace. Since
563 * events are used by the KMS API for vblank and page flip completion this means
564 * all modern display drivers must use it.
566 * See also drm_read().
570 * Mask of POLL flags indicating the current status of the file.
572 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
574 struct drm_file *file_priv = filp->private_data;
577 poll_wait(filp, &file_priv->event_wait, wait);
579 if (!list_empty(&file_priv->event_list))
580 mask |= EPOLLIN | EPOLLRDNORM;
584 EXPORT_SYMBOL(drm_poll);
587 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
589 * @file_priv: DRM file private data
590 * @p: tracking structure for the pending event
591 * @e: actual event data to deliver to userspace
593 * This function prepares the passed in event for eventual delivery. If the event
594 * doesn't get delivered (because the IOCTL fails later on, before queuing up
595 * anything) then the even must be cancelled and freed using
596 * drm_event_cancel_free(). Successfully initialized events should be sent out
597 * using drm_send_event() or drm_send_event_locked() to signal completion of the
598 * asynchronous event to userspace.
600 * If callers embedded @p into a larger structure it must be allocated with
601 * kmalloc and @p must be the first member element.
603 * This is the locked version of drm_event_reserve_init() for callers which
604 * already hold &drm_device.event_lock.
608 * 0 on success or a negative error code on failure.
610 int drm_event_reserve_init_locked(struct drm_device *dev,
611 struct drm_file *file_priv,
612 struct drm_pending_event *p,
615 if (file_priv->event_space < e->length)
618 file_priv->event_space -= e->length;
621 list_add(&p->pending_link, &file_priv->pending_event_list);
622 p->file_priv = file_priv;
626 EXPORT_SYMBOL(drm_event_reserve_init_locked);
629 * drm_event_reserve_init - init a DRM event and reserve space for it
631 * @file_priv: DRM file private data
632 * @p: tracking structure for the pending event
633 * @e: actual event data to deliver to userspace
635 * This function prepares the passed in event for eventual delivery. If the event
636 * doesn't get delivered (because the IOCTL fails later on, before queuing up
637 * anything) then the even must be cancelled and freed using
638 * drm_event_cancel_free(). Successfully initialized events should be sent out
639 * using drm_send_event() or drm_send_event_locked() to signal completion of the
640 * asynchronous event to userspace.
642 * If callers embedded @p into a larger structure it must be allocated with
643 * kmalloc and @p must be the first member element.
645 * Callers which already hold &drm_device.event_lock should use
646 * drm_event_reserve_init_locked() instead.
650 * 0 on success or a negative error code on failure.
652 int drm_event_reserve_init(struct drm_device *dev,
653 struct drm_file *file_priv,
654 struct drm_pending_event *p,
660 spin_lock_irqsave(&dev->event_lock, flags);
661 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
662 spin_unlock_irqrestore(&dev->event_lock, flags);
666 EXPORT_SYMBOL(drm_event_reserve_init);
669 * drm_event_cancel_free - free a DRM event and release its space
671 * @p: tracking structure for the pending event
673 * This function frees the event @p initialized with drm_event_reserve_init()
674 * and releases any allocated space. It is used to cancel an event when the
675 * nonblocking operation could not be submitted and needed to be aborted.
677 void drm_event_cancel_free(struct drm_device *dev,
678 struct drm_pending_event *p)
681 spin_lock_irqsave(&dev->event_lock, flags);
683 p->file_priv->event_space += p->event->length;
684 list_del(&p->pending_link);
686 spin_unlock_irqrestore(&dev->event_lock, flags);
689 dma_fence_put(p->fence);
693 EXPORT_SYMBOL(drm_event_cancel_free);
696 * drm_send_event_locked - send DRM event to file descriptor
698 * @e: DRM event to deliver
700 * This function sends the event @e, initialized with drm_event_reserve_init(),
701 * to its associated userspace DRM file. Callers must already hold
702 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
704 * Note that the core will take care of unlinking and disarming events when the
705 * corresponding DRM file is closed. Drivers need not worry about whether the
706 * DRM file for this event still exists and can call this function upon
707 * completion of the asynchronous work unconditionally.
709 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
711 assert_spin_locked(&dev->event_lock);
714 complete_all(e->completion);
715 e->completion_release(e->completion);
716 e->completion = NULL;
720 dma_fence_signal(e->fence);
721 dma_fence_put(e->fence);
729 list_del(&e->pending_link);
730 list_add_tail(&e->link,
731 &e->file_priv->event_list);
732 wake_up_interruptible(&e->file_priv->event_wait);
734 EXPORT_SYMBOL(drm_send_event_locked);
737 * drm_send_event - send DRM event to file descriptor
739 * @e: DRM event to deliver
741 * This function sends the event @e, initialized with drm_event_reserve_init(),
742 * to its associated userspace DRM file. This function acquires
743 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
746 * Note that the core will take care of unlinking and disarming events when the
747 * corresponding DRM file is closed. Drivers need not worry about whether the
748 * DRM file for this event still exists and can call this function upon
749 * completion of the asynchronous work unconditionally.
751 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
753 unsigned long irqflags;
755 spin_lock_irqsave(&dev->event_lock, irqflags);
756 drm_send_event_locked(dev, e);
757 spin_unlock_irqrestore(&dev->event_lock, irqflags);
759 EXPORT_SYMBOL(drm_send_event);