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 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);
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));
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);
509 * drm_release_noglobal - release method for DRM file
510 * @inode: device inode
511 * @filp: file pointer.
513 * This function may be used by drivers as their &file_operations.release
514 * method. It frees any resources associated with the open file prior to taking
515 * the drm_global_mutex, which then calls the &drm_driver.postclose driver
516 * callback. If this is the last open file for the DRM device also proceeds to
517 * call the &drm_driver.lastclose driver callback.
521 * Always succeeds and returns 0.
523 int drm_release_noglobal(struct inode *inode, struct file *filp)
525 struct drm_file *file_priv = filp->private_data;
526 struct drm_minor *minor = file_priv->minor;
527 struct drm_device *dev = minor->dev;
529 drm_close_helper(filp);
531 if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
533 mutex_unlock(&drm_global_mutex);
536 drm_minor_release(minor);
540 EXPORT_SYMBOL(drm_release_noglobal);
543 * drm_read - read method for DRM file
544 * @filp: file pointer
545 * @buffer: userspace destination pointer for the read
546 * @count: count in bytes to read
547 * @offset: offset to read
549 * This function must be used by drivers as their &file_operations.read
550 * method if they use DRM events for asynchronous signalling to userspace.
551 * Since events are used by the KMS API for vblank and page flip completion this
552 * means all modern display drivers must use it.
554 * @offset is ignored, DRM events are read like a pipe. Polling support is
555 * provided by drm_poll().
557 * This function will only ever read a full event. Therefore userspace must
558 * supply a big enough buffer to fit any event to ensure forward progress. Since
559 * the maximum event space is currently 4K it's recommended to just use that for
564 * Number of bytes read (always aligned to full events, and can be 0) or a
565 * negative error code on failure.
567 ssize_t drm_read(struct file *filp, char __user *buffer,
568 size_t count, loff_t *offset)
570 struct drm_file *file_priv = filp->private_data;
571 struct drm_device *dev = file_priv->minor->dev;
574 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
579 struct drm_pending_event *e = NULL;
581 spin_lock_irq(&dev->event_lock);
582 if (!list_empty(&file_priv->event_list)) {
583 e = list_first_entry(&file_priv->event_list,
584 struct drm_pending_event, link);
585 file_priv->event_space += e->event->length;
588 spin_unlock_irq(&dev->event_lock);
594 if (filp->f_flags & O_NONBLOCK) {
599 mutex_unlock(&file_priv->event_read_lock);
600 ret = wait_event_interruptible(file_priv->event_wait,
601 !list_empty(&file_priv->event_list));
603 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
607 unsigned length = e->event->length;
609 if (length > count - ret) {
611 spin_lock_irq(&dev->event_lock);
612 file_priv->event_space -= length;
613 list_add(&e->link, &file_priv->event_list);
614 spin_unlock_irq(&dev->event_lock);
615 wake_up_interruptible_poll(&file_priv->event_wait,
616 EPOLLIN | EPOLLRDNORM);
620 if (copy_to_user(buffer + ret, e->event, length)) {
630 mutex_unlock(&file_priv->event_read_lock);
634 EXPORT_SYMBOL(drm_read);
637 * drm_poll - poll method for DRM file
638 * @filp: file pointer
639 * @wait: poll waiter table
641 * This function must be used by drivers as their &file_operations.read method
642 * if they use DRM events for asynchronous signalling to userspace. Since
643 * events are used by the KMS API for vblank and page flip completion this means
644 * all modern display drivers must use it.
646 * See also drm_read().
650 * Mask of POLL flags indicating the current status of the file.
652 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
654 struct drm_file *file_priv = filp->private_data;
657 poll_wait(filp, &file_priv->event_wait, wait);
659 if (!list_empty(&file_priv->event_list))
660 mask |= EPOLLIN | EPOLLRDNORM;
664 EXPORT_SYMBOL(drm_poll);
667 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
669 * @file_priv: DRM file private data
670 * @p: tracking structure for the pending event
671 * @e: actual event data to deliver to userspace
673 * This function prepares the passed in event for eventual delivery. If the event
674 * doesn't get delivered (because the IOCTL fails later on, before queuing up
675 * anything) then the even must be cancelled and freed using
676 * drm_event_cancel_free(). Successfully initialized events should be sent out
677 * using drm_send_event() or drm_send_event_locked() to signal completion of the
678 * asynchronous event to userspace.
680 * If callers embedded @p into a larger structure it must be allocated with
681 * kmalloc and @p must be the first member element.
683 * This is the locked version of drm_event_reserve_init() for callers which
684 * already hold &drm_device.event_lock.
688 * 0 on success or a negative error code on failure.
690 int drm_event_reserve_init_locked(struct drm_device *dev,
691 struct drm_file *file_priv,
692 struct drm_pending_event *p,
695 if (file_priv->event_space < e->length)
698 file_priv->event_space -= e->length;
701 list_add(&p->pending_link, &file_priv->pending_event_list);
702 p->file_priv = file_priv;
706 EXPORT_SYMBOL(drm_event_reserve_init_locked);
709 * drm_event_reserve_init - init a DRM event and reserve space for it
711 * @file_priv: DRM file private data
712 * @p: tracking structure for the pending event
713 * @e: actual event data to deliver to userspace
715 * This function prepares the passed in event for eventual delivery. If the event
716 * doesn't get delivered (because the IOCTL fails later on, before queuing up
717 * anything) then the even must be cancelled and freed using
718 * drm_event_cancel_free(). Successfully initialized events should be sent out
719 * using drm_send_event() or drm_send_event_locked() to signal completion of the
720 * asynchronous event to userspace.
722 * If callers embedded @p into a larger structure it must be allocated with
723 * kmalloc and @p must be the first member element.
725 * Callers which already hold &drm_device.event_lock should use
726 * drm_event_reserve_init_locked() instead.
730 * 0 on success or a negative error code on failure.
732 int drm_event_reserve_init(struct drm_device *dev,
733 struct drm_file *file_priv,
734 struct drm_pending_event *p,
740 spin_lock_irqsave(&dev->event_lock, flags);
741 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
742 spin_unlock_irqrestore(&dev->event_lock, flags);
746 EXPORT_SYMBOL(drm_event_reserve_init);
749 * drm_event_cancel_free - free a DRM event and release its space
751 * @p: tracking structure for the pending event
753 * This function frees the event @p initialized with drm_event_reserve_init()
754 * and releases any allocated space. It is used to cancel an event when the
755 * nonblocking operation could not be submitted and needed to be aborted.
757 void drm_event_cancel_free(struct drm_device *dev,
758 struct drm_pending_event *p)
762 spin_lock_irqsave(&dev->event_lock, flags);
764 p->file_priv->event_space += p->event->length;
765 list_del(&p->pending_link);
767 spin_unlock_irqrestore(&dev->event_lock, flags);
770 dma_fence_put(p->fence);
774 EXPORT_SYMBOL(drm_event_cancel_free);
776 static void drm_send_event_helper(struct drm_device *dev,
777 struct drm_pending_event *e, ktime_t timestamp)
779 assert_spin_locked(&dev->event_lock);
782 complete_all(e->completion);
783 e->completion_release(e->completion);
784 e->completion = NULL;
789 dma_fence_signal_timestamp(e->fence, timestamp);
791 dma_fence_signal(e->fence);
792 dma_fence_put(e->fence);
800 list_del(&e->pending_link);
801 list_add_tail(&e->link,
802 &e->file_priv->event_list);
803 wake_up_interruptible_poll(&e->file_priv->event_wait,
804 EPOLLIN | EPOLLRDNORM);
808 * drm_send_event_timestamp_locked - send DRM event to file descriptor
810 * @e: DRM event to deliver
811 * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
814 * This function sends the event @e, initialized with drm_event_reserve_init(),
815 * to its associated userspace DRM file. Callers must already hold
816 * &drm_device.event_lock.
818 * Note that the core will take care of unlinking and disarming events when the
819 * corresponding DRM file is closed. Drivers need not worry about whether the
820 * DRM file for this event still exists and can call this function upon
821 * completion of the asynchronous work unconditionally.
823 void drm_send_event_timestamp_locked(struct drm_device *dev,
824 struct drm_pending_event *e, ktime_t timestamp)
826 drm_send_event_helper(dev, e, timestamp);
828 EXPORT_SYMBOL(drm_send_event_timestamp_locked);
831 * drm_send_event_locked - send DRM event to file descriptor
833 * @e: DRM event to deliver
835 * This function sends the event @e, initialized with drm_event_reserve_init(),
836 * to its associated userspace DRM file. Callers must already hold
837 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
839 * Note that the core will take care of unlinking and disarming events when the
840 * corresponding DRM file is closed. Drivers need not worry about whether the
841 * DRM file for this event still exists and can call this function upon
842 * completion of the asynchronous work unconditionally.
844 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
846 drm_send_event_helper(dev, e, 0);
848 EXPORT_SYMBOL(drm_send_event_locked);
851 * drm_send_event - send DRM event to file descriptor
853 * @e: DRM event to deliver
855 * This function sends the event @e, initialized with drm_event_reserve_init(),
856 * to its associated userspace DRM file. This function acquires
857 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
860 * Note that the core will take care of unlinking and disarming events when the
861 * corresponding DRM file is closed. Drivers need not worry about whether the
862 * DRM file for this event still exists and can call this function upon
863 * completion of the asynchronous work unconditionally.
865 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
867 unsigned long irqflags;
869 spin_lock_irqsave(&dev->event_lock, irqflags);
870 drm_send_event_helper(dev, e, 0);
871 spin_unlock_irqrestore(&dev->event_lock, irqflags);
873 EXPORT_SYMBOL(drm_send_event);
875 static void print_size(struct drm_printer *p, const char *stat,
876 const char *region, u64 sz)
878 const char *units[] = {"", " KiB", " MiB"};
881 for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
884 sz = div_u64(sz, SZ_1K);
887 drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
891 * drm_print_memory_stats - A helper to print memory stats
892 * @p: The printer to print output to
893 * @stats: The collected memory stats
894 * @supported_status: Bitmask of optional stats which are available
895 * @region: The memory region
898 void drm_print_memory_stats(struct drm_printer *p,
899 const struct drm_memory_stats *stats,
900 enum drm_gem_object_status supported_status,
903 print_size(p, "total", region, stats->private + stats->shared);
904 print_size(p, "shared", region, stats->shared);
905 print_size(p, "active", region, stats->active);
907 if (supported_status & DRM_GEM_OBJECT_RESIDENT)
908 print_size(p, "resident", region, stats->resident);
910 if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
911 print_size(p, "purgeable", region, stats->purgeable);
913 EXPORT_SYMBOL(drm_print_memory_stats);
916 * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
917 * @p: the printer to print output to
918 * @file: the DRM file
920 * Helper to iterate over GEM objects with a handle allocated in the specified
923 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
925 struct drm_gem_object *obj;
926 struct drm_memory_stats status = {};
927 enum drm_gem_object_status supported_status;
930 spin_lock(&file->table_lock);
931 idr_for_each_entry (&file->object_idr, obj, id) {
932 enum drm_gem_object_status s = 0;
934 if (obj->funcs && obj->funcs->status) {
935 s = obj->funcs->status(obj);
936 supported_status = DRM_GEM_OBJECT_RESIDENT |
937 DRM_GEM_OBJECT_PURGEABLE;
940 if (obj->handle_count > 1) {
941 status.shared += obj->size;
943 status.private += obj->size;
946 if (s & DRM_GEM_OBJECT_RESIDENT) {
947 status.resident += obj->size;
949 /* If already purged or not yet backed by pages, don't
950 * count it as purgeable:
952 s &= ~DRM_GEM_OBJECT_PURGEABLE;
955 if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
956 status.active += obj->size;
958 /* If still active, don't count as purgeable: */
959 s &= ~DRM_GEM_OBJECT_PURGEABLE;
962 if (s & DRM_GEM_OBJECT_PURGEABLE)
963 status.purgeable += obj->size;
965 spin_unlock(&file->table_lock);
967 drm_print_memory_stats(p, &status, supported_status, "memory");
969 EXPORT_SYMBOL(drm_show_memory_stats);
972 * drm_show_fdinfo - helper for drm file fops
974 * @f: the device file instance
976 * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
977 * process using the GPU. See also &drm_driver.show_fdinfo.
979 * For text output format description please see Documentation/gpu/drm-usage-stats.rst
981 void drm_show_fdinfo(struct seq_file *m, struct file *f)
983 struct drm_file *file = f->private_data;
984 struct drm_device *dev = file->minor->dev;
985 struct drm_printer p = drm_seq_file_printer(m);
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 if (dev->driver->show_fdinfo)
999 dev->driver->show_fdinfo(&p, file);
1001 EXPORT_SYMBOL(drm_show_fdinfo);
1004 * mock_drm_getfile - Create a new struct file for the drm device
1005 * @minor: drm minor to wrap (e.g. #drm_device.primary)
1006 * @flags: file creation mode (O_RDWR etc)
1008 * This create a new struct file that wraps a DRM file context around a
1009 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
1010 * invoking userspace. The struct file may be operated on using its f_op
1011 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
1012 * to userspace facing functions as an internal/anonymous client.
1015 * Pointer to newly created struct file, ERR_PTR on failure.
1017 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
1019 struct drm_device *dev = minor->dev;
1020 struct drm_file *priv;
1023 priv = drm_file_alloc(minor);
1025 return ERR_CAST(priv);
1027 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
1029 drm_file_free(priv);
1033 /* Everyone shares a single global address space */
1034 file->f_mapping = dev->anon_inode->i_mapping;
1041 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);