drm-internals.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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, Description and Date
  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. The driver date, formatted as YYYYMMDD, is meant to identify the date of
  59. the latest modification to the driver. However, as most drivers fail to
  60. update it, its value is mostly useless. The DRM core prints it to the
  61. kernel log at initialization time and passes it to userspace through the
  62. DRM_IOCTL_VERSION ioctl.
  63. Module Initialization
  64. ---------------------
  65. .. kernel-doc:: include/drm/drm_module.h
  66. :doc: overview
  67. Managing Ownership of the Framebuffer Aperture
  68. ----------------------------------------------
  69. .. kernel-doc:: drivers/gpu/drm/drm_aperture.c
  70. :doc: overview
  71. .. kernel-doc:: include/drm/drm_aperture.h
  72. :internal:
  73. .. kernel-doc:: drivers/gpu/drm/drm_aperture.c
  74. :export:
  75. Device Instance and Driver Handling
  76. -----------------------------------
  77. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  78. :doc: driver instance overview
  79. .. kernel-doc:: include/drm/drm_device.h
  80. :internal:
  81. .. kernel-doc:: include/drm/drm_drv.h
  82. :internal:
  83. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  84. :export:
  85. Driver Load
  86. -----------
  87. Component Helper Usage
  88. ~~~~~~~~~~~~~~~~~~~~~~
  89. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  90. :doc: component helper usage recommendations
  91. Memory Manager Initialization
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. Every DRM driver requires a memory manager which must be initialized at
  94. load time. DRM currently contains two memory managers, the Translation
  95. Table Manager (TTM) and the Graphics Execution Manager (GEM). This
  96. document describes the use of the GEM memory manager only. See ? for
  97. details.
  98. Miscellaneous Device Configuration
  99. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. Another task that may be necessary for PCI devices during configuration
  101. is mapping the video BIOS. On many devices, the VBIOS describes device
  102. configuration, LCD panel timings (if any), and contains flags indicating
  103. device state. Mapping the BIOS can be done using the pci_map_rom()
  104. call, a convenience function that takes care of mapping the actual ROM,
  105. whether it has been shadowed into memory (typically at address 0xc0000)
  106. or exists on the PCI device in the ROM BAR. Note that after the ROM has
  107. been mapped and any necessary information has been extracted, it should
  108. be unmapped; on many devices, the ROM address decoder is shared with
  109. other BARs, so leaving it mapped could cause undesired behaviour like
  110. hangs or memory corruption.
  111. Managed Resources
  112. -----------------
  113. .. kernel-doc:: drivers/gpu/drm/drm_managed.c
  114. :doc: managed resources
  115. .. kernel-doc:: drivers/gpu/drm/drm_managed.c
  116. :export:
  117. .. kernel-doc:: include/drm/drm_managed.h
  118. :internal:
  119. Bus-specific Device Registration and PCI Support
  120. ------------------------------------------------
  121. A number of functions are provided to help with device registration. The
  122. functions deal with PCI and platform devices respectively and are only
  123. provided for historical reasons. These are all deprecated and shouldn't
  124. be used in new drivers. Besides that there's a few helpers for pci
  125. drivers.
  126. .. kernel-doc:: drivers/gpu/drm/drm_pci.c
  127. :export:
  128. Open/Close, File Operations and IOCTLs
  129. ======================================
  130. .. _drm_driver_fops:
  131. File Operations
  132. ---------------
  133. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  134. :doc: file operations
  135. .. kernel-doc:: include/drm/drm_file.h
  136. :internal:
  137. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  138. :export:
  139. Misc Utilities
  140. ==============
  141. Printer
  142. -------
  143. .. kernel-doc:: include/drm/drm_print.h
  144. :doc: print
  145. .. kernel-doc:: include/drm/drm_print.h
  146. :internal:
  147. .. kernel-doc:: drivers/gpu/drm/drm_print.c
  148. :export:
  149. Utilities
  150. ---------
  151. .. kernel-doc:: include/drm/drm_util.h
  152. :doc: drm utils
  153. .. kernel-doc:: include/drm/drm_util.h
  154. :internal:
  155. Unit testing
  156. ============
  157. KUnit
  158. -----
  159. KUnit (Kernel unit testing framework) provides a common framework for unit tests
  160. within the Linux kernel.
  161. This section covers the specifics for the DRM subsystem. For general information
  162. about KUnit, please refer to Documentation/dev-tools/kunit/start.rst.
  163. How to run the tests?
  164. ~~~~~~~~~~~~~~~~~~~~~
  165. In order to facilitate running the test suite, a configuration file is present
  166. in ``drivers/gpu/drm/tests/.kunitconfig``. It can be used by ``kunit.py`` as
  167. follows:
  168. .. code-block:: bash
  169. $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/tests \
  170. --kconfig_add CONFIG_VIRTIO_UML=y \
  171. --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y
  172. .. note::
  173. The configuration included in ``.kunitconfig`` should be as generic as
  174. possible.
  175. ``CONFIG_VIRTIO_UML`` and ``CONFIG_UML_PCI_OVER_VIRTIO`` are not
  176. included in it because they are only required for User Mode Linux.
  177. Legacy Support Code
  178. ===================
  179. The section very briefly covers some of the old legacy support code
  180. which is only used by old DRM drivers which have done a so-called
  181. shadow-attach to the underlying device instead of registering as a real
  182. driver. This also includes some of the old generic buffer management and
  183. command submission code. Do not use any of this in new and modern
  184. drivers.
  185. Legacy Suspend/Resume
  186. ---------------------
  187. The DRM core provides some suspend/resume code, but drivers wanting full
  188. suspend/resume support should provide save() and restore() functions.
  189. These are called at suspend, hibernate, or resume time, and should
  190. perform any state save or restore required by your device across suspend
  191. or hibernate states.
  192. int (\*suspend) (struct drm_device \*, pm_message_t state); int
  193. (\*resume) (struct drm_device \*);
  194. Those are legacy suspend and resume methods which *only* work with the
  195. legacy shadow-attach driver registration functions. New driver should
  196. use the power management interface provided by their bus type (usually
  197. through the :c:type:`struct device_driver <device_driver>`
  198. dev_pm_ops) and set these methods to NULL.
  199. Legacy DMA Services
  200. -------------------
  201. This should cover how DMA mapping etc. is supported by the core. These
  202. functions are deprecated and should not be used.