vc4_kms.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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, false);
  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. ret = down_trylock(&vc4->async_modeset);
  100. if (ret) {
  101. kfree(c);
  102. return -EBUSY;
  103. }
  104. } else {
  105. ret = down_interruptible(&vc4->async_modeset);
  106. if (ret) {
  107. kfree(c);
  108. return ret;
  109. }
  110. }
  111. ret = drm_atomic_helper_prepare_planes(dev, state);
  112. if (ret) {
  113. kfree(c);
  114. up(&vc4->async_modeset);
  115. return ret;
  116. }
  117. for_each_plane_in_state(state, plane, new_state, i) {
  118. if ((plane->state->fb != new_state->fb) && new_state->fb) {
  119. struct drm_gem_cma_object *cma_bo =
  120. drm_fb_cma_get_gem_obj(new_state->fb, 0);
  121. struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  122. wait_seqno = max(bo->seqno, wait_seqno);
  123. }
  124. }
  125. /*
  126. * This is the point of no return - everything below never fails except
  127. * when the hw goes bonghits. Which means we can commit the new state on
  128. * the software side now.
  129. */
  130. drm_atomic_helper_swap_state(state, true);
  131. /*
  132. * Everything below can be run asynchronously without the need to grab
  133. * any modeset locks at all under one condition: It must be guaranteed
  134. * that the asynchronous work has either been cancelled (if the driver
  135. * supports it, which at least requires that the framebuffers get
  136. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  137. * before the new state gets committed on the software side with
  138. * drm_atomic_helper_swap_state().
  139. *
  140. * This scheme allows new atomic state updates to be prepared and
  141. * checked in parallel to the asynchronous completion of the previous
  142. * update. Which is important since compositors need to figure out the
  143. * composition of the next frame right after having submitted the
  144. * current layout.
  145. */
  146. if (nonblock) {
  147. vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
  148. vc4_atomic_complete_commit_seqno_cb);
  149. } else {
  150. vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
  151. vc4_atomic_complete_commit(c);
  152. }
  153. return 0;
  154. }
  155. static const struct drm_mode_config_funcs vc4_mode_funcs = {
  156. .output_poll_changed = vc4_output_poll_changed,
  157. .atomic_check = drm_atomic_helper_check,
  158. .atomic_commit = vc4_atomic_commit,
  159. .fb_create = drm_fb_cma_create,
  160. };
  161. int vc4_kms_load(struct drm_device *dev)
  162. {
  163. struct vc4_dev *vc4 = to_vc4_dev(dev);
  164. int ret;
  165. sema_init(&vc4->async_modeset, 1);
  166. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  167. if (ret < 0) {
  168. dev_err(dev->dev, "failed to initialize vblank\n");
  169. return ret;
  170. }
  171. dev->mode_config.max_width = 2048;
  172. dev->mode_config.max_height = 2048;
  173. dev->mode_config.funcs = &vc4_mode_funcs;
  174. dev->mode_config.preferred_depth = 24;
  175. dev->mode_config.async_page_flip = true;
  176. drm_mode_config_reset(dev);
  177. vc4->fbdev = drm_fbdev_cma_init(dev, 32,
  178. dev->mode_config.num_crtc,
  179. dev->mode_config.num_connector);
  180. if (IS_ERR(vc4->fbdev))
  181. vc4->fbdev = NULL;
  182. drm_kms_helper_poll_init(dev);
  183. return 0;
  184. }