drm_atomic_helper.c 115 KB

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