blob: a0bdcef6bf273eef7c40e146b04cf65ecdf29e3e [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 Zanonib183b3f2015-12-23 18:28:11 -0200133static void i8xx_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200134{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200135 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200136 int cfb_pitch;
137 int i;
138 u32 fbc_ctl;
139
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300140 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200141
Jani Nikula60ee5cd2015-02-05 12:04:27 +0200142 /* Note: fbc.threshold == 1 for i8xx */
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200143 cfb_pitch = params->cfb_size / FBC_LL_SIZE;
144 if (params->fb.stride < cfb_pitch)
145 cfb_pitch = params->fb.stride;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200146
147 /* FBC_CTL wants 32B or 64B units */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300148 if (IS_GEN2(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200149 cfb_pitch = (cfb_pitch / 32) - 1;
150 else
151 cfb_pitch = (cfb_pitch / 64) - 1;
152
153 /* Clear old tags */
154 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
Ville Syrjälä4d110c72015-09-18 20:03:18 +0300155 I915_WRITE(FBC_TAG(i), 0);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200156
Paulo Zanoni7733b492015-07-07 15:26:04 -0300157 if (IS_GEN4(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200158 u32 fbc_ctl2;
159
160 /* Set it up... */
161 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200162 fbc_ctl2 |= FBC_CTL_PLANE(params->crtc.plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200163 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200164 I915_WRITE(FBC_FENCE_OFF, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200165 }
166
167 /* enable it... */
168 fbc_ctl = I915_READ(FBC_CONTROL);
169 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
170 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300171 if (IS_I945GM(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200172 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
173 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200174 fbc_ctl |= params->fb.fence_reg;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200175 I915_WRITE(FBC_CONTROL, fbc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200176}
177
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300178static bool i8xx_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200179{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200180 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
181}
182
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200183static void g4x_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200184{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200185 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200186 u32 dpfc_ctl;
187
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300188 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200189
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200190 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane) | DPFC_SR_EN;
191 if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200192 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
193 else
194 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200195 dpfc_ctl |= DPFC_CTL_FENCE_EN | params->fb.fence_reg;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200196
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200197 I915_WRITE(DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200198
199 /* enable it... */
200 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200201}
202
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300203static void g4x_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200204{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200205 u32 dpfc_ctl;
206
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300207 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200208
209 /* Disable compression */
210 dpfc_ctl = I915_READ(DPFC_CONTROL);
211 if (dpfc_ctl & DPFC_CTL_EN) {
212 dpfc_ctl &= ~DPFC_CTL_EN;
213 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200214 }
215}
216
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300217static bool g4x_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200218{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200219 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
220}
221
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200222/* This function forces a CFB recompression through the nuke operation. */
223static void intel_fbc_recompress(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200224{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200225 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
226 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200227}
228
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200229static void ilk_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200230{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200231 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200232 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300233 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200234
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300235 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200236
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200237 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane);
238 if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300239 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200240
Paulo Zanonice65e472015-06-30 10:53:05 -0300241 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200242 case 4:
243 case 3:
244 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
245 break;
246 case 2:
247 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
248 break;
249 case 1:
250 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
251 break;
252 }
253 dpfc_ctl |= DPFC_CTL_FENCE_EN;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300254 if (IS_GEN5(dev_priv))
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200255 dpfc_ctl |= params->fb.fence_reg;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200256
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200257 I915_WRITE(ILK_DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
258 I915_WRITE(ILK_FBC_RT_BASE, params->fb.ggtt_offset | ILK_FBC_RT_VALID);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200259 /* enable it... */
260 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
261
Paulo Zanoni7733b492015-07-07 15:26:04 -0300262 if (IS_GEN6(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200263 I915_WRITE(SNB_DPFC_CTL_SA,
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200264 SNB_CPU_FENCE_ENABLE | params->fb.fence_reg);
265 I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200266 }
267
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200268 intel_fbc_recompress(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200269}
270
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300271static void ilk_fbc_deactivate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200272{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200273 u32 dpfc_ctl;
274
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300275 dev_priv->fbc.active = false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200276
277 /* Disable compression */
278 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
279 if (dpfc_ctl & DPFC_CTL_EN) {
280 dpfc_ctl &= ~DPFC_CTL_EN;
281 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200282 }
283}
284
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300285static bool ilk_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200286{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200287 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
288}
289
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200290static void gen7_fbc_activate(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200291{
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200292 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200293 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300294 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200295
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300296 dev_priv->fbc.active = true;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200297
Paulo Zanonid8514d62015-06-12 14:36:21 -0300298 dpfc_ctl = 0;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300299 if (IS_IVYBRIDGE(dev_priv))
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200300 dpfc_ctl |= IVB_DPFC_CTL_PLANE(params->crtc.plane);
Paulo Zanonid8514d62015-06-12 14:36:21 -0300301
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200302 if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300303 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200304
Paulo Zanonice65e472015-06-30 10:53:05 -0300305 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200306 case 4:
307 case 3:
308 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
309 break;
310 case 2:
311 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
312 break;
313 case 1:
314 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
315 break;
316 }
317
318 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
319
320 if (dev_priv->fbc.false_color)
321 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
322
Paulo Zanoni7733b492015-07-07 15:26:04 -0300323 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200324 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
325 I915_WRITE(ILK_DISPLAY_CHICKEN1,
326 I915_READ(ILK_DISPLAY_CHICKEN1) |
327 ILK_FBCQ_DIS);
Paulo Zanoni40f40222015-09-14 15:20:01 -0300328 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200329 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200330 I915_WRITE(CHICKEN_PIPESL_1(params->crtc.pipe),
331 I915_READ(CHICKEN_PIPESL_1(params->crtc.pipe)) |
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200332 HSW_FBCQ_DIS);
333 }
334
Paulo Zanoni57012be92015-09-14 15:20:00 -0300335 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
336
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200337 I915_WRITE(SNB_DPFC_CTL_SA,
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200338 SNB_CPU_FENCE_ENABLE | params->fb.fence_reg);
339 I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200340
Paulo Zanonid5ce4162015-11-04 17:10:45 -0200341 intel_fbc_recompress(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200342}
343
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800344/**
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300345 * intel_fbc_is_active - Is FBC active?
Paulo Zanoni7733b492015-07-07 15:26:04 -0300346 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800347 *
348 * This function is used to verify the current state of FBC.
349 * FIXME: This should be tracked in the plane config eventually
350 * instead of queried at runtime for most callers.
351 */
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300352bool intel_fbc_is_active(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200353{
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300354 return dev_priv->fbc.active;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200355}
356
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200357static void intel_fbc_work_fn(struct work_struct *__work)
358{
Paulo Zanoni128d7352015-10-26 16:27:49 -0200359 struct drm_i915_private *dev_priv =
360 container_of(__work, struct drm_i915_private, fbc.work.work);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200361 struct intel_fbc *fbc = &dev_priv->fbc;
362 struct intel_fbc_work *work = &fbc->work;
363 struct intel_crtc *crtc = fbc->crtc;
Paulo Zanonica18d512016-01-21 18:03:05 -0200364 struct drm_vblank_crtc *vblank = &dev_priv->dev->vblank[crtc->pipe];
365
366 if (drm_crtc_vblank_get(&crtc->base)) {
367 DRM_ERROR("vblank not available for FBC on pipe %c\n",
368 pipe_name(crtc->pipe));
369
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200370 mutex_lock(&fbc->lock);
Paulo Zanonica18d512016-01-21 18:03:05 -0200371 work->scheduled = false;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200372 mutex_unlock(&fbc->lock);
Paulo Zanonica18d512016-01-21 18:03:05 -0200373 return;
374 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200375
Paulo Zanoni128d7352015-10-26 16:27:49 -0200376retry:
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200377 /* Delay the actual enabling to let pageflipping cease and the
378 * display to settle before starting the compression. Note that
379 * this delay also serves a second purpose: it allows for a
380 * vblank to pass after disabling the FBC before we attempt
381 * to modify the control registers.
382 *
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200383 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Paulo Zanonica18d512016-01-21 18:03:05 -0200384 *
385 * It is also worth mentioning that since work->scheduled_vblank can be
386 * updated multiple times by the other threads, hitting the timeout is
387 * not an error condition. We'll just end up hitting the "goto retry"
388 * case below.
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200389 */
Paulo Zanonica18d512016-01-21 18:03:05 -0200390 wait_event_timeout(vblank->queue,
391 drm_crtc_vblank_count(&crtc->base) != work->scheduled_vblank,
392 msecs_to_jiffies(50));
Paulo Zanoni128d7352015-10-26 16:27:49 -0200393
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200394 mutex_lock(&fbc->lock);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200395
396 /* Were we cancelled? */
397 if (!work->scheduled)
398 goto out;
399
400 /* Were we delayed again while this function was sleeping? */
Paulo Zanonica18d512016-01-21 18:03:05 -0200401 if (drm_crtc_vblank_count(&crtc->base) == work->scheduled_vblank) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200402 mutex_unlock(&fbc->lock);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200403 goto retry;
404 }
405
406 if (crtc->base.primary->fb == work->fb)
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200407 fbc->activate(dev_priv);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200408
409 work->scheduled = false;
410
411out:
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200412 mutex_unlock(&fbc->lock);
Paulo Zanonica18d512016-01-21 18:03:05 -0200413 drm_crtc_vblank_put(&crtc->base);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200414}
415
416static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
417{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200418 struct intel_fbc *fbc = &dev_priv->fbc;
419
420 WARN_ON(!mutex_is_locked(&fbc->lock));
421 fbc->work.scheduled = false;
Paulo Zanoni128d7352015-10-26 16:27:49 -0200422}
423
424static void intel_fbc_schedule_activation(struct intel_crtc *crtc)
425{
426 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200427 struct intel_fbc *fbc = &dev_priv->fbc;
428 struct intel_fbc_work *work = &fbc->work;
Paulo Zanoni128d7352015-10-26 16:27:49 -0200429
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200430 WARN_ON(!mutex_is_locked(&fbc->lock));
Paulo Zanoni128d7352015-10-26 16:27:49 -0200431
Paulo Zanonica18d512016-01-21 18:03:05 -0200432 if (drm_crtc_vblank_get(&crtc->base)) {
433 DRM_ERROR("vblank not available for FBC on pipe %c\n",
434 pipe_name(crtc->pipe));
435 return;
436 }
437
Paulo Zanoni128d7352015-10-26 16:27:49 -0200438 /* It is useless to call intel_fbc_cancel_work() in this function since
439 * we're not releasing fbc.lock, so it won't have an opportunity to grab
440 * it to discover that it was cancelled. So we just update the expected
441 * jiffy count. */
442 work->fb = crtc->base.primary->fb;
443 work->scheduled = true;
Paulo Zanonica18d512016-01-21 18:03:05 -0200444 work->scheduled_vblank = drm_crtc_vblank_count(&crtc->base);
445 drm_crtc_vblank_put(&crtc->base);
Paulo Zanoni128d7352015-10-26 16:27:49 -0200446
447 schedule_work(&work->work);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200448}
449
Paulo Zanonid029bca2015-10-15 10:44:46 -0300450static void __intel_fbc_deactivate(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300451{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200452 struct intel_fbc *fbc = &dev_priv->fbc;
453
454 WARN_ON(!mutex_is_locked(&fbc->lock));
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300455
456 intel_fbc_cancel_work(dev_priv);
457
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200458 if (fbc->active)
459 fbc->deactivate(dev_priv);
Paulo Zanoni754d1132015-10-13 19:13:25 -0300460}
461
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300462/*
Paulo Zanonid029bca2015-10-15 10:44:46 -0300463 * intel_fbc_deactivate - deactivate FBC if it's associated with crtc
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300464 * @crtc: the CRTC
465 *
Paulo Zanonid029bca2015-10-15 10:44:46 -0300466 * This function deactivates FBC if it's associated with the provided CRTC.
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300467 */
Paulo Zanonid029bca2015-10-15 10:44:46 -0300468void intel_fbc_deactivate(struct intel_crtc *crtc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300469{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300470 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200471 struct intel_fbc *fbc = &dev_priv->fbc;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200472
Paulo Zanoni9f218332015-09-23 12:52:27 -0300473 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300474 return;
475
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200476 mutex_lock(&fbc->lock);
477 if (fbc->crtc == crtc)
Paulo Zanonid029bca2015-10-15 10:44:46 -0300478 __intel_fbc_deactivate(dev_priv);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200479 mutex_unlock(&fbc->lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200480}
481
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300482static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200483 const char *reason)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200484{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200485 struct intel_fbc *fbc = &dev_priv->fbc;
486
487 if (fbc->no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300488 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200489
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200490 fbc->no_fbc_reason = reason;
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200491 DRM_DEBUG_KMS("Disabling FBC: %s\n", reason);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200492}
493
Paulo Zanonid029bca2015-10-15 10:44:46 -0300494static bool crtc_can_fbc(struct intel_crtc *crtc)
Paulo Zanoni30c58d52015-11-04 17:10:48 -0200495{
496 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
497
498 if (fbc_on_pipe_a_only(dev_priv) && crtc->pipe != PIPE_A)
499 return false;
500
Paulo Zanonie6cd6dc2015-10-16 17:55:40 -0300501 if (fbc_on_plane_a_only(dev_priv) && crtc->plane != PLANE_A)
502 return false;
503
Paulo Zanonid029bca2015-10-15 10:44:46 -0300504 return true;
505}
506
Paulo Zanoni232fd932015-07-07 15:26:07 -0300507static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
508{
509 enum pipe pipe;
510 int n_pipes = 0;
511 struct drm_crtc *crtc;
512
513 if (INTEL_INFO(dev_priv)->gen > 4)
514 return true;
515
516 for_each_pipe(dev_priv, pipe) {
517 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
518
519 if (intel_crtc_active(crtc) &&
520 to_intel_plane_state(crtc->primary->state)->visible)
521 n_pipes++;
522 }
523
524 return (n_pipes < 2);
525}
526
Paulo Zanoni7733b492015-07-07 15:26:04 -0300527static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300528 struct drm_mm_node *node,
529 int size,
530 int fb_cpp)
531{
Paulo Zanonifc786722015-07-02 19:25:08 -0300532 int compression_threshold = 1;
533 int ret;
Paulo Zanonia9da5122015-09-14 15:19:57 -0300534 u64 end;
535
536 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
537 * reserved range size, so it always assumes the maximum (8mb) is used.
538 * If we enable FBC using a CFB on that memory range we'll get FIFO
539 * underruns, even if that range is not reserved by the BIOS. */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700540 if (IS_BROADWELL(dev_priv) ||
541 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Paulo Zanonia9da5122015-09-14 15:19:57 -0300542 end = dev_priv->gtt.stolen_size - 8 * 1024 * 1024;
543 else
544 end = dev_priv->gtt.stolen_usable_size;
Paulo Zanonifc786722015-07-02 19:25:08 -0300545
546 /* HACK: This code depends on what we will do in *_enable_fbc. If that
547 * code changes, this code needs to change as well.
548 *
549 * The enable_fbc code will attempt to use one of our 2 compression
550 * thresholds, therefore, in that case, we only have 1 resort.
551 */
552
553 /* Try to over-allocate to reduce reallocations and fragmentation. */
Paulo Zanonia9da5122015-09-14 15:19:57 -0300554 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
555 4096, 0, end);
Paulo Zanonifc786722015-07-02 19:25:08 -0300556 if (ret == 0)
557 return compression_threshold;
558
559again:
560 /* HW's ability to limit the CFB is 1:4 */
561 if (compression_threshold > 4 ||
562 (fb_cpp == 2 && compression_threshold == 2))
563 return 0;
564
Paulo Zanonia9da5122015-09-14 15:19:57 -0300565 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
566 4096, 0, end);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300567 if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300568 return 0;
569 } else if (ret) {
570 compression_threshold <<= 1;
571 goto again;
572 } else {
573 return compression_threshold;
574 }
575}
576
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300577static int intel_fbc_alloc_cfb(struct intel_crtc *crtc)
Paulo Zanonifc786722015-07-02 19:25:08 -0300578{
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300579 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200580 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300581 struct drm_framebuffer *fb = crtc->base.primary->state->fb;
Paulo Zanonifc786722015-07-02 19:25:08 -0300582 struct drm_mm_node *uninitialized_var(compressed_llb);
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300583 int size, fb_cpp, ret;
584
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200585 WARN_ON(drm_mm_node_allocated(&fbc->compressed_fb));
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300586
587 size = intel_fbc_calculate_cfb_size(crtc, fb);
588 fb_cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Paulo Zanonifc786722015-07-02 19:25:08 -0300589
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200590 ret = find_compression_threshold(dev_priv, &fbc->compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300591 size, fb_cpp);
592 if (!ret)
593 goto err_llb;
594 else if (ret > 1) {
595 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");
596
597 }
598
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200599 fbc->threshold = ret;
Paulo Zanonifc786722015-07-02 19:25:08 -0300600
601 if (INTEL_INFO(dev_priv)->gen >= 5)
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200602 I915_WRITE(ILK_DPFC_CB_BASE, fbc->compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300603 else if (IS_GM45(dev_priv)) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200604 I915_WRITE(DPFC_CB_BASE, fbc->compressed_fb.start);
Paulo Zanonifc786722015-07-02 19:25:08 -0300605 } else {
606 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
607 if (!compressed_llb)
608 goto err_fb;
609
610 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
611 4096, 4096);
612 if (ret)
613 goto err_fb;
614
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200615 fbc->compressed_llb = compressed_llb;
Paulo Zanonifc786722015-07-02 19:25:08 -0300616
617 I915_WRITE(FBC_CFB_BASE,
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200618 dev_priv->mm.stolen_base + fbc->compressed_fb.start);
Paulo Zanonifc786722015-07-02 19:25:08 -0300619 I915_WRITE(FBC_LL_BASE,
620 dev_priv->mm.stolen_base + compressed_llb->start);
621 }
622
Paulo Zanonib8bf5d72015-09-14 15:19:58 -0300623 DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200624 fbc->compressed_fb.size, fbc->threshold);
Paulo Zanonifc786722015-07-02 19:25:08 -0300625
626 return 0;
627
628err_fb:
629 kfree(compressed_llb);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200630 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
Paulo Zanonifc786722015-07-02 19:25:08 -0300631err_llb:
632 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);
633 return -ENOSPC;
634}
635
Paulo Zanoni7733b492015-07-07 15:26:04 -0300636static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300637{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200638 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonifc786722015-07-02 19:25:08 -0300639
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200640 if (drm_mm_node_allocated(&fbc->compressed_fb))
641 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
642
643 if (fbc->compressed_llb) {
644 i915_gem_stolen_remove_node(dev_priv, fbc->compressed_llb);
645 kfree(fbc->compressed_llb);
Paulo Zanonifc786722015-07-02 19:25:08 -0300646 }
Paulo Zanonifc786722015-07-02 19:25:08 -0300647}
648
Paulo Zanoni7733b492015-07-07 15:26:04 -0300649void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300650{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200651 struct intel_fbc *fbc = &dev_priv->fbc;
652
Paulo Zanoni9f218332015-09-23 12:52:27 -0300653 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300654 return;
655
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200656 mutex_lock(&fbc->lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300657 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200658 mutex_unlock(&fbc->lock);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300659}
660
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300661static bool stride_is_valid(struct drm_i915_private *dev_priv,
662 unsigned int stride)
663{
664 /* These should have been caught earlier. */
665 WARN_ON(stride < 512);
666 WARN_ON((stride & (64 - 1)) != 0);
667
668 /* Below are the additional FBC restrictions. */
669
670 if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
671 return stride == 4096 || stride == 8192;
672
673 if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
674 return false;
675
676 if (stride > 16384)
677 return false;
678
679 return true;
680}
681
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300682static bool pixel_format_is_valid(struct drm_framebuffer *fb)
683{
684 struct drm_device *dev = fb->dev;
685 struct drm_i915_private *dev_priv = dev->dev_private;
686
687 switch (fb->pixel_format) {
688 case DRM_FORMAT_XRGB8888:
689 case DRM_FORMAT_XBGR8888:
690 return true;
691 case DRM_FORMAT_XRGB1555:
692 case DRM_FORMAT_RGB565:
693 /* 16bpp not supported on gen2 */
694 if (IS_GEN2(dev))
695 return false;
696 /* WaFbcOnly1to1Ratio:ctg */
697 if (IS_G4X(dev_priv))
698 return false;
699 return true;
700 default:
701 return false;
702 }
703}
704
Paulo Zanoni856312a2015-10-01 19:57:12 -0300705/*
706 * For some reason, the hardware tracking starts looking at whatever we
707 * programmed as the display plane base address register. It does not look at
708 * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
709 * variables instead of just looking at the pipe/plane size.
710 */
711static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300712{
713 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoni856312a2015-10-01 19:57:12 -0300714 unsigned int effective_w, effective_h, max_w, max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300715
716 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
717 max_w = 4096;
718 max_h = 4096;
719 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
720 max_w = 4096;
721 max_h = 2048;
722 } else {
723 max_w = 2048;
724 max_h = 1536;
725 }
726
Paulo Zanoni856312a2015-10-01 19:57:12 -0300727 intel_fbc_get_plane_source_size(crtc, &effective_w, &effective_h);
728 effective_w += crtc->adjusted_x;
729 effective_h += crtc->adjusted_y;
730
731 return effective_w <= max_w && effective_h <= max_h;
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300732}
733
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200734static bool intel_fbc_can_activate(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200735{
Paulo Zanoni754d1132015-10-13 19:13:25 -0300736 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200737 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200738 struct drm_plane *primary;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200739 struct drm_framebuffer *fb;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200740 struct intel_plane_state *plane_state;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200741 struct drm_i915_gem_object *obj;
742 const struct drm_display_mode *adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200743
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200744 if (!intel_crtc_active(&crtc->base)) {
745 set_no_fbc_reason(dev_priv, "CRTC not active");
746 return false;
Paulo Zanoni754d1132015-10-13 19:13:25 -0300747 }
748
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200749 primary = crtc->base.primary;
750 fb = primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200751 obj = intel_fb_obj(fb);
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200752 adjusted_mode = &crtc->config->base.adjusted_mode;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200753 plane_state = to_intel_plane_state(primary->state);
754
755 if (!plane_state->visible) {
756 set_no_fbc_reason(dev_priv, "primary plane not visible");
757 return false;
758 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200759
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200760 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
761 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200762 set_no_fbc_reason(dev_priv, "incompatible mode");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200763 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200764 }
765
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200766 if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200767 set_no_fbc_reason(dev_priv, "mode too large for compression");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200768 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200769 }
Paulo Zanoni3c5f1742015-09-23 12:52:24 -0300770
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200771 /* The use of a CPU fence is mandatory in order to detect writes
772 * by the CPU to the scanout and trigger updates to the FBC.
773 */
774 if (obj->tiling_mode != I915_TILING_X ||
775 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200776 set_no_fbc_reason(dev_priv, "framebuffer not tiled or fenced");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200777 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200778 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300779 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200780 plane_state->base.rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200781 set_no_fbc_reason(dev_priv, "rotation unsupported");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200782 return false;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200783 }
784
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300785 if (!stride_is_valid(dev_priv, fb->pitches[0])) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200786 set_no_fbc_reason(dev_priv, "framebuffer stride not supported");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200787 return false;
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300788 }
789
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300790 if (!pixel_format_is_valid(fb)) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200791 set_no_fbc_reason(dev_priv, "pixel format is invalid");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200792 return false;
Paulo Zanonib9e831d2015-09-21 19:48:06 -0300793 }
794
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300795 /* WaFbcExceedCdClockThreshold:hsw,bdw */
796 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
Paulo Zanoni45b32a22015-11-04 17:10:49 -0200797 ilk_pipe_pixel_rate(crtc->config) >=
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300798 dev_priv->cdclk_freq * 95 / 100) {
Paulo Zanonibf6189c2015-10-27 14:50:03 -0200799 set_no_fbc_reason(dev_priv, "pixel rate is too big");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200800 return false;
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300801 }
802
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300803 /* It is possible for the required CFB size change without a
804 * crtc->disable + crtc->enable since it is possible to change the
805 * stride without triggering a full modeset. Since we try to
806 * over-allocate the CFB, there's a chance we may keep FBC enabled even
807 * if this happens, but if we exceed the current CFB size we'll have to
808 * disable FBC. Notice that it would be possible to disable FBC, wait
809 * for a frame, free the stolen node, then try to reenable FBC in case
810 * we didn't get any invalidate/deactivate calls, but this would require
811 * a lot of tracking just for a specific case. If we conclude it's an
812 * important case, we can implement it later. */
813 if (intel_fbc_calculate_cfb_size(crtc, fb) >
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200814 fbc->compressed_fb.size * fbc->threshold) {
Paulo Zanonic5ecd462015-10-15 14:19:21 -0300815 set_no_fbc_reason(dev_priv, "CFB requirements changed");
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200816 return false;
817 }
818
819 return true;
820}
821
Paulo Zanoni44a8a252016-01-19 11:35:36 -0200822static bool intel_fbc_can_enable(struct intel_crtc *crtc)
823{
824 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
825
826 if (intel_vgpu_active(dev_priv->dev)) {
827 set_no_fbc_reason(dev_priv, "VGPU is active");
828 return false;
829 }
830
831 if (i915.enable_fbc < 0) {
832 set_no_fbc_reason(dev_priv, "disabled per chip default");
833 return false;
834 }
835
836 if (!i915.enable_fbc) {
837 set_no_fbc_reason(dev_priv, "disabled per module param");
838 return false;
839 }
840
841 if (!crtc_can_fbc(crtc)) {
842 set_no_fbc_reason(dev_priv, "no enabled pipes can have FBC");
843 return false;
844 }
845
846 return true;
847}
848
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200849static void intel_fbc_get_reg_params(struct intel_crtc *crtc,
850 struct intel_fbc_reg_params *params)
851{
852 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
853 struct drm_framebuffer *fb = crtc->base.primary->fb;
854 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
855
856 /* Since all our fields are integer types, use memset here so the
857 * comparison function can rely on memcmp because the padding will be
858 * zero. */
859 memset(params, 0, sizeof(*params));
860
861 params->crtc.pipe = crtc->pipe;
862 params->crtc.plane = crtc->plane;
863 params->crtc.fence_y_offset = get_crtc_fence_y_offset(crtc);
864
865 params->fb.id = fb->base.id;
866 params->fb.pixel_format = fb->pixel_format;
867 params->fb.stride = fb->pitches[0];
868 params->fb.fence_reg = obj->fence_reg;
869
870 params->cfb_size = intel_fbc_calculate_cfb_size(crtc, fb);
871
872 /* FIXME: We lack the proper locking here, so only run this on the
873 * platforms that need. */
874 if (dev_priv->fbc.activate == ilk_fbc_activate)
875 params->fb.ggtt_offset = i915_gem_obj_ggtt_offset(obj);
876}
877
878static bool intel_fbc_reg_params_equal(struct intel_fbc_reg_params *params1,
879 struct intel_fbc_reg_params *params2)
880{
881 /* We can use this since intel_fbc_get_reg_params() does a memset. */
882 return memcmp(params1, params2, sizeof(*params1)) == 0;
883}
884
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200885/**
886 * __intel_fbc_update - activate/deactivate FBC as needed, unlocked
887 * @crtc: the CRTC that triggered the update
888 *
889 * This function completely reevaluates the status of FBC, then activates,
890 * deactivates or maintains it on the same state.
891 */
892static void __intel_fbc_update(struct intel_crtc *crtc)
893{
894 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200895 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200896 struct intel_fbc_reg_params old_params;
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200897
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200898 WARN_ON(!mutex_is_locked(&fbc->lock));
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200899
900 if (!multiple_pipes_ok(dev_priv)) {
901 set_no_fbc_reason(dev_priv, "more than one pipe active");
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200902 goto out_disable;
903 }
904
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200905 if (!fbc->enabled || fbc->crtc != crtc)
Paulo Zanoni615b40d72016-01-19 11:35:35 -0200906 return;
907
908 if (!intel_fbc_can_activate(crtc))
909 goto out_disable;
910
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200911 old_params = fbc->params;
912 intel_fbc_get_reg_params(crtc, &fbc->params);
Paulo Zanonib183b3f2015-12-23 18:28:11 -0200913
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200914 /* If the scanout has not changed, don't modify the FBC settings.
915 * Note that we make the fundamental assumption that the fb->obj
916 * cannot be unpinned (and have its GTT offset and fence revoked)
917 * without first being decoupled from the scanout and FBC disabled.
918 */
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200919 if (fbc->active &&
920 intel_fbc_reg_params_equal(&old_params, &fbc->params))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200921 return;
922
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300923 if (intel_fbc_is_active(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200924 /* We update FBC along two paths, after changing fb/crtc
925 * configuration (modeswitching) and after page-flipping
926 * finishes. For the latter, we know that not only did
927 * we disable the FBC at the start of the page-flip
928 * sequence, but also more than one vblank has passed.
929 *
930 * For the former case of modeswitching, it is possible
931 * to switch between two FBC valid configurations
932 * instantaneously so we do need to disable the FBC
933 * before we can modify its control registers. We also
934 * have to wait for the next vblank for that to take
935 * effect. However, since we delay enabling FBC we can
936 * assume that a vblank has passed since disabling and
937 * that we can safely alter the registers in the deferred
938 * callback.
939 *
940 * In the scenario that we go from a valid to invalid
941 * and then back to valid FBC configuration we have
942 * no strict enforcement that a vblank occurred since
943 * disabling the FBC. However, along all current pipe
944 * disabling paths we do need to wait for a vblank at
945 * some point. And we wait before enabling FBC anyway.
946 */
Paulo Zanonid029bca2015-10-15 10:44:46 -0300947 DRM_DEBUG_KMS("deactivating FBC for update\n");
948 __intel_fbc_deactivate(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200949 }
950
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300951 intel_fbc_schedule_activation(crtc);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200952 fbc->no_fbc_reason = "FBC enabled (not necessarily active)";
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200953 return;
954
955out_disable:
956 /* Multiple disables should be harmless */
Paulo Zanoni0e631ad2015-10-14 17:45:36 -0300957 if (intel_fbc_is_active(dev_priv)) {
Paulo Zanonid029bca2015-10-15 10:44:46 -0300958 DRM_DEBUG_KMS("unsupported config, deactivating FBC\n");
959 __intel_fbc_deactivate(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200960 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300961}
962
963/*
Paulo Zanonid029bca2015-10-15 10:44:46 -0300964 * intel_fbc_update - activate/deactivate FBC as needed
Paulo Zanoni754d1132015-10-13 19:13:25 -0300965 * @crtc: the CRTC that triggered the update
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300966 *
Paulo Zanonid029bca2015-10-15 10:44:46 -0300967 * This function reevaluates the overall state and activates or deactivates FBC.
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300968 */
Paulo Zanoni754d1132015-10-13 19:13:25 -0300969void intel_fbc_update(struct intel_crtc *crtc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300970{
Paulo Zanoni754d1132015-10-13 19:13:25 -0300971 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200972 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanoni754d1132015-10-13 19:13:25 -0300973
Paulo Zanoni9f218332015-09-23 12:52:27 -0300974 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300975 return;
976
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200977 mutex_lock(&fbc->lock);
Paulo Zanoni754d1132015-10-13 19:13:25 -0300978 __intel_fbc_update(crtc);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200979 mutex_unlock(&fbc->lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200980}
981
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200982void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
983 unsigned int frontbuffer_bits,
984 enum fb_op_origin origin)
985{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200986 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200987 unsigned int fbc_bits;
988
Paulo Zanoni9f218332015-09-23 12:52:27 -0300989 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300990 return;
991
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200992 if (origin == ORIGIN_GTT)
993 return;
994
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200995 mutex_lock(&fbc->lock);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300996
Paulo Zanoniab34a7e2016-01-11 17:44:36 -0200997 if (fbc->enabled)
998 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(fbc->crtc->pipe);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200999 else
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001000 fbc_bits = fbc->possible_framebuffer_bits;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001001
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001002 fbc->busy_bits |= (fbc_bits & frontbuffer_bits);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001003
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001004 if (fbc->busy_bits)
Paulo Zanonid029bca2015-10-15 10:44:46 -03001005 __intel_fbc_deactivate(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001006
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001007 mutex_unlock(&fbc->lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001008}
1009
1010void intel_fbc_flush(struct drm_i915_private *dev_priv,
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001011 unsigned int frontbuffer_bits, enum fb_op_origin origin)
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001012{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001013 struct intel_fbc *fbc = &dev_priv->fbc;
1014
Paulo Zanoni9f218332015-09-23 12:52:27 -03001015 if (!fbc_supported(dev_priv))
Paulo Zanoni0bf73c32015-07-03 15:40:54 -03001016 return;
1017
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001018 if (origin == ORIGIN_GTT)
1019 return;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001020
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001021 mutex_lock(&fbc->lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001022
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001023 fbc->busy_bits &= ~frontbuffer_bits;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001024
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001025 if (!fbc->busy_bits && fbc->enabled) {
1026 if (origin != ORIGIN_FLIP && fbc->active) {
Paulo Zanoniee7d6cfa2015-11-11 14:46:22 -02001027 intel_fbc_recompress(dev_priv);
1028 } else {
1029 __intel_fbc_deactivate(dev_priv);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001030 __intel_fbc_update(fbc->crtc);
Paulo Zanoniee7d6cfa2015-11-11 14:46:22 -02001031 }
Paulo Zanoni6f4551f2015-07-14 16:29:10 -03001032 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001033
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001034 mutex_unlock(&fbc->lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001035}
1036
Rodrigo Vivi94b83952014-12-08 06:46:31 -08001037/**
Paulo Zanonid029bca2015-10-15 10:44:46 -03001038 * intel_fbc_enable: tries to enable FBC on the CRTC
1039 * @crtc: the CRTC
1040 *
1041 * This function checks if it's possible to enable FBC on the following CRTC,
1042 * then enables it. Notice that it doesn't activate FBC.
1043 */
1044void intel_fbc_enable(struct intel_crtc *crtc)
1045{
1046 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001047 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001048
1049 if (!fbc_supported(dev_priv))
1050 return;
1051
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001052 mutex_lock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001053
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001054 if (fbc->enabled) {
1055 WARN_ON(fbc->crtc == crtc);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001056 goto out;
1057 }
1058
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001059 WARN_ON(fbc->active);
1060 WARN_ON(fbc->crtc != NULL);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001061
Paulo Zanoni44a8a252016-01-19 11:35:36 -02001062 if (!intel_fbc_can_enable(crtc))
Paulo Zanonid029bca2015-10-15 10:44:46 -03001063 goto out;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001064
Paulo Zanonic5ecd462015-10-15 14:19:21 -03001065 if (intel_fbc_alloc_cfb(crtc)) {
1066 set_no_fbc_reason(dev_priv, "not enough stolen memory");
1067 goto out;
1068 }
1069
Paulo Zanonid029bca2015-10-15 10:44:46 -03001070 DRM_DEBUG_KMS("Enabling FBC on pipe %c\n", pipe_name(crtc->pipe));
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001071 fbc->no_fbc_reason = "FBC enabled but not active yet\n";
Paulo Zanonid029bca2015-10-15 10:44:46 -03001072
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001073 fbc->enabled = true;
1074 fbc->crtc = crtc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001075out:
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001076 mutex_unlock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001077}
1078
1079/**
1080 * __intel_fbc_disable - disable FBC
1081 * @dev_priv: i915 device instance
1082 *
1083 * This is the low level function that actually disables FBC. Callers should
1084 * grab the FBC lock.
1085 */
1086static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
1087{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001088 struct intel_fbc *fbc = &dev_priv->fbc;
1089 struct intel_crtc *crtc = fbc->crtc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001090
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001091 WARN_ON(!mutex_is_locked(&fbc->lock));
1092 WARN_ON(!fbc->enabled);
1093 WARN_ON(fbc->active);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001094 assert_pipe_disabled(dev_priv, crtc->pipe);
1095
1096 DRM_DEBUG_KMS("Disabling FBC on pipe %c\n", pipe_name(crtc->pipe));
1097
Paulo Zanonic5ecd462015-10-15 14:19:21 -03001098 __intel_fbc_cleanup_cfb(dev_priv);
1099
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001100 fbc->enabled = false;
1101 fbc->crtc = NULL;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001102}
1103
1104/**
1105 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
1106 * @crtc: the CRTC
1107 *
1108 * This function disables FBC if it's associated with the provided CRTC.
1109 */
1110void intel_fbc_disable_crtc(struct intel_crtc *crtc)
1111{
1112 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001113 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonid029bca2015-10-15 10:44:46 -03001114
1115 if (!fbc_supported(dev_priv))
1116 return;
1117
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001118 mutex_lock(&fbc->lock);
1119 if (fbc->crtc == crtc) {
1120 WARN_ON(!fbc->enabled);
1121 WARN_ON(fbc->active);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001122 __intel_fbc_disable(dev_priv);
1123 }
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001124 mutex_unlock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001125}
1126
1127/**
1128 * intel_fbc_disable - globally disable FBC
1129 * @dev_priv: i915 device instance
1130 *
1131 * This function disables FBC regardless of which CRTC is associated with it.
1132 */
1133void intel_fbc_disable(struct drm_i915_private *dev_priv)
1134{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001135 struct intel_fbc *fbc = &dev_priv->fbc;
1136
Paulo Zanonid029bca2015-10-15 10:44:46 -03001137 if (!fbc_supported(dev_priv))
1138 return;
1139
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001140 mutex_lock(&fbc->lock);
1141 if (fbc->enabled)
Paulo Zanonid029bca2015-10-15 10:44:46 -03001142 __intel_fbc_disable(dev_priv);
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001143 mutex_unlock(&fbc->lock);
Paulo Zanonid029bca2015-10-15 10:44:46 -03001144}
1145
1146/**
Rodrigo Vivi94b83952014-12-08 06:46:31 -08001147 * intel_fbc_init - Initialize FBC
1148 * @dev_priv: the i915 device
1149 *
1150 * This function might be called during PM init process.
1151 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001152void intel_fbc_init(struct drm_i915_private *dev_priv)
1153{
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001154 struct intel_fbc *fbc = &dev_priv->fbc;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001155 enum pipe pipe;
1156
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001157 INIT_WORK(&fbc->work.work, intel_fbc_work_fn);
1158 mutex_init(&fbc->lock);
1159 fbc->enabled = false;
1160 fbc->active = false;
1161 fbc->work.scheduled = false;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03001162
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001163 if (!HAS_FBC(dev_priv)) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001164 fbc->no_fbc_reason = "unsupported by this chipset";
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001165 return;
1166 }
1167
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001168 for_each_pipe(dev_priv, pipe) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001169 fbc->possible_framebuffer_bits |=
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001170 INTEL_FRONTBUFFER_PRIMARY(pipe);
1171
Paulo Zanoni57105022015-11-04 17:10:46 -02001172 if (fbc_on_pipe_a_only(dev_priv))
Paulo Zanonidbef0f12015-02-13 17:23:46 -02001173 break;
1174 }
1175
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001176 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001177 fbc->is_active = ilk_fbc_is_active;
1178 fbc->activate = gen7_fbc_activate;
1179 fbc->deactivate = ilk_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001180 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001181 fbc->is_active = ilk_fbc_is_active;
1182 fbc->activate = ilk_fbc_activate;
1183 fbc->deactivate = ilk_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001184 } else if (IS_GM45(dev_priv)) {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001185 fbc->is_active = g4x_fbc_is_active;
1186 fbc->activate = g4x_fbc_activate;
1187 fbc->deactivate = g4x_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001188 } else {
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001189 fbc->is_active = i8xx_fbc_is_active;
1190 fbc->activate = i8xx_fbc_activate;
1191 fbc->deactivate = i8xx_fbc_deactivate;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001192
1193 /* This value was pulled out of someone's hat */
1194 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1195 }
1196
Paulo Zanonib07ea0f2015-11-04 17:10:52 -02001197 /* We still don't have any sort of hardware state readout for FBC, so
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03001198 * deactivate it in case the BIOS activated it to make sure software
1199 * matches the hardware state. */
Paulo Zanoniab34a7e2016-01-11 17:44:36 -02001200 if (fbc->is_active(dev_priv))
1201 fbc->deactivate(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001202}