xref: /linux/drivers/gpu/drm/i915/display/intel_fbc.c (revision fcab107abe1ab5be9dbe874baa722372da8f4f73)
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 <linux/debugfs.h>
42 #include <linux/string_helpers.h>
43 
44 #include <drm/drm_blend.h>
45 #include <drm/drm_fourcc.h>
46 
47 #include "gem/i915_gem_stolen.h"
48 #include "gt/intel_gt_types.h"
49 #include "i915_drv.h"
50 #include "i915_reg.h"
51 #include "i915_utils.h"
52 #include "i915_vgpu.h"
53 #include "i915_vma.h"
54 #include "i9xx_plane_regs.h"
55 #include "intel_cdclk.h"
56 #include "intel_de.h"
57 #include "intel_display_device.h"
58 #include "intel_display_rpm.h"
59 #include "intel_display_trace.h"
60 #include "intel_display_types.h"
61 #include "intel_display_wa.h"
62 #include "intel_fbc.h"
63 #include "intel_fbc_regs.h"
64 #include "intel_frontbuffer.h"
65 
66 #define for_each_fbc_id(__display, __fbc_id) \
67 	for ((__fbc_id) = INTEL_FBC_A; (__fbc_id) < I915_MAX_FBCS; (__fbc_id)++) \
68 		for_each_if(DISPLAY_RUNTIME_INFO(__display)->fbc_mask & BIT(__fbc_id))
69 
70 #define for_each_intel_fbc(__display, __fbc, __fbc_id) \
71 	for_each_fbc_id((__display), (__fbc_id)) \
72 		for_each_if((__fbc) = (__display)->fbc[(__fbc_id)])
73 
74 struct intel_fbc_funcs {
75 	void (*activate)(struct intel_fbc *fbc);
76 	void (*deactivate)(struct intel_fbc *fbc);
77 	bool (*is_active)(struct intel_fbc *fbc);
78 	bool (*is_compressing)(struct intel_fbc *fbc);
79 	void (*nuke)(struct intel_fbc *fbc);
80 	void (*program_cfb)(struct intel_fbc *fbc);
81 	void (*set_false_color)(struct intel_fbc *fbc, bool enable);
82 };
83 
84 struct intel_fbc_state {
85 	struct intel_plane *plane;
86 	unsigned int cfb_stride;
87 	unsigned int cfb_size;
88 	unsigned int fence_y_offset;
89 	u16 override_cfb_stride;
90 	u16 interval;
91 	s8 fence_id;
92 	struct drm_rect dirty_rect;
93 };
94 
95 struct intel_fbc {
96 	struct intel_display *display;
97 	const struct intel_fbc_funcs *funcs;
98 
99 	/*
100 	 * This is always the inner lock when overlapping with
101 	 * struct_mutex and it's the outer lock when overlapping
102 	 * with stolen_lock.
103 	 */
104 	struct mutex lock;
105 	unsigned int busy_bits;
106 
107 	struct i915_stolen_fb compressed_fb, compressed_llb;
108 
109 	enum intel_fbc_id id;
110 
111 	u8 limit;
112 
113 	bool false_color;
114 
115 	bool active;
116 	bool activated;
117 	bool flip_pending;
118 
119 	bool underrun_detected;
120 	struct work_struct underrun_work;
121 
122 	/*
123 	 * This structure contains everything that's relevant to program the
124 	 * hardware registers. When we want to figure out if we need to disable
125 	 * and re-enable FBC for a new configuration we just check if there's
126 	 * something different in the struct. The genx_fbc_activate functions
127 	 * are supposed to read from it in order to program the registers.
128 	 */
129 	struct intel_fbc_state state;
130 	const char *no_fbc_reason;
131 };
132 
133 /* plane stride in pixels */
134 static unsigned int intel_fbc_plane_stride(const struct intel_plane_state *plane_state)
135 {
136 	const struct drm_framebuffer *fb = plane_state->hw.fb;
137 	unsigned int stride;
138 
139 	stride = plane_state->view.color_plane[0].mapping_stride;
140 	if (!drm_rotation_90_or_270(plane_state->hw.rotation))
141 		stride /= fb->format->cpp[0];
142 
143 	return stride;
144 }
145 
146 static unsigned int intel_fbc_cfb_cpp(void)
147 {
148 	return 4; /* FBC always 4 bytes per pixel */
149 }
150 
151 /* plane stride based cfb stride in bytes, assuming 1:1 compression limit */
152 static unsigned int intel_fbc_plane_cfb_stride(const struct intel_plane_state *plane_state)
153 {
154 	unsigned int cpp = intel_fbc_cfb_cpp();
155 
156 	return intel_fbc_plane_stride(plane_state) * cpp;
157 }
158 
159 /* minimum acceptable cfb stride in bytes, assuming 1:1 compression limit */
160 static unsigned int skl_fbc_min_cfb_stride(struct intel_display *display,
161 					   unsigned int cpp, unsigned int width)
162 {
163 	unsigned int limit = 4; /* 1:4 compression limit is the worst case */
164 	unsigned int height = 4; /* FBC segment is 4 lines */
165 	unsigned int stride;
166 
167 	/* minimum segment stride we can use */
168 	stride = width * cpp * height / limit;
169 
170 	/*
171 	 * Wa_16011863758: icl+
172 	 * Avoid some hardware segment address miscalculation.
173 	 */
174 	if (DISPLAY_VER(display) >= 11)
175 		stride += 64;
176 
177 	/*
178 	 * At least some of the platforms require each 4 line segment to
179 	 * be 512 byte aligned. Just do it always for simplicity.
180 	 */
181 	stride = ALIGN(stride, 512);
182 
183 	/* convert back to single line equivalent with 1:1 compression limit */
184 	return stride * limit / height;
185 }
186 
187 /* properly aligned cfb stride in bytes, assuming 1:1 compression limit */
188 static unsigned int _intel_fbc_cfb_stride(struct intel_display *display,
189 					  unsigned int cpp, unsigned int width,
190 					  unsigned int stride)
191 {
192 	/*
193 	 * At least some of the platforms require each 4 line segment to
194 	 * be 512 byte aligned. Aligning each line to 512 bytes guarantees
195 	 * that regardless of the compression limit we choose later.
196 	 */
197 	if (DISPLAY_VER(display) >= 9)
198 		return max(ALIGN(stride, 512), skl_fbc_min_cfb_stride(display, cpp, width));
199 	else
200 		return stride;
201 }
202 
203 static unsigned int intel_fbc_cfb_stride(const struct intel_plane_state *plane_state)
204 {
205 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
206 	unsigned int stride = intel_fbc_plane_cfb_stride(plane_state);
207 	unsigned int width = drm_rect_width(&plane_state->uapi.src) >> 16;
208 	unsigned int cpp = intel_fbc_cfb_cpp();
209 
210 	return _intel_fbc_cfb_stride(display, cpp, width, stride);
211 }
212 
213 /*
214  * Maximum height the hardware will compress, on HSW+
215  * additional lines (up to the actual plane height) will
216  * remain uncompressed.
217  */
218 static unsigned int intel_fbc_max_cfb_height(struct intel_display *display)
219 {
220 	if (DISPLAY_VER(display) >= 8)
221 		return 2560;
222 	else if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
223 		return 2048;
224 	else
225 		return 1536;
226 }
227 
228 static unsigned int _intel_fbc_cfb_size(struct intel_display *display,
229 					unsigned int height, unsigned int stride)
230 {
231 	return min(height, intel_fbc_max_cfb_height(display)) * stride;
232 }
233 
234 static unsigned int intel_fbc_cfb_size(const struct intel_plane_state *plane_state)
235 {
236 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
237 	unsigned int height = drm_rect_height(&plane_state->uapi.src) >> 16;
238 
239 	return _intel_fbc_cfb_size(display, height, intel_fbc_cfb_stride(plane_state));
240 }
241 
242 static u16 intel_fbc_override_cfb_stride(const struct intel_plane_state *plane_state)
243 {
244 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
245 	unsigned int stride_aligned = intel_fbc_cfb_stride(plane_state);
246 	unsigned int stride = intel_fbc_plane_cfb_stride(plane_state);
247 	const struct drm_framebuffer *fb = plane_state->hw.fb;
248 
249 	/*
250 	 * Override stride in 64 byte units per 4 line segment.
251 	 *
252 	 * Gen9 hw miscalculates cfb stride for linear as
253 	 * PLANE_STRIDE*512 instead of PLANE_STRIDE*64, so
254 	 * we always need to use the override there.
255 	 *
256 	 * wa_14022269668 For bmg, always program the FBC_STRIDE before fbc enable
257 	 */
258 	if (stride != stride_aligned ||
259 	    (DISPLAY_VER(display) == 9 && fb->modifier == DRM_FORMAT_MOD_LINEAR) ||
260 	    display->platform.battlemage)
261 		return stride_aligned * 4 / 64;
262 
263 	return 0;
264 }
265 
266 static bool intel_fbc_has_fences(struct intel_display *display)
267 {
268 	struct drm_i915_private __maybe_unused *i915 = to_i915(display->drm);
269 
270 	return intel_gt_support_legacy_fencing(to_gt(i915));
271 }
272 
273 static u32 i8xx_fbc_ctl(struct intel_fbc *fbc)
274 {
275 	struct intel_display *display = fbc->display;
276 	const struct intel_fbc_state *fbc_state = &fbc->state;
277 	unsigned int cfb_stride;
278 	u32 fbc_ctl;
279 
280 	cfb_stride = fbc_state->cfb_stride / fbc->limit;
281 
282 	/* FBC_CTL wants 32B or 64B units */
283 	if (DISPLAY_VER(display) == 2)
284 		cfb_stride = (cfb_stride / 32) - 1;
285 	else
286 		cfb_stride = (cfb_stride / 64) - 1;
287 
288 	fbc_ctl = FBC_CTL_PERIODIC |
289 		FBC_CTL_INTERVAL(fbc_state->interval) |
290 		FBC_CTL_STRIDE(cfb_stride);
291 
292 	if (display->platform.i945gm)
293 		fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
294 
295 	if (fbc_state->fence_id >= 0)
296 		fbc_ctl |= FBC_CTL_FENCENO(fbc_state->fence_id);
297 
298 	return fbc_ctl;
299 }
300 
301 static u32 i965_fbc_ctl2(struct intel_fbc *fbc)
302 {
303 	const struct intel_fbc_state *fbc_state = &fbc->state;
304 	u32 fbc_ctl2;
305 
306 	fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM |
307 		FBC_CTL_PLANE(fbc_state->plane->i9xx_plane);
308 
309 	if (fbc_state->fence_id >= 0)
310 		fbc_ctl2 |= FBC_CTL_CPU_FENCE_EN;
311 
312 	return fbc_ctl2;
313 }
314 
315 static void i8xx_fbc_deactivate(struct intel_fbc *fbc)
316 {
317 	struct intel_display *display = fbc->display;
318 	u32 fbc_ctl;
319 
320 	/* Disable compression */
321 	fbc_ctl = intel_de_read(display, FBC_CONTROL);
322 	if ((fbc_ctl & FBC_CTL_EN) == 0)
323 		return;
324 
325 	fbc_ctl &= ~FBC_CTL_EN;
326 	intel_de_write(display, FBC_CONTROL, fbc_ctl);
327 
328 	/* Wait for compressing bit to clear */
329 	if (intel_de_wait_for_clear(display, FBC_STATUS,
330 				    FBC_STAT_COMPRESSING, 10)) {
331 		drm_dbg_kms(display->drm, "FBC idle timed out\n");
332 		return;
333 	}
334 }
335 
336 static void i8xx_fbc_activate(struct intel_fbc *fbc)
337 {
338 	struct intel_display *display = fbc->display;
339 	const struct intel_fbc_state *fbc_state = &fbc->state;
340 	int i;
341 
342 	/* Clear old tags */
343 	for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
344 		intel_de_write(display, FBC_TAG(i), 0);
345 
346 	if (DISPLAY_VER(display) == 4) {
347 		intel_de_write(display, FBC_CONTROL2,
348 			       i965_fbc_ctl2(fbc));
349 		intel_de_write(display, FBC_FENCE_OFF,
350 			       fbc_state->fence_y_offset);
351 	}
352 
353 	intel_de_write(display, FBC_CONTROL,
354 		       FBC_CTL_EN | i8xx_fbc_ctl(fbc));
355 }
356 
357 static bool i8xx_fbc_is_active(struct intel_fbc *fbc)
358 {
359 	return intel_de_read(fbc->display, FBC_CONTROL) & FBC_CTL_EN;
360 }
361 
362 static bool i8xx_fbc_is_compressing(struct intel_fbc *fbc)
363 {
364 	return intel_de_read(fbc->display, FBC_STATUS) &
365 		(FBC_STAT_COMPRESSING | FBC_STAT_COMPRESSED);
366 }
367 
368 static void i8xx_fbc_nuke(struct intel_fbc *fbc)
369 {
370 	struct intel_display *display = fbc->display;
371 	struct intel_fbc_state *fbc_state = &fbc->state;
372 	enum i9xx_plane_id i9xx_plane = fbc_state->plane->i9xx_plane;
373 
374 	intel_de_write_fw(display, DSPADDR(display, i9xx_plane),
375 			  intel_de_read_fw(display, DSPADDR(display, i9xx_plane)));
376 }
377 
378 static void i8xx_fbc_program_cfb(struct intel_fbc *fbc)
379 {
380 	struct intel_display *display = fbc->display;
381 	struct drm_i915_private *i915 = to_i915(display->drm);
382 
383 	drm_WARN_ON(display->drm,
384 		    range_overflows_end_t(u64, i915_gem_stolen_area_address(i915),
385 					  i915_gem_stolen_node_offset(&fbc->compressed_fb),
386 					  U32_MAX));
387 	drm_WARN_ON(display->drm,
388 		    range_overflows_end_t(u64, i915_gem_stolen_area_address(i915),
389 					  i915_gem_stolen_node_offset(&fbc->compressed_llb),
390 					  U32_MAX));
391 	intel_de_write(display, FBC_CFB_BASE,
392 		       i915_gem_stolen_node_address(i915, &fbc->compressed_fb));
393 	intel_de_write(display, FBC_LL_BASE,
394 		       i915_gem_stolen_node_address(i915, &fbc->compressed_llb));
395 }
396 
397 static const struct intel_fbc_funcs i8xx_fbc_funcs = {
398 	.activate = i8xx_fbc_activate,
399 	.deactivate = i8xx_fbc_deactivate,
400 	.is_active = i8xx_fbc_is_active,
401 	.is_compressing = i8xx_fbc_is_compressing,
402 	.nuke = i8xx_fbc_nuke,
403 	.program_cfb = i8xx_fbc_program_cfb,
404 };
405 
406 static void i965_fbc_nuke(struct intel_fbc *fbc)
407 {
408 	struct intel_display *display = fbc->display;
409 	struct intel_fbc_state *fbc_state = &fbc->state;
410 	enum i9xx_plane_id i9xx_plane = fbc_state->plane->i9xx_plane;
411 
412 	intel_de_write_fw(display, DSPSURF(display, i9xx_plane),
413 			  intel_de_read_fw(display, DSPSURF(display, i9xx_plane)));
414 }
415 
416 static const struct intel_fbc_funcs i965_fbc_funcs = {
417 	.activate = i8xx_fbc_activate,
418 	.deactivate = i8xx_fbc_deactivate,
419 	.is_active = i8xx_fbc_is_active,
420 	.is_compressing = i8xx_fbc_is_compressing,
421 	.nuke = i965_fbc_nuke,
422 	.program_cfb = i8xx_fbc_program_cfb,
423 };
424 
425 static u32 g4x_dpfc_ctl_limit(struct intel_fbc *fbc)
426 {
427 	switch (fbc->limit) {
428 	default:
429 		MISSING_CASE(fbc->limit);
430 		fallthrough;
431 	case 1:
432 		return DPFC_CTL_LIMIT_1X;
433 	case 2:
434 		return DPFC_CTL_LIMIT_2X;
435 	case 4:
436 		return DPFC_CTL_LIMIT_4X;
437 	}
438 }
439 
440 static u32 g4x_dpfc_ctl(struct intel_fbc *fbc)
441 {
442 	struct intel_display *display = fbc->display;
443 	const struct intel_fbc_state *fbc_state = &fbc->state;
444 	u32 dpfc_ctl;
445 
446 	dpfc_ctl = g4x_dpfc_ctl_limit(fbc) |
447 		DPFC_CTL_PLANE_G4X(fbc_state->plane->i9xx_plane);
448 
449 	if (display->platform.g4x)
450 		dpfc_ctl |= DPFC_CTL_SR_EN;
451 
452 	if (fbc_state->fence_id >= 0) {
453 		dpfc_ctl |= DPFC_CTL_FENCE_EN_G4X;
454 
455 		if (DISPLAY_VER(display) < 6)
456 			dpfc_ctl |= DPFC_CTL_FENCENO(fbc_state->fence_id);
457 	}
458 
459 	return dpfc_ctl;
460 }
461 
462 static void g4x_fbc_activate(struct intel_fbc *fbc)
463 {
464 	struct intel_display *display = fbc->display;
465 	const struct intel_fbc_state *fbc_state = &fbc->state;
466 
467 	intel_de_write(display, DPFC_FENCE_YOFF,
468 		       fbc_state->fence_y_offset);
469 
470 	intel_de_write(display, DPFC_CONTROL,
471 		       DPFC_CTL_EN | g4x_dpfc_ctl(fbc));
472 }
473 
474 static void g4x_fbc_deactivate(struct intel_fbc *fbc)
475 {
476 	struct intel_display *display = fbc->display;
477 	u32 dpfc_ctl;
478 
479 	/* Disable compression */
480 	dpfc_ctl = intel_de_read(display, DPFC_CONTROL);
481 	if (dpfc_ctl & DPFC_CTL_EN) {
482 		dpfc_ctl &= ~DPFC_CTL_EN;
483 		intel_de_write(display, DPFC_CONTROL, dpfc_ctl);
484 	}
485 }
486 
487 static bool g4x_fbc_is_active(struct intel_fbc *fbc)
488 {
489 	return intel_de_read(fbc->display, DPFC_CONTROL) & DPFC_CTL_EN;
490 }
491 
492 static bool g4x_fbc_is_compressing(struct intel_fbc *fbc)
493 {
494 	return intel_de_read(fbc->display, DPFC_STATUS) & DPFC_COMP_SEG_MASK;
495 }
496 
497 static void g4x_fbc_program_cfb(struct intel_fbc *fbc)
498 {
499 	struct intel_display *display = fbc->display;
500 
501 	intel_de_write(display, DPFC_CB_BASE,
502 		       i915_gem_stolen_node_offset(&fbc->compressed_fb));
503 }
504 
505 static const struct intel_fbc_funcs g4x_fbc_funcs = {
506 	.activate = g4x_fbc_activate,
507 	.deactivate = g4x_fbc_deactivate,
508 	.is_active = g4x_fbc_is_active,
509 	.is_compressing = g4x_fbc_is_compressing,
510 	.nuke = i965_fbc_nuke,
511 	.program_cfb = g4x_fbc_program_cfb,
512 };
513 
514 static void ilk_fbc_activate(struct intel_fbc *fbc)
515 {
516 	struct intel_display *display = fbc->display;
517 	struct intel_fbc_state *fbc_state = &fbc->state;
518 
519 	intel_de_write(display, ILK_DPFC_FENCE_YOFF(fbc->id),
520 		       fbc_state->fence_y_offset);
521 
522 	intel_de_write(display, ILK_DPFC_CONTROL(fbc->id),
523 		       DPFC_CTL_EN | g4x_dpfc_ctl(fbc));
524 }
525 
526 static void fbc_compressor_clkgate_disable_wa(struct intel_fbc *fbc,
527 					      bool disable)
528 {
529 	struct intel_display *display = fbc->display;
530 
531 	if (display->platform.dg2)
532 		intel_de_rmw(display, GEN9_CLKGATE_DIS_4, DG2_DPFC_GATING_DIS,
533 			     disable ? DG2_DPFC_GATING_DIS : 0);
534 	else if (DISPLAY_VER(display) >= 14)
535 		intel_de_rmw(display, MTL_PIPE_CLKGATE_DIS2(fbc->id),
536 			     MTL_DPFC_GATING_DIS,
537 			     disable ? MTL_DPFC_GATING_DIS : 0);
538 }
539 
540 static void ilk_fbc_deactivate(struct intel_fbc *fbc)
541 {
542 	struct intel_display *display = fbc->display;
543 	u32 dpfc_ctl;
544 
545 	if (HAS_FBC_DIRTY_RECT(display))
546 		intel_de_write(display, XE3_FBC_DIRTY_CTL(fbc->id), 0);
547 
548 	/* Disable compression */
549 	dpfc_ctl = intel_de_read(display, ILK_DPFC_CONTROL(fbc->id));
550 	if (dpfc_ctl & DPFC_CTL_EN) {
551 		dpfc_ctl &= ~DPFC_CTL_EN;
552 		intel_de_write(display, ILK_DPFC_CONTROL(fbc->id), dpfc_ctl);
553 
554 		/* wa_18038517565 Enable DPFC clock gating after FBC disable */
555 		if (display->platform.dg2 || DISPLAY_VER(display) >= 14)
556 			fbc_compressor_clkgate_disable_wa(fbc, false);
557 	}
558 }
559 
560 static bool ilk_fbc_is_active(struct intel_fbc *fbc)
561 {
562 	return intel_de_read(fbc->display, ILK_DPFC_CONTROL(fbc->id)) & DPFC_CTL_EN;
563 }
564 
565 static bool ilk_fbc_is_compressing(struct intel_fbc *fbc)
566 {
567 	return intel_de_read(fbc->display, ILK_DPFC_STATUS(fbc->id)) & DPFC_COMP_SEG_MASK;
568 }
569 
570 static void ilk_fbc_program_cfb(struct intel_fbc *fbc)
571 {
572 	struct intel_display *display = fbc->display;
573 
574 	intel_de_write(display, ILK_DPFC_CB_BASE(fbc->id),
575 		       i915_gem_stolen_node_offset(&fbc->compressed_fb));
576 }
577 
578 static const struct intel_fbc_funcs ilk_fbc_funcs = {
579 	.activate = ilk_fbc_activate,
580 	.deactivate = ilk_fbc_deactivate,
581 	.is_active = ilk_fbc_is_active,
582 	.is_compressing = ilk_fbc_is_compressing,
583 	.nuke = i965_fbc_nuke,
584 	.program_cfb = ilk_fbc_program_cfb,
585 };
586 
587 static void snb_fbc_program_fence(struct intel_fbc *fbc)
588 {
589 	struct intel_display *display = fbc->display;
590 	const struct intel_fbc_state *fbc_state = &fbc->state;
591 	u32 ctl = 0;
592 
593 	if (fbc_state->fence_id >= 0)
594 		ctl = SNB_DPFC_FENCE_EN | SNB_DPFC_FENCENO(fbc_state->fence_id);
595 
596 	intel_de_write(display, SNB_DPFC_CTL_SA, ctl);
597 	intel_de_write(display, SNB_DPFC_CPU_FENCE_OFFSET, fbc_state->fence_y_offset);
598 }
599 
600 static void snb_fbc_activate(struct intel_fbc *fbc)
601 {
602 	snb_fbc_program_fence(fbc);
603 
604 	ilk_fbc_activate(fbc);
605 }
606 
607 static void snb_fbc_nuke(struct intel_fbc *fbc)
608 {
609 	struct intel_display *display = fbc->display;
610 
611 	intel_de_write(display, MSG_FBC_REND_STATE(fbc->id), FBC_REND_NUKE);
612 	intel_de_posting_read(display, MSG_FBC_REND_STATE(fbc->id));
613 }
614 
615 static const struct intel_fbc_funcs snb_fbc_funcs = {
616 	.activate = snb_fbc_activate,
617 	.deactivate = ilk_fbc_deactivate,
618 	.is_active = ilk_fbc_is_active,
619 	.is_compressing = ilk_fbc_is_compressing,
620 	.nuke = snb_fbc_nuke,
621 	.program_cfb = ilk_fbc_program_cfb,
622 };
623 
624 static void glk_fbc_program_cfb_stride(struct intel_fbc *fbc)
625 {
626 	struct intel_display *display = fbc->display;
627 	const struct intel_fbc_state *fbc_state = &fbc->state;
628 	u32 val = 0;
629 
630 	if (fbc_state->override_cfb_stride)
631 		val |= FBC_STRIDE_OVERRIDE |
632 			FBC_STRIDE(fbc_state->override_cfb_stride / fbc->limit);
633 
634 	intel_de_write(display, GLK_FBC_STRIDE(fbc->id), val);
635 }
636 
637 static void skl_fbc_program_cfb_stride(struct intel_fbc *fbc)
638 {
639 	struct intel_display *display = fbc->display;
640 	const struct intel_fbc_state *fbc_state = &fbc->state;
641 	u32 val = 0;
642 
643 	/* Display WA #0529: skl, kbl, bxt. */
644 	if (fbc_state->override_cfb_stride)
645 		val |= CHICKEN_FBC_STRIDE_OVERRIDE |
646 			CHICKEN_FBC_STRIDE(fbc_state->override_cfb_stride / fbc->limit);
647 
648 	intel_de_rmw(display, CHICKEN_MISC_4,
649 		     CHICKEN_FBC_STRIDE_OVERRIDE |
650 		     CHICKEN_FBC_STRIDE_MASK, val);
651 }
652 
653 static u32 ivb_dpfc_ctl(struct intel_fbc *fbc)
654 {
655 	struct intel_display *display = fbc->display;
656 	const struct intel_fbc_state *fbc_state = &fbc->state;
657 	u32 dpfc_ctl;
658 
659 	dpfc_ctl = g4x_dpfc_ctl_limit(fbc);
660 
661 	if (display->platform.ivybridge)
662 		dpfc_ctl |= DPFC_CTL_PLANE_IVB(fbc_state->plane->i9xx_plane);
663 
664 	if (DISPLAY_VER(display) >= 20)
665 		dpfc_ctl |= DPFC_CTL_PLANE_BINDING(fbc_state->plane->id);
666 
667 	if (fbc_state->fence_id >= 0)
668 		dpfc_ctl |= DPFC_CTL_FENCE_EN_IVB;
669 
670 	if (fbc->false_color)
671 		dpfc_ctl |= DPFC_CTL_FALSE_COLOR;
672 
673 	return dpfc_ctl;
674 }
675 
676 static void ivb_fbc_activate(struct intel_fbc *fbc)
677 {
678 	struct intel_display *display = fbc->display;
679 	u32 dpfc_ctl;
680 
681 	if (DISPLAY_VER(display) >= 10)
682 		glk_fbc_program_cfb_stride(fbc);
683 	else if (DISPLAY_VER(display) == 9)
684 		skl_fbc_program_cfb_stride(fbc);
685 
686 	if (intel_fbc_has_fences(display))
687 		snb_fbc_program_fence(fbc);
688 
689 	/* wa_14019417088 Alternative WA*/
690 	dpfc_ctl = ivb_dpfc_ctl(fbc);
691 	if (DISPLAY_VER(display) >= 20)
692 		intel_de_write(display, ILK_DPFC_CONTROL(fbc->id), dpfc_ctl);
693 
694 	if (HAS_FBC_DIRTY_RECT(display))
695 		intel_de_write(display, XE3_FBC_DIRTY_CTL(fbc->id),
696 			       FBC_DIRTY_RECT_EN);
697 
698 	intel_de_write(display, ILK_DPFC_CONTROL(fbc->id),
699 		       DPFC_CTL_EN | dpfc_ctl);
700 }
701 
702 static bool ivb_fbc_is_compressing(struct intel_fbc *fbc)
703 {
704 	return intel_de_read(fbc->display, ILK_DPFC_STATUS2(fbc->id)) & DPFC_COMP_SEG_MASK_IVB;
705 }
706 
707 static void ivb_fbc_set_false_color(struct intel_fbc *fbc,
708 				    bool enable)
709 {
710 	intel_de_rmw(fbc->display, ILK_DPFC_CONTROL(fbc->id),
711 		     DPFC_CTL_FALSE_COLOR, enable ? DPFC_CTL_FALSE_COLOR : 0);
712 }
713 
714 static const struct intel_fbc_funcs ivb_fbc_funcs = {
715 	.activate = ivb_fbc_activate,
716 	.deactivate = ilk_fbc_deactivate,
717 	.is_active = ilk_fbc_is_active,
718 	.is_compressing = ivb_fbc_is_compressing,
719 	.nuke = snb_fbc_nuke,
720 	.program_cfb = ilk_fbc_program_cfb,
721 	.set_false_color = ivb_fbc_set_false_color,
722 };
723 
724 static bool intel_fbc_hw_is_active(struct intel_fbc *fbc)
725 {
726 	return fbc->funcs->is_active(fbc);
727 }
728 
729 static void intel_fbc_hw_activate(struct intel_fbc *fbc)
730 {
731 	trace_intel_fbc_activate(fbc->state.plane);
732 
733 	fbc->active = true;
734 	fbc->activated = true;
735 
736 	fbc->funcs->activate(fbc);
737 }
738 
739 static void intel_fbc_hw_deactivate(struct intel_fbc *fbc)
740 {
741 	trace_intel_fbc_deactivate(fbc->state.plane);
742 
743 	fbc->active = false;
744 
745 	fbc->funcs->deactivate(fbc);
746 }
747 
748 static bool intel_fbc_is_compressing(struct intel_fbc *fbc)
749 {
750 	return fbc->funcs->is_compressing(fbc);
751 }
752 
753 static void intel_fbc_nuke(struct intel_fbc *fbc)
754 {
755 	struct intel_display *display = fbc->display;
756 
757 	lockdep_assert_held(&fbc->lock);
758 	drm_WARN_ON(display->drm, fbc->flip_pending);
759 
760 	trace_intel_fbc_nuke(fbc->state.plane);
761 
762 	fbc->funcs->nuke(fbc);
763 }
764 
765 static void intel_fbc_activate(struct intel_fbc *fbc)
766 {
767 	struct intel_display *display = fbc->display;
768 
769 	lockdep_assert_held(&fbc->lock);
770 
771 	/* only the fence can change for a flip nuke */
772 	if (fbc->active && !intel_fbc_has_fences(display))
773 		return;
774 	/*
775 	 * In case of FBC dirt rect, any updates to the FBC registers will
776 	 * trigger the nuke.
777 	 */
778 	drm_WARN_ON(display->drm, fbc->active && HAS_FBC_DIRTY_RECT(display));
779 
780 	intel_fbc_hw_activate(fbc);
781 	intel_fbc_nuke(fbc);
782 
783 	fbc->no_fbc_reason = NULL;
784 }
785 
786 static void intel_fbc_deactivate(struct intel_fbc *fbc, const char *reason)
787 {
788 	lockdep_assert_held(&fbc->lock);
789 
790 	if (fbc->active)
791 		intel_fbc_hw_deactivate(fbc);
792 
793 	fbc->no_fbc_reason = reason;
794 }
795 
796 static u64 intel_fbc_cfb_base_max(struct intel_display *display)
797 {
798 	if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
799 		return BIT_ULL(28);
800 	else
801 		return BIT_ULL(32);
802 }
803 
804 static u64 intel_fbc_stolen_end(struct intel_display *display)
805 {
806 	struct drm_i915_private __maybe_unused *i915 = to_i915(display->drm);
807 	u64 end;
808 
809 	/* The FBC hardware for BDW/SKL doesn't have access to the stolen
810 	 * reserved range size, so it always assumes the maximum (8mb) is used.
811 	 * If we enable FBC using a CFB on that memory range we'll get FIFO
812 	 * underruns, even if that range is not reserved by the BIOS. */
813 	if (display->platform.broadwell ||
814 	    (DISPLAY_VER(display) == 9 && !display->platform.broxton))
815 		end = i915_gem_stolen_area_size(i915) - 8 * 1024 * 1024;
816 	else
817 		end = U64_MAX;
818 
819 	return min(end, intel_fbc_cfb_base_max(display));
820 }
821 
822 static int intel_fbc_min_limit(const struct intel_plane_state *plane_state)
823 {
824 	return plane_state->hw.fb->format->cpp[0] == 2 ? 2 : 1;
825 }
826 
827 static int intel_fbc_max_limit(struct intel_display *display)
828 {
829 	/* WaFbcOnly1to1Ratio:ctg */
830 	if (display->platform.g4x)
831 		return 1;
832 
833 	/*
834 	 * FBC2 can only do 1:1, 1:2, 1:4, we limit
835 	 * FBC1 to the same out of convenience.
836 	 */
837 	return 4;
838 }
839 
840 static int find_compression_limit(struct intel_fbc *fbc,
841 				  unsigned int size, int min_limit)
842 {
843 	struct intel_display *display = fbc->display;
844 	struct drm_i915_private *i915 = to_i915(display->drm);
845 	u64 end = intel_fbc_stolen_end(display);
846 	int ret, limit = min_limit;
847 
848 	size /= limit;
849 
850 	/* Try to over-allocate to reduce reallocations and fragmentation. */
851 	ret = i915_gem_stolen_insert_node_in_range(i915, &fbc->compressed_fb,
852 						   size <<= 1, 4096, 0, end);
853 	if (ret == 0)
854 		return limit;
855 
856 	for (; limit <= intel_fbc_max_limit(display); limit <<= 1) {
857 		ret = i915_gem_stolen_insert_node_in_range(i915, &fbc->compressed_fb,
858 							   size >>= 1, 4096, 0, end);
859 		if (ret == 0)
860 			return limit;
861 	}
862 
863 	return 0;
864 }
865 
866 static int intel_fbc_alloc_cfb(struct intel_fbc *fbc,
867 			       unsigned int size, int min_limit)
868 {
869 	struct intel_display *display = fbc->display;
870 	struct drm_i915_private *i915 = to_i915(display->drm);
871 	int ret;
872 
873 	drm_WARN_ON(display->drm,
874 		    i915_gem_stolen_node_allocated(&fbc->compressed_fb));
875 	drm_WARN_ON(display->drm,
876 		    i915_gem_stolen_node_allocated(&fbc->compressed_llb));
877 
878 	if (DISPLAY_VER(display) < 5 && !display->platform.g4x) {
879 		ret = i915_gem_stolen_insert_node(i915, &fbc->compressed_llb,
880 						  4096, 4096);
881 		if (ret)
882 			goto err;
883 	}
884 
885 	ret = find_compression_limit(fbc, size, min_limit);
886 	if (!ret)
887 		goto err_llb;
888 	else if (ret > min_limit)
889 		drm_info_once(display->drm,
890 			      "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");
891 
892 	fbc->limit = ret;
893 
894 	drm_dbg_kms(display->drm,
895 		    "reserved %llu bytes of contiguous stolen space for FBC, limit: %d\n",
896 		    i915_gem_stolen_node_size(&fbc->compressed_fb), fbc->limit);
897 	return 0;
898 
899 err_llb:
900 	if (i915_gem_stolen_node_allocated(&fbc->compressed_llb))
901 		i915_gem_stolen_remove_node(i915, &fbc->compressed_llb);
902 err:
903 	if (i915_gem_stolen_initialized(i915))
904 		drm_info_once(display->drm,
905 			      "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);
906 	return -ENOSPC;
907 }
908 
909 static void intel_fbc_program_cfb(struct intel_fbc *fbc)
910 {
911 	fbc->funcs->program_cfb(fbc);
912 }
913 
914 static void intel_fbc_program_workarounds(struct intel_fbc *fbc)
915 {
916 	struct intel_display *display = fbc->display;
917 
918 	if (display->platform.skylake || display->platform.broxton) {
919 		/*
920 		 * WaFbcHighMemBwCorruptionAvoidance:skl,bxt
921 		 * Display WA #0883: skl,bxt
922 		 */
923 		intel_de_rmw(display, ILK_DPFC_CHICKEN(fbc->id),
924 			     0, DPFC_DISABLE_DUMMY0);
925 	}
926 
927 	if (display->platform.skylake || display->platform.kabylake ||
928 	    display->platform.coffeelake || display->platform.cometlake) {
929 		/*
930 		 * WaFbcNukeOnHostModify:skl,kbl,cfl
931 		 * Display WA #0873: skl,kbl,cfl
932 		 */
933 		intel_de_rmw(display, ILK_DPFC_CHICKEN(fbc->id),
934 			     0, DPFC_NUKE_ON_ANY_MODIFICATION);
935 	}
936 
937 	/* Wa_1409120013:icl,jsl,tgl,dg1 */
938 	if (IS_DISPLAY_VER(display, 11, 12))
939 		intel_de_rmw(display, ILK_DPFC_CHICKEN(fbc->id),
940 			     0, DPFC_CHICKEN_COMP_DUMMY_PIXEL);
941 
942 	/* Wa_22014263786:icl,jsl,tgl,dg1,rkl,adls,adlp,mtl */
943 	if (DISPLAY_VER(display) >= 11 && !display->platform.dg2)
944 		intel_de_rmw(display, ILK_DPFC_CHICKEN(fbc->id),
945 			     0, DPFC_CHICKEN_FORCE_SLB_INVALIDATION);
946 
947 	/* wa_18038517565 Disable DPFC clock gating before FBC enable */
948 	if (display->platform.dg2 || DISPLAY_VER(display) >= 14)
949 		fbc_compressor_clkgate_disable_wa(fbc, true);
950 }
951 
952 static void __intel_fbc_cleanup_cfb(struct intel_fbc *fbc)
953 {
954 	struct intel_display *display = fbc->display;
955 	struct drm_i915_private *i915 = to_i915(display->drm);
956 
957 	if (WARN_ON(intel_fbc_hw_is_active(fbc)))
958 		return;
959 
960 	if (i915_gem_stolen_node_allocated(&fbc->compressed_llb))
961 		i915_gem_stolen_remove_node(i915, &fbc->compressed_llb);
962 	if (i915_gem_stolen_node_allocated(&fbc->compressed_fb))
963 		i915_gem_stolen_remove_node(i915, &fbc->compressed_fb);
964 }
965 
966 void intel_fbc_cleanup(struct intel_display *display)
967 {
968 	struct intel_fbc *fbc;
969 	enum intel_fbc_id fbc_id;
970 
971 	for_each_intel_fbc(display, fbc, fbc_id) {
972 		mutex_lock(&fbc->lock);
973 		__intel_fbc_cleanup_cfb(fbc);
974 		mutex_unlock(&fbc->lock);
975 
976 		kfree(fbc);
977 	}
978 }
979 
980 static bool i8xx_fbc_stride_is_valid(const struct intel_plane_state *plane_state)
981 {
982 	const struct drm_framebuffer *fb = plane_state->hw.fb;
983 	unsigned int stride = intel_fbc_plane_stride(plane_state) *
984 		fb->format->cpp[0];
985 
986 	return stride == 4096 || stride == 8192;
987 }
988 
989 static bool i965_fbc_stride_is_valid(const struct intel_plane_state *plane_state)
990 {
991 	const struct drm_framebuffer *fb = plane_state->hw.fb;
992 	unsigned int stride = intel_fbc_plane_stride(plane_state) *
993 		fb->format->cpp[0];
994 
995 	return stride >= 2048 && stride <= 16384;
996 }
997 
998 static bool g4x_fbc_stride_is_valid(const struct intel_plane_state *plane_state)
999 {
1000 	return true;
1001 }
1002 
1003 static bool skl_fbc_stride_is_valid(const struct intel_plane_state *plane_state)
1004 {
1005 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1006 	unsigned int stride = intel_fbc_plane_stride(plane_state) *
1007 		fb->format->cpp[0];
1008 
1009 	/* Display WA #1105: skl,bxt,kbl,cfl,glk */
1010 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR && stride & 511)
1011 		return false;
1012 
1013 	return true;
1014 }
1015 
1016 static bool icl_fbc_stride_is_valid(const struct intel_plane_state *plane_state)
1017 {
1018 	return true;
1019 }
1020 
1021 static bool stride_is_valid(const struct intel_plane_state *plane_state)
1022 {
1023 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1024 
1025 	if (DISPLAY_VER(display) >= 11)
1026 		return icl_fbc_stride_is_valid(plane_state);
1027 	else if (DISPLAY_VER(display) >= 9)
1028 		return skl_fbc_stride_is_valid(plane_state);
1029 	else if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
1030 		return g4x_fbc_stride_is_valid(plane_state);
1031 	else if (DISPLAY_VER(display) == 4)
1032 		return i965_fbc_stride_is_valid(plane_state);
1033 	else
1034 		return i8xx_fbc_stride_is_valid(plane_state);
1035 }
1036 
1037 static bool i8xx_fbc_pixel_format_is_valid(const struct intel_plane_state *plane_state)
1038 {
1039 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1040 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1041 
1042 	switch (fb->format->format) {
1043 	case DRM_FORMAT_XRGB8888:
1044 	case DRM_FORMAT_XBGR8888:
1045 		return true;
1046 	case DRM_FORMAT_XRGB1555:
1047 	case DRM_FORMAT_RGB565:
1048 		/* 16bpp not supported on gen2 */
1049 		if (DISPLAY_VER(display) == 2)
1050 			return false;
1051 		return true;
1052 	default:
1053 		return false;
1054 	}
1055 }
1056 
1057 static bool g4x_fbc_pixel_format_is_valid(const struct intel_plane_state *plane_state)
1058 {
1059 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1060 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1061 
1062 	switch (fb->format->format) {
1063 	case DRM_FORMAT_XRGB8888:
1064 	case DRM_FORMAT_XBGR8888:
1065 		return true;
1066 	case DRM_FORMAT_RGB565:
1067 		/* WaFbcOnly1to1Ratio:ctg */
1068 		if (display->platform.g4x)
1069 			return false;
1070 		return true;
1071 	default:
1072 		return false;
1073 	}
1074 }
1075 
1076 static bool lnl_fbc_pixel_format_is_valid(const struct intel_plane_state *plane_state)
1077 {
1078 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1079 
1080 	switch (fb->format->format) {
1081 	case DRM_FORMAT_XRGB8888:
1082 	case DRM_FORMAT_XBGR8888:
1083 	case DRM_FORMAT_ARGB8888:
1084 	case DRM_FORMAT_ABGR8888:
1085 	case DRM_FORMAT_RGB565:
1086 		return true;
1087 	default:
1088 		return false;
1089 	}
1090 }
1091 
1092 static bool pixel_format_is_valid(const struct intel_plane_state *plane_state)
1093 {
1094 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1095 
1096 	if (DISPLAY_VER(display) >= 20)
1097 		return lnl_fbc_pixel_format_is_valid(plane_state);
1098 	else if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
1099 		return g4x_fbc_pixel_format_is_valid(plane_state);
1100 	else
1101 		return i8xx_fbc_pixel_format_is_valid(plane_state);
1102 }
1103 
1104 static bool i8xx_fbc_rotation_is_valid(const struct intel_plane_state *plane_state)
1105 {
1106 	return plane_state->hw.rotation == DRM_MODE_ROTATE_0;
1107 }
1108 
1109 static bool g4x_fbc_rotation_is_valid(const struct intel_plane_state *plane_state)
1110 {
1111 	return true;
1112 }
1113 
1114 static bool skl_fbc_rotation_is_valid(const struct intel_plane_state *plane_state)
1115 {
1116 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1117 	unsigned int rotation = plane_state->hw.rotation;
1118 
1119 	if (fb->format->format == DRM_FORMAT_RGB565 &&
1120 	    drm_rotation_90_or_270(rotation))
1121 		return false;
1122 
1123 	return true;
1124 }
1125 
1126 static bool rotation_is_valid(const struct intel_plane_state *plane_state)
1127 {
1128 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1129 
1130 	if (DISPLAY_VER(display) >= 9)
1131 		return skl_fbc_rotation_is_valid(plane_state);
1132 	else if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
1133 		return g4x_fbc_rotation_is_valid(plane_state);
1134 	else
1135 		return i8xx_fbc_rotation_is_valid(plane_state);
1136 }
1137 
1138 static void intel_fbc_max_surface_size(struct intel_display *display,
1139 				       unsigned int *w, unsigned int *h)
1140 {
1141 	if (DISPLAY_VER(display) >= 11) {
1142 		*w = 8192;
1143 		*h = 4096;
1144 	} else if (DISPLAY_VER(display) >= 10) {
1145 		*w = 5120;
1146 		*h = 4096;
1147 	} else if (DISPLAY_VER(display) >= 7) {
1148 		*w = 4096;
1149 		*h = 4096;
1150 	} else if (DISPLAY_VER(display) >= 5 || display->platform.g4x) {
1151 		*w = 4096;
1152 		*h = 2048;
1153 	} else {
1154 		*w = 2048;
1155 		*h = 1536;
1156 	}
1157 }
1158 
1159 /*
1160  * For some reason, the hardware tracking starts looking at whatever we
1161  * programmed as the display plane base address register. It does not look at
1162  * the X and Y offset registers. That's why we include the src x/y offsets
1163  * instead of just looking at the plane size.
1164  */
1165 static bool intel_fbc_surface_size_ok(const struct intel_plane_state *plane_state)
1166 {
1167 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1168 	unsigned int effective_w, effective_h, max_w, max_h;
1169 
1170 	intel_fbc_max_surface_size(display, &max_w, &max_h);
1171 
1172 	effective_w = plane_state->view.color_plane[0].x +
1173 		(drm_rect_width(&plane_state->uapi.src) >> 16);
1174 	effective_h = plane_state->view.color_plane[0].y +
1175 		(drm_rect_height(&plane_state->uapi.src) >> 16);
1176 
1177 	return effective_w <= max_w && effective_h <= max_h;
1178 }
1179 
1180 static void intel_fbc_max_plane_size(struct intel_display *display,
1181 				     unsigned int *w, unsigned int *h)
1182 {
1183 	if (DISPLAY_VER(display) >= 10) {
1184 		*w = 5120;
1185 		*h = 4096;
1186 	} else if (DISPLAY_VER(display) >= 8 || display->platform.haswell) {
1187 		*w = 4096;
1188 		*h = 4096;
1189 	} else if (DISPLAY_VER(display) >= 5 || display->platform.g4x) {
1190 		*w = 4096;
1191 		*h = 2048;
1192 	} else {
1193 		*w = 2048;
1194 		*h = 1536;
1195 	}
1196 }
1197 
1198 static bool intel_fbc_plane_size_valid(const struct intel_plane_state *plane_state)
1199 {
1200 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1201 	unsigned int w, h, max_w, max_h;
1202 
1203 	intel_fbc_max_plane_size(display, &max_w, &max_h);
1204 
1205 	w = drm_rect_width(&plane_state->uapi.src) >> 16;
1206 	h = drm_rect_height(&plane_state->uapi.src) >> 16;
1207 
1208 	return w <= max_w && h <= max_h;
1209 }
1210 
1211 static bool i8xx_fbc_tiling_valid(const struct intel_plane_state *plane_state)
1212 {
1213 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1214 
1215 	return fb->modifier == I915_FORMAT_MOD_X_TILED;
1216 }
1217 
1218 static bool skl_fbc_tiling_valid(const struct intel_plane_state *plane_state)
1219 {
1220 	return true;
1221 }
1222 
1223 static bool tiling_is_valid(const struct intel_plane_state *plane_state)
1224 {
1225 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1226 
1227 	if (DISPLAY_VER(display) >= 9)
1228 		return skl_fbc_tiling_valid(plane_state);
1229 	else
1230 		return i8xx_fbc_tiling_valid(plane_state);
1231 }
1232 
1233 static void
1234 intel_fbc_invalidate_dirty_rect(struct intel_fbc *fbc)
1235 {
1236 	lockdep_assert_held(&fbc->lock);
1237 
1238 	fbc->state.dirty_rect = DRM_RECT_INIT(0, 0, 0, 0);
1239 }
1240 
1241 static void
1242 intel_fbc_program_dirty_rect(struct intel_dsb *dsb, struct intel_fbc *fbc,
1243 			     const struct drm_rect *fbc_dirty_rect)
1244 {
1245 	struct intel_display *display = fbc->display;
1246 
1247 	drm_WARN_ON(display->drm, fbc_dirty_rect->y2 == 0);
1248 
1249 	intel_de_write_dsb(display, dsb, XE3_FBC_DIRTY_RECT(fbc->id),
1250 			   FBC_DIRTY_RECT_START_LINE(fbc_dirty_rect->y1) |
1251 			   FBC_DIRTY_RECT_END_LINE(fbc_dirty_rect->y2 - 1));
1252 }
1253 
1254 static void
1255 intel_fbc_dirty_rect_update(struct intel_dsb *dsb, struct intel_fbc *fbc)
1256 {
1257 	const struct drm_rect *fbc_dirty_rect = &fbc->state.dirty_rect;
1258 
1259 	lockdep_assert_held(&fbc->lock);
1260 
1261 	if (!drm_rect_visible(fbc_dirty_rect))
1262 		return;
1263 
1264 	intel_fbc_program_dirty_rect(dsb, fbc, fbc_dirty_rect);
1265 }
1266 
1267 void
1268 intel_fbc_dirty_rect_update_noarm(struct intel_dsb *dsb,
1269 				  struct intel_plane *plane)
1270 {
1271 	struct intel_display *display = to_intel_display(plane);
1272 	struct intel_fbc *fbc = plane->fbc;
1273 
1274 	if (!HAS_FBC_DIRTY_RECT(display))
1275 		return;
1276 
1277 	mutex_lock(&fbc->lock);
1278 
1279 	if (fbc->state.plane == plane)
1280 		intel_fbc_dirty_rect_update(dsb, fbc);
1281 
1282 	mutex_unlock(&fbc->lock);
1283 }
1284 
1285 static void
1286 intel_fbc_hw_intialize_dirty_rect(struct intel_fbc *fbc,
1287 				  const struct intel_plane_state *plane_state)
1288 {
1289 	struct drm_rect src;
1290 
1291 	/*
1292 	 * Initializing the FBC HW with the whole plane area as the dirty rect.
1293 	 * This is to ensure that we have valid coords be written to the
1294 	 * HW as dirty rect.
1295 	 */
1296 	drm_rect_fp_to_int(&src, &plane_state->uapi.src);
1297 
1298 	intel_fbc_program_dirty_rect(NULL, fbc, &src);
1299 }
1300 
1301 static void intel_fbc_update_state(struct intel_atomic_state *state,
1302 				   struct intel_crtc *crtc,
1303 				   struct intel_plane *plane)
1304 {
1305 	struct intel_display *display = to_intel_display(state->base.dev);
1306 	const struct intel_crtc_state *crtc_state =
1307 		intel_atomic_get_new_crtc_state(state, crtc);
1308 	const struct intel_plane_state *plane_state =
1309 		intel_atomic_get_new_plane_state(state, plane);
1310 	struct intel_fbc *fbc = plane->fbc;
1311 	struct intel_fbc_state *fbc_state = &fbc->state;
1312 
1313 	WARN_ON(plane_state->no_fbc_reason);
1314 	WARN_ON(fbc_state->plane && fbc_state->plane != plane);
1315 
1316 	fbc_state->plane = plane;
1317 
1318 	/* FBC1 compression interval: arbitrary choice of 1 second */
1319 	fbc_state->interval = drm_mode_vrefresh(&crtc_state->hw.adjusted_mode);
1320 
1321 	fbc_state->fence_y_offset = intel_plane_fence_y_offset(plane_state);
1322 
1323 	drm_WARN_ON(display->drm, plane_state->flags & PLANE_HAS_FENCE &&
1324 		    !intel_fbc_has_fences(display));
1325 
1326 	if (plane_state->flags & PLANE_HAS_FENCE)
1327 		fbc_state->fence_id =  i915_vma_fence_id(plane_state->ggtt_vma);
1328 	else
1329 		fbc_state->fence_id = -1;
1330 
1331 	fbc_state->cfb_stride = intel_fbc_cfb_stride(plane_state);
1332 	fbc_state->cfb_size = intel_fbc_cfb_size(plane_state);
1333 	fbc_state->override_cfb_stride = intel_fbc_override_cfb_stride(plane_state);
1334 }
1335 
1336 static bool intel_fbc_is_fence_ok(const struct intel_plane_state *plane_state)
1337 {
1338 	struct intel_display *display = to_intel_display(plane_state->uapi.plane->dev);
1339 
1340 	/*
1341 	 * The use of a CPU fence is one of two ways to detect writes by the
1342 	 * CPU to the scanout and trigger updates to the FBC.
1343 	 *
1344 	 * The other method is by software tracking (see
1345 	 * intel_fbc_invalidate/flush()), it will manually notify FBC and nuke
1346 	 * the current compressed buffer and recompress it.
1347 	 *
1348 	 * Note that is possible for a tiled surface to be unmappable (and
1349 	 * so have no fence associated with it) due to aperture constraints
1350 	 * at the time of pinning.
1351 	 */
1352 	return DISPLAY_VER(display) >= 9 ||
1353 		(plane_state->flags & PLANE_HAS_FENCE &&
1354 		 i915_vma_fence_id(plane_state->ggtt_vma) != -1);
1355 }
1356 
1357 static bool intel_fbc_is_cfb_ok(const struct intel_plane_state *plane_state)
1358 {
1359 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1360 	struct intel_fbc *fbc = plane->fbc;
1361 
1362 	return intel_fbc_min_limit(plane_state) <= fbc->limit &&
1363 		intel_fbc_cfb_size(plane_state) <= fbc->limit *
1364 			i915_gem_stolen_node_size(&fbc->compressed_fb);
1365 }
1366 
1367 static bool intel_fbc_is_ok(const struct intel_plane_state *plane_state)
1368 {
1369 	return !plane_state->no_fbc_reason &&
1370 		intel_fbc_is_fence_ok(plane_state) &&
1371 		intel_fbc_is_cfb_ok(plane_state);
1372 }
1373 
1374 static void
1375 __intel_fbc_prepare_dirty_rect(const struct intel_plane_state *plane_state,
1376 			       const struct intel_crtc_state *crtc_state)
1377 {
1378 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1379 	struct intel_fbc *fbc = plane->fbc;
1380 	struct drm_rect *fbc_dirty_rect = &fbc->state.dirty_rect;
1381 	int width = drm_rect_width(&plane_state->uapi.src) >> 16;
1382 	const struct drm_rect *damage = &plane_state->damage;
1383 	int y_offset = plane_state->view.color_plane[0].y;
1384 
1385 	lockdep_assert_held(&fbc->lock);
1386 
1387 	if (intel_crtc_needs_modeset(crtc_state) ||
1388 	    !intel_fbc_is_ok(plane_state)) {
1389 		intel_fbc_invalidate_dirty_rect(fbc);
1390 		return;
1391 	}
1392 
1393 	if (drm_rect_visible(damage))
1394 		*fbc_dirty_rect = *damage;
1395 	else
1396 		/* dirty rect must cover at least one line */
1397 		*fbc_dirty_rect = DRM_RECT_INIT(0, y_offset, width, 1);
1398 }
1399 
1400 void
1401 intel_fbc_prepare_dirty_rect(struct intel_atomic_state *state,
1402 			     struct intel_crtc *crtc)
1403 {
1404 	struct intel_display *display = to_intel_display(state);
1405 	const struct intel_crtc_state *crtc_state =
1406 		intel_atomic_get_new_crtc_state(state, crtc);
1407 	struct intel_plane_state *plane_state;
1408 	struct intel_plane *plane;
1409 	int i;
1410 
1411 	if (!HAS_FBC_DIRTY_RECT(display))
1412 		return;
1413 
1414 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1415 		struct intel_fbc *fbc = plane->fbc;
1416 
1417 		if (!fbc || plane->pipe != crtc->pipe)
1418 			continue;
1419 
1420 		mutex_lock(&fbc->lock);
1421 
1422 		if (fbc->state.plane == plane)
1423 			__intel_fbc_prepare_dirty_rect(plane_state,
1424 						       crtc_state);
1425 
1426 		mutex_unlock(&fbc->lock);
1427 	}
1428 }
1429 
1430 static int intel_fbc_check_plane(struct intel_atomic_state *state,
1431 				 struct intel_plane *plane)
1432 {
1433 	struct intel_display *display = to_intel_display(state->base.dev);
1434 	struct drm_i915_private *i915 = to_i915(display->drm);
1435 	struct intel_plane_state *plane_state =
1436 		intel_atomic_get_new_plane_state(state, plane);
1437 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1438 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
1439 	const struct intel_crtc_state *crtc_state;
1440 	struct intel_fbc *fbc = plane->fbc;
1441 
1442 	if (!fbc)
1443 		return 0;
1444 
1445 	if (!i915_gem_stolen_initialized(i915)) {
1446 		plane_state->no_fbc_reason = "stolen memory not initialised";
1447 		return 0;
1448 	}
1449 
1450 	if (intel_vgpu_active(i915)) {
1451 		plane_state->no_fbc_reason = "VGPU active";
1452 		return 0;
1453 	}
1454 
1455 	if (!display->params.enable_fbc) {
1456 		plane_state->no_fbc_reason = "disabled per module param or by default";
1457 		return 0;
1458 	}
1459 
1460 	if (!plane_state->uapi.visible) {
1461 		plane_state->no_fbc_reason = "plane not visible";
1462 		return 0;
1463 	}
1464 
1465 	if (intel_display_needs_wa_16023588340(display)) {
1466 		plane_state->no_fbc_reason = "Wa_16023588340";
1467 		return 0;
1468 	}
1469 
1470 	/* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
1471 	if (i915_vtd_active(i915) && (display->platform.skylake || display->platform.broxton)) {
1472 		plane_state->no_fbc_reason = "VT-d enabled";
1473 		return 0;
1474 	}
1475 
1476 	crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
1477 
1478 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
1479 		plane_state->no_fbc_reason = "interlaced mode not supported";
1480 		return 0;
1481 	}
1482 
1483 	if (crtc_state->double_wide) {
1484 		plane_state->no_fbc_reason = "double wide pipe not supported";
1485 		return 0;
1486 	}
1487 
1488 	/*
1489 	 * Display 12+ is not supporting FBC with PSR2.
1490 	 * Recommendation is to keep this combination disabled
1491 	 * Bspec: 50422 HSD: 14010260002
1492 	 *
1493 	 * TODO: Implement a logic to select between PSR2 selective fetch and
1494 	 * FBC based on Bspec: 68881 in xe2lpd onwards.
1495 	 *
1496 	 * As we still see some strange underruns in those platforms while
1497 	 * disabling PSR2, keep FBC disabled in case of selective update is on
1498 	 * until the selection logic is implemented.
1499 	 */
1500 	if (DISPLAY_VER(display) >= 12 && crtc_state->has_sel_update) {
1501 		plane_state->no_fbc_reason = "Selective update enabled";
1502 		return 0;
1503 	}
1504 
1505 	/* Wa_14016291713 */
1506 	if ((IS_DISPLAY_VER(display, 12, 13) ||
1507 	     IS_DISPLAY_VERx100_STEP(display, 1400, STEP_A0, STEP_C0)) &&
1508 	    crtc_state->has_psr && !crtc_state->has_panel_replay) {
1509 		plane_state->no_fbc_reason = "PSR1 enabled (Wa_14016291713)";
1510 		return 0;
1511 	}
1512 
1513 	if (!pixel_format_is_valid(plane_state)) {
1514 		plane_state->no_fbc_reason = "pixel format not supported";
1515 		return 0;
1516 	}
1517 
1518 	if (!tiling_is_valid(plane_state)) {
1519 		plane_state->no_fbc_reason = "tiling not supported";
1520 		return 0;
1521 	}
1522 
1523 	if (!rotation_is_valid(plane_state)) {
1524 		plane_state->no_fbc_reason = "rotation not supported";
1525 		return 0;
1526 	}
1527 
1528 	if (!stride_is_valid(plane_state)) {
1529 		plane_state->no_fbc_reason = "stride not supported";
1530 		return 0;
1531 	}
1532 
1533 	if (DISPLAY_VER(display) < 20 &&
1534 	    plane_state->hw.pixel_blend_mode != DRM_MODE_BLEND_PIXEL_NONE &&
1535 	    fb->format->has_alpha) {
1536 		plane_state->no_fbc_reason = "per-pixel alpha not supported";
1537 		return 0;
1538 	}
1539 
1540 	if (!intel_fbc_plane_size_valid(plane_state)) {
1541 		plane_state->no_fbc_reason = "plane size too big";
1542 		return 0;
1543 	}
1544 
1545 	if (!intel_fbc_surface_size_ok(plane_state)) {
1546 		plane_state->no_fbc_reason = "surface size too big";
1547 		return 0;
1548 	}
1549 
1550 	/*
1551 	 * Work around a problem on GEN9+ HW, where enabling FBC on a plane
1552 	 * having a Y offset that isn't divisible by 4 causes FIFO underrun
1553 	 * and screen flicker.
1554 	 */
1555 	if (DISPLAY_VER(display) >= 9 &&
1556 	    plane_state->view.color_plane[0].y & 3) {
1557 		plane_state->no_fbc_reason = "plane start Y offset misaligned";
1558 		return 0;
1559 	}
1560 
1561 	/* Wa_22010751166: icl, ehl, tgl, dg1, rkl */
1562 	if (DISPLAY_VER(display) >= 11 &&
1563 	    (plane_state->view.color_plane[0].y +
1564 	     (drm_rect_height(&plane_state->uapi.src) >> 16)) & 3) {
1565 		plane_state->no_fbc_reason = "plane end Y offset misaligned";
1566 		return 0;
1567 	}
1568 
1569 	/* WaFbcExceedCdClockThreshold:hsw,bdw */
1570 	if (display->platform.haswell || display->platform.broadwell) {
1571 		const struct intel_cdclk_state *cdclk_state;
1572 
1573 		cdclk_state = intel_atomic_get_cdclk_state(state);
1574 		if (IS_ERR(cdclk_state))
1575 			return PTR_ERR(cdclk_state);
1576 
1577 		if (crtc_state->pixel_rate >= cdclk_state->logical.cdclk * 95 / 100) {
1578 			plane_state->no_fbc_reason = "pixel rate too high";
1579 			return 0;
1580 		}
1581 	}
1582 
1583 	plane_state->no_fbc_reason = NULL;
1584 
1585 	return 0;
1586 }
1587 
1588 
1589 static bool intel_fbc_can_flip_nuke(struct intel_atomic_state *state,
1590 				    struct intel_crtc *crtc,
1591 				    struct intel_plane *plane)
1592 {
1593 	const struct intel_crtc_state *new_crtc_state =
1594 		intel_atomic_get_new_crtc_state(state, crtc);
1595 	const struct intel_plane_state *old_plane_state =
1596 		intel_atomic_get_old_plane_state(state, plane);
1597 	const struct intel_plane_state *new_plane_state =
1598 		intel_atomic_get_new_plane_state(state, plane);
1599 	const struct drm_framebuffer *old_fb = old_plane_state->hw.fb;
1600 	const struct drm_framebuffer *new_fb = new_plane_state->hw.fb;
1601 
1602 	if (intel_crtc_needs_modeset(new_crtc_state))
1603 		return false;
1604 
1605 	if (!intel_fbc_is_ok(old_plane_state) ||
1606 	    !intel_fbc_is_ok(new_plane_state))
1607 		return false;
1608 
1609 	if (old_fb->format->format != new_fb->format->format)
1610 		return false;
1611 
1612 	if (old_fb->modifier != new_fb->modifier)
1613 		return false;
1614 
1615 	if (intel_fbc_plane_stride(old_plane_state) !=
1616 	    intel_fbc_plane_stride(new_plane_state))
1617 		return false;
1618 
1619 	if (intel_fbc_cfb_stride(old_plane_state) !=
1620 	    intel_fbc_cfb_stride(new_plane_state))
1621 		return false;
1622 
1623 	if (intel_fbc_cfb_size(old_plane_state) !=
1624 	    intel_fbc_cfb_size(new_plane_state))
1625 		return false;
1626 
1627 	if (intel_fbc_override_cfb_stride(old_plane_state) !=
1628 	    intel_fbc_override_cfb_stride(new_plane_state))
1629 		return false;
1630 
1631 	return true;
1632 }
1633 
1634 static bool __intel_fbc_pre_update(struct intel_atomic_state *state,
1635 				   struct intel_crtc *crtc,
1636 				   struct intel_plane *plane)
1637 {
1638 	struct intel_display *display = to_intel_display(state->base.dev);
1639 	struct intel_fbc *fbc = plane->fbc;
1640 	bool need_vblank_wait = false;
1641 
1642 	lockdep_assert_held(&fbc->lock);
1643 
1644 	fbc->flip_pending = true;
1645 
1646 	if (intel_fbc_can_flip_nuke(state, crtc, plane))
1647 		return need_vblank_wait;
1648 
1649 	intel_fbc_deactivate(fbc, "update pending");
1650 
1651 	/*
1652 	 * Display WA #1198: glk+
1653 	 * Need an extra vblank wait between FBC disable and most plane
1654 	 * updates. Bspec says this is only needed for plane disable, but
1655 	 * that is not true. Touching most plane registers will cause the
1656 	 * corruption to appear. Also SKL/derivatives do not seem to be
1657 	 * affected.
1658 	 *
1659 	 * TODO: could optimize this a bit by sampling the frame
1660 	 * counter when we disable FBC (if it was already done earlier)
1661 	 * and skipping the extra vblank wait before the plane update
1662 	 * if at least one frame has already passed.
1663 	 */
1664 	if (fbc->activated && DISPLAY_VER(display) >= 10)
1665 		need_vblank_wait = true;
1666 	fbc->activated = false;
1667 
1668 	return need_vblank_wait;
1669 }
1670 
1671 bool intel_fbc_pre_update(struct intel_atomic_state *state,
1672 			  struct intel_crtc *crtc)
1673 {
1674 	const struct intel_plane_state __maybe_unused *plane_state;
1675 	bool need_vblank_wait = false;
1676 	struct intel_plane *plane;
1677 	int i;
1678 
1679 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1680 		struct intel_fbc *fbc = plane->fbc;
1681 
1682 		if (!fbc || plane->pipe != crtc->pipe)
1683 			continue;
1684 
1685 		mutex_lock(&fbc->lock);
1686 
1687 		if (fbc->state.plane == plane)
1688 			need_vblank_wait |= __intel_fbc_pre_update(state, crtc, plane);
1689 
1690 		mutex_unlock(&fbc->lock);
1691 	}
1692 
1693 	return need_vblank_wait;
1694 }
1695 
1696 static void __intel_fbc_disable(struct intel_fbc *fbc)
1697 {
1698 	struct intel_display *display = fbc->display;
1699 	struct intel_plane *plane = fbc->state.plane;
1700 
1701 	lockdep_assert_held(&fbc->lock);
1702 	drm_WARN_ON(display->drm, fbc->active);
1703 
1704 	drm_dbg_kms(display->drm, "Disabling FBC on [PLANE:%d:%s]\n",
1705 		    plane->base.base.id, plane->base.name);
1706 
1707 	intel_fbc_invalidate_dirty_rect(fbc);
1708 
1709 	__intel_fbc_cleanup_cfb(fbc);
1710 
1711 	fbc->state.plane = NULL;
1712 	fbc->flip_pending = false;
1713 	fbc->busy_bits = 0;
1714 }
1715 
1716 static void __intel_fbc_post_update(struct intel_fbc *fbc)
1717 {
1718 	lockdep_assert_held(&fbc->lock);
1719 
1720 	fbc->flip_pending = false;
1721 	fbc->busy_bits = 0;
1722 
1723 	intel_fbc_activate(fbc);
1724 }
1725 
1726 void intel_fbc_post_update(struct intel_atomic_state *state,
1727 			   struct intel_crtc *crtc)
1728 {
1729 	const struct intel_plane_state __maybe_unused *plane_state;
1730 	struct intel_plane *plane;
1731 	int i;
1732 
1733 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1734 		struct intel_fbc *fbc = plane->fbc;
1735 
1736 		if (!fbc || plane->pipe != crtc->pipe)
1737 			continue;
1738 
1739 		mutex_lock(&fbc->lock);
1740 
1741 		if (fbc->state.plane == plane)
1742 			__intel_fbc_post_update(fbc);
1743 
1744 		mutex_unlock(&fbc->lock);
1745 	}
1746 }
1747 
1748 static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
1749 {
1750 	if (fbc->state.plane)
1751 		return fbc->state.plane->frontbuffer_bit;
1752 	else
1753 		return 0;
1754 }
1755 
1756 static void __intel_fbc_invalidate(struct intel_fbc *fbc,
1757 				   unsigned int frontbuffer_bits,
1758 				   enum fb_op_origin origin)
1759 {
1760 	if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1761 		return;
1762 
1763 	mutex_lock(&fbc->lock);
1764 
1765 	frontbuffer_bits &= intel_fbc_get_frontbuffer_bit(fbc);
1766 	if (!frontbuffer_bits)
1767 		goto out;
1768 
1769 	fbc->busy_bits |= frontbuffer_bits;
1770 	intel_fbc_deactivate(fbc, "frontbuffer write");
1771 
1772 out:
1773 	mutex_unlock(&fbc->lock);
1774 }
1775 
1776 void intel_fbc_invalidate(struct intel_display *display,
1777 			  unsigned int frontbuffer_bits,
1778 			  enum fb_op_origin origin)
1779 {
1780 	struct intel_fbc *fbc;
1781 	enum intel_fbc_id fbc_id;
1782 
1783 	for_each_intel_fbc(display, fbc, fbc_id)
1784 		__intel_fbc_invalidate(fbc, frontbuffer_bits, origin);
1785 
1786 }
1787 
1788 static void __intel_fbc_flush(struct intel_fbc *fbc,
1789 			      unsigned int frontbuffer_bits,
1790 			      enum fb_op_origin origin)
1791 {
1792 	mutex_lock(&fbc->lock);
1793 
1794 	frontbuffer_bits &= intel_fbc_get_frontbuffer_bit(fbc);
1795 	if (!frontbuffer_bits)
1796 		goto out;
1797 
1798 	fbc->busy_bits &= ~frontbuffer_bits;
1799 
1800 	if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1801 		goto out;
1802 
1803 	if (fbc->busy_bits || fbc->flip_pending)
1804 		goto out;
1805 
1806 	if (fbc->active)
1807 		intel_fbc_nuke(fbc);
1808 	else
1809 		intel_fbc_activate(fbc);
1810 
1811 out:
1812 	mutex_unlock(&fbc->lock);
1813 }
1814 
1815 void intel_fbc_flush(struct intel_display *display,
1816 		     unsigned int frontbuffer_bits,
1817 		     enum fb_op_origin origin)
1818 {
1819 	struct intel_fbc *fbc;
1820 	enum intel_fbc_id fbc_id;
1821 
1822 	for_each_intel_fbc(display, fbc, fbc_id)
1823 		__intel_fbc_flush(fbc, frontbuffer_bits, origin);
1824 }
1825 
1826 int intel_fbc_atomic_check(struct intel_atomic_state *state)
1827 {
1828 	struct intel_plane_state __maybe_unused *plane_state;
1829 	struct intel_plane *plane;
1830 	int i;
1831 
1832 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1833 		int ret;
1834 
1835 		ret = intel_fbc_check_plane(state, plane);
1836 		if (ret)
1837 			return ret;
1838 	}
1839 
1840 	return 0;
1841 }
1842 
1843 static void __intel_fbc_enable(struct intel_atomic_state *state,
1844 			       struct intel_crtc *crtc,
1845 			       struct intel_plane *plane)
1846 {
1847 	struct intel_display *display = to_intel_display(state->base.dev);
1848 	const struct intel_plane_state *plane_state =
1849 		intel_atomic_get_new_plane_state(state, plane);
1850 	struct intel_fbc *fbc = plane->fbc;
1851 
1852 	lockdep_assert_held(&fbc->lock);
1853 
1854 	if (fbc->state.plane) {
1855 		if (fbc->state.plane != plane)
1856 			return;
1857 
1858 		if (intel_fbc_is_ok(plane_state)) {
1859 			intel_fbc_update_state(state, crtc, plane);
1860 			return;
1861 		}
1862 
1863 		__intel_fbc_disable(fbc);
1864 	}
1865 
1866 	drm_WARN_ON(display->drm, fbc->active);
1867 
1868 	fbc->no_fbc_reason = plane_state->no_fbc_reason;
1869 	if (fbc->no_fbc_reason)
1870 		return;
1871 
1872 	if (!intel_fbc_is_fence_ok(plane_state)) {
1873 		fbc->no_fbc_reason = "framebuffer not fenced";
1874 		return;
1875 	}
1876 
1877 	if (fbc->underrun_detected) {
1878 		fbc->no_fbc_reason = "FIFO underrun";
1879 		return;
1880 	}
1881 
1882 	if (intel_fbc_alloc_cfb(fbc, intel_fbc_cfb_size(plane_state),
1883 				intel_fbc_min_limit(plane_state))) {
1884 		fbc->no_fbc_reason = "not enough stolen memory";
1885 		return;
1886 	}
1887 
1888 	drm_dbg_kms(display->drm, "Enabling FBC on [PLANE:%d:%s]\n",
1889 		    plane->base.base.id, plane->base.name);
1890 	fbc->no_fbc_reason = "FBC enabled but not active yet\n";
1891 
1892 	intel_fbc_update_state(state, crtc, plane);
1893 
1894 	if (HAS_FBC_DIRTY_RECT(display))
1895 		intel_fbc_hw_intialize_dirty_rect(fbc, plane_state);
1896 
1897 	intel_fbc_program_workarounds(fbc);
1898 	intel_fbc_program_cfb(fbc);
1899 }
1900 
1901 /**
1902  * intel_fbc_disable - disable FBC if it's associated with crtc
1903  * @crtc: the CRTC
1904  *
1905  * This function disables FBC if it's associated with the provided CRTC.
1906  */
1907 void intel_fbc_disable(struct intel_crtc *crtc)
1908 {
1909 	struct intel_display *display = to_intel_display(crtc->base.dev);
1910 	struct intel_plane *plane;
1911 
1912 	for_each_intel_plane(display->drm, plane) {
1913 		struct intel_fbc *fbc = plane->fbc;
1914 
1915 		if (!fbc || plane->pipe != crtc->pipe)
1916 			continue;
1917 
1918 		mutex_lock(&fbc->lock);
1919 		if (fbc->state.plane == plane)
1920 			__intel_fbc_disable(fbc);
1921 		mutex_unlock(&fbc->lock);
1922 	}
1923 }
1924 
1925 void intel_fbc_update(struct intel_atomic_state *state,
1926 		      struct intel_crtc *crtc)
1927 {
1928 	const struct intel_crtc_state *crtc_state =
1929 		intel_atomic_get_new_crtc_state(state, crtc);
1930 	const struct intel_plane_state *plane_state;
1931 	struct intel_plane *plane;
1932 	int i;
1933 
1934 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1935 		struct intel_fbc *fbc = plane->fbc;
1936 
1937 		if (!fbc || plane->pipe != crtc->pipe)
1938 			continue;
1939 
1940 		mutex_lock(&fbc->lock);
1941 
1942 		if (intel_crtc_needs_fastset(crtc_state) &&
1943 		    plane_state->no_fbc_reason) {
1944 			if (fbc->state.plane == plane)
1945 				__intel_fbc_disable(fbc);
1946 		} else {
1947 			__intel_fbc_enable(state, crtc, plane);
1948 		}
1949 
1950 		mutex_unlock(&fbc->lock);
1951 	}
1952 }
1953 
1954 static void intel_fbc_underrun_work_fn(struct work_struct *work)
1955 {
1956 	struct intel_fbc *fbc = container_of(work, typeof(*fbc), underrun_work);
1957 	struct intel_display *display = fbc->display;
1958 
1959 	mutex_lock(&fbc->lock);
1960 
1961 	/* Maybe we were scheduled twice. */
1962 	if (fbc->underrun_detected || !fbc->state.plane)
1963 		goto out;
1964 
1965 	drm_dbg_kms(display->drm, "Disabling FBC due to FIFO underrun.\n");
1966 	fbc->underrun_detected = true;
1967 
1968 	intel_fbc_deactivate(fbc, "FIFO underrun");
1969 	if (!fbc->flip_pending)
1970 		intel_crtc_wait_for_next_vblank(intel_crtc_for_pipe(display, fbc->state.plane->pipe));
1971 	__intel_fbc_disable(fbc);
1972 out:
1973 	mutex_unlock(&fbc->lock);
1974 }
1975 
1976 static void __intel_fbc_reset_underrun(struct intel_fbc *fbc)
1977 {
1978 	struct intel_display *display = fbc->display;
1979 
1980 	cancel_work_sync(&fbc->underrun_work);
1981 
1982 	mutex_lock(&fbc->lock);
1983 
1984 	if (fbc->underrun_detected) {
1985 		drm_dbg_kms(display->drm,
1986 			    "Re-allowing FBC after fifo underrun\n");
1987 		fbc->no_fbc_reason = "FIFO underrun cleared";
1988 	}
1989 
1990 	fbc->underrun_detected = false;
1991 	mutex_unlock(&fbc->lock);
1992 }
1993 
1994 /*
1995  * intel_fbc_reset_underrun - reset FBC fifo underrun status.
1996  * @display: display
1997  *
1998  * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we
1999  * want to re-enable FBC after an underrun to increase test coverage.
2000  */
2001 void intel_fbc_reset_underrun(struct intel_display *display)
2002 {
2003 	struct intel_fbc *fbc;
2004 	enum intel_fbc_id fbc_id;
2005 
2006 	for_each_intel_fbc(display, fbc, fbc_id)
2007 		__intel_fbc_reset_underrun(fbc);
2008 }
2009 
2010 static void __intel_fbc_handle_fifo_underrun_irq(struct intel_fbc *fbc)
2011 {
2012 	struct drm_i915_private *i915 = to_i915(fbc->display->drm);
2013 
2014 	/*
2015 	 * There's no guarantee that underrun_detected won't be set to true
2016 	 * right after this check and before the work is scheduled, but that's
2017 	 * not a problem since we'll check it again under the work function
2018 	 * while FBC is locked. This check here is just to prevent us from
2019 	 * unnecessarily scheduling the work, and it relies on the fact that we
2020 	 * never switch underrun_detect back to false after it's true.
2021 	 */
2022 	if (READ_ONCE(fbc->underrun_detected))
2023 		return;
2024 
2025 	queue_work(i915->unordered_wq, &fbc->underrun_work);
2026 }
2027 
2028 /**
2029  * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
2030  * @display: display
2031  *
2032  * Without FBC, most underruns are harmless and don't really cause too many
2033  * problems, except for an annoying message on dmesg. With FBC, underruns can
2034  * become black screens or even worse, especially when paired with bad
2035  * watermarks. So in order for us to be on the safe side, completely disable FBC
2036  * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
2037  * already suggests that watermarks may be bad, so try to be as safe as
2038  * possible.
2039  *
2040  * This function is called from the IRQ handler.
2041  */
2042 void intel_fbc_handle_fifo_underrun_irq(struct intel_display *display)
2043 {
2044 	struct intel_fbc *fbc;
2045 	enum intel_fbc_id fbc_id;
2046 
2047 	for_each_intel_fbc(display, fbc, fbc_id)
2048 		__intel_fbc_handle_fifo_underrun_irq(fbc);
2049 }
2050 
2051 /*
2052  * The DDX driver changes its behavior depending on the value it reads from
2053  * i915.enable_fbc, so sanitize it by translating the default value into either
2054  * 0 or 1 in order to allow it to know what's going on.
2055  *
2056  * Notice that this is done at driver initialization and we still allow user
2057  * space to change the value during runtime without sanitizing it again. IGT
2058  * relies on being able to change i915.enable_fbc at runtime.
2059  */
2060 static int intel_sanitize_fbc_option(struct intel_display *display)
2061 {
2062 	if (display->params.enable_fbc >= 0)
2063 		return !!display->params.enable_fbc;
2064 
2065 	if (!HAS_FBC(display))
2066 		return 0;
2067 
2068 	if (display->platform.broadwell || DISPLAY_VER(display) >= 9)
2069 		return 1;
2070 
2071 	return 0;
2072 }
2073 
2074 void intel_fbc_add_plane(struct intel_fbc *fbc, struct intel_plane *plane)
2075 {
2076 	plane->fbc = fbc;
2077 }
2078 
2079 static struct intel_fbc *intel_fbc_create(struct intel_display *display,
2080 					  enum intel_fbc_id fbc_id)
2081 {
2082 	struct intel_fbc *fbc;
2083 
2084 	fbc = kzalloc(sizeof(*fbc), GFP_KERNEL);
2085 	if (!fbc)
2086 		return NULL;
2087 
2088 	fbc->id = fbc_id;
2089 	fbc->display = display;
2090 	INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
2091 	mutex_init(&fbc->lock);
2092 
2093 	if (DISPLAY_VER(display) >= 7)
2094 		fbc->funcs = &ivb_fbc_funcs;
2095 	else if (DISPLAY_VER(display) == 6)
2096 		fbc->funcs = &snb_fbc_funcs;
2097 	else if (DISPLAY_VER(display) == 5)
2098 		fbc->funcs = &ilk_fbc_funcs;
2099 	else if (display->platform.g4x)
2100 		fbc->funcs = &g4x_fbc_funcs;
2101 	else if (DISPLAY_VER(display) == 4)
2102 		fbc->funcs = &i965_fbc_funcs;
2103 	else
2104 		fbc->funcs = &i8xx_fbc_funcs;
2105 
2106 	return fbc;
2107 }
2108 
2109 /**
2110  * intel_fbc_init - Initialize FBC
2111  * @display: display
2112  *
2113  * This function might be called during PM init process.
2114  */
2115 void intel_fbc_init(struct intel_display *display)
2116 {
2117 	enum intel_fbc_id fbc_id;
2118 
2119 	display->params.enable_fbc = intel_sanitize_fbc_option(display);
2120 	drm_dbg_kms(display->drm, "Sanitized enable_fbc value: %d\n",
2121 		    display->params.enable_fbc);
2122 
2123 	for_each_fbc_id(display, fbc_id)
2124 		display->fbc[fbc_id] = intel_fbc_create(display, fbc_id);
2125 }
2126 
2127 /**
2128  * intel_fbc_sanitize - Sanitize FBC
2129  * @display: display
2130  *
2131  * Make sure FBC is initially disabled since we have no
2132  * idea eg. into which parts of stolen it might be scribbling
2133  * into.
2134  */
2135 void intel_fbc_sanitize(struct intel_display *display)
2136 {
2137 	struct intel_fbc *fbc;
2138 	enum intel_fbc_id fbc_id;
2139 
2140 	for_each_intel_fbc(display, fbc, fbc_id) {
2141 		if (intel_fbc_hw_is_active(fbc))
2142 			intel_fbc_hw_deactivate(fbc);
2143 	}
2144 }
2145 
2146 static int intel_fbc_debugfs_status_show(struct seq_file *m, void *unused)
2147 {
2148 	struct intel_fbc *fbc = m->private;
2149 	struct intel_display *display = fbc->display;
2150 	struct intel_plane *plane;
2151 	struct ref_tracker *wakeref;
2152 
2153 	drm_modeset_lock_all(display->drm);
2154 
2155 	wakeref = intel_display_rpm_get(display);
2156 	mutex_lock(&fbc->lock);
2157 
2158 	if (fbc->active) {
2159 		seq_puts(m, "FBC enabled\n");
2160 		seq_printf(m, "Compressing: %s\n",
2161 			   str_yes_no(intel_fbc_is_compressing(fbc)));
2162 	} else {
2163 		seq_printf(m, "FBC disabled: %s\n", fbc->no_fbc_reason);
2164 	}
2165 
2166 	for_each_intel_plane(display->drm, plane) {
2167 		const struct intel_plane_state *plane_state =
2168 			to_intel_plane_state(plane->base.state);
2169 
2170 		if (plane->fbc != fbc)
2171 			continue;
2172 
2173 		seq_printf(m, "%c [PLANE:%d:%s]: %s\n",
2174 			   fbc->state.plane == plane ? '*' : ' ',
2175 			   plane->base.base.id, plane->base.name,
2176 			   plane_state->no_fbc_reason ?: "FBC possible");
2177 	}
2178 
2179 	mutex_unlock(&fbc->lock);
2180 	intel_display_rpm_put(display, wakeref);
2181 
2182 	drm_modeset_unlock_all(display->drm);
2183 
2184 	return 0;
2185 }
2186 
2187 DEFINE_SHOW_ATTRIBUTE(intel_fbc_debugfs_status);
2188 
2189 static int intel_fbc_debugfs_false_color_get(void *data, u64 *val)
2190 {
2191 	struct intel_fbc *fbc = data;
2192 
2193 	*val = fbc->false_color;
2194 
2195 	return 0;
2196 }
2197 
2198 static int intel_fbc_debugfs_false_color_set(void *data, u64 val)
2199 {
2200 	struct intel_fbc *fbc = data;
2201 
2202 	mutex_lock(&fbc->lock);
2203 
2204 	fbc->false_color = val;
2205 
2206 	if (fbc->active)
2207 		fbc->funcs->set_false_color(fbc, fbc->false_color);
2208 
2209 	mutex_unlock(&fbc->lock);
2210 
2211 	return 0;
2212 }
2213 
2214 DEFINE_DEBUGFS_ATTRIBUTE(intel_fbc_debugfs_false_color_fops,
2215 			 intel_fbc_debugfs_false_color_get,
2216 			 intel_fbc_debugfs_false_color_set,
2217 			 "%llu\n");
2218 
2219 static void intel_fbc_debugfs_add(struct intel_fbc *fbc,
2220 				  struct dentry *parent)
2221 {
2222 	debugfs_create_file("i915_fbc_status", 0444, parent,
2223 			    fbc, &intel_fbc_debugfs_status_fops);
2224 
2225 	if (fbc->funcs->set_false_color)
2226 		debugfs_create_file_unsafe("i915_fbc_false_color", 0644, parent,
2227 					   fbc, &intel_fbc_debugfs_false_color_fops);
2228 }
2229 
2230 void intel_fbc_crtc_debugfs_add(struct intel_crtc *crtc)
2231 {
2232 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
2233 
2234 	if (plane->fbc)
2235 		intel_fbc_debugfs_add(plane->fbc, crtc->base.debugfs_entry);
2236 }
2237 
2238 /* FIXME: remove this once igt is on board with per-crtc stuff */
2239 void intel_fbc_debugfs_register(struct intel_display *display)
2240 {
2241 	struct drm_minor *minor = display->drm->primary;
2242 	struct intel_fbc *fbc;
2243 
2244 	fbc = display->fbc[INTEL_FBC_A];
2245 	if (fbc)
2246 		intel_fbc_debugfs_add(fbc, minor->debugfs_root);
2247 }
2248