drm_atomic_helper.c 117 KB

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