drm_atomic_helper.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Copyright (C) 2014 Intel Corp.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Rob Clark <robdclark@gmail.com>
  25. * Daniel Vetter <daniel.vetter@ffwll.ch>
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_atomic.h>
  29. #include <drm/drm_plane_helper.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_atomic_helper.h>
  32. #include <linux/dma-fence.h>
  33. #include "drm_crtc_internal.h"
  34. /**
  35. * DOC: overview
  36. *
  37. * This helper library provides implementations of check and commit functions on
  38. * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
  39. * also provides convenience implementations for the atomic state handling
  40. * callbacks for drivers which don't need to subclass the drm core structures to
  41. * add their own additional internal state.
  42. *
  43. * This library also provides default implementations for the check callback in
  44. * drm_atomic_helper_check() and for the commit callback with
  45. * drm_atomic_helper_commit(). But the individual stages and callbacks are
  46. * exposed to allow drivers to mix and match and e.g. use the plane helpers only
  47. * together with a driver private modeset implementation.
  48. *
  49. * This library also provides implementations for all the legacy driver
  50. * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
  51. * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
  52. * various functions to implement set_property callbacks. New drivers must not
  53. * implement these functions themselves but must use the provided helpers.
  54. *
  55. * The atomic helper uses the same function table structures as all other
  56. * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
  57. * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
  58. * also shares the &struct drm_plane_helper_funcs function table with the plane
  59. * helpers.
  60. */
  61. static void
  62. drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
  63. struct drm_plane_state *old_plane_state,
  64. struct drm_plane_state *plane_state,
  65. struct drm_plane *plane)
  66. {
  67. struct drm_crtc_state *crtc_state;
  68. if (old_plane_state->crtc) {
  69. crtc_state = drm_atomic_get_new_crtc_state(state,
  70. old_plane_state->crtc);
  71. if (WARN_ON(!crtc_state))
  72. return;
  73. crtc_state->planes_changed = true;
  74. }
  75. if (plane_state->crtc) {
  76. crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
  77. if (WARN_ON(!crtc_state))
  78. return;
  79. crtc_state->planes_changed = true;
  80. }
  81. }
  82. static int handle_conflicting_encoders(struct drm_atomic_state *state,
  83. bool disable_conflicting_encoders)
  84. {
  85. struct drm_connector_state *new_conn_state;
  86. struct drm_connector *connector;
  87. struct drm_connector_list_iter conn_iter;
  88. struct drm_encoder *encoder;
  89. unsigned encoder_mask = 0;
  90. int i, ret = 0;
  91. /*
  92. * First loop, find all newly assigned encoders from the connectors
  93. * part of the state. If the same encoder is assigned to multiple
  94. * connectors bail out.
  95. */
  96. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  97. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  98. struct drm_encoder *new_encoder;
  99. if (!new_conn_state->crtc)
  100. continue;
  101. if (funcs->atomic_best_encoder)
  102. new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
  103. else if (funcs->best_encoder)
  104. new_encoder = funcs->best_encoder(connector);
  105. else
  106. new_encoder = drm_atomic_helper_best_encoder(connector);
  107. if (new_encoder) {
  108. if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
  109. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
  110. new_encoder->base.id, new_encoder->name,
  111. connector->base.id, connector->name);
  112. return -EINVAL;
  113. }
  114. encoder_mask |= 1 << drm_encoder_index(new_encoder);
  115. }
  116. }
  117. if (!encoder_mask)
  118. return 0;
  119. /*
  120. * Second loop, iterate over all connectors not part of the state.
  121. *
  122. * If a conflicting encoder is found and disable_conflicting_encoders
  123. * is not set, an error is returned. Userspace can provide a solution
  124. * through the atomic ioctl.
  125. *
  126. * If the flag is set conflicting connectors are removed from the crtc
  127. * and the crtc is disabled if no encoder is left. This preserves
  128. * compatibility with the legacy set_config behavior.
  129. */
  130. drm_connector_list_iter_begin(state->dev, &conn_iter);
  131. drm_for_each_connector_iter(connector, &conn_iter) {
  132. struct drm_crtc_state *crtc_state;
  133. if (drm_atomic_get_new_connector_state(state, connector))
  134. continue;
  135. encoder = connector->state->best_encoder;
  136. if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
  137. continue;
  138. if (!disable_conflicting_encoders) {
  139. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
  140. encoder->base.id, encoder->name,
  141. connector->state->crtc->base.id,
  142. connector->state->crtc->name,
  143. connector->base.id, connector->name);
  144. ret = -EINVAL;
  145. goto out;
  146. }
  147. new_conn_state = drm_atomic_get_connector_state(state, connector);
  148. if (IS_ERR(new_conn_state)) {
  149. ret = PTR_ERR(new_conn_state);
  150. goto out;
  151. }
  152. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
  153. encoder->base.id, encoder->name,
  154. new_conn_state->crtc->base.id, new_conn_state->crtc->name,
  155. connector->base.id, connector->name);
  156. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  157. ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
  158. if (ret)
  159. goto out;
  160. if (!crtc_state->connector_mask) {
  161. ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
  162. NULL);
  163. if (ret < 0)
  164. goto out;
  165. crtc_state->active = false;
  166. }
  167. }
  168. out:
  169. drm_connector_list_iter_end(&conn_iter);
  170. return ret;
  171. }
  172. static void
  173. set_best_encoder(struct drm_atomic_state *state,
  174. struct drm_connector_state *conn_state,
  175. struct drm_encoder *encoder)
  176. {
  177. struct drm_crtc_state *crtc_state;
  178. struct drm_crtc *crtc;
  179. if (conn_state->best_encoder) {
  180. /* Unset the encoder_mask in the old crtc state. */
  181. crtc = conn_state->connector->state->crtc;
  182. /* A NULL crtc is an error here because we should have
  183. * duplicated a NULL best_encoder when crtc was NULL.
  184. * As an exception restoring duplicated atomic state
  185. * during resume is allowed, so don't warn when
  186. * best_encoder is equal to encoder we intend to set.
  187. */
  188. WARN_ON(!crtc && encoder != conn_state->best_encoder);
  189. if (crtc) {
  190. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  191. crtc_state->encoder_mask &=
  192. ~(1 << drm_encoder_index(conn_state->best_encoder));
  193. }
  194. }
  195. if (encoder) {
  196. crtc = conn_state->crtc;
  197. WARN_ON(!crtc);
  198. if (crtc) {
  199. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  200. crtc_state->encoder_mask |=
  201. 1 << drm_encoder_index(encoder);
  202. }
  203. }
  204. conn_state->best_encoder = encoder;
  205. }
  206. static void
  207. steal_encoder(struct drm_atomic_state *state,
  208. struct drm_encoder *encoder)
  209. {
  210. struct drm_crtc_state *crtc_state;
  211. struct drm_connector *connector;
  212. struct drm_connector_state *old_connector_state, *new_connector_state;
  213. int i;
  214. for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
  215. struct drm_crtc *encoder_crtc;
  216. if (new_connector_state->best_encoder != encoder)
  217. continue;
  218. encoder_crtc = old_connector_state->crtc;
  219. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
  220. encoder->base.id, encoder->name,
  221. encoder_crtc->base.id, encoder_crtc->name);
  222. set_best_encoder(state, new_connector_state, NULL);
  223. crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
  224. crtc_state->connectors_changed = true;
  225. return;
  226. }
  227. }
  228. static int
  229. update_connector_routing(struct drm_atomic_state *state,
  230. struct drm_connector *connector,
  231. struct drm_connector_state *old_connector_state,
  232. struct drm_connector_state *new_connector_state)
  233. {
  234. const struct drm_connector_helper_funcs *funcs;
  235. struct drm_encoder *new_encoder;
  236. struct drm_crtc_state *crtc_state;
  237. DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
  238. connector->base.id,
  239. connector->name);
  240. if (old_connector_state->crtc != new_connector_state->crtc) {
  241. if (old_connector_state->crtc) {
  242. crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
  243. crtc_state->connectors_changed = true;
  244. }
  245. if (new_connector_state->crtc) {
  246. crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
  247. crtc_state->connectors_changed = true;
  248. }
  249. }
  250. if (!new_connector_state->crtc) {
  251. DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
  252. connector->base.id,
  253. connector->name);
  254. set_best_encoder(state, new_connector_state, NULL);
  255. return 0;
  256. }
  257. funcs = connector->helper_private;
  258. if (funcs->atomic_best_encoder)
  259. new_encoder = funcs->atomic_best_encoder(connector,
  260. new_connector_state);
  261. else if (funcs->best_encoder)
  262. new_encoder = funcs->best_encoder(connector);
  263. else
  264. new_encoder = drm_atomic_helper_best_encoder(connector);
  265. if (!new_encoder) {
  266. DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
  267. connector->base.id,
  268. connector->name);
  269. return -EINVAL;
  270. }
  271. if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
  272. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
  273. new_encoder->base.id,
  274. new_encoder->name,
  275. new_connector_state->crtc->base.id,
  276. new_connector_state->crtc->name);
  277. return -EINVAL;
  278. }
  279. if (new_encoder == new_connector_state->best_encoder) {
  280. set_best_encoder(state, new_connector_state, new_encoder);
  281. DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
  282. connector->base.id,
  283. connector->name,
  284. new_encoder->base.id,
  285. new_encoder->name,
  286. new_connector_state->crtc->base.id,
  287. new_connector_state->crtc->name);
  288. return 0;
  289. }
  290. steal_encoder(state, new_encoder);
  291. set_best_encoder(state, new_connector_state, new_encoder);
  292. crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
  293. crtc_state->connectors_changed = true;
  294. DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
  295. connector->base.id,
  296. connector->name,
  297. new_encoder->base.id,
  298. new_encoder->name,
  299. new_connector_state->crtc->base.id,
  300. new_connector_state->crtc->name);
  301. return 0;
  302. }
  303. static int
  304. mode_fixup(struct drm_atomic_state *state)
  305. {
  306. struct drm_crtc *crtc;
  307. struct drm_crtc_state *new_crtc_state;
  308. struct drm_connector *connector;
  309. struct drm_connector_state *new_conn_state;
  310. int i;
  311. int ret;
  312. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  313. if (!new_crtc_state->mode_changed &&
  314. !new_crtc_state->connectors_changed)
  315. continue;
  316. drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
  317. }
  318. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  319. const struct drm_encoder_helper_funcs *funcs;
  320. struct drm_encoder *encoder;
  321. WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
  322. if (!new_conn_state->crtc || !new_conn_state->best_encoder)
  323. continue;
  324. new_crtc_state =
  325. drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  326. /*
  327. * Each encoder has at most one connector (since we always steal
  328. * it away), so we won't call ->mode_fixup twice.
  329. */
  330. encoder = new_conn_state->best_encoder;
  331. funcs = encoder->helper_private;
  332. ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
  333. &new_crtc_state->adjusted_mode);
  334. if (!ret) {
  335. DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
  336. return -EINVAL;
  337. }
  338. if (funcs && funcs->atomic_check) {
  339. ret = funcs->atomic_check(encoder, new_crtc_state,
  340. new_conn_state);
  341. if (ret) {
  342. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
  343. encoder->base.id, encoder->name);
  344. return ret;
  345. }
  346. } else if (funcs && funcs->mode_fixup) {
  347. ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
  348. &new_crtc_state->adjusted_mode);
  349. if (!ret) {
  350. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
  351. encoder->base.id, encoder->name);
  352. return -EINVAL;
  353. }
  354. }
  355. }
  356. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  357. const struct drm_crtc_helper_funcs *funcs;
  358. if (!new_crtc_state->enable)
  359. continue;
  360. if (!new_crtc_state->mode_changed &&
  361. !new_crtc_state->connectors_changed)
  362. continue;
  363. funcs = crtc->helper_private;
  364. if (!funcs->mode_fixup)
  365. continue;
  366. ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
  367. &new_crtc_state->adjusted_mode);
  368. if (!ret) {
  369. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
  370. crtc->base.id, crtc->name);
  371. return -EINVAL;
  372. }
  373. }
  374. return 0;
  375. }
  376. /**
  377. * drm_atomic_helper_check_modeset - validate state object for modeset changes
  378. * @dev: DRM device
  379. * @state: the driver state object
  380. *
  381. * Check the state object to see if the requested state is physically possible.
  382. * This does all the crtc and connector related computations for an atomic
  383. * update and adds any additional connectors needed for full modesets. It calls
  384. * the various per-object callbacks in the follow order:
  385. *
  386. * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
  387. * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
  388. * 3. If it's determined a modeset is needed then all connectors on the affected crtc
  389. * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
  390. * 4. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
  391. * 5. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
  392. * This function is only called when the encoder will be part of a configured crtc,
  393. * it must not be used for implementing connector property validation.
  394. * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
  395. * instead.
  396. * 6. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
  397. *
  398. * &drm_crtc_state.mode_changed is set when the input mode is changed.
  399. * &drm_crtc_state.connectors_changed is set when a connector is added or
  400. * removed from the crtc. &drm_crtc_state.active_changed is set when
  401. * &drm_crtc_state.active changes, which is used for DPMS.
  402. * See also: drm_atomic_crtc_needs_modeset()
  403. *
  404. * IMPORTANT:
  405. *
  406. * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
  407. * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
  408. * without a full modeset) _must_ call this function afterwards after that
  409. * change. It is permitted to call this function multiple times for the same
  410. * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
  411. * upon the adjusted dotclock for fifo space allocation and watermark
  412. * computation.
  413. *
  414. * RETURNS:
  415. * Zero for success or -errno
  416. */
  417. int
  418. drm_atomic_helper_check_modeset(struct drm_device *dev,
  419. struct drm_atomic_state *state)
  420. {
  421. struct drm_crtc *crtc;
  422. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  423. struct drm_connector *connector;
  424. struct drm_connector_state *old_connector_state, *new_connector_state;
  425. int i, ret;
  426. unsigned connectors_mask = 0;
  427. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  428. bool has_connectors =
  429. !!new_crtc_state->connector_mask;
  430. if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
  431. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
  432. crtc->base.id, crtc->name);
  433. new_crtc_state->mode_changed = true;
  434. }
  435. if (old_crtc_state->enable != new_crtc_state->enable) {
  436. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
  437. crtc->base.id, crtc->name);
  438. /*
  439. * For clarity this assignment is done here, but
  440. * enable == 0 is only true when there are no
  441. * connectors and a NULL mode.
  442. *
  443. * The other way around is true as well. enable != 0
  444. * iff connectors are attached and a mode is set.
  445. */
  446. new_crtc_state->mode_changed = true;
  447. new_crtc_state->connectors_changed = true;
  448. }
  449. if (old_crtc_state->active != new_crtc_state->active) {
  450. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
  451. crtc->base.id, crtc->name);
  452. new_crtc_state->active_changed = true;
  453. }
  454. if (new_crtc_state->enable != has_connectors) {
  455. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
  456. crtc->base.id, crtc->name);
  457. return -EINVAL;
  458. }
  459. }
  460. ret = handle_conflicting_encoders(state, false);
  461. if (ret)
  462. return ret;
  463. for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
  464. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  465. /*
  466. * This only sets crtc->connectors_changed for routing changes,
  467. * drivers must set crtc->connectors_changed themselves when
  468. * connector properties need to be updated.
  469. */
  470. ret = update_connector_routing(state, connector,
  471. old_connector_state,
  472. new_connector_state);
  473. if (ret)
  474. return ret;
  475. if (old_connector_state->crtc) {
  476. new_crtc_state = drm_atomic_get_new_crtc_state(state,
  477. old_connector_state->crtc);
  478. if (old_connector_state->link_status !=
  479. new_connector_state->link_status)
  480. new_crtc_state->connectors_changed = true;
  481. }
  482. if (funcs->atomic_check)
  483. ret = funcs->atomic_check(connector, new_connector_state);
  484. if (ret)
  485. return ret;
  486. connectors_mask += BIT(i);
  487. }
  488. /*
  489. * After all the routing has been prepared we need to add in any
  490. * connector which is itself unchanged, but who's crtc changes it's
  491. * configuration. This must be done before calling mode_fixup in case a
  492. * crtc only changed its mode but has the same set of connectors.
  493. */
  494. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  495. if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
  496. continue;
  497. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
  498. crtc->base.id, crtc->name,
  499. new_crtc_state->enable ? 'y' : 'n',
  500. new_crtc_state->active ? 'y' : 'n');
  501. ret = drm_atomic_add_affected_connectors(state, crtc);
  502. if (ret != 0)
  503. return ret;
  504. ret = drm_atomic_add_affected_planes(state, crtc);
  505. if (ret != 0)
  506. return ret;
  507. }
  508. /*
  509. * Iterate over all connectors again, to make sure atomic_check()
  510. * has been called on them when a modeset is forced.
  511. */
  512. for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
  513. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  514. if (connectors_mask & BIT(i))
  515. continue;
  516. if (funcs->atomic_check)
  517. ret = funcs->atomic_check(connector, new_connector_state);
  518. if (ret)
  519. return ret;
  520. }
  521. return mode_fixup(state);
  522. }
  523. EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
  524. /**
  525. * drm_atomic_helper_check_planes - validate state object for planes changes
  526. * @dev: DRM device
  527. * @state: the driver state object
  528. *
  529. * Check the state object to see if the requested state is physically possible.
  530. * This does all the plane update related checks using by calling into the
  531. * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
  532. * hooks provided by the driver.
  533. *
  534. * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
  535. * updated planes.
  536. *
  537. * RETURNS:
  538. * Zero for success or -errno
  539. */
  540. int
  541. drm_atomic_helper_check_planes(struct drm_device *dev,
  542. struct drm_atomic_state *state)
  543. {
  544. struct drm_crtc *crtc;
  545. struct drm_crtc_state *new_crtc_state;
  546. struct drm_plane *plane;
  547. struct drm_plane_state *new_plane_state, *old_plane_state;
  548. int i, ret = 0;
  549. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  550. const struct drm_plane_helper_funcs *funcs;
  551. funcs = plane->helper_private;
  552. drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
  553. if (!funcs || !funcs->atomic_check)
  554. continue;
  555. ret = funcs->atomic_check(plane, new_plane_state);
  556. if (ret) {
  557. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
  558. plane->base.id, plane->name);
  559. return ret;
  560. }
  561. }
  562. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  563. const struct drm_crtc_helper_funcs *funcs;
  564. funcs = crtc->helper_private;
  565. if (!funcs || !funcs->atomic_check)
  566. continue;
  567. ret = funcs->atomic_check(crtc, new_crtc_state);
  568. if (ret) {
  569. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
  570. crtc->base.id, crtc->name);
  571. return ret;
  572. }
  573. }
  574. return ret;
  575. }
  576. EXPORT_SYMBOL(drm_atomic_helper_check_planes);
  577. /**
  578. * drm_atomic_helper_check - validate state object
  579. * @dev: DRM device
  580. * @state: the driver state object
  581. *
  582. * Check the state object to see if the requested state is physically possible.
  583. * Only crtcs and planes have check callbacks, so for any additional (global)
  584. * checking that a driver needs it can simply wrap that around this function.
  585. * Drivers without such needs can directly use this as their
  586. * &drm_mode_config_funcs.atomic_check callback.
  587. *
  588. * This just wraps the two parts of the state checking for planes and modeset
  589. * state in the default order: First it calls drm_atomic_helper_check_modeset()
  590. * and then drm_atomic_helper_check_planes(). The assumption is that the
  591. * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
  592. * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
  593. * watermarks.
  594. *
  595. * RETURNS:
  596. * Zero for success or -errno
  597. */
  598. int drm_atomic_helper_check(struct drm_device *dev,
  599. struct drm_atomic_state *state)
  600. {
  601. int ret;
  602. ret = drm_atomic_helper_check_modeset(dev, state);
  603. if (ret)
  604. return ret;
  605. ret = drm_atomic_helper_check_planes(dev, state);
  606. if (ret)
  607. return ret;
  608. return ret;
  609. }
  610. EXPORT_SYMBOL(drm_atomic_helper_check);
  611. static void
  612. disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
  613. {
  614. struct drm_connector *connector;
  615. struct drm_connector_state *old_conn_state, *new_conn_state;
  616. struct drm_crtc *crtc;
  617. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  618. int i;
  619. for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
  620. const struct drm_encoder_helper_funcs *funcs;
  621. struct drm_encoder *encoder;
  622. /* Shut down everything that's in the changeset and currently
  623. * still on. So need to check the old, saved state. */
  624. if (!old_conn_state->crtc)
  625. continue;
  626. old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
  627. if (!old_crtc_state->active ||
  628. !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
  629. continue;
  630. encoder = old_conn_state->best_encoder;
  631. /* We shouldn't get this far if we didn't previously have
  632. * an encoder.. but WARN_ON() rather than explode.
  633. */
  634. if (WARN_ON(!encoder))
  635. continue;
  636. funcs = encoder->helper_private;
  637. DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
  638. encoder->base.id, encoder->name);
  639. /*
  640. * Each encoder has at most one connector (since we always steal
  641. * it away), so we won't call disable hooks twice.
  642. */
  643. drm_bridge_disable(encoder->bridge);
  644. /* Right function depends upon target state. */
  645. if (funcs) {
  646. if (new_conn_state->crtc && funcs->prepare)
  647. funcs->prepare(encoder);
  648. else if (funcs->disable)
  649. funcs->disable(encoder);
  650. else if (funcs->dpms)
  651. funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  652. }
  653. drm_bridge_post_disable(encoder->bridge);
  654. }
  655. for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
  656. const struct drm_crtc_helper_funcs *funcs;
  657. /* Shut down everything that needs a full modeset. */
  658. if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
  659. continue;
  660. if (!old_crtc_state->active)
  661. continue;
  662. funcs = crtc->helper_private;
  663. DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
  664. crtc->base.id, crtc->name);
  665. /* Right function depends upon target state. */
  666. if (new_crtc_state->enable && funcs->prepare)
  667. funcs->prepare(crtc);
  668. else if (funcs->atomic_disable)
  669. funcs->atomic_disable(crtc, old_crtc_state);
  670. else if (funcs->disable)
  671. funcs->disable(crtc);
  672. else
  673. funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  674. }
  675. }
  676. /**
  677. * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
  678. * @dev: DRM device
  679. * @old_state: atomic state object with old state structures
  680. *
  681. * This function updates all the various legacy modeset state pointers in
  682. * connectors, encoders and crtcs. It also updates the timestamping constants
  683. * used for precise vblank timestamps by calling
  684. * drm_calc_timestamping_constants().
  685. *
  686. * Drivers can use this for building their own atomic commit if they don't have
  687. * a pure helper-based modeset implementation.
  688. */
  689. void
  690. drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
  691. struct drm_atomic_state *old_state)
  692. {
  693. struct drm_connector *connector;
  694. struct drm_connector_state *old_conn_state, *new_conn_state;
  695. struct drm_crtc *crtc;
  696. struct drm_crtc_state *new_crtc_state;
  697. int i;
  698. /* clear out existing links and update dpms */
  699. for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
  700. if (connector->encoder) {
  701. WARN_ON(!connector->encoder->crtc);
  702. connector->encoder->crtc = NULL;
  703. connector->encoder = NULL;
  704. }
  705. crtc = new_conn_state->crtc;
  706. if ((!crtc && old_conn_state->crtc) ||
  707. (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
  708. struct drm_property *dpms_prop =
  709. dev->mode_config.dpms_property;
  710. int mode = DRM_MODE_DPMS_OFF;
  711. if (crtc && crtc->state->active)
  712. mode = DRM_MODE_DPMS_ON;
  713. connector->dpms = mode;
  714. drm_object_property_set_value(&connector->base,
  715. dpms_prop, mode);
  716. }
  717. }
  718. /* set new links */
  719. for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
  720. if (!new_conn_state->crtc)
  721. continue;
  722. if (WARN_ON(!new_conn_state->best_encoder))
  723. continue;
  724. connector->encoder = new_conn_state->best_encoder;
  725. connector->encoder->crtc = new_conn_state->crtc;
  726. }
  727. /* set legacy state in the crtc structure */
  728. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  729. struct drm_plane *primary = crtc->primary;
  730. struct drm_plane_state *new_plane_state;
  731. crtc->mode = new_crtc_state->mode;
  732. crtc->enabled = new_crtc_state->enable;
  733. new_plane_state =
  734. drm_atomic_get_new_plane_state(old_state, primary);
  735. if (new_plane_state && new_plane_state->crtc == crtc) {
  736. crtc->x = new_plane_state->src_x >> 16;
  737. crtc->y = new_plane_state->src_y >> 16;
  738. }
  739. if (new_crtc_state->enable)
  740. drm_calc_timestamping_constants(crtc,
  741. &new_crtc_state->adjusted_mode);
  742. }
  743. }
  744. EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
  745. static void
  746. crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
  747. {
  748. struct drm_crtc *crtc;
  749. struct drm_crtc_state *new_crtc_state;
  750. struct drm_connector *connector;
  751. struct drm_connector_state *new_conn_state;
  752. int i;
  753. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  754. const struct drm_crtc_helper_funcs *funcs;
  755. if (!new_crtc_state->mode_changed)
  756. continue;
  757. funcs = crtc->helper_private;
  758. if (new_crtc_state->enable && funcs->mode_set_nofb) {
  759. DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
  760. crtc->base.id, crtc->name);
  761. funcs->mode_set_nofb(crtc);
  762. }
  763. }
  764. for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
  765. const struct drm_encoder_helper_funcs *funcs;
  766. struct drm_encoder *encoder;
  767. struct drm_display_mode *mode, *adjusted_mode;
  768. if (!new_conn_state->best_encoder)
  769. continue;
  770. encoder = new_conn_state->best_encoder;
  771. funcs = encoder->helper_private;
  772. new_crtc_state = new_conn_state->crtc->state;
  773. mode = &new_crtc_state->mode;
  774. adjusted_mode = &new_crtc_state->adjusted_mode;
  775. if (!new_crtc_state->mode_changed)
  776. continue;
  777. DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
  778. encoder->base.id, encoder->name);
  779. /*
  780. * Each encoder has at most one connector (since we always steal
  781. * it away), so we won't call mode_set hooks twice.
  782. */
  783. if (funcs && funcs->atomic_mode_set) {
  784. funcs->atomic_mode_set(encoder, new_crtc_state,
  785. new_conn_state);
  786. } else if (funcs && funcs->mode_set) {
  787. funcs->mode_set(encoder, mode, adjusted_mode);
  788. }
  789. drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
  790. }
  791. }
  792. /**
  793. * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
  794. * @dev: DRM device
  795. * @old_state: atomic state object with old state structures
  796. *
  797. * This function shuts down all the outputs that need to be shut down and
  798. * prepares them (if required) with the new mode.
  799. *
  800. * For compatibility with legacy crtc helpers this should be called before
  801. * drm_atomic_helper_commit_planes(), which is what the default commit function
  802. * does. But drivers with different needs can group the modeset commits together
  803. * and do the plane commits at the end. This is useful for drivers doing runtime
  804. * PM since planes updates then only happen when the CRTC is actually enabled.
  805. */
  806. void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
  807. struct drm_atomic_state *old_state)
  808. {
  809. disable_outputs(dev, old_state);
  810. drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
  811. crtc_set_mode(dev, old_state);
  812. }
  813. EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
  814. /**
  815. * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
  816. * @dev: DRM device
  817. * @old_state: atomic state object with old state structures
  818. *
  819. * This function enables all the outputs with the new configuration which had to
  820. * be turned off for the update.
  821. *
  822. * For compatibility with legacy crtc helpers this should be called after
  823. * drm_atomic_helper_commit_planes(), which is what the default commit function
  824. * does. But drivers with different needs can group the modeset commits together
  825. * and do the plane commits at the end. This is useful for drivers doing runtime
  826. * PM since planes updates then only happen when the CRTC is actually enabled.
  827. */
  828. void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
  829. struct drm_atomic_state *old_state)
  830. {
  831. struct drm_crtc *crtc;
  832. struct drm_crtc_state *new_crtc_state;
  833. struct drm_connector *connector;
  834. struct drm_connector_state *new_conn_state;
  835. int i;
  836. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  837. const struct drm_crtc_helper_funcs *funcs;
  838. /* Need to filter out CRTCs where only planes change. */
  839. if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
  840. continue;
  841. if (!new_crtc_state->active)
  842. continue;
  843. funcs = crtc->helper_private;
  844. if (new_crtc_state->enable) {
  845. DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
  846. crtc->base.id, crtc->name);
  847. if (funcs->enable)
  848. funcs->enable(crtc);
  849. else
  850. funcs->commit(crtc);
  851. }
  852. }
  853. for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
  854. const struct drm_encoder_helper_funcs *funcs;
  855. struct drm_encoder *encoder;
  856. if (!new_conn_state->best_encoder)
  857. continue;
  858. if (!new_conn_state->crtc->state->active ||
  859. !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
  860. continue;
  861. encoder = new_conn_state->best_encoder;
  862. funcs = encoder->helper_private;
  863. DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
  864. encoder->base.id, encoder->name);
  865. /*
  866. * Each encoder has at most one connector (since we always steal
  867. * it away), so we won't call enable hooks twice.
  868. */
  869. drm_bridge_pre_enable(encoder->bridge);
  870. if (funcs) {
  871. if (funcs->enable)
  872. funcs->enable(encoder);
  873. else if (funcs->commit)
  874. funcs->commit(encoder);
  875. }
  876. drm_bridge_enable(encoder->bridge);
  877. }
  878. }
  879. EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
  880. /**
  881. * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
  882. * @dev: DRM device
  883. * @state: atomic state object with old state structures
  884. * @pre_swap: If true, do an interruptible wait, and @state is the new state.
  885. * Otherwise @state is the old state.
  886. *
  887. * For implicit sync, driver should fish the exclusive fence out from the
  888. * incoming fb's and stash it in the drm_plane_state. This is called after
  889. * drm_atomic_helper_swap_state() so it uses the current plane state (and
  890. * just uses the atomic state to find the changed planes)
  891. *
  892. * Note that @pre_swap is needed since the point where we block for fences moves
  893. * around depending upon whether an atomic commit is blocking or
  894. * non-blocking. For non-blocking commit all waiting needs to happen after
  895. * drm_atomic_helper_swap_state() is called, but for blocking commits we want
  896. * to wait **before** we do anything that can't be easily rolled back. That is
  897. * before we call drm_atomic_helper_swap_state().
  898. *
  899. * Returns zero if success or < 0 if dma_fence_wait() fails.
  900. */
  901. int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
  902. struct drm_atomic_state *state,
  903. bool pre_swap)
  904. {
  905. struct drm_plane *plane;
  906. struct drm_plane_state *new_plane_state;
  907. int i, ret;
  908. for_each_new_plane_in_state(state, plane, new_plane_state, i) {
  909. if (!new_plane_state->fence)
  910. continue;
  911. WARN_ON(!new_plane_state->fb);
  912. /*
  913. * If waiting for fences pre-swap (ie: nonblock), userspace can
  914. * still interrupt the operation. Instead of blocking until the
  915. * timer expires, make the wait interruptible.
  916. */
  917. ret = dma_fence_wait(new_plane_state->fence, pre_swap);
  918. if (ret)
  919. return ret;
  920. dma_fence_put(new_plane_state->fence);
  921. new_plane_state->fence = NULL;
  922. }
  923. return 0;
  924. }
  925. EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
  926. /**
  927. * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
  928. * @dev: DRM device
  929. * @old_state: atomic state object with old state structures
  930. *
  931. * Helper to, after atomic commit, wait for vblanks on all effected
  932. * crtcs (ie. before cleaning up old framebuffers using
  933. * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
  934. * framebuffers have actually changed to optimize for the legacy cursor and
  935. * plane update use-case.
  936. */
  937. void
  938. drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
  939. struct drm_atomic_state *old_state)
  940. {
  941. struct drm_crtc *crtc;
  942. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  943. int i, ret;
  944. unsigned crtc_mask = 0;
  945. /*
  946. * Legacy cursor ioctls are completely unsynced, and userspace
  947. * relies on that (by doing tons of cursor updates).
  948. */
  949. if (old_state->legacy_cursor_update)
  950. return;
  951. for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
  952. if (!new_crtc_state->active || !new_crtc_state->planes_changed)
  953. continue;
  954. ret = drm_crtc_vblank_get(crtc);
  955. if (ret != 0)
  956. continue;
  957. crtc_mask |= drm_crtc_mask(crtc);
  958. old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
  959. }
  960. for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  961. if (!(crtc_mask & drm_crtc_mask(crtc)))
  962. continue;
  963. ret = wait_event_timeout(dev->vblank[i].queue,
  964. old_state->crtcs[i].last_vblank_count !=
  965. drm_crtc_vblank_count(crtc),
  966. msecs_to_jiffies(50));
  967. WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
  968. crtc->base.id, crtc->name);
  969. drm_crtc_vblank_put(crtc);
  970. }
  971. }
  972. EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
  973. /**
  974. * drm_atomic_helper_commit_tail - commit atomic update to hardware
  975. * @old_state: atomic state object with old state structures
  976. *
  977. * This is the default implementation for the
  978. * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
  979. *
  980. * Note that the default ordering of how the various stages are called is to
  981. * match the legacy modeset helper library closest. One peculiarity of that is
  982. * that it doesn't mesh well with runtime PM at all.
  983. *
  984. * For drivers supporting runtime PM the recommended sequence is instead ::
  985. *
  986. * drm_atomic_helper_commit_modeset_disables(dev, old_state);
  987. *
  988. * drm_atomic_helper_commit_modeset_enables(dev, old_state);
  989. *
  990. * drm_atomic_helper_commit_planes(dev, old_state,
  991. * DRM_PLANE_COMMIT_ACTIVE_ONLY);
  992. *
  993. * for committing the atomic update to hardware. See the kerneldoc entries for
  994. * these three functions for more details.
  995. */
  996. void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
  997. {
  998. struct drm_device *dev = old_state->dev;
  999. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  1000. drm_atomic_helper_commit_planes(dev, old_state, 0);
  1001. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  1002. drm_atomic_helper_commit_hw_done(old_state);
  1003. drm_atomic_helper_wait_for_vblanks(dev, old_state);
  1004. drm_atomic_helper_cleanup_planes(dev, old_state);
  1005. }
  1006. EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
  1007. static void commit_tail(struct drm_atomic_state *old_state)
  1008. {
  1009. struct drm_device *dev = old_state->dev;
  1010. const struct drm_mode_config_helper_funcs *funcs;
  1011. funcs = dev->mode_config.helper_private;
  1012. drm_atomic_helper_wait_for_fences(dev, old_state, false);
  1013. drm_atomic_helper_wait_for_dependencies(old_state);
  1014. if (funcs && funcs->atomic_commit_tail)
  1015. funcs->atomic_commit_tail(old_state);
  1016. else
  1017. drm_atomic_helper_commit_tail(old_state);
  1018. drm_atomic_helper_commit_cleanup_done(old_state);
  1019. drm_atomic_state_put(old_state);
  1020. }
  1021. static void commit_work(struct work_struct *work)
  1022. {
  1023. struct drm_atomic_state *state = container_of(work,
  1024. struct drm_atomic_state,
  1025. commit_work);
  1026. commit_tail(state);
  1027. }
  1028. /**
  1029. * drm_atomic_helper_commit - commit validated state object
  1030. * @dev: DRM device
  1031. * @state: the driver state object
  1032. * @nonblock: whether nonblocking behavior is requested.
  1033. *
  1034. * This function commits a with drm_atomic_helper_check() pre-validated state
  1035. * object. This can still fail when e.g. the framebuffer reservation fails. This
  1036. * function implements nonblocking commits, using
  1037. * drm_atomic_helper_setup_commit() and related functions.
  1038. *
  1039. * Committing the actual hardware state is done through the
  1040. * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
  1041. * implementation drm_atomic_helper_commit_tail().
  1042. *
  1043. * RETURNS:
  1044. * Zero for success or -errno.
  1045. */
  1046. int drm_atomic_helper_commit(struct drm_device *dev,
  1047. struct drm_atomic_state *state,
  1048. bool nonblock)
  1049. {
  1050. int ret;
  1051. ret = drm_atomic_helper_setup_commit(state, nonblock);
  1052. if (ret)
  1053. return ret;
  1054. INIT_WORK(&state->commit_work, commit_work);
  1055. ret = drm_atomic_helper_prepare_planes(dev, state);
  1056. if (ret)
  1057. return ret;
  1058. if (!nonblock) {
  1059. ret = drm_atomic_helper_wait_for_fences(dev, state, true);
  1060. if (ret) {
  1061. drm_atomic_helper_cleanup_planes(dev, state);
  1062. return ret;
  1063. }
  1064. }
  1065. /*
  1066. * This is the point of no return - everything below never fails except
  1067. * when the hw goes bonghits. Which means we can commit the new state on
  1068. * the software side now.
  1069. */
  1070. drm_atomic_helper_swap_state(state, true);
  1071. /*
  1072. * Everything below can be run asynchronously without the need to grab
  1073. * any modeset locks at all under one condition: It must be guaranteed
  1074. * that the asynchronous work has either been cancelled (if the driver
  1075. * supports it, which at least requires that the framebuffers get
  1076. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  1077. * before the new state gets committed on the software side with
  1078. * drm_atomic_helper_swap_state().
  1079. *
  1080. * This scheme allows new atomic state updates to be prepared and
  1081. * checked in parallel to the asynchronous completion of the previous
  1082. * update. Which is important since compositors need to figure out the
  1083. * composition of the next frame right after having submitted the
  1084. * current layout.
  1085. *
  1086. * NOTE: Commit work has multiple phases, first hardware commit, then
  1087. * cleanup. We want them to overlap, hence need system_unbound_wq to
  1088. * make sure work items don't artifically stall on each another.
  1089. */
  1090. drm_atomic_state_get(state);
  1091. if (nonblock)
  1092. queue_work(system_unbound_wq, &state->commit_work);
  1093. else
  1094. commit_tail(state);
  1095. return 0;
  1096. }
  1097. EXPORT_SYMBOL(drm_atomic_helper_commit);
  1098. /**
  1099. * DOC: implementing nonblocking commit
  1100. *
  1101. * Nonblocking atomic commits have to be implemented in the following sequence:
  1102. *
  1103. * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
  1104. * which commit needs to call which can fail, so we want to run it first and
  1105. * synchronously.
  1106. *
  1107. * 2. Synchronize with any outstanding nonblocking commit worker threads which
  1108. * might be affected the new state update. This can be done by either cancelling
  1109. * or flushing the work items, depending upon whether the driver can deal with
  1110. * cancelled updates. Note that it is important to ensure that the framebuffer
  1111. * cleanup is still done when cancelling.
  1112. *
  1113. * Asynchronous workers need to have sufficient parallelism to be able to run
  1114. * different atomic commits on different CRTCs in parallel. The simplest way to
  1115. * achive this is by running them on the &system_unbound_wq work queue. Note
  1116. * that drivers are not required to split up atomic commits and run an
  1117. * individual commit in parallel - userspace is supposed to do that if it cares.
  1118. * But it might be beneficial to do that for modesets, since those necessarily
  1119. * must be done as one global operation, and enabling or disabling a CRTC can
  1120. * take a long time. But even that is not required.
  1121. *
  1122. * 3. The software state is updated synchronously with
  1123. * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
  1124. * locks means concurrent callers never see inconsistent state. And doing this
  1125. * while it's guaranteed that no relevant nonblocking worker runs means that
  1126. * nonblocking workers do not need grab any locks. Actually they must not grab
  1127. * locks, for otherwise the work flushing will deadlock.
  1128. *
  1129. * 4. Schedule a work item to do all subsequent steps, using the split-out
  1130. * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
  1131. * then cleaning up the framebuffers after the old framebuffer is no longer
  1132. * being displayed.
  1133. *
  1134. * The above scheme is implemented in the atomic helper libraries in
  1135. * drm_atomic_helper_commit() using a bunch of helper functions. See
  1136. * drm_atomic_helper_setup_commit() for a starting point.
  1137. */
  1138. static int stall_checks(struct drm_crtc *crtc, bool nonblock)
  1139. {
  1140. struct drm_crtc_commit *commit, *stall_commit = NULL;
  1141. bool completed = true;
  1142. int i;
  1143. long ret = 0;
  1144. spin_lock(&crtc->commit_lock);
  1145. i = 0;
  1146. list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
  1147. if (i == 0) {
  1148. completed = try_wait_for_completion(&commit->flip_done);
  1149. /* Userspace is not allowed to get ahead of the previous
  1150. * commit with nonblocking ones. */
  1151. if (!completed && nonblock) {
  1152. spin_unlock(&crtc->commit_lock);
  1153. return -EBUSY;
  1154. }
  1155. } else if (i == 1) {
  1156. stall_commit = commit;
  1157. drm_crtc_commit_get(stall_commit);
  1158. break;
  1159. }
  1160. i++;
  1161. }
  1162. spin_unlock(&crtc->commit_lock);
  1163. if (!stall_commit)
  1164. return 0;
  1165. /* We don't want to let commits get ahead of cleanup work too much,
  1166. * stalling on 2nd previous commit means triple-buffer won't ever stall.
  1167. */
  1168. ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
  1169. 10*HZ);
  1170. if (ret == 0)
  1171. DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
  1172. crtc->base.id, crtc->name);
  1173. drm_crtc_commit_put(stall_commit);
  1174. return ret < 0 ? ret : 0;
  1175. }
  1176. static void release_crtc_commit(struct completion *completion)
  1177. {
  1178. struct drm_crtc_commit *commit = container_of(completion,
  1179. typeof(*commit),
  1180. flip_done);
  1181. drm_crtc_commit_put(commit);
  1182. }
  1183. /**
  1184. * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
  1185. * @state: new modeset state to be committed
  1186. * @nonblock: whether nonblocking behavior is requested.
  1187. *
  1188. * This function prepares @state to be used by the atomic helper's support for
  1189. * nonblocking commits. Drivers using the nonblocking commit infrastructure
  1190. * should always call this function from their
  1191. * &drm_mode_config_funcs.atomic_commit hook.
  1192. *
  1193. * To be able to use this support drivers need to use a few more helper
  1194. * functions. drm_atomic_helper_wait_for_dependencies() must be called before
  1195. * actually committing the hardware state, and for nonblocking commits this call
  1196. * must be placed in the async worker. See also drm_atomic_helper_swap_state()
  1197. * and it's stall parameter, for when a driver's commit hooks look at the
  1198. * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
  1199. *
  1200. * Completion of the hardware commit step must be signalled using
  1201. * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
  1202. * to read or change any permanent software or hardware modeset state. The only
  1203. * exception is state protected by other means than &drm_modeset_lock locks.
  1204. * Only the free standing @state with pointers to the old state structures can
  1205. * be inspected, e.g. to clean up old buffers using
  1206. * drm_atomic_helper_cleanup_planes().
  1207. *
  1208. * At the very end, before cleaning up @state drivers must call
  1209. * drm_atomic_helper_commit_cleanup_done().
  1210. *
  1211. * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
  1212. * complete and esay-to-use default implementation of the atomic_commit() hook.
  1213. *
  1214. * The tracking of asynchronously executed and still pending commits is done
  1215. * using the core structure &drm_crtc_commit.
  1216. *
  1217. * By default there's no need to clean up resources allocated by this function
  1218. * explicitly: drm_atomic_state_default_clear() will take care of that
  1219. * automatically.
  1220. *
  1221. * Returns:
  1222. *
  1223. * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
  1224. * -ENOMEM on allocation failures and -EINTR when a signal is pending.
  1225. */
  1226. int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
  1227. bool nonblock)
  1228. {
  1229. struct drm_crtc *crtc;
  1230. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  1231. struct drm_crtc_commit *commit;
  1232. int i, ret;
  1233. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  1234. commit = kzalloc(sizeof(*commit), GFP_KERNEL);
  1235. if (!commit)
  1236. return -ENOMEM;
  1237. init_completion(&commit->flip_done);
  1238. init_completion(&commit->hw_done);
  1239. init_completion(&commit->cleanup_done);
  1240. INIT_LIST_HEAD(&commit->commit_entry);
  1241. kref_init(&commit->ref);
  1242. commit->crtc = crtc;
  1243. state->crtcs[i].commit = commit;
  1244. ret = stall_checks(crtc, nonblock);
  1245. if (ret)
  1246. return ret;
  1247. /* Drivers only send out events when at least either current or
  1248. * new CRTC state is active. Complete right away if everything
  1249. * stays off. */
  1250. if (!old_crtc_state->active && !new_crtc_state->active) {
  1251. complete_all(&commit->flip_done);
  1252. continue;
  1253. }
  1254. /* Legacy cursor updates are fully unsynced. */
  1255. if (state->legacy_cursor_update) {
  1256. complete_all(&commit->flip_done);
  1257. continue;
  1258. }
  1259. if (!new_crtc_state->event) {
  1260. commit->event = kzalloc(sizeof(*commit->event),
  1261. GFP_KERNEL);
  1262. if (!commit->event)
  1263. return -ENOMEM;
  1264. new_crtc_state->event = commit->event;
  1265. }
  1266. new_crtc_state->event->base.completion = &commit->flip_done;
  1267. new_crtc_state->event->base.completion_release = release_crtc_commit;
  1268. drm_crtc_commit_get(commit);
  1269. }
  1270. return 0;
  1271. }
  1272. EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
  1273. static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
  1274. {
  1275. struct drm_crtc_commit *commit;
  1276. int i = 0;
  1277. list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
  1278. /* skip the first entry, that's the current commit */
  1279. if (i == 1)
  1280. return commit;
  1281. i++;
  1282. }
  1283. return NULL;
  1284. }
  1285. /**
  1286. * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
  1287. * @old_state: atomic state object with old state structures
  1288. *
  1289. * This function waits for all preceeding commits that touch the same CRTC as
  1290. * @old_state to both be committed to the hardware (as signalled by
  1291. * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
  1292. * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
  1293. *
  1294. * This is part of the atomic helper support for nonblocking commits, see
  1295. * drm_atomic_helper_setup_commit() for an overview.
  1296. */
  1297. void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
  1298. {
  1299. struct drm_crtc *crtc;
  1300. struct drm_crtc_state *new_crtc_state;
  1301. struct drm_crtc_commit *commit;
  1302. int i;
  1303. long ret;
  1304. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  1305. spin_lock(&crtc->commit_lock);
  1306. commit = preceeding_commit(crtc);
  1307. if (commit)
  1308. drm_crtc_commit_get(commit);
  1309. spin_unlock(&crtc->commit_lock);
  1310. if (!commit)
  1311. continue;
  1312. ret = wait_for_completion_timeout(&commit->hw_done,
  1313. 10*HZ);
  1314. if (ret == 0)
  1315. DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
  1316. crtc->base.id, crtc->name);
  1317. /* Currently no support for overwriting flips, hence
  1318. * stall for previous one to execute completely. */
  1319. ret = wait_for_completion_timeout(&commit->flip_done,
  1320. 10*HZ);
  1321. if (ret == 0)
  1322. DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
  1323. crtc->base.id, crtc->name);
  1324. drm_crtc_commit_put(commit);
  1325. }
  1326. }
  1327. EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
  1328. /**
  1329. * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
  1330. * @old_state: atomic state object with old state structures
  1331. *
  1332. * This function is used to signal completion of the hardware commit step. After
  1333. * this step the driver is not allowed to read or change any permanent software
  1334. * or hardware modeset state. The only exception is state protected by other
  1335. * means than &drm_modeset_lock locks.
  1336. *
  1337. * Drivers should try to postpone any expensive or delayed cleanup work after
  1338. * this function is called.
  1339. *
  1340. * This is part of the atomic helper support for nonblocking commits, see
  1341. * drm_atomic_helper_setup_commit() for an overview.
  1342. */
  1343. void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
  1344. {
  1345. struct drm_crtc *crtc;
  1346. struct drm_crtc_state *new_crtc_state;
  1347. struct drm_crtc_commit *commit;
  1348. int i;
  1349. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  1350. commit = old_state->crtcs[i].commit;
  1351. if (!commit)
  1352. continue;
  1353. /* backend must have consumed any event by now */
  1354. WARN_ON(new_crtc_state->event);
  1355. spin_lock(&crtc->commit_lock);
  1356. complete_all(&commit->hw_done);
  1357. spin_unlock(&crtc->commit_lock);
  1358. }
  1359. }
  1360. EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
  1361. /**
  1362. * drm_atomic_helper_commit_cleanup_done - signal completion of commit
  1363. * @old_state: atomic state object with old state structures
  1364. *
  1365. * This signals completion of the atomic update @old_state, including any
  1366. * cleanup work. If used, it must be called right before calling
  1367. * drm_atomic_state_put().
  1368. *
  1369. * This is part of the atomic helper support for nonblocking commits, see
  1370. * drm_atomic_helper_setup_commit() for an overview.
  1371. */
  1372. void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
  1373. {
  1374. struct drm_crtc *crtc;
  1375. struct drm_crtc_state *new_crtc_state;
  1376. struct drm_crtc_commit *commit;
  1377. int i;
  1378. long ret;
  1379. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  1380. commit = old_state->crtcs[i].commit;
  1381. if (WARN_ON(!commit))
  1382. continue;
  1383. spin_lock(&crtc->commit_lock);
  1384. complete_all(&commit->cleanup_done);
  1385. WARN_ON(!try_wait_for_completion(&commit->hw_done));
  1386. /* commit_list borrows our reference, need to remove before we
  1387. * clean up our drm_atomic_state. But only after it actually
  1388. * completed, otherwise subsequent commits won't stall properly. */
  1389. if (try_wait_for_completion(&commit->flip_done))
  1390. goto del_commit;
  1391. spin_unlock(&crtc->commit_lock);
  1392. /* We must wait for the vblank event to signal our completion
  1393. * before releasing our reference, since the vblank work does
  1394. * not hold a reference of its own. */
  1395. ret = wait_for_completion_timeout(&commit->flip_done,
  1396. 10*HZ);
  1397. if (ret == 0)
  1398. DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
  1399. crtc->base.id, crtc->name);
  1400. spin_lock(&crtc->commit_lock);
  1401. del_commit:
  1402. list_del(&commit->commit_entry);
  1403. spin_unlock(&crtc->commit_lock);
  1404. }
  1405. }
  1406. EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
  1407. /**
  1408. * drm_atomic_helper_prepare_planes - prepare plane resources before commit
  1409. * @dev: DRM device
  1410. * @state: atomic state object with new state structures
  1411. *
  1412. * This function prepares plane state, specifically framebuffers, for the new
  1413. * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
  1414. * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
  1415. * any already successfully prepared framebuffer.
  1416. *
  1417. * Returns:
  1418. * 0 on success, negative error code on failure.
  1419. */
  1420. int drm_atomic_helper_prepare_planes(struct drm_device *dev,
  1421. struct drm_atomic_state *state)
  1422. {
  1423. struct drm_plane *plane;
  1424. struct drm_plane_state *new_plane_state;
  1425. int ret, i, j;
  1426. for_each_new_plane_in_state(state, plane, new_plane_state, i) {
  1427. const struct drm_plane_helper_funcs *funcs;
  1428. funcs = plane->helper_private;
  1429. if (funcs->prepare_fb) {
  1430. ret = funcs->prepare_fb(plane, new_plane_state);
  1431. if (ret)
  1432. goto fail;
  1433. }
  1434. }
  1435. return 0;
  1436. fail:
  1437. for_each_new_plane_in_state(state, plane, new_plane_state, j) {
  1438. const struct drm_plane_helper_funcs *funcs;
  1439. if (j >= i)
  1440. continue;
  1441. funcs = plane->helper_private;
  1442. if (funcs->cleanup_fb)
  1443. funcs->cleanup_fb(plane, new_plane_state);
  1444. }
  1445. return ret;
  1446. }
  1447. EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
  1448. static bool plane_crtc_active(const struct drm_plane_state *state)
  1449. {
  1450. return state->crtc && state->crtc->state->active;
  1451. }
  1452. /**
  1453. * drm_atomic_helper_commit_planes - commit plane state
  1454. * @dev: DRM device
  1455. * @old_state: atomic state object with old state structures
  1456. * @flags: flags for committing plane state
  1457. *
  1458. * This function commits the new plane state using the plane and atomic helper
  1459. * functions for planes and crtcs. It assumes that the atomic state has already
  1460. * been pushed into the relevant object state pointers, since this step can no
  1461. * longer fail.
  1462. *
  1463. * It still requires the global state object @old_state to know which planes and
  1464. * crtcs need to be updated though.
  1465. *
  1466. * Note that this function does all plane updates across all CRTCs in one step.
  1467. * If the hardware can't support this approach look at
  1468. * drm_atomic_helper_commit_planes_on_crtc() instead.
  1469. *
  1470. * Plane parameters can be updated by applications while the associated CRTC is
  1471. * disabled. The DRM/KMS core will store the parameters in the plane state,
  1472. * which will be available to the driver when the CRTC is turned on. As a result
  1473. * most drivers don't need to be immediately notified of plane updates for a
  1474. * disabled CRTC.
  1475. *
  1476. * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
  1477. * @flags in order not to receive plane update notifications related to a
  1478. * disabled CRTC. This avoids the need to manually ignore plane updates in
  1479. * driver code when the driver and/or hardware can't or just don't need to deal
  1480. * with updates on disabled CRTCs, for example when supporting runtime PM.
  1481. *
  1482. * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
  1483. * display controllers require to disable a CRTC's planes when the CRTC is
  1484. * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
  1485. * call for a plane if the CRTC of the old plane state needs a modesetting
  1486. * operation. Of course, the drivers need to disable the planes in their CRTC
  1487. * disable callbacks since no one else would do that.
  1488. *
  1489. * The drm_atomic_helper_commit() default implementation doesn't set the
  1490. * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
  1491. * This should not be copied blindly by drivers.
  1492. */
  1493. void drm_atomic_helper_commit_planes(struct drm_device *dev,
  1494. struct drm_atomic_state *old_state,
  1495. uint32_t flags)
  1496. {
  1497. struct drm_crtc *crtc;
  1498. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  1499. struct drm_plane *plane;
  1500. struct drm_plane_state *old_plane_state, *new_plane_state;
  1501. int i;
  1502. bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
  1503. bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
  1504. for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
  1505. const struct drm_crtc_helper_funcs *funcs;
  1506. funcs = crtc->helper_private;
  1507. if (!funcs || !funcs->atomic_begin)
  1508. continue;
  1509. if (active_only && !new_crtc_state->active)
  1510. continue;
  1511. funcs->atomic_begin(crtc, old_crtc_state);
  1512. }
  1513. for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
  1514. const struct drm_plane_helper_funcs *funcs;
  1515. bool disabling;
  1516. funcs = plane->helper_private;
  1517. if (!funcs)
  1518. continue;
  1519. disabling = drm_atomic_plane_disabling(old_plane_state,
  1520. new_plane_state);
  1521. if (active_only) {
  1522. /*
  1523. * Skip planes related to inactive CRTCs. If the plane
  1524. * is enabled use the state of the current CRTC. If the
  1525. * plane is being disabled use the state of the old
  1526. * CRTC to avoid skipping planes being disabled on an
  1527. * active CRTC.
  1528. */
  1529. if (!disabling && !plane_crtc_active(new_plane_state))
  1530. continue;
  1531. if (disabling && !plane_crtc_active(old_plane_state))
  1532. continue;
  1533. }
  1534. /*
  1535. * Special-case disabling the plane if drivers support it.
  1536. */
  1537. if (disabling && funcs->atomic_disable) {
  1538. struct drm_crtc_state *crtc_state;
  1539. crtc_state = old_plane_state->crtc->state;
  1540. if (drm_atomic_crtc_needs_modeset(crtc_state) &&
  1541. no_disable)
  1542. continue;
  1543. funcs->atomic_disable(plane, old_plane_state);
  1544. } else if (new_plane_state->crtc || disabling) {
  1545. funcs->atomic_update(plane, old_plane_state);
  1546. }
  1547. }
  1548. for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
  1549. const struct drm_crtc_helper_funcs *funcs;
  1550. funcs = crtc->helper_private;
  1551. if (!funcs || !funcs->atomic_flush)
  1552. continue;
  1553. if (active_only && !new_crtc_state->active)
  1554. continue;
  1555. funcs->atomic_flush(crtc, old_crtc_state);
  1556. }
  1557. }
  1558. EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
  1559. /**
  1560. * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
  1561. * @old_crtc_state: atomic state object with the old crtc state
  1562. *
  1563. * This function commits the new plane state using the plane and atomic helper
  1564. * functions for planes on the specific crtc. It assumes that the atomic state
  1565. * has already been pushed into the relevant object state pointers, since this
  1566. * step can no longer fail.
  1567. *
  1568. * This function is useful when plane updates should be done crtc-by-crtc
  1569. * instead of one global step like drm_atomic_helper_commit_planes() does.
  1570. *
  1571. * This function can only be savely used when planes are not allowed to move
  1572. * between different CRTCs because this function doesn't handle inter-CRTC
  1573. * depencies. Callers need to ensure that either no such depencies exist,
  1574. * resolve them through ordering of commit calls or through some other means.
  1575. */
  1576. void
  1577. drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
  1578. {
  1579. const struct drm_crtc_helper_funcs *crtc_funcs;
  1580. struct drm_crtc *crtc = old_crtc_state->crtc;
  1581. struct drm_atomic_state *old_state = old_crtc_state->state;
  1582. struct drm_plane *plane;
  1583. unsigned plane_mask;
  1584. plane_mask = old_crtc_state->plane_mask;
  1585. plane_mask |= crtc->state->plane_mask;
  1586. crtc_funcs = crtc->helper_private;
  1587. if (crtc_funcs && crtc_funcs->atomic_begin)
  1588. crtc_funcs->atomic_begin(crtc, old_crtc_state);
  1589. drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
  1590. struct drm_plane_state *old_plane_state =
  1591. drm_atomic_get_old_plane_state(old_state, plane);
  1592. const struct drm_plane_helper_funcs *plane_funcs;
  1593. plane_funcs = plane->helper_private;
  1594. if (!old_plane_state || !plane_funcs)
  1595. continue;
  1596. WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
  1597. if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
  1598. plane_funcs->atomic_disable)
  1599. plane_funcs->atomic_disable(plane, old_plane_state);
  1600. else if (plane->state->crtc ||
  1601. drm_atomic_plane_disabling(old_plane_state, plane->state))
  1602. plane_funcs->atomic_update(plane, old_plane_state);
  1603. }
  1604. if (crtc_funcs && crtc_funcs->atomic_flush)
  1605. crtc_funcs->atomic_flush(crtc, old_crtc_state);
  1606. }
  1607. EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
  1608. /**
  1609. * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
  1610. * @old_crtc_state: atomic state object with the old CRTC state
  1611. * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
  1612. *
  1613. * Disables all planes associated with the given CRTC. This can be
  1614. * used for instance in the CRTC helper atomic_disable callback to disable
  1615. * all planes.
  1616. *
  1617. * If the atomic-parameter is set the function calls the CRTC's
  1618. * atomic_begin hook before and atomic_flush hook after disabling the
  1619. * planes.
  1620. *
  1621. * It is a bug to call this function without having implemented the
  1622. * &drm_plane_helper_funcs.atomic_disable plane hook.
  1623. */
  1624. void
  1625. drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
  1626. bool atomic)
  1627. {
  1628. struct drm_crtc *crtc = old_crtc_state->crtc;
  1629. const struct drm_crtc_helper_funcs *crtc_funcs =
  1630. crtc->helper_private;
  1631. struct drm_plane *plane;
  1632. if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
  1633. crtc_funcs->atomic_begin(crtc, NULL);
  1634. drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
  1635. const struct drm_plane_helper_funcs *plane_funcs =
  1636. plane->helper_private;
  1637. if (!plane_funcs)
  1638. continue;
  1639. WARN_ON(!plane_funcs->atomic_disable);
  1640. if (plane_funcs->atomic_disable)
  1641. plane_funcs->atomic_disable(plane, NULL);
  1642. }
  1643. if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
  1644. crtc_funcs->atomic_flush(crtc, NULL);
  1645. }
  1646. EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
  1647. /**
  1648. * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
  1649. * @dev: DRM device
  1650. * @old_state: atomic state object with old state structures
  1651. *
  1652. * This function cleans up plane state, specifically framebuffers, from the old
  1653. * configuration. Hence the old configuration must be perserved in @old_state to
  1654. * be able to call this function.
  1655. *
  1656. * This function must also be called on the new state when the atomic update
  1657. * fails at any point after calling drm_atomic_helper_prepare_planes().
  1658. */
  1659. void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
  1660. struct drm_atomic_state *old_state)
  1661. {
  1662. struct drm_plane *plane;
  1663. struct drm_plane_state *old_plane_state, *new_plane_state;
  1664. int i;
  1665. for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
  1666. const struct drm_plane_helper_funcs *funcs;
  1667. struct drm_plane_state *plane_state;
  1668. /*
  1669. * This might be called before swapping when commit is aborted,
  1670. * in which case we have to cleanup the new state.
  1671. */
  1672. if (old_plane_state == plane->state)
  1673. plane_state = new_plane_state;
  1674. else
  1675. plane_state = old_plane_state;
  1676. funcs = plane->helper_private;
  1677. if (funcs->cleanup_fb)
  1678. funcs->cleanup_fb(plane, plane_state);
  1679. }
  1680. }
  1681. EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
  1682. /**
  1683. * drm_atomic_helper_swap_state - store atomic state into current sw state
  1684. * @state: atomic state
  1685. * @stall: stall for proceeding commits
  1686. *
  1687. * This function stores the atomic state into the current state pointers in all
  1688. * driver objects. It should be called after all failing steps have been done
  1689. * and succeeded, but before the actual hardware state is committed.
  1690. *
  1691. * For cleanup and error recovery the current state for all changed objects will
  1692. * be swaped into @state.
  1693. *
  1694. * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
  1695. *
  1696. * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
  1697. *
  1698. * 2. Do any other steps that might fail.
  1699. *
  1700. * 3. Put the staged state into the current state pointers with this function.
  1701. *
  1702. * 4. Actually commit the hardware state.
  1703. *
  1704. * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
  1705. * contains the old state. Also do any other cleanup required with that state.
  1706. *
  1707. * @stall must be set when nonblocking commits for this driver directly access
  1708. * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
  1709. * the current atomic helpers this is almost always the case, since the helpers
  1710. * don't pass the right state structures to the callbacks.
  1711. */
  1712. void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
  1713. bool stall)
  1714. {
  1715. int i;
  1716. long ret;
  1717. struct drm_connector *connector;
  1718. struct drm_connector_state *old_conn_state, *new_conn_state;
  1719. struct drm_crtc *crtc;
  1720. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  1721. struct drm_plane *plane;
  1722. struct drm_plane_state *old_plane_state, *new_plane_state;
  1723. struct drm_crtc_commit *commit;
  1724. void *obj, *obj_state;
  1725. const struct drm_private_state_funcs *funcs;
  1726. if (stall) {
  1727. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  1728. spin_lock(&crtc->commit_lock);
  1729. commit = list_first_entry_or_null(&crtc->commit_list,
  1730. struct drm_crtc_commit, commit_entry);
  1731. if (commit)
  1732. drm_crtc_commit_get(commit);
  1733. spin_unlock(&crtc->commit_lock);
  1734. if (!commit)
  1735. continue;
  1736. ret = wait_for_completion_timeout(&commit->hw_done,
  1737. 10*HZ);
  1738. if (ret == 0)
  1739. DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
  1740. crtc->base.id, crtc->name);
  1741. drm_crtc_commit_put(commit);
  1742. }
  1743. }
  1744. for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
  1745. WARN_ON(connector->state != old_conn_state);
  1746. old_conn_state->state = state;
  1747. new_conn_state->state = NULL;
  1748. state->connectors[i].state = old_conn_state;
  1749. connector->state = new_conn_state;
  1750. }
  1751. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  1752. WARN_ON(crtc->state != old_crtc_state);
  1753. old_crtc_state->state = state;
  1754. new_crtc_state->state = NULL;
  1755. state->crtcs[i].state = old_crtc_state;
  1756. crtc->state = new_crtc_state;
  1757. if (state->crtcs[i].commit) {
  1758. spin_lock(&crtc->commit_lock);
  1759. list_add(&state->crtcs[i].commit->commit_entry,
  1760. &crtc->commit_list);
  1761. spin_unlock(&crtc->commit_lock);
  1762. state->crtcs[i].commit->event = NULL;
  1763. }
  1764. }
  1765. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  1766. WARN_ON(plane->state != old_plane_state);
  1767. old_plane_state->state = state;
  1768. new_plane_state->state = NULL;
  1769. state->planes[i].state = old_plane_state;
  1770. plane->state = new_plane_state;
  1771. }
  1772. __for_each_private_obj(state, obj, obj_state, i, funcs)
  1773. funcs->swap_state(obj, &state->private_objs[i].obj_state);
  1774. }
  1775. EXPORT_SYMBOL(drm_atomic_helper_swap_state);
  1776. /**
  1777. * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
  1778. * @plane: plane object to update
  1779. * @crtc: owning CRTC of owning plane
  1780. * @fb: framebuffer to flip onto plane
  1781. * @crtc_x: x offset of primary plane on crtc
  1782. * @crtc_y: y offset of primary plane on crtc
  1783. * @crtc_w: width of primary plane rectangle on crtc
  1784. * @crtc_h: height of primary plane rectangle on crtc
  1785. * @src_x: x offset of @fb for panning
  1786. * @src_y: y offset of @fb for panning
  1787. * @src_w: width of source rectangle in @fb
  1788. * @src_h: height of source rectangle in @fb
  1789. * @ctx: lock acquire context
  1790. *
  1791. * Provides a default plane update handler using the atomic driver interface.
  1792. *
  1793. * RETURNS:
  1794. * Zero on success, error code on failure
  1795. */
  1796. int drm_atomic_helper_update_plane(struct drm_plane *plane,
  1797. struct drm_crtc *crtc,
  1798. struct drm_framebuffer *fb,
  1799. int crtc_x, int crtc_y,
  1800. unsigned int crtc_w, unsigned int crtc_h,
  1801. uint32_t src_x, uint32_t src_y,
  1802. uint32_t src_w, uint32_t src_h,
  1803. struct drm_modeset_acquire_ctx *ctx)
  1804. {
  1805. struct drm_atomic_state *state;
  1806. struct drm_plane_state *plane_state;
  1807. int ret = 0;
  1808. state = drm_atomic_state_alloc(plane->dev);
  1809. if (!state)
  1810. return -ENOMEM;
  1811. state->acquire_ctx = ctx;
  1812. plane_state = drm_atomic_get_plane_state(state, plane);
  1813. if (IS_ERR(plane_state)) {
  1814. ret = PTR_ERR(plane_state);
  1815. goto fail;
  1816. }
  1817. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  1818. if (ret != 0)
  1819. goto fail;
  1820. drm_atomic_set_fb_for_plane(plane_state, fb);
  1821. plane_state->crtc_x = crtc_x;
  1822. plane_state->crtc_y = crtc_y;
  1823. plane_state->crtc_w = crtc_w;
  1824. plane_state->crtc_h = crtc_h;
  1825. plane_state->src_x = src_x;
  1826. plane_state->src_y = src_y;
  1827. plane_state->src_w = src_w;
  1828. plane_state->src_h = src_h;
  1829. if (plane == crtc->cursor)
  1830. state->legacy_cursor_update = true;
  1831. ret = drm_atomic_commit(state);
  1832. fail:
  1833. drm_atomic_state_put(state);
  1834. return ret;
  1835. }
  1836. EXPORT_SYMBOL(drm_atomic_helper_update_plane);
  1837. /**
  1838. * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
  1839. * @plane: plane to disable
  1840. * @ctx: lock acquire context
  1841. *
  1842. * Provides a default plane disable handler using the atomic driver interface.
  1843. *
  1844. * RETURNS:
  1845. * Zero on success, error code on failure
  1846. */
  1847. int drm_atomic_helper_disable_plane(struct drm_plane *plane,
  1848. struct drm_modeset_acquire_ctx *ctx)
  1849. {
  1850. struct drm_atomic_state *state;
  1851. struct drm_plane_state *plane_state;
  1852. int ret = 0;
  1853. state = drm_atomic_state_alloc(plane->dev);
  1854. if (!state)
  1855. return -ENOMEM;
  1856. state->acquire_ctx = ctx;
  1857. plane_state = drm_atomic_get_plane_state(state, plane);
  1858. if (IS_ERR(plane_state)) {
  1859. ret = PTR_ERR(plane_state);
  1860. goto fail;
  1861. }
  1862. if (plane_state->crtc && (plane == plane->crtc->cursor))
  1863. plane_state->state->legacy_cursor_update = true;
  1864. ret = __drm_atomic_helper_disable_plane(plane, plane_state);
  1865. if (ret != 0)
  1866. goto fail;
  1867. ret = drm_atomic_commit(state);
  1868. fail:
  1869. drm_atomic_state_put(state);
  1870. return ret;
  1871. }
  1872. EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
  1873. /* just used from fb-helper and atomic-helper: */
  1874. int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
  1875. struct drm_plane_state *plane_state)
  1876. {
  1877. int ret;
  1878. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  1879. if (ret != 0)
  1880. return ret;
  1881. drm_atomic_set_fb_for_plane(plane_state, NULL);
  1882. plane_state->crtc_x = 0;
  1883. plane_state->crtc_y = 0;
  1884. plane_state->crtc_w = 0;
  1885. plane_state->crtc_h = 0;
  1886. plane_state->src_x = 0;
  1887. plane_state->src_y = 0;
  1888. plane_state->src_w = 0;
  1889. plane_state->src_h = 0;
  1890. return 0;
  1891. }
  1892. static int update_output_state(struct drm_atomic_state *state,
  1893. struct drm_mode_set *set)
  1894. {
  1895. struct drm_device *dev = set->crtc->dev;
  1896. struct drm_crtc *crtc;
  1897. struct drm_crtc_state *new_crtc_state;
  1898. struct drm_connector *connector;
  1899. struct drm_connector_state *new_conn_state;
  1900. int ret, i;
  1901. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  1902. state->acquire_ctx);
  1903. if (ret)
  1904. return ret;
  1905. /* First disable all connectors on the target crtc. */
  1906. ret = drm_atomic_add_affected_connectors(state, set->crtc);
  1907. if (ret)
  1908. return ret;
  1909. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  1910. if (new_conn_state->crtc == set->crtc) {
  1911. ret = drm_atomic_set_crtc_for_connector(new_conn_state,
  1912. NULL);
  1913. if (ret)
  1914. return ret;
  1915. /* Make sure legacy setCrtc always re-trains */
  1916. new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
  1917. }
  1918. }
  1919. /* Then set all connectors from set->connectors on the target crtc */
  1920. for (i = 0; i < set->num_connectors; i++) {
  1921. new_conn_state = drm_atomic_get_connector_state(state,
  1922. set->connectors[i]);
  1923. if (IS_ERR(new_conn_state))
  1924. return PTR_ERR(new_conn_state);
  1925. ret = drm_atomic_set_crtc_for_connector(new_conn_state,
  1926. set->crtc);
  1927. if (ret)
  1928. return ret;
  1929. }
  1930. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  1931. /* Don't update ->enable for the CRTC in the set_config request,
  1932. * since a mismatch would indicate a bug in the upper layers.
  1933. * The actual modeset code later on will catch any
  1934. * inconsistencies here. */
  1935. if (crtc == set->crtc)
  1936. continue;
  1937. if (!new_crtc_state->connector_mask) {
  1938. ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
  1939. NULL);
  1940. if (ret < 0)
  1941. return ret;
  1942. new_crtc_state->active = false;
  1943. }
  1944. }
  1945. return 0;
  1946. }
  1947. /**
  1948. * drm_atomic_helper_set_config - set a new config from userspace
  1949. * @set: mode set configuration
  1950. * @ctx: lock acquisition context
  1951. *
  1952. * Provides a default crtc set_config handler using the atomic driver interface.
  1953. *
  1954. * NOTE: For backwards compatibility with old userspace this automatically
  1955. * resets the "link-status" property to GOOD, to force any link
  1956. * re-training. The SETCRTC ioctl does not define whether an update does
  1957. * need a full modeset or just a plane update, hence we're allowed to do
  1958. * that. See also drm_mode_connector_set_link_status_property().
  1959. *
  1960. * Returns:
  1961. * Returns 0 on success, negative errno numbers on failure.
  1962. */
  1963. int drm_atomic_helper_set_config(struct drm_mode_set *set,
  1964. struct drm_modeset_acquire_ctx *ctx)
  1965. {
  1966. struct drm_atomic_state *state;
  1967. struct drm_crtc *crtc = set->crtc;
  1968. int ret = 0;
  1969. state = drm_atomic_state_alloc(crtc->dev);
  1970. if (!state)
  1971. return -ENOMEM;
  1972. state->acquire_ctx = ctx;
  1973. ret = __drm_atomic_helper_set_config(set, state);
  1974. if (ret != 0)
  1975. goto fail;
  1976. ret = handle_conflicting_encoders(state, true);
  1977. if (ret)
  1978. return ret;
  1979. ret = drm_atomic_commit(state);
  1980. fail:
  1981. drm_atomic_state_put(state);
  1982. return ret;
  1983. }
  1984. EXPORT_SYMBOL(drm_atomic_helper_set_config);
  1985. /* just used from fb-helper and atomic-helper: */
  1986. int __drm_atomic_helper_set_config(struct drm_mode_set *set,
  1987. struct drm_atomic_state *state)
  1988. {
  1989. struct drm_crtc_state *crtc_state;
  1990. struct drm_plane_state *primary_state;
  1991. struct drm_crtc *crtc = set->crtc;
  1992. int hdisplay, vdisplay;
  1993. int ret;
  1994. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1995. if (IS_ERR(crtc_state))
  1996. return PTR_ERR(crtc_state);
  1997. primary_state = drm_atomic_get_plane_state(state, crtc->primary);
  1998. if (IS_ERR(primary_state))
  1999. return PTR_ERR(primary_state);
  2000. if (!set->mode) {
  2001. WARN_ON(set->fb);
  2002. WARN_ON(set->num_connectors);
  2003. ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
  2004. if (ret != 0)
  2005. return ret;
  2006. crtc_state->active = false;
  2007. ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
  2008. if (ret != 0)
  2009. return ret;
  2010. drm_atomic_set_fb_for_plane(primary_state, NULL);
  2011. goto commit;
  2012. }
  2013. WARN_ON(!set->fb);
  2014. WARN_ON(!set->num_connectors);
  2015. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  2016. if (ret != 0)
  2017. return ret;
  2018. crtc_state->active = true;
  2019. ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
  2020. if (ret != 0)
  2021. return ret;
  2022. drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  2023. drm_atomic_set_fb_for_plane(primary_state, set->fb);
  2024. primary_state->crtc_x = 0;
  2025. primary_state->crtc_y = 0;
  2026. primary_state->crtc_w = hdisplay;
  2027. primary_state->crtc_h = vdisplay;
  2028. primary_state->src_x = set->x << 16;
  2029. primary_state->src_y = set->y << 16;
  2030. if (drm_rotation_90_or_270(primary_state->rotation)) {
  2031. primary_state->src_w = vdisplay << 16;
  2032. primary_state->src_h = hdisplay << 16;
  2033. } else {
  2034. primary_state->src_w = hdisplay << 16;
  2035. primary_state->src_h = vdisplay << 16;
  2036. }
  2037. commit:
  2038. ret = update_output_state(state, set);
  2039. if (ret)
  2040. return ret;
  2041. return 0;
  2042. }
  2043. /**
  2044. * drm_atomic_helper_disable_all - disable all currently active outputs
  2045. * @dev: DRM device
  2046. * @ctx: lock acquisition context
  2047. *
  2048. * Loops through all connectors, finding those that aren't turned off and then
  2049. * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
  2050. * that they are connected to.
  2051. *
  2052. * This is used for example in suspend/resume to disable all currently active
  2053. * functions when suspending. If you just want to shut down everything at e.g.
  2054. * driver unload, look at drm_atomic_helper_shutdown().
  2055. *
  2056. * Note that if callers haven't already acquired all modeset locks this might
  2057. * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
  2058. *
  2059. * Returns:
  2060. * 0 on success or a negative error code on failure.
  2061. *
  2062. * See also:
  2063. * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
  2064. * drm_atomic_helper_shutdown().
  2065. */
  2066. int drm_atomic_helper_disable_all(struct drm_device *dev,
  2067. struct drm_modeset_acquire_ctx *ctx)
  2068. {
  2069. struct drm_atomic_state *state;
  2070. struct drm_connector_state *conn_state;
  2071. struct drm_connector *conn;
  2072. struct drm_plane_state *plane_state;
  2073. struct drm_plane *plane;
  2074. struct drm_crtc_state *crtc_state;
  2075. struct drm_crtc *crtc;
  2076. int ret, i;
  2077. state = drm_atomic_state_alloc(dev);
  2078. if (!state)
  2079. return -ENOMEM;
  2080. state->acquire_ctx = ctx;
  2081. drm_for_each_crtc(crtc, dev) {
  2082. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2083. if (IS_ERR(crtc_state)) {
  2084. ret = PTR_ERR(crtc_state);
  2085. goto free;
  2086. }
  2087. crtc_state->active = false;
  2088. ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
  2089. if (ret < 0)
  2090. goto free;
  2091. ret = drm_atomic_add_affected_planes(state, crtc);
  2092. if (ret < 0)
  2093. goto free;
  2094. ret = drm_atomic_add_affected_connectors(state, crtc);
  2095. if (ret < 0)
  2096. goto free;
  2097. }
  2098. for_each_connector_in_state(state, conn, conn_state, i) {
  2099. ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
  2100. if (ret < 0)
  2101. goto free;
  2102. }
  2103. for_each_plane_in_state(state, plane, plane_state, i) {
  2104. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  2105. if (ret < 0)
  2106. goto free;
  2107. drm_atomic_set_fb_for_plane(plane_state, NULL);
  2108. }
  2109. ret = drm_atomic_commit(state);
  2110. free:
  2111. drm_atomic_state_put(state);
  2112. return ret;
  2113. }
  2114. EXPORT_SYMBOL(drm_atomic_helper_disable_all);
  2115. /**
  2116. * drm_atomic_helper_shutdown - shutdown all CRTC
  2117. * @dev: DRM device
  2118. *
  2119. * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
  2120. * suspend should instead be handled with drm_atomic_helper_suspend(), since
  2121. * that also takes a snapshot of the modeset state to be restored on resume.
  2122. *
  2123. * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
  2124. * and it is the atomic version of drm_crtc_force_disable_all().
  2125. */
  2126. void drm_atomic_helper_shutdown(struct drm_device *dev)
  2127. {
  2128. struct drm_modeset_acquire_ctx ctx;
  2129. int ret;
  2130. drm_modeset_acquire_init(&ctx, 0);
  2131. while (1) {
  2132. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  2133. if (!ret)
  2134. ret = drm_atomic_helper_disable_all(dev, &ctx);
  2135. if (ret != -EDEADLK)
  2136. break;
  2137. drm_modeset_backoff(&ctx);
  2138. }
  2139. if (ret)
  2140. DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
  2141. drm_modeset_drop_locks(&ctx);
  2142. drm_modeset_acquire_fini(&ctx);
  2143. }
  2144. EXPORT_SYMBOL(drm_atomic_helper_shutdown);
  2145. /**
  2146. * drm_atomic_helper_suspend - subsystem-level suspend helper
  2147. * @dev: DRM device
  2148. *
  2149. * Duplicates the current atomic state, disables all active outputs and then
  2150. * returns a pointer to the original atomic state to the caller. Drivers can
  2151. * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
  2152. * restore the output configuration that was active at the time the system
  2153. * entered suspend.
  2154. *
  2155. * Note that it is potentially unsafe to use this. The atomic state object
  2156. * returned by this function is assumed to be persistent. Drivers must ensure
  2157. * that this holds true. Before calling this function, drivers must make sure
  2158. * to suspend fbdev emulation so that nothing can be using the device.
  2159. *
  2160. * Returns:
  2161. * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
  2162. * encoded error code on failure. Drivers should store the returned atomic
  2163. * state object and pass it to the drm_atomic_helper_resume() helper upon
  2164. * resume.
  2165. *
  2166. * See also:
  2167. * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
  2168. * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
  2169. */
  2170. struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
  2171. {
  2172. struct drm_modeset_acquire_ctx ctx;
  2173. struct drm_atomic_state *state;
  2174. int err;
  2175. drm_modeset_acquire_init(&ctx, 0);
  2176. retry:
  2177. err = drm_modeset_lock_all_ctx(dev, &ctx);
  2178. if (err < 0) {
  2179. state = ERR_PTR(err);
  2180. goto unlock;
  2181. }
  2182. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  2183. if (IS_ERR(state))
  2184. goto unlock;
  2185. err = drm_atomic_helper_disable_all(dev, &ctx);
  2186. if (err < 0) {
  2187. drm_atomic_state_put(state);
  2188. state = ERR_PTR(err);
  2189. goto unlock;
  2190. }
  2191. unlock:
  2192. if (PTR_ERR(state) == -EDEADLK) {
  2193. drm_modeset_backoff(&ctx);
  2194. goto retry;
  2195. }
  2196. drm_modeset_drop_locks(&ctx);
  2197. drm_modeset_acquire_fini(&ctx);
  2198. return state;
  2199. }
  2200. EXPORT_SYMBOL(drm_atomic_helper_suspend);
  2201. /**
  2202. * drm_atomic_helper_commit_duplicated_state - commit duplicated state
  2203. * @state: duplicated atomic state to commit
  2204. * @ctx: pointer to acquire_ctx to use for commit.
  2205. *
  2206. * The state returned by drm_atomic_helper_duplicate_state() and
  2207. * drm_atomic_helper_suspend() is partially invalid, and needs to
  2208. * be fixed up before commit.
  2209. *
  2210. * Returns:
  2211. * 0 on success or a negative error code on failure.
  2212. *
  2213. * See also:
  2214. * drm_atomic_helper_suspend()
  2215. */
  2216. int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
  2217. struct drm_modeset_acquire_ctx *ctx)
  2218. {
  2219. int i;
  2220. struct drm_plane *plane;
  2221. struct drm_plane_state *new_plane_state;
  2222. struct drm_connector *connector;
  2223. struct drm_connector_state *new_conn_state;
  2224. struct drm_crtc *crtc;
  2225. struct drm_crtc_state *new_crtc_state;
  2226. state->acquire_ctx = ctx;
  2227. for_each_new_plane_in_state(state, plane, new_plane_state, i)
  2228. state->planes[i].old_state = plane->state;
  2229. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  2230. state->crtcs[i].old_state = crtc->state;
  2231. for_each_new_connector_in_state(state, connector, new_conn_state, i)
  2232. state->connectors[i].old_state = connector->state;
  2233. return drm_atomic_commit(state);
  2234. }
  2235. EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
  2236. /**
  2237. * drm_atomic_helper_resume - subsystem-level resume helper
  2238. * @dev: DRM device
  2239. * @state: atomic state to resume to
  2240. *
  2241. * Calls drm_mode_config_reset() to synchronize hardware and software states,
  2242. * grabs all modeset locks and commits the atomic state object. This can be
  2243. * used in conjunction with the drm_atomic_helper_suspend() helper to
  2244. * implement suspend/resume for drivers that support atomic mode-setting.
  2245. *
  2246. * Returns:
  2247. * 0 on success or a negative error code on failure.
  2248. *
  2249. * See also:
  2250. * drm_atomic_helper_suspend()
  2251. */
  2252. int drm_atomic_helper_resume(struct drm_device *dev,
  2253. struct drm_atomic_state *state)
  2254. {
  2255. struct drm_modeset_acquire_ctx ctx;
  2256. int err;
  2257. drm_mode_config_reset(dev);
  2258. drm_modeset_acquire_init(&ctx, 0);
  2259. while (1) {
  2260. err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
  2261. if (err != -EDEADLK)
  2262. break;
  2263. drm_modeset_backoff(&ctx);
  2264. }
  2265. drm_modeset_drop_locks(&ctx);
  2266. drm_modeset_acquire_fini(&ctx);
  2267. return err;
  2268. }
  2269. EXPORT_SYMBOL(drm_atomic_helper_resume);
  2270. /**
  2271. * drm_atomic_helper_crtc_set_property - helper for crtc properties
  2272. * @crtc: DRM crtc
  2273. * @property: DRM property
  2274. * @val: value of property
  2275. *
  2276. * Provides a default crtc set_property handler using the atomic driver
  2277. * interface.
  2278. *
  2279. * RETURNS:
  2280. * Zero on success, error code on failure
  2281. */
  2282. int
  2283. drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
  2284. struct drm_property *property,
  2285. uint64_t val)
  2286. {
  2287. struct drm_atomic_state *state;
  2288. struct drm_crtc_state *crtc_state;
  2289. int ret = 0;
  2290. state = drm_atomic_state_alloc(crtc->dev);
  2291. if (!state)
  2292. return -ENOMEM;
  2293. /* ->set_property is always called with all locks held. */
  2294. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  2295. retry:
  2296. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2297. if (IS_ERR(crtc_state)) {
  2298. ret = PTR_ERR(crtc_state);
  2299. goto fail;
  2300. }
  2301. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  2302. property, val);
  2303. if (ret)
  2304. goto fail;
  2305. ret = drm_atomic_commit(state);
  2306. fail:
  2307. if (ret == -EDEADLK)
  2308. goto backoff;
  2309. drm_atomic_state_put(state);
  2310. return ret;
  2311. backoff:
  2312. drm_atomic_state_clear(state);
  2313. drm_atomic_legacy_backoff(state);
  2314. goto retry;
  2315. }
  2316. EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
  2317. /**
  2318. * drm_atomic_helper_plane_set_property - helper for plane properties
  2319. * @plane: DRM plane
  2320. * @property: DRM property
  2321. * @val: value of property
  2322. *
  2323. * Provides a default plane set_property handler using the atomic driver
  2324. * interface.
  2325. *
  2326. * RETURNS:
  2327. * Zero on success, error code on failure
  2328. */
  2329. int
  2330. drm_atomic_helper_plane_set_property(struct drm_plane *plane,
  2331. struct drm_property *property,
  2332. uint64_t val)
  2333. {
  2334. struct drm_atomic_state *state;
  2335. struct drm_plane_state *plane_state;
  2336. int ret = 0;
  2337. state = drm_atomic_state_alloc(plane->dev);
  2338. if (!state)
  2339. return -ENOMEM;
  2340. /* ->set_property is always called with all locks held. */
  2341. state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
  2342. retry:
  2343. plane_state = drm_atomic_get_plane_state(state, plane);
  2344. if (IS_ERR(plane_state)) {
  2345. ret = PTR_ERR(plane_state);
  2346. goto fail;
  2347. }
  2348. ret = drm_atomic_plane_set_property(plane, plane_state,
  2349. property, val);
  2350. if (ret)
  2351. goto fail;
  2352. ret = drm_atomic_commit(state);
  2353. fail:
  2354. if (ret == -EDEADLK)
  2355. goto backoff;
  2356. drm_atomic_state_put(state);
  2357. return ret;
  2358. backoff:
  2359. drm_atomic_state_clear(state);
  2360. drm_atomic_legacy_backoff(state);
  2361. goto retry;
  2362. }
  2363. EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
  2364. /**
  2365. * drm_atomic_helper_connector_set_property - helper for connector properties
  2366. * @connector: DRM connector
  2367. * @property: DRM property
  2368. * @val: value of property
  2369. *
  2370. * Provides a default connector set_property handler using the atomic driver
  2371. * interface.
  2372. *
  2373. * RETURNS:
  2374. * Zero on success, error code on failure
  2375. */
  2376. int
  2377. drm_atomic_helper_connector_set_property(struct drm_connector *connector,
  2378. struct drm_property *property,
  2379. uint64_t val)
  2380. {
  2381. struct drm_atomic_state *state;
  2382. struct drm_connector_state *connector_state;
  2383. int ret = 0;
  2384. state = drm_atomic_state_alloc(connector->dev);
  2385. if (!state)
  2386. return -ENOMEM;
  2387. /* ->set_property is always called with all locks held. */
  2388. state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
  2389. retry:
  2390. connector_state = drm_atomic_get_connector_state(state, connector);
  2391. if (IS_ERR(connector_state)) {
  2392. ret = PTR_ERR(connector_state);
  2393. goto fail;
  2394. }
  2395. ret = drm_atomic_connector_set_property(connector, connector_state,
  2396. property, val);
  2397. if (ret)
  2398. goto fail;
  2399. ret = drm_atomic_commit(state);
  2400. fail:
  2401. if (ret == -EDEADLK)
  2402. goto backoff;
  2403. drm_atomic_state_put(state);
  2404. return ret;
  2405. backoff:
  2406. drm_atomic_state_clear(state);
  2407. drm_atomic_legacy_backoff(state);
  2408. goto retry;
  2409. }
  2410. EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
  2411. static int page_flip_common(
  2412. struct drm_atomic_state *state,
  2413. struct drm_crtc *crtc,
  2414. struct drm_framebuffer *fb,
  2415. struct drm_pending_vblank_event *event,
  2416. uint32_t flags)
  2417. {
  2418. struct drm_plane *plane = crtc->primary;
  2419. struct drm_plane_state *plane_state;
  2420. struct drm_crtc_state *crtc_state;
  2421. int ret = 0;
  2422. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2423. if (IS_ERR(crtc_state))
  2424. return PTR_ERR(crtc_state);
  2425. crtc_state->event = event;
  2426. crtc_state->pageflip_flags = flags;
  2427. plane_state = drm_atomic_get_plane_state(state, plane);
  2428. if (IS_ERR(plane_state))
  2429. return PTR_ERR(plane_state);
  2430. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  2431. if (ret != 0)
  2432. return ret;
  2433. drm_atomic_set_fb_for_plane(plane_state, fb);
  2434. /* Make sure we don't accidentally do a full modeset. */
  2435. state->allow_modeset = false;
  2436. if (!crtc_state->active) {
  2437. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
  2438. crtc->base.id, crtc->name);
  2439. return -EINVAL;
  2440. }
  2441. return ret;
  2442. }
  2443. /**
  2444. * drm_atomic_helper_page_flip - execute a legacy page flip
  2445. * @crtc: DRM crtc
  2446. * @fb: DRM framebuffer
  2447. * @event: optional DRM event to signal upon completion
  2448. * @flags: flip flags for non-vblank sync'ed updates
  2449. * @ctx: lock acquisition context
  2450. *
  2451. * Provides a default &drm_crtc_funcs.page_flip implementation
  2452. * using the atomic driver interface.
  2453. *
  2454. * Returns:
  2455. * Returns 0 on success, negative errno numbers on failure.
  2456. *
  2457. * See also:
  2458. * drm_atomic_helper_page_flip_target()
  2459. */
  2460. int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
  2461. struct drm_framebuffer *fb,
  2462. struct drm_pending_vblank_event *event,
  2463. uint32_t flags,
  2464. struct drm_modeset_acquire_ctx *ctx)
  2465. {
  2466. struct drm_plane *plane = crtc->primary;
  2467. struct drm_atomic_state *state;
  2468. int ret = 0;
  2469. state = drm_atomic_state_alloc(plane->dev);
  2470. if (!state)
  2471. return -ENOMEM;
  2472. state->acquire_ctx = ctx;
  2473. ret = page_flip_common(state, crtc, fb, event, flags);
  2474. if (ret != 0)
  2475. goto fail;
  2476. ret = drm_atomic_nonblocking_commit(state);
  2477. fail:
  2478. drm_atomic_state_put(state);
  2479. return ret;
  2480. }
  2481. EXPORT_SYMBOL(drm_atomic_helper_page_flip);
  2482. /**
  2483. * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
  2484. * @crtc: DRM crtc
  2485. * @fb: DRM framebuffer
  2486. * @event: optional DRM event to signal upon completion
  2487. * @flags: flip flags for non-vblank sync'ed updates
  2488. * @target: specifying the target vblank period when the flip to take effect
  2489. * @ctx: lock acquisition context
  2490. *
  2491. * Provides a default &drm_crtc_funcs.page_flip_target implementation.
  2492. * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
  2493. * target vblank period to flip.
  2494. *
  2495. * Returns:
  2496. * Returns 0 on success, negative errno numbers on failure.
  2497. */
  2498. int drm_atomic_helper_page_flip_target(
  2499. struct drm_crtc *crtc,
  2500. struct drm_framebuffer *fb,
  2501. struct drm_pending_vblank_event *event,
  2502. uint32_t flags,
  2503. uint32_t target,
  2504. struct drm_modeset_acquire_ctx *ctx)
  2505. {
  2506. struct drm_plane *plane = crtc->primary;
  2507. struct drm_atomic_state *state;
  2508. struct drm_crtc_state *crtc_state;
  2509. int ret = 0;
  2510. state = drm_atomic_state_alloc(plane->dev);
  2511. if (!state)
  2512. return -ENOMEM;
  2513. state->acquire_ctx = ctx;
  2514. ret = page_flip_common(state, crtc, fb, event, flags);
  2515. if (ret != 0)
  2516. goto fail;
  2517. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  2518. if (WARN_ON(!crtc_state)) {
  2519. ret = -EINVAL;
  2520. goto fail;
  2521. }
  2522. crtc_state->target_vblank = target;
  2523. ret = drm_atomic_nonblocking_commit(state);
  2524. fail:
  2525. drm_atomic_state_put(state);
  2526. return ret;
  2527. }
  2528. EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
  2529. /**
  2530. * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
  2531. * @connector: affected connector
  2532. * @mode: DPMS mode
  2533. *
  2534. * This is the main helper function provided by the atomic helper framework for
  2535. * implementing the legacy DPMS connector interface. It computes the new desired
  2536. * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
  2537. * enabled) and updates it.
  2538. *
  2539. * Returns:
  2540. * Returns 0 on success, negative errno numbers on failure.
  2541. */
  2542. int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
  2543. int mode)
  2544. {
  2545. struct drm_mode_config *config = &connector->dev->mode_config;
  2546. struct drm_atomic_state *state;
  2547. struct drm_crtc_state *crtc_state;
  2548. struct drm_crtc *crtc;
  2549. struct drm_connector *tmp_connector;
  2550. struct drm_connector_list_iter conn_iter;
  2551. int ret;
  2552. bool active = false;
  2553. int old_mode = connector->dpms;
  2554. if (mode != DRM_MODE_DPMS_ON)
  2555. mode = DRM_MODE_DPMS_OFF;
  2556. connector->dpms = mode;
  2557. crtc = connector->state->crtc;
  2558. if (!crtc)
  2559. return 0;
  2560. state = drm_atomic_state_alloc(connector->dev);
  2561. if (!state)
  2562. return -ENOMEM;
  2563. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  2564. retry:
  2565. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2566. if (IS_ERR(crtc_state)) {
  2567. ret = PTR_ERR(crtc_state);
  2568. goto fail;
  2569. }
  2570. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  2571. drm_connector_list_iter_begin(connector->dev, &conn_iter);
  2572. drm_for_each_connector_iter(tmp_connector, &conn_iter) {
  2573. if (tmp_connector->state->crtc != crtc)
  2574. continue;
  2575. if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
  2576. active = true;
  2577. break;
  2578. }
  2579. }
  2580. drm_connector_list_iter_end(&conn_iter);
  2581. crtc_state->active = active;
  2582. ret = drm_atomic_commit(state);
  2583. fail:
  2584. if (ret == -EDEADLK)
  2585. goto backoff;
  2586. if (ret != 0)
  2587. connector->dpms = old_mode;
  2588. drm_atomic_state_put(state);
  2589. return ret;
  2590. backoff:
  2591. drm_atomic_state_clear(state);
  2592. drm_atomic_legacy_backoff(state);
  2593. goto retry;
  2594. }
  2595. EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
  2596. /**
  2597. * drm_atomic_helper_best_encoder - Helper for
  2598. * &drm_connector_helper_funcs.best_encoder callback
  2599. * @connector: Connector control structure
  2600. *
  2601. * This is a &drm_connector_helper_funcs.best_encoder callback helper for
  2602. * connectors that support exactly 1 encoder, statically determined at driver
  2603. * init time.
  2604. */
  2605. struct drm_encoder *
  2606. drm_atomic_helper_best_encoder(struct drm_connector *connector)
  2607. {
  2608. WARN_ON(connector->encoder_ids[1]);
  2609. return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
  2610. }
  2611. EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
  2612. /**
  2613. * DOC: atomic state reset and initialization
  2614. *
  2615. * Both the drm core and the atomic helpers assume that there is always the full
  2616. * and correct atomic software state for all connectors, CRTCs and planes
  2617. * available. Which is a bit a problem on driver load and also after system
  2618. * suspend. One way to solve this is to have a hardware state read-out
  2619. * infrastructure which reconstructs the full software state (e.g. the i915
  2620. * driver).
  2621. *
  2622. * The simpler solution is to just reset the software state to everything off,
  2623. * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
  2624. * the atomic helpers provide default reset implementations for all hooks.
  2625. *
  2626. * On the upside the precise state tracking of atomic simplifies system suspend
  2627. * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
  2628. * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
  2629. * For other drivers the building blocks are split out, see the documentation
  2630. * for these functions.
  2631. */
  2632. /**
  2633. * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
  2634. * @crtc: drm CRTC
  2635. *
  2636. * Resets the atomic state for @crtc by freeing the state pointer (which might
  2637. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  2638. */
  2639. void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
  2640. {
  2641. if (crtc->state)
  2642. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  2643. kfree(crtc->state);
  2644. crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
  2645. if (crtc->state)
  2646. crtc->state->crtc = crtc;
  2647. }
  2648. EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
  2649. /**
  2650. * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
  2651. * @crtc: CRTC object
  2652. * @state: atomic CRTC state
  2653. *
  2654. * Copies atomic state from a CRTC's current state and resets inferred values.
  2655. * This is useful for drivers that subclass the CRTC state.
  2656. */
  2657. void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
  2658. struct drm_crtc_state *state)
  2659. {
  2660. memcpy(state, crtc->state, sizeof(*state));
  2661. if (state->mode_blob)
  2662. drm_property_blob_get(state->mode_blob);
  2663. if (state->degamma_lut)
  2664. drm_property_blob_get(state->degamma_lut);
  2665. if (state->ctm)
  2666. drm_property_blob_get(state->ctm);
  2667. if (state->gamma_lut)
  2668. drm_property_blob_get(state->gamma_lut);
  2669. state->mode_changed = false;
  2670. state->active_changed = false;
  2671. state->planes_changed = false;
  2672. state->connectors_changed = false;
  2673. state->color_mgmt_changed = false;
  2674. state->zpos_changed = false;
  2675. state->event = NULL;
  2676. state->pageflip_flags = 0;
  2677. }
  2678. EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
  2679. /**
  2680. * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
  2681. * @crtc: drm CRTC
  2682. *
  2683. * Default CRTC state duplicate hook for drivers which don't have their own
  2684. * subclassed CRTC state structure.
  2685. */
  2686. struct drm_crtc_state *
  2687. drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
  2688. {
  2689. struct drm_crtc_state *state;
  2690. if (WARN_ON(!crtc->state))
  2691. return NULL;
  2692. state = kmalloc(sizeof(*state), GFP_KERNEL);
  2693. if (state)
  2694. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  2695. return state;
  2696. }
  2697. EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
  2698. /**
  2699. * __drm_atomic_helper_crtc_destroy_state - release CRTC state
  2700. * @state: CRTC state object to release
  2701. *
  2702. * Releases all resources stored in the CRTC state without actually freeing
  2703. * the memory of the CRTC state. This is useful for drivers that subclass the
  2704. * CRTC state.
  2705. */
  2706. void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
  2707. {
  2708. drm_property_blob_put(state->mode_blob);
  2709. drm_property_blob_put(state->degamma_lut);
  2710. drm_property_blob_put(state->ctm);
  2711. drm_property_blob_put(state->gamma_lut);
  2712. }
  2713. EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
  2714. /**
  2715. * drm_atomic_helper_crtc_destroy_state - default state destroy hook
  2716. * @crtc: drm CRTC
  2717. * @state: CRTC state object to release
  2718. *
  2719. * Default CRTC state destroy hook for drivers which don't have their own
  2720. * subclassed CRTC state structure.
  2721. */
  2722. void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
  2723. struct drm_crtc_state *state)
  2724. {
  2725. __drm_atomic_helper_crtc_destroy_state(state);
  2726. kfree(state);
  2727. }
  2728. EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
  2729. /**
  2730. * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
  2731. * @plane: drm plane
  2732. *
  2733. * Resets the atomic state for @plane by freeing the state pointer (which might
  2734. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  2735. */
  2736. void drm_atomic_helper_plane_reset(struct drm_plane *plane)
  2737. {
  2738. if (plane->state)
  2739. __drm_atomic_helper_plane_destroy_state(plane->state);
  2740. kfree(plane->state);
  2741. plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
  2742. if (plane->state) {
  2743. plane->state->plane = plane;
  2744. plane->state->rotation = DRM_ROTATE_0;
  2745. }
  2746. }
  2747. EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
  2748. /**
  2749. * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
  2750. * @plane: plane object
  2751. * @state: atomic plane state
  2752. *
  2753. * Copies atomic state from a plane's current state. This is useful for
  2754. * drivers that subclass the plane state.
  2755. */
  2756. void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
  2757. struct drm_plane_state *state)
  2758. {
  2759. memcpy(state, plane->state, sizeof(*state));
  2760. if (state->fb)
  2761. drm_framebuffer_get(state->fb);
  2762. state->fence = NULL;
  2763. }
  2764. EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
  2765. /**
  2766. * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
  2767. * @plane: drm plane
  2768. *
  2769. * Default plane state duplicate hook for drivers which don't have their own
  2770. * subclassed plane state structure.
  2771. */
  2772. struct drm_plane_state *
  2773. drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
  2774. {
  2775. struct drm_plane_state *state;
  2776. if (WARN_ON(!plane->state))
  2777. return NULL;
  2778. state = kmalloc(sizeof(*state), GFP_KERNEL);
  2779. if (state)
  2780. __drm_atomic_helper_plane_duplicate_state(plane, state);
  2781. return state;
  2782. }
  2783. EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
  2784. /**
  2785. * __drm_atomic_helper_plane_destroy_state - release plane state
  2786. * @state: plane state object to release
  2787. *
  2788. * Releases all resources stored in the plane state without actually freeing
  2789. * the memory of the plane state. This is useful for drivers that subclass the
  2790. * plane state.
  2791. */
  2792. void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
  2793. {
  2794. if (state->fb)
  2795. drm_framebuffer_put(state->fb);
  2796. if (state->fence)
  2797. dma_fence_put(state->fence);
  2798. }
  2799. EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
  2800. /**
  2801. * drm_atomic_helper_plane_destroy_state - default state destroy hook
  2802. * @plane: drm plane
  2803. * @state: plane state object to release
  2804. *
  2805. * Default plane state destroy hook for drivers which don't have their own
  2806. * subclassed plane state structure.
  2807. */
  2808. void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
  2809. struct drm_plane_state *state)
  2810. {
  2811. __drm_atomic_helper_plane_destroy_state(state);
  2812. kfree(state);
  2813. }
  2814. EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
  2815. /**
  2816. * __drm_atomic_helper_connector_reset - reset state on connector
  2817. * @connector: drm connector
  2818. * @conn_state: connector state to assign
  2819. *
  2820. * Initializes the newly allocated @conn_state and assigns it to
  2821. * the &drm_conector->state pointer of @connector, usually required when
  2822. * initializing the drivers or when called from the &drm_connector_funcs.reset
  2823. * hook.
  2824. *
  2825. * This is useful for drivers that subclass the connector state.
  2826. */
  2827. void
  2828. __drm_atomic_helper_connector_reset(struct drm_connector *connector,
  2829. struct drm_connector_state *conn_state)
  2830. {
  2831. if (conn_state)
  2832. conn_state->connector = connector;
  2833. connector->state = conn_state;
  2834. }
  2835. EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
  2836. /**
  2837. * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
  2838. * @connector: drm connector
  2839. *
  2840. * Resets the atomic state for @connector by freeing the state pointer (which
  2841. * might be NULL, e.g. at driver load time) and allocating a new empty state
  2842. * object.
  2843. */
  2844. void drm_atomic_helper_connector_reset(struct drm_connector *connector)
  2845. {
  2846. struct drm_connector_state *conn_state =
  2847. kzalloc(sizeof(*conn_state), GFP_KERNEL);
  2848. if (connector->state)
  2849. __drm_atomic_helper_connector_destroy_state(connector->state);
  2850. kfree(connector->state);
  2851. __drm_atomic_helper_connector_reset(connector, conn_state);
  2852. }
  2853. EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
  2854. /**
  2855. * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
  2856. * @connector: connector object
  2857. * @state: atomic connector state
  2858. *
  2859. * Copies atomic state from a connector's current state. This is useful for
  2860. * drivers that subclass the connector state.
  2861. */
  2862. void
  2863. __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
  2864. struct drm_connector_state *state)
  2865. {
  2866. memcpy(state, connector->state, sizeof(*state));
  2867. if (state->crtc)
  2868. drm_connector_get(connector);
  2869. }
  2870. EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
  2871. /**
  2872. * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
  2873. * @connector: drm connector
  2874. *
  2875. * Default connector state duplicate hook for drivers which don't have their own
  2876. * subclassed connector state structure.
  2877. */
  2878. struct drm_connector_state *
  2879. drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
  2880. {
  2881. struct drm_connector_state *state;
  2882. if (WARN_ON(!connector->state))
  2883. return NULL;
  2884. state = kmalloc(sizeof(*state), GFP_KERNEL);
  2885. if (state)
  2886. __drm_atomic_helper_connector_duplicate_state(connector, state);
  2887. return state;
  2888. }
  2889. EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
  2890. /**
  2891. * drm_atomic_helper_duplicate_state - duplicate an atomic state object
  2892. * @dev: DRM device
  2893. * @ctx: lock acquisition context
  2894. *
  2895. * Makes a copy of the current atomic state by looping over all objects and
  2896. * duplicating their respective states. This is used for example by suspend/
  2897. * resume support code to save the state prior to suspend such that it can
  2898. * be restored upon resume.
  2899. *
  2900. * Note that this treats atomic state as persistent between save and restore.
  2901. * Drivers must make sure that this is possible and won't result in confusion
  2902. * or erroneous behaviour.
  2903. *
  2904. * Note that if callers haven't already acquired all modeset locks this might
  2905. * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
  2906. *
  2907. * Returns:
  2908. * A pointer to the copy of the atomic state object on success or an
  2909. * ERR_PTR()-encoded error code on failure.
  2910. *
  2911. * See also:
  2912. * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
  2913. */
  2914. struct drm_atomic_state *
  2915. drm_atomic_helper_duplicate_state(struct drm_device *dev,
  2916. struct drm_modeset_acquire_ctx *ctx)
  2917. {
  2918. struct drm_atomic_state *state;
  2919. struct drm_connector *conn;
  2920. struct drm_connector_list_iter conn_iter;
  2921. struct drm_plane *plane;
  2922. struct drm_crtc *crtc;
  2923. int err = 0;
  2924. state = drm_atomic_state_alloc(dev);
  2925. if (!state)
  2926. return ERR_PTR(-ENOMEM);
  2927. state->acquire_ctx = ctx;
  2928. drm_for_each_crtc(crtc, dev) {
  2929. struct drm_crtc_state *crtc_state;
  2930. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2931. if (IS_ERR(crtc_state)) {
  2932. err = PTR_ERR(crtc_state);
  2933. goto free;
  2934. }
  2935. }
  2936. drm_for_each_plane(plane, dev) {
  2937. struct drm_plane_state *plane_state;
  2938. plane_state = drm_atomic_get_plane_state(state, plane);
  2939. if (IS_ERR(plane_state)) {
  2940. err = PTR_ERR(plane_state);
  2941. goto free;
  2942. }
  2943. }
  2944. drm_connector_list_iter_begin(dev, &conn_iter);
  2945. drm_for_each_connector_iter(conn, &conn_iter) {
  2946. struct drm_connector_state *conn_state;
  2947. conn_state = drm_atomic_get_connector_state(state, conn);
  2948. if (IS_ERR(conn_state)) {
  2949. err = PTR_ERR(conn_state);
  2950. drm_connector_list_iter_end(&conn_iter);
  2951. goto free;
  2952. }
  2953. }
  2954. drm_connector_list_iter_end(&conn_iter);
  2955. /* clear the acquire context so that it isn't accidentally reused */
  2956. state->acquire_ctx = NULL;
  2957. free:
  2958. if (err < 0) {
  2959. drm_atomic_state_put(state);
  2960. state = ERR_PTR(err);
  2961. }
  2962. return state;
  2963. }
  2964. EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
  2965. /**
  2966. * __drm_atomic_helper_connector_destroy_state - release connector state
  2967. * @state: connector state object to release
  2968. *
  2969. * Releases all resources stored in the connector state without actually
  2970. * freeing the memory of the connector state. This is useful for drivers that
  2971. * subclass the connector state.
  2972. */
  2973. void
  2974. __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
  2975. {
  2976. if (state->crtc)
  2977. drm_connector_put(state->connector);
  2978. }
  2979. EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
  2980. /**
  2981. * drm_atomic_helper_connector_destroy_state - default state destroy hook
  2982. * @connector: drm connector
  2983. * @state: connector state object to release
  2984. *
  2985. * Default connector state destroy hook for drivers which don't have their own
  2986. * subclassed connector state structure.
  2987. */
  2988. void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
  2989. struct drm_connector_state *state)
  2990. {
  2991. __drm_atomic_helper_connector_destroy_state(state);
  2992. kfree(state);
  2993. }
  2994. EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
  2995. /**
  2996. * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
  2997. * @crtc: CRTC object
  2998. * @red: red correction table
  2999. * @green: green correction table
  3000. * @blue: green correction table
  3001. * @size: size of the tables
  3002. * @ctx: lock acquire context
  3003. *
  3004. * Implements support for legacy gamma correction table for drivers
  3005. * that support color management through the DEGAMMA_LUT/GAMMA_LUT
  3006. * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
  3007. * how the atomic color management and gamma tables work.
  3008. */
  3009. int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
  3010. u16 *red, u16 *green, u16 *blue,
  3011. uint32_t size,
  3012. struct drm_modeset_acquire_ctx *ctx)
  3013. {
  3014. struct drm_device *dev = crtc->dev;
  3015. struct drm_mode_config *config = &dev->mode_config;
  3016. struct drm_atomic_state *state;
  3017. struct drm_crtc_state *crtc_state;
  3018. struct drm_property_blob *blob = NULL;
  3019. struct drm_color_lut *blob_data;
  3020. int i, ret = 0;
  3021. state = drm_atomic_state_alloc(crtc->dev);
  3022. if (!state)
  3023. return -ENOMEM;
  3024. blob = drm_property_create_blob(dev,
  3025. sizeof(struct drm_color_lut) * size,
  3026. NULL);
  3027. if (IS_ERR(blob)) {
  3028. ret = PTR_ERR(blob);
  3029. blob = NULL;
  3030. goto fail;
  3031. }
  3032. /* Prepare GAMMA_LUT with the legacy values. */
  3033. blob_data = (struct drm_color_lut *) blob->data;
  3034. for (i = 0; i < size; i++) {
  3035. blob_data[i].red = red[i];
  3036. blob_data[i].green = green[i];
  3037. blob_data[i].blue = blue[i];
  3038. }
  3039. state->acquire_ctx = ctx;
  3040. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  3041. if (IS_ERR(crtc_state)) {
  3042. ret = PTR_ERR(crtc_state);
  3043. goto fail;
  3044. }
  3045. /* Reset DEGAMMA_LUT and CTM properties. */
  3046. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  3047. config->degamma_lut_property, 0);
  3048. if (ret)
  3049. goto fail;
  3050. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  3051. config->ctm_property, 0);
  3052. if (ret)
  3053. goto fail;
  3054. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  3055. config->gamma_lut_property, blob->base.id);
  3056. if (ret)
  3057. goto fail;
  3058. ret = drm_atomic_commit(state);
  3059. fail:
  3060. drm_atomic_state_put(state);
  3061. drm_property_blob_put(blob);
  3062. return ret;
  3063. }
  3064. EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);