msm_atomic.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "msm_kms.h"
  19. #include "msm_gem.h"
  20. #include "msm_fence.h"
  21. struct msm_commit {
  22. struct drm_device *dev;
  23. struct drm_atomic_state *state;
  24. struct work_struct work;
  25. uint32_t crtc_mask;
  26. };
  27. static void commit_worker(struct work_struct *work);
  28. /* block until specified crtcs are no longer pending update, and
  29. * atomically mark them as pending update
  30. */
  31. static int start_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
  32. {
  33. int ret;
  34. spin_lock(&priv->pending_crtcs_event.lock);
  35. ret = wait_event_interruptible_locked(priv->pending_crtcs_event,
  36. !(priv->pending_crtcs & crtc_mask));
  37. if (ret == 0) {
  38. DBG("start: %08x", crtc_mask);
  39. priv->pending_crtcs |= crtc_mask;
  40. }
  41. spin_unlock(&priv->pending_crtcs_event.lock);
  42. return ret;
  43. }
  44. /* clear specified crtcs (no longer pending update)
  45. */
  46. static void end_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
  47. {
  48. spin_lock(&priv->pending_crtcs_event.lock);
  49. DBG("end: %08x", crtc_mask);
  50. priv->pending_crtcs &= ~crtc_mask;
  51. wake_up_all_locked(&priv->pending_crtcs_event);
  52. spin_unlock(&priv->pending_crtcs_event.lock);
  53. }
  54. static struct msm_commit *commit_init(struct drm_atomic_state *state)
  55. {
  56. struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
  57. if (!c)
  58. return NULL;
  59. c->dev = state->dev;
  60. c->state = state;
  61. INIT_WORK(&c->work, commit_worker);
  62. return c;
  63. }
  64. static void commit_destroy(struct msm_commit *c)
  65. {
  66. end_atomic(c->dev->dev_private, c->crtc_mask);
  67. kfree(c);
  68. }
  69. static void msm_atomic_wait_for_commit_done(struct drm_device *dev,
  70. struct drm_atomic_state *old_state)
  71. {
  72. struct drm_crtc *crtc;
  73. struct drm_crtc_state *new_crtc_state;
  74. struct msm_drm_private *priv = old_state->dev->dev_private;
  75. struct msm_kms *kms = priv->kms;
  76. int i;
  77. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  78. if (!new_crtc_state->active)
  79. continue;
  80. kms->funcs->wait_for_crtc_commit_done(kms, crtc);
  81. }
  82. }
  83. /* The (potentially) asynchronous part of the commit. At this point
  84. * nothing can fail short of armageddon.
  85. */
  86. static void complete_commit(struct msm_commit *c, bool async)
  87. {
  88. struct drm_atomic_state *state = c->state;
  89. struct drm_device *dev = state->dev;
  90. struct msm_drm_private *priv = dev->dev_private;
  91. struct msm_kms *kms = priv->kms;
  92. drm_atomic_helper_wait_for_fences(dev, state, false);
  93. kms->funcs->prepare_commit(kms, state);
  94. drm_atomic_helper_commit_modeset_disables(dev, state);
  95. drm_atomic_helper_commit_planes(dev, state, 0);
  96. drm_atomic_helper_commit_modeset_enables(dev, state);
  97. /* NOTE: _wait_for_vblanks() only waits for vblank on
  98. * enabled CRTCs. So we end up faulting when disabling
  99. * due to (potentially) unref'ing the outgoing fb's
  100. * before the vblank when the disable has latched.
  101. *
  102. * But if it did wait on disabled (or newly disabled)
  103. * CRTCs, that would be racy (ie. we could have missed
  104. * the irq. We need some way to poll for pipe shut
  105. * down. Or just live with occasionally hitting the
  106. * timeout in the CRTC disable path (which really should
  107. * not be critical path)
  108. */
  109. msm_atomic_wait_for_commit_done(dev, state);
  110. drm_atomic_helper_cleanup_planes(dev, state);
  111. kms->funcs->complete_commit(kms, state);
  112. drm_atomic_state_put(state);
  113. commit_destroy(c);
  114. }
  115. static void commit_worker(struct work_struct *work)
  116. {
  117. complete_commit(container_of(work, struct msm_commit, work), true);
  118. }
  119. /*
  120. * this func is identical to the drm_atomic_helper_check, but we keep this
  121. * because we might eventually need to have a more finegrained check
  122. * sequence without using the atomic helpers.
  123. *
  124. * In the past, we first called drm_atomic_helper_check_planes, and then
  125. * drm_atomic_helper_check_modeset. We needed this because the MDP5 plane's
  126. * ->atomic_check could update ->mode_changed for pixel format changes.
  127. * This, however isn't needed now because if there is a pixel format change,
  128. * we just assign a new hwpipe for it with a new SMP allocation. We might
  129. * eventually hit a condition where we would need to do a full modeset if
  130. * we run out of planes. There, we'd probably need to set mode_changed.
  131. */
  132. int msm_atomic_check(struct drm_device *dev,
  133. struct drm_atomic_state *state)
  134. {
  135. int ret;
  136. ret = drm_atomic_helper_check_modeset(dev, state);
  137. if (ret)
  138. return ret;
  139. ret = drm_atomic_helper_check_planes(dev, state);
  140. if (ret)
  141. return ret;
  142. return ret;
  143. }
  144. /**
  145. * drm_atomic_helper_commit - commit validated state object
  146. * @dev: DRM device
  147. * @state: the driver state object
  148. * @nonblock: nonblocking commit
  149. *
  150. * This function commits a with drm_atomic_helper_check() pre-validated state
  151. * object. This can still fail when e.g. the framebuffer reservation fails.
  152. *
  153. * RETURNS
  154. * Zero for success or -errno.
  155. */
  156. int msm_atomic_commit(struct drm_device *dev,
  157. struct drm_atomic_state *state, bool nonblock)
  158. {
  159. struct msm_drm_private *priv = dev->dev_private;
  160. struct msm_commit *c;
  161. struct drm_crtc *crtc;
  162. struct drm_crtc_state *crtc_state;
  163. struct drm_plane *plane;
  164. struct drm_plane_state *old_plane_state, *new_plane_state;
  165. int i, ret;
  166. ret = drm_atomic_helper_prepare_planes(dev, state);
  167. if (ret)
  168. return ret;
  169. c = commit_init(state);
  170. if (!c) {
  171. ret = -ENOMEM;
  172. goto error;
  173. }
  174. /*
  175. * Figure out what crtcs we have:
  176. */
  177. for_each_new_crtc_in_state(state, crtc, crtc_state, i)
  178. c->crtc_mask |= drm_crtc_mask(crtc);
  179. /*
  180. * Figure out what fence to wait for:
  181. */
  182. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  183. if ((new_plane_state->fb != old_plane_state->fb) && new_plane_state->fb) {
  184. struct drm_gem_object *obj = msm_framebuffer_bo(new_plane_state->fb, 0);
  185. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  186. struct dma_fence *fence = reservation_object_get_excl_rcu(msm_obj->resv);
  187. drm_atomic_set_fence_for_plane(new_plane_state, fence);
  188. }
  189. }
  190. /*
  191. * Wait for pending updates on any of the same crtc's and then
  192. * mark our set of crtc's as busy:
  193. */
  194. ret = start_atomic(dev->dev_private, c->crtc_mask);
  195. if (ret)
  196. goto err_free;
  197. BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
  198. /*
  199. * This is the point of no return - everything below never fails except
  200. * when the hw goes bonghits. Which means we can commit the new state on
  201. * the software side now.
  202. *
  203. * swap driver private state while still holding state_lock
  204. */
  205. if (to_kms_state(state)->state)
  206. priv->kms->funcs->swap_state(priv->kms, state);
  207. /*
  208. * Everything below can be run asynchronously without the need to grab
  209. * any modeset locks at all under one conditions: It must be guaranteed
  210. * that the asynchronous work has either been cancelled (if the driver
  211. * supports it, which at least requires that the framebuffers get
  212. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  213. * before the new state gets committed on the software side with
  214. * drm_atomic_helper_swap_state().
  215. *
  216. * This scheme allows new atomic state updates to be prepared and
  217. * checked in parallel to the asynchronous completion of the previous
  218. * update. Which is important since compositors need to figure out the
  219. * composition of the next frame right after having submitted the
  220. * current layout.
  221. */
  222. drm_atomic_state_get(state);
  223. if (nonblock) {
  224. queue_work(priv->atomic_wq, &c->work);
  225. return 0;
  226. }
  227. complete_commit(c, false);
  228. return 0;
  229. err_free:
  230. kfree(c);
  231. error:
  232. drm_atomic_helper_cleanup_planes(dev, state);
  233. return ret;
  234. }
  235. struct drm_atomic_state *msm_atomic_state_alloc(struct drm_device *dev)
  236. {
  237. struct msm_kms_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  238. if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
  239. kfree(state);
  240. return NULL;
  241. }
  242. return &state->base;
  243. }
  244. void msm_atomic_state_clear(struct drm_atomic_state *s)
  245. {
  246. struct msm_kms_state *state = to_kms_state(s);
  247. drm_atomic_state_default_clear(&state->base);
  248. kfree(state->state);
  249. state->state = NULL;
  250. }
  251. void msm_atomic_state_free(struct drm_atomic_state *state)
  252. {
  253. kfree(to_kms_state(state)->state);
  254. drm_atomic_state_default_release(state);
  255. kfree(state);
  256. }