drm_atomic_helper.c 85 KB

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