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