blob: 6406b1432a2e4eee0c1c1ad516337f5287e26ee6 [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 Vivi94b839572014-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 Vivi94b839572014-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 Vivi94b839572014-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 Vivi94b839572014-12-08 06:46:31 -080041#include "intel_drv.h"
42#include "i915_drv.h"
43
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020044static void i8xx_fbc_disable(struct drm_device *dev)
45{
46 struct drm_i915_private *dev_priv = dev->dev_private;
47 u32 fbc_ctl;
48
49 dev_priv->fbc.enabled = false;
50
51 /* Disable compression */
52 fbc_ctl = I915_READ(FBC_CONTROL);
53 if ((fbc_ctl & FBC_CTL_EN) == 0)
54 return;
55
56 fbc_ctl &= ~FBC_CTL_EN;
57 I915_WRITE(FBC_CONTROL, fbc_ctl);
58
59 /* Wait for compressing bit to clear */
60 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
61 DRM_DEBUG_KMS("FBC idle timed out\n");
62 return;
63 }
64
65 DRM_DEBUG_KMS("disabled FBC\n");
66}
67
68static void i8xx_fbc_enable(struct drm_crtc *crtc)
69{
70 struct drm_device *dev = crtc->dev;
71 struct drm_i915_private *dev_priv = dev->dev_private;
72 struct drm_framebuffer *fb = crtc->primary->fb;
73 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
74 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
75 int cfb_pitch;
76 int i;
77 u32 fbc_ctl;
78
79 dev_priv->fbc.enabled = true;
80
Jani Nikula60ee5cd2015-02-05 12:04:27 +020081 /* Note: fbc.threshold == 1 for i8xx */
82 cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020083 if (fb->pitches[0] < cfb_pitch)
84 cfb_pitch = fb->pitches[0];
85
86 /* FBC_CTL wants 32B or 64B units */
87 if (IS_GEN2(dev))
88 cfb_pitch = (cfb_pitch / 32) - 1;
89 else
90 cfb_pitch = (cfb_pitch / 64) - 1;
91
92 /* Clear old tags */
93 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
94 I915_WRITE(FBC_TAG + (i * 4), 0);
95
96 if (IS_GEN4(dev)) {
97 u32 fbc_ctl2;
98
99 /* Set it up... */
100 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
101 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
102 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
103 I915_WRITE(FBC_FENCE_OFF, crtc->y);
104 }
105
106 /* enable it... */
107 fbc_ctl = I915_READ(FBC_CONTROL);
108 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
109 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
110 if (IS_I945GM(dev))
111 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
112 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
113 fbc_ctl |= obj->fence_reg;
114 I915_WRITE(FBC_CONTROL, fbc_ctl);
115
116 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
117 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
118}
119
120static bool i8xx_fbc_enabled(struct drm_device *dev)
121{
122 struct drm_i915_private *dev_priv = dev->dev_private;
123
124 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
125}
126
127static void g4x_fbc_enable(struct drm_crtc *crtc)
128{
129 struct drm_device *dev = crtc->dev;
130 struct drm_i915_private *dev_priv = dev->dev_private;
131 struct drm_framebuffer *fb = crtc->primary->fb;
132 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
134 u32 dpfc_ctl;
135
136 dev_priv->fbc.enabled = true;
137
138 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
139 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
140 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
141 else
142 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
143 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
144
145 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
146
147 /* enable it... */
148 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
149
150 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
151}
152
153static void g4x_fbc_disable(struct drm_device *dev)
154{
155 struct drm_i915_private *dev_priv = dev->dev_private;
156 u32 dpfc_ctl;
157
158 dev_priv->fbc.enabled = false;
159
160 /* Disable compression */
161 dpfc_ctl = I915_READ(DPFC_CONTROL);
162 if (dpfc_ctl & DPFC_CTL_EN) {
163 dpfc_ctl &= ~DPFC_CTL_EN;
164 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
165
166 DRM_DEBUG_KMS("disabled FBC\n");
167 }
168}
169
170static bool g4x_fbc_enabled(struct drm_device *dev)
171{
172 struct drm_i915_private *dev_priv = dev->dev_private;
173
174 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
175}
176
177static void snb_fbc_blit_update(struct drm_device *dev)
178{
179 struct drm_i915_private *dev_priv = dev->dev_private;
180 u32 blt_ecoskpd;
181
182 /* Make sure blitter notifies FBC of writes */
183
184 /* Blitter is part of Media powerwell on VLV. No impact of
185 * his param in other platforms for now */
Mika Kuoppala59bad942015-01-16 11:34:40 +0200186 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200187
188 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
189 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
190 GEN6_BLITTER_LOCK_SHIFT;
191 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
192 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
193 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
194 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
195 GEN6_BLITTER_LOCK_SHIFT);
196 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
197 POSTING_READ(GEN6_BLITTER_ECOSKPD);
198
Mika Kuoppala59bad942015-01-16 11:34:40 +0200199 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200200}
201
202static void ilk_fbc_enable(struct drm_crtc *crtc)
203{
204 struct drm_device *dev = crtc->dev;
205 struct drm_i915_private *dev_priv = dev->dev_private;
206 struct drm_framebuffer *fb = crtc->primary->fb;
207 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
209 u32 dpfc_ctl;
210
211 dev_priv->fbc.enabled = true;
212
213 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
214 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
215 dev_priv->fbc.threshold++;
216
217 switch (dev_priv->fbc.threshold) {
218 case 4:
219 case 3:
220 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
221 break;
222 case 2:
223 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
224 break;
225 case 1:
226 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
227 break;
228 }
229 dpfc_ctl |= DPFC_CTL_FENCE_EN;
230 if (IS_GEN5(dev))
231 dpfc_ctl |= obj->fence_reg;
232
233 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
234 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
235 /* enable it... */
236 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
237
238 if (IS_GEN6(dev)) {
239 I915_WRITE(SNB_DPFC_CTL_SA,
240 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
241 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
242 snb_fbc_blit_update(dev);
243 }
244
245 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
246}
247
248static void ilk_fbc_disable(struct drm_device *dev)
249{
250 struct drm_i915_private *dev_priv = dev->dev_private;
251 u32 dpfc_ctl;
252
253 dev_priv->fbc.enabled = false;
254
255 /* Disable compression */
256 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
257 if (dpfc_ctl & DPFC_CTL_EN) {
258 dpfc_ctl &= ~DPFC_CTL_EN;
259 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
260
261 DRM_DEBUG_KMS("disabled FBC\n");
262 }
263}
264
265static bool ilk_fbc_enabled(struct drm_device *dev)
266{
267 struct drm_i915_private *dev_priv = dev->dev_private;
268
269 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
270}
271
272static void gen7_fbc_enable(struct drm_crtc *crtc)
273{
274 struct drm_device *dev = crtc->dev;
275 struct drm_i915_private *dev_priv = dev->dev_private;
276 struct drm_framebuffer *fb = crtc->primary->fb;
277 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
279 u32 dpfc_ctl;
280
281 dev_priv->fbc.enabled = true;
282
283 dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
284 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
285 dev_priv->fbc.threshold++;
286
287 switch (dev_priv->fbc.threshold) {
288 case 4:
289 case 3:
290 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
291 break;
292 case 2:
293 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
294 break;
295 case 1:
296 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
297 break;
298 }
299
300 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
301
302 if (dev_priv->fbc.false_color)
303 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
304
305 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
306
307 if (IS_IVYBRIDGE(dev)) {
308 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
309 I915_WRITE(ILK_DISPLAY_CHICKEN1,
310 I915_READ(ILK_DISPLAY_CHICKEN1) |
311 ILK_FBCQ_DIS);
312 } else {
313 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
314 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
315 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
316 HSW_FBCQ_DIS);
317 }
318
319 I915_WRITE(SNB_DPFC_CTL_SA,
320 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
321 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
322
323 snb_fbc_blit_update(dev);
324
325 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
326}
327
Rodrigo Vivi94b839572014-12-08 06:46:31 -0800328/**
329 * intel_fbc_enabled - Is FBC enabled?
330 * @dev: the drm_device
331 *
332 * This function is used to verify the current state of FBC.
333 * FIXME: This should be tracked in the plane config eventually
334 * instead of queried at runtime for most callers.
335 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200336bool intel_fbc_enabled(struct drm_device *dev)
337{
338 struct drm_i915_private *dev_priv = dev->dev_private;
339
340 return dev_priv->fbc.enabled;
341}
342
343void bdw_fbc_sw_flush(struct drm_device *dev, u32 value)
344{
345 struct drm_i915_private *dev_priv = dev->dev_private;
346
347 if (!IS_GEN8(dev))
348 return;
349
350 if (!intel_fbc_enabled(dev))
351 return;
352
353 I915_WRITE(MSG_FBC_REND_STATE, value);
354}
355
356static void intel_fbc_work_fn(struct work_struct *__work)
357{
358 struct intel_fbc_work *work =
359 container_of(to_delayed_work(__work),
360 struct intel_fbc_work, work);
361 struct drm_device *dev = work->crtc->dev;
362 struct drm_i915_private *dev_priv = dev->dev_private;
363
364 mutex_lock(&dev->struct_mutex);
365 if (work == dev_priv->fbc.fbc_work) {
366 /* Double check that we haven't switched fb without cancelling
367 * the prior work.
368 */
369 if (work->crtc->primary->fb == work->fb) {
370 dev_priv->display.enable_fbc(work->crtc);
371
Paulo Zanonie35fef22015-02-09 14:46:29 -0200372 dev_priv->fbc.crtc = to_intel_crtc(work->crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200373 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
374 dev_priv->fbc.y = work->crtc->y;
375 }
376
377 dev_priv->fbc.fbc_work = NULL;
378 }
379 mutex_unlock(&dev->struct_mutex);
380
381 kfree(work);
382}
383
384static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
385{
386 if (dev_priv->fbc.fbc_work == NULL)
387 return;
388
389 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
390
391 /* Synchronisation is provided by struct_mutex and checking of
392 * dev_priv->fbc.fbc_work, so we can perform the cancellation
393 * entirely asynchronously.
394 */
395 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
396 /* tasklet was killed before being run, clean up */
397 kfree(dev_priv->fbc.fbc_work);
398
399 /* Mark the work as no longer wanted so that if it does
400 * wake-up (because the work was already running and waiting
401 * for our mutex), it will discover that is no longer
402 * necessary to run.
403 */
404 dev_priv->fbc.fbc_work = NULL;
405}
406
407static void intel_fbc_enable(struct drm_crtc *crtc)
408{
409 struct intel_fbc_work *work;
410 struct drm_device *dev = crtc->dev;
411 struct drm_i915_private *dev_priv = dev->dev_private;
412
413 if (!dev_priv->display.enable_fbc)
414 return;
415
416 intel_fbc_cancel_work(dev_priv);
417
418 work = kzalloc(sizeof(*work), GFP_KERNEL);
419 if (work == NULL) {
420 DRM_ERROR("Failed to allocate FBC work structure\n");
421 dev_priv->display.enable_fbc(crtc);
422 return;
423 }
424
425 work->crtc = crtc;
426 work->fb = crtc->primary->fb;
427 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
428
429 dev_priv->fbc.fbc_work = work;
430
431 /* Delay the actual enabling to let pageflipping cease and the
432 * display to settle before starting the compression. Note that
433 * this delay also serves a second purpose: it allows for a
434 * vblank to pass after disabling the FBC before we attempt
435 * to modify the control registers.
436 *
437 * A more complicated solution would involve tracking vblanks
438 * following the termination of the page-flipping sequence
439 * and indeed performing the enable as a co-routine and not
440 * waiting synchronously upon the vblank.
441 *
442 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
443 */
444 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
445}
446
Rodrigo Vivi94b839572014-12-08 06:46:31 -0800447/**
448 * intel_fbc_disable - disable FBC
449 * @dev: the drm_device
450 *
451 * This function disables FBC.
452 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200453void intel_fbc_disable(struct drm_device *dev)
454{
455 struct drm_i915_private *dev_priv = dev->dev_private;
456
457 intel_fbc_cancel_work(dev_priv);
458
459 if (!dev_priv->display.disable_fbc)
460 return;
461
462 dev_priv->display.disable_fbc(dev);
Paulo Zanonie35fef22015-02-09 14:46:29 -0200463 dev_priv->fbc.crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200464}
465
466static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
467 enum no_fbc_reason reason)
468{
469 if (dev_priv->fbc.no_fbc_reason == reason)
470 return false;
471
472 dev_priv->fbc.no_fbc_reason = reason;
473 return true;
474}
475
Paulo Zanoni95106752015-02-13 17:23:41 -0200476static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
477{
Paulo Zanoni95106752015-02-13 17:23:41 -0200478 struct drm_crtc *crtc = NULL, *tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200479 enum pipe pipe;
480 bool pipe_a_only = false;
Paulo Zanoni95106752015-02-13 17:23:41 -0200481
Paulo Zanoni68b92142015-02-13 17:23:42 -0200482 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
483 pipe_a_only = true;
484
485 for_each_pipe(dev_priv, pipe) {
486 tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
487
Paulo Zanoni95106752015-02-13 17:23:41 -0200488 if (intel_crtc_active(tmp_crtc) &&
489 to_intel_crtc(tmp_crtc)->primary_enabled) {
490 if (crtc) {
491 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
492 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
493 return NULL;
494 }
495 crtc = tmp_crtc;
496 }
Paulo Zanoni68b92142015-02-13 17:23:42 -0200497
498 if (pipe_a_only)
499 break;
Paulo Zanoni95106752015-02-13 17:23:41 -0200500 }
501
502 if (!crtc || crtc->primary->fb == NULL) {
503 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
504 DRM_DEBUG_KMS("no output, disabling\n");
505 return NULL;
506 }
507
508 return crtc;
509}
510
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200511/**
512 * intel_fbc_update - enable/disable FBC as needed
513 * @dev: the drm_device
514 *
515 * Set up the framebuffer compression hardware at mode set time. We
516 * enable it if possible:
517 * - plane A only (on pre-965)
518 * - no pixel mulitply/line duplication
519 * - no alpha buffer discard
520 * - no dual wide
521 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
522 *
523 * We can't assume that any compression will take place (worst case),
524 * so the compressed buffer has to be the same size as the uncompressed
525 * one. It also must reside (along with the line length buffer) in
526 * stolen memory.
527 *
528 * We need to enable/disable FBC on a global basis.
529 */
530void intel_fbc_update(struct drm_device *dev)
531{
532 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni95106752015-02-13 17:23:41 -0200533 struct drm_crtc *crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200534 struct intel_crtc *intel_crtc;
535 struct drm_framebuffer *fb;
536 struct drm_i915_gem_object *obj;
537 const struct drm_display_mode *adjusted_mode;
538 unsigned int max_width, max_height;
539
Paulo Zanoni104618b2015-02-09 14:46:28 -0200540 if (!HAS_FBC(dev))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200541 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200542
Yu Zhangbd492342015-02-10 19:05:50 +0800543 /* disable framebuffer compression in vGPU */
544 if (intel_vgpu_active(dev))
545 i915.enable_fbc = 0;
546
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200547 if (i915.enable_fbc < 0) {
548 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
549 DRM_DEBUG_KMS("disabled per chip default\n");
550 goto out_disable;
551 }
552
553 if (!i915.enable_fbc || !i915.powersave) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200554 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
555 DRM_DEBUG_KMS("fbc disabled per module param\n");
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200556 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200557 }
558
559 /*
560 * If FBC is already on, we just have to verify that we can
561 * keep it that way...
562 * Need to disable if:
563 * - more than one pipe is active
564 * - changing FBC params (stride, fence, mode)
565 * - new fb is too large to fit in compressed buffer
566 * - going to an unsupported config (interlace, pixel multiply, etc.)
567 */
Paulo Zanoni95106752015-02-13 17:23:41 -0200568 crtc = intel_fbc_find_crtc(dev_priv);
569 if (!crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200570 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200571
572 intel_crtc = to_intel_crtc(crtc);
573 fb = crtc->primary->fb;
574 obj = intel_fb_obj(fb);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200575 adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200576
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200577 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
578 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
579 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
580 DRM_DEBUG_KMS("mode incompatible with compression, "
581 "disabling\n");
582 goto out_disable;
583 }
584
585 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
586 max_width = 4096;
587 max_height = 4096;
588 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
589 max_width = 4096;
590 max_height = 2048;
591 } else {
592 max_width = 2048;
593 max_height = 1536;
594 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200595 if (intel_crtc->config->pipe_src_w > max_width ||
596 intel_crtc->config->pipe_src_h > max_height) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200597 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
598 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
599 goto out_disable;
600 }
601 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
602 intel_crtc->plane != PLANE_A) {
603 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
604 DRM_DEBUG_KMS("plane not A, disabling compression\n");
605 goto out_disable;
606 }
607
608 /* The use of a CPU fence is mandatory in order to detect writes
609 * by the CPU to the scanout and trigger updates to the FBC.
610 */
611 if (obj->tiling_mode != I915_TILING_X ||
612 obj->fence_reg == I915_FENCE_REG_NONE) {
613 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
614 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
615 goto out_disable;
616 }
617 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -0800618 crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200619 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
620 DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
621 goto out_disable;
622 }
623
624 /* If the kernel debugger is active, always disable compression */
625 if (in_dbg_master())
626 goto out_disable;
627
628 if (i915_gem_stolen_setup_compression(dev, obj->base.size,
629 drm_format_plane_cpp(fb->pixel_format, 0))) {
630 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
631 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
632 goto out_disable;
633 }
634
635 /* If the scanout has not changed, don't modify the FBC settings.
636 * Note that we make the fundamental assumption that the fb->obj
637 * cannot be unpinned (and have its GTT offset and fence revoked)
638 * without first being decoupled from the scanout and FBC disabled.
639 */
Paulo Zanonie35fef22015-02-09 14:46:29 -0200640 if (dev_priv->fbc.crtc == intel_crtc &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200641 dev_priv->fbc.fb_id == fb->base.id &&
642 dev_priv->fbc.y == crtc->y)
643 return;
644
645 if (intel_fbc_enabled(dev)) {
646 /* We update FBC along two paths, after changing fb/crtc
647 * configuration (modeswitching) and after page-flipping
648 * finishes. For the latter, we know that not only did
649 * we disable the FBC at the start of the page-flip
650 * sequence, but also more than one vblank has passed.
651 *
652 * For the former case of modeswitching, it is possible
653 * to switch between two FBC valid configurations
654 * instantaneously so we do need to disable the FBC
655 * before we can modify its control registers. We also
656 * have to wait for the next vblank for that to take
657 * effect. However, since we delay enabling FBC we can
658 * assume that a vblank has passed since disabling and
659 * that we can safely alter the registers in the deferred
660 * callback.
661 *
662 * In the scenario that we go from a valid to invalid
663 * and then back to valid FBC configuration we have
664 * no strict enforcement that a vblank occurred since
665 * disabling the FBC. However, along all current pipe
666 * disabling paths we do need to wait for a vblank at
667 * some point. And we wait before enabling FBC anyway.
668 */
669 DRM_DEBUG_KMS("disabling active FBC for update\n");
670 intel_fbc_disable(dev);
671 }
672
673 intel_fbc_enable(crtc);
674 dev_priv->fbc.no_fbc_reason = FBC_OK;
675 return;
676
677out_disable:
678 /* Multiple disables should be harmless */
679 if (intel_fbc_enabled(dev)) {
680 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
681 intel_fbc_disable(dev);
682 }
683 i915_gem_stolen_cleanup_compression(dev);
684}
685
Rodrigo Vivi94b839572014-12-08 06:46:31 -0800686/**
687 * intel_fbc_init - Initialize FBC
688 * @dev_priv: the i915 device
689 *
690 * This function might be called during PM init process.
691 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200692void intel_fbc_init(struct drm_i915_private *dev_priv)
693{
694 if (!HAS_FBC(dev_priv)) {
695 dev_priv->fbc.enabled = false;
Paulo Zanoni104618b2015-02-09 14:46:28 -0200696 dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200697 return;
698 }
699
700 if (INTEL_INFO(dev_priv)->gen >= 7) {
701 dev_priv->display.fbc_enabled = ilk_fbc_enabled;
702 dev_priv->display.enable_fbc = gen7_fbc_enable;
703 dev_priv->display.disable_fbc = ilk_fbc_disable;
704 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
705 dev_priv->display.fbc_enabled = ilk_fbc_enabled;
706 dev_priv->display.enable_fbc = ilk_fbc_enable;
707 dev_priv->display.disable_fbc = ilk_fbc_disable;
708 } else if (IS_GM45(dev_priv)) {
709 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
710 dev_priv->display.enable_fbc = g4x_fbc_enable;
711 dev_priv->display.disable_fbc = g4x_fbc_disable;
712 } else {
713 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
714 dev_priv->display.enable_fbc = i8xx_fbc_enable;
715 dev_priv->display.disable_fbc = i8xx_fbc_disable;
716
717 /* This value was pulled out of someone's hat */
718 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
719 }
720
721 dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
722}