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