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