drm-internals.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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_REQUIRE_AGP
  45. Driver needs AGP interface to function. AGP initialization failure
  46. will become a fatal error.
  47. DRIVER_PCI_DMA
  48. Driver is capable of PCI DMA, mapping of PCI DMA buffers to
  49. userspace will be enabled. Deprecated.
  50. DRIVER_SG
  51. Driver can perform scatter/gather DMA, allocation and mapping of
  52. scatter/gather buffers will be enabled. Deprecated.
  53. DRIVER_HAVE_DMA
  54. Driver supports DMA, the userspace DMA API will be supported.
  55. Deprecated.
  56. DRIVER_HAVE_IRQ; DRIVER_IRQ_SHARED
  57. DRIVER_HAVE_IRQ indicates whether the driver has an IRQ handler
  58. managed by the DRM Core. The core will support simple IRQ handler
  59. installation when the flag is set. The installation process is
  60. described in ?.
  61. DRIVER_IRQ_SHARED indicates whether the device & handler support
  62. shared IRQs (note that this is required of PCI drivers).
  63. DRIVER_GEM
  64. Driver use the GEM memory manager.
  65. DRIVER_MODESET
  66. Driver supports mode setting interfaces (KMS).
  67. DRIVER_PRIME
  68. Driver implements DRM PRIME buffer sharing.
  69. DRIVER_RENDER
  70. Driver supports dedicated render nodes.
  71. DRIVER_ATOMIC
  72. Driver supports atomic properties. In this case the driver must
  73. implement appropriate obj->atomic_get_property() vfuncs for any
  74. modeset objects with driver specific properties.
  75. Major, Minor and Patchlevel
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. int major; int minor; int patchlevel;
  78. The DRM core identifies driver versions by a major, minor and patch
  79. level triplet. The information is printed to the kernel log at
  80. initialization time and passed to userspace through the
  81. DRM_IOCTL_VERSION ioctl.
  82. The major and minor numbers are also used to verify the requested driver
  83. API version passed to DRM_IOCTL_SET_VERSION. When the driver API
  84. changes between minor versions, applications can call
  85. DRM_IOCTL_SET_VERSION to select a specific version of the API. If the
  86. requested major isn't equal to the driver major, or the requested minor
  87. is larger than the driver minor, the DRM_IOCTL_SET_VERSION call will
  88. return an error. Otherwise the driver's set_version() method will be
  89. called with the requested version.
  90. Name, Description and Date
  91. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. char \*name; char \*desc; char \*date;
  93. The driver name is printed to the kernel log at initialization time,
  94. used for IRQ registration and passed to userspace through
  95. DRM_IOCTL_VERSION.
  96. The driver description is a purely informative string passed to
  97. userspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
  98. the kernel.
  99. The driver date, formatted as YYYYMMDD, is meant to identify the date of
  100. the latest modification to the driver. However, as most drivers fail to
  101. update it, its value is mostly useless. The DRM core prints it to the
  102. kernel log at initialization time and passes it to userspace through the
  103. DRM_IOCTL_VERSION ioctl.
  104. Device Instance and Driver Handling
  105. -----------------------------------
  106. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  107. :doc: driver instance overview
  108. .. kernel-doc:: drivers/gpu/drm/drm_drv.c
  109. :export:
  110. Driver Load
  111. -----------
  112. IRQ Registration
  113. ~~~~~~~~~~~~~~~~
  114. The DRM core tries to facilitate IRQ handler registration and
  115. unregistration by providing :c:func:`drm_irq_install()` and
  116. :c:func:`drm_irq_uninstall()` functions. Those functions only
  117. support a single interrupt per device, devices that use more than one
  118. IRQs need to be handled manually.
  119. Managed IRQ Registration
  120. ''''''''''''''''''''''''
  121. :c:func:`drm_irq_install()` starts by calling the irq_preinstall
  122. driver operation. The operation is optional and must make sure that the
  123. interrupt will not get fired by clearing all pending interrupt flags or
  124. disabling the interrupt.
  125. The passed-in IRQ will then be requested by a call to
  126. :c:func:`request_irq()`. If the DRIVER_IRQ_SHARED driver feature
  127. flag is set, a shared (IRQF_SHARED) IRQ handler will be requested.
  128. The IRQ handler function must be provided as the mandatory irq_handler
  129. driver operation. It will get passed directly to
  130. :c:func:`request_irq()` and thus has the same prototype as all IRQ
  131. handlers. It will get called with a pointer to the DRM device as the
  132. second argument.
  133. Finally the function calls the optional irq_postinstall driver
  134. operation. The operation usually enables interrupts (excluding the
  135. vblank interrupt, which is enabled separately), but drivers may choose
  136. to enable/disable interrupts at a different time.
  137. :c:func:`drm_irq_uninstall()` is similarly used to uninstall an
  138. IRQ handler. It starts by waking up all processes waiting on a vblank
  139. interrupt to make sure they don't hang, and then calls the optional
  140. irq_uninstall driver operation. The operation must disable all hardware
  141. interrupts. Finally the function frees the IRQ by calling
  142. :c:func:`free_irq()`.
  143. Manual IRQ Registration
  144. '''''''''''''''''''''''
  145. Drivers that require multiple interrupt handlers can't use the managed
  146. IRQ registration functions. In that case IRQs must be registered and
  147. unregistered manually (usually with the :c:func:`request_irq()` and
  148. :c:func:`free_irq()` functions, or their :c:func:`devm_request_irq()` and
  149. :c:func:`devm_free_irq()` equivalents).
  150. When manually registering IRQs, drivers must not set the
  151. DRIVER_HAVE_IRQ driver feature flag, and must not provide the
  152. irq_handler driver operation. They must set the :c:type:`struct
  153. drm_device <drm_device>` irq_enabled field to 1 upon
  154. registration of the IRQs, and clear it to 0 after unregistering the
  155. IRQs.
  156. Memory Manager Initialization
  157. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  158. Every DRM driver requires a memory manager which must be initialized at
  159. load time. DRM currently contains two memory managers, the Translation
  160. Table Manager (TTM) and the Graphics Execution Manager (GEM). This
  161. document describes the use of the GEM memory manager only. See ? for
  162. details.
  163. Miscellaneous Device Configuration
  164. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165. Another task that may be necessary for PCI devices during configuration
  166. is mapping the video BIOS. On many devices, the VBIOS describes device
  167. configuration, LCD panel timings (if any), and contains flags indicating
  168. device state. Mapping the BIOS can be done using the pci_map_rom()
  169. call, a convenience function that takes care of mapping the actual ROM,
  170. whether it has been shadowed into memory (typically at address 0xc0000)
  171. or exists on the PCI device in the ROM BAR. Note that after the ROM has
  172. been mapped and any necessary information has been extracted, it should
  173. be unmapped; on many devices, the ROM address decoder is shared with
  174. other BARs, so leaving it mapped could cause undesired behaviour like
  175. hangs or memory corruption.
  176. Bus-specific Device Registration and PCI Support
  177. ------------------------------------------------
  178. A number of functions are provided to help with device registration. The
  179. functions deal with PCI and platform devices respectively and are only
  180. provided for historical reasons. These are all deprecated and shouldn't
  181. be used in new drivers. Besides that there's a few helpers for pci
  182. drivers.
  183. .. kernel-doc:: drivers/gpu/drm/drm_pci.c
  184. :export:
  185. .. kernel-doc:: drivers/gpu/drm/drm_platform.c
  186. :export:
  187. Open/Close, File Operations and IOCTLs
  188. ======================================
  189. Open and Close
  190. --------------
  191. Open and close handlers. None of those methods are mandatory::
  192. int (*firstopen) (struct drm_device *);
  193. void (*lastclose) (struct drm_device *);
  194. int (*open) (struct drm_device *, struct drm_file *);
  195. void (*preclose) (struct drm_device *, struct drm_file *);
  196. void (*postclose) (struct drm_device *, struct drm_file *);
  197. The firstopen method is called by the DRM core for legacy UMS (User Mode
  198. Setting) drivers only when an application opens a device that has no
  199. other opened file handle. UMS drivers can implement it to acquire device
  200. resources. KMS drivers can't use the method and must acquire resources
  201. in the load method instead.
  202. Similarly the lastclose method is called when the last application
  203. holding a file handle opened on the device closes it, for both UMS and
  204. KMS drivers. Additionally, the method is also called at module unload
  205. time or, for hot-pluggable devices, when the device is unplugged. The
  206. firstopen and lastclose calls can thus be unbalanced.
  207. The open method is called every time the device is opened by an
  208. application. Drivers can allocate per-file private data in this method
  209. and store them in the struct :c:type:`struct drm_file
  210. <drm_file>` driver_priv field. Note that the open method is
  211. called before firstopen.
  212. The close operation is split into preclose and postclose methods.
  213. Drivers must stop and cleanup all per-file operations in the preclose
  214. method. For instance pending vertical blanking and page flip events must
  215. be cancelled. No per-file operation is allowed on the file handle after
  216. returning from the preclose method.
  217. Finally the postclose method is called as the last step of the close
  218. operation, right before calling the lastclose method if no other open
  219. file handle exists for the device. Drivers that have allocated per-file
  220. private data in the open method should free it here.
  221. The lastclose method should restore CRTC and plane properties to default
  222. value, so that a subsequent open of the device will not inherit state
  223. from the previous user. It can also be used to execute delayed power
  224. switching state changes, e.g. in conjunction with the :ref:`vga_switcheroo`
  225. infrastructure. Beyond that KMS drivers should not do any
  226. further cleanup. Only legacy UMS drivers might need to clean up device
  227. state so that the vga console or an independent fbdev driver could take
  228. over.
  229. File Operations
  230. ---------------
  231. .. kernel-doc:: drivers/gpu/drm/drm_fops.c
  232. :doc: file operations
  233. .. kernel-doc:: drivers/gpu/drm/drm_fops.c
  234. :export:
  235. IOCTLs
  236. ------
  237. struct drm_ioctl_desc \*ioctls; int num_ioctls;
  238. Driver-specific ioctls descriptors table.
  239. Driver-specific ioctls numbers start at DRM_COMMAND_BASE. The ioctls
  240. descriptors table is indexed by the ioctl number offset from the base
  241. value. Drivers can use the DRM_IOCTL_DEF_DRV() macro to initialize
  242. the table entries.
  243. ::
  244. DRM_IOCTL_DEF_DRV(ioctl, func, flags)
  245. ``ioctl`` is the ioctl name. Drivers must define the DRM_##ioctl and
  246. DRM_IOCTL_##ioctl macros to the ioctl number offset from
  247. DRM_COMMAND_BASE and the ioctl number respectively. The first macro is
  248. private to the device while the second must be exposed to userspace in a
  249. public header.
  250. ``func`` is a pointer to the ioctl handler function compatible with the
  251. ``drm_ioctl_t`` type.
  252. ::
  253. typedef int drm_ioctl_t(struct drm_device *dev, void *data,
  254. struct drm_file *file_priv);
  255. ``flags`` is a bitmask combination of the following values. It restricts
  256. how the ioctl is allowed to be called.
  257. - DRM_AUTH - Only authenticated callers allowed
  258. - DRM_MASTER - The ioctl can only be called on the master file handle
  259. - DRM_ROOT_ONLY - Only callers with the SYSADMIN capability allowed
  260. - DRM_CONTROL_ALLOW - The ioctl can only be called on a control
  261. device
  262. - DRM_UNLOCKED - The ioctl handler will be called without locking the
  263. DRM global mutex. This is the enforced default for kms drivers (i.e.
  264. using the DRIVER_MODESET flag) and hence shouldn't be used any more
  265. for new drivers.
  266. .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
  267. :export:
  268. Legacy Support Code
  269. ===================
  270. The section very briefly covers some of the old legacy support code
  271. which is only used by old DRM drivers which have done a so-called
  272. shadow-attach to the underlying device instead of registering as a real
  273. driver. This also includes some of the old generic buffer management and
  274. command submission code. Do not use any of this in new and modern
  275. drivers.
  276. Legacy Suspend/Resume
  277. ---------------------
  278. The DRM core provides some suspend/resume code, but drivers wanting full
  279. suspend/resume support should provide save() and restore() functions.
  280. These are called at suspend, hibernate, or resume time, and should
  281. perform any state save or restore required by your device across suspend
  282. or hibernate states.
  283. int (\*suspend) (struct drm_device \*, pm_message_t state); int
  284. (\*resume) (struct drm_device \*);
  285. Those are legacy suspend and resume methods which *only* work with the
  286. legacy shadow-attach driver registration functions. New driver should
  287. use the power management interface provided by their bus type (usually
  288. through the :c:type:`struct device_driver <device_driver>`
  289. dev_pm_ops) and set these methods to NULL.
  290. Legacy DMA Services
  291. -------------------
  292. This should cover how DMA mapping etc. is supported by the core. These
  293. functions are deprecated and should not be used.