drm_atomic_helper.c 108 KB

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