drm-uapi.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. .. _drm_primary_node:
  18. Primary Nodes, DRM Master and Authentication
  19. ============================================
  20. .. kernel-doc:: drivers/gpu/drm/drm_auth.c
  21. :doc: master and authentication
  22. .. kernel-doc:: drivers/gpu/drm/drm_auth.c
  23. :export:
  24. .. kernel-doc:: include/drm/drm_auth.h
  25. :internal:
  26. Open-Source Userspace Requirements
  27. ==================================
  28. The DRM subsystem has stricter requirements than most other kernel subsystems on
  29. what the userspace side for new uAPI needs to look like. This section here
  30. explains what exactly those requirements are, and why they exist.
  31. The short summary is that any addition of DRM uAPI requires corresponding
  32. open-sourced userspace patches, and those patches must be reviewed and ready for
  33. merging into a suitable and canonical upstream project.
  34. GFX devices (both display and render/GPU side) are really complex bits of
  35. hardware, with userspace and kernel by necessity having to work together really
  36. closely. The interfaces, for rendering and modesetting, must be extremely wide
  37. and flexible, and therefore it is almost always impossible to precisely define
  38. them for every possible corner case. This in turn makes it really practically
  39. infeasible to differentiate between behaviour that's required by userspace, and
  40. which must not be changed to avoid regressions, and behaviour which is only an
  41. accidental artifact of the current implementation.
  42. Without access to the full source code of all userspace users that means it
  43. becomes impossible to change the implementation details, since userspace could
  44. depend upon the accidental behaviour of the current implementation in minute
  45. details. And debugging such regressions without access to source code is pretty
  46. much impossible. As a consequence this means:
  47. - The Linux kernel's "no regression" policy holds in practice only for
  48. open-source userspace of the DRM subsystem. DRM developers are perfectly fine
  49. if closed-source blob drivers in userspace use the same uAPI as the open
  50. drivers, but they must do so in the exact same way as the open drivers.
  51. Creative (ab)use of the interfaces will, and in the past routinely has, lead
  52. to breakage.
  53. - Any new userspace interface must have an open-source implementation as
  54. demonstration vehicle.
  55. The other reason for requiring open-source userspace is uAPI review. Since the
  56. kernel and userspace parts of a GFX stack must work together so closely, code
  57. review can only assess whether a new interface achieves its goals by looking at
  58. both sides. Making sure that the interface indeed covers the use-case fully
  59. leads to a few additional requirements:
  60. - The open-source userspace must not be a toy/test application, but the real
  61. thing. Specifically it needs to handle all the usual error and corner cases.
  62. These are often the places where new uAPI falls apart and hence essential to
  63. assess the fitness of a proposed interface.
  64. - The userspace side must be fully reviewed and tested to the standards of that
  65. userspace project. For e.g. mesa this means piglit testcases and review on the
  66. mailing list. This is again to ensure that the new interface actually gets the
  67. job done.
  68. - The userspace patches must be against the canonical upstream, not some vendor
  69. fork. This is to make sure that no one cheats on the review and testing
  70. requirements by doing a quick fork.
  71. - The kernel patch can only be merged after all the above requirements are met,
  72. but it **must** be merged **before** the userspace patches land. uAPI always flows
  73. from the kernel, doing things the other way round risks divergence of the uAPI
  74. definitions and header files.
  75. These are fairly steep requirements, but have grown out from years of shared
  76. pain and experience with uAPI added hastily, and almost always regretted about
  77. just as fast. GFX devices change really fast, requiring a paradigm shift and
  78. entire new set of uAPI interfaces every few years at least. Together with the
  79. Linux kernel's guarantee to keep existing userspace running for 10+ years this
  80. is already rather painful for the DRM subsystem, with multiple different uAPIs
  81. for the same thing co-existing. If we add a few more complete mistakes into the
  82. mix every year it would be entirely unmanageable.
  83. .. _drm_render_node:
  84. Render nodes
  85. ============
  86. DRM core provides multiple character-devices for user-space to use.
  87. Depending on which device is opened, user-space can perform a different
  88. set of operations (mainly ioctls). The primary node is always created
  89. and called card<num>. Additionally, a currently unused control node,
  90. called controlD<num> is also created. The primary node provides all
  91. legacy operations and historically was the only interface used by
  92. userspace. With KMS, the control node was introduced. However, the
  93. planned KMS control interface has never been written and so the control
  94. node stays unused to date.
  95. With the increased use of offscreen renderers and GPGPU applications,
  96. clients no longer require running compositors or graphics servers to
  97. make use of a GPU. But the DRM API required unprivileged clients to
  98. authenticate to a DRM-Master prior to getting GPU access. To avoid this
  99. step and to grant clients GPU access without authenticating, render
  100. nodes were introduced. Render nodes solely serve render clients, that
  101. is, no modesetting or privileged ioctls can be issued on render nodes.
  102. Only non-global rendering commands are allowed. If a driver supports
  103. render nodes, it must advertise it via the DRIVER_RENDER DRM driver
  104. capability. If not supported, the primary node must be used for render
  105. clients together with the legacy drmAuth authentication procedure.
  106. If a driver advertises render node support, DRM core will create a
  107. separate render node called renderD<num>. There will be one render node
  108. per device. No ioctls except PRIME-related ioctls will be allowed on
  109. this node. Especially GEM_OPEN will be explicitly prohibited. Render
  110. nodes are designed to avoid the buffer-leaks, which occur if clients
  111. guess the flink names or mmap offsets on the legacy interface.
  112. Additionally to this basic interface, drivers must mark their
  113. driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
  114. clients can use them. Driver authors must be careful not to allow any
  115. privileged ioctls on render nodes.
  116. With render nodes, user-space can now control access to the render node
  117. via basic file-system access-modes. A running graphics server which
  118. authenticates clients on the privileged primary/legacy node is no longer
  119. required. Instead, a client can open the render node and is immediately
  120. granted GPU access. Communication between clients (or servers) is done
  121. via PRIME. FLINK from render node to legacy node is not supported. New
  122. clients must not use the insecure FLINK interface.
  123. Besides dropping all modeset/global ioctls, render nodes also drop the
  124. DRM-Master concept. There is no reason to associate render clients with
  125. a DRM-Master as they are independent of any graphics server. Besides,
  126. they must work without any running master, anyway. Drivers must be able
  127. to run without a master object if they support render nodes. If, on the
  128. other hand, a driver requires shared state between clients which is
  129. visible to user-space and accessible beyond open-file boundaries, they
  130. cannot support render nodes.
  131. .. _drm_driver_ioctl:
  132. IOCTL Support on Device Nodes
  133. =============================
  134. .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
  135. :doc: driver specific ioctls
  136. Recommended IOCTL Return Values
  137. -------------------------------
  138. In theory a driver's IOCTL callback is only allowed to return very few error
  139. codes. In practice it's good to abuse a few more. This section documents common
  140. practice within the DRM subsystem:
  141. ENOENT:
  142. Strictly this should only be used when a file doesn't exist e.g. when
  143. calling the open() syscall. We reuse that to signal any kind of object
  144. lookup failure, e.g. for unknown GEM buffer object handles, unknown KMS
  145. object handles and similar cases.
  146. ENOSPC:
  147. Some drivers use this to differentiate "out of kernel memory" from "out
  148. of VRAM". Sometimes also applies to other limited gpu resources used for
  149. rendering (e.g. when you have a special limited compression buffer).
  150. Sometimes resource allocation/reservation issues in command submission
  151. IOCTLs are also signalled through EDEADLK.
  152. Simply running out of kernel/system memory is signalled through ENOMEM.
  153. EPERM/EACCESS:
  154. Returned for an operation that is valid, but needs more privileges.
  155. E.g. root-only or much more common, DRM master-only operations return
  156. this when when called by unpriviledged clients. There's no clear
  157. difference between EACCESS and EPERM.
  158. ENODEV:
  159. Feature (like PRIME, modesetting, GEM) is not supported by the driver.
  160. ENXIO:
  161. Remote failure, either a hardware transaction (like i2c), but also used
  162. when the exporting driver of a shared dma-buf or fence doesn't support a
  163. feature needed.
  164. EINTR:
  165. DRM drivers assume that userspace restarts all IOCTLs. Any DRM IOCTL can
  166. return EINTR and in such a case should be restarted with the IOCTL
  167. parameters left unchanged.
  168. EIO:
  169. The GPU died and couldn't be resurrected through a reset. Modesetting
  170. hardware failures are signalled through the "link status" connector
  171. property.
  172. EINVAL:
  173. Catch-all for anything that is an invalid argument combination which
  174. cannot work.
  175. IOCTL also use other error codes like ETIME, EFAULT, EBUSY, ENOTTY but their
  176. usage is in line with the common meanings. The above list tries to just document
  177. DRM specific patterns. Note that ENOTTY has the slightly unintuitive meaning of
  178. "this IOCTL does not exist", and is used exactly as such in DRM.
  179. .. kernel-doc:: include/drm/drm_ioctl.h
  180. :internal:
  181. .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
  182. :export:
  183. .. kernel-doc:: drivers/gpu/drm/drm_ioc32.c
  184. :export:
  185. Testing and validation
  186. ======================
  187. Validating changes with IGT
  188. ---------------------------
  189. There's a collection of tests that aims to cover the whole functionality of
  190. DRM drivers and that can be used to check that changes to DRM drivers or the
  191. core don't regress existing functionality. This test suite is called IGT and
  192. its code can be found in https://cgit.freedesktop.org/drm/igt-gpu-tools/.
  193. To build IGT, start by installing its build dependencies. In Debian-based
  194. systems::
  195. # apt-get build-dep intel-gpu-tools
  196. And in Fedora-based systems::
  197. # dnf builddep intel-gpu-tools
  198. Then clone the repository::
  199. $ git clone git://anongit.freedesktop.org/drm/igt-gpu-tools
  200. Configure the build system and start the build::
  201. $ cd igt-gpu-tools && ./autogen.sh && make -j6
  202. Download the piglit dependency::
  203. $ ./scripts/run-tests.sh -d
  204. And run the tests::
  205. $ ./scripts/run-tests.sh -t kms -t core -s
  206. run-tests.sh is a wrapper around piglit that will execute the tests matching
  207. the -t options. A report in HTML format will be available in
  208. ./results/html/index.html. Results can be compared with piglit.
  209. Display CRC Support
  210. -------------------
  211. .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
  212. :doc: CRC ABI
  213. .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
  214. :export:
  215. Debugfs Support
  216. ---------------
  217. .. kernel-doc:: include/drm/drm_debugfs.h
  218. :internal:
  219. .. kernel-doc:: drivers/gpu/drm/drm_debugfs.c
  220. :export:
  221. Sysfs Support
  222. =============
  223. .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
  224. :doc: overview
  225. .. kernel-doc:: drivers/gpu/drm/drm_sysfs.c
  226. :export:
  227. VBlank event handling
  228. =====================
  229. The DRM core exposes two vertical blank related ioctls:
  230. DRM_IOCTL_WAIT_VBLANK
  231. This takes a struct drm_wait_vblank structure as its argument, and
  232. it is used to block or request a signal when a specified vblank
  233. event occurs.
  234. DRM_IOCTL_MODESET_CTL
  235. This was only used for user-mode-settind drivers around modesetting
  236. changes to allow the kernel to update the vblank interrupt after
  237. mode setting, since on many devices the vertical blank counter is
  238. reset to 0 at some point during modeset. Modern drivers should not
  239. call this any more since with kernel mode setting it is a no-op.