xref: /linux/drivers/gpu/drm/i915/display/intel_fbc.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
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 "i915_vma.h"
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 "intel_step.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 
intel_fbc_for_pipe(struct intel_display * display,enum pipe pipe)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 */
intel_fbc_plane_stride(const struct intel_plane_state * plane_state)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 
intel_fbc_cfb_cpp(const struct intel_plane_state * plane_state)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 */
intel_fbc_plane_cfb_stride(const struct intel_plane_state * plane_state)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 */
skl_fbc_min_cfb_stride(struct intel_display * display,unsigned int cpp,unsigned int width)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 (DISPLAY_VER(display) >= 11)
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 */
_intel_fbc_cfb_stride(struct intel_display * display,unsigned int cpp,unsigned int width,unsigned int stride)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 
intel_fbc_cfb_stride(const struct intel_plane_state * plane_state)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  */
intel_fbc_max_cfb_height(struct intel_display * display)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 
_intel_fbc_cfb_size(struct intel_display * display,unsigned int height,unsigned int stride)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 
intel_fbc_cfb_size(const struct intel_plane_state * plane_state)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 
intel_fbc_override_cfb_stride(const struct intel_plane_state * plane_state)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 
intel_fbc_has_fences(struct intel_display * display)279 static bool intel_fbc_has_fences(struct intel_display *display)
280 {
281 	return intel_parent_has_fenced_regions(display);
282 }
283 
i8xx_fbc_ctl(struct intel_fbc * fbc)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 
i965_fbc_ctl2(struct intel_fbc * fbc)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 
i8xx_fbc_deactivate(struct intel_fbc * fbc)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 
i8xx_fbc_activate(struct intel_fbc * fbc)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 
i8xx_fbc_is_active(struct intel_fbc * fbc)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 
i8xx_fbc_is_compressing(struct intel_fbc * fbc)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 
i8xx_fbc_nuke(struct intel_fbc * fbc)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 
i8xx_fbc_program_cfb(struct intel_fbc * fbc)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 
i965_fbc_nuke(struct intel_fbc * fbc)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 
g4x_dpfc_ctl_limit(struct intel_fbc * fbc)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 
g4x_dpfc_ctl(struct intel_fbc * fbc)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 
g4x_fbc_activate(struct intel_fbc * fbc)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 
g4x_fbc_deactivate(struct intel_fbc * fbc)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 
g4x_fbc_is_active(struct intel_fbc * fbc)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 
g4x_fbc_is_compressing(struct intel_fbc * fbc)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 
g4x_fbc_program_cfb(struct intel_fbc * fbc)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 
ilk_fbc_activate(struct intel_fbc * fbc)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 
fbc_compressor_clkgate_disable_wa(struct intel_fbc * fbc,bool disable)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 
ilk_fbc_deactivate(struct intel_fbc * fbc)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 
ilk_fbc_is_active(struct intel_fbc * fbc)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 
ilk_fbc_is_compressing(struct intel_fbc * fbc)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 
ilk_fbc_program_cfb(struct intel_fbc * fbc)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 
snb_fbc_program_fence(struct intel_fbc * fbc)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 
snb_fbc_activate(struct intel_fbc * fbc)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 
snb_fbc_nuke(struct intel_fbc * fbc)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 
glk_fbc_program_cfb_stride(struct intel_fbc * fbc)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 
skl_fbc_program_cfb_stride(struct intel_fbc * fbc)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 
ivb_dpfc_ctl(struct intel_fbc * fbc)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 
ivb_fbc_activate(struct intel_fbc * fbc)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 
ivb_fbc_is_compressing(struct intel_fbc * fbc)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 
ivb_fbc_set_false_color(struct intel_fbc * fbc,bool enable)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 
intel_fbc_hw_is_active(struct intel_fbc * fbc)730 static bool intel_fbc_hw_is_active(struct intel_fbc *fbc)
731 {
732 	return fbc->funcs->is_active(fbc);
733 }
734 
intel_fbc_hw_activate(struct intel_fbc * fbc)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 
intel_fbc_hw_deactivate(struct intel_fbc * fbc)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 
intel_fbc_is_compressing(struct intel_fbc * fbc)754 static bool intel_fbc_is_compressing(struct intel_fbc *fbc)
755 {
756 	return fbc->funcs->is_compressing(fbc);
757 }
758 
intel_fbc_nuke(struct intel_fbc * fbc)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 
intel_fbc_activate(struct intel_fbc * fbc)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 
intel_fbc_deactivate(struct intel_fbc * fbc,const char * reason)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 
intel_fbc_cfb_base_max(struct intel_display * display)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 
intel_fbc_stolen_end(struct intel_display * display)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 
intel_fbc_min_limit(const struct intel_plane_state * plane_state)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 
intel_fbc_max_limit(struct intel_display * display)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 
find_compression_limit(struct intel_fbc * fbc,unsigned int size,int min_limit)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 
intel_fbc_alloc_cfb(struct intel_fbc * fbc,unsigned int size,int min_limit)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 
intel_fbc_program_cfb(struct intel_fbc * fbc)924 static void intel_fbc_program_cfb(struct intel_fbc *fbc)
925 {
926 	fbc->funcs->program_cfb(fbc);
927 }
928 
intel_fbc_program_workarounds(struct intel_fbc * fbc)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 (IS_DISPLAY_VER(display, 11, 12))
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, 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 
fbc_sys_cache_update_config(struct intel_display * display,u32 reg,enum intel_fbc_id id)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, 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 
fbc_sys_cache_disable(const struct intel_fbc * fbc)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 
fbc_sys_cache_limit(struct intel_display * display)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 
fbc_sys_cache_enable(const struct intel_fbc * fbc)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 
__intel_fbc_cleanup_cfb(struct intel_fbc * fbc)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 
intel_fbc_cleanup(struct intel_display * display)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 
i8xx_fbc_stride_is_valid(const struct intel_plane_state * plane_state)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 
i965_fbc_stride_is_valid(const struct intel_plane_state * plane_state)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 
g4x_fbc_stride_is_valid(const struct intel_plane_state * plane_state)1089 static bool g4x_fbc_stride_is_valid(const struct intel_plane_state *plane_state)
1090 {
1091 	return true;
1092 }
1093 
skl_fbc_stride_is_valid(const struct intel_plane_state * plane_state)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 
icl_fbc_stride_is_valid(const struct intel_plane_state * plane_state)1107 static bool icl_fbc_stride_is_valid(const struct intel_plane_state *plane_state)
1108 {
1109 	return true;
1110 }
1111 
stride_is_valid(const struct intel_plane_state * plane_state)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 
i8xx_fbc_pixel_format_is_valid(const struct intel_plane_state * plane_state)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 
g4x_fbc_pixel_format_is_valid(const struct intel_plane_state * plane_state)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 
lnl_fbc_pixel_format_is_valid(const struct intel_plane_state * plane_state)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
xe3p_lpd_fbc_fp16_format_is_valid(const struct intel_plane_state * plane_state)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 
xe3p_lpd_fbc_pixel_format_is_valid(const struct intel_plane_state * plane_state)1197 static bool xe3p_lpd_fbc_pixel_format_is_valid(const struct intel_plane_state *plane_state)
1198 {
1199 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1200 
1201 	if (lnl_fbc_pixel_format_is_valid(plane_state))
1202 		return true;
1203 
1204 	if (xe3p_lpd_fbc_fp16_format_is_valid(plane_state))
1205 		return true;
1206 
1207 	switch (fb->format->format) {
1208 	case DRM_FORMAT_XRGB16161616:
1209 	case DRM_FORMAT_XBGR16161616:
1210 	case DRM_FORMAT_ARGB16161616:
1211 	case DRM_FORMAT_ABGR16161616:
1212 		return true;
1213 	default:
1214 		return false;
1215 	}
1216 }
1217 
intel_fbc_need_pixel_normalizer(const struct intel_plane_state * plane_state)1218 bool intel_fbc_need_pixel_normalizer(const struct intel_plane_state *plane_state)
1219 {
1220 	struct intel_display *display = to_intel_display(plane_state);
1221 
1222 	if (HAS_PIXEL_NORMALIZER(display) &&
1223 	    xe3p_lpd_fbc_fp16_format_is_valid(plane_state))
1224 		return true;
1225 
1226 	return false;
1227 }
1228 
pixel_format_is_valid(const struct intel_plane_state * plane_state)1229 static bool pixel_format_is_valid(const struct intel_plane_state *plane_state)
1230 {
1231 	struct intel_display *display = to_intel_display(plane_state);
1232 
1233 	if (DISPLAY_VER(display) >= 35)
1234 		return xe3p_lpd_fbc_pixel_format_is_valid(plane_state);
1235 	else if (DISPLAY_VER(display) >= 20)
1236 		return lnl_fbc_pixel_format_is_valid(plane_state);
1237 	else if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
1238 		return g4x_fbc_pixel_format_is_valid(plane_state);
1239 	else
1240 		return i8xx_fbc_pixel_format_is_valid(plane_state);
1241 }
1242 
i8xx_fbc_rotation_is_valid(const struct intel_plane_state * plane_state)1243 static bool i8xx_fbc_rotation_is_valid(const struct intel_plane_state *plane_state)
1244 {
1245 	return plane_state->hw.rotation == DRM_MODE_ROTATE_0;
1246 }
1247 
g4x_fbc_rotation_is_valid(const struct intel_plane_state * plane_state)1248 static bool g4x_fbc_rotation_is_valid(const struct intel_plane_state *plane_state)
1249 {
1250 	return true;
1251 }
1252 
skl_fbc_rotation_is_valid(const struct intel_plane_state * plane_state)1253 static bool skl_fbc_rotation_is_valid(const struct intel_plane_state *plane_state)
1254 {
1255 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1256 	unsigned int rotation = plane_state->hw.rotation;
1257 
1258 	if (fb->format->format == DRM_FORMAT_RGB565 &&
1259 	    drm_rotation_90_or_270(rotation))
1260 		return false;
1261 
1262 	return true;
1263 }
1264 
rotation_is_valid(const struct intel_plane_state * plane_state)1265 static bool rotation_is_valid(const struct intel_plane_state *plane_state)
1266 {
1267 	struct intel_display *display = to_intel_display(plane_state);
1268 
1269 	if (DISPLAY_VER(display) >= 9)
1270 		return skl_fbc_rotation_is_valid(plane_state);
1271 	else if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
1272 		return g4x_fbc_rotation_is_valid(plane_state);
1273 	else
1274 		return i8xx_fbc_rotation_is_valid(plane_state);
1275 }
1276 
intel_fbc_max_surface_size(struct intel_display * display,unsigned int * w,unsigned int * h)1277 static void intel_fbc_max_surface_size(struct intel_display *display,
1278 				       unsigned int *w, unsigned int *h)
1279 {
1280 	if (DISPLAY_VER(display) >= 11) {
1281 		*w = 8192;
1282 		*h = 4096;
1283 	} else if (DISPLAY_VER(display) >= 10) {
1284 		*w = 5120;
1285 		*h = 4096;
1286 	} else if (DISPLAY_VER(display) >= 7) {
1287 		*w = 4096;
1288 		*h = 4096;
1289 	} else if (DISPLAY_VER(display) >= 5 || display->platform.g4x) {
1290 		*w = 4096;
1291 		*h = 2048;
1292 	} else {
1293 		*w = 2048;
1294 		*h = 1536;
1295 	}
1296 }
1297 
1298 /*
1299  * For some reason, the hardware tracking starts looking at whatever we
1300  * programmed as the display plane base address register. It does not look at
1301  * the X and Y offset registers. That's why we include the src x/y offsets
1302  * instead of just looking at the plane size.
1303  */
intel_fbc_surface_size_ok(const struct intel_plane_state * plane_state)1304 static bool intel_fbc_surface_size_ok(const struct intel_plane_state *plane_state)
1305 {
1306 	struct intel_display *display = to_intel_display(plane_state);
1307 	unsigned int effective_w, effective_h, max_w, max_h;
1308 
1309 	intel_fbc_max_surface_size(display, &max_w, &max_h);
1310 
1311 	effective_w = plane_state->view.color_plane[0].x +
1312 		(drm_rect_width(&plane_state->uapi.src) >> 16);
1313 	effective_h = plane_state->view.color_plane[0].y +
1314 		(drm_rect_height(&plane_state->uapi.src) >> 16);
1315 
1316 	return effective_w <= max_w && effective_h <= max_h;
1317 }
1318 
intel_fbc_max_plane_size(struct intel_display * display,unsigned int * w,unsigned int * h)1319 static void intel_fbc_max_plane_size(struct intel_display *display,
1320 				     unsigned int *w, unsigned int *h)
1321 {
1322 	if (DISPLAY_VER(display) >= 10) {
1323 		*w = 5120;
1324 		*h = 4096;
1325 	} else if (DISPLAY_VER(display) >= 8 || display->platform.haswell) {
1326 		*w = 4096;
1327 		*h = 4096;
1328 	} else if (DISPLAY_VER(display) >= 5 || display->platform.g4x) {
1329 		*w = 4096;
1330 		*h = 2048;
1331 	} else {
1332 		*w = 2048;
1333 		*h = 1536;
1334 	}
1335 }
1336 
intel_fbc_plane_size_valid(const struct intel_plane_state * plane_state)1337 static bool intel_fbc_plane_size_valid(const struct intel_plane_state *plane_state)
1338 {
1339 	struct intel_display *display = to_intel_display(plane_state);
1340 	unsigned int w, h, max_w, max_h;
1341 
1342 	intel_fbc_max_plane_size(display, &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 
i8xx_fbc_tiling_valid(const struct intel_plane_state * plane_state)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 
skl_fbc_tiling_valid(const struct intel_plane_state * plane_state)1357 static bool skl_fbc_tiling_valid(const struct intel_plane_state *plane_state)
1358 {
1359 	return true;
1360 }
1361 
tiling_is_valid(const struct intel_plane_state * plane_state)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
intel_fbc_invalidate_dirty_rect(struct intel_fbc * fbc)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
intel_fbc_program_dirty_rect(struct intel_dsb * dsb,struct intel_fbc * fbc,const struct drm_rect * fbc_dirty_rect)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
intel_fbc_dirty_rect_update(struct intel_dsb * dsb,struct intel_fbc * fbc)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
intel_fbc_dirty_rect_update_noarm(struct intel_dsb * dsb,struct intel_plane * plane)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
intel_fbc_hw_intialize_dirty_rect(struct intel_fbc * fbc,const struct intel_plane_state * plane_state)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 
intel_fbc_update_state(struct intel_atomic_state * state,struct intel_crtc * crtc,struct intel_plane * plane)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->flags & PLANE_HAS_FENCE &&
1463 		    !intel_fbc_has_fences(display));
1464 
1465 	if (plane_state->flags & PLANE_HAS_FENCE)
1466 		fbc_state->fence_id =  i915_vma_fence_id(plane_state->ggtt_vma);
1467 	else
1468 		fbc_state->fence_id = -1;
1469 
1470 	fbc_state->cfb_stride = intel_fbc_cfb_stride(plane_state);
1471 	fbc_state->cfb_size = intel_fbc_cfb_size(plane_state);
1472 	fbc_state->override_cfb_stride = intel_fbc_override_cfb_stride(plane_state);
1473 }
1474 
intel_fbc_is_fence_ok(const struct intel_plane_state * plane_state)1475 static bool intel_fbc_is_fence_ok(const struct intel_plane_state *plane_state)
1476 {
1477 	struct intel_display *display = to_intel_display(plane_state);
1478 
1479 	/*
1480 	 * The use of a CPU fence is one of two ways to detect writes by the
1481 	 * CPU to the scanout and trigger updates to the FBC.
1482 	 *
1483 	 * The other method is by software tracking (see
1484 	 * intel_fbc_invalidate/flush()), it will manually notify FBC and nuke
1485 	 * the current compressed buffer and recompress it.
1486 	 *
1487 	 * Note that is possible for a tiled surface to be unmappable (and
1488 	 * so have no fence associated with it) due to aperture constraints
1489 	 * at the time of pinning.
1490 	 */
1491 	return DISPLAY_VER(display) >= 9 ||
1492 		(plane_state->flags & PLANE_HAS_FENCE &&
1493 		 i915_vma_fence_id(plane_state->ggtt_vma) != -1);
1494 }
1495 
intel_fbc_is_cfb_ok(const struct intel_plane_state * plane_state)1496 static bool intel_fbc_is_cfb_ok(const struct intel_plane_state *plane_state)
1497 {
1498 	struct intel_display *display = to_intel_display(plane_state);
1499 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1500 	struct intel_fbc *fbc = plane->fbc;
1501 
1502 	return intel_fbc_min_limit(plane_state) <= fbc->limit &&
1503 		intel_fbc_cfb_size(plane_state) <= fbc->limit *
1504 			intel_parent_stolen_node_size(display, fbc->compressed_fb);
1505 }
1506 
intel_fbc_is_ok(const struct intel_plane_state * plane_state)1507 static bool intel_fbc_is_ok(const struct intel_plane_state *plane_state)
1508 {
1509 	return !plane_state->no_fbc_reason &&
1510 		intel_fbc_is_fence_ok(plane_state) &&
1511 		intel_fbc_is_cfb_ok(plane_state);
1512 }
1513 
1514 static void
__intel_fbc_prepare_dirty_rect(const struct intel_plane_state * plane_state,const struct intel_crtc_state * crtc_state)1515 __intel_fbc_prepare_dirty_rect(const struct intel_plane_state *plane_state,
1516 			       const struct intel_crtc_state *crtc_state)
1517 {
1518 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1519 	struct intel_fbc *fbc = plane->fbc;
1520 	struct drm_rect *fbc_dirty_rect = &fbc->state.dirty_rect;
1521 	int width = drm_rect_width(&plane_state->uapi.src) >> 16;
1522 	const struct drm_rect *damage = &plane_state->damage;
1523 	int y_offset = plane_state->view.color_plane[0].y;
1524 
1525 	lockdep_assert_held(&fbc->lock);
1526 
1527 	if (intel_crtc_needs_modeset(crtc_state) ||
1528 	    !intel_fbc_is_ok(plane_state)) {
1529 		intel_fbc_invalidate_dirty_rect(fbc);
1530 		return;
1531 	}
1532 
1533 	if (drm_rect_visible(damage))
1534 		*fbc_dirty_rect = *damage;
1535 	else
1536 		/* dirty rect must cover at least one line */
1537 		*fbc_dirty_rect = DRM_RECT_INIT(0, y_offset, width, 1);
1538 }
1539 
1540 void
intel_fbc_prepare_dirty_rect(struct intel_atomic_state * state,struct intel_crtc * crtc)1541 intel_fbc_prepare_dirty_rect(struct intel_atomic_state *state,
1542 			     struct intel_crtc *crtc)
1543 {
1544 	struct intel_display *display = to_intel_display(state);
1545 	const struct intel_crtc_state *crtc_state =
1546 		intel_atomic_get_new_crtc_state(state, crtc);
1547 	struct intel_plane_state *plane_state;
1548 	struct intel_plane *plane;
1549 	int i;
1550 
1551 	if (!HAS_FBC_DIRTY_RECT(display))
1552 		return;
1553 
1554 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1555 		struct intel_fbc *fbc = plane->fbc;
1556 
1557 		if (!fbc || plane->pipe != crtc->pipe)
1558 			continue;
1559 
1560 		mutex_lock(&fbc->lock);
1561 
1562 		if (fbc->state.plane == plane)
1563 			__intel_fbc_prepare_dirty_rect(plane_state,
1564 						       crtc_state);
1565 
1566 		mutex_unlock(&fbc->lock);
1567 	}
1568 }
1569 
_intel_fbc_min_cdclk(const struct intel_crtc_state * crtc_state)1570 static int _intel_fbc_min_cdclk(const struct intel_crtc_state *crtc_state)
1571 {
1572 	struct intel_display *display = to_intel_display(crtc_state);
1573 
1574 	/* WaFbcExceedCdClockThreshold:hsw,bdw */
1575 	if (display->platform.haswell || display->platform.broadwell)
1576 		return DIV_ROUND_UP(crtc_state->pixel_rate * 100, 95);
1577 
1578 	/* no FBC specific limits to worry about */
1579 	return 0;
1580 }
1581 
intel_fbc_check_plane(struct intel_atomic_state * state,struct intel_plane * plane)1582 static int intel_fbc_check_plane(struct intel_atomic_state *state,
1583 				 struct intel_plane *plane)
1584 {
1585 	struct intel_display *display = to_intel_display(state);
1586 	struct intel_plane_state *plane_state =
1587 		intel_atomic_get_new_plane_state(state, plane);
1588 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1589 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
1590 	const struct intel_crtc_state *crtc_state;
1591 	struct intel_fbc *fbc = plane->fbc;
1592 
1593 	if (!fbc)
1594 		return 0;
1595 
1596 	if (!intel_parent_stolen_initialized(display)) {
1597 		plane_state->no_fbc_reason = "stolen memory not initialised";
1598 		return 0;
1599 	}
1600 
1601 	if (intel_parent_vgpu_active(display)) {
1602 		plane_state->no_fbc_reason = "VGPU active";
1603 		return 0;
1604 	}
1605 
1606 	if (!display->params.enable_fbc) {
1607 		plane_state->no_fbc_reason = "disabled per module param or by default";
1608 		return 0;
1609 	}
1610 
1611 	if (!plane_state->uapi.visible) {
1612 		plane_state->no_fbc_reason = "plane not visible";
1613 		return 0;
1614 	}
1615 
1616 	if (intel_display_wa(display, 16023588340)) {
1617 		plane_state->no_fbc_reason = "Wa_16023588340";
1618 		return 0;
1619 	}
1620 
1621 	/*
1622 	 * Wa_15018326506:
1623 	 * Fixes: Underrun during media decode
1624 	 * Workaround: Do not enable FBC
1625 	 */
1626 	if (intel_display_wa(display, 15018326506)) {
1627 		plane_state->no_fbc_reason = "Wa_15018326506";
1628 		return 0;
1629 	}
1630 
1631 	/* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
1632 	if (intel_display_vtd_active(display) &&
1633 	    (display->platform.skylake || display->platform.broxton)) {
1634 		plane_state->no_fbc_reason = "VT-d enabled";
1635 		return 0;
1636 	}
1637 
1638 	crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
1639 
1640 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
1641 		plane_state->no_fbc_reason = "interlaced mode not supported";
1642 		return 0;
1643 	}
1644 
1645 	if (crtc_state->double_wide) {
1646 		plane_state->no_fbc_reason = "double wide pipe not supported";
1647 		return 0;
1648 	}
1649 
1650 	/*
1651 	 * Display 12+ is not supporting FBC with PSR2.
1652 	 * Recommendation is to keep this combination disabled
1653 	 * Bspec: 50422 HSD: 14010260002
1654 	 *
1655 	 * TODO: Implement a logic to select between PSR2 selective fetch and
1656 	 * FBC based on Bspec: 68881 in xe2lpd onwards.
1657 	 *
1658 	 * As we still see some strange underruns in those platforms while
1659 	 * disabling PSR2, keep FBC disabled in case of selective update is on
1660 	 * until the selection logic is implemented.
1661 	 */
1662 	if (DISPLAY_VER(display) >= 12 && crtc_state->has_sel_update) {
1663 		plane_state->no_fbc_reason = "Selective update enabled";
1664 		return 0;
1665 	}
1666 
1667 	/* Wa_14016291713 */
1668 	if ((IS_DISPLAY_VER(display, 12, 13) ||
1669 	     IS_DISPLAY_VERx100_STEP(display, 1400, STEP_A0, STEP_C0)) &&
1670 	    crtc_state->has_psr && !crtc_state->has_panel_replay) {
1671 		plane_state->no_fbc_reason = "PSR1 enabled (Wa_14016291713)";
1672 		return 0;
1673 	}
1674 
1675 	if (!pixel_format_is_valid(plane_state)) {
1676 		plane_state->no_fbc_reason = "pixel format not supported";
1677 		return 0;
1678 	}
1679 
1680 	if (!tiling_is_valid(plane_state)) {
1681 		plane_state->no_fbc_reason = "tiling not supported";
1682 		return 0;
1683 	}
1684 
1685 	if (!rotation_is_valid(plane_state)) {
1686 		plane_state->no_fbc_reason = "rotation not supported";
1687 		return 0;
1688 	}
1689 
1690 	if (!stride_is_valid(plane_state)) {
1691 		plane_state->no_fbc_reason = "stride not supported";
1692 		return 0;
1693 	}
1694 
1695 	if (DISPLAY_VER(display) < 20 &&
1696 	    plane_state->hw.pixel_blend_mode != DRM_MODE_BLEND_PIXEL_NONE &&
1697 	    fb->format->has_alpha) {
1698 		plane_state->no_fbc_reason = "per-pixel alpha not supported";
1699 		return 0;
1700 	}
1701 
1702 	if (!intel_fbc_plane_size_valid(plane_state)) {
1703 		plane_state->no_fbc_reason = "plane size too big";
1704 		return 0;
1705 	}
1706 
1707 	if (!intel_fbc_surface_size_ok(plane_state)) {
1708 		plane_state->no_fbc_reason = "surface size too big";
1709 		return 0;
1710 	}
1711 
1712 	/*
1713 	 * Work around a problem on GEN9+ HW, where enabling FBC on a plane
1714 	 * having a Y offset that isn't divisible by 4 causes FIFO underrun
1715 	 * and screen flicker.
1716 	 */
1717 	if (IS_DISPLAY_VER(display, 9, 12) &&
1718 	    plane_state->view.color_plane[0].y & 3) {
1719 		plane_state->no_fbc_reason = "plane start Y offset misaligned";
1720 		return 0;
1721 	}
1722 
1723 	/* Wa_22010751166: icl, ehl, tgl, dg1, rkl */
1724 	if (IS_DISPLAY_VER(display, 9, 12) &&
1725 	    (plane_state->view.color_plane[0].y +
1726 	     (drm_rect_height(&plane_state->uapi.src) >> 16)) & 3) {
1727 		plane_state->no_fbc_reason = "plane end Y offset misaligned";
1728 		return 0;
1729 	}
1730 
1731 	if (_intel_fbc_min_cdclk(crtc_state) > display->cdclk.max_cdclk_freq) {
1732 		plane_state->no_fbc_reason = "pixel rate too high";
1733 		return 0;
1734 	}
1735 
1736 	plane_state->no_fbc_reason = NULL;
1737 
1738 	return 0;
1739 }
1740 
intel_fbc_min_cdclk(const struct intel_crtc_state * crtc_state)1741 int intel_fbc_min_cdclk(const struct intel_crtc_state *crtc_state)
1742 {
1743 	struct intel_display *display = to_intel_display(crtc_state);
1744 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1745 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1746 	int min_cdclk;
1747 
1748 	if (!plane->fbc)
1749 		return 0;
1750 
1751 	min_cdclk = _intel_fbc_min_cdclk(crtc_state);
1752 
1753 	/*
1754 	 * Do not ask for more than the max CDCLK frequency,
1755 	 * if that is not enough FBC will simply not be used.
1756 	 */
1757 	if (min_cdclk > display->cdclk.max_cdclk_freq)
1758 		return 0;
1759 
1760 	return min_cdclk;
1761 }
1762 
intel_fbc_can_flip_nuke(struct intel_atomic_state * state,struct intel_crtc * crtc,struct intel_plane * plane)1763 static bool intel_fbc_can_flip_nuke(struct intel_atomic_state *state,
1764 				    struct intel_crtc *crtc,
1765 				    struct intel_plane *plane)
1766 {
1767 	const struct intel_crtc_state *new_crtc_state =
1768 		intel_atomic_get_new_crtc_state(state, crtc);
1769 	const struct intel_plane_state *old_plane_state =
1770 		intel_atomic_get_old_plane_state(state, plane);
1771 	const struct intel_plane_state *new_plane_state =
1772 		intel_atomic_get_new_plane_state(state, plane);
1773 	const struct drm_framebuffer *old_fb = old_plane_state->hw.fb;
1774 	const struct drm_framebuffer *new_fb = new_plane_state->hw.fb;
1775 
1776 	if (intel_crtc_needs_modeset(new_crtc_state))
1777 		return false;
1778 
1779 	if (!intel_fbc_is_ok(old_plane_state) ||
1780 	    !intel_fbc_is_ok(new_plane_state))
1781 		return false;
1782 
1783 	if (old_fb->format->format != new_fb->format->format)
1784 		return false;
1785 
1786 	if (old_fb->modifier != new_fb->modifier)
1787 		return false;
1788 
1789 	if (intel_fbc_plane_stride(old_plane_state) !=
1790 	    intel_fbc_plane_stride(new_plane_state))
1791 		return false;
1792 
1793 	if (intel_fbc_cfb_stride(old_plane_state) !=
1794 	    intel_fbc_cfb_stride(new_plane_state))
1795 		return false;
1796 
1797 	if (intel_fbc_cfb_size(old_plane_state) !=
1798 	    intel_fbc_cfb_size(new_plane_state))
1799 		return false;
1800 
1801 	if (intel_fbc_override_cfb_stride(old_plane_state) !=
1802 	    intel_fbc_override_cfb_stride(new_plane_state))
1803 		return false;
1804 
1805 	return true;
1806 }
1807 
__intel_fbc_pre_update(struct intel_atomic_state * state,struct intel_crtc * crtc,struct intel_plane * plane)1808 static bool __intel_fbc_pre_update(struct intel_atomic_state *state,
1809 				   struct intel_crtc *crtc,
1810 				   struct intel_plane *plane)
1811 {
1812 	struct intel_display *display = to_intel_display(state);
1813 	struct intel_fbc *fbc = plane->fbc;
1814 	bool need_vblank_wait = false;
1815 
1816 	lockdep_assert_held(&fbc->lock);
1817 
1818 	fbc->flip_pending = true;
1819 
1820 	if (intel_fbc_can_flip_nuke(state, crtc, plane))
1821 		return need_vblank_wait;
1822 
1823 	intel_fbc_deactivate(fbc, "update pending");
1824 
1825 	/*
1826 	 * Display WA #1198: glk+
1827 	 * Need an extra vblank wait between FBC disable and most plane
1828 	 * updates. Bspec says this is only needed for plane disable, but
1829 	 * that is not true. Touching most plane registers will cause the
1830 	 * corruption to appear. Also SKL/derivatives do not seem to be
1831 	 * affected.
1832 	 *
1833 	 * TODO: could optimize this a bit by sampling the frame
1834 	 * counter when we disable FBC (if it was already done earlier)
1835 	 * and skipping the extra vblank wait before the plane update
1836 	 * if at least one frame has already passed.
1837 	 */
1838 	if (fbc->activated && DISPLAY_VER(display) >= 10)
1839 		need_vblank_wait = true;
1840 	fbc->activated = false;
1841 
1842 	return need_vblank_wait;
1843 }
1844 
intel_fbc_pre_update(struct intel_atomic_state * state,struct intel_crtc * crtc)1845 bool intel_fbc_pre_update(struct intel_atomic_state *state,
1846 			  struct intel_crtc *crtc)
1847 {
1848 	const struct intel_plane_state __maybe_unused *plane_state;
1849 	bool need_vblank_wait = false;
1850 	struct intel_plane *plane;
1851 	int i;
1852 
1853 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1854 		struct intel_fbc *fbc = plane->fbc;
1855 
1856 		if (!fbc || plane->pipe != crtc->pipe)
1857 			continue;
1858 
1859 		mutex_lock(&fbc->lock);
1860 
1861 		if (fbc->state.plane == plane)
1862 			need_vblank_wait |= __intel_fbc_pre_update(state, crtc, plane);
1863 
1864 		mutex_unlock(&fbc->lock);
1865 	}
1866 
1867 	return need_vblank_wait;
1868 }
1869 
__intel_fbc_disable(struct intel_fbc * fbc)1870 static void __intel_fbc_disable(struct intel_fbc *fbc)
1871 {
1872 	struct intel_display *display = fbc->display;
1873 	struct intel_plane *plane = fbc->state.plane;
1874 
1875 	lockdep_assert_held(&fbc->lock);
1876 	drm_WARN_ON(display->drm, fbc->active);
1877 
1878 	drm_dbg_kms(display->drm, "Disabling FBC on [PLANE:%d:%s]\n",
1879 		    plane->base.base.id, plane->base.name);
1880 
1881 	intel_fbc_invalidate_dirty_rect(fbc);
1882 
1883 	__intel_fbc_cleanup_cfb(fbc);
1884 
1885 	fbc_sys_cache_disable(fbc);
1886 
1887 	/* wa_18038517565 Enable DPFC clock gating after FBC disable */
1888 	if (display->platform.dg2 || DISPLAY_VER(display) >= 14)
1889 		fbc_compressor_clkgate_disable_wa(fbc, false);
1890 
1891 	fbc->state.plane = NULL;
1892 	fbc->flip_pending = false;
1893 	fbc->busy_bits = 0;
1894 }
1895 
__intel_fbc_post_update(struct intel_fbc * fbc)1896 static void __intel_fbc_post_update(struct intel_fbc *fbc)
1897 {
1898 	lockdep_assert_held(&fbc->lock);
1899 
1900 	fbc->flip_pending = false;
1901 	fbc->busy_bits = 0;
1902 
1903 	intel_fbc_activate(fbc);
1904 }
1905 
intel_fbc_post_update(struct intel_atomic_state * state,struct intel_crtc * crtc)1906 void intel_fbc_post_update(struct intel_atomic_state *state,
1907 			   struct intel_crtc *crtc)
1908 {
1909 	const struct intel_plane_state __maybe_unused *plane_state;
1910 	struct intel_plane *plane;
1911 	int i;
1912 
1913 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1914 		struct intel_fbc *fbc = plane->fbc;
1915 
1916 		if (!fbc || plane->pipe != crtc->pipe)
1917 			continue;
1918 
1919 		mutex_lock(&fbc->lock);
1920 
1921 		if (fbc->state.plane == plane)
1922 			__intel_fbc_post_update(fbc);
1923 
1924 		mutex_unlock(&fbc->lock);
1925 	}
1926 }
1927 
intel_fbc_get_frontbuffer_bit(struct intel_fbc * fbc)1928 static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
1929 {
1930 	if (fbc->state.plane)
1931 		return fbc->state.plane->frontbuffer_bit;
1932 	else
1933 		return 0;
1934 }
1935 
__intel_fbc_invalidate(struct intel_fbc * fbc,unsigned int frontbuffer_bits,enum fb_op_origin origin)1936 static void __intel_fbc_invalidate(struct intel_fbc *fbc,
1937 				   unsigned int frontbuffer_bits,
1938 				   enum fb_op_origin origin)
1939 {
1940 	if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1941 		return;
1942 
1943 	mutex_lock(&fbc->lock);
1944 
1945 	frontbuffer_bits &= intel_fbc_get_frontbuffer_bit(fbc);
1946 	if (!frontbuffer_bits)
1947 		goto out;
1948 
1949 	fbc->busy_bits |= frontbuffer_bits;
1950 	intel_fbc_deactivate(fbc, "frontbuffer write");
1951 
1952 out:
1953 	mutex_unlock(&fbc->lock);
1954 }
1955 
intel_fbc_invalidate(struct intel_display * display,unsigned int frontbuffer_bits,enum fb_op_origin origin)1956 void intel_fbc_invalidate(struct intel_display *display,
1957 			  unsigned int frontbuffer_bits,
1958 			  enum fb_op_origin origin)
1959 {
1960 	struct intel_fbc *fbc;
1961 	enum intel_fbc_id fbc_id;
1962 
1963 	for_each_intel_fbc(display, fbc, fbc_id)
1964 		__intel_fbc_invalidate(fbc, frontbuffer_bits, origin);
1965 
1966 }
1967 
__intel_fbc_flush(struct intel_fbc * fbc,unsigned int frontbuffer_bits,enum fb_op_origin origin)1968 static void __intel_fbc_flush(struct intel_fbc *fbc,
1969 			      unsigned int frontbuffer_bits,
1970 			      enum fb_op_origin origin)
1971 {
1972 	mutex_lock(&fbc->lock);
1973 
1974 	frontbuffer_bits &= intel_fbc_get_frontbuffer_bit(fbc);
1975 	if (!frontbuffer_bits)
1976 		goto out;
1977 
1978 	fbc->busy_bits &= ~frontbuffer_bits;
1979 
1980 	if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1981 		goto out;
1982 
1983 	if (fbc->busy_bits || fbc->flip_pending)
1984 		goto out;
1985 
1986 	if (fbc->active)
1987 		intel_fbc_nuke(fbc);
1988 	else
1989 		intel_fbc_activate(fbc);
1990 
1991 out:
1992 	mutex_unlock(&fbc->lock);
1993 }
1994 
intel_fbc_flush(struct intel_display * display,unsigned int frontbuffer_bits,enum fb_op_origin origin)1995 void intel_fbc_flush(struct intel_display *display,
1996 		     unsigned int frontbuffer_bits,
1997 		     enum fb_op_origin origin)
1998 {
1999 	struct intel_fbc *fbc;
2000 	enum intel_fbc_id fbc_id;
2001 
2002 	for_each_intel_fbc(display, fbc, fbc_id)
2003 		__intel_fbc_flush(fbc, frontbuffer_bits, origin);
2004 }
2005 
intel_fbc_atomic_check(struct intel_atomic_state * state)2006 int intel_fbc_atomic_check(struct intel_atomic_state *state)
2007 {
2008 	struct intel_plane_state __maybe_unused *plane_state;
2009 	struct intel_plane *plane;
2010 	int i;
2011 
2012 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2013 		int ret;
2014 
2015 		ret = intel_fbc_check_plane(state, plane);
2016 		if (ret)
2017 			return ret;
2018 	}
2019 
2020 	return 0;
2021 }
2022 
__intel_fbc_enable(struct intel_atomic_state * state,struct intel_crtc * crtc,struct intel_plane * plane)2023 static void __intel_fbc_enable(struct intel_atomic_state *state,
2024 			       struct intel_crtc *crtc,
2025 			       struct intel_plane *plane)
2026 {
2027 	struct intel_display *display = to_intel_display(state);
2028 	const struct intel_plane_state *plane_state =
2029 		intel_atomic_get_new_plane_state(state, plane);
2030 	struct intel_fbc *fbc = plane->fbc;
2031 
2032 	lockdep_assert_held(&fbc->lock);
2033 
2034 	if (fbc->state.plane) {
2035 		if (fbc->state.plane != plane)
2036 			return;
2037 
2038 		if (intel_fbc_is_ok(plane_state)) {
2039 			intel_fbc_update_state(state, crtc, plane);
2040 			return;
2041 		}
2042 
2043 		__intel_fbc_disable(fbc);
2044 	}
2045 
2046 	drm_WARN_ON(display->drm, fbc->active);
2047 
2048 	fbc->no_fbc_reason = plane_state->no_fbc_reason;
2049 	if (fbc->no_fbc_reason)
2050 		return;
2051 
2052 	if (!intel_fbc_is_fence_ok(plane_state)) {
2053 		fbc->no_fbc_reason = "framebuffer not fenced";
2054 		return;
2055 	}
2056 
2057 	if (fbc->underrun_detected) {
2058 		fbc->no_fbc_reason = "FIFO underrun";
2059 		return;
2060 	}
2061 
2062 	if (intel_fbc_alloc_cfb(fbc, intel_fbc_cfb_size(plane_state),
2063 				intel_fbc_min_limit(plane_state))) {
2064 		fbc->no_fbc_reason = "not enough stolen memory";
2065 		return;
2066 	}
2067 
2068 	drm_dbg_kms(display->drm, "Enabling FBC on [PLANE:%d:%s]\n",
2069 		    plane->base.base.id, plane->base.name);
2070 	fbc->no_fbc_reason = "FBC enabled but not active yet\n";
2071 
2072 	intel_fbc_update_state(state, crtc, plane);
2073 
2074 	if (HAS_FBC_DIRTY_RECT(display))
2075 		intel_fbc_hw_intialize_dirty_rect(fbc, plane_state);
2076 
2077 	intel_fbc_program_workarounds(fbc);
2078 	intel_fbc_program_cfb(fbc);
2079 
2080 	fbc_sys_cache_enable(fbc);
2081 }
2082 
2083 /**
2084  * intel_fbc_disable - disable FBC if it's associated with crtc
2085  * @crtc: the CRTC
2086  *
2087  * This function disables FBC if it's associated with the provided CRTC.
2088  */
intel_fbc_disable(struct intel_crtc * crtc)2089 void intel_fbc_disable(struct intel_crtc *crtc)
2090 {
2091 	struct intel_display *display = to_intel_display(crtc);
2092 	struct intel_plane *plane;
2093 
2094 	for_each_intel_plane(display->drm, plane) {
2095 		struct intel_fbc *fbc = plane->fbc;
2096 
2097 		if (!fbc || plane->pipe != crtc->pipe)
2098 			continue;
2099 
2100 		mutex_lock(&fbc->lock);
2101 		if (fbc->state.plane == plane)
2102 			__intel_fbc_disable(fbc);
2103 		mutex_unlock(&fbc->lock);
2104 	}
2105 }
2106 
intel_fbc_update(struct intel_atomic_state * state,struct intel_crtc * crtc)2107 void intel_fbc_update(struct intel_atomic_state *state,
2108 		      struct intel_crtc *crtc)
2109 {
2110 	const struct intel_crtc_state *crtc_state =
2111 		intel_atomic_get_new_crtc_state(state, crtc);
2112 	const struct intel_plane_state *plane_state;
2113 	struct intel_plane *plane;
2114 	int i;
2115 
2116 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2117 		struct intel_fbc *fbc = plane->fbc;
2118 
2119 		if (!fbc || plane->pipe != crtc->pipe)
2120 			continue;
2121 
2122 		mutex_lock(&fbc->lock);
2123 
2124 		if (intel_crtc_needs_fastset(crtc_state) &&
2125 		    plane_state->no_fbc_reason) {
2126 			if (fbc->state.plane == plane)
2127 				__intel_fbc_disable(fbc);
2128 		} else {
2129 			__intel_fbc_enable(state, crtc, plane);
2130 		}
2131 
2132 		mutex_unlock(&fbc->lock);
2133 	}
2134 }
2135 
intel_fbc_underrun_work_fn(struct work_struct * work)2136 static void intel_fbc_underrun_work_fn(struct work_struct *work)
2137 {
2138 	struct intel_fbc *fbc = container_of(work, typeof(*fbc), underrun_work);
2139 	struct intel_display *display = fbc->display;
2140 
2141 	mutex_lock(&fbc->lock);
2142 
2143 	/* Maybe we were scheduled twice. */
2144 	if (fbc->underrun_detected || !fbc->state.plane)
2145 		goto out;
2146 
2147 	drm_dbg_kms(display->drm, "Disabling FBC due to FIFO underrun.\n");
2148 	fbc->underrun_detected = true;
2149 
2150 	intel_fbc_deactivate(fbc, "FIFO underrun");
2151 	if (!fbc->flip_pending)
2152 		intel_crtc_wait_for_next_vblank(intel_crtc_for_pipe(display, fbc->state.plane->pipe));
2153 	__intel_fbc_disable(fbc);
2154 out:
2155 	mutex_unlock(&fbc->lock);
2156 }
2157 
__intel_fbc_reset_underrun(struct intel_fbc * fbc)2158 static void __intel_fbc_reset_underrun(struct intel_fbc *fbc)
2159 {
2160 	struct intel_display *display = fbc->display;
2161 
2162 	cancel_work_sync(&fbc->underrun_work);
2163 
2164 	mutex_lock(&fbc->lock);
2165 
2166 	if (fbc->underrun_detected) {
2167 		drm_dbg_kms(display->drm,
2168 			    "Re-allowing FBC after fifo underrun\n");
2169 		fbc->no_fbc_reason = "FIFO underrun cleared";
2170 	}
2171 
2172 	fbc->underrun_detected = false;
2173 	mutex_unlock(&fbc->lock);
2174 }
2175 
2176 /*
2177  * intel_fbc_reset_underrun - reset FBC fifo underrun status.
2178  * @display: display
2179  *
2180  * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we
2181  * want to re-enable FBC after an underrun to increase test coverage.
2182  */
intel_fbc_reset_underrun(struct intel_display * display)2183 void intel_fbc_reset_underrun(struct intel_display *display)
2184 {
2185 	struct intel_fbc *fbc;
2186 	enum intel_fbc_id fbc_id;
2187 
2188 	for_each_intel_fbc(display, fbc, fbc_id)
2189 		__intel_fbc_reset_underrun(fbc);
2190 }
2191 
__intel_fbc_handle_fifo_underrun_irq(struct intel_fbc * fbc)2192 static void __intel_fbc_handle_fifo_underrun_irq(struct intel_fbc *fbc)
2193 {
2194 	struct intel_display *display = fbc->display;
2195 
2196 	/*
2197 	 * There's no guarantee that underrun_detected won't be set to true
2198 	 * right after this check and before the work is scheduled, but that's
2199 	 * not a problem since we'll check it again under the work function
2200 	 * while FBC is locked. This check here is just to prevent us from
2201 	 * unnecessarily scheduling the work, and it relies on the fact that we
2202 	 * never switch underrun_detect back to false after it's true.
2203 	 */
2204 	if (READ_ONCE(fbc->underrun_detected))
2205 		return;
2206 
2207 	queue_work(display->wq.unordered, &fbc->underrun_work);
2208 }
2209 
2210 /**
2211  * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
2212  * @display: display
2213  *
2214  * Without FBC, most underruns are harmless and don't really cause too many
2215  * problems, except for an annoying message on dmesg. With FBC, underruns can
2216  * become black screens or even worse, especially when paired with bad
2217  * watermarks. So in order for us to be on the safe side, completely disable FBC
2218  * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
2219  * already suggests that watermarks may be bad, so try to be as safe as
2220  * possible.
2221  *
2222  * This function is called from the IRQ handler.
2223  */
intel_fbc_handle_fifo_underrun_irq(struct intel_display * display)2224 void intel_fbc_handle_fifo_underrun_irq(struct intel_display *display)
2225 {
2226 	struct intel_fbc *fbc;
2227 	enum intel_fbc_id fbc_id;
2228 
2229 	for_each_intel_fbc(display, fbc, fbc_id)
2230 		__intel_fbc_handle_fifo_underrun_irq(fbc);
2231 }
2232 
2233 /**
2234  * intel_fbc_read_underrun_dbg_info - Read and log FBC-related FIFO underrun debug info
2235  * @display: display device instance
2236  * @pipe: the pipe possibly containing the FBC
2237  * @log: log the info?
2238  *
2239  * If @pipe does not contain an FBC instance, this function bails early.
2240  * Otherwise, FBC-related FIFO underrun is read and cleared, and then, if @log
2241  * is true, printed with error level.
2242  */
intel_fbc_read_underrun_dbg_info(struct intel_display * display,enum pipe pipe,bool log)2243 void intel_fbc_read_underrun_dbg_info(struct intel_display *display,
2244 				      enum pipe pipe, bool log)
2245 {
2246 	struct intel_fbc *fbc = intel_fbc_for_pipe(display, pipe);
2247 	u32 val;
2248 
2249 	if (!fbc)
2250 		return;
2251 
2252 	val = intel_de_read(display, FBC_DEBUG_STATUS(fbc->id));
2253 	if (!(val & FBC_UNDERRUN_DECMPR))
2254 		return;
2255 
2256 	intel_de_write(display, FBC_DEBUG_STATUS(fbc->id), FBC_UNDERRUN_DECMPR);
2257 
2258 	if (log)
2259 		drm_err(display->drm,
2260 			"Pipe %c FIFO underrun info: FBC decompressing\n",
2261 			pipe_name(pipe));
2262 }
2263 
2264 /*
2265  * The DDX driver changes its behavior depending on the value it reads from
2266  * i915.enable_fbc, so sanitize it by translating the default value into either
2267  * 0 or 1 in order to allow it to know what's going on.
2268  *
2269  * Notice that this is done at driver initialization and we still allow user
2270  * space to change the value during runtime without sanitizing it again. IGT
2271  * relies on being able to change i915.enable_fbc at runtime.
2272  */
intel_sanitize_fbc_option(struct intel_display * display)2273 static int intel_sanitize_fbc_option(struct intel_display *display)
2274 {
2275 	if (display->params.enable_fbc >= 0)
2276 		return !!display->params.enable_fbc;
2277 
2278 	if (!HAS_FBC(display))
2279 		return 0;
2280 
2281 	if (display->platform.broadwell || DISPLAY_VER(display) >= 9)
2282 		return 1;
2283 
2284 	return 0;
2285 }
2286 
intel_fbc_add_plane(struct intel_fbc * fbc,struct intel_plane * plane)2287 void intel_fbc_add_plane(struct intel_fbc *fbc, struct intel_plane *plane)
2288 {
2289 	plane->fbc = fbc;
2290 }
2291 
intel_fbc_create(struct intel_display * display,enum intel_fbc_id fbc_id)2292 static struct intel_fbc *intel_fbc_create(struct intel_display *display,
2293 					  enum intel_fbc_id fbc_id)
2294 {
2295 	struct intel_fbc *fbc;
2296 
2297 	fbc = kzalloc_obj(*fbc);
2298 	if (!fbc)
2299 		return NULL;
2300 
2301 	fbc->compressed_fb = intel_parent_stolen_node_alloc(display);
2302 	if (!fbc->compressed_fb)
2303 		goto err;
2304 	fbc->compressed_llb = intel_parent_stolen_node_alloc(display);
2305 	if (!fbc->compressed_llb)
2306 		goto err;
2307 
2308 	fbc->id = fbc_id;
2309 	fbc->display = display;
2310 	INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
2311 	mutex_init(&fbc->lock);
2312 
2313 	if (DISPLAY_VER(display) >= 7)
2314 		fbc->funcs = &ivb_fbc_funcs;
2315 	else if (DISPLAY_VER(display) == 6)
2316 		fbc->funcs = &snb_fbc_funcs;
2317 	else if (DISPLAY_VER(display) == 5)
2318 		fbc->funcs = &ilk_fbc_funcs;
2319 	else if (display->platform.g4x)
2320 		fbc->funcs = &g4x_fbc_funcs;
2321 	else if (DISPLAY_VER(display) == 4)
2322 		fbc->funcs = &i965_fbc_funcs;
2323 	else
2324 		fbc->funcs = &i8xx_fbc_funcs;
2325 
2326 	return fbc;
2327 
2328 err:
2329 	intel_parent_stolen_node_free(display, fbc->compressed_llb);
2330 	intel_parent_stolen_node_free(display, fbc->compressed_fb);
2331 	kfree(fbc);
2332 
2333 	return NULL;
2334 }
2335 
2336 /**
2337  * intel_fbc_init - Initialize FBC
2338  * @display: display
2339  *
2340  * This function might be called during PM init process.
2341  */
intel_fbc_init(struct intel_display * display)2342 void intel_fbc_init(struct intel_display *display)
2343 {
2344 	enum intel_fbc_id fbc_id;
2345 
2346 	display->params.enable_fbc = intel_sanitize_fbc_option(display);
2347 	drm_dbg_kms(display->drm, "Sanitized enable_fbc value: %d\n",
2348 		    display->params.enable_fbc);
2349 
2350 	for_each_fbc_id(display, fbc_id)
2351 		display->fbc.instances[fbc_id] = intel_fbc_create(display, fbc_id);
2352 
2353 	mutex_init(&display->fbc.sys_cache.lock);
2354 	display->fbc.sys_cache.id = FBC_SYS_CACHE_ID_NONE;
2355 }
2356 
2357 /**
2358  * intel_fbc_sanitize - Sanitize FBC
2359  * @display: display
2360  *
2361  * Make sure FBC is initially disabled since we have no
2362  * idea eg. into which parts of stolen it might be scribbling
2363  * into.
2364  */
intel_fbc_sanitize(struct intel_display * display)2365 void intel_fbc_sanitize(struct intel_display *display)
2366 {
2367 	struct intel_fbc *fbc;
2368 	enum intel_fbc_id fbc_id;
2369 
2370 	for_each_intel_fbc(display, fbc, fbc_id) {
2371 		if (intel_fbc_hw_is_active(fbc))
2372 			intel_fbc_hw_deactivate(fbc);
2373 	}
2374 
2375 	/* Ensure the sys cache usage config is clear as well */
2376 	mutex_lock(&display->fbc.sys_cache.lock);
2377 	fbc_sys_cache_update_config(display, 0, FBC_SYS_CACHE_ID_NONE);
2378 	mutex_unlock(&display->fbc.sys_cache.lock);
2379 }
2380 
intel_fbc_debugfs_status_show(struct seq_file * m,void * unused)2381 static int intel_fbc_debugfs_status_show(struct seq_file *m, void *unused)
2382 {
2383 	struct intel_fbc *fbc = m->private;
2384 	struct intel_display *display = fbc->display;
2385 	struct intel_plane *plane;
2386 	struct ref_tracker *wakeref;
2387 
2388 	drm_modeset_lock_all(display->drm);
2389 
2390 	wakeref = intel_display_rpm_get(display);
2391 	mutex_lock(&fbc->lock);
2392 
2393 	if (fbc->active) {
2394 		seq_puts(m, "FBC enabled\n");
2395 		seq_printf(m, "Compressing: %s\n",
2396 			   str_yes_no(intel_fbc_is_compressing(fbc)));
2397 
2398 		mutex_lock(&display->fbc.sys_cache.lock);
2399 		seq_printf(m, "Using system cache: %s\n",
2400 			   str_yes_no(display->fbc.sys_cache.id == fbc->id));
2401 		mutex_unlock(&display->fbc.sys_cache.lock);
2402 	} else {
2403 		seq_printf(m, "FBC disabled: %s\n", fbc->no_fbc_reason);
2404 	}
2405 
2406 	for_each_intel_plane(display->drm, plane) {
2407 		const struct intel_plane_state *plane_state =
2408 			to_intel_plane_state(plane->base.state);
2409 
2410 		if (plane->fbc != fbc)
2411 			continue;
2412 
2413 		seq_printf(m, "%c [PLANE:%d:%s]: %s\n",
2414 			   fbc->state.plane == plane ? '*' : ' ',
2415 			   plane->base.base.id, plane->base.name,
2416 			   plane_state->no_fbc_reason ?: "FBC possible");
2417 	}
2418 
2419 	mutex_unlock(&fbc->lock);
2420 	intel_display_rpm_put(display, wakeref);
2421 
2422 	drm_modeset_unlock_all(display->drm);
2423 
2424 	return 0;
2425 }
2426 
2427 DEFINE_SHOW_ATTRIBUTE(intel_fbc_debugfs_status);
2428 
intel_fbc_debugfs_false_color_get(void * data,u64 * val)2429 static int intel_fbc_debugfs_false_color_get(void *data, u64 *val)
2430 {
2431 	struct intel_fbc *fbc = data;
2432 
2433 	*val = fbc->false_color;
2434 
2435 	return 0;
2436 }
2437 
intel_fbc_debugfs_false_color_set(void * data,u64 val)2438 static int intel_fbc_debugfs_false_color_set(void *data, u64 val)
2439 {
2440 	struct intel_fbc *fbc = data;
2441 
2442 	mutex_lock(&fbc->lock);
2443 
2444 	fbc->false_color = val;
2445 
2446 	if (fbc->active)
2447 		fbc->funcs->set_false_color(fbc, fbc->false_color);
2448 
2449 	mutex_unlock(&fbc->lock);
2450 
2451 	return 0;
2452 }
2453 
2454 DEFINE_DEBUGFS_ATTRIBUTE(intel_fbc_debugfs_false_color_fops,
2455 			 intel_fbc_debugfs_false_color_get,
2456 			 intel_fbc_debugfs_false_color_set,
2457 			 "%llu\n");
2458 
intel_fbc_debugfs_add(struct intel_fbc * fbc,struct dentry * parent)2459 static void intel_fbc_debugfs_add(struct intel_fbc *fbc,
2460 				  struct dentry *parent)
2461 {
2462 	debugfs_create_file("i915_fbc_status", 0444, parent,
2463 			    fbc, &intel_fbc_debugfs_status_fops);
2464 
2465 	if (fbc->funcs->set_false_color)
2466 		debugfs_create_file_unsafe("i915_fbc_false_color", 0644, parent,
2467 					   fbc, &intel_fbc_debugfs_false_color_fops);
2468 }
2469 
intel_fbc_crtc_debugfs_add(struct intel_crtc * crtc)2470 void intel_fbc_crtc_debugfs_add(struct intel_crtc *crtc)
2471 {
2472 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
2473 
2474 	if (plane->fbc)
2475 		intel_fbc_debugfs_add(plane->fbc, crtc->base.debugfs_entry);
2476 }
2477 
2478 /* FIXME: remove this once igt is on board with per-crtc stuff */
intel_fbc_debugfs_register(struct intel_display * display)2479 void intel_fbc_debugfs_register(struct intel_display *display)
2480 {
2481 	struct intel_fbc *fbc;
2482 
2483 	fbc = display->fbc.instances[INTEL_FBC_A];
2484 	if (fbc)
2485 		intel_fbc_debugfs_add(fbc, display->drm->debugfs_root);
2486 }
2487