drm-internals.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. :c:func:`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 :c:func:`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. Driver Features
  35. ~~~~~~~~~~~~~~~
  36. Drivers inform the DRM core about their requirements and supported
  37. features by setting appropriate flags in the driver_features field.
  38. Since those flags influence the DRM core behaviour since registration
  39. time, most of them must be set to registering the :c:type:`struct
  40. drm_driver <drm_driver>` instance.
  41. u32 driver_features;
  42. DRIVER_USE_AGP
  43. Driver uses AGP interface, the DRM core will manage AGP resources.
  44. DRIVER_LEGACY
  45. Denote a legacy driver using shadow attach. Don't use.
  46. DRIVER_KMS_LEGACY_CONTEXT
  47. Used only by nouveau for backwards compatibility with existing userspace.
  48. Don't use.
  49. DRIVER_PCI_DMA
  50. Driver is capable of PCI DMA, mapping of PCI DMA buffers to
  51. userspace will be enabled. Deprecated.
  52. DRIVER_SG
  53. Driver can perform scatter/gather DMA, allocation and mapping of
  54. scatter/gather buffers will be enabled. Deprecated.
  55. DRIVER_HAVE_DMA
  56. Driver supports DMA, the userspace DMA API will be supported.
  57. Deprecated.
  58. DRIVER_HAVE_IRQ; DRIVER_IRQ_SHARED
  59. DRIVER_HAVE_IRQ indicates whether the driver has an IRQ handler
  60. managed by the DRM Core. The core will support simple IRQ handler
  61. installation when the flag is set. The installation process is
  62. described in ?.
  63. DRIVER_IRQ_SHARED indicates whether the device & handler support
  64. shared IRQs (note that this is required of PCI drivers).
  65. DRIVER_GEM
  66. Driver use the GEM memory manager.
  67. DRIVER_MODESET
  68. Driver supports mode setting interfaces (KMS).
  69. DRIVER_PRIME
  70. Driver implements DRM PRIME buffer sharing.
  71. DRIVER_RENDER
  72. Driver supports dedicated render nodes.
  73. DRIVER_ATOMIC
  74. Driver supports atomic properties. In this case the driver must
  75. implement appropriate obj->atomic_get_property() vfuncs for any
  76. modeset objects with driver specific properties.
  77. DRIVER_SYNCOBJ
  78. Driver support drm sync objects.
  79. Major, Minor and Patchlevel
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. int major; int minor; int patchlevel;
  82. The DRM core identifies driver versions by a major, minor and patch
  83. level triplet. The information is printed to the kernel log at
  84. initialization time and passed to userspace through the
  85. DRM_IOCTL_VERSION ioctl.
  86. The major and minor numbers are also used to verify the requested driver
  87. API version passed to DRM_IOCTL_SET_VERSION. When the driver API
  88. changes between minor versions, applications can call
  89. DRM_IOCTL_SET_VERSION to select a specific version of the API. If the
  90. requested major isn't equal to the driver major, or the requested minor
  91. is larger than the driver minor, the DRM_IOCTL_SET_VERSION call will
  92. return an error. Otherwise the driver's set_version() method will be
  93. called with the requested version.
  94. Name, Description and Date
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. char \*name; char \*desc; char \*date;
  97. The driver name is printed to the kernel log at initialization time,
  98. used for IRQ registration and passed to userspace through
  99. DRM_IOCTL_VERSION.
  100. The driver description is a purely informative string passed to
  101. userspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
  102. the kernel.
  103. The driver date, formatted as YYYYMMDD, is meant to identify the date of
  104. the latest modification to the driver. However, as most drivers fail to
  105. update it, its value is mostly useless. The DRM core prints it to the
  106. kernel log at initialization time and passes it to userspace through the
  107. DRM_IOCTL_VERSION ioctl.
  108. Device Instance and Driver Handling
  109. -----------------------------------
  110. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  111. :doc: driver instance overview
  112. .. kernel-doc:: include/drm/drm_drv.h
  113. :internal:
  114. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  115. :export:
  116. Driver Load
  117. -----------
  118. IRQ Helper Library
  119. ~~~~~~~~~~~~~~~~~~
  120. .. kernel-doc:: drivers/gpu/drm/drm_irq.c
  121. :doc: irq helpers
  122. .. kernel-doc:: drivers/gpu/drm/drm_irq.c
  123. :export:
  124. Memory Manager Initialization
  125. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126. Every DRM driver requires a memory manager which must be initialized at
  127. load time. DRM currently contains two memory managers, the Translation
  128. Table Manager (TTM) and the Graphics Execution Manager (GEM). This
  129. document describes the use of the GEM memory manager only. See ? for
  130. details.
  131. Miscellaneous Device Configuration
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. Another task that may be necessary for PCI devices during configuration
  134. is mapping the video BIOS. On many devices, the VBIOS describes device
  135. configuration, LCD panel timings (if any), and contains flags indicating
  136. device state. Mapping the BIOS can be done using the pci_map_rom()
  137. call, a convenience function that takes care of mapping the actual ROM,
  138. whether it has been shadowed into memory (typically at address 0xc0000)
  139. or exists on the PCI device in the ROM BAR. Note that after the ROM has
  140. been mapped and any necessary information has been extracted, it should
  141. be unmapped; on many devices, the ROM address decoder is shared with
  142. other BARs, so leaving it mapped could cause undesired behaviour like
  143. hangs or memory corruption.
  144. Bus-specific Device Registration and PCI Support
  145. ------------------------------------------------
  146. A number of functions are provided to help with device registration. The
  147. functions deal with PCI and platform devices respectively and are only
  148. provided for historical reasons. These are all deprecated and shouldn't
  149. be used in new drivers. Besides that there's a few helpers for pci
  150. drivers.
  151. .. kernel-doc:: drivers/gpu/drm/drm_pci.c
  152. :export:
  153. Open/Close, File Operations and IOCTLs
  154. ======================================
  155. .. _drm_driver_fops:
  156. File Operations
  157. ---------------
  158. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  159. :doc: file operations
  160. .. kernel-doc:: include/drm/drm_file.h
  161. :internal:
  162. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  163. :export:
  164. Misc Utilities
  165. ==============
  166. Printer
  167. -------
  168. .. kernel-doc:: include/drm/drm_print.h
  169. :doc: print
  170. .. kernel-doc:: include/drm/drm_print.h
  171. :internal:
  172. .. kernel-doc:: drivers/gpu/drm/drm_print.c
  173. :export:
  174. Legacy Support Code
  175. ===================
  176. The section very briefly covers some of the old legacy support code
  177. which is only used by old DRM drivers which have done a so-called
  178. shadow-attach to the underlying device instead of registering as a real
  179. driver. This also includes some of the old generic buffer management and
  180. command submission code. Do not use any of this in new and modern
  181. drivers.
  182. Legacy Suspend/Resume
  183. ---------------------
  184. The DRM core provides some suspend/resume code, but drivers wanting full
  185. suspend/resume support should provide save() and restore() functions.
  186. These are called at suspend, hibernate, or resume time, and should
  187. perform any state save or restore required by your device across suspend
  188. or hibernate states.
  189. int (\*suspend) (struct drm_device \*, pm_message_t state); int
  190. (\*resume) (struct drm_device \*);
  191. Those are legacy suspend and resume methods which *only* work with the
  192. legacy shadow-attach driver registration functions. New driver should
  193. use the power management interface provided by their bus type (usually
  194. through the :c:type:`struct device_driver <device_driver>`
  195. dev_pm_ops) and set these methods to NULL.
  196. Legacy DMA Services
  197. -------------------
  198. This should cover how DMA mapping etc. is supported by the core. These
  199. functions are deprecated and should not be used.