blob: fda5fc5f272d5f1d3e901f7f2809d6ed5cea78ae [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{
46 return dev_priv->fbc.enable_fbc != NULL;
47}
48
Paulo Zanoni2db33662015-09-14 15:20:03 -030049/*
50 * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the
51 * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's
52 * origin so the x and y offsets can actually fit the registers. As a
53 * consequence, the fence doesn't really start exactly at the display plane
54 * address we program because it starts at the real start of the buffer, so we
55 * have to take this into consideration here.
56 */
57static unsigned int get_crtc_fence_y_offset(struct intel_crtc *crtc)
58{
59 return crtc->base.y - crtc->adjusted_y;
60}
61
Paulo Zanoni7733b492015-07-07 15:26:04 -030062static void i8xx_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020063{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020064 u32 fbc_ctl;
65
66 dev_priv->fbc.enabled = false;
67
68 /* Disable compression */
69 fbc_ctl = I915_READ(FBC_CONTROL);
70 if ((fbc_ctl & FBC_CTL_EN) == 0)
71 return;
72
73 fbc_ctl &= ~FBC_CTL_EN;
74 I915_WRITE(FBC_CONTROL, fbc_ctl);
75
76 /* Wait for compressing bit to clear */
77 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
78 DRM_DEBUG_KMS("FBC idle timed out\n");
79 return;
80 }
81
82 DRM_DEBUG_KMS("disabled FBC\n");
83}
84
Paulo Zanoni220285f2015-07-07 15:26:05 -030085static void i8xx_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020086{
Paulo Zanoni220285f2015-07-07 15:26:05 -030087 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
88 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020089 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020090 int cfb_pitch;
91 int i;
92 u32 fbc_ctl;
93
94 dev_priv->fbc.enabled = true;
95
Jani Nikula60ee5cd2015-02-05 12:04:27 +020096 /* Note: fbc.threshold == 1 for i8xx */
97 cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020098 if (fb->pitches[0] < cfb_pitch)
99 cfb_pitch = fb->pitches[0];
100
101 /* FBC_CTL wants 32B or 64B units */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300102 if (IS_GEN2(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200103 cfb_pitch = (cfb_pitch / 32) - 1;
104 else
105 cfb_pitch = (cfb_pitch / 64) - 1;
106
107 /* Clear old tags */
108 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
Ville Syrjälä4d110c72015-09-18 20:03:18 +0300109 I915_WRITE(FBC_TAG(i), 0);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200110
Paulo Zanoni7733b492015-07-07 15:26:04 -0300111 if (IS_GEN4(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200112 u32 fbc_ctl2;
113
114 /* Set it up... */
115 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300116 fbc_ctl2 |= FBC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200117 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300118 I915_WRITE(FBC_FENCE_OFF, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200119 }
120
121 /* enable it... */
122 fbc_ctl = I915_READ(FBC_CONTROL);
123 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
124 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300125 if (IS_I945GM(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200126 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
127 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
128 fbc_ctl |= obj->fence_reg;
129 I915_WRITE(FBC_CONTROL, fbc_ctl);
130
131 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Paulo Zanoni220285f2015-07-07 15:26:05 -0300132 cfb_pitch, crtc->base.y, plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200133}
134
Paulo Zanoni7733b492015-07-07 15:26:04 -0300135static bool i8xx_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200136{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200137 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
138}
139
Paulo Zanoni220285f2015-07-07 15:26:05 -0300140static void g4x_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200141{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300142 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
143 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200144 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200145 u32 dpfc_ctl;
146
147 dev_priv->fbc.enabled = true;
148
Paulo Zanoni220285f2015-07-07 15:26:05 -0300149 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane) | DPFC_SR_EN;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200150 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
151 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
152 else
153 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
154 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
155
Paulo Zanoni2db33662015-09-14 15:20:03 -0300156 I915_WRITE(DPFC_FENCE_YOFF, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200157
158 /* enable it... */
159 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
160
Paulo Zanoni220285f2015-07-07 15:26:05 -0300161 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200162}
163
Paulo Zanoni7733b492015-07-07 15:26:04 -0300164static void g4x_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200165{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200166 u32 dpfc_ctl;
167
168 dev_priv->fbc.enabled = false;
169
170 /* Disable compression */
171 dpfc_ctl = I915_READ(DPFC_CONTROL);
172 if (dpfc_ctl & DPFC_CTL_EN) {
173 dpfc_ctl &= ~DPFC_CTL_EN;
174 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
175
176 DRM_DEBUG_KMS("disabled FBC\n");
177 }
178}
179
Paulo Zanoni7733b492015-07-07 15:26:04 -0300180static bool g4x_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200181{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200182 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
183}
184
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200185static void intel_fbc_nuke(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200186{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200187 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
188 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200189}
190
Paulo Zanoni220285f2015-07-07 15:26:05 -0300191static void ilk_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200192{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300193 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
194 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200195 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200196 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300197 int threshold = dev_priv->fbc.threshold;
Paulo Zanoni2db33662015-09-14 15:20:03 -0300198 unsigned int y_offset;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200199
200 dev_priv->fbc.enabled = true;
201
Paulo Zanoni220285f2015-07-07 15:26:05 -0300202 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200203 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300204 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200205
Paulo Zanonice65e472015-06-30 10:53:05 -0300206 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200207 case 4:
208 case 3:
209 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
210 break;
211 case 2:
212 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
213 break;
214 case 1:
215 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
216 break;
217 }
218 dpfc_ctl |= DPFC_CTL_FENCE_EN;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300219 if (IS_GEN5(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200220 dpfc_ctl |= obj->fence_reg;
221
Paulo Zanoni2db33662015-09-14 15:20:03 -0300222 y_offset = get_crtc_fence_y_offset(crtc);
223 I915_WRITE(ILK_DPFC_FENCE_YOFF, y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200224 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
225 /* enable it... */
226 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
227
Paulo Zanoni7733b492015-07-07 15:26:04 -0300228 if (IS_GEN6(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200229 I915_WRITE(SNB_DPFC_CTL_SA,
230 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300231 I915_WRITE(DPFC_CPU_FENCE_OFFSET, y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200232 }
233
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200234 intel_fbc_nuke(dev_priv);
235
Paulo Zanoni220285f2015-07-07 15:26:05 -0300236 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200237}
238
Paulo Zanoni7733b492015-07-07 15:26:04 -0300239static void ilk_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200240{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200241 u32 dpfc_ctl;
242
243 dev_priv->fbc.enabled = false;
244
245 /* Disable compression */
246 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
247 if (dpfc_ctl & DPFC_CTL_EN) {
248 dpfc_ctl &= ~DPFC_CTL_EN;
249 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
250
251 DRM_DEBUG_KMS("disabled FBC\n");
252 }
253}
254
Paulo Zanoni7733b492015-07-07 15:26:04 -0300255static bool ilk_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200256{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200257 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
258}
259
Paulo Zanoni220285f2015-07-07 15:26:05 -0300260static void gen7_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200261{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300262 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
263 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200264 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200265 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300266 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200267
268 dev_priv->fbc.enabled = true;
269
Paulo Zanonid8514d62015-06-12 14:36:21 -0300270 dpfc_ctl = 0;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300271 if (IS_IVYBRIDGE(dev_priv))
Paulo Zanoni220285f2015-07-07 15:26:05 -0300272 dpfc_ctl |= IVB_DPFC_CTL_PLANE(crtc->plane);
Paulo Zanonid8514d62015-06-12 14:36:21 -0300273
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200274 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300275 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200276
Paulo Zanonice65e472015-06-30 10:53:05 -0300277 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200278 case 4:
279 case 3:
280 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
281 break;
282 case 2:
283 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
284 break;
285 case 1:
286 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
287 break;
288 }
289
290 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
291
292 if (dev_priv->fbc.false_color)
293 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
294
Paulo Zanoni7733b492015-07-07 15:26:04 -0300295 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200296 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
297 I915_WRITE(ILK_DISPLAY_CHICKEN1,
298 I915_READ(ILK_DISPLAY_CHICKEN1) |
299 ILK_FBCQ_DIS);
Paulo Zanoni40f40222015-09-14 15:20:01 -0300300 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200301 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Paulo Zanoni220285f2015-07-07 15:26:05 -0300302 I915_WRITE(CHICKEN_PIPESL_1(crtc->pipe),
303 I915_READ(CHICKEN_PIPESL_1(crtc->pipe)) |
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200304 HSW_FBCQ_DIS);
305 }
306
Paulo Zanoni57012be92015-09-14 15:20:00 -0300307 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
308
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200309 I915_WRITE(SNB_DPFC_CTL_SA,
310 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300311 I915_WRITE(DPFC_CPU_FENCE_OFFSET, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200312
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200313 intel_fbc_nuke(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200314
Paulo Zanoni220285f2015-07-07 15:26:05 -0300315 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200316}
317
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800318/**
319 * intel_fbc_enabled - Is FBC enabled?
Paulo Zanoni7733b492015-07-07 15:26:04 -0300320 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800321 *
322 * This function is used to verify the current state of FBC.
323 * FIXME: This should be tracked in the plane config eventually
324 * instead of queried at runtime for most callers.
325 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300326bool intel_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200327{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200328 return dev_priv->fbc.enabled;
329}
330
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300331static void intel_fbc_enable(struct intel_crtc *crtc,
332 const struct drm_framebuffer *fb)
333{
334 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
335
336 dev_priv->fbc.enable_fbc(crtc);
337
338 dev_priv->fbc.crtc = crtc;
339 dev_priv->fbc.fb_id = fb->base.id;
340 dev_priv->fbc.y = crtc->base.y;
341}
342
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200343static void intel_fbc_work_fn(struct work_struct *__work)
344{
345 struct intel_fbc_work *work =
346 container_of(to_delayed_work(__work),
347 struct intel_fbc_work, work);
Paulo Zanoni220285f2015-07-07 15:26:05 -0300348 struct drm_i915_private *dev_priv = work->crtc->base.dev->dev_private;
349 struct drm_framebuffer *crtc_fb = work->crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200350
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300351 mutex_lock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200352 if (work == dev_priv->fbc.fbc_work) {
353 /* Double check that we haven't switched fb without cancelling
354 * the prior work.
355 */
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300356 if (crtc_fb == work->fb)
357 intel_fbc_enable(work->crtc, work->fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200358
359 dev_priv->fbc.fbc_work = NULL;
360 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300361 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200362
363 kfree(work);
364}
365
366static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
367{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300368 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
369
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200370 if (dev_priv->fbc.fbc_work == NULL)
371 return;
372
373 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
374
375 /* Synchronisation is provided by struct_mutex and checking of
376 * dev_priv->fbc.fbc_work, so we can perform the cancellation
377 * entirely asynchronously.
378 */
379 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
380 /* tasklet was killed before being run, clean up */
381 kfree(dev_priv->fbc.fbc_work);
382
383 /* Mark the work as no longer wanted so that if it does
384 * wake-up (because the work was already running and waiting
385 * for our mutex), it will discover that is no longer
386 * necessary to run.
387 */
388 dev_priv->fbc.fbc_work = NULL;
389}
390
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300391static void intel_fbc_schedule_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200392{
393 struct intel_fbc_work *work;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300394 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200395
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300396 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
397
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200398 intel_fbc_cancel_work(dev_priv);
399
400 work = kzalloc(sizeof(*work), GFP_KERNEL);
401 if (work == NULL) {
402 DRM_ERROR("Failed to allocate FBC work structure\n");
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300403 intel_fbc_enable(crtc, crtc->base.primary->fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200404 return;
405 }
406
407 work->crtc = crtc;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300408 work->fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200409 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
410
411 dev_priv->fbc.fbc_work = work;
412
413 /* Delay the actual enabling to let pageflipping cease and the
414 * display to settle before starting the compression. Note that
415 * this delay also serves a second purpose: it allows for a
416 * vblank to pass after disabling the FBC before we attempt
417 * to modify the control registers.
418 *
419 * A more complicated solution would involve tracking vblanks
420 * following the termination of the page-flipping sequence
421 * and indeed performing the enable as a co-routine and not
422 * waiting synchronously upon the vblank.
423 *
424 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
425 */
426 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
427}
428
Paulo Zanoni7733b492015-07-07 15:26:04 -0300429static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300430{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300431 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
432
433 intel_fbc_cancel_work(dev_priv);
434
Paulo Zanoni7733b492015-07-07 15:26:04 -0300435 dev_priv->fbc.disable_fbc(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300436 dev_priv->fbc.crtc = NULL;
437}
438
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800439/**
440 * intel_fbc_disable - disable FBC
Paulo Zanoni7733b492015-07-07 15:26:04 -0300441 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800442 *
443 * This function disables FBC.
444 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300445void intel_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200446{
Paulo Zanoni9f218332015-09-23 12:52:27 -0300447 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300448 return;
449
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300450 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300451 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300452 mutex_unlock(&dev_priv->fbc.lock);
453}
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200454
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300455/*
456 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
457 * @crtc: the CRTC
458 *
459 * This function disables FBC if it's associated with the provided CRTC.
460 */
461void intel_fbc_disable_crtc(struct intel_crtc *crtc)
462{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300463 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200464
Paulo Zanoni9f218332015-09-23 12:52:27 -0300465 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300466 return;
467
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300468 mutex_lock(&dev_priv->fbc.lock);
469 if (dev_priv->fbc.crtc == crtc)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300470 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300471 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200472}
473
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300474const char *intel_no_fbc_reason_str(enum no_fbc_reason reason)
475{
476 switch (reason) {
477 case FBC_OK:
478 return "FBC enabled but currently disabled in hardware";
479 case FBC_UNSUPPORTED:
480 return "unsupported by this chipset";
481 case FBC_NO_OUTPUT:
482 return "no output";
483 case FBC_STOLEN_TOO_SMALL:
484 return "not enough stolen memory";
485 case FBC_UNSUPPORTED_MODE:
486 return "mode incompatible with compression";
487 case FBC_MODE_TOO_LARGE:
488 return "mode too large for compression";
489 case FBC_BAD_PLANE:
490 return "FBC unsupported on plane";
491 case FBC_NOT_TILED:
492 return "framebuffer not tiled or fenced";
493 case FBC_MULTIPLE_PIPES:
494 return "more than one pipe active";
495 case FBC_MODULE_PARAM:
496 return "disabled per module param";
497 case FBC_CHIP_DEFAULT:
498 return "disabled per chip default";
499 case FBC_ROTATION:
500 return "rotation unsupported";
Paulo Zanoni89351082015-07-07 15:26:06 -0300501 case FBC_IN_DBG_MASTER:
502 return "Kernel debugger is active";
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300503 case FBC_BAD_STRIDE:
504 return "framebuffer stride not supported";
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300505 case FBC_PIXEL_RATE:
506 return "pixel rate is too big";
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300507 case FBC_PIXEL_FORMAT:
508 return "pixel format is invalid";
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300509 default:
510 MISSING_CASE(reason);
511 return "unknown reason";
512 }
513}
514
515static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200516 enum no_fbc_reason reason)
517{
518 if (dev_priv->fbc.no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300519 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200520
521 dev_priv->fbc.no_fbc_reason = reason;
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300522 DRM_DEBUG_KMS("Disabling FBC: %s\n", intel_no_fbc_reason_str(reason));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200523}
524
Paulo Zanoni95106752015-02-13 17:23:41 -0200525static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
526{
Paulo Zanoni95106752015-02-13 17:23:41 -0200527 struct drm_crtc *crtc = NULL, *tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200528 enum pipe pipe;
Paulo Zanoni232fd932015-07-07 15:26:07 -0300529 bool pipe_a_only = false;
Paulo Zanoni95106752015-02-13 17:23:41 -0200530
Paulo Zanoni68b92142015-02-13 17:23:42 -0200531 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
532 pipe_a_only = true;
533
534 for_each_pipe(dev_priv, pipe) {
535 tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
536
Paulo Zanoni95106752015-02-13 17:23:41 -0200537 if (intel_crtc_active(tmp_crtc) &&
Paulo Zanoni232fd932015-07-07 15:26:07 -0300538 to_intel_plane_state(tmp_crtc->primary->state)->visible)
Paulo Zanoni95106752015-02-13 17:23:41 -0200539 crtc = tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200540
541 if (pipe_a_only)
542 break;
Paulo Zanoni95106752015-02-13 17:23:41 -0200543 }
544
Paulo Zanoni8df5dd52015-07-07 15:26:08 -0300545 if (!crtc || crtc->primary->fb == NULL)
Paulo Zanoni95106752015-02-13 17:23:41 -0200546 return NULL;
Paulo Zanoni95106752015-02-13 17:23:41 -0200547
548 return crtc;
549}
550
Paulo Zanoni232fd932015-07-07 15:26:07 -0300551static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
552{
553 enum pipe pipe;
554 int n_pipes = 0;
555 struct drm_crtc *crtc;
556
557 if (INTEL_INFO(dev_priv)->gen > 4)
558 return true;
559
560 for_each_pipe(dev_priv, pipe) {
561 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
562
563 if (intel_crtc_active(crtc) &&
564 to_intel_plane_state(crtc->primary->state)->visible)
565 n_pipes++;
566 }
567
568 return (n_pipes < 2);
569}
570
Paulo Zanoni7733b492015-07-07 15:26:04 -0300571static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300572 struct drm_mm_node *node,
573 int size,
574 int fb_cpp)
575{
Paulo Zanonifc786722015-07-02 19:25:08 -0300576 int compression_threshold = 1;
577 int ret;
Paulo Zanonia9da5122015-09-14 15:19:57 -0300578 u64 end;
579
580 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
581 * reserved range size, so it always assumes the maximum (8mb) is used.
582 * If we enable FBC using a CFB on that memory range we'll get FIFO
583 * underruns, even if that range is not reserved by the BIOS. */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700584 if (IS_BROADWELL(dev_priv) ||
585 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Paulo Zanonia9da5122015-09-14 15:19:57 -0300586 end = dev_priv->gtt.stolen_size - 8 * 1024 * 1024;
587 else
588 end = dev_priv->gtt.stolen_usable_size;
Paulo Zanonifc786722015-07-02 19:25:08 -0300589
590 /* HACK: This code depends on what we will do in *_enable_fbc. If that
591 * code changes, this code needs to change as well.
592 *
593 * The enable_fbc code will attempt to use one of our 2 compression
594 * thresholds, therefore, in that case, we only have 1 resort.
595 */
596
597 /* Try to over-allocate to reduce reallocations and fragmentation. */
Paulo Zanonia9da5122015-09-14 15:19:57 -0300598 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
599 4096, 0, end);
Paulo Zanonifc786722015-07-02 19:25:08 -0300600 if (ret == 0)
601 return compression_threshold;
602
603again:
604 /* HW's ability to limit the CFB is 1:4 */
605 if (compression_threshold > 4 ||
606 (fb_cpp == 2 && compression_threshold == 2))
607 return 0;
608
Paulo Zanonia9da5122015-09-14 15:19:57 -0300609 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
610 4096, 0, end);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300611 if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300612 return 0;
613 } else if (ret) {
614 compression_threshold <<= 1;
615 goto again;
616 } else {
617 return compression_threshold;
618 }
619}
620
Paulo Zanoni7733b492015-07-07 15:26:04 -0300621static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv, int size,
622 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300623{
Paulo Zanonifc786722015-07-02 19:25:08 -0300624 struct drm_mm_node *uninitialized_var(compressed_llb);
625 int ret;
626
Paulo Zanoni7733b492015-07-07 15:26:04 -0300627 ret = find_compression_threshold(dev_priv, &dev_priv->fbc.compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300628 size, fb_cpp);
629 if (!ret)
630 goto err_llb;
631 else if (ret > 1) {
632 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");
633
634 }
635
636 dev_priv->fbc.threshold = ret;
637
638 if (INTEL_INFO(dev_priv)->gen >= 5)
639 I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300640 else if (IS_GM45(dev_priv)) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300641 I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
642 } else {
643 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
644 if (!compressed_llb)
645 goto err_fb;
646
647 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
648 4096, 4096);
649 if (ret)
650 goto err_fb;
651
652 dev_priv->fbc.compressed_llb = compressed_llb;
653
654 I915_WRITE(FBC_CFB_BASE,
655 dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
656 I915_WRITE(FBC_LL_BASE,
657 dev_priv->mm.stolen_base + compressed_llb->start);
658 }
659
660 dev_priv->fbc.uncompressed_size = size;
661
Paulo Zanonib8bf5d72015-09-14 15:19:58 -0300662 DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
663 dev_priv->fbc.compressed_fb.size,
664 dev_priv->fbc.threshold);
Paulo Zanonifc786722015-07-02 19:25:08 -0300665
666 return 0;
667
668err_fb:
669 kfree(compressed_llb);
670 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
671err_llb:
672 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);
673 return -ENOSPC;
674}
675
Paulo Zanoni7733b492015-07-07 15:26:04 -0300676static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300677{
Paulo Zanonifc786722015-07-02 19:25:08 -0300678 if (dev_priv->fbc.uncompressed_size == 0)
679 return;
680
681 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
682
683 if (dev_priv->fbc.compressed_llb) {
684 i915_gem_stolen_remove_node(dev_priv,
685 dev_priv->fbc.compressed_llb);
686 kfree(dev_priv->fbc.compressed_llb);
687 }
688
689 dev_priv->fbc.uncompressed_size = 0;
690}
691
Paulo Zanoni7733b492015-07-07 15:26:04 -0300692void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300693{
Paulo Zanoni9f218332015-09-23 12:52:27 -0300694 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300695 return;
696
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300697 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300698 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300699 mutex_unlock(&dev_priv->fbc.lock);
700}
701
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300702/*
703 * For SKL+, the plane source size used by the hardware is based on the value we
704 * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
705 * we wrote to PIPESRC.
706 */
707static void intel_fbc_get_plane_source_size(struct intel_crtc *crtc,
708 int *width, int *height)
Paulo Zanonifc786722015-07-02 19:25:08 -0300709{
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300710 struct intel_plane_state *plane_state =
711 to_intel_plane_state(crtc->base.primary->state);
712 int w, h;
713
714 if (intel_rotation_90_or_270(plane_state->base.rotation)) {
715 w = drm_rect_height(&plane_state->src) >> 16;
716 h = drm_rect_width(&plane_state->src) >> 16;
717 } else {
718 w = drm_rect_width(&plane_state->src) >> 16;
719 h = drm_rect_height(&plane_state->src) >> 16;
720 }
721
722 if (width)
723 *width = w;
724 if (height)
725 *height = h;
726}
727
728static int intel_fbc_calculate_cfb_size(struct intel_crtc *crtc)
729{
730 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
731 struct drm_framebuffer *fb = crtc->base.primary->fb;
732 int lines;
733
734 intel_fbc_get_plane_source_size(crtc, NULL, &lines);
735 if (INTEL_INFO(dev_priv)->gen >= 7)
736 lines = min(lines, 2048);
737
738 return lines * fb->pitches[0];
739}
740
741static int intel_fbc_setup_cfb(struct intel_crtc *crtc)
742{
743 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
744 struct drm_framebuffer *fb = crtc->base.primary->fb;
745 int size, cpp;
746
747 size = intel_fbc_calculate_cfb_size(crtc);
748 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
749
Paulo Zanonifc786722015-07-02 19:25:08 -0300750 if (size <= dev_priv->fbc.uncompressed_size)
751 return 0;
752
753 /* Release any current block */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300754 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanonifc786722015-07-02 19:25:08 -0300755
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300756 return intel_fbc_alloc_cfb(dev_priv, size, cpp);
Paulo Zanonifc786722015-07-02 19:25:08 -0300757}
758
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300759static bool stride_is_valid(struct drm_i915_private *dev_priv,
760 unsigned int stride)
761{
762 /* These should have been caught earlier. */
763 WARN_ON(stride < 512);
764 WARN_ON((stride & (64 - 1)) != 0);
765
766 /* Below are the additional FBC restrictions. */
767
768 if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
769 return stride == 4096 || stride == 8192;
770
771 if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
772 return false;
773
774 if (stride > 16384)
775 return false;
776
777 return true;
778}
779
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300780static bool pixel_format_is_valid(struct drm_framebuffer *fb)
781{
782 struct drm_device *dev = fb->dev;
783 struct drm_i915_private *dev_priv = dev->dev_private;
784
785 switch (fb->pixel_format) {
786 case DRM_FORMAT_XRGB8888:
787 case DRM_FORMAT_XBGR8888:
788 return true;
789 case DRM_FORMAT_XRGB1555:
790 case DRM_FORMAT_RGB565:
791 /* 16bpp not supported on gen2 */
792 if (IS_GEN2(dev))
793 return false;
794 /* WaFbcOnly1to1Ratio:ctg */
795 if (IS_G4X(dev_priv))
796 return false;
797 return true;
798 default:
799 return false;
800 }
801}
802
Paulo Zanoni856312a2015-10-01 19:57:12 -0300803/*
804 * For some reason, the hardware tracking starts looking at whatever we
805 * programmed as the display plane base address register. It does not look at
806 * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
807 * variables instead of just looking at the pipe/plane size.
808 */
809static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300810{
811 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoni856312a2015-10-01 19:57:12 -0300812 unsigned int effective_w, effective_h, max_w, max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300813
814 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
815 max_w = 4096;
816 max_h = 4096;
817 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
818 max_w = 4096;
819 max_h = 2048;
820 } else {
821 max_w = 2048;
822 max_h = 1536;
823 }
824
Paulo Zanoni856312a2015-10-01 19:57:12 -0300825 intel_fbc_get_plane_source_size(crtc, &effective_w, &effective_h);
826 effective_w += crtc->adjusted_x;
827 effective_h += crtc->adjusted_y;
828
829 return effective_w <= max_w && effective_h <= max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300830}
831
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200832/**
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300833 * __intel_fbc_update - enable/disable FBC as needed, unlocked
Paulo Zanoni7733b492015-07-07 15:26:04 -0300834 * @dev_priv: i915 device instance
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200835 *
836 * Set up the framebuffer compression hardware at mode set time. We
837 * enable it if possible:
838 * - plane A only (on pre-965)
839 * - no pixel mulitply/line duplication
840 * - no alpha buffer discard
841 * - no dual wide
842 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
843 *
844 * We can't assume that any compression will take place (worst case),
845 * so the compressed buffer has to be the same size as the uncompressed
846 * one. It also must reside (along with the line length buffer) in
847 * stolen memory.
848 *
849 * We need to enable/disable FBC on a global basis.
850 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300851static void __intel_fbc_update(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200852{
Paulo Zanoni95106752015-02-13 17:23:41 -0200853 struct drm_crtc *crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200854 struct intel_crtc *intel_crtc;
855 struct drm_framebuffer *fb;
856 struct drm_i915_gem_object *obj;
857 const struct drm_display_mode *adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200858
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300859 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
860
Yu Zhangbd492342015-02-10 19:05:50 +0800861 /* disable framebuffer compression in vGPU */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300862 if (intel_vgpu_active(dev_priv->dev))
Yu Zhangbd492342015-02-10 19:05:50 +0800863 i915.enable_fbc = 0;
864
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200865 if (i915.enable_fbc < 0) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300866 set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200867 goto out_disable;
868 }
869
Rodrigo Viviab585de2015-03-24 12:40:09 -0700870 if (!i915.enable_fbc) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300871 set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200872 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200873 }
874
875 /*
876 * If FBC is already on, we just have to verify that we can
877 * keep it that way...
878 * Need to disable if:
879 * - more than one pipe is active
880 * - changing FBC params (stride, fence, mode)
881 * - new fb is too large to fit in compressed buffer
882 * - going to an unsupported config (interlace, pixel multiply, etc.)
883 */
Paulo Zanoni95106752015-02-13 17:23:41 -0200884 crtc = intel_fbc_find_crtc(dev_priv);
Paulo Zanoni8df5dd52015-07-07 15:26:08 -0300885 if (!crtc) {
886 set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200887 goto out_disable;
Paulo Zanoni8df5dd52015-07-07 15:26:08 -0300888 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200889
Paulo Zanoni232fd932015-07-07 15:26:07 -0300890 if (!multiple_pipes_ok(dev_priv)) {
891 set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES);
892 goto out_disable;
893 }
894
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200895 intel_crtc = to_intel_crtc(crtc);
896 fb = crtc->primary->fb;
897 obj = intel_fb_obj(fb);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200898 adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200899
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200900 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
901 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300902 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200903 goto out_disable;
904 }
905
Paulo Zanoni856312a2015-10-01 19:57:12 -0300906 if (!intel_fbc_hw_tracking_covers_screen(intel_crtc)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300907 set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200908 goto out_disable;
909 }
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300910
Paulo Zanoni7733b492015-07-07 15:26:04 -0300911 if ((INTEL_INFO(dev_priv)->gen < 4 || HAS_DDI(dev_priv)) &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200912 intel_crtc->plane != PLANE_A) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300913 set_no_fbc_reason(dev_priv, FBC_BAD_PLANE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200914 goto out_disable;
915 }
916
917 /* The use of a CPU fence is mandatory in order to detect writes
918 * by the CPU to the scanout and trigger updates to the FBC.
919 */
920 if (obj->tiling_mode != I915_TILING_X ||
921 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300922 set_no_fbc_reason(dev_priv, FBC_NOT_TILED);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200923 goto out_disable;
924 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300925 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Matt Roper8e7d6882015-01-21 16:35:41 -0800926 crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300927 set_no_fbc_reason(dev_priv, FBC_ROTATION);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200928 goto out_disable;
929 }
930
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300931 if (!stride_is_valid(dev_priv, fb->pitches[0])) {
932 set_no_fbc_reason(dev_priv, FBC_BAD_STRIDE);
933 goto out_disable;
934 }
935
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300936 if (!pixel_format_is_valid(fb)) {
937 set_no_fbc_reason(dev_priv, FBC_PIXEL_FORMAT);
938 goto out_disable;
939 }
940
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200941 /* If the kernel debugger is active, always disable compression */
Paulo Zanoni89351082015-07-07 15:26:06 -0300942 if (in_dbg_master()) {
943 set_no_fbc_reason(dev_priv, FBC_IN_DBG_MASTER);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200944 goto out_disable;
Paulo Zanoni89351082015-07-07 15:26:06 -0300945 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200946
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300947 /* WaFbcExceedCdClockThreshold:hsw,bdw */
948 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
949 ilk_pipe_pixel_rate(intel_crtc->config) >=
950 dev_priv->cdclk_freq * 95 / 100) {
951 set_no_fbc_reason(dev_priv, FBC_PIXEL_RATE);
952 goto out_disable;
953 }
954
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300955 if (intel_fbc_setup_cfb(intel_crtc)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300956 set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200957 goto out_disable;
958 }
959
960 /* If the scanout has not changed, don't modify the FBC settings.
961 * Note that we make the fundamental assumption that the fb->obj
962 * cannot be unpinned (and have its GTT offset and fence revoked)
963 * without first being decoupled from the scanout and FBC disabled.
964 */
Paulo Zanonie35fef22015-02-09 14:46:29 -0200965 if (dev_priv->fbc.crtc == intel_crtc &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200966 dev_priv->fbc.fb_id == fb->base.id &&
967 dev_priv->fbc.y == crtc->y)
968 return;
969
Paulo Zanoni7733b492015-07-07 15:26:04 -0300970 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200971 /* We update FBC along two paths, after changing fb/crtc
972 * configuration (modeswitching) and after page-flipping
973 * finishes. For the latter, we know that not only did
974 * we disable the FBC at the start of the page-flip
975 * sequence, but also more than one vblank has passed.
976 *
977 * For the former case of modeswitching, it is possible
978 * to switch between two FBC valid configurations
979 * instantaneously so we do need to disable the FBC
980 * before we can modify its control registers. We also
981 * have to wait for the next vblank for that to take
982 * effect. However, since we delay enabling FBC we can
983 * assume that a vblank has passed since disabling and
984 * that we can safely alter the registers in the deferred
985 * callback.
986 *
987 * In the scenario that we go from a valid to invalid
988 * and then back to valid FBC configuration we have
989 * no strict enforcement that a vblank occurred since
990 * disabling the FBC. However, along all current pipe
991 * disabling paths we do need to wait for a vblank at
992 * some point. And we wait before enabling FBC anyway.
993 */
994 DRM_DEBUG_KMS("disabling active FBC for update\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300995 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200996 }
997
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300998 intel_fbc_schedule_enable(intel_crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200999 dev_priv->fbc.no_fbc_reason = FBC_OK;
1000 return;
1001
1002out_disable:
1003 /* Multiple disables should be harmless */
Paulo Zanoni7733b492015-07-07 15:26:04 -03001004 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001005 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -03001006 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001007 }
Paulo Zanoni7733b492015-07-07 15:26:04 -03001008 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001009}
1010
1011/*
1012 * intel_fbc_update - enable/disable FBC as needed
Paulo Zanoni7733b492015-07-07 15:26:04 -03001013 * @dev_priv: i915 device instance
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001014 *
1015 * This function reevaluates the overall state and enables or disables FBC.
1016 */
Paulo Zanoni7733b492015-07-07 15:26:04 -03001017void intel_fbc_update(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001018{
Paulo Zanoni9f218332015-09-23 12:52:27 -03001019 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -03001020 return;
1021
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001022 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -03001023 __intel_fbc_update(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001024 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001025}
1026
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001027void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
1028 unsigned int frontbuffer_bits,
1029 enum fb_op_origin origin)
1030{
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001031 unsigned int fbc_bits;
1032
Paulo Zanoni9f218332015-09-23 12:52:27 -03001033 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -03001034 return;
1035
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001036 if (origin == ORIGIN_GTT)
1037 return;
1038
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001039 mutex_lock(&dev_priv->fbc.lock);
1040
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001041 if (dev_priv->fbc.enabled)
1042 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
1043 else if (dev_priv->fbc.fbc_work)
1044 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(
Paulo Zanoni220285f2015-07-07 15:26:05 -03001045 dev_priv->fbc.fbc_work->crtc->pipe);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001046 else
1047 fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
1048
1049 dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
1050
1051 if (dev_priv->fbc.busy_bits)
Paulo Zanoni7733b492015-07-07 15:26:04 -03001052 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001053
1054 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001055}
1056
1057void intel_fbc_flush(struct drm_i915_private *dev_priv,
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001058 unsigned int frontbuffer_bits, enum fb_op_origin origin)
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001059{
Paulo Zanoni9f218332015-09-23 12:52:27 -03001060 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -03001061 return;
1062
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001063 if (origin == ORIGIN_GTT)
1064 return;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001065
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001066 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001067
1068 dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
1069
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001070 if (!dev_priv->fbc.busy_bits) {
1071 __intel_fbc_disable(dev_priv);
Paulo Zanoni7733b492015-07-07 15:26:04 -03001072 __intel_fbc_update(dev_priv);
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001073 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001074
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001075 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001076}
1077
Rodrigo Vivi94b83952014-12-08 06:46:31 -08001078/**
1079 * intel_fbc_init - Initialize FBC
1080 * @dev_priv: the i915 device
1081 *
1082 * This function might be called during PM init process.
1083 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001084void intel_fbc_init(struct drm_i915_private *dev_priv)
1085{
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001086 enum pipe pipe;
1087
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001088 mutex_init(&dev_priv->fbc.lock);
1089
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001090 if (!HAS_FBC(dev_priv)) {
1091 dev_priv->fbc.enabled = false;
Paulo Zanoni104618b2015-02-09 14:46:28 -02001092 dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001093 return;
1094 }
1095
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001096 for_each_pipe(dev_priv, pipe) {
1097 dev_priv->fbc.possible_framebuffer_bits |=
1098 INTEL_FRONTBUFFER_PRIMARY(pipe);
1099
1100 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
1101 break;
1102 }
1103
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001104 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001105 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
1106 dev_priv->fbc.enable_fbc = gen7_fbc_enable;
1107 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001108 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001109 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
1110 dev_priv->fbc.enable_fbc = ilk_fbc_enable;
1111 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001112 } else if (IS_GM45(dev_priv)) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001113 dev_priv->fbc.fbc_enabled = g4x_fbc_enabled;
1114 dev_priv->fbc.enable_fbc = g4x_fbc_enable;
1115 dev_priv->fbc.disable_fbc = g4x_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001116 } else {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001117 dev_priv->fbc.fbc_enabled = i8xx_fbc_enabled;
1118 dev_priv->fbc.enable_fbc = i8xx_fbc_enable;
1119 dev_priv->fbc.disable_fbc = i8xx_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001120
1121 /* This value was pulled out of someone's hat */
1122 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1123 }
1124
Paulo Zanoni7733b492015-07-07 15:26:04 -03001125 dev_priv->fbc.enabled = dev_priv->fbc.fbc_enabled(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001126}