drm-uapi.rst 19 KB

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