msm_atomic.c 22 KB

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