2 * Copyright © 2014 Intel Corporation
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:
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
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.
25 * DOC: Frame Buffer Compression (FBC)
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.
31 * The benefits of FBC are mostly visible with solid backgrounds and
32 * variation-less patterns. It comes from keeping the memory footprint small
33 * and having fewer memory pages opened and accessed for refreshing the display.
35 * 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.
41 #include <linux/string_helpers.h>
43 #include <drm/drm_blend.h>
44 #include <drm/drm_fourcc.h>
47 #include "i915_utils.h"
48 #include "i915_vgpu.h"
49 #include "intel_cdclk.h"
51 #include "intel_display_trace.h"
52 #include "intel_display_types.h"
53 #include "intel_fbc.h"
54 #include "intel_frontbuffer.h"
56 #define for_each_fbc_id(__dev_priv, __fbc_id) \
57 for ((__fbc_id) = INTEL_FBC_A; (__fbc_id) < I915_MAX_FBCS; (__fbc_id)++) \
58 for_each_if(RUNTIME_INFO(__dev_priv)->fbc_mask & BIT(__fbc_id))
60 #define for_each_intel_fbc(__dev_priv, __fbc, __fbc_id) \
61 for_each_fbc_id((__dev_priv), (__fbc_id)) \
62 for_each_if((__fbc) = (__dev_priv)->display.fbc[(__fbc_id)])
64 struct intel_fbc_funcs {
65 void (*activate)(struct intel_fbc *fbc);
66 void (*deactivate)(struct intel_fbc *fbc);
67 bool (*is_active)(struct intel_fbc *fbc);
68 bool (*is_compressing)(struct intel_fbc *fbc);
69 void (*nuke)(struct intel_fbc *fbc);
70 void (*program_cfb)(struct intel_fbc *fbc);
71 void (*set_false_color)(struct intel_fbc *fbc, bool enable);
74 struct intel_fbc_state {
75 struct intel_plane *plane;
76 unsigned int cfb_stride;
77 unsigned int cfb_size;
78 unsigned int fence_y_offset;
79 u16 override_cfb_stride;
85 struct drm_i915_private *i915;
86 const struct intel_fbc_funcs *funcs;
89 * This is always the inner lock when overlapping with
90 * struct_mutex and it's the outer lock when overlapping
94 unsigned int busy_bits;
96 struct drm_mm_node compressed_fb;
97 struct drm_mm_node compressed_llb;
109 bool underrun_detected;
110 struct work_struct underrun_work;
113 * This structure contains everything that's relevant to program the
114 * hardware registers. When we want to figure out if we need to disable
115 * and re-enable FBC for a new configuration we just check if there's
116 * something different in the struct. The genx_fbc_activate functions
117 * are supposed to read from it in order to program the registers.
119 struct intel_fbc_state state;
120 const char *no_fbc_reason;
123 /* plane stride in pixels */
124 static unsigned int intel_fbc_plane_stride(const struct intel_plane_state *plane_state)
126 const struct drm_framebuffer *fb = plane_state->hw.fb;
129 stride = plane_state->view.color_plane[0].mapping_stride;
130 if (!drm_rotation_90_or_270(plane_state->hw.rotation))
131 stride /= fb->format->cpp[0];
136 /* plane stride based cfb stride in bytes, assuming 1:1 compression limit */
137 static unsigned int _intel_fbc_cfb_stride(const struct intel_plane_state *plane_state)
139 unsigned int cpp = 4; /* FBC always 4 bytes per pixel */
141 return intel_fbc_plane_stride(plane_state) * cpp;
144 /* minimum acceptable cfb stride in bytes, assuming 1:1 compression limit */
145 static unsigned int skl_fbc_min_cfb_stride(const struct intel_plane_state *plane_state)
147 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
148 unsigned int limit = 4; /* 1:4 compression limit is the worst case */
149 unsigned int cpp = 4; /* FBC always 4 bytes per pixel */
150 unsigned int width = drm_rect_width(&plane_state->uapi.src) >> 16;
151 unsigned int height = 4; /* FBC segment is 4 lines */
154 /* minimum segment stride we can use */
155 stride = width * cpp * height / limit;
158 * Wa_16011863758: icl+
159 * Avoid some hardware segment address miscalculation.
161 if (DISPLAY_VER(i915) >= 11)
165 * At least some of the platforms require each 4 line segment to
166 * be 512 byte aligned. Just do it always for simplicity.
168 stride = ALIGN(stride, 512);
170 /* convert back to single line equivalent with 1:1 compression limit */
171 return stride * limit / height;
174 /* properly aligned cfb stride in bytes, assuming 1:1 compression limit */
175 static unsigned int intel_fbc_cfb_stride(const struct intel_plane_state *plane_state)
177 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
178 unsigned int stride = _intel_fbc_cfb_stride(plane_state);
181 * At least some of the platforms require each 4 line segment to
182 * be 512 byte aligned. Aligning each line to 512 bytes guarantees
183 * that regardless of the compression limit we choose later.
185 if (DISPLAY_VER(i915) >= 9)
186 return max(ALIGN(stride, 512), skl_fbc_min_cfb_stride(plane_state));
191 static unsigned int intel_fbc_cfb_size(const struct intel_plane_state *plane_state)
193 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
194 int lines = drm_rect_height(&plane_state->uapi.src) >> 16;
196 if (DISPLAY_VER(i915) == 7)
197 lines = min(lines, 2048);
198 else if (DISPLAY_VER(i915) >= 8)
199 lines = min(lines, 2560);
201 return lines * intel_fbc_cfb_stride(plane_state);
204 static u16 intel_fbc_override_cfb_stride(const struct intel_plane_state *plane_state)
206 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
207 unsigned int stride_aligned = intel_fbc_cfb_stride(plane_state);
208 unsigned int stride = _intel_fbc_cfb_stride(plane_state);
209 const struct drm_framebuffer *fb = plane_state->hw.fb;
212 * Override stride in 64 byte units per 4 line segment.
214 * Gen9 hw miscalculates cfb stride for linear as
215 * PLANE_STRIDE*512 instead of PLANE_STRIDE*64, so
216 * we always need to use the override there.
218 if (stride != stride_aligned ||
219 (DISPLAY_VER(i915) == 9 && fb->modifier == DRM_FORMAT_MOD_LINEAR))
220 return stride_aligned * 4 / 64;
225 static u32 i8xx_fbc_ctl(struct intel_fbc *fbc)
227 const struct intel_fbc_state *fbc_state = &fbc->state;
228 struct drm_i915_private *i915 = fbc->i915;
229 unsigned int cfb_stride;
232 cfb_stride = fbc_state->cfb_stride / fbc->limit;
234 /* FBC_CTL wants 32B or 64B units */
235 if (DISPLAY_VER(i915) == 2)
236 cfb_stride = (cfb_stride / 32) - 1;
238 cfb_stride = (cfb_stride / 64) - 1;
240 fbc_ctl = FBC_CTL_PERIODIC |
241 FBC_CTL_INTERVAL(fbc_state->interval) |
242 FBC_CTL_STRIDE(cfb_stride);
245 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
247 if (fbc_state->fence_id >= 0)
248 fbc_ctl |= FBC_CTL_FENCENO(fbc_state->fence_id);
253 static u32 i965_fbc_ctl2(struct intel_fbc *fbc)
255 const struct intel_fbc_state *fbc_state = &fbc->state;
258 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM |
259 FBC_CTL_PLANE(fbc_state->plane->i9xx_plane);
261 if (fbc_state->fence_id >= 0)
262 fbc_ctl2 |= FBC_CTL_CPU_FENCE_EN;
267 static void i8xx_fbc_deactivate(struct intel_fbc *fbc)
269 struct drm_i915_private *i915 = fbc->i915;
272 /* Disable compression */
273 fbc_ctl = intel_de_read(i915, FBC_CONTROL);
274 if ((fbc_ctl & FBC_CTL_EN) == 0)
277 fbc_ctl &= ~FBC_CTL_EN;
278 intel_de_write(i915, FBC_CONTROL, fbc_ctl);
280 /* Wait for compressing bit to clear */
281 if (intel_de_wait_for_clear(i915, FBC_STATUS,
282 FBC_STAT_COMPRESSING, 10)) {
283 drm_dbg_kms(&i915->drm, "FBC idle timed out\n");
288 static void i8xx_fbc_activate(struct intel_fbc *fbc)
290 const struct intel_fbc_state *fbc_state = &fbc->state;
291 struct drm_i915_private *i915 = fbc->i915;
295 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
296 intel_de_write(i915, FBC_TAG(i), 0);
298 if (DISPLAY_VER(i915) == 4) {
299 intel_de_write(i915, FBC_CONTROL2,
301 intel_de_write(i915, FBC_FENCE_OFF,
302 fbc_state->fence_y_offset);
305 intel_de_write(i915, FBC_CONTROL,
306 FBC_CTL_EN | i8xx_fbc_ctl(fbc));
309 static bool i8xx_fbc_is_active(struct intel_fbc *fbc)
311 return intel_de_read(fbc->i915, FBC_CONTROL) & FBC_CTL_EN;
314 static bool i8xx_fbc_is_compressing(struct intel_fbc *fbc)
316 return intel_de_read(fbc->i915, FBC_STATUS) &
317 (FBC_STAT_COMPRESSING | FBC_STAT_COMPRESSED);
320 static void i8xx_fbc_nuke(struct intel_fbc *fbc)
322 struct intel_fbc_state *fbc_state = &fbc->state;
323 enum i9xx_plane_id i9xx_plane = fbc_state->plane->i9xx_plane;
324 struct drm_i915_private *dev_priv = fbc->i915;
326 intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
327 intel_de_read_fw(dev_priv, DSPADDR(i9xx_plane)));
330 static void i8xx_fbc_program_cfb(struct intel_fbc *fbc)
332 struct drm_i915_private *i915 = fbc->i915;
334 GEM_BUG_ON(range_overflows_end_t(u64, i915->dsm.stolen.start,
335 fbc->compressed_fb.start, U32_MAX));
336 GEM_BUG_ON(range_overflows_end_t(u64, i915->dsm.stolen.start,
337 fbc->compressed_llb.start, U32_MAX));
339 intel_de_write(i915, FBC_CFB_BASE,
340 i915->dsm.stolen.start + fbc->compressed_fb.start);
341 intel_de_write(i915, FBC_LL_BASE,
342 i915->dsm.stolen.start + fbc->compressed_llb.start);
345 static const struct intel_fbc_funcs i8xx_fbc_funcs = {
346 .activate = i8xx_fbc_activate,
347 .deactivate = i8xx_fbc_deactivate,
348 .is_active = i8xx_fbc_is_active,
349 .is_compressing = i8xx_fbc_is_compressing,
350 .nuke = i8xx_fbc_nuke,
351 .program_cfb = i8xx_fbc_program_cfb,
354 static void i965_fbc_nuke(struct intel_fbc *fbc)
356 struct intel_fbc_state *fbc_state = &fbc->state;
357 enum i9xx_plane_id i9xx_plane = fbc_state->plane->i9xx_plane;
358 struct drm_i915_private *dev_priv = fbc->i915;
360 intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
361 intel_de_read_fw(dev_priv, DSPSURF(i9xx_plane)));
364 static const struct intel_fbc_funcs i965_fbc_funcs = {
365 .activate = i8xx_fbc_activate,
366 .deactivate = i8xx_fbc_deactivate,
367 .is_active = i8xx_fbc_is_active,
368 .is_compressing = i8xx_fbc_is_compressing,
369 .nuke = i965_fbc_nuke,
370 .program_cfb = i8xx_fbc_program_cfb,
373 static u32 g4x_dpfc_ctl_limit(struct intel_fbc *fbc)
375 switch (fbc->limit) {
377 MISSING_CASE(fbc->limit);
380 return DPFC_CTL_LIMIT_1X;
382 return DPFC_CTL_LIMIT_2X;
384 return DPFC_CTL_LIMIT_4X;
388 static u32 g4x_dpfc_ctl(struct intel_fbc *fbc)
390 const struct intel_fbc_state *fbc_state = &fbc->state;
391 struct drm_i915_private *i915 = fbc->i915;
394 dpfc_ctl = g4x_dpfc_ctl_limit(fbc) |
395 DPFC_CTL_PLANE_G4X(fbc_state->plane->i9xx_plane);
398 dpfc_ctl |= DPFC_CTL_SR_EN;
400 if (fbc_state->fence_id >= 0) {
401 dpfc_ctl |= DPFC_CTL_FENCE_EN_G4X;
403 if (DISPLAY_VER(i915) < 6)
404 dpfc_ctl |= DPFC_CTL_FENCENO(fbc_state->fence_id);
410 static void g4x_fbc_activate(struct intel_fbc *fbc)
412 const struct intel_fbc_state *fbc_state = &fbc->state;
413 struct drm_i915_private *i915 = fbc->i915;
415 intel_de_write(i915, DPFC_FENCE_YOFF,
416 fbc_state->fence_y_offset);
418 intel_de_write(i915, DPFC_CONTROL,
419 DPFC_CTL_EN | g4x_dpfc_ctl(fbc));
422 static void g4x_fbc_deactivate(struct intel_fbc *fbc)
424 struct drm_i915_private *i915 = fbc->i915;
427 /* Disable compression */
428 dpfc_ctl = intel_de_read(i915, DPFC_CONTROL);
429 if (dpfc_ctl & DPFC_CTL_EN) {
430 dpfc_ctl &= ~DPFC_CTL_EN;
431 intel_de_write(i915, DPFC_CONTROL, dpfc_ctl);
435 static bool g4x_fbc_is_active(struct intel_fbc *fbc)
437 return intel_de_read(fbc->i915, DPFC_CONTROL) & DPFC_CTL_EN;
440 static bool g4x_fbc_is_compressing(struct intel_fbc *fbc)
442 return intel_de_read(fbc->i915, DPFC_STATUS) & DPFC_COMP_SEG_MASK;
445 static void g4x_fbc_program_cfb(struct intel_fbc *fbc)
447 struct drm_i915_private *i915 = fbc->i915;
449 intel_de_write(i915, DPFC_CB_BASE, fbc->compressed_fb.start);
452 static const struct intel_fbc_funcs g4x_fbc_funcs = {
453 .activate = g4x_fbc_activate,
454 .deactivate = g4x_fbc_deactivate,
455 .is_active = g4x_fbc_is_active,
456 .is_compressing = g4x_fbc_is_compressing,
457 .nuke = i965_fbc_nuke,
458 .program_cfb = g4x_fbc_program_cfb,
461 static void ilk_fbc_activate(struct intel_fbc *fbc)
463 struct intel_fbc_state *fbc_state = &fbc->state;
464 struct drm_i915_private *i915 = fbc->i915;
466 intel_de_write(i915, ILK_DPFC_FENCE_YOFF(fbc->id),
467 fbc_state->fence_y_offset);
469 intel_de_write(i915, ILK_DPFC_CONTROL(fbc->id),
470 DPFC_CTL_EN | g4x_dpfc_ctl(fbc));
473 static void ilk_fbc_deactivate(struct intel_fbc *fbc)
475 struct drm_i915_private *i915 = fbc->i915;
478 /* Disable compression */
479 dpfc_ctl = intel_de_read(i915, ILK_DPFC_CONTROL(fbc->id));
480 if (dpfc_ctl & DPFC_CTL_EN) {
481 dpfc_ctl &= ~DPFC_CTL_EN;
482 intel_de_write(i915, ILK_DPFC_CONTROL(fbc->id), dpfc_ctl);
486 static bool ilk_fbc_is_active(struct intel_fbc *fbc)
488 return intel_de_read(fbc->i915, ILK_DPFC_CONTROL(fbc->id)) & DPFC_CTL_EN;
491 static bool ilk_fbc_is_compressing(struct intel_fbc *fbc)
493 return intel_de_read(fbc->i915, ILK_DPFC_STATUS(fbc->id)) & DPFC_COMP_SEG_MASK;
496 static void ilk_fbc_program_cfb(struct intel_fbc *fbc)
498 struct drm_i915_private *i915 = fbc->i915;
500 intel_de_write(i915, ILK_DPFC_CB_BASE(fbc->id), fbc->compressed_fb.start);
503 static const struct intel_fbc_funcs ilk_fbc_funcs = {
504 .activate = ilk_fbc_activate,
505 .deactivate = ilk_fbc_deactivate,
506 .is_active = ilk_fbc_is_active,
507 .is_compressing = ilk_fbc_is_compressing,
508 .nuke = i965_fbc_nuke,
509 .program_cfb = ilk_fbc_program_cfb,
512 static void snb_fbc_program_fence(struct intel_fbc *fbc)
514 const struct intel_fbc_state *fbc_state = &fbc->state;
515 struct drm_i915_private *i915 = fbc->i915;
518 if (fbc_state->fence_id >= 0)
519 ctl = SNB_DPFC_FENCE_EN | SNB_DPFC_FENCENO(fbc_state->fence_id);
521 intel_de_write(i915, SNB_DPFC_CTL_SA, ctl);
522 intel_de_write(i915, SNB_DPFC_CPU_FENCE_OFFSET, fbc_state->fence_y_offset);
525 static void snb_fbc_activate(struct intel_fbc *fbc)
527 snb_fbc_program_fence(fbc);
529 ilk_fbc_activate(fbc);
532 static void snb_fbc_nuke(struct intel_fbc *fbc)
534 struct drm_i915_private *i915 = fbc->i915;
536 intel_de_write(i915, MSG_FBC_REND_STATE(fbc->id), FBC_REND_NUKE);
537 intel_de_posting_read(i915, MSG_FBC_REND_STATE(fbc->id));
540 static const struct intel_fbc_funcs snb_fbc_funcs = {
541 .activate = snb_fbc_activate,
542 .deactivate = ilk_fbc_deactivate,
543 .is_active = ilk_fbc_is_active,
544 .is_compressing = ilk_fbc_is_compressing,
545 .nuke = snb_fbc_nuke,
546 .program_cfb = ilk_fbc_program_cfb,
549 static void glk_fbc_program_cfb_stride(struct intel_fbc *fbc)
551 const struct intel_fbc_state *fbc_state = &fbc->state;
552 struct drm_i915_private *i915 = fbc->i915;
555 if (fbc_state->override_cfb_stride)
556 val |= FBC_STRIDE_OVERRIDE |
557 FBC_STRIDE(fbc_state->override_cfb_stride / fbc->limit);
559 intel_de_write(i915, GLK_FBC_STRIDE(fbc->id), val);
562 static void skl_fbc_program_cfb_stride(struct intel_fbc *fbc)
564 const struct intel_fbc_state *fbc_state = &fbc->state;
565 struct drm_i915_private *i915 = fbc->i915;
568 /* Display WA #0529: skl, kbl, bxt. */
569 if (fbc_state->override_cfb_stride)
570 val |= CHICKEN_FBC_STRIDE_OVERRIDE |
571 CHICKEN_FBC_STRIDE(fbc_state->override_cfb_stride / fbc->limit);
573 intel_de_rmw(i915, CHICKEN_MISC_4,
574 CHICKEN_FBC_STRIDE_OVERRIDE |
575 CHICKEN_FBC_STRIDE_MASK, val);
578 static u32 ivb_dpfc_ctl(struct intel_fbc *fbc)
580 const struct intel_fbc_state *fbc_state = &fbc->state;
581 struct drm_i915_private *i915 = fbc->i915;
584 dpfc_ctl = g4x_dpfc_ctl_limit(fbc);
586 if (IS_IVYBRIDGE(i915))
587 dpfc_ctl |= DPFC_CTL_PLANE_IVB(fbc_state->plane->i9xx_plane);
589 if (fbc_state->fence_id >= 0)
590 dpfc_ctl |= DPFC_CTL_FENCE_EN_IVB;
592 if (fbc->false_color)
593 dpfc_ctl |= DPFC_CTL_FALSE_COLOR;
598 static void ivb_fbc_activate(struct intel_fbc *fbc)
600 struct drm_i915_private *i915 = fbc->i915;
602 if (DISPLAY_VER(i915) >= 10)
603 glk_fbc_program_cfb_stride(fbc);
604 else if (DISPLAY_VER(i915) == 9)
605 skl_fbc_program_cfb_stride(fbc);
607 if (to_gt(i915)->ggtt->num_fences)
608 snb_fbc_program_fence(fbc);
610 intel_de_write(i915, ILK_DPFC_CONTROL(fbc->id),
611 DPFC_CTL_EN | ivb_dpfc_ctl(fbc));
614 static bool ivb_fbc_is_compressing(struct intel_fbc *fbc)
616 return intel_de_read(fbc->i915, ILK_DPFC_STATUS2(fbc->id)) & DPFC_COMP_SEG_MASK_IVB;
619 static void ivb_fbc_set_false_color(struct intel_fbc *fbc,
622 intel_de_rmw(fbc->i915, ILK_DPFC_CONTROL(fbc->id),
623 DPFC_CTL_FALSE_COLOR, enable ? DPFC_CTL_FALSE_COLOR : 0);
626 static const struct intel_fbc_funcs ivb_fbc_funcs = {
627 .activate = ivb_fbc_activate,
628 .deactivate = ilk_fbc_deactivate,
629 .is_active = ilk_fbc_is_active,
630 .is_compressing = ivb_fbc_is_compressing,
631 .nuke = snb_fbc_nuke,
632 .program_cfb = ilk_fbc_program_cfb,
633 .set_false_color = ivb_fbc_set_false_color,
636 static bool intel_fbc_hw_is_active(struct intel_fbc *fbc)
638 return fbc->funcs->is_active(fbc);
641 static void intel_fbc_hw_activate(struct intel_fbc *fbc)
643 trace_intel_fbc_activate(fbc->state.plane);
646 fbc->activated = true;
648 fbc->funcs->activate(fbc);
651 static void intel_fbc_hw_deactivate(struct intel_fbc *fbc)
653 trace_intel_fbc_deactivate(fbc->state.plane);
657 fbc->funcs->deactivate(fbc);
660 static bool intel_fbc_is_compressing(struct intel_fbc *fbc)
662 return fbc->funcs->is_compressing(fbc);
665 static void intel_fbc_nuke(struct intel_fbc *fbc)
667 struct drm_i915_private *i915 = fbc->i915;
669 lockdep_assert_held(&fbc->lock);
670 drm_WARN_ON(&i915->drm, fbc->flip_pending);
672 trace_intel_fbc_nuke(fbc->state.plane);
674 fbc->funcs->nuke(fbc);
677 static void intel_fbc_activate(struct intel_fbc *fbc)
679 lockdep_assert_held(&fbc->lock);
681 intel_fbc_hw_activate(fbc);
684 fbc->no_fbc_reason = NULL;
687 static void intel_fbc_deactivate(struct intel_fbc *fbc, const char *reason)
689 lockdep_assert_held(&fbc->lock);
692 intel_fbc_hw_deactivate(fbc);
694 fbc->no_fbc_reason = reason;
697 static u64 intel_fbc_cfb_base_max(struct drm_i915_private *i915)
699 if (DISPLAY_VER(i915) >= 5 || IS_G4X(i915))
705 static u64 intel_fbc_stolen_end(struct drm_i915_private *i915)
709 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
710 * reserved range size, so it always assumes the maximum (8mb) is used.
711 * If we enable FBC using a CFB on that memory range we'll get FIFO
712 * underruns, even if that range is not reserved by the BIOS. */
713 if (IS_BROADWELL(i915) ||
714 (DISPLAY_VER(i915) == 9 && !IS_BROXTON(i915)))
715 end = resource_size(&i915->dsm.stolen) - 8 * 1024 * 1024;
719 return min(end, intel_fbc_cfb_base_max(i915));
722 static int intel_fbc_min_limit(const struct intel_plane_state *plane_state)
724 return plane_state->hw.fb->format->cpp[0] == 2 ? 2 : 1;
727 static int intel_fbc_max_limit(struct drm_i915_private *i915)
729 /* WaFbcOnly1to1Ratio:ctg */
734 * FBC2 can only do 1:1, 1:2, 1:4, we limit
735 * FBC1 to the same out of convenience.
740 static int find_compression_limit(struct intel_fbc *fbc,
741 unsigned int size, int min_limit)
743 struct drm_i915_private *i915 = fbc->i915;
744 u64 end = intel_fbc_stolen_end(i915);
745 int ret, limit = min_limit;
749 /* Try to over-allocate to reduce reallocations and fragmentation. */
750 ret = i915_gem_stolen_insert_node_in_range(i915, &fbc->compressed_fb,
751 size <<= 1, 4096, 0, end);
755 for (; limit <= intel_fbc_max_limit(i915); limit <<= 1) {
756 ret = i915_gem_stolen_insert_node_in_range(i915, &fbc->compressed_fb,
757 size >>= 1, 4096, 0, end);
765 static int intel_fbc_alloc_cfb(struct intel_fbc *fbc,
766 unsigned int size, int min_limit)
768 struct drm_i915_private *i915 = fbc->i915;
771 drm_WARN_ON(&i915->drm,
772 drm_mm_node_allocated(&fbc->compressed_fb));
773 drm_WARN_ON(&i915->drm,
774 drm_mm_node_allocated(&fbc->compressed_llb));
776 if (DISPLAY_VER(i915) < 5 && !IS_G4X(i915)) {
777 ret = i915_gem_stolen_insert_node(i915, &fbc->compressed_llb,
783 ret = find_compression_limit(fbc, size, min_limit);
786 else if (ret > min_limit)
787 drm_info_once(&i915->drm,
788 "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");
792 drm_dbg_kms(&i915->drm,
793 "reserved %llu bytes of contiguous stolen space for FBC, limit: %d\n",
794 fbc->compressed_fb.size, fbc->limit);
799 if (drm_mm_node_allocated(&fbc->compressed_llb))
800 i915_gem_stolen_remove_node(i915, &fbc->compressed_llb);
802 if (drm_mm_initialized(&i915->mm.stolen))
803 drm_info_once(&i915->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);
807 static void intel_fbc_program_cfb(struct intel_fbc *fbc)
809 fbc->funcs->program_cfb(fbc);
812 static void intel_fbc_program_workarounds(struct intel_fbc *fbc)
814 /* Wa_22014263786:icl,jsl,tgl,dg1,rkl,adls,adlp,mtl */
815 if (DISPLAY_VER(fbc->i915) >= 11 && !IS_DG2(fbc->i915))
816 intel_de_rmw(fbc->i915, ILK_DPFC_CHICKEN(fbc->id), 0,
817 DPFC_CHICKEN_FORCE_SLB_INVALIDATION);
820 static void __intel_fbc_cleanup_cfb(struct intel_fbc *fbc)
822 struct drm_i915_private *i915 = fbc->i915;
824 if (WARN_ON(intel_fbc_hw_is_active(fbc)))
827 if (drm_mm_node_allocated(&fbc->compressed_llb))
828 i915_gem_stolen_remove_node(i915, &fbc->compressed_llb);
829 if (drm_mm_node_allocated(&fbc->compressed_fb))
830 i915_gem_stolen_remove_node(i915, &fbc->compressed_fb);
833 void intel_fbc_cleanup(struct drm_i915_private *i915)
835 struct intel_fbc *fbc;
836 enum intel_fbc_id fbc_id;
838 for_each_intel_fbc(i915, fbc, fbc_id) {
839 mutex_lock(&fbc->lock);
840 __intel_fbc_cleanup_cfb(fbc);
841 mutex_unlock(&fbc->lock);
847 static bool stride_is_valid(const struct intel_plane_state *plane_state)
849 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
850 const struct drm_framebuffer *fb = plane_state->hw.fb;
851 unsigned int stride = intel_fbc_plane_stride(plane_state) *
854 /* This should have been caught earlier. */
855 if (drm_WARN_ON_ONCE(&i915->drm, (stride & (64 - 1)) != 0))
858 /* Below are the additional FBC restrictions. */
862 if (DISPLAY_VER(i915) == 2 || DISPLAY_VER(i915) == 3)
863 return stride == 4096 || stride == 8192;
865 if (DISPLAY_VER(i915) == 4 && !IS_G4X(i915) && stride < 2048)
868 /* Display WA #1105: skl,bxt,kbl,cfl,glk */
869 if ((DISPLAY_VER(i915) == 9 || IS_GEMINILAKE(i915)) &&
870 fb->modifier == DRM_FORMAT_MOD_LINEAR && stride & 511)
879 static bool pixel_format_is_valid(const struct intel_plane_state *plane_state)
881 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
882 const struct drm_framebuffer *fb = plane_state->hw.fb;
884 switch (fb->format->format) {
885 case DRM_FORMAT_XRGB8888:
886 case DRM_FORMAT_XBGR8888:
888 case DRM_FORMAT_XRGB1555:
889 case DRM_FORMAT_RGB565:
890 /* 16bpp not supported on gen2 */
891 if (DISPLAY_VER(i915) == 2)
893 /* WaFbcOnly1to1Ratio:ctg */
902 static bool rotation_is_valid(const struct intel_plane_state *plane_state)
904 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
905 const struct drm_framebuffer *fb = plane_state->hw.fb;
906 unsigned int rotation = plane_state->hw.rotation;
908 if (DISPLAY_VER(i915) >= 9 && fb->format->format == DRM_FORMAT_RGB565 &&
909 drm_rotation_90_or_270(rotation))
911 else if (DISPLAY_VER(i915) <= 4 && !IS_G4X(i915) &&
912 rotation != DRM_MODE_ROTATE_0)
919 * For some reason, the hardware tracking starts looking at whatever we
920 * programmed as the display plane base address register. It does not look at
921 * the X and Y offset registers. That's why we include the src x/y offsets
922 * instead of just looking at the plane size.
924 static bool intel_fbc_hw_tracking_covers_screen(const struct intel_plane_state *plane_state)
926 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
927 unsigned int effective_w, effective_h, max_w, max_h;
929 if (DISPLAY_VER(i915) >= 10) {
932 } else if (DISPLAY_VER(i915) >= 8 || IS_HASWELL(i915)) {
935 } else if (IS_G4X(i915) || DISPLAY_VER(i915) >= 5) {
943 effective_w = plane_state->view.color_plane[0].x +
944 (drm_rect_width(&plane_state->uapi.src) >> 16);
945 effective_h = plane_state->view.color_plane[0].y +
946 (drm_rect_height(&plane_state->uapi.src) >> 16);
948 return effective_w <= max_w && effective_h <= max_h;
951 static bool tiling_is_valid(const struct intel_plane_state *plane_state)
953 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
954 const struct drm_framebuffer *fb = plane_state->hw.fb;
956 switch (fb->modifier) {
957 case DRM_FORMAT_MOD_LINEAR:
958 case I915_FORMAT_MOD_Y_TILED:
959 case I915_FORMAT_MOD_Yf_TILED:
960 return DISPLAY_VER(i915) >= 9;
961 case I915_FORMAT_MOD_4_TILED:
962 case I915_FORMAT_MOD_X_TILED:
969 static void intel_fbc_update_state(struct intel_atomic_state *state,
970 struct intel_crtc *crtc,
971 struct intel_plane *plane)
973 struct drm_i915_private *i915 = to_i915(state->base.dev);
974 const struct intel_crtc_state *crtc_state =
975 intel_atomic_get_new_crtc_state(state, crtc);
976 const struct intel_plane_state *plane_state =
977 intel_atomic_get_new_plane_state(state, plane);
978 struct intel_fbc *fbc = plane->fbc;
979 struct intel_fbc_state *fbc_state = &fbc->state;
981 WARN_ON(plane_state->no_fbc_reason);
982 WARN_ON(fbc_state->plane && fbc_state->plane != plane);
984 fbc_state->plane = plane;
986 /* FBC1 compression interval: arbitrary choice of 1 second */
987 fbc_state->interval = drm_mode_vrefresh(&crtc_state->hw.adjusted_mode);
989 fbc_state->fence_y_offset = intel_plane_fence_y_offset(plane_state);
991 drm_WARN_ON(&i915->drm, plane_state->flags & PLANE_HAS_FENCE &&
992 !plane_state->ggtt_vma->fence);
994 if (plane_state->flags & PLANE_HAS_FENCE &&
995 plane_state->ggtt_vma->fence)
996 fbc_state->fence_id = plane_state->ggtt_vma->fence->id;
998 fbc_state->fence_id = -1;
1000 fbc_state->cfb_stride = intel_fbc_cfb_stride(plane_state);
1001 fbc_state->cfb_size = intel_fbc_cfb_size(plane_state);
1002 fbc_state->override_cfb_stride = intel_fbc_override_cfb_stride(plane_state);
1005 static bool intel_fbc_is_fence_ok(const struct intel_plane_state *plane_state)
1007 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
1010 * The use of a CPU fence is one of two ways to detect writes by the
1011 * CPU to the scanout and trigger updates to the FBC.
1013 * The other method is by software tracking (see
1014 * intel_fbc_invalidate/flush()), it will manually notify FBC and nuke
1015 * the current compressed buffer and recompress it.
1017 * Note that is possible for a tiled surface to be unmappable (and
1018 * so have no fence associated with it) due to aperture constraints
1019 * at the time of pinning.
1021 return DISPLAY_VER(i915) >= 9 ||
1022 (plane_state->flags & PLANE_HAS_FENCE &&
1023 plane_state->ggtt_vma->fence);
1026 static bool intel_fbc_is_cfb_ok(const struct intel_plane_state *plane_state)
1028 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1029 struct intel_fbc *fbc = plane->fbc;
1031 return intel_fbc_min_limit(plane_state) <= fbc->limit &&
1032 intel_fbc_cfb_size(plane_state) <= fbc->compressed_fb.size * fbc->limit;
1035 static bool intel_fbc_is_ok(const struct intel_plane_state *plane_state)
1037 return !plane_state->no_fbc_reason &&
1038 intel_fbc_is_fence_ok(plane_state) &&
1039 intel_fbc_is_cfb_ok(plane_state);
1042 static int intel_fbc_check_plane(struct intel_atomic_state *state,
1043 struct intel_plane *plane)
1045 struct drm_i915_private *i915 = to_i915(state->base.dev);
1046 struct intel_plane_state *plane_state =
1047 intel_atomic_get_new_plane_state(state, plane);
1048 const struct drm_framebuffer *fb = plane_state->hw.fb;
1049 struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
1050 const struct intel_crtc_state *crtc_state;
1051 struct intel_fbc *fbc = plane->fbc;
1056 if (intel_vgpu_active(i915)) {
1057 plane_state->no_fbc_reason = "VGPU active";
1061 if (!i915->params.enable_fbc) {
1062 plane_state->no_fbc_reason = "disabled per module param or by default";
1066 if (!plane_state->uapi.visible) {
1067 plane_state->no_fbc_reason = "plane not visible";
1071 crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
1073 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
1074 plane_state->no_fbc_reason = "interlaced mode not supported";
1078 if (crtc_state->double_wide) {
1079 plane_state->no_fbc_reason = "double wide pipe not supported";
1084 * Display 12+ is not supporting FBC with PSR2.
1085 * Recommendation is to keep this combination disabled
1086 * Bspec: 50422 HSD: 14010260002
1088 if (DISPLAY_VER(i915) >= 12 && crtc_state->has_psr2) {
1089 plane_state->no_fbc_reason = "PSR2 enabled";
1093 /* Wa_14016291713 */
1094 if ((IS_DISPLAY_VER(i915, 12, 13) ||
1095 IS_MTL_DISPLAY_STEP(i915, STEP_A0, STEP_C0)) &&
1096 crtc_state->has_psr) {
1097 plane_state->no_fbc_reason = "PSR1 enabled (Wa_14016291713)";
1101 if (!pixel_format_is_valid(plane_state)) {
1102 plane_state->no_fbc_reason = "pixel format not supported";
1106 if (!tiling_is_valid(plane_state)) {
1107 plane_state->no_fbc_reason = "tiling not supported";
1111 if (!rotation_is_valid(plane_state)) {
1112 plane_state->no_fbc_reason = "rotation not supported";
1116 if (!stride_is_valid(plane_state)) {
1117 plane_state->no_fbc_reason = "stride not supported";
1121 if (plane_state->hw.pixel_blend_mode != DRM_MODE_BLEND_PIXEL_NONE &&
1122 fb->format->has_alpha) {
1123 plane_state->no_fbc_reason = "per-pixel alpha not supported";
1127 if (!intel_fbc_hw_tracking_covers_screen(plane_state)) {
1128 plane_state->no_fbc_reason = "plane size too big";
1133 * Work around a problem on GEN9+ HW, where enabling FBC on a plane
1134 * having a Y offset that isn't divisible by 4 causes FIFO underrun
1135 * and screen flicker.
1137 if (DISPLAY_VER(i915) >= 9 &&
1138 plane_state->view.color_plane[0].y & 3) {
1139 plane_state->no_fbc_reason = "plane start Y offset misaligned";
1143 /* Wa_22010751166: icl, ehl, tgl, dg1, rkl */
1144 if (DISPLAY_VER(i915) >= 11 &&
1145 (plane_state->view.color_plane[0].y +
1146 (drm_rect_height(&plane_state->uapi.src) >> 16)) & 3) {
1147 plane_state->no_fbc_reason = "plane end Y offset misaligned";
1151 /* WaFbcExceedCdClockThreshold:hsw,bdw */
1152 if (IS_HASWELL(i915) || IS_BROADWELL(i915)) {
1153 const struct intel_cdclk_state *cdclk_state;
1155 cdclk_state = intel_atomic_get_cdclk_state(state);
1156 if (IS_ERR(cdclk_state))
1157 return PTR_ERR(cdclk_state);
1159 if (crtc_state->pixel_rate >= cdclk_state->logical.cdclk * 95 / 100) {
1160 plane_state->no_fbc_reason = "pixel rate too high";
1165 plane_state->no_fbc_reason = NULL;
1171 static bool intel_fbc_can_flip_nuke(struct intel_atomic_state *state,
1172 struct intel_crtc *crtc,
1173 struct intel_plane *plane)
1175 const struct intel_crtc_state *new_crtc_state =
1176 intel_atomic_get_new_crtc_state(state, crtc);
1177 const struct intel_plane_state *old_plane_state =
1178 intel_atomic_get_old_plane_state(state, plane);
1179 const struct intel_plane_state *new_plane_state =
1180 intel_atomic_get_new_plane_state(state, plane);
1181 const struct drm_framebuffer *old_fb = old_plane_state->hw.fb;
1182 const struct drm_framebuffer *new_fb = new_plane_state->hw.fb;
1184 if (intel_crtc_needs_modeset(new_crtc_state))
1187 if (!intel_fbc_is_ok(old_plane_state) ||
1188 !intel_fbc_is_ok(new_plane_state))
1191 if (old_fb->format->format != new_fb->format->format)
1194 if (old_fb->modifier != new_fb->modifier)
1197 if (intel_fbc_plane_stride(old_plane_state) !=
1198 intel_fbc_plane_stride(new_plane_state))
1201 if (intel_fbc_cfb_stride(old_plane_state) !=
1202 intel_fbc_cfb_stride(new_plane_state))
1205 if (intel_fbc_cfb_size(old_plane_state) !=
1206 intel_fbc_cfb_size(new_plane_state))
1209 if (intel_fbc_override_cfb_stride(old_plane_state) !=
1210 intel_fbc_override_cfb_stride(new_plane_state))
1216 static bool __intel_fbc_pre_update(struct intel_atomic_state *state,
1217 struct intel_crtc *crtc,
1218 struct intel_plane *plane)
1220 struct drm_i915_private *i915 = to_i915(state->base.dev);
1221 struct intel_fbc *fbc = plane->fbc;
1222 bool need_vblank_wait = false;
1224 lockdep_assert_held(&fbc->lock);
1226 fbc->flip_pending = true;
1228 if (intel_fbc_can_flip_nuke(state, crtc, plane))
1229 return need_vblank_wait;
1231 intel_fbc_deactivate(fbc, "update pending");
1234 * Display WA #1198: glk+
1235 * Need an extra vblank wait between FBC disable and most plane
1236 * updates. Bspec says this is only needed for plane disable, but
1237 * that is not true. Touching most plane registers will cause the
1238 * corruption to appear. Also SKL/derivatives do not seem to be
1241 * TODO: could optimize this a bit by sampling the frame
1242 * counter when we disable FBC (if it was already done earlier)
1243 * and skipping the extra vblank wait before the plane update
1244 * if at least one frame has already passed.
1246 if (fbc->activated && DISPLAY_VER(i915) >= 10)
1247 need_vblank_wait = true;
1248 fbc->activated = false;
1250 return need_vblank_wait;
1253 bool intel_fbc_pre_update(struct intel_atomic_state *state,
1254 struct intel_crtc *crtc)
1256 const struct intel_plane_state *plane_state;
1257 bool need_vblank_wait = false;
1258 struct intel_plane *plane;
1261 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1262 struct intel_fbc *fbc = plane->fbc;
1264 if (!fbc || plane->pipe != crtc->pipe)
1267 mutex_lock(&fbc->lock);
1269 if (fbc->state.plane == plane)
1270 need_vblank_wait |= __intel_fbc_pre_update(state, crtc, plane);
1272 mutex_unlock(&fbc->lock);
1275 return need_vblank_wait;
1278 static void __intel_fbc_disable(struct intel_fbc *fbc)
1280 struct drm_i915_private *i915 = fbc->i915;
1281 struct intel_plane *plane = fbc->state.plane;
1283 lockdep_assert_held(&fbc->lock);
1284 drm_WARN_ON(&i915->drm, fbc->active);
1286 drm_dbg_kms(&i915->drm, "Disabling FBC on [PLANE:%d:%s]\n",
1287 plane->base.base.id, plane->base.name);
1289 __intel_fbc_cleanup_cfb(fbc);
1291 fbc->state.plane = NULL;
1292 fbc->flip_pending = false;
1296 static void __intel_fbc_post_update(struct intel_fbc *fbc)
1298 lockdep_assert_held(&fbc->lock);
1300 fbc->flip_pending = false;
1302 if (!fbc->busy_bits)
1303 intel_fbc_activate(fbc);
1305 intel_fbc_deactivate(fbc, "frontbuffer write");
1308 void intel_fbc_post_update(struct intel_atomic_state *state,
1309 struct intel_crtc *crtc)
1311 const struct intel_plane_state *plane_state;
1312 struct intel_plane *plane;
1315 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1316 struct intel_fbc *fbc = plane->fbc;
1318 if (!fbc || plane->pipe != crtc->pipe)
1321 mutex_lock(&fbc->lock);
1323 if (fbc->state.plane == plane)
1324 __intel_fbc_post_update(fbc);
1326 mutex_unlock(&fbc->lock);
1330 static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
1332 if (fbc->state.plane)
1333 return fbc->state.plane->frontbuffer_bit;
1338 static void __intel_fbc_invalidate(struct intel_fbc *fbc,
1339 unsigned int frontbuffer_bits,
1340 enum fb_op_origin origin)
1342 if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1345 mutex_lock(&fbc->lock);
1347 frontbuffer_bits &= intel_fbc_get_frontbuffer_bit(fbc);
1348 if (!frontbuffer_bits)
1351 fbc->busy_bits |= frontbuffer_bits;
1352 intel_fbc_deactivate(fbc, "frontbuffer write");
1355 mutex_unlock(&fbc->lock);
1358 void intel_fbc_invalidate(struct drm_i915_private *i915,
1359 unsigned int frontbuffer_bits,
1360 enum fb_op_origin origin)
1362 struct intel_fbc *fbc;
1363 enum intel_fbc_id fbc_id;
1365 for_each_intel_fbc(i915, fbc, fbc_id)
1366 __intel_fbc_invalidate(fbc, frontbuffer_bits, origin);
1370 static void __intel_fbc_flush(struct intel_fbc *fbc,
1371 unsigned int frontbuffer_bits,
1372 enum fb_op_origin origin)
1374 mutex_lock(&fbc->lock);
1376 frontbuffer_bits &= intel_fbc_get_frontbuffer_bit(fbc);
1377 if (!frontbuffer_bits)
1380 fbc->busy_bits &= ~frontbuffer_bits;
1382 if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1385 if (fbc->busy_bits || fbc->flip_pending)
1389 intel_fbc_nuke(fbc);
1391 intel_fbc_activate(fbc);
1394 mutex_unlock(&fbc->lock);
1397 void intel_fbc_flush(struct drm_i915_private *i915,
1398 unsigned int frontbuffer_bits,
1399 enum fb_op_origin origin)
1401 struct intel_fbc *fbc;
1402 enum intel_fbc_id fbc_id;
1404 for_each_intel_fbc(i915, fbc, fbc_id)
1405 __intel_fbc_flush(fbc, frontbuffer_bits, origin);
1408 int intel_fbc_atomic_check(struct intel_atomic_state *state)
1410 struct intel_plane_state *plane_state;
1411 struct intel_plane *plane;
1414 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1417 ret = intel_fbc_check_plane(state, plane);
1425 static void __intel_fbc_enable(struct intel_atomic_state *state,
1426 struct intel_crtc *crtc,
1427 struct intel_plane *plane)
1429 struct drm_i915_private *i915 = to_i915(state->base.dev);
1430 const struct intel_plane_state *plane_state =
1431 intel_atomic_get_new_plane_state(state, plane);
1432 struct intel_fbc *fbc = plane->fbc;
1434 lockdep_assert_held(&fbc->lock);
1436 if (fbc->state.plane) {
1437 if (fbc->state.plane != plane)
1440 if (intel_fbc_is_ok(plane_state)) {
1441 intel_fbc_update_state(state, crtc, plane);
1445 __intel_fbc_disable(fbc);
1448 drm_WARN_ON(&i915->drm, fbc->active);
1450 fbc->no_fbc_reason = plane_state->no_fbc_reason;
1451 if (fbc->no_fbc_reason)
1454 if (!intel_fbc_is_fence_ok(plane_state)) {
1455 fbc->no_fbc_reason = "framebuffer not fenced";
1459 if (fbc->underrun_detected) {
1460 fbc->no_fbc_reason = "FIFO underrun";
1464 if (intel_fbc_alloc_cfb(fbc, intel_fbc_cfb_size(plane_state),
1465 intel_fbc_min_limit(plane_state))) {
1466 fbc->no_fbc_reason = "not enough stolen memory";
1470 drm_dbg_kms(&i915->drm, "Enabling FBC on [PLANE:%d:%s]\n",
1471 plane->base.base.id, plane->base.name);
1472 fbc->no_fbc_reason = "FBC enabled but not active yet\n";
1474 intel_fbc_update_state(state, crtc, plane);
1476 intel_fbc_program_workarounds(fbc);
1477 intel_fbc_program_cfb(fbc);
1481 * intel_fbc_disable - disable FBC if it's associated with crtc
1484 * This function disables FBC if it's associated with the provided CRTC.
1486 void intel_fbc_disable(struct intel_crtc *crtc)
1488 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1489 struct intel_plane *plane;
1491 for_each_intel_plane(&i915->drm, plane) {
1492 struct intel_fbc *fbc = plane->fbc;
1494 if (!fbc || plane->pipe != crtc->pipe)
1497 mutex_lock(&fbc->lock);
1498 if (fbc->state.plane == plane)
1499 __intel_fbc_disable(fbc);
1500 mutex_unlock(&fbc->lock);
1504 void intel_fbc_update(struct intel_atomic_state *state,
1505 struct intel_crtc *crtc)
1507 const struct intel_crtc_state *crtc_state =
1508 intel_atomic_get_new_crtc_state(state, crtc);
1509 const struct intel_plane_state *plane_state;
1510 struct intel_plane *plane;
1513 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1514 struct intel_fbc *fbc = plane->fbc;
1516 if (!fbc || plane->pipe != crtc->pipe)
1519 mutex_lock(&fbc->lock);
1521 if (intel_crtc_needs_fastset(crtc_state) &&
1522 plane_state->no_fbc_reason) {
1523 if (fbc->state.plane == plane)
1524 __intel_fbc_disable(fbc);
1526 __intel_fbc_enable(state, crtc, plane);
1529 mutex_unlock(&fbc->lock);
1533 static void intel_fbc_underrun_work_fn(struct work_struct *work)
1535 struct intel_fbc *fbc = container_of(work, typeof(*fbc), underrun_work);
1536 struct drm_i915_private *i915 = fbc->i915;
1538 mutex_lock(&fbc->lock);
1540 /* Maybe we were scheduled twice. */
1541 if (fbc->underrun_detected || !fbc->state.plane)
1544 drm_dbg_kms(&i915->drm, "Disabling FBC due to FIFO underrun.\n");
1545 fbc->underrun_detected = true;
1547 intel_fbc_deactivate(fbc, "FIFO underrun");
1548 if (!fbc->flip_pending)
1549 intel_crtc_wait_for_next_vblank(intel_crtc_for_pipe(i915, fbc->state.plane->pipe));
1550 __intel_fbc_disable(fbc);
1552 mutex_unlock(&fbc->lock);
1555 static void __intel_fbc_reset_underrun(struct intel_fbc *fbc)
1557 struct drm_i915_private *i915 = fbc->i915;
1559 cancel_work_sync(&fbc->underrun_work);
1561 mutex_lock(&fbc->lock);
1563 if (fbc->underrun_detected) {
1564 drm_dbg_kms(&i915->drm,
1565 "Re-allowing FBC after fifo underrun\n");
1566 fbc->no_fbc_reason = "FIFO underrun cleared";
1569 fbc->underrun_detected = false;
1570 mutex_unlock(&fbc->lock);
1574 * intel_fbc_reset_underrun - reset FBC fifo underrun status.
1575 * @i915: the i915 device
1577 * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we
1578 * want to re-enable FBC after an underrun to increase test coverage.
1580 void intel_fbc_reset_underrun(struct drm_i915_private *i915)
1582 struct intel_fbc *fbc;
1583 enum intel_fbc_id fbc_id;
1585 for_each_intel_fbc(i915, fbc, fbc_id)
1586 __intel_fbc_reset_underrun(fbc);
1589 static void __intel_fbc_handle_fifo_underrun_irq(struct intel_fbc *fbc)
1592 * There's no guarantee that underrun_detected won't be set to true
1593 * right after this check and before the work is scheduled, but that's
1594 * not a problem since we'll check it again under the work function
1595 * while FBC is locked. This check here is just to prevent us from
1596 * unnecessarily scheduling the work, and it relies on the fact that we
1597 * never switch underrun_detect back to false after it's true.
1599 if (READ_ONCE(fbc->underrun_detected))
1602 schedule_work(&fbc->underrun_work);
1606 * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
1607 * @i915: i915 device
1609 * Without FBC, most underruns are harmless and don't really cause too many
1610 * problems, except for an annoying message on dmesg. With FBC, underruns can
1611 * become black screens or even worse, especially when paired with bad
1612 * watermarks. So in order for us to be on the safe side, completely disable FBC
1613 * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
1614 * already suggests that watermarks may be bad, so try to be as safe as
1617 * This function is called from the IRQ handler.
1619 void intel_fbc_handle_fifo_underrun_irq(struct drm_i915_private *i915)
1621 struct intel_fbc *fbc;
1622 enum intel_fbc_id fbc_id;
1624 for_each_intel_fbc(i915, fbc, fbc_id)
1625 __intel_fbc_handle_fifo_underrun_irq(fbc);
1629 * The DDX driver changes its behavior depending on the value it reads from
1630 * i915.enable_fbc, so sanitize it by translating the default value into either
1631 * 0 or 1 in order to allow it to know what's going on.
1633 * Notice that this is done at driver initialization and we still allow user
1634 * space to change the value during runtime without sanitizing it again. IGT
1635 * relies on being able to change i915.enable_fbc at runtime.
1637 static int intel_sanitize_fbc_option(struct drm_i915_private *i915)
1639 if (i915->params.enable_fbc >= 0)
1640 return !!i915->params.enable_fbc;
1645 if (IS_BROADWELL(i915) || DISPLAY_VER(i915) >= 9)
1651 static bool need_fbc_vtd_wa(struct drm_i915_private *i915)
1653 /* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
1654 if (i915_vtd_active(i915) &&
1655 (IS_SKYLAKE(i915) || IS_BROXTON(i915))) {
1656 drm_info(&i915->drm,
1657 "Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
1664 void intel_fbc_add_plane(struct intel_fbc *fbc, struct intel_plane *plane)
1669 static struct intel_fbc *intel_fbc_create(struct drm_i915_private *i915,
1670 enum intel_fbc_id fbc_id)
1672 struct intel_fbc *fbc;
1674 fbc = kzalloc(sizeof(*fbc), GFP_KERNEL);
1680 INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
1681 mutex_init(&fbc->lock);
1683 if (DISPLAY_VER(i915) >= 7)
1684 fbc->funcs = &ivb_fbc_funcs;
1685 else if (DISPLAY_VER(i915) == 6)
1686 fbc->funcs = &snb_fbc_funcs;
1687 else if (DISPLAY_VER(i915) == 5)
1688 fbc->funcs = &ilk_fbc_funcs;
1689 else if (IS_G4X(i915))
1690 fbc->funcs = &g4x_fbc_funcs;
1691 else if (DISPLAY_VER(i915) == 4)
1692 fbc->funcs = &i965_fbc_funcs;
1694 fbc->funcs = &i8xx_fbc_funcs;
1700 * intel_fbc_init - Initialize FBC
1701 * @i915: the i915 device
1703 * This function might be called during PM init process.
1705 void intel_fbc_init(struct drm_i915_private *i915)
1707 enum intel_fbc_id fbc_id;
1709 if (!drm_mm_initialized(&i915->mm.stolen))
1710 RUNTIME_INFO(i915)->fbc_mask = 0;
1712 if (need_fbc_vtd_wa(i915))
1713 RUNTIME_INFO(i915)->fbc_mask = 0;
1715 i915->params.enable_fbc = intel_sanitize_fbc_option(i915);
1716 drm_dbg_kms(&i915->drm, "Sanitized enable_fbc value: %d\n",
1717 i915->params.enable_fbc);
1719 for_each_fbc_id(i915, fbc_id)
1720 i915->display.fbc[fbc_id] = intel_fbc_create(i915, fbc_id);
1724 * intel_fbc_sanitize - Sanitize FBC
1725 * @i915: the i915 device
1727 * Make sure FBC is initially disabled since we have no
1728 * idea eg. into which parts of stolen it might be scribbling
1731 void intel_fbc_sanitize(struct drm_i915_private *i915)
1733 struct intel_fbc *fbc;
1734 enum intel_fbc_id fbc_id;
1736 for_each_intel_fbc(i915, fbc, fbc_id) {
1737 if (intel_fbc_hw_is_active(fbc))
1738 intel_fbc_hw_deactivate(fbc);
1742 static int intel_fbc_debugfs_status_show(struct seq_file *m, void *unused)
1744 struct intel_fbc *fbc = m->private;
1745 struct drm_i915_private *i915 = fbc->i915;
1746 struct intel_plane *plane;
1747 intel_wakeref_t wakeref;
1749 drm_modeset_lock_all(&i915->drm);
1751 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1752 mutex_lock(&fbc->lock);
1755 seq_puts(m, "FBC enabled\n");
1756 seq_printf(m, "Compressing: %s\n",
1757 str_yes_no(intel_fbc_is_compressing(fbc)));
1759 seq_printf(m, "FBC disabled: %s\n", fbc->no_fbc_reason);
1762 for_each_intel_plane(&i915->drm, plane) {
1763 const struct intel_plane_state *plane_state =
1764 to_intel_plane_state(plane->base.state);
1766 if (plane->fbc != fbc)
1769 seq_printf(m, "%c [PLANE:%d:%s]: %s\n",
1770 fbc->state.plane == plane ? '*' : ' ',
1771 plane->base.base.id, plane->base.name,
1772 plane_state->no_fbc_reason ?: "FBC possible");
1775 mutex_unlock(&fbc->lock);
1776 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1778 drm_modeset_unlock_all(&i915->drm);
1783 DEFINE_SHOW_ATTRIBUTE(intel_fbc_debugfs_status);
1785 static int intel_fbc_debugfs_false_color_get(void *data, u64 *val)
1787 struct intel_fbc *fbc = data;
1789 *val = fbc->false_color;
1794 static int intel_fbc_debugfs_false_color_set(void *data, u64 val)
1796 struct intel_fbc *fbc = data;
1798 mutex_lock(&fbc->lock);
1800 fbc->false_color = val;
1803 fbc->funcs->set_false_color(fbc, fbc->false_color);
1805 mutex_unlock(&fbc->lock);
1810 DEFINE_DEBUGFS_ATTRIBUTE(intel_fbc_debugfs_false_color_fops,
1811 intel_fbc_debugfs_false_color_get,
1812 intel_fbc_debugfs_false_color_set,
1815 static void intel_fbc_debugfs_add(struct intel_fbc *fbc,
1816 struct dentry *parent)
1818 debugfs_create_file("i915_fbc_status", 0444, parent,
1819 fbc, &intel_fbc_debugfs_status_fops);
1821 if (fbc->funcs->set_false_color)
1822 debugfs_create_file_unsafe("i915_fbc_false_color", 0644, parent,
1823 fbc, &intel_fbc_debugfs_false_color_fops);
1826 void intel_fbc_crtc_debugfs_add(struct intel_crtc *crtc)
1828 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1831 intel_fbc_debugfs_add(plane->fbc, crtc->base.debugfs_entry);
1834 /* FIXME: remove this once igt is on board with per-crtc stuff */
1835 void intel_fbc_debugfs_register(struct drm_i915_private *i915)
1837 struct drm_minor *minor = i915->drm.primary;
1838 struct intel_fbc *fbc;
1840 fbc = i915->display.fbc[INTEL_FBC_A];
1842 intel_fbc_debugfs_add(fbc, minor->debugfs_root);