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