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  */
55 static 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 
64 static 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 
79 static 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 
99 static 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 
146 static 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 
151 static 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 
174 static 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 
186 static 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. */
192 static 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 
202 static 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 
250 static 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 
262 static 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 
267 static 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 
339 static 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 
349 static 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 
368 static 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  */
393 bool intel_fbc_is_active(struct drm_i915_private *dev_priv)
394 {
395 	return dev_priv->fbc.active;
396 }
397 
398 static 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 
411 static 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 
442 again:
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 
460 static 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 
517 err_fb:
518 	kfree(compressed_llb);
519 	i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
520 err_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 
526 static 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 
544 void 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 
556 static 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 
584 static 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 
605 static 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  */
624 static 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 
652 static 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 
668 static 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 
714 static 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 
722 static bool intel_fbc_can_enable(struct drm_i915_private *dev_priv)
723 {
724 	struct intel_fbc *fbc = &dev_priv->fbc;
725 
726 	if (intel_vgpu_active(dev_priv)) {
727 		fbc->no_fbc_reason = "VGPU is active";
728 		return false;
729 	}
730 
731 	if (!i915_modparams.enable_fbc) {
732 		fbc->no_fbc_reason = "disabled per module param or by default";
733 		return false;
734 	}
735 
736 	if (fbc->underrun_detected) {
737 		fbc->no_fbc_reason = "underrun detected";
738 		return false;
739 	}
740 
741 	return true;
742 }
743 
744 static bool intel_fbc_can_activate(struct intel_crtc *crtc)
745 {
746 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
747 	struct intel_fbc *fbc = &dev_priv->fbc;
748 	struct intel_fbc_state_cache *cache = &fbc->state_cache;
749 
750 	if (!intel_fbc_can_enable(dev_priv))
751 		return false;
752 
753 	if (!cache->plane.visible) {
754 		fbc->no_fbc_reason = "primary plane not visible";
755 		return false;
756 	}
757 
758 	/* We don't need to use a state cache here since this information is
759 	 * global for all CRTC.
760 	 */
761 	if (fbc->underrun_detected) {
762 		fbc->no_fbc_reason = "underrun detected";
763 		return false;
764 	}
765 
766 	if (cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) {
767 		fbc->no_fbc_reason = "incompatible mode";
768 		return false;
769 	}
770 
771 	if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
772 		fbc->no_fbc_reason = "mode too large for compression";
773 		return false;
774 	}
775 
776 	/* The use of a CPU fence is one of two ways to detect writes by the
777 	 * CPU to the scanout and trigger updates to the FBC.
778 	 *
779 	 * The other method is by software tracking (see
780 	 * intel_fbc_invalidate/flush()), it will manually notify FBC and nuke
781 	 * the current compressed buffer and recompress it.
782 	 *
783 	 * Note that is possible for a tiled surface to be unmappable (and
784 	 * so have no fence associated with it) due to aperture constraints
785 	 * at the time of pinning.
786 	 *
787 	 * FIXME with 90/270 degree rotation we should use the fence on
788 	 * the normal GTT view (the rotated view doesn't even have a
789 	 * fence). Would need changes to the FBC fence Y offset as well.
790 	 * For now this will effectively disable FBC with 90/270 degree
791 	 * rotation.
792 	 */
793 	if (INTEL_GEN(dev_priv) < 9 && cache->fence_id < 0) {
794 		fbc->no_fbc_reason = "framebuffer not tiled or fenced";
795 		return false;
796 	}
797 
798 	if (!rotation_is_valid(dev_priv, cache->fb.format->format,
799 			       cache->plane.rotation)) {
800 		fbc->no_fbc_reason = "rotation unsupported";
801 		return false;
802 	}
803 
804 	if (!tiling_is_valid(dev_priv, cache->fb.modifier)) {
805 		fbc->no_fbc_reason = "tiling unsupported";
806 		return false;
807 	}
808 
809 	if (!stride_is_valid(dev_priv, cache->fb.modifier, cache->fb.stride)) {
810 		fbc->no_fbc_reason = "framebuffer stride not supported";
811 		return false;
812 	}
813 
814 	if (!pixel_format_is_valid(dev_priv, cache->fb.format->format)) {
815 		fbc->no_fbc_reason = "pixel format is invalid";
816 		return false;
817 	}
818 
819 	if (cache->plane.pixel_blend_mode != DRM_MODE_BLEND_PIXEL_NONE &&
820 	    cache->fb.format->has_alpha) {
821 		fbc->no_fbc_reason = "per-pixel alpha blending is incompatible with FBC";
822 		return false;
823 	}
824 
825 	/* WaFbcExceedCdClockThreshold:hsw,bdw */
826 	if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
827 	    cache->crtc.hsw_bdw_pixel_rate >= dev_priv->cdclk.hw.cdclk * 95 / 100) {
828 		fbc->no_fbc_reason = "pixel rate is too big";
829 		return false;
830 	}
831 
832 	/* It is possible for the required CFB size change without a
833 	 * crtc->disable + crtc->enable since it is possible to change the
834 	 * stride without triggering a full modeset. Since we try to
835 	 * over-allocate the CFB, there's a chance we may keep FBC enabled even
836 	 * if this happens, but if we exceed the current CFB size we'll have to
837 	 * disable FBC. Notice that it would be possible to disable FBC, wait
838 	 * for a frame, free the stolen node, then try to reenable FBC in case
839 	 * we didn't get any invalidate/deactivate calls, but this would require
840 	 * a lot of tracking just for a specific case. If we conclude it's an
841 	 * important case, we can implement it later. */
842 	if (intel_fbc_cfb_size_changed(dev_priv)) {
843 		fbc->no_fbc_reason = "CFB requirements changed";
844 		return false;
845 	}
846 
847 	/*
848 	 * Work around a problem on GEN9+ HW, where enabling FBC on a plane
849 	 * having a Y offset that isn't divisible by 4 causes FIFO underrun
850 	 * and screen flicker.
851 	 */
852 	if (INTEL_GEN(dev_priv) >= 9 &&
853 	    (fbc->state_cache.plane.adjusted_y & 3)) {
854 		fbc->no_fbc_reason = "plane Y offset is misaligned";
855 		return false;
856 	}
857 
858 	return true;
859 }
860 
861 static void intel_fbc_get_reg_params(struct intel_crtc *crtc,
862 				     struct intel_fbc_reg_params *params)
863 {
864 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
865 	struct intel_fbc *fbc = &dev_priv->fbc;
866 	struct intel_fbc_state_cache *cache = &fbc->state_cache;
867 
868 	/* Since all our fields are integer types, use memset here so the
869 	 * comparison function can rely on memcmp because the padding will be
870 	 * zero. */
871 	memset(params, 0, sizeof(*params));
872 
873 	params->fence_id = cache->fence_id;
874 	params->fence_y_offset = cache->fence_y_offset;
875 
876 	params->crtc.pipe = crtc->pipe;
877 	params->crtc.i9xx_plane = to_intel_plane(crtc->base.primary)->i9xx_plane;
878 
879 	params->fb.format = cache->fb.format;
880 	params->fb.stride = cache->fb.stride;
881 
882 	params->cfb_size = intel_fbc_calculate_cfb_size(dev_priv, cache);
883 
884 	params->gen9_wa_cfb_stride = cache->gen9_wa_cfb_stride;
885 
886 	params->plane_visible = cache->plane.visible;
887 }
888 
889 static bool intel_fbc_can_flip_nuke(const struct intel_crtc_state *crtc_state)
890 {
891 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
892 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
893 	const struct intel_fbc *fbc = &dev_priv->fbc;
894 	const struct intel_fbc_state_cache *cache = &fbc->state_cache;
895 	const struct intel_fbc_reg_params *params = &fbc->params;
896 
897 	if (drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
898 		return false;
899 
900 	if (!params->plane_visible)
901 		return false;
902 
903 	if (!intel_fbc_can_activate(crtc))
904 		return false;
905 
906 	if (params->fb.format != cache->fb.format)
907 		return false;
908 
909 	if (params->fb.stride != cache->fb.stride)
910 		return false;
911 
912 	if (params->cfb_size != intel_fbc_calculate_cfb_size(dev_priv, cache))
913 		return false;
914 
915 	if (params->gen9_wa_cfb_stride != cache->gen9_wa_cfb_stride)
916 		return false;
917 
918 	return true;
919 }
920 
921 bool intel_fbc_pre_update(struct intel_atomic_state *state,
922 			  struct intel_crtc *crtc)
923 {
924 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
925 	const struct intel_crtc_state *crtc_state =
926 		intel_atomic_get_new_crtc_state(state, crtc);
927 	const struct intel_plane_state *plane_state =
928 		intel_atomic_get_new_plane_state(state, plane);
929 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
930 	struct intel_fbc *fbc = &dev_priv->fbc;
931 	const char *reason = "update pending";
932 	bool need_vblank_wait = false;
933 
934 	if (!plane->has_fbc || !plane_state)
935 		return need_vblank_wait;
936 
937 	mutex_lock(&fbc->lock);
938 
939 	if (fbc->crtc != crtc)
940 		goto unlock;
941 
942 	intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
943 	fbc->flip_pending = true;
944 
945 	if (!intel_fbc_can_flip_nuke(crtc_state)) {
946 		intel_fbc_deactivate(dev_priv, reason);
947 
948 		/*
949 		 * Display WA #1198: glk+
950 		 * Need an extra vblank wait between FBC disable and most plane
951 		 * updates. Bspec says this is only needed for plane disable, but
952 		 * that is not true. Touching most plane registers will cause the
953 		 * corruption to appear. Also SKL/derivatives do not seem to be
954 		 * affected.
955 		 *
956 		 * TODO: could optimize this a bit by sampling the frame
957 		 * counter when we disable FBC (if it was already done earlier)
958 		 * and skipping the extra vblank wait before the plane update
959 		 * if at least one frame has already passed.
960 		 */
961 		if (fbc->activated &&
962 		    (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)))
963 			need_vblank_wait = true;
964 		fbc->activated = false;
965 	}
966 unlock:
967 	mutex_unlock(&fbc->lock);
968 
969 	return need_vblank_wait;
970 }
971 
972 /**
973  * __intel_fbc_disable - disable FBC
974  * @dev_priv: i915 device instance
975  *
976  * This is the low level function that actually disables FBC. Callers should
977  * grab the FBC lock.
978  */
979 static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
980 {
981 	struct intel_fbc *fbc = &dev_priv->fbc;
982 	struct intel_crtc *crtc = fbc->crtc;
983 
984 	drm_WARN_ON(&dev_priv->drm, !mutex_is_locked(&fbc->lock));
985 	drm_WARN_ON(&dev_priv->drm, !fbc->crtc);
986 	drm_WARN_ON(&dev_priv->drm, fbc->active);
987 
988 	drm_dbg_kms(&dev_priv->drm, "Disabling FBC on pipe %c\n",
989 		    pipe_name(crtc->pipe));
990 
991 	__intel_fbc_cleanup_cfb(dev_priv);
992 
993 	fbc->crtc = NULL;
994 }
995 
996 static void __intel_fbc_post_update(struct intel_crtc *crtc)
997 {
998 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
999 	struct intel_fbc *fbc = &dev_priv->fbc;
1000 
1001 	drm_WARN_ON(&dev_priv->drm, !mutex_is_locked(&fbc->lock));
1002 
1003 	if (fbc->crtc != crtc)
1004 		return;
1005 
1006 	fbc->flip_pending = false;
1007 
1008 	if (!i915_modparams.enable_fbc) {
1009 		intel_fbc_deactivate(dev_priv, "disabled at runtime per module param");
1010 		__intel_fbc_disable(dev_priv);
1011 
1012 		return;
1013 	}
1014 
1015 	intel_fbc_get_reg_params(crtc, &fbc->params);
1016 
1017 	if (!intel_fbc_can_activate(crtc))
1018 		return;
1019 
1020 	if (!fbc->busy_bits)
1021 		intel_fbc_hw_activate(dev_priv);
1022 	else
1023 		intel_fbc_deactivate(dev_priv, "frontbuffer write");
1024 }
1025 
1026 void intel_fbc_post_update(struct intel_atomic_state *state,
1027 			   struct intel_crtc *crtc)
1028 {
1029 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1030 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1031 	const struct intel_plane_state *plane_state =
1032 		intel_atomic_get_new_plane_state(state, plane);
1033 	struct intel_fbc *fbc = &dev_priv->fbc;
1034 
1035 	if (!plane->has_fbc || !plane_state)
1036 		return;
1037 
1038 	mutex_lock(&fbc->lock);
1039 	__intel_fbc_post_update(crtc);
1040 	mutex_unlock(&fbc->lock);
1041 }
1042 
1043 static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
1044 {
1045 	if (fbc->crtc)
1046 		return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit;
1047 	else
1048 		return fbc->possible_framebuffer_bits;
1049 }
1050 
1051 void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
1052 			  unsigned int frontbuffer_bits,
1053 			  enum fb_op_origin origin)
1054 {
1055 	struct intel_fbc *fbc = &dev_priv->fbc;
1056 
1057 	if (!HAS_FBC(dev_priv))
1058 		return;
1059 
1060 	if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
1061 		return;
1062 
1063 	mutex_lock(&fbc->lock);
1064 
1065 	fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits;
1066 
1067 	if (fbc->crtc && fbc->busy_bits)
1068 		intel_fbc_deactivate(dev_priv, "frontbuffer write");
1069 
1070 	mutex_unlock(&fbc->lock);
1071 }
1072 
1073 void intel_fbc_flush(struct drm_i915_private *dev_priv,
1074 		     unsigned int frontbuffer_bits, enum fb_op_origin origin)
1075 {
1076 	struct intel_fbc *fbc = &dev_priv->fbc;
1077 
1078 	if (!HAS_FBC(dev_priv))
1079 		return;
1080 
1081 	mutex_lock(&fbc->lock);
1082 
1083 	fbc->busy_bits &= ~frontbuffer_bits;
1084 
1085 	if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
1086 		goto out;
1087 
1088 	if (!fbc->busy_bits && fbc->crtc &&
1089 	    (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) {
1090 		if (fbc->active)
1091 			intel_fbc_recompress(dev_priv);
1092 		else if (!fbc->flip_pending)
1093 			__intel_fbc_post_update(fbc->crtc);
1094 	}
1095 
1096 out:
1097 	mutex_unlock(&fbc->lock);
1098 }
1099 
1100 /**
1101  * intel_fbc_choose_crtc - select a CRTC to enable FBC on
1102  * @dev_priv: i915 device instance
1103  * @state: the atomic state structure
1104  *
1105  * This function looks at the proposed state for CRTCs and planes, then chooses
1106  * which pipe is going to have FBC by setting intel_crtc_state->enable_fbc to
1107  * true.
1108  *
1109  * Later, intel_fbc_enable is going to look for state->enable_fbc and then maybe
1110  * enable FBC for the chosen CRTC. If it does, it will set dev_priv->fbc.crtc.
1111  */
1112 void intel_fbc_choose_crtc(struct drm_i915_private *dev_priv,
1113 			   struct intel_atomic_state *state)
1114 {
1115 	struct intel_fbc *fbc = &dev_priv->fbc;
1116 	struct intel_plane *plane;
1117 	struct intel_plane_state *plane_state;
1118 	bool crtc_chosen = false;
1119 	int i;
1120 
1121 	mutex_lock(&fbc->lock);
1122 
1123 	/* Does this atomic commit involve the CRTC currently tied to FBC? */
1124 	if (fbc->crtc &&
1125 	    !intel_atomic_get_new_crtc_state(state, fbc->crtc))
1126 		goto out;
1127 
1128 	if (!intel_fbc_can_enable(dev_priv))
1129 		goto out;
1130 
1131 	/* Simply choose the first CRTC that is compatible and has a visible
1132 	 * plane. We could go for fancier schemes such as checking the plane
1133 	 * size, but this would just affect the few platforms that don't tie FBC
1134 	 * to pipe or plane A. */
1135 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1136 		struct intel_crtc_state *crtc_state;
1137 		struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
1138 
1139 		if (!plane->has_fbc)
1140 			continue;
1141 
1142 		if (!plane_state->uapi.visible)
1143 			continue;
1144 
1145 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
1146 
1147 		crtc_state->enable_fbc = true;
1148 		crtc_chosen = true;
1149 		break;
1150 	}
1151 
1152 	if (!crtc_chosen)
1153 		fbc->no_fbc_reason = "no suitable CRTC for FBC";
1154 
1155 out:
1156 	mutex_unlock(&fbc->lock);
1157 }
1158 
1159 /**
1160  * intel_fbc_enable: tries to enable FBC on the CRTC
1161  * @crtc: the CRTC
1162  * @state: corresponding &drm_crtc_state for @crtc
1163  *
1164  * This function checks if the given CRTC was chosen for FBC, then enables it if
1165  * possible. Notice that it doesn't activate FBC. It is valid to call
1166  * intel_fbc_enable multiple times for the same pipe without an
1167  * intel_fbc_disable in the middle, as long as it is deactivated.
1168  */
1169 void intel_fbc_enable(struct intel_atomic_state *state,
1170 		      struct intel_crtc *crtc)
1171 {
1172 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1173 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1174 	const struct intel_crtc_state *crtc_state =
1175 		intel_atomic_get_new_crtc_state(state, crtc);
1176 	const struct intel_plane_state *plane_state =
1177 		intel_atomic_get_new_plane_state(state, plane);
1178 	struct intel_fbc *fbc = &dev_priv->fbc;
1179 	struct intel_fbc_state_cache *cache = &fbc->state_cache;
1180 
1181 	if (!plane->has_fbc || !plane_state)
1182 		return;
1183 
1184 	mutex_lock(&fbc->lock);
1185 
1186 	if (fbc->crtc) {
1187 		if (fbc->crtc != crtc ||
1188 		    !intel_fbc_cfb_size_changed(dev_priv))
1189 			goto out;
1190 
1191 		__intel_fbc_disable(dev_priv);
1192 	}
1193 
1194 	drm_WARN_ON(&dev_priv->drm, fbc->active);
1195 
1196 	intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
1197 
1198 	/* FIXME crtc_state->enable_fbc lies :( */
1199 	if (!cache->plane.visible)
1200 		goto out;
1201 
1202 	if (intel_fbc_alloc_cfb(dev_priv,
1203 				intel_fbc_calculate_cfb_size(dev_priv, cache),
1204 				plane_state->hw.fb->format->cpp[0])) {
1205 		cache->plane.visible = false;
1206 		fbc->no_fbc_reason = "not enough stolen memory";
1207 		goto out;
1208 	}
1209 
1210 	if ((IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) &&
1211 	    plane_state->hw.fb->modifier != I915_FORMAT_MOD_X_TILED)
1212 		cache->gen9_wa_cfb_stride =
1213 			DIV_ROUND_UP(cache->plane.src_w, 32 * fbc->threshold) * 8;
1214 	else
1215 		cache->gen9_wa_cfb_stride = 0;
1216 
1217 	drm_dbg_kms(&dev_priv->drm, "Enabling FBC on pipe %c\n",
1218 		    pipe_name(crtc->pipe));
1219 	fbc->no_fbc_reason = "FBC enabled but not active yet\n";
1220 
1221 	fbc->crtc = crtc;
1222 out:
1223 	mutex_unlock(&fbc->lock);
1224 }
1225 
1226 /**
1227  * intel_fbc_disable - disable FBC if it's associated with crtc
1228  * @crtc: the CRTC
1229  *
1230  * This function disables FBC if it's associated with the provided CRTC.
1231  */
1232 void intel_fbc_disable(struct intel_crtc *crtc)
1233 {
1234 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1235 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1236 	struct intel_fbc *fbc = &dev_priv->fbc;
1237 
1238 	if (!plane->has_fbc)
1239 		return;
1240 
1241 	mutex_lock(&fbc->lock);
1242 	if (fbc->crtc == crtc)
1243 		__intel_fbc_disable(dev_priv);
1244 	mutex_unlock(&fbc->lock);
1245 }
1246 
1247 /**
1248  * intel_fbc_global_disable - globally disable FBC
1249  * @dev_priv: i915 device instance
1250  *
1251  * This function disables FBC regardless of which CRTC is associated with it.
1252  */
1253 void intel_fbc_global_disable(struct drm_i915_private *dev_priv)
1254 {
1255 	struct intel_fbc *fbc = &dev_priv->fbc;
1256 
1257 	if (!HAS_FBC(dev_priv))
1258 		return;
1259 
1260 	mutex_lock(&fbc->lock);
1261 	if (fbc->crtc) {
1262 		drm_WARN_ON(&dev_priv->drm, fbc->crtc->active);
1263 		__intel_fbc_disable(dev_priv);
1264 	}
1265 	mutex_unlock(&fbc->lock);
1266 }
1267 
1268 static void intel_fbc_underrun_work_fn(struct work_struct *work)
1269 {
1270 	struct drm_i915_private *dev_priv =
1271 		container_of(work, struct drm_i915_private, fbc.underrun_work);
1272 	struct intel_fbc *fbc = &dev_priv->fbc;
1273 
1274 	mutex_lock(&fbc->lock);
1275 
1276 	/* Maybe we were scheduled twice. */
1277 	if (fbc->underrun_detected || !fbc->crtc)
1278 		goto out;
1279 
1280 	drm_dbg_kms(&dev_priv->drm, "Disabling FBC due to FIFO underrun.\n");
1281 	fbc->underrun_detected = true;
1282 
1283 	intel_fbc_deactivate(dev_priv, "FIFO underrun");
1284 out:
1285 	mutex_unlock(&fbc->lock);
1286 }
1287 
1288 /*
1289  * intel_fbc_reset_underrun - reset FBC fifo underrun status.
1290  * @dev_priv: i915 device instance
1291  *
1292  * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we
1293  * want to re-enable FBC after an underrun to increase test coverage.
1294  */
1295 int intel_fbc_reset_underrun(struct drm_i915_private *dev_priv)
1296 {
1297 	int ret;
1298 
1299 	cancel_work_sync(&dev_priv->fbc.underrun_work);
1300 
1301 	ret = mutex_lock_interruptible(&dev_priv->fbc.lock);
1302 	if (ret)
1303 		return ret;
1304 
1305 	if (dev_priv->fbc.underrun_detected) {
1306 		drm_dbg_kms(&dev_priv->drm,
1307 			    "Re-allowing FBC after fifo underrun\n");
1308 		dev_priv->fbc.no_fbc_reason = "FIFO underrun cleared";
1309 	}
1310 
1311 	dev_priv->fbc.underrun_detected = false;
1312 	mutex_unlock(&dev_priv->fbc.lock);
1313 
1314 	return 0;
1315 }
1316 
1317 /**
1318  * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
1319  * @dev_priv: i915 device instance
1320  *
1321  * Without FBC, most underruns are harmless and don't really cause too many
1322  * problems, except for an annoying message on dmesg. With FBC, underruns can
1323  * become black screens or even worse, especially when paired with bad
1324  * watermarks. So in order for us to be on the safe side, completely disable FBC
1325  * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
1326  * already suggests that watermarks may be bad, so try to be as safe as
1327  * possible.
1328  *
1329  * This function is called from the IRQ handler.
1330  */
1331 void intel_fbc_handle_fifo_underrun_irq(struct drm_i915_private *dev_priv)
1332 {
1333 	struct intel_fbc *fbc = &dev_priv->fbc;
1334 
1335 	if (!HAS_FBC(dev_priv))
1336 		return;
1337 
1338 	/* There's no guarantee that underrun_detected won't be set to true
1339 	 * right after this check and before the work is scheduled, but that's
1340 	 * not a problem since we'll check it again under the work function
1341 	 * while FBC is locked. This check here is just to prevent us from
1342 	 * unnecessarily scheduling the work, and it relies on the fact that we
1343 	 * never switch underrun_detect back to false after it's true. */
1344 	if (READ_ONCE(fbc->underrun_detected))
1345 		return;
1346 
1347 	schedule_work(&fbc->underrun_work);
1348 }
1349 
1350 /*
1351  * The DDX driver changes its behavior depending on the value it reads from
1352  * i915.enable_fbc, so sanitize it by translating the default value into either
1353  * 0 or 1 in order to allow it to know what's going on.
1354  *
1355  * Notice that this is done at driver initialization and we still allow user
1356  * space to change the value during runtime without sanitizing it again. IGT
1357  * relies on being able to change i915.enable_fbc at runtime.
1358  */
1359 static int intel_sanitize_fbc_option(struct drm_i915_private *dev_priv)
1360 {
1361 	if (i915_modparams.enable_fbc >= 0)
1362 		return !!i915_modparams.enable_fbc;
1363 
1364 	if (!HAS_FBC(dev_priv))
1365 		return 0;
1366 
1367 	if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9)
1368 		return 1;
1369 
1370 	return 0;
1371 }
1372 
1373 static bool need_fbc_vtd_wa(struct drm_i915_private *dev_priv)
1374 {
1375 	/* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
1376 	if (intel_vtd_active() &&
1377 	    (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))) {
1378 		drm_info(&dev_priv->drm,
1379 			 "Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
1380 		return true;
1381 	}
1382 
1383 	return false;
1384 }
1385 
1386 /**
1387  * intel_fbc_init - Initialize FBC
1388  * @dev_priv: the i915 device
1389  *
1390  * This function might be called during PM init process.
1391  */
1392 void intel_fbc_init(struct drm_i915_private *dev_priv)
1393 {
1394 	struct intel_fbc *fbc = &dev_priv->fbc;
1395 
1396 	INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
1397 	mutex_init(&fbc->lock);
1398 	fbc->active = false;
1399 
1400 	if (!drm_mm_initialized(&dev_priv->mm.stolen))
1401 		mkwrite_device_info(dev_priv)->display.has_fbc = false;
1402 
1403 	if (need_fbc_vtd_wa(dev_priv))
1404 		mkwrite_device_info(dev_priv)->display.has_fbc = false;
1405 
1406 	i915_modparams.enable_fbc = intel_sanitize_fbc_option(dev_priv);
1407 	drm_dbg_kms(&dev_priv->drm, "Sanitized enable_fbc value: %d\n",
1408 		    i915_modparams.enable_fbc);
1409 
1410 	if (!HAS_FBC(dev_priv)) {
1411 		fbc->no_fbc_reason = "unsupported by this chipset";
1412 		return;
1413 	}
1414 
1415 	/* This value was pulled out of someone's hat */
1416 	if (INTEL_GEN(dev_priv) <= 4 && !IS_GM45(dev_priv))
1417 		intel_de_write(dev_priv, FBC_CONTROL,
1418 		               500 << FBC_CTL_INTERVAL_SHIFT);
1419 
1420 	/* We still don't have any sort of hardware state readout for FBC, so
1421 	 * deactivate it in case the BIOS activated it to make sure software
1422 	 * matches the hardware state. */
1423 	if (intel_fbc_hw_is_active(dev_priv))
1424 		intel_fbc_hw_deactivate(dev_priv);
1425 }
1426