drm.tmpl 134 KB

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