1 /* $NetBSD: intel_fbc.c,v 1.3 2021/12/19 12:32:15 riastradh Exp $ */ 2 3 /* 4 * Copyright 2014 Intel Corporation 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 */ 25 26 /** 27 * DOC: Frame Buffer Compression (FBC) 28 * 29 * FBC tries to save memory bandwidth (and so power consumption) by 30 * compressing the amount of memory used by the display. It is total 31 * transparent to user space and completely handled in the kernel. 32 * 33 * The benefits of FBC are mostly visible with solid backgrounds and 34 * variation-less patterns. It comes from keeping the memory footprint small 35 * and having fewer memory pages opened and accessed for refreshing the display. 36 * 37 * i915 is responsible to reserve stolen memory for FBC and configure its 38 * offset on proper registers. The hardware takes care of all 39 * compress/decompress. However there are many known cases where we have to 40 * forcibly disable it to allow proper screen updates. 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: intel_fbc.c,v 1.3 2021/12/19 12:32:15 riastradh Exp $"); 45 46 #include <drm/drm_fourcc.h> 47 48 #include "i915_drv.h" 49 #include "intel_display_types.h" 50 #include "intel_fbc.h" 51 #include "intel_frontbuffer.h" 52 53 #include <linux/nbsd-namespace.h> 54 55 static inline bool fbc_supported(struct drm_i915_private *dev_priv) 56 { 57 return HAS_FBC(dev_priv); 58 } 59 60 /* 61 * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the 62 * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's 63 * origin so the x and y offsets can actually fit the registers. As a 64 * consequence, the fence doesn't really start exactly at the display plane 65 * address we program because it starts at the real start of the buffer, so we 66 * have to take this into consideration here. 67 */ 68 static unsigned int get_crtc_fence_y_offset(struct intel_fbc *fbc) 69 { 70 return fbc->state_cache.plane.y - fbc->state_cache.plane.adjusted_y; 71 } 72 73 /* 74 * For SKL+, the plane source size used by the hardware is based on the value we 75 * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value 76 * we wrote to PIPESRC. 77 */ 78 static void intel_fbc_get_plane_source_size(const struct intel_fbc_state_cache *cache, 79 int *width, int *height) 80 { 81 if (width) 82 *width = cache->plane.src_w; 83 if (height) 84 *height = cache->plane.src_h; 85 } 86 87 static int intel_fbc_calculate_cfb_size(struct drm_i915_private *dev_priv, 88 const struct intel_fbc_state_cache *cache) 89 { 90 int lines; 91 92 intel_fbc_get_plane_source_size(cache, NULL, &lines); 93 if (IS_GEN(dev_priv, 7)) 94 lines = min(lines, 2048); 95 else if (INTEL_GEN(dev_priv) >= 8) 96 lines = min(lines, 2560); 97 98 /* Hardware needs the full buffer stride, not just the active area. */ 99 return lines * cache->fb.stride; 100 } 101 102 static void i8xx_fbc_deactivate(struct drm_i915_private *dev_priv) 103 { 104 u32 fbc_ctl; 105 106 /* Disable compression */ 107 fbc_ctl = I915_READ(FBC_CONTROL); 108 if ((fbc_ctl & FBC_CTL_EN) == 0) 109 return; 110 111 fbc_ctl &= ~FBC_CTL_EN; 112 I915_WRITE(FBC_CONTROL, fbc_ctl); 113 114 /* Wait for compressing bit to clear */ 115 if (intel_de_wait_for_clear(dev_priv, FBC_STATUS, 116 FBC_STAT_COMPRESSING, 10)) { 117 DRM_DEBUG_KMS("FBC idle timed out\n"); 118 return; 119 } 120 } 121 122 static void i8xx_fbc_activate(struct drm_i915_private *dev_priv) 123 { 124 struct intel_fbc_reg_params *params = &dev_priv->fbc.params; 125 int cfb_pitch; 126 int i; 127 u32 fbc_ctl; 128 129 /* Note: fbc.threshold == 1 for i8xx */ 130 cfb_pitch = params->cfb_size / FBC_LL_SIZE; 131 if (params->fb.stride < cfb_pitch) 132 cfb_pitch = params->fb.stride; 133 134 /* FBC_CTL wants 32B or 64B units */ 135 if (IS_GEN(dev_priv, 2)) 136 cfb_pitch = (cfb_pitch / 32) - 1; 137 else 138 cfb_pitch = (cfb_pitch / 64) - 1; 139 140 /* Clear old tags */ 141 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++) 142 I915_WRITE(FBC_TAG(i), 0); 143 144 if (IS_GEN(dev_priv, 4)) { 145 u32 fbc_ctl2; 146 147 /* Set it up... */ 148 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM; 149 fbc_ctl2 |= FBC_CTL_PLANE(params->crtc.i9xx_plane); 150 if (params->fence_id >= 0) 151 fbc_ctl2 |= FBC_CTL_CPU_FENCE; 152 I915_WRITE(FBC_CONTROL2, fbc_ctl2); 153 I915_WRITE(FBC_FENCE_OFF, params->crtc.fence_y_offset); 154 } 155 156 /* enable it... */ 157 fbc_ctl = I915_READ(FBC_CONTROL); 158 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT; 159 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC; 160 if (IS_I945GM(dev_priv)) 161 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */ 162 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT; 163 if (params->fence_id >= 0) 164 fbc_ctl |= params->fence_id; 165 I915_WRITE(FBC_CONTROL, fbc_ctl); 166 } 167 168 static bool i8xx_fbc_is_active(struct drm_i915_private *dev_priv) 169 { 170 return I915_READ(FBC_CONTROL) & FBC_CTL_EN; 171 } 172 173 static void g4x_fbc_activate(struct drm_i915_private *dev_priv) 174 { 175 struct intel_fbc_reg_params *params = &dev_priv->fbc.params; 176 u32 dpfc_ctl; 177 178 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.i9xx_plane) | DPFC_SR_EN; 179 if (params->fb.format->cpp[0] == 2) 180 dpfc_ctl |= DPFC_CTL_LIMIT_2X; 181 else 182 dpfc_ctl |= DPFC_CTL_LIMIT_1X; 183 184 if (params->fence_id >= 0) { 185 dpfc_ctl |= DPFC_CTL_FENCE_EN | params->fence_id; 186 I915_WRITE(DPFC_FENCE_YOFF, params->crtc.fence_y_offset); 187 } else { 188 I915_WRITE(DPFC_FENCE_YOFF, 0); 189 } 190 191 /* enable it... */ 192 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN); 193 } 194 195 static void g4x_fbc_deactivate(struct drm_i915_private *dev_priv) 196 { 197 u32 dpfc_ctl; 198 199 /* Disable compression */ 200 dpfc_ctl = I915_READ(DPFC_CONTROL); 201 if (dpfc_ctl & DPFC_CTL_EN) { 202 dpfc_ctl &= ~DPFC_CTL_EN; 203 I915_WRITE(DPFC_CONTROL, dpfc_ctl); 204 } 205 } 206 207 static bool g4x_fbc_is_active(struct drm_i915_private *dev_priv) 208 { 209 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN; 210 } 211 212 /* This function forces a CFB recompression through the nuke operation. */ 213 static void intel_fbc_recompress(struct drm_i915_private *dev_priv) 214 { 215 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE); 216 POSTING_READ(MSG_FBC_REND_STATE); 217 } 218 219 static void ilk_fbc_activate(struct drm_i915_private *dev_priv) 220 { 221 struct intel_fbc_reg_params *params = &dev_priv->fbc.params; 222 u32 dpfc_ctl; 223 int threshold = dev_priv->fbc.threshold; 224 225 dpfc_ctl = DPFC_CTL_PLANE(params->crtc.i9xx_plane); 226 if (params->fb.format->cpp[0] == 2) 227 threshold++; 228 229 switch (threshold) { 230 case 4: 231 case 3: 232 dpfc_ctl |= DPFC_CTL_LIMIT_4X; 233 break; 234 case 2: 235 dpfc_ctl |= DPFC_CTL_LIMIT_2X; 236 break; 237 case 1: 238 dpfc_ctl |= DPFC_CTL_LIMIT_1X; 239 break; 240 } 241 242 if (params->fence_id >= 0) { 243 dpfc_ctl |= DPFC_CTL_FENCE_EN; 244 if (IS_GEN(dev_priv, 5)) 245 dpfc_ctl |= params->fence_id; 246 if (IS_GEN(dev_priv, 6)) { 247 I915_WRITE(SNB_DPFC_CTL_SA, 248 SNB_CPU_FENCE_ENABLE | 249 params->fence_id); 250 I915_WRITE(DPFC_CPU_FENCE_OFFSET, 251 params->crtc.fence_y_offset); 252 } 253 } else { 254 if (IS_GEN(dev_priv, 6)) { 255 I915_WRITE(SNB_DPFC_CTL_SA, 0); 256 I915_WRITE(DPFC_CPU_FENCE_OFFSET, 0); 257 } 258 } 259 260 I915_WRITE(ILK_DPFC_FENCE_YOFF, params->crtc.fence_y_offset); 261 /* enable it... */ 262 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN); 263 264 intel_fbc_recompress(dev_priv); 265 } 266 267 static void ilk_fbc_deactivate(struct drm_i915_private *dev_priv) 268 { 269 u32 dpfc_ctl; 270 271 /* Disable compression */ 272 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL); 273 if (dpfc_ctl & DPFC_CTL_EN) { 274 dpfc_ctl &= ~DPFC_CTL_EN; 275 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl); 276 } 277 } 278 279 static bool ilk_fbc_is_active(struct drm_i915_private *dev_priv) 280 { 281 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN; 282 } 283 284 static void gen7_fbc_activate(struct drm_i915_private *dev_priv) 285 { 286 struct intel_fbc_reg_params *params = &dev_priv->fbc.params; 287 u32 dpfc_ctl; 288 int threshold = dev_priv->fbc.threshold; 289 290 /* Display WA #0529: skl, kbl, bxt. */ 291 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) { 292 u32 val = I915_READ(CHICKEN_MISC_4); 293 294 val &= ~(FBC_STRIDE_OVERRIDE | FBC_STRIDE_MASK); 295 296 if (params->gen9_wa_cfb_stride) 297 val |= FBC_STRIDE_OVERRIDE | params->gen9_wa_cfb_stride; 298 299 I915_WRITE(CHICKEN_MISC_4, val); 300 } 301 302 dpfc_ctl = 0; 303 if (IS_IVYBRIDGE(dev_priv)) 304 dpfc_ctl |= IVB_DPFC_CTL_PLANE(params->crtc.i9xx_plane); 305 306 if (params->fb.format->cpp[0] == 2) 307 threshold++; 308 309 switch (threshold) { 310 case 4: 311 case 3: 312 dpfc_ctl |= DPFC_CTL_LIMIT_4X; 313 break; 314 case 2: 315 dpfc_ctl |= DPFC_CTL_LIMIT_2X; 316 break; 317 case 1: 318 dpfc_ctl |= DPFC_CTL_LIMIT_1X; 319 break; 320 } 321 322 if (params->fence_id >= 0) { 323 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN; 324 I915_WRITE(SNB_DPFC_CTL_SA, 325 SNB_CPU_FENCE_ENABLE | 326 params->fence_id); 327 I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset); 328 } else { 329 I915_WRITE(SNB_DPFC_CTL_SA,0); 330 I915_WRITE(DPFC_CPU_FENCE_OFFSET, 0); 331 } 332 333 if (dev_priv->fbc.false_color) 334 dpfc_ctl |= FBC_CTL_FALSE_COLOR; 335 336 if (IS_IVYBRIDGE(dev_priv)) { 337 /* WaFbcAsynchFlipDisableFbcQueue:ivb */ 338 I915_WRITE(ILK_DISPLAY_CHICKEN1, 339 I915_READ(ILK_DISPLAY_CHICKEN1) | 340 ILK_FBCQ_DIS); 341 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) { 342 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */ 343 I915_WRITE(CHICKEN_PIPESL_1(params->crtc.pipe), 344 I915_READ(CHICKEN_PIPESL_1(params->crtc.pipe)) | 345 HSW_FBCQ_DIS); 346 } 347 348 if (INTEL_GEN(dev_priv) >= 11) 349 /* Wa_1409120013:icl,ehl,tgl */ 350 I915_WRITE(ILK_DPFC_CHICKEN, ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL); 351 352 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN); 353 354 intel_fbc_recompress(dev_priv); 355 } 356 357 static bool intel_fbc_hw_is_active(struct drm_i915_private *dev_priv) 358 { 359 if (INTEL_GEN(dev_priv) >= 5) 360 return ilk_fbc_is_active(dev_priv); 361 else if (IS_GM45(dev_priv)) 362 return g4x_fbc_is_active(dev_priv); 363 else 364 return i8xx_fbc_is_active(dev_priv); 365 } 366 367 static void intel_fbc_hw_activate(struct drm_i915_private *dev_priv) 368 { 369 struct intel_fbc *fbc = &dev_priv->fbc; 370 371 fbc->active = true; 372 fbc->activated = true; 373 374 if (INTEL_GEN(dev_priv) >= 7) 375 gen7_fbc_activate(dev_priv); 376 else if (INTEL_GEN(dev_priv) >= 5) 377 ilk_fbc_activate(dev_priv); 378 else if (IS_GM45(dev_priv)) 379 g4x_fbc_activate(dev_priv); 380 else 381 i8xx_fbc_activate(dev_priv); 382 } 383 384 static void intel_fbc_hw_deactivate(struct drm_i915_private *dev_priv) 385 { 386 struct intel_fbc *fbc = &dev_priv->fbc; 387 388 fbc->active = false; 389 390 if (INTEL_GEN(dev_priv) >= 5) 391 ilk_fbc_deactivate(dev_priv); 392 else if (IS_GM45(dev_priv)) 393 g4x_fbc_deactivate(dev_priv); 394 else 395 i8xx_fbc_deactivate(dev_priv); 396 } 397 398 /** 399 * intel_fbc_is_active - Is FBC active? 400 * @dev_priv: i915 device instance 401 * 402 * This function is used to verify the current state of FBC. 403 * 404 * FIXME: This should be tracked in the plane config eventually 405 * instead of queried at runtime for most callers. 406 */ 407 bool intel_fbc_is_active(struct drm_i915_private *dev_priv) 408 { 409 return dev_priv->fbc.active; 410 } 411 412 static void intel_fbc_deactivate(struct drm_i915_private *dev_priv, 413 const char *reason) 414 { 415 struct intel_fbc *fbc = &dev_priv->fbc; 416 417 WARN_ON(!mutex_is_locked(&fbc->lock)); 418 419 if (fbc->active) 420 intel_fbc_hw_deactivate(dev_priv); 421 422 fbc->no_fbc_reason = reason; 423 } 424 425 static int find_compression_threshold(struct drm_i915_private *dev_priv, 426 struct drm_mm_node *node, 427 unsigned int size, 428 unsigned int fb_cpp) 429 { 430 int compression_threshold = 1; 431 int ret; 432 u64 end; 433 434 /* The FBC hardware for BDW/SKL doesn't have access to the stolen 435 * reserved range size, so it always assumes the maximum (8mb) is used. 436 * If we enable FBC using a CFB on that memory range we'll get FIFO 437 * underruns, even if that range is not reserved by the BIOS. */ 438 if (IS_BROADWELL(dev_priv) || IS_GEN9_BC(dev_priv)) 439 end = resource_size(&dev_priv->dsm) - 8 * 1024 * 1024; 440 else 441 end = U64_MAX; 442 443 /* HACK: This code depends on what we will do in *_enable_fbc. If that 444 * code changes, this code needs to change as well. 445 * 446 * The enable_fbc code will attempt to use one of our 2 compression 447 * thresholds, therefore, in that case, we only have 1 resort. 448 */ 449 450 /* Try to over-allocate to reduce reallocations and fragmentation. */ 451 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1, 452 4096, 0, end); 453 if (ret == 0) 454 return compression_threshold; 455 456 again: 457 /* HW's ability to limit the CFB is 1:4 */ 458 if (compression_threshold > 4 || 459 (fb_cpp == 2 && compression_threshold == 2)) 460 return 0; 461 462 ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1, 463 4096, 0, end); 464 if (ret && INTEL_GEN(dev_priv) <= 4) { 465 return 0; 466 } else if (ret) { 467 compression_threshold <<= 1; 468 goto again; 469 } else { 470 return compression_threshold; 471 } 472 } 473 474 static int intel_fbc_alloc_cfb(struct drm_i915_private *dev_priv, 475 unsigned int size, unsigned int fb_cpp) 476 { 477 struct intel_fbc *fbc = &dev_priv->fbc; 478 struct drm_mm_node *uninitialized_var(compressed_llb); 479 int ret; 480 481 WARN_ON(drm_mm_node_allocated(&fbc->compressed_fb)); 482 483 ret = find_compression_threshold(dev_priv, &fbc->compressed_fb, 484 size, fb_cpp); 485 if (!ret) 486 goto err_llb; 487 else if (ret > 1) { 488 DRM_INFO("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"); 489 490 } 491 492 fbc->threshold = ret; 493 494 if (INTEL_GEN(dev_priv) >= 5) 495 I915_WRITE(ILK_DPFC_CB_BASE, fbc->compressed_fb.start); 496 else if (IS_GM45(dev_priv)) { 497 I915_WRITE(DPFC_CB_BASE, fbc->compressed_fb.start); 498 } else { 499 compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL); 500 if (!compressed_llb) 501 goto err_fb; 502 503 ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb, 504 4096, 4096); 505 if (ret) 506 goto err_fb; 507 508 fbc->compressed_llb = compressed_llb; 509 510 GEM_BUG_ON(range_overflows_t(u64, dev_priv->dsm.start, 511 fbc->compressed_fb.start, 512 U32_MAX)); 513 GEM_BUG_ON(range_overflows_t(u64, dev_priv->dsm.start, 514 fbc->compressed_llb->start, 515 U32_MAX)); 516 I915_WRITE(FBC_CFB_BASE, 517 dev_priv->dsm.start + fbc->compressed_fb.start); 518 I915_WRITE(FBC_LL_BASE, 519 dev_priv->dsm.start + compressed_llb->start); 520 } 521 522 DRM_DEBUG_KMS("reserved %"PRIu64" bytes of contiguous stolen space for FBC, threshold: %d\n", 523 fbc->compressed_fb.size, fbc->threshold); 524 525 return 0; 526 527 err_fb: 528 kfree(compressed_llb); 529 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb); 530 err_llb: 531 if (drm_mm_initialized(&dev_priv->mm.stolen)) 532 pr_info_once("drm: 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); 533 return -ENOSPC; 534 } 535 536 static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv) 537 { 538 struct intel_fbc *fbc = &dev_priv->fbc; 539 540 if (drm_mm_node_allocated(&fbc->compressed_fb)) 541 i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb); 542 543 if (fbc->compressed_llb) { 544 i915_gem_stolen_remove_node(dev_priv, fbc->compressed_llb); 545 kfree(fbc->compressed_llb); 546 } 547 } 548 549 void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv) 550 { 551 struct intel_fbc *fbc = &dev_priv->fbc; 552 553 if (!fbc_supported(dev_priv)) 554 return; 555 556 mutex_lock(&fbc->lock); 557 __intel_fbc_cleanup_cfb(dev_priv); 558 mutex_unlock(&fbc->lock); 559 } 560 561 static bool stride_is_valid(struct drm_i915_private *dev_priv, 562 unsigned int stride) 563 { 564 /* This should have been caught earlier. */ 565 if (WARN_ON_ONCE((stride & (64 - 1)) != 0)) 566 return false; 567 568 /* Below are the additional FBC restrictions. */ 569 if (stride < 512) 570 return false; 571 572 if (IS_GEN(dev_priv, 2) || IS_GEN(dev_priv, 3)) 573 return stride == 4096 || stride == 8192; 574 575 if (IS_GEN(dev_priv, 4) && !IS_G4X(dev_priv) && stride < 2048) 576 return false; 577 578 if (stride > 16384) 579 return false; 580 581 return true; 582 } 583 584 static bool pixel_format_is_valid(struct drm_i915_private *dev_priv, 585 u32 pixel_format) 586 { 587 switch (pixel_format) { 588 case DRM_FORMAT_XRGB8888: 589 case DRM_FORMAT_XBGR8888: 590 return true; 591 case DRM_FORMAT_XRGB1555: 592 case DRM_FORMAT_RGB565: 593 /* 16bpp not supported on gen2 */ 594 if (IS_GEN(dev_priv, 2)) 595 return false; 596 /* WaFbcOnly1to1Ratio:ctg */ 597 if (IS_G4X(dev_priv)) 598 return false; 599 return true; 600 default: 601 return false; 602 } 603 } 604 605 /* 606 * For some reason, the hardware tracking starts looking at whatever we 607 * programmed as the display plane base address register. It does not look at 608 * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y} 609 * variables instead of just looking at the pipe/plane size. 610 */ 611 static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc) 612 { 613 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 614 struct intel_fbc *fbc = &dev_priv->fbc; 615 unsigned int effective_w, effective_h, max_w, max_h; 616 617 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) { 618 max_w = 5120; 619 max_h = 4096; 620 } else if (INTEL_GEN(dev_priv) >= 8 || IS_HASWELL(dev_priv)) { 621 max_w = 4096; 622 max_h = 4096; 623 } else if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) { 624 max_w = 4096; 625 max_h = 2048; 626 } else { 627 max_w = 2048; 628 max_h = 1536; 629 } 630 631 intel_fbc_get_plane_source_size(&fbc->state_cache, &effective_w, 632 &effective_h); 633 effective_w += fbc->state_cache.plane.adjusted_x; 634 effective_h += fbc->state_cache.plane.adjusted_y; 635 636 return effective_w <= max_w && effective_h <= max_h; 637 } 638 639 static void intel_fbc_update_state_cache(struct intel_crtc *crtc, 640 const struct intel_crtc_state *crtc_state, 641 const struct intel_plane_state *plane_state) 642 { 643 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 644 struct intel_fbc *fbc = &dev_priv->fbc; 645 struct intel_fbc_state_cache *cache = &fbc->state_cache; 646 struct drm_framebuffer *fb = plane_state->hw.fb; 647 648 cache->plane.visible = plane_state->uapi.visible; 649 if (!cache->plane.visible) 650 return; 651 652 cache->crtc.mode_flags = crtc_state->hw.adjusted_mode.flags; 653 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) 654 cache->crtc.hsw_bdw_pixel_rate = crtc_state->pixel_rate; 655 656 cache->plane.rotation = plane_state->hw.rotation; 657 /* 658 * Src coordinates are already rotated by 270 degrees for 659 * the 90/270 degree plane rotation cases (to match the 660 * GTT mapping), hence no need to account for rotation here. 661 */ 662 cache->plane.src_w = drm_rect_width(&plane_state->uapi.src) >> 16; 663 cache->plane.src_h = drm_rect_height(&plane_state->uapi.src) >> 16; 664 cache->plane.adjusted_x = plane_state->color_plane[0].x; 665 cache->plane.adjusted_y = plane_state->color_plane[0].y; 666 cache->plane.y = plane_state->uapi.src.y1 >> 16; 667 668 cache->plane.pixel_blend_mode = plane_state->hw.pixel_blend_mode; 669 670 cache->fb.format = fb->format; 671 cache->fb.stride = fb->pitches[0]; 672 673 WARN_ON(plane_state->flags & PLANE_HAS_FENCE && 674 !plane_state->vma->fence); 675 676 if (plane_state->flags & PLANE_HAS_FENCE && 677 plane_state->vma->fence) 678 cache->fence_id = plane_state->vma->fence->id; 679 else 680 cache->fence_id = -1; 681 } 682 683 static bool intel_fbc_cfb_size_changed(struct drm_i915_private *dev_priv) 684 { 685 struct intel_fbc *fbc = &dev_priv->fbc; 686 687 return intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache) > 688 fbc->compressed_fb.size * fbc->threshold; 689 } 690 691 static bool intel_fbc_can_activate(struct intel_crtc *crtc) 692 { 693 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 694 struct intel_fbc *fbc = &dev_priv->fbc; 695 struct intel_fbc_state_cache *cache = &fbc->state_cache; 696 697 if (!cache->plane.visible) { 698 fbc->no_fbc_reason = "primary plane not visible"; 699 return false; 700 } 701 702 /* We don't need to use a state cache here since this information is 703 * global for all CRTC. 704 */ 705 if (fbc->underrun_detected) { 706 fbc->no_fbc_reason = "underrun detected"; 707 return false; 708 } 709 710 if (cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) { 711 fbc->no_fbc_reason = "incompatible mode"; 712 return false; 713 } 714 715 if (!intel_fbc_hw_tracking_covers_screen(crtc)) { 716 fbc->no_fbc_reason = "mode too large for compression"; 717 return false; 718 } 719 720 /* The use of a CPU fence is mandatory in order to detect writes 721 * by the CPU to the scanout and trigger updates to the FBC. 722 * 723 * Note that is possible for a tiled surface to be unmappable (and 724 * so have no fence associated with it) due to aperture constaints 725 * at the time of pinning. 726 * 727 * FIXME with 90/270 degree rotation we should use the fence on 728 * the normal GTT view (the rotated view doesn't even have a 729 * fence). Would need changes to the FBC fence Y offset as well. 730 * For now this will effecively disable FBC with 90/270 degree 731 * rotation. 732 */ 733 if (cache->fence_id < 0) { 734 fbc->no_fbc_reason = "framebuffer not tiled or fenced"; 735 return false; 736 } 737 if (INTEL_GEN(dev_priv) <= 4 && !IS_G4X(dev_priv) && 738 cache->plane.rotation != DRM_MODE_ROTATE_0) { 739 fbc->no_fbc_reason = "rotation unsupported"; 740 return false; 741 } 742 743 if (!stride_is_valid(dev_priv, cache->fb.stride)) { 744 fbc->no_fbc_reason = "framebuffer stride not supported"; 745 return false; 746 } 747 748 if (!pixel_format_is_valid(dev_priv, cache->fb.format->format)) { 749 fbc->no_fbc_reason = "pixel format is invalid"; 750 return false; 751 } 752 753 if (cache->plane.pixel_blend_mode != DRM_MODE_BLEND_PIXEL_NONE && 754 cache->fb.format->has_alpha) { 755 fbc->no_fbc_reason = "per-pixel alpha blending is incompatible with FBC"; 756 return false; 757 } 758 759 /* WaFbcExceedCdClockThreshold:hsw,bdw */ 760 if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) && 761 cache->crtc.hsw_bdw_pixel_rate >= dev_priv->cdclk.hw.cdclk * 95 / 100) { 762 fbc->no_fbc_reason = "pixel rate is too big"; 763 return false; 764 } 765 766 /* It is possible for the required CFB size change without a 767 * crtc->disable + crtc->enable since it is possible to change the 768 * stride without triggering a full modeset. Since we try to 769 * over-allocate the CFB, there's a chance we may keep FBC enabled even 770 * if this happens, but if we exceed the current CFB size we'll have to 771 * disable FBC. Notice that it would be possible to disable FBC, wait 772 * for a frame, free the stolen node, then try to reenable FBC in case 773 * we didn't get any invalidate/deactivate calls, but this would require 774 * a lot of tracking just for a specific case. If we conclude it's an 775 * important case, we can implement it later. */ 776 if (intel_fbc_cfb_size_changed(dev_priv)) { 777 fbc->no_fbc_reason = "CFB requirements changed"; 778 return false; 779 } 780 781 /* 782 * Work around a problem on GEN9+ HW, where enabling FBC on a plane 783 * having a Y offset that isn't divisible by 4 causes FIFO underrun 784 * and screen flicker. 785 */ 786 if (INTEL_GEN(dev_priv) >= 9 && 787 (fbc->state_cache.plane.adjusted_y & 3)) { 788 fbc->no_fbc_reason = "plane Y offset is misaligned"; 789 return false; 790 } 791 792 return true; 793 } 794 795 static bool intel_fbc_can_enable(struct drm_i915_private *dev_priv) 796 { 797 struct intel_fbc *fbc = &dev_priv->fbc; 798 799 if (intel_vgpu_active(dev_priv)) { 800 fbc->no_fbc_reason = "VGPU is active"; 801 return false; 802 } 803 804 if (!i915_modparams.enable_fbc) { 805 fbc->no_fbc_reason = "disabled per module param or by default"; 806 return false; 807 } 808 809 if (fbc->underrun_detected) { 810 fbc->no_fbc_reason = "underrun detected"; 811 return false; 812 } 813 814 return true; 815 } 816 817 static void intel_fbc_get_reg_params(struct intel_crtc *crtc, 818 struct intel_fbc_reg_params *params) 819 { 820 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 821 struct intel_fbc *fbc = &dev_priv->fbc; 822 struct intel_fbc_state_cache *cache = &fbc->state_cache; 823 824 /* Since all our fields are integer types, use memset here so the 825 * comparison function can rely on memcmp because the padding will be 826 * zero. */ 827 memset(params, 0, sizeof(*params)); 828 829 params->fence_id = cache->fence_id; 830 831 params->crtc.pipe = crtc->pipe; 832 params->crtc.i9xx_plane = to_intel_plane(crtc->base.primary)->i9xx_plane; 833 params->crtc.fence_y_offset = get_crtc_fence_y_offset(fbc); 834 835 params->fb.format = cache->fb.format; 836 params->fb.stride = cache->fb.stride; 837 838 params->cfb_size = intel_fbc_calculate_cfb_size(dev_priv, cache); 839 840 params->gen9_wa_cfb_stride = cache->gen9_wa_cfb_stride; 841 842 params->plane_visible = cache->plane.visible; 843 } 844 845 static bool intel_fbc_can_flip_nuke(const struct intel_crtc_state *crtc_state) 846 { 847 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 848 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 849 const struct intel_fbc *fbc = &dev_priv->fbc; 850 const struct intel_fbc_state_cache *cache = &fbc->state_cache; 851 const struct intel_fbc_reg_params *params = &fbc->params; 852 853 if (drm_atomic_crtc_needs_modeset(&crtc_state->uapi)) 854 return false; 855 856 if (!params->plane_visible) 857 return false; 858 859 if (!intel_fbc_can_activate(crtc)) 860 return false; 861 862 if (params->fb.format != cache->fb.format) 863 return false; 864 865 if (params->fb.stride != cache->fb.stride) 866 return false; 867 868 if (params->cfb_size != intel_fbc_calculate_cfb_size(dev_priv, cache)) 869 return false; 870 871 if (params->gen9_wa_cfb_stride != cache->gen9_wa_cfb_stride) 872 return false; 873 874 return true; 875 } 876 877 bool intel_fbc_pre_update(struct intel_crtc *crtc, 878 const struct intel_crtc_state *crtc_state, 879 const struct intel_plane_state *plane_state) 880 { 881 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 882 struct intel_fbc *fbc = &dev_priv->fbc; 883 const char *reason = "update pending"; 884 bool need_vblank_wait = false; 885 886 if (!fbc_supported(dev_priv)) 887 return need_vblank_wait; 888 889 mutex_lock(&fbc->lock); 890 891 if (fbc->crtc != crtc) 892 goto unlock; 893 894 intel_fbc_update_state_cache(crtc, crtc_state, plane_state); 895 fbc->flip_pending = true; 896 897 if (!intel_fbc_can_flip_nuke(crtc_state)) { 898 intel_fbc_deactivate(dev_priv, reason); 899 900 /* 901 * Display WA #1198: glk+ 902 * Need an extra vblank wait between FBC disable and most plane 903 * updates. Bspec says this is only needed for plane disable, but 904 * that is not true. Touching most plane registers will cause the 905 * corruption to appear. Also SKL/derivatives do not seem to be 906 * affected. 907 * 908 * TODO: could optimize this a bit by sampling the frame 909 * counter when we disable FBC (if it was already done earlier) 910 * and skipping the extra vblank wait before the plane update 911 * if at least one frame has already passed. 912 */ 913 if (fbc->activated && 914 (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))) 915 need_vblank_wait = true; 916 fbc->activated = false; 917 } 918 unlock: 919 mutex_unlock(&fbc->lock); 920 921 return need_vblank_wait; 922 } 923 924 /** 925 * __intel_fbc_disable - disable FBC 926 * @dev_priv: i915 device instance 927 * 928 * This is the low level function that actually disables FBC. Callers should 929 * grab the FBC lock. 930 */ 931 static void __intel_fbc_disable(struct drm_i915_private *dev_priv) 932 { 933 struct intel_fbc *fbc = &dev_priv->fbc; 934 struct intel_crtc *crtc = fbc->crtc; 935 936 WARN_ON(!mutex_is_locked(&fbc->lock)); 937 WARN_ON(!fbc->crtc); 938 WARN_ON(fbc->active); 939 940 DRM_DEBUG_KMS("Disabling FBC on pipe %c\n", pipe_name(crtc->pipe)); 941 942 __intel_fbc_cleanup_cfb(dev_priv); 943 944 fbc->crtc = NULL; 945 } 946 947 static void __intel_fbc_post_update(struct intel_crtc *crtc) 948 { 949 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 950 struct intel_fbc *fbc = &dev_priv->fbc; 951 952 WARN_ON(!mutex_is_locked(&fbc->lock)); 953 954 if (fbc->crtc != crtc) 955 return; 956 957 fbc->flip_pending = false; 958 959 if (!i915_modparams.enable_fbc) { 960 intel_fbc_deactivate(dev_priv, "disabled at runtime per module param"); 961 __intel_fbc_disable(dev_priv); 962 963 return; 964 } 965 966 intel_fbc_get_reg_params(crtc, &fbc->params); 967 968 if (!intel_fbc_can_activate(crtc)) 969 return; 970 971 if (!fbc->busy_bits) 972 intel_fbc_hw_activate(dev_priv); 973 else 974 intel_fbc_deactivate(dev_priv, "frontbuffer write"); 975 } 976 977 void intel_fbc_post_update(struct intel_crtc *crtc) 978 { 979 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 980 struct intel_fbc *fbc = &dev_priv->fbc; 981 982 if (!fbc_supported(dev_priv)) 983 return; 984 985 mutex_lock(&fbc->lock); 986 __intel_fbc_post_update(crtc); 987 mutex_unlock(&fbc->lock); 988 } 989 990 static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc) 991 { 992 if (fbc->crtc) 993 return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit; 994 else 995 return fbc->possible_framebuffer_bits; 996 } 997 998 void intel_fbc_invalidate(struct drm_i915_private *dev_priv, 999 unsigned int frontbuffer_bits, 1000 enum fb_op_origin origin) 1001 { 1002 struct intel_fbc *fbc = &dev_priv->fbc; 1003 1004 if (!fbc_supported(dev_priv)) 1005 return; 1006 1007 if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP) 1008 return; 1009 1010 mutex_lock(&fbc->lock); 1011 1012 fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits; 1013 1014 if (fbc->crtc && fbc->busy_bits) 1015 intel_fbc_deactivate(dev_priv, "frontbuffer write"); 1016 1017 mutex_unlock(&fbc->lock); 1018 } 1019 1020 void intel_fbc_flush(struct drm_i915_private *dev_priv, 1021 unsigned int frontbuffer_bits, enum fb_op_origin origin) 1022 { 1023 struct intel_fbc *fbc = &dev_priv->fbc; 1024 1025 if (!fbc_supported(dev_priv)) 1026 return; 1027 1028 mutex_lock(&fbc->lock); 1029 1030 fbc->busy_bits &= ~frontbuffer_bits; 1031 1032 if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP) 1033 goto out; 1034 1035 if (!fbc->busy_bits && fbc->crtc && 1036 (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) { 1037 if (fbc->active) 1038 intel_fbc_recompress(dev_priv); 1039 else if (!fbc->flip_pending) 1040 __intel_fbc_post_update(fbc->crtc); 1041 } 1042 1043 out: 1044 mutex_unlock(&fbc->lock); 1045 } 1046 1047 /** 1048 * intel_fbc_choose_crtc - select a CRTC to enable FBC on 1049 * @dev_priv: i915 device instance 1050 * @state: the atomic state structure 1051 * 1052 * This function looks at the proposed state for CRTCs and planes, then chooses 1053 * which pipe is going to have FBC by setting intel_crtc_state->enable_fbc to 1054 * true. 1055 * 1056 * Later, intel_fbc_enable is going to look for state->enable_fbc and then maybe 1057 * enable FBC for the chosen CRTC. If it does, it will set dev_priv->fbc.crtc. 1058 */ 1059 void intel_fbc_choose_crtc(struct drm_i915_private *dev_priv, 1060 struct intel_atomic_state *state) 1061 { 1062 struct intel_fbc *fbc = &dev_priv->fbc; 1063 struct intel_plane *plane; 1064 struct intel_plane_state *plane_state; 1065 bool crtc_chosen = false; 1066 int i; 1067 1068 mutex_lock(&fbc->lock); 1069 1070 /* Does this atomic commit involve the CRTC currently tied to FBC? */ 1071 if (fbc->crtc && 1072 !intel_atomic_get_new_crtc_state(state, fbc->crtc)) 1073 goto out; 1074 1075 if (!intel_fbc_can_enable(dev_priv)) 1076 goto out; 1077 1078 /* Simply choose the first CRTC that is compatible and has a visible 1079 * plane. We could go for fancier schemes such as checking the plane 1080 * size, but this would just affect the few platforms that don't tie FBC 1081 * to pipe or plane A. */ 1082 for_each_new_intel_plane_in_state(state, plane, plane_state, i) { 1083 struct intel_crtc_state *crtc_state; 1084 struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc); 1085 1086 if (!plane->has_fbc) 1087 continue; 1088 1089 if (!plane_state->uapi.visible) 1090 continue; 1091 1092 crtc_state = intel_atomic_get_new_crtc_state(state, crtc); 1093 1094 crtc_state->enable_fbc = true; 1095 crtc_chosen = true; 1096 break; 1097 } 1098 1099 if (!crtc_chosen) 1100 fbc->no_fbc_reason = "no suitable CRTC for FBC"; 1101 1102 out: 1103 mutex_unlock(&fbc->lock); 1104 } 1105 1106 /** 1107 * intel_fbc_enable: tries to enable FBC on the CRTC 1108 * @crtc: the CRTC 1109 * @crtc_state: corresponding &drm_crtc_state for @crtc 1110 * @plane_state: corresponding &drm_plane_state for the primary plane of @crtc 1111 * 1112 * This function checks if the given CRTC was chosen for FBC, then enables it if 1113 * possible. Notice that it doesn't activate FBC. It is valid to call 1114 * intel_fbc_enable multiple times for the same pipe without an 1115 * intel_fbc_disable in the middle, as long as it is deactivated. 1116 */ 1117 void intel_fbc_enable(struct intel_crtc *crtc, 1118 const struct intel_crtc_state *crtc_state, 1119 const struct intel_plane_state *plane_state) 1120 { 1121 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 1122 struct intel_fbc *fbc = &dev_priv->fbc; 1123 struct intel_fbc_state_cache *cache = &fbc->state_cache; 1124 const struct drm_framebuffer *fb = plane_state->hw.fb; 1125 1126 if (!fbc_supported(dev_priv)) 1127 return; 1128 1129 mutex_lock(&fbc->lock); 1130 1131 if (fbc->crtc) { 1132 if (fbc->crtc != crtc || 1133 !intel_fbc_cfb_size_changed(dev_priv)) 1134 goto out; 1135 1136 __intel_fbc_disable(dev_priv); 1137 } 1138 1139 WARN_ON(fbc->active); 1140 1141 intel_fbc_update_state_cache(crtc, crtc_state, plane_state); 1142 1143 /* FIXME crtc_state->enable_fbc lies :( */ 1144 if (!cache->plane.visible) 1145 goto out; 1146 1147 if (intel_fbc_alloc_cfb(dev_priv, 1148 intel_fbc_calculate_cfb_size(dev_priv, cache), 1149 fb->format->cpp[0])) { 1150 cache->plane.visible = false; 1151 fbc->no_fbc_reason = "not enough stolen memory"; 1152 goto out; 1153 } 1154 1155 if ((IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) && 1156 fb->modifier != I915_FORMAT_MOD_X_TILED) 1157 cache->gen9_wa_cfb_stride = 1158 DIV_ROUND_UP(cache->plane.src_w, 32 * fbc->threshold) * 8; 1159 else 1160 cache->gen9_wa_cfb_stride = 0; 1161 1162 DRM_DEBUG_KMS("Enabling FBC on pipe %c\n", pipe_name(crtc->pipe)); 1163 fbc->no_fbc_reason = "FBC enabled but not active yet\n"; 1164 1165 fbc->crtc = crtc; 1166 out: 1167 mutex_unlock(&fbc->lock); 1168 } 1169 1170 /** 1171 * intel_fbc_disable - disable FBC if it's associated with crtc 1172 * @crtc: the CRTC 1173 * 1174 * This function disables FBC if it's associated with the provided CRTC. 1175 */ 1176 void intel_fbc_disable(struct intel_crtc *crtc) 1177 { 1178 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 1179 struct intel_fbc *fbc = &dev_priv->fbc; 1180 1181 if (!fbc_supported(dev_priv)) 1182 return; 1183 1184 mutex_lock(&fbc->lock); 1185 if (fbc->crtc == crtc) 1186 __intel_fbc_disable(dev_priv); 1187 mutex_unlock(&fbc->lock); 1188 } 1189 1190 /** 1191 * intel_fbc_global_disable - globally disable FBC 1192 * @dev_priv: i915 device instance 1193 * 1194 * This function disables FBC regardless of which CRTC is associated with it. 1195 */ 1196 void intel_fbc_global_disable(struct drm_i915_private *dev_priv) 1197 { 1198 struct intel_fbc *fbc = &dev_priv->fbc; 1199 1200 if (!fbc_supported(dev_priv)) 1201 return; 1202 1203 mutex_lock(&fbc->lock); 1204 if (fbc->crtc) { 1205 WARN_ON(fbc->crtc->active); 1206 __intel_fbc_disable(dev_priv); 1207 } 1208 mutex_unlock(&fbc->lock); 1209 } 1210 1211 static void intel_fbc_underrun_work_fn(struct work_struct *work) 1212 { 1213 struct drm_i915_private *dev_priv = 1214 container_of(work, struct drm_i915_private, fbc.underrun_work); 1215 struct intel_fbc *fbc = &dev_priv->fbc; 1216 1217 mutex_lock(&fbc->lock); 1218 1219 /* Maybe we were scheduled twice. */ 1220 if (fbc->underrun_detected || !fbc->crtc) 1221 goto out; 1222 1223 DRM_DEBUG_KMS("Disabling FBC due to FIFO underrun.\n"); 1224 fbc->underrun_detected = true; 1225 1226 intel_fbc_deactivate(dev_priv, "FIFO underrun"); 1227 out: 1228 mutex_unlock(&fbc->lock); 1229 } 1230 1231 /* 1232 * intel_fbc_reset_underrun - reset FBC fifo underrun status. 1233 * @dev_priv: i915 device instance 1234 * 1235 * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we 1236 * want to re-enable FBC after an underrun to increase test coverage. 1237 */ 1238 int intel_fbc_reset_underrun(struct drm_i915_private *dev_priv) 1239 { 1240 int ret; 1241 1242 cancel_work_sync(&dev_priv->fbc.underrun_work); 1243 1244 ret = mutex_lock_interruptible(&dev_priv->fbc.lock); 1245 if (ret) 1246 return ret; 1247 1248 if (dev_priv->fbc.underrun_detected) { 1249 DRM_DEBUG_KMS("Re-allowing FBC after fifo underrun\n"); 1250 dev_priv->fbc.no_fbc_reason = "FIFO underrun cleared"; 1251 } 1252 1253 dev_priv->fbc.underrun_detected = false; 1254 mutex_unlock(&dev_priv->fbc.lock); 1255 1256 return 0; 1257 } 1258 1259 /** 1260 * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun 1261 * @dev_priv: i915 device instance 1262 * 1263 * Without FBC, most underruns are harmless and don't really cause too many 1264 * problems, except for an annoying message on dmesg. With FBC, underruns can 1265 * become black screens or even worse, especially when paired with bad 1266 * watermarks. So in order for us to be on the safe side, completely disable FBC 1267 * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe 1268 * already suggests that watermarks may be bad, so try to be as safe as 1269 * possible. 1270 * 1271 * This function is called from the IRQ handler. 1272 */ 1273 void intel_fbc_handle_fifo_underrun_irq(struct drm_i915_private *dev_priv) 1274 { 1275 struct intel_fbc *fbc = &dev_priv->fbc; 1276 1277 if (!fbc_supported(dev_priv)) 1278 return; 1279 1280 /* There's no guarantee that underrun_detected won't be set to true 1281 * right after this check and before the work is scheduled, but that's 1282 * not a problem since we'll check it again under the work function 1283 * while FBC is locked. This check here is just to prevent us from 1284 * unnecessarily scheduling the work, and it relies on the fact that we 1285 * never switch underrun_detect back to false after it's true. */ 1286 if (READ_ONCE(fbc->underrun_detected)) 1287 return; 1288 1289 schedule_work(&fbc->underrun_work); 1290 } 1291 1292 /* 1293 * The DDX driver changes its behavior depending on the value it reads from 1294 * i915.enable_fbc, so sanitize it by translating the default value into either 1295 * 0 or 1 in order to allow it to know what's going on. 1296 * 1297 * Notice that this is done at driver initialization and we still allow user 1298 * space to change the value during runtime without sanitizing it again. IGT 1299 * relies on being able to change i915.enable_fbc at runtime. 1300 */ 1301 static int intel_sanitize_fbc_option(struct drm_i915_private *dev_priv) 1302 { 1303 if (i915_modparams.enable_fbc >= 0) 1304 return !!i915_modparams.enable_fbc; 1305 1306 if (!HAS_FBC(dev_priv)) 1307 return 0; 1308 1309 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) 1310 return 1; 1311 1312 return 0; 1313 } 1314 1315 static bool need_fbc_vtd_wa(struct drm_i915_private *dev_priv) 1316 { 1317 /* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */ 1318 if (intel_vtd_active() && 1319 (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))) { 1320 DRM_INFO("Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n"); 1321 return true; 1322 } 1323 1324 return false; 1325 } 1326 1327 /** 1328 * intel_fbc_init - Initialize FBC 1329 * @dev_priv: the i915 device 1330 * 1331 * This function might be called during PM init process. 1332 */ 1333 void intel_fbc_init(struct drm_i915_private *dev_priv) 1334 { 1335 struct intel_fbc *fbc = &dev_priv->fbc; 1336 1337 INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn); 1338 mutex_init(&fbc->lock); 1339 fbc->active = false; 1340 1341 if (!drm_mm_initialized(&dev_priv->mm.stolen)) 1342 mkwrite_device_info(dev_priv)->display.has_fbc = false; 1343 1344 if (need_fbc_vtd_wa(dev_priv)) 1345 mkwrite_device_info(dev_priv)->display.has_fbc = false; 1346 1347 i915_modparams.enable_fbc = intel_sanitize_fbc_option(dev_priv); 1348 DRM_DEBUG_KMS("Sanitized enable_fbc value: %d\n", 1349 i915_modparams.enable_fbc); 1350 1351 if (!HAS_FBC(dev_priv)) { 1352 fbc->no_fbc_reason = "unsupported by this chipset"; 1353 return; 1354 } 1355 1356 /* This value was pulled out of someone's hat */ 1357 if (INTEL_GEN(dev_priv) <= 4 && !IS_GM45(dev_priv)) 1358 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT); 1359 1360 /* We still don't have any sort of hardware state readout for FBC, so 1361 * deactivate it in case the BIOS activated it to make sure software 1362 * matches the hardware state. */ 1363 if (intel_fbc_hw_is_active(dev_priv)) 1364 intel_fbc_hw_deactivate(dev_priv); 1365 } 1366 1367 void 1368 intel_fbc_cleanup(struct drm_i915_private *dev_priv) 1369 { 1370 struct intel_fbc *fbc = &dev_priv->fbc; 1371 1372 mutex_destroy(&fbc->lock); 1373 } 1374