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