drm_atomic_helper.c 120 KB

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