2 * \author Rickard E. (Rik) Faith <faith@valinux.com>
3 * \author Daryll Strauss <daryll@valinux.com>
4 * \author Gareth Hughes <gareth@valinux.com>
8 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
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/anon_inodes.h>
35 #include <linux/dma-fence.h>
36 #include <linux/file.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/poll.h>
40 #include <linux/slab.h>
42 #include <drm/drm_client.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_file.h>
45 #include <drm/drm_gem.h>
46 #include <drm/drm_print.h>
48 #include "drm_crtc_internal.h"
49 #include "drm_internal.h"
50 #include "drm_legacy.h"
52 /* from BKL pushdown */
53 DEFINE_MUTEX(drm_global_mutex);
55 bool drm_dev_needs_global_mutex(struct drm_device *dev)
58 * Legacy drivers rely on all kinds of BKL locking semantics, don't
59 * bother. They also still need BKL locking for their ioctls, so better
62 if (drm_core_check_feature(dev, DRIVER_LEGACY))
66 * The deprecated ->load callback must be called after the driver is
67 * already registered. This means such drivers rely on the BKL to make
68 * sure an open can't proceed until the driver is actually fully set up.
69 * Similar hilarity holds for the unload callback.
71 if (dev->driver->load || dev->driver->unload)
75 * Drivers with the lastclose callback assume that it's synchronized
76 * against concurrent opens, which again needs the BKL. The proper fix
77 * is to use the drm_client infrastructure with proper locking for each
80 if (dev->driver->lastclose)
87 * DOC: file operations
89 * Drivers must define the file operations structure that forms the DRM
90 * userspace API entry point, even though most of those operations are
91 * implemented in the DRM core. The resulting &struct file_operations must be
92 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
93 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
94 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
95 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
96 * that require 32/64 bit compatibility support must provide their own
97 * &file_operations.compat_ioctl handler that processes private ioctls and calls
98 * drm_compat_ioctl() for core ioctls.
100 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
101 * events are a generic and extensible means to send asynchronous events to
102 * userspace through the file descriptor. They are used to send vblank event and
103 * page flip completions by the KMS API. But drivers can also use it for their
104 * own needs, e.g. to signal completion of rendering.
106 * For the driver-side event interface see drm_event_reserve_init() and
107 * drm_send_event() as the main starting points.
109 * The memory mapping implementation will vary depending on how the driver
110 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
111 * function, modern drivers should use one of the provided memory-manager
112 * specific implementations. For GEM-based drivers this is drm_gem_mmap().
114 * No other file operations are supported by the DRM userspace API. Overall the
115 * following is an example &file_operations structure::
117 * static const example_drm_fops = {
118 * .owner = THIS_MODULE,
120 * .release = drm_release,
121 * .unlocked_ioctl = drm_ioctl,
122 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
125 * .llseek = no_llseek,
126 * .mmap = drm_gem_mmap,
129 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
130 * DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS() macro to make this
133 * The driver's &file_operations must be stored in &drm_driver.fops.
135 * For driver-private IOCTL handling see the more detailed discussion in
136 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
140 * drm_file_alloc - allocate file context
141 * @minor: minor to allocate on
143 * This allocates a new DRM file context. It is not linked into any context and
144 * can be used by the caller freely. Note that the context keeps a pointer to
145 * @minor, so it must be freed before @minor is.
148 * Pointer to newly allocated context, ERR_PTR on failure.
150 struct drm_file *drm_file_alloc(struct drm_minor *minor)
152 static atomic64_t ident = ATOMIC_INIT(0);
153 struct drm_device *dev = minor->dev;
154 struct drm_file *file;
157 file = kzalloc(sizeof(*file), GFP_KERNEL);
159 return ERR_PTR(-ENOMEM);
161 /* Get a unique identifier for fdinfo: */
162 file->client_id = atomic64_inc_return(&ident);
163 rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
166 /* for compatibility root is always authenticated */
167 file->authenticated = capable(CAP_SYS_ADMIN);
169 INIT_LIST_HEAD(&file->lhead);
170 INIT_LIST_HEAD(&file->fbs);
171 mutex_init(&file->fbs_lock);
172 INIT_LIST_HEAD(&file->blobs);
173 INIT_LIST_HEAD(&file->pending_event_list);
174 INIT_LIST_HEAD(&file->event_list);
175 init_waitqueue_head(&file->event_wait);
176 file->event_space = 4096; /* set aside 4k for event buffer */
178 spin_lock_init(&file->master_lookup_lock);
179 mutex_init(&file->event_read_lock);
181 if (drm_core_check_feature(dev, DRIVER_GEM))
182 drm_gem_open(dev, file);
184 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
185 drm_syncobj_open(file);
187 drm_prime_init_file_private(&file->prime);
189 if (dev->driver->open) {
190 ret = dev->driver->open(dev, file);
192 goto out_prime_destroy;
198 drm_prime_destroy_file_private(&file->prime);
199 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
200 drm_syncobj_release(file);
201 if (drm_core_check_feature(dev, DRIVER_GEM))
202 drm_gem_release(dev, file);
203 put_pid(rcu_access_pointer(file->pid));
209 static void drm_events_release(struct drm_file *file_priv)
211 struct drm_device *dev = file_priv->minor->dev;
212 struct drm_pending_event *e, *et;
215 spin_lock_irqsave(&dev->event_lock, flags);
217 /* Unlink pending events */
218 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
220 list_del(&e->pending_link);
224 /* Remove unconsumed events */
225 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
230 spin_unlock_irqrestore(&dev->event_lock, flags);
234 * drm_file_free - free file context
235 * @file: context to free, or NULL
237 * This destroys and deallocates a DRM file context previously allocated via
238 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
239 * before calling this.
241 * If NULL is passed, this is a no-op.
243 void drm_file_free(struct drm_file *file)
245 struct drm_device *dev;
250 dev = file->minor->dev;
252 drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n",
253 current->comm, task_pid_nr(current),
254 (long)old_encode_dev(file->minor->kdev->devt),
255 atomic_read(&dev->open_count));
257 #ifdef CONFIG_DRM_LEGACY
258 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
259 dev->driver->preclose)
260 dev->driver->preclose(dev, file);
263 if (drm_core_check_feature(dev, DRIVER_LEGACY))
264 drm_legacy_lock_release(dev, file->filp);
266 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
267 drm_legacy_reclaim_buffers(dev, file);
269 drm_events_release(file);
271 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
272 drm_fb_release(file);
273 drm_property_destroy_user_blobs(dev, file);
276 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
277 drm_syncobj_release(file);
279 if (drm_core_check_feature(dev, DRIVER_GEM))
280 drm_gem_release(dev, file);
282 drm_legacy_ctxbitmap_flush(dev, file);
284 if (drm_is_primary_client(file))
285 drm_master_release(file);
287 if (dev->driver->postclose)
288 dev->driver->postclose(dev, file);
290 drm_prime_destroy_file_private(&file->prime);
292 WARN_ON(!list_empty(&file->event_list));
294 put_pid(rcu_access_pointer(file->pid));
298 static void drm_close_helper(struct file *filp)
300 struct drm_file *file_priv = filp->private_data;
301 struct drm_device *dev = file_priv->minor->dev;
303 mutex_lock(&dev->filelist_mutex);
304 list_del(&file_priv->lhead);
305 mutex_unlock(&dev->filelist_mutex);
307 drm_file_free(file_priv);
311 * Check whether DRI will run on this CPU.
313 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
315 static int drm_cpu_valid(void)
317 #if defined(__sparc__) && !defined(__sparc_v9__)
318 return 0; /* No cmpxchg before v9 sparc. */
324 * Called whenever a process opens a drm node
326 * \param filp file pointer.
327 * \param minor acquired minor-object.
328 * \return zero on success or a negative number on failure.
330 * Creates and initializes a drm_file structure for the file private data in \p
331 * filp and add it into the double linked list in \p dev.
333 int drm_open_helper(struct file *filp, struct drm_minor *minor)
335 struct drm_device *dev = minor->dev;
336 struct drm_file *priv;
339 if (filp->f_flags & O_EXCL)
340 return -EBUSY; /* No exclusive opens */
341 if (!drm_cpu_valid())
343 if (dev->switch_power_state != DRM_SWITCH_POWER_ON &&
344 dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
347 drm_dbg_core(dev, "comm=\"%s\", pid=%d, minor=%d\n",
348 current->comm, task_pid_nr(current), minor->index);
350 priv = drm_file_alloc(minor);
352 return PTR_ERR(priv);
354 if (drm_is_primary_client(priv)) {
355 ret = drm_master_open(priv);
362 filp->private_data = priv;
363 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
366 mutex_lock(&dev->filelist_mutex);
367 list_add(&priv->lhead, &dev->filelist);
368 mutex_unlock(&dev->filelist_mutex);
370 #ifdef CONFIG_DRM_LEGACY
376 struct pci_dev *pci_dev;
378 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
380 dev->hose = pci_dev->sysdata;
381 pci_dev_put(pci_dev);
384 struct pci_bus *b = list_entry(pci_root_buses.next,
385 struct pci_bus, node);
387 dev->hose = b->sysdata;
397 * drm_open - open method for DRM file
398 * @inode: device inode
399 * @filp: file pointer.
401 * This function must be used by drivers as their &file_operations.open method.
402 * It looks up the correct DRM device and instantiates all the per-file
403 * resources for it. It also calls the &drm_driver.open driver callback.
407 * 0 on success or negative errno value on failure.
409 int drm_open(struct inode *inode, struct file *filp)
411 struct drm_device *dev;
412 struct drm_minor *minor;
416 minor = drm_minor_acquire(iminor(inode));
418 return PTR_ERR(minor);
421 if (drm_dev_needs_global_mutex(dev))
422 mutex_lock(&drm_global_mutex);
424 if (!atomic_fetch_inc(&dev->open_count))
427 /* share address_space across all char-devs of a single device */
428 filp->f_mapping = dev->anon_inode->i_mapping;
430 retcode = drm_open_helper(filp, minor);
434 retcode = drm_legacy_setup(dev);
436 drm_close_helper(filp);
441 if (drm_dev_needs_global_mutex(dev))
442 mutex_unlock(&drm_global_mutex);
447 atomic_dec(&dev->open_count);
448 if (drm_dev_needs_global_mutex(dev))
449 mutex_unlock(&drm_global_mutex);
450 drm_minor_release(minor);
453 EXPORT_SYMBOL(drm_open);
455 void drm_lastclose(struct drm_device * dev)
457 drm_dbg_core(dev, "\n");
459 if (dev->driver->lastclose)
460 dev->driver->lastclose(dev);
461 drm_dbg_core(dev, "driver lastclose completed\n");
463 if (drm_core_check_feature(dev, DRIVER_LEGACY))
464 drm_legacy_dev_reinit(dev);
466 drm_client_dev_restore(dev);
470 * drm_release - release method for DRM file
471 * @inode: device inode
472 * @filp: file pointer.
474 * This function must be used by drivers as their &file_operations.release
475 * method. It frees any resources associated with the open file, and calls the
476 * &drm_driver.postclose driver callback. If this is the last open file for the
477 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
481 * Always succeeds and returns 0.
483 int drm_release(struct inode *inode, struct file *filp)
485 struct drm_file *file_priv = filp->private_data;
486 struct drm_minor *minor = file_priv->minor;
487 struct drm_device *dev = minor->dev;
489 if (drm_dev_needs_global_mutex(dev))
490 mutex_lock(&drm_global_mutex);
492 drm_dbg_core(dev, "open_count = %d\n", atomic_read(&dev->open_count));
494 drm_close_helper(filp);
496 if (atomic_dec_and_test(&dev->open_count))
499 if (drm_dev_needs_global_mutex(dev))
500 mutex_unlock(&drm_global_mutex);
502 drm_minor_release(minor);
506 EXPORT_SYMBOL(drm_release);
508 void drm_file_update_pid(struct drm_file *filp)
510 struct drm_device *dev;
511 struct pid *pid, *old;
514 * Master nodes need to keep the original ownership in order for
515 * drm_master_check_perm to keep working correctly. (See comment in
518 if (filp->was_master)
521 pid = task_tgid(current);
524 * Quick unlocked check since the model is a single handover followed by
525 * exclusive repeated use.
527 if (pid == rcu_access_pointer(filp->pid))
530 dev = filp->minor->dev;
531 mutex_lock(&dev->filelist_mutex);
532 old = rcu_replace_pointer(filp->pid, pid, 1);
533 mutex_unlock(&dev->filelist_mutex);
543 * drm_release_noglobal - release method for DRM file
544 * @inode: device inode
545 * @filp: file pointer.
547 * This function may be used by drivers as their &file_operations.release
548 * method. It frees any resources associated with the open file prior to taking
549 * the drm_global_mutex, which then calls the &drm_driver.postclose driver
550 * callback. If this is the last open file for the DRM device also proceeds to
551 * call the &drm_driver.lastclose driver callback.
555 * Always succeeds and returns 0.
557 int drm_release_noglobal(struct inode *inode, struct file *filp)
559 struct drm_file *file_priv = filp->private_data;
560 struct drm_minor *minor = file_priv->minor;
561 struct drm_device *dev = minor->dev;
563 drm_close_helper(filp);
565 if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
567 mutex_unlock(&drm_global_mutex);
570 drm_minor_release(minor);
574 EXPORT_SYMBOL(drm_release_noglobal);
577 * drm_read - read method for DRM file
578 * @filp: file pointer
579 * @buffer: userspace destination pointer for the read
580 * @count: count in bytes to read
581 * @offset: offset to read
583 * This function must be used by drivers as their &file_operations.read
584 * method if they use DRM events for asynchronous signalling to userspace.
585 * Since events are used by the KMS API for vblank and page flip completion this
586 * means all modern display drivers must use it.
588 * @offset is ignored, DRM events are read like a pipe. Polling support is
589 * provided by drm_poll().
591 * This function will only ever read a full event. Therefore userspace must
592 * supply a big enough buffer to fit any event to ensure forward progress. Since
593 * the maximum event space is currently 4K it's recommended to just use that for
598 * Number of bytes read (always aligned to full events, and can be 0) or a
599 * negative error code on failure.
601 ssize_t drm_read(struct file *filp, char __user *buffer,
602 size_t count, loff_t *offset)
604 struct drm_file *file_priv = filp->private_data;
605 struct drm_device *dev = file_priv->minor->dev;
608 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
613 struct drm_pending_event *e = NULL;
615 spin_lock_irq(&dev->event_lock);
616 if (!list_empty(&file_priv->event_list)) {
617 e = list_first_entry(&file_priv->event_list,
618 struct drm_pending_event, link);
619 file_priv->event_space += e->event->length;
622 spin_unlock_irq(&dev->event_lock);
628 if (filp->f_flags & O_NONBLOCK) {
633 mutex_unlock(&file_priv->event_read_lock);
634 ret = wait_event_interruptible(file_priv->event_wait,
635 !list_empty(&file_priv->event_list));
637 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
641 unsigned length = e->event->length;
643 if (length > count - ret) {
645 spin_lock_irq(&dev->event_lock);
646 file_priv->event_space -= length;
647 list_add(&e->link, &file_priv->event_list);
648 spin_unlock_irq(&dev->event_lock);
649 wake_up_interruptible_poll(&file_priv->event_wait,
650 EPOLLIN | EPOLLRDNORM);
654 if (copy_to_user(buffer + ret, e->event, length)) {
664 mutex_unlock(&file_priv->event_read_lock);
668 EXPORT_SYMBOL(drm_read);
671 * drm_poll - poll method for DRM file
672 * @filp: file pointer
673 * @wait: poll waiter table
675 * This function must be used by drivers as their &file_operations.read method
676 * if they use DRM events for asynchronous signalling to userspace. Since
677 * events are used by the KMS API for vblank and page flip completion this means
678 * all modern display drivers must use it.
680 * See also drm_read().
684 * Mask of POLL flags indicating the current status of the file.
686 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
688 struct drm_file *file_priv = filp->private_data;
691 poll_wait(filp, &file_priv->event_wait, wait);
693 if (!list_empty(&file_priv->event_list))
694 mask |= EPOLLIN | EPOLLRDNORM;
698 EXPORT_SYMBOL(drm_poll);
701 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
703 * @file_priv: DRM file private data
704 * @p: tracking structure for the pending event
705 * @e: actual event data to deliver to userspace
707 * This function prepares the passed in event for eventual delivery. If the event
708 * doesn't get delivered (because the IOCTL fails later on, before queuing up
709 * anything) then the even must be cancelled and freed using
710 * drm_event_cancel_free(). Successfully initialized events should be sent out
711 * using drm_send_event() or drm_send_event_locked() to signal completion of the
712 * asynchronous event to userspace.
714 * If callers embedded @p into a larger structure it must be allocated with
715 * kmalloc and @p must be the first member element.
717 * This is the locked version of drm_event_reserve_init() for callers which
718 * already hold &drm_device.event_lock.
722 * 0 on success or a negative error code on failure.
724 int drm_event_reserve_init_locked(struct drm_device *dev,
725 struct drm_file *file_priv,
726 struct drm_pending_event *p,
729 if (file_priv->event_space < e->length)
732 file_priv->event_space -= e->length;
735 list_add(&p->pending_link, &file_priv->pending_event_list);
736 p->file_priv = file_priv;
740 EXPORT_SYMBOL(drm_event_reserve_init_locked);
743 * drm_event_reserve_init - init a DRM event and reserve space for it
745 * @file_priv: DRM file private data
746 * @p: tracking structure for the pending event
747 * @e: actual event data to deliver to userspace
749 * This function prepares the passed in event for eventual delivery. If the event
750 * doesn't get delivered (because the IOCTL fails later on, before queuing up
751 * anything) then the even must be cancelled and freed using
752 * drm_event_cancel_free(). Successfully initialized events should be sent out
753 * using drm_send_event() or drm_send_event_locked() to signal completion of the
754 * asynchronous event to userspace.
756 * If callers embedded @p into a larger structure it must be allocated with
757 * kmalloc and @p must be the first member element.
759 * Callers which already hold &drm_device.event_lock should use
760 * drm_event_reserve_init_locked() instead.
764 * 0 on success or a negative error code on failure.
766 int drm_event_reserve_init(struct drm_device *dev,
767 struct drm_file *file_priv,
768 struct drm_pending_event *p,
774 spin_lock_irqsave(&dev->event_lock, flags);
775 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
776 spin_unlock_irqrestore(&dev->event_lock, flags);
780 EXPORT_SYMBOL(drm_event_reserve_init);
783 * drm_event_cancel_free - free a DRM event and release its space
785 * @p: tracking structure for the pending event
787 * This function frees the event @p initialized with drm_event_reserve_init()
788 * and releases any allocated space. It is used to cancel an event when the
789 * nonblocking operation could not be submitted and needed to be aborted.
791 void drm_event_cancel_free(struct drm_device *dev,
792 struct drm_pending_event *p)
796 spin_lock_irqsave(&dev->event_lock, flags);
798 p->file_priv->event_space += p->event->length;
799 list_del(&p->pending_link);
801 spin_unlock_irqrestore(&dev->event_lock, flags);
804 dma_fence_put(p->fence);
808 EXPORT_SYMBOL(drm_event_cancel_free);
810 static void drm_send_event_helper(struct drm_device *dev,
811 struct drm_pending_event *e, ktime_t timestamp)
813 assert_spin_locked(&dev->event_lock);
816 complete_all(e->completion);
817 e->completion_release(e->completion);
818 e->completion = NULL;
823 dma_fence_signal_timestamp(e->fence, timestamp);
825 dma_fence_signal(e->fence);
826 dma_fence_put(e->fence);
834 list_del(&e->pending_link);
835 list_add_tail(&e->link,
836 &e->file_priv->event_list);
837 wake_up_interruptible_poll(&e->file_priv->event_wait,
838 EPOLLIN | EPOLLRDNORM);
842 * drm_send_event_timestamp_locked - send DRM event to file descriptor
844 * @e: DRM event to deliver
845 * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
848 * This function sends the event @e, initialized with drm_event_reserve_init(),
849 * to its associated userspace DRM file. Callers must already hold
850 * &drm_device.event_lock.
852 * Note that the core will take care of unlinking and disarming events when the
853 * corresponding DRM file is closed. Drivers need not worry about whether the
854 * DRM file for this event still exists and can call this function upon
855 * completion of the asynchronous work unconditionally.
857 void drm_send_event_timestamp_locked(struct drm_device *dev,
858 struct drm_pending_event *e, ktime_t timestamp)
860 drm_send_event_helper(dev, e, timestamp);
862 EXPORT_SYMBOL(drm_send_event_timestamp_locked);
865 * drm_send_event_locked - send DRM event to file descriptor
867 * @e: DRM event to deliver
869 * This function sends the event @e, initialized with drm_event_reserve_init(),
870 * to its associated userspace DRM file. Callers must already hold
871 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
873 * Note that the core will take care of unlinking and disarming events when the
874 * corresponding DRM file is closed. Drivers need not worry about whether the
875 * DRM file for this event still exists and can call this function upon
876 * completion of the asynchronous work unconditionally.
878 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
880 drm_send_event_helper(dev, e, 0);
882 EXPORT_SYMBOL(drm_send_event_locked);
885 * drm_send_event - send DRM event to file descriptor
887 * @e: DRM event to deliver
889 * This function sends the event @e, initialized with drm_event_reserve_init(),
890 * to its associated userspace DRM file. This function acquires
891 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
894 * Note that the core will take care of unlinking and disarming events when the
895 * corresponding DRM file is closed. Drivers need not worry about whether the
896 * DRM file for this event still exists and can call this function upon
897 * completion of the asynchronous work unconditionally.
899 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
901 unsigned long irqflags;
903 spin_lock_irqsave(&dev->event_lock, irqflags);
904 drm_send_event_helper(dev, e, 0);
905 spin_unlock_irqrestore(&dev->event_lock, irqflags);
907 EXPORT_SYMBOL(drm_send_event);
909 static void print_size(struct drm_printer *p, const char *stat,
910 const char *region, u64 sz)
912 const char *units[] = {"", " KiB", " MiB"};
915 for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
918 sz = div_u64(sz, SZ_1K);
921 drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
925 * drm_print_memory_stats - A helper to print memory stats
926 * @p: The printer to print output to
927 * @stats: The collected memory stats
928 * @supported_status: Bitmask of optional stats which are available
929 * @region: The memory region
932 void drm_print_memory_stats(struct drm_printer *p,
933 const struct drm_memory_stats *stats,
934 enum drm_gem_object_status supported_status,
937 print_size(p, "total", region, stats->private + stats->shared);
938 print_size(p, "shared", region, stats->shared);
939 print_size(p, "active", region, stats->active);
941 if (supported_status & DRM_GEM_OBJECT_RESIDENT)
942 print_size(p, "resident", region, stats->resident);
944 if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
945 print_size(p, "purgeable", region, stats->purgeable);
947 EXPORT_SYMBOL(drm_print_memory_stats);
950 * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
951 * @p: the printer to print output to
952 * @file: the DRM file
954 * Helper to iterate over GEM objects with a handle allocated in the specified
957 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
959 struct drm_gem_object *obj;
960 struct drm_memory_stats status = {};
961 enum drm_gem_object_status supported_status;
964 spin_lock(&file->table_lock);
965 idr_for_each_entry (&file->object_idr, obj, id) {
966 enum drm_gem_object_status s = 0;
968 if (obj->funcs && obj->funcs->status) {
969 s = obj->funcs->status(obj);
970 supported_status = DRM_GEM_OBJECT_RESIDENT |
971 DRM_GEM_OBJECT_PURGEABLE;
974 if (obj->handle_count > 1) {
975 status.shared += obj->size;
977 status.private += obj->size;
980 if (s & DRM_GEM_OBJECT_RESIDENT) {
981 status.resident += obj->size;
983 /* If already purged or not yet backed by pages, don't
984 * count it as purgeable:
986 s &= ~DRM_GEM_OBJECT_PURGEABLE;
989 if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
990 status.active += obj->size;
992 /* If still active, don't count as purgeable: */
993 s &= ~DRM_GEM_OBJECT_PURGEABLE;
996 if (s & DRM_GEM_OBJECT_PURGEABLE)
997 status.purgeable += obj->size;
999 spin_unlock(&file->table_lock);
1001 drm_print_memory_stats(p, &status, supported_status, "memory");
1003 EXPORT_SYMBOL(drm_show_memory_stats);
1006 * drm_show_fdinfo - helper for drm file fops
1008 * @f: the device file instance
1010 * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
1011 * process using the GPU. See also &drm_driver.show_fdinfo.
1013 * For text output format description please see Documentation/gpu/drm-usage-stats.rst
1015 void drm_show_fdinfo(struct seq_file *m, struct file *f)
1017 struct drm_file *file = f->private_data;
1018 struct drm_device *dev = file->minor->dev;
1019 struct drm_printer p = drm_seq_file_printer(m);
1021 drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
1022 drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
1024 if (dev_is_pci(dev->dev)) {
1025 struct pci_dev *pdev = to_pci_dev(dev->dev);
1027 drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
1028 pci_domain_nr(pdev->bus), pdev->bus->number,
1029 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1032 if (dev->driver->show_fdinfo)
1033 dev->driver->show_fdinfo(&p, file);
1035 EXPORT_SYMBOL(drm_show_fdinfo);
1038 * mock_drm_getfile - Create a new struct file for the drm device
1039 * @minor: drm minor to wrap (e.g. #drm_device.primary)
1040 * @flags: file creation mode (O_RDWR etc)
1042 * This create a new struct file that wraps a DRM file context around a
1043 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
1044 * invoking userspace. The struct file may be operated on using its f_op
1045 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
1046 * to userspace facing functions as an internal/anonymous client.
1049 * Pointer to newly created struct file, ERR_PTR on failure.
1051 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
1053 struct drm_device *dev = minor->dev;
1054 struct drm_file *priv;
1057 priv = drm_file_alloc(minor);
1059 return ERR_CAST(priv);
1061 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
1063 drm_file_free(priv);
1067 /* Everyone shares a single global address space */
1068 file->f_mapping = dev->anon_inode->i_mapping;
1075 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);