drm.tmpl 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  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. <sect4>
  733. <title>DRM PRIME Helper Functions Reference</title>
  734. !Pdrivers/gpu/drm/drm_prime.c PRIME Helpers
  735. </sect4>
  736. </sect3>
  737. <sect3 id="drm-gem-objects-mapping">
  738. <title>GEM Objects Mapping</title>
  739. <para>
  740. Because mapping operations are fairly heavyweight GEM favours
  741. read/write-like access to buffers, implemented through driver-specific
  742. ioctls, over mapping buffers to userspace. However, when random access
  743. to the buffer is needed (to perform software rendering for instance),
  744. direct access to the object can be more efficient.
  745. </para>
  746. <para>
  747. The mmap system call can't be used directly to map GEM objects, as they
  748. don't have their own file handle. Two alternative methods currently
  749. co-exist to map GEM objects to userspace. The first method uses a
  750. driver-specific ioctl to perform the mapping operation, calling
  751. <function>do_mmap</function> under the hood. This is often considered
  752. dubious, seems to be discouraged for new GEM-enabled drivers, and will
  753. thus not be described here.
  754. </para>
  755. <para>
  756. The second method uses the mmap system call on the DRM file handle.
  757. <synopsis>void *mmap(void *addr, size_t length, int prot, int flags, int fd,
  758. off_t offset);</synopsis>
  759. DRM identifies the GEM object to be mapped by a fake offset passed
  760. through the mmap offset argument. Prior to being mapped, a GEM object
  761. must thus be associated with a fake offset. To do so, drivers must call
  762. <function>drm_gem_create_mmap_offset</function> on the object. The
  763. function allocates a fake offset range from a pool and stores the
  764. offset divided by PAGE_SIZE in
  765. <literal>obj-&gt;map_list.hash.key</literal>. Care must be taken not to
  766. call <function>drm_gem_create_mmap_offset</function> if a fake offset
  767. has already been allocated for the object. This can be tested by
  768. <literal>obj-&gt;map_list.map</literal> being non-NULL.
  769. </para>
  770. <para>
  771. Once allocated, the fake offset value
  772. (<literal>obj-&gt;map_list.hash.key &lt;&lt; PAGE_SHIFT</literal>)
  773. must be passed to the application in a driver-specific way and can then
  774. be used as the mmap offset argument.
  775. </para>
  776. <para>
  777. The GEM core provides a helper method <function>drm_gem_mmap</function>
  778. to handle object mapping. The method can be set directly as the mmap
  779. file operation handler. It will look up the GEM object based on the
  780. offset value and set the VMA operations to the
  781. <structname>drm_driver</structname> <structfield>gem_vm_ops</structfield>
  782. field. Note that <function>drm_gem_mmap</function> doesn't map memory to
  783. userspace, but relies on the driver-provided fault handler to map pages
  784. individually.
  785. </para>
  786. <para>
  787. To use <function>drm_gem_mmap</function>, drivers must fill the struct
  788. <structname>drm_driver</structname> <structfield>gem_vm_ops</structfield>
  789. field with a pointer to VM operations.
  790. </para>
  791. <para>
  792. <synopsis>struct vm_operations_struct *gem_vm_ops
  793. struct vm_operations_struct {
  794. void (*open)(struct vm_area_struct * area);
  795. void (*close)(struct vm_area_struct * area);
  796. int (*fault)(struct vm_area_struct *vma, struct vm_fault *vmf);
  797. };</synopsis>
  798. </para>
  799. <para>
  800. The <methodname>open</methodname> and <methodname>close</methodname>
  801. operations must update the GEM object reference count. Drivers can use
  802. the <function>drm_gem_vm_open</function> and
  803. <function>drm_gem_vm_close</function> helper functions directly as open
  804. and close handlers.
  805. </para>
  806. <para>
  807. The fault operation handler is responsible for mapping individual pages
  808. to userspace when a page fault occurs. Depending on the memory
  809. allocation scheme, drivers can allocate pages at fault time, or can
  810. decide to allocate memory for the GEM object at the time the object is
  811. created.
  812. </para>
  813. <para>
  814. Drivers that want to map the GEM object upfront instead of handling page
  815. faults can implement their own mmap file operation handler.
  816. </para>
  817. </sect3>
  818. <sect3>
  819. <title>Dumb GEM Objects</title>
  820. <para>
  821. The GEM API doesn't standardize GEM objects creation and leaves it to
  822. driver-specific ioctls. While not an issue for full-fledged graphics
  823. stacks that include device-specific userspace components (in libdrm for
  824. instance), this limit makes DRM-based early boot graphics unnecessarily
  825. complex.
  826. </para>
  827. <para>
  828. Dumb GEM objects partly alleviate the problem by providing a standard
  829. API to create dumb buffers suitable for scanout, which can then be used
  830. to create KMS frame buffers.
  831. </para>
  832. <para>
  833. To support dumb GEM objects drivers must implement the
  834. <methodname>dumb_create</methodname>,
  835. <methodname>dumb_destroy</methodname> and
  836. <methodname>dumb_map_offset</methodname> operations.
  837. </para>
  838. <itemizedlist>
  839. <listitem>
  840. <synopsis>int (*dumb_create)(struct drm_file *file_priv, struct drm_device *dev,
  841. struct drm_mode_create_dumb *args);</synopsis>
  842. <para>
  843. The <methodname>dumb_create</methodname> operation creates a GEM
  844. object suitable for scanout based on the width, height and depth
  845. from the struct <structname>drm_mode_create_dumb</structname>
  846. argument. It fills the argument's <structfield>handle</structfield>,
  847. <structfield>pitch</structfield> and <structfield>size</structfield>
  848. fields with a handle for the newly created GEM object and its line
  849. pitch and size in bytes.
  850. </para>
  851. </listitem>
  852. <listitem>
  853. <synopsis>int (*dumb_destroy)(struct drm_file *file_priv, struct drm_device *dev,
  854. uint32_t handle);</synopsis>
  855. <para>
  856. The <methodname>dumb_destroy</methodname> operation destroys a dumb
  857. GEM object created by <methodname>dumb_create</methodname>.
  858. </para>
  859. </listitem>
  860. <listitem>
  861. <synopsis>int (*dumb_map_offset)(struct drm_file *file_priv, struct drm_device *dev,
  862. uint32_t handle, uint64_t *offset);</synopsis>
  863. <para>
  864. The <methodname>dumb_map_offset</methodname> operation associates an
  865. mmap fake offset with the GEM object given by the handle and returns
  866. it. Drivers must use the
  867. <function>drm_gem_create_mmap_offset</function> function to
  868. associate the fake offset as described in
  869. <xref linkend="drm-gem-objects-mapping"/>.
  870. </para>
  871. </listitem>
  872. </itemizedlist>
  873. </sect3>
  874. <sect3>
  875. <title>Memory Coherency</title>
  876. <para>
  877. When mapped to the device or used in a command buffer, backing pages
  878. for an object are flushed to memory and marked write combined so as to
  879. be coherent with the GPU. Likewise, if the CPU accesses an object
  880. after the GPU has finished rendering to the object, then the object
  881. must be made coherent with the CPU's view of memory, usually involving
  882. GPU cache flushing of various kinds. This core CPU&lt;-&gt;GPU
  883. coherency management is provided by a device-specific ioctl, which
  884. evaluates an object's current domain and performs any necessary
  885. flushing or synchronization to put the object into the desired
  886. coherency domain (note that the object may be busy, i.e. an active
  887. render target; in that case, setting the domain blocks the client and
  888. waits for rendering to complete before performing any necessary
  889. flushing operations).
  890. </para>
  891. </sect3>
  892. <sect3>
  893. <title>Command Execution</title>
  894. <para>
  895. Perhaps the most important GEM function for GPU devices is providing a
  896. command execution interface to clients. Client programs construct
  897. command buffers containing references to previously allocated memory
  898. objects, and then submit them to GEM. At that point, GEM takes care to
  899. bind all the objects into the GTT, execute the buffer, and provide
  900. necessary synchronization between clients accessing the same buffers.
  901. This often involves evicting some objects from the GTT and re-binding
  902. others (a fairly expensive operation), and providing relocation
  903. support which hides fixed GTT offsets from clients. Clients must take
  904. care not to submit command buffers that reference more objects than
  905. can fit in the GTT; otherwise, GEM will reject them and no rendering
  906. will occur. Similarly, if several objects in the buffer require fence
  907. registers to be allocated for correct rendering (e.g. 2D blits on
  908. pre-965 chips), care must be taken not to require more fence registers
  909. than are available to the client. Such resource management should be
  910. abstracted from the client in libdrm.
  911. </para>
  912. </sect3>
  913. </sect2>
  914. </sect1>
  915. <!-- Internals: mode setting -->
  916. <sect1 id="drm-mode-setting">
  917. <title>Mode Setting</title>
  918. <para>
  919. Drivers must initialize the mode setting core by calling
  920. <function>drm_mode_config_init</function> on the DRM device. The function
  921. initializes the <structname>drm_device</structname>
  922. <structfield>mode_config</structfield> field and never fails. Once done,
  923. mode configuration must be setup by initializing the following fields.
  924. </para>
  925. <itemizedlist>
  926. <listitem>
  927. <synopsis>int min_width, min_height;
  928. int max_width, max_height;</synopsis>
  929. <para>
  930. Minimum and maximum width and height of the frame buffers in pixel
  931. units.
  932. </para>
  933. </listitem>
  934. <listitem>
  935. <synopsis>struct drm_mode_config_funcs *funcs;</synopsis>
  936. <para>Mode setting functions.</para>
  937. </listitem>
  938. </itemizedlist>
  939. <sect2>
  940. <title>Frame Buffer Creation</title>
  941. <synopsis>struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
  942. struct drm_file *file_priv,
  943. struct drm_mode_fb_cmd2 *mode_cmd);</synopsis>
  944. <para>
  945. Frame buffers are abstract memory objects that provide a source of
  946. pixels to scanout to a CRTC. Applications explicitly request the
  947. creation of frame buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and
  948. receive an opaque handle that can be passed to the KMS CRTC control,
  949. plane configuration and page flip functions.
  950. </para>
  951. <para>
  952. Frame buffers rely on the underneath memory manager for low-level memory
  953. operations. When creating a frame buffer applications pass a memory
  954. handle (or a list of memory handles for multi-planar formats) through
  955. the <parameter>drm_mode_fb_cmd2</parameter> argument. This document
  956. assumes that the driver uses GEM, those handles thus reference GEM
  957. objects.
  958. </para>
  959. <para>
  960. Drivers must first validate the requested frame buffer parameters passed
  961. through the mode_cmd argument. In particular this is where invalid
  962. sizes, pixel formats or pitches can be caught.
  963. </para>
  964. <para>
  965. If the parameters are deemed valid, drivers then create, initialize and
  966. return an instance of struct <structname>drm_framebuffer</structname>.
  967. If desired the instance can be embedded in a larger driver-specific
  968. structure. Drivers must fill its <structfield>width</structfield>,
  969. <structfield>height</structfield>, <structfield>pitches</structfield>,
  970. <structfield>offsets</structfield>, <structfield>depth</structfield>,
  971. <structfield>bits_per_pixel</structfield> and
  972. <structfield>pixel_format</structfield> fields from the values passed
  973. through the <parameter>drm_mode_fb_cmd2</parameter> argument. They
  974. should call the <function>drm_helper_mode_fill_fb_struct</function>
  975. helper function to do so.
  976. </para>
  977. <para>
  978. The initailization of the new framebuffer instance is finalized with a
  979. call to <function>drm_framebuffer_init</function> which takes a pointer
  980. to DRM frame buffer operations (struct
  981. <structname>drm_framebuffer_funcs</structname>). Note that this function
  982. publishes the framebuffer and so from this point on it can be accessed
  983. concurrently from other threads. Hence it must be the last step in the
  984. driver's framebuffer initialization sequence. Frame buffer operations
  985. are
  986. <itemizedlist>
  987. <listitem>
  988. <synopsis>int (*create_handle)(struct drm_framebuffer *fb,
  989. struct drm_file *file_priv, unsigned int *handle);</synopsis>
  990. <para>
  991. Create a handle to the frame buffer underlying memory object. If
  992. the frame buffer uses a multi-plane format, the handle will
  993. reference the memory object associated with the first plane.
  994. </para>
  995. <para>
  996. Drivers call <function>drm_gem_handle_create</function> to create
  997. the handle.
  998. </para>
  999. </listitem>
  1000. <listitem>
  1001. <synopsis>void (*destroy)(struct drm_framebuffer *framebuffer);</synopsis>
  1002. <para>
  1003. Destroy the frame buffer object and frees all associated
  1004. resources. Drivers must call
  1005. <function>drm_framebuffer_cleanup</function> to free resources
  1006. allocated by the DRM core for the frame buffer object, and must
  1007. make sure to unreference all memory objects associated with the
  1008. frame buffer. Handles created by the
  1009. <methodname>create_handle</methodname> operation are released by
  1010. the DRM core.
  1011. </para>
  1012. </listitem>
  1013. <listitem>
  1014. <synopsis>int (*dirty)(struct drm_framebuffer *framebuffer,
  1015. struct drm_file *file_priv, unsigned flags, unsigned color,
  1016. struct drm_clip_rect *clips, unsigned num_clips);</synopsis>
  1017. <para>
  1018. This optional operation notifies the driver that a region of the
  1019. frame buffer has changed in response to a DRM_IOCTL_MODE_DIRTYFB
  1020. ioctl call.
  1021. </para>
  1022. </listitem>
  1023. </itemizedlist>
  1024. </para>
  1025. <para>
  1026. The lifetime of a drm framebuffer is controlled with a reference count,
  1027. drivers can grab additional references with
  1028. <function>drm_framebuffer_reference</function> </para> and drop them
  1029. again with <function>drm_framebuffer_unreference</function>. For
  1030. driver-private framebuffers for which the last reference is never
  1031. dropped (e.g. for the fbdev framebuffer when the struct
  1032. <structname>drm_framebuffer</structname> is embedded into the fbdev
  1033. helper struct) drivers can manually clean up a framebuffer at module
  1034. unload time with
  1035. <function>drm_framebuffer_unregister_private</function>.
  1036. </sect2>
  1037. <sect2>
  1038. <title>Output Polling</title>
  1039. <synopsis>void (*output_poll_changed)(struct drm_device *dev);</synopsis>
  1040. <para>
  1041. This operation notifies the driver that the status of one or more
  1042. connectors has changed. Drivers that use the fb helper can just call the
  1043. <function>drm_fb_helper_hotplug_event</function> function to handle this
  1044. operation.
  1045. </para>
  1046. </sect2>
  1047. <sect2>
  1048. <title>Locking</title>
  1049. <para>
  1050. Beside some lookup structures with their own locking (which is hidden
  1051. behind the interface functions) most of the modeset state is protected
  1052. by the <code>dev-&lt;mode_config.lock</code> mutex and additionally
  1053. per-crtc locks to allow cursor updates, pageflips and similar operations
  1054. to occur concurrently with background tasks like output detection.
  1055. Operations which cross domains like a full modeset always grab all
  1056. locks. Drivers there need to protect resources shared between crtcs with
  1057. additional locking. They also need to be careful to always grab the
  1058. relevant crtc locks if a modset functions touches crtc state, e.g. for
  1059. load detection (which does only grab the <code>mode_config.lock</code>
  1060. to allow concurrent screen updates on live crtcs).
  1061. </para>
  1062. </sect2>
  1063. </sect1>
  1064. <!-- Internals: kms initialization and cleanup -->
  1065. <sect1 id="drm-kms-init">
  1066. <title>KMS Initialization and Cleanup</title>
  1067. <para>
  1068. A KMS device is abstracted and exposed as a set of planes, CRTCs, encoders
  1069. and connectors. KMS drivers must thus create and initialize all those
  1070. objects at load time after initializing mode setting.
  1071. </para>
  1072. <sect2>
  1073. <title>CRTCs (struct <structname>drm_crtc</structname>)</title>
  1074. <para>
  1075. A CRTC is an abstraction representing a part of the chip that contains a
  1076. pointer to a scanout buffer. Therefore, the number of CRTCs available
  1077. determines how many independent scanout buffers can be active at any
  1078. given time. The CRTC structure contains several fields to support this:
  1079. a pointer to some video memory (abstracted as a frame buffer object), a
  1080. display mode, and an (x, y) offset into the video memory to support
  1081. panning or configurations where one piece of video memory spans multiple
  1082. CRTCs.
  1083. </para>
  1084. <sect3>
  1085. <title>CRTC Initialization</title>
  1086. <para>
  1087. A KMS device must create and register at least one struct
  1088. <structname>drm_crtc</structname> instance. The instance is allocated
  1089. and zeroed by the driver, possibly as part of a larger structure, and
  1090. registered with a call to <function>drm_crtc_init</function> with a
  1091. pointer to CRTC functions.
  1092. </para>
  1093. </sect3>
  1094. <sect3>
  1095. <title>CRTC Operations</title>
  1096. <sect4>
  1097. <title>Set Configuration</title>
  1098. <synopsis>int (*set_config)(struct drm_mode_set *set);</synopsis>
  1099. <para>
  1100. Apply a new CRTC configuration to the device. The configuration
  1101. specifies a CRTC, a frame buffer to scan out from, a (x,y) position in
  1102. the frame buffer, a display mode and an array of connectors to drive
  1103. with the CRTC if possible.
  1104. </para>
  1105. <para>
  1106. If the frame buffer specified in the configuration is NULL, the driver
  1107. must detach all encoders connected to the CRTC and all connectors
  1108. attached to those encoders and disable them.
  1109. </para>
  1110. <para>
  1111. This operation is called with the mode config lock held.
  1112. </para>
  1113. <note><para>
  1114. FIXME: How should set_config interact with DPMS? If the CRTC is
  1115. suspended, should it be resumed?
  1116. </para></note>
  1117. </sect4>
  1118. <sect4>
  1119. <title>Page Flipping</title>
  1120. <synopsis>int (*page_flip)(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1121. struct drm_pending_vblank_event *event);</synopsis>
  1122. <para>
  1123. Schedule a page flip to the given frame buffer for the CRTC. This
  1124. operation is called with the mode config mutex held.
  1125. </para>
  1126. <para>
  1127. Page flipping is a synchronization mechanism that replaces the frame
  1128. buffer being scanned out by the CRTC with a new frame buffer during
  1129. vertical blanking, avoiding tearing. When an application requests a page
  1130. flip the DRM core verifies that the new frame buffer is large enough to
  1131. be scanned out by the CRTC in the currently configured mode and then
  1132. calls the CRTC <methodname>page_flip</methodname> operation with a
  1133. pointer to the new frame buffer.
  1134. </para>
  1135. <para>
  1136. The <methodname>page_flip</methodname> operation schedules a page flip.
  1137. Once any pending rendering targetting the new frame buffer has
  1138. completed, the CRTC will be reprogrammed to display that frame buffer
  1139. after the next vertical refresh. The operation must return immediately
  1140. without waiting for rendering or page flip to complete and must block
  1141. any new rendering to the frame buffer until the page flip completes.
  1142. </para>
  1143. <para>
  1144. If a page flip can be successfully scheduled the driver must set the
  1145. <code>drm_crtc-&lt;fb</code> field to the new framebuffer pointed to
  1146. by <code>fb</code>. This is important so that the reference counting
  1147. on framebuffers stays balanced.
  1148. </para>
  1149. <para>
  1150. If a page flip is already pending, the
  1151. <methodname>page_flip</methodname> operation must return
  1152. -<errorname>EBUSY</errorname>.
  1153. </para>
  1154. <para>
  1155. To synchronize page flip to vertical blanking the driver will likely
  1156. need to enable vertical blanking interrupts. It should call
  1157. <function>drm_vblank_get</function> for that purpose, and call
  1158. <function>drm_vblank_put</function> after the page flip completes.
  1159. </para>
  1160. <para>
  1161. If the application has requested to be notified when page flip completes
  1162. the <methodname>page_flip</methodname> operation will be called with a
  1163. non-NULL <parameter>event</parameter> argument pointing to a
  1164. <structname>drm_pending_vblank_event</structname> instance. Upon page
  1165. flip completion the driver must call <methodname>drm_send_vblank_event</methodname>
  1166. to fill in the event and send to wake up any waiting processes.
  1167. This can be performed with
  1168. <programlisting><![CDATA[
  1169. spin_lock_irqsave(&dev->event_lock, flags);
  1170. ...
  1171. drm_send_vblank_event(dev, pipe, event);
  1172. spin_unlock_irqrestore(&dev->event_lock, flags);
  1173. ]]></programlisting>
  1174. </para>
  1175. <note><para>
  1176. FIXME: Could drivers that don't need to wait for rendering to complete
  1177. just add the event to <literal>dev-&gt;vblank_event_list</literal> and
  1178. let the DRM core handle everything, as for "normal" vertical blanking
  1179. events?
  1180. </para></note>
  1181. <para>
  1182. While waiting for the page flip to complete, the
  1183. <literal>event-&gt;base.link</literal> list head can be used freely by
  1184. the driver to store the pending event in a driver-specific list.
  1185. </para>
  1186. <para>
  1187. If the file handle is closed before the event is signaled, drivers must
  1188. take care to destroy the event in their
  1189. <methodname>preclose</methodname> operation (and, if needed, call
  1190. <function>drm_vblank_put</function>).
  1191. </para>
  1192. </sect4>
  1193. <sect4>
  1194. <title>Miscellaneous</title>
  1195. <itemizedlist>
  1196. <listitem>
  1197. <synopsis>void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  1198. uint32_t start, uint32_t size);</synopsis>
  1199. <para>
  1200. Apply a gamma table to the device. The operation is optional.
  1201. </para>
  1202. </listitem>
  1203. <listitem>
  1204. <synopsis>void (*destroy)(struct drm_crtc *crtc);</synopsis>
  1205. <para>
  1206. Destroy the CRTC when not needed anymore. See
  1207. <xref linkend="drm-kms-init"/>.
  1208. </para>
  1209. </listitem>
  1210. </itemizedlist>
  1211. </sect4>
  1212. </sect3>
  1213. </sect2>
  1214. <sect2>
  1215. <title>Planes (struct <structname>drm_plane</structname>)</title>
  1216. <para>
  1217. A plane represents an image source that can be blended with or overlayed
  1218. on top of a CRTC during the scanout process. Planes are associated with
  1219. a frame buffer to crop a portion of the image memory (source) and
  1220. optionally scale it to a destination size. The result is then blended
  1221. with or overlayed on top of a CRTC.
  1222. </para>
  1223. <sect3>
  1224. <title>Plane Initialization</title>
  1225. <para>
  1226. Planes are optional. To create a plane, a KMS drivers allocates and
  1227. zeroes an instances of struct <structname>drm_plane</structname>
  1228. (possibly as part of a larger structure) and registers it with a call
  1229. to <function>drm_plane_init</function>. The function takes a bitmask
  1230. of the CRTCs that can be associated with the plane, a pointer to the
  1231. plane functions and a list of format supported formats.
  1232. </para>
  1233. </sect3>
  1234. <sect3>
  1235. <title>Plane Operations</title>
  1236. <itemizedlist>
  1237. <listitem>
  1238. <synopsis>int (*update_plane)(struct drm_plane *plane, struct drm_crtc *crtc,
  1239. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  1240. unsigned int crtc_w, unsigned int crtc_h,
  1241. uint32_t src_x, uint32_t src_y,
  1242. uint32_t src_w, uint32_t src_h);</synopsis>
  1243. <para>
  1244. Enable and configure the plane to use the given CRTC and frame buffer.
  1245. </para>
  1246. <para>
  1247. The source rectangle in frame buffer memory coordinates is given by
  1248. the <parameter>src_x</parameter>, <parameter>src_y</parameter>,
  1249. <parameter>src_w</parameter> and <parameter>src_h</parameter>
  1250. parameters (as 16.16 fixed point values). Devices that don't support
  1251. subpixel plane coordinates can ignore the fractional part.
  1252. </para>
  1253. <para>
  1254. The destination rectangle in CRTC coordinates is given by the
  1255. <parameter>crtc_x</parameter>, <parameter>crtc_y</parameter>,
  1256. <parameter>crtc_w</parameter> and <parameter>crtc_h</parameter>
  1257. parameters (as integer values). Devices scale the source rectangle to
  1258. the destination rectangle. If scaling is not supported, and the source
  1259. rectangle size doesn't match the destination rectangle size, the
  1260. driver must return a -<errorname>EINVAL</errorname> error.
  1261. </para>
  1262. </listitem>
  1263. <listitem>
  1264. <synopsis>int (*disable_plane)(struct drm_plane *plane);</synopsis>
  1265. <para>
  1266. Disable the plane. The DRM core calls this method in response to a
  1267. DRM_IOCTL_MODE_SETPLANE ioctl call with the frame buffer ID set to 0.
  1268. Disabled planes must not be processed by the CRTC.
  1269. </para>
  1270. </listitem>
  1271. <listitem>
  1272. <synopsis>void (*destroy)(struct drm_plane *plane);</synopsis>
  1273. <para>
  1274. Destroy the plane when not needed anymore. See
  1275. <xref linkend="drm-kms-init"/>.
  1276. </para>
  1277. </listitem>
  1278. </itemizedlist>
  1279. </sect3>
  1280. </sect2>
  1281. <sect2>
  1282. <title>Encoders (struct <structname>drm_encoder</structname>)</title>
  1283. <para>
  1284. An encoder takes pixel data from a CRTC and converts it to a format
  1285. suitable for any attached connectors. On some devices, it may be
  1286. possible to have a CRTC send data to more than one encoder. In that
  1287. case, both encoders would receive data from the same scanout buffer,
  1288. resulting in a "cloned" display configuration across the connectors
  1289. attached to each encoder.
  1290. </para>
  1291. <sect3>
  1292. <title>Encoder Initialization</title>
  1293. <para>
  1294. As for CRTCs, a KMS driver must create, initialize and register at
  1295. least one struct <structname>drm_encoder</structname> instance. The
  1296. instance is allocated and zeroed by the driver, possibly as part of a
  1297. larger structure.
  1298. </para>
  1299. <para>
  1300. Drivers must initialize the struct <structname>drm_encoder</structname>
  1301. <structfield>possible_crtcs</structfield> and
  1302. <structfield>possible_clones</structfield> fields before registering the
  1303. encoder. Both fields are bitmasks of respectively the CRTCs that the
  1304. encoder can be connected to, and sibling encoders candidate for cloning.
  1305. </para>
  1306. <para>
  1307. After being initialized, the encoder must be registered with a call to
  1308. <function>drm_encoder_init</function>. The function takes a pointer to
  1309. the encoder functions and an encoder type. Supported types are
  1310. <itemizedlist>
  1311. <listitem>
  1312. DRM_MODE_ENCODER_DAC for VGA and analog on DVI-I/DVI-A
  1313. </listitem>
  1314. <listitem>
  1315. DRM_MODE_ENCODER_TMDS for DVI, HDMI and (embedded) DisplayPort
  1316. </listitem>
  1317. <listitem>
  1318. DRM_MODE_ENCODER_LVDS for display panels
  1319. </listitem>
  1320. <listitem>
  1321. DRM_MODE_ENCODER_TVDAC for TV output (Composite, S-Video, Component,
  1322. SCART)
  1323. </listitem>
  1324. <listitem>
  1325. DRM_MODE_ENCODER_VIRTUAL for virtual machine displays
  1326. </listitem>
  1327. </itemizedlist>
  1328. </para>
  1329. <para>
  1330. Encoders must be attached to a CRTC to be used. DRM drivers leave
  1331. encoders unattached at initialization time. Applications (or the fbdev
  1332. compatibility layer when implemented) are responsible for attaching the
  1333. encoders they want to use to a CRTC.
  1334. </para>
  1335. </sect3>
  1336. <sect3>
  1337. <title>Encoder Operations</title>
  1338. <itemizedlist>
  1339. <listitem>
  1340. <synopsis>void (*destroy)(struct drm_encoder *encoder);</synopsis>
  1341. <para>
  1342. Called to destroy the encoder when not needed anymore. See
  1343. <xref linkend="drm-kms-init"/>.
  1344. </para>
  1345. </listitem>
  1346. </itemizedlist>
  1347. </sect3>
  1348. </sect2>
  1349. <sect2>
  1350. <title>Connectors (struct <structname>drm_connector</structname>)</title>
  1351. <para>
  1352. A connector is the final destination for pixel data on a device, and
  1353. usually connects directly to an external display device like a monitor
  1354. or laptop panel. A connector can only be attached to one encoder at a
  1355. time. The connector is also the structure where information about the
  1356. attached display is kept, so it contains fields for display data, EDID
  1357. data, DPMS &amp; connection status, and information about modes
  1358. supported on the attached displays.
  1359. </para>
  1360. <sect3>
  1361. <title>Connector Initialization</title>
  1362. <para>
  1363. Finally a KMS driver must create, initialize, register and attach at
  1364. least one struct <structname>drm_connector</structname> instance. The
  1365. instance is created as other KMS objects and initialized by setting the
  1366. following fields.
  1367. </para>
  1368. <variablelist>
  1369. <varlistentry>
  1370. <term><structfield>interlace_allowed</structfield></term>
  1371. <listitem><para>
  1372. Whether the connector can handle interlaced modes.
  1373. </para></listitem>
  1374. </varlistentry>
  1375. <varlistentry>
  1376. <term><structfield>doublescan_allowed</structfield></term>
  1377. <listitem><para>
  1378. Whether the connector can handle doublescan.
  1379. </para></listitem>
  1380. </varlistentry>
  1381. <varlistentry>
  1382. <term><structfield>display_info
  1383. </structfield></term>
  1384. <listitem><para>
  1385. Display information is filled from EDID information when a display
  1386. is detected. For non hot-pluggable displays such as flat panels in
  1387. embedded systems, the driver should initialize the
  1388. <structfield>display_info</structfield>.<structfield>width_mm</structfield>
  1389. and
  1390. <structfield>display_info</structfield>.<structfield>height_mm</structfield>
  1391. fields with the physical size of the display.
  1392. </para></listitem>
  1393. </varlistentry>
  1394. <varlistentry>
  1395. <term id="drm-kms-connector-polled"><structfield>polled</structfield></term>
  1396. <listitem><para>
  1397. Connector polling mode, a combination of
  1398. <variablelist>
  1399. <varlistentry>
  1400. <term>DRM_CONNECTOR_POLL_HPD</term>
  1401. <listitem><para>
  1402. The connector generates hotplug events and doesn't need to be
  1403. periodically polled. The CONNECT and DISCONNECT flags must not
  1404. be set together with the HPD flag.
  1405. </para></listitem>
  1406. </varlistentry>
  1407. <varlistentry>
  1408. <term>DRM_CONNECTOR_POLL_CONNECT</term>
  1409. <listitem><para>
  1410. Periodically poll the connector for connection.
  1411. </para></listitem>
  1412. </varlistentry>
  1413. <varlistentry>
  1414. <term>DRM_CONNECTOR_POLL_DISCONNECT</term>
  1415. <listitem><para>
  1416. Periodically poll the connector for disconnection.
  1417. </para></listitem>
  1418. </varlistentry>
  1419. </variablelist>
  1420. Set to 0 for connectors that don't support connection status
  1421. discovery.
  1422. </para></listitem>
  1423. </varlistentry>
  1424. </variablelist>
  1425. <para>
  1426. The connector is then registered with a call to
  1427. <function>drm_connector_init</function> with a pointer to the connector
  1428. functions and a connector type, and exposed through sysfs with a call to
  1429. <function>drm_sysfs_connector_add</function>.
  1430. </para>
  1431. <para>
  1432. Supported connector types are
  1433. <itemizedlist>
  1434. <listitem>DRM_MODE_CONNECTOR_VGA</listitem>
  1435. <listitem>DRM_MODE_CONNECTOR_DVII</listitem>
  1436. <listitem>DRM_MODE_CONNECTOR_DVID</listitem>
  1437. <listitem>DRM_MODE_CONNECTOR_DVIA</listitem>
  1438. <listitem>DRM_MODE_CONNECTOR_Composite</listitem>
  1439. <listitem>DRM_MODE_CONNECTOR_SVIDEO</listitem>
  1440. <listitem>DRM_MODE_CONNECTOR_LVDS</listitem>
  1441. <listitem>DRM_MODE_CONNECTOR_Component</listitem>
  1442. <listitem>DRM_MODE_CONNECTOR_9PinDIN</listitem>
  1443. <listitem>DRM_MODE_CONNECTOR_DisplayPort</listitem>
  1444. <listitem>DRM_MODE_CONNECTOR_HDMIA</listitem>
  1445. <listitem>DRM_MODE_CONNECTOR_HDMIB</listitem>
  1446. <listitem>DRM_MODE_CONNECTOR_TV</listitem>
  1447. <listitem>DRM_MODE_CONNECTOR_eDP</listitem>
  1448. <listitem>DRM_MODE_CONNECTOR_VIRTUAL</listitem>
  1449. </itemizedlist>
  1450. </para>
  1451. <para>
  1452. Connectors must be attached to an encoder to be used. For devices that
  1453. map connectors to encoders 1:1, the connector should be attached at
  1454. initialization time with a call to
  1455. <function>drm_mode_connector_attach_encoder</function>. The driver must
  1456. also set the <structname>drm_connector</structname>
  1457. <structfield>encoder</structfield> field to point to the attached
  1458. encoder.
  1459. </para>
  1460. <para>
  1461. Finally, drivers must initialize the connectors state change detection
  1462. with a call to <function>drm_kms_helper_poll_init</function>. If at
  1463. least one connector is pollable but can't generate hotplug interrupts
  1464. (indicated by the DRM_CONNECTOR_POLL_CONNECT and
  1465. DRM_CONNECTOR_POLL_DISCONNECT connector flags), a delayed work will
  1466. automatically be queued to periodically poll for changes. Connectors
  1467. that can generate hotplug interrupts must be marked with the
  1468. DRM_CONNECTOR_POLL_HPD flag instead, and their interrupt handler must
  1469. call <function>drm_helper_hpd_irq_event</function>. The function will
  1470. queue a delayed work to check the state of all connectors, but no
  1471. periodic polling will be done.
  1472. </para>
  1473. </sect3>
  1474. <sect3>
  1475. <title>Connector Operations</title>
  1476. <note><para>
  1477. Unless otherwise state, all operations are mandatory.
  1478. </para></note>
  1479. <sect4>
  1480. <title>DPMS</title>
  1481. <synopsis>void (*dpms)(struct drm_connector *connector, int mode);</synopsis>
  1482. <para>
  1483. The DPMS operation sets the power state of a connector. The mode
  1484. argument is one of
  1485. <itemizedlist>
  1486. <listitem><para>DRM_MODE_DPMS_ON</para></listitem>
  1487. <listitem><para>DRM_MODE_DPMS_STANDBY</para></listitem>
  1488. <listitem><para>DRM_MODE_DPMS_SUSPEND</para></listitem>
  1489. <listitem><para>DRM_MODE_DPMS_OFF</para></listitem>
  1490. </itemizedlist>
  1491. </para>
  1492. <para>
  1493. In all but DPMS_ON mode the encoder to which the connector is attached
  1494. should put the display in low-power mode by driving its signals
  1495. appropriately. If more than one connector is attached to the encoder
  1496. care should be taken not to change the power state of other displays as
  1497. a side effect. Low-power mode should be propagated to the encoders and
  1498. CRTCs when all related connectors are put in low-power mode.
  1499. </para>
  1500. </sect4>
  1501. <sect4>
  1502. <title>Modes</title>
  1503. <synopsis>int (*fill_modes)(struct drm_connector *connector, uint32_t max_width,
  1504. uint32_t max_height);</synopsis>
  1505. <para>
  1506. Fill the mode list with all supported modes for the connector. If the
  1507. <parameter>max_width</parameter> and <parameter>max_height</parameter>
  1508. arguments are non-zero, the implementation must ignore all modes wider
  1509. than <parameter>max_width</parameter> or higher than
  1510. <parameter>max_height</parameter>.
  1511. </para>
  1512. <para>
  1513. The connector must also fill in this operation its
  1514. <structfield>display_info</structfield>
  1515. <structfield>width_mm</structfield> and
  1516. <structfield>height_mm</structfield> fields with the connected display
  1517. physical size in millimeters. The fields should be set to 0 if the value
  1518. isn't known or is not applicable (for instance for projector devices).
  1519. </para>
  1520. </sect4>
  1521. <sect4>
  1522. <title>Connection Status</title>
  1523. <para>
  1524. The connection status is updated through polling or hotplug events when
  1525. supported (see <xref linkend="drm-kms-connector-polled"/>). The status
  1526. value is reported to userspace through ioctls and must not be used
  1527. inside the driver, as it only gets initialized by a call to
  1528. <function>drm_mode_getconnector</function> from userspace.
  1529. </para>
  1530. <synopsis>enum drm_connector_status (*detect)(struct drm_connector *connector,
  1531. bool force);</synopsis>
  1532. <para>
  1533. Check to see if anything is attached to the connector. The
  1534. <parameter>force</parameter> parameter is set to false whilst polling or
  1535. to true when checking the connector due to user request.
  1536. <parameter>force</parameter> can be used by the driver to avoid
  1537. expensive, destructive operations during automated probing.
  1538. </para>
  1539. <para>
  1540. Return connector_status_connected if something is connected to the
  1541. connector, connector_status_disconnected if nothing is connected and
  1542. connector_status_unknown if the connection state isn't known.
  1543. </para>
  1544. <para>
  1545. Drivers should only return connector_status_connected if the connection
  1546. status has really been probed as connected. Connectors that can't detect
  1547. the connection status, or failed connection status probes, should return
  1548. connector_status_unknown.
  1549. </para>
  1550. </sect4>
  1551. <sect4>
  1552. <title>Miscellaneous</title>
  1553. <itemizedlist>
  1554. <listitem>
  1555. <synopsis>void (*destroy)(struct drm_connector *connector);</synopsis>
  1556. <para>
  1557. Destroy the connector when not needed anymore. See
  1558. <xref linkend="drm-kms-init"/>.
  1559. </para>
  1560. </listitem>
  1561. </itemizedlist>
  1562. </sect4>
  1563. </sect3>
  1564. </sect2>
  1565. <sect2>
  1566. <title>Cleanup</title>
  1567. <para>
  1568. The DRM core manages its objects' lifetime. When an object is not needed
  1569. anymore the core calls its destroy function, which must clean up and
  1570. free every resource allocated for the object. Every
  1571. <function>drm_*_init</function> call must be matched with a
  1572. corresponding <function>drm_*_cleanup</function> call to cleanup CRTCs
  1573. (<function>drm_crtc_cleanup</function>), planes
  1574. (<function>drm_plane_cleanup</function>), encoders
  1575. (<function>drm_encoder_cleanup</function>) and connectors
  1576. (<function>drm_connector_cleanup</function>). Furthermore, connectors
  1577. that have been added to sysfs must be removed by a call to
  1578. <function>drm_sysfs_connector_remove</function> before calling
  1579. <function>drm_connector_cleanup</function>.
  1580. </para>
  1581. <para>
  1582. Connectors state change detection must be cleanup up with a call to
  1583. <function>drm_kms_helper_poll_fini</function>.
  1584. </para>
  1585. </sect2>
  1586. <sect2>
  1587. <title>Output discovery and initialization example</title>
  1588. <programlisting><![CDATA[
  1589. void intel_crt_init(struct drm_device *dev)
  1590. {
  1591. struct drm_connector *connector;
  1592. struct intel_output *intel_output;
  1593. intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
  1594. if (!intel_output)
  1595. return;
  1596. connector = &intel_output->base;
  1597. drm_connector_init(dev, &intel_output->base,
  1598. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  1599. drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
  1600. DRM_MODE_ENCODER_DAC);
  1601. drm_mode_connector_attach_encoder(&intel_output->base,
  1602. &intel_output->enc);
  1603. /* Set up the DDC bus. */
  1604. intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
  1605. if (!intel_output->ddc_bus) {
  1606. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  1607. "failed.\n");
  1608. return;
  1609. }
  1610. intel_output->type = INTEL_OUTPUT_ANALOG;
  1611. connector->interlace_allowed = 0;
  1612. connector->doublescan_allowed = 0;
  1613. drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
  1614. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  1615. drm_sysfs_connector_add(connector);
  1616. }]]></programlisting>
  1617. <para>
  1618. In the example above (taken from the i915 driver), a CRTC, connector and
  1619. encoder combination is created. A device-specific i2c bus is also
  1620. created for fetching EDID data and performing monitor detection. Once
  1621. the process is complete, the new connector is registered with sysfs to
  1622. make its properties available to applications.
  1623. </para>
  1624. </sect2>
  1625. <sect2>
  1626. <title>KMS API Functions</title>
  1627. !Edrivers/gpu/drm/drm_crtc.c
  1628. </sect2>
  1629. </sect1>
  1630. <!-- Internals: kms helper functions -->
  1631. <sect1>
  1632. <title>Mode Setting Helper Functions</title>
  1633. <para>
  1634. The CRTC, encoder and connector functions provided by the drivers
  1635. implement the DRM API. They're called by the DRM core and ioctl handlers
  1636. to handle device state changes and configuration request. As implementing
  1637. those functions often requires logic not specific to drivers, mid-layer
  1638. helper functions are available to avoid duplicating boilerplate code.
  1639. </para>
  1640. <para>
  1641. The DRM core contains one mid-layer implementation. The mid-layer provides
  1642. implementations of several CRTC, encoder and connector functions (called
  1643. from the top of the mid-layer) that pre-process requests and call
  1644. lower-level functions provided by the driver (at the bottom of the
  1645. mid-layer). For instance, the
  1646. <function>drm_crtc_helper_set_config</function> function can be used to
  1647. fill the struct <structname>drm_crtc_funcs</structname>
  1648. <structfield>set_config</structfield> field. When called, it will split
  1649. the <methodname>set_config</methodname> operation in smaller, simpler
  1650. operations and call the driver to handle them.
  1651. </para>
  1652. <para>
  1653. To use the mid-layer, drivers call <function>drm_crtc_helper_add</function>,
  1654. <function>drm_encoder_helper_add</function> and
  1655. <function>drm_connector_helper_add</function> functions to install their
  1656. mid-layer bottom operations handlers, and fill the
  1657. <structname>drm_crtc_funcs</structname>,
  1658. <structname>drm_encoder_funcs</structname> and
  1659. <structname>drm_connector_funcs</structname> structures with pointers to
  1660. the mid-layer top API functions. Installing the mid-layer bottom operation
  1661. handlers is best done right after registering the corresponding KMS object.
  1662. </para>
  1663. <para>
  1664. The mid-layer is not split between CRTC, encoder and connector operations.
  1665. To use it, a driver must provide bottom functions for all of the three KMS
  1666. entities.
  1667. </para>
  1668. <sect2>
  1669. <title>Helper Functions</title>
  1670. <itemizedlist>
  1671. <listitem>
  1672. <synopsis>int drm_crtc_helper_set_config(struct drm_mode_set *set);</synopsis>
  1673. <para>
  1674. The <function>drm_crtc_helper_set_config</function> helper function
  1675. is a CRTC <methodname>set_config</methodname> implementation. It
  1676. first tries to locate the best encoder for each connector by calling
  1677. the connector <methodname>best_encoder</methodname> helper
  1678. operation.
  1679. </para>
  1680. <para>
  1681. After locating the appropriate encoders, the helper function will
  1682. call the <methodname>mode_fixup</methodname> encoder and CRTC helper
  1683. operations to adjust the requested mode, or reject it completely in
  1684. which case an error will be returned to the application. If the new
  1685. configuration after mode adjustment is identical to the current
  1686. configuration the helper function will return without performing any
  1687. other operation.
  1688. </para>
  1689. <para>
  1690. If the adjusted mode is identical to the current mode but changes to
  1691. the frame buffer need to be applied, the
  1692. <function>drm_crtc_helper_set_config</function> function will call
  1693. the CRTC <methodname>mode_set_base</methodname> helper operation. If
  1694. the adjusted mode differs from the current mode, or if the
  1695. <methodname>mode_set_base</methodname> helper operation is not
  1696. provided, the helper function performs a full mode set sequence by
  1697. calling the <methodname>prepare</methodname>,
  1698. <methodname>mode_set</methodname> and
  1699. <methodname>commit</methodname> CRTC and encoder helper operations,
  1700. in that order.
  1701. </para>
  1702. </listitem>
  1703. <listitem>
  1704. <synopsis>void drm_helper_connector_dpms(struct drm_connector *connector, int mode);</synopsis>
  1705. <para>
  1706. The <function>drm_helper_connector_dpms</function> helper function
  1707. is a connector <methodname>dpms</methodname> implementation that
  1708. tracks power state of connectors. To use the function, drivers must
  1709. provide <methodname>dpms</methodname> helper operations for CRTCs
  1710. and encoders to apply the DPMS state to the device.
  1711. </para>
  1712. <para>
  1713. The mid-layer doesn't track the power state of CRTCs and encoders.
  1714. The <methodname>dpms</methodname> helper operations can thus be
  1715. called with a mode identical to the currently active mode.
  1716. </para>
  1717. </listitem>
  1718. <listitem>
  1719. <synopsis>int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  1720. uint32_t maxX, uint32_t maxY);</synopsis>
  1721. <para>
  1722. The <function>drm_helper_probe_single_connector_modes</function> helper
  1723. function is a connector <methodname>fill_modes</methodname>
  1724. implementation that updates the connection status for the connector
  1725. and then retrieves a list of modes by calling the connector
  1726. <methodname>get_modes</methodname> helper operation.
  1727. </para>
  1728. <para>
  1729. The function filters out modes larger than
  1730. <parameter>max_width</parameter> and <parameter>max_height</parameter>
  1731. if specified. It then calls the connector
  1732. <methodname>mode_valid</methodname> helper operation for each mode in
  1733. the probed list to check whether the mode is valid for the connector.
  1734. </para>
  1735. </listitem>
  1736. </itemizedlist>
  1737. </sect2>
  1738. <sect2>
  1739. <title>CRTC Helper Operations</title>
  1740. <itemizedlist>
  1741. <listitem id="drm-helper-crtc-mode-fixup">
  1742. <synopsis>bool (*mode_fixup)(struct drm_crtc *crtc,
  1743. const struct drm_display_mode *mode,
  1744. struct drm_display_mode *adjusted_mode);</synopsis>
  1745. <para>
  1746. Let CRTCs adjust the requested mode or reject it completely. This
  1747. operation returns true if the mode is accepted (possibly after being
  1748. adjusted) or false if it is rejected.
  1749. </para>
  1750. <para>
  1751. The <methodname>mode_fixup</methodname> operation should reject the
  1752. mode if it can't reasonably use it. The definition of "reasonable"
  1753. is currently fuzzy in this context. One possible behaviour would be
  1754. to set the adjusted mode to the panel timings when a fixed-mode
  1755. panel is used with hardware capable of scaling. Another behaviour
  1756. would be to accept any input mode and adjust it to the closest mode
  1757. supported by the hardware (FIXME: This needs to be clarified).
  1758. </para>
  1759. </listitem>
  1760. <listitem>
  1761. <synopsis>int (*mode_set_base)(struct drm_crtc *crtc, int x, int y,
  1762. struct drm_framebuffer *old_fb)</synopsis>
  1763. <para>
  1764. Move the CRTC on the current frame buffer (stored in
  1765. <literal>crtc-&gt;fb</literal>) to position (x,y). Any of the frame
  1766. buffer, x position or y position may have been modified.
  1767. </para>
  1768. <para>
  1769. This helper operation is optional. If not provided, the
  1770. <function>drm_crtc_helper_set_config</function> function will fall
  1771. back to the <methodname>mode_set</methodname> helper operation.
  1772. </para>
  1773. <note><para>
  1774. FIXME: Why are x and y passed as arguments, as they can be accessed
  1775. through <literal>crtc-&gt;x</literal> and
  1776. <literal>crtc-&gt;y</literal>?
  1777. </para></note>
  1778. </listitem>
  1779. <listitem>
  1780. <synopsis>void (*prepare)(struct drm_crtc *crtc);</synopsis>
  1781. <para>
  1782. Prepare the CRTC for mode setting. This operation is called after
  1783. validating the requested mode. Drivers use it to perform
  1784. device-specific operations required before setting the new mode.
  1785. </para>
  1786. </listitem>
  1787. <listitem>
  1788. <synopsis>int (*mode_set)(struct drm_crtc *crtc, struct drm_display_mode *mode,
  1789. struct drm_display_mode *adjusted_mode, int x, int y,
  1790. struct drm_framebuffer *old_fb);</synopsis>
  1791. <para>
  1792. Set a new mode, position and frame buffer. Depending on the device
  1793. requirements, the mode can be stored internally by the driver and
  1794. applied in the <methodname>commit</methodname> operation, or
  1795. programmed to the hardware immediately.
  1796. </para>
  1797. <para>
  1798. The <methodname>mode_set</methodname> operation returns 0 on success
  1799. or a negative error code if an error occurs.
  1800. </para>
  1801. </listitem>
  1802. <listitem>
  1803. <synopsis>void (*commit)(struct drm_crtc *crtc);</synopsis>
  1804. <para>
  1805. Commit a mode. This operation is called after setting the new mode.
  1806. Upon return the device must use the new mode and be fully
  1807. operational.
  1808. </para>
  1809. </listitem>
  1810. </itemizedlist>
  1811. </sect2>
  1812. <sect2>
  1813. <title>Encoder Helper Operations</title>
  1814. <itemizedlist>
  1815. <listitem>
  1816. <synopsis>bool (*mode_fixup)(struct drm_encoder *encoder,
  1817. const struct drm_display_mode *mode,
  1818. struct drm_display_mode *adjusted_mode);</synopsis>
  1819. <note><para>
  1820. FIXME: The mode argument be const, but the i915 driver modifies
  1821. mode-&gt;clock in <function>intel_dp_mode_fixup</function>.
  1822. </para></note>
  1823. <para>
  1824. Let encoders adjust the requested mode or reject it completely. This
  1825. operation returns true if the mode is accepted (possibly after being
  1826. adjusted) or false if it is rejected. See the
  1827. <link linkend="drm-helper-crtc-mode-fixup">mode_fixup CRTC helper
  1828. operation</link> for an explanation of the allowed adjustments.
  1829. </para>
  1830. </listitem>
  1831. <listitem>
  1832. <synopsis>void (*prepare)(struct drm_encoder *encoder);</synopsis>
  1833. <para>
  1834. Prepare the encoder for mode setting. This operation is called after
  1835. validating the requested mode. Drivers use it to perform
  1836. device-specific operations required before setting the new mode.
  1837. </para>
  1838. </listitem>
  1839. <listitem>
  1840. <synopsis>void (*mode_set)(struct drm_encoder *encoder,
  1841. struct drm_display_mode *mode,
  1842. struct drm_display_mode *adjusted_mode);</synopsis>
  1843. <para>
  1844. Set a new mode. Depending on the device requirements, the mode can
  1845. be stored internally by the driver and applied in the
  1846. <methodname>commit</methodname> operation, or programmed to the
  1847. hardware immediately.
  1848. </para>
  1849. </listitem>
  1850. <listitem>
  1851. <synopsis>void (*commit)(struct drm_encoder *encoder);</synopsis>
  1852. <para>
  1853. Commit a mode. This operation is called after setting the new mode.
  1854. Upon return the device must use the new mode and be fully
  1855. operational.
  1856. </para>
  1857. </listitem>
  1858. </itemizedlist>
  1859. </sect2>
  1860. <sect2>
  1861. <title>Connector Helper Operations</title>
  1862. <itemizedlist>
  1863. <listitem>
  1864. <synopsis>struct drm_encoder *(*best_encoder)(struct drm_connector *connector);</synopsis>
  1865. <para>
  1866. Return a pointer to the best encoder for the connecter. Device that
  1867. map connectors to encoders 1:1 simply return the pointer to the
  1868. associated encoder. This operation is mandatory.
  1869. </para>
  1870. </listitem>
  1871. <listitem>
  1872. <synopsis>int (*get_modes)(struct drm_connector *connector);</synopsis>
  1873. <para>
  1874. Fill the connector's <structfield>probed_modes</structfield> list
  1875. by parsing EDID data with <function>drm_add_edid_modes</function> or
  1876. calling <function>drm_mode_probed_add</function> directly for every
  1877. supported mode and return the number of modes it has detected. This
  1878. operation is mandatory.
  1879. </para>
  1880. <para>
  1881. When adding modes manually the driver creates each mode with a call to
  1882. <function>drm_mode_create</function> and must fill the following fields.
  1883. <itemizedlist>
  1884. <listitem>
  1885. <synopsis>__u32 type;</synopsis>
  1886. <para>
  1887. Mode type bitmask, a combination of
  1888. <variablelist>
  1889. <varlistentry>
  1890. <term>DRM_MODE_TYPE_BUILTIN</term>
  1891. <listitem><para>not used?</para></listitem>
  1892. </varlistentry>
  1893. <varlistentry>
  1894. <term>DRM_MODE_TYPE_CLOCK_C</term>
  1895. <listitem><para>not used?</para></listitem>
  1896. </varlistentry>
  1897. <varlistentry>
  1898. <term>DRM_MODE_TYPE_CRTC_C</term>
  1899. <listitem><para>not used?</para></listitem>
  1900. </varlistentry>
  1901. <varlistentry>
  1902. <term>
  1903. DRM_MODE_TYPE_PREFERRED - The preferred mode for the connector
  1904. </term>
  1905. <listitem>
  1906. <para>not used?</para>
  1907. </listitem>
  1908. </varlistentry>
  1909. <varlistentry>
  1910. <term>DRM_MODE_TYPE_DEFAULT</term>
  1911. <listitem><para>not used?</para></listitem>
  1912. </varlistentry>
  1913. <varlistentry>
  1914. <term>DRM_MODE_TYPE_USERDEF</term>
  1915. <listitem><para>not used?</para></listitem>
  1916. </varlistentry>
  1917. <varlistentry>
  1918. <term>DRM_MODE_TYPE_DRIVER</term>
  1919. <listitem>
  1920. <para>
  1921. The mode has been created by the driver (as opposed to
  1922. to user-created modes).
  1923. </para>
  1924. </listitem>
  1925. </varlistentry>
  1926. </variablelist>
  1927. Drivers must set the DRM_MODE_TYPE_DRIVER bit for all modes they
  1928. create, and set the DRM_MODE_TYPE_PREFERRED bit for the preferred
  1929. mode.
  1930. </para>
  1931. </listitem>
  1932. <listitem>
  1933. <synopsis>__u32 clock;</synopsis>
  1934. <para>Pixel clock frequency in kHz unit</para>
  1935. </listitem>
  1936. <listitem>
  1937. <synopsis>__u16 hdisplay, hsync_start, hsync_end, htotal;
  1938. __u16 vdisplay, vsync_start, vsync_end, vtotal;</synopsis>
  1939. <para>Horizontal and vertical timing information</para>
  1940. <screen><![CDATA[
  1941. Active Front Sync Back
  1942. Region Porch Porch
  1943. <-----------------------><----------------><-------------><-------------->
  1944. //////////////////////|
  1945. ////////////////////// |
  1946. ////////////////////// |.................. ................
  1947. _______________
  1948. <----- [hv]display ----->
  1949. <------------- [hv]sync_start ------------>
  1950. <--------------------- [hv]sync_end --------------------->
  1951. <-------------------------------- [hv]total ----------------------------->
  1952. ]]></screen>
  1953. </listitem>
  1954. <listitem>
  1955. <synopsis>__u16 hskew;
  1956. __u16 vscan;</synopsis>
  1957. <para>Unknown</para>
  1958. </listitem>
  1959. <listitem>
  1960. <synopsis>__u32 flags;</synopsis>
  1961. <para>
  1962. Mode flags, a combination of
  1963. <variablelist>
  1964. <varlistentry>
  1965. <term>DRM_MODE_FLAG_PHSYNC</term>
  1966. <listitem><para>
  1967. Horizontal sync is active high
  1968. </para></listitem>
  1969. </varlistentry>
  1970. <varlistentry>
  1971. <term>DRM_MODE_FLAG_NHSYNC</term>
  1972. <listitem><para>
  1973. Horizontal sync is active low
  1974. </para></listitem>
  1975. </varlistentry>
  1976. <varlistentry>
  1977. <term>DRM_MODE_FLAG_PVSYNC</term>
  1978. <listitem><para>
  1979. Vertical sync is active high
  1980. </para></listitem>
  1981. </varlistentry>
  1982. <varlistentry>
  1983. <term>DRM_MODE_FLAG_NVSYNC</term>
  1984. <listitem><para>
  1985. Vertical sync is active low
  1986. </para></listitem>
  1987. </varlistentry>
  1988. <varlistentry>
  1989. <term>DRM_MODE_FLAG_INTERLACE</term>
  1990. <listitem><para>
  1991. Mode is interlaced
  1992. </para></listitem>
  1993. </varlistentry>
  1994. <varlistentry>
  1995. <term>DRM_MODE_FLAG_DBLSCAN</term>
  1996. <listitem><para>
  1997. Mode uses doublescan
  1998. </para></listitem>
  1999. </varlistentry>
  2000. <varlistentry>
  2001. <term>DRM_MODE_FLAG_CSYNC</term>
  2002. <listitem><para>
  2003. Mode uses composite sync
  2004. </para></listitem>
  2005. </varlistentry>
  2006. <varlistentry>
  2007. <term>DRM_MODE_FLAG_PCSYNC</term>
  2008. <listitem><para>
  2009. Composite sync is active high
  2010. </para></listitem>
  2011. </varlistentry>
  2012. <varlistentry>
  2013. <term>DRM_MODE_FLAG_NCSYNC</term>
  2014. <listitem><para>
  2015. Composite sync is active low
  2016. </para></listitem>
  2017. </varlistentry>
  2018. <varlistentry>
  2019. <term>DRM_MODE_FLAG_HSKEW</term>
  2020. <listitem><para>
  2021. hskew provided (not used?)
  2022. </para></listitem>
  2023. </varlistentry>
  2024. <varlistentry>
  2025. <term>DRM_MODE_FLAG_BCAST</term>
  2026. <listitem><para>
  2027. not used?
  2028. </para></listitem>
  2029. </varlistentry>
  2030. <varlistentry>
  2031. <term>DRM_MODE_FLAG_PIXMUX</term>
  2032. <listitem><para>
  2033. not used?
  2034. </para></listitem>
  2035. </varlistentry>
  2036. <varlistentry>
  2037. <term>DRM_MODE_FLAG_DBLCLK</term>
  2038. <listitem><para>
  2039. not used?
  2040. </para></listitem>
  2041. </varlistentry>
  2042. <varlistentry>
  2043. <term>DRM_MODE_FLAG_CLKDIV2</term>
  2044. <listitem><para>
  2045. ?
  2046. </para></listitem>
  2047. </varlistentry>
  2048. </variablelist>
  2049. </para>
  2050. <para>
  2051. Note that modes marked with the INTERLACE or DBLSCAN flags will be
  2052. filtered out by
  2053. <function>drm_helper_probe_single_connector_modes</function> if
  2054. the connector's <structfield>interlace_allowed</structfield> or
  2055. <structfield>doublescan_allowed</structfield> field is set to 0.
  2056. </para>
  2057. </listitem>
  2058. <listitem>
  2059. <synopsis>char name[DRM_DISPLAY_MODE_LEN];</synopsis>
  2060. <para>
  2061. Mode name. The driver must call
  2062. <function>drm_mode_set_name</function> to fill the mode name from
  2063. <structfield>hdisplay</structfield>,
  2064. <structfield>vdisplay</structfield> and interlace flag after
  2065. filling the corresponding fields.
  2066. </para>
  2067. </listitem>
  2068. </itemizedlist>
  2069. </para>
  2070. <para>
  2071. The <structfield>vrefresh</structfield> value is computed by
  2072. <function>drm_helper_probe_single_connector_modes</function>.
  2073. </para>
  2074. <para>
  2075. When parsing EDID data, <function>drm_add_edid_modes</function> fill the
  2076. connector <structfield>display_info</structfield>
  2077. <structfield>width_mm</structfield> and
  2078. <structfield>height_mm</structfield> fields. When creating modes
  2079. manually the <methodname>get_modes</methodname> helper operation must
  2080. set the <structfield>display_info</structfield>
  2081. <structfield>width_mm</structfield> and
  2082. <structfield>height_mm</structfield> fields if they haven't been set
  2083. already (for instance at initilization time when a fixed-size panel is
  2084. attached to the connector). The mode <structfield>width_mm</structfield>
  2085. and <structfield>height_mm</structfield> fields are only used internally
  2086. during EDID parsing and should not be set when creating modes manually.
  2087. </para>
  2088. </listitem>
  2089. <listitem>
  2090. <synopsis>int (*mode_valid)(struct drm_connector *connector,
  2091. struct drm_display_mode *mode);</synopsis>
  2092. <para>
  2093. Verify whether a mode is valid for the connector. Return MODE_OK for
  2094. supported modes and one of the enum drm_mode_status values (MODE_*)
  2095. for unsupported modes. This operation is mandatory.
  2096. </para>
  2097. <para>
  2098. As the mode rejection reason is currently not used beside for
  2099. immediately removing the unsupported mode, an implementation can
  2100. return MODE_BAD regardless of the exact reason why the mode is not
  2101. valid.
  2102. </para>
  2103. <note><para>
  2104. Note that the <methodname>mode_valid</methodname> helper operation is
  2105. only called for modes detected by the device, and
  2106. <emphasis>not</emphasis> for modes set by the user through the CRTC
  2107. <methodname>set_config</methodname> operation.
  2108. </para></note>
  2109. </listitem>
  2110. </itemizedlist>
  2111. </sect2>
  2112. <sect2>
  2113. <title>Modeset Helper Functions Reference</title>
  2114. !Edrivers/gpu/drm/drm_crtc_helper.c
  2115. </sect2>
  2116. <sect2>
  2117. <title>fbdev Helper Functions Reference</title>
  2118. !Pdrivers/gpu/drm/drm_fb_helper.c fbdev helpers
  2119. !Edrivers/gpu/drm/drm_fb_helper.c
  2120. !Iinclude/drm/drm_fb_helper.h
  2121. </sect2>
  2122. <sect2>
  2123. <title>Display Port Helper Functions Reference</title>
  2124. !Pdrivers/gpu/drm/drm_dp_helper.c dp helpers
  2125. !Iinclude/drm/drm_dp_helper.h
  2126. !Edrivers/gpu/drm/drm_dp_helper.c
  2127. </sect2>
  2128. <sect2>
  2129. <title>EDID Helper Functions Reference</title>
  2130. !Edrivers/gpu/drm/drm_edid.c
  2131. </sect2>
  2132. </sect1>
  2133. <!-- Internals: vertical blanking -->
  2134. <sect1 id="drm-vertical-blank">
  2135. <title>Vertical Blanking</title>
  2136. <para>
  2137. Vertical blanking plays a major role in graphics rendering. To achieve
  2138. tear-free display, users must synchronize page flips and/or rendering to
  2139. vertical blanking. The DRM API offers ioctls to perform page flips
  2140. synchronized to vertical blanking and wait for vertical blanking.
  2141. </para>
  2142. <para>
  2143. The DRM core handles most of the vertical blanking management logic, which
  2144. involves filtering out spurious interrupts, keeping race-free blanking
  2145. counters, coping with counter wrap-around and resets and keeping use
  2146. counts. It relies on the driver to generate vertical blanking interrupts
  2147. and optionally provide a hardware vertical blanking counter. Drivers must
  2148. implement the following operations.
  2149. </para>
  2150. <itemizedlist>
  2151. <listitem>
  2152. <synopsis>int (*enable_vblank) (struct drm_device *dev, int crtc);
  2153. void (*disable_vblank) (struct drm_device *dev, int crtc);</synopsis>
  2154. <para>
  2155. Enable or disable vertical blanking interrupts for the given CRTC.
  2156. </para>
  2157. </listitem>
  2158. <listitem>
  2159. <synopsis>u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);</synopsis>
  2160. <para>
  2161. Retrieve the value of the vertical blanking counter for the given
  2162. CRTC. If the hardware maintains a vertical blanking counter its value
  2163. should be returned. Otherwise drivers can use the
  2164. <function>drm_vblank_count</function> helper function to handle this
  2165. operation.
  2166. </para>
  2167. </listitem>
  2168. </itemizedlist>
  2169. <para>
  2170. Drivers must initialize the vertical blanking handling core with a call to
  2171. <function>drm_vblank_init</function> in their
  2172. <methodname>load</methodname> operation. The function will set the struct
  2173. <structname>drm_device</structname>
  2174. <structfield>vblank_disable_allowed</structfield> field to 0. This will
  2175. keep vertical blanking interrupts enabled permanently until the first mode
  2176. set operation, where <structfield>vblank_disable_allowed</structfield> is
  2177. set to 1. The reason behind this is not clear. Drivers can set the field
  2178. to 1 after <function>calling drm_vblank_init</function> to make vertical
  2179. blanking interrupts dynamically managed from the beginning.
  2180. </para>
  2181. <para>
  2182. Vertical blanking interrupts can be enabled by the DRM core or by drivers
  2183. themselves (for instance to handle page flipping operations). The DRM core
  2184. maintains a vertical blanking use count to ensure that the interrupts are
  2185. not disabled while a user still needs them. To increment the use count,
  2186. drivers call <function>drm_vblank_get</function>. Upon return vertical
  2187. blanking interrupts are guaranteed to be enabled.
  2188. </para>
  2189. <para>
  2190. To decrement the use count drivers call
  2191. <function>drm_vblank_put</function>. Only when the use count drops to zero
  2192. will the DRM core disable the vertical blanking interrupts after a delay
  2193. by scheduling a timer. The delay is accessible through the vblankoffdelay
  2194. module parameter or the <varname>drm_vblank_offdelay</varname> global
  2195. variable and expressed in milliseconds. Its default value is 5000 ms.
  2196. </para>
  2197. <para>
  2198. When a vertical blanking interrupt occurs drivers only need to call the
  2199. <function>drm_handle_vblank</function> function to account for the
  2200. interrupt.
  2201. </para>
  2202. <para>
  2203. Resources allocated by <function>drm_vblank_init</function> must be freed
  2204. with a call to <function>drm_vblank_cleanup</function> in the driver
  2205. <methodname>unload</methodname> operation handler.
  2206. </para>
  2207. </sect1>
  2208. <!-- Internals: open/close, file operations and ioctls -->
  2209. <sect1>
  2210. <title>Open/Close, File Operations and IOCTLs</title>
  2211. <sect2>
  2212. <title>Open and Close</title>
  2213. <synopsis>int (*firstopen) (struct drm_device *);
  2214. void (*lastclose) (struct drm_device *);
  2215. int (*open) (struct drm_device *, struct drm_file *);
  2216. void (*preclose) (struct drm_device *, struct drm_file *);
  2217. void (*postclose) (struct drm_device *, struct drm_file *);</synopsis>
  2218. <abstract>Open and close handlers. None of those methods are mandatory.
  2219. </abstract>
  2220. <para>
  2221. The <methodname>firstopen</methodname> method is called by the DRM core
  2222. when an application opens a device that has no other opened file handle.
  2223. Similarly the <methodname>lastclose</methodname> method is called when
  2224. the last application holding a file handle opened on the device closes
  2225. it. Both methods are mostly used for UMS (User Mode Setting) drivers to
  2226. acquire and release device resources which should be done in the
  2227. <methodname>load</methodname> and <methodname>unload</methodname>
  2228. methods for KMS drivers.
  2229. </para>
  2230. <para>
  2231. Note that the <methodname>lastclose</methodname> method is also called
  2232. at module unload time or, for hot-pluggable devices, when the device is
  2233. unplugged. The <methodname>firstopen</methodname> and
  2234. <methodname>lastclose</methodname> calls can thus be unbalanced.
  2235. </para>
  2236. <para>
  2237. The <methodname>open</methodname> method is called every time the device
  2238. is opened by an application. Drivers can allocate per-file private data
  2239. in this method and store them in the struct
  2240. <structname>drm_file</structname> <structfield>driver_priv</structfield>
  2241. field. Note that the <methodname>open</methodname> method is called
  2242. before <methodname>firstopen</methodname>.
  2243. </para>
  2244. <para>
  2245. The close operation is split into <methodname>preclose</methodname> and
  2246. <methodname>postclose</methodname> methods. Drivers must stop and
  2247. cleanup all per-file operations in the <methodname>preclose</methodname>
  2248. method. For instance pending vertical blanking and page flip events must
  2249. be cancelled. No per-file operation is allowed on the file handle after
  2250. returning from the <methodname>preclose</methodname> method.
  2251. </para>
  2252. <para>
  2253. Finally the <methodname>postclose</methodname> method is called as the
  2254. last step of the close operation, right before calling the
  2255. <methodname>lastclose</methodname> method if no other open file handle
  2256. exists for the device. Drivers that have allocated per-file private data
  2257. in the <methodname>open</methodname> method should free it here.
  2258. </para>
  2259. <para>
  2260. The <methodname>lastclose</methodname> method should restore CRTC and
  2261. plane properties to default value, so that a subsequent open of the
  2262. device will not inherit state from the previous user.
  2263. </para>
  2264. </sect2>
  2265. <sect2>
  2266. <title>File Operations</title>
  2267. <synopsis>const struct file_operations *fops</synopsis>
  2268. <abstract>File operations for the DRM device node.</abstract>
  2269. <para>
  2270. Drivers must define the file operations structure that forms the DRM
  2271. userspace API entry point, even though most of those operations are
  2272. implemented in the DRM core. The <methodname>open</methodname>,
  2273. <methodname>release</methodname> and <methodname>ioctl</methodname>
  2274. operations are handled by
  2275. <programlisting>
  2276. .owner = THIS_MODULE,
  2277. .open = drm_open,
  2278. .release = drm_release,
  2279. .unlocked_ioctl = drm_ioctl,
  2280. #ifdef CONFIG_COMPAT
  2281. .compat_ioctl = drm_compat_ioctl,
  2282. #endif
  2283. </programlisting>
  2284. </para>
  2285. <para>
  2286. Drivers that implement private ioctls that requires 32/64bit
  2287. compatibility support must provide their own
  2288. <methodname>compat_ioctl</methodname> handler that processes private
  2289. ioctls and calls <function>drm_compat_ioctl</function> for core ioctls.
  2290. </para>
  2291. <para>
  2292. The <methodname>read</methodname> and <methodname>poll</methodname>
  2293. operations provide support for reading DRM events and polling them. They
  2294. are implemented by
  2295. <programlisting>
  2296. .poll = drm_poll,
  2297. .read = drm_read,
  2298. .fasync = drm_fasync,
  2299. .llseek = no_llseek,
  2300. </programlisting>
  2301. </para>
  2302. <para>
  2303. The memory mapping implementation varies depending on how the driver
  2304. manages memory. Pre-GEM drivers will use <function>drm_mmap</function>,
  2305. while GEM-aware drivers will use <function>drm_gem_mmap</function>. See
  2306. <xref linkend="drm-gem"/>.
  2307. <programlisting>
  2308. .mmap = drm_gem_mmap,
  2309. </programlisting>
  2310. </para>
  2311. <para>
  2312. No other file operation is supported by the DRM API.
  2313. </para>
  2314. </sect2>
  2315. <sect2>
  2316. <title>IOCTLs</title>
  2317. <synopsis>struct drm_ioctl_desc *ioctls;
  2318. int num_ioctls;</synopsis>
  2319. <abstract>Driver-specific ioctls descriptors table.</abstract>
  2320. <para>
  2321. Driver-specific ioctls numbers start at DRM_COMMAND_BASE. The ioctls
  2322. descriptors table is indexed by the ioctl number offset from the base
  2323. value. Drivers can use the DRM_IOCTL_DEF_DRV() macro to initialize the
  2324. table entries.
  2325. </para>
  2326. <para>
  2327. <programlisting>DRM_IOCTL_DEF_DRV(ioctl, func, flags)</programlisting>
  2328. <para>
  2329. <parameter>ioctl</parameter> is the ioctl name. Drivers must define
  2330. the DRM_##ioctl and DRM_IOCTL_##ioctl macros to the ioctl number
  2331. offset from DRM_COMMAND_BASE and the ioctl number respectively. The
  2332. first macro is private to the device while the second must be exposed
  2333. to userspace in a public header.
  2334. </para>
  2335. <para>
  2336. <parameter>func</parameter> is a pointer to the ioctl handler function
  2337. compatible with the <type>drm_ioctl_t</type> type.
  2338. <programlisting>typedef int drm_ioctl_t(struct drm_device *dev, void *data,
  2339. struct drm_file *file_priv);</programlisting>
  2340. </para>
  2341. <para>
  2342. <parameter>flags</parameter> is a bitmask combination of the following
  2343. values. It restricts how the ioctl is allowed to be called.
  2344. <itemizedlist>
  2345. <listitem><para>
  2346. DRM_AUTH - Only authenticated callers allowed
  2347. </para></listitem>
  2348. <listitem><para>
  2349. DRM_MASTER - The ioctl can only be called on the master file
  2350. handle
  2351. </para></listitem>
  2352. <listitem><para>
  2353. DRM_ROOT_ONLY - Only callers with the SYSADMIN capability allowed
  2354. </para></listitem>
  2355. <listitem><para>
  2356. DRM_CONTROL_ALLOW - The ioctl can only be called on a control
  2357. device
  2358. </para></listitem>
  2359. <listitem><para>
  2360. DRM_UNLOCKED - The ioctl handler will be called without locking
  2361. the DRM global mutex
  2362. </para></listitem>
  2363. </itemizedlist>
  2364. </para>
  2365. </para>
  2366. </sect2>
  2367. </sect1>
  2368. <sect1>
  2369. <title>Command submission &amp; fencing</title>
  2370. <para>
  2371. This should cover a few device-specific command submission
  2372. implementations.
  2373. </para>
  2374. </sect1>
  2375. <!-- Internals: suspend/resume -->
  2376. <sect1>
  2377. <title>Suspend/Resume</title>
  2378. <para>
  2379. The DRM core provides some suspend/resume code, but drivers wanting full
  2380. suspend/resume support should provide save() and restore() functions.
  2381. These are called at suspend, hibernate, or resume time, and should perform
  2382. any state save or restore required by your device across suspend or
  2383. hibernate states.
  2384. </para>
  2385. <synopsis>int (*suspend) (struct drm_device *, pm_message_t state);
  2386. int (*resume) (struct drm_device *);</synopsis>
  2387. <para>
  2388. Those are legacy suspend and resume methods. New driver should use the
  2389. power management interface provided by their bus type (usually through
  2390. the struct <structname>device_driver</structname> dev_pm_ops) and set
  2391. these methods to NULL.
  2392. </para>
  2393. </sect1>
  2394. <sect1>
  2395. <title>DMA services</title>
  2396. <para>
  2397. This should cover how DMA mapping etc. is supported by the core.
  2398. These functions are deprecated and should not be used.
  2399. </para>
  2400. </sect1>
  2401. </chapter>
  2402. <!-- TODO
  2403. - Add a glossary
  2404. - Document the struct_mutex catch-all lock
  2405. - Document connector properties
  2406. - Why is the load method optional?
  2407. - What are drivers supposed to set the initial display state to, and how?
  2408. Connector's DPMS states are not initialized and are thus equal to
  2409. DRM_MODE_DPMS_ON. The fbcon compatibility layer calls
  2410. drm_helper_disable_unused_functions(), which disables unused encoders and
  2411. CRTCs, but doesn't touch the connectors' DPMS state, and
  2412. drm_helper_connector_dpms() in reaction to fbdev blanking events. Do drivers
  2413. that don't implement (or just don't use) fbcon compatibility need to call
  2414. those functions themselves?
  2415. - KMS drivers must call drm_vblank_pre_modeset() and drm_vblank_post_modeset()
  2416. around mode setting. Should this be done in the DRM core?
  2417. - vblank_disable_allowed is set to 1 in the first drm_vblank_post_modeset()
  2418. call and never set back to 0. It seems to be safe to permanently set it to 1
  2419. in drm_vblank_init() for KMS driver, and it might be safe for UMS drivers as
  2420. well. This should be investigated.
  2421. - crtc and connector .save and .restore operations are only used internally in
  2422. drivers, should they be removed from the core?
  2423. - encoder mid-layer .save and .restore operations are only used internally in
  2424. drivers, should they be removed from the core?
  2425. - encoder mid-layer .detect operation is only used internally in drivers,
  2426. should it be removed from the core?
  2427. -->
  2428. <!-- External interfaces -->
  2429. <chapter id="drmExternals">
  2430. <title>Userland interfaces</title>
  2431. <para>
  2432. The DRM core exports several interfaces to applications,
  2433. generally intended to be used through corresponding libdrm
  2434. wrapper functions. In addition, drivers export device-specific
  2435. interfaces for use by userspace drivers &amp; device-aware
  2436. applications through ioctls and sysfs files.
  2437. </para>
  2438. <para>
  2439. External interfaces include: memory mapping, context management,
  2440. DMA operations, AGP management, vblank control, fence
  2441. management, memory management, and output management.
  2442. </para>
  2443. <para>
  2444. Cover generic ioctls and sysfs layout here. We only need high-level
  2445. info, since man pages should cover the rest.
  2446. </para>
  2447. <!-- External: vblank handling -->
  2448. <sect1>
  2449. <title>VBlank event handling</title>
  2450. <para>
  2451. The DRM core exposes two vertical blank related ioctls:
  2452. <variablelist>
  2453. <varlistentry>
  2454. <term>DRM_IOCTL_WAIT_VBLANK</term>
  2455. <listitem>
  2456. <para>
  2457. This takes a struct drm_wait_vblank structure as its argument,
  2458. and it is used to block or request a signal when a specified
  2459. vblank event occurs.
  2460. </para>
  2461. </listitem>
  2462. </varlistentry>
  2463. <varlistentry>
  2464. <term>DRM_IOCTL_MODESET_CTL</term>
  2465. <listitem>
  2466. <para>
  2467. This should be called by application level drivers before and
  2468. after mode setting, since on many devices the vertical blank
  2469. counter is reset at that time. Internally, the DRM snapshots
  2470. the last vblank count when the ioctl is called with the
  2471. _DRM_PRE_MODESET command, so that the counter won't go backwards
  2472. (which is dealt with when _DRM_POST_MODESET is used).
  2473. </para>
  2474. </listitem>
  2475. </varlistentry>
  2476. </variablelist>
  2477. <!--!Edrivers/char/drm/drm_irq.c-->
  2478. </para>
  2479. </sect1>
  2480. </chapter>
  2481. <!-- API reference -->
  2482. <appendix id="drmDriverApi">
  2483. <title>DRM Driver API</title>
  2484. <para>
  2485. Include auto-generated API reference here (need to reference it
  2486. from paragraphs above too).
  2487. </para>
  2488. </appendix>
  2489. </book>