msm_atomic.c 23 KB

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