drm_atomic_helper.c 105 KB

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