Blame view

Documentation/DocBook/drm.tmpl 32.2 KB
2d2ef8227   Jesse Barnes   drm: add initial ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  	"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  
  <book id="drmDevelopersGuide">
    <bookinfo>
      <title>Linux DRM Developer's Guide</title>
  
      <copyright>
        <year>2008-2009</year>
        <holder>
  	Intel Corporation (Jesse Barnes &lt;jesse.barnes@intel.com&gt;)
        </holder>
      </copyright>
  
      <legalnotice>
        <para>
  	The contents of this file may be used under the terms of the GNU
  	General Public License version 2 (the "GPL") as distributed in
  	the kernel source COPYING file.
        </para>
      </legalnotice>
    </bookinfo>
  
  <toc></toc>
  
    <!-- Introduction -->
  
    <chapter id="drmIntroduction">
      <title>Introduction</title>
      <para>
        The Linux DRM layer contains code intended to support the needs
        of complex graphics devices, usually containing programmable
        pipelines well suited to 3D graphics acceleration.  Graphics
f11aca045   Michael Witten   DocBook/drm: can ...
35
        drivers in the kernel may make use of DRM functions to make
2d2ef8227   Jesse Barnes   drm: add initial ...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        tasks like memory management, interrupt handling and DMA easier,
        and provide a uniform interface to applications.
      </para>
      <para>
        A note on versions: this guide covers features found in the DRM
        tree, including the TTM memory manager, output configuration and
        mode setting, and the new vblank internals, in addition to all
        the regular features found in current kernels.
      </para>
      <para>
        [Insert diagram of typical DRM stack here]
      </para>
    </chapter>
  
    <!-- Internals -->
  
    <chapter id="drmInternals">
      <title>DRM Internals</title>
      <para>
        This chapter documents DRM internals relevant to driver authors
        and developers working to add support for the latest features to
        existing drivers.
      </para>
      <para>
a78f6787a   Michael Witten   DocBook/drm: Erad...
60
        First, we go over some typical driver initialization
2d2ef8227   Jesse Barnes   drm: add initial ...
61
62
        requirements, like setting up command buffers, creating an
        initial output configuration, and initializing core services.
a78f6787a   Michael Witten   DocBook/drm: Erad...
63
        Subsequent sections cover core internals in more detail,
2d2ef8227   Jesse Barnes   drm: add initial ...
64
65
66
67
68
69
70
71
72
73
74
75
        providing implementation notes and examples.
      </para>
      <para>
        The DRM layer provides several services to graphics drivers,
        many of them driven by the application interfaces it provides
        through libdrm, the library that wraps most of the DRM ioctls.
        These include vblank event handling, memory
        management, output management, framebuffer management, command
        submission &amp; fencing, suspend/resume support, and DMA
        services.
      </para>
      <para>
bd91572e7   Xiao Jiang   drm: fix wrong us...
76
        The core of every DRM driver is struct drm_driver.  Drivers
a78f6787a   Michael Witten   DocBook/drm: Erad...
77
        typically statically initialize a drm_driver structure,
2d2ef8227   Jesse Barnes   drm: add initial ...
78
79
80
81
82
83
84
85
86
        then pass it to drm_init() at load time.
      </para>
  
    <!-- Internals: driver init -->
  
    <sect1>
      <title>Driver initialization</title>
      <para>
        Before calling the DRM initialization routines, the driver must
bd91572e7   Xiao Jiang   drm: fix wrong us...
87
        first create and fill out a struct drm_driver structure.
2d2ef8227   Jesse Barnes   drm: add initial ...
88
89
90
      </para>
      <programlisting>
        static struct drm_driver driver = {
0c54781bc   Michael Witten   DocBook/drm: Clea...
91
92
  	/* Don't use MTRRs here; the Xserver or userspace app should
  	 * deal with them for Intel hardware.
2d2ef8227   Jesse Barnes   drm: add initial ...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  	 */
  	.driver_features =
  	    DRIVER_USE_AGP | DRIVER_REQUIRE_AGP |
  	    DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_MODESET,
  	.load = i915_driver_load,
  	.unload = i915_driver_unload,
  	.firstopen = i915_driver_firstopen,
  	.lastclose = i915_driver_lastclose,
  	.preclose = i915_driver_preclose,
  	.save = i915_save,
  	.restore = i915_restore,
  	.device_is_agp = i915_driver_device_is_agp,
  	.get_vblank_counter = i915_get_vblank_counter,
  	.enable_vblank = i915_enable_vblank,
  	.disable_vblank = i915_disable_vblank,
  	.irq_preinstall = i915_driver_irq_preinstall,
  	.irq_postinstall = i915_driver_irq_postinstall,
  	.irq_uninstall = i915_driver_irq_uninstall,
  	.irq_handler = i915_driver_irq_handler,
  	.reclaim_buffers = drm_core_reclaim_buffers,
  	.get_map_ofs = drm_core_get_map_ofs,
  	.get_reg_ofs = drm_core_get_reg_ofs,
  	.fb_probe = intelfb_probe,
  	.fb_remove = intelfb_remove,
  	.fb_resize = intelfb_resize,
  	.master_create = i915_master_create,
  	.master_destroy = i915_master_destroy,
  #if defined(CONFIG_DEBUG_FS)
  	.debugfs_init = i915_debugfs_init,
  	.debugfs_cleanup = i915_debugfs_cleanup,
  #endif
  	.gem_init_object = i915_gem_init_object,
  	.gem_free_object = i915_gem_free_object,
  	.gem_vm_ops = &amp;i915_gem_vm_ops,
  	.ioctls = i915_ioctls,
  	.fops = {
  		.owner = THIS_MODULE,
  		.open = drm_open,
  		.release = drm_release,
  		.ioctl = drm_ioctl,
  		.mmap = drm_mmap,
  		.poll = drm_poll,
  		.fasync = drm_fasync,
  #ifdef CONFIG_COMPAT
  		.compat_ioctl = i915_compat_ioctl,
  #endif
dc880abef   Arnd Bergmann   drm: use noop_llseek
139
  		.llseek = noop_llseek,
2d2ef8227   Jesse Barnes   drm: add initial ...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  		},
  	.pci_driver = {
  		.name = DRIVER_NAME,
  		.id_table = pciidlist,
  		.probe = probe,
  		.remove = __devexit_p(drm_cleanup_pci),
  		},
  	.name = DRIVER_NAME,
  	.desc = DRIVER_DESC,
  	.date = DRIVER_DATE,
  	.major = DRIVER_MAJOR,
  	.minor = DRIVER_MINOR,
  	.patchlevel = DRIVER_PATCHLEVEL,
        };
      </programlisting>
      <para>
        In the example above, taken from the i915 DRM driver, the driver
2c267e9e0   Michael Witten   DocBook/drm: Use ...
157
158
        sets several flags indicating what core features it supports;
        we go over the individual callbacks in later sections.  Since
2d2ef8227   Jesse Barnes   drm: add initial ...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
        flags indicate which features your driver supports to the DRM
        core, you need to set most of them prior to calling drm_init().  Some,
        like DRIVER_MODESET can be set later based on user supplied parameters,
        but that's the exception rather than the rule.
      </para>
      <variablelist>
        <title>Driver flags</title>
        <varlistentry>
  	<term>DRIVER_USE_AGP</term>
  	<listitem><para>
  	    Driver uses AGP interface
  	</para></listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_REQUIRE_AGP</term>
  	<listitem><para>
  	    Driver needs AGP interface to function.
  	</para></listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_USE_MTRR</term>
  	<listitem>
  	  <para>
  	    Driver uses MTRR interface for mapping memory.  Deprecated.
  	  </para>
  	</listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_PCI_DMA</term>
  	<listitem><para>
  	    Driver is capable of PCI DMA.  Deprecated.
  	</para></listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_SG</term>
  	<listitem><para>
  	    Driver can perform scatter/gather DMA.  Deprecated.
  	</para></listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_HAVE_DMA</term>
  	<listitem><para>Driver supports DMA.  Deprecated.</para></listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_HAVE_IRQ</term><term>DRIVER_IRQ_SHARED</term>
  	<listitem>
  	  <para>
02391f1fe   Michael Witten   DocBook/drm: a -> an
206
  	    DRIVER_HAVE_IRQ indicates whether the driver has an IRQ
b1f95bdc1   Michael Witten   DocBook/drm: , -> .
207
  	    handler.  DRIVER_IRQ_SHARED indicates whether the device &amp;
2d2ef8227   Jesse Barnes   drm: add initial ...
208
209
210
211
212
213
214
215
216
  	    handler support shared IRQs (note that this is required of
  	    PCI drivers).
  	  </para>
  	</listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_DMA_QUEUE</term>
  	<listitem>
  	  <para>
80c84e6f3   Michael Witten   DocBook/drm: Move...
217
218
  	    Should be set if the driver queues DMA requests and completes them
  	    asynchronously.  Deprecated.
2d2ef8227   Jesse Barnes   drm: add initial ...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  	  </para>
  	</listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_FB_DMA</term>
  	<listitem>
  	  <para>
  	    Driver supports DMA to/from the framebuffer.  Deprecated.
  	  </para>
  	</listitem>
        </varlistentry>
        <varlistentry>
  	<term>DRIVER_MODESET</term>
  	<listitem>
  	  <para>
  	    Driver supports mode setting interfaces.
  	  </para>
  	</listitem>
        </varlistentry>
      </variablelist>
      <para>
        In this specific case, the driver requires AGP and supports
a5294e01f   Michael Witten   DocBook/drm: `(de...
241
        IRQs.  DMA, as discussed later, is handled by device-specific ioctls
2d2ef8227   Jesse Barnes   drm: add initial ...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
        in this case.  It also supports the kernel mode setting APIs, though
        unlike in the actual i915 driver source, this example unconditionally
        exports KMS capability.
      </para>
    </sect1>
  
    <!-- Internals: driver load -->
  
    <sect1>
      <title>Driver load</title>
      <para>
        In the previous section, we saw what a typical drm_driver
        structure might look like.  One of the more important fields in
        the structure is the hook for the load function.
      </para>
      <programlisting>
        static struct drm_driver driver = {
        	...
        	.load = i915_driver_load,
          ...
        };
      </programlisting>
      <para>
        The load function has many responsibilities: allocating a driver
        private structure, specifying supported performance counters,
        configuring the device (e.g. mapping registers &amp; command
        buffers), initializing the memory manager, and setting up the
        initial output configuration.
      </para>
      <para>
6e375f44b   Michael Witten   DocBook/drm: Repl...
272
273
274
275
        If compatibility is a concern (e.g. with drivers converted over
        to the new interfaces from the old ones), care must be taken to
        prevent device initialization and control that is incompatible with
        currently active userspace drivers.  For instance, if user
2d2ef8227   Jesse Barnes   drm: add initial ...
276
277
        level mode setting drivers are in use, it would be problematic
        to perform output discovery &amp; configuration at load time.
58f1d652d   Michael Witten   DocBook/drm: Clea...
278
        Likewise, if user-level drivers unaware of memory management are
2d2ef8227   Jesse Barnes   drm: add initial ...
279
        in use, memory management and command buffer setup may need to
a5294e01f   Michael Witten   DocBook/drm: `(de...
280
        be omitted.  These requirements are driver-specific, and care
2d2ef8227   Jesse Barnes   drm: add initial ...
281
282
283
        needs to be taken to keep both old and new applications and
        libraries working.  The i915 driver supports the "modeset"
        module parameter to control whether advanced features are
6e375f44b   Michael Witten   DocBook/drm: Repl...
284
        enabled at load time or in legacy fashion.
2d2ef8227   Jesse Barnes   drm: add initial ...
285
286
287
288
289
290
      </para>
  
      <sect2>
        <title>Driver private &amp; performance counters</title>
        <para>
  	The driver private hangs off the main drm_device structure and
a5294e01f   Michael Witten   DocBook/drm: `(de...
291
  	can be used for tracking various device-specific bits of
2d2ef8227   Jesse Barnes   drm: add initial ...
292
293
  	information, like register offsets, command buffer status,
  	register state for suspend/resume, etc.  At load time, a
f11aca045   Michael Witten   DocBook/drm: can ...
294
  	driver may simply allocate one and set drm_device.dev_priv
06fa7b806   Michael Witten   DocBook/drm: Bett...
295
296
  	appropriately; it should be freed and drm_device.dev_priv set
  	to NULL when the driver is unloaded.
2d2ef8227   Jesse Barnes   drm: add initial ...
297
298
        </para>
        <para>
f11aca045   Michael Witten   DocBook/drm: can ...
299
  	The DRM supports several counters which may be used for rough
2d2ef8227   Jesse Barnes   drm: add initial ...
300
301
302
303
304
305
306
307
  	performance characterization.  Note that the DRM stat counter
  	system is not often used by applications, and supporting
  	additional counters is completely optional.
        </para>
        <para>
  	These interfaces are deprecated and should not be used.  If performance
  	monitoring is desired, the developer should investigate and
  	potentially enhance the kernel perf and tracing infrastructure to export
57a15fd66   Michael Witten   DocBook/drm: Clea...
308
309
  	GPU related performance information for consumption by performance
  	monitoring tools and applications.
2d2ef8227   Jesse Barnes   drm: add initial ...
310
311
312
313
314
315
        </para>
      </sect2>
  
      <sect2>
        <title>Configuring the device</title>
        <para>
a5294e01f   Michael Witten   DocBook/drm: `(de...
316
  	Obviously, device configuration is device-specific.
2d2ef8227   Jesse Barnes   drm: add initial ...
317
318
319
320
321
322
323
  	However, there are several common operations: finding a
  	device's PCI resources, mapping them, and potentially setting
  	up an IRQ handler.
        </para>
        <para>
  	Finding &amp; mapping resources is fairly straightforward.  The
  	DRM wrapper functions, drm_get_resource_start() and
8814630f0   Michael Witten   DocBook/drm: Inse...
324
  	drm_get_resource_len(), may be used to find BARs on the given
2d2ef8227   Jesse Barnes   drm: add initial ...
325
326
  	drm_device struct.  Once those values have been retrieved, the
  	driver load function can call drm_addmap() to create a new
8814630f0   Michael Witten   DocBook/drm: Inse...
327
  	mapping for the BAR in question.  Note that you probably want a
2d2ef8227   Jesse Barnes   drm: add initial ...
328
329
330
331
332
333
334
335
  	drm_local_map_t in your driver private structure to track any
  	mappings you create.
  <!-- !Fdrivers/gpu/drm/drm_bufs.c drm_get_resource_* -->
  <!-- !Finclude/drm/drmP.h drm_local_map_t -->
        </para>
        <para>
  	if compatibility with other operating systems isn't a concern
  	(DRM drivers can run under various BSD variants and OpenSolaris),
f11aca045   Michael Witten   DocBook/drm: can ...
336
  	native Linux calls may be used for the above, e.g. pci_resource_*
2d2ef8227   Jesse Barnes   drm: add initial ...
337
338
339
340
  	and iomap*/iounmap.  See the Linux device driver book for more
  	info.
        </para>
        <para>
f11aca045   Michael Witten   DocBook/drm: can ...
341
  	Once you have a register map, you may use the DRM_READn() and
2d2ef8227   Jesse Barnes   drm: add initial ...
342
  	DRM_WRITEn() macros to access the registers on your device, or
a5294e01f   Michael Witten   DocBook/drm: `(de...
343
344
  	use driver-specific versions to offset into your MMIO space
  	relative to a driver-specific base pointer (see I915_READ for
f07faf693   Michael Witten   DocBook/drm: Inse...
345
  	an example).
2d2ef8227   Jesse Barnes   drm: add initial ...
346
347
348
        </para>
        <para>
  	If your device supports interrupt generation, you may want to
bb49a6a1f   Michael Witten   DocBook/drm: `at ...
349
  	set up an interrupt handler when the driver is loaded.  This
2d2ef8227   Jesse Barnes   drm: add initial ...
350
351
352
353
354
355
356
357
  	is done using the drm_irq_install() function.  If your device
  	supports vertical blank interrupts, it should call
  	drm_vblank_init() to initialize the core vblank handling code before
  	enabling interrupts on your device.  This ensures the vblank related
  	structures are allocated and allows the core to handle vblank events.
        </para>
  <!--!Fdrivers/char/drm/drm_irq.c drm_irq_install-->
        <para>
a78f6787a   Michael Witten   DocBook/drm: Erad...
358
  	Once your interrupt handler is registered (it uses your
2d2ef8227   Jesse Barnes   drm: add initial ...
359
360
361
362
363
364
365
366
367
368
369
370
371
  	drm_driver.irq_handler as the actual interrupt handling
  	function), you can safely enable interrupts on your device,
  	assuming any other state your interrupt handler uses is also
  	initialized.
        </para>
        <para>
  	Another task that may be necessary during configuration is
  	mapping the video BIOS.  On many devices, the VBIOS describes
  	device configuration, LCD panel timings (if any), and contains
  	flags indicating device state.  Mapping the BIOS can be done
  	using the pci_map_rom() call, a convenience function that
  	takes care of mapping the actual ROM, whether it has been
  	shadowed into memory (typically at address 0xc0000) or exists
9c2416ada   Michael Witten   DocBook/drm: Use ...
372
373
  	on the PCI device in the ROM BAR.  Note that after the ROM
  	has been mapped and any necessary information has been extracted,
118bdd70b   Michael Witten   DocBook/drm: Offs...
374
  	it should be unmapped; on many devices, the ROM address decoder is
8d36ffae6   Michael Witten   DocBook/drm: can ...
375
  	shared with other BARs, so leaving it mapped could cause
2d2ef8227   Jesse Barnes   drm: add initial ...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
  	undesired behavior like hangs or memory corruption.
  <!--!Fdrivers/pci/rom.c pci_map_rom-->
        </para>
      </sect2>
  
      <sect2>
        <title>Memory manager initialization</title>
        <para>
  	In order to allocate command buffers, cursor memory, scanout
  	buffers, etc., as well as support the latest features provided
  	by packages like Mesa and the X.Org X server, your driver
  	should support a memory manager.
        </para>
        <para>
a78f6787a   Michael Witten   DocBook/drm: Erad...
390
  	If your driver supports memory management (it should!), you
ce04cc089   Nicolas Kaiser   drm: fix typos in...
391
  	need to set that up at load time as well.  How you initialize
eb2b8d427   Michael Witten   DocBook/drm: , -> :
392
  	it depends on which memory manager you're using: TTM or GEM.
2d2ef8227   Jesse Barnes   drm: add initial ...
393
394
395
396
397
398
399
400
        </para>
        <sect3>
  	<title>TTM initialization</title>
  	<para>
  	  TTM (for Translation Table Manager) manages video memory and
  	  aperture space for graphics devices. TTM supports both UMA devices
  	  and devices with dedicated video RAM (VRAM), i.e. most discrete
  	  graphics devices.  If your device has dedicated RAM, supporting
ce04cc089   Nicolas Kaiser   drm: fix typos in...
401
  	  TTM is desirable.  TTM also integrates tightly with your
a5294e01f   Michael Witten   DocBook/drm: `(de...
402
  	  driver-specific buffer execution function.  See the radeon
2d2ef8227   Jesse Barnes   drm: add initial ...
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
  	  driver for examples.
  	</para>
  	<para>
  	  The core TTM structure is the ttm_bo_driver struct.  It contains
  	  several fields with function pointers for initializing the TTM,
  	  allocating and freeing memory, waiting for command completion
  	  and fence synchronization, and memory migration.  See the
  	  radeon_ttm.c file for an example of usage.
  	</para>
  	<para>
  	  The ttm_global_reference structure is made up of several fields:
  	</para>
  	<programlisting>
  	  struct ttm_global_reference {
  	  	enum ttm_global_types global_type;
  	  	size_t size;
  	  	void *object;
  	  	int (*init) (struct ttm_global_reference *);
  	  	void (*release) (struct ttm_global_reference *);
  	  };
  	</programlisting>
  	<para>
  	  There should be one global reference structure for your memory
  	  manager as a whole, and there will be others for each object
  	  created by the memory manager at runtime.  Your global TTM should
  	  have a type of TTM_GLOBAL_TTM_MEM.  The size field for the global
  	  object should be sizeof(struct ttm_mem_global), and the init and
a5294e01f   Michael Witten   DocBook/drm: `(de...
430
  	  release hooks should point at your driver-specific init and
a78f6787a   Michael Witten   DocBook/drm: Erad...
431
  	  release routines, which probably eventually call
005d7f4a0   Michael Witten   DocBook/drm: Inse...
432
  	  ttm_mem_global_init and ttm_mem_global_release, respectively.
2d2ef8227   Jesse Barnes   drm: add initial ...
433
434
435
  	</para>
  	<para>
  	  Once your global TTM accounting structure is set up and initialized
ae63d793a   Michael Witten   DocBook/drm: Inse...
436
  	  by calling ttm_global_item_ref() on it,
1c86de221   Michael Witten   DocBook/drm: Remo...
437
  	  you need to create a buffer object TTM to
2d2ef8227   Jesse Barnes   drm: add initial ...
438
439
440
  	  provide a pool for buffer object allocation by clients and the
  	  kernel itself.  The type of this object should be TTM_GLOBAL_TTM_BO,
  	  and its size should be sizeof(struct ttm_bo_global).  Again,
a5294e01f   Michael Witten   DocBook/drm: `(de...
441
  	  driver-specific init and release functions may be provided,
ae63d793a   Michael Witten   DocBook/drm: Inse...
442
443
444
  	  likely eventually calling ttm_bo_global_init() and
  	  ttm_bo_global_release(), respectively.  Also, like the previous
  	  object, ttm_global_item_ref() is used to create an initial reference
ce04cc089   Nicolas Kaiser   drm: fix typos in...
445
  	  count for the TTM, which will call your initialization function.
2d2ef8227   Jesse Barnes   drm: add initial ...
446
447
448
449
450
451
452
453
  	</para>
        </sect3>
        <sect3>
  	<title>GEM initialization</title>
  	<para>
  	  GEM is an alternative to TTM, designed specifically for UMA
  	  devices.  It has simpler initialization and execution requirements
  	  than TTM, but has no VRAM management capability.  Core GEM
049cc903e   Michael Witten   DocBook/drm: Stre...
454
  	  is initialized by calling drm_mm_init() to create
2d2ef8227   Jesse Barnes   drm: add initial ...
455
  	  a GTT DRM MM object, which provides an address space pool for
a78f6787a   Michael Witten   DocBook/drm: Erad...
456
457
  	  object allocation.  In a KMS configuration, the driver
  	  needs to allocate and initialize a command ring buffer following
9029bd7a4   Michael Witten   DocBook/drm: The ...
458
  	  core GEM initialization.  A UMA device usually has what is called a
2d2ef8227   Jesse Barnes   drm: add initial ...
459
460
  	  "stolen" memory region, which provides space for the initial
  	  framebuffer and large, contiguous memory regions required by the
1dbd39c3e   Michael Witten   DocBook/drm: Inse...
461
  	  device.  This space is not typically managed by GEM, and it must
2d2ef8227   Jesse Barnes   drm: add initial ...
462
463
464
  	  be initialized separately into its own DRM MM object.
  	</para>
  	<para>
a5294e01f   Michael Witten   DocBook/drm: `(de...
465
  	  Initialization is driver-specific. In the case of Intel
2d2ef8227   Jesse Barnes   drm: add initial ...
466
467
468
  	  integrated graphics chips like 965GM, GEM initialization can
  	  be done by calling the internal GEM init function,
  	  i915_gem_do_init().  Since the 965GM is a UMA device
a78f6787a   Michael Witten   DocBook/drm: Erad...
469
  	  (i.e. it doesn't have dedicated VRAM), GEM manages
2d2ef8227   Jesse Barnes   drm: add initial ...
470
471
  	  making regular RAM available for GPU operations.  Memory set
  	  aside by the BIOS (called "stolen" memory by the i915
a78f6787a   Michael Witten   DocBook/drm: Erad...
472
473
  	  driver) is managed by the DRM memrange allocator; the
  	  rest of the aperture is managed by GEM.
2d2ef8227   Jesse Barnes   drm: add initial ...
474
475
476
477
478
479
480
481
482
  	  <programlisting>
  	    /* Basic memrange allocator for stolen space (aka vram) */
  	    drm_memrange_init(&amp;dev_priv->vram, 0, prealloc_size);
  	    /* Let GEM Manage from end of prealloc space to end of aperture */
  	    i915_gem_do_init(dev, prealloc_size, agp_size);
  	  </programlisting>
  <!--!Edrivers/char/drm/drm_memrange.c-->
  	</para>
  	<para>
f11aca045   Michael Witten   DocBook/drm: can ...
483
  	  Once the memory manager has been set up, we may allocate the
2d2ef8227   Jesse Barnes   drm: add initial ...
484
485
486
487
488
489
490
491
492
  	  command buffer.  In the i915 case, this is also done with a
  	  GEM function, i915_gem_init_ringbuffer().
  	</para>
        </sect3>
      </sect2>
  
      <sect2>
        <title>Output configuration</title>
        <para>
327d6fb96   Michael Witten   DocBook/drm: Clar...
493
494
495
496
497
498
499
500
501
502
503
504
505
  	The final initialization task is output configuration.  This involves:
  	<itemizedlist>
  	  <listitem>
  	    Finding and initializing the CRTCs, encoders, and connectors
  	    for the device.
  	  </listitem>
  	  <listitem>
  	    Creating an initial configuration.
  	  </listitem>
  	  <listitem>
  	    Registering a framebuffer console driver.
  	  </listitem>
  	</itemizedlist>
2d2ef8227   Jesse Barnes   drm: add initial ...
506
507
508
509
        </para>
        <sect3>
  	<title>Output discovery and initialization</title>
  	<para>
005d7f4a0   Michael Witten   DocBook/drm: Inse...
510
  	  Several core functions exist to create CRTCs, encoders, and
8a9ba910a   Michael Witten   DocBook/drm: Use ...
511
  	  connectors, namely: drm_crtc_init(), drm_connector_init(), and
2d2ef8227   Jesse Barnes   drm: add initial ...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  	  drm_encoder_init(), along with several "helper" functions to
  	  perform common tasks.
  	</para>
  	<para>
  	  Connectors should be registered with sysfs once they've been
  	  detected and initialized, using the
  	  drm_sysfs_connector_add() function.  Likewise, when they're
  	  removed from the system, they should be destroyed with
  	  drm_sysfs_connector_remove().
  	</para>
  	<programlisting>
  <![CDATA[
  void intel_crt_init(struct drm_device *dev)
  {
  	struct drm_connector *connector;
  	struct intel_output *intel_output;
  
  	intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
  	if (!intel_output)
  		return;
  
  	connector = &intel_output->base;
  	drm_connector_init(dev, &intel_output->base,
  			   &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  
  	drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
  			 DRM_MODE_ENCODER_DAC);
  
  	drm_mode_connector_attach_encoder(&intel_output->base,
  					  &intel_output->enc);
  
  	/* Set up the DDC bus. */
  	intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
  	if (!intel_output->ddc_bus) {
  		dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  			   "failed.
  ");
  		return;
  	}
  
  	intel_output->type = INTEL_OUTPUT_ANALOG;
  	connector->interlace_allowed = 0;
  	connector->doublescan_allowed = 0;
  
  	drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
  	drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  
  	drm_sysfs_connector_add(connector);
  }
  ]]>
  	</programlisting>
  	<para>
  	  In the example above (again, taken from the i915 driver), a
a5294e01f   Michael Witten   DocBook/drm: `(de...
565
566
  	  CRT connector and encoder combination is created.  A device-specific
  	  i2c bus is also created for fetching EDID data and
2d2ef8227   Jesse Barnes   drm: add initial ...
567
  	  performing monitor detection.  Once the process is complete,
896ee65fb   Michael Witten   DocBook/drm: Remo...
568
  	  the new connector is registered with sysfs to make its
2d2ef8227   Jesse Barnes   drm: add initial ...
569
570
571
572
573
574
575
576
  	  properties available to applications.
  	</para>
  	<sect4>
  	  <title>Helper functions and core functions</title>
  	  <para>
  	    Since many PC-class graphics devices have similar display output
  	    designs, the DRM provides a set of helper functions to make
  	    output management easier.  The core helper routines handle
4dc0152d5   Michael Witten   DocBook/drm: Inse...
577
578
  	    encoder re-routing and the disabling of unused functions following
  	    mode setting.  Using the helpers is optional, but recommended for
2d2ef8227   Jesse Barnes   drm: add initial ...
579
580
581
  	    devices with PC-style architectures (i.e. a set of display planes
  	    for feeding pixels to encoders which are in turn routed to
  	    connectors).  Devices with more complex requirements needing
f11aca045   Michael Witten   DocBook/drm: can ...
582
  	    finer grained management may opt to use the core callbacks
2d2ef8227   Jesse Barnes   drm: add initial ...
583
584
585
586
587
588
589
  	    directly.
  	  </para>
  	  <para>
  	    [Insert typical diagram here.]  [Insert OMAP style config here.]
  	  </para>
  	</sect4>
  	<para>
65ffef508   Michael Witten   DocBook/drm: Use ...
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  	  Each encoder object needs to provide:
  	  <itemizedlist>
  	    <listitem>
  	      A DPMS (basically on/off) function.
  	    </listitem>
  	    <listitem>
  	      A mode-fixup function (for converting requested modes into
  	      native hardware timings).
  	    </listitem>
  	    <listitem>
  	      Functions (prepare, set, and commit) for use by the core DRM
  	      helper functions.
  	    </listitem>
  	  </itemizedlist>
  	  Connector helpers need to provide functions (mode-fetch, validity,
  	  and encoder-matching) for returning an ideal encoder for a given
  	  connector.  The core connector functions include a DPMS callback,
  	  save/restore routines (deprecated), detection, mode probing,
  	  property handling, and cleanup functions.
2d2ef8227   Jesse Barnes   drm: add initial ...
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  	</para>
  <!--!Edrivers/char/drm/drm_crtc.h-->
  <!--!Edrivers/char/drm/drm_crtc.c-->
  <!--!Edrivers/char/drm/drm_crtc_helper.c-->
        </sect3>
      </sect2>
    </sect1>
  
    <!-- Internals: vblank handling -->
  
    <sect1>
      <title>VBlank event handling</title>
      <para>
        The DRM core exposes two vertical blank related ioctls:
51b9500de   Michael Witten   DocBook/drm: Use ...
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
        <variablelist>
          <varlistentry>
            <term>DRM_IOCTL_WAIT_VBLANK</term>
            <listitem>
              <para>
                This takes a struct drm_wait_vblank structure as its argument,
                and it is used to block or request a signal when a specified
                vblank event occurs.
              </para>
            </listitem>
          </varlistentry>
          <varlistentry>
            <term>DRM_IOCTL_MODESET_CTL</term>
            <listitem>
              <para>
                This should be called by application level drivers before and
                after mode setting, since on many devices the vertical blank
                counter is reset at that time.  Internally, the DRM snapshots
                the last vblank count when the ioctl is called with the
f877bd4ad   Michael Witten   DocBook/drm: Inse...
642
                _DRM_PRE_MODESET command, so that the counter won't go backwards
51b9500de   Michael Witten   DocBook/drm: Use ...
643
644
645
646
647
                (which is dealt with when _DRM_POST_MODESET is used).
              </para>
            </listitem>
          </varlistentry>
        </variablelist>
2d2ef8227   Jesse Barnes   drm: add initial ...
648
649
650
  <!--!Edrivers/char/drm/drm_irq.c-->
      </para>
      <para>
2d2ef8227   Jesse Barnes   drm: add initial ...
651
652
653
654
655
656
657
658
659
660
        To support the functions above, the DRM core provides several
        helper functions for tracking vertical blank counters, and
        requires drivers to provide several callbacks:
        get_vblank_counter(), enable_vblank() and disable_vblank().  The
        core uses get_vblank_counter() to keep the counter accurate
        across interrupt disable periods.  It should return the current
        vertical blank event count, which is often tracked in a device
        register.  The enable and disable vblank callbacks should enable
        and disable vertical blank interrupts, respectively.  In the
        absence of DRM clients waiting on vblank events, the core DRM
a78f6787a   Michael Witten   DocBook/drm: Erad...
661
662
        code uses the disable_vblank() function to disable
        interrupts, which saves power.  They are re-enabled again when
2d2ef8227   Jesse Barnes   drm: add initial ...
663
664
665
        a client calls the vblank wait ioctl above.
      </para>
      <para>
54f2cb8fc   Michael Witten   DocBook/drm: Use ...
666
        A device that doesn't provide a count register may simply use an
2d2ef8227   Jesse Barnes   drm: add initial ...
667
        internal atomic counter incremented on every vertical blank
54f2cb8fc   Michael Witten   DocBook/drm: Use ...
668
669
        interrupt (and then treat the enable_vblank() and disable_vblank()
        callbacks as no-ops).
2d2ef8227   Jesse Barnes   drm: add initial ...
670
671
672
673
674
675
      </para>
    </sect1>
  
    <sect1>
      <title>Memory management</title>
      <para>
2c267e9e0   Michael Witten   DocBook/drm: Use ...
676
677
        The memory manager lies at the heart of many DRM operations; it
        is required to support advanced client features like OpenGL
eb2b8d427   Michael Witten   DocBook/drm: , -> :
678
        pbuffers.  The DRM currently contains two memory managers: TTM
2d2ef8227   Jesse Barnes   drm: add initial ...
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
        and GEM.
      </para>
  
      <sect2>
        <title>The Translation Table Manager (TTM)</title>
        <para>
  	TTM was developed by Tungsten Graphics, primarily by Thomas
  	Hellström, and is intended to be a flexible, high performance
  	graphics memory manager.
        </para>
        <para>
  	Drivers wishing to support TTM must fill out a drm_bo_driver
  	structure.
        </para>
        <para>
  	TTM design background and information belongs here.
        </para>
      </sect2>
  
      <sect2>
        <title>The Graphics Execution Manager (GEM)</title>
        <para>
  	GEM is an Intel project, authored by Eric Anholt and Keith
  	Packard.  It provides simpler interfaces than TTM, and is well
  	suited for UMA devices.
        </para>
        <para>
  	GEM-enabled drivers must provide gem_init_object() and
  	gem_free_object() callbacks to support the core memory
a5294e01f   Michael Witten   DocBook/drm: `(de...
708
709
  	allocation routines.  They should also provide several driver-specific
  	ioctls to support command execution, pinning, buffer
2d2ef8227   Jesse Barnes   drm: add initial ...
710
711
712
  	read &amp; write, mapping, and domain ownership transfers.
        </para>
        <para>
0c2d91a80   Michael Witten   DocBook/drm: Use ...
713
714
715
716
717
718
719
  	On a fundamental level, GEM involves several operations:
  	<itemizedlist>
  	  <listitem>Memory allocation and freeing</listitem>
  	  <listitem>Command execution</listitem>
  	  <listitem>Aperture management at command execution time</listitem>
  	</itemizedlist>
  	Buffer object allocation is relatively
2d2ef8227   Jesse Barnes   drm: add initial ...
720
721
722
723
  	straightforward and largely provided by Linux's shmem layer, which
  	provides memory to back each object.  When mapped into the GTT
  	or used in a command buffer, the backing pages for an object are
  	flushed to memory and marked write combined so as to be coherent
2d43f5d66   Michael Witten   DocBook/drm: Impr...
724
725
726
  	with the GPU.  Likewise, if the CPU accesses an object after the GPU
  	has finished rendering to the object, then the object must be made
  	coherent with the CPU's view
2d2ef8227   Jesse Barnes   drm: add initial ...
727
  	of memory, usually involving GPU cache flushing of various kinds.
b8c6e0fe4   Michael Witten   DocBook/drm: Refe...
728
729
  	This core CPU&lt;-&gt;GPU coherency management is provided by a
  	device-specific ioctl, which evaluates an object's current domain and
2d2ef8227   Jesse Barnes   drm: add initial ...
730
731
  	performs any necessary flushing or synchronization to put the object
  	into the desired coherency domain (note that the object may be busy,
b8c6e0fe4   Michael Witten   DocBook/drm: Refe...
732
  	i.e. an active render target; in that case, setting the domain
a78f6787a   Michael Witten   DocBook/drm: Erad...
733
  	blocks the client and waits for rendering to complete before
2d2ef8227   Jesse Barnes   drm: add initial ...
734
735
736
737
738
  	performing any necessary flushing operations).
        </para>
        <para>
  	Perhaps the most important GEM function is providing a command
  	execution interface to clients.  Client programs construct command
e355b2014   Michael Witten   DocBook/drm: Bett...
739
740
  	buffers containing references to previously allocated memory objects,
  	and then submit them to GEM.  At that point, GEM takes care to bind
2d2ef8227   Jesse Barnes   drm: add initial ...
741
742
743
744
745
746
  	all the objects into the GTT, execute the buffer, and provide
  	necessary synchronization between clients accessing the same buffers.
  	This often involves evicting some objects from the GTT and re-binding
  	others (a fairly expensive operation), and providing relocation
  	support which hides fixed GTT offsets from clients.  Clients must
  	take care not to submit command buffers that reference more objects
964d32dcb   Michael Witten   DocBook/drm: Use ...
747
  	than can fit in the GTT; otherwise, GEM will reject them and no rendering
2d2ef8227   Jesse Barnes   drm: add initial ...
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
  	will occur.  Similarly, if several objects in the buffer require
  	fence registers to be allocated for correct rendering (e.g. 2D blits
  	on pre-965 chips), care must be taken not to require more fence
  	registers than are available to the client.  Such resource management
  	should be abstracted from the client in libdrm.
        </para>
      </sect2>
  
    </sect1>
  
    <!-- Output management -->
    <sect1>
      <title>Output management</title>
      <para>
        At the core of the DRM output management code is a set of
005d7f4a0   Michael Witten   DocBook/drm: Inse...
763
        structures representing CRTCs, encoders, and connectors.
2d2ef8227   Jesse Barnes   drm: add initial ...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
      </para>
      <para>
        A CRTC is an abstraction representing a part of the chip that
        contains a pointer to a scanout buffer.  Therefore, the number
        of CRTCs available determines how many independent scanout
        buffers can be active at any given time.  The CRTC structure
        contains several fields to support this: a pointer to some video
        memory, a display mode, and an (x, y) offset into the video
        memory to support panning or configurations where one piece of
        video memory spans multiple CRTCs.
      </para>
      <para>
        An encoder takes pixel data from a CRTC and converts it to a
        format suitable for any attached connectors.  On some devices,
        it may be possible to have a CRTC send data to more than one
        encoder.  In that case, both encoders would receive data from
        the same scanout buffer, resulting in a "cloned" display
        configuration across the connectors attached to each encoder.
      </para>
      <para>
        A connector is the final destination for pixel data on a device,
        and usually connects directly to an external display device like
        a monitor or laptop panel.  A connector can only be attached to
        one encoder at a time.  The connector is also the structure
        where information about the attached display is kept, so it
        contains fields for display data, EDID data, DPMS &amp;
        connection status, and information about modes supported on the
        attached displays.
      </para>
  <!--!Edrivers/char/drm/drm_crtc.c-->
    </sect1>
  
    <sect1>
      <title>Framebuffer management</title>
      <para>
5a462d58c   Michael Witten   DocBook/drm: Clea...
799
800
        Clients need to provide a framebuffer object which provides a source
        of pixels for a CRTC to deliver to the encoder(s) and ultimately the
a5294e01f   Michael Witten   DocBook/drm: `(de...
801
        connector(s). A framebuffer is fundamentally a driver-specific memory
5a462d58c   Michael Witten   DocBook/drm: Clea...
802
803
804
        object, made into an opaque handle by the DRM's addfb() function.
        Once a framebuffer has been created this way, it may be passed to the
        KMS mode setting routines for use in a completed configuration.
2d2ef8227   Jesse Barnes   drm: add initial ...
805
806
807
808
809
810
      </para>
    </sect1>
  
    <sect1>
      <title>Command submission &amp; fencing</title>
      <para>
a5294e01f   Michael Witten   DocBook/drm: `(de...
811
        This should cover a few device-specific command submission
2d2ef8227   Jesse Barnes   drm: add initial ...
812
813
814
815
816
817
818
819
820
        implementations.
      </para>
    </sect1>
  
    <sect1>
      <title>Suspend/resume</title>
      <para>
        The DRM core provides some suspend/resume code, but drivers
        wanting full suspend/resume support should provide save() and
a78f6787a   Michael Witten   DocBook/drm: Erad...
821
        restore() functions.  These are called at suspend,
2d2ef8227   Jesse Barnes   drm: add initial ...
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
        hibernate, or resume time, and should perform any state save or
        restore required by your device across suspend or hibernate
        states.
      </para>
    </sect1>
  
    <sect1>
      <title>DMA services</title>
      <para>
        This should cover how DMA mapping etc. is supported by the core.
        These functions are deprecated and should not be used.
      </para>
    </sect1>
    </chapter>
  
    <!-- External interfaces -->
  
    <chapter id="drmExternals">
      <title>Userland interfaces</title>
      <para>
        The DRM core exports several interfaces to applications,
        generally intended to be used through corresponding libdrm
a5294e01f   Michael Witten   DocBook/drm: `(de...
844
        wrapper functions.  In addition, drivers export device-specific
7f0925aca   Michael Witten   DocBook/drm: `dev...
845
        interfaces for use by userspace drivers &amp; device-aware
2d2ef8227   Jesse Barnes   drm: add initial ...
846
847
848
849
850
851
852
853
        applications through ioctls and sysfs files.
      </para>
      <para>
        External interfaces include: memory mapping, context management,
        DMA operations, AGP management, vblank control, fence
        management, memory management, and output management.
      </para>
      <para>
bcd3cfc12   Michael Witten   DocBook/drm: Clea...
854
855
        Cover generic ioctls and sysfs layout here.  We only need high-level
        info, since man pages should cover the rest.
2d2ef8227   Jesse Barnes   drm: add initial ...
856
857
858
859
860
861
862
863
864
865
866
867
868
869
      </para>
    </chapter>
  
    <!-- API reference -->
  
    <appendix id="drmDriverApi">
      <title>DRM Driver API</title>
      <para>
        Include auto-generated API reference here (need to reference it
        from paragraphs above too).
      </para>
    </appendix>
  
  </book>