drm_atomic_helper.c 70 KB

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