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