5 The DRM core exports several interfaces to applications, generally
6 intended to be used through corresponding libdrm wrapper functions. In
7 addition, drivers export device-specific interfaces for use by userspace
8 drivers & device-aware applications through ioctls and sysfs files.
10 External interfaces include: memory mapping, context management, DMA
11 operations, AGP management, vblank control, fence management, memory
12 management, and output management.
14 Cover generic ioctls and sysfs layout here. We only need high-level
15 info, since man pages should cover the rest.
20 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
21 :doc: getunique and setversion story
26 Primary Nodes, DRM Master and Authentication
27 ============================================
29 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
30 :doc: master and authentication
32 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
35 .. kernel-doc:: include/drm/drm_auth.h
38 Open-Source Userspace Requirements
39 ==================================
41 The DRM subsystem has stricter requirements than most other kernel subsystems on
42 what the userspace side for new uAPI needs to look like. This section here
43 explains what exactly those requirements are, and why they exist.
45 The short summary is that any addition of DRM uAPI requires corresponding
46 open-sourced userspace patches, and those patches must be reviewed and ready for
47 merging into a suitable and canonical upstream project.
49 GFX devices (both display and render/GPU side) are really complex bits of
50 hardware, with userspace and kernel by necessity having to work together really
51 closely. The interfaces, for rendering and modesetting, must be extremely wide
52 and flexible, and therefore it is almost always impossible to precisely define
53 them for every possible corner case. This in turn makes it really practically
54 infeasible to differentiate between behaviour that's required by userspace, and
55 which must not be changed to avoid regressions, and behaviour which is only an
56 accidental artifact of the current implementation.
58 Without access to the full source code of all userspace users that means it
59 becomes impossible to change the implementation details, since userspace could
60 depend upon the accidental behaviour of the current implementation in minute
61 details. And debugging such regressions without access to source code is pretty
62 much impossible. As a consequence this means:
64 - The Linux kernel's "no regression" policy holds in practice only for
65 open-source userspace of the DRM subsystem. DRM developers are perfectly fine
66 if closed-source blob drivers in userspace use the same uAPI as the open
67 drivers, but they must do so in the exact same way as the open drivers.
68 Creative (ab)use of the interfaces will, and in the past routinely has, lead
71 - Any new userspace interface must have an open-source implementation as
72 demonstration vehicle.
74 The other reason for requiring open-source userspace is uAPI review. Since the
75 kernel and userspace parts of a GFX stack must work together so closely, code
76 review can only assess whether a new interface achieves its goals by looking at
77 both sides. Making sure that the interface indeed covers the use-case fully
78 leads to a few additional requirements:
80 - The open-source userspace must not be a toy/test application, but the real
81 thing. Specifically it needs to handle all the usual error and corner cases.
82 These are often the places where new uAPI falls apart and hence essential to
83 assess the fitness of a proposed interface.
85 - The userspace side must be fully reviewed and tested to the standards of that
86 userspace project. For e.g. mesa this means piglit testcases and review on the
87 mailing list. This is again to ensure that the new interface actually gets the
88 job done. The userspace-side reviewer should also provide an Acked-by on the
89 kernel uAPI patch indicating that they believe the proposed uAPI is sound and
90 sufficiently documented and validated for userspace's consumption.
92 - The userspace patches must be against the canonical upstream, not some vendor
93 fork. This is to make sure that no one cheats on the review and testing
94 requirements by doing a quick fork.
96 - The kernel patch can only be merged after all the above requirements are met,
97 but it **must** be merged to either drm-next or drm-misc-next **before** the
98 userspace patches land. uAPI always flows from the kernel, doing things the
99 other way round risks divergence of the uAPI definitions and header files.
101 These are fairly steep requirements, but have grown out from years of shared
102 pain and experience with uAPI added hastily, and almost always regretted about
103 just as fast. GFX devices change really fast, requiring a paradigm shift and
104 entire new set of uAPI interfaces every few years at least. Together with the
105 Linux kernel's guarantee to keep existing userspace running for 10+ years this
106 is already rather painful for the DRM subsystem, with multiple different uAPIs
107 for the same thing co-existing. If we add a few more complete mistakes into the
108 mix every year it would be entirely unmanageable.
115 DRM core provides multiple character-devices for user-space to use.
116 Depending on which device is opened, user-space can perform a different
117 set of operations (mainly ioctls). The primary node is always created
118 and called card<num>. Additionally, a currently unused control node,
119 called controlD<num> is also created. The primary node provides all
120 legacy operations and historically was the only interface used by
121 userspace. With KMS, the control node was introduced. However, the
122 planned KMS control interface has never been written and so the control
123 node stays unused to date.
125 With the increased use of offscreen renderers and GPGPU applications,
126 clients no longer require running compositors or graphics servers to
127 make use of a GPU. But the DRM API required unprivileged clients to
128 authenticate to a DRM-Master prior to getting GPU access. To avoid this
129 step and to grant clients GPU access without authenticating, render
130 nodes were introduced. Render nodes solely serve render clients, that
131 is, no modesetting or privileged ioctls can be issued on render nodes.
132 Only non-global rendering commands are allowed. If a driver supports
133 render nodes, it must advertise it via the DRIVER_RENDER DRM driver
134 capability. If not supported, the primary node must be used for render
135 clients together with the legacy drmAuth authentication procedure.
137 If a driver advertises render node support, DRM core will create a
138 separate render node called renderD<num>. There will be one render node
139 per device. No ioctls except PRIME-related ioctls will be allowed on
140 this node. Especially GEM_OPEN will be explicitly prohibited. Render
141 nodes are designed to avoid the buffer-leaks, which occur if clients
142 guess the flink names or mmap offsets on the legacy interface.
143 Additionally to this basic interface, drivers must mark their
144 driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
145 clients can use them. Driver authors must be careful not to allow any
146 privileged ioctls on render nodes.
148 With render nodes, user-space can now control access to the render node
149 via basic file-system access-modes. A running graphics server which
150 authenticates clients on the privileged primary/legacy node is no longer
151 required. Instead, a client can open the render node and is immediately
152 granted GPU access. Communication between clients (or servers) is done
153 via PRIME. FLINK from render node to legacy node is not supported. New
154 clients must not use the insecure FLINK interface.
156 Besides dropping all modeset/global ioctls, render nodes also drop the
157 DRM-Master concept. There is no reason to associate render clients with
158 a DRM-Master as they are independent of any graphics server. Besides,
159 they must work without any running master, anyway. Drivers must be able
160 to run without a master object if they support render nodes. If, on the
161 other hand, a driver requires shared state between clients which is
162 visible to user-space and accessible beyond open-file boundaries, they
163 cannot support render nodes.
165 .. _drm_driver_ioctl:
167 IOCTL Support on Device Nodes
168 =============================
170 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
171 :doc: driver specific ioctls
173 Recommended IOCTL Return Values
174 -------------------------------
176 In theory a driver's IOCTL callback is only allowed to return very few error
177 codes. In practice it's good to abuse a few more. This section documents common
178 practice within the DRM subsystem:
181 Strictly this should only be used when a file doesn't exist e.g. when
182 calling the open() syscall. We reuse that to signal any kind of object
183 lookup failure, e.g. for unknown GEM buffer object handles, unknown KMS
184 object handles and similar cases.
187 Some drivers use this to differentiate "out of kernel memory" from "out
188 of VRAM". Sometimes also applies to other limited gpu resources used for
189 rendering (e.g. when you have a special limited compression buffer).
190 Sometimes resource allocation/reservation issues in command submission
191 IOCTLs are also signalled through EDEADLK.
193 Simply running out of kernel/system memory is signalled through ENOMEM.
196 Returned for an operation that is valid, but needs more privileges.
197 E.g. root-only or much more common, DRM master-only operations return
198 this when called by unpriviledged clients. There's no clear
199 difference between EACCES and EPERM.
202 The device is not (yet) present or fully initialized.
205 Feature (like PRIME, modesetting, GEM) is not supported by the driver.
208 Remote failure, either a hardware transaction (like i2c), but also used
209 when the exporting driver of a shared dma-buf or fence doesn't support a
213 DRM drivers assume that userspace restarts all IOCTLs. Any DRM IOCTL can
214 return EINTR and in such a case should be restarted with the IOCTL
215 parameters left unchanged.
218 The GPU died and couldn't be resurrected through a reset. Modesetting
219 hardware failures are signalled through the "link status" connector
223 Catch-all for anything that is an invalid argument combination which
226 IOCTL also use other error codes like ETIME, EFAULT, EBUSY, ENOTTY but their
227 usage is in line with the common meanings. The above list tries to just document
228 DRM specific patterns. Note that ENOTTY has the slightly unintuitive meaning of
229 "this IOCTL does not exist", and is used exactly as such in DRM.
231 .. kernel-doc:: include/drm/drm_ioctl.h
234 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
237 .. kernel-doc:: drivers/gpu/drm/drm_ioc32.c
240 Testing and validation
241 ======================
243 Testing Requirements for userspace API
244 --------------------------------------
246 New cross-driver userspace interface extensions, like new IOCTL, new KMS
247 properties, new files in sysfs or anything else that constitutes an API change
248 should have driver-agnostic testcases in IGT for that feature, if such a test
249 can be reasonably made using IGT for the target hardware.
251 Validating changes with IGT
252 ---------------------------
254 There's a collection of tests that aims to cover the whole functionality of
255 DRM drivers and that can be used to check that changes to DRM drivers or the
256 core don't regress existing functionality. This test suite is called IGT and
257 its code and instructions to build and run can be found in
258 https://gitlab.freedesktop.org/drm/igt-gpu-tools/.
260 Using VKMS to test DRM API
261 --------------------------
263 VKMS is a software-only model of a KMS driver that is useful for testing
264 and for running compositors. VKMS aims to enable a virtual display without
265 the need for a hardware display capability. These characteristics made VKMS
266 a perfect tool for validating the DRM core behavior and also support the
267 compositor developer. VKMS makes it possible to test DRM functions in a
268 virtual machine without display, simplifying the validation of some of the
271 To Validate changes in DRM API with VKMS, start setting the kernel: make
272 sure to enable VKMS module; compile the kernel with the VKMS enabled and
273 install it in the target machine. VKMS can be run in a Virtual Machine
274 (QEMU, virtme or similar). It's recommended the use of KVM with the minimum
275 of 1GB of RAM and four cores.
277 It's possible to run the IGT-tests in a VM in two ways:
279 1. Use IGT inside a VM
280 2. Use IGT from the host machine and write the results in a shared directory.
282 As follow, there is an example of using a VM with a shared directory with
283 the host machine to run igt-tests. As an example it's used virtme::
285 $ virtme-run --rwdir /path/for/shared_dir --kdir=path/for/kernel/directory --mods=auto
287 Run the igt-tests in the guest machine, as example it's ran the 'kms_flip'
290 $ /path/for/igt-gpu-tools/scripts/run-tests.sh -p -s -t "kms_flip.*" -v
292 In this example, instead of build the igt_runner, Piglit is used
293 (-p option); it's created html summary of the tests results and it's saved
294 in the folder "igt-gpu-tools/results"; it's executed only the igt-tests
295 matching the -t option.
300 .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
303 .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
309 .. kernel-doc:: include/drm/drm_debugfs.h
312 .. kernel-doc:: drivers/gpu/drm/drm_debugfs.c
318 .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
321 .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
325 VBlank event handling
326 =====================
328 The DRM core exposes two vertical blank related ioctls:
330 DRM_IOCTL_WAIT_VBLANK
331 This takes a struct drm_wait_vblank structure as its argument, and
332 it is used to block or request a signal when a specified vblank
335 DRM_IOCTL_MODESET_CTL
336 This was only used for user-mode-settind drivers around modesetting
337 changes to allow the kernel to update the vblank interrupt after
338 mode setting, since on many devices the vertical blank counter is
339 reset to 0 at some point during modeset. Modern drivers should not
340 call this any more since with kernel mode setting it is a no-op.
342 Userspace API Structures
343 ========================
345 .. kernel-doc:: include/uapi/drm/drm_mode.h
348 .. kernel-doc:: include/uapi/drm/drm_mode.h