intel_frontbuffer.c 8.8 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. static 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, origin);
  120. intel_fbc_flush(dev_priv, frontbuffer_bits, origin);
  121. }
  122. /**
  123. * intel_fb_obj_flush - flush frontbuffer object
  124. * @obj: GEM object to flush
  125. * @retire: set when retiring asynchronous rendering
  126. * @origin: which operation caused the flush
  127. *
  128. * This function gets called every time rendering on the given object has
  129. * completed and frontbuffer caching can be started again. If @retire is true
  130. * then any delayed flushes will be unblocked.
  131. */
  132. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  133. bool retire, enum fb_op_origin origin)
  134. {
  135. struct drm_device *dev = obj->base.dev;
  136. struct drm_i915_private *dev_priv = to_i915(dev);
  137. unsigned frontbuffer_bits;
  138. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  139. if (!obj->frontbuffer_bits)
  140. return;
  141. frontbuffer_bits = obj->frontbuffer_bits;
  142. if (retire) {
  143. mutex_lock(&dev_priv->fb_tracking.lock);
  144. /* Filter out new bits since rendering started. */
  145. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  146. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  147. mutex_unlock(&dev_priv->fb_tracking.lock);
  148. }
  149. intel_frontbuffer_flush(dev, frontbuffer_bits, origin);
  150. }
  151. /**
  152. * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
  153. * @dev: DRM device
  154. * @frontbuffer_bits: frontbuffer plane tracking bits
  155. *
  156. * This function gets called after scheduling a flip on @obj. The actual
  157. * frontbuffer flushing will be delayed until completion is signalled with
  158. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  159. * flush will be cancelled.
  160. *
  161. * Can be called without any locks held.
  162. */
  163. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  164. unsigned frontbuffer_bits)
  165. {
  166. struct drm_i915_private *dev_priv = to_i915(dev);
  167. mutex_lock(&dev_priv->fb_tracking.lock);
  168. dev_priv->fb_tracking.flip_bits |= frontbuffer_bits;
  169. /* Remove stale busy bits due to the old buffer. */
  170. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  171. mutex_unlock(&dev_priv->fb_tracking.lock);
  172. intel_psr_single_frame_update(dev, frontbuffer_bits);
  173. }
  174. /**
  175. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
  176. * @dev: DRM device
  177. * @frontbuffer_bits: frontbuffer plane tracking bits
  178. *
  179. * This function gets called after the flip has been latched and will complete
  180. * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
  181. *
  182. * Can be called without any locks held.
  183. */
  184. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  185. unsigned frontbuffer_bits)
  186. {
  187. struct drm_i915_private *dev_priv = to_i915(dev);
  188. mutex_lock(&dev_priv->fb_tracking.lock);
  189. /* Mask any cancelled flips. */
  190. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  191. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  192. mutex_unlock(&dev_priv->fb_tracking.lock);
  193. intel_frontbuffer_flush(dev, frontbuffer_bits, ORIGIN_FLIP);
  194. }
  195. /**
  196. * intel_frontbuffer_flip - synchronous frontbuffer flip
  197. * @dev: DRM device
  198. * @frontbuffer_bits: frontbuffer plane tracking bits
  199. *
  200. * This function gets called after scheduling a flip on @obj. This is for
  201. * synchronous plane updates which will happen on the next vblank and which will
  202. * not get delayed by pending gpu rendering.
  203. *
  204. * Can be called without any locks held.
  205. */
  206. void intel_frontbuffer_flip(struct drm_device *dev,
  207. unsigned frontbuffer_bits)
  208. {
  209. struct drm_i915_private *dev_priv = to_i915(dev);
  210. mutex_lock(&dev_priv->fb_tracking.lock);
  211. /* Remove stale busy bits due to the old buffer. */
  212. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  213. mutex_unlock(&dev_priv->fb_tracking.lock);
  214. intel_frontbuffer_flush(dev, frontbuffer_bits, ORIGIN_FLIP);
  215. }