blob: 790edb3a2f39a5a415e869debe0c16d2cd4ee69a [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
Paulo Zanoni220285f2015-07-07 15:26:05 -030067static void i8xx_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020068{
Paulo Zanoni220285f2015-07-07 15:26:05 -030069 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
70 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020071 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020072 int cfb_pitch;
73 int i;
74 u32 fbc_ctl;
75
76 dev_priv->fbc.enabled = true;
77
Jani Nikula60ee5cd2015-02-05 12:04:27 +020078 /* Note: fbc.threshold == 1 for i8xx */
79 cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020080 if (fb->pitches[0] < cfb_pitch)
81 cfb_pitch = fb->pitches[0];
82
83 /* FBC_CTL wants 32B or 64B units */
Paulo Zanoni7733b492015-07-07 15:26:04 -030084 if (IS_GEN2(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020085 cfb_pitch = (cfb_pitch / 32) - 1;
86 else
87 cfb_pitch = (cfb_pitch / 64) - 1;
88
89 /* Clear old tags */
90 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
91 I915_WRITE(FBC_TAG + (i * 4), 0);
92
Paulo Zanoni7733b492015-07-07 15:26:04 -030093 if (IS_GEN4(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020094 u32 fbc_ctl2;
95
96 /* Set it up... */
97 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Paulo Zanoni220285f2015-07-07 15:26:05 -030098 fbc_ctl2 |= FBC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020099 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
Paulo Zanoni220285f2015-07-07 15:26:05 -0300100 I915_WRITE(FBC_FENCE_OFF, crtc->base.y);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200101 }
102
103 /* enable it... */
104 fbc_ctl = I915_READ(FBC_CONTROL);
105 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
106 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300107 if (IS_I945GM(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200108 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
109 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
110 fbc_ctl |= obj->fence_reg;
111 I915_WRITE(FBC_CONTROL, fbc_ctl);
112
113 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Paulo Zanoni220285f2015-07-07 15:26:05 -0300114 cfb_pitch, crtc->base.y, plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200115}
116
Paulo Zanoni7733b492015-07-07 15:26:04 -0300117static bool i8xx_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200118{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200119 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
120}
121
Paulo Zanoni220285f2015-07-07 15:26:05 -0300122static void g4x_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200123{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300124 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
125 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200126 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200127 u32 dpfc_ctl;
128
129 dev_priv->fbc.enabled = true;
130
Paulo Zanoni220285f2015-07-07 15:26:05 -0300131 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane) | DPFC_SR_EN;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200132 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
133 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
134 else
135 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
136 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
137
Paulo Zanoni220285f2015-07-07 15:26:05 -0300138 I915_WRITE(DPFC_FENCE_YOFF, crtc->base.y);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200139
140 /* enable it... */
141 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
142
Paulo Zanoni220285f2015-07-07 15:26:05 -0300143 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200144}
145
Paulo Zanoni7733b492015-07-07 15:26:04 -0300146static void g4x_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200147{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200148 u32 dpfc_ctl;
149
150 dev_priv->fbc.enabled = false;
151
152 /* Disable compression */
153 dpfc_ctl = I915_READ(DPFC_CONTROL);
154 if (dpfc_ctl & DPFC_CTL_EN) {
155 dpfc_ctl &= ~DPFC_CTL_EN;
156 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
157
158 DRM_DEBUG_KMS("disabled FBC\n");
159 }
160}
161
Paulo Zanoni7733b492015-07-07 15:26:04 -0300162static bool g4x_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200163{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200164 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
165}
166
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200167static void intel_fbc_nuke(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200168{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200169 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
170 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200171}
172
Paulo Zanoni220285f2015-07-07 15:26:05 -0300173static void ilk_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200174{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300175 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
176 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200177 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200178 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300179 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200180
181 dev_priv->fbc.enabled = true;
182
Paulo Zanoni220285f2015-07-07 15:26:05 -0300183 dpfc_ctl = DPFC_CTL_PLANE(crtc->plane);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200184 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300185 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200186
Paulo Zanonice65e472015-06-30 10:53:05 -0300187 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200188 case 4:
189 case 3:
190 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
191 break;
192 case 2:
193 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
194 break;
195 case 1:
196 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
197 break;
198 }
199 dpfc_ctl |= DPFC_CTL_FENCE_EN;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300200 if (IS_GEN5(dev_priv))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200201 dpfc_ctl |= obj->fence_reg;
202
Paulo Zanoni220285f2015-07-07 15:26:05 -0300203 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->base.y);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200204 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
205 /* enable it... */
206 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
207
Paulo Zanoni7733b492015-07-07 15:26:04 -0300208 if (IS_GEN6(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200209 I915_WRITE(SNB_DPFC_CTL_SA,
210 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni220285f2015-07-07 15:26:05 -0300211 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->base.y);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200212 }
213
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200214 intel_fbc_nuke(dev_priv);
215
Paulo Zanoni220285f2015-07-07 15:26:05 -0300216 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200217}
218
Paulo Zanoni7733b492015-07-07 15:26:04 -0300219static void ilk_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200220{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200221 u32 dpfc_ctl;
222
223 dev_priv->fbc.enabled = false;
224
225 /* Disable compression */
226 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
227 if (dpfc_ctl & DPFC_CTL_EN) {
228 dpfc_ctl &= ~DPFC_CTL_EN;
229 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
230
231 DRM_DEBUG_KMS("disabled FBC\n");
232 }
233}
234
Paulo Zanoni7733b492015-07-07 15:26:04 -0300235static bool ilk_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200236{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200237 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
238}
239
Paulo Zanoni220285f2015-07-07 15:26:05 -0300240static void gen7_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200241{
Paulo Zanoni220285f2015-07-07 15:26:05 -0300242 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
243 struct drm_framebuffer *fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200244 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200245 u32 dpfc_ctl;
Paulo Zanonice65e472015-06-30 10:53:05 -0300246 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200247
248 dev_priv->fbc.enabled = true;
249
Paulo Zanonid8514d62015-06-12 14:36:21 -0300250 dpfc_ctl = 0;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300251 if (IS_IVYBRIDGE(dev_priv))
Paulo Zanoni220285f2015-07-07 15:26:05 -0300252 dpfc_ctl |= IVB_DPFC_CTL_PLANE(crtc->plane);
Paulo Zanonid8514d62015-06-12 14:36:21 -0300253
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200254 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300255 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200256
Paulo Zanonice65e472015-06-30 10:53:05 -0300257 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200258 case 4:
259 case 3:
260 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
261 break;
262 case 2:
263 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
264 break;
265 case 1:
266 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
267 break;
268 }
269
270 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
271
272 if (dev_priv->fbc.false_color)
273 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
274
275 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
276
Paulo Zanoni7733b492015-07-07 15:26:04 -0300277 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200278 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
279 I915_WRITE(ILK_DISPLAY_CHICKEN1,
280 I915_READ(ILK_DISPLAY_CHICKEN1) |
281 ILK_FBCQ_DIS);
282 } else {
283 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Paulo Zanoni220285f2015-07-07 15:26:05 -0300284 I915_WRITE(CHICKEN_PIPESL_1(crtc->pipe),
285 I915_READ(CHICKEN_PIPESL_1(crtc->pipe)) |
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200286 HSW_FBCQ_DIS);
287 }
288
289 I915_WRITE(SNB_DPFC_CTL_SA,
290 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Paulo Zanoni220285f2015-07-07 15:26:05 -0300291 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->base.y);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200292
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200293 intel_fbc_nuke(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200294
Paulo Zanoni220285f2015-07-07 15:26:05 -0300295 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(crtc->plane));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200296}
297
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800298/**
299 * intel_fbc_enabled - Is FBC enabled?
Paulo Zanoni7733b492015-07-07 15:26:04 -0300300 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800301 *
302 * This function is used to verify the current state of FBC.
303 * FIXME: This should be tracked in the plane config eventually
304 * instead of queried at runtime for most callers.
305 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300306bool intel_fbc_enabled(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200307{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200308 return dev_priv->fbc.enabled;
309}
310
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200311static void intel_fbc_work_fn(struct work_struct *__work)
312{
313 struct intel_fbc_work *work =
314 container_of(to_delayed_work(__work),
315 struct intel_fbc_work, work);
Paulo Zanoni220285f2015-07-07 15:26:05 -0300316 struct drm_i915_private *dev_priv = work->crtc->base.dev->dev_private;
317 struct drm_framebuffer *crtc_fb = work->crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200318
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300319 mutex_lock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200320 if (work == dev_priv->fbc.fbc_work) {
321 /* Double check that we haven't switched fb without cancelling
322 * the prior work.
323 */
Paulo Zanoni220285f2015-07-07 15:26:05 -0300324 if (crtc_fb == work->fb) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300325 dev_priv->fbc.enable_fbc(work->crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200326
Paulo Zanoni220285f2015-07-07 15:26:05 -0300327 dev_priv->fbc.crtc = work->crtc;
328 dev_priv->fbc.fb_id = crtc_fb->base.id;
329 dev_priv->fbc.y = work->crtc->base.y;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200330 }
331
332 dev_priv->fbc.fbc_work = NULL;
333 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300334 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200335
336 kfree(work);
337}
338
339static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
340{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300341 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
342
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200343 if (dev_priv->fbc.fbc_work == NULL)
344 return;
345
346 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
347
348 /* Synchronisation is provided by struct_mutex and checking of
349 * dev_priv->fbc.fbc_work, so we can perform the cancellation
350 * entirely asynchronously.
351 */
352 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
353 /* tasklet was killed before being run, clean up */
354 kfree(dev_priv->fbc.fbc_work);
355
356 /* Mark the work as no longer wanted so that if it does
357 * wake-up (because the work was already running and waiting
358 * for our mutex), it will discover that is no longer
359 * necessary to run.
360 */
361 dev_priv->fbc.fbc_work = NULL;
362}
363
Paulo Zanoni220285f2015-07-07 15:26:05 -0300364static void intel_fbc_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200365{
366 struct intel_fbc_work *work;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300367 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200368
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300369 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
370
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200371 intel_fbc_cancel_work(dev_priv);
372
373 work = kzalloc(sizeof(*work), GFP_KERNEL);
374 if (work == NULL) {
375 DRM_ERROR("Failed to allocate FBC work structure\n");
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300376 dev_priv->fbc.enable_fbc(crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200377 return;
378 }
379
380 work->crtc = crtc;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300381 work->fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200382 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
383
384 dev_priv->fbc.fbc_work = work;
385
386 /* Delay the actual enabling to let pageflipping cease and the
387 * display to settle before starting the compression. Note that
388 * this delay also serves a second purpose: it allows for a
389 * vblank to pass after disabling the FBC before we attempt
390 * to modify the control registers.
391 *
392 * A more complicated solution would involve tracking vblanks
393 * following the termination of the page-flipping sequence
394 * and indeed performing the enable as a co-routine and not
395 * waiting synchronously upon the vblank.
396 *
397 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
398 */
399 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
400}
401
Paulo Zanoni7733b492015-07-07 15:26:04 -0300402static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300403{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300404 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
405
406 intel_fbc_cancel_work(dev_priv);
407
Paulo Zanoni7733b492015-07-07 15:26:04 -0300408 dev_priv->fbc.disable_fbc(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300409 dev_priv->fbc.crtc = NULL;
410}
411
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800412/**
413 * intel_fbc_disable - disable FBC
Paulo Zanoni7733b492015-07-07 15:26:04 -0300414 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800415 *
416 * This function disables FBC.
417 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300418void intel_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200419{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300420 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300421 return;
422
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300423 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300424 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300425 mutex_unlock(&dev_priv->fbc.lock);
426}
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200427
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300428/*
429 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
430 * @crtc: the CRTC
431 *
432 * This function disables FBC if it's associated with the provided CRTC.
433 */
434void intel_fbc_disable_crtc(struct intel_crtc *crtc)
435{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300436 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200437
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300438 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300439 return;
440
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300441 mutex_lock(&dev_priv->fbc.lock);
442 if (dev_priv->fbc.crtc == crtc)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300443 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300444 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200445}
446
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300447const char *intel_no_fbc_reason_str(enum no_fbc_reason reason)
448{
449 switch (reason) {
450 case FBC_OK:
451 return "FBC enabled but currently disabled in hardware";
452 case FBC_UNSUPPORTED:
453 return "unsupported by this chipset";
454 case FBC_NO_OUTPUT:
455 return "no output";
456 case FBC_STOLEN_TOO_SMALL:
457 return "not enough stolen memory";
458 case FBC_UNSUPPORTED_MODE:
459 return "mode incompatible with compression";
460 case FBC_MODE_TOO_LARGE:
461 return "mode too large for compression";
462 case FBC_BAD_PLANE:
463 return "FBC unsupported on plane";
464 case FBC_NOT_TILED:
465 return "framebuffer not tiled or fenced";
466 case FBC_MULTIPLE_PIPES:
467 return "more than one pipe active";
468 case FBC_MODULE_PARAM:
469 return "disabled per module param";
470 case FBC_CHIP_DEFAULT:
471 return "disabled per chip default";
472 case FBC_ROTATION:
473 return "rotation unsupported";
Paulo Zanoni89351082015-07-07 15:26:06 -0300474 case FBC_IN_DBG_MASTER:
475 return "Kernel debugger is active";
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300476 default:
477 MISSING_CASE(reason);
478 return "unknown reason";
479 }
480}
481
482static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200483 enum no_fbc_reason reason)
484{
485 if (dev_priv->fbc.no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300486 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200487
488 dev_priv->fbc.no_fbc_reason = reason;
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300489 DRM_DEBUG_KMS("Disabling FBC: %s\n", intel_no_fbc_reason_str(reason));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200490}
491
Paulo Zanoni95106752015-02-13 17:23:41 -0200492static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
493{
Paulo Zanoni95106752015-02-13 17:23:41 -0200494 struct drm_crtc *crtc = NULL, *tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200495 enum pipe pipe;
Paulo Zanoni232fd932015-07-07 15:26:07 -0300496 bool pipe_a_only = false;
Paulo Zanoni95106752015-02-13 17:23:41 -0200497
Paulo Zanoni68b92142015-02-13 17:23:42 -0200498 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
499 pipe_a_only = true;
500
501 for_each_pipe(dev_priv, pipe) {
502 tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
503
Paulo Zanoni95106752015-02-13 17:23:41 -0200504 if (intel_crtc_active(tmp_crtc) &&
Paulo Zanoni232fd932015-07-07 15:26:07 -0300505 to_intel_plane_state(tmp_crtc->primary->state)->visible)
Paulo Zanoni95106752015-02-13 17:23:41 -0200506 crtc = tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200507
508 if (pipe_a_only)
509 break;
Paulo Zanoni95106752015-02-13 17:23:41 -0200510 }
511
512 if (!crtc || crtc->primary->fb == NULL) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300513 set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT);
Paulo Zanoni95106752015-02-13 17:23:41 -0200514 return NULL;
515 }
516
517 return crtc;
518}
519
Paulo Zanoni232fd932015-07-07 15:26:07 -0300520static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
521{
522 enum pipe pipe;
523 int n_pipes = 0;
524 struct drm_crtc *crtc;
525
526 if (INTEL_INFO(dev_priv)->gen > 4)
527 return true;
528
529 for_each_pipe(dev_priv, pipe) {
530 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
531
532 if (intel_crtc_active(crtc) &&
533 to_intel_plane_state(crtc->primary->state)->visible)
534 n_pipes++;
535 }
536
537 return (n_pipes < 2);
538}
539
Paulo Zanoni7733b492015-07-07 15:26:04 -0300540static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300541 struct drm_mm_node *node,
542 int size,
543 int fb_cpp)
544{
Paulo Zanonifc786722015-07-02 19:25:08 -0300545 int compression_threshold = 1;
546 int ret;
547
548 /* HACK: This code depends on what we will do in *_enable_fbc. If that
549 * code changes, this code needs to change as well.
550 *
551 * The enable_fbc code will attempt to use one of our 2 compression
552 * thresholds, therefore, in that case, we only have 1 resort.
553 */
554
555 /* Try to over-allocate to reduce reallocations and fragmentation. */
556 ret = i915_gem_stolen_insert_node(dev_priv, node, size <<= 1, 4096);
557 if (ret == 0)
558 return compression_threshold;
559
560again:
561 /* HW's ability to limit the CFB is 1:4 */
562 if (compression_threshold > 4 ||
563 (fb_cpp == 2 && compression_threshold == 2))
564 return 0;
565
566 ret = i915_gem_stolen_insert_node(dev_priv, node, size >>= 1, 4096);
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 Zanoni7733b492015-07-07 15:26:04 -0300577static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv, int size,
578 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300579{
Paulo Zanonifc786722015-07-02 19:25:08 -0300580 struct drm_mm_node *uninitialized_var(compressed_llb);
581 int ret;
582
Paulo Zanoni7733b492015-07-07 15:26:04 -0300583 ret = find_compression_threshold(dev_priv, &dev_priv->fbc.compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300584 size, fb_cpp);
585 if (!ret)
586 goto err_llb;
587 else if (ret > 1) {
588 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");
589
590 }
591
592 dev_priv->fbc.threshold = ret;
593
594 if (INTEL_INFO(dev_priv)->gen >= 5)
595 I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300596 else if (IS_GM45(dev_priv)) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300597 I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
598 } else {
599 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
600 if (!compressed_llb)
601 goto err_fb;
602
603 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
604 4096, 4096);
605 if (ret)
606 goto err_fb;
607
608 dev_priv->fbc.compressed_llb = compressed_llb;
609
610 I915_WRITE(FBC_CFB_BASE,
611 dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
612 I915_WRITE(FBC_LL_BASE,
613 dev_priv->mm.stolen_base + compressed_llb->start);
614 }
615
616 dev_priv->fbc.uncompressed_size = size;
617
618 DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n",
619 size);
620
621 return 0;
622
623err_fb:
624 kfree(compressed_llb);
625 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
626err_llb:
627 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);
628 return -ENOSPC;
629}
630
Paulo Zanoni7733b492015-07-07 15:26:04 -0300631static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300632{
Paulo Zanonifc786722015-07-02 19:25:08 -0300633 if (dev_priv->fbc.uncompressed_size == 0)
634 return;
635
636 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
637
638 if (dev_priv->fbc.compressed_llb) {
639 i915_gem_stolen_remove_node(dev_priv,
640 dev_priv->fbc.compressed_llb);
641 kfree(dev_priv->fbc.compressed_llb);
642 }
643
644 dev_priv->fbc.uncompressed_size = 0;
645}
646
Paulo Zanoni7733b492015-07-07 15:26:04 -0300647void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300648{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300649 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300650 return;
651
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300652 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300653 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300654 mutex_unlock(&dev_priv->fbc.lock);
655}
656
Paulo Zanoni7733b492015-07-07 15:26:04 -0300657static int intel_fbc_setup_cfb(struct drm_i915_private *dev_priv, int size,
658 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300659{
Paulo Zanonifc786722015-07-02 19:25:08 -0300660 if (size <= dev_priv->fbc.uncompressed_size)
661 return 0;
662
663 /* Release any current block */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300664 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanonifc786722015-07-02 19:25:08 -0300665
Paulo Zanoni7733b492015-07-07 15:26:04 -0300666 return intel_fbc_alloc_cfb(dev_priv, size, fb_cpp);
Paulo Zanonifc786722015-07-02 19:25:08 -0300667}
668
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200669/**
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300670 * __intel_fbc_update - enable/disable FBC as needed, unlocked
Paulo Zanoni7733b492015-07-07 15:26:04 -0300671 * @dev_priv: i915 device instance
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200672 *
673 * Set up the framebuffer compression hardware at mode set time. We
674 * enable it if possible:
675 * - plane A only (on pre-965)
676 * - no pixel mulitply/line duplication
677 * - no alpha buffer discard
678 * - no dual wide
679 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
680 *
681 * We can't assume that any compression will take place (worst case),
682 * so the compressed buffer has to be the same size as the uncompressed
683 * one. It also must reside (along with the line length buffer) in
684 * stolen memory.
685 *
686 * We need to enable/disable FBC on a global basis.
687 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300688static void __intel_fbc_update(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200689{
Paulo Zanoni95106752015-02-13 17:23:41 -0200690 struct drm_crtc *crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200691 struct intel_crtc *intel_crtc;
692 struct drm_framebuffer *fb;
693 struct drm_i915_gem_object *obj;
694 const struct drm_display_mode *adjusted_mode;
695 unsigned int max_width, max_height;
696
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300697 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
698
Yu Zhangbd492342015-02-10 19:05:50 +0800699 /* disable framebuffer compression in vGPU */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300700 if (intel_vgpu_active(dev_priv->dev))
Yu Zhangbd492342015-02-10 19:05:50 +0800701 i915.enable_fbc = 0;
702
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200703 if (i915.enable_fbc < 0) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300704 set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200705 goto out_disable;
706 }
707
Rodrigo Viviab585de2015-03-24 12:40:09 -0700708 if (!i915.enable_fbc) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300709 set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200710 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200711 }
712
713 /*
714 * If FBC is already on, we just have to verify that we can
715 * keep it that way...
716 * Need to disable if:
717 * - more than one pipe is active
718 * - changing FBC params (stride, fence, mode)
719 * - new fb is too large to fit in compressed buffer
720 * - going to an unsupported config (interlace, pixel multiply, etc.)
721 */
Paulo Zanoni95106752015-02-13 17:23:41 -0200722 crtc = intel_fbc_find_crtc(dev_priv);
723 if (!crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200724 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200725
Paulo Zanoni232fd932015-07-07 15:26:07 -0300726 if (!multiple_pipes_ok(dev_priv)) {
727 set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES);
728 goto out_disable;
729 }
730
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200731 intel_crtc = to_intel_crtc(crtc);
732 fb = crtc->primary->fb;
733 obj = intel_fb_obj(fb);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200734 adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200735
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200736 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
737 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300738 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200739 goto out_disable;
740 }
741
Paulo Zanoni7733b492015-07-07 15:26:04 -0300742 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200743 max_width = 4096;
744 max_height = 4096;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300745 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200746 max_width = 4096;
747 max_height = 2048;
748 } else {
749 max_width = 2048;
750 max_height = 1536;
751 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200752 if (intel_crtc->config->pipe_src_w > max_width ||
753 intel_crtc->config->pipe_src_h > max_height) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300754 set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200755 goto out_disable;
756 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300757 if ((INTEL_INFO(dev_priv)->gen < 4 || HAS_DDI(dev_priv)) &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200758 intel_crtc->plane != PLANE_A) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300759 set_no_fbc_reason(dev_priv, FBC_BAD_PLANE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200760 goto out_disable;
761 }
762
763 /* The use of a CPU fence is mandatory in order to detect writes
764 * by the CPU to the scanout and trigger updates to the FBC.
765 */
766 if (obj->tiling_mode != I915_TILING_X ||
767 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300768 set_no_fbc_reason(dev_priv, FBC_NOT_TILED);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200769 goto out_disable;
770 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300771 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Matt Roper8e7d6882015-01-21 16:35:41 -0800772 crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300773 set_no_fbc_reason(dev_priv, FBC_ROTATION);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200774 goto out_disable;
775 }
776
777 /* If the kernel debugger is active, always disable compression */
Paulo Zanoni89351082015-07-07 15:26:06 -0300778 if (in_dbg_master()) {
779 set_no_fbc_reason(dev_priv, FBC_IN_DBG_MASTER);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200780 goto out_disable;
Paulo Zanoni89351082015-07-07 15:26:06 -0300781 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200782
Paulo Zanoni7733b492015-07-07 15:26:04 -0300783 if (intel_fbc_setup_cfb(dev_priv, obj->base.size,
Paulo Zanonifc786722015-07-02 19:25:08 -0300784 drm_format_plane_cpp(fb->pixel_format, 0))) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300785 set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200786 goto out_disable;
787 }
788
789 /* If the scanout has not changed, don't modify the FBC settings.
790 * Note that we make the fundamental assumption that the fb->obj
791 * cannot be unpinned (and have its GTT offset and fence revoked)
792 * without first being decoupled from the scanout and FBC disabled.
793 */
Paulo Zanonie35fef22015-02-09 14:46:29 -0200794 if (dev_priv->fbc.crtc == intel_crtc &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200795 dev_priv->fbc.fb_id == fb->base.id &&
796 dev_priv->fbc.y == crtc->y)
797 return;
798
Paulo Zanoni7733b492015-07-07 15:26:04 -0300799 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200800 /* We update FBC along two paths, after changing fb/crtc
801 * configuration (modeswitching) and after page-flipping
802 * finishes. For the latter, we know that not only did
803 * we disable the FBC at the start of the page-flip
804 * sequence, but also more than one vblank has passed.
805 *
806 * For the former case of modeswitching, it is possible
807 * to switch between two FBC valid configurations
808 * instantaneously so we do need to disable the FBC
809 * before we can modify its control registers. We also
810 * have to wait for the next vblank for that to take
811 * effect. However, since we delay enabling FBC we can
812 * assume that a vblank has passed since disabling and
813 * that we can safely alter the registers in the deferred
814 * callback.
815 *
816 * In the scenario that we go from a valid to invalid
817 * and then back to valid FBC configuration we have
818 * no strict enforcement that a vblank occurred since
819 * disabling the FBC. However, along all current pipe
820 * disabling paths we do need to wait for a vblank at
821 * some point. And we wait before enabling FBC anyway.
822 */
823 DRM_DEBUG_KMS("disabling active FBC for update\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300824 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200825 }
826
Paulo Zanoni220285f2015-07-07 15:26:05 -0300827 intel_fbc_enable(intel_crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200828 dev_priv->fbc.no_fbc_reason = FBC_OK;
829 return;
830
831out_disable:
832 /* Multiple disables should be harmless */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300833 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200834 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300835 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200836 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300837 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300838}
839
840/*
841 * intel_fbc_update - enable/disable FBC as needed
Paulo Zanoni7733b492015-07-07 15:26:04 -0300842 * @dev_priv: i915 device instance
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300843 *
844 * This function reevaluates the overall state and enables or disables FBC.
845 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300846void intel_fbc_update(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300847{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300848 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300849 return;
850
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300851 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300852 __intel_fbc_update(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300853 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200854}
855
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200856void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
857 unsigned int frontbuffer_bits,
858 enum fb_op_origin origin)
859{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200860 unsigned int fbc_bits;
861
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300862 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300863 return;
864
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200865 if (origin == ORIGIN_GTT)
866 return;
867
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300868 mutex_lock(&dev_priv->fbc.lock);
869
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200870 if (dev_priv->fbc.enabled)
871 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
872 else if (dev_priv->fbc.fbc_work)
873 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(
Paulo Zanoni220285f2015-07-07 15:26:05 -0300874 dev_priv->fbc.fbc_work->crtc->pipe);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200875 else
876 fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
877
878 dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
879
880 if (dev_priv->fbc.busy_bits)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300881 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300882
883 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200884}
885
886void intel_fbc_flush(struct drm_i915_private *dev_priv,
887 unsigned int frontbuffer_bits)
888{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300889 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300890 return;
891
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300892 mutex_lock(&dev_priv->fbc.lock);
893
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200894 if (!dev_priv->fbc.busy_bits)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300895 goto out;
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200896
897 dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
898
899 if (!dev_priv->fbc.busy_bits)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300900 __intel_fbc_update(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300901
902out:
903 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200904}
905
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800906/**
907 * intel_fbc_init - Initialize FBC
908 * @dev_priv: the i915 device
909 *
910 * This function might be called during PM init process.
911 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200912void intel_fbc_init(struct drm_i915_private *dev_priv)
913{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200914 enum pipe pipe;
915
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300916 mutex_init(&dev_priv->fbc.lock);
917
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200918 if (!HAS_FBC(dev_priv)) {
919 dev_priv->fbc.enabled = false;
Paulo Zanoni104618b2015-02-09 14:46:28 -0200920 dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200921 return;
922 }
923
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200924 for_each_pipe(dev_priv, pipe) {
925 dev_priv->fbc.possible_framebuffer_bits |=
926 INTEL_FRONTBUFFER_PRIMARY(pipe);
927
928 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
929 break;
930 }
931
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200932 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300933 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
934 dev_priv->fbc.enable_fbc = gen7_fbc_enable;
935 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200936 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300937 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
938 dev_priv->fbc.enable_fbc = ilk_fbc_enable;
939 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200940 } else if (IS_GM45(dev_priv)) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300941 dev_priv->fbc.fbc_enabled = g4x_fbc_enabled;
942 dev_priv->fbc.enable_fbc = g4x_fbc_enable;
943 dev_priv->fbc.disable_fbc = g4x_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200944 } else {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300945 dev_priv->fbc.fbc_enabled = i8xx_fbc_enabled;
946 dev_priv->fbc.enable_fbc = i8xx_fbc_enable;
947 dev_priv->fbc.disable_fbc = i8xx_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200948
949 /* This value was pulled out of someone's hat */
950 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
951 }
952
Paulo Zanoni7733b492015-07-07 15:26:04 -0300953 dev_priv->fbc.enabled = dev_priv->fbc.fbc_enabled(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200954}