drm.tmpl 32 KB

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