Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
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
24/**
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.
30 *
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.
34 *
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.
39 */
40
41#include <drm/drm_fourcc.h>
42
43#include "i915_drv.h"
44#include "i915_trace.h"
45#include "i915_vgpu.h"
46#include "intel_display_types.h"
47#include "intel_fbc.h"
48#include "intel_frontbuffer.h"
49
50/*
51 * For SKL+, the plane source size used by the hardware is based on the value we
52 * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
53 * we wrote to PIPESRC.
54 */
55static void intel_fbc_get_plane_source_size(const struct intel_fbc_state_cache *cache,
56 int *width, int *height)
57{
58 if (width)
59 *width = cache->plane.src_w;
60 if (height)
61 *height = cache->plane.src_h;
62}
63
64static int intel_fbc_calculate_cfb_size(struct drm_i915_private *dev_priv,
65 const struct intel_fbc_state_cache *cache)
66{
67 int lines;
68
69 intel_fbc_get_plane_source_size(cache, NULL, &lines);
70 if (IS_GEN(dev_priv, 7))
71 lines = min(lines, 2048);
72 else if (INTEL_GEN(dev_priv) >= 8)
73 lines = min(lines, 2560);
74
75 /* Hardware needs the full buffer stride, not just the active area. */
76 return lines * cache->fb.stride;
77}
78
79static void i8xx_fbc_deactivate(struct drm_i915_private *dev_priv)
80{
81 u32 fbc_ctl;
82
83 /* Disable compression */
84 fbc_ctl = intel_de_read(dev_priv, FBC_CONTROL);
85 if ((fbc_ctl & FBC_CTL_EN) == 0)
86 return;
87
88 fbc_ctl &= ~FBC_CTL_EN;
89 intel_de_write(dev_priv, FBC_CONTROL, fbc_ctl);
90
91 /* Wait for compressing bit to clear */
92 if (intel_de_wait_for_clear(dev_priv, FBC_STATUS,
93 FBC_STAT_COMPRESSING, 10)) {
94 drm_dbg_kms(&dev_priv->drm, "FBC idle timed out\n");
95 return;
96 }
97}
98
99static void i8xx_fbc_activate(struct drm_i915_private *dev_priv)
100{
101 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
102 int cfb_pitch;
103 int i;
104 u32 fbc_ctl;
105
106 /* Note: fbc.threshold == 1 for i8xx */
107 cfb_pitch = params->cfb_size / FBC_LL_SIZE;
108 if (params->fb.stride < cfb_pitch)
109 cfb_pitch = params->fb.stride;
110
111 /* FBC_CTL wants 32B or 64B units */
112 if (IS_GEN(dev_priv, 2))
113 cfb_pitch = (cfb_pitch / 32) - 1;
114 else
115 cfb_pitch = (cfb_pitch / 64) - 1;
116
117 /* Clear old tags */
118 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
119 intel_de_write(dev_priv, FBC_TAG(i), 0);
120
121 if (IS_GEN(dev_priv, 4)) {
122 u32 fbc_ctl2;
123
124 /* Set it up... */
125 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM;
126 fbc_ctl2 |= FBC_CTL_PLANE(params->crtc.i9xx_plane);
127 if (params->fence_id >= 0)
128 fbc_ctl2 |= FBC_CTL_CPU_FENCE;
129 intel_de_write(dev_priv, FBC_CONTROL2, fbc_ctl2);
130 intel_de_write(dev_priv, FBC_FENCE_OFF,
131 params->fence_y_offset);
132 }
133
134 /* enable it... */
135 fbc_ctl = intel_de_read(dev_priv, FBC_CONTROL);
136 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
137 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
138 if (IS_I945GM(dev_priv))
139 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
140 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
141 if (params->fence_id >= 0)
142 fbc_ctl |= params->fence_id;
143 intel_de_write(dev_priv, FBC_CONTROL, fbc_ctl);
144}
145
146static bool i8xx_fbc_is_active(struct drm_i915_private *dev_priv)
147{
148 return intel_de_read(dev_priv, FBC_CONTROL) & FBC_CTL_EN;
149}
150
151static void g4x_fbc_activate(struct drm_i915_private *dev_priv)
152{
153 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
154 u32 dpfc_ctl;
155
156 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.i9xx_plane) | DPFC_SR_EN;
157 if (params->fb.format->cpp[0] == 2)
158 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
159 else
160 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
161
162 if (params->fence_id >= 0) {
163 dpfc_ctl |= DPFC_CTL_FENCE_EN | params->fence_id;
164 intel_de_write(dev_priv, DPFC_FENCE_YOFF,
165 params->fence_y_offset);
166 } else {
167 intel_de_write(dev_priv, DPFC_FENCE_YOFF, 0);
168 }
169
170 /* enable it... */
171 intel_de_write(dev_priv, DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
172}
173
174static void g4x_fbc_deactivate(struct drm_i915_private *dev_priv)
175{
176 u32 dpfc_ctl;
177
178 /* Disable compression */
179 dpfc_ctl = intel_de_read(dev_priv, DPFC_CONTROL);
180 if (dpfc_ctl & DPFC_CTL_EN) {
181 dpfc_ctl &= ~DPFC_CTL_EN;
182 intel_de_write(dev_priv, DPFC_CONTROL, dpfc_ctl);
183 }
184}
185
186static bool g4x_fbc_is_active(struct drm_i915_private *dev_priv)
187{
188 return intel_de_read(dev_priv, DPFC_CONTROL) & DPFC_CTL_EN;
189}
190
191/* This function forces a CFB recompression through the nuke operation. */
192static void intel_fbc_recompress(struct drm_i915_private *dev_priv)
193{
194 struct intel_fbc *fbc = &dev_priv->fbc;
195
196 trace_intel_fbc_nuke(fbc->crtc);
197
198 intel_de_write(dev_priv, MSG_FBC_REND_STATE, FBC_REND_NUKE);
199 intel_de_posting_read(dev_priv, MSG_FBC_REND_STATE);
200}
201
202static void ilk_fbc_activate(struct drm_i915_private *dev_priv)
203{
204 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
205 u32 dpfc_ctl;
206 int threshold = dev_priv->fbc.threshold;
207
208 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.i9xx_plane);
209 if (params->fb.format->cpp[0] == 2)
210 threshold++;
211
212 switch (threshold) {
213 case 4:
214 case 3:
215 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
216 break;
217 case 2:
218 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
219 break;
220 case 1:
221 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
222 break;
223 }
224
225 if (params->fence_id >= 0) {
226 dpfc_ctl |= DPFC_CTL_FENCE_EN;
227 if (IS_GEN(dev_priv, 5))
228 dpfc_ctl |= params->fence_id;
229 if (IS_GEN(dev_priv, 6)) {
230 intel_de_write(dev_priv, SNB_DPFC_CTL_SA,
231 SNB_CPU_FENCE_ENABLE | params->fence_id);
232 intel_de_write(dev_priv, DPFC_CPU_FENCE_OFFSET,
233 params->fence_y_offset);
234 }
235 } else {
236 if (IS_GEN(dev_priv, 6)) {
237 intel_de_write(dev_priv, SNB_DPFC_CTL_SA, 0);
238 intel_de_write(dev_priv, DPFC_CPU_FENCE_OFFSET, 0);
239 }
240 }
241
242 intel_de_write(dev_priv, ILK_DPFC_FENCE_YOFF,
243 params->fence_y_offset);
244 /* enable it... */
245 intel_de_write(dev_priv, ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
246
247 intel_fbc_recompress(dev_priv);
248}
249
250static void ilk_fbc_deactivate(struct drm_i915_private *dev_priv)
251{
252 u32 dpfc_ctl;
253
254 /* Disable compression */
255 dpfc_ctl = intel_de_read(dev_priv, ILK_DPFC_CONTROL);
256 if (dpfc_ctl & DPFC_CTL_EN) {
257 dpfc_ctl &= ~DPFC_CTL_EN;
258 intel_de_write(dev_priv, ILK_DPFC_CONTROL, dpfc_ctl);
259 }
260}
261
262static bool ilk_fbc_is_active(struct drm_i915_private *dev_priv)
263{
264 return intel_de_read(dev_priv, ILK_DPFC_CONTROL) & DPFC_CTL_EN;
265}
266
267static void gen7_fbc_activate(struct drm_i915_private *dev_priv)
268{
269 struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
270 u32 dpfc_ctl;
271 int threshold = dev_priv->fbc.threshold;
272
273 /* Display WA #0529: skl, kbl, bxt. */
274 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
275 u32 val = intel_de_read(dev_priv, CHICKEN_MISC_4);
276
277 val &= ~(FBC_STRIDE_OVERRIDE | FBC_STRIDE_MASK);
278
279 if (params->gen9_wa_cfb_stride)
280 val |= FBC_STRIDE_OVERRIDE | params->gen9_wa_cfb_stride;
281
282 intel_de_write(dev_priv, CHICKEN_MISC_4, val);
283 }
284
285 dpfc_ctl = 0;
286 if (IS_IVYBRIDGE(dev_priv))
287 dpfc_ctl |= IVB_DPFC_CTL_PLANE(params->crtc.i9xx_plane);
288
289 if (params->fb.format->cpp[0] == 2)
290 threshold++;
291
292 switch (threshold) {
293 case 4:
294 case 3:
295 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
296 break;
297 case 2:
298 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
299 break;
300 case 1:
301 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
302 break;
303 }
304
305 if (params->fence_id >= 0) {
306 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
307 intel_de_write(dev_priv, SNB_DPFC_CTL_SA,
308 SNB_CPU_FENCE_ENABLE | params->fence_id);
309 intel_de_write(dev_priv, DPFC_CPU_FENCE_OFFSET,
310 params->fence_y_offset);
311 } else if (dev_priv->ggtt.num_fences) {
312 intel_de_write(dev_priv, SNB_DPFC_CTL_SA, 0);
313 intel_de_write(dev_priv, DPFC_CPU_FENCE_OFFSET, 0);
314 }
315
316 if (dev_priv->fbc.false_color)
317 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
318
319 if (IS_IVYBRIDGE(dev_priv)) {
320 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
321 intel_de_write(dev_priv, ILK_DISPLAY_CHICKEN1,
322 intel_de_read(dev_priv, ILK_DISPLAY_CHICKEN1) | ILK_FBCQ_DIS);
323 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
324 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
325 intel_de_write(dev_priv, CHICKEN_PIPESL_1(params->crtc.pipe),
326 intel_de_read(dev_priv, CHICKEN_PIPESL_1(params->crtc.pipe)) | HSW_FBCQ_DIS);
327 }
328
329 if (INTEL_GEN(dev_priv) >= 11)
330 /* Wa_1409120013:icl,ehl,tgl */
331 intel_de_write(dev_priv, ILK_DPFC_CHICKEN,
332 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
333
334 intel_de_write(dev_priv, ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
335
336 intel_fbc_recompress(dev_priv);
337}
338
339static bool intel_fbc_hw_is_active(struct drm_i915_private *dev_priv)
340{
341 if (INTEL_GEN(dev_priv) >= 5)
342 return ilk_fbc_is_active(dev_priv);
343 else if (IS_GM45(dev_priv))
344 return g4x_fbc_is_active(dev_priv);
345 else
346 return i8xx_fbc_is_active(dev_priv);
347}
348
349static void intel_fbc_hw_activate(struct drm_i915_private *dev_priv)
350{
351 struct intel_fbc *fbc = &dev_priv->fbc;
352
353 trace_intel_fbc_activate(fbc->crtc);
354
355 fbc->active = true;
356 fbc->activated = true;
357
358 if (INTEL_GEN(dev_priv) >= 7)
359 gen7_fbc_activate(dev_priv);
360 else if (INTEL_GEN(dev_priv) >= 5)
361 ilk_fbc_activate(dev_priv);
362 else if (IS_GM45(dev_priv))
363 g4x_fbc_activate(dev_priv);
364 else
365 i8xx_fbc_activate(dev_priv);
366}
367
368static void intel_fbc_hw_deactivate(struct drm_i915_private *dev_priv)
369{
370 struct intel_fbc *fbc = &dev_priv->fbc;
371
372 trace_intel_fbc_deactivate(fbc->crtc);
373
374 fbc->active = false;
375
376 if (INTEL_GEN(dev_priv) >= 5)
377 ilk_fbc_deactivate(dev_priv);
378 else if (IS_GM45(dev_priv))
379 g4x_fbc_deactivate(dev_priv);
380 else
381 i8xx_fbc_deactivate(dev_priv);
382}
383
384/**
385 * intel_fbc_is_active - Is FBC active?
386 * @dev_priv: i915 device instance
387 *
388 * This function is used to verify the current state of FBC.
389 *
390 * FIXME: This should be tracked in the plane config eventually
391 * instead of queried at runtime for most callers.
392 */
393bool intel_fbc_is_active(struct drm_i915_private *dev_priv)
394{
395 return dev_priv->fbc.active;
396}
397
398static void intel_fbc_deactivate(struct drm_i915_private *dev_priv,
399 const char *reason)
400{
401 struct intel_fbc *fbc = &dev_priv->fbc;
402
403 drm_WARN_ON(&dev_priv->drm, !mutex_is_locked(&fbc->lock));
404
405 if (fbc->active)
406 intel_fbc_hw_deactivate(dev_priv);
407
408 fbc->no_fbc_reason = reason;
409}
410
411static int find_compression_threshold(struct drm_i915_private *dev_priv,
412 struct drm_mm_node *node,
413 unsigned int size,
414 unsigned int fb_cpp)
415{
416 int compression_threshold = 1;
417 int ret;
418 u64 end;
419
420 /* The FBC hardware for BDW/SKL doesn't have access to the stolen
421 * reserved range size, so it always assumes the maximum (8mb) is used.
422 * If we enable FBC using a CFB on that memory range we'll get FIFO
423 * underruns, even if that range is not reserved by the BIOS. */
424 if (IS_BROADWELL(dev_priv) || IS_GEN9_BC(dev_priv))
425 end = resource_size(&dev_priv->dsm) - 8 * 1024 * 1024;
426 else
427 end = U64_MAX;
428
429 /* HACK: This code depends on what we will do in *_enable_fbc. If that
430 * code changes, this code needs to change as well.
431 *
432 * The enable_fbc code will attempt to use one of our 2 compression
433 * thresholds, therefore, in that case, we only have 1 resort.
434 */
435
436 /* Try to over-allocate to reduce reallocations and fragmentation. */
437 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
438 4096, 0, end);
439 if (ret == 0)
440 return compression_threshold;
441
442again:
443 /* HW's ability to limit the CFB is 1:4 */
444 if (compression_threshold > 4 ||
445 (fb_cpp == 2 && compression_threshold == 2))
446 return 0;
447
448 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
449 4096, 0, end);
450 if (ret && INTEL_GEN(dev_priv) <= 4) {
451 return 0;
452 } else if (ret) {
453 compression_threshold <<= 1;
454 goto again;
455 } else {
456 return compression_threshold;
457 }
458}
459
460static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv,
461 unsigned int size, unsigned int fb_cpp)
462{
463 struct intel_fbc *fbc = &dev_priv->fbc;
464 struct drm_mm_node *uninitialized_var(compressed_llb);
465 int ret;
466
467 drm_WARN_ON(&dev_priv->drm,
468 drm_mm_node_allocated(&fbc->compressed_fb));
469
470 ret = find_compression_threshold(dev_priv, &fbc->compressed_fb,
471 size, fb_cpp);
472 if (!ret)
473 goto err_llb;
474 else if (ret > 1) {
475 drm_info_once(&dev_priv->drm,
476 "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");
477 }
478
479 fbc->threshold = ret;
480
481 if (INTEL_GEN(dev_priv) >= 5)
482 intel_de_write(dev_priv, ILK_DPFC_CB_BASE,
483 fbc->compressed_fb.start);
484 else if (IS_GM45(dev_priv)) {
485 intel_de_write(dev_priv, DPFC_CB_BASE,
486 fbc->compressed_fb.start);
487 } else {
488 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
489 if (!compressed_llb)
490 goto err_fb;
491
492 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
493 4096, 4096);
494 if (ret)
495 goto err_fb;
496
497 fbc->compressed_llb = compressed_llb;
498
499 GEM_BUG_ON(range_overflows_end_t(u64, dev_priv->dsm.start,
500 fbc->compressed_fb.start,
501 U32_MAX));
502 GEM_BUG_ON(range_overflows_end_t(u64, dev_priv->dsm.start,
503 fbc->compressed_llb->start,
504 U32_MAX));
505 intel_de_write(dev_priv, FBC_CFB_BASE,
506 dev_priv->dsm.start + fbc->compressed_fb.start);
507 intel_de_write(dev_priv, FBC_LL_BASE,
508 dev_priv->dsm.start + compressed_llb->start);
509 }
510
511 drm_dbg_kms(&dev_priv->drm,
512 "reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
513 fbc->compressed_fb.size, fbc->threshold);
514
515 return 0;
516
517err_fb:
518 kfree(compressed_llb);
519 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
520err_llb:
521 if (drm_mm_initialized(&dev_priv->mm.stolen))
522 drm_info_once(&dev_priv->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);
523 return -ENOSPC;
524}
525
526static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
527{
528 struct intel_fbc *fbc = &dev_priv->fbc;
529
530 if (WARN_ON(intel_fbc_hw_is_active(dev_priv)))
531 return;
532
533 if (!drm_mm_node_allocated(&fbc->compressed_fb))
534 return;
535
536 if (fbc->compressed_llb) {
537 i915_gem_stolen_remove_node(dev_priv, fbc->compressed_llb);
538 kfree(fbc->compressed_llb);
539 }
540
541 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
542}
543
544void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
545{
546 struct intel_fbc *fbc = &dev_priv->fbc;
547
548 if (!HAS_FBC(dev_priv))
549 return;
550
551 mutex_lock(&fbc->lock);
552 __intel_fbc_cleanup_cfb(dev_priv);
553 mutex_unlock(&fbc->lock);
554}
555
556static bool stride_is_valid(struct drm_i915_private *dev_priv,
557 u64 modifier, unsigned int stride)
558{
559 /* This should have been caught earlier. */
560 if (drm_WARN_ON_ONCE(&dev_priv->drm, (stride & (64 - 1)) != 0))
561 return false;
562
563 /* Below are the additional FBC restrictions. */
564 if (stride < 512)
565 return false;
566
567 if (IS_GEN(dev_priv, 2) || IS_GEN(dev_priv, 3))
568 return stride == 4096 || stride == 8192;
569
570 if (IS_GEN(dev_priv, 4) && !IS_G4X(dev_priv) && stride < 2048)
571 return false;
572
573 /* Display WA #1105: skl,bxt,kbl,cfl,glk */
574 if (IS_GEN(dev_priv, 9) &&
575 modifier == DRM_FORMAT_MOD_LINEAR && stride & 511)
576 return false;
577
578 if (stride > 16384)
579 return false;
580
581 return true;
582}
583
584static bool pixel_format_is_valid(struct drm_i915_private *dev_priv,
585 u32 pixel_format)
586{
587 switch (pixel_format) {
588 case DRM_FORMAT_XRGB8888:
589 case DRM_FORMAT_XBGR8888:
590 return true;
591 case DRM_FORMAT_XRGB1555:
592 case DRM_FORMAT_RGB565:
593 /* 16bpp not supported on gen2 */
594 if (IS_GEN(dev_priv, 2))
595 return false;
596 /* WaFbcOnly1to1Ratio:ctg */
597 if (IS_G4X(dev_priv))
598 return false;
599 return true;
600 default:
601 return false;
602 }
603}
604
605static bool rotation_is_valid(struct drm_i915_private *dev_priv,
606 u32 pixel_format, unsigned int rotation)
607{
608 if (INTEL_GEN(dev_priv) >= 9 && pixel_format == DRM_FORMAT_RGB565 &&
609 drm_rotation_90_or_270(rotation))
610 return false;
611 else if (INTEL_GEN(dev_priv) <= 4 && !IS_G4X(dev_priv) &&
612 rotation != DRM_MODE_ROTATE_0)
613 return false;
614
615 return true;
616}
617
618/*
619 * For some reason, the hardware tracking starts looking at whatever we
620 * programmed as the display plane base address register. It does not look at
621 * the X and Y offset registers. That's why we include the src x/y offsets
622 * instead of just looking at the plane size.
623 */
624static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
625{
626 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
627 struct intel_fbc *fbc = &dev_priv->fbc;
628 unsigned int effective_w, effective_h, max_w, max_h;
629
630 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
631 max_w = 5120;
632 max_h = 4096;
633 } else if (INTEL_GEN(dev_priv) >= 8 || IS_HASWELL(dev_priv)) {
634 max_w = 4096;
635 max_h = 4096;
636 } else if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
637 max_w = 4096;
638 max_h = 2048;
639 } else {
640 max_w = 2048;
641 max_h = 1536;
642 }
643
644 intel_fbc_get_plane_source_size(&fbc->state_cache, &effective_w,
645 &effective_h);
646 effective_w += fbc->state_cache.plane.adjusted_x;
647 effective_h += fbc->state_cache.plane.adjusted_y;
648
649 return effective_w <= max_w && effective_h <= max_h;
650}
651
652static bool tiling_is_valid(struct drm_i915_private *dev_priv,
653 uint64_t modifier)
654{
655 switch (modifier) {
656 case DRM_FORMAT_MOD_LINEAR:
657 if (INTEL_GEN(dev_priv) >= 9)
658 return true;
659 return false;
660 case I915_FORMAT_MOD_X_TILED:
661 case I915_FORMAT_MOD_Y_TILED:
662 return true;
663 default:
664 return false;
665 }
666}
667
668static void intel_fbc_update_state_cache(struct intel_crtc *crtc,
669 const struct intel_crtc_state *crtc_state,
670 const struct intel_plane_state *plane_state)
671{
672 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
673 struct intel_fbc *fbc = &dev_priv->fbc;
674 struct intel_fbc_state_cache *cache = &fbc->state_cache;
675 struct drm_framebuffer *fb = plane_state->hw.fb;
676
677 cache->plane.visible = plane_state->uapi.visible;
678 if (!cache->plane.visible)
679 return;
680
681 cache->crtc.mode_flags = crtc_state->hw.adjusted_mode.flags;
682 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
683 cache->crtc.hsw_bdw_pixel_rate = crtc_state->pixel_rate;
684
685 cache->plane.rotation = plane_state->hw.rotation;
686 /*
687 * Src coordinates are already rotated by 270 degrees for
688 * the 90/270 degree plane rotation cases (to match the
689 * GTT mapping), hence no need to account for rotation here.
690 */
691 cache->plane.src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
692 cache->plane.src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
693 cache->plane.adjusted_x = plane_state->color_plane[0].x;
694 cache->plane.adjusted_y = plane_state->color_plane[0].y;
695
696 cache->plane.pixel_blend_mode = plane_state->hw.pixel_blend_mode;
697
698 cache->fb.format = fb->format;
699 cache->fb.stride = fb->pitches[0];
700 cache->fb.modifier = fb->modifier;
701
702 cache->fence_y_offset = intel_plane_fence_y_offset(plane_state);
703
704 drm_WARN_ON(&dev_priv->drm, plane_state->flags & PLANE_HAS_FENCE &&
705 !plane_state->vma->fence);
706
707 if (plane_state->flags & PLANE_HAS_FENCE &&
708 plane_state->vma->fence)
709 cache->fence_id = plane_state->vma->fence->id;
710 else
711 cache->fence_id = -1;
712}
713
714static bool intel_fbc_cfb_size_changed(struct drm_i915_private *dev_priv)
715{
716 struct intel_fbc *fbc = &dev_priv->fbc;
717
718 return intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache) >
719 fbc->compressed_fb.size * fbc->threshold;
720}
721
722static u16 intel_fbc_gen9_wa_cfb_stride(struct drm_i915_private *dev_priv)
723{
724 struct intel_fbc *fbc = &dev_priv->fbc;
725 struct intel_fbc_state_cache *cache = &fbc->state_cache;
726
727 if ((IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) &&
728 cache->fb.modifier != I915_FORMAT_MOD_X_TILED)
729 return DIV_ROUND_UP(cache->plane.src_w, 32 * fbc->threshold) * 8;
730 else
731 return 0;
732}
733
734static bool intel_fbc_gen9_wa_cfb_stride_changed(struct drm_i915_private *dev_priv)
735{
736 struct intel_fbc *fbc = &dev_priv->fbc;
737
738 return fbc->params.gen9_wa_cfb_stride != intel_fbc_gen9_wa_cfb_stride(dev_priv);
739}
740
741static bool intel_fbc_can_enable(struct drm_i915_private *dev_priv)
742{
743 struct intel_fbc *fbc = &dev_priv->fbc;
744
745 if (intel_vgpu_active(dev_priv)) {
746 fbc->no_fbc_reason = "VGPU is active";
747 return false;
748 }
749
750 if (!i915_modparams.enable_fbc) {
751 fbc->no_fbc_reason = "disabled per module param or by default";
752 return false;
753 }
754
755 if (fbc->underrun_detected) {
756 fbc->no_fbc_reason = "underrun detected";
757 return false;
758 }
759
760 return true;
761}
762
763static bool intel_fbc_can_activate(struct intel_crtc *crtc)
764{
765 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
766 struct intel_fbc *fbc = &dev_priv->fbc;
767 struct intel_fbc_state_cache *cache = &fbc->state_cache;
768
769 if (!intel_fbc_can_enable(dev_priv))
770 return false;
771
772 if (!cache->plane.visible) {
773 fbc->no_fbc_reason = "primary plane not visible";
774 return false;
775 }
776
777 /* We don't need to use a state cache here since this information is
778 * global for all CRTC.
779 */
780 if (fbc->underrun_detected) {
781 fbc->no_fbc_reason = "underrun detected";
782 return false;
783 }
784
785 if (cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) {
786 fbc->no_fbc_reason = "incompatible mode";
787 return false;
788 }
789
790 if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
791 fbc->no_fbc_reason = "mode too large for compression";
792 return false;
793 }
794
795 /* The use of a CPU fence is one of two ways to detect writes by the
796 * CPU to the scanout and trigger updates to the FBC.
797 *
798 * The other method is by software tracking (see
799 * intel_fbc_invalidate/flush()), it will manually notify FBC and nuke
800 * the current compressed buffer and recompress it.
801 *
802 * Note that is possible for a tiled surface to be unmappable (and
803 * so have no fence associated with it) due to aperture constraints
804 * at the time of pinning.
805 *
806 * FIXME with 90/270 degree rotation we should use the fence on
807 * the normal GTT view (the rotated view doesn't even have a
808 * fence). Would need changes to the FBC fence Y offset as well.
809 * For now this will effectively disable FBC with 90/270 degree
810 * rotation.
811 */
812 if (INTEL_GEN(dev_priv) < 9 && cache->fence_id < 0) {
813 fbc->no_fbc_reason = "framebuffer not tiled or fenced";
814 return false;
815 }
816
817 if (!rotation_is_valid(dev_priv, cache->fb.format->format,
818 cache->plane.rotation)) {
819 fbc->no_fbc_reason = "rotation unsupported";
820 return false;
821 }
822
823 if (!tiling_is_valid(dev_priv, cache->fb.modifier)) {
824 fbc->no_fbc_reason = "tiling unsupported";
825 return false;
826 }
827
828 if (!stride_is_valid(dev_priv, cache->fb.modifier, cache->fb.stride)) {
829 fbc->no_fbc_reason = "framebuffer stride not supported";
830 return false;
831 }
832
833 if (!pixel_format_is_valid(dev_priv, cache->fb.format->format)) {
834 fbc->no_fbc_reason = "pixel format is invalid";
835 return false;
836 }
837
838 if (cache->plane.pixel_blend_mode != DRM_MODE_BLEND_PIXEL_NONE &&
839 cache->fb.format->has_alpha) {
840 fbc->no_fbc_reason = "per-pixel alpha blending is incompatible with FBC";
841 return false;
842 }
843
844 /* WaFbcExceedCdClockThreshold:hsw,bdw */
845 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
846 cache->crtc.hsw_bdw_pixel_rate >= dev_priv->cdclk.hw.cdclk * 95 / 100) {
847 fbc->no_fbc_reason = "pixel rate is too big";
848 return false;
849 }
850
851 /* It is possible for the required CFB size change without a
852 * crtc->disable + crtc->enable since it is possible to change the
853 * stride without triggering a full modeset. Since we try to
854 * over-allocate the CFB, there's a chance we may keep FBC enabled even
855 * if this happens, but if we exceed the current CFB size we'll have to
856 * disable FBC. Notice that it would be possible to disable FBC, wait
857 * for a frame, free the stolen node, then try to reenable FBC in case
858 * we didn't get any invalidate/deactivate calls, but this would require
859 * a lot of tracking just for a specific case. If we conclude it's an
860 * important case, we can implement it later. */
861 if (intel_fbc_cfb_size_changed(dev_priv)) {
862 fbc->no_fbc_reason = "CFB requirements changed";
863 return false;
864 }
865
866 /*
867 * Work around a problem on GEN9+ HW, where enabling FBC on a plane
868 * having a Y offset that isn't divisible by 4 causes FIFO underrun
869 * and screen flicker.
870 */
871 if (INTEL_GEN(dev_priv) >= 9 &&
872 (fbc->state_cache.plane.adjusted_y & 3)) {
873 fbc->no_fbc_reason = "plane Y offset is misaligned";
874 return false;
875 }
876
877 return true;
878}
879
880static void intel_fbc_get_reg_params(struct intel_crtc *crtc,
881 struct intel_fbc_reg_params *params)
882{
883 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
884 struct intel_fbc *fbc = &dev_priv->fbc;
885 struct intel_fbc_state_cache *cache = &fbc->state_cache;
886
887 /* Since all our fields are integer types, use memset here so the
888 * comparison function can rely on memcmp because the padding will be
889 * zero. */
890 memset(params, 0, sizeof(*params));
891
892 params->fence_id = cache->fence_id;
893 params->fence_y_offset = cache->fence_y_offset;
894
895 params->crtc.pipe = crtc->pipe;
896 params->crtc.i9xx_plane = to_intel_plane(crtc->base.primary)->i9xx_plane;
897
898 params->fb.format = cache->fb.format;
899 params->fb.modifier = cache->fb.modifier;
900 params->fb.stride = cache->fb.stride;
901
902 params->cfb_size = intel_fbc_calculate_cfb_size(dev_priv, cache);
903
904 params->gen9_wa_cfb_stride = cache->gen9_wa_cfb_stride;
905
906 params->plane_visible = cache->plane.visible;
907}
908
909static bool intel_fbc_can_flip_nuke(const struct intel_crtc_state *crtc_state)
910{
911 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
912 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
913 const struct intel_fbc *fbc = &dev_priv->fbc;
914 const struct intel_fbc_state_cache *cache = &fbc->state_cache;
915 const struct intel_fbc_reg_params *params = &fbc->params;
916
917 if (drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
918 return false;
919
920 if (!params->plane_visible)
921 return false;
922
923 if (!intel_fbc_can_activate(crtc))
924 return false;
925
926 if (params->fb.format != cache->fb.format)
927 return false;
928
929 if (params->fb.modifier != cache->fb.modifier)
930 return false;
931
932 if (params->fb.stride != cache->fb.stride)
933 return false;
934
935 if (params->cfb_size != intel_fbc_calculate_cfb_size(dev_priv, cache))
936 return false;
937
938 if (params->gen9_wa_cfb_stride != cache->gen9_wa_cfb_stride)
939 return false;
940
941 return true;
942}
943
944bool intel_fbc_pre_update(struct intel_atomic_state *state,
945 struct intel_crtc *crtc)
946{
947 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
948 const struct intel_crtc_state *crtc_state =
949 intel_atomic_get_new_crtc_state(state, crtc);
950 const struct intel_plane_state *plane_state =
951 intel_atomic_get_new_plane_state(state, plane);
952 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
953 struct intel_fbc *fbc = &dev_priv->fbc;
954 const char *reason = "update pending";
955 bool need_vblank_wait = false;
956
957 if (!plane->has_fbc || !plane_state)
958 return need_vblank_wait;
959
960 mutex_lock(&fbc->lock);
961
962 if (fbc->crtc != crtc)
963 goto unlock;
964
965 intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
966 fbc->flip_pending = true;
967
968 if (!intel_fbc_can_flip_nuke(crtc_state)) {
969 intel_fbc_deactivate(dev_priv, reason);
970
971 /*
972 * Display WA #1198: glk+
973 * Need an extra vblank wait between FBC disable and most plane
974 * updates. Bspec says this is only needed for plane disable, but
975 * that is not true. Touching most plane registers will cause the
976 * corruption to appear. Also SKL/derivatives do not seem to be
977 * affected.
978 *
979 * TODO: could optimize this a bit by sampling the frame
980 * counter when we disable FBC (if it was already done earlier)
981 * and skipping the extra vblank wait before the plane update
982 * if at least one frame has already passed.
983 */
984 if (fbc->activated &&
985 (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)))
986 need_vblank_wait = true;
987 fbc->activated = false;
988 }
989unlock:
990 mutex_unlock(&fbc->lock);
991
992 return need_vblank_wait;
993}
994
995/**
996 * __intel_fbc_disable - disable FBC
997 * @dev_priv: i915 device instance
998 *
999 * This is the low level function that actually disables FBC. Callers should
1000 * grab the FBC lock.
1001 */
1002static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
1003{
1004 struct intel_fbc *fbc = &dev_priv->fbc;
1005 struct intel_crtc *crtc = fbc->crtc;
1006
1007 drm_WARN_ON(&dev_priv->drm, !mutex_is_locked(&fbc->lock));
1008 drm_WARN_ON(&dev_priv->drm, !fbc->crtc);
1009 drm_WARN_ON(&dev_priv->drm, fbc->active);
1010
1011 drm_dbg_kms(&dev_priv->drm, "Disabling FBC on pipe %c\n",
1012 pipe_name(crtc->pipe));
1013
1014 __intel_fbc_cleanup_cfb(dev_priv);
1015
1016 fbc->crtc = NULL;
1017}
1018
1019static void __intel_fbc_post_update(struct intel_crtc *crtc)
1020{
1021 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1022 struct intel_fbc *fbc = &dev_priv->fbc;
1023
1024 drm_WARN_ON(&dev_priv->drm, !mutex_is_locked(&fbc->lock));
1025
1026 if (fbc->crtc != crtc)
1027 return;
1028
1029 fbc->flip_pending = false;
1030
1031 if (!i915_modparams.enable_fbc) {
1032 intel_fbc_deactivate(dev_priv, "disabled at runtime per module param");
1033 __intel_fbc_disable(dev_priv);
1034
1035 return;
1036 }
1037
1038 intel_fbc_get_reg_params(crtc, &fbc->params);
1039
1040 if (!intel_fbc_can_activate(crtc))
1041 return;
1042
1043 if (!fbc->busy_bits)
1044 intel_fbc_hw_activate(dev_priv);
1045 else
1046 intel_fbc_deactivate(dev_priv, "frontbuffer write");
1047}
1048
1049void intel_fbc_post_update(struct intel_atomic_state *state,
1050 struct intel_crtc *crtc)
1051{
1052 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1053 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1054 const struct intel_plane_state *plane_state =
1055 intel_atomic_get_new_plane_state(state, plane);
1056 struct intel_fbc *fbc = &dev_priv->fbc;
1057
1058 if (!plane->has_fbc || !plane_state)
1059 return;
1060
1061 mutex_lock(&fbc->lock);
1062 __intel_fbc_post_update(crtc);
1063 mutex_unlock(&fbc->lock);
1064}
1065
1066static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
1067{
1068 if (fbc->crtc)
1069 return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit;
1070 else
1071 return fbc->possible_framebuffer_bits;
1072}
1073
1074void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
1075 unsigned int frontbuffer_bits,
1076 enum fb_op_origin origin)
1077{
1078 struct intel_fbc *fbc = &dev_priv->fbc;
1079
1080 if (!HAS_FBC(dev_priv))
1081 return;
1082
1083 if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
1084 return;
1085
1086 mutex_lock(&fbc->lock);
1087
1088 fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits;
1089
1090 if (fbc->crtc && fbc->busy_bits)
1091 intel_fbc_deactivate(dev_priv, "frontbuffer write");
1092
1093 mutex_unlock(&fbc->lock);
1094}
1095
1096void intel_fbc_flush(struct drm_i915_private *dev_priv,
1097 unsigned int frontbuffer_bits, enum fb_op_origin origin)
1098{
1099 struct intel_fbc *fbc = &dev_priv->fbc;
1100
1101 if (!HAS_FBC(dev_priv))
1102 return;
1103
1104 mutex_lock(&fbc->lock);
1105
1106 fbc->busy_bits &= ~frontbuffer_bits;
1107
1108 if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
1109 goto out;
1110
1111 if (!fbc->busy_bits && fbc->crtc &&
1112 (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) {
1113 if (fbc->active)
1114 intel_fbc_recompress(dev_priv);
1115 else if (!fbc->flip_pending)
1116 __intel_fbc_post_update(fbc->crtc);
1117 }
1118
1119out:
1120 mutex_unlock(&fbc->lock);
1121}
1122
1123/**
1124 * intel_fbc_choose_crtc - select a CRTC to enable FBC on
1125 * @dev_priv: i915 device instance
1126 * @state: the atomic state structure
1127 *
1128 * This function looks at the proposed state for CRTCs and planes, then chooses
1129 * which pipe is going to have FBC by setting intel_crtc_state->enable_fbc to
1130 * true.
1131 *
1132 * Later, intel_fbc_enable is going to look for state->enable_fbc and then maybe
1133 * enable FBC for the chosen CRTC. If it does, it will set dev_priv->fbc.crtc.
1134 */
1135void intel_fbc_choose_crtc(struct drm_i915_private *dev_priv,
1136 struct intel_atomic_state *state)
1137{
1138 struct intel_fbc *fbc = &dev_priv->fbc;
1139 struct intel_plane *plane;
1140 struct intel_plane_state *plane_state;
1141 bool crtc_chosen = false;
1142 int i;
1143
1144 mutex_lock(&fbc->lock);
1145
1146 /* Does this atomic commit involve the CRTC currently tied to FBC? */
1147 if (fbc->crtc &&
1148 !intel_atomic_get_new_crtc_state(state, fbc->crtc))
1149 goto out;
1150
1151 if (!intel_fbc_can_enable(dev_priv))
1152 goto out;
1153
1154 /* Simply choose the first CRTC that is compatible and has a visible
1155 * plane. We could go for fancier schemes such as checking the plane
1156 * size, but this would just affect the few platforms that don't tie FBC
1157 * to pipe or plane A. */
1158 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1159 struct intel_crtc_state *crtc_state;
1160 struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
1161
1162 if (!plane->has_fbc)
1163 continue;
1164
1165 if (!plane_state->uapi.visible)
1166 continue;
1167
1168 crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
1169
1170 crtc_state->enable_fbc = true;
1171 crtc_chosen = true;
1172 break;
1173 }
1174
1175 if (!crtc_chosen)
1176 fbc->no_fbc_reason = "no suitable CRTC for FBC";
1177
1178out:
1179 mutex_unlock(&fbc->lock);
1180}
1181
1182/**
1183 * intel_fbc_enable: tries to enable FBC on the CRTC
1184 * @crtc: the CRTC
1185 * @state: corresponding &drm_crtc_state for @crtc
1186 *
1187 * This function checks if the given CRTC was chosen for FBC, then enables it if
1188 * possible. Notice that it doesn't activate FBC. It is valid to call
1189 * intel_fbc_enable multiple times for the same pipe without an
1190 * intel_fbc_disable in the middle, as long as it is deactivated.
1191 */
1192void intel_fbc_enable(struct intel_atomic_state *state,
1193 struct intel_crtc *crtc)
1194{
1195 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1196 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1197 const struct intel_crtc_state *crtc_state =
1198 intel_atomic_get_new_crtc_state(state, crtc);
1199 const struct intel_plane_state *plane_state =
1200 intel_atomic_get_new_plane_state(state, plane);
1201 struct intel_fbc *fbc = &dev_priv->fbc;
1202 struct intel_fbc_state_cache *cache = &fbc->state_cache;
1203
1204 if (!plane->has_fbc || !plane_state)
1205 return;
1206
1207 mutex_lock(&fbc->lock);
1208
1209 if (fbc->crtc) {
1210 if (fbc->crtc != crtc ||
1211 (!intel_fbc_cfb_size_changed(dev_priv) &&
1212 !intel_fbc_gen9_wa_cfb_stride_changed(dev_priv)))
1213 goto out;
1214
1215 __intel_fbc_disable(dev_priv);
1216 }
1217
1218 drm_WARN_ON(&dev_priv->drm, fbc->active);
1219
1220 intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
1221
1222 /* FIXME crtc_state->enable_fbc lies :( */
1223 if (!cache->plane.visible)
1224 goto out;
1225
1226 if (intel_fbc_alloc_cfb(dev_priv,
1227 intel_fbc_calculate_cfb_size(dev_priv, cache),
1228 plane_state->hw.fb->format->cpp[0])) {
1229 cache->plane.visible = false;
1230 fbc->no_fbc_reason = "not enough stolen memory";
1231 goto out;
1232 }
1233
1234 cache->gen9_wa_cfb_stride = intel_fbc_gen9_wa_cfb_stride(dev_priv);
1235
1236 drm_dbg_kms(&dev_priv->drm, "Enabling FBC on pipe %c\n",
1237 pipe_name(crtc->pipe));
1238 fbc->no_fbc_reason = "FBC enabled but not active yet\n";
1239
1240 fbc->crtc = crtc;
1241out:
1242 mutex_unlock(&fbc->lock);
1243}
1244
1245/**
1246 * intel_fbc_disable - disable FBC if it's associated with crtc
1247 * @crtc: the CRTC
1248 *
1249 * This function disables FBC if it's associated with the provided CRTC.
1250 */
1251void intel_fbc_disable(struct intel_crtc *crtc)
1252{
1253 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1254 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1255 struct intel_fbc *fbc = &dev_priv->fbc;
1256
1257 if (!plane->has_fbc)
1258 return;
1259
1260 mutex_lock(&fbc->lock);
1261 if (fbc->crtc == crtc)
1262 __intel_fbc_disable(dev_priv);
1263 mutex_unlock(&fbc->lock);
1264}
1265
1266/**
1267 * intel_fbc_global_disable - globally disable FBC
1268 * @dev_priv: i915 device instance
1269 *
1270 * This function disables FBC regardless of which CRTC is associated with it.
1271 */
1272void intel_fbc_global_disable(struct drm_i915_private *dev_priv)
1273{
1274 struct intel_fbc *fbc = &dev_priv->fbc;
1275
1276 if (!HAS_FBC(dev_priv))
1277 return;
1278
1279 mutex_lock(&fbc->lock);
1280 if (fbc->crtc) {
1281 drm_WARN_ON(&dev_priv->drm, fbc->crtc->active);
1282 __intel_fbc_disable(dev_priv);
1283 }
1284 mutex_unlock(&fbc->lock);
1285}
1286
1287static void intel_fbc_underrun_work_fn(struct work_struct *work)
1288{
1289 struct drm_i915_private *dev_priv =
1290 container_of(work, struct drm_i915_private, fbc.underrun_work);
1291 struct intel_fbc *fbc = &dev_priv->fbc;
1292
1293 mutex_lock(&fbc->lock);
1294
1295 /* Maybe we were scheduled twice. */
1296 if (fbc->underrun_detected || !fbc->crtc)
1297 goto out;
1298
1299 drm_dbg_kms(&dev_priv->drm, "Disabling FBC due to FIFO underrun.\n");
1300 fbc->underrun_detected = true;
1301
1302 intel_fbc_deactivate(dev_priv, "FIFO underrun");
1303out:
1304 mutex_unlock(&fbc->lock);
1305}
1306
1307/*
1308 * intel_fbc_reset_underrun - reset FBC fifo underrun status.
1309 * @dev_priv: i915 device instance
1310 *
1311 * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we
1312 * want to re-enable FBC after an underrun to increase test coverage.
1313 */
1314int intel_fbc_reset_underrun(struct drm_i915_private *dev_priv)
1315{
1316 int ret;
1317
1318 cancel_work_sync(&dev_priv->fbc.underrun_work);
1319
1320 ret = mutex_lock_interruptible(&dev_priv->fbc.lock);
1321 if (ret)
1322 return ret;
1323
1324 if (dev_priv->fbc.underrun_detected) {
1325 drm_dbg_kms(&dev_priv->drm,
1326 "Re-allowing FBC after fifo underrun\n");
1327 dev_priv->fbc.no_fbc_reason = "FIFO underrun cleared";
1328 }
1329
1330 dev_priv->fbc.underrun_detected = false;
1331 mutex_unlock(&dev_priv->fbc.lock);
1332
1333 return 0;
1334}
1335
1336/**
1337 * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
1338 * @dev_priv: i915 device instance
1339 *
1340 * Without FBC, most underruns are harmless and don't really cause too many
1341 * problems, except for an annoying message on dmesg. With FBC, underruns can
1342 * become black screens or even worse, especially when paired with bad
1343 * watermarks. So in order for us to be on the safe side, completely disable FBC
1344 * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
1345 * already suggests that watermarks may be bad, so try to be as safe as
1346 * possible.
1347 *
1348 * This function is called from the IRQ handler.
1349 */
1350void intel_fbc_handle_fifo_underrun_irq(struct drm_i915_private *dev_priv)
1351{
1352 struct intel_fbc *fbc = &dev_priv->fbc;
1353
1354 if (!HAS_FBC(dev_priv))
1355 return;
1356
1357 /* There's no guarantee that underrun_detected won't be set to true
1358 * right after this check and before the work is scheduled, but that's
1359 * not a problem since we'll check it again under the work function
1360 * while FBC is locked. This check here is just to prevent us from
1361 * unnecessarily scheduling the work, and it relies on the fact that we
1362 * never switch underrun_detect back to false after it's true. */
1363 if (READ_ONCE(fbc->underrun_detected))
1364 return;
1365
1366 schedule_work(&fbc->underrun_work);
1367}
1368
1369/*
1370 * The DDX driver changes its behavior depending on the value it reads from
1371 * i915.enable_fbc, so sanitize it by translating the default value into either
1372 * 0 or 1 in order to allow it to know what's going on.
1373 *
1374 * Notice that this is done at driver initialization and we still allow user
1375 * space to change the value during runtime without sanitizing it again. IGT
1376 * relies on being able to change i915.enable_fbc at runtime.
1377 */
1378static int intel_sanitize_fbc_option(struct drm_i915_private *dev_priv)
1379{
1380 if (i915_modparams.enable_fbc >= 0)
1381 return !!i915_modparams.enable_fbc;
1382
1383 if (!HAS_FBC(dev_priv))
1384 return 0;
1385
1386 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9)
1387 return 1;
1388
1389 return 0;
1390}
1391
1392static bool need_fbc_vtd_wa(struct drm_i915_private *dev_priv)
1393{
1394 /* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
1395 if (intel_vtd_active() &&
1396 (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))) {
1397 drm_info(&dev_priv->drm,
1398 "Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
1399 return true;
1400 }
1401
1402 return false;
1403}
1404
1405/**
1406 * intel_fbc_init - Initialize FBC
1407 * @dev_priv: the i915 device
1408 *
1409 * This function might be called during PM init process.
1410 */
1411void intel_fbc_init(struct drm_i915_private *dev_priv)
1412{
1413 struct intel_fbc *fbc = &dev_priv->fbc;
1414
1415 INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
1416 mutex_init(&fbc->lock);
1417 fbc->active = false;
1418
1419 if (!drm_mm_initialized(&dev_priv->mm.stolen))
1420 mkwrite_device_info(dev_priv)->display.has_fbc = false;
1421
1422 if (need_fbc_vtd_wa(dev_priv))
1423 mkwrite_device_info(dev_priv)->display.has_fbc = false;
1424
1425 i915_modparams.enable_fbc = intel_sanitize_fbc_option(dev_priv);
1426 drm_dbg_kms(&dev_priv->drm, "Sanitized enable_fbc value: %d\n",
1427 i915_modparams.enable_fbc);
1428
1429 if (!HAS_FBC(dev_priv)) {
1430 fbc->no_fbc_reason = "unsupported by this chipset";
1431 return;
1432 }
1433
1434 /* This value was pulled out of someone's hat */
1435 if (INTEL_GEN(dev_priv) <= 4 && !IS_GM45(dev_priv))
1436 intel_de_write(dev_priv, FBC_CONTROL,
1437 500 << FBC_CTL_INTERVAL_SHIFT);
1438
1439 /* We still don't have any sort of hardware state readout for FBC, so
1440 * deactivate it in case the BIOS activated it to make sure software
1441 * matches the hardware state. */
1442 if (intel_fbc_hw_is_active(dev_priv))
1443 intel_fbc_hw_deactivate(dev_priv);
1444}