xref: /linux/Documentation/gpu/drm-uapi.rst (revision c3fb1fb9e65fa6a108b4d19c61bdcb47fd4fe180)
1.. Copyright 2020 DisplayLink (UK) Ltd.
2
3===================
4Userland interfaces
5===================
6
7The DRM core exports several interfaces to applications, generally
8intended to be used through corresponding libdrm wrapper functions. In
9addition, drivers export device-specific interfaces for use by userspace
10drivers & device-aware applications through ioctls and sysfs files.
11
12External interfaces include: memory mapping, context management, DMA
13operations, AGP management, vblank control, fence management, memory
14management, and output management.
15
16Cover generic ioctls and sysfs layout here. We only need high-level
17info, since man pages should cover the rest.
18
19libdrm Device Lookup
20====================
21
22.. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
23   :doc: getunique and setversion story
24
25
26.. _drm_primary_node:
27
28Primary Nodes, DRM Master and Authentication
29============================================
30
31.. kernel-doc:: drivers/gpu/drm/drm_auth.c
32   :doc: master and authentication
33
34.. kernel-doc:: drivers/gpu/drm/drm_auth.c
35   :export:
36
37.. kernel-doc:: include/drm/drm_auth.h
38   :internal:
39
40
41.. _drm_leasing:
42
43DRM Display Resource Leasing
44============================
45
46.. kernel-doc:: drivers/gpu/drm/drm_lease.c
47   :doc: drm leasing
48
49Open-Source Userspace Requirements
50==================================
51
52The DRM subsystem has stricter requirements than most other kernel subsystems on
53what the userspace side for new uAPI needs to look like. This section here
54explains what exactly those requirements are, and why they exist.
55
56The short summary is that any addition of DRM uAPI requires corresponding
57open-sourced userspace patches, and those patches must be reviewed and ready for
58merging into a suitable and canonical upstream project.
59
60GFX devices (both display and render/GPU side) are really complex bits of
61hardware, with userspace and kernel by necessity having to work together really
62closely.  The interfaces, for rendering and modesetting, must be extremely wide
63and flexible, and therefore it is almost always impossible to precisely define
64them for every possible corner case. This in turn makes it really practically
65infeasible to differentiate between behaviour that's required by userspace, and
66which must not be changed to avoid regressions, and behaviour which is only an
67accidental artifact of the current implementation.
68
69Without access to the full source code of all userspace users that means it
70becomes impossible to change the implementation details, since userspace could
71depend upon the accidental behaviour of the current implementation in minute
72details. And debugging such regressions without access to source code is pretty
73much impossible. As a consequence this means:
74
75- The Linux kernel's "no regression" policy holds in practice only for
76  open-source userspace of the DRM subsystem. DRM developers are perfectly fine
77  if closed-source blob drivers in userspace use the same uAPI as the open
78  drivers, but they must do so in the exact same way as the open drivers.
79  Creative (ab)use of the interfaces will, and in the past routinely has, lead
80  to breakage.
81
82- Any new userspace interface must have an open-source implementation as
83  demonstration vehicle.
84
85The other reason for requiring open-source userspace is uAPI review. Since the
86kernel and userspace parts of a GFX stack must work together so closely, code
87review can only assess whether a new interface achieves its goals by looking at
88both sides. Making sure that the interface indeed covers the use-case fully
89leads to a few additional requirements:
90
91- The open-source userspace must not be a toy/test application, but the real
92  thing. Specifically it needs to handle all the usual error and corner cases.
93  These are often the places where new uAPI falls apart and hence essential to
94  assess the fitness of a proposed interface.
95
96- The userspace side must be fully reviewed and tested to the standards of that
97  userspace project. For e.g. mesa this means piglit testcases and review on the
98  mailing list. This is again to ensure that the new interface actually gets the
99  job done.  The userspace-side reviewer should also provide an Acked-by on the
100  kernel uAPI patch indicating that they believe the proposed uAPI is sound and
101  sufficiently documented and validated for userspace's consumption.
102
103- The userspace patches must be against the canonical upstream, not some vendor
104  fork. This is to make sure that no one cheats on the review and testing
105  requirements by doing a quick fork.
106
107- The kernel patch can only be merged after all the above requirements are met,
108  but it **must** be merged to either drm-next or drm-misc-next **before** the
109  userspace patches land. uAPI always flows from the kernel, doing things the
110  other way round risks divergence of the uAPI definitions and header files.
111
112These are fairly steep requirements, but have grown out from years of shared
113pain and experience with uAPI added hastily, and almost always regretted about
114just as fast. GFX devices change really fast, requiring a paradigm shift and
115entire new set of uAPI interfaces every few years at least. Together with the
116Linux kernel's guarantee to keep existing userspace running for 10+ years this
117is already rather painful for the DRM subsystem, with multiple different uAPIs
118for the same thing co-existing. If we add a few more complete mistakes into the
119mix every year it would be entirely unmanageable.
120
121The DRM subsystem has however no concern with independent closed-source
122userspace implementations. To officialize that position, the DRM uAPI headers
123are covered by the MIT license.
124
125.. _drm_render_node:
126
127Render nodes
128============
129
130DRM core provides multiple character-devices for user-space to use.
131Depending on which device is opened, user-space can perform a different
132set of operations (mainly ioctls). The primary node is always created
133and called card<num>. Additionally, a currently unused control node,
134called controlD<num> is also created. The primary node provides all
135legacy operations and historically was the only interface used by
136userspace. With KMS, the control node was introduced. However, the
137planned KMS control interface has never been written and so the control
138node stays unused to date.
139
140With the increased use of offscreen renderers and GPGPU applications,
141clients no longer require running compositors or graphics servers to
142make use of a GPU. But the DRM API required unprivileged clients to
143authenticate to a DRM-Master prior to getting GPU access. To avoid this
144step and to grant clients GPU access without authenticating, render
145nodes were introduced. Render nodes solely serve render clients, that
146is, no modesetting or privileged ioctls can be issued on render nodes.
147Only non-global rendering commands are allowed. If a driver supports
148render nodes, it must advertise it via the DRIVER_RENDER DRM driver
149capability. If not supported, the primary node must be used for render
150clients together with the legacy drmAuth authentication procedure.
151
152If a driver advertises render node support, DRM core will create a
153separate render node called renderD<num>. There will be one render node
154per device. No ioctls except PRIME-related ioctls will be allowed on
155this node. Especially GEM_OPEN will be explicitly prohibited. For a
156complete list of driver-independent ioctls that can be used on render
157nodes, see the ioctls marked DRM_RENDER_ALLOW in drm_ioctl.c  Render
158nodes are designed to avoid the buffer-leaks, which occur if clients
159guess the flink names or mmap offsets on the legacy interface.
160Additionally to this basic interface, drivers must mark their
161driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
162clients can use them. Driver authors must be careful not to allow any
163privileged ioctls on render nodes.
164
165With render nodes, user-space can now control access to the render node
166via basic file-system access-modes. A running graphics server which
167authenticates clients on the privileged primary/legacy node is no longer
168required. Instead, a client can open the render node and is immediately
169granted GPU access. Communication between clients (or servers) is done
170via PRIME. FLINK from render node to legacy node is not supported. New
171clients must not use the insecure FLINK interface.
172
173Besides dropping all modeset/global ioctls, render nodes also drop the
174DRM-Master concept. There is no reason to associate render clients with
175a DRM-Master as they are independent of any graphics server. Besides,
176they must work without any running master, anyway. Drivers must be able
177to run without a master object if they support render nodes. If, on the
178other hand, a driver requires shared state between clients which is
179visible to user-space and accessible beyond open-file boundaries, they
180cannot support render nodes.
181
182Device Hot-Unplug
183=================
184
185.. note::
186   The following is the plan. Implementation is not there yet
187   (2020 May).
188
189Graphics devices (display and/or render) may be connected via USB (e.g.
190display adapters or docking stations) or Thunderbolt (e.g. eGPU). An end
191user is able to hot-unplug this kind of devices while they are being
192used, and expects that the very least the machine does not crash. Any
193damage from hot-unplugging a DRM device needs to be limited as much as
194possible and userspace must be given the chance to handle it if it wants
195to. Ideally, unplugging a DRM device still lets a desktop continue to
196run, but that is going to need explicit support throughout the whole
197graphics stack: from kernel and userspace drivers, through display
198servers, via window system protocols, and in applications and libraries.
199
200Other scenarios that should lead to the same are: unrecoverable GPU
201crash, PCI device disappearing off the bus, or forced unbind of a driver
202from the physical device.
203
204In other words, from userspace perspective everything needs to keep on
205working more or less, until userspace stops using the disappeared DRM
206device and closes it completely. Userspace will learn of the device
207disappearance from the device removed uevent, ioctls returning ENODEV
208(or driver-specific ioctls returning driver-specific things), or open()
209returning ENXIO.
210
211Only after userspace has closed all relevant DRM device and dmabuf file
212descriptors and removed all mmaps, the DRM driver can tear down its
213instance for the device that no longer exists. If the same physical
214device somehow comes back in the mean time, it shall be a new DRM
215device.
216
217Similar to PIDs, chardev minor numbers are not recycled immediately. A
218new DRM device always picks the next free minor number compared to the
219previous one allocated, and wraps around when minor numbers are
220exhausted.
221
222The goal raises at least the following requirements for the kernel and
223drivers.
224
225Requirements for KMS UAPI
226-------------------------
227
228- KMS connectors must change their status to disconnected.
229
230- Legacy modesets and pageflips, and atomic commits, both real and
231  TEST_ONLY, and any other ioctls either fail with ENODEV or fake
232  success.
233
234- Pending non-blocking KMS operations deliver the DRM events userspace
235  is expecting. This applies also to ioctls that faked success.
236
237- open() on a device node whose underlying device has disappeared will
238  fail with ENXIO.
239
240- Attempting to create a DRM lease on a disappeared DRM device will
241  fail with ENODEV. Existing DRM leases remain and work as listed
242  above.
243
244Requirements for Render and Cross-Device UAPI
245---------------------------------------------
246
247- All GPU jobs that can no longer run must have their fences
248  force-signalled to avoid inflicting hangs on userspace.
249  The associated error code is ENODEV.
250
251- Some userspace APIs already define what should happen when the device
252  disappears (OpenGL, GL ES: `GL_KHR_robustness`_; `Vulkan`_:
253  VK_ERROR_DEVICE_LOST; etc.). DRM drivers are free to implement this
254  behaviour the way they see best, e.g. returning failures in
255  driver-specific ioctls and handling those in userspace drivers, or
256  rely on uevents, and so on.
257
258- dmabuf which point to memory that has disappeared will either fail to
259  import with ENODEV or continue to be successfully imported if it would
260  have succeeded before the disappearance. See also about memory maps
261  below for already imported dmabufs.
262
263- Attempting to import a dmabuf to a disappeared device will either fail
264  with ENODEV or succeed if it would have succeeded without the
265  disappearance.
266
267- open() on a device node whose underlying device has disappeared will
268  fail with ENXIO.
269
270.. _GL_KHR_robustness: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_robustness.txt
271.. _Vulkan: https://www.khronos.org/vulkan/
272
273Requirements for Memory Maps
274----------------------------
275
276Memory maps have further requirements that apply to both existing maps
277and maps created after the device has disappeared. If the underlying
278memory disappears, the map is created or modified such that reads and
279writes will still complete successfully but the result is undefined.
280This applies to both userspace mmap()'d memory and memory pointed to by
281dmabuf which might be mapped to other devices (cross-device dmabuf
282imports).
283
284Raising SIGBUS is not an option, because userspace cannot realistically
285handle it. Signal handlers are global, which makes them extremely
286difficult to use correctly from libraries like those that Mesa produces.
287Signal handlers are not composable, you can't have different handlers
288for GPU1 and GPU2 from different vendors, and a third handler for
289mmapped regular files. Threads cause additional pain with signal
290handling as well.
291
292Device reset
293============
294
295The GPU stack is really complex and is prone to errors, from hardware bugs,
296faulty applications and everything in between the many layers. Some errors
297require resetting the device in order to make the device usable again. This
298section describes the expectations for DRM and usermode drivers when a
299device resets and how to propagate the reset status.
300
301Device resets can not be disabled without tainting the kernel, which can lead to
302hanging the entire kernel through shrinkers/mmu_notifiers. Userspace role in
303device resets is to propagate the message to the application and apply any
304special policy for blocking guilty applications, if any. Corollary is that
305debugging a hung GPU context require hardware support to be able to preempt such
306a GPU context while it's stopped.
307
308Kernel Mode Driver
309------------------
310
311The KMD is responsible for checking if the device needs a reset, and to perform
312it as needed. Usually a hang is detected when a job gets stuck executing.
313
314Propagation of errors to userspace has proven to be tricky since it goes in
315the opposite direction of the usual flow of commands. Because of this vendor
316independent error handling was added to the &dma_fence object, this way drivers
317can add an error code to their fences before signaling them. See function
318dma_fence_set_error() on how to do this and for examples of error codes to use.
319
320The DRM scheduler also allows setting error codes on all pending fences when
321hardware submissions are restarted after an reset. Error codes are also
322forwarded from the hardware fence to the scheduler fence to bubble up errors
323to the higher levels of the stack and eventually userspace.
324
325Fence errors can be queried by userspace through the generic SYNC_IOC_FILE_INFO
326IOCTL as well as through driver specific interfaces.
327
328Additional to setting fence errors drivers should also keep track of resets per
329context, the DRM scheduler provides the drm_sched_entity_error() function as
330helper for this use case. After a reset, KMD should reject new command
331submissions for affected contexts.
332
333User Mode Driver
334----------------
335
336After command submission, UMD should check if the submission was accepted or
337rejected. After a reset, KMD should reject submissions, and UMD can issue an
338ioctl to the KMD to check the reset status, and this can be checked more often
339if the UMD requires it. After detecting a reset, UMD will then proceed to report
340it to the application using the appropriate API error code, as explained in the
341section below about robustness.
342
343Robustness
344----------
345
346The only way to try to keep a graphical API context working after a reset is if
347it complies with the robustness aspects of the graphical API that it is using.
348
349Graphical APIs provide ways to applications to deal with device resets. However,
350there is no guarantee that the app will use such features correctly, and a
351userspace that doesn't support robust interfaces (like a non-robust
352OpenGL context or API without any robustness support like libva) leave the
353robustness handling entirely to the userspace driver. There is no strong
354community consensus on what the userspace driver should do in that case,
355since all reasonable approaches have some clear downsides.
356
357OpenGL
358~~~~~~
359
360Apps using OpenGL should use the available robust interfaces, like the
361extension ``GL_ARB_robustness`` (or ``GL_EXT_robustness`` for OpenGL ES). This
362interface tells if a reset has happened, and if so, all the context state is
363considered lost and the app proceeds by creating new ones. There's no consensus
364on what to do to if robustness is not in use.
365
366Vulkan
367~~~~~~
368
369Apps using Vulkan should check for ``VK_ERROR_DEVICE_LOST`` for submissions.
370This error code means, among other things, that a device reset has happened and
371it needs to recreate the contexts to keep going.
372
373Reporting causes of resets
374--------------------------
375
376Apart from propagating the reset through the stack so apps can recover, it's
377really useful for driver developers to learn more about what caused the reset in
378the first place. For this, drivers can make use of devcoredump to store relevant
379information about the reset and send device wedged event with ``none`` recovery
380method (as explained in "Device Wedging" chapter) to notify userspace, so this
381information can be collected and added to user bug reports.
382
383Device Wedging
384==============
385
386Drivers can optionally make use of device wedged event (implemented as
387drm_dev_wedged_event() in DRM subsystem), which notifies userspace of 'wedged'
388(hanged/unusable) state of the DRM device through a uevent. This is useful
389especially in cases where the device is no longer operating as expected and has
390become unrecoverable from driver context. Purpose of this implementation is to
391provide drivers a generic way to recover the device with the help of userspace
392intervention, without taking any drastic measures (like resetting or
393re-enumerating the full bus, on which the underlying physical device is sitting)
394in the driver.
395
396A 'wedged' device is basically a device that is declared dead by the driver
397after exhausting all possible attempts to recover it from driver context. The
398uevent is the notification that is sent to userspace along with a hint about
399what could possibly be attempted to recover the device from userspace and bring
400it back to usable state. Different drivers may have different ideas of a
401'wedged' device depending on hardware implementation of the underlying physical
402device, and hence the vendor agnostic nature of the event. It is up to the
403drivers to decide when they see the need for device recovery and how they want
404to recover from the available methods.
405
406Driver prerequisites
407--------------------
408
409The driver, before opting for recovery, needs to make sure that the 'wedged'
410device doesn't harm the system as a whole by taking care of the prerequisites.
411Necessary actions must include disabling DMA to system memory as well as any
412communication channels with other devices. Further, the driver must ensure
413that all dma_fences are signalled and any device state that the core kernel
414might depend on is cleaned up. All existing mmaps should be invalidated and
415page faults should be redirected to a dummy page. Once the event is sent, the
416device must be kept in 'wedged' state until the recovery is performed. New
417accesses to the device (IOCTLs) should be rejected, preferably with an error
418code that resembles the type of failure the device has encountered. This will
419signify the reason for wedging, which can be reported to the application if
420needed.
421
422Recovery
423--------
424
425Current implementation defines four recovery methods, out of which, drivers
426can use any one, multiple or none. Method(s) of choice will be sent in the
427uevent environment as ``WEDGED=<method1>[,..,<methodN>]`` in order of less to
428more side-effects. See the section `Vendor Specific Recovery`_
429for ``WEDGED=vendor-specific``. If driver is unsure about recovery or
430method is unknown, ``WEDGED=unknown`` will be sent instead.
431
432Userspace consumers can parse this event and attempt recovery as per the
433following expectations.
434
435    =============== ========================================
436    Recovery method Consumer expectations
437    =============== ========================================
438    none            optional telemetry collection
439    rebind          unbind + bind driver
440    bus-reset       unbind + bus reset/re-enumeration + bind
441    vendor-specific vendor specific recovery method
442    unknown         consumer policy
443    =============== ========================================
444
445No Recovery
446-----------
447
448Here ``WEDGED=none`` signifies that no recovery is expected from the consumer
449but it can still try to gather telemetry information (devcoredump, syslog) for
450debug purpose in order to root cause the hang. This is useful because the first
451hang is usually the most critical one which can result in consequential hangs
452or complete wedging.
453
454Vendor Specific Recovery
455------------------------
456
457When ``WEDGED=vendor-specific`` is sent, it indicates that the device requires
458a recovery procedure specific to the hardware vendor and is not one of the
459standardized approaches.
460
461``WEDGED=vendor-specific`` may be used to indicate different cases within a
462single vendor driver, each requiring a distinct recovery procedure.
463In such scenarios, the vendor driver must provide comprehensive documentation
464that describes each case, include additional hints to identify specific case and
465outline the corresponding recovery procedure. The documentation includes:
466
467Case - A list of all cases that sends the ``WEDGED=vendor-specific`` recovery method.
468
469Hints - Additional Information to assist the userspace consumer in identifying and
470differentiating between different cases. This can be exposed through sysfs, debugfs,
471traces, dmesg etc.
472
473Recovery Procedure - Clear instructions and guidance for recovering each case.
474This may include userspace scripts, tools needed for the recovery procedure.
475
476It is the responsibility of the admin/userspace consumer to identify the case and
477verify additional identification hints before attempting a recovery procedure.
478
479Example: If the device uses the Xe driver, then userspace consumer should refer to
480:ref:`Xe Device Wedging <xe-device-wedging>` for the detailed documentation.
481
482Task information
483----------------
484
485The information about which application (if any) was involved in the device
486wedging is useful for userspace if they want to notify the user about what
487happened (e.g. the compositor display a message to the user "The <task name>
488caused a graphical error and the system recovered") or to implement policies
489(e.g. the daemon may "ban" an task that keeps resetting the device). If the task
490information is available, the uevent will display as ``PID=<pid>`` and
491``TASK=<task name>``. Otherwise, ``PID`` and ``TASK`` will not appear in the
492event string.
493
494The reliability of this information is driver and hardware specific, and should
495be taken with a caution regarding it's precision. To have a big picture of what
496really happened, the devcoredump file provides much more detailed information
497about the device state and about the event.
498
499Consumer prerequisites
500----------------------
501
502It is the responsibility of the consumer to make sure that the device or its
503resources are not in use by any process before attempting recovery. With IOCTLs
504erroring out, all device memory should be unmapped and file descriptors should
505be closed to prevent leaks or undefined behaviour. The idea here is to clear the
506device of all user context beforehand and set the stage for a clean recovery.
507
508For ``WEDGED=vendor-specific`` recovery method, it is the responsibility of the
509consumer to check the driver documentation and the usecase before attempting
510a recovery.
511
512Example - rebind
513----------------
514
515Udev rule::
516
517    SUBSYSTEM=="drm", ENV{WEDGED}=="rebind", DEVPATH=="*/drm/card[0-9]",
518    RUN+="/path/to/rebind.sh $env{DEVPATH}"
519
520Recovery script::
521
522    #!/bin/sh
523
524    DEVPATH=$(readlink -f /sys/$1/device)
525    DEVICE=$(basename $DEVPATH)
526    DRIVER=$(readlink -f $DEVPATH/driver)
527
528    echo -n $DEVICE > $DRIVER/unbind
529    echo -n $DEVICE > $DRIVER/bind
530
531Customization
532-------------
533
534Although basic recovery is possible with a simple script, consumers can define
535custom policies around recovery. For example, if the driver supports multiple
536recovery methods, consumers can opt for the suitable one depending on scenarios
537like repeat offences or vendor specific failures. Consumers can also choose to
538have the device available for debugging or telemetry collection and base their
539recovery decision on the findings. This is useful especially when the driver is
540unsure about recovery or method is unknown.
541
542.. _drm_driver_ioctl:
543
544IOCTL Support on Device Nodes
545=============================
546
547.. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
548   :doc: driver specific ioctls
549
550Recommended IOCTL Return Values
551-------------------------------
552
553In theory a driver's IOCTL callback is only allowed to return very few error
554codes. In practice it's good to abuse a few more. This section documents common
555practice within the DRM subsystem:
556
557ENOENT:
558        Strictly this should only be used when a file doesn't exist e.g. when
559        calling the open() syscall. We reuse that to signal any kind of object
560        lookup failure, e.g. for unknown GEM buffer object handles, unknown KMS
561        object handles and similar cases.
562
563ENOSPC:
564        Some drivers use this to differentiate "out of kernel memory" from "out
565        of VRAM". Sometimes also applies to other limited gpu resources used for
566        rendering (e.g. when you have a special limited compression buffer).
567        Sometimes resource allocation/reservation issues in command submission
568        IOCTLs are also signalled through EDEADLK.
569
570        Simply running out of kernel/system memory is signalled through ENOMEM.
571
572EPERM/EACCES:
573        Returned for an operation that is valid, but needs more privileges.
574        E.g. root-only or much more common, DRM master-only operations return
575        this when called by unpriviledged clients. There's no clear
576        difference between EACCES and EPERM.
577
578ENODEV:
579        The device is not present anymore or is not yet fully initialized.
580
581EOPNOTSUPP:
582        Feature (like PRIME, modesetting, GEM) is not supported by the driver.
583
584ENXIO:
585        Remote failure, either a hardware transaction (like i2c), but also used
586        when the exporting driver of a shared dma-buf or fence doesn't support a
587        feature needed.
588
589EINTR:
590        DRM drivers assume that userspace restarts all IOCTLs. Any DRM IOCTL can
591        return EINTR and in such a case should be restarted with the IOCTL
592        parameters left unchanged.
593
594EIO:
595        The GPU died and couldn't be resurrected through a reset. Modesetting
596        hardware failures are signalled through the "link status" connector
597        property.
598
599EINVAL:
600        Catch-all for anything that is an invalid argument combination which
601        cannot work.
602
603IOCTL also use other error codes like ETIME, EFAULT, EBUSY, ENOTTY but their
604usage is in line with the common meanings. The above list tries to just document
605DRM specific patterns. Note that ENOTTY has the slightly unintuitive meaning of
606"this IOCTL does not exist", and is used exactly as such in DRM.
607
608.. kernel-doc:: include/drm/drm_ioctl.h
609   :internal:
610
611.. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
612   :export:
613
614.. kernel-doc:: drivers/gpu/drm/drm_ioc32.c
615   :export:
616
617Testing and validation
618======================
619
620Testing Requirements for userspace API
621--------------------------------------
622
623New cross-driver userspace interface extensions, like new IOCTL, new KMS
624properties, new files in sysfs or anything else that constitutes an API change
625should have driver-agnostic testcases in IGT for that feature, if such a test
626can be reasonably made using IGT for the target hardware.
627
628Validating changes with IGT
629---------------------------
630
631There's a collection of tests that aims to cover the whole functionality of
632DRM drivers and that can be used to check that changes to DRM drivers or the
633core don't regress existing functionality. This test suite is called IGT and
634its code and instructions to build and run can be found in
635https://gitlab.freedesktop.org/drm/igt-gpu-tools/.
636
637Using VKMS to test DRM API
638--------------------------
639
640VKMS is a software-only model of a KMS driver that is useful for testing
641and for running compositors. VKMS aims to enable a virtual display without
642the need for a hardware display capability. These characteristics made VKMS
643a perfect tool for validating the DRM core behavior and also support the
644compositor developer. VKMS makes it possible to test DRM functions in a
645virtual machine without display, simplifying the validation of some of the
646core changes.
647
648To Validate changes in DRM API with VKMS, start setting the kernel: make
649sure to enable VKMS module; compile the kernel with the VKMS enabled and
650install it in the target machine. VKMS can be run in a Virtual Machine
651(QEMU, virtme or similar). It's recommended the use of KVM with the minimum
652of 1GB of RAM and four cores.
653
654It's possible to run the IGT-tests in a VM in two ways:
655
656	1. Use IGT inside a VM
657	2. Use IGT from the host machine and write the results in a shared directory.
658
659Following is an example of using a VM with a shared directory with
660the host machine to run igt-tests. This example uses virtme::
661
662	$ virtme-run --rwdir /path/for/shared_dir --kdir=path/for/kernel/directory --mods=auto
663
664Run the igt-tests in the guest machine. This example runs the 'kms_flip'
665tests::
666
667	$ /path/for/igt-gpu-tools/scripts/run-tests.sh -p -s -t "kms_flip.*" -v
668
669In this example, instead of building the igt_runner, Piglit is used
670(-p option). It creates an HTML summary of the test results and saves
671them in the folder "igt-gpu-tools/results". It executes only the igt-tests
672matching the -t option.
673
674Display CRC Support
675-------------------
676
677.. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
678   :doc: CRC ABI
679
680.. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
681   :export:
682
683Debugfs Support
684---------------
685
686.. kernel-doc:: include/drm/drm_debugfs.h
687   :internal:
688
689.. kernel-doc:: drivers/gpu/drm/drm_debugfs.c
690   :export:
691
692Sysfs Support
693=============
694
695.. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
696   :doc: overview
697
698.. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
699   :export:
700
701
702VBlank event handling
703=====================
704
705The DRM core exposes two vertical blank related ioctls:
706
707:c:macro:`DRM_IOCTL_WAIT_VBLANK`
708    This takes a struct drm_wait_vblank structure as its argument, and
709    it is used to block or request a signal when a specified vblank
710    event occurs.
711
712:c:macro:`DRM_IOCTL_MODESET_CTL`
713    This was only used for user-mode-settind drivers around modesetting
714    changes to allow the kernel to update the vblank interrupt after
715    mode setting, since on many devices the vertical blank counter is
716    reset to 0 at some point during modeset. Modern drivers should not
717    call this any more since with kernel mode setting it is a no-op.
718
719Userspace API Structures
720========================
721
722.. kernel-doc:: include/uapi/drm/drm_mode.h
723   :doc: overview
724
725.. _crtc_index:
726
727CRTC index
728----------
729
730CRTC's have both an object ID and an index, and they are not the same thing.
731The index is used in cases where a densely packed identifier for a CRTC is
732needed, for instance a bitmask of CRTC's. The member possible_crtcs of struct
733drm_mode_get_plane is an example.
734
735:c:macro:`DRM_IOCTL_MODE_GETRESOURCES` populates a structure with an array of
736CRTC ID's, and the CRTC index is its position in this array.
737
738.. kernel-doc:: include/uapi/drm/drm.h
739   :internal:
740
741.. kernel-doc:: include/uapi/drm/drm_mode.h
742   :internal:
743
744
745dma-buf interoperability
746========================
747
748Please see Documentation/userspace-api/dma-buf-alloc-exchange.rst for
749information on how dma-buf is integrated and exposed within DRM.
750
751
752Trace events
753============
754
755See Documentation/trace/tracepoints.rst for information about using
756Linux Kernel Tracepoints.
757In the DRM subsystem, some events are considered stable uAPI to avoid
758breaking tools (e.g.: GPUVis, umr) relying on them. Stable means that fields
759cannot be removed, nor their formatting updated. Adding new fields is
760possible, under the normal uAPI requirements.
761
762Stable uAPI events
763------------------
764
765From ``drivers/gpu/drm/scheduler/gpu_scheduler_trace.h``
766
767.. kernel-doc::  drivers/gpu/drm/scheduler/gpu_scheduler_trace.h
768   :doc: uAPI trace events