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: atomic plane helpers 26 * 27 * The functions here are used by the atomic plane helper functions to 28 * implement legacy plane updates (i.e., drm_plane->update_plane() and 29 * drm_plane->disable_plane()). This allows plane updates to use the 30 * atomic state infrastructure and perform plane updates as separate 31 * prepare/check/commit/cleanup steps. 32 */ 33 34 #include <linux/dma-fence-chain.h> 35 #include <linux/dma-resv.h> 36 37 #include <drm/drm_atomic_helper.h> 38 #include <drm/drm_blend.h> 39 #include <drm/drm_fourcc.h> 40 #include <drm/drm_gem.h> 41 #include <drm/drm_gem_atomic_helper.h> 42 43 #include "i915_drv.h" 44 #include "i915_config.h" 45 #include "i9xx_plane_regs.h" 46 #include "intel_atomic_plane.h" 47 #include "intel_cdclk.h" 48 #include "intel_cursor.h" 49 #include "intel_display_rps.h" 50 #include "intel_display_trace.h" 51 #include "intel_display_types.h" 52 #include "intel_fb.h" 53 #include "intel_fb_pin.h" 54 #include "skl_scaler.h" 55 #include "skl_watermark.h" 56 57 static void intel_plane_state_reset(struct intel_plane_state *plane_state, 58 struct intel_plane *plane) 59 { 60 memset(plane_state, 0, sizeof(*plane_state)); 61 62 __drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base); 63 64 plane_state->scaler_id = -1; 65 } 66 67 struct intel_plane *intel_plane_alloc(void) 68 { 69 struct intel_plane_state *plane_state; 70 struct intel_plane *plane; 71 72 plane = kzalloc(sizeof(*plane), GFP_KERNEL); 73 if (!plane) 74 return ERR_PTR(-ENOMEM); 75 76 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL); 77 if (!plane_state) { 78 kfree(plane); 79 return ERR_PTR(-ENOMEM); 80 } 81 82 intel_plane_state_reset(plane_state, plane); 83 84 plane->base.state = &plane_state->uapi; 85 86 return plane; 87 } 88 89 void intel_plane_free(struct intel_plane *plane) 90 { 91 intel_plane_destroy_state(&plane->base, plane->base.state); 92 kfree(plane); 93 } 94 95 /** 96 * intel_plane_duplicate_state - duplicate plane state 97 * @plane: drm plane 98 * 99 * Allocates and returns a copy of the plane state (both common and 100 * Intel-specific) for the specified plane. 101 * 102 * Returns: The newly allocated plane state, or NULL on failure. 103 */ 104 struct drm_plane_state * 105 intel_plane_duplicate_state(struct drm_plane *plane) 106 { 107 struct intel_plane_state *intel_state; 108 109 intel_state = to_intel_plane_state(plane->state); 110 intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL); 111 112 if (!intel_state) 113 return NULL; 114 115 __drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi); 116 117 intel_state->ggtt_vma = NULL; 118 intel_state->dpt_vma = NULL; 119 intel_state->flags = 0; 120 121 /* add reference to fb */ 122 if (intel_state->hw.fb) 123 drm_framebuffer_get(intel_state->hw.fb); 124 125 return &intel_state->uapi; 126 } 127 128 /** 129 * intel_plane_destroy_state - destroy plane state 130 * @plane: drm plane 131 * @state: state object to destroy 132 * 133 * Destroys the plane state (both common and Intel-specific) for the 134 * specified plane. 135 */ 136 void 137 intel_plane_destroy_state(struct drm_plane *plane, 138 struct drm_plane_state *state) 139 { 140 struct intel_plane_state *plane_state = to_intel_plane_state(state); 141 142 drm_WARN_ON(plane->dev, plane_state->ggtt_vma); 143 drm_WARN_ON(plane->dev, plane_state->dpt_vma); 144 145 __drm_atomic_helper_plane_destroy_state(&plane_state->uapi); 146 if (plane_state->hw.fb) 147 drm_framebuffer_put(plane_state->hw.fb); 148 kfree(plane_state); 149 } 150 151 bool intel_plane_needs_physical(struct intel_plane *plane) 152 { 153 struct drm_i915_private *i915 = to_i915(plane->base.dev); 154 155 return plane->id == PLANE_CURSOR && 156 DISPLAY_INFO(i915)->cursor_needs_physical; 157 } 158 159 unsigned int intel_adjusted_rate(const struct drm_rect *src, 160 const struct drm_rect *dst, 161 unsigned int rate) 162 { 163 unsigned int src_w, src_h, dst_w, dst_h; 164 165 src_w = drm_rect_width(src) >> 16; 166 src_h = drm_rect_height(src) >> 16; 167 dst_w = drm_rect_width(dst); 168 dst_h = drm_rect_height(dst); 169 170 /* Downscaling limits the maximum pixel rate */ 171 dst_w = min(src_w, dst_w); 172 dst_h = min(src_h, dst_h); 173 174 return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h), 175 dst_w * dst_h); 176 } 177 178 unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state, 179 const struct intel_plane_state *plane_state) 180 { 181 /* 182 * Note we don't check for plane visibility here as 183 * we want to use this when calculating the cursor 184 * watermarks even if the cursor is fully offscreen. 185 * That depends on the src/dst rectangles being 186 * correctly populated whenever the watermark code 187 * considers the cursor to be visible, whether or not 188 * it is actually visible. 189 * 190 * See: intel_wm_plane_visible() and intel_check_cursor() 191 */ 192 193 return intel_adjusted_rate(&plane_state->uapi.src, 194 &plane_state->uapi.dst, 195 crtc_state->pixel_rate); 196 } 197 198 unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state, 199 const struct intel_plane_state *plane_state, 200 int color_plane) 201 { 202 const struct drm_framebuffer *fb = plane_state->hw.fb; 203 204 if (!plane_state->uapi.visible) 205 return 0; 206 207 return intel_plane_pixel_rate(crtc_state, plane_state) * 208 fb->format->cpp[color_plane]; 209 } 210 211 static unsigned int 212 intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state, 213 const struct intel_plane_state *plane_state, 214 int color_plane) 215 { 216 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 217 const struct drm_framebuffer *fb = plane_state->hw.fb; 218 unsigned int rel_data_rate; 219 int width, height; 220 221 if (plane->id == PLANE_CURSOR) 222 return 0; 223 224 if (!plane_state->uapi.visible) 225 return 0; 226 227 /* 228 * Src coordinates are already rotated by 270 degrees for 229 * the 90/270 degree plane rotation cases (to match the 230 * GTT mapping), hence no need to account for rotation here. 231 */ 232 width = drm_rect_width(&plane_state->uapi.src) >> 16; 233 height = drm_rect_height(&plane_state->uapi.src) >> 16; 234 235 /* UV plane does 1/2 pixel sub-sampling */ 236 if (color_plane == 1) { 237 width /= 2; 238 height /= 2; 239 } 240 241 rel_data_rate = 242 skl_plane_relative_data_rate(crtc_state, plane, width, height, 243 fb->format->cpp[color_plane]); 244 if (!rel_data_rate) 245 return 0; 246 247 return intel_adjusted_rate(&plane_state->uapi.src, 248 &plane_state->uapi.dst, 249 rel_data_rate); 250 } 251 252 int intel_plane_calc_min_cdclk(struct intel_atomic_state *state, 253 struct intel_plane *plane, 254 bool *need_cdclk_calc) 255 { 256 struct drm_i915_private *dev_priv = to_i915(plane->base.dev); 257 const struct intel_plane_state *plane_state = 258 intel_atomic_get_new_plane_state(state, plane); 259 struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc); 260 const struct intel_cdclk_state *cdclk_state; 261 const struct intel_crtc_state *old_crtc_state; 262 struct intel_crtc_state *new_crtc_state; 263 264 if (!plane_state->uapi.visible || !plane->min_cdclk) 265 return 0; 266 267 old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc); 268 new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc); 269 270 new_crtc_state->min_cdclk[plane->id] = 271 plane->min_cdclk(new_crtc_state, plane_state); 272 273 /* 274 * No need to check against the cdclk state if 275 * the min cdclk for the plane doesn't increase. 276 * 277 * Ie. we only ever increase the cdclk due to plane 278 * requirements. This can reduce back and forth 279 * display blinking due to constant cdclk changes. 280 */ 281 if (new_crtc_state->min_cdclk[plane->id] <= 282 old_crtc_state->min_cdclk[plane->id]) 283 return 0; 284 285 cdclk_state = intel_atomic_get_cdclk_state(state); 286 if (IS_ERR(cdclk_state)) 287 return PTR_ERR(cdclk_state); 288 289 /* 290 * No need to recalculate the cdclk state if 291 * the min cdclk for the pipe doesn't increase. 292 * 293 * Ie. we only ever increase the cdclk due to plane 294 * requirements. This can reduce back and forth 295 * display blinking due to constant cdclk changes. 296 */ 297 if (new_crtc_state->min_cdclk[plane->id] <= 298 cdclk_state->min_cdclk[crtc->pipe]) 299 return 0; 300 301 drm_dbg_kms(&dev_priv->drm, 302 "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n", 303 plane->base.base.id, plane->base.name, 304 new_crtc_state->min_cdclk[plane->id], 305 crtc->base.base.id, crtc->base.name, 306 cdclk_state->min_cdclk[crtc->pipe]); 307 *need_cdclk_calc = true; 308 309 return 0; 310 } 311 312 static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state) 313 { 314 if (plane_state->hw.fb) 315 drm_framebuffer_put(plane_state->hw.fb); 316 317 memset(&plane_state->hw, 0, sizeof(plane_state->hw)); 318 } 319 320 void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state, 321 const struct intel_plane_state *from_plane_state, 322 struct intel_crtc *crtc) 323 { 324 intel_plane_clear_hw_state(plane_state); 325 326 /* 327 * For the joiner secondary uapi.crtc will point at 328 * the primary crtc. So we explicitly assign the right 329 * secondary crtc to hw.crtc. uapi.crtc!=NULL simply 330 * indicates the plane is logically enabled on the uapi level. 331 */ 332 plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL; 333 334 plane_state->hw.fb = from_plane_state->uapi.fb; 335 if (plane_state->hw.fb) 336 drm_framebuffer_get(plane_state->hw.fb); 337 338 plane_state->hw.alpha = from_plane_state->uapi.alpha; 339 plane_state->hw.pixel_blend_mode = 340 from_plane_state->uapi.pixel_blend_mode; 341 plane_state->hw.rotation = from_plane_state->uapi.rotation; 342 plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding; 343 plane_state->hw.color_range = from_plane_state->uapi.color_range; 344 plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter; 345 346 plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi); 347 plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi); 348 } 349 350 void intel_plane_copy_hw_state(struct intel_plane_state *plane_state, 351 const struct intel_plane_state *from_plane_state) 352 { 353 intel_plane_clear_hw_state(plane_state); 354 355 memcpy(&plane_state->hw, &from_plane_state->hw, 356 sizeof(plane_state->hw)); 357 358 if (plane_state->hw.fb) 359 drm_framebuffer_get(plane_state->hw.fb); 360 } 361 362 void intel_plane_set_invisible(struct intel_crtc_state *crtc_state, 363 struct intel_plane_state *plane_state) 364 { 365 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 366 367 crtc_state->active_planes &= ~BIT(plane->id); 368 crtc_state->scaled_planes &= ~BIT(plane->id); 369 crtc_state->nv12_planes &= ~BIT(plane->id); 370 crtc_state->c8_planes &= ~BIT(plane->id); 371 crtc_state->async_flip_planes &= ~BIT(plane->id); 372 crtc_state->data_rate[plane->id] = 0; 373 crtc_state->data_rate_y[plane->id] = 0; 374 crtc_state->rel_data_rate[plane->id] = 0; 375 crtc_state->rel_data_rate_y[plane->id] = 0; 376 crtc_state->min_cdclk[plane->id] = 0; 377 378 plane_state->uapi.visible = false; 379 } 380 381 static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state) 382 { 383 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16; 384 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16; 385 int dst_w = drm_rect_width(&plane_state->uapi.dst); 386 int dst_h = drm_rect_height(&plane_state->uapi.dst); 387 388 return src_w != dst_w || src_h != dst_h; 389 } 390 391 static bool intel_plane_do_async_flip(struct intel_plane *plane, 392 const struct intel_crtc_state *old_crtc_state, 393 const struct intel_crtc_state *new_crtc_state) 394 { 395 struct drm_i915_private *i915 = to_i915(plane->base.dev); 396 397 if (!plane->async_flip) 398 return false; 399 400 if (!new_crtc_state->uapi.async_flip) 401 return false; 402 403 /* 404 * In platforms after DISPLAY13, we might need to override 405 * first async flip in order to change watermark levels 406 * as part of optimization. 407 * 408 * And let's do this for all skl+ so that we can eg. change the 409 * modifier as well. 410 * 411 * TODO: For older platforms there is less reason to do this as 412 * only X-tile is supported with async flips, though we could 413 * extend this so other scanout parameters (stride/etc) could 414 * be changed as well... 415 */ 416 return DISPLAY_VER(i915) < 9 || old_crtc_state->uapi.async_flip; 417 } 418 419 static bool i9xx_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state, 420 const struct intel_plane_state *old_plane_state, 421 const struct intel_plane_state *new_plane_state) 422 { 423 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane); 424 bool old_visible = old_plane_state->uapi.visible; 425 bool new_visible = new_plane_state->uapi.visible; 426 u32 old_ctl = old_plane_state->ctl; 427 u32 new_ctl = new_plane_state->ctl; 428 bool modeset, turn_on, turn_off; 429 430 if (plane->id == PLANE_CURSOR) 431 return false; 432 433 modeset = intel_crtc_needs_modeset(new_crtc_state); 434 turn_off = old_visible && (!new_visible || modeset); 435 turn_on = new_visible && (!old_visible || modeset); 436 437 /* Must disable CxSR around plane enable/disable */ 438 if (turn_on || turn_off) 439 return true; 440 441 if (!old_visible || !new_visible) 442 return false; 443 444 /* 445 * Most plane control register updates are blocked while in CxSR. 446 * 447 * Tiling mode is one exception where the primary plane can 448 * apparently handle it, whereas the sprites can not (the 449 * sprite issue being only relevant on VLV/CHV where CxSR 450 * is actually possible with a sprite enabled). 451 */ 452 if (plane->id == PLANE_PRIMARY) { 453 old_ctl &= ~DISP_TILED; 454 new_ctl &= ~DISP_TILED; 455 } 456 457 return old_ctl != new_ctl; 458 } 459 460 static bool ilk_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state, 461 const struct intel_plane_state *old_plane_state, 462 const struct intel_plane_state *new_plane_state) 463 { 464 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane); 465 bool old_visible = old_plane_state->uapi.visible; 466 bool new_visible = new_plane_state->uapi.visible; 467 bool modeset, turn_on; 468 469 if (plane->id == PLANE_CURSOR) 470 return false; 471 472 modeset = intel_crtc_needs_modeset(new_crtc_state); 473 turn_on = new_visible && (!old_visible || modeset); 474 475 /* 476 * ILK/SNB DVSACNTR/Sprite Enable 477 * IVB SPR_CTL/Sprite Enable 478 * "When in Self Refresh Big FIFO mode, a write to enable the 479 * plane will be internally buffered and delayed while Big FIFO 480 * mode is exiting." 481 * 482 * Which means that enabling the sprite can take an extra frame 483 * when we start in big FIFO mode (LP1+). Thus we need to drop 484 * down to LP0 and wait for vblank in order to make sure the 485 * sprite gets enabled on the next vblank after the register write. 486 * Doing otherwise would risk enabling the sprite one frame after 487 * we've already signalled flip completion. We can resume LP1+ 488 * once the sprite has been enabled. 489 * 490 * With experimental results seems this is needed also for primary 491 * plane, not only sprite plane. 492 */ 493 if (turn_on) 494 return true; 495 496 /* 497 * WaCxSRDisabledForSpriteScaling:ivb 498 * IVB SPR_SCALE/Scaling Enable 499 * "Low Power watermarks must be disabled for at least one 500 * frame before enabling sprite scaling, and kept disabled 501 * until sprite scaling is disabled." 502 * 503 * ILK/SNB DVSASCALE/Scaling Enable 504 * "When in Self Refresh Big FIFO mode, scaling enable will be 505 * masked off while Big FIFO mode is exiting." 506 * 507 * Despite the w/a only being listed for IVB we assume that 508 * the ILK/SNB note has similar ramifications, hence we apply 509 * the w/a on all three platforms. 510 */ 511 return !intel_plane_is_scaled(old_plane_state) && 512 intel_plane_is_scaled(new_plane_state); 513 } 514 515 static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state, 516 struct intel_crtc_state *new_crtc_state, 517 const struct intel_plane_state *old_plane_state, 518 struct intel_plane_state *new_plane_state) 519 { 520 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc); 521 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane); 522 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev); 523 bool mode_changed = intel_crtc_needs_modeset(new_crtc_state); 524 bool was_crtc_enabled = old_crtc_state->hw.active; 525 bool is_crtc_enabled = new_crtc_state->hw.active; 526 bool turn_off, turn_on, visible, was_visible; 527 int ret; 528 529 if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) { 530 ret = skl_update_scaler_plane(new_crtc_state, new_plane_state); 531 if (ret) 532 return ret; 533 } 534 535 was_visible = old_plane_state->uapi.visible; 536 visible = new_plane_state->uapi.visible; 537 538 if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible)) 539 was_visible = false; 540 541 /* 542 * Visibility is calculated as if the crtc was on, but 543 * after scaler setup everything depends on it being off 544 * when the crtc isn't active. 545 * 546 * FIXME this is wrong for watermarks. Watermarks should also 547 * be computed as if the pipe would be active. Perhaps move 548 * per-plane wm computation to the .check_plane() hook, and 549 * only combine the results from all planes in the current place? 550 */ 551 if (!is_crtc_enabled) { 552 intel_plane_set_invisible(new_crtc_state, new_plane_state); 553 visible = false; 554 } 555 556 if (!was_visible && !visible) 557 return 0; 558 559 turn_off = was_visible && (!visible || mode_changed); 560 turn_on = visible && (!was_visible || mode_changed); 561 562 drm_dbg_atomic(&dev_priv->drm, 563 "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n", 564 crtc->base.base.id, crtc->base.name, 565 plane->base.base.id, plane->base.name, 566 was_visible, visible, 567 turn_off, turn_on, mode_changed); 568 569 if (visible || was_visible) 570 new_crtc_state->fb_bits |= plane->frontbuffer_bit; 571 572 if (HAS_GMCH(dev_priv) && 573 i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state)) 574 new_crtc_state->disable_cxsr = true; 575 576 if ((IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv)) && 577 ilk_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state)) 578 new_crtc_state->disable_cxsr = true; 579 580 if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state)) { 581 new_crtc_state->do_async_flip = true; 582 new_crtc_state->async_flip_planes |= BIT(plane->id); 583 } else if (plane->need_async_flip_toggle_wa && 584 new_crtc_state->uapi.async_flip) { 585 /* 586 * On platforms with double buffered async flip bit we 587 * set the bit already one frame early during the sync 588 * flip (see {i9xx,skl}_plane_update_arm()). The 589 * hardware will therefore be ready to perform a real 590 * async flip during the next commit, without having 591 * to wait yet another frame for the bit to latch. 592 */ 593 new_crtc_state->async_flip_planes |= BIT(plane->id); 594 } 595 596 return 0; 597 } 598 599 int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state, 600 struct intel_crtc_state *new_crtc_state, 601 const struct intel_plane_state *old_plane_state, 602 struct intel_plane_state *new_plane_state) 603 { 604 struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane); 605 const struct drm_framebuffer *fb = new_plane_state->hw.fb; 606 int ret; 607 608 intel_plane_set_invisible(new_crtc_state, new_plane_state); 609 new_crtc_state->enabled_planes &= ~BIT(plane->id); 610 611 if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc) 612 return 0; 613 614 ret = plane->check_plane(new_crtc_state, new_plane_state); 615 if (ret) 616 return ret; 617 618 if (fb) 619 new_crtc_state->enabled_planes |= BIT(plane->id); 620 621 /* FIXME pre-g4x don't work like this */ 622 if (new_plane_state->uapi.visible) 623 new_crtc_state->active_planes |= BIT(plane->id); 624 625 if (new_plane_state->uapi.visible && 626 intel_plane_is_scaled(new_plane_state)) 627 new_crtc_state->scaled_planes |= BIT(plane->id); 628 629 if (new_plane_state->uapi.visible && 630 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) 631 new_crtc_state->nv12_planes |= BIT(plane->id); 632 633 if (new_plane_state->uapi.visible && 634 fb->format->format == DRM_FORMAT_C8) 635 new_crtc_state->c8_planes |= BIT(plane->id); 636 637 if (new_plane_state->uapi.visible || old_plane_state->uapi.visible) 638 new_crtc_state->update_planes |= BIT(plane->id); 639 640 if (new_plane_state->uapi.visible && 641 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) { 642 new_crtc_state->data_rate_y[plane->id] = 643 intel_plane_data_rate(new_crtc_state, new_plane_state, 0); 644 new_crtc_state->data_rate[plane->id] = 645 intel_plane_data_rate(new_crtc_state, new_plane_state, 1); 646 647 new_crtc_state->rel_data_rate_y[plane->id] = 648 intel_plane_relative_data_rate(new_crtc_state, 649 new_plane_state, 0); 650 new_crtc_state->rel_data_rate[plane->id] = 651 intel_plane_relative_data_rate(new_crtc_state, 652 new_plane_state, 1); 653 } else if (new_plane_state->uapi.visible) { 654 new_crtc_state->data_rate[plane->id] = 655 intel_plane_data_rate(new_crtc_state, new_plane_state, 0); 656 657 new_crtc_state->rel_data_rate[plane->id] = 658 intel_plane_relative_data_rate(new_crtc_state, 659 new_plane_state, 0); 660 } 661 662 return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state, 663 old_plane_state, new_plane_state); 664 } 665 666 static struct intel_plane * 667 intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id) 668 { 669 struct drm_i915_private *i915 = to_i915(crtc->base.dev); 670 struct intel_plane *plane; 671 672 for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) { 673 if (plane->id == plane_id) 674 return plane; 675 } 676 677 return NULL; 678 } 679 680 int intel_plane_atomic_check(struct intel_atomic_state *state, 681 struct intel_plane *plane) 682 { 683 struct intel_display *display = to_intel_display(state); 684 struct intel_plane_state *new_plane_state = 685 intel_atomic_get_new_plane_state(state, plane); 686 const struct intel_plane_state *old_plane_state = 687 intel_atomic_get_old_plane_state(state, plane); 688 const struct intel_plane_state *new_primary_crtc_plane_state; 689 struct intel_crtc *crtc = intel_crtc_for_pipe(display, plane->pipe); 690 const struct intel_crtc_state *old_crtc_state = 691 intel_atomic_get_old_crtc_state(state, crtc); 692 struct intel_crtc_state *new_crtc_state = 693 intel_atomic_get_new_crtc_state(state, crtc); 694 695 if (new_crtc_state && intel_crtc_is_joiner_secondary(new_crtc_state)) { 696 struct intel_crtc *primary_crtc = 697 intel_primary_crtc(new_crtc_state); 698 struct intel_plane *primary_crtc_plane = 699 intel_crtc_get_plane(primary_crtc, plane->id); 700 701 new_primary_crtc_plane_state = 702 intel_atomic_get_new_plane_state(state, primary_crtc_plane); 703 } else { 704 new_primary_crtc_plane_state = new_plane_state; 705 } 706 707 intel_plane_copy_uapi_to_hw_state(new_plane_state, 708 new_primary_crtc_plane_state, 709 crtc); 710 711 new_plane_state->uapi.visible = false; 712 if (!new_crtc_state) 713 return 0; 714 715 return intel_plane_atomic_check_with_state(old_crtc_state, 716 new_crtc_state, 717 old_plane_state, 718 new_plane_state); 719 } 720 721 static struct intel_plane * 722 skl_next_plane_to_commit(struct intel_atomic_state *state, 723 struct intel_crtc *crtc, 724 struct skl_ddb_entry ddb[I915_MAX_PLANES], 725 struct skl_ddb_entry ddb_y[I915_MAX_PLANES], 726 unsigned int *update_mask) 727 { 728 struct intel_crtc_state *crtc_state = 729 intel_atomic_get_new_crtc_state(state, crtc); 730 struct intel_plane_state __maybe_unused *plane_state; 731 struct intel_plane *plane; 732 int i; 733 734 if (*update_mask == 0) 735 return NULL; 736 737 for_each_new_intel_plane_in_state(state, plane, plane_state, i) { 738 enum plane_id plane_id = plane->id; 739 740 if (crtc->pipe != plane->pipe || 741 !(*update_mask & BIT(plane_id))) 742 continue; 743 744 if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id], 745 ddb, I915_MAX_PLANES, plane_id) || 746 skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id], 747 ddb_y, I915_MAX_PLANES, plane_id)) 748 continue; 749 750 *update_mask &= ~BIT(plane_id); 751 ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id]; 752 ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id]; 753 754 return plane; 755 } 756 757 /* should never happen */ 758 drm_WARN_ON(state->base.dev, 1); 759 760 return NULL; 761 } 762 763 void intel_plane_update_noarm(struct intel_dsb *dsb, 764 struct intel_plane *plane, 765 const struct intel_crtc_state *crtc_state, 766 const struct intel_plane_state *plane_state) 767 { 768 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 769 770 trace_intel_plane_update_noarm(plane, crtc); 771 772 if (plane->update_noarm) 773 plane->update_noarm(dsb, plane, crtc_state, plane_state); 774 } 775 776 void intel_plane_async_flip(struct intel_dsb *dsb, 777 struct intel_plane *plane, 778 const struct intel_crtc_state *crtc_state, 779 const struct intel_plane_state *plane_state, 780 bool async_flip) 781 { 782 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 783 784 trace_intel_plane_async_flip(plane, crtc, async_flip); 785 plane->async_flip(dsb, plane, crtc_state, plane_state, async_flip); 786 } 787 788 void intel_plane_update_arm(struct intel_dsb *dsb, 789 struct intel_plane *plane, 790 const struct intel_crtc_state *crtc_state, 791 const struct intel_plane_state *plane_state) 792 { 793 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 794 795 if (crtc_state->do_async_flip && plane->async_flip) { 796 intel_plane_async_flip(dsb, plane, crtc_state, plane_state, true); 797 return; 798 } 799 800 trace_intel_plane_update_arm(plane, crtc); 801 plane->update_arm(dsb, plane, crtc_state, plane_state); 802 } 803 804 void intel_plane_disable_arm(struct intel_dsb *dsb, 805 struct intel_plane *plane, 806 const struct intel_crtc_state *crtc_state) 807 { 808 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 809 810 trace_intel_plane_disable_arm(plane, crtc); 811 plane->disable_arm(dsb, plane, crtc_state); 812 } 813 814 void intel_crtc_planes_update_noarm(struct intel_dsb *dsb, 815 struct intel_atomic_state *state, 816 struct intel_crtc *crtc) 817 { 818 struct intel_crtc_state *new_crtc_state = 819 intel_atomic_get_new_crtc_state(state, crtc); 820 u32 update_mask = new_crtc_state->update_planes; 821 struct intel_plane_state *new_plane_state; 822 struct intel_plane *plane; 823 int i; 824 825 if (new_crtc_state->do_async_flip) 826 return; 827 828 /* 829 * Since we only write non-arming registers here, 830 * the order does not matter even for skl+. 831 */ 832 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) { 833 if (crtc->pipe != plane->pipe || 834 !(update_mask & BIT(plane->id))) 835 continue; 836 837 /* TODO: for mailbox updates this should be skipped */ 838 if (new_plane_state->uapi.visible || 839 new_plane_state->planar_slave) 840 intel_plane_update_noarm(dsb, plane, 841 new_crtc_state, new_plane_state); 842 } 843 } 844 845 static void skl_crtc_planes_update_arm(struct intel_dsb *dsb, 846 struct intel_atomic_state *state, 847 struct intel_crtc *crtc) 848 { 849 struct intel_crtc_state *old_crtc_state = 850 intel_atomic_get_old_crtc_state(state, crtc); 851 struct intel_crtc_state *new_crtc_state = 852 intel_atomic_get_new_crtc_state(state, crtc); 853 struct skl_ddb_entry ddb[I915_MAX_PLANES]; 854 struct skl_ddb_entry ddb_y[I915_MAX_PLANES]; 855 u32 update_mask = new_crtc_state->update_planes; 856 struct intel_plane *plane; 857 858 memcpy(ddb, old_crtc_state->wm.skl.plane_ddb, 859 sizeof(old_crtc_state->wm.skl.plane_ddb)); 860 memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y, 861 sizeof(old_crtc_state->wm.skl.plane_ddb_y)); 862 863 while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) { 864 struct intel_plane_state *new_plane_state = 865 intel_atomic_get_new_plane_state(state, plane); 866 867 /* 868 * TODO: for mailbox updates intel_plane_update_noarm() 869 * would have to be called here as well. 870 */ 871 if (new_plane_state->uapi.visible || 872 new_plane_state->planar_slave) 873 intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state); 874 else 875 intel_plane_disable_arm(dsb, plane, new_crtc_state); 876 } 877 } 878 879 static void i9xx_crtc_planes_update_arm(struct intel_dsb *dsb, 880 struct intel_atomic_state *state, 881 struct intel_crtc *crtc) 882 { 883 struct intel_crtc_state *new_crtc_state = 884 intel_atomic_get_new_crtc_state(state, crtc); 885 u32 update_mask = new_crtc_state->update_planes; 886 struct intel_plane_state *new_plane_state; 887 struct intel_plane *plane; 888 int i; 889 890 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) { 891 if (crtc->pipe != plane->pipe || 892 !(update_mask & BIT(plane->id))) 893 continue; 894 895 /* 896 * TODO: for mailbox updates intel_plane_update_noarm() 897 * would have to be called here as well. 898 */ 899 if (new_plane_state->uapi.visible) 900 intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state); 901 else 902 intel_plane_disable_arm(dsb, plane, new_crtc_state); 903 } 904 } 905 906 void intel_crtc_planes_update_arm(struct intel_dsb *dsb, 907 struct intel_atomic_state *state, 908 struct intel_crtc *crtc) 909 { 910 struct drm_i915_private *i915 = to_i915(state->base.dev); 911 912 if (DISPLAY_VER(i915) >= 9) 913 skl_crtc_planes_update_arm(dsb, state, crtc); 914 else 915 i9xx_crtc_planes_update_arm(dsb, state, crtc); 916 } 917 918 int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state, 919 struct intel_crtc_state *crtc_state, 920 int min_scale, int max_scale, 921 bool can_position) 922 { 923 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev); 924 struct drm_framebuffer *fb = plane_state->hw.fb; 925 struct drm_rect *src = &plane_state->uapi.src; 926 struct drm_rect *dst = &plane_state->uapi.dst; 927 const struct drm_rect *clip = &crtc_state->pipe_src; 928 unsigned int rotation = plane_state->hw.rotation; 929 int hscale, vscale; 930 931 if (!fb) { 932 plane_state->uapi.visible = false; 933 return 0; 934 } 935 936 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation); 937 938 /* Check scaling */ 939 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale); 940 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale); 941 if (hscale < 0 || vscale < 0) { 942 drm_dbg_kms(&i915->drm, "Invalid scaling of plane\n"); 943 drm_rect_debug_print("src: ", src, true); 944 drm_rect_debug_print("dst: ", dst, false); 945 return -ERANGE; 946 } 947 948 /* 949 * FIXME: This might need further adjustment for seamless scaling 950 * with phase information, for the 2p2 and 2p1 scenarios. 951 */ 952 plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip); 953 954 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation); 955 956 if (!can_position && plane_state->uapi.visible && 957 !drm_rect_equals(dst, clip)) { 958 drm_dbg_kms(&i915->drm, "Plane must cover entire CRTC\n"); 959 drm_rect_debug_print("dst: ", dst, false); 960 drm_rect_debug_print("clip: ", clip, false); 961 return -EINVAL; 962 } 963 964 /* final plane coordinates will be relative to the plane's pipe */ 965 drm_rect_translate(dst, -clip->x1, -clip->y1); 966 967 return 0; 968 } 969 970 int intel_plane_check_src_coordinates(struct intel_plane_state *plane_state) 971 { 972 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev); 973 const struct drm_framebuffer *fb = plane_state->hw.fb; 974 struct drm_rect *src = &plane_state->uapi.src; 975 u32 src_x, src_y, src_w, src_h, hsub, vsub; 976 bool rotated = drm_rotation_90_or_270(plane_state->hw.rotation); 977 978 /* 979 * FIXME hsub/vsub vs. block size is a mess. Pre-tgl CCS 980 * abuses hsub/vsub so we can't use them here. But as they 981 * are limited to 32bpp RGB formats we don't actually need 982 * to check anything. 983 */ 984 if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS || 985 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) 986 return 0; 987 988 /* 989 * Hardware doesn't handle subpixel coordinates. 990 * Adjust to (macro)pixel boundary, but be careful not to 991 * increase the source viewport size, because that could 992 * push the downscaling factor out of bounds. 993 */ 994 src_x = src->x1 >> 16; 995 src_w = drm_rect_width(src) >> 16; 996 src_y = src->y1 >> 16; 997 src_h = drm_rect_height(src) >> 16; 998 999 drm_rect_init(src, src_x << 16, src_y << 16, 1000 src_w << 16, src_h << 16); 1001 1002 if (fb->format->format == DRM_FORMAT_RGB565 && rotated) { 1003 hsub = 2; 1004 vsub = 2; 1005 } else if (DISPLAY_VER(i915) >= 20 && 1006 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) { 1007 /* 1008 * This allows NV12 and P0xx formats to have odd size and/or odd 1009 * source coordinates on DISPLAY_VER(i915) >= 20 1010 */ 1011 hsub = 1; 1012 vsub = 1; 1013 1014 /* Wa_16023981245 */ 1015 if ((DISPLAY_VERx100(i915) == 2000 || 1016 DISPLAY_VERx100(i915) == 3000) && 1017 src_x % 2 != 0) 1018 hsub = 2; 1019 } else { 1020 hsub = fb->format->hsub; 1021 vsub = fb->format->vsub; 1022 } 1023 1024 if (rotated) 1025 hsub = vsub = max(hsub, vsub); 1026 1027 if (src_x % hsub || src_w % hsub) { 1028 drm_dbg_kms(&i915->drm, "src x/w (%u, %u) must be a multiple of %u (rotated: %s)\n", 1029 src_x, src_w, hsub, str_yes_no(rotated)); 1030 return -EINVAL; 1031 } 1032 1033 if (src_y % vsub || src_h % vsub) { 1034 drm_dbg_kms(&i915->drm, "src y/h (%u, %u) must be a multiple of %u (rotated: %s)\n", 1035 src_y, src_h, vsub, str_yes_no(rotated)); 1036 return -EINVAL; 1037 } 1038 1039 return 0; 1040 } 1041 1042 static int add_dma_resv_fences(struct dma_resv *resv, 1043 struct drm_plane_state *new_plane_state) 1044 { 1045 struct dma_fence *fence = dma_fence_get(new_plane_state->fence); 1046 struct dma_fence *new; 1047 int ret; 1048 1049 ret = dma_resv_get_singleton(resv, dma_resv_usage_rw(false), &new); 1050 if (ret) 1051 goto error; 1052 1053 if (new && fence) { 1054 struct dma_fence_chain *chain = dma_fence_chain_alloc(); 1055 1056 if (!chain) { 1057 ret = -ENOMEM; 1058 goto error; 1059 } 1060 1061 dma_fence_chain_init(chain, fence, new, 1); 1062 fence = &chain->base; 1063 1064 } else if (new) { 1065 fence = new; 1066 } 1067 1068 dma_fence_put(new_plane_state->fence); 1069 new_plane_state->fence = fence; 1070 return 0; 1071 1072 error: 1073 dma_fence_put(fence); 1074 return ret; 1075 } 1076 1077 /** 1078 * intel_prepare_plane_fb - Prepare fb for usage on plane 1079 * @_plane: drm plane to prepare for 1080 * @_new_plane_state: the plane state being prepared 1081 * 1082 * Prepares a framebuffer for usage on a display plane. Generally this 1083 * involves pinning the underlying object and updating the frontbuffer tracking 1084 * bits. Some older platforms need special physical address handling for 1085 * cursor planes. 1086 * 1087 * Returns 0 on success, negative error code on failure. 1088 */ 1089 static int 1090 intel_prepare_plane_fb(struct drm_plane *_plane, 1091 struct drm_plane_state *_new_plane_state) 1092 { 1093 struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY }; 1094 struct intel_plane *plane = to_intel_plane(_plane); 1095 struct intel_plane_state *new_plane_state = 1096 to_intel_plane_state(_new_plane_state); 1097 struct intel_atomic_state *state = 1098 to_intel_atomic_state(new_plane_state->uapi.state); 1099 struct drm_i915_private *dev_priv = to_i915(plane->base.dev); 1100 struct intel_plane_state *old_plane_state = 1101 intel_atomic_get_old_plane_state(state, plane); 1102 struct drm_gem_object *obj = intel_fb_bo(new_plane_state->hw.fb); 1103 struct drm_gem_object *old_obj = intel_fb_bo(old_plane_state->hw.fb); 1104 int ret; 1105 1106 if (old_obj) { 1107 const struct intel_crtc_state *new_crtc_state = 1108 intel_atomic_get_new_crtc_state(state, 1109 to_intel_crtc(old_plane_state->hw.crtc)); 1110 1111 /* Big Hammer, we also need to ensure that any pending 1112 * MI_WAIT_FOR_EVENT inside a user batch buffer on the 1113 * current scanout is retired before unpinning the old 1114 * framebuffer. Note that we rely on userspace rendering 1115 * into the buffer attached to the pipe they are waiting 1116 * on. If not, userspace generates a GPU hang with IPEHR 1117 * point to the MI_WAIT_FOR_EVENT. 1118 * 1119 * This should only fail upon a hung GPU, in which case we 1120 * can safely continue. 1121 */ 1122 if (new_crtc_state && intel_crtc_needs_modeset(new_crtc_state)) { 1123 ret = add_dma_resv_fences(old_obj->resv, 1124 &new_plane_state->uapi); 1125 if (ret < 0) 1126 return ret; 1127 } 1128 } 1129 1130 if (!obj) 1131 return 0; 1132 1133 ret = intel_plane_pin_fb(new_plane_state); 1134 if (ret) 1135 return ret; 1136 1137 ret = drm_gem_plane_helper_prepare_fb(&plane->base, &new_plane_state->uapi); 1138 if (ret < 0) 1139 goto unpin_fb; 1140 1141 if (new_plane_state->uapi.fence) { 1142 i915_gem_fence_wait_priority(new_plane_state->uapi.fence, 1143 &attr); 1144 1145 intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc, 1146 new_plane_state->uapi.fence); 1147 } 1148 1149 /* 1150 * We declare pageflips to be interactive and so merit a small bias 1151 * towards upclocking to deliver the frame on time. By only changing 1152 * the RPS thresholds to sample more regularly and aim for higher 1153 * clocks we can hopefully deliver low power workloads (like kodi) 1154 * that are not quite steady state without resorting to forcing 1155 * maximum clocks following a vblank miss (see do_rps_boost()). 1156 */ 1157 intel_display_rps_mark_interactive(dev_priv, state, true); 1158 1159 return 0; 1160 1161 unpin_fb: 1162 intel_plane_unpin_fb(new_plane_state); 1163 1164 return ret; 1165 } 1166 1167 /** 1168 * intel_cleanup_plane_fb - Cleans up an fb after plane use 1169 * @plane: drm plane to clean up for 1170 * @_old_plane_state: the state from the previous modeset 1171 * 1172 * Cleans up a framebuffer that has just been removed from a plane. 1173 */ 1174 static void 1175 intel_cleanup_plane_fb(struct drm_plane *plane, 1176 struct drm_plane_state *_old_plane_state) 1177 { 1178 struct intel_plane_state *old_plane_state = 1179 to_intel_plane_state(_old_plane_state); 1180 struct intel_atomic_state *state = 1181 to_intel_atomic_state(old_plane_state->uapi.state); 1182 struct drm_i915_private *dev_priv = to_i915(plane->dev); 1183 struct drm_gem_object *obj = intel_fb_bo(old_plane_state->hw.fb); 1184 1185 if (!obj) 1186 return; 1187 1188 intel_display_rps_mark_interactive(dev_priv, state, false); 1189 1190 intel_plane_unpin_fb(old_plane_state); 1191 } 1192 1193 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = { 1194 .prepare_fb = intel_prepare_plane_fb, 1195 .cleanup_fb = intel_cleanup_plane_fb, 1196 }; 1197 1198 void intel_plane_helper_add(struct intel_plane *plane) 1199 { 1200 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs); 1201 } 1202 1203 void intel_plane_init_cursor_vblank_work(struct intel_plane_state *old_plane_state, 1204 struct intel_plane_state *new_plane_state) 1205 { 1206 if (!old_plane_state->ggtt_vma || 1207 old_plane_state->ggtt_vma == new_plane_state->ggtt_vma) 1208 return; 1209 1210 drm_vblank_work_init(&old_plane_state->unpin_work, old_plane_state->uapi.crtc, 1211 intel_cursor_unpin_work); 1212 } 1213