drm_atomic_helper.c 51 KB

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