blob: 1c26d65cdd33aa661cb3e524e07dbebf95314a2d [file] [log] [blame]
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001/*
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
Rodrigo Vivi94b83952014-12-08 06:46:31 -080024/**
25 * DOC: Frame Buffer Compression (FBC)
26 *
27 * FBC tries to save memory bandwidth (and so power consumption) by
28 * compressing the amount of memory used by the display. It is total
29 * transparent to user space and completely handled in the kernel.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020030 *
31 * The benefits of FBC are mostly visible with solid backgrounds and
Rodrigo Vivi94b83952014-12-08 06:46:31 -080032 * variation-less patterns. It comes from keeping the memory footprint small
33 * and having fewer memory pages opened and accessed for refreshing the display.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020034 *
Rodrigo Vivi94b83952014-12-08 06:46:31 -080035 * i915 is responsible to reserve stolen memory for FBC and configure its
36 * offset on proper registers. The hardware takes care of all
37 * compress/decompress. However there are many known cases where we have to
38 * forcibly disable it to allow proper screen updates.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020039 */
40
Rodrigo Vivi94b83952014-12-08 06:46:31 -080041#include "intel_drv.h"
42#include "i915_drv.h"
43
Paulo Zanoni9f218332015-09-23 12:52:27 -030044static inline bool fbc_supported(struct drm_i915_private *dev_priv)
45{
Paulo Zanoni0e631ad2015-10-14 17:45:36 -030046 return dev_priv->fbc.activate != NULL;
Paulo Zanoni9f218332015-09-23 12:52:27 -030047}
48
Paulo Zanoni57105022015-11-04 17:10:46 -020049static inline bool fbc_on_pipe_a_only(struct drm_i915_private *dev_priv)
50{
51 return IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8;
52}
53
Paulo Zanonie6cd6dc2015-10-16 17:55:40 -030054static inline bool fbc_on_plane_a_only(struct drm_i915_private *dev_priv)
55{
56 return INTEL_INFO(dev_priv)->gen < 4;
57}
58
Paulo Zanoni2db33662015-09-14 15:20:03 -030059/*
60 * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the
61 * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's
62 * origin so the x and y offsets can actually fit the registers. As a
63 * consequence, the fence doesn't really start exactly at the display plane
64 * address we program because it starts at the real start of the buffer, so we
65 * have to take this into consideration here.
66 */
67static unsigned int get_crtc_fence_y_offset(struct intel_crtc *crtc)
68{
69 return crtc->base.y - crtc->adjusted_y;
70}
71
Paulo Zanonic5ecd462015-10-15 14:19:21 -030072/*
73 * For SKL+, the plane source size used by the hardware is based on the value we
74 * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
75 * we wrote to PIPESRC.
76 */
Paulo Zanoniaaf78d22016-01-19 11:35:42 -020077static void intel_fbc_get_plane_source_size(struct intel_fbc_state_cache *cache,
Paulo Zanonic5ecd462015-10-15 14:19:21 -030078 int *width, int *height)
79{
Paulo Zanonic5ecd462015-10-15 14:19:21 -030080 int w, h;
81
Paulo Zanoniaaf78d22016-01-19 11:35:42 -020082 if (intel_rotation_90_or_270(cache->plane.rotation)) {
83 w = cache->plane.src_h;
84 h = cache->plane.src_w;
Paulo Zanonic5ecd462015-10-15 14:19:21 -030085 } else {
Paulo Zanoniaaf78d22016-01-19 11:35:42 -020086 w = cache->plane.src_w;
87 h = cache->plane.src_h;
Paulo Zanonic5ecd462015-10-15 14:19:21 -030088 }
89
90 if (width)
91 *width = w;
92 if (height)
93 *height = h;
94}
95
Paulo Zanoniaaf78d22016-01-19 11:35:42 -020096static int intel_fbc_calculate_cfb_size(struct drm_i915_private *dev_priv,
97 struct intel_fbc_state_cache *cache)
Paulo Zanonic5ecd462015-10-15 14:19:21 -030098{
Paulo Zanonic5ecd462015-10-15 14:19:21 -030099 int lines;
100
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200101 intel_fbc_get_plane_source_size(cache, NULL, &lines);
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300102 if (INTEL_INFO(dev_priv)->gen >= 7)
103 lines = min(lines, 2048);
104
105 /* Hardware needs the full buffer stride, not just the active area. */
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200106 return lines * cache->fb.stride;
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300107}
108
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300109static void i8xx_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200110{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200111 u32 fbc_ctl;
112
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300113 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200114
115 /* Disable compression */
116 fbc_ctl = I915_READ(FBC_CONTROL);
117 if ((fbc_ctl & FBC_CTL_EN) == 0)
118 return;
119
120 fbc_ctl &= ~FBC_CTL_EN;
121 I915_WRITE(FBC_CONTROL, fbc_ctl);
122
123 /* Wait for compressing bit to clear */
124 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
125 DRM_DEBUG_KMS("FBC idle timed out\n");
126 return;
127 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200128}
129
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200130static void i8xx_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200131{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200132 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200133 int cfb_pitch;
134 int i;
135 u32 fbc_ctl;
136
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300137 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200138
Jani Nikula60ee5cd2015-02-05 12:04:27 +0200139 /* Note: fbc.threshold == 1 for i8xx */
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200140 cfb_pitch = params->cfb_size / FBC_LL_SIZE;
141 if (params->fb.stride < cfb_pitch)
142 cfb_pitch = params->fb.stride;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200143
144 /* FBC_CTL wants 32B or 64B units */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300145 if (IS_GEN2(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200146 cfb_pitch = (cfb_pitch / 32) - 1;
147 else
148 cfb_pitch = (cfb_pitch / 64) - 1;
149
150 /* Clear old tags */
151 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
Ville Syrjälä4d110c72015-09-18 20:03:18 +0300152 I915_WRITE(FBC_TAG(i), 0);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200153
Paulo Zanoni7733b492015-07-07 15:26:04 -0300154 if (IS_GEN4(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200155 u32 fbc_ctl2;
156
157 /* Set it up... */
158 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200159 fbc_ctl2 |= FBC_CTL_PLANE(params->crtc.plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200160 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200161 I915_WRITE(FBC_FENCE_OFF, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200162 }
163
164 /* enable it... */
165 fbc_ctl = I915_READ(FBC_CONTROL);
166 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
167 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300168 if (IS_I945GM(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200169 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
170 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200171 fbc_ctl |= params->fb.fence_reg;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200172 I915_WRITE(FBC_CONTROL, fbc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200173}
174
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300175static bool i8xx_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200176{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200177 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
178}
179
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200180static void g4x_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200181{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200182 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200183 u32 dpfc_ctl;
184
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300185 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200186
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200187 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane) | DPFC_SR_EN;
188 if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200189 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
190 else
191 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200192 dpfc_ctl |= DPFC_CTL_FENCE_EN | params->fb.fence_reg;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200193
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200194 I915_WRITE(DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200195
196 /* enable it... */
197 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200198}
199
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300200static void g4x_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200201{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200202 u32 dpfc_ctl;
203
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300204 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200205
206 /* Disable compression */
207 dpfc_ctl = I915_READ(DPFC_CONTROL);
208 if (dpfc_ctl & DPFC_CTL_EN) {
209 dpfc_ctl &= ~DPFC_CTL_EN;
210 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200211 }
212}
213
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300214static bool g4x_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200215{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200216 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
217}
218
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200219/* This function forces a CFB recompression through the nuke operation. */
220static void intel_fbc_recompress(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200221{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200222 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
223 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200224}
225
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200226static void ilk_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200227{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200228 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200229 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300230 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200231
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300232 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200233
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200234 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane);
235 if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300236 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200237
Paulo Zanonice65e472015-06-30 10:53:05 -0300238 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200239 case 4:
240 case 3:
241 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
242 break;
243 case 2:
244 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
245 break;
246 case 1:
247 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
248 break;
249 }
250 dpfc_ctl |= DPFC_CTL_FENCE_EN;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300251 if (IS_GEN5(dev_priv))
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200252 dpfc_ctl |= params->fb.fence_reg;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200253
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200254 I915_WRITE(ILK_DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
255 I915_WRITE(ILK_FBC_RT_BASE, params->fb.ggtt_offset | ILK_FBC_RT_VALID);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200256 /* enable it... */
257 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
258
Paulo Zanoni7733b492015-07-07 15:26:04 -0300259 if (IS_GEN6(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200260 I915_WRITE(SNB_DPFC_CTL_SA,
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200261 SNB_CPU_FENCE_ENABLE | params->fb.fence_reg);
262 I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200263 }
264
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200265 intel_fbc_recompress(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200266}
267
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300268static void ilk_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200269{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200270 u32 dpfc_ctl;
271
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300272 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200273
274 /* Disable compression */
275 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
276 if (dpfc_ctl & DPFC_CTL_EN) {
277 dpfc_ctl &= ~DPFC_CTL_EN;
278 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200279 }
280}
281
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300282static bool ilk_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200283{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200284 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
285}
286
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200287static void gen7_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200288{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200289 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200290 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300291 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200292
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300293 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200294
Paulo Zanonid8514d62015-06-12 14:36:21 -0300295 dpfc_ctl = 0;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300296 if (IS_IVYBRIDGE(dev_priv))
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200297 dpfc_ctl |= IVB_DPFC_CTL_PLANE(params->crtc.plane);
Paulo Zanonid8514d62015-06-12 14:36:21 -0300298
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200299 if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300300 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200301
Paulo Zanonice65e472015-06-30 10:53:05 -0300302 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200303 case 4:
304 case 3:
305 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
306 break;
307 case 2:
308 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
309 break;
310 case 1:
311 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
312 break;
313 }
314
315 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
316
317 if (dev_priv->fbc.false_color)
318 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
319
Paulo Zanoni7733b492015-07-07 15:26:04 -0300320 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200321 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
322 I915_WRITE(ILK_DISPLAY_CHICKEN1,
323 I915_READ(ILK_DISPLAY_CHICKEN1) |
324 ILK_FBCQ_DIS);
Paulo Zanoni40f40222015-09-14 15:20:01 -0300325 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200326 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200327 I915_WRITE(CHICKEN_PIPESL_1(params->crtc.pipe),
328 I915_READ(CHICKEN_PIPESL_1(params->crtc.pipe)) |
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200329 HSW_FBCQ_DIS);
330 }
331
Paulo Zanoni57012be92015-09-14 15:20:00 -0300332 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
333
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200334 I915_WRITE(SNB_DPFC_CTL_SA,
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200335 SNB_CPU_FENCE_ENABLE | params->fb.fence_reg);
336 I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200337
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200338 intel_fbc_recompress(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200339}
340
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800341/**
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300342 * intel_fbc_is_active - Is FBC active?
Paulo Zanoni7733b492015-07-07 15:26:04 -0300343 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800344 *
345 * This function is used to verify the current state of FBC.
346 * FIXME: This should be tracked in the plane config eventually
347 * instead of queried at runtime for most callers.
348 */
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300349bool intel_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200350{
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300351 return dev_priv->fbc.active;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200352}
353
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200354static void intel_fbc_work_fn(struct work_struct *__work)
355{
Paulo Zanoni128d7352015-10-26 16:27:49 -0200356 struct drm_i915_private *dev_priv =
357 container_of(__work, struct drm_i915_private, fbc.work.work);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200358 struct intel_fbc *fbc = &dev_priv->fbc;
359 struct intel_fbc_work *work = &fbc->work;
360 struct intel_crtc *crtc = fbc->crtc;
Paulo Zanonica18d512016-01-21 18:03:05 -0200361 struct drm_vblank_crtc *vblank = &dev_priv->dev->vblank[crtc->pipe];
362
363 if (drm_crtc_vblank_get(&crtc->base)) {
364 DRM_ERROR("vblank not available for FBC on pipe %c\n",
365 pipe_name(crtc->pipe));
366
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200367 mutex_lock(&fbc->lock);
Paulo Zanonica18d512016-01-21 18:03:05 -0200368 work->scheduled = false;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200369 mutex_unlock(&fbc->lock);
Paulo Zanonica18d512016-01-21 18:03:05 -0200370 return;
371 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200372
Paulo Zanoni128d7352015-10-26 16:27:49 -0200373retry:
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200374 /* Delay the actual enabling to let pageflipping cease and the
375 * display to settle before starting the compression. Note that
376 * this delay also serves a second purpose: it allows for a
377 * vblank to pass after disabling the FBC before we attempt
378 * to modify the control registers.
379 *
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200380 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Paulo Zanonica18d512016-01-21 18:03:05 -0200381 *
382 * It is also worth mentioning that since work->scheduled_vblank can be
383 * updated multiple times by the other threads, hitting the timeout is
384 * not an error condition. We'll just end up hitting the "goto retry"
385 * case below.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200386 */
Paulo Zanonica18d512016-01-21 18:03:05 -0200387 wait_event_timeout(vblank->queue,
388 drm_crtc_vblank_count(&crtc->base) != work->scheduled_vblank,
389 msecs_to_jiffies(50));
Paulo Zanoni128d7352015-10-26 16:27:49 -0200390
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200391 mutex_lock(&fbc->lock);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200392
393 /* Were we cancelled? */
394 if (!work->scheduled)
395 goto out;
396
397 /* Were we delayed again while this function was sleeping? */
Paulo Zanonica18d512016-01-21 18:03:05 -0200398 if (drm_crtc_vblank_count(&crtc->base) == work->scheduled_vblank) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200399 mutex_unlock(&fbc->lock);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200400 goto retry;
401 }
402
403 if (crtc->base.primary->fb == work->fb)
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200404 fbc->activate(dev_priv);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200405
406 work->scheduled = false;
407
408out:
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200409 mutex_unlock(&fbc->lock);
Paulo Zanonica18d512016-01-21 18:03:05 -0200410 drm_crtc_vblank_put(&crtc->base);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200411}
412
413static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
414{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200415 struct intel_fbc *fbc = &dev_priv->fbc;
416
417 WARN_ON(!mutex_is_locked(&fbc->lock));
418 fbc->work.scheduled = false;
Paulo Zanoni128d7352015-10-26 16:27:49 -0200419}
420
421static void intel_fbc_schedule_activation(struct intel_crtc *crtc)
422{
423 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200424 struct intel_fbc *fbc = &dev_priv->fbc;
425 struct intel_fbc_work *work = &fbc->work;
Paulo Zanoni128d7352015-10-26 16:27:49 -0200426
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200427 WARN_ON(!mutex_is_locked(&fbc->lock));
Paulo Zanoni128d7352015-10-26 16:27:49 -0200428
Paulo Zanonica18d512016-01-21 18:03:05 -0200429 if (drm_crtc_vblank_get(&crtc->base)) {
430 DRM_ERROR("vblank not available for FBC on pipe %c\n",
431 pipe_name(crtc->pipe));
432 return;
433 }
434
Paulo Zanoni128d7352015-10-26 16:27:49 -0200435 /* It is useless to call intel_fbc_cancel_work() in this function since
436 * we're not releasing fbc.lock, so it won't have an opportunity to grab
437 * it to discover that it was cancelled. So we just update the expected
438 * jiffy count. */
439 work->fb = crtc->base.primary->fb;
440 work->scheduled = true;
Paulo Zanonica18d512016-01-21 18:03:05 -0200441 work->scheduled_vblank = drm_crtc_vblank_count(&crtc->base);
442 drm_crtc_vblank_put(&crtc->base);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200443
444 schedule_work(&work->work);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200445}
446
Paulo Zanonid029bca2015-10-15 10:44:46 -0300447static void __intel_fbc_deactivate(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300448{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200449 struct intel_fbc *fbc = &dev_priv->fbc;
450
451 WARN_ON(!mutex_is_locked(&fbc->lock));
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300452
453 intel_fbc_cancel_work(dev_priv);
454
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200455 if (fbc->active)
456 fbc->deactivate(dev_priv);
Paulo Zanoni754d1132015-10-13 19:13:25 -0300457}
458
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300459/*
Paulo Zanonid029bca2015-10-15 10:44:46 -0300460 * intel_fbc_deactivate - deactivate FBC if it's associated with crtc
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300461 * @crtc: the CRTC
462 *
Paulo Zanonid029bca2015-10-15 10:44:46 -0300463 * This function deactivates FBC if it's associated with the provided CRTC.
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300464 */
Paulo Zanonid029bca2015-10-15 10:44:46 -0300465void intel_fbc_deactivate(struct intel_crtc *crtc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300466{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300467 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200468 struct intel_fbc *fbc = &dev_priv->fbc;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200469
Paulo Zanoni9f218332015-09-23 12:52:27 -0300470 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300471 return;
472
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200473 mutex_lock(&fbc->lock);
474 if (fbc->crtc == crtc)
Paulo Zanonid029bca2015-10-15 10:44:46 -0300475 __intel_fbc_deactivate(dev_priv);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200476 mutex_unlock(&fbc->lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200477}
478
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300479static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200480 const char *reason)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200481{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200482 struct intel_fbc *fbc = &dev_priv->fbc;
483
484 if (fbc->no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300485 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200486
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200487 fbc->no_fbc_reason = reason;
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200488 DRM_DEBUG_KMS("Disabling FBC: %s\n", reason);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200489}
490
Paulo Zanonid029bca2015-10-15 10:44:46 -0300491static bool crtc_can_fbc(struct intel_crtc *crtc)
Paulo Zanoni30c58d52015-11-04 17:10:48 -0200492{
493 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
494
495 if (fbc_on_pipe_a_only(dev_priv) && crtc->pipe != PIPE_A)
496 return false;
497
Paulo Zanonie6cd6dc2015-10-16 17:55:40 -0300498 if (fbc_on_plane_a_only(dev_priv) && crtc->plane != PLANE_A)
499 return false;
500
Paulo Zanonid029bca2015-10-15 10:44:46 -0300501 return true;
502}
503
Paulo Zanoni232fd932015-07-07 15:26:07 -0300504static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
505{
506 enum pipe pipe;
507 int n_pipes = 0;
508 struct drm_crtc *crtc;
509
510 if (INTEL_INFO(dev_priv)->gen > 4)
511 return true;
512
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200513 /* FIXME: we don't have the appropriate state locks to do this here. */
Paulo Zanoni232fd932015-07-07 15:26:07 -0300514 for_each_pipe(dev_priv, pipe) {
515 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
516
517 if (intel_crtc_active(crtc) &&
518 to_intel_plane_state(crtc->primary->state)->visible)
519 n_pipes++;
520 }
521
522 return (n_pipes < 2);
523}
524
Paulo Zanoni7733b492015-07-07 15:26:04 -0300525static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300526 struct drm_mm_node *node,
527 int size,
528 int fb_cpp)
529{
Paulo Zanonifc786722015-07-02 19:25:08 -0300530 int compression_threshold = 1;
531 int ret;
Paulo Zanonia9da5122015-09-14 15:19:57 -0300532 u64 end;
533
534 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
535 * reserved range size, so it always assumes the maximum (8mb) is used.
536 * If we enable FBC using a CFB on that memory range we'll get FIFO
537 * underruns, even if that range is not reserved by the BIOS. */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700538 if (IS_BROADWELL(dev_priv) ||
539 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Paulo Zanonia9da5122015-09-14 15:19:57 -0300540 end = dev_priv->gtt.stolen_size - 8 * 1024 * 1024;
541 else
542 end = dev_priv->gtt.stolen_usable_size;
Paulo Zanonifc786722015-07-02 19:25:08 -0300543
544 /* HACK: This code depends on what we will do in *_enable_fbc. If that
545 * code changes, this code needs to change as well.
546 *
547 * The enable_fbc code will attempt to use one of our 2 compression
548 * thresholds, therefore, in that case, we only have 1 resort.
549 */
550
551 /* Try to over-allocate to reduce reallocations and fragmentation. */
Paulo Zanonia9da5122015-09-14 15:19:57 -0300552 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
553 4096, 0, end);
Paulo Zanonifc786722015-07-02 19:25:08 -0300554 if (ret == 0)
555 return compression_threshold;
556
557again:
558 /* HW's ability to limit the CFB is 1:4 */
559 if (compression_threshold > 4 ||
560 (fb_cpp == 2 && compression_threshold == 2))
561 return 0;
562
Paulo Zanonia9da5122015-09-14 15:19:57 -0300563 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
564 4096, 0, end);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300565 if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300566 return 0;
567 } else if (ret) {
568 compression_threshold <<= 1;
569 goto again;
570 } else {
571 return compression_threshold;
572 }
573}
574
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300575static int intel_fbc_alloc_cfb(struct intel_crtc *crtc)
Paulo Zanonifc786722015-07-02 19:25:08 -0300576{
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300577 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200578 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonifc786722015-07-02 19:25:08 -0300579 struct drm_mm_node *uninitialized_var(compressed_llb);
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300580 int size, fb_cpp, ret;
581
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200582 WARN_ON(drm_mm_node_allocated(&fbc->compressed_fb));
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300583
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200584 size = intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache);
585 fb_cpp = drm_format_plane_cpp(fbc->state_cache.fb.pixel_format, 0);
Paulo Zanonifc786722015-07-02 19:25:08 -0300586
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200587 ret = find_compression_threshold(dev_priv, &fbc->compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300588 size, fb_cpp);
589 if (!ret)
590 goto err_llb;
591 else if (ret > 1) {
592 DRM_INFO("Reducing the compressed framebuffer size. This may lead to less power savings than a non-reduced-size. Try to increase stolen memory size if available in BIOS.\n");
593
594 }
595
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200596 fbc->threshold = ret;
Paulo Zanonifc786722015-07-02 19:25:08 -0300597
598 if (INTEL_INFO(dev_priv)->gen >= 5)
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200599 I915_WRITE(ILK_DPFC_CB_BASE, fbc->compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300600 else if (IS_GM45(dev_priv)) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200601 I915_WRITE(DPFC_CB_BASE, fbc->compressed_fb.start);
Paulo Zanonifc786722015-07-02 19:25:08 -0300602 } else {
603 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
604 if (!compressed_llb)
605 goto err_fb;
606
607 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
608 4096, 4096);
609 if (ret)
610 goto err_fb;
611
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200612 fbc->compressed_llb = compressed_llb;
Paulo Zanonifc786722015-07-02 19:25:08 -0300613
614 I915_WRITE(FBC_CFB_BASE,
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200615 dev_priv->mm.stolen_base + fbc->compressed_fb.start);
Paulo Zanonifc786722015-07-02 19:25:08 -0300616 I915_WRITE(FBC_LL_BASE,
617 dev_priv->mm.stolen_base + compressed_llb->start);
618 }
619
Paulo Zanonib8bf5d72015-09-14 15:19:58 -0300620 DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200621 fbc->compressed_fb.size, fbc->threshold);
Paulo Zanonifc786722015-07-02 19:25:08 -0300622
623 return 0;
624
625err_fb:
626 kfree(compressed_llb);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200627 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
Paulo Zanonifc786722015-07-02 19:25:08 -0300628err_llb:
629 pr_info_once("drm: not enough stolen space for compressed buffer (need %d more bytes), disabling. Hint: you may be able to increase stolen memory size in the BIOS to avoid this.\n", size);
630 return -ENOSPC;
631}
632
Paulo Zanoni7733b492015-07-07 15:26:04 -0300633static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300634{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200635 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonifc786722015-07-02 19:25:08 -0300636
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200637 if (drm_mm_node_allocated(&fbc->compressed_fb))
638 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
639
640 if (fbc->compressed_llb) {
641 i915_gem_stolen_remove_node(dev_priv, fbc->compressed_llb);
642 kfree(fbc->compressed_llb);
Paulo Zanonifc786722015-07-02 19:25:08 -0300643 }
Paulo Zanonifc786722015-07-02 19:25:08 -0300644}
645
Paulo Zanoni7733b492015-07-07 15:26:04 -0300646void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300647{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200648 struct intel_fbc *fbc = &dev_priv->fbc;
649
Paulo Zanoni9f218332015-09-23 12:52:27 -0300650 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300651 return;
652
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200653 mutex_lock(&fbc->lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300654 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200655 mutex_unlock(&fbc->lock);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300656}
657
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300658static bool stride_is_valid(struct drm_i915_private *dev_priv,
659 unsigned int stride)
660{
661 /* These should have been caught earlier. */
662 WARN_ON(stride < 512);
663 WARN_ON((stride & (64 - 1)) != 0);
664
665 /* Below are the additional FBC restrictions. */
666
667 if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
668 return stride == 4096 || stride == 8192;
669
670 if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
671 return false;
672
673 if (stride > 16384)
674 return false;
675
676 return true;
677}
678
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200679static bool pixel_format_is_valid(struct drm_i915_private *dev_priv,
680 uint32_t pixel_format)
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300681{
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200682 switch (pixel_format) {
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300683 case DRM_FORMAT_XRGB8888:
684 case DRM_FORMAT_XBGR8888:
685 return true;
686 case DRM_FORMAT_XRGB1555:
687 case DRM_FORMAT_RGB565:
688 /* 16bpp not supported on gen2 */
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200689 if (IS_GEN2(dev_priv))
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300690 return false;
691 /* WaFbcOnly1to1Ratio:ctg */
692 if (IS_G4X(dev_priv))
693 return false;
694 return true;
695 default:
696 return false;
697 }
698}
699
Paulo Zanoni856312a2015-10-01 19:57:12 -0300700/*
701 * For some reason, the hardware tracking starts looking at whatever we
702 * programmed as the display plane base address register. It does not look at
703 * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
704 * variables instead of just looking at the pipe/plane size.
705 */
706static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300707{
708 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200709 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanoni856312a2015-10-01 19:57:12 -0300710 unsigned int effective_w, effective_h, max_w, max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300711
712 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
713 max_w = 4096;
714 max_h = 4096;
715 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
716 max_w = 4096;
717 max_h = 2048;
718 } else {
719 max_w = 2048;
720 max_h = 1536;
721 }
722
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200723 intel_fbc_get_plane_source_size(&fbc->state_cache, &effective_w,
724 &effective_h);
Paulo Zanoni856312a2015-10-01 19:57:12 -0300725 effective_w += crtc->adjusted_x;
726 effective_h += crtc->adjusted_y;
727
728 return effective_w <= max_w && effective_h <= max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300729}
730
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200731static void intel_fbc_update_state_cache(struct intel_crtc *crtc)
732{
733 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
734 struct intel_fbc *fbc = &dev_priv->fbc;
735 struct intel_fbc_state_cache *cache = &fbc->state_cache;
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200736 struct intel_crtc_state *crtc_state =
737 to_intel_crtc_state(crtc->base.state);
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200738 struct intel_plane_state *plane_state =
739 to_intel_plane_state(crtc->base.primary->state);
740 struct drm_framebuffer *fb = plane_state->base.fb;
741 struct drm_i915_gem_object *obj;
742
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200743 WARN_ON(!drm_modeset_is_locked(&crtc->base.mutex));
744 WARN_ON(!drm_modeset_is_locked(&crtc->base.primary->mutex));
745
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200746 cache->crtc.mode_flags = crtc_state->base.adjusted_mode.flags;
747 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
748 cache->crtc.hsw_bdw_pixel_rate =
749 ilk_pipe_pixel_rate(crtc_state);
750
751 cache->plane.rotation = plane_state->base.rotation;
752 cache->plane.src_w = drm_rect_width(&plane_state->src) >> 16;
753 cache->plane.src_h = drm_rect_height(&plane_state->src) >> 16;
754 cache->plane.visible = plane_state->visible;
755
756 if (!cache->plane.visible)
757 return;
758
759 obj = intel_fb_obj(fb);
760
761 /* FIXME: We lack the proper locking here, so only run this on the
762 * platforms that need. */
763 if (dev_priv->fbc.activate == ilk_fbc_activate)
764 cache->fb.ilk_ggtt_offset = i915_gem_obj_ggtt_offset(obj);
765 cache->fb.id = fb->base.id;
766 cache->fb.pixel_format = fb->pixel_format;
767 cache->fb.stride = fb->pitches[0];
768 cache->fb.fence_reg = obj->fence_reg;
769 cache->fb.tiling_mode = obj->tiling_mode;
770}
771
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200772static bool intel_fbc_can_activate(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200773{
Paulo Zanoni754d1132015-10-13 19:13:25 -0300774 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200775 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200776 struct intel_fbc_state_cache *cache = &fbc->state_cache;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200777
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200778 if (!cache->plane.visible) {
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200779 set_no_fbc_reason(dev_priv, "primary plane not visible");
780 return false;
781 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200782
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200783 if ((cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) ||
784 (cache->crtc.mode_flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200785 set_no_fbc_reason(dev_priv, "incompatible mode");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200786 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200787 }
788
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200789 if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200790 set_no_fbc_reason(dev_priv, "mode too large for compression");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200791 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200792 }
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300793
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200794 /* The use of a CPU fence is mandatory in order to detect writes
795 * by the CPU to the scanout and trigger updates to the FBC.
796 */
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200797 if (cache->fb.tiling_mode != I915_TILING_X ||
798 cache->fb.fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200799 set_no_fbc_reason(dev_priv, "framebuffer not tiled or fenced");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200800 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200801 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300802 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200803 cache->plane.rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200804 set_no_fbc_reason(dev_priv, "rotation unsupported");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200805 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200806 }
807
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200808 if (!stride_is_valid(dev_priv, cache->fb.stride)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200809 set_no_fbc_reason(dev_priv, "framebuffer stride not supported");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200810 return false;
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300811 }
812
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200813 if (!pixel_format_is_valid(dev_priv, cache->fb.pixel_format)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200814 set_no_fbc_reason(dev_priv, "pixel format is invalid");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200815 return false;
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300816 }
817
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300818 /* WaFbcExceedCdClockThreshold:hsw,bdw */
819 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200820 cache->crtc.hsw_bdw_pixel_rate >= dev_priv->cdclk_freq * 95 / 100) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200821 set_no_fbc_reason(dev_priv, "pixel rate is too big");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200822 return false;
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300823 }
824
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300825 /* It is possible for the required CFB size change without a
826 * crtc->disable + crtc->enable since it is possible to change the
827 * stride without triggering a full modeset. Since we try to
828 * over-allocate the CFB, there's a chance we may keep FBC enabled even
829 * if this happens, but if we exceed the current CFB size we'll have to
830 * disable FBC. Notice that it would be possible to disable FBC, wait
831 * for a frame, free the stolen node, then try to reenable FBC in case
832 * we didn't get any invalidate/deactivate calls, but this would require
833 * a lot of tracking just for a specific case. If we conclude it's an
834 * important case, we can implement it later. */
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200835 if (intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache) >
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200836 fbc->compressed_fb.size * fbc->threshold) {
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300837 set_no_fbc_reason(dev_priv, "CFB requirements changed");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200838 return false;
839 }
840
841 return true;
842}
843
Paulo Zanoni44a8a252016-01-19 11:35:36 -0200844static bool intel_fbc_can_enable(struct intel_crtc *crtc)
845{
846 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
847
848 if (intel_vgpu_active(dev_priv->dev)) {
849 set_no_fbc_reason(dev_priv, "VGPU is active");
850 return false;
851 }
852
853 if (i915.enable_fbc < 0) {
854 set_no_fbc_reason(dev_priv, "disabled per chip default");
855 return false;
856 }
857
858 if (!i915.enable_fbc) {
859 set_no_fbc_reason(dev_priv, "disabled per module param");
860 return false;
861 }
862
863 if (!crtc_can_fbc(crtc)) {
864 set_no_fbc_reason(dev_priv, "no enabled pipes can have FBC");
865 return false;
866 }
867
868 return true;
869}
870
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200871static void intel_fbc_get_reg_params(struct intel_crtc *crtc,
872 struct intel_fbc_reg_params *params)
873{
874 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200875 struct intel_fbc *fbc = &dev_priv->fbc;
876 struct intel_fbc_state_cache *cache = &fbc->state_cache;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200877
878 /* Since all our fields are integer types, use memset here so the
879 * comparison function can rely on memcmp because the padding will be
880 * zero. */
881 memset(params, 0, sizeof(*params));
882
883 params->crtc.pipe = crtc->pipe;
884 params->crtc.plane = crtc->plane;
885 params->crtc.fence_y_offset = get_crtc_fence_y_offset(crtc);
886
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200887 params->fb.id = cache->fb.id;
888 params->fb.pixel_format = cache->fb.pixel_format;
889 params->fb.stride = cache->fb.stride;
890 params->fb.fence_reg = cache->fb.fence_reg;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200891
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200892 params->cfb_size = intel_fbc_calculate_cfb_size(dev_priv, cache);
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200893
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200894 params->fb.ggtt_offset = cache->fb.ilk_ggtt_offset;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200895}
896
897static bool intel_fbc_reg_params_equal(struct intel_fbc_reg_params *params1,
898 struct intel_fbc_reg_params *params2)
899{
900 /* We can use this since intel_fbc_get_reg_params() does a memset. */
901 return memcmp(params1, params2, sizeof(*params1)) == 0;
902}
903
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200904void intel_fbc_pre_update(struct intel_crtc *crtc)
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200905{
906 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200907 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200908
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200909 if (!fbc_supported(dev_priv))
910 return;
911
912 mutex_lock(&fbc->lock);
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200913
914 if (!multiple_pipes_ok(dev_priv)) {
915 set_no_fbc_reason(dev_priv, "more than one pipe active");
Paulo Zanoni212890c2016-01-19 11:35:43 -0200916 goto deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200917 }
918
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200919 if (!fbc->enabled || fbc->crtc != crtc)
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200920 goto unlock;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200921
Paulo Zanoniaaf78d22016-01-19 11:35:42 -0200922 intel_fbc_update_state_cache(crtc);
923
Paulo Zanoni212890c2016-01-19 11:35:43 -0200924deactivate:
925 __intel_fbc_deactivate(dev_priv);
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200926unlock:
927 mutex_unlock(&fbc->lock);
Paulo Zanoni212890c2016-01-19 11:35:43 -0200928}
929
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200930static void __intel_fbc_post_update(struct intel_crtc *crtc)
Paulo Zanoni212890c2016-01-19 11:35:43 -0200931{
932 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
933 struct intel_fbc *fbc = &dev_priv->fbc;
934 struct intel_fbc_reg_params old_params;
935
936 WARN_ON(!mutex_is_locked(&fbc->lock));
937
938 if (!fbc->enabled || fbc->crtc != crtc)
939 return;
940
941 if (!intel_fbc_can_activate(crtc)) {
942 WARN_ON(fbc->active);
943 return;
944 }
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200945
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200946 old_params = fbc->params;
947 intel_fbc_get_reg_params(crtc, &fbc->params);
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200948
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200949 /* If the scanout has not changed, don't modify the FBC settings.
950 * Note that we make the fundamental assumption that the fb->obj
951 * cannot be unpinned (and have its GTT offset and fence revoked)
952 * without first being decoupled from the scanout and FBC disabled.
953 */
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200954 if (fbc->active &&
955 intel_fbc_reg_params_equal(&old_params, &fbc->params))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200956 return;
957
Paulo Zanoni212890c2016-01-19 11:35:43 -0200958 __intel_fbc_deactivate(dev_priv);
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300959 intel_fbc_schedule_activation(crtc);
Paulo Zanoni212890c2016-01-19 11:35:43 -0200960 fbc->no_fbc_reason = "FBC enabled (active or scheduled)";
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300961}
962
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200963void intel_fbc_post_update(struct intel_crtc *crtc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300964{
Paulo Zanoni754d1132015-10-13 19:13:25 -0300965 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200966 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanoni754d1132015-10-13 19:13:25 -0300967
Paulo Zanoni9f218332015-09-23 12:52:27 -0300968 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300969 return;
970
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200971 mutex_lock(&fbc->lock);
Paulo Zanoni1eb52232016-01-19 11:35:44 -0200972 __intel_fbc_post_update(crtc);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200973 mutex_unlock(&fbc->lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200974}
975
Paulo Zanoni261fe992016-01-19 11:35:40 -0200976static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
977{
978 if (fbc->enabled)
979 return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit;
980 else
981 return fbc->possible_framebuffer_bits;
982}
983
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200984void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
985 unsigned int frontbuffer_bits,
986 enum fb_op_origin origin)
987{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200988 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200989
Paulo Zanoni9f218332015-09-23 12:52:27 -0300990 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300991 return;
992
Paulo Zanoni0dd81542016-01-19 11:35:39 -0200993 if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200994 return;
995
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200996 mutex_lock(&fbc->lock);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300997
Paulo Zanoni261fe992016-01-19 11:35:40 -0200998 fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits;
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200999
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001000 if (fbc->busy_bits)
Paulo Zanonid029bca2015-10-15 10:44:46 -03001001 __intel_fbc_deactivate(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001002
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001003 mutex_unlock(&fbc->lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001004}
1005
1006void intel_fbc_flush(struct drm_i915_private *dev_priv,
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001007 unsigned int frontbuffer_bits, enum fb_op_origin origin)
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001008{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001009 struct intel_fbc *fbc = &dev_priv->fbc;
1010
Paulo Zanoni9f218332015-09-23 12:52:27 -03001011 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -03001012 return;
1013
Paulo Zanoni0dd81542016-01-19 11:35:39 -02001014 if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001015 return;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001016
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001017 mutex_lock(&fbc->lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001018
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001019 fbc->busy_bits &= ~frontbuffer_bits;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001020
Paulo Zanoni261fe992016-01-19 11:35:40 -02001021 if (!fbc->busy_bits && fbc->enabled &&
1022 (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) {
Paulo Zanoni0dd81542016-01-19 11:35:39 -02001023 if (fbc->active)
Paulo Zanoniee7d6cfa2015-11-11 14:46:22 -02001024 intel_fbc_recompress(dev_priv);
Paulo Zanoni0dd81542016-01-19 11:35:39 -02001025 else
Paulo Zanoni1eb52232016-01-19 11:35:44 -02001026 __intel_fbc_post_update(fbc->crtc);
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001027 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001028
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001029 mutex_unlock(&fbc->lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001030}
1031
Rodrigo Vivi94b83952014-12-08 06:46:31 -08001032/**
Paulo Zanonid029bca2015-10-15 10:44:46 -03001033 * intel_fbc_enable: tries to enable FBC on the CRTC
1034 * @crtc: the CRTC
1035 *
1036 * This function checks if it's possible to enable FBC on the following CRTC,
1037 * then enables it. Notice that it doesn't activate FBC.
1038 */
1039void intel_fbc_enable(struct intel_crtc *crtc)
1040{
1041 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001042 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001043
1044 if (!fbc_supported(dev_priv))
1045 return;
1046
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001047 mutex_lock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001048
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001049 if (fbc->enabled) {
1050 WARN_ON(fbc->crtc == crtc);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001051 goto out;
1052 }
1053
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001054 WARN_ON(fbc->active);
1055 WARN_ON(fbc->crtc != NULL);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001056
Paulo Zanoni44a8a252016-01-19 11:35:36 -02001057 if (!intel_fbc_can_enable(crtc))
Paulo Zanonid029bca2015-10-15 10:44:46 -03001058 goto out;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001059
Paulo Zanoniaaf78d22016-01-19 11:35:42 -02001060 intel_fbc_update_state_cache(crtc);
Paulo Zanonic5ecd462015-10-15 14:19:21 -03001061 if (intel_fbc_alloc_cfb(crtc)) {
1062 set_no_fbc_reason(dev_priv, "not enough stolen memory");
1063 goto out;
1064 }
1065
Paulo Zanonid029bca2015-10-15 10:44:46 -03001066 DRM_DEBUG_KMS("Enabling FBC on pipe %c\n", pipe_name(crtc->pipe));
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001067 fbc->no_fbc_reason = "FBC enabled but not active yet\n";
Paulo Zanonid029bca2015-10-15 10:44:46 -03001068
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001069 fbc->enabled = true;
1070 fbc->crtc = crtc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001071out:
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001072 mutex_unlock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001073}
1074
1075/**
1076 * __intel_fbc_disable - disable FBC
1077 * @dev_priv: i915 device instance
1078 *
1079 * This is the low level function that actually disables FBC. Callers should
1080 * grab the FBC lock.
1081 */
1082static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
1083{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001084 struct intel_fbc *fbc = &dev_priv->fbc;
1085 struct intel_crtc *crtc = fbc->crtc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001086
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001087 WARN_ON(!mutex_is_locked(&fbc->lock));
1088 WARN_ON(!fbc->enabled);
1089 WARN_ON(fbc->active);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001090 assert_pipe_disabled(dev_priv, crtc->pipe);
1091
1092 DRM_DEBUG_KMS("Disabling FBC on pipe %c\n", pipe_name(crtc->pipe));
1093
Paulo Zanonic5ecd462015-10-15 14:19:21 -03001094 __intel_fbc_cleanup_cfb(dev_priv);
1095
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001096 fbc->enabled = false;
1097 fbc->crtc = NULL;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001098}
1099
1100/**
1101 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
1102 * @crtc: the CRTC
1103 *
1104 * This function disables FBC if it's associated with the provided CRTC.
1105 */
1106void intel_fbc_disable_crtc(struct intel_crtc *crtc)
1107{
1108 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001109 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001110
1111 if (!fbc_supported(dev_priv))
1112 return;
1113
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001114 mutex_lock(&fbc->lock);
1115 if (fbc->crtc == crtc) {
1116 WARN_ON(!fbc->enabled);
1117 WARN_ON(fbc->active);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001118 __intel_fbc_disable(dev_priv);
1119 }
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001120 mutex_unlock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001121}
1122
1123/**
1124 * intel_fbc_disable - globally disable FBC
1125 * @dev_priv: i915 device instance
1126 *
1127 * This function disables FBC regardless of which CRTC is associated with it.
1128 */
1129void intel_fbc_disable(struct drm_i915_private *dev_priv)
1130{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001131 struct intel_fbc *fbc = &dev_priv->fbc;
1132
Paulo Zanonid029bca2015-10-15 10:44:46 -03001133 if (!fbc_supported(dev_priv))
1134 return;
1135
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001136 mutex_lock(&fbc->lock);
1137 if (fbc->enabled)
Paulo Zanonid029bca2015-10-15 10:44:46 -03001138 __intel_fbc_disable(dev_priv);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001139 mutex_unlock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001140}
1141
1142/**
Rodrigo Vivi94b83952014-12-08 06:46:31 -08001143 * intel_fbc_init - Initialize FBC
1144 * @dev_priv: the i915 device
1145 *
1146 * This function might be called during PM init process.
1147 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001148void intel_fbc_init(struct drm_i915_private *dev_priv)
1149{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001150 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001151 enum pipe pipe;
1152
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001153 INIT_WORK(&fbc->work.work, intel_fbc_work_fn);
1154 mutex_init(&fbc->lock);
1155 fbc->enabled = false;
1156 fbc->active = false;
1157 fbc->work.scheduled = false;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001158
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001159 if (!HAS_FBC(dev_priv)) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001160 fbc->no_fbc_reason = "unsupported by this chipset";
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001161 return;
1162 }
1163
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001164 for_each_pipe(dev_priv, pipe) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001165 fbc->possible_framebuffer_bits |=
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001166 INTEL_FRONTBUFFER_PRIMARY(pipe);
1167
Paulo Zanoni57105022015-11-04 17:10:46 -02001168 if (fbc_on_pipe_a_only(dev_priv))
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001169 break;
1170 }
1171
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001172 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001173 fbc->is_active = ilk_fbc_is_active;
1174 fbc->activate = gen7_fbc_activate;
1175 fbc->deactivate = ilk_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001176 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001177 fbc->is_active = ilk_fbc_is_active;
1178 fbc->activate = ilk_fbc_activate;
1179 fbc->deactivate = ilk_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001180 } else if (IS_GM45(dev_priv)) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001181 fbc->is_active = g4x_fbc_is_active;
1182 fbc->activate = g4x_fbc_activate;
1183 fbc->deactivate = g4x_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001184 } else {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001185 fbc->is_active = i8xx_fbc_is_active;
1186 fbc->activate = i8xx_fbc_activate;
1187 fbc->deactivate = i8xx_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001188
1189 /* This value was pulled out of someone's hat */
1190 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1191 }
1192
Paulo Zanonib07ea0f2015-11-04 17:10:52 -02001193 /* We still don't have any sort of hardware state readout for FBC, so
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001194 * deactivate it in case the BIOS activated it to make sure software
1195 * matches the hardware state. */
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001196 if (fbc->is_active(dev_priv))
1197 fbc->deactivate(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001198}