msm_atomic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2014 Red Hat
  4. * Author: Rob Clark <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <drm/drm_panel.h>
  19. #include <drm/drm_vblank.h>
  20. #include "msm_drv.h"
  21. #include "msm_gem.h"
  22. #include "msm_kms.h"
  23. #include "sde_trace.h"
  24. #include <drm/drm_atomic_uapi.h>
  25. #define MULTIPLE_CONN_DETECTED(x) (x > 1)
  26. struct msm_commit {
  27. struct drm_device *dev;
  28. struct drm_atomic_state *state;
  29. uint32_t crtc_mask;
  30. uint32_t plane_mask;
  31. bool nonblock;
  32. struct kthread_work commit_work;
  33. };
  34. static inline bool _msm_seamless_for_crtc(struct drm_atomic_state *state,
  35. struct drm_crtc_state *crtc_state, bool enable)
  36. {
  37. struct drm_connector *connector = NULL;
  38. struct drm_connector_state *conn_state = NULL;
  39. int i = 0;
  40. int conn_cnt = 0;
  41. if (msm_is_mode_seamless(&crtc_state->mode) ||
  42. msm_is_mode_seamless_vrr(&crtc_state->adjusted_mode) ||
  43. msm_is_mode_seamless_poms(&crtc_state->adjusted_mode) ||
  44. msm_is_mode_seamless_dyn_clk(&crtc_state->adjusted_mode))
  45. return true;
  46. if (msm_is_mode_seamless_dms(&crtc_state->adjusted_mode) && !enable)
  47. return true;
  48. if (!crtc_state->mode_changed && crtc_state->connectors_changed) {
  49. for_each_old_connector_in_state(state, connector,
  50. conn_state, i) {
  51. if ((conn_state->crtc == crtc_state->crtc) ||
  52. (connector->state->crtc ==
  53. crtc_state->crtc))
  54. conn_cnt++;
  55. if (MULTIPLE_CONN_DETECTED(conn_cnt))
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. static inline bool _msm_seamless_for_conn(struct drm_connector *connector,
  62. struct drm_connector_state *old_conn_state, bool enable)
  63. {
  64. if (!old_conn_state || !old_conn_state->crtc)
  65. return false;
  66. if (!old_conn_state->crtc->state->mode_changed &&
  67. !old_conn_state->crtc->state->active_changed &&
  68. old_conn_state->crtc->state->connectors_changed) {
  69. if (old_conn_state->crtc == connector->state->crtc)
  70. return true;
  71. }
  72. if (enable)
  73. return false;
  74. if (!connector->state->crtc &&
  75. old_conn_state->crtc->state->connectors_changed)
  76. return false;
  77. if (msm_is_mode_seamless(&old_conn_state->crtc->state->mode))
  78. return true;
  79. if (msm_is_mode_seamless_vrr(
  80. &old_conn_state->crtc->state->adjusted_mode))
  81. return true;
  82. if (msm_is_mode_seamless_dyn_clk(
  83. &old_conn_state->crtc->state->adjusted_mode))
  84. return true;
  85. if (msm_is_mode_seamless_dms(
  86. &old_conn_state->crtc->state->adjusted_mode))
  87. return true;
  88. return false;
  89. }
  90. /* clear specified crtcs (no longer pending update) */
  91. static void commit_destroy(struct msm_commit *c)
  92. {
  93. struct msm_drm_private *priv = c->dev->dev_private;
  94. uint32_t crtc_mask = c->crtc_mask;
  95. uint32_t plane_mask = c->plane_mask;
  96. /* End_atomic */
  97. spin_lock(&priv->pending_crtcs_event.lock);
  98. DBG("end: %08x", crtc_mask);
  99. priv->pending_crtcs &= ~crtc_mask;
  100. priv->pending_planes &= ~plane_mask;
  101. wake_up_all_locked(&priv->pending_crtcs_event);
  102. spin_unlock(&priv->pending_crtcs_event.lock);
  103. if (c->nonblock)
  104. kfree(c);
  105. }
  106. static void msm_atomic_wait_for_commit_done(
  107. struct drm_device *dev,
  108. struct drm_atomic_state *old_state)
  109. {
  110. struct drm_crtc *crtc;
  111. struct drm_crtc_state *new_crtc_state;
  112. struct msm_drm_private *priv = old_state->dev->dev_private;
  113. struct msm_kms *kms = priv->kms;
  114. int i;
  115. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  116. if (!new_crtc_state->active)
  117. continue;
  118. kms->funcs->wait_for_crtc_commit_done(kms, crtc);
  119. }
  120. }
  121. static void
  122. msm_disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
  123. {
  124. struct drm_connector *connector;
  125. struct drm_connector_state *old_conn_state;
  126. struct drm_crtc *crtc;
  127. struct drm_crtc_state *old_crtc_state;
  128. int i;
  129. SDE_ATRACE_BEGIN("msm_disable");
  130. for_each_old_connector_in_state(old_state, connector,
  131. old_conn_state, i) {
  132. const struct drm_encoder_helper_funcs *funcs;
  133. struct drm_encoder *encoder;
  134. struct drm_bridge *bridge;
  135. /*
  136. * Shut down everything that's in the changeset and currently
  137. * still on. So need to check the old, saved state.
  138. */
  139. if (!old_conn_state->crtc)
  140. continue;
  141. old_crtc_state = drm_atomic_get_old_crtc_state(old_state,
  142. old_conn_state->crtc);
  143. if (!old_crtc_state->active ||
  144. !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
  145. continue;
  146. encoder = old_conn_state->best_encoder;
  147. /* We shouldn't get this far if we didn't previously have
  148. * an encoder.. but WARN_ON() rather than explode.
  149. */
  150. if (WARN_ON(!encoder))
  151. continue;
  152. if (_msm_seamless_for_conn(connector, old_conn_state, false))
  153. continue;
  154. funcs = encoder->helper_private;
  155. DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
  156. encoder->base.id, encoder->name);
  157. /*
  158. * Each encoder has at most one connector (since we always steal
  159. * it away), so we won't call disable hooks twice.
  160. */
  161. bridge = drm_bridge_chain_get_first_bridge(encoder);
  162. drm_bridge_chain_disable(bridge);
  163. /* Right function depends upon target state. */
  164. if (connector->state->crtc && funcs->prepare)
  165. funcs->prepare(encoder);
  166. else if (funcs->disable)
  167. funcs->disable(encoder);
  168. else
  169. funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  170. drm_bridge_chain_post_disable(bridge);
  171. }
  172. for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  173. const struct drm_crtc_helper_funcs *funcs;
  174. /* Shut down everything that needs a full modeset. */
  175. if (!drm_atomic_crtc_needs_modeset(crtc->state))
  176. continue;
  177. if (!old_crtc_state->active)
  178. continue;
  179. if (_msm_seamless_for_crtc(old_state, crtc->state, false))
  180. continue;
  181. funcs = crtc->helper_private;
  182. DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
  183. crtc->base.id);
  184. /* Right function depends upon target state. */
  185. if (crtc->state->enable && funcs->prepare)
  186. funcs->prepare(crtc);
  187. else if (funcs->disable)
  188. funcs->disable(crtc);
  189. else
  190. funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  191. }
  192. SDE_ATRACE_END("msm_disable");
  193. }
  194. static void
  195. msm_crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
  196. {
  197. struct drm_crtc *crtc;
  198. struct drm_crtc_state *old_crtc_state;
  199. struct drm_connector *connector;
  200. struct drm_connector_state *old_conn_state;
  201. int i;
  202. for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  203. const struct drm_crtc_helper_funcs *funcs;
  204. if (!crtc->state->mode_changed)
  205. continue;
  206. funcs = crtc->helper_private;
  207. if (crtc->state->enable && funcs->mode_set_nofb) {
  208. DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
  209. crtc->base.id);
  210. funcs->mode_set_nofb(crtc);
  211. }
  212. }
  213. for_each_old_connector_in_state(old_state, connector,
  214. old_conn_state, i) {
  215. const struct drm_encoder_helper_funcs *funcs;
  216. struct drm_crtc_state *new_crtc_state;
  217. struct drm_encoder *encoder;
  218. struct drm_display_mode *mode, *adjusted_mode;
  219. struct drm_bridge *bridge;
  220. if (!connector->state->best_encoder)
  221. continue;
  222. encoder = connector->state->best_encoder;
  223. funcs = encoder->helper_private;
  224. new_crtc_state = connector->state->crtc->state;
  225. mode = &new_crtc_state->mode;
  226. adjusted_mode = &new_crtc_state->adjusted_mode;
  227. if (!new_crtc_state->mode_changed &&
  228. new_crtc_state->connectors_changed) {
  229. if (_msm_seamless_for_conn(connector,
  230. old_conn_state, false))
  231. continue;
  232. } else if (!new_crtc_state->mode_changed) {
  233. continue;
  234. }
  235. DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
  236. encoder->base.id, encoder->name);
  237. SDE_ATRACE_BEGIN("msm_set_mode");
  238. /*
  239. * Each encoder has at most one connector (since we always steal
  240. * it away), so we won't call mode_set hooks twice.
  241. */
  242. if (funcs->mode_set)
  243. funcs->mode_set(encoder, mode, adjusted_mode);
  244. bridge = drm_bridge_chain_get_first_bridge(encoder);
  245. drm_bridge_chain_mode_set(bridge, mode, adjusted_mode);
  246. SDE_ATRACE_END("msm_set_mode");
  247. }
  248. }
  249. /**
  250. * msm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
  251. * @dev: DRM device
  252. * @old_state: atomic state object with old state structures
  253. *
  254. * This function shuts down all the outputs that need to be shut down and
  255. * prepares them (if required) with the new mode.
  256. *
  257. * For compatibility with legacy crtc helpers this should be called before
  258. * drm_atomic_helper_commit_planes(), which is what the default commit function
  259. * does. But drivers with different needs can group the modeset commits together
  260. * and do the plane commits at the end. This is useful for drivers doing runtime
  261. * PM since planes updates then only happen when the CRTC is actually enabled.
  262. */
  263. void msm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
  264. struct drm_atomic_state *old_state)
  265. {
  266. msm_disable_outputs(dev, old_state);
  267. drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
  268. msm_crtc_set_mode(dev, old_state);
  269. }
  270. /**
  271. * msm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
  272. * @dev: DRM device
  273. * @old_state: atomic state object with old state structures
  274. *
  275. * This function enables all the outputs with the new configuration which had to
  276. * be turned off for the update.
  277. *
  278. * For compatibility with legacy crtc helpers this should be called after
  279. * drm_atomic_helper_commit_planes(), which is what the default commit function
  280. * does. But drivers with different needs can group the modeset commits together
  281. * and do the plane commits at the end. This is useful for drivers doing runtime
  282. * PM since planes updates then only happen when the CRTC is actually enabled.
  283. */
  284. static void msm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
  285. struct drm_atomic_state *old_state)
  286. {
  287. struct drm_crtc *crtc;
  288. struct drm_crtc_state *old_crtc_state;
  289. struct drm_crtc_state *new_crtc_state;
  290. struct drm_connector *connector;
  291. struct drm_connector_state *new_conn_state;
  292. struct msm_drm_private *priv = dev->dev_private;
  293. struct msm_kms *kms = priv->kms;
  294. int bridge_enable_count = 0;
  295. int i;
  296. SDE_ATRACE_BEGIN("msm_enable");
  297. for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state,
  298. new_crtc_state, i) {
  299. const struct drm_crtc_helper_funcs *funcs;
  300. /* Need to filter out CRTCs where only planes change. */
  301. if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
  302. continue;
  303. if (!new_crtc_state->active)
  304. continue;
  305. if (_msm_seamless_for_crtc(old_state, crtc->state, true))
  306. continue;
  307. funcs = crtc->helper_private;
  308. if (crtc->state->enable) {
  309. DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
  310. crtc->base.id);
  311. if (funcs->atomic_enable)
  312. funcs->atomic_enable(crtc, old_crtc_state);
  313. else
  314. funcs->commit(crtc);
  315. }
  316. if (msm_needs_vblank_pre_modeset(
  317. &new_crtc_state->adjusted_mode))
  318. drm_crtc_wait_one_vblank(crtc);
  319. }
  320. for_each_new_connector_in_state(old_state, connector,
  321. new_conn_state, i) {
  322. const struct drm_encoder_helper_funcs *funcs;
  323. struct drm_encoder *encoder;
  324. struct drm_connector_state *old_conn_state;
  325. struct drm_bridge *bridge;
  326. if (!new_conn_state->best_encoder)
  327. continue;
  328. if (!new_conn_state->crtc->state->active ||
  329. !drm_atomic_crtc_needs_modeset(
  330. new_conn_state->crtc->state))
  331. continue;
  332. old_conn_state = drm_atomic_get_old_connector_state(
  333. old_state, connector);
  334. if (_msm_seamless_for_conn(connector, old_conn_state, true))
  335. continue;
  336. encoder = connector->state->best_encoder;
  337. funcs = encoder->helper_private;
  338. DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
  339. encoder->base.id, encoder->name);
  340. /*
  341. * Each encoder has at most one connector (since we always steal
  342. * it away), so we won't call enable hooks twice.
  343. */
  344. bridge = drm_bridge_chain_get_first_bridge(encoder);
  345. drm_bridge_chain_pre_enable(bridge);
  346. ++bridge_enable_count;
  347. if (funcs->enable)
  348. funcs->enable(encoder);
  349. else
  350. funcs->commit(encoder);
  351. }
  352. if (kms && kms->funcs && kms->funcs->commit) {
  353. DRM_DEBUG_ATOMIC("triggering commit\n");
  354. kms->funcs->commit(kms, old_state);
  355. }
  356. /* If no bridges were pre_enabled, skip iterating over them again */
  357. if (bridge_enable_count == 0) {
  358. SDE_ATRACE_END("msm_enable");
  359. return;
  360. }
  361. for_each_new_connector_in_state(old_state, connector,
  362. new_conn_state, i) {
  363. struct drm_encoder *encoder;
  364. struct drm_connector_state *old_conn_state;
  365. struct drm_bridge *bridge;
  366. if (!new_conn_state->best_encoder)
  367. continue;
  368. if (!new_conn_state->crtc->state->active ||
  369. !drm_atomic_crtc_needs_modeset(
  370. new_conn_state->crtc->state))
  371. continue;
  372. old_conn_state = drm_atomic_get_old_connector_state(
  373. old_state, connector);
  374. if (_msm_seamless_for_conn(connector, old_conn_state, true))
  375. continue;
  376. encoder = connector->state->best_encoder;
  377. DRM_DEBUG_ATOMIC("bridge enable enabling [ENCODER:%d:%s]\n",
  378. encoder->base.id, encoder->name);
  379. bridge = drm_bridge_chain_get_first_bridge(encoder);
  380. drm_bridge_chain_enable(bridge);
  381. }
  382. SDE_ATRACE_END("msm_enable");
  383. }
  384. int msm_atomic_prepare_fb(struct drm_plane *plane,
  385. struct drm_plane_state *new_state)
  386. {
  387. struct msm_drm_private *priv = plane->dev->dev_private;
  388. struct msm_kms *kms = priv->kms;
  389. struct drm_gem_object *obj;
  390. struct msm_gem_object *msm_obj;
  391. struct dma_fence *fence;
  392. if (!new_state->fb)
  393. return 0;
  394. obj = msm_framebuffer_bo(new_state->fb, 0);
  395. msm_obj = to_msm_bo(obj);
  396. fence = dma_resv_get_excl_rcu(msm_obj->resv);
  397. drm_atomic_set_fence_for_plane(new_state, fence);
  398. return msm_framebuffer_prepare(new_state->fb, kms->aspace);
  399. }
  400. /* The (potentially) asynchronous part of the commit. At this point
  401. * nothing can fail short of armageddon.
  402. */
  403. static void complete_commit(struct msm_commit *c)
  404. {
  405. struct drm_atomic_state *state = c->state;
  406. struct drm_device *dev = state->dev;
  407. struct msm_drm_private *priv = dev->dev_private;
  408. struct msm_kms *kms = priv->kms;
  409. drm_atomic_helper_wait_for_fences(dev, state, false);
  410. kms->funcs->prepare_commit(kms, state);
  411. msm_atomic_helper_commit_modeset_disables(dev, state);
  412. drm_atomic_helper_commit_planes(dev, state,
  413. DRM_PLANE_COMMIT_ACTIVE_ONLY);
  414. msm_atomic_helper_commit_modeset_enables(dev, state);
  415. /* NOTE: _wait_for_vblanks() only waits for vblank on
  416. * enabled CRTCs. So we end up faulting when disabling
  417. * due to (potentially) unref'ing the outgoing fb's
  418. * before the vblank when the disable has latched.
  419. *
  420. * But if it did wait on disabled (or newly disabled)
  421. * CRTCs, that would be racy (ie. we could have missed
  422. * the irq. We need some way to poll for pipe shut
  423. * down. Or just live with occasionally hitting the
  424. * timeout in the CRTC disable path (which really should
  425. * not be critical path)
  426. */
  427. msm_atomic_wait_for_commit_done(dev, state);
  428. drm_atomic_helper_cleanup_planes(dev, state);
  429. kms->funcs->complete_commit(kms, state);
  430. drm_atomic_state_put(state);
  431. commit_destroy(c);
  432. }
  433. static void _msm_drm_commit_work_cb(struct kthread_work *work)
  434. {
  435. struct msm_commit *commit = NULL;
  436. if (!work) {
  437. DRM_ERROR("%s: Invalid commit work data!\n", __func__);
  438. return;
  439. }
  440. commit = container_of(work, struct msm_commit, commit_work);
  441. SDE_ATRACE_BEGIN("complete_commit");
  442. complete_commit(commit);
  443. SDE_ATRACE_END("complete_commit");
  444. }
  445. static struct msm_commit *commit_init(struct drm_atomic_state *state,
  446. bool nonblock)
  447. {
  448. struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
  449. if (!c)
  450. return NULL;
  451. c->dev = state->dev;
  452. c->state = state;
  453. c->nonblock = nonblock;
  454. kthread_init_work(&c->commit_work, _msm_drm_commit_work_cb);
  455. return c;
  456. }
  457. /* Start display thread function */
  458. static void msm_atomic_commit_dispatch(struct drm_device *dev,
  459. struct drm_atomic_state *state, struct msm_commit *commit)
  460. {
  461. struct msm_drm_private *priv = dev->dev_private;
  462. struct drm_crtc *crtc = NULL;
  463. struct drm_crtc_state *crtc_state = NULL;
  464. int ret = -ECANCELED, i = 0, j = 0;
  465. bool nonblock;
  466. /* cache since work will kfree commit in non-blocking case */
  467. nonblock = commit->nonblock;
  468. for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
  469. for (j = 0; j < priv->num_crtcs; j++) {
  470. if (priv->disp_thread[j].crtc_id ==
  471. crtc->base.id) {
  472. if (priv->disp_thread[j].thread) {
  473. kthread_queue_work(
  474. &priv->disp_thread[j].worker,
  475. &commit->commit_work);
  476. /* only return zero if work is
  477. * queued successfully.
  478. */
  479. ret = 0;
  480. } else {
  481. DRM_ERROR(" Error for crtc_id: %d\n",
  482. priv->disp_thread[j].crtc_id);
  483. ret = -EINVAL;
  484. }
  485. break;
  486. }
  487. }
  488. /*
  489. * TODO: handle cases where there will be more than
  490. * one crtc per commit cycle. Remove this check then.
  491. * Current assumption is there will be only one crtc
  492. * per commit cycle.
  493. */
  494. if (j < priv->num_crtcs)
  495. break;
  496. }
  497. if (ret) {
  498. if (ret == -EINVAL)
  499. DRM_ERROR("failed to dispatch commit to any CRTC\n");
  500. else
  501. DRM_DEBUG("empty crtc state\n");
  502. /**
  503. * this is not expected to happen, but at this point the state
  504. * has been swapped, but we couldn't dispatch to a crtc thread.
  505. * fallback now to a synchronous complete_commit to try and
  506. * ensure that SW and HW state don't get out of sync.
  507. */
  508. complete_commit(commit);
  509. } else if (!nonblock) {
  510. kthread_flush_work(&commit->commit_work);
  511. }
  512. /* free nonblocking commits in this context, after processing */
  513. if (!nonblock)
  514. kfree(commit);
  515. }
  516. /**
  517. * drm_atomic_helper_commit - commit validated state object
  518. * @dev: DRM device
  519. * @state: the driver state object
  520. * @nonblock: nonblocking commit
  521. *
  522. * This function commits a with drm_atomic_helper_check() pre-validated state
  523. * object. This can still fail when e.g. the framebuffer reservation fails.
  524. *
  525. * RETURNS
  526. * Zero for success or -errno.
  527. */
  528. int msm_atomic_commit(struct drm_device *dev,
  529. struct drm_atomic_state *state, bool nonblock)
  530. {
  531. struct msm_drm_private *priv = dev->dev_private;
  532. struct msm_commit *c;
  533. struct drm_crtc *crtc;
  534. struct drm_crtc_state *crtc_state;
  535. struct drm_plane *plane;
  536. struct drm_plane_state *old_plane_state, *new_plane_state;
  537. int i, ret;
  538. if (!priv || priv->shutdown_in_progress) {
  539. DRM_ERROR("priv is null or shutdwon is in-progress\n");
  540. return -EINVAL;
  541. }
  542. SDE_ATRACE_BEGIN("atomic_commit");
  543. ret = drm_atomic_helper_prepare_planes(dev, state);
  544. if (ret) {
  545. SDE_ATRACE_END("atomic_commit");
  546. return ret;
  547. }
  548. c = commit_init(state, nonblock);
  549. if (!c) {
  550. ret = -ENOMEM;
  551. goto error;
  552. }
  553. /*
  554. * Figure out what crtcs we have:
  555. */
  556. for_each_new_crtc_in_state(state, crtc, crtc_state, i)
  557. c->crtc_mask |= drm_crtc_mask(crtc);
  558. /*
  559. * Figure out what fence to wait for:
  560. */
  561. for_each_oldnew_plane_in_state(state, plane, old_plane_state,
  562. new_plane_state, i) {
  563. if ((new_plane_state->fb != old_plane_state->fb)
  564. && new_plane_state->fb) {
  565. struct drm_gem_object *obj =
  566. msm_framebuffer_bo(new_plane_state->fb, 0);
  567. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  568. struct dma_fence *fence =
  569. dma_resv_get_excl_rcu(msm_obj->resv);
  570. drm_atomic_set_fence_for_plane(new_plane_state, fence);
  571. }
  572. c->plane_mask |= (1 << drm_plane_index(plane));
  573. }
  574. /*
  575. * Wait for pending updates on any of the same crtc's and then
  576. * mark our set of crtc's as busy:
  577. */
  578. /* Start Atomic */
  579. spin_lock(&priv->pending_crtcs_event.lock);
  580. ret = wait_event_interruptible_locked(priv->pending_crtcs_event,
  581. !(priv->pending_crtcs & c->crtc_mask) &&
  582. !(priv->pending_planes & c->plane_mask));
  583. if (ret == 0) {
  584. DBG("start: %08x", c->crtc_mask);
  585. priv->pending_crtcs |= c->crtc_mask;
  586. priv->pending_planes |= c->plane_mask;
  587. }
  588. spin_unlock(&priv->pending_crtcs_event.lock);
  589. if (ret)
  590. goto err_free;
  591. WARN_ON(drm_atomic_helper_swap_state(state, false) < 0);
  592. /*
  593. * Provide the driver a chance to prepare for output fences. This is
  594. * done after the point of no return, but before asynchronous commits
  595. * are dispatched to work queues, so that the fence preparation is
  596. * finished before the .atomic_commit returns.
  597. */
  598. if (priv && priv->kms && priv->kms->funcs &&
  599. priv->kms->funcs->prepare_fence)
  600. priv->kms->funcs->prepare_fence(priv->kms, state);
  601. /*
  602. * Everything below can be run asynchronously without the need to grab
  603. * any modeset locks at all under one conditions: It must be guaranteed
  604. * that the asynchronous work has either been cancelled (if the driver
  605. * supports it, which at least requires that the framebuffers get
  606. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  607. * before the new state gets committed on the software side with
  608. * drm_atomic_helper_swap_state().
  609. *
  610. * This scheme allows new atomic state updates to be prepared and
  611. * checked in parallel to the asynchronous completion of the previous
  612. * update. Which is important since compositors need to figure out the
  613. * composition of the next frame right after having submitted the
  614. * current layout
  615. */
  616. drm_atomic_state_get(state);
  617. msm_atomic_commit_dispatch(dev, state, c);
  618. SDE_ATRACE_END("atomic_commit");
  619. return 0;
  620. err_free:
  621. kfree(c);
  622. error:
  623. drm_atomic_helper_cleanup_planes(dev, state);
  624. SDE_ATRACE_END("atomic_commit");
  625. return ret;
  626. }
  627. struct drm_atomic_state *msm_atomic_state_alloc(struct drm_device *dev)
  628. {
  629. struct msm_kms_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  630. if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
  631. kfree(state);
  632. return NULL;
  633. }
  634. return &state->base;
  635. }
  636. void msm_atomic_state_clear(struct drm_atomic_state *s)
  637. {
  638. struct msm_kms_state *state = to_kms_state(s);
  639. drm_atomic_state_default_clear(&state->base);
  640. kfree(state->state);
  641. state->state = NULL;
  642. }
  643. void msm_atomic_state_free(struct drm_atomic_state *state)
  644. {
  645. kfree(to_kms_state(state)->state);
  646. drm_atomic_state_default_release(state);
  647. kfree(state);
  648. }
  649. void msm_atomic_commit_tail(struct drm_atomic_state *state)
  650. {
  651. struct drm_device *dev = state->dev;
  652. struct msm_drm_private *priv = dev->dev_private;
  653. struct msm_kms *kms = priv->kms;
  654. kms->funcs->prepare_commit(kms, state);
  655. drm_atomic_helper_commit_modeset_disables(dev, state);
  656. drm_atomic_helper_commit_planes(dev, state, 0);
  657. drm_atomic_helper_commit_modeset_enables(dev, state);
  658. if (kms->funcs->commit) {
  659. DRM_DEBUG_ATOMIC("triggering commit\n");
  660. kms->funcs->commit(kms, state);
  661. }
  662. if (!state->legacy_cursor_update)
  663. msm_atomic_wait_for_commit_done(dev, state);
  664. kms->funcs->complete_commit(kms, state);
  665. drm_atomic_helper_wait_for_vblanks(dev, state);
  666. drm_atomic_helper_commit_hw_done(state);
  667. drm_atomic_helper_cleanup_planes(dev, state);
  668. }