msm_atomic.c 23 KB

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