intel_frontbuffer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. for_each_pipe(dev_priv, pipe) {
  102. if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
  103. continue;
  104. intel_increase_pllclock(dev, pipe);
  105. }
  106. }
  107. /**
  108. * intel_fb_obj_invalidate - invalidate frontbuffer object
  109. * @obj: GEM object to invalidate
  110. * @ring: set for asynchronous rendering
  111. * @origin: which operation caused the invalidation
  112. *
  113. * This function gets called every time rendering on the given object starts and
  114. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  115. * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
  116. * until the rendering completes or a flip on this frontbuffer plane is
  117. * scheduled.
  118. */
  119. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  120. struct intel_engine_cs *ring,
  121. enum fb_op_origin origin)
  122. {
  123. struct drm_device *dev = obj->base.dev;
  124. struct drm_i915_private *dev_priv = dev->dev_private;
  125. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  126. if (!obj->frontbuffer_bits)
  127. return;
  128. if (ring) {
  129. mutex_lock(&dev_priv->fb_tracking.lock);
  130. dev_priv->fb_tracking.busy_bits
  131. |= obj->frontbuffer_bits;
  132. dev_priv->fb_tracking.flip_bits
  133. &= ~obj->frontbuffer_bits;
  134. mutex_unlock(&dev_priv->fb_tracking.lock);
  135. }
  136. intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
  137. intel_psr_invalidate(dev, obj->frontbuffer_bits);
  138. intel_edp_drrs_invalidate(dev, obj->frontbuffer_bits);
  139. intel_fbc_invalidate(dev_priv, obj->frontbuffer_bits, origin);
  140. }
  141. /**
  142. * intel_frontbuffer_flush - flush frontbuffer
  143. * @dev: DRM device
  144. * @frontbuffer_bits: frontbuffer plane tracking bits
  145. *
  146. * This function gets called every time rendering on the given planes has
  147. * completed and frontbuffer caching can be started again. Flushes will get
  148. * delayed if they're blocked by some outstanding asynchronous rendering.
  149. *
  150. * Can be called without any locks held.
  151. */
  152. void intel_frontbuffer_flush(struct drm_device *dev,
  153. unsigned frontbuffer_bits)
  154. {
  155. struct drm_i915_private *dev_priv = dev->dev_private;
  156. /* Delay flushing when rings are still busy.*/
  157. mutex_lock(&dev_priv->fb_tracking.lock);
  158. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  159. mutex_unlock(&dev_priv->fb_tracking.lock);
  160. intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
  161. intel_edp_drrs_flush(dev, frontbuffer_bits);
  162. intel_psr_flush(dev, frontbuffer_bits);
  163. intel_fbc_flush(dev_priv, frontbuffer_bits);
  164. }
  165. /**
  166. * intel_fb_obj_flush - flush frontbuffer object
  167. * @obj: GEM object to flush
  168. * @retire: set when retiring asynchronous rendering
  169. *
  170. * This function gets called every time rendering on the given object has
  171. * completed and frontbuffer caching can be started again. If @retire is true
  172. * then any delayed flushes will be unblocked.
  173. */
  174. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  175. bool retire)
  176. {
  177. struct drm_device *dev = obj->base.dev;
  178. struct drm_i915_private *dev_priv = dev->dev_private;
  179. unsigned frontbuffer_bits;
  180. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  181. if (!obj->frontbuffer_bits)
  182. return;
  183. frontbuffer_bits = obj->frontbuffer_bits;
  184. if (retire) {
  185. mutex_lock(&dev_priv->fb_tracking.lock);
  186. /* Filter out new bits since rendering started. */
  187. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  188. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  189. mutex_unlock(&dev_priv->fb_tracking.lock);
  190. }
  191. intel_frontbuffer_flush(dev, frontbuffer_bits);
  192. }
  193. /**
  194. * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
  195. * @dev: DRM device
  196. * @frontbuffer_bits: frontbuffer plane tracking bits
  197. *
  198. * This function gets called after scheduling a flip on @obj. The actual
  199. * frontbuffer flushing will be delayed until completion is signalled with
  200. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  201. * flush will be cancelled.
  202. *
  203. * Can be called without any locks held.
  204. */
  205. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  206. unsigned frontbuffer_bits)
  207. {
  208. struct drm_i915_private *dev_priv = dev->dev_private;
  209. mutex_lock(&dev_priv->fb_tracking.lock);
  210. dev_priv->fb_tracking.flip_bits |= frontbuffer_bits;
  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. }
  215. /**
  216. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
  217. * @dev: DRM device
  218. * @frontbuffer_bits: frontbuffer plane tracking bits
  219. *
  220. * This function gets called after the flip has been latched and will complete
  221. * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
  222. *
  223. * Can be called without any locks held.
  224. */
  225. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  226. unsigned frontbuffer_bits)
  227. {
  228. struct drm_i915_private *dev_priv = dev->dev_private;
  229. mutex_lock(&dev_priv->fb_tracking.lock);
  230. /* Mask any cancelled flips. */
  231. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  232. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  233. mutex_unlock(&dev_priv->fb_tracking.lock);
  234. intel_frontbuffer_flush(dev, frontbuffer_bits);
  235. }