msm_atomic.c 23 KB

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