drm_atomic_helper.c 95 KB

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