drm-kms.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. =========================
  2. Kernel Mode Setting (KMS)
  3. =========================
  4. Drivers must initialize the mode setting core by calling
  5. :c:func:`drm_mode_config_init()` on the DRM device. The function
  6. initializes the :c:type:`struct drm_device <drm_device>`
  7. mode_config field and never fails. Once done, mode configuration must
  8. be setup by initializing the following fields.
  9. - int min_width, min_height; int max_width, max_height;
  10. Minimum and maximum width and height of the frame buffers in pixel
  11. units.
  12. - struct drm_mode_config_funcs \*funcs;
  13. Mode setting functions.
  14. Overview
  15. ========
  16. .. kernel-render:: DOT
  17. :alt: KMS Display Pipeline
  18. :caption: KMS Display Pipeline Overview
  19. digraph "KMS" {
  20. node [shape=box]
  21. subgraph cluster_static {
  22. style=dashed
  23. label="Static Objects"
  24. node [bgcolor=grey style=filled]
  25. "drm_plane A" -> "drm_crtc"
  26. "drm_plane B" -> "drm_crtc"
  27. "drm_crtc" -> "drm_encoder A"
  28. "drm_crtc" -> "drm_encoder B"
  29. }
  30. subgraph cluster_user_created {
  31. style=dashed
  32. label="Userspace-Created"
  33. node [shape=oval]
  34. "drm_framebuffer 1" -> "drm_plane A"
  35. "drm_framebuffer 2" -> "drm_plane B"
  36. }
  37. subgraph cluster_connector {
  38. style=dashed
  39. label="Hotpluggable"
  40. "drm_encoder A" -> "drm_connector A"
  41. "drm_encoder B" -> "drm_connector B"
  42. }
  43. }
  44. The basic object structure KMS presents to userspace is fairly simple.
  45. Framebuffers (represented by :c:type:`struct drm_framebuffer <drm_framebuffer>`,
  46. see `Frame Buffer Abstraction`_) feed into planes. Planes are represented by
  47. :c:type:`struct drm_plane <drm_plane>`, see `Plane Abstraction`_ for more
  48. details. One or more (or even no) planes feed their pixel data into a CRTC
  49. (represented by :c:type:`struct drm_crtc <drm_crtc>`, see `CRTC Abstraction`_)
  50. for blending. The precise blending step is explained in more detail in `Plane
  51. Composition Properties`_ and related chapters.
  52. For the output routing the first step is encoders (represented by
  53. :c:type:`struct drm_encoder <drm_encoder>`, see `Encoder Abstraction`_). Those
  54. are really just internal artifacts of the helper libraries used to implement KMS
  55. drivers. Besides that they make it unecessarily more complicated for userspace
  56. to figure out which connections between a CRTC and a connector are possible, and
  57. what kind of cloning is supported, they serve no purpose in the userspace API.
  58. Unfortunately encoders have been exposed to userspace, hence can't remove them
  59. at this point. Futhermore the exposed restrictions are often wrongly set by
  60. drivers, and in many cases not powerful enough to express the real restrictions.
  61. A CRTC can be connected to multiple encoders, and for an active CRTC there must
  62. be at least one encoder.
  63. The final, and real, endpoint in the display chain is the connector (represented
  64. by :c:type:`struct drm_connector <drm_connector>`, see `Connector
  65. Abstraction`_). Connectors can have different possible encoders, but the kernel
  66. driver selects which encoder to use for each connector. The use case is DVI,
  67. which could switch between an analog and a digital encoder. Encoders can also
  68. drive multiple different connectors. There is exactly one active connector for
  69. every active encoder.
  70. Internally the output pipeline is a bit more complex and matches today's
  71. hardware more closely:
  72. .. kernel-render:: DOT
  73. :alt: KMS Output Pipeline
  74. :caption: KMS Output Pipeline
  75. digraph "Output Pipeline" {
  76. node [shape=box]
  77. subgraph {
  78. "drm_crtc" [bgcolor=grey style=filled]
  79. }
  80. subgraph cluster_internal {
  81. style=dashed
  82. label="Internal Pipeline"
  83. {
  84. node [bgcolor=grey style=filled]
  85. "drm_encoder A";
  86. "drm_encoder B";
  87. "drm_encoder C";
  88. }
  89. {
  90. node [bgcolor=grey style=filled]
  91. "drm_encoder B" -> "drm_bridge B"
  92. "drm_encoder C" -> "drm_bridge C1"
  93. "drm_bridge C1" -> "drm_bridge C2";
  94. }
  95. }
  96. "drm_crtc" -> "drm_encoder A"
  97. "drm_crtc" -> "drm_encoder B"
  98. "drm_crtc" -> "drm_encoder C"
  99. subgraph cluster_output {
  100. style=dashed
  101. label="Outputs"
  102. "drm_encoder A" -> "drm_connector A";
  103. "drm_bridge B" -> "drm_connector B";
  104. "drm_bridge C2" -> "drm_connector C";
  105. "drm_panel"
  106. }
  107. }
  108. Internally two additional helper objects come into play. First, to be able to
  109. share code for encoders (sometimes on the same SoC, sometimes off-chip) one or
  110. more :ref:`drm_bridges` (represented by :c:type:`struct drm_bridge
  111. <drm_bridge>`) can be linked to an encoder. This link is static and cannot be
  112. changed, which means the cross-bar (if there is any) needs to be mapped between
  113. the CRTC and any encoders. Often for drivers with bridges there's no code left
  114. at the encoder level. Atomic drivers can leave out all the encoder callbacks to
  115. essentially only leave a dummy routing object behind, which is needed for
  116. backwards compatibility since encoders are exposed to userspace.
  117. The second object is for panels, represented by :c:type:`struct drm_panel
  118. <drm_panel>`, see :ref:`drm_panel_helper`. Panels do not have a fixed binding
  119. point, but are generally linked to the driver private structure that embeds
  120. :c:type:`struct drm_connector <drm_connector>`.
  121. Note that currently the bridge chaining and interactions with connectors and
  122. panels are still in-flux and not really fully sorted out yet.
  123. KMS Core Structures and Functions
  124. =================================
  125. .. kernel-doc:: include/drm/drm_mode_config.h
  126. :internal:
  127. .. kernel-doc:: drivers/gpu/drm/drm_mode_config.c
  128. :export:
  129. Modeset Base Object Abstraction
  130. ===============================
  131. .. kernel-render:: DOT
  132. :alt: Mode Objects and Properties
  133. :caption: Mode Objects and Properties
  134. digraph {
  135. node [shape=box]
  136. "drm_property A" -> "drm_mode_object A"
  137. "drm_property A" -> "drm_mode_object B"
  138. "drm_property B" -> "drm_mode_object A"
  139. }
  140. The base structure for all KMS objects is :c:type:`struct drm_mode_object
  141. <drm_mode_object>`. One of the base services it provides is tracking properties,
  142. which are especially important for the atomic IOCTL (see `Atomic Mode
  143. Setting`_). The somewhat surprising part here is that properties are not
  144. directly instantiated on each object, but free-standing mode objects themselves,
  145. represented by :c:type:`struct drm_property <drm_property>`, which only specify
  146. the type and value range of a property. Any given property can be attached
  147. multiple times to different objects using :c:func:`drm_object_attach_property()
  148. <drm_object_attach_property>`.
  149. .. kernel-doc:: include/drm/drm_mode_object.h
  150. :internal:
  151. .. kernel-doc:: drivers/gpu/drm/drm_mode_object.c
  152. :export:
  153. Atomic Mode Setting
  154. ===================
  155. .. kernel-render:: DOT
  156. :alt: Mode Objects and Properties
  157. :caption: Mode Objects and Properties
  158. digraph {
  159. node [shape=box]
  160. subgraph cluster_state {
  161. style=dashed
  162. label="Free-standing state"
  163. "drm_atomic_state" -> "duplicated drm_plane_state A"
  164. "drm_atomic_state" -> "duplicated drm_plane_state B"
  165. "drm_atomic_state" -> "duplicated drm_crtc_state"
  166. "drm_atomic_state" -> "duplicated drm_connector_state"
  167. "drm_atomic_state" -> "duplicated driver private state"
  168. }
  169. subgraph cluster_current {
  170. style=dashed
  171. label="Current state"
  172. "drm_device" -> "drm_plane A"
  173. "drm_device" -> "drm_plane B"
  174. "drm_device" -> "drm_crtc"
  175. "drm_device" -> "drm_connector"
  176. "drm_device" -> "driver private object"
  177. "drm_plane A" -> "drm_plane_state A"
  178. "drm_plane B" -> "drm_plane_state B"
  179. "drm_crtc" -> "drm_crtc_state"
  180. "drm_connector" -> "drm_connector_state"
  181. "driver private object" -> "driver private state"
  182. }
  183. "drm_atomic_state" -> "drm_device" [label="atomic_commit"]
  184. "duplicated drm_plane_state A" -> "drm_device"[style=invis]
  185. }
  186. Atomic provides transactional modeset (including planes) updates, but a
  187. bit differently from the usual transactional approach of try-commit and
  188. rollback:
  189. - Firstly, no hardware changes are allowed when the commit would fail. This
  190. allows us to implement the DRM_MODE_ATOMIC_TEST_ONLY mode, which allows
  191. userspace to explore whether certain configurations would work or not.
  192. - This would still allow setting and rollback of just the software state,
  193. simplifying conversion of existing drivers. But auditing drivers for
  194. correctness of the atomic_check code becomes really hard with that: Rolling
  195. back changes in data structures all over the place is hard to get right.
  196. - Lastly, for backwards compatibility and to support all use-cases, atomic
  197. updates need to be incremental and be able to execute in parallel. Hardware
  198. doesn't always allow it, but where possible plane updates on different CRTCs
  199. should not interfere, and not get stalled due to output routing changing on
  200. different CRTCs.
  201. Taken all together there's two consequences for the atomic design:
  202. - The overall state is split up into per-object state structures:
  203. :c:type:`struct drm_plane_state <drm_plane_state>` for planes, :c:type:`struct
  204. drm_crtc_state <drm_crtc_state>` for CRTCs and :c:type:`struct
  205. drm_connector_state <drm_connector_state>` for connectors. These are the only
  206. objects with userspace-visible and settable state. For internal state drivers
  207. can subclass these structures through embeddeding, or add entirely new state
  208. structures for their globally shared hardware functions.
  209. - An atomic update is assembled and validated as an entirely free-standing pile
  210. of structures within the :c:type:`drm_atomic_state <drm_atomic_state>`
  211. container. Driver private state structures are also tracked in the same
  212. structure; see the next chapter. Only when a state is committed is it applied
  213. to the driver and modeset objects. This way rolling back an update boils down
  214. to releasing memory and unreferencing objects like framebuffers.
  215. Read on in this chapter, and also in :ref:`drm_atomic_helper` for more detailed
  216. coverage of specific topics.
  217. Handling Driver Private State
  218. -----------------------------
  219. .. kernel-doc:: drivers/gpu/drm/drm_atomic.c
  220. :doc: handling driver private state
  221. Atomic Mode Setting Function Reference
  222. --------------------------------------
  223. .. kernel-doc:: include/drm/drm_atomic.h
  224. :internal:
  225. .. kernel-doc:: drivers/gpu/drm/drm_atomic.c
  226. :export:
  227. .. kernel-doc:: drivers/gpu/drm/drm_atomic.c
  228. :internal:
  229. CRTC Abstraction
  230. ================
  231. .. kernel-doc:: drivers/gpu/drm/drm_crtc.c
  232. :doc: overview
  233. CRTC Functions Reference
  234. --------------------------------
  235. .. kernel-doc:: include/drm/drm_crtc.h
  236. :internal:
  237. .. kernel-doc:: drivers/gpu/drm/drm_crtc.c
  238. :export:
  239. Frame Buffer Abstraction
  240. ========================
  241. .. kernel-doc:: drivers/gpu/drm/drm_framebuffer.c
  242. :doc: overview
  243. Frame Buffer Functions Reference
  244. --------------------------------
  245. .. kernel-doc:: include/drm/drm_framebuffer.h
  246. :internal:
  247. .. kernel-doc:: drivers/gpu/drm/drm_framebuffer.c
  248. :export:
  249. DRM Format Handling
  250. ===================
  251. .. kernel-doc:: include/drm/drm_fourcc.h
  252. :internal:
  253. .. kernel-doc:: drivers/gpu/drm/drm_fourcc.c
  254. :export:
  255. Dumb Buffer Objects
  256. ===================
  257. .. kernel-doc:: drivers/gpu/drm/drm_dumb_buffers.c
  258. :doc: overview
  259. Plane Abstraction
  260. =================
  261. .. kernel-doc:: drivers/gpu/drm/drm_plane.c
  262. :doc: overview
  263. Plane Functions Reference
  264. -------------------------
  265. .. kernel-doc:: include/drm/drm_plane.h
  266. :internal:
  267. .. kernel-doc:: drivers/gpu/drm/drm_plane.c
  268. :export:
  269. Display Modes Function Reference
  270. ================================
  271. .. kernel-doc:: include/drm/drm_modes.h
  272. :internal:
  273. .. kernel-doc:: drivers/gpu/drm/drm_modes.c
  274. :export:
  275. Connector Abstraction
  276. =====================
  277. .. kernel-doc:: drivers/gpu/drm/drm_connector.c
  278. :doc: overview
  279. Connector Functions Reference
  280. -----------------------------
  281. .. kernel-doc:: include/drm/drm_connector.h
  282. :internal:
  283. .. kernel-doc:: drivers/gpu/drm/drm_connector.c
  284. :export:
  285. Writeback Connectors
  286. --------------------
  287. .. kernel-doc:: drivers/gpu/drm/drm_writeback.c
  288. :doc: overview
  289. .. kernel-doc:: drivers/gpu/drm/drm_writeback.c
  290. :export:
  291. Encoder Abstraction
  292. ===================
  293. .. kernel-doc:: drivers/gpu/drm/drm_encoder.c
  294. :doc: overview
  295. Encoder Functions Reference
  296. ---------------------------
  297. .. kernel-doc:: include/drm/drm_encoder.h
  298. :internal:
  299. .. kernel-doc:: drivers/gpu/drm/drm_encoder.c
  300. :export:
  301. KMS Initialization and Cleanup
  302. ==============================
  303. A KMS device is abstracted and exposed as a set of planes, CRTCs,
  304. encoders and connectors. KMS drivers must thus create and initialize all
  305. those objects at load time after initializing mode setting.
  306. CRTCs (:c:type:`struct drm_crtc <drm_crtc>`)
  307. --------------------------------------------
  308. A CRTC is an abstraction representing a part of the chip that contains a
  309. pointer to a scanout buffer. Therefore, the number of CRTCs available
  310. determines how many independent scanout buffers can be active at any
  311. given time. The CRTC structure contains several fields to support this:
  312. a pointer to some video memory (abstracted as a frame buffer object), a
  313. display mode, and an (x, y) offset into the video memory to support
  314. panning or configurations where one piece of video memory spans multiple
  315. CRTCs.
  316. CRTC Initialization
  317. ~~~~~~~~~~~~~~~~~~~
  318. A KMS device must create and register at least one struct
  319. :c:type:`struct drm_crtc <drm_crtc>` instance. The instance is
  320. allocated and zeroed by the driver, possibly as part of a larger
  321. structure, and registered with a call to :c:func:`drm_crtc_init()`
  322. with a pointer to CRTC functions.
  323. Cleanup
  324. -------
  325. The DRM core manages its objects' lifetime. When an object is not needed
  326. anymore the core calls its destroy function, which must clean up and
  327. free every resource allocated for the object. Every
  328. :c:func:`drm_\*_init()` call must be matched with a corresponding
  329. :c:func:`drm_\*_cleanup()` call to cleanup CRTCs
  330. (:c:func:`drm_crtc_cleanup()`), planes
  331. (:c:func:`drm_plane_cleanup()`), encoders
  332. (:c:func:`drm_encoder_cleanup()`) and connectors
  333. (:c:func:`drm_connector_cleanup()`). Furthermore, connectors that
  334. have been added to sysfs must be removed by a call to
  335. :c:func:`drm_connector_unregister()` before calling
  336. :c:func:`drm_connector_cleanup()`.
  337. Connectors state change detection must be cleanup up with a call to
  338. :c:func:`drm_kms_helper_poll_fini()`.
  339. Output discovery and initialization example
  340. -------------------------------------------
  341. .. code-block:: c
  342. void intel_crt_init(struct drm_device *dev)
  343. {
  344. struct drm_connector *connector;
  345. struct intel_output *intel_output;
  346. intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
  347. if (!intel_output)
  348. return;
  349. connector = &intel_output->base;
  350. drm_connector_init(dev, &intel_output->base,
  351. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  352. drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
  353. DRM_MODE_ENCODER_DAC);
  354. drm_connector_attach_encoder(&intel_output->base,
  355. &intel_output->enc);
  356. /* Set up the DDC bus. */
  357. intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
  358. if (!intel_output->ddc_bus) {
  359. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  360. "failed.\n");
  361. return;
  362. }
  363. intel_output->type = INTEL_OUTPUT_ANALOG;
  364. connector->interlace_allowed = 0;
  365. connector->doublescan_allowed = 0;
  366. drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
  367. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  368. drm_connector_register(connector);
  369. }
  370. In the example above (taken from the i915 driver), a CRTC, connector and
  371. encoder combination is created. A device-specific i2c bus is also
  372. created for fetching EDID data and performing monitor detection. Once
  373. the process is complete, the new connector is registered with sysfs to
  374. make its properties available to applications.
  375. KMS Locking
  376. ===========
  377. .. kernel-doc:: drivers/gpu/drm/drm_modeset_lock.c
  378. :doc: kms locking
  379. .. kernel-doc:: include/drm/drm_modeset_lock.h
  380. :internal:
  381. .. kernel-doc:: drivers/gpu/drm/drm_modeset_lock.c
  382. :export:
  383. KMS Properties
  384. ==============
  385. Property Types and Blob Property Support
  386. ----------------------------------------
  387. .. kernel-doc:: drivers/gpu/drm/drm_property.c
  388. :doc: overview
  389. .. kernel-doc:: include/drm/drm_property.h
  390. :internal:
  391. .. kernel-doc:: drivers/gpu/drm/drm_property.c
  392. :export:
  393. Standard Connector Properties
  394. -----------------------------
  395. .. kernel-doc:: drivers/gpu/drm/drm_connector.c
  396. :doc: standard connector properties
  397. HDMI Specific Connector Properties
  398. ----------------------------------
  399. .. kernel-doc:: drivers/gpu/drm/drm_connector.c
  400. :doc: HDMI connector properties
  401. Plane Composition Properties
  402. ----------------------------
  403. .. kernel-doc:: drivers/gpu/drm/drm_blend.c
  404. :doc: overview
  405. .. kernel-doc:: drivers/gpu/drm/drm_blend.c
  406. :export:
  407. Color Management Properties
  408. ---------------------------
  409. .. kernel-doc:: drivers/gpu/drm/drm_color_mgmt.c
  410. :doc: overview
  411. .. kernel-doc:: drivers/gpu/drm/drm_color_mgmt.c
  412. :export:
  413. Tile Group Property
  414. -------------------
  415. .. kernel-doc:: drivers/gpu/drm/drm_connector.c
  416. :doc: Tile group
  417. Explicit Fencing Properties
  418. ---------------------------
  419. .. kernel-doc:: drivers/gpu/drm/drm_atomic.c
  420. :doc: explicit fencing properties
  421. Existing KMS Properties
  422. -----------------------
  423. The following table gives description of drm properties exposed by various
  424. modules/drivers. Because this table is very unwieldy, do not add any new
  425. properties here. Instead document them in a section above.
  426. .. csv-table::
  427. :header-rows: 1
  428. :file: kms-properties.csv
  429. Vertical Blanking
  430. =================
  431. .. kernel-doc:: drivers/gpu/drm/drm_vblank.c
  432. :doc: vblank handling
  433. Vertical Blanking and Interrupt Handling Functions Reference
  434. ------------------------------------------------------------
  435. .. kernel-doc:: include/drm/drm_vblank.h
  436. :internal:
  437. .. kernel-doc:: drivers/gpu/drm/drm_vblank.c
  438. :export: