Back to home page

OSCL-LXR

 
 

    


0001 .. Copyright 2020 DisplayLink (UK) Ltd.
0002 
0003 ===================
0004 Userland interfaces
0005 ===================
0006 
0007 The DRM core exports several interfaces to applications, generally
0008 intended to be used through corresponding libdrm wrapper functions. In
0009 addition, drivers export device-specific interfaces for use by userspace
0010 drivers & device-aware applications through ioctls and sysfs files.
0011 
0012 External interfaces include: memory mapping, context management, DMA
0013 operations, AGP management, vblank control, fence management, memory
0014 management, and output management.
0015 
0016 Cover generic ioctls and sysfs layout here. We only need high-level
0017 info, since man pages should cover the rest.
0018 
0019 libdrm Device Lookup
0020 ====================
0021 
0022 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
0023    :doc: getunique and setversion story
0024 
0025 
0026 .. _drm_primary_node:
0027 
0028 Primary Nodes, DRM Master and Authentication
0029 ============================================
0030 
0031 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
0032    :doc: master and authentication
0033 
0034 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
0035    :export:
0036 
0037 .. kernel-doc:: include/drm/drm_auth.h
0038    :internal:
0039 
0040 
0041 .. _drm_leasing:
0042 
0043 DRM Display Resource Leasing
0044 ============================
0045 
0046 .. kernel-doc:: drivers/gpu/drm/drm_lease.c
0047    :doc: drm leasing
0048 
0049 Open-Source Userspace Requirements
0050 ==================================
0051 
0052 The DRM subsystem has stricter requirements than most other kernel subsystems on
0053 what the userspace side for new uAPI needs to look like. This section here
0054 explains what exactly those requirements are, and why they exist.
0055 
0056 The short summary is that any addition of DRM uAPI requires corresponding
0057 open-sourced userspace patches, and those patches must be reviewed and ready for
0058 merging into a suitable and canonical upstream project.
0059 
0060 GFX devices (both display and render/GPU side) are really complex bits of
0061 hardware, with userspace and kernel by necessity having to work together really
0062 closely.  The interfaces, for rendering and modesetting, must be extremely wide
0063 and flexible, and therefore it is almost always impossible to precisely define
0064 them for every possible corner case. This in turn makes it really practically
0065 infeasible to differentiate between behaviour that's required by userspace, and
0066 which must not be changed to avoid regressions, and behaviour which is only an
0067 accidental artifact of the current implementation.
0068 
0069 Without access to the full source code of all userspace users that means it
0070 becomes impossible to change the implementation details, since userspace could
0071 depend upon the accidental behaviour of the current implementation in minute
0072 details. And debugging such regressions without access to source code is pretty
0073 much impossible. As a consequence this means:
0074 
0075 - The Linux kernel's "no regression" policy holds in practice only for
0076   open-source userspace of the DRM subsystem. DRM developers are perfectly fine
0077   if closed-source blob drivers in userspace use the same uAPI as the open
0078   drivers, but they must do so in the exact same way as the open drivers.
0079   Creative (ab)use of the interfaces will, and in the past routinely has, lead
0080   to breakage.
0081 
0082 - Any new userspace interface must have an open-source implementation as
0083   demonstration vehicle.
0084 
0085 The other reason for requiring open-source userspace is uAPI review. Since the
0086 kernel and userspace parts of a GFX stack must work together so closely, code
0087 review can only assess whether a new interface achieves its goals by looking at
0088 both sides. Making sure that the interface indeed covers the use-case fully
0089 leads to a few additional requirements:
0090 
0091 - The open-source userspace must not be a toy/test application, but the real
0092   thing. Specifically it needs to handle all the usual error and corner cases.
0093   These are often the places where new uAPI falls apart and hence essential to
0094   assess the fitness of a proposed interface.
0095 
0096 - The userspace side must be fully reviewed and tested to the standards of that
0097   userspace project. For e.g. mesa this means piglit testcases and review on the
0098   mailing list. This is again to ensure that the new interface actually gets the
0099   job done.  The userspace-side reviewer should also provide an Acked-by on the
0100   kernel uAPI patch indicating that they believe the proposed uAPI is sound and
0101   sufficiently documented and validated for userspace's consumption.
0102 
0103 - The userspace patches must be against the canonical upstream, not some vendor
0104   fork. This is to make sure that no one cheats on the review and testing
0105   requirements by doing a quick fork.
0106 
0107 - The kernel patch can only be merged after all the above requirements are met,
0108   but it **must** be merged to either drm-next or drm-misc-next **before** the
0109   userspace patches land. uAPI always flows from the kernel, doing things the
0110   other way round risks divergence of the uAPI definitions and header files.
0111 
0112 These are fairly steep requirements, but have grown out from years of shared
0113 pain and experience with uAPI added hastily, and almost always regretted about
0114 just as fast. GFX devices change really fast, requiring a paradigm shift and
0115 entire new set of uAPI interfaces every few years at least. Together with the
0116 Linux kernel's guarantee to keep existing userspace running for 10+ years this
0117 is already rather painful for the DRM subsystem, with multiple different uAPIs
0118 for the same thing co-existing. If we add a few more complete mistakes into the
0119 mix every year it would be entirely unmanageable.
0120 
0121 .. _drm_render_node:
0122 
0123 Render nodes
0124 ============
0125 
0126 DRM core provides multiple character-devices for user-space to use.
0127 Depending on which device is opened, user-space can perform a different
0128 set of operations (mainly ioctls). The primary node is always created
0129 and called card<num>. Additionally, a currently unused control node,
0130 called controlD<num> is also created. The primary node provides all
0131 legacy operations and historically was the only interface used by
0132 userspace. With KMS, the control node was introduced. However, the
0133 planned KMS control interface has never been written and so the control
0134 node stays unused to date.
0135 
0136 With the increased use of offscreen renderers and GPGPU applications,
0137 clients no longer require running compositors or graphics servers to
0138 make use of a GPU. But the DRM API required unprivileged clients to
0139 authenticate to a DRM-Master prior to getting GPU access. To avoid this
0140 step and to grant clients GPU access without authenticating, render
0141 nodes were introduced. Render nodes solely serve render clients, that
0142 is, no modesetting or privileged ioctls can be issued on render nodes.
0143 Only non-global rendering commands are allowed. If a driver supports
0144 render nodes, it must advertise it via the DRIVER_RENDER DRM driver
0145 capability. If not supported, the primary node must be used for render
0146 clients together with the legacy drmAuth authentication procedure.
0147 
0148 If a driver advertises render node support, DRM core will create a
0149 separate render node called renderD<num>. There will be one render node
0150 per device. No ioctls except PRIME-related ioctls will be allowed on
0151 this node. Especially GEM_OPEN will be explicitly prohibited. For a
0152 complete list of driver-independent ioctls that can be used on render
0153 nodes, see the ioctls marked DRM_RENDER_ALLOW in drm_ioctl.c  Render
0154 nodes are designed to avoid the buffer-leaks, which occur if clients
0155 guess the flink names or mmap offsets on the legacy interface.
0156 Additionally to this basic interface, drivers must mark their
0157 driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
0158 clients can use them. Driver authors must be careful not to allow any
0159 privileged ioctls on render nodes.
0160 
0161 With render nodes, user-space can now control access to the render node
0162 via basic file-system access-modes. A running graphics server which
0163 authenticates clients on the privileged primary/legacy node is no longer
0164 required. Instead, a client can open the render node and is immediately
0165 granted GPU access. Communication between clients (or servers) is done
0166 via PRIME. FLINK from render node to legacy node is not supported. New
0167 clients must not use the insecure FLINK interface.
0168 
0169 Besides dropping all modeset/global ioctls, render nodes also drop the
0170 DRM-Master concept. There is no reason to associate render clients with
0171 a DRM-Master as they are independent of any graphics server. Besides,
0172 they must work without any running master, anyway. Drivers must be able
0173 to run without a master object if they support render nodes. If, on the
0174 other hand, a driver requires shared state between clients which is
0175 visible to user-space and accessible beyond open-file boundaries, they
0176 cannot support render nodes.
0177 
0178 Device Hot-Unplug
0179 =================
0180 
0181 .. note::
0182    The following is the plan. Implementation is not there yet
0183    (2020 May).
0184 
0185 Graphics devices (display and/or render) may be connected via USB (e.g.
0186 display adapters or docking stations) or Thunderbolt (e.g. eGPU). An end
0187 user is able to hot-unplug this kind of devices while they are being
0188 used, and expects that the very least the machine does not crash. Any
0189 damage from hot-unplugging a DRM device needs to be limited as much as
0190 possible and userspace must be given the chance to handle it if it wants
0191 to. Ideally, unplugging a DRM device still lets a desktop continue to
0192 run, but that is going to need explicit support throughout the whole
0193 graphics stack: from kernel and userspace drivers, through display
0194 servers, via window system protocols, and in applications and libraries.
0195 
0196 Other scenarios that should lead to the same are: unrecoverable GPU
0197 crash, PCI device disappearing off the bus, or forced unbind of a driver
0198 from the physical device.
0199 
0200 In other words, from userspace perspective everything needs to keep on
0201 working more or less, until userspace stops using the disappeared DRM
0202 device and closes it completely. Userspace will learn of the device
0203 disappearance from the device removed uevent, ioctls returning ENODEV
0204 (or driver-specific ioctls returning driver-specific things), or open()
0205 returning ENXIO.
0206 
0207 Only after userspace has closed all relevant DRM device and dmabuf file
0208 descriptors and removed all mmaps, the DRM driver can tear down its
0209 instance for the device that no longer exists. If the same physical
0210 device somehow comes back in the mean time, it shall be a new DRM
0211 device.
0212 
0213 Similar to PIDs, chardev minor numbers are not recycled immediately. A
0214 new DRM device always picks the next free minor number compared to the
0215 previous one allocated, and wraps around when minor numbers are
0216 exhausted.
0217 
0218 The goal raises at least the following requirements for the kernel and
0219 drivers.
0220 
0221 Requirements for KMS UAPI
0222 -------------------------
0223 
0224 - KMS connectors must change their status to disconnected.
0225 
0226 - Legacy modesets and pageflips, and atomic commits, both real and
0227   TEST_ONLY, and any other ioctls either fail with ENODEV or fake
0228   success.
0229 
0230 - Pending non-blocking KMS operations deliver the DRM events userspace
0231   is expecting. This applies also to ioctls that faked success.
0232 
0233 - open() on a device node whose underlying device has disappeared will
0234   fail with ENXIO.
0235 
0236 - Attempting to create a DRM lease on a disappeared DRM device will
0237   fail with ENODEV. Existing DRM leases remain and work as listed
0238   above.
0239 
0240 Requirements for Render and Cross-Device UAPI
0241 ---------------------------------------------
0242 
0243 - All GPU jobs that can no longer run must have their fences
0244   force-signalled to avoid inflicting hangs on userspace.
0245   The associated error code is ENODEV.
0246 
0247 - Some userspace APIs already define what should happen when the device
0248   disappears (OpenGL, GL ES: `GL_KHR_robustness`_; `Vulkan`_:
0249   VK_ERROR_DEVICE_LOST; etc.). DRM drivers are free to implement this
0250   behaviour the way they see best, e.g. returning failures in
0251   driver-specific ioctls and handling those in userspace drivers, or
0252   rely on uevents, and so on.
0253 
0254 - dmabuf which point to memory that has disappeared will either fail to
0255   import with ENODEV or continue to be successfully imported if it would
0256   have succeeded before the disappearance. See also about memory maps
0257   below for already imported dmabufs.
0258 
0259 - Attempting to import a dmabuf to a disappeared device will either fail
0260   with ENODEV or succeed if it would have succeeded without the
0261   disappearance.
0262 
0263 - open() on a device node whose underlying device has disappeared will
0264   fail with ENXIO.
0265 
0266 .. _GL_KHR_robustness: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_robustness.txt
0267 .. _Vulkan: https://www.khronos.org/vulkan/
0268 
0269 Requirements for Memory Maps
0270 ----------------------------
0271 
0272 Memory maps have further requirements that apply to both existing maps
0273 and maps created after the device has disappeared. If the underlying
0274 memory disappears, the map is created or modified such that reads and
0275 writes will still complete successfully but the result is undefined.
0276 This applies to both userspace mmap()'d memory and memory pointed to by
0277 dmabuf which might be mapped to other devices (cross-device dmabuf
0278 imports).
0279 
0280 Raising SIGBUS is not an option, because userspace cannot realistically
0281 handle it. Signal handlers are global, which makes them extremely
0282 difficult to use correctly from libraries like those that Mesa produces.
0283 Signal handlers are not composable, you can't have different handlers
0284 for GPU1 and GPU2 from different vendors, and a third handler for
0285 mmapped regular files. Threads cause additional pain with signal
0286 handling as well.
0287 
0288 .. _drm_driver_ioctl:
0289 
0290 IOCTL Support on Device Nodes
0291 =============================
0292 
0293 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
0294    :doc: driver specific ioctls
0295 
0296 Recommended IOCTL Return Values
0297 -------------------------------
0298 
0299 In theory a driver's IOCTL callback is only allowed to return very few error
0300 codes. In practice it's good to abuse a few more. This section documents common
0301 practice within the DRM subsystem:
0302 
0303 ENOENT:
0304         Strictly this should only be used when a file doesn't exist e.g. when
0305         calling the open() syscall. We reuse that to signal any kind of object
0306         lookup failure, e.g. for unknown GEM buffer object handles, unknown KMS
0307         object handles and similar cases.
0308 
0309 ENOSPC:
0310         Some drivers use this to differentiate "out of kernel memory" from "out
0311         of VRAM". Sometimes also applies to other limited gpu resources used for
0312         rendering (e.g. when you have a special limited compression buffer).
0313         Sometimes resource allocation/reservation issues in command submission
0314         IOCTLs are also signalled through EDEADLK.
0315 
0316         Simply running out of kernel/system memory is signalled through ENOMEM.
0317 
0318 EPERM/EACCES:
0319         Returned for an operation that is valid, but needs more privileges.
0320         E.g. root-only or much more common, DRM master-only operations return
0321         this when called by unpriviledged clients. There's no clear
0322         difference between EACCES and EPERM.
0323 
0324 ENODEV:
0325         The device is not present anymore or is not yet fully initialized.
0326 
0327 EOPNOTSUPP:
0328         Feature (like PRIME, modesetting, GEM) is not supported by the driver.
0329 
0330 ENXIO:
0331         Remote failure, either a hardware transaction (like i2c), but also used
0332         when the exporting driver of a shared dma-buf or fence doesn't support a
0333         feature needed.
0334 
0335 EINTR:
0336         DRM drivers assume that userspace restarts all IOCTLs. Any DRM IOCTL can
0337         return EINTR and in such a case should be restarted with the IOCTL
0338         parameters left unchanged.
0339 
0340 EIO:
0341         The GPU died and couldn't be resurrected through a reset. Modesetting
0342         hardware failures are signalled through the "link status" connector
0343         property.
0344 
0345 EINVAL:
0346         Catch-all for anything that is an invalid argument combination which
0347         cannot work.
0348 
0349 IOCTL also use other error codes like ETIME, EFAULT, EBUSY, ENOTTY but their
0350 usage is in line with the common meanings. The above list tries to just document
0351 DRM specific patterns. Note that ENOTTY has the slightly unintuitive meaning of
0352 "this IOCTL does not exist", and is used exactly as such in DRM.
0353 
0354 .. kernel-doc:: include/drm/drm_ioctl.h
0355    :internal:
0356 
0357 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
0358    :export:
0359 
0360 .. kernel-doc:: drivers/gpu/drm/drm_ioc32.c
0361    :export:
0362 
0363 Testing and validation
0364 ======================
0365 
0366 Testing Requirements for userspace API
0367 --------------------------------------
0368 
0369 New cross-driver userspace interface extensions, like new IOCTL, new KMS
0370 properties, new files in sysfs or anything else that constitutes an API change
0371 should have driver-agnostic testcases in IGT for that feature, if such a test
0372 can be reasonably made using IGT for the target hardware.
0373 
0374 Validating changes with IGT
0375 ---------------------------
0376 
0377 There's a collection of tests that aims to cover the whole functionality of
0378 DRM drivers and that can be used to check that changes to DRM drivers or the
0379 core don't regress existing functionality. This test suite is called IGT and
0380 its code and instructions to build and run can be found in
0381 https://gitlab.freedesktop.org/drm/igt-gpu-tools/.
0382 
0383 Using VKMS to test DRM API
0384 --------------------------
0385 
0386 VKMS is a software-only model of a KMS driver that is useful for testing
0387 and for running compositors. VKMS aims to enable a virtual display without
0388 the need for a hardware display capability. These characteristics made VKMS
0389 a perfect tool for validating the DRM core behavior and also support the
0390 compositor developer. VKMS makes it possible to test DRM functions in a
0391 virtual machine without display, simplifying the validation of some of the
0392 core changes.
0393 
0394 To Validate changes in DRM API with VKMS, start setting the kernel: make
0395 sure to enable VKMS module; compile the kernel with the VKMS enabled and
0396 install it in the target machine. VKMS can be run in a Virtual Machine
0397 (QEMU, virtme or similar). It's recommended the use of KVM with the minimum
0398 of 1GB of RAM and four cores.
0399 
0400 It's possible to run the IGT-tests in a VM in two ways:
0401 
0402         1. Use IGT inside a VM
0403         2. Use IGT from the host machine and write the results in a shared directory.
0404 
0405 As follow, there is an example of using a VM with a shared directory with
0406 the host machine to run igt-tests. As an example it's used virtme::
0407 
0408         $ virtme-run --rwdir /path/for/shared_dir --kdir=path/for/kernel/directory --mods=auto
0409 
0410 Run the igt-tests in the guest machine, as example it's ran the 'kms_flip'
0411 tests::
0412 
0413         $ /path/for/igt-gpu-tools/scripts/run-tests.sh -p -s -t "kms_flip.*" -v
0414 
0415 In this example, instead of build the igt_runner, Piglit is used
0416 (-p option); it's created html summary of the tests results and it's saved
0417 in the folder "igt-gpu-tools/results"; it's executed only the igt-tests
0418 matching the -t option.
0419 
0420 Display CRC Support
0421 -------------------
0422 
0423 .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
0424    :doc: CRC ABI
0425 
0426 .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
0427    :export:
0428 
0429 Debugfs Support
0430 ---------------
0431 
0432 .. kernel-doc:: include/drm/drm_debugfs.h
0433    :internal:
0434 
0435 .. kernel-doc:: drivers/gpu/drm/drm_debugfs.c
0436    :export:
0437 
0438 Sysfs Support
0439 =============
0440 
0441 .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
0442    :doc: overview
0443 
0444 .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
0445    :export:
0446 
0447 
0448 VBlank event handling
0449 =====================
0450 
0451 The DRM core exposes two vertical blank related ioctls:
0452 
0453 DRM_IOCTL_WAIT_VBLANK
0454     This takes a struct drm_wait_vblank structure as its argument, and
0455     it is used to block or request a signal when a specified vblank
0456     event occurs.
0457 
0458 DRM_IOCTL_MODESET_CTL
0459     This was only used for user-mode-settind drivers around modesetting
0460     changes to allow the kernel to update the vblank interrupt after
0461     mode setting, since on many devices the vertical blank counter is
0462     reset to 0 at some point during modeset. Modern drivers should not
0463     call this any more since with kernel mode setting it is a no-op.
0464 
0465 Userspace API Structures
0466 ========================
0467 
0468 .. kernel-doc:: include/uapi/drm/drm_mode.h
0469    :doc: overview
0470 
0471 .. _crtc_index:
0472 
0473 CRTC index
0474 ----------
0475 
0476 CRTC's have both an object ID and an index, and they are not the same thing.
0477 The index is used in cases where a densely packed identifier for a CRTC is
0478 needed, for instance a bitmask of CRTC's. The member possible_crtcs of struct
0479 drm_mode_get_plane is an example.
0480 
0481 DRM_IOCTL_MODE_GETRESOURCES populates a structure with an array of CRTC ID's,
0482 and the CRTC index is its position in this array.
0483 
0484 .. kernel-doc:: include/uapi/drm/drm.h
0485    :internal:
0486 
0487 .. kernel-doc:: include/uapi/drm/drm_mode.h
0488    :internal: