blob: 641db5cb656c8308ccb42070204bcc2880bf74dd [file] [log] [blame]
Jesse Barnes2d2ef822009-10-26 13:06:31 -07001<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
4
5<book id="drmDevelopersGuide">
6 <bookinfo>
7 <title>Linux DRM Developer's Guide</title>
8
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02009 <authorgroup>
10 <author>
11 <firstname>Jesse</firstname>
12 <surname>Barnes</surname>
13 <contrib>Initial version</contrib>
14 <affiliation>
15 <orgname>Intel Corporation</orgname>
16 <address>
17 <email>jesse.barnes@intel.com</email>
18 </address>
19 </affiliation>
20 </author>
21 <author>
22 <firstname>Laurent</firstname>
23 <surname>Pinchart</surname>
24 <contrib>Driver internals</contrib>
25 <affiliation>
26 <orgname>Ideas on board SPRL</orgname>
27 <address>
28 <email>laurent.pinchart@ideasonboard.com</email>
29 </address>
30 </affiliation>
31 </author>
32 </authorgroup>
33
Jesse Barnes2d2ef822009-10-26 13:06:31 -070034 <copyright>
35 <year>2008-2009</year>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +020036 <year>2012</year>
37 <holder>Intel Corporation</holder>
38 <holder>Laurent Pinchart</holder>
Jesse Barnes2d2ef822009-10-26 13:06:31 -070039 </copyright>
40
41 <legalnotice>
42 <para>
43 The contents of this file may be used under the terms of the GNU
44 General Public License version 2 (the "GPL") as distributed in
45 the kernel source COPYING file.
46 </para>
47 </legalnotice>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +020048
49 <revhistory>
50 <!-- Put document revisions here, newest first. -->
51 <revision>
52 <revnumber>1.0</revnumber>
53 <date>2012-07-13</date>
54 <authorinitials>LP</authorinitials>
55 <revremark>Added extensive documentation about driver internals.
56 </revremark>
57 </revision>
58 </revhistory>
Jesse Barnes2d2ef822009-10-26 13:06:31 -070059 </bookinfo>
60
61<toc></toc>
62
Daniel Vetter3519f702014-01-22 12:21:16 +010063<part id="drmCore">
64 <title>DRM Core</title>
65 <partintro>
66 <para>
67 This first part of the DRM Developer's Guide documents core DRM code,
68 helper libraries for writting drivers and generic userspace interfaces
69 exposed by DRM drivers.
70 </para>
71 </partintro>
Jesse Barnes2d2ef822009-10-26 13:06:31 -070072
73 <chapter id="drmIntroduction">
74 <title>Introduction</title>
75 <para>
76 The Linux DRM layer contains code intended to support the needs
77 of complex graphics devices, usually containing programmable
78 pipelines well suited to 3D graphics acceleration. Graphics
Michael Wittenf11aca02011-08-25 17:21:31 +000079 drivers in the kernel may make use of DRM functions to make
Jesse Barnes2d2ef822009-10-26 13:06:31 -070080 tasks like memory management, interrupt handling and DMA easier,
81 and provide a uniform interface to applications.
82 </para>
83 <para>
84 A note on versions: this guide covers features found in the DRM
85 tree, including the TTM memory manager, output configuration and
86 mode setting, and the new vblank internals, in addition to all
87 the regular features found in current kernels.
88 </para>
89 <para>
90 [Insert diagram of typical DRM stack here]
91 </para>
92 </chapter>
93
94 <!-- Internals -->
95
96 <chapter id="drmInternals">
97 <title>DRM Internals</title>
98 <para>
99 This chapter documents DRM internals relevant to driver authors
100 and developers working to add support for the latest features to
101 existing drivers.
102 </para>
103 <para>
Michael Wittena78f6782011-08-25 17:18:08 +0000104 First, we go over some typical driver initialization
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700105 requirements, like setting up command buffers, creating an
106 initial output configuration, and initializing core services.
Michael Wittena78f6782011-08-25 17:18:08 +0000107 Subsequent sections cover core internals in more detail,
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700108 providing implementation notes and examples.
109 </para>
110 <para>
111 The DRM layer provides several services to graphics drivers,
112 many of them driven by the application interfaces it provides
113 through libdrm, the library that wraps most of the DRM ioctls.
114 These include vblank event handling, memory
115 management, output management, framebuffer management, command
116 submission &amp; fencing, suspend/resume support, and DMA
117 services.
118 </para>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700119
120 <!-- Internals: driver init -->
121
122 <sect1>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200123 <title>Driver Initialization</title>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700124 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200125 At the core of every DRM driver is a <structname>drm_driver</structname>
126 structure. Drivers typically statically initialize a drm_driver structure,
127 and then pass it to one of the <function>drm_*_init()</function> functions
128 to register it with the DRM subsystem.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700129 </para>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700130 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200131 The <structname>drm_driver</structname> structure contains static
132 information that describes the driver and features it supports, and
133 pointers to methods that the DRM core will call to implement the DRM API.
134 We will first go through the <structname>drm_driver</structname> static
135 information fields, and will then describe individual operations in
136 details as they get used in later sections.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700137 </para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200138 <sect2>
139 <title>Driver Information</title>
140 <sect3>
141 <title>Driver Features</title>
142 <para>
143 Drivers inform the DRM core about their requirements and supported
144 features by setting appropriate flags in the
145 <structfield>driver_features</structfield> field. Since those flags
146 influence the DRM core behaviour since registration time, most of them
147 must be set to registering the <structname>drm_driver</structname>
148 instance.
149 </para>
150 <synopsis>u32 driver_features;</synopsis>
151 <variablelist>
152 <title>Driver Feature Flags</title>
153 <varlistentry>
154 <term>DRIVER_USE_AGP</term>
155 <listitem><para>
156 Driver uses AGP interface, the DRM core will manage AGP resources.
157 </para></listitem>
158 </varlistentry>
159 <varlistentry>
160 <term>DRIVER_REQUIRE_AGP</term>
161 <listitem><para>
162 Driver needs AGP interface to function. AGP initialization failure
163 will become a fatal error.
164 </para></listitem>
165 </varlistentry>
166 <varlistentry>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200167 <term>DRIVER_PCI_DMA</term>
168 <listitem><para>
169 Driver is capable of PCI DMA, mapping of PCI DMA buffers to
170 userspace will be enabled. Deprecated.
171 </para></listitem>
172 </varlistentry>
173 <varlistentry>
174 <term>DRIVER_SG</term>
175 <listitem><para>
176 Driver can perform scatter/gather DMA, allocation and mapping of
177 scatter/gather buffers will be enabled. Deprecated.
178 </para></listitem>
179 </varlistentry>
180 <varlistentry>
181 <term>DRIVER_HAVE_DMA</term>
182 <listitem><para>
183 Driver supports DMA, the userspace DMA API will be supported.
184 Deprecated.
185 </para></listitem>
186 </varlistentry>
187 <varlistentry>
188 <term>DRIVER_HAVE_IRQ</term><term>DRIVER_IRQ_SHARED</term>
189 <listitem><para>
Laurent Pinchart02b62982013-06-22 14:10:59 +0200190 DRIVER_HAVE_IRQ indicates whether the driver has an IRQ handler
191 managed by the DRM Core. The core will support simple IRQ handler
192 installation when the flag is set. The installation process is
193 described in <xref linkend="drm-irq-registration"/>.</para>
194 <para>DRIVER_IRQ_SHARED indicates whether the device &amp; handler
195 support shared IRQs (note that this is required of PCI drivers).
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200196 </para></listitem>
197 </varlistentry>
198 <varlistentry>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200199 <term>DRIVER_GEM</term>
200 <listitem><para>
201 Driver use the GEM memory manager.
202 </para></listitem>
203 </varlistentry>
204 <varlistentry>
205 <term>DRIVER_MODESET</term>
206 <listitem><para>
207 Driver supports mode setting interfaces (KMS).
208 </para></listitem>
209 </varlistentry>
210 <varlistentry>
211 <term>DRIVER_PRIME</term>
212 <listitem><para>
213 Driver implements DRM PRIME buffer sharing.
214 </para></listitem>
215 </varlistentry>
David Herrmann17931262013-08-25 18:29:00 +0200216 <varlistentry>
217 <term>DRIVER_RENDER</term>
218 <listitem><para>
219 Driver supports dedicated render nodes.
220 </para></listitem>
221 </varlistentry>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200222 </variablelist>
223 </sect3>
224 <sect3>
225 <title>Major, Minor and Patchlevel</title>
226 <synopsis>int major;
227int minor;
228int patchlevel;</synopsis>
229 <para>
230 The DRM core identifies driver versions by a major, minor and patch
231 level triplet. The information is printed to the kernel log at
232 initialization time and passed to userspace through the
233 DRM_IOCTL_VERSION ioctl.
234 </para>
235 <para>
236 The major and minor numbers are also used to verify the requested driver
237 API version passed to DRM_IOCTL_SET_VERSION. When the driver API changes
238 between minor versions, applications can call DRM_IOCTL_SET_VERSION to
239 select a specific version of the API. If the requested major isn't equal
240 to the driver major, or the requested minor is larger than the driver
241 minor, the DRM_IOCTL_SET_VERSION call will return an error. Otherwise
242 the driver's set_version() method will be called with the requested
243 version.
244 </para>
245 </sect3>
246 <sect3>
247 <title>Name, Description and Date</title>
248 <synopsis>char *name;
249char *desc;
250char *date;</synopsis>
251 <para>
252 The driver name is printed to the kernel log at initialization time,
253 used for IRQ registration and passed to userspace through
254 DRM_IOCTL_VERSION.
255 </para>
256 <para>
257 The driver description is a purely informative string passed to
258 userspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
259 the kernel.
260 </para>
261 <para>
262 The driver date, formatted as YYYYMMDD, is meant to identify the date of
263 the latest modification to the driver. However, as most drivers fail to
264 update it, its value is mostly useless. The DRM core prints it to the
265 kernel log at initialization time and passes it to userspace through the
266 DRM_IOCTL_VERSION ioctl.
267 </para>
268 </sect3>
269 </sect2>
270 <sect2>
271 <title>Driver Load</title>
272 <para>
273 The <methodname>load</methodname> method is the driver and device
274 initialization entry point. The method is responsible for allocating and
Daniel Vettere1f8ebd2014-01-22 16:32:47 +0100275 initializing driver private data, performing resource allocation and
276 mapping (e.g. acquiring
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200277 clocks, mapping registers or allocating command buffers), initializing
278 the memory manager (<xref linkend="drm-memory-management"/>), installing
279 the IRQ handler (<xref linkend="drm-irq-registration"/>), setting up
280 vertical blanking handling (<xref linkend="drm-vertical-blank"/>), mode
281 setting (<xref linkend="drm-mode-setting"/>) and initial output
282 configuration (<xref linkend="drm-kms-init"/>).
283 </para>
284 <note><para>
285 If compatibility is a concern (e.g. with drivers converted over from
286 User Mode Setting to Kernel Mode Setting), care must be taken to prevent
287 device initialization and control that is incompatible with currently
288 active userspace drivers. For instance, if user level mode setting
289 drivers are in use, it would be problematic to perform output discovery
290 &amp; configuration at load time. Likewise, if user-level drivers
291 unaware of memory management are in use, memory management and command
292 buffer setup may need to be omitted. These requirements are
293 driver-specific, and care needs to be taken to keep both old and new
294 applications and libraries working.
295 </para></note>
296 <synopsis>int (*load) (struct drm_device *, unsigned long flags);</synopsis>
297 <para>
298 The method takes two arguments, a pointer to the newly created
299 <structname>drm_device</structname> and flags. The flags are used to
300 pass the <structfield>driver_data</structfield> field of the device id
301 corresponding to the device passed to <function>drm_*_init()</function>.
302 Only PCI devices currently use this, USB and platform DRM drivers have
303 their <methodname>load</methodname> method called with flags to 0.
304 </para>
305 <sect3>
Daniel Vettere1f8ebd2014-01-22 16:32:47 +0100306 <title>Driver Private Data</title>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200307 <para>
308 The driver private hangs off the main
309 <structname>drm_device</structname> structure and can be used for
310 tracking various device-specific bits of information, like register
311 offsets, command buffer status, register state for suspend/resume, etc.
312 At load time, a driver may simply allocate one and set
313 <structname>drm_device</structname>.<structfield>dev_priv</structfield>
314 appropriately; it should be freed and
315 <structname>drm_device</structname>.<structfield>dev_priv</structfield>
316 set to NULL when the driver is unloaded.
317 </para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200318 </sect3>
319 <sect3 id="drm-irq-registration">
320 <title>IRQ Registration</title>
321 <para>
322 The DRM core tries to facilitate IRQ handler registration and
323 unregistration by providing <function>drm_irq_install</function> and
324 <function>drm_irq_uninstall</function> functions. Those functions only
Laurent Pinchart02b62982013-06-22 14:10:59 +0200325 support a single interrupt per device, devices that use more than one
326 IRQs need to be handled manually.
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200327 </para>
Laurent Pinchart02b62982013-06-22 14:10:59 +0200328 <sect4>
329 <title>Managed IRQ Registration</title>
330 <para>
331 Both the <function>drm_irq_install</function> and
332 <function>drm_irq_uninstall</function> functions get the device IRQ by
333 calling <function>drm_dev_to_irq</function>. This inline function will
334 call a bus-specific operation to retrieve the IRQ number. For platform
335 devices, <function>platform_get_irq</function>(..., 0) is used to
336 retrieve the IRQ number.
337 </para>
338 <para>
339 <function>drm_irq_install</function> starts by calling the
340 <methodname>irq_preinstall</methodname> driver operation. The operation
341 is optional and must make sure that the interrupt will not get fired by
342 clearing all pending interrupt flags or disabling the interrupt.
343 </para>
344 <para>
345 The IRQ will then be requested by a call to
346 <function>request_irq</function>. If the DRIVER_IRQ_SHARED driver
347 feature flag is set, a shared (IRQF_SHARED) IRQ handler will be
348 requested.
349 </para>
350 <para>
351 The IRQ handler function must be provided as the mandatory irq_handler
352 driver operation. It will get passed directly to
353 <function>request_irq</function> and thus has the same prototype as all
354 IRQ handlers. It will get called with a pointer to the DRM device as the
355 second argument.
356 </para>
357 <para>
358 Finally the function calls the optional
359 <methodname>irq_postinstall</methodname> driver operation. The operation
360 usually enables interrupts (excluding the vblank interrupt, which is
361 enabled separately), but drivers may choose to enable/disable interrupts
362 at a different time.
363 </para>
364 <para>
365 <function>drm_irq_uninstall</function> is similarly used to uninstall an
366 IRQ handler. It starts by waking up all processes waiting on a vblank
367 interrupt to make sure they don't hang, and then calls the optional
368 <methodname>irq_uninstall</methodname> driver operation. The operation
369 must disable all hardware interrupts. Finally the function frees the IRQ
370 by calling <function>free_irq</function>.
371 </para>
372 </sect4>
373 <sect4>
374 <title>Manual IRQ Registration</title>
375 <para>
376 Drivers that require multiple interrupt handlers can't use the managed
377 IRQ registration functions. In that case IRQs must be registered and
378 unregistered manually (usually with the <function>request_irq</function>
379 and <function>free_irq</function> functions, or their devm_* equivalent).
380 </para>
381 <para>
382 When manually registering IRQs, drivers must not set the DRIVER_HAVE_IRQ
383 driver feature flag, and must not provide the
384 <methodname>irq_handler</methodname> driver operation. They must set the
385 <structname>drm_device</structname> <structfield>irq_enabled</structfield>
386 field to 1 upon registration of the IRQs, and clear it to 0 after
387 unregistering the IRQs.
388 </para>
389 </sect4>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200390 </sect3>
391 <sect3>
392 <title>Memory Manager Initialization</title>
393 <para>
394 Every DRM driver requires a memory manager which must be initialized at
395 load time. DRM currently contains two memory managers, the Translation
396 Table Manager (TTM) and the Graphics Execution Manager (GEM).
397 This document describes the use of the GEM memory manager only. See
398 <xref linkend="drm-memory-management"/> for details.
399 </para>
400 </sect3>
401 <sect3>
402 <title>Miscellaneous Device Configuration</title>
403 <para>
404 Another task that may be necessary for PCI devices during configuration
405 is mapping the video BIOS. On many devices, the VBIOS describes device
406 configuration, LCD panel timings (if any), and contains flags indicating
407 device state. Mapping the BIOS can be done using the pci_map_rom() call,
408 a convenience function that takes care of mapping the actual ROM,
409 whether it has been shadowed into memory (typically at address 0xc0000)
410 or exists on the PCI device in the ROM BAR. Note that after the ROM has
411 been mapped and any necessary information has been extracted, it should
412 be unmapped; on many devices, the ROM address decoder is shared with
413 other BARs, so leaving it mapped could cause undesired behaviour like
414 hangs or memory corruption.
415 <!--!Fdrivers/pci/rom.c pci_map_rom-->
416 </para>
417 </sect3>
418 </sect2>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700419 </sect1>
420
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200421 <!-- Internals: memory management -->
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700422
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200423 <sect1 id="drm-memory-management">
424 <title>Memory management</title>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700425 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200426 Modern Linux systems require large amount of graphics memory to store
427 frame buffers, textures, vertices and other graphics-related data. Given
428 the very dynamic nature of many of that data, managing graphics memory
429 efficiently is thus crucial for the graphics stack and plays a central
430 role in the DRM infrastructure.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700431 </para>
432 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200433 The DRM core includes two memory managers, namely Translation Table Maps
434 (TTM) and Graphics Execution Manager (GEM). TTM was the first DRM memory
435 manager to be developed and tried to be a one-size-fits-them all
Anatol Pomozovf884ab12013-05-08 16:56:16 -0700436 solution. It provides a single userspace API to accommodate the need of
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200437 all hardware, supporting both Unified Memory Architecture (UMA) devices
438 and devices with dedicated video RAM (i.e. most discrete video cards).
439 This resulted in a large, complex piece of code that turned out to be
440 hard to use for driver development.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700441 </para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200442 <para>
443 GEM started as an Intel-sponsored project in reaction to TTM's
444 complexity. Its design philosophy is completely different: instead of
445 providing a solution to every graphics memory-related problems, GEM
446 identified common code between drivers and created a support library to
447 share it. GEM has simpler initialization and execution requirements than
448 TTM, but has no video RAM management capabitilies and is thus limited to
449 UMA devices.
450 </para>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700451 <sect2>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200452 <title>The Translation Table Manager (TTM)</title>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700453 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200454 TTM design background and information belongs here.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700455 </para>
456 <sect3>
457 <title>TTM initialization</title>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200458 <warning><para>This section is outdated.</para></warning>
459 <para>
460 Drivers wishing to support TTM must fill out a drm_bo_driver
461 structure. The structure contains several fields with function
462 pointers for initializing the TTM, allocating and freeing memory,
463 waiting for command completion and fence synchronization, and memory
464 migration. See the radeon_ttm.c file for an example of usage.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700465 </para>
466 <para>
467 The ttm_global_reference structure is made up of several fields:
468 </para>
469 <programlisting>
470 struct ttm_global_reference {
471 enum ttm_global_types global_type;
472 size_t size;
473 void *object;
474 int (*init) (struct ttm_global_reference *);
475 void (*release) (struct ttm_global_reference *);
476 };
477 </programlisting>
478 <para>
479 There should be one global reference structure for your memory
480 manager as a whole, and there will be others for each object
481 created by the memory manager at runtime. Your global TTM should
482 have a type of TTM_GLOBAL_TTM_MEM. The size field for the global
483 object should be sizeof(struct ttm_mem_global), and the init and
Michael Wittena5294e02011-08-29 18:05:52 +0000484 release hooks should point at your driver-specific init and
Michael Wittena78f6782011-08-25 17:18:08 +0000485 release routines, which probably eventually call
Michael Witten005d7f42011-08-25 19:02:52 +0000486 ttm_mem_global_init and ttm_mem_global_release, respectively.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700487 </para>
488 <para>
489 Once your global TTM accounting structure is set up and initialized
Michael Wittenae63d792011-08-25 19:19:18 +0000490 by calling ttm_global_item_ref() on it,
Michael Witten1c86de22011-08-25 19:14:26 +0000491 you need to create a buffer object TTM to
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700492 provide a pool for buffer object allocation by clients and the
493 kernel itself. The type of this object should be TTM_GLOBAL_TTM_BO,
494 and its size should be sizeof(struct ttm_bo_global). Again,
Michael Wittena5294e02011-08-29 18:05:52 +0000495 driver-specific init and release functions may be provided,
Michael Wittenae63d792011-08-25 19:19:18 +0000496 likely eventually calling ttm_bo_global_init() and
497 ttm_bo_global_release(), respectively. Also, like the previous
498 object, ttm_global_item_ref() is used to create an initial reference
Nicolas Kaiserce04cc02010-05-28 07:33:49 +0200499 count for the TTM, which will call your initialization function.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700500 </para>
501 </sect3>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700502 </sect2>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200503 <sect2 id="drm-gem">
504 <title>The Graphics Execution Manager (GEM)</title>
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700505 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200506 The GEM design approach has resulted in a memory manager that doesn't
507 provide full coverage of all (or even all common) use cases in its
508 userspace or kernel API. GEM exposes a set of standard memory-related
509 operations to userspace and a set of helper functions to drivers, and let
510 drivers implement hardware-specific operations with their own private API.
511 </para>
512 <para>
513 The GEM userspace API is described in the
514 <ulink url="http://lwn.net/Articles/283798/"><citetitle>GEM - the Graphics
515 Execution Manager</citetitle></ulink> article on LWN. While slightly
516 outdated, the document provides a good overview of the GEM API principles.
517 Buffer allocation and read and write operations, described as part of the
518 common GEM API, are currently implemented using driver-specific ioctls.
519 </para>
520 <para>
521 GEM is data-agnostic. It manages abstract buffer objects without knowing
522 what individual buffers contain. APIs that require knowledge of buffer
523 contents or purpose, such as buffer allocation or synchronization
524 primitives, are thus outside of the scope of GEM and must be implemented
525 using driver-specific ioctls.
526 </para>
527 <para>
528 On a fundamental level, GEM involves several operations:
Michael Witten327d6fb2011-08-25 20:18:14 +0000529 <itemizedlist>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200530 <listitem>Memory allocation and freeing</listitem>
531 <listitem>Command execution</listitem>
532 <listitem>Aperture management at command execution time</listitem>
Michael Witten327d6fb2011-08-25 20:18:14 +0000533 </itemizedlist>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200534 Buffer object allocation is relatively straightforward and largely
535 provided by Linux's shmem layer, which provides memory to back each
536 object.
537 </para>
538 <para>
539 Device-specific operations, such as command execution, pinning, buffer
540 read &amp; write, mapping, and domain ownership transfers are left to
541 driver-specific ioctls.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700542 </para>
543 <sect3>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200544 <title>GEM Initialization</title>
545 <para>
546 Drivers that use GEM must set the DRIVER_GEM bit in the struct
547 <structname>drm_driver</structname>
548 <structfield>driver_features</structfield> field. The DRM core will
549 then automatically initialize the GEM core before calling the
550 <methodname>load</methodname> operation. Behind the scene, this will
551 create a DRM Memory Manager object which provides an address space
552 pool for object allocation.
553 </para>
554 <para>
555 In a KMS configuration, drivers need to allocate and initialize a
556 command ring buffer following core GEM initialization if required by
557 the hardware. UMA devices usually have what is called a "stolen"
558 memory region, which provides space for the initial framebuffer and
559 large, contiguous memory regions required by the device. This space is
560 typically not managed by GEM, and must be initialized separately into
561 its own DRM MM object.
562 </para>
563 </sect3>
564 <sect3>
565 <title>GEM Objects Creation</title>
566 <para>
567 GEM splits creation of GEM objects and allocation of the memory that
568 backs them in two distinct operations.
569 </para>
570 <para>
571 GEM objects are represented by an instance of struct
572 <structname>drm_gem_object</structname>. Drivers usually need to extend
573 GEM objects with private information and thus create a driver-specific
574 GEM object structure type that embeds an instance of struct
575 <structname>drm_gem_object</structname>.
576 </para>
577 <para>
578 To create a GEM object, a driver allocates memory for an instance of its
579 specific GEM object type and initializes the embedded struct
580 <structname>drm_gem_object</structname> with a call to
581 <function>drm_gem_object_init</function>. The function takes a pointer to
582 the DRM device, a pointer to the GEM object and the buffer object size
583 in bytes.
584 </para>
585 <para>
586 GEM uses shmem to allocate anonymous pageable memory.
587 <function>drm_gem_object_init</function> will create an shmfs file of
588 the requested size and store it into the struct
589 <structname>drm_gem_object</structname> <structfield>filp</structfield>
590 field. The memory is used as either main storage for the object when the
591 graphics hardware uses system memory directly or as a backing store
592 otherwise.
593 </para>
594 <para>
595 Drivers are responsible for the actual physical pages allocation by
596 calling <function>shmem_read_mapping_page_gfp</function> for each page.
597 Note that they can decide to allocate pages when initializing the GEM
598 object, or to delay allocation until the memory is needed (for instance
599 when a page fault occurs as a result of a userspace memory access or
600 when the driver needs to start a DMA transfer involving the memory).
601 </para>
602 <para>
603 Anonymous pageable memory allocation is not always desired, for instance
604 when the hardware requires physically contiguous system memory as is
605 often the case in embedded devices. Drivers can create GEM objects with
606 no shmfs backing (called private GEM objects) by initializing them with
607 a call to <function>drm_gem_private_object_init</function> instead of
608 <function>drm_gem_object_init</function>. Storage for private GEM
609 objects must be managed by drivers.
610 </para>
611 <para>
612 Drivers that do not need to extend GEM objects with private information
613 can call the <function>drm_gem_object_alloc</function> function to
614 allocate and initialize a struct <structname>drm_gem_object</structname>
615 instance. The GEM core will call the optional driver
616 <methodname>gem_init_object</methodname> operation after initializing
617 the GEM object with <function>drm_gem_object_init</function>.
618 <synopsis>int (*gem_init_object) (struct drm_gem_object *obj);</synopsis>
619 </para>
620 <para>
621 No alloc-and-init function exists for private GEM objects.
622 </para>
623 </sect3>
624 <sect3>
625 <title>GEM Objects Lifetime</title>
626 <para>
627 All GEM objects are reference-counted by the GEM core. References can be
628 acquired and release by <function>calling drm_gem_object_reference</function>
629 and <function>drm_gem_object_unreference</function> respectively. The
630 caller must hold the <structname>drm_device</structname>
631 <structfield>struct_mutex</structfield> lock. As a convenience, GEM
632 provides the <function>drm_gem_object_reference_unlocked</function> and
633 <function>drm_gem_object_unreference_unlocked</function> functions that
634 can be called without holding the lock.
635 </para>
636 <para>
637 When the last reference to a GEM object is released the GEM core calls
638 the <structname>drm_driver</structname>
639 <methodname>gem_free_object</methodname> operation. That operation is
640 mandatory for GEM-enabled drivers and must free the GEM object and all
641 associated resources.
642 </para>
643 <para>
644 <synopsis>void (*gem_free_object) (struct drm_gem_object *obj);</synopsis>
645 Drivers are responsible for freeing all GEM object resources, including
646 the resources created by the GEM core. If an mmap offset has been
647 created for the object (in which case
648 <structname>drm_gem_object</structname>::<structfield>map_list</structfield>::<structfield>map</structfield>
649 is not NULL) it must be freed by a call to
650 <function>drm_gem_free_mmap_offset</function>. The shmfs backing store
651 must be released by calling <function>drm_gem_object_release</function>
652 (that function can safely be called if no shmfs backing store has been
653 created).
654 </para>
655 </sect3>
656 <sect3>
657 <title>GEM Objects Naming</title>
658 <para>
659 Communication between userspace and the kernel refers to GEM objects
660 using local handles, global names or, more recently, file descriptors.
661 All of those are 32-bit integer values; the usual Linux kernel limits
662 apply to the file descriptors.
663 </para>
664 <para>
665 GEM handles are local to a DRM file. Applications get a handle to a GEM
666 object through a driver-specific ioctl, and can use that handle to refer
667 to the GEM object in other standard or driver-specific ioctls. Closing a
668 DRM file handle frees all its GEM handles and dereferences the
669 associated GEM objects.
670 </para>
671 <para>
672 To create a handle for a GEM object drivers call
673 <function>drm_gem_handle_create</function>. The function takes a pointer
674 to the DRM file and the GEM object and returns a locally unique handle.
675 When the handle is no longer needed drivers delete it with a call to
676 <function>drm_gem_handle_delete</function>. Finally the GEM object
677 associated with a handle can be retrieved by a call to
678 <function>drm_gem_object_lookup</function>.
679 </para>
680 <para>
681 Handles don't take ownership of GEM objects, they only take a reference
682 to the object that will be dropped when the handle is destroyed. To
683 avoid leaking GEM objects, drivers must make sure they drop the
684 reference(s) they own (such as the initial reference taken at object
685 creation time) as appropriate, without any special consideration for the
686 handle. For example, in the particular case of combined GEM object and
687 handle creation in the implementation of the
688 <methodname>dumb_create</methodname> operation, drivers must drop the
689 initial reference to the GEM object before returning the handle.
690 </para>
691 <para>
692 GEM names are similar in purpose to handles but are not local to DRM
693 files. They can be passed between processes to reference a GEM object
694 globally. Names can't be used directly to refer to objects in the DRM
695 API, applications must convert handles to names and names to handles
696 using the DRM_IOCTL_GEM_FLINK and DRM_IOCTL_GEM_OPEN ioctls
697 respectively. The conversion is handled by the DRM core without any
698 driver-specific support.
699 </para>
Daniel Vetter251261d2014-01-22 18:46:33 +0100700 <para>
701 GEM also supports buffer sharing with dma-buf file descriptors through
702 PRIME. GEM-based drivers must use the provided helpers functions to
703 implement the exporting and importing correctly. See <xref linkend="drm-prime-support" />.
704 Since sharing file descriptors is inherently more secure than the
705 easily guessable and global GEM names it is the preferred buffer
706 sharing mechanism. Sharing buffers through GEM names is only supported
707 for legacy userspace. Furthermore PRIME also allows cross-device
708 buffer sharing since it is based on dma-bufs.
709 </para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200710 </sect3>
711 <sect3 id="drm-gem-objects-mapping">
712 <title>GEM Objects Mapping</title>
713 <para>
714 Because mapping operations are fairly heavyweight GEM favours
715 read/write-like access to buffers, implemented through driver-specific
716 ioctls, over mapping buffers to userspace. However, when random access
717 to the buffer is needed (to perform software rendering for instance),
718 direct access to the object can be more efficient.
719 </para>
720 <para>
721 The mmap system call can't be used directly to map GEM objects, as they
722 don't have their own file handle. Two alternative methods currently
723 co-exist to map GEM objects to userspace. The first method uses a
724 driver-specific ioctl to perform the mapping operation, calling
725 <function>do_mmap</function> under the hood. This is often considered
726 dubious, seems to be discouraged for new GEM-enabled drivers, and will
727 thus not be described here.
728 </para>
729 <para>
730 The second method uses the mmap system call on the DRM file handle.
731 <synopsis>void *mmap(void *addr, size_t length, int prot, int flags, int fd,
732 off_t offset);</synopsis>
733 DRM identifies the GEM object to be mapped by a fake offset passed
734 through the mmap offset argument. Prior to being mapped, a GEM object
735 must thus be associated with a fake offset. To do so, drivers must call
736 <function>drm_gem_create_mmap_offset</function> on the object. The
737 function allocates a fake offset range from a pool and stores the
738 offset divided by PAGE_SIZE in
739 <literal>obj-&gt;map_list.hash.key</literal>. Care must be taken not to
740 call <function>drm_gem_create_mmap_offset</function> if a fake offset
741 has already been allocated for the object. This can be tested by
742 <literal>obj-&gt;map_list.map</literal> being non-NULL.
743 </para>
744 <para>
745 Once allocated, the fake offset value
746 (<literal>obj-&gt;map_list.hash.key &lt;&lt; PAGE_SHIFT</literal>)
747 must be passed to the application in a driver-specific way and can then
748 be used as the mmap offset argument.
749 </para>
750 <para>
751 The GEM core provides a helper method <function>drm_gem_mmap</function>
752 to handle object mapping. The method can be set directly as the mmap
753 file operation handler. It will look up the GEM object based on the
754 offset value and set the VMA operations to the
755 <structname>drm_driver</structname> <structfield>gem_vm_ops</structfield>
756 field. Note that <function>drm_gem_mmap</function> doesn't map memory to
757 userspace, but relies on the driver-provided fault handler to map pages
758 individually.
759 </para>
760 <para>
761 To use <function>drm_gem_mmap</function>, drivers must fill the struct
762 <structname>drm_driver</structname> <structfield>gem_vm_ops</structfield>
763 field with a pointer to VM operations.
764 </para>
765 <para>
766 <synopsis>struct vm_operations_struct *gem_vm_ops
767
768 struct vm_operations_struct {
769 void (*open)(struct vm_area_struct * area);
770 void (*close)(struct vm_area_struct * area);
771 int (*fault)(struct vm_area_struct *vma, struct vm_fault *vmf);
772 };</synopsis>
773 </para>
774 <para>
775 The <methodname>open</methodname> and <methodname>close</methodname>
776 operations must update the GEM object reference count. Drivers can use
777 the <function>drm_gem_vm_open</function> and
778 <function>drm_gem_vm_close</function> helper functions directly as open
779 and close handlers.
780 </para>
781 <para>
782 The fault operation handler is responsible for mapping individual pages
783 to userspace when a page fault occurs. Depending on the memory
784 allocation scheme, drivers can allocate pages at fault time, or can
785 decide to allocate memory for the GEM object at the time the object is
786 created.
787 </para>
788 <para>
789 Drivers that want to map the GEM object upfront instead of handling page
790 faults can implement their own mmap file operation handler.
791 </para>
792 </sect3>
793 <sect3>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200794 <title>Memory Coherency</title>
795 <para>
796 When mapped to the device or used in a command buffer, backing pages
797 for an object are flushed to memory and marked write combined so as to
798 be coherent with the GPU. Likewise, if the CPU accesses an object
799 after the GPU has finished rendering to the object, then the object
800 must be made coherent with the CPU's view of memory, usually involving
801 GPU cache flushing of various kinds. This core CPU&lt;-&gt;GPU
802 coherency management is provided by a device-specific ioctl, which
803 evaluates an object's current domain and performs any necessary
804 flushing or synchronization to put the object into the desired
805 coherency domain (note that the object may be busy, i.e. an active
806 render target; in that case, setting the domain blocks the client and
807 waits for rendering to complete before performing any necessary
808 flushing operations).
809 </para>
810 </sect3>
811 <sect3>
812 <title>Command Execution</title>
813 <para>
814 Perhaps the most important GEM function for GPU devices is providing a
815 command execution interface to clients. Client programs construct
816 command buffers containing references to previously allocated memory
817 objects, and then submit them to GEM. At that point, GEM takes care to
818 bind all the objects into the GTT, execute the buffer, and provide
819 necessary synchronization between clients accessing the same buffers.
820 This often involves evicting some objects from the GTT and re-binding
821 others (a fairly expensive operation), and providing relocation
822 support which hides fixed GTT offsets from clients. Clients must take
823 care not to submit command buffers that reference more objects than
824 can fit in the GTT; otherwise, GEM will reject them and no rendering
825 will occur. Similarly, if several objects in the buffer require fence
826 registers to be allocated for correct rendering (e.g. 2D blits on
827 pre-965 chips), care must be taken not to require more fence registers
828 than are available to the client. Such resource management should be
829 abstracted from the client in libdrm.
830 </para>
831 </sect3>
Daniel Vetter251261d2014-01-22 18:46:33 +0100832 <sect3>
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100833 <title>GEM Function Reference</title>
834!Edrivers/gpu/drm/drm_gem.c
Daniel Vetter251261d2014-01-22 18:46:33 +0100835 </sect3>
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100836 </sect2>
Daniel Vetter4c5acf32014-01-22 12:28:42 +0100837 <sect2>
838 <title>VMA Offset Manager</title>
839!Pdrivers/gpu/drm/drm_vma_manager.c vma offset manager
840!Edrivers/gpu/drm/drm_vma_manager.c
841!Iinclude/drm/drm_vma_manager.h
842 </sect2>
Daniel Vetter251261d2014-01-22 18:46:33 +0100843 <sect2 id="drm-prime-support">
844 <title>PRIME Buffer Sharing</title>
845 <para>
846 PRIME is the cross device buffer sharing framework in drm, originally
847 created for the OPTIMUS range of multi-gpu platforms. To userspace
848 PRIME buffers are dma-buf based file descriptors.
849 </para>
850 <sect3>
851 <title>Overview and Driver Interface</title>
852 <para>
853 Similar to GEM global names, PRIME file descriptors are
854 also used to share buffer objects across processes. They offer
855 additional security: as file descriptors must be explicitly sent over
856 UNIX domain sockets to be shared between applications, they can't be
857 guessed like the globally unique GEM names.
858 </para>
859 <para>
860 Drivers that support the PRIME
861 API must set the DRIVER_PRIME bit in the struct
862 <structname>drm_driver</structname>
863 <structfield>driver_features</structfield> field, and implement the
864 <methodname>prime_handle_to_fd</methodname> and
865 <methodname>prime_fd_to_handle</methodname> operations.
866 </para>
867 <para>
868 <synopsis>int (*prime_handle_to_fd)(struct drm_device *dev,
869 struct drm_file *file_priv, uint32_t handle,
870 uint32_t flags, int *prime_fd);
871int (*prime_fd_to_handle)(struct drm_device *dev,
872 struct drm_file *file_priv, int prime_fd,
873 uint32_t *handle);</synopsis>
874 Those two operations convert a handle to a PRIME file descriptor and
875 vice versa. Drivers must use the kernel dma-buf buffer sharing framework
876 to manage the PRIME file descriptors. Similar to the mode setting
877 API PRIME is agnostic to the underlying buffer object manager, as
878 long as handles are 32bit unsinged integers.
879 </para>
880 <para>
881 While non-GEM drivers must implement the operations themselves, GEM
882 drivers must use the <function>drm_gem_prime_handle_to_fd</function>
883 and <function>drm_gem_prime_fd_to_handle</function> helper functions.
884 Those helpers rely on the driver
885 <methodname>gem_prime_export</methodname> and
886 <methodname>gem_prime_import</methodname> operations to create a dma-buf
887 instance from a GEM object (dma-buf exporter role) and to create a GEM
888 object from a dma-buf instance (dma-buf importer role).
889 </para>
890 <para>
891 <synopsis>struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
892 struct drm_gem_object *obj,
893 int flags);
894struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
895 struct dma_buf *dma_buf);</synopsis>
896 These two operations are mandatory for GEM drivers that support
897 PRIME.
898 </para>
899 </sect3>
900 <sect3>
901 <title>PRIME Helper Functions Reference</title>
902!Pdrivers/gpu/drm/drm_prime.c PRIME Helpers
903 </sect3>
904 </sect2>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200905 </sect1>
906
907 <!-- Internals: mode setting -->
908
909 <sect1 id="drm-mode-setting">
910 <title>Mode Setting</title>
911 <para>
912 Drivers must initialize the mode setting core by calling
913 <function>drm_mode_config_init</function> on the DRM device. The function
914 initializes the <structname>drm_device</structname>
915 <structfield>mode_config</structfield> field and never fails. Once done,
916 mode configuration must be setup by initializing the following fields.
917 </para>
918 <itemizedlist>
919 <listitem>
920 <synopsis>int min_width, min_height;
921int max_width, max_height;</synopsis>
922 <para>
923 Minimum and maximum width and height of the frame buffers in pixel
924 units.
Jesse Barnes2d2ef822009-10-26 13:06:31 -0700925 </para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200926 </listitem>
927 <listitem>
928 <synopsis>struct drm_mode_config_funcs *funcs;</synopsis>
929 <para>Mode setting functions.</para>
930 </listitem>
931 </itemizedlist>
932 <sect2>
933 <title>Frame Buffer Creation</title>
934 <synopsis>struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
935 struct drm_file *file_priv,
936 struct drm_mode_fb_cmd2 *mode_cmd);</synopsis>
937 <para>
938 Frame buffers are abstract memory objects that provide a source of
939 pixels to scanout to a CRTC. Applications explicitly request the
940 creation of frame buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and
941 receive an opaque handle that can be passed to the KMS CRTC control,
942 plane configuration and page flip functions.
943 </para>
944 <para>
945 Frame buffers rely on the underneath memory manager for low-level memory
946 operations. When creating a frame buffer applications pass a memory
947 handle (or a list of memory handles for multi-planar formats) through
Daniel Vetter065a5022014-01-21 12:01:41 +0100948 the <parameter>drm_mode_fb_cmd2</parameter> argument. For drivers using
949 GEM as their userspace buffer management interface this would be a GEM
950 handle. Drivers are however free to use their own backing storage object
951 handles, e.g. vmwgfx directly exposes special TTM handles to userspace
952 and so expects TTM handles in the create ioctl and not GEM handles.
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200953 </para>
954 <para>
955 Drivers must first validate the requested frame buffer parameters passed
956 through the mode_cmd argument. In particular this is where invalid
957 sizes, pixel formats or pitches can be caught.
958 </para>
959 <para>
960 If the parameters are deemed valid, drivers then create, initialize and
961 return an instance of struct <structname>drm_framebuffer</structname>.
962 If desired the instance can be embedded in a larger driver-specific
Daniel Vetter5d7a9512013-01-04 22:31:20 +0100963 structure. Drivers must fill its <structfield>width</structfield>,
964 <structfield>height</structfield>, <structfield>pitches</structfield>,
965 <structfield>offsets</structfield>, <structfield>depth</structfield>,
966 <structfield>bits_per_pixel</structfield> and
967 <structfield>pixel_format</structfield> fields from the values passed
968 through the <parameter>drm_mode_fb_cmd2</parameter> argument. They
969 should call the <function>drm_helper_mode_fill_fb_struct</function>
970 helper function to do so.
971 </para>
972
973 <para>
Daniel Vetter065a5022014-01-21 12:01:41 +0100974 The initialization of the new framebuffer instance is finalized with a
Daniel Vetter5d7a9512013-01-04 22:31:20 +0100975 call to <function>drm_framebuffer_init</function> which takes a pointer
976 to DRM frame buffer operations (struct
977 <structname>drm_framebuffer_funcs</structname>). Note that this function
978 publishes the framebuffer and so from this point on it can be accessed
979 concurrently from other threads. Hence it must be the last step in the
980 driver's framebuffer initialization sequence. Frame buffer operations
981 are
Laurent Pinchart9cad9c92012-07-13 00:57:26 +0200982 <itemizedlist>
983 <listitem>
984 <synopsis>int (*create_handle)(struct drm_framebuffer *fb,
985 struct drm_file *file_priv, unsigned int *handle);</synopsis>
986 <para>
987 Create a handle to the frame buffer underlying memory object. If
988 the frame buffer uses a multi-plane format, the handle will
989 reference the memory object associated with the first plane.
990 </para>
991 <para>
992 Drivers call <function>drm_gem_handle_create</function> to create
993 the handle.
994 </para>
995 </listitem>
996 <listitem>
997 <synopsis>void (*destroy)(struct drm_framebuffer *framebuffer);</synopsis>
998 <para>
999 Destroy the frame buffer object and frees all associated
1000 resources. Drivers must call
1001 <function>drm_framebuffer_cleanup</function> to free resources
1002 allocated by the DRM core for the frame buffer object, and must
1003 make sure to unreference all memory objects associated with the
1004 frame buffer. Handles created by the
1005 <methodname>create_handle</methodname> operation are released by
1006 the DRM core.
1007 </para>
1008 </listitem>
1009 <listitem>
1010 <synopsis>int (*dirty)(struct drm_framebuffer *framebuffer,
1011 struct drm_file *file_priv, unsigned flags, unsigned color,
1012 struct drm_clip_rect *clips, unsigned num_clips);</synopsis>
1013 <para>
1014 This optional operation notifies the driver that a region of the
1015 frame buffer has changed in response to a DRM_IOCTL_MODE_DIRTYFB
1016 ioctl call.
1017 </para>
1018 </listitem>
1019 </itemizedlist>
1020 </para>
1021 <para>
Daniel Vetter5d7a9512013-01-04 22:31:20 +01001022 The lifetime of a drm framebuffer is controlled with a reference count,
1023 drivers can grab additional references with
1024 <function>drm_framebuffer_reference</function> </para> and drop them
1025 again with <function>drm_framebuffer_unreference</function>. For
1026 driver-private framebuffers for which the last reference is never
1027 dropped (e.g. for the fbdev framebuffer when the struct
1028 <structname>drm_framebuffer</structname> is embedded into the fbdev
1029 helper struct) drivers can manually clean up a framebuffer at module
1030 unload time with
1031 <function>drm_framebuffer_unregister_private</function>.
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001032 </sect2>
1033 <sect2>
Daniel Vetter065a5022014-01-21 12:01:41 +01001034 <title>Dumb Buffer Objects</title>
1035 <para>
1036 The KMS API doesn't standardize backing storage object creation and
1037 leaves it to driver-specific ioctls. Furthermore actually creating a
1038 buffer object even for GEM-based drivers is done through a
1039 driver-specific ioctl - GEM only has a common userspace interface for
1040 sharing and destroying objects. While not an issue for full-fledged
1041 graphics stacks that include device-specific userspace components (in
1042 libdrm for instance), this limit makes DRM-based early boot graphics
1043 unnecessarily complex.
1044 </para>
1045 <para>
1046 Dumb objects partly alleviate the problem by providing a standard
1047 API to create dumb buffers suitable for scanout, which can then be used
1048 to create KMS frame buffers.
1049 </para>
1050 <para>
1051 To support dumb objects drivers must implement the
1052 <methodname>dumb_create</methodname>,
1053 <methodname>dumb_destroy</methodname> and
1054 <methodname>dumb_map_offset</methodname> operations.
1055 </para>
1056 <itemizedlist>
1057 <listitem>
1058 <synopsis>int (*dumb_create)(struct drm_file *file_priv, struct drm_device *dev,
1059 struct drm_mode_create_dumb *args);</synopsis>
1060 <para>
1061 The <methodname>dumb_create</methodname> operation creates a driver
1062 object (GEM or TTM handle) suitable for scanout based on the
1063 width, height and depth from the struct
1064 <structname>drm_mode_create_dumb</structname> argument. It fills the
1065 argument's <structfield>handle</structfield>,
1066 <structfield>pitch</structfield> and <structfield>size</structfield>
1067 fields with a handle for the newly created object and its line
1068 pitch and size in bytes.
1069 </para>
1070 </listitem>
1071 <listitem>
1072 <synopsis>int (*dumb_destroy)(struct drm_file *file_priv, struct drm_device *dev,
1073 uint32_t handle);</synopsis>
1074 <para>
1075 The <methodname>dumb_destroy</methodname> operation destroys a dumb
1076 object created by <methodname>dumb_create</methodname>.
1077 </para>
1078 </listitem>
1079 <listitem>
1080 <synopsis>int (*dumb_map_offset)(struct drm_file *file_priv, struct drm_device *dev,
1081 uint32_t handle, uint64_t *offset);</synopsis>
1082 <para>
1083 The <methodname>dumb_map_offset</methodname> operation associates an
1084 mmap fake offset with the object given by the handle and returns
1085 it. Drivers must use the
1086 <function>drm_gem_create_mmap_offset</function> function to
1087 associate the fake offset as described in
1088 <xref linkend="drm-gem-objects-mapping"/>.
1089 </para>
1090 </listitem>
1091 </itemizedlist>
1092 <para>
1093 Note that dumb objects may not be used for gpu acceleration, as has been
1094 attempted on some ARM embedded platforms. Such drivers really must have
1095 a hardware-specific ioctl to allocate suitable buffer objects.
1096 </para>
1097 </sect2>
1098 <sect2>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001099 <title>Output Polling</title>
1100 <synopsis>void (*output_poll_changed)(struct drm_device *dev);</synopsis>
1101 <para>
1102 This operation notifies the driver that the status of one or more
1103 connectors has changed. Drivers that use the fb helper can just call the
1104 <function>drm_fb_helper_hotplug_event</function> function to handle this
1105 operation.
1106 </para>
1107 </sect2>
Daniel Vetter5d7a9512013-01-04 22:31:20 +01001108 <sect2>
1109 <title>Locking</title>
1110 <para>
1111 Beside some lookup structures with their own locking (which is hidden
1112 behind the interface functions) most of the modeset state is protected
1113 by the <code>dev-&lt;mode_config.lock</code> mutex and additionally
1114 per-crtc locks to allow cursor updates, pageflips and similar operations
1115 to occur concurrently with background tasks like output detection.
1116 Operations which cross domains like a full modeset always grab all
1117 locks. Drivers there need to protect resources shared between crtcs with
1118 additional locking. They also need to be careful to always grab the
1119 relevant crtc locks if a modset functions touches crtc state, e.g. for
1120 load detection (which does only grab the <code>mode_config.lock</code>
1121 to allow concurrent screen updates on live crtcs).
1122 </para>
1123 </sect2>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001124 </sect1>
1125
1126 <!-- Internals: kms initialization and cleanup -->
1127
1128 <sect1 id="drm-kms-init">
1129 <title>KMS Initialization and Cleanup</title>
1130 <para>
1131 A KMS device is abstracted and exposed as a set of planes, CRTCs, encoders
1132 and connectors. KMS drivers must thus create and initialize all those
1133 objects at load time after initializing mode setting.
1134 </para>
1135 <sect2>
1136 <title>CRTCs (struct <structname>drm_crtc</structname>)</title>
1137 <para>
1138 A CRTC is an abstraction representing a part of the chip that contains a
1139 pointer to a scanout buffer. Therefore, the number of CRTCs available
1140 determines how many independent scanout buffers can be active at any
1141 given time. The CRTC structure contains several fields to support this:
1142 a pointer to some video memory (abstracted as a frame buffer object), a
1143 display mode, and an (x, y) offset into the video memory to support
1144 panning or configurations where one piece of video memory spans multiple
1145 CRTCs.
1146 </para>
1147 <sect3>
1148 <title>CRTC Initialization</title>
1149 <para>
1150 A KMS device must create and register at least one struct
1151 <structname>drm_crtc</structname> instance. The instance is allocated
1152 and zeroed by the driver, possibly as part of a larger structure, and
1153 registered with a call to <function>drm_crtc_init</function> with a
1154 pointer to CRTC functions.
1155 </para>
1156 </sect3>
1157 <sect3>
1158 <title>CRTC Operations</title>
1159 <sect4>
1160 <title>Set Configuration</title>
1161 <synopsis>int (*set_config)(struct drm_mode_set *set);</synopsis>
1162 <para>
1163 Apply a new CRTC configuration to the device. The configuration
1164 specifies a CRTC, a frame buffer to scan out from, a (x,y) position in
1165 the frame buffer, a display mode and an array of connectors to drive
1166 with the CRTC if possible.
1167 </para>
1168 <para>
1169 If the frame buffer specified in the configuration is NULL, the driver
1170 must detach all encoders connected to the CRTC and all connectors
1171 attached to those encoders and disable them.
1172 </para>
1173 <para>
1174 This operation is called with the mode config lock held.
1175 </para>
1176 <note><para>
Daniel Vetteraa4cd912014-01-22 16:42:02 +01001177 Note that the drm core has no notion of restoring the mode setting
1178 state after resume, since all resume handling is in the full
1179 responsibility of the driver. The common mode setting helper library
1180 though provides a helper which can be used for this:
1181 <function>drm_helper_resume_force_mode</function>.
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001182 </para></note>
1183 </sect4>
1184 <sect4>
1185 <title>Page Flipping</title>
1186 <synopsis>int (*page_flip)(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1187 struct drm_pending_vblank_event *event);</synopsis>
1188 <para>
1189 Schedule a page flip to the given frame buffer for the CRTC. This
1190 operation is called with the mode config mutex held.
1191 </para>
1192 <para>
1193 Page flipping is a synchronization mechanism that replaces the frame
1194 buffer being scanned out by the CRTC with a new frame buffer during
1195 vertical blanking, avoiding tearing. When an application requests a page
1196 flip the DRM core verifies that the new frame buffer is large enough to
1197 be scanned out by the CRTC in the currently configured mode and then
1198 calls the CRTC <methodname>page_flip</methodname> operation with a
1199 pointer to the new frame buffer.
1200 </para>
1201 <para>
1202 The <methodname>page_flip</methodname> operation schedules a page flip.
Anatol Pomozovf884ab12013-05-08 16:56:16 -07001203 Once any pending rendering targeting the new frame buffer has
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001204 completed, the CRTC will be reprogrammed to display that frame buffer
1205 after the next vertical refresh. The operation must return immediately
1206 without waiting for rendering or page flip to complete and must block
1207 any new rendering to the frame buffer until the page flip completes.
1208 </para>
1209 <para>
Thierry Reding8cf1e982013-02-13 16:08:33 +01001210 If a page flip can be successfully scheduled the driver must set the
1211 <code>drm_crtc-&lt;fb</code> field to the new framebuffer pointed to
1212 by <code>fb</code>. This is important so that the reference counting
1213 on framebuffers stays balanced.
1214 </para>
1215 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001216 If a page flip is already pending, the
1217 <methodname>page_flip</methodname> operation must return
1218 -<errorname>EBUSY</errorname>.
1219 </para>
1220 <para>
1221 To synchronize page flip to vertical blanking the driver will likely
1222 need to enable vertical blanking interrupts. It should call
1223 <function>drm_vblank_get</function> for that purpose, and call
1224 <function>drm_vblank_put</function> after the page flip completes.
1225 </para>
1226 <para>
1227 If the application has requested to be notified when page flip completes
1228 the <methodname>page_flip</methodname> operation will be called with a
1229 non-NULL <parameter>event</parameter> argument pointing to a
1230 <structname>drm_pending_vblank_event</structname> instance. Upon page
Rob Clarkc6eefa12012-10-16 22:48:40 +00001231 flip completion the driver must call <methodname>drm_send_vblank_event</methodname>
1232 to fill in the event and send to wake up any waiting processes.
1233 This can be performed with
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001234 <programlisting><![CDATA[
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001235 spin_lock_irqsave(&dev->event_lock, flags);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001236 ...
1237 drm_send_vblank_event(dev, pipe, event);
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001238 spin_unlock_irqrestore(&dev->event_lock, flags);
1239 ]]></programlisting>
1240 </para>
1241 <note><para>
1242 FIXME: Could drivers that don't need to wait for rendering to complete
1243 just add the event to <literal>dev-&gt;vblank_event_list</literal> and
1244 let the DRM core handle everything, as for "normal" vertical blanking
1245 events?
1246 </para></note>
1247 <para>
1248 While waiting for the page flip to complete, the
1249 <literal>event-&gt;base.link</literal> list head can be used freely by
1250 the driver to store the pending event in a driver-specific list.
1251 </para>
1252 <para>
1253 If the file handle is closed before the event is signaled, drivers must
1254 take care to destroy the event in their
1255 <methodname>preclose</methodname> operation (and, if needed, call
1256 <function>drm_vblank_put</function>).
1257 </para>
1258 </sect4>
1259 <sect4>
1260 <title>Miscellaneous</title>
1261 <itemizedlist>
1262 <listitem>
Laurent Pinchart421cda32013-06-22 16:10:30 +02001263 <synopsis>void (*set_property)(struct drm_crtc *crtc,
1264 struct drm_property *property, uint64_t value);</synopsis>
1265 <para>
1266 Set the value of the given CRTC property to
1267 <parameter>value</parameter>. See <xref linkend="drm-kms-properties"/>
1268 for more information about properties.
1269 </para>
1270 </listitem>
1271 <listitem>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001272 <synopsis>void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
1273 uint32_t start, uint32_t size);</synopsis>
1274 <para>
1275 Apply a gamma table to the device. The operation is optional.
1276 </para>
1277 </listitem>
1278 <listitem>
1279 <synopsis>void (*destroy)(struct drm_crtc *crtc);</synopsis>
1280 <para>
1281 Destroy the CRTC when not needed anymore. See
1282 <xref linkend="drm-kms-init"/>.
1283 </para>
1284 </listitem>
1285 </itemizedlist>
1286 </sect4>
1287 </sect3>
1288 </sect2>
1289 <sect2>
1290 <title>Planes (struct <structname>drm_plane</structname>)</title>
1291 <para>
1292 A plane represents an image source that can be blended with or overlayed
1293 on top of a CRTC during the scanout process. Planes are associated with
1294 a frame buffer to crop a portion of the image memory (source) and
1295 optionally scale it to a destination size. The result is then blended
1296 with or overlayed on top of a CRTC.
1297 </para>
1298 <sect3>
1299 <title>Plane Initialization</title>
1300 <para>
1301 Planes are optional. To create a plane, a KMS drivers allocates and
1302 zeroes an instances of struct <structname>drm_plane</structname>
1303 (possibly as part of a larger structure) and registers it with a call
1304 to <function>drm_plane_init</function>. The function takes a bitmask
1305 of the CRTCs that can be associated with the plane, a pointer to the
1306 plane functions and a list of format supported formats.
1307 </para>
1308 </sect3>
1309 <sect3>
1310 <title>Plane Operations</title>
1311 <itemizedlist>
1312 <listitem>
1313 <synopsis>int (*update_plane)(struct drm_plane *plane, struct drm_crtc *crtc,
1314 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
1315 unsigned int crtc_w, unsigned int crtc_h,
1316 uint32_t src_x, uint32_t src_y,
1317 uint32_t src_w, uint32_t src_h);</synopsis>
1318 <para>
1319 Enable and configure the plane to use the given CRTC and frame buffer.
1320 </para>
1321 <para>
1322 The source rectangle in frame buffer memory coordinates is given by
1323 the <parameter>src_x</parameter>, <parameter>src_y</parameter>,
1324 <parameter>src_w</parameter> and <parameter>src_h</parameter>
1325 parameters (as 16.16 fixed point values). Devices that don't support
1326 subpixel plane coordinates can ignore the fractional part.
1327 </para>
1328 <para>
1329 The destination rectangle in CRTC coordinates is given by the
1330 <parameter>crtc_x</parameter>, <parameter>crtc_y</parameter>,
1331 <parameter>crtc_w</parameter> and <parameter>crtc_h</parameter>
1332 parameters (as integer values). Devices scale the source rectangle to
1333 the destination rectangle. If scaling is not supported, and the source
1334 rectangle size doesn't match the destination rectangle size, the
1335 driver must return a -<errorname>EINVAL</errorname> error.
1336 </para>
1337 </listitem>
1338 <listitem>
1339 <synopsis>int (*disable_plane)(struct drm_plane *plane);</synopsis>
1340 <para>
1341 Disable the plane. The DRM core calls this method in response to a
1342 DRM_IOCTL_MODE_SETPLANE ioctl call with the frame buffer ID set to 0.
1343 Disabled planes must not be processed by the CRTC.
1344 </para>
1345 </listitem>
1346 <listitem>
1347 <synopsis>void (*destroy)(struct drm_plane *plane);</synopsis>
1348 <para>
1349 Destroy the plane when not needed anymore. See
1350 <xref linkend="drm-kms-init"/>.
1351 </para>
1352 </listitem>
1353 </itemizedlist>
1354 </sect3>
1355 </sect2>
1356 <sect2>
1357 <title>Encoders (struct <structname>drm_encoder</structname>)</title>
1358 <para>
1359 An encoder takes pixel data from a CRTC and converts it to a format
1360 suitable for any attached connectors. On some devices, it may be
1361 possible to have a CRTC send data to more than one encoder. In that
1362 case, both encoders would receive data from the same scanout buffer,
1363 resulting in a "cloned" display configuration across the connectors
1364 attached to each encoder.
1365 </para>
1366 <sect3>
1367 <title>Encoder Initialization</title>
1368 <para>
1369 As for CRTCs, a KMS driver must create, initialize and register at
1370 least one struct <structname>drm_encoder</structname> instance. The
1371 instance is allocated and zeroed by the driver, possibly as part of a
1372 larger structure.
1373 </para>
1374 <para>
1375 Drivers must initialize the struct <structname>drm_encoder</structname>
1376 <structfield>possible_crtcs</structfield> and
1377 <structfield>possible_clones</structfield> fields before registering the
1378 encoder. Both fields are bitmasks of respectively the CRTCs that the
1379 encoder can be connected to, and sibling encoders candidate for cloning.
1380 </para>
1381 <para>
1382 After being initialized, the encoder must be registered with a call to
1383 <function>drm_encoder_init</function>. The function takes a pointer to
1384 the encoder functions and an encoder type. Supported types are
1385 <itemizedlist>
1386 <listitem>
1387 DRM_MODE_ENCODER_DAC for VGA and analog on DVI-I/DVI-A
1388 </listitem>
1389 <listitem>
1390 DRM_MODE_ENCODER_TMDS for DVI, HDMI and (embedded) DisplayPort
1391 </listitem>
1392 <listitem>
1393 DRM_MODE_ENCODER_LVDS for display panels
1394 </listitem>
1395 <listitem>
1396 DRM_MODE_ENCODER_TVDAC for TV output (Composite, S-Video, Component,
1397 SCART)
1398 </listitem>
1399 <listitem>
1400 DRM_MODE_ENCODER_VIRTUAL for virtual machine displays
1401 </listitem>
1402 </itemizedlist>
1403 </para>
1404 <para>
1405 Encoders must be attached to a CRTC to be used. DRM drivers leave
1406 encoders unattached at initialization time. Applications (or the fbdev
1407 compatibility layer when implemented) are responsible for attaching the
1408 encoders they want to use to a CRTC.
1409 </para>
1410 </sect3>
1411 <sect3>
1412 <title>Encoder Operations</title>
1413 <itemizedlist>
1414 <listitem>
1415 <synopsis>void (*destroy)(struct drm_encoder *encoder);</synopsis>
1416 <para>
1417 Called to destroy the encoder when not needed anymore. See
1418 <xref linkend="drm-kms-init"/>.
1419 </para>
1420 </listitem>
Laurent Pinchart421cda32013-06-22 16:10:30 +02001421 <listitem>
1422 <synopsis>void (*set_property)(struct drm_plane *plane,
1423 struct drm_property *property, uint64_t value);</synopsis>
1424 <para>
1425 Set the value of the given plane property to
1426 <parameter>value</parameter>. See <xref linkend="drm-kms-properties"/>
1427 for more information about properties.
1428 </para>
1429 </listitem>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001430 </itemizedlist>
1431 </sect3>
1432 </sect2>
1433 <sect2>
1434 <title>Connectors (struct <structname>drm_connector</structname>)</title>
1435 <para>
1436 A connector is the final destination for pixel data on a device, and
1437 usually connects directly to an external display device like a monitor
1438 or laptop panel. A connector can only be attached to one encoder at a
1439 time. The connector is also the structure where information about the
1440 attached display is kept, so it contains fields for display data, EDID
1441 data, DPMS &amp; connection status, and information about modes
1442 supported on the attached displays.
1443 </para>
1444 <sect3>
1445 <title>Connector Initialization</title>
1446 <para>
1447 Finally a KMS driver must create, initialize, register and attach at
1448 least one struct <structname>drm_connector</structname> instance. The
1449 instance is created as other KMS objects and initialized by setting the
1450 following fields.
1451 </para>
1452 <variablelist>
1453 <varlistentry>
1454 <term><structfield>interlace_allowed</structfield></term>
1455 <listitem><para>
1456 Whether the connector can handle interlaced modes.
1457 </para></listitem>
1458 </varlistentry>
1459 <varlistentry>
1460 <term><structfield>doublescan_allowed</structfield></term>
1461 <listitem><para>
1462 Whether the connector can handle doublescan.
1463 </para></listitem>
1464 </varlistentry>
1465 <varlistentry>
1466 <term><structfield>display_info
1467 </structfield></term>
1468 <listitem><para>
1469 Display information is filled from EDID information when a display
1470 is detected. For non hot-pluggable displays such as flat panels in
1471 embedded systems, the driver should initialize the
1472 <structfield>display_info</structfield>.<structfield>width_mm</structfield>
1473 and
1474 <structfield>display_info</structfield>.<structfield>height_mm</structfield>
1475 fields with the physical size of the display.
1476 </para></listitem>
1477 </varlistentry>
1478 <varlistentry>
1479 <term id="drm-kms-connector-polled"><structfield>polled</structfield></term>
1480 <listitem><para>
1481 Connector polling mode, a combination of
1482 <variablelist>
1483 <varlistentry>
1484 <term>DRM_CONNECTOR_POLL_HPD</term>
1485 <listitem><para>
1486 The connector generates hotplug events and doesn't need to be
1487 periodically polled. The CONNECT and DISCONNECT flags must not
1488 be set together with the HPD flag.
1489 </para></listitem>
1490 </varlistentry>
1491 <varlistentry>
1492 <term>DRM_CONNECTOR_POLL_CONNECT</term>
1493 <listitem><para>
1494 Periodically poll the connector for connection.
1495 </para></listitem>
1496 </varlistentry>
1497 <varlistentry>
1498 <term>DRM_CONNECTOR_POLL_DISCONNECT</term>
1499 <listitem><para>
1500 Periodically poll the connector for disconnection.
1501 </para></listitem>
1502 </varlistentry>
1503 </variablelist>
1504 Set to 0 for connectors that don't support connection status
1505 discovery.
1506 </para></listitem>
1507 </varlistentry>
1508 </variablelist>
1509 <para>
1510 The connector is then registered with a call to
1511 <function>drm_connector_init</function> with a pointer to the connector
1512 functions and a connector type, and exposed through sysfs with a call to
1513 <function>drm_sysfs_connector_add</function>.
1514 </para>
1515 <para>
1516 Supported connector types are
1517 <itemizedlist>
1518 <listitem>DRM_MODE_CONNECTOR_VGA</listitem>
1519 <listitem>DRM_MODE_CONNECTOR_DVII</listitem>
1520 <listitem>DRM_MODE_CONNECTOR_DVID</listitem>
1521 <listitem>DRM_MODE_CONNECTOR_DVIA</listitem>
1522 <listitem>DRM_MODE_CONNECTOR_Composite</listitem>
1523 <listitem>DRM_MODE_CONNECTOR_SVIDEO</listitem>
1524 <listitem>DRM_MODE_CONNECTOR_LVDS</listitem>
1525 <listitem>DRM_MODE_CONNECTOR_Component</listitem>
1526 <listitem>DRM_MODE_CONNECTOR_9PinDIN</listitem>
1527 <listitem>DRM_MODE_CONNECTOR_DisplayPort</listitem>
1528 <listitem>DRM_MODE_CONNECTOR_HDMIA</listitem>
1529 <listitem>DRM_MODE_CONNECTOR_HDMIB</listitem>
1530 <listitem>DRM_MODE_CONNECTOR_TV</listitem>
1531 <listitem>DRM_MODE_CONNECTOR_eDP</listitem>
1532 <listitem>DRM_MODE_CONNECTOR_VIRTUAL</listitem>
1533 </itemizedlist>
1534 </para>
1535 <para>
1536 Connectors must be attached to an encoder to be used. For devices that
1537 map connectors to encoders 1:1, the connector should be attached at
1538 initialization time with a call to
1539 <function>drm_mode_connector_attach_encoder</function>. The driver must
1540 also set the <structname>drm_connector</structname>
1541 <structfield>encoder</structfield> field to point to the attached
1542 encoder.
1543 </para>
1544 <para>
1545 Finally, drivers must initialize the connectors state change detection
1546 with a call to <function>drm_kms_helper_poll_init</function>. If at
1547 least one connector is pollable but can't generate hotplug interrupts
1548 (indicated by the DRM_CONNECTOR_POLL_CONNECT and
1549 DRM_CONNECTOR_POLL_DISCONNECT connector flags), a delayed work will
1550 automatically be queued to periodically poll for changes. Connectors
1551 that can generate hotplug interrupts must be marked with the
1552 DRM_CONNECTOR_POLL_HPD flag instead, and their interrupt handler must
1553 call <function>drm_helper_hpd_irq_event</function>. The function will
1554 queue a delayed work to check the state of all connectors, but no
1555 periodic polling will be done.
1556 </para>
1557 </sect3>
1558 <sect3>
1559 <title>Connector Operations</title>
1560 <note><para>
1561 Unless otherwise state, all operations are mandatory.
1562 </para></note>
1563 <sect4>
1564 <title>DPMS</title>
1565 <synopsis>void (*dpms)(struct drm_connector *connector, int mode);</synopsis>
1566 <para>
1567 The DPMS operation sets the power state of a connector. The mode
1568 argument is one of
1569 <itemizedlist>
1570 <listitem><para>DRM_MODE_DPMS_ON</para></listitem>
1571 <listitem><para>DRM_MODE_DPMS_STANDBY</para></listitem>
1572 <listitem><para>DRM_MODE_DPMS_SUSPEND</para></listitem>
1573 <listitem><para>DRM_MODE_DPMS_OFF</para></listitem>
1574 </itemizedlist>
1575 </para>
1576 <para>
1577 In all but DPMS_ON mode the encoder to which the connector is attached
1578 should put the display in low-power mode by driving its signals
1579 appropriately. If more than one connector is attached to the encoder
1580 care should be taken not to change the power state of other displays as
1581 a side effect. Low-power mode should be propagated to the encoders and
1582 CRTCs when all related connectors are put in low-power mode.
1583 </para>
1584 </sect4>
1585 <sect4>
1586 <title>Modes</title>
1587 <synopsis>int (*fill_modes)(struct drm_connector *connector, uint32_t max_width,
1588 uint32_t max_height);</synopsis>
1589 <para>
1590 Fill the mode list with all supported modes for the connector. If the
1591 <parameter>max_width</parameter> and <parameter>max_height</parameter>
1592 arguments are non-zero, the implementation must ignore all modes wider
1593 than <parameter>max_width</parameter> or higher than
1594 <parameter>max_height</parameter>.
1595 </para>
1596 <para>
1597 The connector must also fill in this operation its
1598 <structfield>display_info</structfield>
1599 <structfield>width_mm</structfield> and
1600 <structfield>height_mm</structfield> fields with the connected display
1601 physical size in millimeters. The fields should be set to 0 if the value
1602 isn't known or is not applicable (for instance for projector devices).
1603 </para>
1604 </sect4>
1605 <sect4>
1606 <title>Connection Status</title>
1607 <para>
1608 The connection status is updated through polling or hotplug events when
1609 supported (see <xref linkend="drm-kms-connector-polled"/>). The status
1610 value is reported to userspace through ioctls and must not be used
1611 inside the driver, as it only gets initialized by a call to
1612 <function>drm_mode_getconnector</function> from userspace.
1613 </para>
1614 <synopsis>enum drm_connector_status (*detect)(struct drm_connector *connector,
1615 bool force);</synopsis>
1616 <para>
1617 Check to see if anything is attached to the connector. The
1618 <parameter>force</parameter> parameter is set to false whilst polling or
1619 to true when checking the connector due to user request.
1620 <parameter>force</parameter> can be used by the driver to avoid
1621 expensive, destructive operations during automated probing.
1622 </para>
1623 <para>
1624 Return connector_status_connected if something is connected to the
1625 connector, connector_status_disconnected if nothing is connected and
1626 connector_status_unknown if the connection state isn't known.
1627 </para>
1628 <para>
1629 Drivers should only return connector_status_connected if the connection
1630 status has really been probed as connected. Connectors that can't detect
1631 the connection status, or failed connection status probes, should return
1632 connector_status_unknown.
1633 </para>
1634 </sect4>
1635 <sect4>
1636 <title>Miscellaneous</title>
1637 <itemizedlist>
1638 <listitem>
Laurent Pinchart421cda32013-06-22 16:10:30 +02001639 <synopsis>void (*set_property)(struct drm_connector *connector,
1640 struct drm_property *property, uint64_t value);</synopsis>
1641 <para>
1642 Set the value of the given connector property to
1643 <parameter>value</parameter>. See <xref linkend="drm-kms-properties"/>
1644 for more information about properties.
1645 </para>
1646 </listitem>
1647 <listitem>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001648 <synopsis>void (*destroy)(struct drm_connector *connector);</synopsis>
1649 <para>
1650 Destroy the connector when not needed anymore. See
1651 <xref linkend="drm-kms-init"/>.
1652 </para>
1653 </listitem>
1654 </itemizedlist>
1655 </sect4>
1656 </sect3>
1657 </sect2>
1658 <sect2>
1659 <title>Cleanup</title>
1660 <para>
1661 The DRM core manages its objects' lifetime. When an object is not needed
1662 anymore the core calls its destroy function, which must clean up and
1663 free every resource allocated for the object. Every
1664 <function>drm_*_init</function> call must be matched with a
1665 corresponding <function>drm_*_cleanup</function> call to cleanup CRTCs
1666 (<function>drm_crtc_cleanup</function>), planes
1667 (<function>drm_plane_cleanup</function>), encoders
1668 (<function>drm_encoder_cleanup</function>) and connectors
1669 (<function>drm_connector_cleanup</function>). Furthermore, connectors
1670 that have been added to sysfs must be removed by a call to
1671 <function>drm_sysfs_connector_remove</function> before calling
1672 <function>drm_connector_cleanup</function>.
1673 </para>
1674 <para>
1675 Connectors state change detection must be cleanup up with a call to
1676 <function>drm_kms_helper_poll_fini</function>.
1677 </para>
1678 </sect2>
1679 <sect2>
1680 <title>Output discovery and initialization example</title>
1681 <programlisting><![CDATA[
Jesse Barnes2d2ef822009-10-26 13:06:31 -07001682void intel_crt_init(struct drm_device *dev)
1683{
1684 struct drm_connector *connector;
1685 struct intel_output *intel_output;
1686
1687 intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
1688 if (!intel_output)
1689 return;
1690
1691 connector = &intel_output->base;
1692 drm_connector_init(dev, &intel_output->base,
1693 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1694
1695 drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
1696 DRM_MODE_ENCODER_DAC);
1697
1698 drm_mode_connector_attach_encoder(&intel_output->base,
1699 &intel_output->enc);
1700
1701 /* Set up the DDC bus. */
1702 intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
1703 if (!intel_output->ddc_bus) {
1704 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
1705 "failed.\n");
1706 return;
1707 }
1708
1709 intel_output->type = INTEL_OUTPUT_ANALOG;
1710 connector->interlace_allowed = 0;
1711 connector->doublescan_allowed = 0;
1712
1713 drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
1714 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
1715
1716 drm_sysfs_connector_add(connector);
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001717}]]></programlisting>
1718 <para>
1719 In the example above (taken from the i915 driver), a CRTC, connector and
1720 encoder combination is created. A device-specific i2c bus is also
1721 created for fetching EDID data and performing monitor detection. Once
1722 the process is complete, the new connector is registered with sysfs to
1723 make its properties available to applications.
1724 </para>
1725 </sect2>
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001726 <sect2>
1727 <title>KMS API Functions</title>
1728!Edrivers/gpu/drm/drm_crtc.c
1729 </sect2>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001730 </sect1>
1731
Daniel Vettere4949f22012-11-01 14:45:15 +01001732 <!-- Internals: kms helper functions -->
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001733
1734 <sect1>
Daniel Vettere4949f22012-11-01 14:45:15 +01001735 <title>Mode Setting Helper Functions</title>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001736 <para>
1737 The CRTC, encoder and connector functions provided by the drivers
1738 implement the DRM API. They're called by the DRM core and ioctl handlers
1739 to handle device state changes and configuration request. As implementing
1740 those functions often requires logic not specific to drivers, mid-layer
1741 helper functions are available to avoid duplicating boilerplate code.
1742 </para>
1743 <para>
1744 The DRM core contains one mid-layer implementation. The mid-layer provides
1745 implementations of several CRTC, encoder and connector functions (called
1746 from the top of the mid-layer) that pre-process requests and call
1747 lower-level functions provided by the driver (at the bottom of the
1748 mid-layer). For instance, the
1749 <function>drm_crtc_helper_set_config</function> function can be used to
1750 fill the struct <structname>drm_crtc_funcs</structname>
1751 <structfield>set_config</structfield> field. When called, it will split
1752 the <methodname>set_config</methodname> operation in smaller, simpler
1753 operations and call the driver to handle them.
1754 </para>
1755 <para>
1756 To use the mid-layer, drivers call <function>drm_crtc_helper_add</function>,
1757 <function>drm_encoder_helper_add</function> and
1758 <function>drm_connector_helper_add</function> functions to install their
1759 mid-layer bottom operations handlers, and fill the
1760 <structname>drm_crtc_funcs</structname>,
1761 <structname>drm_encoder_funcs</structname> and
1762 <structname>drm_connector_funcs</structname> structures with pointers to
1763 the mid-layer top API functions. Installing the mid-layer bottom operation
1764 handlers is best done right after registering the corresponding KMS object.
1765 </para>
1766 <para>
1767 The mid-layer is not split between CRTC, encoder and connector operations.
1768 To use it, a driver must provide bottom functions for all of the three KMS
1769 entities.
1770 </para>
1771 <sect2>
1772 <title>Helper Functions</title>
1773 <itemizedlist>
1774 <listitem>
1775 <synopsis>int drm_crtc_helper_set_config(struct drm_mode_set *set);</synopsis>
1776 <para>
1777 The <function>drm_crtc_helper_set_config</function> helper function
1778 is a CRTC <methodname>set_config</methodname> implementation. It
1779 first tries to locate the best encoder for each connector by calling
1780 the connector <methodname>best_encoder</methodname> helper
1781 operation.
1782 </para>
1783 <para>
1784 After locating the appropriate encoders, the helper function will
1785 call the <methodname>mode_fixup</methodname> encoder and CRTC helper
1786 operations to adjust the requested mode, or reject it completely in
1787 which case an error will be returned to the application. If the new
1788 configuration after mode adjustment is identical to the current
1789 configuration the helper function will return without performing any
1790 other operation.
1791 </para>
1792 <para>
1793 If the adjusted mode is identical to the current mode but changes to
1794 the frame buffer need to be applied, the
1795 <function>drm_crtc_helper_set_config</function> function will call
1796 the CRTC <methodname>mode_set_base</methodname> helper operation. If
1797 the adjusted mode differs from the current mode, or if the
1798 <methodname>mode_set_base</methodname> helper operation is not
1799 provided, the helper function performs a full mode set sequence by
1800 calling the <methodname>prepare</methodname>,
1801 <methodname>mode_set</methodname> and
1802 <methodname>commit</methodname> CRTC and encoder helper operations,
1803 in that order.
1804 </para>
1805 </listitem>
1806 <listitem>
1807 <synopsis>void drm_helper_connector_dpms(struct drm_connector *connector, int mode);</synopsis>
1808 <para>
1809 The <function>drm_helper_connector_dpms</function> helper function
1810 is a connector <methodname>dpms</methodname> implementation that
1811 tracks power state of connectors. To use the function, drivers must
1812 provide <methodname>dpms</methodname> helper operations for CRTCs
1813 and encoders to apply the DPMS state to the device.
1814 </para>
1815 <para>
1816 The mid-layer doesn't track the power state of CRTCs and encoders.
1817 The <methodname>dpms</methodname> helper operations can thus be
1818 called with a mode identical to the currently active mode.
1819 </para>
1820 </listitem>
1821 <listitem>
1822 <synopsis>int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
1823 uint32_t maxX, uint32_t maxY);</synopsis>
1824 <para>
1825 The <function>drm_helper_probe_single_connector_modes</function> helper
1826 function is a connector <methodname>fill_modes</methodname>
1827 implementation that updates the connection status for the connector
1828 and then retrieves a list of modes by calling the connector
1829 <methodname>get_modes</methodname> helper operation.
1830 </para>
1831 <para>
1832 The function filters out modes larger than
1833 <parameter>max_width</parameter> and <parameter>max_height</parameter>
1834 if specified. It then calls the connector
1835 <methodname>mode_valid</methodname> helper operation for each mode in
1836 the probed list to check whether the mode is valid for the connector.
1837 </para>
1838 </listitem>
1839 </itemizedlist>
1840 </sect2>
1841 <sect2>
1842 <title>CRTC Helper Operations</title>
1843 <itemizedlist>
1844 <listitem id="drm-helper-crtc-mode-fixup">
1845 <synopsis>bool (*mode_fixup)(struct drm_crtc *crtc,
1846 const struct drm_display_mode *mode,
1847 struct drm_display_mode *adjusted_mode);</synopsis>
1848 <para>
1849 Let CRTCs adjust the requested mode or reject it completely. This
1850 operation returns true if the mode is accepted (possibly after being
1851 adjusted) or false if it is rejected.
1852 </para>
1853 <para>
1854 The <methodname>mode_fixup</methodname> operation should reject the
1855 mode if it can't reasonably use it. The definition of "reasonable"
1856 is currently fuzzy in this context. One possible behaviour would be
1857 to set the adjusted mode to the panel timings when a fixed-mode
1858 panel is used with hardware capable of scaling. Another behaviour
1859 would be to accept any input mode and adjust it to the closest mode
1860 supported by the hardware (FIXME: This needs to be clarified).
1861 </para>
1862 </listitem>
1863 <listitem>
1864 <synopsis>int (*mode_set_base)(struct drm_crtc *crtc, int x, int y,
1865 struct drm_framebuffer *old_fb)</synopsis>
1866 <para>
1867 Move the CRTC on the current frame buffer (stored in
1868 <literal>crtc-&gt;fb</literal>) to position (x,y). Any of the frame
1869 buffer, x position or y position may have been modified.
1870 </para>
1871 <para>
1872 This helper operation is optional. If not provided, the
1873 <function>drm_crtc_helper_set_config</function> function will fall
1874 back to the <methodname>mode_set</methodname> helper operation.
1875 </para>
1876 <note><para>
1877 FIXME: Why are x and y passed as arguments, as they can be accessed
1878 through <literal>crtc-&gt;x</literal> and
1879 <literal>crtc-&gt;y</literal>?
1880 </para></note>
1881 </listitem>
1882 <listitem>
1883 <synopsis>void (*prepare)(struct drm_crtc *crtc);</synopsis>
1884 <para>
1885 Prepare the CRTC for mode setting. This operation is called after
1886 validating the requested mode. Drivers use it to perform
1887 device-specific operations required before setting the new mode.
1888 </para>
1889 </listitem>
1890 <listitem>
1891 <synopsis>int (*mode_set)(struct drm_crtc *crtc, struct drm_display_mode *mode,
1892 struct drm_display_mode *adjusted_mode, int x, int y,
1893 struct drm_framebuffer *old_fb);</synopsis>
1894 <para>
1895 Set a new mode, position and frame buffer. Depending on the device
1896 requirements, the mode can be stored internally by the driver and
1897 applied in the <methodname>commit</methodname> operation, or
1898 programmed to the hardware immediately.
1899 </para>
1900 <para>
1901 The <methodname>mode_set</methodname> operation returns 0 on success
1902 or a negative error code if an error occurs.
1903 </para>
1904 </listitem>
1905 <listitem>
1906 <synopsis>void (*commit)(struct drm_crtc *crtc);</synopsis>
1907 <para>
1908 Commit a mode. This operation is called after setting the new mode.
1909 Upon return the device must use the new mode and be fully
1910 operational.
1911 </para>
1912 </listitem>
1913 </itemizedlist>
1914 </sect2>
1915 <sect2>
1916 <title>Encoder Helper Operations</title>
1917 <itemizedlist>
1918 <listitem>
1919 <synopsis>bool (*mode_fixup)(struct drm_encoder *encoder,
1920 const struct drm_display_mode *mode,
1921 struct drm_display_mode *adjusted_mode);</synopsis>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02001922 <para>
1923 Let encoders adjust the requested mode or reject it completely. This
1924 operation returns true if the mode is accepted (possibly after being
1925 adjusted) or false if it is rejected. See the
1926 <link linkend="drm-helper-crtc-mode-fixup">mode_fixup CRTC helper
1927 operation</link> for an explanation of the allowed adjustments.
1928 </para>
1929 </listitem>
1930 <listitem>
1931 <synopsis>void (*prepare)(struct drm_encoder *encoder);</synopsis>
1932 <para>
1933 Prepare the encoder for mode setting. This operation is called after
1934 validating the requested mode. Drivers use it to perform
1935 device-specific operations required before setting the new mode.
1936 </para>
1937 </listitem>
1938 <listitem>
1939 <synopsis>void (*mode_set)(struct drm_encoder *encoder,
1940 struct drm_display_mode *mode,
1941 struct drm_display_mode *adjusted_mode);</synopsis>
1942 <para>
1943 Set a new mode. Depending on the device requirements, the mode can
1944 be stored internally by the driver and applied in the
1945 <methodname>commit</methodname> operation, or programmed to the
1946 hardware immediately.
1947 </para>
1948 </listitem>
1949 <listitem>
1950 <synopsis>void (*commit)(struct drm_encoder *encoder);</synopsis>
1951 <para>
1952 Commit a mode. This operation is called after setting the new mode.
1953 Upon return the device must use the new mode and be fully
1954 operational.
1955 </para>
1956 </listitem>
1957 </itemizedlist>
1958 </sect2>
1959 <sect2>
1960 <title>Connector Helper Operations</title>
1961 <itemizedlist>
1962 <listitem>
1963 <synopsis>struct drm_encoder *(*best_encoder)(struct drm_connector *connector);</synopsis>
1964 <para>
1965 Return a pointer to the best encoder for the connecter. Device that
1966 map connectors to encoders 1:1 simply return the pointer to the
1967 associated encoder. This operation is mandatory.
1968 </para>
1969 </listitem>
1970 <listitem>
1971 <synopsis>int (*get_modes)(struct drm_connector *connector);</synopsis>
1972 <para>
1973 Fill the connector's <structfield>probed_modes</structfield> list
1974 by parsing EDID data with <function>drm_add_edid_modes</function> or
1975 calling <function>drm_mode_probed_add</function> directly for every
1976 supported mode and return the number of modes it has detected. This
1977 operation is mandatory.
1978 </para>
1979 <para>
1980 When adding modes manually the driver creates each mode with a call to
1981 <function>drm_mode_create</function> and must fill the following fields.
1982 <itemizedlist>
1983 <listitem>
1984 <synopsis>__u32 type;</synopsis>
1985 <para>
1986 Mode type bitmask, a combination of
1987 <variablelist>
1988 <varlistentry>
1989 <term>DRM_MODE_TYPE_BUILTIN</term>
1990 <listitem><para>not used?</para></listitem>
1991 </varlistentry>
1992 <varlistentry>
1993 <term>DRM_MODE_TYPE_CLOCK_C</term>
1994 <listitem><para>not used?</para></listitem>
1995 </varlistentry>
1996 <varlistentry>
1997 <term>DRM_MODE_TYPE_CRTC_C</term>
1998 <listitem><para>not used?</para></listitem>
1999 </varlistentry>
2000 <varlistentry>
2001 <term>
2002 DRM_MODE_TYPE_PREFERRED - The preferred mode for the connector
2003 </term>
2004 <listitem>
2005 <para>not used?</para>
2006 </listitem>
2007 </varlistentry>
2008 <varlistentry>
2009 <term>DRM_MODE_TYPE_DEFAULT</term>
2010 <listitem><para>not used?</para></listitem>
2011 </varlistentry>
2012 <varlistentry>
2013 <term>DRM_MODE_TYPE_USERDEF</term>
2014 <listitem><para>not used?</para></listitem>
2015 </varlistentry>
2016 <varlistentry>
2017 <term>DRM_MODE_TYPE_DRIVER</term>
2018 <listitem>
2019 <para>
2020 The mode has been created by the driver (as opposed to
2021 to user-created modes).
2022 </para>
2023 </listitem>
2024 </varlistentry>
2025 </variablelist>
2026 Drivers must set the DRM_MODE_TYPE_DRIVER bit for all modes they
2027 create, and set the DRM_MODE_TYPE_PREFERRED bit for the preferred
2028 mode.
2029 </para>
2030 </listitem>
2031 <listitem>
2032 <synopsis>__u32 clock;</synopsis>
2033 <para>Pixel clock frequency in kHz unit</para>
2034 </listitem>
2035 <listitem>
2036 <synopsis>__u16 hdisplay, hsync_start, hsync_end, htotal;
2037 __u16 vdisplay, vsync_start, vsync_end, vtotal;</synopsis>
2038 <para>Horizontal and vertical timing information</para>
2039 <screen><![CDATA[
2040 Active Front Sync Back
2041 Region Porch Porch
2042 <-----------------------><----------------><-------------><-------------->
2043
2044 //////////////////////|
2045 ////////////////////// |
2046 ////////////////////// |.................. ................
2047 _______________
2048
2049 <----- [hv]display ----->
2050 <------------- [hv]sync_start ------------>
2051 <--------------------- [hv]sync_end --------------------->
2052 <-------------------------------- [hv]total ----------------------------->
2053]]></screen>
2054 </listitem>
2055 <listitem>
2056 <synopsis>__u16 hskew;
2057 __u16 vscan;</synopsis>
2058 <para>Unknown</para>
2059 </listitem>
2060 <listitem>
2061 <synopsis>__u32 flags;</synopsis>
2062 <para>
2063 Mode flags, a combination of
2064 <variablelist>
2065 <varlistentry>
2066 <term>DRM_MODE_FLAG_PHSYNC</term>
2067 <listitem><para>
2068 Horizontal sync is active high
2069 </para></listitem>
2070 </varlistentry>
2071 <varlistentry>
2072 <term>DRM_MODE_FLAG_NHSYNC</term>
2073 <listitem><para>
2074 Horizontal sync is active low
2075 </para></listitem>
2076 </varlistentry>
2077 <varlistentry>
2078 <term>DRM_MODE_FLAG_PVSYNC</term>
2079 <listitem><para>
2080 Vertical sync is active high
2081 </para></listitem>
2082 </varlistentry>
2083 <varlistentry>
2084 <term>DRM_MODE_FLAG_NVSYNC</term>
2085 <listitem><para>
2086 Vertical sync is active low
2087 </para></listitem>
2088 </varlistentry>
2089 <varlistentry>
2090 <term>DRM_MODE_FLAG_INTERLACE</term>
2091 <listitem><para>
2092 Mode is interlaced
2093 </para></listitem>
2094 </varlistentry>
2095 <varlistentry>
2096 <term>DRM_MODE_FLAG_DBLSCAN</term>
2097 <listitem><para>
2098 Mode uses doublescan
2099 </para></listitem>
2100 </varlistentry>
2101 <varlistentry>
2102 <term>DRM_MODE_FLAG_CSYNC</term>
2103 <listitem><para>
2104 Mode uses composite sync
2105 </para></listitem>
2106 </varlistentry>
2107 <varlistentry>
2108 <term>DRM_MODE_FLAG_PCSYNC</term>
2109 <listitem><para>
2110 Composite sync is active high
2111 </para></listitem>
2112 </varlistentry>
2113 <varlistentry>
2114 <term>DRM_MODE_FLAG_NCSYNC</term>
2115 <listitem><para>
2116 Composite sync is active low
2117 </para></listitem>
2118 </varlistentry>
2119 <varlistentry>
2120 <term>DRM_MODE_FLAG_HSKEW</term>
2121 <listitem><para>
2122 hskew provided (not used?)
2123 </para></listitem>
2124 </varlistentry>
2125 <varlistentry>
2126 <term>DRM_MODE_FLAG_BCAST</term>
2127 <listitem><para>
2128 not used?
2129 </para></listitem>
2130 </varlistentry>
2131 <varlistentry>
2132 <term>DRM_MODE_FLAG_PIXMUX</term>
2133 <listitem><para>
2134 not used?
2135 </para></listitem>
2136 </varlistentry>
2137 <varlistentry>
2138 <term>DRM_MODE_FLAG_DBLCLK</term>
2139 <listitem><para>
2140 not used?
2141 </para></listitem>
2142 </varlistentry>
2143 <varlistentry>
2144 <term>DRM_MODE_FLAG_CLKDIV2</term>
2145 <listitem><para>
2146 ?
2147 </para></listitem>
2148 </varlistentry>
2149 </variablelist>
2150 </para>
2151 <para>
2152 Note that modes marked with the INTERLACE or DBLSCAN flags will be
2153 filtered out by
2154 <function>drm_helper_probe_single_connector_modes</function> if
2155 the connector's <structfield>interlace_allowed</structfield> or
2156 <structfield>doublescan_allowed</structfield> field is set to 0.
2157 </para>
2158 </listitem>
2159 <listitem>
2160 <synopsis>char name[DRM_DISPLAY_MODE_LEN];</synopsis>
2161 <para>
2162 Mode name. The driver must call
2163 <function>drm_mode_set_name</function> to fill the mode name from
2164 <structfield>hdisplay</structfield>,
2165 <structfield>vdisplay</structfield> and interlace flag after
2166 filling the corresponding fields.
2167 </para>
2168 </listitem>
2169 </itemizedlist>
2170 </para>
2171 <para>
2172 The <structfield>vrefresh</structfield> value is computed by
2173 <function>drm_helper_probe_single_connector_modes</function>.
2174 </para>
2175 <para>
2176 When parsing EDID data, <function>drm_add_edid_modes</function> fill the
2177 connector <structfield>display_info</structfield>
2178 <structfield>width_mm</structfield> and
2179 <structfield>height_mm</structfield> fields. When creating modes
2180 manually the <methodname>get_modes</methodname> helper operation must
2181 set the <structfield>display_info</structfield>
2182 <structfield>width_mm</structfield> and
2183 <structfield>height_mm</structfield> fields if they haven't been set
Daniel Vetter065a5022014-01-21 12:01:41 +01002184 already (for instance at initialization time when a fixed-size panel is
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002185 attached to the connector). The mode <structfield>width_mm</structfield>
2186 and <structfield>height_mm</structfield> fields are only used internally
2187 during EDID parsing and should not be set when creating modes manually.
2188 </para>
2189 </listitem>
2190 <listitem>
2191 <synopsis>int (*mode_valid)(struct drm_connector *connector,
2192 struct drm_display_mode *mode);</synopsis>
2193 <para>
2194 Verify whether a mode is valid for the connector. Return MODE_OK for
2195 supported modes and one of the enum drm_mode_status values (MODE_*)
2196 for unsupported modes. This operation is mandatory.
2197 </para>
2198 <para>
2199 As the mode rejection reason is currently not used beside for
2200 immediately removing the unsupported mode, an implementation can
2201 return MODE_BAD regardless of the exact reason why the mode is not
2202 valid.
2203 </para>
2204 <note><para>
2205 Note that the <methodname>mode_valid</methodname> helper operation is
2206 only called for modes detected by the device, and
2207 <emphasis>not</emphasis> for modes set by the user through the CRTC
2208 <methodname>set_config</methodname> operation.
2209 </para></note>
2210 </listitem>
2211 </itemizedlist>
2212 </sect2>
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +01002213 <sect2>
2214 <title>Modeset Helper Functions Reference</title>
2215!Edrivers/gpu/drm/drm_crtc_helper.c
2216 </sect2>
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002217 <sect2>
2218 <title>fbdev Helper Functions Reference</title>
2219!Pdrivers/gpu/drm/drm_fb_helper.c fbdev helpers
2220!Edrivers/gpu/drm/drm_fb_helper.c
Daniel Vetter207fd322013-01-20 22:13:14 +01002221!Iinclude/drm/drm_fb_helper.h
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002222 </sect2>
Daniel Vetter28164fd2012-11-01 14:45:18 +01002223 <sect2>
2224 <title>Display Port Helper Functions Reference</title>
2225!Pdrivers/gpu/drm/drm_dp_helper.c dp helpers
2226!Iinclude/drm/drm_dp_helper.h
2227!Edrivers/gpu/drm/drm_dp_helper.c
2228 </sect2>
Thierry Reding5e308592013-01-14 09:00:31 +01002229 <sect2>
2230 <title>EDID Helper Functions Reference</title>
2231!Edrivers/gpu/drm/drm_edid.c
2232 </sect2>
Ville Syrjälä03973532013-05-08 17:16:45 +03002233 <sect2>
2234 <title>Rectangle Utilities Reference</title>
2235!Pinclude/drm/drm_rect.h rect utils
2236!Iinclude/drm/drm_rect.h
2237!Edrivers/gpu/drm/drm_rect.c
2238 </sect2>
David Herrmannfe3078f2013-07-24 21:06:15 +02002239 <sect2>
Rob Clarkcabaafc2013-08-07 14:41:54 -04002240 <title>Flip-work Helper Reference</title>
2241!Pinclude/drm/drm_flip_work.h flip utils
2242!Iinclude/drm/drm_flip_work.h
2243!Edrivers/gpu/drm/drm_flip_work.c
2244 </sect2>
Daniel Vetter2d123f42014-01-22 18:26:16 +01002245 <sect2>
2246 <title>HDMI Infoframes Helper Reference</title>
2247 <para>
2248 Strictly speaking this is not a DRM helper library but generally useable
2249 by any driver interfacing with HDMI outputs like v4l or alsa drivers.
2250 But it nicely fits into the overall topic of mode setting helper
2251 libraries and hence is also included here.
2252 </para>
2253!Iinclude/linux/hdmi.h
2254!Edrivers/video/hdmi.c
2255 </sect2>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002256 </sect1>
2257
Laurent Pinchart421cda32013-06-22 16:10:30 +02002258 <!-- Internals: kms properties -->
2259
2260 <sect1 id="drm-kms-properties">
2261 <title>KMS Properties</title>
2262 <para>
2263 Drivers may need to expose additional parameters to applications than
2264 those described in the previous sections. KMS supports attaching
2265 properties to CRTCs, connectors and planes and offers a userspace API to
2266 list, get and set the property values.
2267 </para>
2268 <para>
2269 Properties are identified by a name that uniquely defines the property
2270 purpose, and store an associated value. For all property types except blob
2271 properties the value is a 64-bit unsigned integer.
2272 </para>
2273 <para>
2274 KMS differentiates between properties and property instances. Drivers
2275 first create properties and then create and associate individual instances
2276 of those properties to objects. A property can be instantiated multiple
2277 times and associated with different objects. Values are stored in property
2278 instances, and all other property information are stored in the propery
2279 and shared between all instances of the property.
2280 </para>
2281 <para>
2282 Every property is created with a type that influences how the KMS core
2283 handles the property. Supported property types are
2284 <variablelist>
2285 <varlistentry>
2286 <term>DRM_MODE_PROP_RANGE</term>
2287 <listitem><para>Range properties report their minimum and maximum
2288 admissible values. The KMS core verifies that values set by
2289 application fit in that range.</para></listitem>
2290 </varlistentry>
2291 <varlistentry>
2292 <term>DRM_MODE_PROP_ENUM</term>
2293 <listitem><para>Enumerated properties take a numerical value that
2294 ranges from 0 to the number of enumerated values defined by the
2295 property minus one, and associate a free-formed string name to each
2296 value. Applications can retrieve the list of defined value-name pairs
2297 and use the numerical value to get and set property instance values.
2298 </para></listitem>
2299 </varlistentry>
2300 <varlistentry>
2301 <term>DRM_MODE_PROP_BITMASK</term>
2302 <listitem><para>Bitmask properties are enumeration properties that
2303 additionally restrict all enumerated values to the 0..63 range.
2304 Bitmask property instance values combine one or more of the
2305 enumerated bits defined by the property.</para></listitem>
2306 </varlistentry>
2307 <varlistentry>
2308 <term>DRM_MODE_PROP_BLOB</term>
2309 <listitem><para>Blob properties store a binary blob without any format
2310 restriction. The binary blobs are created as KMS standalone objects,
2311 and blob property instance values store the ID of their associated
2312 blob object.</para>
2313 <para>Blob properties are only used for the connector EDID property
2314 and cannot be created by drivers.</para></listitem>
2315 </varlistentry>
2316 </variablelist>
2317 </para>
2318 <para>
2319 To create a property drivers call one of the following functions depending
2320 on the property type. All property creation functions take property flags
2321 and name, as well as type-specific arguments.
2322 <itemizedlist>
2323 <listitem>
2324 <synopsis>struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2325 const char *name,
2326 uint64_t min, uint64_t max);</synopsis>
2327 <para>Create a range property with the given minimum and maximum
2328 values.</para>
2329 </listitem>
2330 <listitem>
2331 <synopsis>struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2332 const char *name,
2333 const struct drm_prop_enum_list *props,
2334 int num_values);</synopsis>
2335 <para>Create an enumerated property. The <parameter>props</parameter>
2336 argument points to an array of <parameter>num_values</parameter>
2337 value-name pairs.</para>
2338 </listitem>
2339 <listitem>
2340 <synopsis>struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2341 int flags, const char *name,
2342 const struct drm_prop_enum_list *props,
2343 int num_values);</synopsis>
2344 <para>Create a bitmask property. The <parameter>props</parameter>
2345 argument points to an array of <parameter>num_values</parameter>
2346 value-name pairs.</para>
2347 </listitem>
2348 </itemizedlist>
2349 </para>
2350 <para>
2351 Properties can additionally be created as immutable, in which case they
2352 will be read-only for applications but can be modified by the driver. To
2353 create an immutable property drivers must set the DRM_MODE_PROP_IMMUTABLE
2354 flag at property creation time.
2355 </para>
2356 <para>
2357 When no array of value-name pairs is readily available at property
2358 creation time for enumerated or range properties, drivers can create
2359 the property using the <function>drm_property_create</function> function
2360 and manually add enumeration value-name pairs by calling the
2361 <function>drm_property_add_enum</function> function. Care must be taken to
2362 properly specify the property type through the <parameter>flags</parameter>
2363 argument.
2364 </para>
2365 <para>
2366 After creating properties drivers can attach property instances to CRTC,
2367 connector and plane objects by calling the
2368 <function>drm_object_attach_property</function>. The function takes a
2369 pointer to the target object, a pointer to the previously created property
2370 and an initial instance value.
2371 </para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002372 </sect1>
2373
2374 <!-- Internals: vertical blanking -->
2375
2376 <sect1 id="drm-vertical-blank">
2377 <title>Vertical Blanking</title>
2378 <para>
2379 Vertical blanking plays a major role in graphics rendering. To achieve
2380 tear-free display, users must synchronize page flips and/or rendering to
2381 vertical blanking. The DRM API offers ioctls to perform page flips
2382 synchronized to vertical blanking and wait for vertical blanking.
2383 </para>
2384 <para>
2385 The DRM core handles most of the vertical blanking management logic, which
2386 involves filtering out spurious interrupts, keeping race-free blanking
2387 counters, coping with counter wrap-around and resets and keeping use
2388 counts. It relies on the driver to generate vertical blanking interrupts
2389 and optionally provide a hardware vertical blanking counter. Drivers must
2390 implement the following operations.
2391 </para>
2392 <itemizedlist>
2393 <listitem>
2394 <synopsis>int (*enable_vblank) (struct drm_device *dev, int crtc);
2395void (*disable_vblank) (struct drm_device *dev, int crtc);</synopsis>
2396 <para>
2397 Enable or disable vertical blanking interrupts for the given CRTC.
2398 </para>
2399 </listitem>
2400 <listitem>
2401 <synopsis>u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);</synopsis>
2402 <para>
2403 Retrieve the value of the vertical blanking counter for the given
2404 CRTC. If the hardware maintains a vertical blanking counter its value
2405 should be returned. Otherwise drivers can use the
2406 <function>drm_vblank_count</function> helper function to handle this
2407 operation.
2408 </para>
2409 </listitem>
2410 </itemizedlist>
2411 <para>
2412 Drivers must initialize the vertical blanking handling core with a call to
2413 <function>drm_vblank_init</function> in their
2414 <methodname>load</methodname> operation. The function will set the struct
2415 <structname>drm_device</structname>
2416 <structfield>vblank_disable_allowed</structfield> field to 0. This will
2417 keep vertical blanking interrupts enabled permanently until the first mode
2418 set operation, where <structfield>vblank_disable_allowed</structfield> is
2419 set to 1. The reason behind this is not clear. Drivers can set the field
2420 to 1 after <function>calling drm_vblank_init</function> to make vertical
2421 blanking interrupts dynamically managed from the beginning.
2422 </para>
2423 <para>
2424 Vertical blanking interrupts can be enabled by the DRM core or by drivers
2425 themselves (for instance to handle page flipping operations). The DRM core
2426 maintains a vertical blanking use count to ensure that the interrupts are
2427 not disabled while a user still needs them. To increment the use count,
2428 drivers call <function>drm_vblank_get</function>. Upon return vertical
2429 blanking interrupts are guaranteed to be enabled.
2430 </para>
2431 <para>
2432 To decrement the use count drivers call
2433 <function>drm_vblank_put</function>. Only when the use count drops to zero
2434 will the DRM core disable the vertical blanking interrupts after a delay
2435 by scheduling a timer. The delay is accessible through the vblankoffdelay
2436 module parameter or the <varname>drm_vblank_offdelay</varname> global
2437 variable and expressed in milliseconds. Its default value is 5000 ms.
2438 </para>
2439 <para>
2440 When a vertical blanking interrupt occurs drivers only need to call the
2441 <function>drm_handle_vblank</function> function to account for the
2442 interrupt.
2443 </para>
2444 <para>
2445 Resources allocated by <function>drm_vblank_init</function> must be freed
2446 with a call to <function>drm_vblank_cleanup</function> in the driver
2447 <methodname>unload</methodname> operation handler.
2448 </para>
2449 </sect1>
2450
2451 <!-- Internals: open/close, file operations and ioctls -->
2452
2453 <sect1>
2454 <title>Open/Close, File Operations and IOCTLs</title>
2455 <sect2>
2456 <title>Open and Close</title>
2457 <synopsis>int (*firstopen) (struct drm_device *);
2458void (*lastclose) (struct drm_device *);
2459int (*open) (struct drm_device *, struct drm_file *);
2460void (*preclose) (struct drm_device *, struct drm_file *);
2461void (*postclose) (struct drm_device *, struct drm_file *);</synopsis>
2462 <abstract>Open and close handlers. None of those methods are mandatory.
2463 </abstract>
2464 <para>
2465 The <methodname>firstopen</methodname> method is called by the DRM core
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +02002466 for legacy UMS (User Mode Setting) drivers only when an application
2467 opens a device that has no other opened file handle. UMS drivers can
2468 implement it to acquire device resources. KMS drivers can't use the
2469 method and must acquire resources in the <methodname>load</methodname>
2470 method instead.
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002471 </para>
2472 <para>
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +02002473 Similarly the <methodname>lastclose</methodname> method is called when
2474 the last application holding a file handle opened on the device closes
2475 it, for both UMS and KMS drivers. Additionally, the method is also
2476 called at module unload time or, for hot-pluggable devices, when the
2477 device is unplugged. The <methodname>firstopen</methodname> and
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002478 <methodname>lastclose</methodname> calls can thus be unbalanced.
2479 </para>
2480 <para>
2481 The <methodname>open</methodname> method is called every time the device
2482 is opened by an application. Drivers can allocate per-file private data
2483 in this method and store them in the struct
2484 <structname>drm_file</structname> <structfield>driver_priv</structfield>
2485 field. Note that the <methodname>open</methodname> method is called
2486 before <methodname>firstopen</methodname>.
2487 </para>
2488 <para>
2489 The close operation is split into <methodname>preclose</methodname> and
2490 <methodname>postclose</methodname> methods. Drivers must stop and
2491 cleanup all per-file operations in the <methodname>preclose</methodname>
2492 method. For instance pending vertical blanking and page flip events must
2493 be cancelled. No per-file operation is allowed on the file handle after
2494 returning from the <methodname>preclose</methodname> method.
2495 </para>
2496 <para>
2497 Finally the <methodname>postclose</methodname> method is called as the
2498 last step of the close operation, right before calling the
2499 <methodname>lastclose</methodname> method if no other open file handle
2500 exists for the device. Drivers that have allocated per-file private data
2501 in the <methodname>open</methodname> method should free it here.
2502 </para>
2503 <para>
2504 The <methodname>lastclose</methodname> method should restore CRTC and
2505 plane properties to default value, so that a subsequent open of the
Daniel Vetter7d14bb6b2013-08-08 15:41:15 +02002506 device will not inherit state from the previous user. It can also be
2507 used to execute delayed power switching state changes, e.g. in
2508 conjunction with the vga-switcheroo infrastructure. Beyond that KMS
2509 drivers should not do any further cleanup. Only legacy UMS drivers might
2510 need to clean up device state so that the vga console or an independent
2511 fbdev driver could take over.
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002512 </para>
2513 </sect2>
2514 <sect2>
2515 <title>File Operations</title>
2516 <synopsis>const struct file_operations *fops</synopsis>
2517 <abstract>File operations for the DRM device node.</abstract>
2518 <para>
2519 Drivers must define the file operations structure that forms the DRM
2520 userspace API entry point, even though most of those operations are
2521 implemented in the DRM core. The <methodname>open</methodname>,
2522 <methodname>release</methodname> and <methodname>ioctl</methodname>
2523 operations are handled by
2524 <programlisting>
2525 .owner = THIS_MODULE,
2526 .open = drm_open,
2527 .release = drm_release,
2528 .unlocked_ioctl = drm_ioctl,
2529 #ifdef CONFIG_COMPAT
2530 .compat_ioctl = drm_compat_ioctl,
2531 #endif
2532 </programlisting>
2533 </para>
2534 <para>
2535 Drivers that implement private ioctls that requires 32/64bit
2536 compatibility support must provide their own
2537 <methodname>compat_ioctl</methodname> handler that processes private
2538 ioctls and calls <function>drm_compat_ioctl</function> for core ioctls.
2539 </para>
2540 <para>
2541 The <methodname>read</methodname> and <methodname>poll</methodname>
2542 operations provide support for reading DRM events and polling them. They
2543 are implemented by
2544 <programlisting>
2545 .poll = drm_poll,
2546 .read = drm_read,
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002547 .llseek = no_llseek,
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002548 </programlisting>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002549 </para>
2550 <para>
2551 The memory mapping implementation varies depending on how the driver
2552 manages memory. Pre-GEM drivers will use <function>drm_mmap</function>,
2553 while GEM-aware drivers will use <function>drm_gem_mmap</function>. See
2554 <xref linkend="drm-gem"/>.
2555 <programlisting>
2556 .mmap = drm_gem_mmap,
2557 </programlisting>
2558 </para>
2559 <para>
2560 No other file operation is supported by the DRM API.
2561 </para>
2562 </sect2>
2563 <sect2>
2564 <title>IOCTLs</title>
2565 <synopsis>struct drm_ioctl_desc *ioctls;
2566int num_ioctls;</synopsis>
2567 <abstract>Driver-specific ioctls descriptors table.</abstract>
2568 <para>
2569 Driver-specific ioctls numbers start at DRM_COMMAND_BASE. The ioctls
2570 descriptors table is indexed by the ioctl number offset from the base
2571 value. Drivers can use the DRM_IOCTL_DEF_DRV() macro to initialize the
2572 table entries.
2573 </para>
2574 <para>
2575 <programlisting>DRM_IOCTL_DEF_DRV(ioctl, func, flags)</programlisting>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002576 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002577 <parameter>ioctl</parameter> is the ioctl name. Drivers must define
2578 the DRM_##ioctl and DRM_IOCTL_##ioctl macros to the ioctl number
2579 offset from DRM_COMMAND_BASE and the ioctl number respectively. The
2580 first macro is private to the device while the second must be exposed
2581 to userspace in a public header.
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002582 </para>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002583 <para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002584 <parameter>func</parameter> is a pointer to the ioctl handler function
2585 compatible with the <type>drm_ioctl_t</type> type.
2586 <programlisting>typedef int drm_ioctl_t(struct drm_device *dev, void *data,
2587 struct drm_file *file_priv);</programlisting>
2588 </para>
2589 <para>
2590 <parameter>flags</parameter> is a bitmask combination of the following
2591 values. It restricts how the ioctl is allowed to be called.
Michael Witten65ffef52011-08-25 20:55:58 +00002592 <itemizedlist>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002593 <listitem><para>
2594 DRM_AUTH - Only authenticated callers allowed
2595 </para></listitem>
2596 <listitem><para>
2597 DRM_MASTER - The ioctl can only be called on the master file
2598 handle
2599 </para></listitem>
2600 <listitem><para>
2601 DRM_ROOT_ONLY - Only callers with the SYSADMIN capability allowed
2602 </para></listitem>
2603 <listitem><para>
2604 DRM_CONTROL_ALLOW - The ioctl can only be called on a control
2605 device
2606 </para></listitem>
2607 <listitem><para>
2608 DRM_UNLOCKED - The ioctl handler will be called without locking
2609 the DRM global mutex
2610 </para></listitem>
Michael Witten65ffef52011-08-25 20:55:58 +00002611 </itemizedlist>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002612 </para>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002613 </para>
2614 </sect2>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002615 </sect1>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002616 <sect1>
Daniel Vetter4c6e2df2014-01-22 16:46:44 +01002617 <title>Legacy Support Code</title>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002618 <para>
Daniel Vetter4c6e2df2014-01-22 16:46:44 +01002619 The section very brievely covers some of the old legacy support code which
2620 is only used by old DRM drivers which have done a so-called shadow-attach
2621 to the underlying device instead of registering as a real driver. This
2622 also includes some of the old generic buffer mangement and command
2623 submission code. Do not use any of this in new and modern drivers.
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002624 </para>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002625
Daniel Vetter4c6e2df2014-01-22 16:46:44 +01002626 <sect2>
2627 <title>Legacy Suspend/Resume</title>
2628 <para>
2629 The DRM core provides some suspend/resume code, but drivers wanting full
2630 suspend/resume support should provide save() and restore() functions.
2631 These are called at suspend, hibernate, or resume time, and should perform
2632 any state save or restore required by your device across suspend or
2633 hibernate states.
2634 </para>
2635 <synopsis>int (*suspend) (struct drm_device *, pm_message_t state);
2636 int (*resume) (struct drm_device *);</synopsis>
2637 <para>
2638 Those are legacy suspend and resume methods which
2639 <emphasis>only</emphasis> work with the legacy shadow-attach driver
2640 registration functions. New driver should use the power management
2641 interface provided by their bus type (usually through
2642 the struct <structname>device_driver</structname> dev_pm_ops) and set
2643 these methods to NULL.
2644 </para>
2645 </sect2>
2646
2647 <sect2>
2648 <title>Legacy DMA Services</title>
2649 <para>
2650 This should cover how DMA mapping etc. is supported by the core.
2651 These functions are deprecated and should not be used.
2652 </para>
2653 </sect2>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002654 </sect1>
2655 </chapter>
2656
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002657<!-- TODO
2658
2659- Add a glossary
2660- Document the struct_mutex catch-all lock
2661- Document connector properties
2662
2663- Why is the load method optional?
2664- What are drivers supposed to set the initial display state to, and how?
2665 Connector's DPMS states are not initialized and are thus equal to
2666 DRM_MODE_DPMS_ON. The fbcon compatibility layer calls
2667 drm_helper_disable_unused_functions(), which disables unused encoders and
2668 CRTCs, but doesn't touch the connectors' DPMS state, and
2669 drm_helper_connector_dpms() in reaction to fbdev blanking events. Do drivers
2670 that don't implement (or just don't use) fbcon compatibility need to call
2671 those functions themselves?
2672- KMS drivers must call drm_vblank_pre_modeset() and drm_vblank_post_modeset()
2673 around mode setting. Should this be done in the DRM core?
2674- vblank_disable_allowed is set to 1 in the first drm_vblank_post_modeset()
2675 call and never set back to 0. It seems to be safe to permanently set it to 1
2676 in drm_vblank_init() for KMS driver, and it might be safe for UMS drivers as
2677 well. This should be investigated.
2678- crtc and connector .save and .restore operations are only used internally in
2679 drivers, should they be removed from the core?
2680- encoder mid-layer .save and .restore operations are only used internally in
2681 drivers, should they be removed from the core?
2682- encoder mid-layer .detect operation is only used internally in drivers,
2683 should it be removed from the core?
2684-->
2685
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002686 <!-- External interfaces -->
2687
2688 <chapter id="drmExternals">
2689 <title>Userland interfaces</title>
2690 <para>
2691 The DRM core exports several interfaces to applications,
2692 generally intended to be used through corresponding libdrm
Michael Wittena5294e02011-08-29 18:05:52 +00002693 wrapper functions. In addition, drivers export device-specific
Michael Witten7f0925a2011-08-29 18:07:13 +00002694 interfaces for use by userspace drivers &amp; device-aware
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002695 applications through ioctls and sysfs files.
2696 </para>
2697 <para>
2698 External interfaces include: memory mapping, context management,
2699 DMA operations, AGP management, vblank control, fence
2700 management, memory management, and output management.
2701 </para>
2702 <para>
Michael Wittenbcd3cfc2011-08-29 19:29:16 +00002703 Cover generic ioctls and sysfs layout here. We only need high-level
2704 info, since man pages should cover the rest.
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002705 </para>
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002706
David Herrmann17931262013-08-25 18:29:00 +02002707 <!-- External: render nodes -->
2708
2709 <sect1>
2710 <title>Render nodes</title>
2711 <para>
2712 DRM core provides multiple character-devices for user-space to use.
2713 Depending on which device is opened, user-space can perform a different
2714 set of operations (mainly ioctls). The primary node is always created
Daniel Vetter00153ae2014-01-21 12:51:43 +01002715 and called card&lt;num&gt;. Additionally, a currently
2716 unused control node, called controlD&lt;num&gt; is also
David Herrmann17931262013-08-25 18:29:00 +02002717 created. The primary node provides all legacy operations and
2718 historically was the only interface used by userspace. With KMS, the
2719 control node was introduced. However, the planned KMS control interface
2720 has never been written and so the control node stays unused to date.
2721 </para>
2722 <para>
2723 With the increased use of offscreen renderers and GPGPU applications,
2724 clients no longer require running compositors or graphics servers to
2725 make use of a GPU. But the DRM API required unprivileged clients to
2726 authenticate to a DRM-Master prior to getting GPU access. To avoid this
2727 step and to grant clients GPU access without authenticating, render
2728 nodes were introduced. Render nodes solely serve render clients, that
2729 is, no modesetting or privileged ioctls can be issued on render nodes.
2730 Only non-global rendering commands are allowed. If a driver supports
Daniel Vetter00153ae2014-01-21 12:51:43 +01002731 render nodes, it must advertise it via the DRIVER_RENDER
David Herrmann17931262013-08-25 18:29:00 +02002732 DRM driver capability. If not supported, the primary node must be used
2733 for render clients together with the legacy drmAuth authentication
2734 procedure.
2735 </para>
2736 <para>
2737 If a driver advertises render node support, DRM core will create a
Daniel Vetter00153ae2014-01-21 12:51:43 +01002738 separate render node called renderD&lt;num&gt;. There will
David Herrmann17931262013-08-25 18:29:00 +02002739 be one render node per device. No ioctls except PRIME-related ioctls
Daniel Vetter00153ae2014-01-21 12:51:43 +01002740 will be allowed on this node. Especially GEM_OPEN will be
David Herrmann17931262013-08-25 18:29:00 +02002741 explicitly prohibited. Render nodes are designed to avoid the
2742 buffer-leaks, which occur if clients guess the flink names or mmap
2743 offsets on the legacy interface. Additionally to this basic interface,
2744 drivers must mark their driver-dependent render-only ioctls as
Daniel Vetter00153ae2014-01-21 12:51:43 +01002745 DRM_RENDER_ALLOW so render clients can use them. Driver
David Herrmann17931262013-08-25 18:29:00 +02002746 authors must be careful not to allow any privileged ioctls on render
2747 nodes.
2748 </para>
2749 <para>
2750 With render nodes, user-space can now control access to the render node
2751 via basic file-system access-modes. A running graphics server which
2752 authenticates clients on the privileged primary/legacy node is no longer
2753 required. Instead, a client can open the render node and is immediately
2754 granted GPU access. Communication between clients (or servers) is done
2755 via PRIME. FLINK from render node to legacy node is not supported. New
2756 clients must not use the insecure FLINK interface.
2757 </para>
2758 <para>
2759 Besides dropping all modeset/global ioctls, render nodes also drop the
2760 DRM-Master concept. There is no reason to associate render clients with
2761 a DRM-Master as they are independent of any graphics server. Besides,
2762 they must work without any running master, anyway.
2763 Drivers must be able to run without a master object if they support
2764 render nodes. If, on the other hand, a driver requires shared state
2765 between clients which is visible to user-space and accessible beyond
2766 open-file boundaries, they cannot support render nodes.
2767 </para>
2768 </sect1>
2769
Laurent Pinchart9cad9c92012-07-13 00:57:26 +02002770 <!-- External: vblank handling -->
2771
2772 <sect1>
2773 <title>VBlank event handling</title>
2774 <para>
2775 The DRM core exposes two vertical blank related ioctls:
2776 <variablelist>
2777 <varlistentry>
2778 <term>DRM_IOCTL_WAIT_VBLANK</term>
2779 <listitem>
2780 <para>
2781 This takes a struct drm_wait_vblank structure as its argument,
2782 and it is used to block or request a signal when a specified
2783 vblank event occurs.
2784 </para>
2785 </listitem>
2786 </varlistentry>
2787 <varlistentry>
2788 <term>DRM_IOCTL_MODESET_CTL</term>
2789 <listitem>
2790 <para>
2791 This should be called by application level drivers before and
2792 after mode setting, since on many devices the vertical blank
2793 counter is reset at that time. Internally, the DRM snapshots
2794 the last vblank count when the ioctl is called with the
2795 _DRM_PRE_MODESET command, so that the counter won't go backwards
2796 (which is dealt with when _DRM_POST_MODESET is used).
2797 </para>
2798 </listitem>
2799 </varlistentry>
2800 </variablelist>
2801<!--!Edrivers/char/drm/drm_irq.c-->
2802 </para>
2803 </sect1>
2804
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002805 </chapter>
Daniel Vetter3519f702014-01-22 12:21:16 +01002806</part>
2807<part id="drmDrivers">
2808 <title>DRM Drivers</title>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002809
Daniel Vetter3519f702014-01-22 12:21:16 +01002810 <partintro>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002811 <para>
Daniel Vetter3519f702014-01-22 12:21:16 +01002812 This second part of the DRM Developer's Guide documents driver code,
2813 implementation details and also all the driver-specific userspace
2814 interfaces. Especially since all hardware-acceleration interfaces to
2815 userspace are driver specific for efficiency and other reasons these
2816 interfaces can be rather substantial. Hence every driver has its own
2817 chapter.
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002818 </para>
Daniel Vetter3519f702014-01-22 12:21:16 +01002819 </partintro>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002820
Daniel Vetter3519f702014-01-22 12:21:16 +01002821 <chapter id="drmI915">
2822 <title>drm/i915 Intel GFX Driver</title>
2823 <para>
2824 The drm/i915 driver supports all (with the exception of some very early
2825 models) integrated GFX chipsets with both Intel display and rendering
2826 blocks. This excludes a set of SoC platforms with an SGX rendering unit,
2827 those have basic support through the gma500 drm driver.
2828 </para>
2829 <sect1>
2830 <title>Display Hardware Handling</title>
2831 <para>
2832 This section covers everything related to the display hardware including
2833 the mode setting infrastructure, plane, sprite and cursor handling and
2834 display, output probing and related topics.
2835 </para>
2836 <sect2>
2837 <title>Mode Setting Infrastructure</title>
2838 <para>
2839 The i915 driver is thus far the only DRM driver which doesn't use the
2840 common DRM helper code to implement mode setting sequences. Thus it
2841 has its own tailor-made infrastructure for executing a display
2842 configuration change.
2843 </para>
2844 </sect2>
2845 <sect2>
2846 <title>Plane Configuration</title>
2847 <para>
2848 This section covers plane configuration and composition with the
2849 primary plane, sprites, cursors and overlays. This includes the
2850 infrastructure to do atomic vsync'ed updates of all this state and
2851 also tightly coupled topics like watermark setup and computation,
2852 framebuffer compression and panel self refresh.
2853 </para>
2854 </sect2>
2855 <sect2>
2856 <title>Output Probing</title>
2857 <para>
2858 This section covers output probing and related infrastructure like the
2859 hotplug interrupt storm detection and mitigation code. Note that the
2860 i915 driver still uses most of the common DRM helper code for output
2861 probing, so those sections fully apply.
2862 </para>
2863 </sect2>
2864 </sect1>
2865
2866 <sect1>
2867 <title>Memory Management and Command Submission</title>
2868 <para>
2869 This sections covers all things related to the GEM implementation in the
2870 i915 driver.
2871 </para>
2872 </sect1>
2873 </chapter>
2874</part>
Jesse Barnes2d2ef822009-10-26 13:06:31 -07002875</book>