vc4_kms.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /**
  9. * DOC: VC4 KMS
  10. *
  11. * This is the general code for implementing KMS mode setting that
  12. * doesn't clearly associate with any of the other objects (plane,
  13. * crtc, HDMI encoder).
  14. */
  15. #include "drm_crtc.h"
  16. #include "drm_atomic.h"
  17. #include "drm_atomic_helper.h"
  18. #include "drm_crtc_helper.h"
  19. #include "drm_plane_helper.h"
  20. #include "drm_fb_cma_helper.h"
  21. #include "vc4_drv.h"
  22. static void vc4_output_poll_changed(struct drm_device *dev)
  23. {
  24. struct vc4_dev *vc4 = to_vc4_dev(dev);
  25. drm_fbdev_cma_hotplug_event(vc4->fbdev);
  26. }
  27. struct vc4_commit {
  28. struct drm_device *dev;
  29. struct drm_atomic_state *state;
  30. struct vc4_seqno_cb cb;
  31. };
  32. static void
  33. vc4_atomic_complete_commit(struct vc4_commit *c)
  34. {
  35. struct drm_atomic_state *state = c->state;
  36. struct drm_device *dev = state->dev;
  37. struct vc4_dev *vc4 = to_vc4_dev(dev);
  38. drm_atomic_helper_commit_modeset_disables(dev, state);
  39. drm_atomic_helper_commit_planes(dev, state, 0);
  40. drm_atomic_helper_commit_modeset_enables(dev, state);
  41. /* Make sure that drm_atomic_helper_wait_for_vblanks()
  42. * actually waits for vblank. If we're doing a full atomic
  43. * modeset (as opposed to a vc4_update_plane() short circuit),
  44. * then we need to wait for scanout to be done with our
  45. * display lists before we free it and potentially reallocate
  46. * and overwrite the dlist memory with a new modeset.
  47. */
  48. state->legacy_cursor_update = false;
  49. drm_atomic_helper_wait_for_vblanks(dev, state);
  50. drm_atomic_helper_cleanup_planes(dev, state);
  51. drm_atomic_state_free(state);
  52. up(&vc4->async_modeset);
  53. kfree(c);
  54. }
  55. static void
  56. vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
  57. {
  58. struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
  59. vc4_atomic_complete_commit(c);
  60. }
  61. static struct vc4_commit *commit_init(struct drm_atomic_state *state)
  62. {
  63. struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
  64. if (!c)
  65. return NULL;
  66. c->dev = state->dev;
  67. c->state = state;
  68. return c;
  69. }
  70. /**
  71. * vc4_atomic_commit - commit validated state object
  72. * @dev: DRM device
  73. * @state: the driver state object
  74. * @nonblock: nonblocking commit
  75. *
  76. * This function commits a with drm_atomic_helper_check() pre-validated state
  77. * object. This can still fail when e.g. the framebuffer reservation fails. For
  78. * now this doesn't implement asynchronous commits.
  79. *
  80. * RETURNS
  81. * Zero for success or -errno.
  82. */
  83. static int vc4_atomic_commit(struct drm_device *dev,
  84. struct drm_atomic_state *state,
  85. bool nonblock)
  86. {
  87. struct vc4_dev *vc4 = to_vc4_dev(dev);
  88. int ret;
  89. int i;
  90. uint64_t wait_seqno = 0;
  91. struct vc4_commit *c;
  92. struct drm_plane *plane;
  93. struct drm_plane_state *new_state;
  94. c = commit_init(state);
  95. if (!c)
  96. return -ENOMEM;
  97. /* Make sure that any outstanding modesets have finished. */
  98. if (nonblock) {
  99. struct drm_crtc *crtc;
  100. struct drm_crtc_state *crtc_state;
  101. unsigned long flags;
  102. bool busy = false;
  103. /*
  104. * If there's an undispatched event to send then we're
  105. * obviously still busy. If there isn't, then we can
  106. * unconditionally wait for the semaphore because it
  107. * shouldn't be contended (for long).
  108. *
  109. * This is to prevent a race where queuing a new flip
  110. * from userspace immediately on receipt of an event
  111. * beats our clean-up and returns EBUSY.
  112. */
  113. spin_lock_irqsave(&dev->event_lock, flags);
  114. for_each_crtc_in_state(state, crtc, crtc_state, i)
  115. busy |= vc4_event_pending(crtc);
  116. spin_unlock_irqrestore(&dev->event_lock, flags);
  117. if (busy) {
  118. kfree(c);
  119. return -EBUSY;
  120. }
  121. }
  122. ret = down_interruptible(&vc4->async_modeset);
  123. if (ret) {
  124. kfree(c);
  125. return ret;
  126. }
  127. ret = drm_atomic_helper_prepare_planes(dev, state);
  128. if (ret) {
  129. kfree(c);
  130. up(&vc4->async_modeset);
  131. return ret;
  132. }
  133. for_each_plane_in_state(state, plane, new_state, i) {
  134. if ((plane->state->fb != new_state->fb) && new_state->fb) {
  135. struct drm_gem_cma_object *cma_bo =
  136. drm_fb_cma_get_gem_obj(new_state->fb, 0);
  137. struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  138. wait_seqno = max(bo->seqno, wait_seqno);
  139. }
  140. }
  141. /*
  142. * This is the point of no return - everything below never fails except
  143. * when the hw goes bonghits. Which means we can commit the new state on
  144. * the software side now.
  145. */
  146. drm_atomic_helper_swap_state(state, true);
  147. /*
  148. * Everything below can be run asynchronously without the need to grab
  149. * any modeset locks at all under one condition: It must be guaranteed
  150. * that the asynchronous work has either been cancelled (if the driver
  151. * supports it, which at least requires that the framebuffers get
  152. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  153. * before the new state gets committed on the software side with
  154. * drm_atomic_helper_swap_state().
  155. *
  156. * This scheme allows new atomic state updates to be prepared and
  157. * checked in parallel to the asynchronous completion of the previous
  158. * update. Which is important since compositors need to figure out the
  159. * composition of the next frame right after having submitted the
  160. * current layout.
  161. */
  162. if (nonblock) {
  163. vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
  164. vc4_atomic_complete_commit_seqno_cb);
  165. } else {
  166. vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
  167. vc4_atomic_complete_commit(c);
  168. }
  169. return 0;
  170. }
  171. static const struct drm_mode_config_funcs vc4_mode_funcs = {
  172. .output_poll_changed = vc4_output_poll_changed,
  173. .atomic_check = drm_atomic_helper_check,
  174. .atomic_commit = vc4_atomic_commit,
  175. .fb_create = drm_fb_cma_create,
  176. };
  177. int vc4_kms_load(struct drm_device *dev)
  178. {
  179. struct vc4_dev *vc4 = to_vc4_dev(dev);
  180. int ret;
  181. sema_init(&vc4->async_modeset, 1);
  182. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  183. if (ret < 0) {
  184. dev_err(dev->dev, "failed to initialize vblank\n");
  185. return ret;
  186. }
  187. dev->mode_config.max_width = 2048;
  188. dev->mode_config.max_height = 2048;
  189. dev->mode_config.funcs = &vc4_mode_funcs;
  190. dev->mode_config.preferred_depth = 24;
  191. dev->mode_config.async_page_flip = true;
  192. drm_mode_config_reset(dev);
  193. vc4->fbdev = drm_fbdev_cma_init(dev, 32,
  194. dev->mode_config.num_crtc,
  195. dev->mode_config.num_connector);
  196. if (IS_ERR(vc4->fbdev))
  197. vc4->fbdev = NULL;
  198. drm_kms_helper_poll_init(dev);
  199. return 0;
  200. }