drm.tmpl 32 KB

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