drm_atomic_helper.c 97 KB

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