1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2020 Intel Corporation 4 */ 5 #include <linux/kernel.h> 6 7 #include <drm/drm_atomic_helper.h> 8 #include <drm/drm_atomic_uapi.h> 9 #include <drm/drm_blend.h> 10 #include <drm/drm_damage_helper.h> 11 #include <drm/drm_fourcc.h> 12 #include <drm/drm_print.h> 13 #include <drm/drm_vblank.h> 14 15 #include "intel_atomic.h" 16 #include "intel_crtc.h" 17 #include "intel_cursor.h" 18 #include "intel_cursor_regs.h" 19 #include "intel_de.h" 20 #include "intel_display.h" 21 #include "intel_display_types.h" 22 #include "intel_display_utils.h" 23 #include "intel_display_wa.h" 24 #include "intel_fb.h" 25 #include "intel_frontbuffer.h" 26 #include "intel_plane.h" 27 #include "intel_psr.h" 28 #include "intel_psr_regs.h" 29 #include "intel_vblank.h" 30 #include "skl_watermark.h" 31 32 /* Cursor formats */ 33 static const u32 intel_cursor_formats[] = { 34 DRM_FORMAT_ARGB8888, 35 }; 36 37 static u32 intel_cursor_surf_offset(const struct intel_plane_state *plane_state) 38 { 39 return plane_state->view.color_plane[0].offset; 40 } 41 42 static u32 intel_cursor_position(const struct intel_crtc_state *crtc_state, 43 const struct intel_plane_state *plane_state, 44 bool early_tpt) 45 { 46 int x = plane_state->uapi.dst.x1; 47 int y = plane_state->uapi.dst.y1; 48 u32 pos = 0; 49 50 /* 51 * Formula from Bspec: 52 * MAX(-1 * <Cursor vertical size from CUR_CTL base on cursor mode 53 * select setting> + 1, CUR_POS Y Position - Update region Y position 54 */ 55 if (early_tpt) 56 y = max(-1 * drm_rect_height(&plane_state->uapi.dst) + 1, 57 y - crtc_state->psr2_su_area.y1); 58 59 if (x < 0) { 60 pos |= CURSOR_POS_X_SIGN; 61 x = -x; 62 } 63 pos |= CURSOR_POS_X(x); 64 65 if (y < 0) { 66 pos |= CURSOR_POS_Y_SIGN; 67 y = -y; 68 } 69 pos |= CURSOR_POS_Y(y); 70 71 return pos; 72 } 73 74 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state) 75 { 76 const struct drm_mode_config *config = 77 &plane_state->uapi.plane->dev->mode_config; 78 int width = drm_rect_width(&plane_state->uapi.dst); 79 int height = drm_rect_height(&plane_state->uapi.dst); 80 81 return width > 0 && width <= config->cursor_width && 82 height > 0 && height <= config->cursor_height; 83 } 84 85 static int intel_cursor_check_surface(struct intel_plane_state *plane_state) 86 { 87 struct intel_display *display = to_intel_display(plane_state); 88 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 89 unsigned int rotation = plane_state->hw.rotation; 90 int src_x, src_y; 91 u32 offset; 92 int ret; 93 94 ret = intel_plane_compute_gtt(plane_state); 95 if (ret) 96 return ret; 97 98 if (!plane_state->uapi.visible) 99 return 0; 100 101 src_x = plane_state->uapi.src.x1 >> 16; 102 src_y = plane_state->uapi.src.y1 >> 16; 103 104 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0); 105 offset = intel_plane_compute_aligned_offset(&src_x, &src_y, 106 plane_state, 0); 107 108 if (src_x != 0 || src_y != 0) { 109 drm_dbg_kms(display->drm, 110 "[PLANE:%d:%s] arbitrary cursor panning not supported\n", 111 plane->base.base.id, plane->base.name); 112 return -EINVAL; 113 } 114 115 /* 116 * Put the final coordinates back so that the src 117 * coordinate checks will see the right values. 118 */ 119 drm_rect_translate_to(&plane_state->uapi.src, 120 src_x << 16, src_y << 16); 121 122 /* ILK+ do this automagically in hardware */ 123 if (HAS_GMCH(display) && rotation & DRM_MODE_ROTATE_180) { 124 const struct drm_framebuffer *fb = plane_state->hw.fb; 125 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16; 126 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16; 127 128 offset += (src_h * src_w - 1) * fb->format->cpp[0]; 129 } 130 131 plane_state->view.color_plane[0].offset = offset; 132 plane_state->view.color_plane[0].x = src_x; 133 plane_state->view.color_plane[0].y = src_y; 134 135 return 0; 136 } 137 138 static int intel_check_cursor(struct intel_crtc_state *crtc_state, 139 struct intel_plane_state *plane_state) 140 { 141 struct intel_display *display = to_intel_display(plane_state); 142 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 143 const struct drm_framebuffer *fb = plane_state->hw.fb; 144 const struct drm_rect src = plane_state->uapi.src; 145 const struct drm_rect dst = plane_state->uapi.dst; 146 int ret; 147 148 if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) { 149 drm_dbg_kms(display->drm, "[PLANE:%d:%s] cursor cannot be tiled\n", 150 plane->base.base.id, plane->base.name); 151 return -EINVAL; 152 } 153 154 ret = intel_plane_check_clipping(plane_state, crtc_state, 155 DRM_PLANE_NO_SCALING, 156 DRM_PLANE_NO_SCALING, 157 true); 158 if (ret) 159 return ret; 160 161 /* Use the unclipped src/dst rectangles, which we program to hw */ 162 plane_state->uapi.src = src; 163 plane_state->uapi.dst = dst; 164 165 /* final plane coordinates will be relative to the plane's pipe */ 166 drm_rect_translate(&plane_state->uapi.dst, 167 -crtc_state->pipe_src.x1, 168 -crtc_state->pipe_src.y1); 169 170 ret = intel_cursor_check_surface(plane_state); 171 if (ret) 172 return ret; 173 174 if (!plane_state->uapi.visible) 175 return 0; 176 177 ret = intel_plane_check_src_coordinates(plane_state); 178 if (ret) 179 return ret; 180 181 return 0; 182 } 183 184 static unsigned int 185 i845_cursor_max_stride(struct intel_plane *plane, 186 const struct drm_format_info *info, 187 u64 modifier, unsigned int rotation) 188 { 189 return 2048; 190 } 191 192 static unsigned int i845_cursor_min_alignment(struct intel_plane *plane, 193 const struct drm_framebuffer *fb, 194 int color_plane) 195 { 196 return 32; 197 } 198 199 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state) 200 { 201 u32 cntl = 0; 202 203 if (crtc_state->gamma_enable) 204 cntl |= CURSOR_PIPE_GAMMA_ENABLE; 205 206 return cntl; 207 } 208 209 static u32 i845_cursor_ctl(const struct intel_plane_state *plane_state) 210 { 211 return CURSOR_ENABLE | 212 CURSOR_FORMAT_ARGB | 213 CURSOR_STRIDE(plane_state->view.color_plane[0].mapping_stride); 214 } 215 216 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state) 217 { 218 int width = drm_rect_width(&plane_state->uapi.dst); 219 220 /* 221 * 845g/865g are only limited by the width of their cursors, 222 * the height is arbitrary up to the precision of the register. 223 */ 224 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64); 225 } 226 227 static int i845_check_cursor(struct intel_crtc_state *crtc_state, 228 struct intel_plane_state *plane_state) 229 { 230 struct intel_display *display = to_intel_display(plane_state); 231 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 232 const struct drm_framebuffer *fb = plane_state->hw.fb; 233 int ret; 234 235 ret = intel_check_cursor(crtc_state, plane_state); 236 if (ret) 237 return ret; 238 239 /* if we want to turn off the cursor ignore width and height */ 240 if (!fb) 241 return 0; 242 243 /* Check for which cursor types we support */ 244 if (!i845_cursor_size_ok(plane_state)) { 245 drm_dbg_kms(display->drm, 246 "[PLANE:%d:%s] cursor dimension %dx%d not supported\n", 247 plane->base.base.id, plane->base.name, 248 drm_rect_width(&plane_state->uapi.dst), 249 drm_rect_height(&plane_state->uapi.dst)); 250 return -EINVAL; 251 } 252 253 drm_WARN_ON(display->drm, plane_state->uapi.visible && 254 plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]); 255 256 switch (fb->pitches[0]) { 257 case 256: 258 case 512: 259 case 1024: 260 case 2048: 261 break; 262 default: 263 drm_dbg_kms(display->drm, "[PLANE:%d:%s] invalid cursor stride (%u)\n", 264 plane->base.base.id, plane->base.name, 265 fb->pitches[0]); 266 return -EINVAL; 267 } 268 269 plane_state->ctl = i845_cursor_ctl(plane_state); 270 271 return 0; 272 } 273 274 /* TODO: split into noarm+arm pair */ 275 static void i845_cursor_update_arm(struct intel_dsb *dsb, 276 struct intel_plane *plane, 277 const struct intel_crtc_state *crtc_state, 278 const struct intel_plane_state *plane_state) 279 { 280 struct intel_display *display = to_intel_display(plane); 281 u32 cntl = 0, base = 0, pos = 0, size = 0; 282 283 if (plane_state && plane_state->uapi.visible) { 284 unsigned int width = drm_rect_width(&plane_state->uapi.dst); 285 unsigned int height = drm_rect_height(&plane_state->uapi.dst); 286 287 cntl = plane_state->ctl | 288 i845_cursor_ctl_crtc(crtc_state); 289 290 size = CURSOR_HEIGHT(height) | CURSOR_WIDTH(width); 291 292 base = plane_state->surf; 293 pos = intel_cursor_position(crtc_state, plane_state, false); 294 } 295 296 /* On these chipsets we can only modify the base/size/stride 297 * whilst the cursor is disabled. 298 */ 299 if (plane->cursor.base != base || 300 plane->cursor.size != size || 301 plane->cursor.cntl != cntl) { 302 intel_de_write_fw(display, CURCNTR(display, PIPE_A), 0); 303 intel_de_write_fw(display, CURBASE(display, PIPE_A), base); 304 intel_de_write_fw(display, CURSIZE(display, PIPE_A), size); 305 intel_de_write_fw(display, CURPOS(display, PIPE_A), pos); 306 intel_de_write_fw(display, CURCNTR(display, PIPE_A), cntl); 307 308 plane->cursor.base = base; 309 plane->cursor.size = size; 310 plane->cursor.cntl = cntl; 311 } else { 312 intel_de_write_fw(display, CURPOS(display, PIPE_A), pos); 313 } 314 } 315 316 static void i845_cursor_disable_arm(struct intel_dsb *dsb, 317 struct intel_plane *plane, 318 const struct intel_crtc_state *crtc_state) 319 { 320 i845_cursor_update_arm(dsb, plane, crtc_state, NULL); 321 } 322 323 static bool i845_cursor_get_hw_state(struct intel_plane *plane, 324 enum pipe *pipe) 325 { 326 struct intel_display *display = to_intel_display(plane); 327 enum intel_display_power_domain power_domain; 328 struct ref_tracker *wakeref; 329 bool ret; 330 331 power_domain = POWER_DOMAIN_PIPE(PIPE_A); 332 wakeref = intel_display_power_get_if_enabled(display, power_domain); 333 if (!wakeref) 334 return false; 335 336 ret = intel_de_read(display, CURCNTR(display, PIPE_A)) & CURSOR_ENABLE; 337 338 *pipe = PIPE_A; 339 340 intel_display_power_put(display, power_domain, wakeref); 341 342 return ret; 343 } 344 345 static unsigned int 346 i9xx_cursor_max_stride(struct intel_plane *plane, 347 const struct drm_format_info *info, 348 u64 modifier, unsigned int rotation) 349 { 350 return plane->base.dev->mode_config.cursor_width * 4; 351 } 352 353 static unsigned int i830_cursor_min_alignment(struct intel_plane *plane, 354 const struct drm_framebuffer *fb, 355 int color_plane) 356 { 357 /* "AlmadorM Errata – Requires 32-bpp cursor data to be 16KB aligned." */ 358 return 16 * 1024; /* physical */ 359 } 360 361 static unsigned int i85x_cursor_min_alignment(struct intel_plane *plane, 362 const struct drm_framebuffer *fb, 363 int color_plane) 364 { 365 return 256; /* physical */ 366 } 367 368 static unsigned int i9xx_cursor_min_alignment(struct intel_plane *plane, 369 const struct drm_framebuffer *fb, 370 int color_plane) 371 { 372 struct intel_display *display = to_intel_display(plane); 373 374 if (intel_scanout_needs_vtd_wa(display)) 375 return 64 * 1024; 376 377 return 4 * 1024; /* physical for i915/i945 */ 378 } 379 380 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state) 381 { 382 struct intel_display *display = to_intel_display(crtc_state); 383 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 384 u32 cntl = 0; 385 386 if (DISPLAY_VER(display) >= 11) 387 return cntl; 388 389 if (crtc_state->gamma_enable) 390 cntl = MCURSOR_PIPE_GAMMA_ENABLE; 391 392 if (crtc_state->csc_enable) 393 cntl |= MCURSOR_PIPE_CSC_ENABLE; 394 395 if (DISPLAY_VER(display) < 5 && !display->platform.g4x) 396 cntl |= MCURSOR_PIPE_SEL(crtc->pipe); 397 398 return cntl; 399 } 400 401 static u32 i9xx_cursor_ctl(const struct intel_plane_state *plane_state) 402 { 403 struct intel_display *display = to_intel_display(plane_state); 404 u32 cntl = 0; 405 406 if (display->platform.sandybridge || display->platform.ivybridge) 407 cntl |= MCURSOR_TRICKLE_FEED_DISABLE; 408 409 switch (drm_rect_width(&plane_state->uapi.dst)) { 410 case 64: 411 cntl |= MCURSOR_MODE_64_ARGB_AX; 412 break; 413 case 128: 414 cntl |= MCURSOR_MODE_128_ARGB_AX; 415 break; 416 case 256: 417 cntl |= MCURSOR_MODE_256_ARGB_AX; 418 break; 419 default: 420 MISSING_CASE(drm_rect_width(&plane_state->uapi.dst)); 421 return 0; 422 } 423 424 if (plane_state->hw.rotation & DRM_MODE_ROTATE_180) 425 cntl |= MCURSOR_ROTATE_180; 426 427 /* Wa_22012358565:adl-p */ 428 if (intel_display_wa(display, INTEL_DISPLAY_WA_22012358565)) 429 cntl |= MCURSOR_ARB_SLOTS(1); 430 431 return cntl; 432 } 433 434 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state) 435 { 436 struct intel_display *display = to_intel_display(plane_state); 437 int width = drm_rect_width(&plane_state->uapi.dst); 438 int height = drm_rect_height(&plane_state->uapi.dst); 439 440 if (!intel_cursor_size_ok(plane_state)) 441 return false; 442 443 /* Cursor width is limited to a few power-of-two sizes */ 444 switch (width) { 445 case 256: 446 case 128: 447 case 64: 448 break; 449 default: 450 return false; 451 } 452 453 /* 454 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor 455 * height from 8 lines up to the cursor width, when the 456 * cursor is not rotated. Everything else requires square 457 * cursors. 458 */ 459 if (HAS_CUR_FBC(display) && 460 plane_state->hw.rotation & DRM_MODE_ROTATE_0) { 461 if (height < 8 || height > width) 462 return false; 463 } else { 464 if (height != width) 465 return false; 466 } 467 468 return true; 469 } 470 471 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state, 472 struct intel_plane_state *plane_state) 473 { 474 struct intel_display *display = to_intel_display(plane_state); 475 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 476 const struct drm_framebuffer *fb = plane_state->hw.fb; 477 enum pipe pipe = plane->pipe; 478 int ret; 479 480 ret = intel_check_cursor(crtc_state, plane_state); 481 if (ret) 482 return ret; 483 484 /* if we want to turn off the cursor ignore width and height */ 485 if (!fb) 486 return 0; 487 488 /* Check for which cursor types we support */ 489 if (!i9xx_cursor_size_ok(plane_state)) { 490 drm_dbg_kms(display->drm, 491 "[PLANE:%d:%s] cursor dimension %dx%d not supported\n", 492 plane->base.base.id, plane->base.name, 493 drm_rect_width(&plane_state->uapi.dst), 494 drm_rect_height(&plane_state->uapi.dst)); 495 return -EINVAL; 496 } 497 498 drm_WARN_ON(display->drm, plane_state->uapi.visible && 499 plane_state->view.color_plane[0].mapping_stride != fb->pitches[0]); 500 501 if (fb->pitches[0] != 502 drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) { 503 drm_dbg_kms(display->drm, 504 "[PLANE:%d:%s] invalid cursor stride (%u) (cursor width %d)\n", 505 plane->base.base.id, plane->base.name, 506 fb->pitches[0], drm_rect_width(&plane_state->uapi.dst)); 507 return -EINVAL; 508 } 509 510 /* 511 * There's something wrong with the cursor on CHV pipe C. 512 * If it straddles the left edge of the screen then 513 * moving it away from the edge or disabling it often 514 * results in a pipe underrun, and often that can lead to 515 * dead pipe (constant underrun reported, and it scans 516 * out just a solid color). To recover from that, the 517 * display power well must be turned off and on again. 518 * Refuse the put the cursor into that compromised position. 519 */ 520 if (display->platform.cherryview && pipe == PIPE_C && 521 plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) { 522 drm_dbg_kms(display->drm, 523 "[PLANE:%d:%s] cursor not allowed to straddle the left screen edge\n", 524 plane->base.base.id, plane->base.name); 525 return -EINVAL; 526 } 527 528 plane_state->ctl = i9xx_cursor_ctl(plane_state); 529 530 return 0; 531 } 532 533 static void i9xx_cursor_disable_sel_fetch_arm(struct intel_dsb *dsb, 534 struct intel_plane *plane, 535 const struct intel_crtc_state *crtc_state) 536 { 537 struct intel_display *display = to_intel_display(plane); 538 enum pipe pipe = plane->pipe; 539 540 if (!crtc_state->enable_psr2_sel_fetch && 541 !crtc_state->clear_psr2_sel_fetch) 542 return; 543 544 intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), 0); 545 } 546 547 static void wa_16021440873(struct intel_dsb *dsb, 548 struct intel_plane *plane, 549 const struct intel_crtc_state *crtc_state, 550 const struct intel_plane_state *plane_state) 551 { 552 struct intel_display *display = to_intel_display(plane); 553 u32 ctl = plane_state->ctl; 554 int et_y_position = drm_rect_height(&crtc_state->pipe_src) + 1; 555 enum pipe pipe = plane->pipe; 556 557 ctl &= ~MCURSOR_MODE_MASK; 558 ctl |= MCURSOR_MODE_64_2B; 559 560 intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), ctl); 561 562 intel_de_write_dsb(display, dsb, CURPOS_ERLY_TPT(display, pipe), 563 CURSOR_POS_Y(et_y_position)); 564 } 565 566 static void i9xx_cursor_update_sel_fetch_arm(struct intel_dsb *dsb, 567 struct intel_plane *plane, 568 const struct intel_crtc_state *crtc_state, 569 const struct intel_plane_state *plane_state) 570 { 571 struct intel_display *display = to_intel_display(plane); 572 enum pipe pipe = plane->pipe; 573 574 if (!crtc_state->enable_psr2_sel_fetch) { 575 i9xx_cursor_disable_sel_fetch_arm(dsb, plane, crtc_state); 576 return; 577 } 578 579 if (drm_rect_height(&plane_state->psr2_sel_fetch_area) > 0) { 580 if (crtc_state->enable_psr2_su_region_et) { 581 u32 val = intel_cursor_position(crtc_state, plane_state, 582 true); 583 584 intel_de_write_dsb(display, dsb, CURPOS_ERLY_TPT(display, pipe), val); 585 } 586 587 intel_de_write_dsb(display, dsb, SEL_FETCH_CUR_CTL(pipe), plane_state->ctl); 588 } else { 589 /* Wa_16021440873 */ 590 if (crtc_state->enable_psr2_su_region_et) 591 wa_16021440873(dsb, plane, crtc_state, plane_state); 592 else 593 i9xx_cursor_disable_sel_fetch_arm(dsb, plane, crtc_state); 594 } 595 } 596 597 static u32 skl_cursor_ddb_reg_val(const struct skl_ddb_entry *entry) 598 { 599 if (!entry->end) 600 return 0; 601 602 return CUR_BUF_END(entry->end - 1) | 603 CUR_BUF_START(entry->start); 604 } 605 606 static u32 skl_cursor_wm_reg_val(const struct skl_wm_level *level) 607 { 608 u32 val = 0; 609 610 if (level->enable) 611 val |= CUR_WM_EN; 612 if (level->ignore_lines) 613 val |= CUR_WM_IGNORE_LINES; 614 val |= REG_FIELD_PREP(CUR_WM_BLOCKS_MASK, level->blocks); 615 val |= REG_FIELD_PREP(CUR_WM_LINES_MASK, level->lines); 616 617 return val; 618 } 619 620 static void skl_write_cursor_wm(struct intel_dsb *dsb, 621 struct intel_plane *plane, 622 const struct intel_crtc_state *crtc_state) 623 { 624 struct intel_display *display = to_intel_display(plane->base.dev); 625 enum plane_id plane_id = plane->id; 626 enum pipe pipe = plane->pipe; 627 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal; 628 const struct skl_ddb_entry *ddb = 629 &crtc_state->wm.skl.plane_ddb[plane_id]; 630 int level; 631 632 for (level = 0; level < display->wm.num_levels; level++) 633 intel_de_write_dsb(display, dsb, CUR_WM(pipe, level), 634 skl_cursor_wm_reg_val(skl_plane_wm_level(pipe_wm, plane_id, level))); 635 636 intel_de_write_dsb(display, dsb, CUR_WM_TRANS(pipe), 637 skl_cursor_wm_reg_val(skl_plane_trans_wm(pipe_wm, plane_id))); 638 639 if (HAS_HW_SAGV_WM(display)) { 640 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id]; 641 642 intel_de_write_dsb(display, dsb, CUR_WM_SAGV(pipe), 643 skl_cursor_wm_reg_val(&wm->sagv.wm0)); 644 intel_de_write_dsb(display, dsb, CUR_WM_SAGV_TRANS(pipe), 645 skl_cursor_wm_reg_val(&wm->sagv.trans_wm)); 646 } 647 648 intel_de_write_dsb(display, dsb, CUR_BUF_CFG(pipe), 649 skl_cursor_ddb_reg_val(ddb)); 650 } 651 652 /* TODO: split into noarm+arm pair */ 653 static void i9xx_cursor_update_arm(struct intel_dsb *dsb, 654 struct intel_plane *plane, 655 const struct intel_crtc_state *crtc_state, 656 const struct intel_plane_state *plane_state) 657 { 658 struct intel_display *display = to_intel_display(plane); 659 enum pipe pipe = plane->pipe; 660 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0; 661 662 if (plane_state && plane_state->uapi.visible) { 663 int width = drm_rect_width(&plane_state->uapi.dst); 664 int height = drm_rect_height(&plane_state->uapi.dst); 665 666 cntl = plane_state->ctl | 667 i9xx_cursor_ctl_crtc(crtc_state); 668 669 if (DISPLAY_VER(display) < 14 && width != height) 670 fbc_ctl = CUR_FBC_EN | CUR_FBC_HEIGHT(height - 1); 671 672 base = plane_state->surf; 673 pos = intel_cursor_position(crtc_state, plane_state, false); 674 } 675 676 /* 677 * On some platforms writing CURCNTR first will also 678 * cause CURPOS to be armed by the CURBASE write. 679 * Without the CURCNTR write the CURPOS write would 680 * arm itself. Thus we always update CURCNTR before 681 * CURPOS. 682 * 683 * On other platforms CURPOS always requires the 684 * CURBASE write to arm the update. Additionally 685 * a write to any of the cursor register will cancel 686 * an already armed cursor update. Thus leaving out 687 * the CURBASE write after CURPOS could lead to a 688 * cursor that doesn't appear to move, or even change 689 * shape. Thus we always write CURBASE. 690 * 691 * The other registers are armed by the CURBASE write 692 * except when the plane is getting enabled at which time 693 * the CURCNTR write arms the update. 694 */ 695 696 if (DISPLAY_VER(display) >= 9) 697 skl_write_cursor_wm(dsb, plane, crtc_state); 698 699 if (plane_state) 700 i9xx_cursor_update_sel_fetch_arm(dsb, plane, crtc_state, plane_state); 701 else 702 i9xx_cursor_disable_sel_fetch_arm(dsb, plane, crtc_state); 703 704 if (plane->cursor.base != base || 705 plane->cursor.size != fbc_ctl || 706 plane->cursor.cntl != cntl) { 707 if (HAS_CUR_FBC(display)) 708 intel_de_write_dsb(display, dsb, CUR_FBC_CTL(display, pipe), fbc_ctl); 709 intel_de_write_dsb(display, dsb, CURCNTR(display, pipe), cntl); 710 intel_de_write_dsb(display, dsb, CURPOS(display, pipe), pos); 711 intel_de_write_dsb(display, dsb, CURBASE(display, pipe), base); 712 713 plane->cursor.base = base; 714 plane->cursor.size = fbc_ctl; 715 plane->cursor.cntl = cntl; 716 } else { 717 intel_de_write_dsb(display, dsb, CURPOS(display, pipe), pos); 718 intel_de_write_dsb(display, dsb, CURBASE(display, pipe), base); 719 } 720 } 721 722 static void i9xx_cursor_disable_arm(struct intel_dsb *dsb, 723 struct intel_plane *plane, 724 const struct intel_crtc_state *crtc_state) 725 { 726 i9xx_cursor_update_arm(dsb, plane, crtc_state, NULL); 727 } 728 729 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane, 730 enum pipe *pipe) 731 { 732 struct intel_display *display = to_intel_display(plane); 733 enum intel_display_power_domain power_domain; 734 struct ref_tracker *wakeref; 735 bool ret; 736 u32 val; 737 738 /* 739 * Not 100% correct for planes that can move between pipes, 740 * but that's only the case for gen2-3 which don't have any 741 * display power wells. 742 */ 743 power_domain = POWER_DOMAIN_PIPE(plane->pipe); 744 wakeref = intel_display_power_get_if_enabled(display, power_domain); 745 if (!wakeref) 746 return false; 747 748 val = intel_de_read(display, CURCNTR(display, plane->pipe)); 749 750 ret = val & MCURSOR_MODE_MASK; 751 752 if (DISPLAY_VER(display) >= 5 || display->platform.g4x) 753 *pipe = plane->pipe; 754 else 755 *pipe = REG_FIELD_GET(MCURSOR_PIPE_SEL_MASK, val); 756 757 intel_display_power_put(display, power_domain, wakeref); 758 759 return ret; 760 } 761 762 static void g4x_cursor_capture_error(struct intel_crtc *crtc, 763 struct intel_plane *plane, 764 struct intel_plane_error *error) 765 { 766 struct intel_display *display = to_intel_display(plane); 767 768 error->ctl = intel_de_read(display, CURCNTR(display, crtc->pipe)); 769 error->surf = intel_de_read(display, CURBASE(display, crtc->pipe)); 770 error->surflive = intel_de_read(display, CURSURFLIVE(display, crtc->pipe)); 771 } 772 773 static void i9xx_cursor_capture_error(struct intel_crtc *crtc, 774 struct intel_plane *plane, 775 struct intel_plane_error *error) 776 { 777 struct intel_display *display = to_intel_display(plane); 778 779 error->ctl = intel_de_read(display, CURCNTR(display, crtc->pipe)); 780 error->surf = intel_de_read(display, CURBASE(display, crtc->pipe)); 781 } 782 783 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane, 784 u32 format, u64 modifier) 785 { 786 if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier)) 787 return false; 788 789 return format == DRM_FORMAT_ARGB8888; 790 } 791 792 void intel_cursor_unpin_work(struct kthread_work *base) 793 { 794 struct drm_vblank_work *work = to_drm_vblank_work(base); 795 struct intel_plane_state *plane_state = 796 container_of(work, typeof(*plane_state), unpin_work); 797 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 798 799 intel_plane_unpin_fb(plane_state); 800 intel_plane_destroy_state(&plane->base, &plane_state->uapi); 801 } 802 803 static int intel_cursor_lock_joined_planes(struct intel_display *display, 804 const struct intel_crtc_state *crtc_state, 805 struct drm_modeset_acquire_ctx *ctx) 806 { 807 struct intel_crtc *pipe_crtc; 808 int ret; 809 810 for_each_intel_crtc_in_pipe_mask(display, pipe_crtc, 811 intel_crtc_joined_pipe_mask(crtc_state)) { 812 struct intel_plane *pipe_plane = 813 intel_crtc_get_plane(pipe_crtc, PLANE_CURSOR); 814 815 ret = drm_modeset_lock(&pipe_crtc->base.mutex, ctx); 816 if (ret) 817 return ret; 818 819 ret = drm_modeset_lock(&pipe_plane->base.mutex, ctx); 820 if (ret) 821 return ret; 822 } 823 return 0; 824 } 825 826 static bool 827 intel_cursor_joiner_commits_idle(struct intel_display *display, 828 const struct intel_crtc_state *crtc_state) 829 { 830 struct intel_crtc *pipe_crtc; 831 832 for_each_intel_crtc_in_pipe_mask(display, pipe_crtc, 833 intel_crtc_joined_pipe_mask(crtc_state)) { 834 struct intel_plane *pipe_plane = 835 intel_crtc_get_plane(pipe_crtc, PLANE_CURSOR); 836 struct intel_plane_state *pipe_plane_state = 837 to_intel_plane_state(pipe_plane->base.state); 838 839 if (pipe_plane_state->uapi.commit && 840 !try_wait_for_completion(&pipe_plane_state->uapi.commit->hw_done)) 841 return false; 842 } 843 844 return true; 845 } 846 847 static void 848 intel_cursor_fastpath_update_plane_state(struct intel_plane_state *plane_state, 849 const struct intel_plane_state *from_plane_state, 850 struct drm_framebuffer *fb, 851 struct intel_crtc *hw_crtc, 852 int crtc_x, int crtc_y, 853 unsigned int crtc_w, unsigned int crtc_h, 854 u32 src_x, u32 src_y, 855 u32 src_w, u32 src_h) 856 { 857 /* 858 * Only the primary owns its uapi state; a secondary mirrors it, so 859 * its uapi.crtc/fb stay NULL and hw.crtc comes from hw_crtc. 860 */ 861 if (plane_state == from_plane_state) { 862 drm_atomic_set_fb_for_plane(&plane_state->uapi, fb); 863 864 plane_state->uapi.src_x = src_x; 865 plane_state->uapi.src_y = src_y; 866 plane_state->uapi.src_w = src_w; 867 plane_state->uapi.src_h = src_h; 868 plane_state->uapi.crtc_x = crtc_x; 869 plane_state->uapi.crtc_y = crtc_y; 870 plane_state->uapi.crtc_w = crtc_w; 871 plane_state->uapi.crtc_h = crtc_h; 872 } 873 874 intel_plane_copy_uapi_to_hw_state(NULL, plane_state, from_plane_state, hw_crtc); 875 } 876 877 struct intel_cursor_joiner_state { 878 struct intel_plane *plane; 879 struct intel_crtc *crtc; 880 struct intel_crtc_state *crtc_state; 881 struct intel_plane_state *old_plane_state; 882 struct intel_plane_state *new_plane_state; 883 }; 884 885 static int 886 intel_legacy_cursor_update(struct drm_plane *_plane, 887 struct drm_crtc *_crtc, 888 struct drm_framebuffer *fb, 889 int crtc_x, int crtc_y, 890 unsigned int crtc_w, unsigned int crtc_h, 891 u32 src_x, u32 src_y, 892 u32 src_w, u32 src_h, 893 struct drm_modeset_acquire_ctx *ctx) 894 { 895 struct intel_plane *plane = to_intel_plane(_plane); 896 struct intel_crtc *crtc = to_intel_crtc(_crtc); 897 struct intel_display *display = to_intel_display(plane); 898 struct intel_plane_state *old_plane_state = 899 to_intel_plane_state(plane->base.state); 900 struct intel_crtc_state *crtc_state = 901 to_intel_crtc_state(crtc->base.state); 902 struct intel_vblank_evade_ctx evade; 903 struct intel_cursor_joiner_state joined_pipe_state[I915_MAX_PIPES] = {}; 904 struct intel_crtc *pipe_crtc; 905 int num_pipes = 0; 906 u32 start_vbl_count, end_vbl_count; 907 int ret; 908 909 /* 910 * When crtc is inactive or there is a modeset pending, 911 * wait for it to complete in the slowpath. 912 * PSR2 selective fetch also requires the slow path as 913 * PSR2 plane and transcoder registers can only be updated during 914 * vblank. 915 */ 916 if (!crtc_state->hw.active || 917 intel_crtc_needs_modeset(crtc_state) || 918 intel_crtc_needs_fastset(crtc_state)) 919 goto slow; 920 921 /* 922 * If any parameters change that may affect watermarks, 923 * take the slowpath. Only changing fb or position should be 924 * in the fastpath. 925 */ 926 if (old_plane_state->uapi.crtc != &crtc->base || 927 old_plane_state->uapi.src_w != src_w || 928 old_plane_state->uapi.src_h != src_h || 929 old_plane_state->uapi.crtc_w != crtc_w || 930 old_plane_state->uapi.crtc_h != crtc_h || 931 !old_plane_state->uapi.fb != !fb) 932 goto slow; 933 934 ret = intel_cursor_lock_joined_planes(display, crtc_state, ctx); 935 if (ret == -EDEADLK) 936 return ret; 937 if (ret) 938 goto slow; 939 940 /* 941 * Don't do an async update if there is an outstanding commit modifying 942 * any of the joined cursor planes. This prevents our async update's 943 * changes from getting overridden by a previous synchronous update's 944 * state. 945 */ 946 if (!intel_cursor_joiner_commits_idle(display, crtc_state)) 947 goto slow; 948 949 /* 950 * Iterate over all joined pipes (primary and secondary) uniformly. 951 * The joined pipe mask includes both the primary pipe and all 952 * secondary joiner pipes, allowing us to handle them all the same way. 953 */ 954 for_each_intel_crtc_in_pipe_mask(display, pipe_crtc, 955 intel_crtc_joined_pipe_mask(crtc_state)) { 956 struct intel_cursor_joiner_state *j = &joined_pipe_state[num_pipes]; 957 958 j->plane = intel_crtc_get_plane(pipe_crtc, PLANE_CURSOR); 959 j->crtc = pipe_crtc; 960 j->crtc_state = to_intel_crtc_state(pipe_crtc->base.state); 961 j->old_plane_state = to_intel_plane_state(j->plane->base.state); 962 j->new_plane_state = 963 to_intel_plane_state(intel_plane_duplicate_state(&j->plane->base)); 964 965 if (!j->new_plane_state) { 966 ret = -ENOMEM; 967 goto out_free; 968 } 969 970 intel_cursor_fastpath_update_plane_state(j->new_plane_state, 971 joined_pipe_state[0].new_plane_state, 972 fb, pipe_crtc, 973 crtc_x, crtc_y, 974 crtc_w, crtc_h, 975 src_x, src_y, 976 src_w, src_h); 977 978 ret = j->plane->check_plane(j->crtc_state, j->new_plane_state); 979 if (ret) { 980 intel_plane_destroy_state(&j->plane->base, 981 &j->new_plane_state->uapi); 982 goto out_free; 983 } 984 985 ret = intel_plane_pin_fb(j->new_plane_state, j->old_plane_state); 986 if (ret) { 987 intel_plane_destroy_state(&j->plane->base, 988 &j->new_plane_state->uapi); 989 goto out_free; 990 } 991 992 num_pipes++; 993 } 994 995 intel_frontbuffer_flush(to_intel_frontbuffer(joined_pipe_state[0].new_plane_state->hw.fb), 996 ORIGIN_CURSOR_UPDATE); 997 998 for (int i = 0; i < num_pipes; i++) { 999 struct intel_frontbuffer *old_front = 1000 to_intel_frontbuffer(joined_pipe_state[i].old_plane_state->hw.fb); 1001 struct intel_frontbuffer *new_front = 1002 to_intel_frontbuffer(joined_pipe_state[i].new_plane_state->hw.fb); 1003 1004 intel_frontbuffer_track(old_front, new_front, 1005 joined_pipe_state[i].plane->frontbuffer_bit); 1006 } 1007 1008 for (int i = 0; i < num_pipes; i++) { 1009 joined_pipe_state[i].plane->base.state = 1010 &joined_pipe_state[i].new_plane_state->uapi; 1011 1012 if (joined_pipe_state[i].new_plane_state->uapi.visible) 1013 joined_pipe_state[i].crtc_state->active_planes |= BIT(PLANE_CURSOR); 1014 else 1015 joined_pipe_state[i].crtc_state->active_planes &= ~BIT(PLANE_CURSOR); 1016 } 1017 1018 /* 1019 * We cannot swap crtc_state as it may be in use by an atomic commit or 1020 * page flip that's running simultaneously. If we swap crtc_state and 1021 * destroy the old state, we will cause a use-after-free there. 1022 * 1023 * Only update active_planes, which is needed for our internal 1024 * bookkeeping. Either value will do the right thing when updating 1025 * planes atomically. If the cursor was part of the atomic update then 1026 * we would have taken the slowpath. 1027 */ 1028 1029 intel_vblank_evade_init(crtc_state, crtc_state, &evade); 1030 1031 intel_psr_lock(crtc_state); 1032 1033 if (!drm_WARN_ON(display->drm, drm_crtc_vblank_get(&crtc->base))) { 1034 /* 1035 * TODO: maybe check if we're still in PSR 1036 * and skip the vblank evasion entirely? 1037 */ 1038 intel_psr_wait_for_idle_locked(crtc_state); 1039 1040 local_irq_disable(); 1041 1042 intel_vblank_evade(&evade); 1043 1044 drm_crtc_vblank_put(&crtc->base); 1045 } else { 1046 local_irq_disable(); 1047 } 1048 1049 /* 1050 * Joiner pipes are vblank-synchronized, so sampling only the primary 1051 * pipe is sufficient to detect a straddle across all joined pipes. 1052 * The vblank evasion above also operates on the primary pipe only. 1053 */ 1054 start_vbl_count = intel_crtc_get_vblank_counter(joined_pipe_state[0].crtc); 1055 1056 for (int i = 0; i < num_pipes; i++) { 1057 if (joined_pipe_state[i].new_plane_state->uapi.visible) { 1058 intel_plane_update_noarm(NULL, joined_pipe_state[i].plane, 1059 joined_pipe_state[i].crtc_state, 1060 joined_pipe_state[i].new_plane_state); 1061 intel_plane_update_arm(NULL, joined_pipe_state[i].plane, 1062 joined_pipe_state[i].crtc_state, 1063 joined_pipe_state[i].new_plane_state); 1064 } else { 1065 intel_plane_disable_arm(NULL, joined_pipe_state[i].plane, 1066 joined_pipe_state[i].crtc_state); 1067 } 1068 } 1069 1070 end_vbl_count = intel_crtc_get_vblank_counter(joined_pipe_state[0].crtc); 1071 1072 local_irq_enable(); 1073 1074 if (start_vbl_count != end_vbl_count) 1075 drm_err(display->drm, 1076 "Atomic update failure on pipe %c (start=%u end=%u)\n", 1077 pipe_name(joined_pipe_state[0].crtc->pipe), 1078 start_vbl_count, end_vbl_count); 1079 1080 intel_psr_unlock(crtc_state); 1081 1082 /* 1083 * Schedule or immediately unpin old framebuffers. 1084 * Protect against concurrent access. 1085 */ 1086 for (int i = 0; i < num_pipes; i++) { 1087 struct intel_plane_state *old = joined_pipe_state[i].old_plane_state; 1088 1089 if (old->ggtt_vma != joined_pipe_state[i].new_plane_state->ggtt_vma) { 1090 drm_vblank_work_init(&old->unpin_work, &crtc->base, 1091 intel_cursor_unpin_work); 1092 drm_vblank_work_schedule(&old->unpin_work, 1093 drm_crtc_accurate_vblank_count(&crtc->base) + 1, 1094 false); 1095 joined_pipe_state[i].old_plane_state = NULL; 1096 } else { 1097 intel_plane_unpin_fb(old); 1098 } 1099 } 1100 1101 out_free: 1102 if (ret) { 1103 for (int i = 0; i < num_pipes; i++) { 1104 intel_plane_unpin_fb(joined_pipe_state[i].new_plane_state); 1105 intel_plane_destroy_state(&joined_pipe_state[i].plane->base, 1106 &joined_pipe_state[i].new_plane_state->uapi); 1107 } 1108 } else { 1109 for (int i = 0; i < num_pipes; i++) { 1110 if (!joined_pipe_state[i].old_plane_state) 1111 continue; 1112 1113 intel_plane_destroy_state(&joined_pipe_state[i].plane->base, 1114 &joined_pipe_state[i].old_plane_state->uapi); 1115 } 1116 } 1117 return ret; 1118 1119 slow: 1120 return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb, 1121 crtc_x, crtc_y, crtc_w, crtc_h, 1122 src_x, src_y, src_w, src_h, ctx); 1123 } 1124 1125 static const struct drm_plane_funcs intel_cursor_plane_funcs = { 1126 .update_plane = intel_legacy_cursor_update, 1127 .disable_plane = drm_atomic_helper_disable_plane, 1128 .destroy = intel_plane_destroy, 1129 .atomic_duplicate_state = intel_plane_duplicate_state, 1130 .atomic_destroy_state = intel_plane_destroy_state, 1131 .format_mod_supported = intel_cursor_format_mod_supported, 1132 .format_mod_supported_async = intel_plane_format_mod_supported_async, 1133 }; 1134 1135 static void intel_cursor_add_size_hints_property(struct intel_plane *plane) 1136 { 1137 struct intel_display *display = to_intel_display(plane); 1138 const struct drm_mode_config *config = &display->drm->mode_config; 1139 struct drm_plane_size_hint hints[4]; 1140 int size, max_size, num_hints = 0; 1141 1142 max_size = min(config->cursor_width, config->cursor_height); 1143 1144 /* for simplicity only enumerate the supported square+POT sizes */ 1145 for (size = 64; size <= max_size; size *= 2) { 1146 if (drm_WARN_ON(display->drm, num_hints >= ARRAY_SIZE(hints))) 1147 break; 1148 1149 hints[num_hints].width = size; 1150 hints[num_hints].height = size; 1151 num_hints++; 1152 } 1153 1154 drm_plane_add_size_hints_property(&plane->base, hints, num_hints); 1155 } 1156 1157 struct intel_plane * 1158 intel_cursor_plane_create(struct intel_display *display, 1159 enum pipe pipe) 1160 { 1161 struct intel_plane *cursor; 1162 int ret, zpos; 1163 u64 *modifiers; 1164 1165 cursor = intel_plane_alloc(); 1166 if (IS_ERR(cursor)) 1167 return cursor; 1168 1169 cursor->pipe = pipe; 1170 cursor->i9xx_plane = (enum i9xx_plane_id) pipe; 1171 cursor->id = PLANE_CURSOR; 1172 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id); 1173 1174 if (display->platform.i845g || display->platform.i865g) { 1175 cursor->max_stride = i845_cursor_max_stride; 1176 cursor->min_alignment = i845_cursor_min_alignment; 1177 cursor->update_arm = i845_cursor_update_arm; 1178 cursor->disable_arm = i845_cursor_disable_arm; 1179 cursor->get_hw_state = i845_cursor_get_hw_state; 1180 cursor->check_plane = i845_check_cursor; 1181 } else { 1182 cursor->max_stride = i9xx_cursor_max_stride; 1183 1184 if (display->platform.i830) 1185 cursor->min_alignment = i830_cursor_min_alignment; 1186 else if (display->platform.i85x) 1187 cursor->min_alignment = i85x_cursor_min_alignment; 1188 else 1189 cursor->min_alignment = i9xx_cursor_min_alignment; 1190 1191 if (intel_scanout_needs_vtd_wa(display)) 1192 cursor->vtd_guard = 2; 1193 1194 cursor->update_arm = i9xx_cursor_update_arm; 1195 cursor->disable_arm = i9xx_cursor_disable_arm; 1196 cursor->get_hw_state = i9xx_cursor_get_hw_state; 1197 cursor->check_plane = i9xx_check_cursor; 1198 } 1199 1200 cursor->surf_offset = intel_cursor_surf_offset; 1201 1202 if (DISPLAY_VER(display) >= 5 || display->platform.g4x) 1203 cursor->capture_error = g4x_cursor_capture_error; 1204 else 1205 cursor->capture_error = i9xx_cursor_capture_error; 1206 1207 cursor->cursor.base = ~0; 1208 cursor->cursor.cntl = ~0; 1209 1210 if (display->platform.i845g || display->platform.i865g || HAS_CUR_FBC(display)) 1211 cursor->cursor.size = ~0; 1212 1213 modifiers = intel_fb_plane_get_modifiers(display, INTEL_PLANE_CAP_NONE); 1214 1215 ret = drm_universal_plane_init(display->drm, &cursor->base, 1216 0, &intel_cursor_plane_funcs, 1217 intel_cursor_formats, 1218 ARRAY_SIZE(intel_cursor_formats), 1219 modifiers, 1220 DRM_PLANE_TYPE_CURSOR, 1221 "cursor %c", pipe_name(pipe)); 1222 1223 kfree(modifiers); 1224 1225 if (ret) 1226 goto fail; 1227 1228 if (DISPLAY_VER(display) >= 4) 1229 drm_plane_create_rotation_property(&cursor->base, 1230 DRM_MODE_ROTATE_0, 1231 DRM_MODE_ROTATE_0 | 1232 DRM_MODE_ROTATE_180); 1233 1234 intel_cursor_add_size_hints_property(cursor); 1235 1236 drm_plane_create_blend_mode_property(&cursor->base, 1237 BIT(DRM_MODE_BLEND_PREMULTI)); 1238 1239 zpos = DISPLAY_RUNTIME_INFO(display)->num_sprites[pipe] + 1; 1240 drm_plane_create_zpos_immutable_property(&cursor->base, zpos); 1241 1242 if (DISPLAY_VER(display) >= 12) 1243 drm_plane_enable_fb_damage_clips(&cursor->base); 1244 1245 intel_plane_helper_add(cursor); 1246 1247 return cursor; 1248 1249 fail: 1250 intel_plane_free(cursor); 1251 1252 return ERR_PTR(ret); 1253 } 1254 1255 void intel_cursor_mode_config_init(struct intel_display *display) 1256 { 1257 struct drm_mode_config *mode_config = &display->drm->mode_config; 1258 1259 if (display->platform.i845g) { 1260 mode_config->cursor_width = 64; 1261 mode_config->cursor_height = 1023; 1262 } else if (display->platform.i865g) { 1263 mode_config->cursor_width = 512; 1264 mode_config->cursor_height = 1023; 1265 } else if (display->platform.i830 || display->platform.i85x || 1266 display->platform.i915g || display->platform.i915gm) { 1267 mode_config->cursor_width = 64; 1268 mode_config->cursor_height = 64; 1269 } else { 1270 mode_config->cursor_width = 256; 1271 mode_config->cursor_height = 256; 1272 } 1273 } 1274