msm_atomic.c 23 KB

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