drm-internals.rst 14 KB

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