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