drm.tmpl 113 KB

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