drm_atomic_helper.c 117 KB

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