drm-uapi.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ===================
  2. Userland interfaces
  3. ===================
  4. The DRM core exports several interfaces to applications, generally
  5. intended to be used through corresponding libdrm wrapper functions. In
  6. addition, drivers export device-specific interfaces for use by userspace
  7. drivers & device-aware applications through ioctls and sysfs files.
  8. External interfaces include: memory mapping, context management, DMA
  9. operations, AGP management, vblank control, fence management, memory
  10. management, and output management.
  11. Cover generic ioctls and sysfs layout here. We only need high-level
  12. info, since man pages should cover the rest.
  13. libdrm Device Lookup
  14. ====================
  15. .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
  16. :doc: getunique and setversion story
  17. Primary Nodes, DRM Master and Authentication
  18. ============================================
  19. .. kernel-doc:: drivers/gpu/drm/drm_auth.c
  20. :doc: master and authentication
  21. .. kernel-doc:: drivers/gpu/drm/drm_auth.c
  22. :export:
  23. .. kernel-doc:: include/drm/drm_auth.h
  24. :internal:
  25. Open-Source Userspace Requirements
  26. ==================================
  27. The DRM subsystem has stricter requirements than most other kernel subsystems on
  28. what the userspace side for new uAPI needs to look like. This section here
  29. explains what exactly those requirements are, and why they exist.
  30. The short summary is that any addition of DRM uAPI requires corresponding
  31. open-sourced userspace patches, and those patches must be reviewed and ready for
  32. merging into a suitable and canonical upstream project.
  33. GFX devices (both display and render/GPU side) are really complex bits of
  34. hardware, with userspace and kernel by necessity having to work together really
  35. closely. The interfaces, for rendering and modesetting, must be extremely wide
  36. and flexible, and therefore it is almost always impossible to precisely define
  37. them for every possible corner case. This in turn makes it really practically
  38. infeasible to differentiate between behaviour that's required by userspace, and
  39. which must not be changed to avoid regressions, and behaviour which is only an
  40. accidental artifact of the current implementation.
  41. Without access to the full source code of all userspace users that means it
  42. becomes impossible to change the implementation details, since userspace could
  43. depend upon the accidental behaviour of the current implementation in minute
  44. details. And debugging such regressions without access to source code is pretty
  45. much impossible. As a consequence this means:
  46. - The Linux kernel's "no regression" policy holds in practice only for
  47. open-source userspace of the DRM subsystem. DRM developers are perfectly fine
  48. if closed-source blob drivers in userspace use the same uAPI as the open
  49. drivers, but they must do so in the exact same way as the open drivers.
  50. Creative (ab)use of the interfaces will, and in the past routinely has, lead
  51. to breakage.
  52. - Any new userspace interface must have an open-source implementation as
  53. demonstration vehicle.
  54. The other reason for requiring open-source userspace is uAPI review. Since the
  55. kernel and userspace parts of a GFX stack must work together so closely, code
  56. review can only assess whether a new interface achieves its goals by looking at
  57. both sides. Making sure that the interface indeed covers the use-case fully
  58. leads to a few additional requirements:
  59. - The open-source userspace must not be a toy/test application, but the real
  60. thing. Specifically it needs to handle all the usual error and corner cases.
  61. These are often the places where new uAPI falls apart and hence essential to
  62. assess the fitness of a proposed interface.
  63. - The userspace side must be fully reviewed and tested to the standards of that
  64. userspace project. For e.g. mesa this means piglit testcases and review on the
  65. mailing list. This is again to ensure that the new interface actually gets the
  66. job done.
  67. - The userspace patches must be against the canonical upstream, not some vendor
  68. fork. This is to make sure that no one cheats on the review and testing
  69. requirements by doing a quick fork.
  70. - The kernel patch can only be merged after all the above requirements are met,
  71. but it **must** be merged **before** the userspace patches land. uAPI always flows
  72. from the kernel, doing things the other way round risks divergence of the uAPI
  73. definitions and header files.
  74. These are fairly steep requirements, but have grown out from years of shared
  75. pain and experience with uAPI added hastily, and almost always regretted about
  76. just as fast. GFX devices change really fast, requiring a paradigm shift and
  77. entire new set of uAPI interfaces every few years at least. Together with the
  78. Linux kernel's guarantee to keep existing userspace running for 10+ years this
  79. is already rather painful for the DRM subsystem, with multiple different uAPIs
  80. for the same thing co-existing. If we add a few more complete mistakes into the
  81. mix every year it would be entirely unmanageable.
  82. Render nodes
  83. ============
  84. DRM core provides multiple character-devices for user-space to use.
  85. Depending on which device is opened, user-space can perform a different
  86. set of operations (mainly ioctls). The primary node is always created
  87. and called card<num>. Additionally, a currently unused control node,
  88. called controlD<num> is also created. The primary node provides all
  89. legacy operations and historically was the only interface used by
  90. userspace. With KMS, the control node was introduced. However, the
  91. planned KMS control interface has never been written and so the control
  92. node stays unused to date.
  93. With the increased use of offscreen renderers and GPGPU applications,
  94. clients no longer require running compositors or graphics servers to
  95. make use of a GPU. But the DRM API required unprivileged clients to
  96. authenticate to a DRM-Master prior to getting GPU access. To avoid this
  97. step and to grant clients GPU access without authenticating, render
  98. nodes were introduced. Render nodes solely serve render clients, that
  99. is, no modesetting or privileged ioctls can be issued on render nodes.
  100. Only non-global rendering commands are allowed. If a driver supports
  101. render nodes, it must advertise it via the DRIVER_RENDER DRM driver
  102. capability. If not supported, the primary node must be used for render
  103. clients together with the legacy drmAuth authentication procedure.
  104. If a driver advertises render node support, DRM core will create a
  105. separate render node called renderD<num>. There will be one render node
  106. per device. No ioctls except PRIME-related ioctls will be allowed on
  107. this node. Especially GEM_OPEN will be explicitly prohibited. Render
  108. nodes are designed to avoid the buffer-leaks, which occur if clients
  109. guess the flink names or mmap offsets on the legacy interface.
  110. Additionally to this basic interface, drivers must mark their
  111. driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
  112. clients can use them. Driver authors must be careful not to allow any
  113. privileged ioctls on render nodes.
  114. With render nodes, user-space can now control access to the render node
  115. via basic file-system access-modes. A running graphics server which
  116. authenticates clients on the privileged primary/legacy node is no longer
  117. required. Instead, a client can open the render node and is immediately
  118. granted GPU access. Communication between clients (or servers) is done
  119. via PRIME. FLINK from render node to legacy node is not supported. New
  120. clients must not use the insecure FLINK interface.
  121. Besides dropping all modeset/global ioctls, render nodes also drop the
  122. DRM-Master concept. There is no reason to associate render clients with
  123. a DRM-Master as they are independent of any graphics server. Besides,
  124. they must work without any running master, anyway. Drivers must be able
  125. to run without a master object if they support render nodes. If, on the
  126. other hand, a driver requires shared state between clients which is
  127. visible to user-space and accessible beyond open-file boundaries, they
  128. cannot support render nodes.
  129. Validating changes with IGT
  130. ===========================
  131. There's a collection of tests that aims to cover the whole functionality of
  132. DRM drivers and that can be used to check that changes to DRM drivers or the
  133. core don't regress existing functionality. This test suite is called IGT and
  134. its code can be found in https://cgit.freedesktop.org/drm/igt-gpu-tools/.
  135. To build IGT, start by installing its build dependencies. In Debian-based
  136. systems::
  137. # apt-get build-dep intel-gpu-tools
  138. And in Fedora-based systems::
  139. # dnf builddep intel-gpu-tools
  140. Then clone the repository::
  141. $ git clone git://anongit.freedesktop.org/drm/igt-gpu-tools
  142. Configure the build system and start the build::
  143. $ cd igt-gpu-tools && ./autogen.sh && make -j6
  144. Download the piglit dependency::
  145. $ ./scripts/run-tests.sh -d
  146. And run the tests::
  147. $ ./scripts/run-tests.sh -t kms -t core -s
  148. run-tests.sh is a wrapper around piglit that will execute the tests matching
  149. the -t options. A report in HTML format will be available in
  150. ./results/html/index.html. Results can be compared with piglit.
  151. VBlank event handling
  152. =====================
  153. The DRM core exposes two vertical blank related ioctls:
  154. DRM_IOCTL_WAIT_VBLANK
  155. This takes a struct drm_wait_vblank structure as its argument, and
  156. it is used to block or request a signal when a specified vblank
  157. event occurs.
  158. DRM_IOCTL_MODESET_CTL
  159. This was only used for user-mode-settind drivers around modesetting
  160. changes to allow the kernel to update the vblank interrupt after
  161. mode setting, since on many devices the vertical blank counter is
  162. reset to 0 at some point during modeset. Modern drivers should not
  163. call this any more since with kernel mode setting it is a no-op.
  164. This second part of the GPU Driver Developer's Guide documents driver
  165. code, implementation details and also all the driver-specific userspace
  166. interfaces. Especially since all hardware-acceleration interfaces to
  167. userspace are driver specific for efficiency and other reasons these
  168. interfaces can be rather substantial. Hence every driver has its own
  169. chapter.