intel_frontbuffer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Daniel Vetter <daniel.vetter@ffwll.ch>
  25. */
  26. /**
  27. * DOC: frontbuffer tracking
  28. *
  29. * Many features require us to track changes to the currently active
  30. * frontbuffer, especially rendering targeted at the frontbuffer.
  31. *
  32. * To be able to do so GEM tracks frontbuffers using a bitmask for all possible
  33. * frontbuffer slots through i915_gem_track_fb(). The function in this file are
  34. * then called when the contents of the frontbuffer are invalidated, when
  35. * frontbuffer rendering has stopped again to flush out all the changes and when
  36. * the frontbuffer is exchanged with a flip. Subsystems interested in
  37. * frontbuffer changes (e.g. PSR, FBC, DRRS) should directly put their callbacks
  38. * into the relevant places and filter for the frontbuffer slots that they are
  39. * interested int.
  40. *
  41. * On a high level there are two types of powersaving features. The first one
  42. * work like a special cache (FBC and PSR) and are interested when they should
  43. * stop caching and when to restart caching. This is done by placing callbacks
  44. * into the invalidate and the flush functions: At invalidate the caching must
  45. * be stopped and at flush time it can be restarted. And maybe they need to know
  46. * when the frontbuffer changes (e.g. when the hw doesn't initiate an invalidate
  47. * and flush on its own) which can be achieved with placing callbacks into the
  48. * flip functions.
  49. *
  50. * The other type of display power saving feature only cares about busyness
  51. * (e.g. DRRS). In that case all three (invalidate, flush and flip) indicate
  52. * busyness. There is no direct way to detect idleness. Instead an idle timer
  53. * work delayed work should be started from the flush and flip functions and
  54. * cancelled as soon as busyness is detected.
  55. *
  56. * Note that there's also an older frontbuffer activity tracking scheme which
  57. * just tracks general activity. This is done by the various mark_busy and
  58. * mark_idle functions. For display power management features using these
  59. * functions is deprecated and should be avoided.
  60. */
  61. #include <drm/drmP.h>
  62. #include "intel_drv.h"
  63. #include "i915_drv.h"
  64. /**
  65. * intel_fb_obj_invalidate - invalidate frontbuffer object
  66. * @obj: GEM object to invalidate
  67. * @origin: which operation caused the invalidation
  68. *
  69. * This function gets called every time rendering on the given object starts and
  70. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  71. * be invalidated. For ORIGIN_CS any subsequent invalidation will be delayed
  72. * until the rendering completes or a flip on this frontbuffer plane is
  73. * scheduled.
  74. */
  75. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  76. enum fb_op_origin origin)
  77. {
  78. struct drm_device *dev = obj->base.dev;
  79. struct drm_i915_private *dev_priv = to_i915(dev);
  80. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  81. if (!obj->frontbuffer_bits)
  82. return;
  83. if (origin == ORIGIN_CS) {
  84. mutex_lock(&dev_priv->fb_tracking.lock);
  85. dev_priv->fb_tracking.busy_bits
  86. |= obj->frontbuffer_bits;
  87. dev_priv->fb_tracking.flip_bits
  88. &= ~obj->frontbuffer_bits;
  89. mutex_unlock(&dev_priv->fb_tracking.lock);
  90. }
  91. intel_psr_invalidate(dev, obj->frontbuffer_bits);
  92. intel_edp_drrs_invalidate(dev, obj->frontbuffer_bits);
  93. intel_fbc_invalidate(dev_priv, obj->frontbuffer_bits, origin);
  94. }
  95. /**
  96. * intel_frontbuffer_flush - flush frontbuffer
  97. * @dev: DRM device
  98. * @frontbuffer_bits: frontbuffer plane tracking bits
  99. * @origin: which operation caused the flush
  100. *
  101. * This function gets called every time rendering on the given planes has
  102. * completed and frontbuffer caching can be started again. Flushes will get
  103. * delayed if they're blocked by some outstanding asynchronous rendering.
  104. *
  105. * Can be called without any locks held.
  106. */
  107. void intel_frontbuffer_flush(struct drm_device *dev,
  108. unsigned frontbuffer_bits,
  109. enum fb_op_origin origin)
  110. {
  111. struct drm_i915_private *dev_priv = to_i915(dev);
  112. /* Delay flushing when rings are still busy.*/
  113. mutex_lock(&dev_priv->fb_tracking.lock);
  114. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  115. mutex_unlock(&dev_priv->fb_tracking.lock);
  116. if (!frontbuffer_bits)
  117. return;
  118. intel_edp_drrs_flush(dev, frontbuffer_bits);
  119. intel_psr_flush(dev, frontbuffer_bits);
  120. intel_fbc_flush(dev_priv, frontbuffer_bits);
  121. }
  122. /**
  123. * intel_fb_obj_flush - flush frontbuffer object
  124. * @obj: GEM object to flush
  125. * @retire: set when retiring asynchronous rendering
  126. *
  127. * This function gets called every time rendering on the given object has
  128. * completed and frontbuffer caching can be started again. If @retire is true
  129. * then any delayed flushes will be unblocked.
  130. */
  131. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  132. bool retire, enum fb_op_origin origin)
  133. {
  134. struct drm_device *dev = obj->base.dev;
  135. struct drm_i915_private *dev_priv = to_i915(dev);
  136. unsigned frontbuffer_bits;
  137. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  138. if (!obj->frontbuffer_bits)
  139. return;
  140. frontbuffer_bits = obj->frontbuffer_bits;
  141. if (retire) {
  142. mutex_lock(&dev_priv->fb_tracking.lock);
  143. /* Filter out new bits since rendering started. */
  144. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  145. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  146. mutex_unlock(&dev_priv->fb_tracking.lock);
  147. }
  148. intel_frontbuffer_flush(dev, frontbuffer_bits, origin);
  149. }
  150. /**
  151. * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
  152. * @dev: DRM device
  153. * @frontbuffer_bits: frontbuffer plane tracking bits
  154. *
  155. * This function gets called after scheduling a flip on @obj. The actual
  156. * frontbuffer flushing will be delayed until completion is signalled with
  157. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  158. * flush will be cancelled.
  159. *
  160. * Can be called without any locks held.
  161. */
  162. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  163. unsigned frontbuffer_bits)
  164. {
  165. struct drm_i915_private *dev_priv = to_i915(dev);
  166. mutex_lock(&dev_priv->fb_tracking.lock);
  167. dev_priv->fb_tracking.flip_bits |= frontbuffer_bits;
  168. /* Remove stale busy bits due to the old buffer. */
  169. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  170. mutex_unlock(&dev_priv->fb_tracking.lock);
  171. intel_psr_single_frame_update(dev, frontbuffer_bits);
  172. }
  173. /**
  174. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
  175. * @dev: DRM device
  176. * @frontbuffer_bits: frontbuffer plane tracking bits
  177. *
  178. * This function gets called after the flip has been latched and will complete
  179. * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
  180. *
  181. * Can be called without any locks held.
  182. */
  183. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  184. unsigned frontbuffer_bits)
  185. {
  186. struct drm_i915_private *dev_priv = to_i915(dev);
  187. mutex_lock(&dev_priv->fb_tracking.lock);
  188. /* Mask any cancelled flips. */
  189. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  190. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  191. mutex_unlock(&dev_priv->fb_tracking.lock);
  192. intel_frontbuffer_flush(dev, frontbuffer_bits, ORIGIN_FLIP);
  193. }
  194. /**
  195. * intel_frontbuffer_flip - synchronous frontbuffer flip
  196. * @dev: DRM device
  197. * @frontbuffer_bits: frontbuffer plane tracking bits
  198. *
  199. * This function gets called after scheduling a flip on @obj. This is for
  200. * synchronous plane updates which will happen on the next vblank and which will
  201. * not get delayed by pending gpu rendering.
  202. *
  203. * Can be called without any locks held.
  204. */
  205. void intel_frontbuffer_flip(struct drm_device *dev,
  206. unsigned frontbuffer_bits)
  207. {
  208. struct drm_i915_private *dev_priv = to_i915(dev);
  209. mutex_lock(&dev_priv->fb_tracking.lock);
  210. /* Remove stale busy bits due to the old buffer. */
  211. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  212. mutex_unlock(&dev_priv->fb_tracking.lock);
  213. intel_frontbuffer_flush(dev, frontbuffer_bits, ORIGIN_FLIP);
  214. }