vc4_kms.c 6.2 KB

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