msm_atomic.c 24 KB

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