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