blob: 449dbe42569f96557571845f552bcd79e069c3ce [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 Zanoni7733b492015-07-07 15:26:04 -030044static void i8xx_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020045{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020046 u32 fbc_ctl;
47
48 dev_priv->fbc.enabled = false;
49
50 /* Disable compression */
51 fbc_ctl = I915_READ(FBC_CONTROL);
52 if ((fbc_ctl & FBC_CTL_EN) == 0)
53 return;
54
55 fbc_ctl &= ~FBC_CTL_EN;
56 I915_WRITE(FBC_CONTROL, fbc_ctl);
57
58 /* Wait for compressing bit to clear */
59 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
60 DRM_DEBUG_KMS("FBC idle timed out\n");
61 return;
62 }
63
64 DRM_DEBUG_KMS("disabled FBC\n");
65}
66
67static void i8xx_fbc_enable(struct drm_crtc *crtc)
68{
Paulo Zanoni7733b492015-07-07 15:26:04 -030069 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020070 struct drm_framebuffer *fb = crtc->primary->fb;
71 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
72 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73 int cfb_pitch;
74 int i;
75 u32 fbc_ctl;
76
77 dev_priv->fbc.enabled = true;
78
Jani Nikula60ee5cd2015-02-05 12:04:27 +020079 /* Note: fbc.threshold == 1 for i8xx */
80 cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020081 if (fb->pitches[0] < cfb_pitch)
82 cfb_pitch = fb->pitches[0];
83
84 /* FBC_CTL wants 32B or 64B units */
Paulo Zanoni7733b492015-07-07 15:26:04 -030085 if (IS_GEN2(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020086 cfb_pitch = (cfb_pitch / 32) - 1;
87 else
88 cfb_pitch = (cfb_pitch / 64) - 1;
89
90 /* Clear old tags */
91 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
92 I915_WRITE(FBC_TAG + (i * 4), 0);
93
Paulo Zanoni7733b492015-07-07 15:26:04 -030094 if (IS_GEN4(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020095 u32 fbc_ctl2;
96
97 /* Set it up... */
98 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
99 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
100 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
101 I915_WRITE(FBC_FENCE_OFF, crtc->y);
102 }
103
104 /* enable it... */
105 fbc_ctl = I915_READ(FBC_CONTROL);
106 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
107 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300108 if (IS_I945GM(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200109 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
110 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
111 fbc_ctl |= obj->fence_reg;
112 I915_WRITE(FBC_CONTROL, fbc_ctl);
113
114 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
115 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
116}
117
Paulo Zanoni7733b492015-07-07 15:26:04 -0300118static bool i8xx_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200119{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200120 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
121}
122
123static void g4x_fbc_enable(struct drm_crtc *crtc)
124{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300125 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200126 struct drm_framebuffer *fb = crtc->primary->fb;
127 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
129 u32 dpfc_ctl;
130
131 dev_priv->fbc.enabled = true;
132
133 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
134 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
135 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
136 else
137 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
138 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
139
140 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
141
142 /* enable it... */
143 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
144
145 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
146}
147
Paulo Zanoni7733b492015-07-07 15:26:04 -0300148static void g4x_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200149{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200150 u32 dpfc_ctl;
151
152 dev_priv->fbc.enabled = false;
153
154 /* Disable compression */
155 dpfc_ctl = I915_READ(DPFC_CONTROL);
156 if (dpfc_ctl & DPFC_CTL_EN) {
157 dpfc_ctl &= ~DPFC_CTL_EN;
158 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
159
160 DRM_DEBUG_KMS("disabled FBC\n");
161 }
162}
163
Paulo Zanoni7733b492015-07-07 15:26:04 -0300164static bool g4x_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200165{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200166 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
167}
168
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200169static void intel_fbc_nuke(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200170{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200171 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
172 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200173}
174
175static void ilk_fbc_enable(struct drm_crtc *crtc)
176{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300177 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200178 struct drm_framebuffer *fb = crtc->primary->fb;
179 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
181 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300182 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200183
184 dev_priv->fbc.enabled = true;
185
186 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
187 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300188 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200189
Paulo Zanonice65e472015-06-30 10:53:05 -0300190 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200191 case 4:
192 case 3:
193 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
194 break;
195 case 2:
196 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
197 break;
198 case 1:
199 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
200 break;
201 }
202 dpfc_ctl |= DPFC_CTL_FENCE_EN;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300203 if (IS_GEN5(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200204 dpfc_ctl |= obj->fence_reg;
205
206 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
207 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
208 /* enable it... */
209 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
210
Paulo Zanoni7733b492015-07-07 15:26:04 -0300211 if (IS_GEN6(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200212 I915_WRITE(SNB_DPFC_CTL_SA,
213 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
214 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200215 }
216
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200217 intel_fbc_nuke(dev_priv);
218
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200219 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
220}
221
Paulo Zanoni7733b492015-07-07 15:26:04 -0300222static void ilk_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200223{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200224 u32 dpfc_ctl;
225
226 dev_priv->fbc.enabled = false;
227
228 /* Disable compression */
229 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
230 if (dpfc_ctl & DPFC_CTL_EN) {
231 dpfc_ctl &= ~DPFC_CTL_EN;
232 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
233
234 DRM_DEBUG_KMS("disabled FBC\n");
235 }
236}
237
Paulo Zanoni7733b492015-07-07 15:26:04 -0300238static bool ilk_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200239{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200240 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
241}
242
243static void gen7_fbc_enable(struct drm_crtc *crtc)
244{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300245 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200246 struct drm_framebuffer *fb = crtc->primary->fb;
247 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
249 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300250 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200251
252 dev_priv->fbc.enabled = true;
253
Paulo Zanonid8514d62015-06-12 14:36:21 -0300254 dpfc_ctl = 0;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300255 if (IS_IVYBRIDGE(dev_priv))
Paulo Zanonid8514d62015-06-12 14:36:21 -0300256 dpfc_ctl |= IVB_DPFC_CTL_PLANE(intel_crtc->plane);
257
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200258 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300259 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200260
Paulo Zanonice65e472015-06-30 10:53:05 -0300261 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200262 case 4:
263 case 3:
264 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
265 break;
266 case 2:
267 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
268 break;
269 case 1:
270 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
271 break;
272 }
273
274 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
275
276 if (dev_priv->fbc.false_color)
277 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
278
279 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
280
Paulo Zanoni7733b492015-07-07 15:26:04 -0300281 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200282 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
283 I915_WRITE(ILK_DISPLAY_CHICKEN1,
284 I915_READ(ILK_DISPLAY_CHICKEN1) |
285 ILK_FBCQ_DIS);
286 } else {
287 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
288 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
289 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
290 HSW_FBCQ_DIS);
291 }
292
293 I915_WRITE(SNB_DPFC_CTL_SA,
294 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
295 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
296
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200297 intel_fbc_nuke(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200298
299 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
300}
301
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800302/**
303 * intel_fbc_enabled - Is FBC enabled?
Paulo Zanoni7733b492015-07-07 15:26:04 -0300304 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800305 *
306 * This function is used to verify the current state of FBC.
307 * FIXME: This should be tracked in the plane config eventually
308 * instead of queried at runtime for most callers.
309 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300310bool intel_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200311{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200312 return dev_priv->fbc.enabled;
313}
314
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200315static void intel_fbc_work_fn(struct work_struct *__work)
316{
317 struct intel_fbc_work *work =
318 container_of(to_delayed_work(__work),
319 struct intel_fbc_work, work);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300320 struct drm_i915_private *dev_priv = work->crtc->dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200321
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300322 mutex_lock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200323 if (work == dev_priv->fbc.fbc_work) {
324 /* Double check that we haven't switched fb without cancelling
325 * the prior work.
326 */
327 if (work->crtc->primary->fb == work->fb) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300328 dev_priv->fbc.enable_fbc(work->crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200329
Paulo Zanonie35fef22015-02-09 14:46:29 -0200330 dev_priv->fbc.crtc = to_intel_crtc(work->crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200331 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
332 dev_priv->fbc.y = work->crtc->y;
333 }
334
335 dev_priv->fbc.fbc_work = NULL;
336 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300337 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200338
339 kfree(work);
340}
341
342static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
343{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300344 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
345
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200346 if (dev_priv->fbc.fbc_work == NULL)
347 return;
348
349 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
350
351 /* Synchronisation is provided by struct_mutex and checking of
352 * dev_priv->fbc.fbc_work, so we can perform the cancellation
353 * entirely asynchronously.
354 */
355 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
356 /* tasklet was killed before being run, clean up */
357 kfree(dev_priv->fbc.fbc_work);
358
359 /* Mark the work as no longer wanted so that if it does
360 * wake-up (because the work was already running and waiting
361 * for our mutex), it will discover that is no longer
362 * necessary to run.
363 */
364 dev_priv->fbc.fbc_work = NULL;
365}
366
367static void intel_fbc_enable(struct drm_crtc *crtc)
368{
369 struct intel_fbc_work *work;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300370 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200371
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300372 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
373
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200374 intel_fbc_cancel_work(dev_priv);
375
376 work = kzalloc(sizeof(*work), GFP_KERNEL);
377 if (work == NULL) {
378 DRM_ERROR("Failed to allocate FBC work structure\n");
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300379 dev_priv->fbc.enable_fbc(crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200380 return;
381 }
382
383 work->crtc = crtc;
384 work->fb = crtc->primary->fb;
385 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
386
387 dev_priv->fbc.fbc_work = work;
388
389 /* Delay the actual enabling to let pageflipping cease and the
390 * display to settle before starting the compression. Note that
391 * this delay also serves a second purpose: it allows for a
392 * vblank to pass after disabling the FBC before we attempt
393 * to modify the control registers.
394 *
395 * A more complicated solution would involve tracking vblanks
396 * following the termination of the page-flipping sequence
397 * and indeed performing the enable as a co-routine and not
398 * waiting synchronously upon the vblank.
399 *
400 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
401 */
402 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
403}
404
Paulo Zanoni7733b492015-07-07 15:26:04 -0300405static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300406{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300407 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
408
409 intel_fbc_cancel_work(dev_priv);
410
Paulo Zanoni7733b492015-07-07 15:26:04 -0300411 dev_priv->fbc.disable_fbc(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300412 dev_priv->fbc.crtc = NULL;
413}
414
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800415/**
416 * intel_fbc_disable - disable FBC
Paulo Zanoni7733b492015-07-07 15:26:04 -0300417 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800418 *
419 * This function disables FBC.
420 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300421void intel_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200422{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300423 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300424 return;
425
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300426 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300427 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300428 mutex_unlock(&dev_priv->fbc.lock);
429}
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200430
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300431/*
432 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
433 * @crtc: the CRTC
434 *
435 * This function disables FBC if it's associated with the provided CRTC.
436 */
437void intel_fbc_disable_crtc(struct intel_crtc *crtc)
438{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300439 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200440
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300441 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300442 return;
443
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300444 mutex_lock(&dev_priv->fbc.lock);
445 if (dev_priv->fbc.crtc == crtc)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300446 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300447 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200448}
449
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300450const char *intel_no_fbc_reason_str(enum no_fbc_reason reason)
451{
452 switch (reason) {
453 case FBC_OK:
454 return "FBC enabled but currently disabled in hardware";
455 case FBC_UNSUPPORTED:
456 return "unsupported by this chipset";
457 case FBC_NO_OUTPUT:
458 return "no output";
459 case FBC_STOLEN_TOO_SMALL:
460 return "not enough stolen memory";
461 case FBC_UNSUPPORTED_MODE:
462 return "mode incompatible with compression";
463 case FBC_MODE_TOO_LARGE:
464 return "mode too large for compression";
465 case FBC_BAD_PLANE:
466 return "FBC unsupported on plane";
467 case FBC_NOT_TILED:
468 return "framebuffer not tiled or fenced";
469 case FBC_MULTIPLE_PIPES:
470 return "more than one pipe active";
471 case FBC_MODULE_PARAM:
472 return "disabled per module param";
473 case FBC_CHIP_DEFAULT:
474 return "disabled per chip default";
475 case FBC_ROTATION:
476 return "rotation unsupported";
477 default:
478 MISSING_CASE(reason);
479 return "unknown reason";
480 }
481}
482
483static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200484 enum no_fbc_reason reason)
485{
486 if (dev_priv->fbc.no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300487 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200488
489 dev_priv->fbc.no_fbc_reason = reason;
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300490 DRM_DEBUG_KMS("Disabling FBC: %s\n", intel_no_fbc_reason_str(reason));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200491}
492
Paulo Zanoni95106752015-02-13 17:23:41 -0200493static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
494{
Paulo Zanoni95106752015-02-13 17:23:41 -0200495 struct drm_crtc *crtc = NULL, *tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200496 enum pipe pipe;
Paulo Zanonie489e382015-02-13 17:23:43 -0200497 bool pipe_a_only = false, one_pipe_only = false;
Paulo Zanoni95106752015-02-13 17:23:41 -0200498
Paulo Zanoni68b92142015-02-13 17:23:42 -0200499 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
500 pipe_a_only = true;
Paulo Zanonie489e382015-02-13 17:23:43 -0200501 else if (INTEL_INFO(dev_priv)->gen <= 4)
502 one_pipe_only = true;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200503
504 for_each_pipe(dev_priv, pipe) {
505 tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
506
Paulo Zanoni95106752015-02-13 17:23:41 -0200507 if (intel_crtc_active(tmp_crtc) &&
Maarten Lankhorstb70709a2015-04-21 17:12:53 +0300508 to_intel_plane_state(tmp_crtc->primary->state)->visible) {
Paulo Zanonie489e382015-02-13 17:23:43 -0200509 if (one_pipe_only && crtc) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300510 set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES);
Paulo Zanoni95106752015-02-13 17:23:41 -0200511 return NULL;
512 }
513 crtc = tmp_crtc;
514 }
Paulo Zanoni68b92142015-02-13 17:23:42 -0200515
516 if (pipe_a_only)
517 break;
Paulo Zanoni95106752015-02-13 17:23:41 -0200518 }
519
520 if (!crtc || crtc->primary->fb == NULL) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300521 set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT);
Paulo Zanoni95106752015-02-13 17:23:41 -0200522 return NULL;
523 }
524
525 return crtc;
526}
527
Paulo Zanoni7733b492015-07-07 15:26:04 -0300528static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300529 struct drm_mm_node *node,
530 int size,
531 int fb_cpp)
532{
Paulo Zanonifc786722015-07-02 19:25:08 -0300533 int compression_threshold = 1;
534 int ret;
535
536 /* HACK: This code depends on what we will do in *_enable_fbc. If that
537 * code changes, this code needs to change as well.
538 *
539 * The enable_fbc code will attempt to use one of our 2 compression
540 * thresholds, therefore, in that case, we only have 1 resort.
541 */
542
543 /* Try to over-allocate to reduce reallocations and fragmentation. */
544 ret = i915_gem_stolen_insert_node(dev_priv, node, size <<= 1, 4096);
545 if (ret == 0)
546 return compression_threshold;
547
548again:
549 /* HW's ability to limit the CFB is 1:4 */
550 if (compression_threshold > 4 ||
551 (fb_cpp == 2 && compression_threshold == 2))
552 return 0;
553
554 ret = i915_gem_stolen_insert_node(dev_priv, node, size >>= 1, 4096);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300555 if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300556 return 0;
557 } else if (ret) {
558 compression_threshold <<= 1;
559 goto again;
560 } else {
561 return compression_threshold;
562 }
563}
564
Paulo Zanoni7733b492015-07-07 15:26:04 -0300565static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv, int size,
566 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300567{
Paulo Zanonifc786722015-07-02 19:25:08 -0300568 struct drm_mm_node *uninitialized_var(compressed_llb);
569 int ret;
570
Paulo Zanoni7733b492015-07-07 15:26:04 -0300571 ret = find_compression_threshold(dev_priv, &dev_priv->fbc.compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300572 size, fb_cpp);
573 if (!ret)
574 goto err_llb;
575 else if (ret > 1) {
576 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");
577
578 }
579
580 dev_priv->fbc.threshold = ret;
581
582 if (INTEL_INFO(dev_priv)->gen >= 5)
583 I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300584 else if (IS_GM45(dev_priv)) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300585 I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
586 } else {
587 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
588 if (!compressed_llb)
589 goto err_fb;
590
591 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
592 4096, 4096);
593 if (ret)
594 goto err_fb;
595
596 dev_priv->fbc.compressed_llb = compressed_llb;
597
598 I915_WRITE(FBC_CFB_BASE,
599 dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
600 I915_WRITE(FBC_LL_BASE,
601 dev_priv->mm.stolen_base + compressed_llb->start);
602 }
603
604 dev_priv->fbc.uncompressed_size = size;
605
606 DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n",
607 size);
608
609 return 0;
610
611err_fb:
612 kfree(compressed_llb);
613 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
614err_llb:
615 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);
616 return -ENOSPC;
617}
618
Paulo Zanoni7733b492015-07-07 15:26:04 -0300619static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300620{
Paulo Zanonifc786722015-07-02 19:25:08 -0300621 if (dev_priv->fbc.uncompressed_size == 0)
622 return;
623
624 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
625
626 if (dev_priv->fbc.compressed_llb) {
627 i915_gem_stolen_remove_node(dev_priv,
628 dev_priv->fbc.compressed_llb);
629 kfree(dev_priv->fbc.compressed_llb);
630 }
631
632 dev_priv->fbc.uncompressed_size = 0;
633}
634
Paulo Zanoni7733b492015-07-07 15:26:04 -0300635void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300636{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300637 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300638 return;
639
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300640 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300641 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300642 mutex_unlock(&dev_priv->fbc.lock);
643}
644
Paulo Zanoni7733b492015-07-07 15:26:04 -0300645static int intel_fbc_setup_cfb(struct drm_i915_private *dev_priv, int size,
646 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300647{
Paulo Zanonifc786722015-07-02 19:25:08 -0300648 if (size <= dev_priv->fbc.uncompressed_size)
649 return 0;
650
651 /* Release any current block */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300652 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanonifc786722015-07-02 19:25:08 -0300653
Paulo Zanoni7733b492015-07-07 15:26:04 -0300654 return intel_fbc_alloc_cfb(dev_priv, size, fb_cpp);
Paulo Zanonifc786722015-07-02 19:25:08 -0300655}
656
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200657/**
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300658 * __intel_fbc_update - enable/disable FBC as needed, unlocked
Paulo Zanoni7733b492015-07-07 15:26:04 -0300659 * @dev_priv: i915 device instance
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200660 *
661 * Set up the framebuffer compression hardware at mode set time. We
662 * enable it if possible:
663 * - plane A only (on pre-965)
664 * - no pixel mulitply/line duplication
665 * - no alpha buffer discard
666 * - no dual wide
667 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
668 *
669 * We can't assume that any compression will take place (worst case),
670 * so the compressed buffer has to be the same size as the uncompressed
671 * one. It also must reside (along with the line length buffer) in
672 * stolen memory.
673 *
674 * We need to enable/disable FBC on a global basis.
675 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300676static void __intel_fbc_update(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200677{
Paulo Zanoni95106752015-02-13 17:23:41 -0200678 struct drm_crtc *crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200679 struct intel_crtc *intel_crtc;
680 struct drm_framebuffer *fb;
681 struct drm_i915_gem_object *obj;
682 const struct drm_display_mode *adjusted_mode;
683 unsigned int max_width, max_height;
684
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300685 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
686
Yu Zhangbd492342015-02-10 19:05:50 +0800687 /* disable framebuffer compression in vGPU */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300688 if (intel_vgpu_active(dev_priv->dev))
Yu Zhangbd492342015-02-10 19:05:50 +0800689 i915.enable_fbc = 0;
690
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200691 if (i915.enable_fbc < 0) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300692 set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200693 goto out_disable;
694 }
695
Rodrigo Viviab585de2015-03-24 12:40:09 -0700696 if (!i915.enable_fbc) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300697 set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200698 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200699 }
700
701 /*
702 * If FBC is already on, we just have to verify that we can
703 * keep it that way...
704 * Need to disable if:
705 * - more than one pipe is active
706 * - changing FBC params (stride, fence, mode)
707 * - new fb is too large to fit in compressed buffer
708 * - going to an unsupported config (interlace, pixel multiply, etc.)
709 */
Paulo Zanoni95106752015-02-13 17:23:41 -0200710 crtc = intel_fbc_find_crtc(dev_priv);
711 if (!crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200712 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200713
714 intel_crtc = to_intel_crtc(crtc);
715 fb = crtc->primary->fb;
716 obj = intel_fb_obj(fb);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200717 adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200718
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200719 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
720 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300721 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200722 goto out_disable;
723 }
724
Paulo Zanoni7733b492015-07-07 15:26:04 -0300725 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200726 max_width = 4096;
727 max_height = 4096;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300728 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200729 max_width = 4096;
730 max_height = 2048;
731 } else {
732 max_width = 2048;
733 max_height = 1536;
734 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200735 if (intel_crtc->config->pipe_src_w > max_width ||
736 intel_crtc->config->pipe_src_h > max_height) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300737 set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200738 goto out_disable;
739 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300740 if ((INTEL_INFO(dev_priv)->gen < 4 || HAS_DDI(dev_priv)) &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200741 intel_crtc->plane != PLANE_A) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300742 set_no_fbc_reason(dev_priv, FBC_BAD_PLANE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200743 goto out_disable;
744 }
745
746 /* The use of a CPU fence is mandatory in order to detect writes
747 * by the CPU to the scanout and trigger updates to the FBC.
748 */
749 if (obj->tiling_mode != I915_TILING_X ||
750 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300751 set_no_fbc_reason(dev_priv, FBC_NOT_TILED);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200752 goto out_disable;
753 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300754 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Matt Roper8e7d6882015-01-21 16:35:41 -0800755 crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300756 set_no_fbc_reason(dev_priv, FBC_ROTATION);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200757 goto out_disable;
758 }
759
760 /* If the kernel debugger is active, always disable compression */
761 if (in_dbg_master())
762 goto out_disable;
763
Paulo Zanoni7733b492015-07-07 15:26:04 -0300764 if (intel_fbc_setup_cfb(dev_priv, obj->base.size,
Paulo Zanonifc786722015-07-02 19:25:08 -0300765 drm_format_plane_cpp(fb->pixel_format, 0))) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300766 set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200767 goto out_disable;
768 }
769
770 /* If the scanout has not changed, don't modify the FBC settings.
771 * Note that we make the fundamental assumption that the fb->obj
772 * cannot be unpinned (and have its GTT offset and fence revoked)
773 * without first being decoupled from the scanout and FBC disabled.
774 */
Paulo Zanonie35fef22015-02-09 14:46:29 -0200775 if (dev_priv->fbc.crtc == intel_crtc &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200776 dev_priv->fbc.fb_id == fb->base.id &&
777 dev_priv->fbc.y == crtc->y)
778 return;
779
Paulo Zanoni7733b492015-07-07 15:26:04 -0300780 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200781 /* We update FBC along two paths, after changing fb/crtc
782 * configuration (modeswitching) and after page-flipping
783 * finishes. For the latter, we know that not only did
784 * we disable the FBC at the start of the page-flip
785 * sequence, but also more than one vblank has passed.
786 *
787 * For the former case of modeswitching, it is possible
788 * to switch between two FBC valid configurations
789 * instantaneously so we do need to disable the FBC
790 * before we can modify its control registers. We also
791 * have to wait for the next vblank for that to take
792 * effect. However, since we delay enabling FBC we can
793 * assume that a vblank has passed since disabling and
794 * that we can safely alter the registers in the deferred
795 * callback.
796 *
797 * In the scenario that we go from a valid to invalid
798 * and then back to valid FBC configuration we have
799 * no strict enforcement that a vblank occurred since
800 * disabling the FBC. However, along all current pipe
801 * disabling paths we do need to wait for a vblank at
802 * some point. And we wait before enabling FBC anyway.
803 */
804 DRM_DEBUG_KMS("disabling active FBC for update\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300805 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200806 }
807
808 intel_fbc_enable(crtc);
809 dev_priv->fbc.no_fbc_reason = FBC_OK;
810 return;
811
812out_disable:
813 /* Multiple disables should be harmless */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300814 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200815 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300816 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200817 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300818 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300819}
820
821/*
822 * intel_fbc_update - enable/disable FBC as needed
Paulo Zanoni7733b492015-07-07 15:26:04 -0300823 * @dev_priv: i915 device instance
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300824 *
825 * This function reevaluates the overall state and enables or disables FBC.
826 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300827void intel_fbc_update(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300828{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300829 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300830 return;
831
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300832 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300833 __intel_fbc_update(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300834 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200835}
836
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200837void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
838 unsigned int frontbuffer_bits,
839 enum fb_op_origin origin)
840{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200841 unsigned int fbc_bits;
842
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300843 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300844 return;
845
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200846 if (origin == ORIGIN_GTT)
847 return;
848
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300849 mutex_lock(&dev_priv->fbc.lock);
850
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200851 if (dev_priv->fbc.enabled)
852 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
853 else if (dev_priv->fbc.fbc_work)
854 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(
855 to_intel_crtc(dev_priv->fbc.fbc_work->crtc)->pipe);
856 else
857 fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
858
859 dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
860
861 if (dev_priv->fbc.busy_bits)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300862 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300863
864 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200865}
866
867void intel_fbc_flush(struct drm_i915_private *dev_priv,
868 unsigned int frontbuffer_bits)
869{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300870 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300871 return;
872
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300873 mutex_lock(&dev_priv->fbc.lock);
874
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200875 if (!dev_priv->fbc.busy_bits)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300876 goto out;
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200877
878 dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
879
880 if (!dev_priv->fbc.busy_bits)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300881 __intel_fbc_update(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300882
883out:
884 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200885}
886
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800887/**
888 * intel_fbc_init - Initialize FBC
889 * @dev_priv: the i915 device
890 *
891 * This function might be called during PM init process.
892 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200893void intel_fbc_init(struct drm_i915_private *dev_priv)
894{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200895 enum pipe pipe;
896
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300897 mutex_init(&dev_priv->fbc.lock);
898
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200899 if (!HAS_FBC(dev_priv)) {
900 dev_priv->fbc.enabled = false;
Paulo Zanoni104618b2015-02-09 14:46:28 -0200901 dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200902 return;
903 }
904
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200905 for_each_pipe(dev_priv, pipe) {
906 dev_priv->fbc.possible_framebuffer_bits |=
907 INTEL_FRONTBUFFER_PRIMARY(pipe);
908
909 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
910 break;
911 }
912
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200913 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300914 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
915 dev_priv->fbc.enable_fbc = gen7_fbc_enable;
916 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200917 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300918 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
919 dev_priv->fbc.enable_fbc = ilk_fbc_enable;
920 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200921 } else if (IS_GM45(dev_priv)) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300922 dev_priv->fbc.fbc_enabled = g4x_fbc_enabled;
923 dev_priv->fbc.enable_fbc = g4x_fbc_enable;
924 dev_priv->fbc.disable_fbc = g4x_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200925 } else {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300926 dev_priv->fbc.fbc_enabled = i8xx_fbc_enabled;
927 dev_priv->fbc.enable_fbc = i8xx_fbc_enable;
928 dev_priv->fbc.disable_fbc = i8xx_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200929
930 /* This value was pulled out of someone's hat */
931 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
932 }
933
Paulo Zanoni7733b492015-07-07 15:26:04 -0300934 dev_priv->fbc.enabled = dev_priv->fbc.fbc_enabled(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200935}