1 .. Copyright 2020 DisplayLink (UK) Ltd.
7 The DRM core exports several interfaces to applications, generally
8 intended to be used through corresponding libdrm wrapper functions. In
9 addition, drivers export device-specific interfaces for use by userspace
10 drivers & device-aware applications through ioctls and sysfs files.
12 External interfaces include: memory mapping, context management, DMA
13 operations, AGP management, vblank control, fence management, memory
14 management, and output management.
16 Cover generic ioctls and sysfs layout here. We only need high-level
17 info, since man pages should cover the rest.
22 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
23 :doc: getunique and setversion story
28 Primary Nodes, DRM Master and Authentication
29 ============================================
31 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
32 :doc: master and authentication
34 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
37 .. kernel-doc:: include/drm/drm_auth.h
40 Open-Source Userspace Requirements
41 ==================================
43 The DRM subsystem has stricter requirements than most other kernel subsystems on
44 what the userspace side for new uAPI needs to look like. This section here
45 explains what exactly those requirements are, and why they exist.
47 The short summary is that any addition of DRM uAPI requires corresponding
48 open-sourced userspace patches, and those patches must be reviewed and ready for
49 merging into a suitable and canonical upstream project.
51 GFX devices (both display and render/GPU side) are really complex bits of
52 hardware, with userspace and kernel by necessity having to work together really
53 closely. The interfaces, for rendering and modesetting, must be extremely wide
54 and flexible, and therefore it is almost always impossible to precisely define
55 them for every possible corner case. This in turn makes it really practically
56 infeasible to differentiate between behaviour that's required by userspace, and
57 which must not be changed to avoid regressions, and behaviour which is only an
58 accidental artifact of the current implementation.
60 Without access to the full source code of all userspace users that means it
61 becomes impossible to change the implementation details, since userspace could
62 depend upon the accidental behaviour of the current implementation in minute
63 details. And debugging such regressions without access to source code is pretty
64 much impossible. As a consequence this means:
66 - The Linux kernel's "no regression" policy holds in practice only for
67 open-source userspace of the DRM subsystem. DRM developers are perfectly fine
68 if closed-source blob drivers in userspace use the same uAPI as the open
69 drivers, but they must do so in the exact same way as the open drivers.
70 Creative (ab)use of the interfaces will, and in the past routinely has, lead
73 - Any new userspace interface must have an open-source implementation as
74 demonstration vehicle.
76 The other reason for requiring open-source userspace is uAPI review. Since the
77 kernel and userspace parts of a GFX stack must work together so closely, code
78 review can only assess whether a new interface achieves its goals by looking at
79 both sides. Making sure that the interface indeed covers the use-case fully
80 leads to a few additional requirements:
82 - The open-source userspace must not be a toy/test application, but the real
83 thing. Specifically it needs to handle all the usual error and corner cases.
84 These are often the places where new uAPI falls apart and hence essential to
85 assess the fitness of a proposed interface.
87 - The userspace side must be fully reviewed and tested to the standards of that
88 userspace project. For e.g. mesa this means piglit testcases and review on the
89 mailing list. This is again to ensure that the new interface actually gets the
90 job done. The userspace-side reviewer should also provide an Acked-by on the
91 kernel uAPI patch indicating that they believe the proposed uAPI is sound and
92 sufficiently documented and validated for userspace's consumption.
94 - The userspace patches must be against the canonical upstream, not some vendor
95 fork. This is to make sure that no one cheats on the review and testing
96 requirements by doing a quick fork.
98 - The kernel patch can only be merged after all the above requirements are met,
99 but it **must** be merged to either drm-next or drm-misc-next **before** the
100 userspace patches land. uAPI always flows from the kernel, doing things the
101 other way round risks divergence of the uAPI definitions and header files.
103 These are fairly steep requirements, but have grown out from years of shared
104 pain and experience with uAPI added hastily, and almost always regretted about
105 just as fast. GFX devices change really fast, requiring a paradigm shift and
106 entire new set of uAPI interfaces every few years at least. Together with the
107 Linux kernel's guarantee to keep existing userspace running for 10+ years this
108 is already rather painful for the DRM subsystem, with multiple different uAPIs
109 for the same thing co-existing. If we add a few more complete mistakes into the
110 mix every year it would be entirely unmanageable.
117 DRM core provides multiple character-devices for user-space to use.
118 Depending on which device is opened, user-space can perform a different
119 set of operations (mainly ioctls). The primary node is always created
120 and called card<num>. Additionally, a currently unused control node,
121 called controlD<num> is also created. The primary node provides all
122 legacy operations and historically was the only interface used by
123 userspace. With KMS, the control node was introduced. However, the
124 planned KMS control interface has never been written and so the control
125 node stays unused to date.
127 With the increased use of offscreen renderers and GPGPU applications,
128 clients no longer require running compositors or graphics servers to
129 make use of a GPU. But the DRM API required unprivileged clients to
130 authenticate to a DRM-Master prior to getting GPU access. To avoid this
131 step and to grant clients GPU access without authenticating, render
132 nodes were introduced. Render nodes solely serve render clients, that
133 is, no modesetting or privileged ioctls can be issued on render nodes.
134 Only non-global rendering commands are allowed. If a driver supports
135 render nodes, it must advertise it via the DRIVER_RENDER DRM driver
136 capability. If not supported, the primary node must be used for render
137 clients together with the legacy drmAuth authentication procedure.
139 If a driver advertises render node support, DRM core will create a
140 separate render node called renderD<num>. There will be one render node
141 per device. No ioctls except PRIME-related ioctls will be allowed on
142 this node. Especially GEM_OPEN will be explicitly prohibited. Render
143 nodes are designed to avoid the buffer-leaks, which occur if clients
144 guess the flink names or mmap offsets on the legacy interface.
145 Additionally to this basic interface, drivers must mark their
146 driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
147 clients can use them. Driver authors must be careful not to allow any
148 privileged ioctls on render nodes.
150 With render nodes, user-space can now control access to the render node
151 via basic file-system access-modes. A running graphics server which
152 authenticates clients on the privileged primary/legacy node is no longer
153 required. Instead, a client can open the render node and is immediately
154 granted GPU access. Communication between clients (or servers) is done
155 via PRIME. FLINK from render node to legacy node is not supported. New
156 clients must not use the insecure FLINK interface.
158 Besides dropping all modeset/global ioctls, render nodes also drop the
159 DRM-Master concept. There is no reason to associate render clients with
160 a DRM-Master as they are independent of any graphics server. Besides,
161 they must work without any running master, anyway. Drivers must be able
162 to run without a master object if they support render nodes. If, on the
163 other hand, a driver requires shared state between clients which is
164 visible to user-space and accessible beyond open-file boundaries, they
165 cannot support render nodes.
171 The following is the plan. Implementation is not there yet
174 Graphics devices (display and/or render) may be connected via USB (e.g.
175 display adapters or docking stations) or Thunderbolt (e.g. eGPU). An end
176 user is able to hot-unplug this kind of devices while they are being
177 used, and expects that the very least the machine does not crash. Any
178 damage from hot-unplugging a DRM device needs to be limited as much as
179 possible and userspace must be given the chance to handle it if it wants
180 to. Ideally, unplugging a DRM device still lets a desktop continue to
181 run, but that is going to need explicit support throughout the whole
182 graphics stack: from kernel and userspace drivers, through display
183 servers, via window system protocols, and in applications and libraries.
185 Other scenarios that should lead to the same are: unrecoverable GPU
186 crash, PCI device disappearing off the bus, or forced unbind of a driver
187 from the physical device.
189 In other words, from userspace perspective everything needs to keep on
190 working more or less, until userspace stops using the disappeared DRM
191 device and closes it completely. Userspace will learn of the device
192 disappearance from the device removed uevent, ioctls returning ENODEV
193 (or driver-specific ioctls returning driver-specific things), or open()
196 Only after userspace has closed all relevant DRM device and dmabuf file
197 descriptors and removed all mmaps, the DRM driver can tear down its
198 instance for the device that no longer exists. If the same physical
199 device somehow comes back in the mean time, it shall be a new DRM
202 Similar to PIDs, chardev minor numbers are not recycled immediately. A
203 new DRM device always picks the next free minor number compared to the
204 previous one allocated, and wraps around when minor numbers are
207 The goal raises at least the following requirements for the kernel and
210 Requirements for KMS UAPI
211 -------------------------
213 - KMS connectors must change their status to disconnected.
215 - Legacy modesets and pageflips, and atomic commits, both real and
216 TEST_ONLY, and any other ioctls either fail with ENODEV or fake
219 - Pending non-blocking KMS operations deliver the DRM events userspace
220 is expecting. This applies also to ioctls that faked success.
222 - open() on a device node whose underlying device has disappeared will
225 - Attempting to create a DRM lease on a disappeared DRM device will
226 fail with ENODEV. Existing DRM leases remain and work as listed
229 Requirements for Render and Cross-Device UAPI
230 ---------------------------------------------
232 - All GPU jobs that can no longer run must have their fences
233 force-signalled to avoid inflicting hangs on userspace.
234 The associated error code is ENODEV.
236 - Some userspace APIs already define what should happen when the device
237 disappears (OpenGL, GL ES: `GL_KHR_robustness`_; `Vulkan`_:
238 VK_ERROR_DEVICE_LOST; etc.). DRM drivers are free to implement this
239 behaviour the way they see best, e.g. returning failures in
240 driver-specific ioctls and handling those in userspace drivers, or
241 rely on uevents, and so on.
243 - dmabuf which point to memory that has disappeared will either fail to
244 import with ENODEV or continue to be successfully imported if it would
245 have succeeded before the disappearance. See also about memory maps
246 below for already imported dmabufs.
248 - Attempting to import a dmabuf to a disappeared device will either fail
249 with ENODEV or succeed if it would have succeeded without the
252 - open() on a device node whose underlying device has disappeared will
255 .. _GL_KHR_robustness: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_robustness.txt
256 .. _Vulkan: https://www.khronos.org/vulkan/
258 Requirements for Memory Maps
259 ----------------------------
261 Memory maps have further requirements that apply to both existing maps
262 and maps created after the device has disappeared. If the underlying
263 memory disappears, the map is created or modified such that reads and
264 writes will still complete successfully but the result is undefined.
265 This applies to both userspace mmap()'d memory and memory pointed to by
266 dmabuf which might be mapped to other devices (cross-device dmabuf
269 Raising SIGBUS is not an option, because userspace cannot realistically
270 handle it. Signal handlers are global, which makes them extremely
271 difficult to use correctly from libraries like those that Mesa produces.
272 Signal handlers are not composable, you can't have different handlers
273 for GPU1 and GPU2 from different vendors, and a third handler for
274 mmapped regular files. Threads cause additional pain with signal
277 .. _drm_driver_ioctl:
279 IOCTL Support on Device Nodes
280 =============================
282 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
283 :doc: driver specific ioctls
285 Recommended IOCTL Return Values
286 -------------------------------
288 In theory a driver's IOCTL callback is only allowed to return very few error
289 codes. In practice it's good to abuse a few more. This section documents common
290 practice within the DRM subsystem:
293 Strictly this should only be used when a file doesn't exist e.g. when
294 calling the open() syscall. We reuse that to signal any kind of object
295 lookup failure, e.g. for unknown GEM buffer object handles, unknown KMS
296 object handles and similar cases.
299 Some drivers use this to differentiate "out of kernel memory" from "out
300 of VRAM". Sometimes also applies to other limited gpu resources used for
301 rendering (e.g. when you have a special limited compression buffer).
302 Sometimes resource allocation/reservation issues in command submission
303 IOCTLs are also signalled through EDEADLK.
305 Simply running out of kernel/system memory is signalled through ENOMEM.
308 Returned for an operation that is valid, but needs more privileges.
309 E.g. root-only or much more common, DRM master-only operations return
310 this when called by unpriviledged clients. There's no clear
311 difference between EACCES and EPERM.
314 The device is not present anymore or is not yet fully initialized.
317 Feature (like PRIME, modesetting, GEM) is not supported by the driver.
320 Remote failure, either a hardware transaction (like i2c), but also used
321 when the exporting driver of a shared dma-buf or fence doesn't support a
325 DRM drivers assume that userspace restarts all IOCTLs. Any DRM IOCTL can
326 return EINTR and in such a case should be restarted with the IOCTL
327 parameters left unchanged.
330 The GPU died and couldn't be resurrected through a reset. Modesetting
331 hardware failures are signalled through the "link status" connector
335 Catch-all for anything that is an invalid argument combination which
338 IOCTL also use other error codes like ETIME, EFAULT, EBUSY, ENOTTY but their
339 usage is in line with the common meanings. The above list tries to just document
340 DRM specific patterns. Note that ENOTTY has the slightly unintuitive meaning of
341 "this IOCTL does not exist", and is used exactly as such in DRM.
343 .. kernel-doc:: include/drm/drm_ioctl.h
346 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
349 .. kernel-doc:: drivers/gpu/drm/drm_ioc32.c
352 Testing and validation
353 ======================
355 Testing Requirements for userspace API
356 --------------------------------------
358 New cross-driver userspace interface extensions, like new IOCTL, new KMS
359 properties, new files in sysfs or anything else that constitutes an API change
360 should have driver-agnostic testcases in IGT for that feature, if such a test
361 can be reasonably made using IGT for the target hardware.
363 Validating changes with IGT
364 ---------------------------
366 There's a collection of tests that aims to cover the whole functionality of
367 DRM drivers and that can be used to check that changes to DRM drivers or the
368 core don't regress existing functionality. This test suite is called IGT and
369 its code and instructions to build and run can be found in
370 https://gitlab.freedesktop.org/drm/igt-gpu-tools/.
372 Using VKMS to test DRM API
373 --------------------------
375 VKMS is a software-only model of a KMS driver that is useful for testing
376 and for running compositors. VKMS aims to enable a virtual display without
377 the need for a hardware display capability. These characteristics made VKMS
378 a perfect tool for validating the DRM core behavior and also support the
379 compositor developer. VKMS makes it possible to test DRM functions in a
380 virtual machine without display, simplifying the validation of some of the
383 To Validate changes in DRM API with VKMS, start setting the kernel: make
384 sure to enable VKMS module; compile the kernel with the VKMS enabled and
385 install it in the target machine. VKMS can be run in a Virtual Machine
386 (QEMU, virtme or similar). It's recommended the use of KVM with the minimum
387 of 1GB of RAM and four cores.
389 It's possible to run the IGT-tests in a VM in two ways:
391 1. Use IGT inside a VM
392 2. Use IGT from the host machine and write the results in a shared directory.
394 As follow, there is an example of using a VM with a shared directory with
395 the host machine to run igt-tests. As an example it's used virtme::
397 $ virtme-run --rwdir /path/for/shared_dir --kdir=path/for/kernel/directory --mods=auto
399 Run the igt-tests in the guest machine, as example it's ran the 'kms_flip'
402 $ /path/for/igt-gpu-tools/scripts/run-tests.sh -p -s -t "kms_flip.*" -v
404 In this example, instead of build the igt_runner, Piglit is used
405 (-p option); it's created html summary of the tests results and it's saved
406 in the folder "igt-gpu-tools/results"; it's executed only the igt-tests
407 matching the -t option.
412 .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
415 .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
421 .. kernel-doc:: include/drm/drm_debugfs.h
424 .. kernel-doc:: drivers/gpu/drm/drm_debugfs.c
430 .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
433 .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
437 VBlank event handling
438 =====================
440 The DRM core exposes two vertical blank related ioctls:
442 DRM_IOCTL_WAIT_VBLANK
443 This takes a struct drm_wait_vblank structure as its argument, and
444 it is used to block or request a signal when a specified vblank
447 DRM_IOCTL_MODESET_CTL
448 This was only used for user-mode-settind drivers around modesetting
449 changes to allow the kernel to update the vblank interrupt after
450 mode setting, since on many devices the vertical blank counter is
451 reset to 0 at some point during modeset. Modern drivers should not
452 call this any more since with kernel mode setting it is a no-op.
454 Userspace API Structures
455 ========================
457 .. kernel-doc:: include/uapi/drm/drm_mode.h
460 .. kernel-doc:: include/uapi/drm/drm_mode.h