drm-internals.rst 7.8 KB

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