blob: 56cf11094a04db3afa5346496c93a974246d2f51 [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
Paulo Zanoni7733b492015-07-07 15:26:04 -0300275 if (IS_IVYBRIDGE(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200276 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
277 I915_WRITE(ILK_DISPLAY_CHICKEN1,
278 I915_READ(ILK_DISPLAY_CHICKEN1) |
279 ILK_FBCQ_DIS);
Paulo Zanoni40f40222015-09-14 15:20:01 -0300280 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200281 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Paulo Zanoni220285f2015-07-07 15:26:05 -0300282 I915_WRITE(CHICKEN_PIPESL_1(crtc->pipe),
283 I915_READ(CHICKEN_PIPESL_1(crtc->pipe)) |
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200284 HSW_FBCQ_DIS);
285 }
286
Paulo Zanoni57012be92015-09-14 15:20:00 -0300287 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
288
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200289 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
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300311static void intel_fbc_enable(struct intel_crtc *crtc,
312 const struct drm_framebuffer *fb)
313{
314 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
315
316 dev_priv->fbc.enable_fbc(crtc);
317
318 dev_priv->fbc.crtc = crtc;
319 dev_priv->fbc.fb_id = fb->base.id;
320 dev_priv->fbc.y = crtc->base.y;
321}
322
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200323static void intel_fbc_work_fn(struct work_struct *__work)
324{
325 struct intel_fbc_work *work =
326 container_of(to_delayed_work(__work),
327 struct intel_fbc_work, work);
Paulo Zanoni220285f2015-07-07 15:26:05 -0300328 struct drm_i915_private *dev_priv = work->crtc->base.dev->dev_private;
329 struct drm_framebuffer *crtc_fb = work->crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200330
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300331 mutex_lock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200332 if (work == dev_priv->fbc.fbc_work) {
333 /* Double check that we haven't switched fb without cancelling
334 * the prior work.
335 */
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300336 if (crtc_fb == work->fb)
337 intel_fbc_enable(work->crtc, work->fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200338
339 dev_priv->fbc.fbc_work = NULL;
340 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300341 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200342
343 kfree(work);
344}
345
346static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
347{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300348 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
349
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200350 if (dev_priv->fbc.fbc_work == NULL)
351 return;
352
353 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
354
355 /* Synchronisation is provided by struct_mutex and checking of
356 * dev_priv->fbc.fbc_work, so we can perform the cancellation
357 * entirely asynchronously.
358 */
359 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
360 /* tasklet was killed before being run, clean up */
361 kfree(dev_priv->fbc.fbc_work);
362
363 /* Mark the work as no longer wanted so that if it does
364 * wake-up (because the work was already running and waiting
365 * for our mutex), it will discover that is no longer
366 * necessary to run.
367 */
368 dev_priv->fbc.fbc_work = NULL;
369}
370
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300371static void intel_fbc_schedule_enable(struct intel_crtc *crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200372{
373 struct intel_fbc_work *work;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300374 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200375
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300376 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
377
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200378 intel_fbc_cancel_work(dev_priv);
379
380 work = kzalloc(sizeof(*work), GFP_KERNEL);
381 if (work == NULL) {
382 DRM_ERROR("Failed to allocate FBC work structure\n");
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300383 intel_fbc_enable(crtc, crtc->base.primary->fb);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200384 return;
385 }
386
387 work->crtc = crtc;
Paulo Zanoni220285f2015-07-07 15:26:05 -0300388 work->fb = crtc->base.primary->fb;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200389 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
390
391 dev_priv->fbc.fbc_work = work;
392
393 /* Delay the actual enabling to let pageflipping cease and the
394 * display to settle before starting the compression. Note that
395 * this delay also serves a second purpose: it allows for a
396 * vblank to pass after disabling the FBC before we attempt
397 * to modify the control registers.
398 *
399 * A more complicated solution would involve tracking vblanks
400 * following the termination of the page-flipping sequence
401 * and indeed performing the enable as a co-routine and not
402 * waiting synchronously upon the vblank.
403 *
404 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
405 */
406 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
407}
408
Paulo Zanoni7733b492015-07-07 15:26:04 -0300409static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300410{
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300411 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
412
413 intel_fbc_cancel_work(dev_priv);
414
Paulo Zanoni7733b492015-07-07 15:26:04 -0300415 dev_priv->fbc.disable_fbc(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300416 dev_priv->fbc.crtc = NULL;
417}
418
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800419/**
420 * intel_fbc_disable - disable FBC
Paulo Zanoni7733b492015-07-07 15:26:04 -0300421 * @dev_priv: i915 device instance
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800422 *
423 * This function disables FBC.
424 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300425void intel_fbc_disable(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200426{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300427 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300428 return;
429
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300430 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300431 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300432 mutex_unlock(&dev_priv->fbc.lock);
433}
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200434
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300435/*
436 * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
437 * @crtc: the CRTC
438 *
439 * This function disables FBC if it's associated with the provided CRTC.
440 */
441void intel_fbc_disable_crtc(struct intel_crtc *crtc)
442{
Paulo Zanoni7733b492015-07-07 15:26:04 -0300443 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200444
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300445 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300446 return;
447
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300448 mutex_lock(&dev_priv->fbc.lock);
449 if (dev_priv->fbc.crtc == crtc)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300450 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300451 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200452}
453
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300454const char *intel_no_fbc_reason_str(enum no_fbc_reason reason)
455{
456 switch (reason) {
457 case FBC_OK:
458 return "FBC enabled but currently disabled in hardware";
459 case FBC_UNSUPPORTED:
460 return "unsupported by this chipset";
461 case FBC_NO_OUTPUT:
462 return "no output";
463 case FBC_STOLEN_TOO_SMALL:
464 return "not enough stolen memory";
465 case FBC_UNSUPPORTED_MODE:
466 return "mode incompatible with compression";
467 case FBC_MODE_TOO_LARGE:
468 return "mode too large for compression";
469 case FBC_BAD_PLANE:
470 return "FBC unsupported on plane";
471 case FBC_NOT_TILED:
472 return "framebuffer not tiled or fenced";
473 case FBC_MULTIPLE_PIPES:
474 return "more than one pipe active";
475 case FBC_MODULE_PARAM:
476 return "disabled per module param";
477 case FBC_CHIP_DEFAULT:
478 return "disabled per chip default";
479 case FBC_ROTATION:
480 return "rotation unsupported";
Paulo Zanoni89351082015-07-07 15:26:06 -0300481 case FBC_IN_DBG_MASTER:
482 return "Kernel debugger is active";
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300483 case FBC_BAD_STRIDE:
484 return "framebuffer stride not supported";
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300485 case FBC_PIXEL_RATE:
486 return "pixel rate is too big";
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300487 default:
488 MISSING_CASE(reason);
489 return "unknown reason";
490 }
491}
492
493static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200494 enum no_fbc_reason reason)
495{
496 if (dev_priv->fbc.no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300497 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200498
499 dev_priv->fbc.no_fbc_reason = reason;
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300500 DRM_DEBUG_KMS("Disabling FBC: %s\n", intel_no_fbc_reason_str(reason));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200501}
502
Paulo Zanoni95106752015-02-13 17:23:41 -0200503static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
504{
Paulo Zanoni95106752015-02-13 17:23:41 -0200505 struct drm_crtc *crtc = NULL, *tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200506 enum pipe pipe;
Paulo Zanoni232fd932015-07-07 15:26:07 -0300507 bool pipe_a_only = false;
Paulo Zanoni95106752015-02-13 17:23:41 -0200508
Paulo Zanoni68b92142015-02-13 17:23:42 -0200509 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
510 pipe_a_only = true;
511
512 for_each_pipe(dev_priv, pipe) {
513 tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
514
Paulo Zanoni95106752015-02-13 17:23:41 -0200515 if (intel_crtc_active(tmp_crtc) &&
Paulo Zanoni232fd932015-07-07 15:26:07 -0300516 to_intel_plane_state(tmp_crtc->primary->state)->visible)
Paulo Zanoni95106752015-02-13 17:23:41 -0200517 crtc = tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200518
519 if (pipe_a_only)
520 break;
Paulo Zanoni95106752015-02-13 17:23:41 -0200521 }
522
Paulo Zanoni8df5dd52015-07-07 15:26:08 -0300523 if (!crtc || crtc->primary->fb == NULL)
Paulo Zanoni95106752015-02-13 17:23:41 -0200524 return NULL;
Paulo Zanoni95106752015-02-13 17:23:41 -0200525
526 return crtc;
527}
528
Paulo Zanoni232fd932015-07-07 15:26:07 -0300529static bool multiple_pipes_ok(struct drm_i915_private *dev_priv)
530{
531 enum pipe pipe;
532 int n_pipes = 0;
533 struct drm_crtc *crtc;
534
535 if (INTEL_INFO(dev_priv)->gen > 4)
536 return true;
537
538 for_each_pipe(dev_priv, pipe) {
539 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
540
541 if (intel_crtc_active(crtc) &&
542 to_intel_plane_state(crtc->primary->state)->visible)
543 n_pipes++;
544 }
545
546 return (n_pipes < 2);
547}
548
Paulo Zanoni7733b492015-07-07 15:26:04 -0300549static int find_compression_threshold(struct drm_i915_private *dev_priv,
Paulo Zanonifc786722015-07-02 19:25:08 -0300550 struct drm_mm_node *node,
551 int size,
552 int fb_cpp)
553{
Paulo Zanonifc786722015-07-02 19:25:08 -0300554 int compression_threshold = 1;
555 int ret;
Paulo Zanonia9da5122015-09-14 15:19:57 -0300556 u64 end;
557
558 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
559 * reserved range size, so it always assumes the maximum (8mb) is used.
560 * If we enable FBC using a CFB on that memory range we'll get FIFO
561 * underruns, even if that range is not reserved by the BIOS. */
562 if (IS_BROADWELL(dev_priv) || IS_SKYLAKE(dev_priv))
563 end = dev_priv->gtt.stolen_size - 8 * 1024 * 1024;
564 else
565 end = dev_priv->gtt.stolen_usable_size;
Paulo Zanonifc786722015-07-02 19:25:08 -0300566
567 /* HACK: This code depends on what we will do in *_enable_fbc. If that
568 * code changes, this code needs to change as well.
569 *
570 * The enable_fbc code will attempt to use one of our 2 compression
571 * thresholds, therefore, in that case, we only have 1 resort.
572 */
573
574 /* Try to over-allocate to reduce reallocations and fragmentation. */
Paulo Zanonia9da5122015-09-14 15:19:57 -0300575 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
576 4096, 0, end);
Paulo Zanonifc786722015-07-02 19:25:08 -0300577 if (ret == 0)
578 return compression_threshold;
579
580again:
581 /* HW's ability to limit the CFB is 1:4 */
582 if (compression_threshold > 4 ||
583 (fb_cpp == 2 && compression_threshold == 2))
584 return 0;
585
Paulo Zanonia9da5122015-09-14 15:19:57 -0300586 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
587 4096, 0, end);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300588 if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300589 return 0;
590 } else if (ret) {
591 compression_threshold <<= 1;
592 goto again;
593 } else {
594 return compression_threshold;
595 }
596}
597
Paulo Zanoni7733b492015-07-07 15:26:04 -0300598static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv, int size,
599 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300600{
Paulo Zanonifc786722015-07-02 19:25:08 -0300601 struct drm_mm_node *uninitialized_var(compressed_llb);
602 int ret;
603
Paulo Zanoni7733b492015-07-07 15:26:04 -0300604 ret = find_compression_threshold(dev_priv, &dev_priv->fbc.compressed_fb,
Paulo Zanonifc786722015-07-02 19:25:08 -0300605 size, fb_cpp);
606 if (!ret)
607 goto err_llb;
608 else if (ret > 1) {
609 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");
610
611 }
612
613 dev_priv->fbc.threshold = ret;
614
615 if (INTEL_INFO(dev_priv)->gen >= 5)
616 I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300617 else if (IS_GM45(dev_priv)) {
Paulo Zanonifc786722015-07-02 19:25:08 -0300618 I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
619 } else {
620 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
621 if (!compressed_llb)
622 goto err_fb;
623
624 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
625 4096, 4096);
626 if (ret)
627 goto err_fb;
628
629 dev_priv->fbc.compressed_llb = compressed_llb;
630
631 I915_WRITE(FBC_CFB_BASE,
632 dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
633 I915_WRITE(FBC_LL_BASE,
634 dev_priv->mm.stolen_base + compressed_llb->start);
635 }
636
637 dev_priv->fbc.uncompressed_size = size;
638
Paulo Zanonib8bf5d72015-09-14 15:19:58 -0300639 DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
640 dev_priv->fbc.compressed_fb.size,
641 dev_priv->fbc.threshold);
Paulo Zanonifc786722015-07-02 19:25:08 -0300642
643 return 0;
644
645err_fb:
646 kfree(compressed_llb);
647 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
648err_llb:
649 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);
650 return -ENOSPC;
651}
652
Paulo Zanoni7733b492015-07-07 15:26:04 -0300653static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanonifc786722015-07-02 19:25:08 -0300654{
Paulo Zanonifc786722015-07-02 19:25:08 -0300655 if (dev_priv->fbc.uncompressed_size == 0)
656 return;
657
658 i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb);
659
660 if (dev_priv->fbc.compressed_llb) {
661 i915_gem_stolen_remove_node(dev_priv,
662 dev_priv->fbc.compressed_llb);
663 kfree(dev_priv->fbc.compressed_llb);
664 }
665
666 dev_priv->fbc.uncompressed_size = 0;
667}
668
Paulo Zanoni7733b492015-07-07 15:26:04 -0300669void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300670{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300671 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300672 return;
673
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300674 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300675 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300676 mutex_unlock(&dev_priv->fbc.lock);
677}
678
Paulo Zanoni7733b492015-07-07 15:26:04 -0300679static int intel_fbc_setup_cfb(struct drm_i915_private *dev_priv, int size,
680 int fb_cpp)
Paulo Zanonifc786722015-07-02 19:25:08 -0300681{
Paulo Zanonifc786722015-07-02 19:25:08 -0300682 if (size <= dev_priv->fbc.uncompressed_size)
683 return 0;
684
685 /* Release any current block */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300686 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanonifc786722015-07-02 19:25:08 -0300687
Paulo Zanoni7733b492015-07-07 15:26:04 -0300688 return intel_fbc_alloc_cfb(dev_priv, size, fb_cpp);
Paulo Zanonifc786722015-07-02 19:25:08 -0300689}
690
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300691static bool stride_is_valid(struct drm_i915_private *dev_priv,
692 unsigned int stride)
693{
694 /* These should have been caught earlier. */
695 WARN_ON(stride < 512);
696 WARN_ON((stride & (64 - 1)) != 0);
697
698 /* Below are the additional FBC restrictions. */
699
700 if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
701 return stride == 4096 || stride == 8192;
702
703 if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
704 return false;
705
706 if (stride > 16384)
707 return false;
708
709 return true;
710}
711
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200712/**
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300713 * __intel_fbc_update - enable/disable FBC as needed, unlocked
Paulo Zanoni7733b492015-07-07 15:26:04 -0300714 * @dev_priv: i915 device instance
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200715 *
716 * Set up the framebuffer compression hardware at mode set time. We
717 * enable it if possible:
718 * - plane A only (on pre-965)
719 * - no pixel mulitply/line duplication
720 * - no alpha buffer discard
721 * - no dual wide
722 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
723 *
724 * We can't assume that any compression will take place (worst case),
725 * so the compressed buffer has to be the same size as the uncompressed
726 * one. It also must reside (along with the line length buffer) in
727 * stolen memory.
728 *
729 * We need to enable/disable FBC on a global basis.
730 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300731static void __intel_fbc_update(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200732{
Paulo Zanoni95106752015-02-13 17:23:41 -0200733 struct drm_crtc *crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200734 struct intel_crtc *intel_crtc;
735 struct drm_framebuffer *fb;
736 struct drm_i915_gem_object *obj;
737 const struct drm_display_mode *adjusted_mode;
738 unsigned int max_width, max_height;
739
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300740 WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
741
Yu Zhangbd492342015-02-10 19:05:50 +0800742 /* disable framebuffer compression in vGPU */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300743 if (intel_vgpu_active(dev_priv->dev))
Yu Zhangbd492342015-02-10 19:05:50 +0800744 i915.enable_fbc = 0;
745
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200746 if (i915.enable_fbc < 0) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300747 set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200748 goto out_disable;
749 }
750
Rodrigo Viviab585de2015-03-24 12:40:09 -0700751 if (!i915.enable_fbc) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300752 set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200753 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200754 }
755
756 /*
757 * If FBC is already on, we just have to verify that we can
758 * keep it that way...
759 * Need to disable if:
760 * - more than one pipe is active
761 * - changing FBC params (stride, fence, mode)
762 * - new fb is too large to fit in compressed buffer
763 * - going to an unsupported config (interlace, pixel multiply, etc.)
764 */
Paulo Zanoni95106752015-02-13 17:23:41 -0200765 crtc = intel_fbc_find_crtc(dev_priv);
Paulo Zanoni8df5dd52015-07-07 15:26:08 -0300766 if (!crtc) {
767 set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200768 goto out_disable;
Paulo Zanoni8df5dd52015-07-07 15:26:08 -0300769 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200770
Paulo Zanoni232fd932015-07-07 15:26:07 -0300771 if (!multiple_pipes_ok(dev_priv)) {
772 set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES);
773 goto out_disable;
774 }
775
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200776 intel_crtc = to_intel_crtc(crtc);
777 fb = crtc->primary->fb;
778 obj = intel_fb_obj(fb);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200779 adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200780
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200781 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
782 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300783 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200784 goto out_disable;
785 }
786
Paulo Zanoni7733b492015-07-07 15:26:04 -0300787 if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200788 max_width = 4096;
789 max_height = 4096;
Paulo Zanoni7733b492015-07-07 15:26:04 -0300790 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200791 max_width = 4096;
792 max_height = 2048;
793 } else {
794 max_width = 2048;
795 max_height = 1536;
796 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200797 if (intel_crtc->config->pipe_src_w > max_width ||
798 intel_crtc->config->pipe_src_h > max_height) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300799 set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200800 goto out_disable;
801 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300802 if ((INTEL_INFO(dev_priv)->gen < 4 || HAS_DDI(dev_priv)) &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200803 intel_crtc->plane != PLANE_A) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300804 set_no_fbc_reason(dev_priv, FBC_BAD_PLANE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200805 goto out_disable;
806 }
807
808 /* The use of a CPU fence is mandatory in order to detect writes
809 * by the CPU to the scanout and trigger updates to the FBC.
810 */
811 if (obj->tiling_mode != I915_TILING_X ||
812 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300813 set_no_fbc_reason(dev_priv, FBC_NOT_TILED);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200814 goto out_disable;
815 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300816 if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
Matt Roper8e7d6882015-01-21 16:35:41 -0800817 crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300818 set_no_fbc_reason(dev_priv, FBC_ROTATION);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200819 goto out_disable;
820 }
821
Paulo Zanoniadf70c62015-09-14 15:19:56 -0300822 if (!stride_is_valid(dev_priv, fb->pitches[0])) {
823 set_no_fbc_reason(dev_priv, FBC_BAD_STRIDE);
824 goto out_disable;
825 }
826
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200827 /* If the kernel debugger is active, always disable compression */
Paulo Zanoni89351082015-07-07 15:26:06 -0300828 if (in_dbg_master()) {
829 set_no_fbc_reason(dev_priv, FBC_IN_DBG_MASTER);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200830 goto out_disable;
Paulo Zanoni89351082015-07-07 15:26:06 -0300831 }
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200832
Paulo Zanoni7b24c9a2015-09-14 15:19:59 -0300833 /* WaFbcExceedCdClockThreshold:hsw,bdw */
834 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
835 ilk_pipe_pixel_rate(intel_crtc->config) >=
836 dev_priv->cdclk_freq * 95 / 100) {
837 set_no_fbc_reason(dev_priv, FBC_PIXEL_RATE);
838 goto out_disable;
839 }
840
Paulo Zanoni7733b492015-07-07 15:26:04 -0300841 if (intel_fbc_setup_cfb(dev_priv, obj->base.size,
Paulo Zanonifc786722015-07-02 19:25:08 -0300842 drm_format_plane_cpp(fb->pixel_format, 0))) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300843 set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200844 goto out_disable;
845 }
846
847 /* If the scanout has not changed, don't modify the FBC settings.
848 * Note that we make the fundamental assumption that the fb->obj
849 * cannot be unpinned (and have its GTT offset and fence revoked)
850 * without first being decoupled from the scanout and FBC disabled.
851 */
Paulo Zanonie35fef22015-02-09 14:46:29 -0200852 if (dev_priv->fbc.crtc == intel_crtc &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200853 dev_priv->fbc.fb_id == fb->base.id &&
854 dev_priv->fbc.y == crtc->y)
855 return;
856
Paulo Zanoni7733b492015-07-07 15:26:04 -0300857 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200858 /* We update FBC along two paths, after changing fb/crtc
859 * configuration (modeswitching) and after page-flipping
860 * finishes. For the latter, we know that not only did
861 * we disable the FBC at the start of the page-flip
862 * sequence, but also more than one vblank has passed.
863 *
864 * For the former case of modeswitching, it is possible
865 * to switch between two FBC valid configurations
866 * instantaneously so we do need to disable the FBC
867 * before we can modify its control registers. We also
868 * have to wait for the next vblank for that to take
869 * effect. However, since we delay enabling FBC we can
870 * assume that a vblank has passed since disabling and
871 * that we can safely alter the registers in the deferred
872 * callback.
873 *
874 * In the scenario that we go from a valid to invalid
875 * and then back to valid FBC configuration we have
876 * no strict enforcement that a vblank occurred since
877 * disabling the FBC. However, along all current pipe
878 * disabling paths we do need to wait for a vblank at
879 * some point. And we wait before enabling FBC anyway.
880 */
881 DRM_DEBUG_KMS("disabling active FBC for update\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300882 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200883 }
884
Paulo Zanonie8cb8d62015-09-14 15:19:55 -0300885 intel_fbc_schedule_enable(intel_crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200886 dev_priv->fbc.no_fbc_reason = FBC_OK;
887 return;
888
889out_disable:
890 /* Multiple disables should be harmless */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300891 if (intel_fbc_enabled(dev_priv)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200892 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Paulo Zanoni7733b492015-07-07 15:26:04 -0300893 __intel_fbc_disable(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200894 }
Paulo Zanoni7733b492015-07-07 15:26:04 -0300895 __intel_fbc_cleanup_cfb(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300896}
897
898/*
899 * intel_fbc_update - enable/disable FBC as needed
Paulo Zanoni7733b492015-07-07 15:26:04 -0300900 * @dev_priv: i915 device instance
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300901 *
902 * This function reevaluates the overall state and enables or disables FBC.
903 */
Paulo Zanoni7733b492015-07-07 15:26:04 -0300904void intel_fbc_update(struct drm_i915_private *dev_priv)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300905{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300906 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300907 return;
908
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300909 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300910 __intel_fbc_update(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300911 mutex_unlock(&dev_priv->fbc.lock);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200912}
913
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200914void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
915 unsigned int frontbuffer_bits,
916 enum fb_op_origin origin)
917{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200918 unsigned int fbc_bits;
919
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300920 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300921 return;
922
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200923 if (origin == ORIGIN_GTT)
924 return;
925
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300926 mutex_lock(&dev_priv->fbc.lock);
927
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200928 if (dev_priv->fbc.enabled)
929 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
930 else if (dev_priv->fbc.fbc_work)
931 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(
Paulo Zanoni220285f2015-07-07 15:26:05 -0300932 dev_priv->fbc.fbc_work->crtc->pipe);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200933 else
934 fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
935
936 dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
937
938 if (dev_priv->fbc.busy_bits)
Paulo Zanoni7733b492015-07-07 15:26:04 -0300939 __intel_fbc_disable(dev_priv);
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300940
941 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200942}
943
944void intel_fbc_flush(struct drm_i915_private *dev_priv,
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300945 unsigned int frontbuffer_bits, enum fb_op_origin origin)
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200946{
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300947 if (!dev_priv->fbc.enable_fbc)
Paulo Zanoni0bf73c32015-07-03 15:40:54 -0300948 return;
949
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300950 if (origin == ORIGIN_GTT)
951 return;
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300952
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300953 mutex_lock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200954
955 dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
956
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300957 if (!dev_priv->fbc.busy_bits) {
958 __intel_fbc_disable(dev_priv);
Paulo Zanoni7733b492015-07-07 15:26:04 -0300959 __intel_fbc_update(dev_priv);
Paulo Zanoni6f4551f2015-07-14 16:29:10 -0300960 }
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300961
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300962 mutex_unlock(&dev_priv->fbc.lock);
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200963}
964
Rodrigo Vivi94b83952014-12-08 06:46:31 -0800965/**
966 * intel_fbc_init - Initialize FBC
967 * @dev_priv: the i915 device
968 *
969 * This function might be called during PM init process.
970 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200971void intel_fbc_init(struct drm_i915_private *dev_priv)
972{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200973 enum pipe pipe;
974
Paulo Zanoni25ad93f2015-07-02 19:25:10 -0300975 mutex_init(&dev_priv->fbc.lock);
976
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200977 if (!HAS_FBC(dev_priv)) {
978 dev_priv->fbc.enabled = false;
Paulo Zanoni104618b2015-02-09 14:46:28 -0200979 dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200980 return;
981 }
982
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200983 for_each_pipe(dev_priv, pipe) {
984 dev_priv->fbc.possible_framebuffer_bits |=
985 INTEL_FRONTBUFFER_PRIMARY(pipe);
986
987 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
988 break;
989 }
990
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200991 if (INTEL_INFO(dev_priv)->gen >= 7) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300992 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
993 dev_priv->fbc.enable_fbc = gen7_fbc_enable;
994 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200995 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -0300996 dev_priv->fbc.fbc_enabled = ilk_fbc_enabled;
997 dev_priv->fbc.enable_fbc = ilk_fbc_enable;
998 dev_priv->fbc.disable_fbc = ilk_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200999 } else if (IS_GM45(dev_priv)) {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001000 dev_priv->fbc.fbc_enabled = g4x_fbc_enabled;
1001 dev_priv->fbc.enable_fbc = g4x_fbc_enable;
1002 dev_priv->fbc.disable_fbc = g4x_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001003 } else {
Paulo Zanoniff2a3112015-07-07 15:26:03 -03001004 dev_priv->fbc.fbc_enabled = i8xx_fbc_enabled;
1005 dev_priv->fbc.enable_fbc = i8xx_fbc_enable;
1006 dev_priv->fbc.disable_fbc = i8xx_fbc_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001007
1008 /* This value was pulled out of someone's hat */
1009 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1010 }
1011
Paulo Zanoni7733b492015-07-07 15:26:04 -03001012 dev_priv->fbc.enabled = dev_priv->fbc.fbc_enabled(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001013}