vc4_kms.c 6.2 KB

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