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