drm-internals.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. Major, Minor and Patchlevel
  78. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  79. int major; int minor; int patchlevel;
  80. The DRM core identifies driver versions by a major, minor and patch
  81. level triplet. The information is printed to the kernel log at
  82. initialization time and passed to userspace through the
  83. DRM_IOCTL_VERSION ioctl.
  84. The major and minor numbers are also used to verify the requested driver
  85. API version passed to DRM_IOCTL_SET_VERSION. When the driver API
  86. changes between minor versions, applications can call
  87. DRM_IOCTL_SET_VERSION to select a specific version of the API. If the
  88. requested major isn't equal to the driver major, or the requested minor
  89. is larger than the driver minor, the DRM_IOCTL_SET_VERSION call will
  90. return an error. Otherwise the driver's set_version() method will be
  91. called with the requested version.
  92. Name, Description and Date
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. char \*name; char \*desc; char \*date;
  95. The driver name is printed to the kernel log at initialization time,
  96. used for IRQ registration and passed to userspace through
  97. DRM_IOCTL_VERSION.
  98. The driver description is a purely informative string passed to
  99. userspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
  100. the kernel.
  101. The driver date, formatted as YYYYMMDD, is meant to identify the date of
  102. the latest modification to the driver. However, as most drivers fail to
  103. update it, its value is mostly useless. The DRM core prints it to the
  104. kernel log at initialization time and passes it to userspace through the
  105. DRM_IOCTL_VERSION ioctl.
  106. Device Instance and Driver Handling
  107. -----------------------------------
  108. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  109. :doc: driver instance overview
  110. .. kernel-doc:: include/drm/drm_drv.h
  111. :internal:
  112. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  113. :export:
  114. Driver Load
  115. -----------
  116. IRQ Registration
  117. ~~~~~~~~~~~~~~~~
  118. The DRM core tries to facilitate IRQ handler registration and
  119. unregistration by providing :c:func:`drm_irq_install()` and
  120. :c:func:`drm_irq_uninstall()` functions. Those functions only
  121. support a single interrupt per device, devices that use more than one
  122. IRQs need to be handled manually.
  123. Managed IRQ Registration
  124. ''''''''''''''''''''''''
  125. :c:func:`drm_irq_install()` starts by calling the irq_preinstall
  126. driver operation. The operation is optional and must make sure that the
  127. interrupt will not get fired by clearing all pending interrupt flags or
  128. disabling the interrupt.
  129. The passed-in IRQ will then be requested by a call to
  130. :c:func:`request_irq()`. If the DRIVER_IRQ_SHARED driver feature
  131. flag is set, a shared (IRQF_SHARED) IRQ handler will be requested.
  132. The IRQ handler function must be provided as the mandatory irq_handler
  133. driver operation. It will get passed directly to
  134. :c:func:`request_irq()` and thus has the same prototype as all IRQ
  135. handlers. It will get called with a pointer to the DRM device as the
  136. second argument.
  137. Finally the function calls the optional irq_postinstall driver
  138. operation. The operation usually enables interrupts (excluding the
  139. vblank interrupt, which is enabled separately), but drivers may choose
  140. to enable/disable interrupts at a different time.
  141. :c:func:`drm_irq_uninstall()` is similarly used to uninstall an
  142. IRQ handler. It starts by waking up all processes waiting on a vblank
  143. interrupt to make sure they don't hang, and then calls the optional
  144. irq_uninstall driver operation. The operation must disable all hardware
  145. interrupts. Finally the function frees the IRQ by calling
  146. :c:func:`free_irq()`.
  147. Manual IRQ Registration
  148. '''''''''''''''''''''''
  149. Drivers that require multiple interrupt handlers can't use the managed
  150. IRQ registration functions. In that case IRQs must be registered and
  151. unregistered manually (usually with the :c:func:`request_irq()` and
  152. :c:func:`free_irq()` functions, or their :c:func:`devm_request_irq()` and
  153. :c:func:`devm_free_irq()` equivalents).
  154. When manually registering IRQs, drivers must not set the
  155. DRIVER_HAVE_IRQ driver feature flag, and must not provide the
  156. irq_handler driver operation. They must set the :c:type:`struct
  157. drm_device <drm_device>` irq_enabled field to 1 upon
  158. registration of the IRQs, and clear it to 0 after unregistering the
  159. IRQs.
  160. IRQ Helper Library
  161. ~~~~~~~~~~~~~~~~~~
  162. .. kernel-doc:: drivers/gpu/drm/drm_irq.c
  163. :doc: irq helpers
  164. .. kernel-doc:: drivers/gpu/drm/drm_irq.c
  165. :export:
  166. Memory Manager Initialization
  167. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168. Every DRM driver requires a memory manager which must be initialized at
  169. load time. DRM currently contains two memory managers, the Translation
  170. Table Manager (TTM) and the Graphics Execution Manager (GEM). This
  171. document describes the use of the GEM memory manager only. See ? for
  172. details.
  173. Miscellaneous Device Configuration
  174. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. Another task that may be necessary for PCI devices during configuration
  176. is mapping the video BIOS. On many devices, the VBIOS describes device
  177. configuration, LCD panel timings (if any), and contains flags indicating
  178. device state. Mapping the BIOS can be done using the pci_map_rom()
  179. call, a convenience function that takes care of mapping the actual ROM,
  180. whether it has been shadowed into memory (typically at address 0xc0000)
  181. or exists on the PCI device in the ROM BAR. Note that after the ROM has
  182. been mapped and any necessary information has been extracted, it should
  183. be unmapped; on many devices, the ROM address decoder is shared with
  184. other BARs, so leaving it mapped could cause undesired behaviour like
  185. hangs or memory corruption.
  186. Bus-specific Device Registration and PCI Support
  187. ------------------------------------------------
  188. A number of functions are provided to help with device registration. The
  189. functions deal with PCI and platform devices respectively and are only
  190. provided for historical reasons. These are all deprecated and shouldn't
  191. be used in new drivers. Besides that there's a few helpers for pci
  192. drivers.
  193. .. kernel-doc:: drivers/gpu/drm/drm_pci.c
  194. :export:
  195. Open/Close, File Operations and IOCTLs
  196. ======================================
  197. File Operations
  198. ---------------
  199. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  200. :doc: file operations
  201. .. kernel-doc:: include/drm/drm_file.h
  202. :internal:
  203. .. kernel-doc:: drivers/gpu/drm/drm_file.c
  204. :export:
  205. Misc Utilities
  206. ==============
  207. Printer
  208. -------
  209. .. kernel-doc:: include/drm/drm_print.h
  210. :doc: print
  211. .. kernel-doc:: include/drm/drm_print.h
  212. :internal:
  213. .. kernel-doc:: drivers/gpu/drm/drm_print.c
  214. :export:
  215. Legacy Support Code
  216. ===================
  217. The section very briefly covers some of the old legacy support code
  218. which is only used by old DRM drivers which have done a so-called
  219. shadow-attach to the underlying device instead of registering as a real
  220. driver. This also includes some of the old generic buffer management and
  221. command submission code. Do not use any of this in new and modern
  222. drivers.
  223. Legacy Suspend/Resume
  224. ---------------------
  225. The DRM core provides some suspend/resume code, but drivers wanting full
  226. suspend/resume support should provide save() and restore() functions.
  227. These are called at suspend, hibernate, or resume time, and should
  228. perform any state save or restore required by your device across suspend
  229. or hibernate states.
  230. int (\*suspend) (struct drm_device \*, pm_message_t state); int
  231. (\*resume) (struct drm_device \*);
  232. Those are legacy suspend and resume methods which *only* work with the
  233. legacy shadow-attach driver registration functions. New driver should
  234. use the power management interface provided by their bus type (usually
  235. through the :c:type:`struct device_driver <device_driver>`
  236. dev_pm_ops) and set these methods to NULL.
  237. Legacy DMA Services
  238. -------------------
  239. This should cover how DMA mapping etc. is supported by the core. These
  240. functions are deprecated and should not be used.