blob: 70b55c0cf28e9215631f54548b5300bc917beb82 [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 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 Zanoni2db33662015-09-14 15:20:03 -030054/*
55 * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the
56 * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's
57 * origin so the x and y offsets can actually fit the registers. As a
58 * consequence, the fence doesn't really start exactly at the display plane
59 * address we program because it starts at the real start of the buffer, so we
60 * have to take this into consideration here.
61 */
62static unsigned int get_crtc_fence_y_offset(struct intel_crtc *crtc)
63{
64 return crtc->base.y - crtc->adjusted_y;
65}
66
Paulo Zanoni7733b492015-07-07 15:26:04 -030067static void i8xx_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020068{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020069 u32 fbc_ctl;
70
71 dev_priv->fbc.enabled = false;
72
73 /* Disable compression */
74 fbc_ctl = I915_READ(FBC_CONTROL);
75 if ((fbc_ctl & FBC_CTL_EN) == 0)
76 return;
77
78 fbc_ctl &= ~FBC_CTL_EN;
79 I915_WRITE(FBC_CONTROL, fbc_ctl);
80
81 /* Wait for compressing bit to clear */
82 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
83 DRM_DEBUG_KMS("FBC idle timed out\n");
84 return;
85 }
86
87 DRM_DEBUG_KMS("disabled FBC\n");
88}
89
Paulo Zanoni220285f2015-07-07 15:26:05 -030090static void i8xx_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020091{
Paulo Zanoni220285f2015-07-07 15:26:05 -030092 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
93 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020094 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020095 int cfb_pitch;
96 int i;
97 u32 fbc_ctl;
98
99 dev_priv->fbc.enabled = true;
100
Jani Nikula60ee5cd2015-02-05 12:04:27 +0200101 /* Note: fbc.threshold == 1 for i8xx */
102 cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200103 if (fb->pitches[0] < cfb_pitch)
104 cfb_pitch = fb->pitches[0];
105
106 /* FBC_CTL wants 32B or 64B units */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300107 if (IS_GEN2(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200108 cfb_pitch = (cfb_pitch / 32) - 1;
109 else
110 cfb_pitch = (cfb_pitch / 64) - 1;
111
112 /* Clear old tags */
113 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
Ville Syrjälä4d110c72015-09-18 20:03:18 +0300114 I915_WRITE(FBC_TAG(i), 0);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200115
Paulo Zanoni7733b492015-07-07 15:26:04 -0300116 if (IS_GEN4(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200117 u32 fbc_ctl2;
118
119 /* Set it up... */
120 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300121 fbc_ctl2 |= FBC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200122 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300123 I915_WRITE(FBC_FENCE_OFF, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200124 }
125
126 /* enable it... */
127 fbc_ctl = I915_READ(FBC_CONTROL);
128 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
129 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300130 if (IS_I945GM(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200131 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
132 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
133 fbc_ctl |= obj->fence_reg;
134 I915_WRITE(FBC_CONTROL, fbc_ctl);
135
136 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Paulo Zanoni220285f2015-07-07 15:26:05 -0300137 cfb_pitch, crtc->base.y, plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200138}
139
Paulo Zanoni7733b492015-07-07 15:26:04 -0300140static bool i8xx_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200141{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200142 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
143}
144
Paulo Zanoni220285f2015-07-07 15:26:05 -0300145static void g4x_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200146{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300147 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
148 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200149 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200150 u32 dpfc_ctl;
151
152 dev_priv->fbc.enabled = true;
153
Paulo Zanoni220285f2015-07-07 15:26:05 -0300154 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane) | DPFC_SR_EN;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200155 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
156 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
157 else
158 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
159 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
160
Paulo Zanoni2db33662015-09-14 15:20:03 -0300161 I915_WRITE(DPFC_FENCE_YOFF, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200162
163 /* enable it... */
164 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
165
Paulo Zanoni220285f2015-07-07 15:26:05 -0300166 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200167}
168
Paulo Zanoni7733b492015-07-07 15:26:04 -0300169static void g4x_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200170{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200171 u32 dpfc_ctl;
172
173 dev_priv->fbc.enabled = false;
174
175 /* Disable compression */
176 dpfc_ctl = I915_READ(DPFC_CONTROL);
177 if (dpfc_ctl & DPFC_CTL_EN) {
178 dpfc_ctl &= ~DPFC_CTL_EN;
179 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
180
181 DRM_DEBUG_KMS("disabled FBC\n");
182 }
183}
184
Paulo Zanoni7733b492015-07-07 15:26:04 -0300185static bool g4x_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200186{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200187 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
188}
189
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200190/* This function forces a CFB recompression through the nuke operation. */
191static void intel_fbc_recompress(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200192{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200193 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
194 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200195}
196
Paulo Zanoni220285f2015-07-07 15:26:05 -0300197static void ilk_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200198{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300199 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
200 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200201 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200202 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300203 int threshold = dev_priv->fbc.threshold;
Paulo Zanoni2db33662015-09-14 15:20:03 -0300204 unsigned int y_offset;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200205
206 dev_priv->fbc.enabled = true;
207
Paulo Zanoni220285f2015-07-07 15:26:05 -0300208 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200209 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300210 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200211
Paulo Zanonice65e472015-06-30 10:53:05 -0300212 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200213 case 4:
214 case 3:
215 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
216 break;
217 case 2:
218 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
219 break;
220 case 1:
221 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
222 break;
223 }
224 dpfc_ctl |= DPFC_CTL_FENCE_EN;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300225 if (IS_GEN5(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200226 dpfc_ctl |= obj->fence_reg;
227
Paulo Zanoni2db33662015-09-14 15:20:03 -0300228 y_offset = get_crtc_fence_y_offset(crtc);
229 I915_WRITE(ILK_DPFC_FENCE_YOFF, y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200230 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
231 /* enable it... */
232 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
233
Paulo Zanoni7733b492015-07-07 15:26:04 -0300234 if (IS_GEN6(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200235 I915_WRITE(SNB_DPFC_CTL_SA,
236 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300237 I915_WRITE(DPFC_CPU_FENCE_OFFSET, y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200238 }
239
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200240 intel_fbc_recompress(dev_priv);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200241
Paulo Zanoni220285f2015-07-07 15:26:05 -0300242 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200243}
244
Paulo Zanoni7733b492015-07-07 15:26:04 -0300245static void ilk_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200246{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200247 u32 dpfc_ctl;
248
249 dev_priv->fbc.enabled = false;
250
251 /* Disable compression */
252 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
253 if (dpfc_ctl & DPFC_CTL_EN) {
254 dpfc_ctl &= ~DPFC_CTL_EN;
255 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
256
257 DRM_DEBUG_KMS("disabled FBC\n");
258 }
259}
260
Paulo Zanoni7733b492015-07-07 15:26:04 -0300261static bool ilk_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200262{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200263 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
264}
265
Paulo Zanoni220285f2015-07-07 15:26:05 -0300266static void gen7_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200267{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300268 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
269 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200270 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200271 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300272 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200273
274 dev_priv->fbc.enabled = true;
275
Paulo Zanonid8514d62015-06-12 14:36:21 -0300276 dpfc_ctl = 0;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300277 if (IS_IVYBRIDGE(dev_priv))
Paulo Zanoni220285f2015-07-07 15:26:05 -0300278 dpfc_ctl |= IVB_DPFC_CTL_PLANE(crtc->plane);
Paulo Zanonid8514d62015-06-12 14:36:21 -0300279
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200280 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300281 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200282
Paulo Zanonice65e472015-06-30 10:53:05 -0300283 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200284 case 4:
285 case 3:
286 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
287 break;
288 case 2:
289 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
290 break;
291 case 1:
292 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
293 break;
294 }
295
296 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
297
298 if (dev_priv->fbc.false_color)
299 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
300
Paulo Zanoni7733b492015-07-07 15:26:04 -0300301 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200302 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
303 I915_WRITE(ILK_DISPLAY_CHICKEN1,
304 I915_READ(ILK_DISPLAY_CHICKEN1) |
305 ILK_FBCQ_DIS);
Paulo Zanoni40f40222015-09-14 15:20:01 -0300306 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200307 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Paulo Zanoni220285f2015-07-07 15:26:05 -0300308 I915_WRITE(CHICKEN_PIPESL_1(crtc->pipe),
309 I915_READ(CHICKEN_PIPESL_1(crtc->pipe)) |
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200310 HSW_FBCQ_DIS);
311 }
312
Paulo Zanoni57012be92015-09-14 15:20:00 -0300313 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
314
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200315 I915_WRITE(SNB_DPFC_CTL_SA,
316 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300317 I915_WRITE(DPFC_CPU_FENCE_OFFSET, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200318
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200319 intel_fbc_recompress(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200320
Paulo Zanoni220285f2015-07-07 15:26:05 -0300321 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200322}
323
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800324/**
325 * intel_fbc_enabled - Is FBC enabled?
Paulo Zanoni7733b492015-07-07 15:26:04 -0300326 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800327 *
328 * This function is used to verify the current state of FBC.
329 * FIXME: This should be tracked in the plane config eventually
330 * instead of queried at runtime for most callers.
331 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300332bool intel_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200333{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200334 return dev_priv->fbc.enabled;
335}
336
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300337static void intel_fbc_enable(const struct drm_framebuffer *fb)
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300338{
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300339 struct drm_i915_private *dev_priv = fb->dev->dev_private;
340 struct intel_crtc *crtc = dev_priv->fbc.crtc;
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300341
342 dev_priv->fbc.enable_fbc(crtc);
343
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300344 dev_priv->fbc.fb_id = fb->base.id;
345 dev_priv->fbc.y = crtc->base.y;
346}
347
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200348static void intel_fbc_work_fn(struct work_struct *__work)
349{
350 struct intel_fbc_work *work =
351 container_of(to_delayed_work(__work),
352 struct intel_fbc_work, work);
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300353 struct drm_i915_private *dev_priv = work->fb->dev->dev_private;
354 struct drm_framebuffer *crtc_fb = dev_priv->fbc.crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200355
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300356 mutex_lock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200357 if (work == dev_priv->fbc.fbc_work) {
358 /* Double check that we haven't switched fb without cancelling
359 * the prior work.
360 */
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300361 if (crtc_fb == work->fb)
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300362 intel_fbc_enable(work->fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200363
364 dev_priv->fbc.fbc_work = NULL;
365 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300366 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200367
368 kfree(work);
369}
370
371static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
372{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300373 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
374
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200375 if (dev_priv->fbc.fbc_work == NULL)
376 return;
377
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200378 /* Synchronisation is provided by struct_mutex and checking of
379 * dev_priv->fbc.fbc_work, so we can perform the cancellation
380 * entirely asynchronously.
381 */
382 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
383 /* tasklet was killed before being run, clean up */
384 kfree(dev_priv->fbc.fbc_work);
385
386 /* Mark the work as no longer wanted so that if it does
387 * wake-up (because the work was already running and waiting
388 * for our mutex), it will discover that is no longer
389 * necessary to run.
390 */
391 dev_priv->fbc.fbc_work = NULL;
392}
393
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300394static void intel_fbc_schedule_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200395{
396 struct intel_fbc_work *work;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300397 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200398
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300399 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
400
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200401 intel_fbc_cancel_work(dev_priv);
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300402 dev_priv->fbc.crtc = crtc;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200403
404 work = kzalloc(sizeof(*work), GFP_KERNEL);
405 if (work == NULL) {
406 DRM_ERROR("Failed to allocate FBC work structure\n");
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300407 intel_fbc_enable(crtc->base.primary->fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200408 return;
409 }
410
Paulo Zanoni220285f2015-07-07 15:26:05 -0300411 work->fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200412 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
413
414 dev_priv->fbc.fbc_work = work;
415
416 /* Delay the actual enabling to let pageflipping cease and the
417 * display to settle before starting the compression. Note that
418 * this delay also serves a second purpose: it allows for a
419 * vblank to pass after disabling the FBC before we attempt
420 * to modify the control registers.
421 *
422 * A more complicated solution would involve tracking vblanks
423 * following the termination of the page-flipping sequence
424 * and indeed performing the enable as a co-routine and not
425 * waiting synchronously upon the vblank.
426 *
427 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
428 */
429 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
430}
431
Paulo Zanoni7733b492015-07-07 15:26:04 -0300432static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300433{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300434 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
435
436 intel_fbc_cancel_work(dev_priv);
437
Paulo Zanonic68ae3392015-11-04 17:10:51 -0200438 if (dev_priv->fbc.enabled)
439 dev_priv->fbc.disable_fbc(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300440 dev_priv->fbc.crtc = NULL;
441}
442
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800443/**
444 * intel_fbc_disable - disable FBC
Paulo Zanoni7733b492015-07-07 15:26:04 -0300445 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800446 *
447 * This function disables FBC.
448 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300449void intel_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200450{
Paulo Zanoni9f218332015-09-23 12:52:27 -0300451 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300452 return;
453
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300454 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300455 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300456 mutex_unlock(&dev_priv->fbc.lock);
457}
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200458
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300459/*
460 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
461 * @crtc: the CRTC
462 *
463 * This function disables FBC if it's associated with the provided CRTC.
464 */
465void intel_fbc_disable_crtc(struct intel_crtc *crtc)
466{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300467 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200468
Paulo Zanoni9f218332015-09-23 12:52:27 -0300469 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300470 return;
471
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300472 mutex_lock(&dev_priv->fbc.lock);
473 if (dev_priv->fbc.crtc == crtc)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300474 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300475 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200476}
477
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300478static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200479 const char *reason)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200480{
481 if (dev_priv->fbc.no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300482 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200483
484 dev_priv->fbc.no_fbc_reason = reason;
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200485 DRM_DEBUG_KMS("Disabling FBC: %s\n", reason);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200486}
487
Paulo Zanoni30c58d52015-11-04 17:10:48 -0200488static bool crtc_is_valid(struct intel_crtc *crtc)
489{
490 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
491
492 if (fbc_on_pipe_a_only(dev_priv) && crtc->pipe != PIPE_A)
493 return false;
494
495 if (!intel_crtc_active(&crtc->base))
496 return false;
497
498 if (!to_intel_plane_state(crtc->base.primary->state)->visible)
499 return false;
500
501 return true;
502}
503
Paulo Zanoni95106752015-02-13 17:23:41 -0200504static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
505{
Paulo Zanoni95106752015-02-13 17:23:41 -0200506 struct drm_crtc *crtc = NULL, *tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200507 enum pipe pipe;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200508
509 for_each_pipe(dev_priv, pipe) {
510 tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
511
Paulo Zanoni30c58d52015-11-04 17:10:48 -0200512 if (crtc_is_valid(to_intel_crtc(tmp_crtc)))
Paulo Zanoni95106752015-02-13 17:23:41 -0200513 crtc = tmp_crtc;
Paulo Zanoni95106752015-02-13 17:23:41 -0200514 }
515
Paulo Zanonia4dedd52015-11-04 17:10:47 -0200516 if (!crtc)
Paulo Zanoni95106752015-02-13 17:23:41 -0200517 return NULL;
Paulo Zanoni95106752015-02-13 17:23:41 -0200518
519 return crtc;
520}
521
Paulo Zanoni232fd932015-07-07 15:26:07 -0300522static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
523{
524 enum pipe pipe;
525 int n_pipes = 0;
526 struct drm_crtc *crtc;
527
528 if (INTEL_INFO(dev_priv)->gen > 4)
529 return true;
530
531 for_each_pipe(dev_priv, pipe) {
532 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
533
534 if (intel_crtc_active(crtc) &&
535 to_intel_plane_state(crtc->primary->state)->visible)
536 n_pipes++;
537 }
538
539 return (n_pipes < 2);
540}
541
Paulo Zanoni7733b492015-07-07 15:26:04 -0300542static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300543 struct drm_mm_node *node,
544 int size,
545 int fb_cpp)
546{
Paulo Zanonifc786722015-07-02 19:25:08 -0300547 int compression_threshold = 1;
548 int ret;
Paulo Zanonia9da5122015-09-14 15:19:57 -0300549 u64 end;
550
551 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
552 * reserved range size, so it always assumes the maximum (8mb) is used.
553 * If we enable FBC using a CFB on that memory range we'll get FIFO
554 * underruns, even if that range is not reserved by the BIOS. */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700555 if (IS_BROADWELL(dev_priv) ||
556 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Paulo Zanonia9da5122015-09-14 15:19:57 -0300557 end = dev_priv->gtt.stolen_size - 8 * 1024 * 1024;
558 else
559 end = dev_priv->gtt.stolen_usable_size;
Paulo Zanonifc786722015-07-02 19:25:08 -0300560
561 /* HACK: This code depends on what we will do in *_enable_fbc. If that
562 * code changes, this code needs to change as well.
563 *
564 * The enable_fbc code will attempt to use one of our 2 compression
565 * thresholds, therefore, in that case, we only have 1 resort.
566 */
567
568 /* Try to over-allocate to reduce reallocations and fragmentation. */
Paulo Zanonia9da5122015-09-14 15:19:57 -0300569 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
570 4096, 0, end);
Paulo Zanonifc786722015-07-02 19:25:08 -0300571 if (ret == 0)
572 return compression_threshold;
573
574again:
575 /* HW's ability to limit the CFB is 1:4 */
576 if (compression_threshold > 4 ||
577 (fb_cpp == 2 && compression_threshold == 2))
578 return 0;
579
Paulo Zanonia9da5122015-09-14 15:19:57 -0300580 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
581 4096, 0, end);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300582 if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300583 return 0;
584 } else if (ret) {
585 compression_threshold <<= 1;
586 goto again;
587 } else {
588 return compression_threshold;
589 }
590}
591
Paulo Zanoni7733b492015-07-07 15:26:04 -0300592static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv, int size,
593 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300594{
Paulo Zanonifc786722015-07-02 19:25:08 -0300595 struct drm_mm_node *uninitialized_var(compressed_llb);
596 int ret;
597
Paulo Zanoni7733b492015-07-07 15:26:04 -0300598 ret = find_compression_threshold(dev_priv, &dev_priv->fbc.compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300599 size, fb_cpp);
600 if (!ret)
601 goto err_llb;
602 else if (ret > 1) {
603 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");
604
605 }
606
607 dev_priv->fbc.threshold = ret;
608
609 if (INTEL_INFO(dev_priv)->gen >= 5)
610 I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300611 else if (IS_GM45(dev_priv)) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300612 I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
613 } else {
614 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
615 if (!compressed_llb)
616 goto err_fb;
617
618 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
619 4096, 4096);
620 if (ret)
621 goto err_fb;
622
623 dev_priv->fbc.compressed_llb = compressed_llb;
624
625 I915_WRITE(FBC_CFB_BASE,
626 dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
627 I915_WRITE(FBC_LL_BASE,
628 dev_priv->mm.stolen_base + compressed_llb->start);
629 }
630
631 dev_priv->fbc.uncompressed_size = size;
632
Paulo Zanonib8bf5d72015-09-14 15:19:58 -0300633 DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
634 dev_priv->fbc.compressed_fb.size,
635 dev_priv->fbc.threshold);
Paulo Zanonifc786722015-07-02 19:25:08 -0300636
637 return 0;
638
639err_fb:
640 kfree(compressed_llb);
641 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
642err_llb:
643 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);
644 return -ENOSPC;
645}
646
Paulo Zanoni7733b492015-07-07 15:26:04 -0300647static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300648{
Paulo Zanonifc786722015-07-02 19:25:08 -0300649 if (dev_priv->fbc.uncompressed_size == 0)
650 return;
651
652 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
653
654 if (dev_priv->fbc.compressed_llb) {
655 i915_gem_stolen_remove_node(dev_priv,
656 dev_priv->fbc.compressed_llb);
657 kfree(dev_priv->fbc.compressed_llb);
658 }
659
660 dev_priv->fbc.uncompressed_size = 0;
661}
662
Paulo Zanoni7733b492015-07-07 15:26:04 -0300663void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300664{
Paulo Zanoni9f218332015-09-23 12:52:27 -0300665 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300666 return;
667
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300668 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300669 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300670 mutex_unlock(&dev_priv->fbc.lock);
671}
672
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300673/*
674 * For SKL+, the plane source size used by the hardware is based on the value we
675 * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
676 * we wrote to PIPESRC.
677 */
678static void intel_fbc_get_plane_source_size(struct intel_crtc *crtc,
679 int *width, int *height)
Paulo Zanonifc786722015-07-02 19:25:08 -0300680{
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300681 struct intel_plane_state *plane_state =
682 to_intel_plane_state(crtc->base.primary->state);
683 int w, h;
684
685 if (intel_rotation_90_or_270(plane_state->base.rotation)) {
686 w = drm_rect_height(&plane_state->src) >> 16;
687 h = drm_rect_width(&plane_state->src) >> 16;
688 } else {
689 w = drm_rect_width(&plane_state->src) >> 16;
690 h = drm_rect_height(&plane_state->src) >> 16;
691 }
692
693 if (width)
694 *width = w;
695 if (height)
696 *height = h;
697}
698
699static int intel_fbc_calculate_cfb_size(struct intel_crtc *crtc)
700{
701 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
702 struct drm_framebuffer *fb = crtc->base.primary->fb;
703 int lines;
704
705 intel_fbc_get_plane_source_size(crtc, NULL, &lines);
706 if (INTEL_INFO(dev_priv)->gen >= 7)
707 lines = min(lines, 2048);
708
Paulo Zanoni850bfaa2015-11-04 17:10:55 -0200709 /* Hardware needs the full buffer stride, not just the active area. */
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300710 return lines * fb->pitches[0];
711}
712
713static int intel_fbc_setup_cfb(struct intel_crtc *crtc)
714{
715 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
716 struct drm_framebuffer *fb = crtc->base.primary->fb;
717 int size, cpp;
718
719 size = intel_fbc_calculate_cfb_size(crtc);
720 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
721
Paulo Zanoni90d52342015-10-16 16:44:43 -0300722 if (drm_mm_node_allocated(&dev_priv->fbc.compressed_fb) &&
723 size <= dev_priv->fbc.compressed_fb.size * dev_priv->fbc.threshold)
Paulo Zanonifc786722015-07-02 19:25:08 -0300724 return 0;
725
726 /* Release any current block */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300727 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanonifc786722015-07-02 19:25:08 -0300728
Paulo Zanonic4ffd402015-10-01 19:55:57 -0300729 return intel_fbc_alloc_cfb(dev_priv, size, cpp);
Paulo Zanonifc786722015-07-02 19:25:08 -0300730}
731
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300732static bool stride_is_valid(struct drm_i915_private *dev_priv,
733 unsigned int stride)
734{
735 /* These should have been caught earlier. */
736 WARN_ON(stride < 512);
737 WARN_ON((stride & (64 - 1)) != 0);
738
739 /* Below are the additional FBC restrictions. */
740
741 if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
742 return stride == 4096 || stride == 8192;
743
744 if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
745 return false;
746
747 if (stride > 16384)
748 return false;
749
750 return true;
751}
752
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300753static bool pixel_format_is_valid(struct drm_framebuffer *fb)
754{
755 struct drm_device *dev = fb->dev;
756 struct drm_i915_private *dev_priv = dev->dev_private;
757
758 switch (fb->pixel_format) {
759 case DRM_FORMAT_XRGB8888:
760 case DRM_FORMAT_XBGR8888:
761 return true;
762 case DRM_FORMAT_XRGB1555:
763 case DRM_FORMAT_RGB565:
764 /* 16bpp not supported on gen2 */
765 if (IS_GEN2(dev))
766 return false;
767 /* WaFbcOnly1to1Ratio:ctg */
768 if (IS_G4X(dev_priv))
769 return false;
770 return true;
771 default:
772 return false;
773 }
774}
775
Paulo Zanoni856312a2015-10-01 19:57:12 -0300776/*
777 * For some reason, the hardware tracking starts looking at whatever we
778 * programmed as the display plane base address register. It does not look at
779 * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
780 * variables instead of just looking at the pipe/plane size.
781 */
782static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300783{
784 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoni856312a2015-10-01 19:57:12 -0300785 unsigned int effective_w, effective_h, max_w, max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300786
787 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
788 max_w = 4096;
789 max_h = 4096;
790 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
791 max_w = 4096;
792 max_h = 2048;
793 } else {
794 max_w = 2048;
795 max_h = 1536;
796 }
797
Paulo Zanoni856312a2015-10-01 19:57:12 -0300798 intel_fbc_get_plane_source_size(crtc, &effective_w, &effective_h);
799 effective_w += crtc->adjusted_x;
800 effective_h += crtc->adjusted_y;
801
802 return effective_w <= max_w && effective_h <= max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300803}
804
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200805/**
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300806 * __intel_fbc_update - enable/disable FBC as needed, unlocked
Paulo Zanoni7733b492015-07-07 15:26:04 -0300807 * @dev_priv: i915 device instance
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200808 *
Paulo Zanoni548043a2015-11-04 17:10:50 -0200809 * This function completely reevaluates the status of FBC, then enables,
810 * disables or maintains it on the same state.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200811 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300812static void __intel_fbc_update(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200813{
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200814 struct drm_crtc *drm_crtc = NULL;
815 struct intel_crtc *crtc;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200816 struct drm_framebuffer *fb;
817 struct drm_i915_gem_object *obj;
818 const struct drm_display_mode *adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200819
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300820 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
821
Paulo Zanoni7733b492015-07-07 15:26:04 -0300822 if (intel_vgpu_active(dev_priv->dev))
Yu Zhangbd492342015-02-10 19:05:50 +0800823 i915.enable_fbc = 0;
824
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200825 if (i915.enable_fbc < 0) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200826 set_no_fbc_reason(dev_priv, "disabled per chip default");
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200827 goto out_disable;
828 }
829
Rodrigo Viviab585de2015-03-24 12:40:09 -0700830 if (!i915.enable_fbc) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200831 set_no_fbc_reason(dev_priv, "disabled per module param");
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200832 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200833 }
834
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200835 drm_crtc = intel_fbc_find_crtc(dev_priv);
836 if (!drm_crtc) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200837 set_no_fbc_reason(dev_priv, "no output");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200838 goto out_disable;
Paulo Zanoni8df5dd52015-07-07 15:26:08 -0300839 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200840
Paulo Zanoni232fd932015-07-07 15:26:07 -0300841 if (!multiple_pipes_ok(dev_priv)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200842 set_no_fbc_reason(dev_priv, "more than one pipe active");
Paulo Zanoni232fd932015-07-07 15:26:07 -0300843 goto out_disable;
844 }
845
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200846 crtc = to_intel_crtc(drm_crtc);
847 fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200848 obj = intel_fb_obj(fb);
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200849 adjusted_mode = &crtc->config->base.adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200850
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200851 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
852 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200853 set_no_fbc_reason(dev_priv, "incompatible mode");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200854 goto out_disable;
855 }
856
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200857 if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200858 set_no_fbc_reason(dev_priv, "mode too large for compression");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200859 goto out_disable;
860 }
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300861
Paulo Zanoni7733b492015-07-07 15:26:04 -0300862 if ((INTEL_INFO(dev_priv)->gen < 4 || HAS_DDI(dev_priv)) &&
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200863 crtc->plane != PLANE_A) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200864 set_no_fbc_reason(dev_priv, "FBC unsupported on plane");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200865 goto out_disable;
866 }
867
868 /* The use of a CPU fence is mandatory in order to detect writes
869 * by the CPU to the scanout and trigger updates to the FBC.
870 */
871 if (obj->tiling_mode != I915_TILING_X ||
872 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200873 set_no_fbc_reason(dev_priv, "framebuffer not tiled or fenced");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200874 goto out_disable;
875 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300876 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200877 crtc->base.primary->state->rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200878 set_no_fbc_reason(dev_priv, "rotation unsupported");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200879 goto out_disable;
880 }
881
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300882 if (!stride_is_valid(dev_priv, fb->pitches[0])) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200883 set_no_fbc_reason(dev_priv, "framebuffer stride not supported");
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300884 goto out_disable;
885 }
886
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300887 if (!pixel_format_is_valid(fb)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200888 set_no_fbc_reason(dev_priv, "pixel format is invalid");
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300889 goto out_disable;
890 }
891
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300892 /* WaFbcExceedCdClockThreshold:hsw,bdw */
893 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200894 ilk_pipe_pixel_rate(crtc->config) >=
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300895 dev_priv->cdclk_freq * 95 / 100) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200896 set_no_fbc_reason(dev_priv, "pixel rate is too big");
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300897 goto out_disable;
898 }
899
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200900 if (intel_fbc_setup_cfb(crtc)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200901 set_no_fbc_reason(dev_priv, "not enough stolen memory");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200902 goto out_disable;
903 }
904
905 /* If the scanout has not changed, don't modify the FBC settings.
906 * Note that we make the fundamental assumption that the fb->obj
907 * cannot be unpinned (and have its GTT offset and fence revoked)
908 * without first being decoupled from the scanout and FBC disabled.
909 */
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200910 if (dev_priv->fbc.crtc == crtc &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200911 dev_priv->fbc.fb_id == fb->base.id &&
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200912 dev_priv->fbc.y == crtc->base.y)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200913 return;
914
Paulo Zanoni7733b492015-07-07 15:26:04 -0300915 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200916 /* We update FBC along two paths, after changing fb/crtc
917 * configuration (modeswitching) and after page-flipping
918 * finishes. For the latter, we know that not only did
919 * we disable the FBC at the start of the page-flip
920 * sequence, but also more than one vblank has passed.
921 *
922 * For the former case of modeswitching, it is possible
923 * to switch between two FBC valid configurations
924 * instantaneously so we do need to disable the FBC
925 * before we can modify its control registers. We also
926 * have to wait for the next vblank for that to take
927 * effect. However, since we delay enabling FBC we can
928 * assume that a vblank has passed since disabling and
929 * that we can safely alter the registers in the deferred
930 * callback.
931 *
932 * In the scenario that we go from a valid to invalid
933 * and then back to valid FBC configuration we have
934 * no strict enforcement that a vblank occurred since
935 * disabling the FBC. However, along all current pipe
936 * disabling paths we do need to wait for a vblank at
937 * some point. And we wait before enabling FBC anyway.
938 */
939 DRM_DEBUG_KMS("disabling active FBC for update\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300940 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200941 }
942
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200943 intel_fbc_schedule_enable(crtc);
Paulo Zanoni793af072015-11-04 17:10:57 -0200944 dev_priv->fbc.no_fbc_reason = "FBC enabled (not necessarily active)";
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200945 return;
946
947out_disable:
948 /* Multiple disables should be harmless */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300949 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200950 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300951 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200952 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300953 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300954}
955
956/*
957 * intel_fbc_update - enable/disable FBC as needed
Paulo Zanoni7733b492015-07-07 15:26:04 -0300958 * @dev_priv: i915 device instance
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300959 *
960 * This function reevaluates the overall state and enables or disables FBC.
961 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300962void intel_fbc_update(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300963{
Paulo Zanoni9f218332015-09-23 12:52:27 -0300964 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300965 return;
966
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300967 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300968 __intel_fbc_update(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300969 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200970}
971
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200972void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
973 unsigned int frontbuffer_bits,
974 enum fb_op_origin origin)
975{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200976 unsigned int fbc_bits;
977
Paulo Zanoni9f218332015-09-23 12:52:27 -0300978 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300979 return;
980
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200981 if (origin == ORIGIN_GTT)
982 return;
983
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300984 mutex_lock(&dev_priv->fbc.lock);
985
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300986 if (dev_priv->fbc.enabled || dev_priv->fbc.fbc_work)
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200987 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200988 else
989 fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
990
991 dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
992
993 if (dev_priv->fbc.busy_bits)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300994 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300995
996 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200997}
998
999void intel_fbc_flush(struct drm_i915_private *dev_priv,
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001000 unsigned int frontbuffer_bits, enum fb_op_origin origin)
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001001{
Paulo Zanoni9f218332015-09-23 12:52:27 -03001002 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -03001003 return;
1004
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001005 if (origin == ORIGIN_GTT)
1006 return;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001007
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001008 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001009
1010 dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
1011
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001012 if (!dev_priv->fbc.busy_bits) {
1013 __intel_fbc_disable(dev_priv);
Paulo Zanoni7733b492015-07-07 15:26:04 -03001014 __intel_fbc_update(dev_priv);
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001015 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001016
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001017 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001018}
1019
Rodrigo Vivi94b83952014-12-08 06:46:31 -08001020/**
1021 * intel_fbc_init - Initialize FBC
1022 * @dev_priv: the i915 device
1023 *
1024 * This function might be called during PM init process.
1025 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001026void intel_fbc_init(struct drm_i915_private *dev_priv)
1027{
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001028 enum pipe pipe;
1029
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001030 mutex_init(&dev_priv->fbc.lock);
Paulo Zanonib07ea0f2015-11-04 17:10:52 -02001031 dev_priv->fbc.enabled = false;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001032
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001033 if (!HAS_FBC(dev_priv)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -02001034 dev_priv->fbc.no_fbc_reason = "unsupported by this chipset";
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001035 return;
1036 }
1037
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001038 for_each_pipe(dev_priv, pipe) {
1039 dev_priv->fbc.possible_framebuffer_bits |=
1040 INTEL_FRONTBUFFER_PRIMARY(pipe);
1041
Paulo Zanoni57105022015-11-04 17:10:46 -02001042 if (fbc_on_pipe_a_only(dev_priv))
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001043 break;
1044 }
1045
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001046 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001047 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
1048 dev_priv->fbc.enable_fbc = gen7_fbc_enable;
1049 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001050 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001051 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
1052 dev_priv->fbc.enable_fbc = ilk_fbc_enable;
1053 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001054 } else if (IS_GM45(dev_priv)) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001055 dev_priv->fbc.fbc_enabled = g4x_fbc_enabled;
1056 dev_priv->fbc.enable_fbc = g4x_fbc_enable;
1057 dev_priv->fbc.disable_fbc = g4x_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001058 } else {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001059 dev_priv->fbc.fbc_enabled = i8xx_fbc_enabled;
1060 dev_priv->fbc.enable_fbc = i8xx_fbc_enable;
1061 dev_priv->fbc.disable_fbc = i8xx_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001062
1063 /* This value was pulled out of someone's hat */
1064 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1065 }
1066
Paulo Zanonib07ea0f2015-11-04 17:10:52 -02001067 /* We still don't have any sort of hardware state readout for FBC, so
1068 * disable it in case the BIOS enabled it to make sure software matches
1069 * the hardware state. */
1070 if (dev_priv->fbc.fbc_enabled(dev_priv))
1071 dev_priv->fbc.disable_fbc(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001072}