drm_atomic_helper.c 97 KB

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