intel_frontbuffer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. static void intel_increase_pllclock(struct drm_device *dev,
  65. enum pipe pipe)
  66. {
  67. struct drm_i915_private *dev_priv = dev->dev_private;
  68. int dpll_reg = DPLL(pipe);
  69. int dpll;
  70. if (!HAS_GMCH_DISPLAY(dev))
  71. return;
  72. if (!dev_priv->lvds_downclock_avail)
  73. return;
  74. dpll = I915_READ(dpll_reg);
  75. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  76. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  77. assert_panel_unlocked(dev_priv, pipe);
  78. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  79. I915_WRITE(dpll_reg, dpll);
  80. intel_wait_for_vblank(dev, pipe);
  81. dpll = I915_READ(dpll_reg);
  82. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  83. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  84. }
  85. }
  86. /**
  87. * intel_mark_fb_busy - mark given planes as busy
  88. * @dev: DRM device
  89. * @frontbuffer_bits: bits for the affected planes
  90. * @ring: optional ring for asynchronous commands
  91. *
  92. * This function gets called every time the screen contents change. It can be
  93. * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
  94. */
  95. static void intel_mark_fb_busy(struct drm_device *dev,
  96. unsigned frontbuffer_bits,
  97. struct intel_engine_cs *ring)
  98. {
  99. struct drm_i915_private *dev_priv = dev->dev_private;
  100. enum pipe pipe;
  101. if (!i915.powersave)
  102. return;
  103. for_each_pipe(dev_priv, pipe) {
  104. if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
  105. continue;
  106. intel_increase_pllclock(dev, pipe);
  107. }
  108. }
  109. /**
  110. * intel_fb_obj_invalidate - invalidate frontbuffer object
  111. * @obj: GEM object to invalidate
  112. * @ring: set for asynchronous rendering
  113. * @origin: which operation caused the invalidation
  114. *
  115. * This function gets called every time rendering on the given object starts and
  116. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  117. * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
  118. * until the rendering completes or a flip on this frontbuffer plane is
  119. * scheduled.
  120. */
  121. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  122. struct intel_engine_cs *ring,
  123. enum fb_op_origin origin)
  124. {
  125. struct drm_device *dev = obj->base.dev;
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  128. if (!obj->frontbuffer_bits)
  129. return;
  130. if (ring) {
  131. mutex_lock(&dev_priv->fb_tracking.lock);
  132. dev_priv->fb_tracking.busy_bits
  133. |= obj->frontbuffer_bits;
  134. dev_priv->fb_tracking.flip_bits
  135. &= ~obj->frontbuffer_bits;
  136. mutex_unlock(&dev_priv->fb_tracking.lock);
  137. }
  138. intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
  139. intel_psr_invalidate(dev, obj->frontbuffer_bits);
  140. intel_edp_drrs_invalidate(dev, obj->frontbuffer_bits);
  141. intel_fbc_invalidate(dev_priv, obj->frontbuffer_bits, origin);
  142. }
  143. /**
  144. * intel_frontbuffer_flush - flush frontbuffer
  145. * @dev: DRM device
  146. * @frontbuffer_bits: frontbuffer plane tracking bits
  147. *
  148. * This function gets called every time rendering on the given planes has
  149. * completed and frontbuffer caching can be started again. Flushes will get
  150. * delayed if they're blocked by some outstanding asynchronous rendering.
  151. *
  152. * Can be called without any locks held.
  153. */
  154. void intel_frontbuffer_flush(struct drm_device *dev,
  155. unsigned frontbuffer_bits)
  156. {
  157. struct drm_i915_private *dev_priv = dev->dev_private;
  158. /* Delay flushing when rings are still busy.*/
  159. mutex_lock(&dev_priv->fb_tracking.lock);
  160. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  161. mutex_unlock(&dev_priv->fb_tracking.lock);
  162. intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
  163. intel_edp_drrs_flush(dev, frontbuffer_bits);
  164. intel_psr_flush(dev, frontbuffer_bits);
  165. intel_fbc_flush(dev_priv, frontbuffer_bits);
  166. }
  167. /**
  168. * intel_fb_obj_flush - flush frontbuffer object
  169. * @obj: GEM object to flush
  170. * @retire: set when retiring asynchronous rendering
  171. *
  172. * This function gets called every time rendering on the given object has
  173. * completed and frontbuffer caching can be started again. If @retire is true
  174. * then any delayed flushes will be unblocked.
  175. */
  176. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  177. bool retire)
  178. {
  179. struct drm_device *dev = obj->base.dev;
  180. struct drm_i915_private *dev_priv = dev->dev_private;
  181. unsigned frontbuffer_bits;
  182. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  183. if (!obj->frontbuffer_bits)
  184. return;
  185. frontbuffer_bits = obj->frontbuffer_bits;
  186. if (retire) {
  187. mutex_lock(&dev_priv->fb_tracking.lock);
  188. /* Filter out new bits since rendering started. */
  189. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  190. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  191. mutex_unlock(&dev_priv->fb_tracking.lock);
  192. }
  193. intel_frontbuffer_flush(dev, frontbuffer_bits);
  194. }
  195. /**
  196. * intel_frontbuffer_flip_prepare - prepare asynchronous 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. The actual
  201. * frontbuffer flushing will be delayed until completion is signalled with
  202. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  203. * flush will be cancelled.
  204. *
  205. * Can be called without any locks held.
  206. */
  207. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  208. unsigned frontbuffer_bits)
  209. {
  210. struct drm_i915_private *dev_priv = dev->dev_private;
  211. mutex_lock(&dev_priv->fb_tracking.lock);
  212. dev_priv->fb_tracking.flip_bits |= frontbuffer_bits;
  213. /* Remove stale busy bits due to the old buffer. */
  214. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  215. mutex_unlock(&dev_priv->fb_tracking.lock);
  216. }
  217. /**
  218. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
  219. * @dev: DRM device
  220. * @frontbuffer_bits: frontbuffer plane tracking bits
  221. *
  222. * This function gets called after the flip has been latched and will complete
  223. * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
  224. *
  225. * Can be called without any locks held.
  226. */
  227. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  228. unsigned frontbuffer_bits)
  229. {
  230. struct drm_i915_private *dev_priv = dev->dev_private;
  231. mutex_lock(&dev_priv->fb_tracking.lock);
  232. /* Mask any cancelled flips. */
  233. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  234. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  235. mutex_unlock(&dev_priv->fb_tracking.lock);
  236. intel_frontbuffer_flush(dev, frontbuffer_bits);
  237. }