blob: 1440bb32b912cdf35c8787f9fe958a17069bb3d4 [file] [log] [blame]
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
Rodrigo Vivi94b83952014-12-08 06:46:31 -080024/**
25 * DOC: Frame Buffer Compression (FBC)
26 *
27 * FBC tries to save memory bandwidth (and so power consumption) by
28 * compressing the amount of memory used by the display. It is total
29 * transparent to user space and completely handled in the kernel.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020030 *
31 * The benefits of FBC are mostly visible with solid backgrounds and
Rodrigo Vivi94b83952014-12-08 06:46:31 -080032 * variation-less patterns. It comes from keeping the memory footprint small
33 * and having fewer memory pages opened and accessed for refreshing the display.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020034 *
Rodrigo Vivi94b83952014-12-08 06:46:31 -080035 * i915 is responsible to reserve stolen memory for FBC and configure its
36 * offset on proper registers. The hardware takes care of all
37 * compress/decompress. However there are many known cases where we have to
38 * forcibly disable it to allow proper screen updates.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020039 */
40
Rodrigo Vivi94b83952014-12-08 06:46:31 -080041#include "intel_drv.h"
42#include "i915_drv.h"
43
Paulo Zanoni9f218332015-09-23 12:52:27 -030044static inline bool fbc_supported(struct drm_i915_private *dev_priv)
45{
Paulo Zanoni0e631ad2015-10-14 17:45:36 -030046 return dev_priv->fbc.activate != NULL;
Paulo Zanoni9f218332015-09-23 12:52:27 -030047}
48
Paulo Zanoni57105022015-11-04 17:10:46 -020049static inline bool fbc_on_pipe_a_only(struct drm_i915_private *dev_priv)
50{
51 return IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8;
52}
53
Paulo Zanonie6cd6dc2015-10-16 17:55:40 -030054static inline bool fbc_on_plane_a_only(struct drm_i915_private *dev_priv)
55{
56 return INTEL_INFO(dev_priv)->gen < 4;
57}
58
Paulo Zanoni2db33662015-09-14 15:20:03 -030059/*
60 * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the
61 * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's
62 * origin so the x and y offsets can actually fit the registers. As a
63 * consequence, the fence doesn't really start exactly at the display plane
64 * address we program because it starts at the real start of the buffer, so we
65 * have to take this into consideration here.
66 */
67static unsigned int get_crtc_fence_y_offset(struct intel_crtc *crtc)
68{
69 return crtc->base.y - crtc->adjusted_y;
70}
71
Paulo Zanonic5ecd462015-10-15 14:19:21 -030072/*
73 * For SKL+, the plane source size used by the hardware is based on the value we
74 * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
75 * we wrote to PIPESRC.
76 */
77static void intel_fbc_get_plane_source_size(struct intel_crtc *crtc,
78 int *width, int *height)
79{
80 struct intel_plane_state *plane_state =
81 to_intel_plane_state(crtc->base.primary->state);
82 int w, h;
83
84 if (intel_rotation_90_or_270(plane_state->base.rotation)) {
85 w = drm_rect_height(&plane_state->src) >> 16;
86 h = drm_rect_width(&plane_state->src) >> 16;
87 } else {
88 w = drm_rect_width(&plane_state->src) >> 16;
89 h = drm_rect_height(&plane_state->src) >> 16;
90 }
91
92 if (width)
93 *width = w;
94 if (height)
95 *height = h;
96}
97
98static int intel_fbc_calculate_cfb_size(struct intel_crtc *crtc,
99 struct drm_framebuffer *fb)
100{
101 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
102 int lines;
103
104 intel_fbc_get_plane_source_size(crtc, NULL, &lines);
105 if (INTEL_INFO(dev_priv)->gen >= 7)
106 lines = min(lines, 2048);
107
108 /* Hardware needs the full buffer stride, not just the active area. */
109 return lines * fb->pitches[0];
110}
111
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300112static void i8xx_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200113{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200114 u32 fbc_ctl;
115
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300116 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200117
118 /* Disable compression */
119 fbc_ctl = I915_READ(FBC_CONTROL);
120 if ((fbc_ctl & FBC_CTL_EN) == 0)
121 return;
122
123 fbc_ctl &= ~FBC_CTL_EN;
124 I915_WRITE(FBC_CONTROL, fbc_ctl);
125
126 /* Wait for compressing bit to clear */
127 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
128 DRM_DEBUG_KMS("FBC idle timed out\n");
129 return;
130 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200131}
132
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300133static void i8xx_fbc_activate(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200134{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300135 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
136 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200137 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200138 int cfb_pitch;
139 int i;
140 u32 fbc_ctl;
141
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300142 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200143
Jani Nikula60ee5cd2015-02-05 12:04:27 +0200144 /* Note: fbc.threshold == 1 for i8xx */
Paulo Zanoni559d9132015-10-26 18:44:25 -0200145 cfb_pitch = intel_fbc_calculate_cfb_size(crtc, fb) / FBC_LL_SIZE;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200146 if (fb->pitches[0] < cfb_pitch)
147 cfb_pitch = fb->pitches[0];
148
149 /* FBC_CTL wants 32B or 64B units */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300150 if (IS_GEN2(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200151 cfb_pitch = (cfb_pitch / 32) - 1;
152 else
153 cfb_pitch = (cfb_pitch / 64) - 1;
154
155 /* Clear old tags */
156 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
Ville Syrjälä4d110c72015-09-18 20:03:18 +0300157 I915_WRITE(FBC_TAG(i), 0);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200158
Paulo Zanoni7733b492015-07-07 15:26:04 -0300159 if (IS_GEN4(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200160 u32 fbc_ctl2;
161
162 /* Set it up... */
163 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300164 fbc_ctl2 |= FBC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200165 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300166 I915_WRITE(FBC_FENCE_OFF, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200167 }
168
169 /* enable it... */
170 fbc_ctl = I915_READ(FBC_CONTROL);
171 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
172 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300173 if (IS_I945GM(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200174 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
175 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
176 fbc_ctl |= obj->fence_reg;
177 I915_WRITE(FBC_CONTROL, fbc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200178}
179
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300180static bool i8xx_fbc_is_active(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(FBC_CONTROL) & FBC_CTL_EN;
183}
184
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300185static void g4x_fbc_activate(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200186{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300187 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
188 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200189 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200190 u32 dpfc_ctl;
191
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300192 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200193
Paulo Zanoni220285f2015-07-07 15:26:05 -0300194 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane) | DPFC_SR_EN;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200195 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
196 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
197 else
198 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
199 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
200
Paulo Zanoni2db33662015-09-14 15:20:03 -0300201 I915_WRITE(DPFC_FENCE_YOFF, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200202
203 /* enable it... */
204 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200205}
206
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300207static void g4x_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200208{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200209 u32 dpfc_ctl;
210
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300211 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200212
213 /* Disable compression */
214 dpfc_ctl = I915_READ(DPFC_CONTROL);
215 if (dpfc_ctl & DPFC_CTL_EN) {
216 dpfc_ctl &= ~DPFC_CTL_EN;
217 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200218 }
219}
220
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300221static bool g4x_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200222{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200223 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
224}
225
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200226/* This function forces a CFB recompression through the nuke operation. */
227static void intel_fbc_recompress(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200228{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200229 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
230 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200231}
232
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300233static void ilk_fbc_activate(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200234{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300235 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
236 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200237 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200238 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300239 int threshold = dev_priv->fbc.threshold;
Paulo Zanoni2db33662015-09-14 15:20:03 -0300240 unsigned int y_offset;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200241
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300242 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200243
Paulo Zanoni220285f2015-07-07 15:26:05 -0300244 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200245 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300246 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200247
Paulo Zanonice65e472015-06-30 10:53:05 -0300248 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200249 case 4:
250 case 3:
251 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
252 break;
253 case 2:
254 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
255 break;
256 case 1:
257 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
258 break;
259 }
260 dpfc_ctl |= DPFC_CTL_FENCE_EN;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300261 if (IS_GEN5(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200262 dpfc_ctl |= obj->fence_reg;
263
Paulo Zanoni2db33662015-09-14 15:20:03 -0300264 y_offset = get_crtc_fence_y_offset(crtc);
265 I915_WRITE(ILK_DPFC_FENCE_YOFF, y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200266 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
267 /* enable it... */
268 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
269
Paulo Zanoni7733b492015-07-07 15:26:04 -0300270 if (IS_GEN6(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200271 I915_WRITE(SNB_DPFC_CTL_SA,
272 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300273 I915_WRITE(DPFC_CPU_FENCE_OFFSET, y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200274 }
275
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200276 intel_fbc_recompress(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200277}
278
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300279static void ilk_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200280{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200281 u32 dpfc_ctl;
282
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300283 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200284
285 /* Disable compression */
286 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
287 if (dpfc_ctl & DPFC_CTL_EN) {
288 dpfc_ctl &= ~DPFC_CTL_EN;
289 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200290 }
291}
292
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300293static bool ilk_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200294{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200295 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
296}
297
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300298static void gen7_fbc_activate(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200299{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300300 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
301 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200302 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200303 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300304 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200305
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300306 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200307
Paulo Zanonid8514d62015-06-12 14:36:21 -0300308 dpfc_ctl = 0;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300309 if (IS_IVYBRIDGE(dev_priv))
Paulo Zanoni220285f2015-07-07 15:26:05 -0300310 dpfc_ctl |= IVB_DPFC_CTL_PLANE(crtc->plane);
Paulo Zanonid8514d62015-06-12 14:36:21 -0300311
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200312 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300313 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200314
Paulo Zanonice65e472015-06-30 10:53:05 -0300315 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200316 case 4:
317 case 3:
318 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
319 break;
320 case 2:
321 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
322 break;
323 case 1:
324 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
325 break;
326 }
327
328 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
329
330 if (dev_priv->fbc.false_color)
331 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
332
Paulo Zanoni7733b492015-07-07 15:26:04 -0300333 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200334 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
335 I915_WRITE(ILK_DISPLAY_CHICKEN1,
336 I915_READ(ILK_DISPLAY_CHICKEN1) |
337 ILK_FBCQ_DIS);
Paulo Zanoni40f40222015-09-14 15:20:01 -0300338 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200339 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Paulo Zanoni220285f2015-07-07 15:26:05 -0300340 I915_WRITE(CHICKEN_PIPESL_1(crtc->pipe),
341 I915_READ(CHICKEN_PIPESL_1(crtc->pipe)) |
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200342 HSW_FBCQ_DIS);
343 }
344
Paulo Zanoni57012be92015-09-14 15:20:00 -0300345 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
346
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200347 I915_WRITE(SNB_DPFC_CTL_SA,
348 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni2db33662015-09-14 15:20:03 -0300349 I915_WRITE(DPFC_CPU_FENCE_OFFSET, get_crtc_fence_y_offset(crtc));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200350
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200351 intel_fbc_recompress(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200352}
353
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800354/**
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300355 * intel_fbc_is_active - Is FBC active?
Paulo Zanoni7733b492015-07-07 15:26:04 -0300356 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800357 *
358 * This function is used to verify the current state of FBC.
359 * FIXME: This should be tracked in the plane config eventually
360 * instead of queried at runtime for most callers.
361 */
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300362bool intel_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200363{
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300364 return dev_priv->fbc.active;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200365}
366
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300367static void intel_fbc_activate(const struct drm_framebuffer *fb)
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300368{
Paulo Zanonie9c5fd22015-10-13 18:04:45 -0300369 struct drm_i915_private *dev_priv = fb->dev->dev_private;
370 struct intel_crtc *crtc = dev_priv->fbc.crtc;
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300371
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300372 dev_priv->fbc.activate(crtc);
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300373
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300374 dev_priv->fbc.fb_id = fb->base.id;
375 dev_priv->fbc.y = crtc->base.y;
376}
377
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200378static void intel_fbc_work_fn(struct work_struct *__work)
379{
Paulo Zanoni128d7352015-10-26 16:27:49 -0200380 struct drm_i915_private *dev_priv =
381 container_of(__work, struct drm_i915_private, fbc.work.work);
382 struct intel_fbc_work *work = &dev_priv->fbc.work;
383 struct intel_crtc *crtc = dev_priv->fbc.crtc;
Paulo Zanonica18d512016-01-21 18:03:05 -0200384 struct drm_vblank_crtc *vblank = &dev_priv->dev->vblank[crtc->pipe];
385
386 if (drm_crtc_vblank_get(&crtc->base)) {
387 DRM_ERROR("vblank not available for FBC on pipe %c\n",
388 pipe_name(crtc->pipe));
389
390 mutex_lock(&dev_priv->fbc.lock);
391 work->scheduled = false;
392 mutex_unlock(&dev_priv->fbc.lock);
393 return;
394 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200395
Paulo Zanoni128d7352015-10-26 16:27:49 -0200396retry:
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200397 /* Delay the actual enabling to let pageflipping cease and the
398 * display to settle before starting the compression. Note that
399 * this delay also serves a second purpose: it allows for a
400 * vblank to pass after disabling the FBC before we attempt
401 * to modify the control registers.
402 *
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200403 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Paulo Zanonica18d512016-01-21 18:03:05 -0200404 *
405 * It is also worth mentioning that since work->scheduled_vblank can be
406 * updated multiple times by the other threads, hitting the timeout is
407 * not an error condition. We'll just end up hitting the "goto retry"
408 * case below.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200409 */
Paulo Zanonica18d512016-01-21 18:03:05 -0200410 wait_event_timeout(vblank->queue,
411 drm_crtc_vblank_count(&crtc->base) != work->scheduled_vblank,
412 msecs_to_jiffies(50));
Paulo Zanoni128d7352015-10-26 16:27:49 -0200413
414 mutex_lock(&dev_priv->fbc.lock);
415
416 /* Were we cancelled? */
417 if (!work->scheduled)
418 goto out;
419
420 /* Were we delayed again while this function was sleeping? */
Paulo Zanonica18d512016-01-21 18:03:05 -0200421 if (drm_crtc_vblank_count(&crtc->base) == work->scheduled_vblank) {
Paulo Zanoni128d7352015-10-26 16:27:49 -0200422 mutex_unlock(&dev_priv->fbc.lock);
423 goto retry;
424 }
425
426 if (crtc->base.primary->fb == work->fb)
427 intel_fbc_activate(work->fb);
428
429 work->scheduled = false;
430
431out:
432 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonica18d512016-01-21 18:03:05 -0200433 drm_crtc_vblank_put(&crtc->base);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200434}
435
436static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
437{
438 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
439 dev_priv->fbc.work.scheduled = false;
440}
441
442static void intel_fbc_schedule_activation(struct intel_crtc *crtc)
443{
444 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
445 struct intel_fbc_work *work = &dev_priv->fbc.work;
446
447 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
448
Paulo Zanonica18d512016-01-21 18:03:05 -0200449 if (drm_crtc_vblank_get(&crtc->base)) {
450 DRM_ERROR("vblank not available for FBC on pipe %c\n",
451 pipe_name(crtc->pipe));
452 return;
453 }
454
Paulo Zanoni128d7352015-10-26 16:27:49 -0200455 /* It is useless to call intel_fbc_cancel_work() in this function since
456 * we're not releasing fbc.lock, so it won't have an opportunity to grab
457 * it to discover that it was cancelled. So we just update the expected
458 * jiffy count. */
459 work->fb = crtc->base.primary->fb;
460 work->scheduled = true;
Paulo Zanonica18d512016-01-21 18:03:05 -0200461 work->scheduled_vblank = drm_crtc_vblank_count(&crtc->base);
462 drm_crtc_vblank_put(&crtc->base);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200463
464 schedule_work(&work->work);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200465}
466
Paulo Zanonid029bca2015-10-15 10:44:46 -0300467static void __intel_fbc_deactivate(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300468{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300469 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
470
471 intel_fbc_cancel_work(dev_priv);
472
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300473 if (dev_priv->fbc.active)
474 dev_priv->fbc.deactivate(dev_priv);
Paulo Zanoni754d1132015-10-13 19:13:25 -0300475}
476
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300477/*
Paulo Zanonid029bca2015-10-15 10:44:46 -0300478 * intel_fbc_deactivate - deactivate FBC if it's associated with crtc
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300479 * @crtc: the CRTC
480 *
Paulo Zanonid029bca2015-10-15 10:44:46 -0300481 * This function deactivates FBC if it's associated with the provided CRTC.
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300482 */
Paulo Zanonid029bca2015-10-15 10:44:46 -0300483void intel_fbc_deactivate(struct intel_crtc *crtc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300484{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300485 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200486
Paulo Zanoni9f218332015-09-23 12:52:27 -0300487 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300488 return;
489
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300490 mutex_lock(&dev_priv->fbc.lock);
491 if (dev_priv->fbc.crtc == crtc)
Paulo Zanonid029bca2015-10-15 10:44:46 -0300492 __intel_fbc_deactivate(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300493 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200494}
495
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300496static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200497 const char *reason)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200498{
499 if (dev_priv->fbc.no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300500 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200501
502 dev_priv->fbc.no_fbc_reason = reason;
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200503 DRM_DEBUG_KMS("Disabling FBC: %s\n", reason);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200504}
505
Paulo Zanonid029bca2015-10-15 10:44:46 -0300506static bool crtc_can_fbc(struct intel_crtc *crtc)
Paulo Zanoni30c58d52015-11-04 17:10:48 -0200507{
508 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
509
510 if (fbc_on_pipe_a_only(dev_priv) && crtc->pipe != PIPE_A)
511 return false;
512
Paulo Zanonie6cd6dc2015-10-16 17:55:40 -0300513 if (fbc_on_plane_a_only(dev_priv) && crtc->plane != PLANE_A)
514 return false;
515
Paulo Zanonid029bca2015-10-15 10:44:46 -0300516 return true;
517}
518
Paulo Zanoni232fd932015-07-07 15:26:07 -0300519static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
520{
521 enum pipe pipe;
522 int n_pipes = 0;
523 struct drm_crtc *crtc;
524
525 if (INTEL_INFO(dev_priv)->gen > 4)
526 return true;
527
528 for_each_pipe(dev_priv, pipe) {
529 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
530
531 if (intel_crtc_active(crtc) &&
532 to_intel_plane_state(crtc->primary->state)->visible)
533 n_pipes++;
534 }
535
536 return (n_pipes < 2);
537}
538
Paulo Zanoni7733b492015-07-07 15:26:04 -0300539static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300540 struct drm_mm_node *node,
541 int size,
542 int fb_cpp)
543{
Paulo Zanonifc786722015-07-02 19:25:08 -0300544 int compression_threshold = 1;
545 int ret;
Paulo Zanonia9da5122015-09-14 15:19:57 -0300546 u64 end;
547
548 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
549 * reserved range size, so it always assumes the maximum (8mb) is used.
550 * If we enable FBC using a CFB on that memory range we'll get FIFO
551 * underruns, even if that range is not reserved by the BIOS. */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700552 if (IS_BROADWELL(dev_priv) ||
553 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Paulo Zanonia9da5122015-09-14 15:19:57 -0300554 end = dev_priv->gtt.stolen_size - 8 * 1024 * 1024;
555 else
556 end = dev_priv->gtt.stolen_usable_size;
Paulo Zanonifc786722015-07-02 19:25:08 -0300557
558 /* HACK: This code depends on what we will do in *_enable_fbc. If that
559 * code changes, this code needs to change as well.
560 *
561 * The enable_fbc code will attempt to use one of our 2 compression
562 * thresholds, therefore, in that case, we only have 1 resort.
563 */
564
565 /* Try to over-allocate to reduce reallocations and fragmentation. */
Paulo Zanonia9da5122015-09-14 15:19:57 -0300566 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
567 4096, 0, end);
Paulo Zanonifc786722015-07-02 19:25:08 -0300568 if (ret == 0)
569 return compression_threshold;
570
571again:
572 /* HW's ability to limit the CFB is 1:4 */
573 if (compression_threshold > 4 ||
574 (fb_cpp == 2 && compression_threshold == 2))
575 return 0;
576
Paulo Zanonia9da5122015-09-14 15:19:57 -0300577 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
578 4096, 0, end);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300579 if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300580 return 0;
581 } else if (ret) {
582 compression_threshold <<= 1;
583 goto again;
584 } else {
585 return compression_threshold;
586 }
587}
588
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300589static int intel_fbc_alloc_cfb(struct intel_crtc *crtc)
Paulo Zanonifc786722015-07-02 19:25:08 -0300590{
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300591 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
592 struct drm_framebuffer *fb = crtc->base.primary->state->fb;
Paulo Zanonifc786722015-07-02 19:25:08 -0300593 struct drm_mm_node *uninitialized_var(compressed_llb);
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300594 int size, fb_cpp, ret;
595
596 WARN_ON(drm_mm_node_allocated(&dev_priv->fbc.compressed_fb));
597
598 size = intel_fbc_calculate_cfb_size(crtc, fb);
599 fb_cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Paulo Zanonifc786722015-07-02 19:25:08 -0300600
Paulo Zanoni7733b492015-07-07 15:26:04 -0300601 ret = find_compression_threshold(dev_priv, &dev_priv->fbc.compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300602 size, fb_cpp);
603 if (!ret)
604 goto err_llb;
605 else if (ret > 1) {
606 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");
607
608 }
609
610 dev_priv->fbc.threshold = ret;
611
612 if (INTEL_INFO(dev_priv)->gen >= 5)
613 I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300614 else if (IS_GM45(dev_priv)) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300615 I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
616 } else {
617 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
618 if (!compressed_llb)
619 goto err_fb;
620
621 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
622 4096, 4096);
623 if (ret)
624 goto err_fb;
625
626 dev_priv->fbc.compressed_llb = compressed_llb;
627
628 I915_WRITE(FBC_CFB_BASE,
629 dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
630 I915_WRITE(FBC_LL_BASE,
631 dev_priv->mm.stolen_base + compressed_llb->start);
632 }
633
Paulo Zanonib8bf5d72015-09-14 15:19:58 -0300634 DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
635 dev_priv->fbc.compressed_fb.size,
636 dev_priv->fbc.threshold);
Paulo Zanonifc786722015-07-02 19:25:08 -0300637
638 return 0;
639
640err_fb:
641 kfree(compressed_llb);
642 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
643err_llb:
644 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);
645 return -ENOSPC;
646}
647
Paulo Zanoni7733b492015-07-07 15:26:04 -0300648static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300649{
Paulo Zanoni559d9132015-10-26 18:44:25 -0200650 if (drm_mm_node_allocated(&dev_priv->fbc.compressed_fb))
651 i915_gem_stolen_remove_node(dev_priv,
652 &dev_priv->fbc.compressed_fb);
Paulo Zanonifc786722015-07-02 19:25:08 -0300653
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 }
Paulo Zanonifc786722015-07-02 19:25:08 -0300659}
660
Paulo Zanoni7733b492015-07-07 15:26:04 -0300661void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300662{
Paulo Zanoni9f218332015-09-23 12:52:27 -0300663 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300664 return;
665
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300666 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300667 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300668 mutex_unlock(&dev_priv->fbc.lock);
669}
670
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300671static bool stride_is_valid(struct drm_i915_private *dev_priv,
672 unsigned int stride)
673{
674 /* These should have been caught earlier. */
675 WARN_ON(stride < 512);
676 WARN_ON((stride & (64 - 1)) != 0);
677
678 /* Below are the additional FBC restrictions. */
679
680 if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
681 return stride == 4096 || stride == 8192;
682
683 if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
684 return false;
685
686 if (stride > 16384)
687 return false;
688
689 return true;
690}
691
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300692static bool pixel_format_is_valid(struct drm_framebuffer *fb)
693{
694 struct drm_device *dev = fb->dev;
695 struct drm_i915_private *dev_priv = dev->dev_private;
696
697 switch (fb->pixel_format) {
698 case DRM_FORMAT_XRGB8888:
699 case DRM_FORMAT_XBGR8888:
700 return true;
701 case DRM_FORMAT_XRGB1555:
702 case DRM_FORMAT_RGB565:
703 /* 16bpp not supported on gen2 */
704 if (IS_GEN2(dev))
705 return false;
706 /* WaFbcOnly1to1Ratio:ctg */
707 if (IS_G4X(dev_priv))
708 return false;
709 return true;
710 default:
711 return false;
712 }
713}
714
Paulo Zanoni856312a2015-10-01 19:57:12 -0300715/*
716 * For some reason, the hardware tracking starts looking at whatever we
717 * programmed as the display plane base address register. It does not look at
718 * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
719 * variables instead of just looking at the pipe/plane size.
720 */
721static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300722{
723 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoni856312a2015-10-01 19:57:12 -0300724 unsigned int effective_w, effective_h, max_w, max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300725
726 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
727 max_w = 4096;
728 max_h = 4096;
729 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
730 max_w = 4096;
731 max_h = 2048;
732 } else {
733 max_w = 2048;
734 max_h = 1536;
735 }
736
Paulo Zanoni856312a2015-10-01 19:57:12 -0300737 intel_fbc_get_plane_source_size(crtc, &effective_w, &effective_h);
738 effective_w += crtc->adjusted_x;
739 effective_h += crtc->adjusted_y;
740
741 return effective_w <= max_w && effective_h <= max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300742}
743
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200744static bool intel_fbc_can_activate(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200745{
Paulo Zanoni754d1132015-10-13 19:13:25 -0300746 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200747 struct drm_plane *primary;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200748 struct drm_framebuffer *fb;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200749 struct intel_plane_state *plane_state;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200750 struct drm_i915_gem_object *obj;
751 const struct drm_display_mode *adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200752
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200753 if (!intel_crtc_active(&crtc->base)) {
754 set_no_fbc_reason(dev_priv, "CRTC not active");
755 return false;
Paulo Zanoni754d1132015-10-13 19:13:25 -0300756 }
757
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200758 primary = crtc->base.primary;
759 fb = primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200760 obj = intel_fb_obj(fb);
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200761 adjusted_mode = &crtc->config->base.adjusted_mode;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200762 plane_state = to_intel_plane_state(primary->state);
763
764 if (!plane_state->visible) {
765 set_no_fbc_reason(dev_priv, "primary plane not visible");
766 return false;
767 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200768
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200769 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
770 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200771 set_no_fbc_reason(dev_priv, "incompatible mode");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200772 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200773 }
774
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200775 if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200776 set_no_fbc_reason(dev_priv, "mode too large for compression");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200777 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200778 }
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300779
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200780 /* The use of a CPU fence is mandatory in order to detect writes
781 * by the CPU to the scanout and trigger updates to the FBC.
782 */
783 if (obj->tiling_mode != I915_TILING_X ||
784 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200785 set_no_fbc_reason(dev_priv, "framebuffer not tiled or fenced");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200786 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200787 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300788 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200789 plane_state->base.rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200790 set_no_fbc_reason(dev_priv, "rotation unsupported");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200791 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200792 }
793
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300794 if (!stride_is_valid(dev_priv, fb->pitches[0])) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200795 set_no_fbc_reason(dev_priv, "framebuffer stride not supported");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200796 return false;
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300797 }
798
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300799 if (!pixel_format_is_valid(fb)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200800 set_no_fbc_reason(dev_priv, "pixel format is invalid");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200801 return false;
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300802 }
803
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300804 /* WaFbcExceedCdClockThreshold:hsw,bdw */
805 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200806 ilk_pipe_pixel_rate(crtc->config) >=
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300807 dev_priv->cdclk_freq * 95 / 100) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200808 set_no_fbc_reason(dev_priv, "pixel rate is too big");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200809 return false;
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300810 }
811
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300812 /* It is possible for the required CFB size change without a
813 * crtc->disable + crtc->enable since it is possible to change the
814 * stride without triggering a full modeset. Since we try to
815 * over-allocate the CFB, there's a chance we may keep FBC enabled even
816 * if this happens, but if we exceed the current CFB size we'll have to
817 * disable FBC. Notice that it would be possible to disable FBC, wait
818 * for a frame, free the stolen node, then try to reenable FBC in case
819 * we didn't get any invalidate/deactivate calls, but this would require
820 * a lot of tracking just for a specific case. If we conclude it's an
821 * important case, we can implement it later. */
822 if (intel_fbc_calculate_cfb_size(crtc, fb) >
823 dev_priv->fbc.compressed_fb.size * dev_priv->fbc.threshold) {
824 set_no_fbc_reason(dev_priv, "CFB requirements changed");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200825 return false;
826 }
827
828 return true;
829}
830
831/**
832 * __intel_fbc_update - activate/deactivate FBC as needed, unlocked
833 * @crtc: the CRTC that triggered the update
834 *
835 * This function completely reevaluates the status of FBC, then activates,
836 * deactivates or maintains it on the same state.
837 */
838static void __intel_fbc_update(struct intel_crtc *crtc)
839{
840 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
841
842 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
843
844 if (!multiple_pipes_ok(dev_priv)) {
845 set_no_fbc_reason(dev_priv, "more than one pipe active");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200846 goto out_disable;
847 }
848
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200849 if (!dev_priv->fbc.enabled || dev_priv->fbc.crtc != crtc)
850 return;
851
852 if (!intel_fbc_can_activate(crtc))
853 goto out_disable;
854
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200855 /* If the scanout has not changed, don't modify the FBC settings.
856 * Note that we make the fundamental assumption that the fb->obj
857 * cannot be unpinned (and have its GTT offset and fence revoked)
858 * without first being decoupled from the scanout and FBC disabled.
859 */
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200860 if (dev_priv->fbc.crtc == crtc &&
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200861 dev_priv->fbc.fb_id == crtc->base.primary->fb->base.id &&
Paulo Zanoni754d1132015-10-13 19:13:25 -0300862 dev_priv->fbc.y == crtc->base.y &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300863 dev_priv->fbc.active)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200864 return;
865
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300866 if (intel_fbc_is_active(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200867 /* We update FBC along two paths, after changing fb/crtc
868 * configuration (modeswitching) and after page-flipping
869 * finishes. For the latter, we know that not only did
870 * we disable the FBC at the start of the page-flip
871 * sequence, but also more than one vblank has passed.
872 *
873 * For the former case of modeswitching, it is possible
874 * to switch between two FBC valid configurations
875 * instantaneously so we do need to disable the FBC
876 * before we can modify its control registers. We also
877 * have to wait for the next vblank for that to take
878 * effect. However, since we delay enabling FBC we can
879 * assume that a vblank has passed since disabling and
880 * that we can safely alter the registers in the deferred
881 * callback.
882 *
883 * In the scenario that we go from a valid to invalid
884 * and then back to valid FBC configuration we have
885 * no strict enforcement that a vblank occurred since
886 * disabling the FBC. However, along all current pipe
887 * disabling paths we do need to wait for a vblank at
888 * some point. And we wait before enabling FBC anyway.
889 */
Paulo Zanonid029bca2015-10-15 10:44:46 -0300890 DRM_DEBUG_KMS("deactivating FBC for update\n");
891 __intel_fbc_deactivate(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200892 }
893
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300894 intel_fbc_schedule_activation(crtc);
Paulo Zanoni793af072015-11-04 17:10:57 -0200895 dev_priv->fbc.no_fbc_reason = "FBC enabled (not necessarily active)";
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200896 return;
897
898out_disable:
899 /* Multiple disables should be harmless */
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300900 if (intel_fbc_is_active(dev_priv)) {
Paulo Zanonid029bca2015-10-15 10:44:46 -0300901 DRM_DEBUG_KMS("unsupported config, deactivating FBC\n");
902 __intel_fbc_deactivate(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200903 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300904}
905
906/*
Paulo Zanonid029bca2015-10-15 10:44:46 -0300907 * intel_fbc_update - activate/deactivate FBC as needed
Paulo Zanoni754d1132015-10-13 19:13:25 -0300908 * @crtc: the CRTC that triggered the update
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300909 *
Paulo Zanonid029bca2015-10-15 10:44:46 -0300910 * This function reevaluates the overall state and activates or deactivates FBC.
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300911 */
Paulo Zanoni754d1132015-10-13 19:13:25 -0300912void intel_fbc_update(struct intel_crtc *crtc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300913{
Paulo Zanoni754d1132015-10-13 19:13:25 -0300914 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
915
Paulo Zanoni9f218332015-09-23 12:52:27 -0300916 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300917 return;
918
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300919 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni754d1132015-10-13 19:13:25 -0300920 __intel_fbc_update(crtc);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300921 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200922}
923
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200924void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
925 unsigned int frontbuffer_bits,
926 enum fb_op_origin origin)
927{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200928 unsigned int fbc_bits;
929
Paulo Zanoni9f218332015-09-23 12:52:27 -0300930 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300931 return;
932
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200933 if (origin == ORIGIN_GTT)
934 return;
935
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300936 mutex_lock(&dev_priv->fbc.lock);
937
Paulo Zanonid029bca2015-10-15 10:44:46 -0300938 if (dev_priv->fbc.enabled)
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200939 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200940 else
941 fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
942
943 dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
944
945 if (dev_priv->fbc.busy_bits)
Paulo Zanonid029bca2015-10-15 10:44:46 -0300946 __intel_fbc_deactivate(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300947
948 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200949}
950
951void intel_fbc_flush(struct drm_i915_private *dev_priv,
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300952 unsigned int frontbuffer_bits, enum fb_op_origin origin)
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200953{
Paulo Zanoni9f218332015-09-23 12:52:27 -0300954 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300955 return;
956
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300957 if (origin == ORIGIN_GTT)
958 return;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300959
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300960 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200961
962 dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
963
Paulo Zanonid029bca2015-10-15 10:44:46 -0300964 if (!dev_priv->fbc.busy_bits && dev_priv->fbc.enabled) {
Paulo Zanoniee7d6cfa2015-11-11 14:46:22 -0200965 if (origin != ORIGIN_FLIP && dev_priv->fbc.active) {
966 intel_fbc_recompress(dev_priv);
967 } else {
968 __intel_fbc_deactivate(dev_priv);
969 __intel_fbc_update(dev_priv->fbc.crtc);
970 }
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300971 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300972
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300973 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200974}
975
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800976/**
Paulo Zanonid029bca2015-10-15 10:44:46 -0300977 * intel_fbc_enable: tries to enable FBC on the CRTC
978 * @crtc: the CRTC
979 *
980 * This function checks if it's possible to enable FBC on the following CRTC,
981 * then enables it. Notice that it doesn't activate FBC.
982 */
983void intel_fbc_enable(struct intel_crtc *crtc)
984{
985 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
986
987 if (!fbc_supported(dev_priv))
988 return;
989
990 mutex_lock(&dev_priv->fbc.lock);
991
992 if (dev_priv->fbc.enabled) {
993 WARN_ON(dev_priv->fbc.crtc == crtc);
994 goto out;
995 }
996
997 WARN_ON(dev_priv->fbc.active);
998 WARN_ON(dev_priv->fbc.crtc != NULL);
999
1000 if (intel_vgpu_active(dev_priv->dev)) {
1001 set_no_fbc_reason(dev_priv, "VGPU is active");
1002 goto out;
1003 }
1004
1005 if (i915.enable_fbc < 0) {
1006 set_no_fbc_reason(dev_priv, "disabled per chip default");
1007 goto out;
1008 }
1009
1010 if (!i915.enable_fbc) {
1011 set_no_fbc_reason(dev_priv, "disabled per module param");
1012 goto out;
1013 }
1014
1015 if (!crtc_can_fbc(crtc)) {
1016 set_no_fbc_reason(dev_priv, "no enabled pipes can have FBC");
1017 goto out;
1018 }
1019
Paulo Zanonic5ecd462015-10-15 14:19:21 -03001020 if (intel_fbc_alloc_cfb(crtc)) {
1021 set_no_fbc_reason(dev_priv, "not enough stolen memory");
1022 goto out;
1023 }
1024
Paulo Zanonid029bca2015-10-15 10:44:46 -03001025 DRM_DEBUG_KMS("Enabling FBC on pipe %c\n", pipe_name(crtc->pipe));
1026 dev_priv->fbc.no_fbc_reason = "FBC enabled but not active yet\n";
1027
1028 dev_priv->fbc.enabled = true;
1029 dev_priv->fbc.crtc = crtc;
1030out:
1031 mutex_unlock(&dev_priv->fbc.lock);
1032}
1033
1034/**
1035 * __intel_fbc_disable - disable FBC
1036 * @dev_priv: i915 device instance
1037 *
1038 * This is the low level function that actually disables FBC. Callers should
1039 * grab the FBC lock.
1040 */
1041static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
1042{
1043 struct intel_crtc *crtc = dev_priv->fbc.crtc;
1044
1045 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
1046 WARN_ON(!dev_priv->fbc.enabled);
1047 WARN_ON(dev_priv->fbc.active);
1048 assert_pipe_disabled(dev_priv, crtc->pipe);
1049
1050 DRM_DEBUG_KMS("Disabling FBC on pipe %c\n", pipe_name(crtc->pipe));
1051
Paulo Zanonic5ecd462015-10-15 14:19:21 -03001052 __intel_fbc_cleanup_cfb(dev_priv);
1053
Paulo Zanonid029bca2015-10-15 10:44:46 -03001054 dev_priv->fbc.enabled = false;
1055 dev_priv->fbc.crtc = NULL;
1056}
1057
1058/**
1059 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
1060 * @crtc: the CRTC
1061 *
1062 * This function disables FBC if it's associated with the provided CRTC.
1063 */
1064void intel_fbc_disable_crtc(struct intel_crtc *crtc)
1065{
1066 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1067
1068 if (!fbc_supported(dev_priv))
1069 return;
1070
1071 mutex_lock(&dev_priv->fbc.lock);
1072 if (dev_priv->fbc.crtc == crtc) {
1073 WARN_ON(!dev_priv->fbc.enabled);
1074 WARN_ON(dev_priv->fbc.active);
1075 __intel_fbc_disable(dev_priv);
1076 }
1077 mutex_unlock(&dev_priv->fbc.lock);
1078}
1079
1080/**
1081 * intel_fbc_disable - globally disable FBC
1082 * @dev_priv: i915 device instance
1083 *
1084 * This function disables FBC regardless of which CRTC is associated with it.
1085 */
1086void intel_fbc_disable(struct drm_i915_private *dev_priv)
1087{
1088 if (!fbc_supported(dev_priv))
1089 return;
1090
1091 mutex_lock(&dev_priv->fbc.lock);
1092 if (dev_priv->fbc.enabled)
1093 __intel_fbc_disable(dev_priv);
1094 mutex_unlock(&dev_priv->fbc.lock);
1095}
1096
1097/**
Rodrigo Vivi94b83952014-12-08 06:46:31 -08001098 * intel_fbc_init - Initialize FBC
1099 * @dev_priv: the i915 device
1100 *
1101 * This function might be called during PM init process.
1102 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001103void intel_fbc_init(struct drm_i915_private *dev_priv)
1104{
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001105 enum pipe pipe;
1106
Paulo Zanoni128d7352015-10-26 16:27:49 -02001107 INIT_WORK(&dev_priv->fbc.work.work, intel_fbc_work_fn);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001108 mutex_init(&dev_priv->fbc.lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001109 dev_priv->fbc.enabled = false;
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001110 dev_priv->fbc.active = false;
Paulo Zanoni128d7352015-10-26 16:27:49 -02001111 dev_priv->fbc.work.scheduled = false;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001112
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001113 if (!HAS_FBC(dev_priv)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -02001114 dev_priv->fbc.no_fbc_reason = "unsupported by this chipset";
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001115 return;
1116 }
1117
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001118 for_each_pipe(dev_priv, pipe) {
1119 dev_priv->fbc.possible_framebuffer_bits |=
1120 INTEL_FRONTBUFFER_PRIMARY(pipe);
1121
Paulo Zanoni57105022015-11-04 17:10:46 -02001122 if (fbc_on_pipe_a_only(dev_priv))
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001123 break;
1124 }
1125
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001126 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001127 dev_priv->fbc.is_active = ilk_fbc_is_active;
1128 dev_priv->fbc.activate = gen7_fbc_activate;
1129 dev_priv->fbc.deactivate = ilk_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001130 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001131 dev_priv->fbc.is_active = ilk_fbc_is_active;
1132 dev_priv->fbc.activate = ilk_fbc_activate;
1133 dev_priv->fbc.deactivate = ilk_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001134 } else if (IS_GM45(dev_priv)) {
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001135 dev_priv->fbc.is_active = g4x_fbc_is_active;
1136 dev_priv->fbc.activate = g4x_fbc_activate;
1137 dev_priv->fbc.deactivate = g4x_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001138 } else {
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001139 dev_priv->fbc.is_active = i8xx_fbc_is_active;
1140 dev_priv->fbc.activate = i8xx_fbc_activate;
1141 dev_priv->fbc.deactivate = i8xx_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001142
1143 /* This value was pulled out of someone's hat */
1144 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1145 }
1146
Paulo Zanonib07ea0f2015-11-04 17:10:52 -02001147 /* We still don't have any sort of hardware state readout for FBC, so
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001148 * deactivate it in case the BIOS activated it to make sure software
1149 * matches the hardware state. */
1150 if (dev_priv->fbc.is_active(dev_priv))
1151 dev_priv->fbc.deactivate(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001152}