vc4_kms.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (vc4->fbdev)
  26. drm_fbdev_cma_hotplug_event(vc4->fbdev);
  27. }
  28. struct vc4_commit {
  29. struct drm_device *dev;
  30. struct drm_atomic_state *state;
  31. struct vc4_seqno_cb cb;
  32. };
  33. static void
  34. vc4_atomic_complete_commit(struct vc4_commit *c)
  35. {
  36. struct drm_atomic_state *state = c->state;
  37. struct drm_device *dev = state->dev;
  38. struct vc4_dev *vc4 = to_vc4_dev(dev);
  39. drm_atomic_helper_commit_modeset_disables(dev, state);
  40. drm_atomic_helper_commit_planes(dev, state, false);
  41. drm_atomic_helper_commit_modeset_enables(dev, state);
  42. /* Make sure that drm_atomic_helper_wait_for_vblanks()
  43. * actually waits for vblank. If we're doing a full atomic
  44. * modeset (as opposed to a vc4_update_plane() short circuit),
  45. * then we need to wait for scanout to be done with our
  46. * display lists before we free it and potentially reallocate
  47. * and overwrite the dlist memory with a new modeset.
  48. */
  49. state->legacy_cursor_update = false;
  50. drm_atomic_helper_wait_for_vblanks(dev, state);
  51. drm_atomic_helper_cleanup_planes(dev, state);
  52. drm_atomic_state_free(state);
  53. up(&vc4->async_modeset);
  54. kfree(c);
  55. }
  56. static void
  57. vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
  58. {
  59. struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
  60. vc4_atomic_complete_commit(c);
  61. }
  62. static struct vc4_commit *commit_init(struct drm_atomic_state *state)
  63. {
  64. struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
  65. if (!c)
  66. return NULL;
  67. c->dev = state->dev;
  68. c->state = state;
  69. return c;
  70. }
  71. /**
  72. * vc4_atomic_commit - commit validated state object
  73. * @dev: DRM device
  74. * @state: the driver state object
  75. * @nonblock: nonblocking commit
  76. *
  77. * This function commits a with drm_atomic_helper_check() pre-validated state
  78. * object. This can still fail when e.g. the framebuffer reservation fails. For
  79. * now this doesn't implement asynchronous commits.
  80. *
  81. * RETURNS
  82. * Zero for success or -errno.
  83. */
  84. static int vc4_atomic_commit(struct drm_device *dev,
  85. struct drm_atomic_state *state,
  86. bool nonblock)
  87. {
  88. struct vc4_dev *vc4 = to_vc4_dev(dev);
  89. int ret;
  90. int i;
  91. uint64_t wait_seqno = 0;
  92. struct vc4_commit *c;
  93. c = commit_init(state);
  94. if (!c)
  95. return -ENOMEM;
  96. /* Make sure that any outstanding modesets have finished. */
  97. if (nonblock) {
  98. ret = down_trylock(&vc4->async_modeset);
  99. if (ret) {
  100. kfree(c);
  101. return -EBUSY;
  102. }
  103. } else {
  104. ret = down_interruptible(&vc4->async_modeset);
  105. if (ret) {
  106. kfree(c);
  107. return ret;
  108. }
  109. }
  110. ret = drm_atomic_helper_prepare_planes(dev, state);
  111. if (ret) {
  112. kfree(c);
  113. up(&vc4->async_modeset);
  114. return ret;
  115. }
  116. for (i = 0; i < dev->mode_config.num_total_plane; i++) {
  117. struct drm_plane *plane = state->planes[i];
  118. struct drm_plane_state *new_state = state->plane_states[i];
  119. if (!plane)
  120. continue;
  121. if ((plane->state->fb != new_state->fb) && new_state->fb) {
  122. struct drm_gem_cma_object *cma_bo =
  123. drm_fb_cma_get_gem_obj(new_state->fb, 0);
  124. struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  125. wait_seqno = max(bo->seqno, wait_seqno);
  126. }
  127. }
  128. /*
  129. * This is the point of no return - everything below never fails except
  130. * when the hw goes bonghits. Which means we can commit the new state on
  131. * the software side now.
  132. */
  133. drm_atomic_helper_swap_state(dev, state);
  134. /*
  135. * Everything below can be run asynchronously without the need to grab
  136. * any modeset locks at all under one condition: It must be guaranteed
  137. * that the asynchronous work has either been cancelled (if the driver
  138. * supports it, which at least requires that the framebuffers get
  139. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  140. * before the new state gets committed on the software side with
  141. * drm_atomic_helper_swap_state().
  142. *
  143. * This scheme allows new atomic state updates to be prepared and
  144. * checked in parallel to the asynchronous completion of the previous
  145. * update. Which is important since compositors need to figure out the
  146. * composition of the next frame right after having submitted the
  147. * current layout.
  148. */
  149. if (nonblock) {
  150. vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
  151. vc4_atomic_complete_commit_seqno_cb);
  152. } else {
  153. vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
  154. vc4_atomic_complete_commit(c);
  155. }
  156. return 0;
  157. }
  158. static const struct drm_mode_config_funcs vc4_mode_funcs = {
  159. .output_poll_changed = vc4_output_poll_changed,
  160. .atomic_check = drm_atomic_helper_check,
  161. .atomic_commit = vc4_atomic_commit,
  162. .fb_create = drm_fb_cma_create,
  163. };
  164. int vc4_kms_load(struct drm_device *dev)
  165. {
  166. struct vc4_dev *vc4 = to_vc4_dev(dev);
  167. int ret;
  168. sema_init(&vc4->async_modeset, 1);
  169. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  170. if (ret < 0) {
  171. dev_err(dev->dev, "failed to initialize vblank\n");
  172. return ret;
  173. }
  174. dev->mode_config.max_width = 2048;
  175. dev->mode_config.max_height = 2048;
  176. dev->mode_config.funcs = &vc4_mode_funcs;
  177. dev->mode_config.preferred_depth = 24;
  178. dev->mode_config.async_page_flip = true;
  179. drm_mode_config_reset(dev);
  180. vc4->fbdev = drm_fbdev_cma_init(dev, 32,
  181. dev->mode_config.num_crtc,
  182. dev->mode_config.num_connector);
  183. if (IS_ERR(vc4->fbdev))
  184. vc4->fbdev = NULL;
  185. drm_kms_helper_poll_init(dev);
  186. return 0;
  187. }