vc4_kms.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/drm_crtc.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <drm/drm_fb_helper.h>
  21. #include <drm/drm_fb_cma_helper.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include "vc4_drv.h"
  24. static void
  25. vc4_atomic_complete_commit(struct drm_atomic_state *state)
  26. {
  27. struct drm_device *dev = state->dev;
  28. struct vc4_dev *vc4 = to_vc4_dev(dev);
  29. drm_atomic_helper_wait_for_fences(dev, state, false);
  30. drm_atomic_helper_wait_for_dependencies(state);
  31. drm_atomic_helper_commit_modeset_disables(dev, state);
  32. drm_atomic_helper_commit_planes(dev, state, 0);
  33. drm_atomic_helper_commit_modeset_enables(dev, state);
  34. /* Make sure that drm_atomic_helper_wait_for_vblanks()
  35. * actually waits for vblank. If we're doing a full atomic
  36. * modeset (as opposed to a vc4_update_plane() short circuit),
  37. * then we need to wait for scanout to be done with our
  38. * display lists before we free it and potentially reallocate
  39. * and overwrite the dlist memory with a new modeset.
  40. */
  41. state->legacy_cursor_update = false;
  42. drm_atomic_helper_commit_hw_done(state);
  43. drm_atomic_helper_wait_for_vblanks(dev, state);
  44. drm_atomic_helper_cleanup_planes(dev, state);
  45. drm_atomic_helper_commit_cleanup_done(state);
  46. drm_atomic_state_put(state);
  47. up(&vc4->async_modeset);
  48. }
  49. static void commit_work(struct work_struct *work)
  50. {
  51. struct drm_atomic_state *state = container_of(work,
  52. struct drm_atomic_state,
  53. commit_work);
  54. vc4_atomic_complete_commit(state);
  55. }
  56. /**
  57. * vc4_atomic_commit - commit validated state object
  58. * @dev: DRM device
  59. * @state: the driver state object
  60. * @nonblock: nonblocking commit
  61. *
  62. * This function commits a with drm_atomic_helper_check() pre-validated state
  63. * object. This can still fail when e.g. the framebuffer reservation fails. For
  64. * now this doesn't implement asynchronous commits.
  65. *
  66. * RETURNS
  67. * Zero for success or -errno.
  68. */
  69. static int vc4_atomic_commit(struct drm_device *dev,
  70. struct drm_atomic_state *state,
  71. bool nonblock)
  72. {
  73. struct vc4_dev *vc4 = to_vc4_dev(dev);
  74. int ret;
  75. ret = drm_atomic_helper_setup_commit(state, nonblock);
  76. if (ret)
  77. return ret;
  78. INIT_WORK(&state->commit_work, commit_work);
  79. ret = down_interruptible(&vc4->async_modeset);
  80. if (ret)
  81. return ret;
  82. ret = drm_atomic_helper_prepare_planes(dev, state);
  83. if (ret) {
  84. up(&vc4->async_modeset);
  85. return ret;
  86. }
  87. if (!nonblock) {
  88. ret = drm_atomic_helper_wait_for_fences(dev, state, true);
  89. if (ret) {
  90. drm_atomic_helper_cleanup_planes(dev, state);
  91. up(&vc4->async_modeset);
  92. return ret;
  93. }
  94. }
  95. /*
  96. * This is the point of no return - everything below never fails except
  97. * when the hw goes bonghits. Which means we can commit the new state on
  98. * the software side now.
  99. */
  100. BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
  101. /*
  102. * Everything below can be run asynchronously without the need to grab
  103. * any modeset locks at all under one condition: It must be guaranteed
  104. * that the asynchronous work has either been cancelled (if the driver
  105. * supports it, which at least requires that the framebuffers get
  106. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  107. * before the new state gets committed on the software side with
  108. * drm_atomic_helper_swap_state().
  109. *
  110. * This scheme allows new atomic state updates to be prepared and
  111. * checked in parallel to the asynchronous completion of the previous
  112. * update. Which is important since compositors need to figure out the
  113. * composition of the next frame right after having submitted the
  114. * current layout.
  115. */
  116. drm_atomic_state_get(state);
  117. if (nonblock)
  118. queue_work(system_unbound_wq, &state->commit_work);
  119. else
  120. vc4_atomic_complete_commit(state);
  121. return 0;
  122. }
  123. static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
  124. struct drm_file *file_priv,
  125. const struct drm_mode_fb_cmd2 *mode_cmd)
  126. {
  127. struct drm_mode_fb_cmd2 mode_cmd_local;
  128. /* If the user didn't specify a modifier, use the
  129. * vc4_set_tiling_ioctl() state for the BO.
  130. */
  131. if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
  132. struct drm_gem_object *gem_obj;
  133. struct vc4_bo *bo;
  134. gem_obj = drm_gem_object_lookup(file_priv,
  135. mode_cmd->handles[0]);
  136. if (!gem_obj) {
  137. DRM_DEBUG("Failed to look up GEM BO %d\n",
  138. mode_cmd->handles[0]);
  139. return ERR_PTR(-ENOENT);
  140. }
  141. bo = to_vc4_bo(gem_obj);
  142. mode_cmd_local = *mode_cmd;
  143. if (bo->t_format) {
  144. mode_cmd_local.modifier[0] =
  145. DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
  146. } else {
  147. mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
  148. }
  149. drm_gem_object_put_unlocked(gem_obj);
  150. mode_cmd = &mode_cmd_local;
  151. }
  152. return drm_gem_fb_create(dev, file_priv, mode_cmd);
  153. }
  154. static const struct drm_mode_config_funcs vc4_mode_funcs = {
  155. .output_poll_changed = drm_fb_helper_output_poll_changed,
  156. .atomic_check = drm_atomic_helper_check,
  157. .atomic_commit = vc4_atomic_commit,
  158. .fb_create = vc4_fb_create,
  159. };
  160. int vc4_kms_load(struct drm_device *dev)
  161. {
  162. struct vc4_dev *vc4 = to_vc4_dev(dev);
  163. int ret;
  164. sema_init(&vc4->async_modeset, 1);
  165. /* Set support for vblank irq fast disable, before drm_vblank_init() */
  166. dev->vblank_disable_immediate = true;
  167. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  168. if (ret < 0) {
  169. dev_err(dev->dev, "failed to initialize vblank\n");
  170. return ret;
  171. }
  172. dev->mode_config.max_width = 2048;
  173. dev->mode_config.max_height = 2048;
  174. dev->mode_config.funcs = &vc4_mode_funcs;
  175. dev->mode_config.preferred_depth = 24;
  176. dev->mode_config.async_page_flip = true;
  177. drm_mode_config_reset(dev);
  178. if (dev->mode_config.num_connector)
  179. drm_fb_cma_fbdev_init(dev, 32, 0);
  180. drm_kms_helper_poll_init(dev);
  181. return 0;
  182. }