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/export.h>
37 #include <linux/file.h>
38 #include <linux/module.h>
39 #include <linux/pci.h>
40 #include <linux/poll.h>
41 #include <linux/slab.h>
42 #include <linux/vga_switcheroo.h>
44 #include <drm/drm_client_event.h>
45 #include <drm/drm_drv.h>
46 #include <drm/drm_file.h>
47 #include <drm/drm_gem.h>
48 #include <drm/drm_print.h>
49 #include <drm/drm_debugfs.h>
51 #include "drm_crtc_internal.h"
52 #include "drm_internal.h"
54 /* from BKL pushdown */
55 DEFINE_MUTEX(drm_global_mutex);
57 bool drm_dev_needs_global_mutex(struct drm_device *dev)
60 * The deprecated ->load callback must be called after the driver is
61 * already registered. This means such drivers rely on the BKL to make
62 * sure an open can't proceed until the driver is actually fully set up.
63 * Similar hilarity holds for the unload callback.
65 if (dev->driver->load || dev->driver->unload)
72 * DOC: file operations
74 * Drivers must define the file operations structure that forms the DRM
75 * userspace API entry point, even though most of those operations are
76 * implemented in the DRM core. The resulting &struct file_operations must be
77 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
78 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
79 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
80 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
81 * that require 32/64 bit compatibility support must provide their own
82 * &file_operations.compat_ioctl handler that processes private ioctls and calls
83 * drm_compat_ioctl() for core ioctls.
85 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
86 * events are a generic and extensible means to send asynchronous events to
87 * userspace through the file descriptor. They are used to send vblank event and
88 * page flip completions by the KMS API. But drivers can also use it for their
89 * own needs, e.g. to signal completion of rendering.
91 * For the driver-side event interface see drm_event_reserve_init() and
92 * drm_send_event() as the main starting points.
94 * The memory mapping implementation will vary depending on how the driver
95 * manages memory. For GEM-based drivers this is drm_gem_mmap().
97 * No other file operations are supported by the DRM userspace API. Overall the
98 * following is an example &file_operations structure::
100 * static const example_drm_fops = {
101 * .owner = THIS_MODULE,
103 * .release = drm_release,
104 * .unlocked_ioctl = drm_ioctl,
105 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
108 * .mmap = drm_gem_mmap,
111 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
112 * DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS() macro to make this
115 * The driver's &file_operations must be stored in &drm_driver.fops.
117 * For driver-private IOCTL handling see the more detailed discussion in
118 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
122 * drm_file_alloc - allocate file context
123 * @minor: minor to allocate on
125 * This allocates a new DRM file context. It is not linked into any context and
126 * can be used by the caller freely. Note that the context keeps a pointer to
127 * @minor, so it must be freed before @minor is.
130 * Pointer to newly allocated context, ERR_PTR on failure.
132 struct drm_file *drm_file_alloc(struct drm_minor *minor)
134 static atomic64_t ident = ATOMIC64_INIT(0);
135 struct drm_device *dev = minor->dev;
136 struct drm_file *file;
139 file = kzalloc_obj(*file);
141 return ERR_PTR(-ENOMEM);
143 /* Get a unique identifier for fdinfo: */
144 file->client_id = atomic64_inc_return(&ident);
145 rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
148 /* for compatibility root is always authenticated */
149 file->authenticated = capable(CAP_SYS_ADMIN);
151 INIT_LIST_HEAD(&file->lhead);
152 INIT_LIST_HEAD(&file->fbs);
153 mutex_init(&file->fbs_lock);
154 INIT_LIST_HEAD(&file->blobs);
155 INIT_LIST_HEAD(&file->pending_event_list);
156 INIT_LIST_HEAD(&file->event_list);
157 init_waitqueue_head(&file->event_wait);
158 file->event_space = 4096; /* set aside 4k for event buffer */
160 spin_lock_init(&file->master_lookup_lock);
161 mutex_init(&file->event_read_lock);
162 mutex_init(&file->client_name_lock);
164 if (drm_core_check_feature(dev, DRIVER_GEM))
165 drm_gem_open(dev, file);
167 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
168 drm_syncobj_open(file);
170 drm_prime_init_file_private(&file->prime);
172 if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
173 drm_debugfs_clients_add(file);
175 if (dev->driver->open) {
176 ret = dev->driver->open(dev, file);
178 goto out_prime_destroy;
184 drm_prime_destroy_file_private(&file->prime);
185 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
186 drm_syncobj_release(file);
187 if (drm_core_check_feature(dev, DRIVER_GEM))
188 drm_gem_release(dev, file);
190 if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
191 drm_debugfs_clients_remove(file);
193 put_pid(rcu_access_pointer(file->pid));
199 static void drm_events_release(struct drm_file *file_priv)
201 struct drm_device *dev = file_priv->minor->dev;
202 struct drm_pending_event *e, *et;
205 spin_lock_irqsave(&dev->event_lock, flags);
207 /* Unlink pending events */
208 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
210 list_del(&e->pending_link);
214 /* Remove unconsumed events */
215 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
220 spin_unlock_irqrestore(&dev->event_lock, flags);
224 * drm_file_free - free file context
225 * @file: context to free, or NULL
227 * This destroys and deallocates a DRM file context previously allocated via
228 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
229 * before calling this.
231 * If NULL is passed, this is a no-op.
233 void drm_file_free(struct drm_file *file)
235 struct drm_device *dev;
241 dev = file->minor->dev;
243 drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n",
244 current->comm, task_pid_nr(current),
245 (long)old_encode_dev(file->minor->kdev->devt),
246 atomic_read(&dev->open_count));
248 if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
249 drm_debugfs_clients_remove(file);
251 drm_events_release(file);
253 if (drm_core_check_feature(dev, DRIVER_MODESET) &&
254 drm_dev_enter(dev, &idx)) {
255 drm_fb_release(file);
256 drm_property_destroy_user_blobs(dev, file);
260 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
261 drm_syncobj_release(file);
263 if (drm_core_check_feature(dev, DRIVER_GEM))
264 drm_gem_release(dev, file);
266 if (drm_is_primary_client(file))
267 drm_master_release(file);
269 if (dev->driver->postclose)
270 dev->driver->postclose(dev, file);
272 drm_prime_destroy_file_private(&file->prime);
274 WARN_ON(!list_empty(&file->event_list));
276 put_pid(rcu_access_pointer(file->pid));
278 mutex_destroy(&file->client_name_lock);
279 kfree(file->client_name);
284 static void drm_close_helper(struct file *filp)
286 struct drm_file *file_priv = filp->private_data;
287 struct drm_device *dev = file_priv->minor->dev;
289 mutex_lock(&dev->filelist_mutex);
290 list_del(&file_priv->lhead);
291 mutex_unlock(&dev->filelist_mutex);
293 drm_file_free(file_priv);
297 * Check whether DRI will run on this CPU.
299 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
301 static int drm_cpu_valid(void)
303 #if defined(__sparc__) && !defined(__sparc_v9__)
304 return 0; /* No cmpxchg before v9 sparc. */
310 * Called whenever a process opens a drm node
312 * \param filp file pointer.
313 * \param minor acquired minor-object.
314 * \return zero on success or a negative number on failure.
316 * Creates and initializes a drm_file structure for the file private data in \p
317 * filp and add it into the double linked list in \p dev.
319 int drm_open_helper(struct file *filp, struct drm_minor *minor)
321 struct drm_device *dev = minor->dev;
322 struct drm_file *priv;
325 if (filp->f_flags & O_EXCL)
326 return -EBUSY; /* No exclusive opens */
327 if (!drm_cpu_valid())
329 if (dev->switch_power_state != DRM_SWITCH_POWER_ON &&
330 dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
332 if (WARN_ON_ONCE(!(filp->f_op->fop_flags & FOP_UNSIGNED_OFFSET)))
335 drm_dbg_core(dev, "comm=\"%s\", pid=%d, minor=%d\n",
336 current->comm, task_pid_nr(current), minor->index);
338 priv = drm_file_alloc(minor);
340 return PTR_ERR(priv);
342 if (drm_is_primary_client(priv)) {
343 ret = drm_master_open(priv);
350 filp->private_data = priv;
353 mutex_lock(&dev->filelist_mutex);
354 list_add(&priv->lhead, &dev->filelist);
355 mutex_unlock(&dev->filelist_mutex);
361 * drm_open - open method for DRM file
362 * @inode: device inode
363 * @filp: file pointer.
365 * This function must be used by drivers as their &file_operations.open method.
366 * It looks up the correct DRM device and instantiates all the per-file
367 * resources for it. It also calls the &drm_driver.open driver callback.
370 * 0 on success or negative errno value on failure.
372 int drm_open(struct inode *inode, struct file *filp)
374 struct drm_device *dev;
375 struct drm_minor *minor;
378 minor = drm_minor_acquire(&drm_minors_xa, iminor(inode));
380 return PTR_ERR(minor);
383 if (drm_dev_needs_global_mutex(dev))
384 mutex_lock(&drm_global_mutex);
386 atomic_fetch_inc(&dev->open_count);
388 /* share address_space across all char-devs of a single device */
389 filp->f_mapping = dev->anon_inode->i_mapping;
391 retcode = drm_open_helper(filp, minor);
395 if (drm_dev_needs_global_mutex(dev))
396 mutex_unlock(&drm_global_mutex);
401 atomic_dec(&dev->open_count);
402 if (drm_dev_needs_global_mutex(dev))
403 mutex_unlock(&drm_global_mutex);
404 drm_minor_release(minor);
407 EXPORT_SYMBOL(drm_open);
409 static void drm_lastclose(struct drm_device *dev)
411 drm_client_dev_restore(dev, false);
413 if (dev_is_pci(dev->dev))
414 vga_switcheroo_process_delayed_switch();
418 * drm_release - release method for DRM file
419 * @inode: device inode
420 * @filp: file pointer.
422 * This function must be used by drivers as their &file_operations.release
423 * method. It frees any resources associated with the open file. If this
424 * is the last open file for the DRM device, it also restores the active
425 * in-kernel DRM client.
428 * Always succeeds and returns 0.
430 int drm_release(struct inode *inode, struct file *filp)
432 struct drm_file *file_priv = filp->private_data;
433 struct drm_minor *minor = file_priv->minor;
434 struct drm_device *dev = minor->dev;
436 if (drm_dev_needs_global_mutex(dev))
437 mutex_lock(&drm_global_mutex);
439 drm_dbg_core(dev, "open_count = %d\n", atomic_read(&dev->open_count));
441 drm_close_helper(filp);
443 if (atomic_dec_and_test(&dev->open_count))
446 if (drm_dev_needs_global_mutex(dev))
447 mutex_unlock(&drm_global_mutex);
449 drm_minor_release(minor);
453 EXPORT_SYMBOL(drm_release);
455 void drm_file_update_pid(struct drm_file *filp)
457 struct drm_device *dev;
458 struct pid *pid, *old;
461 * Master nodes need to keep the original ownership in order for
462 * drm_master_check_perm to keep working correctly. (See comment in
465 if (filp->was_master)
468 pid = task_tgid(current);
471 * Quick unlocked check since the model is a single handover followed by
472 * exclusive repeated use.
474 if (pid == rcu_access_pointer(filp->pid))
477 dev = filp->minor->dev;
478 mutex_lock(&dev->filelist_mutex);
480 old = rcu_replace_pointer(filp->pid, pid, 1);
481 mutex_unlock(&dev->filelist_mutex);
488 * drm_release_noglobal - release method for DRM file
489 * @inode: device inode
490 * @filp: file pointer.
492 * This function may be used by drivers as their &file_operations.release
493 * method. It frees any resources associated with the open file prior to taking
494 * the drm_global_mutex. If this is the last open file for the DRM device, it
495 * then restores the active in-kernel DRM client.
498 * Always succeeds and returns 0.
500 int drm_release_noglobal(struct inode *inode, struct file *filp)
502 struct drm_file *file_priv = filp->private_data;
503 struct drm_minor *minor = file_priv->minor;
504 struct drm_device *dev = minor->dev;
506 drm_close_helper(filp);
508 if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
510 mutex_unlock(&drm_global_mutex);
513 drm_minor_release(minor);
517 EXPORT_SYMBOL(drm_release_noglobal);
520 * drm_read - read method for DRM file
521 * @filp: file pointer
522 * @buffer: userspace destination pointer for the read
523 * @count: count in bytes to read
524 * @offset: offset to read
526 * This function must be used by drivers as their &file_operations.read
527 * method if they use DRM events for asynchronous signalling to userspace.
528 * Since events are used by the KMS API for vblank and page flip completion this
529 * means all modern display drivers must use it.
531 * @offset is ignored, DRM events are read like a pipe. Polling support is
532 * provided by drm_poll().
534 * This function will only ever read a full event. Therefore userspace must
535 * supply a big enough buffer to fit any event to ensure forward progress. Since
536 * the maximum event space is currently 4K it's recommended to just use that for
540 * Number of bytes read (always aligned to full events, and can be 0) or a
541 * negative error code on failure.
543 ssize_t drm_read(struct file *filp, char __user *buffer,
544 size_t count, loff_t *offset)
546 struct drm_file *file_priv = filp->private_data;
547 struct drm_device *dev = file_priv->minor->dev;
550 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
555 struct drm_pending_event *e = NULL;
557 spin_lock_irq(&dev->event_lock);
558 if (!list_empty(&file_priv->event_list)) {
559 e = list_first_entry(&file_priv->event_list,
560 struct drm_pending_event, link);
561 file_priv->event_space += e->event->length;
564 spin_unlock_irq(&dev->event_lock);
570 if (filp->f_flags & O_NONBLOCK) {
575 mutex_unlock(&file_priv->event_read_lock);
576 ret = wait_event_interruptible(file_priv->event_wait,
577 !list_empty(&file_priv->event_list));
579 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
583 unsigned length = e->event->length;
585 if (length > count - ret) {
587 spin_lock_irq(&dev->event_lock);
588 file_priv->event_space -= length;
589 list_add(&e->link, &file_priv->event_list);
590 spin_unlock_irq(&dev->event_lock);
591 wake_up_interruptible_poll(&file_priv->event_wait,
592 EPOLLIN | EPOLLRDNORM);
596 if (copy_to_user(buffer + ret, e->event, length)) {
606 mutex_unlock(&file_priv->event_read_lock);
610 EXPORT_SYMBOL(drm_read);
613 * drm_poll - poll method for DRM file
614 * @filp: file pointer
615 * @wait: poll waiter table
617 * This function must be used by drivers as their &file_operations.read method
618 * if they use DRM events for asynchronous signalling to userspace. Since
619 * events are used by the KMS API for vblank and page flip completion this means
620 * all modern display drivers must use it.
622 * See also drm_read().
625 * Mask of POLL flags indicating the current status of the file.
627 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
629 struct drm_file *file_priv = filp->private_data;
632 poll_wait(filp, &file_priv->event_wait, wait);
634 if (!list_empty(&file_priv->event_list))
635 mask |= EPOLLIN | EPOLLRDNORM;
639 EXPORT_SYMBOL(drm_poll);
642 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
644 * @file_priv: DRM file private data
645 * @p: tracking structure for the pending event
646 * @e: actual event data to deliver to userspace
648 * This function prepares the passed in event for eventual delivery. If the event
649 * doesn't get delivered (because the IOCTL fails later on, before queuing up
650 * anything) then the even must be cancelled and freed using
651 * drm_event_cancel_free(). Successfully initialized events should be sent out
652 * using drm_send_event() or drm_send_event_locked() to signal completion of the
653 * asynchronous event to userspace.
655 * If callers embedded @p into a larger structure it must be allocated with
656 * kmalloc and @p must be the first member element.
658 * This is the locked version of drm_event_reserve_init() for callers which
659 * already hold &drm_device.event_lock.
662 * 0 on success or a negative error code on failure.
664 int drm_event_reserve_init_locked(struct drm_device *dev,
665 struct drm_file *file_priv,
666 struct drm_pending_event *p,
669 if (file_priv->event_space < e->length)
672 file_priv->event_space -= e->length;
675 list_add(&p->pending_link, &file_priv->pending_event_list);
676 p->file_priv = file_priv;
680 EXPORT_SYMBOL(drm_event_reserve_init_locked);
683 * drm_event_reserve_init - init a DRM event and reserve space for it
685 * @file_priv: DRM file private data
686 * @p: tracking structure for the pending event
687 * @e: actual event data to deliver to userspace
689 * This function prepares the passed in event for eventual delivery. If the event
690 * doesn't get delivered (because the IOCTL fails later on, before queuing up
691 * anything) then the even must be cancelled and freed using
692 * drm_event_cancel_free(). Successfully initialized events should be sent out
693 * using drm_send_event() or drm_send_event_locked() to signal completion of the
694 * asynchronous event to userspace.
696 * If callers embedded @p into a larger structure it must be allocated with
697 * kmalloc and @p must be the first member element.
699 * Callers which already hold &drm_device.event_lock should use
700 * drm_event_reserve_init_locked() instead.
703 * 0 on success or a negative error code on failure.
705 int drm_event_reserve_init(struct drm_device *dev,
706 struct drm_file *file_priv,
707 struct drm_pending_event *p,
713 spin_lock_irqsave(&dev->event_lock, flags);
714 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
715 spin_unlock_irqrestore(&dev->event_lock, flags);
719 EXPORT_SYMBOL(drm_event_reserve_init);
722 * drm_event_cancel_free - free a DRM event and release its space
724 * @p: tracking structure for the pending event
726 * This function frees the event @p initialized with drm_event_reserve_init()
727 * and releases any allocated space. It is used to cancel an event when the
728 * nonblocking operation could not be submitted and needed to be aborted.
730 void drm_event_cancel_free(struct drm_device *dev,
731 struct drm_pending_event *p)
735 spin_lock_irqsave(&dev->event_lock, flags);
737 p->file_priv->event_space += p->event->length;
738 list_del(&p->pending_link);
740 spin_unlock_irqrestore(&dev->event_lock, flags);
743 dma_fence_put(p->fence);
747 EXPORT_SYMBOL(drm_event_cancel_free);
749 static void drm_send_event_helper(struct drm_device *dev,
750 struct drm_pending_event *e, ktime_t timestamp)
752 assert_spin_locked(&dev->event_lock);
755 complete_all(e->completion);
756 e->completion_release(e->completion);
757 e->completion = NULL;
762 dma_fence_signal_timestamp(e->fence, timestamp);
764 dma_fence_signal(e->fence);
765 dma_fence_put(e->fence);
773 list_del(&e->pending_link);
774 list_add_tail(&e->link,
775 &e->file_priv->event_list);
776 wake_up_interruptible_poll(&e->file_priv->event_wait,
777 EPOLLIN | EPOLLRDNORM);
781 * drm_send_event_timestamp_locked - send DRM event to file descriptor
783 * @e: DRM event to deliver
784 * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
787 * This function sends the event @e, initialized with drm_event_reserve_init(),
788 * to its associated userspace DRM file. Callers must already hold
789 * &drm_device.event_lock.
791 * Note that the core will take care of unlinking and disarming events when the
792 * corresponding DRM file is closed. Drivers need not worry about whether the
793 * DRM file for this event still exists and can call this function upon
794 * completion of the asynchronous work unconditionally.
796 void drm_send_event_timestamp_locked(struct drm_device *dev,
797 struct drm_pending_event *e, ktime_t timestamp)
799 drm_send_event_helper(dev, e, timestamp);
801 EXPORT_SYMBOL(drm_send_event_timestamp_locked);
804 * drm_send_event_locked - send DRM event to file descriptor
806 * @e: DRM event to deliver
808 * This function sends the event @e, initialized with drm_event_reserve_init(),
809 * to its associated userspace DRM file. Callers must already hold
810 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
812 * Note that the core will take care of unlinking and disarming events when the
813 * corresponding DRM file is closed. Drivers need not worry about whether the
814 * DRM file for this event still exists and can call this function upon
815 * completion of the asynchronous work unconditionally.
817 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
819 drm_send_event_helper(dev, e, 0);
821 EXPORT_SYMBOL(drm_send_event_locked);
824 * drm_send_event - send DRM event to file descriptor
826 * @e: DRM event to deliver
828 * This function sends the event @e, initialized with drm_event_reserve_init(),
829 * to its associated userspace DRM file. This function acquires
830 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
833 * Note that the core will take care of unlinking and disarming events when the
834 * corresponding DRM file is closed. Drivers need not worry about whether the
835 * DRM file for this event still exists and can call this function upon
836 * completion of the asynchronous work unconditionally.
838 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
840 unsigned long irqflags;
842 spin_lock_irqsave(&dev->event_lock, irqflags);
843 drm_send_event_helper(dev, e, 0);
844 spin_unlock_irqrestore(&dev->event_lock, irqflags);
846 EXPORT_SYMBOL(drm_send_event);
848 void drm_fdinfo_print_size(struct drm_printer *p,
854 const char *units[] = {"", " KiB", " MiB"};
857 for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
858 if (sz == 0 || !IS_ALIGNED(sz, SZ_1K))
860 sz = div_u64(sz, SZ_1K);
863 drm_printf(p, "%s-%s-%s:\t%llu%s\n",
864 prefix, stat, region, sz, units[u]);
866 EXPORT_SYMBOL(drm_fdinfo_print_size);
868 int drm_memory_stats_is_zero(const struct drm_memory_stats *stats)
870 return (stats->shared == 0 &&
871 stats->private == 0 &&
872 stats->resident == 0 &&
873 stats->purgeable == 0 &&
876 EXPORT_SYMBOL(drm_memory_stats_is_zero);
879 * drm_print_memory_stats - A helper to print memory stats
880 * @p: The printer to print output to
881 * @stats: The collected memory stats
882 * @supported_status: Bitmask of optional stats which are available
883 * @region: The memory region
886 void drm_print_memory_stats(struct drm_printer *p,
887 const struct drm_memory_stats *stats,
888 enum drm_gem_object_status supported_status,
891 const char *prefix = "drm";
893 drm_fdinfo_print_size(p, prefix, "total", region,
894 stats->private + stats->shared);
895 drm_fdinfo_print_size(p, prefix, "shared", region, stats->shared);
897 if (supported_status & DRM_GEM_OBJECT_ACTIVE)
898 drm_fdinfo_print_size(p, prefix, "active", region, stats->active);
900 if (supported_status & DRM_GEM_OBJECT_RESIDENT)
901 drm_fdinfo_print_size(p, prefix, "resident", region,
904 if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
905 drm_fdinfo_print_size(p, prefix, "purgeable", region,
908 EXPORT_SYMBOL(drm_print_memory_stats);
911 * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
912 * @p: the printer to print output to
913 * @file: the DRM file
915 * Helper to iterate over GEM objects with a handle allocated in the specified
918 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
920 struct drm_gem_object *obj;
921 struct drm_memory_stats status = {};
922 enum drm_gem_object_status supported_status = 0;
925 spin_lock(&file->table_lock);
926 idr_for_each_entry (&file->object_idr, obj, id) {
927 enum drm_gem_object_status s = 0;
928 size_t add_size = (obj->funcs && obj->funcs->rss) ?
929 obj->funcs->rss(obj) : obj->size;
931 if (obj->funcs && obj->funcs->status) {
932 s = obj->funcs->status(obj);
933 supported_status |= s;
936 if (drm_gem_object_is_shared_for_memory_stats(obj))
937 status.shared += obj->size;
939 status.private += obj->size;
941 if (s & DRM_GEM_OBJECT_RESIDENT) {
942 status.resident += add_size;
944 /* If already purged or not yet backed by pages, don't
945 * count it as purgeable:
947 s &= ~DRM_GEM_OBJECT_PURGEABLE;
950 if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
951 status.active += add_size;
952 supported_status |= DRM_GEM_OBJECT_ACTIVE;
954 /* If still active, don't count as purgeable: */
955 s &= ~DRM_GEM_OBJECT_PURGEABLE;
958 if (s & DRM_GEM_OBJECT_PURGEABLE)
959 status.purgeable += add_size;
961 spin_unlock(&file->table_lock);
963 drm_print_memory_stats(p, &status, supported_status, "memory");
965 EXPORT_SYMBOL(drm_show_memory_stats);
968 * drm_show_fdinfo - helper for drm file fops
970 * @f: the device file instance
972 * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
973 * process using the GPU. See also &drm_driver.show_fdinfo.
975 * For text output format description please see Documentation/gpu/drm-usage-stats.rst
977 void drm_show_fdinfo(struct seq_file *m, struct file *f)
979 struct drm_file *file = f->private_data;
980 struct drm_device *dev = file->minor->dev;
981 struct drm_printer p = drm_seq_file_printer(m);
984 if (!drm_dev_enter(dev, &idx))
987 drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
988 drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
990 if (dev_is_pci(dev->dev)) {
991 struct pci_dev *pdev = to_pci_dev(dev->dev);
993 drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
994 pci_domain_nr(pdev->bus), pdev->bus->number,
995 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
998 mutex_lock(&file->client_name_lock);
999 if (file->client_name)
1000 drm_printf(&p, "drm-client-name:\t%s\n", file->client_name);
1001 mutex_unlock(&file->client_name_lock);
1003 if (dev->driver->show_fdinfo)
1004 dev->driver->show_fdinfo(&p, file);
1008 EXPORT_SYMBOL(drm_show_fdinfo);
1011 * drm_file_err - log process name, pid and client_name associated with a drm_file
1012 * @file_priv: context of interest for process name and pid
1013 * @fmt: printf() like format string
1015 * Helper function for clients which needs to log process details such
1016 * as name and pid etc along with user logs.
1018 void drm_file_err(struct drm_file *file_priv, const char *fmt, ...)
1021 struct va_format vaf;
1023 struct task_struct *task;
1024 struct drm_device *dev = file_priv->minor->dev;
1026 va_start(args, fmt);
1030 mutex_lock(&file_priv->client_name_lock);
1032 pid = rcu_dereference(file_priv->pid);
1033 task = pid_task(pid, PIDTYPE_TGID);
1035 drm_err(dev, "comm: %s pid: %d client-id:%llu client: %s ... %pV",
1036 task ? task->comm : "Unset",
1037 task ? task->pid : 0, file_priv->client_id,
1038 file_priv->client_name ?: "Unset", &vaf);
1042 mutex_unlock(&file_priv->client_name_lock);
1044 EXPORT_SYMBOL(drm_file_err);
1047 * mock_drm_getfile - Create a new struct file for the drm device
1048 * @minor: drm minor to wrap (e.g. #drm_device.primary)
1049 * @flags: file creation mode (O_RDWR etc)
1051 * This create a new struct file that wraps a DRM file context around a
1052 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
1053 * invoking userspace. The struct file may be operated on using its f_op
1054 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
1055 * to userspace facing functions as an internal/anonymous client.
1058 * Pointer to newly created struct file, ERR_PTR on failure.
1060 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
1062 struct drm_device *dev = minor->dev;
1063 struct drm_file *priv;
1066 priv = drm_file_alloc(minor);
1068 return ERR_CAST(priv);
1070 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
1072 drm_file_free(priv);
1076 /* Everyone shares a single global address space */
1077 file->f_mapping = dev->anon_inode->i_mapping;
1084 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);