msm_atomic.c 24 KB

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