drm-internals.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. =============
  2. DRM Internals
  3. =============
  4. This chapter documents DRM internals relevant to driver authors and
  5. developers working to add support for the latest features to existing
  6. drivers.
  7. First, we go over some typical driver initialization requirements, like
  8. setting up command buffers, creating an initial output configuration,
  9. and initializing core services. Subsequent sections cover core internals
  10. in more detail, providing implementation notes and examples.
  11. The DRM layer provides several services to graphics drivers, many of
  12. them driven by the application interfaces it provides through libdrm,
  13. the library that wraps most of the DRM ioctls. These include vblank
  14. event handling, memory management, output management, framebuffer
  15. management, command submission & fencing, suspend/resume support, and
  16. DMA services.
  17. Driver Initialization
  18. =====================
  19. At the core of every DRM driver is a :c:type:`struct drm_driver
  20. <drm_driver>` structure. Drivers typically statically initialize
  21. a drm_driver structure, and then pass it to
  22. drm_dev_alloc() to allocate a device instance. After the
  23. device instance is fully initialized it can be registered (which makes
  24. it accessible from userspace) using drm_dev_register().
  25. The :c:type:`struct drm_driver <drm_driver>` structure
  26. contains static information that describes the driver and features it
  27. supports, and pointers to methods that the DRM core will call to
  28. implement the DRM API. We will first go through the :c:type:`struct
  29. drm_driver <drm_driver>` static information fields, and will
  30. then describe individual operations in details as they get used in later
  31. sections.
  32. Driver Information
  33. ------------------
  34. Major, Minor and Patchlevel
  35. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. int major; int minor; int patchlevel;
  37. The DRM core identifies driver versions by a major, minor and patch
  38. level triplet. The information is printed to the kernel log at
  39. initialization time and passed to userspace through the
  40. DRM_IOCTL_VERSION ioctl.
  41. The major and minor numbers are also used to verify the requested driver
  42. API version passed to DRM_IOCTL_SET_VERSION. When the driver API
  43. changes between minor versions, applications can call
  44. DRM_IOCTL_SET_VERSION to select a specific version of the API. If the
  45. requested major isn't equal to the driver major, or the requested minor
  46. is larger than the driver minor, the DRM_IOCTL_SET_VERSION call will
  47. return an error. Otherwise the driver's set_version() method will be
  48. called with the requested version.
  49. Name and Description
  50. ~~~~~~~~~~~~~~~~~~~~
  51. char \*name; char \*desc; char \*date;
  52. The driver name is printed to the kernel log at initialization time,
  53. used for IRQ registration and passed to userspace through
  54. DRM_IOCTL_VERSION.
  55. The driver description is a purely informative string passed to
  56. userspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
  57. the kernel.
  58. Module Initialization
  59. ---------------------
  60. .. kernel-doc:: include/drm/drm_module.h
  61. :doc: overview
  62. Device Instance and Driver Handling
  63. -----------------------------------
  64. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  65. :doc: driver instance overview
  66. .. kernel-doc:: include/drm/drm_device.h
  67. :internal:
  68. .. kernel-doc:: include/drm/drm_drv.h
  69. :internal:
  70. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  71. :export:
  72. Driver Load
  73. -----------
  74. Component Helper Usage
  75. ~~~~~~~~~~~~~~~~~~~~~~
  76. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  77. :doc: component helper usage recommendations
  78. Memory Manager Initialization
  79. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80. Every DRM driver requires a memory manager which must be initialized at
  81. load time. DRM currently contains two memory managers, the Translation
  82. Table Manager (TTM) and the Graphics Execution Manager (GEM). This
  83. document describes the use of the GEM memory manager only. See ? for
  84. details.
  85. Miscellaneous Device Configuration
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. Another task that may be necessary for PCI devices during configuration
  88. is mapping the video BIOS. On many devices, the VBIOS describes device
  89. configuration, LCD panel timings (if any), and contains flags indicating
  90. device state. Mapping the BIOS can be done using the pci_map_rom()
  91. call, a convenience function that takes care of mapping the actual ROM,
  92. whether it has been shadowed into memory (typically at address 0xc0000)
  93. or exists on the PCI device in the ROM BAR. Note that after the ROM has
  94. been mapped and any necessary information has been extracted, it should
  95. be unmapped; on many devices, the ROM address decoder is shared with
  96. other BARs, so leaving it mapped could cause undesired behaviour like
  97. hangs or memory corruption.
  98. Managed Resources
  99. -----------------
  100. .. kernel-doc:: drivers/gpu/drm/drm_managed.c
  101. :doc: managed resources
  102. .. kernel-doc:: drivers/gpu/drm/drm_managed.c
  103. :export:
  104. .. kernel-doc:: include/drm/drm_managed.h
  105. :internal:
  106. Open/Close, File Operations and IOCTLs
  107. ======================================
  108. .. _drm_driver_fops:
  109. File Operations
  110. ---------------
  111. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  112. :doc: file operations
  113. .. kernel-doc:: include/drm/drm_file.h
  114. :internal:
  115. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  116. :export:
  117. Misc Utilities
  118. ==============
  119. Printer
  120. -------
  121. .. kernel-doc:: include/drm/drm_print.h
  122. :doc: print
  123. .. kernel-doc:: include/drm/drm_print.h
  124. :internal:
  125. .. kernel-doc:: drivers/gpu/drm/drm_print.c
  126. :export:
  127. Utilities
  128. ---------
  129. .. kernel-doc:: include/drm/drm_util.h
  130. :doc: drm utils
  131. .. kernel-doc:: include/drm/drm_util.h
  132. :internal:
  133. Unit testing
  134. ============
  135. KUnit
  136. -----
  137. KUnit (Kernel unit testing framework) provides a common framework for unit tests
  138. within the Linux kernel.
  139. This section covers the specifics for the DRM subsystem. For general information
  140. about KUnit, please refer to Documentation/dev-tools/kunit/start.rst.
  141. How to run the tests?
  142. ~~~~~~~~~~~~~~~~~~~~~
  143. In order to facilitate running the test suite, a configuration file is present
  144. in ``drivers/gpu/drm/tests/.kunitconfig``. It can be used by ``kunit.py`` as
  145. follows:
  146. .. code-block:: bash
  147. $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/tests \
  148. --kconfig_add CONFIG_VIRTIO_UML=y \
  149. --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y
  150. .. note::
  151. The configuration included in ``.kunitconfig`` should be as generic as
  152. possible.
  153. ``CONFIG_VIRTIO_UML`` and ``CONFIG_UML_PCI_OVER_VIRTIO`` are not
  154. included in it because they are only required for User Mode Linux.
  155. KUnit Coverage Rules
  156. ~~~~~~~~~~~~~~~~~~~~
  157. KUnit support is gradually added to the DRM framework and helpers. There's no
  158. general requirement for the framework and helpers to have KUnit tests at the
  159. moment. However, patches that are affecting a function or helper already
  160. covered by KUnit tests must provide tests if the change calls for one.
  161. Legacy Support Code
  162. ===================
  163. The section very briefly covers some of the old legacy support code
  164. which is only used by old DRM drivers which have done a so-called
  165. shadow-attach to the underlying device instead of registering as a real
  166. driver. This also includes some of the old generic buffer management and
  167. command submission code. Do not use any of this in new and modern
  168. drivers.
  169. Legacy Suspend/Resume
  170. ---------------------
  171. The DRM core provides some suspend/resume code, but drivers wanting full
  172. suspend/resume support should provide save() and restore() functions.
  173. These are called at suspend, hibernate, or resume time, and should
  174. perform any state save or restore required by your device across suspend
  175. or hibernate states.
  176. int (\*suspend) (struct drm_device \*, pm_message_t state); int
  177. (\*resume) (struct drm_device \*);
  178. Those are legacy suspend and resume methods which *only* work with the
  179. legacy shadow-attach driver registration functions. New driver should
  180. use the power management interface provided by their bus type (usually
  181. through the :c:type:`struct device_driver <device_driver>`
  182. dev_pm_ops) and set these methods to NULL.
  183. Legacy DMA Services
  184. -------------------
  185. This should cover how DMA mapping etc. is supported by the core. These
  186. functions are deprecated and should not be used.