drm.tmpl 123 KB

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