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