ba09b6f1e5b3990e6800938ebaa7c11c74e49536
[firefly-linux-kernel-4.4.55.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       <author>
33         <firstname>Daniel</firstname>
34         <surname>Vetter</surname>
35         <contrib>Contributions all over the place</contrib>
36         <affiliation>
37           <orgname>Intel Corporation</orgname>
38           <address>
39             <email>daniel.vetter@ffwll.ch</email>
40           </address>
41         </affiliation>
42       </author>
43     </authorgroup>
44
45     <copyright>
46       <year>2008-2009</year>
47       <year>2013-2014</year>
48       <holder>Intel Corporation</holder>
49     </copyright>
50     <copyright>
51       <year>2012</year>
52       <holder>Laurent Pinchart</holder>
53     </copyright>
54
55     <legalnotice>
56       <para>
57         The contents of this file may be used under the terms of the GNU
58         General Public License version 2 (the "GPL") as distributed in
59         the kernel source COPYING file.
60       </para>
61     </legalnotice>
62
63     <revhistory>
64       <!-- Put document revisions here, newest first. -->
65       <revision>
66         <revnumber>1.0</revnumber>
67         <date>2012-07-13</date>
68         <authorinitials>LP</authorinitials>
69         <revremark>Added extensive documentation about driver internals.
70         </revremark>
71       </revision>
72     </revhistory>
73   </bookinfo>
74
75 <toc></toc>
76
77 <part id="drmCore">
78   <title>DRM Core</title>
79   <partintro>
80     <para>
81       This first part of the DRM Developer's Guide documents core DRM code,
82       helper libraries for writing drivers and generic userspace interfaces
83       exposed by DRM drivers.
84     </para>
85   </partintro>
86
87   <chapter id="drmIntroduction">
88     <title>Introduction</title>
89     <para>
90       The Linux DRM layer contains code intended to support the needs
91       of complex graphics devices, usually containing programmable
92       pipelines well suited to 3D graphics acceleration.  Graphics
93       drivers in the kernel may make use of DRM functions to make
94       tasks like memory management, interrupt handling and DMA easier,
95       and provide a uniform interface to applications.
96     </para>
97     <para>
98       A note on versions: this guide covers features found in the DRM
99       tree, including the TTM memory manager, output configuration and
100       mode setting, and the new vblank internals, in addition to all
101       the regular features found in current kernels.
102     </para>
103     <para>
104       [Insert diagram of typical DRM stack here]
105     </para>
106   </chapter>
107
108   <!-- Internals -->
109
110   <chapter id="drmInternals">
111     <title>DRM Internals</title>
112     <para>
113       This chapter documents DRM internals relevant to driver authors
114       and developers working to add support for the latest features to
115       existing drivers.
116     </para>
117     <para>
118       First, we go over some typical driver initialization
119       requirements, like setting up command buffers, creating an
120       initial output configuration, and initializing core services.
121       Subsequent sections cover core internals in more detail,
122       providing implementation notes and examples.
123     </para>
124     <para>
125       The DRM layer provides several services to graphics drivers,
126       many of them driven by the application interfaces it provides
127       through libdrm, the library that wraps most of the DRM ioctls.
128       These include vblank event handling, memory
129       management, output management, framebuffer management, command
130       submission &amp; fencing, suspend/resume support, and DMA
131       services.
132     </para>
133
134   <!-- Internals: driver init -->
135
136   <sect1>
137     <title>Driver Initialization</title>
138     <para>
139       At the core of every DRM driver is a <structname>drm_driver</structname>
140       structure. Drivers typically statically initialize a drm_driver structure,
141       and then pass it to one of the <function>drm_*_init()</function> functions
142       to register it with the DRM subsystem.
143     </para>
144     <para>
145       The <structname>drm_driver</structname> structure contains static
146       information that describes the driver and features it supports, and
147       pointers to methods that the DRM core will call to implement the DRM API.
148       We will first go through the <structname>drm_driver</structname> static
149       information fields, and will then describe individual operations in
150       details as they get used in later sections.
151     </para>
152     <sect2>
153       <title>Driver Information</title>
154       <sect3>
155         <title>Driver Features</title>
156         <para>
157           Drivers inform the DRM core about their requirements and supported
158           features by setting appropriate flags in the
159           <structfield>driver_features</structfield> field. Since those flags
160           influence the DRM core behaviour since registration time, most of them
161           must be set to registering the <structname>drm_driver</structname>
162           instance.
163         </para>
164         <synopsis>u32 driver_features;</synopsis>
165         <variablelist>
166           <title>Driver Feature Flags</title>
167           <varlistentry>
168             <term>DRIVER_USE_AGP</term>
169             <listitem><para>
170               Driver uses AGP interface, the DRM core will manage AGP resources.
171             </para></listitem>
172           </varlistentry>
173           <varlistentry>
174             <term>DRIVER_REQUIRE_AGP</term>
175             <listitem><para>
176               Driver needs AGP interface to function. AGP initialization failure
177               will become a fatal error.
178             </para></listitem>
179           </varlistentry>
180           <varlistentry>
181             <term>DRIVER_PCI_DMA</term>
182             <listitem><para>
183               Driver is capable of PCI DMA, mapping of PCI DMA buffers to
184               userspace will be enabled. Deprecated.
185             </para></listitem>
186           </varlistentry>
187           <varlistentry>
188             <term>DRIVER_SG</term>
189             <listitem><para>
190               Driver can perform scatter/gather DMA, allocation and mapping of
191               scatter/gather buffers will be enabled. Deprecated.
192             </para></listitem>
193           </varlistentry>
194           <varlistentry>
195             <term>DRIVER_HAVE_DMA</term>
196             <listitem><para>
197               Driver supports DMA, the userspace DMA API will be supported.
198               Deprecated.
199             </para></listitem>
200           </varlistentry>
201           <varlistentry>
202             <term>DRIVER_HAVE_IRQ</term><term>DRIVER_IRQ_SHARED</term>
203             <listitem><para>
204               DRIVER_HAVE_IRQ indicates whether the driver has an IRQ handler
205               managed by the DRM Core. The core will support simple IRQ handler
206               installation when the flag is set. The installation process is
207               described in <xref linkend="drm-irq-registration"/>.</para>
208               <para>DRIVER_IRQ_SHARED indicates whether the device &amp; handler
209               support shared IRQs (note that this is required of PCI  drivers).
210             </para></listitem>
211           </varlistentry>
212           <varlistentry>
213             <term>DRIVER_GEM</term>
214             <listitem><para>
215               Driver use the GEM memory manager.
216             </para></listitem>
217           </varlistentry>
218           <varlistentry>
219             <term>DRIVER_MODESET</term>
220             <listitem><para>
221               Driver supports mode setting interfaces (KMS).
222             </para></listitem>
223           </varlistentry>
224           <varlistentry>
225             <term>DRIVER_PRIME</term>
226             <listitem><para>
227               Driver implements DRM PRIME buffer sharing.
228             </para></listitem>
229           </varlistentry>
230           <varlistentry>
231             <term>DRIVER_RENDER</term>
232             <listitem><para>
233               Driver supports dedicated render nodes.
234             </para></listitem>
235           </varlistentry>
236         </variablelist>
237       </sect3>
238       <sect3>
239         <title>Major, Minor and Patchlevel</title>
240         <synopsis>int major;
241 int minor;
242 int patchlevel;</synopsis>
243         <para>
244           The DRM core identifies driver versions by a major, minor and patch
245           level triplet. The information is printed to the kernel log at
246           initialization time and passed to userspace through the
247           DRM_IOCTL_VERSION ioctl.
248         </para>
249         <para>
250           The major and minor numbers are also used to verify the requested driver
251           API version passed to DRM_IOCTL_SET_VERSION. When the driver API changes
252           between minor versions, applications can call DRM_IOCTL_SET_VERSION to
253           select a specific version of the API. If the requested major isn't equal
254           to the driver major, or the requested minor is larger than the driver
255           minor, the DRM_IOCTL_SET_VERSION call will return an error. Otherwise
256           the driver's set_version() method will be called with the requested
257           version.
258         </para>
259       </sect3>
260       <sect3>
261         <title>Name, Description and Date</title>
262         <synopsis>char *name;
263 char *desc;
264 char *date;</synopsis>
265         <para>
266           The driver name is printed to the kernel log at initialization time,
267           used for IRQ registration and passed to userspace through
268           DRM_IOCTL_VERSION.
269         </para>
270         <para>
271           The driver description is a purely informative string passed to
272           userspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
273           the kernel.
274         </para>
275         <para>
276           The driver date, formatted as YYYYMMDD, is meant to identify the date of
277           the latest modification to the driver. However, as most drivers fail to
278           update it, its value is mostly useless. The DRM core prints it to the
279           kernel log at initialization time and passes it to userspace through the
280           DRM_IOCTL_VERSION ioctl.
281         </para>
282       </sect3>
283     </sect2>
284     <sect2>
285       <title>Device Registration</title>
286       <para>
287         A number of functions are provided to help with device registration.
288         The functions deal with PCI, USB and platform devices, respectively.
289       </para>
290 !Edrivers/gpu/drm/drm_pci.c
291 !Edrivers/gpu/drm/drm_usb.c
292 !Edrivers/gpu/drm/drm_platform.c
293     </sect2>
294     <sect2>
295       <title>Driver Load</title>
296       <para>
297         The <methodname>load</methodname> method is the driver and device
298         initialization entry point. The method is responsible for allocating and
299         initializing driver private data, performing resource allocation and
300         mapping (e.g. acquiring
301         clocks, mapping registers or allocating command buffers), initializing
302         the memory manager (<xref linkend="drm-memory-management"/>), installing
303         the IRQ handler (<xref linkend="drm-irq-registration"/>), setting up
304         vertical blanking handling (<xref linkend="drm-vertical-blank"/>), mode
305         setting (<xref linkend="drm-mode-setting"/>) and initial output
306         configuration (<xref linkend="drm-kms-init"/>).
307       </para>
308       <note><para>
309         If compatibility is a concern (e.g. with drivers converted over from
310         User Mode Setting to Kernel Mode Setting), care must be taken to prevent
311         device initialization and control that is incompatible with currently
312         active userspace drivers. For instance, if user level mode setting
313         drivers are in use, it would be problematic to perform output discovery
314         &amp; configuration at load time. Likewise, if user-level drivers
315         unaware of memory management are in use, memory management and command
316         buffer setup may need to be omitted. These requirements are
317         driver-specific, and care needs to be taken to keep both old and new
318         applications and libraries working.
319       </para></note>
320       <synopsis>int (*load) (struct drm_device *, unsigned long flags);</synopsis>
321       <para>
322         The method takes two arguments, a pointer to the newly created
323         <structname>drm_device</structname> and flags. The flags are used to
324         pass the <structfield>driver_data</structfield> field of the device id
325         corresponding to the device passed to <function>drm_*_init()</function>.
326         Only PCI devices currently use this, USB and platform DRM drivers have
327         their <methodname>load</methodname> method called with flags to 0.
328       </para>
329       <sect3>
330         <title>Driver Private Data</title>
331         <para>
332           The driver private hangs off the main
333           <structname>drm_device</structname> structure and can be used for
334           tracking various device-specific bits of information, like register
335           offsets, command buffer status, register state for suspend/resume, etc.
336           At load time, a driver may simply allocate one and set
337           <structname>drm_device</structname>.<structfield>dev_priv</structfield>
338           appropriately; it should be freed and
339           <structname>drm_device</structname>.<structfield>dev_priv</structfield>
340           set to NULL when the driver is unloaded.
341         </para>
342       </sect3>
343       <sect3 id="drm-irq-registration">
344         <title>IRQ Registration</title>
345         <para>
346           The DRM core tries to facilitate IRQ handler registration and
347           unregistration by providing <function>drm_irq_install</function> and
348           <function>drm_irq_uninstall</function> functions. Those functions only
349           support a single interrupt per device, devices that use more than one
350           IRQs need to be handled manually.
351         </para>
352         <sect4>
353           <title>Managed IRQ Registration</title>
354           <para>
355             <function>drm_irq_install</function> starts by calling the
356             <methodname>irq_preinstall</methodname> driver operation. The operation
357             is optional and must make sure that the interrupt will not get fired by
358             clearing all pending interrupt flags or disabling the interrupt.
359           </para>
360           <para>
361             The passed-in IRQ will then be requested by a call to
362             <function>request_irq</function>. If the DRIVER_IRQ_SHARED driver
363             feature flag is set, a shared (IRQF_SHARED) IRQ handler will be
364             requested.
365           </para>
366           <para>
367             The IRQ handler function must be provided as the mandatory irq_handler
368             driver operation. It will get passed directly to
369             <function>request_irq</function> and thus has the same prototype as all
370             IRQ handlers. It will get called with a pointer to the DRM device as the
371             second argument.
372           </para>
373           <para>
374             Finally the function calls the optional
375             <methodname>irq_postinstall</methodname> driver operation. The operation
376             usually enables interrupts (excluding the vblank interrupt, which is
377             enabled separately), but drivers may choose to enable/disable interrupts
378             at a different time.
379           </para>
380           <para>
381             <function>drm_irq_uninstall</function> is similarly used to uninstall an
382             IRQ handler. It starts by waking up all processes waiting on a vblank
383             interrupt to make sure they don't hang, and then calls the optional
384             <methodname>irq_uninstall</methodname> driver operation. The operation
385             must disable all hardware interrupts. Finally the function frees the IRQ
386             by calling <function>free_irq</function>.
387           </para>
388         </sect4>
389         <sect4>
390           <title>Manual IRQ Registration</title>
391           <para>
392             Drivers that require multiple interrupt handlers can't use the managed
393             IRQ registration functions. In that case IRQs must be registered and
394             unregistered manually (usually with the <function>request_irq</function>
395             and <function>free_irq</function> functions, or their devm_* equivalent).
396           </para>
397           <para>
398             When manually registering IRQs, drivers must not set the DRIVER_HAVE_IRQ
399             driver feature flag, and must not provide the
400             <methodname>irq_handler</methodname> driver operation. They must set the
401             <structname>drm_device</structname> <structfield>irq_enabled</structfield>
402             field to 1 upon registration of the IRQs, and clear it to 0 after
403             unregistering the IRQs.
404           </para>
405         </sect4>
406       </sect3>
407       <sect3>
408         <title>Memory Manager Initialization</title>
409         <para>
410           Every DRM driver requires a memory manager which must be initialized at
411           load time. DRM currently contains two memory managers, the Translation
412           Table Manager (TTM) and the Graphics Execution Manager (GEM).
413           This document describes the use of the GEM memory manager only. See
414           <xref linkend="drm-memory-management"/> for details.
415         </para>
416       </sect3>
417       <sect3>
418         <title>Miscellaneous Device Configuration</title>
419         <para>
420           Another task that may be necessary for PCI devices during configuration
421           is mapping the video BIOS. On many devices, the VBIOS describes device
422           configuration, LCD panel timings (if any), and contains flags indicating
423           device state. Mapping the BIOS can be done using the pci_map_rom() call,
424           a convenience function that takes care of mapping the actual ROM,
425           whether it has been shadowed into memory (typically at address 0xc0000)
426           or exists on the PCI device in the ROM BAR. Note that after the ROM has
427           been mapped and any necessary information has been extracted, it should
428           be unmapped; on many devices, the ROM address decoder is shared with
429           other BARs, so leaving it mapped could cause undesired behaviour like
430           hangs or memory corruption.
431   <!--!Fdrivers/pci/rom.c pci_map_rom-->
432         </para>
433       </sect3>
434     </sect2>
435   </sect1>
436
437   <!-- Internals: memory management -->
438
439   <sect1 id="drm-memory-management">
440     <title>Memory management</title>
441     <para>
442       Modern Linux systems require large amount of graphics memory to store
443       frame buffers, textures, vertices and other graphics-related data. Given
444       the very dynamic nature of many of that data, managing graphics memory
445       efficiently is thus crucial for the graphics stack and plays a central
446       role in the DRM infrastructure.
447     </para>
448     <para>
449       The DRM core includes two memory managers, namely Translation Table Maps
450       (TTM) and Graphics Execution Manager (GEM). TTM was the first DRM memory
451       manager to be developed and tried to be a one-size-fits-them all
452       solution. It provides a single userspace API to accommodate the need of
453       all hardware, supporting both Unified Memory Architecture (UMA) devices
454       and devices with dedicated video RAM (i.e. most discrete video cards).
455       This resulted in a large, complex piece of code that turned out to be
456       hard to use for driver development.
457     </para>
458     <para>
459       GEM started as an Intel-sponsored project in reaction to TTM's
460       complexity. Its design philosophy is completely different: instead of
461       providing a solution to every graphics memory-related problems, GEM
462       identified common code between drivers and created a support library to
463       share it. GEM has simpler initialization and execution requirements than
464       TTM, but has no video RAM management capabilities and is thus limited to
465       UMA devices.
466     </para>
467     <sect2>
468       <title>The Translation Table Manager (TTM)</title>
469       <para>
470         TTM design background and information belongs here.
471       </para>
472       <sect3>
473         <title>TTM initialization</title>
474         <warning><para>This section is outdated.</para></warning>
475         <para>
476           Drivers wishing to support TTM must fill out a drm_bo_driver
477           structure. The structure contains several fields with function
478           pointers for initializing the TTM, allocating and freeing memory,
479           waiting for command completion and fence synchronization, and memory
480           migration. See the radeon_ttm.c file for an example of usage.
481         </para>
482         <para>
483           The ttm_global_reference structure is made up of several fields:
484         </para>
485         <programlisting>
486           struct ttm_global_reference {
487                 enum ttm_global_types global_type;
488                 size_t size;
489                 void *object;
490                 int (*init) (struct ttm_global_reference *);
491                 void (*release) (struct ttm_global_reference *);
492           };
493         </programlisting>
494         <para>
495           There should be one global reference structure for your memory
496           manager as a whole, and there will be others for each object
497           created by the memory manager at runtime.  Your global TTM should
498           have a type of TTM_GLOBAL_TTM_MEM.  The size field for the global
499           object should be sizeof(struct ttm_mem_global), and the init and
500           release hooks should point at your driver-specific init and
501           release routines, which probably eventually call
502           ttm_mem_global_init and ttm_mem_global_release, respectively.
503         </para>
504         <para>
505           Once your global TTM accounting structure is set up and initialized
506           by calling ttm_global_item_ref() on it,
507           you need to create a buffer object TTM to
508           provide a pool for buffer object allocation by clients and the
509           kernel itself.  The type of this object should be TTM_GLOBAL_TTM_BO,
510           and its size should be sizeof(struct ttm_bo_global).  Again,
511           driver-specific init and release functions may be provided,
512           likely eventually calling ttm_bo_global_init() and
513           ttm_bo_global_release(), respectively.  Also, like the previous
514           object, ttm_global_item_ref() is used to create an initial reference
515           count for the TTM, which will call your initialization function.
516         </para>
517       </sect3>
518     </sect2>
519     <sect2 id="drm-gem">
520       <title>The Graphics Execution Manager (GEM)</title>
521       <para>
522         The GEM design approach has resulted in a memory manager that doesn't
523         provide full coverage of all (or even all common) use cases in its
524         userspace or kernel API. GEM exposes a set of standard memory-related
525         operations to userspace and a set of helper functions to drivers, and let
526         drivers implement hardware-specific operations with their own private API.
527       </para>
528       <para>
529         The GEM userspace API is described in the
530         <ulink url="http://lwn.net/Articles/283798/"><citetitle>GEM - the Graphics
531         Execution Manager</citetitle></ulink> article on LWN. While slightly
532         outdated, the document provides a good overview of the GEM API principles.
533         Buffer allocation and read and write operations, described as part of the
534         common GEM API, are currently implemented using driver-specific ioctls.
535       </para>
536       <para>
537         GEM is data-agnostic. It manages abstract buffer objects without knowing
538         what individual buffers contain. APIs that require knowledge of buffer
539         contents or purpose, such as buffer allocation or synchronization
540         primitives, are thus outside of the scope of GEM and must be implemented
541         using driver-specific ioctls.
542       </para>
543       <para>
544         On a fundamental level, GEM involves several operations:
545         <itemizedlist>
546           <listitem>Memory allocation and freeing</listitem>
547           <listitem>Command execution</listitem>
548           <listitem>Aperture management at command execution time</listitem>
549         </itemizedlist>
550         Buffer object allocation is relatively straightforward and largely
551         provided by Linux's shmem layer, which provides memory to back each
552         object.
553       </para>
554       <para>
555         Device-specific operations, such as command execution, pinning, buffer
556         read &amp; write, mapping, and domain ownership transfers are left to
557         driver-specific ioctls.
558       </para>
559       <sect3>
560         <title>GEM Initialization</title>
561         <para>
562           Drivers that use GEM must set the DRIVER_GEM bit in the struct
563           <structname>drm_driver</structname>
564           <structfield>driver_features</structfield> field. The DRM core will
565           then automatically initialize the GEM core before calling the
566           <methodname>load</methodname> operation. Behind the scene, this will
567           create a DRM Memory Manager object which provides an address space
568           pool for object allocation.
569         </para>
570         <para>
571           In a KMS configuration, drivers need to allocate and initialize a
572           command ring buffer following core GEM initialization if required by
573           the hardware. UMA devices usually have what is called a "stolen"
574           memory region, which provides space for the initial framebuffer and
575           large, contiguous memory regions required by the device. This space is
576           typically not managed by GEM, and must be initialized separately into
577           its own DRM MM object.
578         </para>
579       </sect3>
580       <sect3>
581         <title>GEM Objects Creation</title>
582         <para>
583           GEM splits creation of GEM objects and allocation of the memory that
584           backs them in two distinct operations.
585         </para>
586         <para>
587           GEM objects are represented by an instance of struct
588           <structname>drm_gem_object</structname>. Drivers usually need to extend
589           GEM objects with private information and thus create a driver-specific
590           GEM object structure type that embeds an instance of struct
591           <structname>drm_gem_object</structname>.
592         </para>
593         <para>
594           To create a GEM object, a driver allocates memory for an instance of its
595           specific GEM object type and initializes the embedded struct
596           <structname>drm_gem_object</structname> with a call to
597           <function>drm_gem_object_init</function>. The function takes a pointer to
598           the DRM device, a pointer to the GEM object and the buffer object size
599           in bytes.
600         </para>
601         <para>
602           GEM uses shmem to allocate anonymous pageable memory.
603           <function>drm_gem_object_init</function> will create an shmfs file of
604           the requested size and store it into the struct
605           <structname>drm_gem_object</structname> <structfield>filp</structfield>
606           field. The memory is used as either main storage for the object when the
607           graphics hardware uses system memory directly or as a backing store
608           otherwise.
609         </para>
610         <para>
611           Drivers are responsible for the actual physical pages allocation by
612           calling <function>shmem_read_mapping_page_gfp</function> for each page.
613           Note that they can decide to allocate pages when initializing the GEM
614           object, or to delay allocation until the memory is needed (for instance
615           when a page fault occurs as a result of a userspace memory access or
616           when the driver needs to start a DMA transfer involving the memory).
617         </para>
618         <para>
619           Anonymous pageable memory allocation is not always desired, for instance
620           when the hardware requires physically contiguous system memory as is
621           often the case in embedded devices. Drivers can create GEM objects with
622           no shmfs backing (called private GEM objects) by initializing them with
623           a call to <function>drm_gem_private_object_init</function> instead of
624           <function>drm_gem_object_init</function>. Storage for private GEM
625           objects must be managed by drivers.
626         </para>
627         <para>
628           Drivers that do not need to extend GEM objects with private information
629           can call the <function>drm_gem_object_alloc</function> function to
630           allocate and initialize a struct <structname>drm_gem_object</structname>
631           instance. The GEM core will call the optional driver
632           <methodname>gem_init_object</methodname> operation after initializing
633           the GEM object with <function>drm_gem_object_init</function>.
634           <synopsis>int (*gem_init_object) (struct drm_gem_object *obj);</synopsis>
635         </para>
636         <para>
637           No alloc-and-init function exists for private GEM objects.
638         </para>
639       </sect3>
640       <sect3>
641         <title>GEM Objects Lifetime</title>
642         <para>
643           All GEM objects are reference-counted by the GEM core. References can be
644           acquired and release by <function>calling drm_gem_object_reference</function>
645           and <function>drm_gem_object_unreference</function> respectively. The
646           caller must hold the <structname>drm_device</structname>
647           <structfield>struct_mutex</structfield> lock. As a convenience, GEM
648           provides the <function>drm_gem_object_reference_unlocked</function> and
649           <function>drm_gem_object_unreference_unlocked</function> functions that
650           can be called without holding the lock.
651         </para>
652         <para>
653           When the last reference to a GEM object is released the GEM core calls
654           the <structname>drm_driver</structname>
655           <methodname>gem_free_object</methodname> operation. That operation is
656           mandatory for GEM-enabled drivers and must free the GEM object and all
657           associated resources.
658         </para>
659         <para>
660           <synopsis>void (*gem_free_object) (struct drm_gem_object *obj);</synopsis>
661           Drivers are responsible for freeing all GEM object resources, including
662           the resources created by the GEM core. If an mmap offset has been
663           created for the object (in which case
664           <structname>drm_gem_object</structname>::<structfield>map_list</structfield>::<structfield>map</structfield>
665           is not NULL) it must be freed by a call to
666           <function>drm_gem_free_mmap_offset</function>. The shmfs backing store
667           must be released by calling <function>drm_gem_object_release</function>
668           (that function can safely be called if no shmfs backing store has been
669           created).
670         </para>
671       </sect3>
672       <sect3>
673         <title>GEM Objects Naming</title>
674         <para>
675           Communication between userspace and the kernel refers to GEM objects
676           using local handles, global names or, more recently, file descriptors.
677           All of those are 32-bit integer values; the usual Linux kernel limits
678           apply to the file descriptors.
679         </para>
680         <para>
681           GEM handles are local to a DRM file. Applications get a handle to a GEM
682           object through a driver-specific ioctl, and can use that handle to refer
683           to the GEM object in other standard or driver-specific ioctls. Closing a
684           DRM file handle frees all its GEM handles and dereferences the
685           associated GEM objects.
686         </para>
687         <para>
688           To create a handle for a GEM object drivers call
689           <function>drm_gem_handle_create</function>. The function takes a pointer
690           to the DRM file and the GEM object and returns a locally unique handle.
691           When the handle is no longer needed drivers delete it with a call to
692           <function>drm_gem_handle_delete</function>. Finally the GEM object
693           associated with a handle can be retrieved by a call to
694           <function>drm_gem_object_lookup</function>.
695         </para>
696         <para>
697           Handles don't take ownership of GEM objects, they only take a reference
698           to the object that will be dropped when the handle is destroyed. To
699           avoid leaking GEM objects, drivers must make sure they drop the
700           reference(s) they own (such as the initial reference taken at object
701           creation time) as appropriate, without any special consideration for the
702           handle. For example, in the particular case of combined GEM object and
703           handle creation in the implementation of the
704           <methodname>dumb_create</methodname> operation, drivers must drop the
705           initial reference to the GEM object before returning the handle.
706         </para>
707         <para>
708           GEM names are similar in purpose to handles but are not local to DRM
709           files. They can be passed between processes to reference a GEM object
710           globally. Names can't be used directly to refer to objects in the DRM
711           API, applications must convert handles to names and names to handles
712           using the DRM_IOCTL_GEM_FLINK and DRM_IOCTL_GEM_OPEN ioctls
713           respectively. The conversion is handled by the DRM core without any
714           driver-specific support.
715         </para>
716         <para>
717           GEM also supports buffer sharing with dma-buf file descriptors through
718           PRIME. GEM-based drivers must use the provided helpers functions to
719           implement the exporting and importing correctly. See <xref linkend="drm-prime-support" />.
720           Since sharing file descriptors is inherently more secure than the
721           easily guessable and global GEM names it is the preferred buffer
722           sharing mechanism. Sharing buffers through GEM names is only supported
723           for legacy userspace. Furthermore PRIME also allows cross-device
724           buffer sharing since it is based on dma-bufs.
725         </para>
726       </sect3>
727       <sect3 id="drm-gem-objects-mapping">
728         <title>GEM Objects Mapping</title>
729         <para>
730           Because mapping operations are fairly heavyweight GEM favours
731           read/write-like access to buffers, implemented through driver-specific
732           ioctls, over mapping buffers to userspace. However, when random access
733           to the buffer is needed (to perform software rendering for instance),
734           direct access to the object can be more efficient.
735         </para>
736         <para>
737           The mmap system call can't be used directly to map GEM objects, as they
738           don't have their own file handle. Two alternative methods currently
739           co-exist to map GEM objects to userspace. The first method uses a
740           driver-specific ioctl to perform the mapping operation, calling
741           <function>do_mmap</function> under the hood. This is often considered
742           dubious, seems to be discouraged for new GEM-enabled drivers, and will
743           thus not be described here.
744         </para>
745         <para>
746           The second method uses the mmap system call on the DRM file handle.
747           <synopsis>void *mmap(void *addr, size_t length, int prot, int flags, int fd,
748              off_t offset);</synopsis>
749           DRM identifies the GEM object to be mapped by a fake offset passed
750           through the mmap offset argument. Prior to being mapped, a GEM object
751           must thus be associated with a fake offset. To do so, drivers must call
752           <function>drm_gem_create_mmap_offset</function> on the object. The
753           function allocates a fake offset range from a pool and stores the
754           offset divided by PAGE_SIZE in
755           <literal>obj-&gt;map_list.hash.key</literal>. Care must be taken not to
756           call <function>drm_gem_create_mmap_offset</function> if a fake offset
757           has already been allocated for the object. This can be tested by
758           <literal>obj-&gt;map_list.map</literal> being non-NULL.
759         </para>
760         <para>
761           Once allocated, the fake offset value
762           (<literal>obj-&gt;map_list.hash.key &lt;&lt; PAGE_SHIFT</literal>)
763           must be passed to the application in a driver-specific way and can then
764           be used as the mmap offset argument.
765         </para>
766         <para>
767           The GEM core provides a helper method <function>drm_gem_mmap</function>
768           to handle object mapping. The method can be set directly as the mmap
769           file operation handler. It will look up the GEM object based on the
770           offset value and set the VMA operations to the
771           <structname>drm_driver</structname> <structfield>gem_vm_ops</structfield>
772           field. Note that <function>drm_gem_mmap</function> doesn't map memory to
773           userspace, but relies on the driver-provided fault handler to map pages
774           individually.
775         </para>
776         <para>
777           To use <function>drm_gem_mmap</function>, drivers must fill the struct
778           <structname>drm_driver</structname> <structfield>gem_vm_ops</structfield>
779           field with a pointer to VM operations.
780         </para>
781         <para>
782           <synopsis>struct vm_operations_struct *gem_vm_ops
783
784   struct vm_operations_struct {
785           void (*open)(struct vm_area_struct * area);
786           void (*close)(struct vm_area_struct * area);
787           int (*fault)(struct vm_area_struct *vma, struct vm_fault *vmf);
788   };</synopsis>
789         </para>
790         <para>
791           The <methodname>open</methodname> and <methodname>close</methodname>
792           operations must update the GEM object reference count. Drivers can use
793           the <function>drm_gem_vm_open</function> and
794           <function>drm_gem_vm_close</function> helper functions directly as open
795           and close handlers.
796         </para>
797         <para>
798           The fault operation handler is responsible for mapping individual pages
799           to userspace when a page fault occurs. Depending on the memory
800           allocation scheme, drivers can allocate pages at fault time, or can
801           decide to allocate memory for the GEM object at the time the object is
802           created.
803         </para>
804         <para>
805           Drivers that want to map the GEM object upfront instead of handling page
806           faults can implement their own mmap file operation handler.
807         </para>
808       </sect3>
809       <sect3>
810         <title>Memory Coherency</title>
811         <para>
812           When mapped to the device or used in a command buffer, backing pages
813           for an object are flushed to memory and marked write combined so as to
814           be coherent with the GPU. Likewise, if the CPU accesses an object
815           after the GPU has finished rendering to the object, then the object
816           must be made coherent with the CPU's view of memory, usually involving
817           GPU cache flushing of various kinds. This core CPU&lt;-&gt;GPU
818           coherency management is provided by a device-specific ioctl, which
819           evaluates an object's current domain and performs any necessary
820           flushing or synchronization to put the object into the desired
821           coherency domain (note that the object may be busy, i.e. an active
822           render target; in that case, setting the domain blocks the client and
823           waits for rendering to complete before performing any necessary
824           flushing operations).
825         </para>
826       </sect3>
827       <sect3>
828         <title>Command Execution</title>
829         <para>
830           Perhaps the most important GEM function for GPU devices is providing a
831           command execution interface to clients. Client programs construct
832           command buffers containing references to previously allocated memory
833           objects, and then submit them to GEM. At that point, GEM takes care to
834           bind all the objects into the GTT, execute the buffer, and provide
835           necessary synchronization between clients accessing the same buffers.
836           This often involves evicting some objects from the GTT and re-binding
837           others (a fairly expensive operation), and providing relocation
838           support which hides fixed GTT offsets from clients. Clients must take
839           care not to submit command buffers that reference more objects than
840           can fit in the GTT; otherwise, GEM will reject them and no rendering
841           will occur. Similarly, if several objects in the buffer require fence
842           registers to be allocated for correct rendering (e.g. 2D blits on
843           pre-965 chips), care must be taken not to require more fence registers
844           than are available to the client. Such resource management should be
845           abstracted from the client in libdrm.
846         </para>
847       </sect3>
848       <sect3>
849         <title>GEM Function Reference</title>
850 !Edrivers/gpu/drm/drm_gem.c
851       </sect3>
852       </sect2>
853       <sect2>
854         <title>VMA Offset Manager</title>
855 !Pdrivers/gpu/drm/drm_vma_manager.c vma offset manager
856 !Edrivers/gpu/drm/drm_vma_manager.c
857 !Iinclude/drm/drm_vma_manager.h
858       </sect2>
859       <sect2 id="drm-prime-support">
860         <title>PRIME Buffer Sharing</title>
861         <para>
862           PRIME is the cross device buffer sharing framework in drm, originally
863           created for the OPTIMUS range of multi-gpu platforms. To userspace
864           PRIME buffers are dma-buf based file descriptors.
865         </para>
866         <sect3>
867           <title>Overview and Driver Interface</title>
868           <para>
869             Similar to GEM global names, PRIME file descriptors are
870             also used to share buffer objects across processes. They offer
871             additional security: as file descriptors must be explicitly sent over
872             UNIX domain sockets to be shared between applications, they can't be
873             guessed like the globally unique GEM names.
874           </para>
875           <para>
876             Drivers that support the PRIME
877             API must set the DRIVER_PRIME bit in the struct
878             <structname>drm_driver</structname>
879             <structfield>driver_features</structfield> field, and implement the
880             <methodname>prime_handle_to_fd</methodname> and
881             <methodname>prime_fd_to_handle</methodname> operations.
882           </para>
883           <para>
884             <synopsis>int (*prime_handle_to_fd)(struct drm_device *dev,
885                           struct drm_file *file_priv, uint32_t handle,
886                           uint32_t flags, int *prime_fd);
887 int (*prime_fd_to_handle)(struct drm_device *dev,
888                           struct drm_file *file_priv, int prime_fd,
889                           uint32_t *handle);</synopsis>
890             Those two operations convert a handle to a PRIME file descriptor and
891             vice versa. Drivers must use the kernel dma-buf buffer sharing framework
892             to manage the PRIME file descriptors. Similar to the mode setting
893             API PRIME is agnostic to the underlying buffer object manager, as
894             long as handles are 32bit unsigned integers.
895           </para>
896           <para>
897             While non-GEM drivers must implement the operations themselves, GEM
898             drivers must use the <function>drm_gem_prime_handle_to_fd</function>
899             and <function>drm_gem_prime_fd_to_handle</function> helper functions.
900             Those helpers rely on the driver
901             <methodname>gem_prime_export</methodname> and
902             <methodname>gem_prime_import</methodname> operations to create a dma-buf
903             instance from a GEM object (dma-buf exporter role) and to create a GEM
904             object from a dma-buf instance (dma-buf importer role).
905           </para>
906           <para>
907             <synopsis>struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
908                                      struct drm_gem_object *obj,
909                                      int flags);
910 struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
911                                             struct dma_buf *dma_buf);</synopsis>
912             These two operations are mandatory for GEM drivers that support
913             PRIME.
914           </para>
915         </sect3>
916         <sect3>
917           <title>PRIME Helper Functions</title>
918 !Pdrivers/gpu/drm/drm_prime.c PRIME Helpers
919         </sect3>
920       </sect2>
921       <sect2>
922         <title>PRIME Function References</title>
923 !Edrivers/gpu/drm/drm_prime.c
924       </sect2>
925       <sect2>
926         <title>DRM MM Range Allocator</title>
927         <sect3>
928           <title>Overview</title>
929 !Pdrivers/gpu/drm/drm_mm.c Overview
930         </sect3>
931         <sect3>
932           <title>LRU Scan/Eviction Support</title>
933 !Pdrivers/gpu/drm/drm_mm.c lru scan roaster
934         </sect3>
935       </sect2>
936       <sect2>
937         <title>DRM MM Range Allocator Function References</title>
938 !Edrivers/gpu/drm/drm_mm.c
939 !Iinclude/drm/drm_mm.h
940       </sect2>
941   </sect1>
942
943   <!-- Internals: mode setting -->
944
945   <sect1 id="drm-mode-setting">
946     <title>Mode Setting</title>
947     <para>
948       Drivers must initialize the mode setting core by calling
949       <function>drm_mode_config_init</function> on the DRM device. The function
950       initializes the <structname>drm_device</structname>
951       <structfield>mode_config</structfield> field and never fails. Once done,
952       mode configuration must be setup by initializing the following fields.
953     </para>
954     <itemizedlist>
955       <listitem>
956         <synopsis>int min_width, min_height;
957 int max_width, max_height;</synopsis>
958         <para>
959           Minimum and maximum width and height of the frame buffers in pixel
960           units.
961         </para>
962       </listitem>
963       <listitem>
964         <synopsis>struct drm_mode_config_funcs *funcs;</synopsis>
965         <para>Mode setting functions.</para>
966       </listitem>
967     </itemizedlist>
968     <sect2>
969       <title>Display Modes Function Reference</title>
970 !Iinclude/drm/drm_modes.h
971 !Edrivers/gpu/drm/drm_modes.c
972     </sect2>
973     <sect2>
974       <title>Frame Buffer Creation</title>
975       <synopsis>struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
976                                      struct drm_file *file_priv,
977                                      struct drm_mode_fb_cmd2 *mode_cmd);</synopsis>
978       <para>
979         Frame buffers are abstract memory objects that provide a source of
980         pixels to scanout to a CRTC. Applications explicitly request the
981         creation of frame buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and
982         receive an opaque handle that can be passed to the KMS CRTC control,
983         plane configuration and page flip functions.
984       </para>
985       <para>
986         Frame buffers rely on the underneath memory manager for low-level memory
987         operations. When creating a frame buffer applications pass a memory
988         handle (or a list of memory handles for multi-planar formats) through
989         the <parameter>drm_mode_fb_cmd2</parameter> argument. For drivers using
990         GEM as their userspace buffer management interface this would be a GEM
991         handle.  Drivers are however free to use their own backing storage object
992         handles, e.g. vmwgfx directly exposes special TTM handles to userspace
993         and so expects TTM handles in the create ioctl and not GEM handles.
994       </para>
995       <para>
996         Drivers must first validate the requested frame buffer parameters passed
997         through the mode_cmd argument. In particular this is where invalid
998         sizes, pixel formats or pitches can be caught.
999       </para>
1000       <para>
1001         If the parameters are deemed valid, drivers then create, initialize and
1002         return an instance of struct <structname>drm_framebuffer</structname>.
1003         If desired the instance can be embedded in a larger driver-specific
1004         structure. Drivers must fill its <structfield>width</structfield>,
1005         <structfield>height</structfield>, <structfield>pitches</structfield>,
1006         <structfield>offsets</structfield>, <structfield>depth</structfield>,
1007         <structfield>bits_per_pixel</structfield> and
1008         <structfield>pixel_format</structfield> fields from the values passed
1009         through the <parameter>drm_mode_fb_cmd2</parameter> argument. They
1010         should call the <function>drm_helper_mode_fill_fb_struct</function>
1011         helper function to do so.
1012       </para>
1013
1014       <para>
1015         The initialization of the new framebuffer instance is finalized with a
1016         call to <function>drm_framebuffer_init</function> which takes a pointer
1017         to DRM frame buffer operations (struct
1018         <structname>drm_framebuffer_funcs</structname>). Note that this function
1019         publishes the framebuffer and so from this point on it can be accessed
1020         concurrently from other threads. Hence it must be the last step in the
1021         driver's framebuffer initialization sequence. Frame buffer operations
1022         are
1023         <itemizedlist>
1024           <listitem>
1025             <synopsis>int (*create_handle)(struct drm_framebuffer *fb,
1026                      struct drm_file *file_priv, unsigned int *handle);</synopsis>
1027             <para>
1028               Create a handle to the frame buffer underlying memory object. If
1029               the frame buffer uses a multi-plane format, the handle will
1030               reference the memory object associated with the first plane.
1031             </para>
1032             <para>
1033               Drivers call <function>drm_gem_handle_create</function> to create
1034               the handle.
1035             </para>
1036           </listitem>
1037           <listitem>
1038             <synopsis>void (*destroy)(struct drm_framebuffer *framebuffer);</synopsis>
1039             <para>
1040               Destroy the frame buffer object and frees all associated
1041               resources. Drivers must call
1042               <function>drm_framebuffer_cleanup</function> to free resources
1043               allocated by the DRM core for the frame buffer object, and must
1044               make sure to unreference all memory objects associated with the
1045               frame buffer. Handles created by the
1046               <methodname>create_handle</methodname> operation are released by
1047               the DRM core.
1048             </para>
1049           </listitem>
1050           <listitem>
1051             <synopsis>int (*dirty)(struct drm_framebuffer *framebuffer,
1052              struct drm_file *file_priv, unsigned flags, unsigned color,
1053              struct drm_clip_rect *clips, unsigned num_clips);</synopsis>
1054             <para>
1055               This optional operation notifies the driver that a region of the
1056               frame buffer has changed in response to a DRM_IOCTL_MODE_DIRTYFB
1057               ioctl call.
1058             </para>
1059           </listitem>
1060         </itemizedlist>
1061       </para>
1062       <para>
1063         The lifetime of a drm framebuffer is controlled with a reference count,
1064         drivers can grab additional references with
1065         <function>drm_framebuffer_reference</function>and drop them
1066         again with <function>drm_framebuffer_unreference</function>. For
1067         driver-private framebuffers for which the last reference is never
1068         dropped (e.g. for the fbdev framebuffer when the struct
1069         <structname>drm_framebuffer</structname> is embedded into the fbdev
1070         helper struct) drivers can manually clean up a framebuffer at module
1071         unload time with
1072         <function>drm_framebuffer_unregister_private</function>.
1073       </para>
1074     </sect2>
1075     <sect2>
1076       <title>Dumb Buffer Objects</title>
1077       <para>
1078         The KMS API doesn't standardize backing storage object creation and
1079         leaves it to driver-specific ioctls. Furthermore actually creating a
1080         buffer object even for GEM-based drivers is done through a
1081         driver-specific ioctl - GEM only has a common userspace interface for
1082         sharing and destroying objects. While not an issue for full-fledged
1083         graphics stacks that include device-specific userspace components (in
1084         libdrm for instance), this limit makes DRM-based early boot graphics
1085         unnecessarily complex.
1086       </para>
1087       <para>
1088         Dumb objects partly alleviate the problem by providing a standard
1089         API to create dumb buffers suitable for scanout, which can then be used
1090         to create KMS frame buffers.
1091       </para>
1092       <para>
1093         To support dumb objects drivers must implement the
1094         <methodname>dumb_create</methodname>,
1095         <methodname>dumb_destroy</methodname> and
1096         <methodname>dumb_map_offset</methodname> operations.
1097       </para>
1098       <itemizedlist>
1099         <listitem>
1100           <synopsis>int (*dumb_create)(struct drm_file *file_priv, struct drm_device *dev,
1101                    struct drm_mode_create_dumb *args);</synopsis>
1102           <para>
1103             The <methodname>dumb_create</methodname> operation creates a driver
1104             object (GEM or TTM handle) suitable for scanout based on the
1105             width, height and depth from the struct
1106             <structname>drm_mode_create_dumb</structname> argument. It fills the
1107             argument's <structfield>handle</structfield>,
1108             <structfield>pitch</structfield> and <structfield>size</structfield>
1109             fields with a handle for the newly created object and its line
1110             pitch and size in bytes.
1111           </para>
1112         </listitem>
1113         <listitem>
1114           <synopsis>int (*dumb_destroy)(struct drm_file *file_priv, struct drm_device *dev,
1115                     uint32_t handle);</synopsis>
1116           <para>
1117             The <methodname>dumb_destroy</methodname> operation destroys a dumb
1118             object created by <methodname>dumb_create</methodname>.
1119           </para>
1120         </listitem>
1121         <listitem>
1122           <synopsis>int (*dumb_map_offset)(struct drm_file *file_priv, struct drm_device *dev,
1123                        uint32_t handle, uint64_t *offset);</synopsis>
1124           <para>
1125             The <methodname>dumb_map_offset</methodname> operation associates an
1126             mmap fake offset with the object given by the handle and returns
1127             it. Drivers must use the
1128             <function>drm_gem_create_mmap_offset</function> function to
1129             associate the fake offset as described in
1130             <xref linkend="drm-gem-objects-mapping"/>.
1131           </para>
1132         </listitem>
1133       </itemizedlist>
1134       <para>
1135         Note that dumb objects may not be used for gpu acceleration, as has been
1136         attempted on some ARM embedded platforms. Such drivers really must have
1137         a hardware-specific ioctl to allocate suitable buffer objects.
1138       </para>
1139     </sect2>
1140     <sect2>
1141       <title>Output Polling</title>
1142       <synopsis>void (*output_poll_changed)(struct drm_device *dev);</synopsis>
1143       <para>
1144         This operation notifies the driver that the status of one or more
1145         connectors has changed. Drivers that use the fb helper can just call the
1146         <function>drm_fb_helper_hotplug_event</function> function to handle this
1147         operation.
1148       </para>
1149     </sect2>
1150     <sect2>
1151       <title>Locking</title>
1152       <para>
1153         Beside some lookup structures with their own locking (which is hidden
1154         behind the interface functions) most of the modeset state is protected
1155         by the <code>dev-&lt;mode_config.lock</code> mutex and additionally
1156         per-crtc locks to allow cursor updates, pageflips and similar operations
1157         to occur concurrently with background tasks like output detection.
1158         Operations which cross domains like a full modeset always grab all
1159         locks. Drivers there need to protect resources shared between crtcs with
1160         additional locking. They also need to be careful to always grab the
1161         relevant crtc locks if a modset functions touches crtc state, e.g. for
1162         load detection (which does only grab the <code>mode_config.lock</code>
1163         to allow concurrent screen updates on live crtcs).
1164       </para>
1165     </sect2>
1166   </sect1>
1167
1168   <!-- Internals: kms initialization and cleanup -->
1169
1170   <sect1 id="drm-kms-init">
1171     <title>KMS Initialization and Cleanup</title>
1172     <para>
1173       A KMS device is abstracted and exposed as a set of planes, CRTCs, encoders
1174       and connectors. KMS drivers must thus create and initialize all those
1175       objects at load time after initializing mode setting.
1176     </para>
1177     <sect2>
1178       <title>CRTCs (struct <structname>drm_crtc</structname>)</title>
1179       <para>
1180         A CRTC is an abstraction representing a part of the chip that contains a
1181         pointer to a scanout buffer. Therefore, the number of CRTCs available
1182         determines how many independent scanout buffers can be active at any
1183         given time. The CRTC structure contains several fields to support this:
1184         a pointer to some video memory (abstracted as a frame buffer object), a
1185         display mode, and an (x, y) offset into the video memory to support
1186         panning or configurations where one piece of video memory spans multiple
1187         CRTCs.
1188       </para>
1189       <sect3>
1190         <title>CRTC Initialization</title>
1191         <para>
1192           A KMS device must create and register at least one struct
1193           <structname>drm_crtc</structname> instance. The instance is allocated
1194           and zeroed by the driver, possibly as part of a larger structure, and
1195           registered with a call to <function>drm_crtc_init</function> with a
1196           pointer to CRTC functions.
1197         </para>
1198       </sect3>
1199       <sect3 id="drm-kms-crtcops">
1200         <title>CRTC Operations</title>
1201         <sect4>
1202           <title>Set Configuration</title>
1203           <synopsis>int (*set_config)(struct drm_mode_set *set);</synopsis>
1204           <para>
1205             Apply a new CRTC configuration to the device. The configuration
1206             specifies a CRTC, a frame buffer to scan out from, a (x,y) position in
1207             the frame buffer, a display mode and an array of connectors to drive
1208             with the CRTC if possible.
1209           </para>
1210           <para>
1211             If the frame buffer specified in the configuration is NULL, the driver
1212             must detach all encoders connected to the CRTC and all connectors
1213             attached to those encoders and disable them.
1214           </para>
1215           <para>
1216             This operation is called with the mode config lock held.
1217           </para>
1218           <note><para>
1219             Note that the drm core has no notion of restoring the mode setting
1220             state after resume, since all resume handling is in the full
1221             responsibility of the driver. The common mode setting helper library
1222             though provides a helper which can be used for this:
1223             <function>drm_helper_resume_force_mode</function>.
1224           </para></note>
1225         </sect4>
1226         <sect4>
1227           <title>Page Flipping</title>
1228           <synopsis>int (*page_flip)(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1229                    struct drm_pending_vblank_event *event);</synopsis>
1230           <para>
1231             Schedule a page flip to the given frame buffer for the CRTC. This
1232             operation is called with the mode config mutex held.
1233           </para>
1234           <para>
1235             Page flipping is a synchronization mechanism that replaces the frame
1236             buffer being scanned out by the CRTC with a new frame buffer during
1237             vertical blanking, avoiding tearing. When an application requests a page
1238             flip the DRM core verifies that the new frame buffer is large enough to
1239             be scanned out by  the CRTC in the currently configured mode and then
1240             calls the CRTC <methodname>page_flip</methodname> operation with a
1241             pointer to the new frame buffer.
1242           </para>
1243           <para>
1244             The <methodname>page_flip</methodname> operation schedules a page flip.
1245             Once any pending rendering targeting the new frame buffer has
1246             completed, the CRTC will be reprogrammed to display that frame buffer
1247             after the next vertical refresh. The operation must return immediately
1248             without waiting for rendering or page flip to complete and must block
1249             any new rendering to the frame buffer until the page flip completes.
1250           </para>
1251           <para>
1252             If a page flip can be successfully scheduled the driver must set the
1253             <code>drm_crtc-&lt;fb</code> field to the new framebuffer pointed to
1254             by <code>fb</code>. This is important so that the reference counting
1255             on framebuffers stays balanced.
1256           </para>
1257           <para>
1258             If a page flip is already pending, the
1259             <methodname>page_flip</methodname> operation must return
1260             -<errorname>EBUSY</errorname>.
1261           </para>
1262           <para>
1263             To synchronize page flip to vertical blanking the driver will likely
1264             need to enable vertical blanking interrupts. It should call
1265             <function>drm_vblank_get</function> for that purpose, and call
1266             <function>drm_vblank_put</function> after the page flip completes.
1267           </para>
1268           <para>
1269             If the application has requested to be notified when page flip completes
1270             the <methodname>page_flip</methodname> operation will be called with a
1271             non-NULL <parameter>event</parameter> argument pointing to a
1272             <structname>drm_pending_vblank_event</structname> instance. Upon page
1273             flip completion the driver must call <methodname>drm_send_vblank_event</methodname>
1274             to fill in the event and send to wake up any waiting processes.
1275             This can be performed with
1276             <programlisting><![CDATA[
1277             spin_lock_irqsave(&dev->event_lock, flags);
1278             ...
1279             drm_send_vblank_event(dev, pipe, event);
1280             spin_unlock_irqrestore(&dev->event_lock, flags);
1281             ]]></programlisting>
1282           </para>
1283           <note><para>
1284             FIXME: Could drivers that don't need to wait for rendering to complete
1285             just add the event to <literal>dev-&gt;vblank_event_list</literal> and
1286             let the DRM core handle everything, as for "normal" vertical blanking
1287             events?
1288           </para></note>
1289           <para>
1290             While waiting for the page flip to complete, the
1291             <literal>event-&gt;base.link</literal> list head can be used freely by
1292             the driver to store the pending event in a driver-specific list.
1293           </para>
1294           <para>
1295             If the file handle is closed before the event is signaled, drivers must
1296             take care to destroy the event in their
1297             <methodname>preclose</methodname> operation (and, if needed, call
1298             <function>drm_vblank_put</function>).
1299           </para>
1300         </sect4>
1301         <sect4>
1302           <title>Miscellaneous</title>
1303           <itemizedlist>
1304             <listitem>
1305               <synopsis>void (*set_property)(struct drm_crtc *crtc,
1306                      struct drm_property *property, uint64_t value);</synopsis>
1307               <para>
1308                 Set the value of the given CRTC property to
1309                 <parameter>value</parameter>. See <xref linkend="drm-kms-properties"/>
1310                 for more information about properties.
1311               </para>
1312             </listitem>
1313             <listitem>
1314               <synopsis>void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
1315                         uint32_t start, uint32_t size);</synopsis>
1316               <para>
1317                 Apply a gamma table to the device. The operation is optional.
1318               </para>
1319             </listitem>
1320             <listitem>
1321               <synopsis>void (*destroy)(struct drm_crtc *crtc);</synopsis>
1322               <para>
1323                 Destroy the CRTC when not needed anymore. See
1324                 <xref linkend="drm-kms-init"/>.
1325               </para>
1326             </listitem>
1327           </itemizedlist>
1328         </sect4>
1329       </sect3>
1330     </sect2>
1331     <sect2>
1332       <title>Planes (struct <structname>drm_plane</structname>)</title>
1333       <para>
1334         A plane represents an image source that can be blended with or overlayed
1335         on top of a CRTC during the scanout process. Planes are associated with
1336         a frame buffer to crop a portion of the image memory (source) and
1337         optionally scale it to a destination size. The result is then blended
1338         with or overlayed on top of a CRTC.
1339       </para>
1340       <para>
1341       The DRM core recognizes three types of planes:
1342       <itemizedlist>
1343         <listitem>
1344         DRM_PLANE_TYPE_PRIMARY represents a "main" plane for a CRTC.  Primary
1345         planes are the planes operated upon by by CRTC modesetting and flipping
1346         operations described in <xref linkend="drm-kms-crtcops"/>.
1347         </listitem>
1348         <listitem>
1349         DRM_PLANE_TYPE_CURSOR represents a "cursor" plane for a CRTC.  Cursor
1350         planes are the planes operated upon by the DRM_IOCTL_MODE_CURSOR and
1351         DRM_IOCTL_MODE_CURSOR2 ioctls.
1352         </listitem>
1353         <listitem>
1354         DRM_PLANE_TYPE_OVERLAY represents all non-primary, non-cursor planes.
1355         Some drivers refer to these types of planes as "sprites" internally.
1356         </listitem>
1357       </itemizedlist>
1358       For compatibility with legacy userspace, only overlay planes are made
1359       available to userspace by default.  Userspace clients may set the
1360       DRM_CLIENT_CAP_UNIVERSAL_PLANES client capability bit to indicate that
1361       they wish to receive a universal plane list containing all plane types.
1362       </para>
1363       <sect3>
1364         <title>Plane Initialization</title>
1365         <para>
1366           To create a plane, a KMS drivers allocates and
1367           zeroes an instances of struct <structname>drm_plane</structname>
1368           (possibly as part of a larger structure) and registers it with a call
1369           to <function>drm_universal_plane_init</function>. The function takes a bitmask
1370           of the CRTCs that can be associated with the plane, a pointer to the
1371           plane functions, a list of format supported formats, and the type of
1372           plane (primary, cursor, or overlay) being initialized.
1373         </para>
1374         <para>
1375           Cursor and overlay planes are optional.  All drivers should provide
1376           one primary plane per CRTC (although this requirement may change in
1377           the future); drivers that do not wish to provide special handling for
1378           primary planes may make use of the helper functions described in
1379           <xref linkend="drm-kms-planehelpers"/> to create and register a
1380           primary plane with standard capabilities.
1381         </para>
1382       </sect3>
1383       <sect3>
1384         <title>Plane Operations</title>
1385         <itemizedlist>
1386           <listitem>
1387             <synopsis>int (*update_plane)(struct drm_plane *plane, struct drm_crtc *crtc,
1388                         struct drm_framebuffer *fb, int crtc_x, int crtc_y,
1389                         unsigned int crtc_w, unsigned int crtc_h,
1390                         uint32_t src_x, uint32_t src_y,
1391                         uint32_t src_w, uint32_t src_h);</synopsis>
1392             <para>
1393               Enable and configure the plane to use the given CRTC and frame buffer.
1394             </para>
1395             <para>
1396               The source rectangle in frame buffer memory coordinates is given by
1397               the <parameter>src_x</parameter>, <parameter>src_y</parameter>,
1398               <parameter>src_w</parameter> and <parameter>src_h</parameter>
1399               parameters (as 16.16 fixed point values). Devices that don't support
1400               subpixel plane coordinates can ignore the fractional part.
1401             </para>
1402             <para>
1403               The destination rectangle in CRTC coordinates is given by the
1404               <parameter>crtc_x</parameter>, <parameter>crtc_y</parameter>,
1405               <parameter>crtc_w</parameter> and <parameter>crtc_h</parameter>
1406               parameters (as integer values). Devices scale the source rectangle to
1407               the destination rectangle. If scaling is not supported, and the source
1408               rectangle size doesn't match the destination rectangle size, the
1409               driver must return a -<errorname>EINVAL</errorname> error.
1410             </para>
1411           </listitem>
1412           <listitem>
1413             <synopsis>int (*disable_plane)(struct drm_plane *plane);</synopsis>
1414             <para>
1415               Disable the plane. The DRM core calls this method in response to a
1416               DRM_IOCTL_MODE_SETPLANE ioctl call with the frame buffer ID set to 0.
1417               Disabled planes must not be processed by the CRTC.
1418             </para>
1419           </listitem>
1420           <listitem>
1421             <synopsis>void (*destroy)(struct drm_plane *plane);</synopsis>
1422             <para>
1423               Destroy the plane when not needed anymore. See
1424               <xref linkend="drm-kms-init"/>.
1425             </para>
1426           </listitem>
1427         </itemizedlist>
1428       </sect3>
1429     </sect2>
1430     <sect2>
1431       <title>Encoders (struct <structname>drm_encoder</structname>)</title>
1432       <para>
1433         An encoder takes pixel data from a CRTC and converts it to a format
1434         suitable for any attached connectors. On some devices, it may be
1435         possible to have a CRTC send data to more than one encoder. In that
1436         case, both encoders would receive data from the same scanout buffer,
1437         resulting in a "cloned" display configuration across the connectors
1438         attached to each encoder.
1439       </para>
1440       <sect3>
1441         <title>Encoder Initialization</title>
1442         <para>
1443           As for CRTCs, a KMS driver must create, initialize and register at
1444           least one struct <structname>drm_encoder</structname> instance. The
1445           instance is allocated and zeroed by the driver, possibly as part of a
1446           larger structure.
1447         </para>
1448         <para>
1449           Drivers must initialize the struct <structname>drm_encoder</structname>
1450           <structfield>possible_crtcs</structfield> and
1451           <structfield>possible_clones</structfield> fields before registering the
1452           encoder. Both fields are bitmasks of respectively the CRTCs that the
1453           encoder can be connected to, and sibling encoders candidate for cloning.
1454         </para>
1455         <para>
1456           After being initialized, the encoder must be registered with a call to
1457           <function>drm_encoder_init</function>. The function takes a pointer to
1458           the encoder functions and an encoder type. Supported types are
1459           <itemizedlist>
1460             <listitem>
1461               DRM_MODE_ENCODER_DAC for VGA and analog on DVI-I/DVI-A
1462               </listitem>
1463             <listitem>
1464               DRM_MODE_ENCODER_TMDS for DVI, HDMI and (embedded) DisplayPort
1465             </listitem>
1466             <listitem>
1467               DRM_MODE_ENCODER_LVDS for display panels
1468             </listitem>
1469             <listitem>
1470               DRM_MODE_ENCODER_TVDAC for TV output (Composite, S-Video, Component,
1471               SCART)
1472             </listitem>
1473             <listitem>
1474               DRM_MODE_ENCODER_VIRTUAL for virtual machine displays
1475             </listitem>
1476           </itemizedlist>
1477         </para>
1478         <para>
1479           Encoders must be attached to a CRTC to be used. DRM drivers leave
1480           encoders unattached at initialization time. Applications (or the fbdev
1481           compatibility layer when implemented) are responsible for attaching the
1482           encoders they want to use to a CRTC.
1483         </para>
1484       </sect3>
1485       <sect3>
1486         <title>Encoder Operations</title>
1487         <itemizedlist>
1488           <listitem>
1489             <synopsis>void (*destroy)(struct drm_encoder *encoder);</synopsis>
1490             <para>
1491               Called to destroy the encoder when not needed anymore. See
1492               <xref linkend="drm-kms-init"/>.
1493             </para>
1494           </listitem>
1495           <listitem>
1496             <synopsis>void (*set_property)(struct drm_plane *plane,
1497                      struct drm_property *property, uint64_t value);</synopsis>
1498             <para>
1499               Set the value of the given plane property to
1500               <parameter>value</parameter>. See <xref linkend="drm-kms-properties"/>
1501               for more information about properties.
1502             </para>
1503           </listitem>
1504         </itemizedlist>
1505       </sect3>
1506     </sect2>
1507     <sect2>
1508       <title>Connectors (struct <structname>drm_connector</structname>)</title>
1509       <para>
1510         A connector is the final destination for pixel data on a device, and
1511         usually connects directly to an external display device like a monitor
1512         or laptop panel. A connector can only be attached to one encoder at a
1513         time. The connector is also the structure where information about the
1514         attached display is kept, so it contains fields for display data, EDID
1515         data, DPMS &amp; connection status, and information about modes
1516         supported on the attached displays.
1517       </para>
1518       <sect3>
1519         <title>Connector Initialization</title>
1520         <para>
1521           Finally a KMS driver must create, initialize, register and attach at
1522           least one struct <structname>drm_connector</structname> instance. The
1523           instance is created as other KMS objects and initialized by setting the
1524           following fields.
1525         </para>
1526         <variablelist>
1527           <varlistentry>
1528             <term><structfield>interlace_allowed</structfield></term>
1529             <listitem><para>
1530               Whether the connector can handle interlaced modes.
1531             </para></listitem>
1532           </varlistentry>
1533           <varlistentry>
1534             <term><structfield>doublescan_allowed</structfield></term>
1535             <listitem><para>
1536               Whether the connector can handle doublescan.
1537             </para></listitem>
1538           </varlistentry>
1539           <varlistentry>
1540             <term><structfield>display_info
1541             </structfield></term>
1542             <listitem><para>
1543               Display information is filled from EDID information when a display
1544               is detected. For non hot-pluggable displays such as flat panels in
1545               embedded systems, the driver should initialize the
1546               <structfield>display_info</structfield>.<structfield>width_mm</structfield>
1547               and
1548               <structfield>display_info</structfield>.<structfield>height_mm</structfield>
1549               fields with the physical size of the display.
1550             </para></listitem>
1551           </varlistentry>
1552           <varlistentry>
1553             <term id="drm-kms-connector-polled"><structfield>polled</structfield></term>
1554             <listitem><para>
1555               Connector polling mode, a combination of
1556               <variablelist>
1557                 <varlistentry>
1558                   <term>DRM_CONNECTOR_POLL_HPD</term>
1559                   <listitem><para>
1560                     The connector generates hotplug events and doesn't need to be
1561                     periodically polled. The CONNECT and DISCONNECT flags must not
1562                     be set together with the HPD flag.
1563                   </para></listitem>
1564                 </varlistentry>
1565                 <varlistentry>
1566                   <term>DRM_CONNECTOR_POLL_CONNECT</term>
1567                   <listitem><para>
1568                     Periodically poll the connector for connection.
1569                   </para></listitem>
1570                 </varlistentry>
1571                 <varlistentry>
1572                   <term>DRM_CONNECTOR_POLL_DISCONNECT</term>
1573                   <listitem><para>
1574                     Periodically poll the connector for disconnection.
1575                   </para></listitem>
1576                 </varlistentry>
1577               </variablelist>
1578               Set to 0 for connectors that don't support connection status
1579               discovery.
1580             </para></listitem>
1581           </varlistentry>
1582         </variablelist>
1583         <para>
1584           The connector is then registered with a call to
1585           <function>drm_connector_init</function> with a pointer to the connector
1586           functions and a connector type, and exposed through sysfs with a call to
1587           <function>drm_sysfs_connector_add</function>.
1588         </para>
1589         <para>
1590           Supported connector types are
1591           <itemizedlist>
1592             <listitem>DRM_MODE_CONNECTOR_VGA</listitem>
1593             <listitem>DRM_MODE_CONNECTOR_DVII</listitem>
1594             <listitem>DRM_MODE_CONNECTOR_DVID</listitem>
1595             <listitem>DRM_MODE_CONNECTOR_DVIA</listitem>
1596             <listitem>DRM_MODE_CONNECTOR_Composite</listitem>
1597             <listitem>DRM_MODE_CONNECTOR_SVIDEO</listitem>
1598             <listitem>DRM_MODE_CONNECTOR_LVDS</listitem>
1599             <listitem>DRM_MODE_CONNECTOR_Component</listitem>
1600             <listitem>DRM_MODE_CONNECTOR_9PinDIN</listitem>
1601             <listitem>DRM_MODE_CONNECTOR_DisplayPort</listitem>
1602             <listitem>DRM_MODE_CONNECTOR_HDMIA</listitem>
1603             <listitem>DRM_MODE_CONNECTOR_HDMIB</listitem>
1604             <listitem>DRM_MODE_CONNECTOR_TV</listitem>
1605             <listitem>DRM_MODE_CONNECTOR_eDP</listitem>
1606             <listitem>DRM_MODE_CONNECTOR_VIRTUAL</listitem>
1607           </itemizedlist>
1608         </para>
1609         <para>
1610           Connectors must be attached to an encoder to be used. For devices that
1611           map connectors to encoders 1:1, the connector should be attached at
1612           initialization time with a call to
1613           <function>drm_mode_connector_attach_encoder</function>. The driver must
1614           also set the <structname>drm_connector</structname>
1615           <structfield>encoder</structfield> field to point to the attached
1616           encoder.
1617         </para>
1618         <para>
1619           Finally, drivers must initialize the connectors state change detection
1620           with a call to <function>drm_kms_helper_poll_init</function>. If at
1621           least one connector is pollable but can't generate hotplug interrupts
1622           (indicated by the DRM_CONNECTOR_POLL_CONNECT and
1623           DRM_CONNECTOR_POLL_DISCONNECT connector flags), a delayed work will
1624           automatically be queued to periodically poll for changes. Connectors
1625           that can generate hotplug interrupts must be marked with the
1626           DRM_CONNECTOR_POLL_HPD flag instead, and their interrupt handler must
1627           call <function>drm_helper_hpd_irq_event</function>. The function will
1628           queue a delayed work to check the state of all connectors, but no
1629           periodic polling will be done.
1630         </para>
1631       </sect3>
1632       <sect3>
1633         <title>Connector Operations</title>
1634         <note><para>
1635           Unless otherwise state, all operations are mandatory.
1636         </para></note>
1637         <sect4>
1638           <title>DPMS</title>
1639           <synopsis>void (*dpms)(struct drm_connector *connector, int mode);</synopsis>
1640           <para>
1641             The DPMS operation sets the power state of a connector. The mode
1642             argument is one of
1643             <itemizedlist>
1644               <listitem><para>DRM_MODE_DPMS_ON</para></listitem>
1645               <listitem><para>DRM_MODE_DPMS_STANDBY</para></listitem>
1646               <listitem><para>DRM_MODE_DPMS_SUSPEND</para></listitem>
1647               <listitem><para>DRM_MODE_DPMS_OFF</para></listitem>
1648             </itemizedlist>
1649           </para>
1650           <para>
1651             In all but DPMS_ON mode the encoder to which the connector is attached
1652             should put the display in low-power mode by driving its signals
1653             appropriately. If more than one connector is attached to the encoder
1654             care should be taken not to change the power state of other displays as
1655             a side effect. Low-power mode should be propagated to the encoders and
1656             CRTCs when all related connectors are put in low-power mode.
1657           </para>
1658         </sect4>
1659         <sect4>
1660           <title>Modes</title>
1661           <synopsis>int (*fill_modes)(struct drm_connector *connector, uint32_t max_width,
1662                       uint32_t max_height);</synopsis>
1663           <para>
1664             Fill the mode list with all supported modes for the connector. If the
1665             <parameter>max_width</parameter> and <parameter>max_height</parameter>
1666             arguments are non-zero, the implementation must ignore all modes wider
1667             than <parameter>max_width</parameter> or higher than
1668             <parameter>max_height</parameter>.
1669           </para>
1670           <para>
1671             The connector must also fill in this operation its
1672             <structfield>display_info</structfield>
1673             <structfield>width_mm</structfield> and
1674             <structfield>height_mm</structfield> fields with the connected display
1675             physical size in millimeters. The fields should be set to 0 if the value
1676             isn't known or is not applicable (for instance for projector devices).
1677           </para>
1678         </sect4>
1679         <sect4>
1680           <title>Connection Status</title>
1681           <para>
1682             The connection status is updated through polling or hotplug events when
1683             supported (see <xref linkend="drm-kms-connector-polled"/>). The status
1684             value is reported to userspace through ioctls and must not be used
1685             inside the driver, as it only gets initialized by a call to
1686             <function>drm_mode_getconnector</function> from userspace.
1687           </para>
1688           <synopsis>enum drm_connector_status (*detect)(struct drm_connector *connector,
1689                                         bool force);</synopsis>
1690           <para>
1691             Check to see if anything is attached to the connector. The
1692             <parameter>force</parameter> parameter is set to false whilst polling or
1693             to true when checking the connector due to user request.
1694             <parameter>force</parameter> can be used by the driver to avoid
1695             expensive, destructive operations during automated probing.
1696           </para>
1697           <para>
1698             Return connector_status_connected if something is connected to the
1699             connector, connector_status_disconnected if nothing is connected and
1700             connector_status_unknown if the connection state isn't known.
1701           </para>
1702           <para>
1703             Drivers should only return connector_status_connected if the connection
1704             status has really been probed as connected. Connectors that can't detect
1705             the connection status, or failed connection status probes, should return
1706             connector_status_unknown.
1707           </para>
1708         </sect4>
1709         <sect4>
1710           <title>Miscellaneous</title>
1711           <itemizedlist>
1712             <listitem>
1713               <synopsis>void (*set_property)(struct drm_connector *connector,
1714                      struct drm_property *property, uint64_t value);</synopsis>
1715               <para>
1716                 Set the value of the given connector property to
1717                 <parameter>value</parameter>. See <xref linkend="drm-kms-properties"/>
1718                 for more information about properties.
1719               </para>
1720             </listitem>
1721             <listitem>
1722               <synopsis>void (*destroy)(struct drm_connector *connector);</synopsis>
1723               <para>
1724                 Destroy the connector when not needed anymore. See
1725                 <xref linkend="drm-kms-init"/>.
1726               </para>
1727             </listitem>
1728           </itemizedlist>
1729         </sect4>
1730       </sect3>
1731     </sect2>
1732     <sect2>
1733       <title>Cleanup</title>
1734       <para>
1735         The DRM core manages its objects' lifetime. When an object is not needed
1736         anymore the core calls its destroy function, which must clean up and
1737         free every resource allocated for the object. Every
1738         <function>drm_*_init</function> call must be matched with a
1739         corresponding <function>drm_*_cleanup</function> call to cleanup CRTCs
1740         (<function>drm_crtc_cleanup</function>), planes
1741         (<function>drm_plane_cleanup</function>), encoders
1742         (<function>drm_encoder_cleanup</function>) and connectors
1743         (<function>drm_connector_cleanup</function>). Furthermore, connectors
1744         that have been added to sysfs must be removed by a call to
1745         <function>drm_sysfs_connector_remove</function> before calling
1746         <function>drm_connector_cleanup</function>.
1747       </para>
1748       <para>
1749         Connectors state change detection must be cleanup up with a call to
1750         <function>drm_kms_helper_poll_fini</function>.
1751       </para>
1752     </sect2>
1753     <sect2>
1754       <title>Output discovery and initialization example</title>
1755       <programlisting><![CDATA[
1756 void intel_crt_init(struct drm_device *dev)
1757 {
1758         struct drm_connector *connector;
1759         struct intel_output *intel_output;
1760
1761         intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
1762         if (!intel_output)
1763                 return;
1764
1765         connector = &intel_output->base;
1766         drm_connector_init(dev, &intel_output->base,
1767                            &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1768
1769         drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
1770                          DRM_MODE_ENCODER_DAC);
1771
1772         drm_mode_connector_attach_encoder(&intel_output->base,
1773                                           &intel_output->enc);
1774
1775         /* Set up the DDC bus. */
1776         intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
1777         if (!intel_output->ddc_bus) {
1778                 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
1779                            "failed.\n");
1780                 return;
1781         }
1782
1783         intel_output->type = INTEL_OUTPUT_ANALOG;
1784         connector->interlace_allowed = 0;
1785         connector->doublescan_allowed = 0;
1786
1787         drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
1788         drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
1789
1790         drm_sysfs_connector_add(connector);
1791 }]]></programlisting>
1792       <para>
1793         In the example above (taken from the i915 driver), a CRTC, connector and
1794         encoder combination is created. A device-specific i2c bus is also
1795         created for fetching EDID data and performing monitor detection. Once
1796         the process is complete, the new connector is registered with sysfs to
1797         make its properties available to applications.
1798       </para>
1799     </sect2>
1800     <sect2>
1801       <title>KMS API Functions</title>
1802 !Edrivers/gpu/drm/drm_crtc.c
1803     </sect2>
1804     <sect2>
1805       <title>KMS Locking</title>
1806 !Pdrivers/gpu/drm/drm_modeset_lock.c kms locking
1807 !Iinclude/drm/drm_modeset_lock.h
1808 !Edrivers/gpu/drm/drm_modeset_lock.c
1809     </sect2>
1810   </sect1>
1811
1812   <!-- Internals: kms helper functions -->
1813
1814   <sect1>
1815     <title>Mode Setting Helper Functions</title>
1816     <para>
1817       The plane, CRTC, encoder and connector functions provided by the drivers
1818       implement the DRM API. They're called by the DRM core and ioctl handlers
1819       to handle device state changes and configuration request. As implementing
1820       those functions often requires logic not specific to drivers, mid-layer
1821       helper functions are available to avoid duplicating boilerplate code.
1822     </para>
1823     <para>
1824       The DRM core contains one mid-layer implementation. The mid-layer provides
1825       implementations of several plane, CRTC, encoder and connector functions
1826       (called from the top of the mid-layer) that pre-process requests and call
1827       lower-level functions provided by the driver (at the bottom of the
1828       mid-layer). For instance, the
1829       <function>drm_crtc_helper_set_config</function> function can be used to
1830       fill the struct <structname>drm_crtc_funcs</structname>
1831       <structfield>set_config</structfield> field. When called, it will split
1832       the <methodname>set_config</methodname> operation in smaller, simpler
1833       operations and call the driver to handle them.
1834     </para>
1835     <para>
1836       To use the mid-layer, drivers call <function>drm_crtc_helper_add</function>,
1837       <function>drm_encoder_helper_add</function> and
1838       <function>drm_connector_helper_add</function> functions to install their
1839       mid-layer bottom operations handlers, and fill the
1840       <structname>drm_crtc_funcs</structname>,
1841       <structname>drm_encoder_funcs</structname> and
1842       <structname>drm_connector_funcs</structname> structures with pointers to
1843       the mid-layer top API functions. Installing the mid-layer bottom operation
1844       handlers is best done right after registering the corresponding KMS object.
1845     </para>
1846     <para>
1847       The mid-layer is not split between CRTC, encoder and connector operations.
1848       To use it, a driver must provide bottom functions for all of the three KMS
1849       entities.
1850     </para>
1851     <sect2>
1852       <title>Helper Functions</title>
1853       <itemizedlist>
1854         <listitem>
1855           <synopsis>int drm_crtc_helper_set_config(struct drm_mode_set *set);</synopsis>
1856           <para>
1857             The <function>drm_crtc_helper_set_config</function> helper function
1858             is a CRTC <methodname>set_config</methodname> implementation. It
1859             first tries to locate the best encoder for each connector by calling
1860             the connector <methodname>best_encoder</methodname> helper
1861             operation.
1862           </para>
1863           <para>
1864             After locating the appropriate encoders, the helper function will
1865             call the <methodname>mode_fixup</methodname> encoder and CRTC helper
1866             operations to adjust the requested mode, or reject it completely in
1867             which case an error will be returned to the application. If the new
1868             configuration after mode adjustment is identical to the current
1869             configuration the helper function will return without performing any
1870             other operation.
1871           </para>
1872           <para>
1873             If the adjusted mode is identical to the current mode but changes to
1874             the frame buffer need to be applied, the
1875             <function>drm_crtc_helper_set_config</function> function will call
1876             the CRTC <methodname>mode_set_base</methodname> helper operation. If
1877             the adjusted mode differs from the current mode, or if the
1878             <methodname>mode_set_base</methodname> helper operation is not
1879             provided, the helper function performs a full mode set sequence by
1880             calling the <methodname>prepare</methodname>,
1881             <methodname>mode_set</methodname> and
1882             <methodname>commit</methodname> CRTC and encoder helper operations,
1883             in that order.
1884           </para>
1885         </listitem>
1886         <listitem>
1887           <synopsis>void drm_helper_connector_dpms(struct drm_connector *connector, int mode);</synopsis>
1888           <para>
1889             The <function>drm_helper_connector_dpms</function> helper function
1890             is a connector <methodname>dpms</methodname> implementation that
1891             tracks power state of connectors. To use the function, drivers must
1892             provide <methodname>dpms</methodname> helper operations for CRTCs
1893             and encoders to apply the DPMS state to the device.
1894           </para>
1895           <para>
1896             The mid-layer doesn't track the power state of CRTCs and encoders.
1897             The <methodname>dpms</methodname> helper operations can thus be
1898             called with a mode identical to the currently active mode.
1899           </para>
1900         </listitem>
1901         <listitem>
1902           <synopsis>int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
1903                                             uint32_t maxX, uint32_t maxY);</synopsis>
1904           <para>
1905             The <function>drm_helper_probe_single_connector_modes</function> helper
1906             function is a connector <methodname>fill_modes</methodname>
1907             implementation that updates the connection status for the connector
1908             and then retrieves a list of modes by calling the connector
1909             <methodname>get_modes</methodname> helper operation.
1910           </para>
1911           <para>
1912             The function filters out modes larger than
1913             <parameter>max_width</parameter> and <parameter>max_height</parameter>
1914             if specified. It then calls the optional connector
1915             <methodname>mode_valid</methodname> helper operation for each mode in
1916             the probed list to check whether the mode is valid for the connector.
1917           </para>
1918         </listitem>
1919       </itemizedlist>
1920     </sect2>
1921     <sect2>
1922       <title>CRTC Helper Operations</title>
1923       <itemizedlist>
1924         <listitem id="drm-helper-crtc-mode-fixup">
1925           <synopsis>bool (*mode_fixup)(struct drm_crtc *crtc,
1926                        const struct drm_display_mode *mode,
1927                        struct drm_display_mode *adjusted_mode);</synopsis>
1928           <para>
1929             Let CRTCs adjust the requested mode or reject it completely. This
1930             operation returns true if the mode is accepted (possibly after being
1931             adjusted) or false if it is rejected.
1932           </para>
1933           <para>
1934             The <methodname>mode_fixup</methodname> operation should reject the
1935             mode if it can't reasonably use it. The definition of "reasonable"
1936             is currently fuzzy in this context. One possible behaviour would be
1937             to set the adjusted mode to the panel timings when a fixed-mode
1938             panel is used with hardware capable of scaling. Another behaviour
1939             would be to accept any input mode and adjust it to the closest mode
1940             supported by the hardware (FIXME: This needs to be clarified).
1941           </para>
1942         </listitem>
1943         <listitem>
1944           <synopsis>int (*mode_set_base)(struct drm_crtc *crtc, int x, int y,
1945                      struct drm_framebuffer *old_fb)</synopsis>
1946           <para>
1947             Move the CRTC on the current frame buffer (stored in
1948             <literal>crtc-&gt;fb</literal>) to position (x,y). Any of the frame
1949             buffer, x position or y position may have been modified.
1950           </para>
1951           <para>
1952             This helper operation is optional. If not provided, the
1953             <function>drm_crtc_helper_set_config</function> function will fall
1954             back to the <methodname>mode_set</methodname> helper operation.
1955           </para>
1956           <note><para>
1957             FIXME: Why are x and y passed as arguments, as they can be accessed
1958             through <literal>crtc-&gt;x</literal> and
1959             <literal>crtc-&gt;y</literal>?
1960           </para></note>
1961         </listitem>
1962         <listitem>
1963           <synopsis>void (*prepare)(struct drm_crtc *crtc);</synopsis>
1964           <para>
1965             Prepare the CRTC for mode setting. This operation is called after
1966             validating the requested mode. Drivers use it to perform
1967             device-specific operations required before setting the new mode.
1968           </para>
1969         </listitem>
1970         <listitem>
1971           <synopsis>int (*mode_set)(struct drm_crtc *crtc, struct drm_display_mode *mode,
1972                 struct drm_display_mode *adjusted_mode, int x, int y,
1973                 struct drm_framebuffer *old_fb);</synopsis>
1974           <para>
1975             Set a new mode, position and frame buffer. Depending on the device
1976             requirements, the mode can be stored internally by the driver and
1977             applied in the <methodname>commit</methodname> operation, or
1978             programmed to the hardware immediately.
1979           </para>
1980           <para>
1981             The <methodname>mode_set</methodname> operation returns 0 on success
1982             or a negative error code if an error occurs.
1983           </para>
1984         </listitem>
1985         <listitem>
1986           <synopsis>void (*commit)(struct drm_crtc *crtc);</synopsis>
1987           <para>
1988             Commit a mode. This operation is called after setting the new mode.
1989             Upon return the device must use the new mode and be fully
1990             operational.
1991           </para>
1992         </listitem>
1993       </itemizedlist>
1994     </sect2>
1995     <sect2>
1996       <title>Encoder Helper Operations</title>
1997       <itemizedlist>
1998         <listitem>
1999           <synopsis>bool (*mode_fixup)(struct drm_encoder *encoder,
2000                        const struct drm_display_mode *mode,
2001                        struct drm_display_mode *adjusted_mode);</synopsis>
2002           <para>
2003             Let encoders adjust the requested mode or reject it completely. This
2004             operation returns true if the mode is accepted (possibly after being
2005             adjusted) or false if it is rejected. See the
2006             <link linkend="drm-helper-crtc-mode-fixup">mode_fixup CRTC helper
2007             operation</link> for an explanation of the allowed adjustments.
2008           </para>
2009         </listitem>
2010         <listitem>
2011           <synopsis>void (*prepare)(struct drm_encoder *encoder);</synopsis>
2012           <para>
2013             Prepare the encoder for mode setting. This operation is called after
2014             validating the requested mode. Drivers use it to perform
2015             device-specific operations required before setting the new mode.
2016           </para>
2017         </listitem>
2018         <listitem>
2019           <synopsis>void (*mode_set)(struct drm_encoder *encoder,
2020                  struct drm_display_mode *mode,
2021                  struct drm_display_mode *adjusted_mode);</synopsis>
2022           <para>
2023             Set a new mode. Depending on the device requirements, the mode can
2024             be stored internally by the driver and applied in the
2025             <methodname>commit</methodname> operation, or programmed to the
2026             hardware immediately.
2027           </para>
2028         </listitem>
2029         <listitem>
2030           <synopsis>void (*commit)(struct drm_encoder *encoder);</synopsis>
2031           <para>
2032             Commit a mode. This operation is called after setting the new mode.
2033             Upon return the device must use the new mode and be fully
2034             operational.
2035           </para>
2036         </listitem>
2037       </itemizedlist>
2038     </sect2>
2039     <sect2>
2040       <title>Connector Helper Operations</title>
2041       <itemizedlist>
2042         <listitem>
2043           <synopsis>struct drm_encoder *(*best_encoder)(struct drm_connector *connector);</synopsis>
2044           <para>
2045             Return a pointer to the best encoder for the connecter. Device that
2046             map connectors to encoders 1:1 simply return the pointer to the
2047             associated encoder. This operation is mandatory.
2048           </para>
2049         </listitem>
2050         <listitem>
2051           <synopsis>int (*get_modes)(struct drm_connector *connector);</synopsis>
2052           <para>
2053             Fill the connector's <structfield>probed_modes</structfield> list
2054             by parsing EDID data with <function>drm_add_edid_modes</function> or
2055             calling <function>drm_mode_probed_add</function> directly for every
2056             supported mode and return the number of modes it has detected. This
2057             operation is mandatory.
2058           </para>
2059           <para>
2060             When adding modes manually the driver creates each mode with a call to
2061             <function>drm_mode_create</function> and must fill the following fields.
2062             <itemizedlist>
2063               <listitem>
2064                 <synopsis>__u32 type;</synopsis>
2065                 <para>
2066                   Mode type bitmask, a combination of
2067                   <variablelist>
2068                     <varlistentry>
2069                       <term>DRM_MODE_TYPE_BUILTIN</term>
2070                       <listitem><para>not used?</para></listitem>
2071                     </varlistentry>
2072                     <varlistentry>
2073                       <term>DRM_MODE_TYPE_CLOCK_C</term>
2074                       <listitem><para>not used?</para></listitem>
2075                     </varlistentry>
2076                     <varlistentry>
2077                       <term>DRM_MODE_TYPE_CRTC_C</term>
2078                       <listitem><para>not used?</para></listitem>
2079                     </varlistentry>
2080                     <varlistentry>
2081                       <term>
2082         DRM_MODE_TYPE_PREFERRED - The preferred mode for the connector
2083                       </term>
2084                       <listitem>
2085                         <para>not used?</para>
2086                       </listitem>
2087                     </varlistentry>
2088                     <varlistentry>
2089                       <term>DRM_MODE_TYPE_DEFAULT</term>
2090                       <listitem><para>not used?</para></listitem>
2091                     </varlistentry>
2092                     <varlistentry>
2093                       <term>DRM_MODE_TYPE_USERDEF</term>
2094                       <listitem><para>not used?</para></listitem>
2095                     </varlistentry>
2096                     <varlistentry>
2097                       <term>DRM_MODE_TYPE_DRIVER</term>
2098                       <listitem>
2099                         <para>
2100                           The mode has been created by the driver (as opposed to
2101                           to user-created modes).
2102                         </para>
2103                       </listitem>
2104                     </varlistentry>
2105                   </variablelist>
2106                   Drivers must set the DRM_MODE_TYPE_DRIVER bit for all modes they
2107                   create, and set the DRM_MODE_TYPE_PREFERRED bit for the preferred
2108                   mode.
2109                 </para>
2110               </listitem>
2111               <listitem>
2112                 <synopsis>__u32 clock;</synopsis>
2113                 <para>Pixel clock frequency in kHz unit</para>
2114               </listitem>
2115               <listitem>
2116                 <synopsis>__u16 hdisplay, hsync_start, hsync_end, htotal;
2117     __u16 vdisplay, vsync_start, vsync_end, vtotal;</synopsis>
2118                 <para>Horizontal and vertical timing information</para>
2119                 <screen><![CDATA[
2120              Active                 Front           Sync           Back
2121              Region                 Porch                          Porch
2122     <-----------------------><----------------><-------------><-------------->
2123
2124       //////////////////////|
2125      ////////////////////// |
2126     //////////////////////  |..................               ................
2127                                                _______________
2128
2129     <----- [hv]display ----->
2130     <------------- [hv]sync_start ------------>
2131     <--------------------- [hv]sync_end --------------------->
2132     <-------------------------------- [hv]total ----------------------------->
2133 ]]></screen>
2134               </listitem>
2135               <listitem>
2136                 <synopsis>__u16 hskew;
2137     __u16 vscan;</synopsis>
2138                 <para>Unknown</para>
2139               </listitem>
2140               <listitem>
2141                 <synopsis>__u32 flags;</synopsis>
2142                 <para>
2143                   Mode flags, a combination of
2144                   <variablelist>
2145                     <varlistentry>
2146                       <term>DRM_MODE_FLAG_PHSYNC</term>
2147                       <listitem><para>
2148                         Horizontal sync is active high
2149                       </para></listitem>
2150                     </varlistentry>
2151                     <varlistentry>
2152                       <term>DRM_MODE_FLAG_NHSYNC</term>
2153                       <listitem><para>
2154                         Horizontal sync is active low
2155                       </para></listitem>
2156                     </varlistentry>
2157                     <varlistentry>
2158                       <term>DRM_MODE_FLAG_PVSYNC</term>
2159                       <listitem><para>
2160                         Vertical sync is active high
2161                       </para></listitem>
2162                     </varlistentry>
2163                     <varlistentry>
2164                       <term>DRM_MODE_FLAG_NVSYNC</term>
2165                       <listitem><para>
2166                         Vertical sync is active low
2167                       </para></listitem>
2168                     </varlistentry>
2169                     <varlistentry>
2170                       <term>DRM_MODE_FLAG_INTERLACE</term>
2171                       <listitem><para>
2172                         Mode is interlaced
2173                       </para></listitem>
2174                     </varlistentry>
2175                     <varlistentry>
2176                       <term>DRM_MODE_FLAG_DBLSCAN</term>
2177                       <listitem><para>
2178                         Mode uses doublescan
2179                       </para></listitem>
2180                     </varlistentry>
2181                     <varlistentry>
2182                       <term>DRM_MODE_FLAG_CSYNC</term>
2183                       <listitem><para>
2184                         Mode uses composite sync
2185                       </para></listitem>
2186                     </varlistentry>
2187                     <varlistentry>
2188                       <term>DRM_MODE_FLAG_PCSYNC</term>
2189                       <listitem><para>
2190                         Composite sync is active high
2191                       </para></listitem>
2192                     </varlistentry>
2193                     <varlistentry>
2194                       <term>DRM_MODE_FLAG_NCSYNC</term>
2195                       <listitem><para>
2196                         Composite sync is active low
2197                       </para></listitem>
2198                     </varlistentry>
2199                     <varlistentry>
2200                       <term>DRM_MODE_FLAG_HSKEW</term>
2201                       <listitem><para>
2202                         hskew provided (not used?)
2203                       </para></listitem>
2204                     </varlistentry>
2205                     <varlistentry>
2206                       <term>DRM_MODE_FLAG_BCAST</term>
2207                       <listitem><para>
2208                         not used?
2209                       </para></listitem>
2210                     </varlistentry>
2211                     <varlistentry>
2212                       <term>DRM_MODE_FLAG_PIXMUX</term>
2213                       <listitem><para>
2214                         not used?
2215                       </para></listitem>
2216                     </varlistentry>
2217                     <varlistentry>
2218                       <term>DRM_MODE_FLAG_DBLCLK</term>
2219                       <listitem><para>
2220                         not used?
2221                       </para></listitem>
2222                     </varlistentry>
2223                     <varlistentry>
2224                       <term>DRM_MODE_FLAG_CLKDIV2</term>
2225                       <listitem><para>
2226                         ?
2227                       </para></listitem>
2228                     </varlistentry>
2229                   </variablelist>
2230                 </para>
2231                 <para>
2232                   Note that modes marked with the INTERLACE or DBLSCAN flags will be
2233                   filtered out by
2234                   <function>drm_helper_probe_single_connector_modes</function> if
2235                   the connector's <structfield>interlace_allowed</structfield> or
2236                   <structfield>doublescan_allowed</structfield> field is set to 0.
2237                 </para>
2238               </listitem>
2239               <listitem>
2240                 <synopsis>char name[DRM_DISPLAY_MODE_LEN];</synopsis>
2241                 <para>
2242                   Mode name. The driver must call
2243                   <function>drm_mode_set_name</function> to fill the mode name from
2244                   <structfield>hdisplay</structfield>,
2245                   <structfield>vdisplay</structfield> and interlace flag after
2246                   filling the corresponding fields.
2247                 </para>
2248               </listitem>
2249             </itemizedlist>
2250           </para>
2251           <para>
2252             The <structfield>vrefresh</structfield> value is computed by
2253             <function>drm_helper_probe_single_connector_modes</function>.
2254           </para>
2255           <para>
2256             When parsing EDID data, <function>drm_add_edid_modes</function> fill the
2257             connector <structfield>display_info</structfield>
2258             <structfield>width_mm</structfield> and
2259             <structfield>height_mm</structfield> fields. When creating modes
2260             manually the <methodname>get_modes</methodname> helper operation must
2261             set the <structfield>display_info</structfield>
2262             <structfield>width_mm</structfield> and
2263             <structfield>height_mm</structfield> fields if they haven't been set
2264             already (for instance at initialization time when a fixed-size panel is
2265             attached to the connector). The mode <structfield>width_mm</structfield>
2266             and <structfield>height_mm</structfield> fields are only used internally
2267             during EDID parsing and should not be set when creating modes manually.
2268           </para>
2269         </listitem>
2270         <listitem>
2271           <synopsis>int (*mode_valid)(struct drm_connector *connector,
2272                   struct drm_display_mode *mode);</synopsis>
2273           <para>
2274             Verify whether a mode is valid for the connector. Return MODE_OK for
2275             supported modes and one of the enum drm_mode_status values (MODE_*)
2276             for unsupported modes. This operation is optional.
2277           </para>
2278           <para>
2279             As the mode rejection reason is currently not used beside for
2280             immediately removing the unsupported mode, an implementation can
2281             return MODE_BAD regardless of the exact reason why the mode is not
2282             valid.
2283           </para>
2284           <note><para>
2285             Note that the <methodname>mode_valid</methodname> helper operation is
2286             only called for modes detected by the device, and
2287             <emphasis>not</emphasis> for modes set by the user through the CRTC
2288             <methodname>set_config</methodname> operation.
2289           </para></note>
2290         </listitem>
2291       </itemizedlist>
2292     </sect2>
2293     <sect2>
2294       <title>Modeset Helper Functions Reference</title>
2295 !Edrivers/gpu/drm/drm_crtc_helper.c
2296     </sect2>
2297     <sect2>
2298       <title>Output Probing Helper Functions Reference</title>
2299 !Pdrivers/gpu/drm/drm_probe_helper.c output probing helper overview
2300 !Edrivers/gpu/drm/drm_probe_helper.c
2301     </sect2>
2302     <sect2>
2303       <title>fbdev Helper Functions Reference</title>
2304 !Pdrivers/gpu/drm/drm_fb_helper.c fbdev helpers
2305 !Edrivers/gpu/drm/drm_fb_helper.c
2306 !Iinclude/drm/drm_fb_helper.h
2307     </sect2>
2308     <sect2>
2309       <title>Display Port Helper Functions Reference</title>
2310 !Pdrivers/gpu/drm/drm_dp_helper.c dp helpers
2311 !Iinclude/drm/drm_dp_helper.h
2312 !Edrivers/gpu/drm/drm_dp_helper.c
2313     </sect2>
2314     <sect2>
2315       <title>EDID Helper Functions Reference</title>
2316 !Edrivers/gpu/drm/drm_edid.c
2317     </sect2>
2318     <sect2>
2319       <title>Rectangle Utilities Reference</title>
2320 !Pinclude/drm/drm_rect.h rect utils
2321 !Iinclude/drm/drm_rect.h
2322 !Edrivers/gpu/drm/drm_rect.c
2323     </sect2>
2324     <sect2>
2325       <title>Flip-work Helper Reference</title>
2326 !Pinclude/drm/drm_flip_work.h flip utils
2327 !Iinclude/drm/drm_flip_work.h
2328 !Edrivers/gpu/drm/drm_flip_work.c
2329     </sect2>
2330     <sect2>
2331       <title>HDMI Infoframes Helper Reference</title>
2332       <para>
2333         Strictly speaking this is not a DRM helper library but generally useable
2334         by any driver interfacing with HDMI outputs like v4l or alsa drivers.
2335         But it nicely fits into the overall topic of mode setting helper
2336         libraries and hence is also included here.
2337       </para>
2338 !Iinclude/linux/hdmi.h
2339 !Edrivers/video/hdmi.c
2340     </sect2>
2341     <sect2>
2342       <title id="drm-kms-planehelpers">Plane Helper Reference</title>
2343 !Edrivers/gpu/drm/drm_plane_helper.c Plane Helpers
2344     </sect2>
2345   </sect1>
2346
2347   <!-- Internals: kms properties -->
2348
2349   <sect1 id="drm-kms-properties">
2350     <title>KMS Properties</title>
2351     <para>
2352       Drivers may need to expose additional parameters to applications than
2353       those described in the previous sections. KMS supports attaching
2354       properties to CRTCs, connectors and planes and offers a userspace API to
2355       list, get and set the property values.
2356     </para>
2357     <para>
2358       Properties are identified by a name that uniquely defines the property
2359       purpose, and store an associated value. For all property types except blob
2360       properties the value is a 64-bit unsigned integer.
2361     </para>
2362     <para>
2363       KMS differentiates between properties and property instances. Drivers
2364       first create properties and then create and associate individual instances
2365       of those properties to objects. A property can be instantiated multiple
2366       times and associated with different objects. Values are stored in property
2367       instances, and all other property information are stored in the property
2368       and shared between all instances of the property.
2369     </para>
2370     <para>
2371       Every property is created with a type that influences how the KMS core
2372       handles the property. Supported property types are
2373       <variablelist>
2374         <varlistentry>
2375           <term>DRM_MODE_PROP_RANGE</term>
2376           <listitem><para>Range properties report their minimum and maximum
2377             admissible values. The KMS core verifies that values set by
2378             application fit in that range.</para></listitem>
2379         </varlistentry>
2380         <varlistentry>
2381           <term>DRM_MODE_PROP_ENUM</term>
2382           <listitem><para>Enumerated properties take a numerical value that
2383             ranges from 0 to the number of enumerated values defined by the
2384             property minus one, and associate a free-formed string name to each
2385             value. Applications can retrieve the list of defined value-name pairs
2386             and use the numerical value to get and set property instance values.
2387             </para></listitem>
2388         </varlistentry>
2389         <varlistentry>
2390           <term>DRM_MODE_PROP_BITMASK</term>
2391           <listitem><para>Bitmask properties are enumeration properties that
2392             additionally restrict all enumerated values to the 0..63 range.
2393             Bitmask property instance values combine one or more of the
2394             enumerated bits defined by the property.</para></listitem>
2395         </varlistentry>
2396         <varlistentry>
2397           <term>DRM_MODE_PROP_BLOB</term>
2398           <listitem><para>Blob properties store a binary blob without any format
2399             restriction. The binary blobs are created as KMS standalone objects,
2400             and blob property instance values store the ID of their associated
2401             blob object.</para>
2402             <para>Blob properties are only used for the connector EDID property
2403             and cannot be created by drivers.</para></listitem>
2404         </varlistentry>
2405       </variablelist>
2406     </para>
2407     <para>
2408       To create a property drivers call one of the following functions depending
2409       on the property type. All property creation functions take property flags
2410       and name, as well as type-specific arguments.
2411       <itemizedlist>
2412         <listitem>
2413           <synopsis>struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2414                                                const char *name,
2415                                                uint64_t min, uint64_t max);</synopsis>
2416           <para>Create a range property with the given minimum and maximum
2417             values.</para>
2418         </listitem>
2419         <listitem>
2420           <synopsis>struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2421                                               const char *name,
2422                                               const struct drm_prop_enum_list *props,
2423                                               int num_values);</synopsis>
2424           <para>Create an enumerated property. The <parameter>props</parameter>
2425             argument points to an array of <parameter>num_values</parameter>
2426             value-name pairs.</para>
2427         </listitem>
2428         <listitem>
2429           <synopsis>struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2430                                                  int flags, const char *name,
2431                                                  const struct drm_prop_enum_list *props,
2432                                                  int num_values);</synopsis>
2433           <para>Create a bitmask property. The <parameter>props</parameter>
2434             argument points to an array of <parameter>num_values</parameter>
2435             value-name pairs.</para>
2436         </listitem>
2437       </itemizedlist>
2438     </para>
2439     <para>
2440       Properties can additionally be created as immutable, in which case they
2441       will be read-only for applications but can be modified by the driver. To
2442       create an immutable property drivers must set the DRM_MODE_PROP_IMMUTABLE
2443       flag at property creation time.
2444     </para>
2445     <para>
2446       When no array of value-name pairs is readily available at property
2447       creation time for enumerated or range properties, drivers can create
2448       the property using the <function>drm_property_create</function> function
2449       and manually add enumeration value-name pairs by calling the
2450       <function>drm_property_add_enum</function> function. Care must be taken to
2451       properly specify the property type through the <parameter>flags</parameter>
2452       argument.
2453     </para>
2454     <para>
2455       After creating properties drivers can attach property instances to CRTC,
2456       connector and plane objects by calling the
2457       <function>drm_object_attach_property</function>. The function takes a
2458       pointer to the target object, a pointer to the previously created property
2459       and an initial instance value.
2460     </para>
2461     <sect2>
2462         <title>Existing KMS Properties</title>
2463         <para>
2464         The following table gives description of drm properties exposed by various
2465         modules/drivers.
2466         </para>
2467         <table border="1" cellpadding="0" cellspacing="0">
2468         <tbody>
2469         <tr style="font-weight: bold;">
2470         <td valign="top" >Owner Module/Drivers</td>
2471         <td valign="top" >Group</td>
2472         <td valign="top" >Property Name</td>
2473         <td valign="top" >Type</td>
2474         <td valign="top" >Property Values</td>
2475         <td valign="top" >Object attached</td>
2476         <td valign="top" >Description/Restrictions</td>
2477         </tr>
2478         <tr>
2479         <td rowspan="19" valign="top" >DRM</td>
2480         <td rowspan="2" valign="top" >Generic</td>
2481         <td valign="top" >“EDID”</td>
2482         <td valign="top" >BLOB | IMMUTABLE</td>
2483         <td valign="top" >0</td>
2484         <td valign="top" >Connector</td>
2485         <td valign="top" >Contains id of edid blob ptr object.</td>
2486         </tr>
2487         <tr>
2488         <td valign="top" >“DPMS”</td>
2489         <td valign="top" >ENUM</td>
2490         <td valign="top" >{ “On”, “Standby”, “Suspend”, “Off” }</td>
2491         <td valign="top" >Connector</td>
2492         <td valign="top" >Contains DPMS operation mode value.</td>
2493         </tr>
2494         <tr>
2495         <td rowspan="2" valign="top" >DVI-I</td>
2496         <td valign="top" >“subconnector”</td>
2497         <td valign="top" >ENUM</td>
2498         <td valign="top" >{ “Unknown”, “DVI-D”, “DVI-A” }</td>
2499         <td valign="top" >Connector</td>
2500         <td valign="top" >TBD</td>
2501         </tr>
2502         <tr>
2503         <td valign="top" >“select subconnector”</td>
2504         <td valign="top" >ENUM</td>
2505         <td valign="top" >{ “Automatic”, “DVI-D”, “DVI-A” }</td>
2506         <td valign="top" >Connector</td>
2507         <td valign="top" >TBD</td>
2508         </tr>
2509         <tr>
2510         <td rowspan="13" valign="top" >TV</td>
2511         <td valign="top" >“subconnector”</td>
2512         <td valign="top" >ENUM</td>
2513         <td valign="top" >{ "Unknown", "Composite", "SVIDEO", "Component", "SCART" }</td>
2514         <td valign="top" >Connector</td>
2515         <td valign="top" >TBD</td>
2516         </tr>
2517         <tr>
2518         <td valign="top" >“select subconnector”</td>
2519         <td valign="top" >ENUM</td>
2520         <td valign="top" >{ "Automatic", "Composite", "SVIDEO", "Component", "SCART" }</td>
2521         <td valign="top" >Connector</td>
2522         <td valign="top" >TBD</td>
2523         </tr>
2524         <tr>
2525         <td valign="top" >“mode”</td>
2526         <td valign="top" >ENUM</td>
2527         <td valign="top" >{ "NTSC_M", "NTSC_J", "NTSC_443", "PAL_B" } etc.</td>
2528         <td valign="top" >Connector</td>
2529         <td valign="top" >TBD</td>
2530         </tr>
2531         <tr>
2532         <td valign="top" >“left margin”</td>
2533         <td valign="top" >RANGE</td>
2534         <td valign="top" >Min=0, Max=100</td>
2535         <td valign="top" >Connector</td>
2536         <td valign="top" >TBD</td>
2537         </tr>
2538         <tr>
2539         <td valign="top" >“right margin”</td>
2540         <td valign="top" >RANGE</td>
2541         <td valign="top" >Min=0, Max=100</td>
2542         <td valign="top" >Connector</td>
2543         <td valign="top" >TBD</td>
2544         </tr>
2545         <tr>
2546         <td valign="top" >“top margin”</td>
2547         <td valign="top" >RANGE</td>
2548         <td valign="top" >Min=0, Max=100</td>
2549         <td valign="top" >Connector</td>
2550         <td valign="top" >TBD</td>
2551         </tr>
2552         <tr>
2553         <td valign="top" >“bottom margin”</td>
2554         <td valign="top" >RANGE</td>
2555         <td valign="top" >Min=0, Max=100</td>
2556         <td valign="top" >Connector</td>
2557         <td valign="top" >TBD</td>
2558         </tr>
2559         <tr>
2560         <td valign="top" >“brightness”</td>
2561         <td valign="top" >RANGE</td>
2562         <td valign="top" >Min=0, Max=100</td>
2563         <td valign="top" >Connector</td>
2564         <td valign="top" >TBD</td>
2565         </tr>
2566         <tr>
2567         <td valign="top" >“contrast”</td>
2568         <td valign="top" >RANGE</td>
2569         <td valign="top" >Min=0, Max=100</td>
2570         <td valign="top" >Connector</td>
2571         <td valign="top" >TBD</td>
2572         </tr>
2573         <tr>
2574         <td valign="top" >“flicker reduction”</td>
2575         <td valign="top" >RANGE</td>
2576         <td valign="top" >Min=0, Max=100</td>
2577         <td valign="top" >Connector</td>
2578         <td valign="top" >TBD</td>
2579         </tr>
2580         <tr>
2581         <td valign="top" >“overscan”</td>
2582         <td valign="top" >RANGE</td>
2583         <td valign="top" >Min=0, Max=100</td>
2584         <td valign="top" >Connector</td>
2585         <td valign="top" >TBD</td>
2586         </tr>
2587         <tr>
2588         <td valign="top" >“saturation”</td>
2589         <td valign="top" >RANGE</td>
2590         <td valign="top" >Min=0, Max=100</td>
2591         <td valign="top" >Connector</td>
2592         <td valign="top" >TBD</td>
2593         </tr>
2594         <tr>
2595         <td valign="top" >“hue”</td>
2596         <td valign="top" >RANGE</td>
2597         <td valign="top" >Min=0, Max=100</td>
2598         <td valign="top" >Connector</td>
2599         <td valign="top" >TBD</td>
2600         </tr>
2601         <tr>
2602         <td rowspan="2" valign="top" >Optional</td>
2603         <td valign="top" >“scaling mode”</td>
2604         <td valign="top" >ENUM</td>
2605         <td valign="top" >{ "None", "Full", "Center", "Full aspect" }</td>
2606         <td valign="top" >Connector</td>
2607         <td valign="top" >TBD</td>
2608         </tr>
2609         <tr>
2610         <td valign="top" >“dirty”</td>
2611         <td valign="top" >ENUM | IMMUTABLE</td>
2612         <td valign="top" >{ "Off", "On", "Annotate" }</td>
2613         <td valign="top" >Connector</td>
2614         <td valign="top" >TBD</td>
2615         </tr>
2616         <tr>
2617         <td rowspan="21" valign="top" >i915</td>
2618         <td rowspan="3" valign="top" >Generic</td>
2619         <td valign="top" >"Broadcast RGB"</td>
2620         <td valign="top" >ENUM</td>
2621         <td valign="top" >{ "Automatic", "Full", "Limited 16:235" }</td>
2622         <td valign="top" >Connector</td>
2623         <td valign="top" >TBD</td>
2624         </tr>
2625         <tr>
2626         <td valign="top" >“audio”</td>
2627         <td valign="top" >ENUM</td>
2628         <td valign="top" >{ "force-dvi", "off", "auto", "on" }</td>
2629         <td valign="top" >Connector</td>
2630         <td valign="top" >TBD</td>
2631         </tr>
2632         <tr>
2633         <td valign="top" >Standard name as in DRM</td>
2634         <td valign="top" >Standard type as in DRM</td>
2635         <td valign="top" >Standard value as in DRM</td>
2636         <td valign="top" >Standard Object as in DRM</td>
2637         <td valign="top" >TBD</td>
2638         </tr>
2639         <tr>
2640         <td rowspan="17" valign="top" >SDVO-TV</td>
2641         <td valign="top" >“mode”</td>
2642         <td valign="top" >ENUM</td>
2643         <td valign="top" >{ "NTSC_M", "NTSC_J", "NTSC_443", "PAL_B" } etc.</td>
2644         <td valign="top" >Connector</td>
2645         <td valign="top" >TBD</td>
2646         </tr>
2647         <tr>
2648         <td valign="top" >"left_margin"</td>
2649         <td valign="top" >RANGE</td>
2650         <td valign="top" >Min=0, Max= SDVO dependent</td>
2651         <td valign="top" >Connector</td>
2652         <td valign="top" >TBD</td>
2653         </tr>
2654         <tr>
2655         <td valign="top" >"right_margin"</td>
2656         <td valign="top" >RANGE</td>
2657         <td valign="top" >Min=0, Max= SDVO dependent</td>
2658         <td valign="top" >Connector</td>
2659         <td valign="top" >TBD</td>
2660         </tr>
2661         <tr>
2662         <td valign="top" >"top_margin"</td>
2663         <td valign="top" >RANGE</td>
2664         <td valign="top" >Min=0, Max= SDVO dependent</td>
2665         <td valign="top" >Connector</td>
2666         <td valign="top" >TBD</td>
2667         </tr>
2668         <tr>
2669         <td valign="top" >"bottom_margin"</td>
2670         <td valign="top" >RANGE</td>
2671         <td valign="top" >Min=0, Max= SDVO dependent</td>
2672         <td valign="top" >Connector</td>
2673         <td valign="top" >TBD</td>
2674         </tr>
2675         <tr>
2676         <td valign="top" >“hpos”</td>
2677         <td valign="top" >RANGE</td>
2678         <td valign="top" >Min=0, Max= SDVO dependent</td>
2679         <td valign="top" >Connector</td>
2680         <td valign="top" >TBD</td>
2681         </tr>
2682         <tr>
2683         <td valign="top" >“vpos”</td>
2684         <td valign="top" >RANGE</td>
2685         <td valign="top" >Min=0, Max= SDVO dependent</td>
2686         <td valign="top" >Connector</td>
2687         <td valign="top" >TBD</td>
2688         </tr>
2689         <tr>
2690         <td valign="top" >“contrast”</td>
2691         <td valign="top" >RANGE</td>
2692         <td valign="top" >Min=0, Max= SDVO dependent</td>
2693         <td valign="top" >Connector</td>
2694         <td valign="top" >TBD</td>
2695         </tr>
2696         <tr>
2697         <td valign="top" >“saturation”</td>
2698         <td valign="top" >RANGE</td>
2699         <td valign="top" >Min=0, Max= SDVO dependent</td>
2700         <td valign="top" >Connector</td>
2701         <td valign="top" >TBD</td>
2702         </tr>
2703         <tr>
2704         <td valign="top" >“hue”</td>
2705         <td valign="top" >RANGE</td>
2706         <td valign="top" >Min=0, Max= SDVO dependent</td>
2707         <td valign="top" >Connector</td>
2708         <td valign="top" >TBD</td>
2709         </tr>
2710         <tr>
2711         <td valign="top" >“sharpness”</td>
2712         <td valign="top" >RANGE</td>
2713         <td valign="top" >Min=0, Max= SDVO dependent</td>
2714         <td valign="top" >Connector</td>
2715         <td valign="top" >TBD</td>
2716         </tr>
2717         <tr>
2718         <td valign="top" >“flicker_filter”</td>
2719         <td valign="top" >RANGE</td>
2720         <td valign="top" >Min=0, Max= SDVO dependent</td>
2721         <td valign="top" >Connector</td>
2722         <td valign="top" >TBD</td>
2723         </tr>
2724         <tr>
2725         <td valign="top" >“flicker_filter_adaptive”</td>
2726         <td valign="top" >RANGE</td>
2727         <td valign="top" >Min=0, Max= SDVO dependent</td>
2728         <td valign="top" >Connector</td>
2729         <td valign="top" >TBD</td>
2730         </tr>
2731         <tr>
2732         <td valign="top" >“flicker_filter_2d”</td>
2733         <td valign="top" >RANGE</td>
2734         <td valign="top" >Min=0, Max= SDVO dependent</td>
2735         <td valign="top" >Connector</td>
2736         <td valign="top" >TBD</td>
2737         </tr>
2738         <tr>
2739         <td valign="top" >“tv_chroma_filter”</td>
2740         <td valign="top" >RANGE</td>
2741         <td valign="top" >Min=0, Max= SDVO dependent</td>
2742         <td valign="top" >Connector</td>
2743         <td valign="top" >TBD</td>
2744         </tr>
2745         <tr>
2746         <td valign="top" >“tv_luma_filter”</td>
2747         <td valign="top" >RANGE</td>
2748         <td valign="top" >Min=0, Max= SDVO dependent</td>
2749         <td valign="top" >Connector</td>
2750         <td valign="top" >TBD</td>
2751         </tr>
2752         <tr>
2753         <td valign="top" >“dot_crawl”</td>
2754         <td valign="top" >RANGE</td>
2755         <td valign="top" >Min=0, Max=1</td>
2756         <td valign="top" >Connector</td>
2757         <td valign="top" >TBD</td>
2758         </tr>
2759         <tr>
2760         <td valign="top" >SDVO-TV/LVDS</td>
2761         <td valign="top" >“brightness”</td>
2762         <td valign="top" >RANGE</td>
2763         <td valign="top" >Min=0, Max= SDVO dependent</td>
2764         <td valign="top" >Connector</td>
2765         <td valign="top" >TBD</td>
2766         </tr>
2767         <tr>
2768         <td rowspan="3" valign="top" >CDV gma-500</td>
2769         <td rowspan="3" valign="top" >Generic</td>
2770         <td valign="top" >"Broadcast RGB"</td>
2771         <td valign="top" >ENUM</td>
2772         <td valign="top" >{ “Full”, “Limited 16:235” }</td>
2773         <td valign="top" >Connector</td>
2774         <td valign="top" >TBD</td>
2775         </tr>
2776         <tr>
2777         <td valign="top" >"Broadcast RGB"</td>
2778         <td valign="top" >ENUM</td>
2779         <td valign="top" >{ “off”, “auto”, “on” }</td>
2780         <td valign="top" >Connector</td>
2781         <td valign="top" >TBD</td>
2782         </tr>
2783         <tr>
2784         <td valign="top" >Standard name as in DRM</td>
2785         <td valign="top" >Standard type as in DRM</td>
2786         <td valign="top" >Standard value as in DRM</td>
2787         <td valign="top" >Standard Object as in DRM</td>
2788         <td valign="top" >TBD</td>
2789         </tr>
2790         <tr>
2791         <td rowspan="20" valign="top" >Poulsbo</td>
2792         <td rowspan="2" valign="top" >Generic</td>
2793         <td valign="top" >“backlight”</td>
2794         <td valign="top" >RANGE</td>
2795         <td valign="top" >Min=0, Max=100</td>
2796         <td valign="top" >Connector</td>
2797         <td valign="top" >TBD</td>
2798         </tr>
2799         <tr>
2800         <td valign="top" >Standard name as in DRM</td>
2801         <td valign="top" >Standard type as in DRM</td>
2802         <td valign="top" >Standard value as in DRM</td>
2803         <td valign="top" >Standard Object as in DRM</td>
2804         <td valign="top" >TBD</td>
2805         </tr>
2806         <tr>
2807         <td rowspan="17" valign="top" >SDVO-TV</td>
2808         <td valign="top" >“mode”</td>
2809         <td valign="top" >ENUM</td>
2810         <td valign="top" >{ "NTSC_M", "NTSC_J", "NTSC_443", "PAL_B" } etc.</td>
2811         <td valign="top" >Connector</td>
2812         <td valign="top" >TBD</td>
2813         </tr>
2814         <tr>
2815         <td valign="top" >"left_margin"</td>
2816         <td valign="top" >RANGE</td>
2817         <td valign="top" >Min=0, Max= SDVO dependent</td>
2818         <td valign="top" >Connector</td>
2819         <td valign="top" >TBD</td>
2820         </tr>
2821         <tr>
2822         <td valign="top" >"right_margin"</td>
2823         <td valign="top" >RANGE</td>
2824         <td valign="top" >Min=0, Max= SDVO dependent</td>
2825         <td valign="top" >Connector</td>
2826         <td valign="top" >TBD</td>
2827         </tr>
2828         <tr>
2829         <td valign="top" >"top_margin"</td>
2830         <td valign="top" >RANGE</td>
2831         <td valign="top" >Min=0, Max= SDVO dependent</td>
2832         <td valign="top" >Connector</td>
2833         <td valign="top" >TBD</td>
2834         </tr>
2835         <tr>
2836         <td valign="top" >"bottom_margin"</td>
2837         <td valign="top" >RANGE</td>
2838         <td valign="top" >Min=0, Max= SDVO dependent</td>
2839         <td valign="top" >Connector</td>
2840         <td valign="top" >TBD</td>
2841         </tr>
2842         <tr>
2843         <td valign="top" >“hpos”</td>
2844         <td valign="top" >RANGE</td>
2845         <td valign="top" >Min=0, Max= SDVO dependent</td>
2846         <td valign="top" >Connector</td>
2847         <td valign="top" >TBD</td>
2848         </tr>
2849         <tr>
2850         <td valign="top" >“vpos”</td>
2851         <td valign="top" >RANGE</td>
2852         <td valign="top" >Min=0, Max= SDVO dependent</td>
2853         <td valign="top" >Connector</td>
2854         <td valign="top" >TBD</td>
2855         </tr>
2856         <tr>
2857         <td valign="top" >“contrast”</td>
2858         <td valign="top" >RANGE</td>
2859         <td valign="top" >Min=0, Max= SDVO dependent</td>
2860         <td valign="top" >Connector</td>
2861         <td valign="top" >TBD</td>
2862         </tr>
2863         <tr>
2864         <td valign="top" >“saturation”</td>
2865         <td valign="top" >RANGE</td>
2866         <td valign="top" >Min=0, Max= SDVO dependent</td>
2867         <td valign="top" >Connector</td>
2868         <td valign="top" >TBD</td>
2869         </tr>
2870         <tr>
2871         <td valign="top" >“hue”</td>
2872         <td valign="top" >RANGE</td>
2873         <td valign="top" >Min=0, Max= SDVO dependent</td>
2874         <td valign="top" >Connector</td>
2875         <td valign="top" >TBD</td>
2876         </tr>
2877         <tr>
2878         <td valign="top" >“sharpness”</td>
2879         <td valign="top" >RANGE</td>
2880         <td valign="top" >Min=0, Max= SDVO dependent</td>
2881         <td valign="top" >Connector</td>
2882         <td valign="top" >TBD</td>
2883         </tr>
2884         <tr>
2885         <td valign="top" >“flicker_filter”</td>
2886         <td valign="top" >RANGE</td>
2887         <td valign="top" >Min=0, Max= SDVO dependent</td>
2888         <td valign="top" >Connector</td>
2889         <td valign="top" >TBD</td>
2890         </tr>
2891         <tr>
2892         <td valign="top" >“flicker_filter_adaptive”</td>
2893         <td valign="top" >RANGE</td>
2894         <td valign="top" >Min=0, Max= SDVO dependent</td>
2895         <td valign="top" >Connector</td>
2896         <td valign="top" >TBD</td>
2897         </tr>
2898         <tr>
2899         <td valign="top" >“flicker_filter_2d”</td>
2900         <td valign="top" >RANGE</td>
2901         <td valign="top" >Min=0, Max= SDVO dependent</td>
2902         <td valign="top" >Connector</td>
2903         <td valign="top" >TBD</td>
2904         </tr>
2905         <tr>
2906         <td valign="top" >“tv_chroma_filter”</td>
2907         <td valign="top" >RANGE</td>
2908         <td valign="top" >Min=0, Max= SDVO dependent</td>
2909         <td valign="top" >Connector</td>
2910         <td valign="top" >TBD</td>
2911         </tr>
2912         <tr>
2913         <td valign="top" >“tv_luma_filter”</td>
2914         <td valign="top" >RANGE</td>
2915         <td valign="top" >Min=0, Max= SDVO dependent</td>
2916         <td valign="top" >Connector</td>
2917         <td valign="top" >TBD</td>
2918         </tr>
2919         <tr>
2920         <td valign="top" >“dot_crawl”</td>
2921         <td valign="top" >RANGE</td>
2922         <td valign="top" >Min=0, Max=1</td>
2923         <td valign="top" >Connector</td>
2924         <td valign="top" >TBD</td>
2925         </tr>
2926         <tr>
2927         <td valign="top" >SDVO-TV/LVDS</td>
2928         <td valign="top" >“brightness”</td>
2929         <td valign="top" >RANGE</td>
2930         <td valign="top" >Min=0, Max= SDVO dependent</td>
2931         <td valign="top" >Connector</td>
2932         <td valign="top" >TBD</td>
2933         </tr>
2934         <tr>
2935         <td rowspan="11" valign="top" >armada</td>
2936         <td rowspan="2" valign="top" >CRTC</td>
2937         <td valign="top" >"CSC_YUV"</td>
2938         <td valign="top" >ENUM</td>
2939         <td valign="top" >{ "Auto" , "CCIR601", "CCIR709" }</td>
2940         <td valign="top" >CRTC</td>
2941         <td valign="top" >TBD</td>
2942         </tr>
2943         <tr>
2944         <td valign="top" >"CSC_RGB"</td>
2945         <td valign="top" >ENUM</td>
2946         <td valign="top" >{ "Auto", "Computer system", "Studio" }</td>
2947         <td valign="top" >CRTC</td>
2948         <td valign="top" >TBD</td>
2949         </tr>
2950         <tr>
2951         <td rowspan="9" valign="top" >Overlay</td>
2952         <td valign="top" >"colorkey"</td>
2953         <td valign="top" >RANGE</td>
2954         <td valign="top" >Min=0, Max=0xffffff</td>
2955         <td valign="top" >Plane</td>
2956         <td valign="top" >TBD</td>
2957         </tr>
2958         <tr>
2959         <td valign="top" >"colorkey_min"</td>
2960         <td valign="top" >RANGE</td>
2961         <td valign="top" >Min=0, Max=0xffffff</td>
2962         <td valign="top" >Plane</td>
2963         <td valign="top" >TBD</td>
2964         </tr>
2965         <tr>
2966         <td valign="top" >"colorkey_max"</td>
2967         <td valign="top" >RANGE</td>
2968         <td valign="top" >Min=0, Max=0xffffff</td>
2969         <td valign="top" >Plane</td>
2970         <td valign="top" >TBD</td>
2971         </tr>
2972         <tr>
2973         <td valign="top" >"colorkey_val"</td>
2974         <td valign="top" >RANGE</td>
2975         <td valign="top" >Min=0, Max=0xffffff</td>
2976         <td valign="top" >Plane</td>
2977         <td valign="top" >TBD</td>
2978         </tr>
2979         <tr>
2980         <td valign="top" >"colorkey_alpha"</td>
2981         <td valign="top" >RANGE</td>
2982         <td valign="top" >Min=0, Max=0xffffff</td>
2983         <td valign="top" >Plane</td>
2984         <td valign="top" >TBD</td>
2985         </tr>
2986         <tr>
2987         <td valign="top" >"colorkey_mode"</td>
2988         <td valign="top" >ENUM</td>
2989         <td valign="top" >{ "disabled", "Y component", "U component"
2990         , "V component", "RGB", “R component", "G component", "B component" }</td>
2991         <td valign="top" >Plane</td>
2992         <td valign="top" >TBD</td>
2993         </tr>
2994         <tr>
2995         <td valign="top" >"brightness"</td>
2996         <td valign="top" >RANGE</td>
2997         <td valign="top" >Min=0, Max=256 + 255</td>
2998         <td valign="top" >Plane</td>
2999         <td valign="top" >TBD</td>
3000         </tr>
3001         <tr>
3002         <td valign="top" >"contrast"</td>
3003         <td valign="top" >RANGE</td>
3004         <td valign="top" >Min=0, Max=0x7fff</td>
3005         <td valign="top" >Plane</td>
3006         <td valign="top" >TBD</td>
3007         </tr>
3008         <tr>
3009         <td valign="top" >"saturation"</td>
3010         <td valign="top" >RANGE</td>
3011         <td valign="top" >Min=0, Max=0x7fff</td>
3012         <td valign="top" >Plane</td>
3013         <td valign="top" >TBD</td>
3014         </tr>
3015         <tr>
3016         <td rowspan="2" valign="top" >exynos</td>
3017         <td valign="top" >CRTC</td>
3018         <td valign="top" >“mode”</td>
3019         <td valign="top" >ENUM</td>
3020         <td valign="top" >{ "normal", "blank" }</td>
3021         <td valign="top" >CRTC</td>
3022         <td valign="top" >TBD</td>
3023         </tr>
3024         <tr>
3025         <td valign="top" >Overlay</td>
3026         <td valign="top" >“zpos”</td>
3027         <td valign="top" >RANGE</td>
3028         <td valign="top" >Min=0, Max=MAX_PLANE-1</td>
3029         <td valign="top" >Plane</td>
3030         <td valign="top" >TBD</td>
3031         </tr>
3032         <tr>
3033         <td rowspan="3" valign="top" >i2c/ch7006_drv</td>
3034         <td valign="top" >Generic</td>
3035         <td valign="top" >“scale”</td>
3036         <td valign="top" >RANGE</td>
3037         <td valign="top" >Min=0, Max=2</td>
3038         <td valign="top" >Connector</td>
3039         <td valign="top" >TBD</td>
3040         </tr>
3041         <tr>
3042         <td rowspan="2" valign="top" >TV</td>
3043         <td valign="top" >Standard names as in DRM</td>
3044         <td valign="top" >Standard types as in DRM</td>
3045         <td valign="top" >Standard Values as in DRM</td>
3046         <td valign="top" >Standard object as in DRM</td>
3047         <td valign="top" >TBD</td>
3048         </tr>
3049         <tr>
3050         <td valign="top" >“mode”</td>
3051         <td valign="top" >ENUM</td>
3052         <td valign="top" >{ "PAL", "PAL-M","PAL-N"}, ”PAL-Nc"
3053         , "PAL-60", "NTSC-M", "NTSC-J" }</td>
3054         <td valign="top" >Connector</td>
3055         <td valign="top" >TBD</td>
3056         </tr>
3057         <tr>
3058         <td rowspan="16" valign="top" >noveau</td>
3059         <td rowspan="6" valign="top" >NV10 Overlay</td>
3060         <td valign="top" >"colorkey"</td>
3061         <td valign="top" >RANGE</td>
3062         <td valign="top" >Min=0, Max=0x01ffffff</td>
3063         <td valign="top" >Plane</td>
3064         <td valign="top" >TBD</td>
3065         </tr>
3066         <tr>
3067         <td valign="top" >“contrast”</td>
3068         <td valign="top" >RANGE</td>
3069         <td valign="top" >Min=0, Max=8192-1</td>
3070         <td valign="top" >Plane</td>
3071         <td valign="top" >TBD</td>
3072         </tr>
3073         <tr>
3074         <td valign="top" >“brightness”</td>
3075         <td valign="top" >RANGE</td>
3076         <td valign="top" >Min=0, Max=1024</td>
3077         <td valign="top" >Plane</td>
3078         <td valign="top" >TBD</td>
3079         </tr>
3080         <tr>
3081         <td valign="top" >“hue”</td>
3082         <td valign="top" >RANGE</td>
3083         <td valign="top" >Min=0, Max=359</td>
3084         <td valign="top" >Plane</td>
3085         <td valign="top" >TBD</td>
3086         </tr>
3087         <tr>
3088         <td valign="top" >“saturation”</td>
3089         <td valign="top" >RANGE</td>
3090         <td valign="top" >Min=0, Max=8192-1</td>
3091         <td valign="top" >Plane</td>
3092         <td valign="top" >TBD</td>
3093         </tr>
3094         <tr>
3095         <td valign="top" >“iturbt_709”</td>
3096         <td valign="top" >RANGE</td>
3097         <td valign="top" >Min=0, Max=1</td>
3098         <td valign="top" >Plane</td>
3099         <td valign="top" >TBD</td>
3100         </tr>
3101         <tr>
3102         <td rowspan="2" valign="top" >Nv04 Overlay</td>
3103         <td valign="top" >“colorkey”</td>
3104         <td valign="top" >RANGE</td>
3105         <td valign="top" >Min=0, Max=0x01ffffff</td>
3106         <td valign="top" >Plane</td>
3107         <td valign="top" >TBD</td>
3108         </tr>
3109         <tr>
3110         <td valign="top" >“brightness”</td>
3111         <td valign="top" >RANGE</td>
3112         <td valign="top" >Min=0, Max=1024</td>
3113         <td valign="top" >Plane</td>
3114         <td valign="top" >TBD</td>
3115         </tr>
3116         <tr>
3117         <td rowspan="7" valign="top" >Display</td>
3118         <td valign="top" >“dithering mode”</td>
3119         <td valign="top" >ENUM</td>
3120         <td valign="top" >{ "auto", "off", "on" }</td>
3121         <td valign="top" >Connector</td>
3122         <td valign="top" >TBD</td>
3123         </tr>
3124         <tr>
3125         <td valign="top" >“dithering depth”</td>
3126         <td valign="top" >ENUM</td>
3127         <td valign="top" >{ "auto", "off", "on", "static 2x2", "dynamic 2x2", "temporal" }</td>
3128         <td valign="top" >Connector</td>
3129         <td valign="top" >TBD</td>
3130         </tr>
3131         <tr>
3132         <td valign="top" >“underscan”</td>
3133         <td valign="top" >ENUM</td>
3134         <td valign="top" >{ "auto", "6 bpc", "8 bpc" }</td>
3135         <td valign="top" >Connector</td>
3136         <td valign="top" >TBD</td>
3137         </tr>
3138         <tr>
3139         <td valign="top" >“underscan hborder”</td>
3140         <td valign="top" >RANGE</td>
3141         <td valign="top" >Min=0, Max=128</td>
3142         <td valign="top" >Connector</td>
3143         <td valign="top" >TBD</td>
3144         </tr>
3145         <tr>
3146         <td valign="top" >“underscan vborder”</td>
3147         <td valign="top" >RANGE</td>
3148         <td valign="top" >Min=0, Max=128</td>
3149         <td valign="top" >Connector</td>
3150         <td valign="top" >TBD</td>
3151         </tr>
3152         <tr>
3153         <td valign="top" >“vibrant hue”</td>
3154         <td valign="top" >RANGE</td>
3155         <td valign="top" >Min=0, Max=180</td>
3156         <td valign="top" >Connector</td>
3157         <td valign="top" >TBD</td>
3158         </tr>
3159         <tr>
3160         <td valign="top" >“color vibrance”</td>
3161         <td valign="top" >RANGE</td>
3162         <td valign="top" >Min=0, Max=200</td>
3163         <td valign="top" >Connector</td>
3164         <td valign="top" >TBD</td>
3165         </tr>
3166         <tr>
3167         <td valign="top" >Generic</td>
3168         <td valign="top" >Standard name as in DRM</td>
3169         <td valign="top" >Standard type as in DRM</td>
3170         <td valign="top" >Standard value as in DRM</td>
3171         <td valign="top" >Standard Object as in DRM</td>
3172         <td valign="top" >TBD</td>
3173         </tr>
3174         <tr>
3175         <td rowspan="2" valign="top" >omap</td>
3176         <td rowspan="2" valign="top" >Generic</td>
3177         <td valign="top" >“rotation”</td>
3178         <td valign="top" >BITMASK</td>
3179         <td valign="top" >{ 0, "rotate-0" },
3180         { 1, "rotate-90" },
3181         { 2, "rotate-180" },
3182         { 3, "rotate-270" },
3183         { 4, "reflect-x" },
3184         { 5, "reflect-y" }</td>
3185         <td valign="top" >CRTC, Plane</td>
3186         <td valign="top" >TBD</td>
3187         </tr>
3188         <tr>
3189         <td valign="top" >“zorder”</td>
3190         <td valign="top" >RANGE</td>
3191         <td valign="top" >Min=0, Max=3</td>
3192         <td valign="top" >CRTC, Plane</td>
3193         <td valign="top" >TBD</td>
3194         </tr>
3195         <tr>
3196         <td valign="top" >qxl</td>
3197         <td valign="top" >Generic</td>
3198         <td valign="top" >“hotplug_mode_update"</td>
3199         <td valign="top" >RANGE</td>
3200         <td valign="top" >Min=0, Max=1</td>
3201         <td valign="top" >Connector</td>
3202         <td valign="top" >TBD</td>
3203         </tr>
3204         <tr>
3205         <td rowspan="10" valign="top" >radeon</td>
3206         <td valign="top" >DVI-I</td>
3207         <td valign="top" >“coherent”</td>
3208         <td valign="top" >RANGE</td>
3209         <td valign="top" >Min=0, Max=1</td>
3210         <td valign="top" >Connector</td>
3211         <td valign="top" >TBD</td>
3212         </tr>
3213         <tr>
3214         <td valign="top" >DAC enable load detect</td>
3215         <td valign="top" >“load detection”</td>
3216         <td valign="top" >RANGE</td>
3217         <td valign="top" >Min=0, Max=1</td>
3218         <td valign="top" >Connector</td>
3219         <td valign="top" >TBD</td>
3220         </tr>
3221         <tr>
3222         <td valign="top" >TV Standard</td>
3223         <td valign="top" >"tv standard"</td>
3224         <td valign="top" >ENUM</td>
3225         <td valign="top" >{ "ntsc", "pal", "pal-m", "pal-60", "ntsc-j"
3226         , "scart-pal", "pal-cn", "secam" }</td>
3227         <td valign="top" >Connector</td>
3228         <td valign="top" >TBD</td>
3229         </tr>
3230         <tr>
3231         <td valign="top" >legacy TMDS PLL detect</td>
3232         <td valign="top" >"tmds_pll"</td>
3233         <td valign="top" >ENUM</td>
3234         <td valign="top" >{ "driver", "bios" }</td>
3235         <td valign="top" >-</td>
3236         <td valign="top" >TBD</td>
3237         </tr>
3238         <tr>
3239         <td rowspan="3" valign="top" >Underscan</td>
3240         <td valign="top" >"underscan"</td>
3241         <td valign="top" >ENUM</td>
3242         <td valign="top" >{ "off", "on", "auto" }</td>
3243         <td valign="top" >Connector</td>
3244         <td valign="top" >TBD</td>
3245         </tr>
3246         <tr>
3247         <td valign="top" >"underscan hborder"</td>
3248         <td valign="top" >RANGE</td>
3249         <td valign="top" >Min=0, Max=128</td>
3250         <td valign="top" >Connector</td>
3251         <td valign="top" >TBD</td>
3252         </tr>
3253         <tr>
3254         <td valign="top" >"underscan vborder"</td>
3255         <td valign="top" >RANGE</td>
3256         <td valign="top" >Min=0, Max=128</td>
3257         <td valign="top" >Connector</td>
3258         <td valign="top" >TBD</td>
3259         </tr>
3260         <tr>
3261         <td valign="top" >Audio</td>
3262         <td valign="top" >“audio”</td>
3263         <td valign="top" >ENUM</td>
3264         <td valign="top" >{ "off", "on", "auto" }</td>
3265         <td valign="top" >Connector</td>
3266         <td valign="top" >TBD</td>
3267         </tr>
3268         <tr>
3269         <td valign="top" >FMT Dithering</td>
3270         <td valign="top" >“dither”</td>
3271         <td valign="top" >ENUM</td>
3272         <td valign="top" >{ "off", "on" }</td>
3273         <td valign="top" >Connector</td>
3274         <td valign="top" >TBD</td>
3275         </tr>
3276         <tr>
3277         <td valign="top" >Generic</td>
3278         <td valign="top" >Standard name as in DRM</td>
3279         <td valign="top" >Standard type as in DRM</td>
3280         <td valign="top" >Standard value as in DRM</td>
3281         <td valign="top" >Standard Object as in DRM</td>
3282         <td valign="top" >TBD</td>
3283         </tr>
3284         <tr>
3285         <td rowspan="3" valign="top" >rcar-du</td>
3286         <td rowspan="3" valign="top" >Generic</td>
3287         <td valign="top" >"alpha"</td>
3288         <td valign="top" >RANGE</td>
3289         <td valign="top" >Min=0, Max=255</td>
3290         <td valign="top" >Plane</td>
3291         <td valign="top" >TBD</td>
3292         </tr>
3293         <tr>
3294         <td valign="top" >"colorkey"</td>
3295         <td valign="top" >RANGE</td>
3296         <td valign="top" >Min=0, Max=0x01ffffff</td>
3297         <td valign="top" >Plane</td>
3298         <td valign="top" >TBD</td>
3299         </tr>
3300         <tr>
3301         <td valign="top" >"zpos"</td>
3302         <td valign="top" >RANGE</td>
3303         <td valign="top" >Min=1, Max=7</td>
3304         <td valign="top" >Plane</td>
3305         <td valign="top" >TBD</td>
3306         </tr>
3307         </tbody>
3308         </table>
3309     </sect2>
3310   </sect1>
3311
3312   <!-- Internals: vertical blanking -->
3313
3314   <sect1 id="drm-vertical-blank">
3315     <title>Vertical Blanking</title>
3316     <para>
3317       Vertical blanking plays a major role in graphics rendering. To achieve
3318       tear-free display, users must synchronize page flips and/or rendering to
3319       vertical blanking. The DRM API offers ioctls to perform page flips
3320       synchronized to vertical blanking and wait for vertical blanking.
3321     </para>
3322     <para>
3323       The DRM core handles most of the vertical blanking management logic, which
3324       involves filtering out spurious interrupts, keeping race-free blanking
3325       counters, coping with counter wrap-around and resets and keeping use
3326       counts. It relies on the driver to generate vertical blanking interrupts
3327       and optionally provide a hardware vertical blanking counter. Drivers must
3328       implement the following operations.
3329     </para>
3330     <itemizedlist>
3331       <listitem>
3332         <synopsis>int (*enable_vblank) (struct drm_device *dev, int crtc);
3333 void (*disable_vblank) (struct drm_device *dev, int crtc);</synopsis>
3334         <para>
3335           Enable or disable vertical blanking interrupts for the given CRTC.
3336         </para>
3337       </listitem>
3338       <listitem>
3339         <synopsis>u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);</synopsis>
3340         <para>
3341           Retrieve the value of the vertical blanking counter for the given
3342           CRTC. If the hardware maintains a vertical blanking counter its value
3343           should be returned. Otherwise drivers can use the
3344           <function>drm_vblank_count</function> helper function to handle this
3345           operation.
3346         </para>
3347       </listitem>
3348     </itemizedlist>
3349     <para>
3350       Drivers must initialize the vertical blanking handling core with a call to
3351       <function>drm_vblank_init</function> in their
3352       <methodname>load</methodname> operation. The function will set the struct
3353       <structname>drm_device</structname>
3354       <structfield>vblank_disable_allowed</structfield> field to 0. This will
3355       keep vertical blanking interrupts enabled permanently until the first mode
3356       set operation, where <structfield>vblank_disable_allowed</structfield> is
3357       set to 1. The reason behind this is not clear. Drivers can set the field
3358       to 1 after <function>calling drm_vblank_init</function> to make vertical
3359       blanking interrupts dynamically managed from the beginning.
3360     </para>
3361     <para>
3362       Vertical blanking interrupts can be enabled by the DRM core or by drivers
3363       themselves (for instance to handle page flipping operations). The DRM core
3364       maintains a vertical blanking use count to ensure that the interrupts are
3365       not disabled while a user still needs them. To increment the use count,
3366       drivers call <function>drm_vblank_get</function>. Upon return vertical
3367       blanking interrupts are guaranteed to be enabled.
3368     </para>
3369     <para>
3370       To decrement the use count drivers call
3371       <function>drm_vblank_put</function>. Only when the use count drops to zero
3372       will the DRM core disable the vertical blanking interrupts after a delay
3373       by scheduling a timer. The delay is accessible through the vblankoffdelay
3374       module parameter or the <varname>drm_vblank_offdelay</varname> global
3375       variable and expressed in milliseconds. Its default value is 5000 ms.
3376     </para>
3377     <para>
3378       When a vertical blanking interrupt occurs drivers only need to call the
3379       <function>drm_handle_vblank</function> function to account for the
3380       interrupt.
3381     </para>
3382     <para>
3383       Resources allocated by <function>drm_vblank_init</function> must be freed
3384       with a call to <function>drm_vblank_cleanup</function> in the driver
3385       <methodname>unload</methodname> operation handler.
3386     </para>
3387     <sect2>
3388       <title>Vertical Blanking and Interrupt Handling Functions Reference</title>
3389 !Edrivers/gpu/drm/drm_irq.c
3390     </sect2>
3391   </sect1>
3392
3393   <!-- Internals: open/close, file operations and ioctls -->
3394
3395   <sect1>
3396     <title>Open/Close, File Operations and IOCTLs</title>
3397     <sect2>
3398       <title>Open and Close</title>
3399       <synopsis>int (*firstopen) (struct drm_device *);
3400 void (*lastclose) (struct drm_device *);
3401 int (*open) (struct drm_device *, struct drm_file *);
3402 void (*preclose) (struct drm_device *, struct drm_file *);
3403 void (*postclose) (struct drm_device *, struct drm_file *);</synopsis>
3404       <abstract>Open and close handlers. None of those methods are mandatory.
3405       </abstract>
3406       <para>
3407         The <methodname>firstopen</methodname> method is called by the DRM core
3408         for legacy UMS (User Mode Setting) drivers only when an application
3409         opens a device that has no other opened file handle. UMS drivers can
3410         implement it to acquire device resources. KMS drivers can't use the
3411         method and must acquire resources in the <methodname>load</methodname>
3412         method instead.
3413       </para>
3414       <para>
3415         Similarly the <methodname>lastclose</methodname> method is called when
3416         the last application holding a file handle opened on the device closes
3417         it, for both UMS and KMS drivers. Additionally, the method is also
3418         called at module unload time or, for hot-pluggable devices, when the
3419         device is unplugged. The <methodname>firstopen</methodname> and
3420         <methodname>lastclose</methodname> calls can thus be unbalanced.
3421       </para>
3422       <para>
3423         The <methodname>open</methodname> method is called every time the device
3424         is opened by an application. Drivers can allocate per-file private data
3425         in this method and store them in the struct
3426         <structname>drm_file</structname> <structfield>driver_priv</structfield>
3427         field. Note that the <methodname>open</methodname> method is called
3428         before <methodname>firstopen</methodname>.
3429       </para>
3430       <para>
3431         The close operation is split into <methodname>preclose</methodname> and
3432         <methodname>postclose</methodname> methods. Drivers must stop and
3433         cleanup all per-file operations in the <methodname>preclose</methodname>
3434         method. For instance pending vertical blanking and page flip events must
3435         be cancelled. No per-file operation is allowed on the file handle after
3436         returning from the <methodname>preclose</methodname> method.
3437       </para>
3438       <para>
3439         Finally the <methodname>postclose</methodname> method is called as the
3440         last step of the close operation, right before calling the
3441         <methodname>lastclose</methodname> method if no other open file handle
3442         exists for the device. Drivers that have allocated per-file private data
3443         in the <methodname>open</methodname> method should free it here.
3444       </para>
3445       <para>
3446         The <methodname>lastclose</methodname> method should restore CRTC and
3447         plane properties to default value, so that a subsequent open of the
3448         device will not inherit state from the previous user. It can also be
3449         used to execute delayed power switching state changes, e.g. in
3450         conjunction with the vga-switcheroo infrastructure. Beyond that KMS
3451         drivers should not do any further cleanup. Only legacy UMS drivers might
3452         need to clean up device state so that the vga console or an independent
3453         fbdev driver could take over.
3454       </para>
3455     </sect2>
3456     <sect2>
3457       <title>File Operations</title>
3458       <synopsis>const struct file_operations *fops</synopsis>
3459       <abstract>File operations for the DRM device node.</abstract>
3460       <para>
3461         Drivers must define the file operations structure that forms the DRM
3462         userspace API entry point, even though most of those operations are
3463         implemented in the DRM core. The <methodname>open</methodname>,
3464         <methodname>release</methodname> and <methodname>ioctl</methodname>
3465         operations are handled by
3466         <programlisting>
3467         .owner = THIS_MODULE,
3468         .open = drm_open,
3469         .release = drm_release,
3470         .unlocked_ioctl = drm_ioctl,
3471   #ifdef CONFIG_COMPAT
3472         .compat_ioctl = drm_compat_ioctl,
3473   #endif
3474         </programlisting>
3475       </para>
3476       <para>
3477         Drivers that implement private ioctls that requires 32/64bit
3478         compatibility support must provide their own
3479         <methodname>compat_ioctl</methodname> handler that processes private
3480         ioctls and calls <function>drm_compat_ioctl</function> for core ioctls.
3481       </para>
3482       <para>
3483         The <methodname>read</methodname> and <methodname>poll</methodname>
3484         operations provide support for reading DRM events and polling them. They
3485         are implemented by
3486         <programlisting>
3487         .poll = drm_poll,
3488         .read = drm_read,
3489         .llseek = no_llseek,
3490         </programlisting>
3491       </para>
3492       <para>
3493         The memory mapping implementation varies depending on how the driver
3494         manages memory. Pre-GEM drivers will use <function>drm_mmap</function>,
3495         while GEM-aware drivers will use <function>drm_gem_mmap</function>. See
3496         <xref linkend="drm-gem"/>.
3497         <programlisting>
3498         .mmap = drm_gem_mmap,
3499         </programlisting>
3500       </para>
3501       <para>
3502         No other file operation is supported by the DRM API.
3503       </para>
3504     </sect2>
3505     <sect2>
3506       <title>IOCTLs</title>
3507       <synopsis>struct drm_ioctl_desc *ioctls;
3508 int num_ioctls;</synopsis>
3509       <abstract>Driver-specific ioctls descriptors table.</abstract>
3510       <para>
3511         Driver-specific ioctls numbers start at DRM_COMMAND_BASE. The ioctls
3512         descriptors table is indexed by the ioctl number offset from the base
3513         value. Drivers can use the DRM_IOCTL_DEF_DRV() macro to initialize the
3514         table entries.
3515       </para>
3516       <para>
3517         <programlisting>DRM_IOCTL_DEF_DRV(ioctl, func, flags)</programlisting>
3518         <para>
3519           <parameter>ioctl</parameter> is the ioctl name. Drivers must define
3520           the DRM_##ioctl and DRM_IOCTL_##ioctl macros to the ioctl number
3521           offset from DRM_COMMAND_BASE and the ioctl number respectively. The
3522           first macro is private to the device while the second must be exposed
3523           to userspace in a public header.
3524         </para>
3525         <para>
3526           <parameter>func</parameter> is a pointer to the ioctl handler function
3527           compatible with the <type>drm_ioctl_t</type> type.
3528           <programlisting>typedef int drm_ioctl_t(struct drm_device *dev, void *data,
3529                 struct drm_file *file_priv);</programlisting>
3530         </para>
3531         <para>
3532           <parameter>flags</parameter> is a bitmask combination of the following
3533           values. It restricts how the ioctl is allowed to be called.
3534           <itemizedlist>
3535             <listitem><para>
3536               DRM_AUTH - Only authenticated callers allowed
3537             </para></listitem>
3538             <listitem><para>
3539               DRM_MASTER - The ioctl can only be called on the master file
3540               handle
3541             </para></listitem>
3542             <listitem><para>
3543               DRM_ROOT_ONLY - Only callers with the SYSADMIN capability allowed
3544             </para></listitem>
3545             <listitem><para>
3546               DRM_CONTROL_ALLOW - The ioctl can only be called on a control
3547               device
3548             </para></listitem>
3549             <listitem><para>
3550               DRM_UNLOCKED - The ioctl handler will be called without locking
3551               the DRM global mutex
3552             </para></listitem>
3553           </itemizedlist>
3554         </para>
3555       </para>
3556     </sect2>
3557   </sect1>
3558   <sect1>
3559     <title>Legacy Support Code</title>
3560     <para>
3561       The section very briefly covers some of the old legacy support code which
3562       is only used by old DRM drivers which have done a so-called shadow-attach
3563       to the underlying device instead of registering as a real driver. This
3564       also includes some of the old generic buffer management and command
3565       submission code. Do not use any of this in new and modern drivers.
3566     </para>
3567
3568     <sect2>
3569       <title>Legacy Suspend/Resume</title>
3570       <para>
3571         The DRM core provides some suspend/resume code, but drivers wanting full
3572         suspend/resume support should provide save() and restore() functions.
3573         These are called at suspend, hibernate, or resume time, and should perform
3574         any state save or restore required by your device across suspend or
3575         hibernate states.
3576       </para>
3577       <synopsis>int (*suspend) (struct drm_device *, pm_message_t state);
3578   int (*resume) (struct drm_device *);</synopsis>
3579       <para>
3580         Those are legacy suspend and resume methods which
3581         <emphasis>only</emphasis> work with the legacy shadow-attach driver
3582         registration functions. New driver should use the power management
3583         interface provided by their bus type (usually through
3584         the struct <structname>device_driver</structname> dev_pm_ops) and set
3585         these methods to NULL.
3586       </para>
3587     </sect2>
3588
3589     <sect2>
3590       <title>Legacy DMA Services</title>
3591       <para>
3592         This should cover how DMA mapping etc. is supported by the core.
3593         These functions are deprecated and should not be used.
3594       </para>
3595     </sect2>
3596   </sect1>
3597   </chapter>
3598
3599 <!-- TODO
3600
3601 - Add a glossary
3602 - Document the struct_mutex catch-all lock
3603 - Document connector properties
3604
3605 - Why is the load method optional?
3606 - What are drivers supposed to set the initial display state to, and how?
3607   Connector's DPMS states are not initialized and are thus equal to
3608   DRM_MODE_DPMS_ON. The fbcon compatibility layer calls
3609   drm_helper_disable_unused_functions(), which disables unused encoders and
3610   CRTCs, but doesn't touch the connectors' DPMS state, and
3611   drm_helper_connector_dpms() in reaction to fbdev blanking events. Do drivers
3612   that don't implement (or just don't use) fbcon compatibility need to call
3613   those functions themselves?
3614 - KMS drivers must call drm_vblank_pre_modeset() and drm_vblank_post_modeset()
3615   around mode setting. Should this be done in the DRM core?
3616 - vblank_disable_allowed is set to 1 in the first drm_vblank_post_modeset()
3617   call and never set back to 0. It seems to be safe to permanently set it to 1
3618   in drm_vblank_init() for KMS driver, and it might be safe for UMS drivers as
3619   well. This should be investigated.
3620 - crtc and connector .save and .restore operations are only used internally in
3621   drivers, should they be removed from the core?
3622 - encoder mid-layer .save and .restore operations are only used internally in
3623   drivers, should they be removed from the core?
3624 - encoder mid-layer .detect operation is only used internally in drivers,
3625   should it be removed from the core?
3626 -->
3627
3628   <!-- External interfaces -->
3629
3630   <chapter id="drmExternals">
3631     <title>Userland interfaces</title>
3632     <para>
3633       The DRM core exports several interfaces to applications,
3634       generally intended to be used through corresponding libdrm
3635       wrapper functions.  In addition, drivers export device-specific
3636       interfaces for use by userspace drivers &amp; device-aware
3637       applications through ioctls and sysfs files.
3638     </para>
3639     <para>
3640       External interfaces include: memory mapping, context management,
3641       DMA operations, AGP management, vblank control, fence
3642       management, memory management, and output management.
3643     </para>
3644     <para>
3645       Cover generic ioctls and sysfs layout here.  We only need high-level
3646       info, since man pages should cover the rest.
3647     </para>
3648
3649   <!-- External: render nodes -->
3650
3651     <sect1>
3652       <title>Render nodes</title>
3653       <para>
3654         DRM core provides multiple character-devices for user-space to use.
3655         Depending on which device is opened, user-space can perform a different
3656         set of operations (mainly ioctls). The primary node is always created
3657         and called card&lt;num&gt;. Additionally, a currently
3658         unused control node, called controlD&lt;num&gt; is also
3659         created. The primary node provides all legacy operations and
3660         historically was the only interface used by userspace. With KMS, the
3661         control node was introduced. However, the planned KMS control interface
3662         has never been written and so the control node stays unused to date.
3663       </para>
3664       <para>
3665         With the increased use of offscreen renderers and GPGPU applications,
3666         clients no longer require running compositors or graphics servers to
3667         make use of a GPU. But the DRM API required unprivileged clients to
3668         authenticate to a DRM-Master prior to getting GPU access. To avoid this
3669         step and to grant clients GPU access without authenticating, render
3670         nodes were introduced. Render nodes solely serve render clients, that
3671         is, no modesetting or privileged ioctls can be issued on render nodes.
3672         Only non-global rendering commands are allowed. If a driver supports
3673         render nodes, it must advertise it via the DRIVER_RENDER
3674         DRM driver capability. If not supported, the primary node must be used
3675         for render clients together with the legacy drmAuth authentication
3676         procedure.
3677       </para>
3678       <para>
3679         If a driver advertises render node support, DRM core will create a
3680         separate render node called renderD&lt;num&gt;. There will
3681         be one render node per device. No ioctls except  PRIME-related ioctls
3682         will be allowed on this node. Especially GEM_OPEN will be
3683         explicitly prohibited. Render nodes are designed to avoid the
3684         buffer-leaks, which occur if clients guess the flink names or mmap
3685         offsets on the legacy interface. Additionally to this basic interface,
3686         drivers must mark their driver-dependent render-only ioctls as
3687         DRM_RENDER_ALLOW so render clients can use them. Driver
3688         authors must be careful not to allow any privileged ioctls on render
3689         nodes.
3690       </para>
3691       <para>
3692         With render nodes, user-space can now control access to the render node
3693         via basic file-system access-modes. A running graphics server which
3694         authenticates clients on the privileged primary/legacy node is no longer
3695         required. Instead, a client can open the render node and is immediately
3696         granted GPU access. Communication between clients (or servers) is done
3697         via PRIME. FLINK from render node to legacy node is not supported. New
3698         clients must not use the insecure FLINK interface.
3699       </para>
3700       <para>
3701         Besides dropping all modeset/global ioctls, render nodes also drop the
3702         DRM-Master concept. There is no reason to associate render clients with
3703         a DRM-Master as they are independent of any graphics server. Besides,
3704         they must work without any running master, anyway.
3705         Drivers must be able to run without a master object if they support
3706         render nodes. If, on the other hand, a driver requires shared state
3707         between clients which is visible to user-space and accessible beyond
3708         open-file boundaries, they cannot support render nodes.
3709       </para>
3710     </sect1>
3711
3712   <!-- External: vblank handling -->
3713
3714     <sect1>
3715       <title>VBlank event handling</title>
3716       <para>
3717         The DRM core exposes two vertical blank related ioctls:
3718         <variablelist>
3719           <varlistentry>
3720             <term>DRM_IOCTL_WAIT_VBLANK</term>
3721             <listitem>
3722               <para>
3723                 This takes a struct drm_wait_vblank structure as its argument,
3724                 and it is used to block or request a signal when a specified
3725                 vblank event occurs.
3726               </para>
3727             </listitem>
3728           </varlistentry>
3729           <varlistentry>
3730             <term>DRM_IOCTL_MODESET_CTL</term>
3731             <listitem>
3732               <para>
3733                 This was only used for user-mode-settind drivers around
3734                 modesetting changes to allow the kernel to update the vblank
3735                 interrupt after mode setting, since on many devices the vertical
3736                 blank counter is reset to 0 at some point during modeset. Modern
3737                 drivers should not call this any more since with kernel mode
3738                 setting it is a no-op.
3739               </para>
3740             </listitem>
3741           </varlistentry>
3742         </variablelist>
3743       </para>
3744     </sect1>
3745
3746   </chapter>
3747 </part>
3748 <part id="drmDrivers">
3749   <title>DRM Drivers</title>
3750
3751   <partintro>
3752     <para>
3753       This second part of the DRM Developer's Guide documents driver code,
3754       implementation details and also all the driver-specific userspace
3755       interfaces. Especially since all hardware-acceleration interfaces to
3756       userspace are driver specific for efficiency and other reasons these
3757       interfaces can be rather substantial. Hence every driver has its own
3758       chapter.
3759     </para>
3760   </partintro>
3761
3762   <chapter id="drmI915">
3763     <title>drm/i915 Intel GFX Driver</title>
3764     <para>
3765       The drm/i915 driver supports all (with the exception of some very early
3766       models) integrated GFX chipsets with both Intel display and rendering
3767       blocks. This excludes a set of SoC platforms with an SGX rendering unit,
3768       those have basic support through the gma500 drm driver.
3769     </para>
3770     <sect1>
3771       <title>Display Hardware Handling</title>
3772       <para>
3773         This section covers everything related to the display hardware including
3774         the mode setting infrastructure, plane, sprite and cursor handling and
3775         display, output probing and related topics.
3776       </para>
3777       <sect2>
3778         <title>Mode Setting Infrastructure</title>
3779         <para>
3780           The i915 driver is thus far the only DRM driver which doesn't use the
3781           common DRM helper code to implement mode setting sequences. Thus it
3782           has its own tailor-made infrastructure for executing a display
3783           configuration change.
3784         </para>
3785       </sect2>
3786       <sect2>
3787         <title>Plane Configuration</title>
3788         <para>
3789           This section covers plane configuration and composition with the
3790           primary plane, sprites, cursors and overlays. This includes the
3791           infrastructure to do atomic vsync'ed updates of all this state and
3792           also tightly coupled topics like watermark setup and computation,
3793           framebuffer compression and panel self refresh.
3794         </para>
3795       </sect2>
3796       <sect2>
3797         <title>Output Probing</title>
3798         <para>
3799           This section covers output probing and related infrastructure like the
3800           hotplug interrupt storm detection and mitigation code. Note that the
3801           i915 driver still uses most of the common DRM helper code for output
3802           probing, so those sections fully apply.
3803         </para>
3804       </sect2>
3805       <sect2>
3806         <title>DPIO</title>
3807 !Pdrivers/gpu/drm/i915/i915_reg.h DPIO
3808         <table id="dpiox2">
3809           <title>Dual channel PHY (VLV/CHV)</title>
3810           <tgroup cols="8">
3811             <colspec colname="c0" />
3812             <colspec colname="c1" />
3813             <colspec colname="c2" />
3814             <colspec colname="c3" />
3815             <colspec colname="c4" />
3816             <colspec colname="c5" />
3817             <colspec colname="c6" />
3818             <colspec colname="c7" />
3819             <spanspec spanname="ch0" namest="c0" nameend="c3" />
3820             <spanspec spanname="ch1" namest="c4" nameend="c7" />
3821             <spanspec spanname="ch0pcs01" namest="c0" nameend="c1" />
3822             <spanspec spanname="ch0pcs23" namest="c2" nameend="c3" />
3823             <spanspec spanname="ch1pcs01" namest="c4" nameend="c5" />
3824             <spanspec spanname="ch1pcs23" namest="c6" nameend="c7" />
3825             <thead>
3826               <row>
3827                 <entry spanname="ch0">CH0</entry>
3828                 <entry spanname="ch1">CH1</entry>
3829               </row>
3830             </thead>
3831             <tbody valign="top" align="center">
3832               <row>
3833                 <entry spanname="ch0">CMN/PLL/REF</entry>
3834                 <entry spanname="ch1">CMN/PLL/REF</entry>
3835               </row>
3836               <row>
3837                 <entry spanname="ch0pcs01">PCS01</entry>
3838                 <entry spanname="ch0pcs23">PCS23</entry>
3839                 <entry spanname="ch1pcs01">PCS01</entry>
3840                 <entry spanname="ch1pcs23">PCS23</entry>
3841               </row>
3842               <row>
3843                 <entry>TX0</entry>
3844                 <entry>TX1</entry>
3845                 <entry>TX2</entry>
3846                 <entry>TX3</entry>
3847                 <entry>TX0</entry>
3848                 <entry>TX1</entry>
3849                 <entry>TX2</entry>
3850                 <entry>TX3</entry>
3851               </row>
3852               <row>
3853                 <entry spanname="ch0">DDI0</entry>
3854                 <entry spanname="ch1">DDI1</entry>
3855               </row>
3856             </tbody>
3857           </tgroup>
3858         </table>
3859         <table id="dpiox1">
3860           <title>Single channel PHY (CHV)</title>
3861           <tgroup cols="4">
3862             <colspec colname="c0" />
3863             <colspec colname="c1" />
3864             <colspec colname="c2" />
3865             <colspec colname="c3" />
3866             <spanspec spanname="ch0" namest="c0" nameend="c3" />
3867             <spanspec spanname="ch0pcs01" namest="c0" nameend="c1" />
3868             <spanspec spanname="ch0pcs23" namest="c2" nameend="c3" />
3869             <thead>
3870               <row>
3871                 <entry spanname="ch0">CH0</entry>
3872               </row>
3873             </thead>
3874             <tbody valign="top" align="center">
3875               <row>
3876                 <entry spanname="ch0">CMN/PLL/REF</entry>
3877               </row>
3878               <row>
3879                 <entry spanname="ch0pcs01">PCS01</entry>
3880                 <entry spanname="ch0pcs23">PCS23</entry>
3881               </row>
3882               <row>
3883                 <entry>TX0</entry>
3884                 <entry>TX1</entry>
3885                 <entry>TX2</entry>
3886                 <entry>TX3</entry>
3887               </row>
3888               <row>
3889                 <entry spanname="ch0">DDI2</entry>
3890               </row>
3891             </tbody>
3892           </tgroup>
3893         </table>
3894       </sect2>
3895     </sect1>
3896
3897     <sect1>
3898       <title>Memory Management and Command Submission</title>
3899       <para>
3900         This sections covers all things related to the GEM implementation in the
3901         i915 driver.
3902       </para>
3903       <sect2>
3904         <title>Batchbuffer Parsing</title>
3905 !Pdrivers/gpu/drm/i915/i915_cmd_parser.c batch buffer command parser
3906 !Idrivers/gpu/drm/i915/i915_cmd_parser.c
3907       </sect2>
3908     </sect1>
3909   </chapter>
3910 </part>
3911 </book>