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