vc4_kms.c 5.2 KB

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