msm_atomic.c 28 KB

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