msm_atomic.c 26 KB

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