drm_atomic_helper.c 124 KB

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