msm_atomic.c 23 KB

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