drm_atomic_helper.c 100 KB

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