drm_atomic_helper.c 61 KB

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