drm_atomic_helper.c 113 KB

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