msm_atomic.c 24 KB

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