drm.tmpl 127 KB

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