drm_atomic_helper.c 79 KB

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