msm_atomic.c 23 KB

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