drm_atomic_helper.c 77 KB

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