msm_atomic.c 26 KB

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