1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022-2023 Intel Corporation 4 */ 5 6 #include <linux/iopoll.h> 7 8 #include <drm/drm_print.h> 9 #include <drm/drm_vblank.h> 10 11 #include "i915_drv.h" 12 #include "i915_utils.h" 13 #include "intel_color.h" 14 #include "intel_crtc.h" 15 #include "intel_de.h" 16 #include "intel_display_regs.h" 17 #include "intel_display_types.h" 18 #include "intel_vblank.h" 19 #include "intel_vrr.h" 20 21 /* 22 * This timing diagram depicts the video signal in and 23 * around the vertical blanking period. 24 * 25 * Assumptions about the fictitious mode used in this example: 26 * vblank_start >= 3 27 * vsync_start = vblank_start + 1 28 * vsync_end = vblank_start + 2 29 * vtotal = vblank_start + 3 30 * 31 * start of vblank: 32 * latch double buffered registers 33 * increment frame counter (ctg+) 34 * generate start of vblank interrupt (gen4+) 35 * | 36 * | frame start: 37 * | generate frame start interrupt (aka. vblank interrupt) (gmch) 38 * | may be shifted forward 1-3 extra lines via TRANSCONF 39 * | | 40 * | | start of vsync: 41 * | | generate vsync interrupt 42 * | | | 43 * ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx 44 * . \hs/ . \hs/ \hs/ \hs/ . \hs/ 45 * ----va---> <-----------------vb--------------------> <--------va------------- 46 * | | <----vs-----> | 47 * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2) 48 * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+) 49 * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi) 50 * | | | 51 * last visible pixel first visible pixel 52 * | increment frame counter (gen3/4) 53 * pixel counter = vblank_start * htotal pixel counter = 0 (gen3/4) 54 * 55 * x = horizontal active 56 * _ = horizontal blanking 57 * hs = horizontal sync 58 * va = vertical active 59 * vb = vertical blanking 60 * vs = vertical sync 61 * vbs = vblank_start (number) 62 * 63 * Summary: 64 * - most events happen at the start of horizontal sync 65 * - frame start happens at the start of horizontal blank, 1-4 lines 66 * (depending on TRANSCONF settings) after the start of vblank 67 * - gen3/4 pixel and frame counter are synchronized with the start 68 * of horizontal active on the first line of vertical active 69 */ 70 71 /* 72 * Called from drm generic code, passed a 'crtc', which we use as a pipe index. 73 */ 74 u32 i915_get_vblank_counter(struct drm_crtc *crtc) 75 { 76 struct intel_display *display = to_intel_display(crtc->dev); 77 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc); 78 const struct drm_display_mode *mode = &vblank->hwmode; 79 enum pipe pipe = to_intel_crtc(crtc)->pipe; 80 u32 pixel, vbl_start, hsync_start, htotal; 81 u64 frame; 82 83 /* 84 * On i965gm TV output the frame counter only works up to 85 * the point when we enable the TV encoder. After that the 86 * frame counter ceases to work and reads zero. We need a 87 * vblank wait before enabling the TV encoder and so we 88 * have to enable vblank interrupts while the frame counter 89 * is still in a working state. However the core vblank code 90 * does not like us returning non-zero frame counter values 91 * when we've told it that we don't have a working frame 92 * counter. Thus we must stop non-zero values leaking out. 93 */ 94 if (!vblank->max_vblank_count) 95 return 0; 96 97 htotal = mode->crtc_htotal; 98 hsync_start = mode->crtc_hsync_start; 99 vbl_start = intel_mode_vblank_start(mode); 100 101 /* Convert to pixel count */ 102 vbl_start *= htotal; 103 104 /* Start of vblank event occurs at start of hsync */ 105 vbl_start -= htotal - hsync_start; 106 107 /* 108 * High & low register fields aren't synchronized, so make sure 109 * we get a low value that's stable across two reads of the high 110 * register. 111 */ 112 frame = intel_de_read64_2x32(display, PIPEFRAMEPIXEL(display, pipe), 113 PIPEFRAME(display, pipe)); 114 115 pixel = frame & PIPE_PIXEL_MASK; 116 frame = (frame >> PIPE_FRAME_LOW_SHIFT) & 0xffffff; 117 118 /* 119 * The frame counter increments at beginning of active. 120 * Cook up a vblank counter by also checking the pixel 121 * counter against vblank start. 122 */ 123 return (frame + (pixel >= vbl_start)) & 0xffffff; 124 } 125 126 u32 g4x_get_vblank_counter(struct drm_crtc *crtc) 127 { 128 struct intel_display *display = to_intel_display(crtc->dev); 129 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc); 130 enum pipe pipe = to_intel_crtc(crtc)->pipe; 131 132 if (!vblank->max_vblank_count) 133 return 0; 134 135 return intel_de_read(display, PIPE_FRMCOUNT_G4X(display, pipe)); 136 } 137 138 static u32 intel_crtc_scanlines_since_frame_timestamp(struct intel_crtc *crtc) 139 { 140 struct intel_display *display = to_intel_display(crtc); 141 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base); 142 const struct drm_display_mode *mode = &vblank->hwmode; 143 u32 htotal = mode->crtc_htotal; 144 u32 clock = mode->crtc_clock; 145 u32 scan_prev_time, scan_curr_time, scan_post_time; 146 147 /* 148 * To avoid the race condition where we might cross into the 149 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR 150 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR 151 * during the same frame. 152 */ 153 do { 154 /* 155 * This field provides read back of the display 156 * pipe frame time stamp. The time stamp value 157 * is sampled at every start of vertical blank. 158 */ 159 scan_prev_time = intel_de_read_fw(display, 160 PIPE_FRMTMSTMP(crtc->pipe)); 161 162 /* 163 * The TIMESTAMP_CTR register has the current 164 * time stamp value. 165 */ 166 scan_curr_time = intel_de_read_fw(display, IVB_TIMESTAMP_CTR); 167 168 scan_post_time = intel_de_read_fw(display, 169 PIPE_FRMTMSTMP(crtc->pipe)); 170 } while (scan_post_time != scan_prev_time); 171 172 return div_u64(mul_u32_u32(scan_curr_time - scan_prev_time, 173 clock), 1000 * htotal); 174 } 175 176 /* 177 * On certain encoders on certain platforms, pipe 178 * scanline register will not work to get the scanline, 179 * since the timings are driven from the PORT or issues 180 * with scanline register updates. 181 * This function will use Framestamp and current 182 * timestamp registers to calculate the scanline. 183 */ 184 static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc) 185 { 186 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base); 187 const struct drm_display_mode *mode = &vblank->hwmode; 188 u32 vblank_start = mode->crtc_vblank_start; 189 u32 vtotal = mode->crtc_vtotal; 190 u32 scanline; 191 192 scanline = intel_crtc_scanlines_since_frame_timestamp(crtc); 193 scanline = min(scanline, vtotal - 1); 194 scanline = (scanline + vblank_start) % vtotal; 195 196 return scanline; 197 } 198 199 int intel_crtc_scanline_offset(const struct intel_crtc_state *crtc_state) 200 { 201 struct intel_display *display = to_intel_display(crtc_state); 202 203 /* 204 * The scanline counter increments at the leading edge of hsync. 205 * 206 * On most platforms it starts counting from vtotal-1 on the 207 * first active line. That means the scanline counter value is 208 * always one less than what we would expect. Ie. just after 209 * start of vblank, which also occurs at start of hsync (on the 210 * last active line), the scanline counter will read vblank_start-1. 211 * 212 * On gen2 the scanline counter starts counting from 1 instead 213 * of vtotal-1, so we have to subtract one. 214 * 215 * On HSW+ the behaviour of the scanline counter depends on the output 216 * type. For DP ports it behaves like most other platforms, but on HDMI 217 * there's an extra 1 line difference. So we need to add two instead of 218 * one to the value. 219 * 220 * On VLV/CHV DSI the scanline counter would appear to increment 221 * approx. 1/3 of a scanline before start of vblank. Unfortunately 222 * that means we can't tell whether we're in vblank or not while 223 * we're on that particular line. We must still set scanline_offset 224 * to 1 so that the vblank timestamps come out correct when we query 225 * the scanline counter from within the vblank interrupt handler. 226 * However if queried just before the start of vblank we'll get an 227 * answer that's slightly in the future. 228 */ 229 if (DISPLAY_VER(display) >= 20 || display->platform.battlemage) 230 return 1; 231 else if (DISPLAY_VER(display) >= 9 || 232 display->platform.broadwell || display->platform.haswell) 233 return intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ? 2 : 1; 234 else if (DISPLAY_VER(display) >= 3) 235 return 1; 236 else 237 return -1; 238 } 239 240 /* 241 * intel_de_read_fw(), only for fast reads of display block, no need for 242 * forcewake etc. 243 */ 244 static int __intel_get_crtc_scanline(struct intel_crtc *crtc) 245 { 246 struct intel_display *display = to_intel_display(crtc); 247 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base); 248 const struct drm_display_mode *mode = &vblank->hwmode; 249 enum pipe pipe = crtc->pipe; 250 int position, vtotal; 251 252 if (!crtc->active) 253 return 0; 254 255 if (crtc->mode_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP) 256 return __intel_get_crtc_scanline_from_timestamp(crtc); 257 258 vtotal = intel_mode_vtotal(mode); 259 260 position = intel_de_read_fw(display, PIPEDSL(display, pipe)) & PIPEDSL_LINE_MASK; 261 262 /* 263 * On HSW, the DSL reg (0x70000) appears to return 0 if we 264 * read it just before the start of vblank. So try it again 265 * so we don't accidentally end up spanning a vblank frame 266 * increment, causing the pipe_update_end() code to squak at us. 267 * 268 * The nature of this problem means we can't simply check the ISR 269 * bit and return the vblank start value; nor can we use the scanline 270 * debug register in the transcoder as it appears to have the same 271 * problem. We may need to extend this to include other platforms, 272 * but so far testing only shows the problem on HSW. 273 */ 274 if (HAS_DDI(display) && !position) { 275 int i, temp; 276 277 for (i = 0; i < 100; i++) { 278 udelay(1); 279 temp = intel_de_read_fw(display, 280 PIPEDSL(display, pipe)) & PIPEDSL_LINE_MASK; 281 if (temp != position) { 282 position = temp; 283 break; 284 } 285 } 286 } 287 288 /* 289 * See update_scanline_offset() for the details on the 290 * scanline_offset adjustment. 291 */ 292 return (position + vtotal + crtc->scanline_offset) % vtotal; 293 } 294 295 /* 296 * The uncore version of the spin lock functions is used to decide 297 * whether we need to lock the uncore lock or not. This is only 298 * needed in i915, not in Xe. 299 * 300 * This lock in i915 is needed because some old platforms (at least 301 * IVB and possibly HSW as well), which are not supported in Xe, need 302 * all register accesses to the same cacheline to be serialized, 303 * otherwise they may hang. 304 */ 305 #ifdef I915 306 static void intel_vblank_section_enter(struct intel_display *display) 307 __acquires(i915->uncore.lock) 308 { 309 struct drm_i915_private *i915 = to_i915(display->drm); 310 spin_lock(&i915->uncore.lock); 311 } 312 313 static void intel_vblank_section_exit(struct intel_display *display) 314 __releases(i915->uncore.lock) 315 { 316 struct drm_i915_private *i915 = to_i915(display->drm); 317 spin_unlock(&i915->uncore.lock); 318 } 319 #else 320 static void intel_vblank_section_enter(struct intel_display *display) 321 { 322 } 323 324 static void intel_vblank_section_exit(struct intel_display *display) 325 { 326 } 327 #endif 328 329 static bool i915_get_crtc_scanoutpos(struct drm_crtc *_crtc, 330 bool in_vblank_irq, 331 int *vpos, int *hpos, 332 ktime_t *stime, ktime_t *etime, 333 const struct drm_display_mode *mode) 334 { 335 struct intel_display *display = to_intel_display(_crtc->dev); 336 struct intel_crtc *crtc = to_intel_crtc(_crtc); 337 enum pipe pipe = crtc->pipe; 338 int position; 339 int vbl_start, vbl_end, hsync_start, htotal, vtotal; 340 unsigned long irqflags; 341 bool use_scanline_counter = DISPLAY_VER(display) >= 5 || 342 display->platform.g4x || DISPLAY_VER(display) == 2 || 343 crtc->mode_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER; 344 345 if (drm_WARN_ON(display->drm, !mode->crtc_clock)) { 346 drm_dbg(display->drm, 347 "trying to get scanoutpos for disabled pipe %c\n", 348 pipe_name(pipe)); 349 return false; 350 } 351 352 htotal = mode->crtc_htotal; 353 hsync_start = mode->crtc_hsync_start; 354 vtotal = intel_mode_vtotal(mode); 355 vbl_start = intel_mode_vblank_start(mode); 356 vbl_end = intel_mode_vblank_end(mode); 357 358 /* 359 * Enter vblank critical section, as we will do multiple 360 * timing critical raw register reads, potentially with 361 * preemption disabled, so the following code must not block. 362 */ 363 local_irq_save(irqflags); 364 intel_vblank_section_enter(display); 365 366 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */ 367 368 /* Get optional system timestamp before query. */ 369 if (stime) 370 *stime = ktime_get(); 371 372 if (crtc->mode_flags & I915_MODE_FLAG_VRR) { 373 int scanlines = intel_crtc_scanlines_since_frame_timestamp(crtc); 374 375 position = __intel_get_crtc_scanline(crtc); 376 377 /* 378 * Already exiting vblank? If so, shift our position 379 * so it looks like we're already approaching the full 380 * vblank end. This should make the generated timestamp 381 * more or less match when the active portion will start. 382 */ 383 if (position >= vbl_start && scanlines < position) 384 position = min(crtc->vmax_vblank_start + scanlines, vtotal - 1); 385 } else if (use_scanline_counter) { 386 /* No obvious pixelcount register. Only query vertical 387 * scanout position from Display scan line register. 388 */ 389 position = __intel_get_crtc_scanline(crtc); 390 } else { 391 /* 392 * Have access to pixelcount since start of frame. 393 * We can split this into vertical and horizontal 394 * scanout position. 395 */ 396 position = (intel_de_read_fw(display, PIPEFRAMEPIXEL(display, pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT; 397 398 /* convert to pixel counts */ 399 vbl_start *= htotal; 400 vbl_end *= htotal; 401 vtotal *= htotal; 402 403 /* 404 * In interlaced modes, the pixel counter counts all pixels, 405 * so one field will have htotal more pixels. In order to avoid 406 * the reported position from jumping backwards when the pixel 407 * counter is beyond the length of the shorter field, just 408 * clamp the position the length of the shorter field. This 409 * matches how the scanline counter based position works since 410 * the scanline counter doesn't count the two half lines. 411 */ 412 position = min(position, vtotal - 1); 413 414 /* 415 * Start of vblank interrupt is triggered at start of hsync, 416 * just prior to the first active line of vblank. However we 417 * consider lines to start at the leading edge of horizontal 418 * active. So, should we get here before we've crossed into 419 * the horizontal active of the first line in vblank, we would 420 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that, 421 * always add htotal-hsync_start to the current pixel position. 422 */ 423 position = (position + htotal - hsync_start) % vtotal; 424 } 425 426 /* Get optional system timestamp after query. */ 427 if (etime) 428 *etime = ktime_get(); 429 430 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */ 431 432 intel_vblank_section_exit(display); 433 local_irq_restore(irqflags); 434 435 /* 436 * While in vblank, position will be negative 437 * counting up towards 0 at vbl_end. And outside 438 * vblank, position will be positive counting 439 * up since vbl_end. 440 */ 441 if (position >= vbl_start) 442 position -= vbl_end; 443 else 444 position += vtotal - vbl_end; 445 446 if (use_scanline_counter) { 447 *vpos = position; 448 *hpos = 0; 449 } else { 450 *vpos = position / htotal; 451 *hpos = position - (*vpos * htotal); 452 } 453 454 return true; 455 } 456 457 bool intel_crtc_get_vblank_timestamp(struct drm_crtc *crtc, int *max_error, 458 ktime_t *vblank_time, bool in_vblank_irq) 459 { 460 return drm_crtc_vblank_helper_get_vblank_timestamp_internal( 461 crtc, max_error, vblank_time, in_vblank_irq, 462 i915_get_crtc_scanoutpos); 463 } 464 465 int intel_get_crtc_scanline(struct intel_crtc *crtc) 466 { 467 struct intel_display *display = to_intel_display(crtc); 468 unsigned long irqflags; 469 int position; 470 471 local_irq_save(irqflags); 472 intel_vblank_section_enter(display); 473 474 position = __intel_get_crtc_scanline(crtc); 475 476 intel_vblank_section_exit(display); 477 local_irq_restore(irqflags); 478 479 return position; 480 } 481 482 static bool pipe_scanline_is_moving(struct intel_display *display, 483 enum pipe pipe) 484 { 485 i915_reg_t reg = PIPEDSL(display, pipe); 486 u32 line1, line2; 487 488 line1 = intel_de_read(display, reg) & PIPEDSL_LINE_MASK; 489 msleep(5); 490 line2 = intel_de_read(display, reg) & PIPEDSL_LINE_MASK; 491 492 return line1 != line2; 493 } 494 495 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state) 496 { 497 struct intel_display *display = to_intel_display(crtc); 498 enum pipe pipe = crtc->pipe; 499 bool is_moving; 500 int ret; 501 502 /* Wait for the display line to settle/start moving */ 503 ret = poll_timeout_us(is_moving = pipe_scanline_is_moving(display, pipe), 504 is_moving == state, 505 500, 100 * 1000, false); 506 if (ret) 507 drm_err(display->drm, 508 "pipe %c scanline %s wait timed out\n", 509 pipe_name(pipe), str_on_off(state)); 510 } 511 512 void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc) 513 { 514 wait_for_pipe_scanline_moving(crtc, false); 515 } 516 517 void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc) 518 { 519 wait_for_pipe_scanline_moving(crtc, true); 520 } 521 522 static void intel_crtc_active_timings(struct drm_display_mode *mode, 523 int *vmax_vblank_start, 524 const struct intel_crtc_state *crtc_state, 525 bool vrr_enable) 526 { 527 drm_mode_init(mode, &crtc_state->hw.adjusted_mode); 528 *vmax_vblank_start = 0; 529 530 if (!vrr_enable) 531 return; 532 533 mode->crtc_vtotal = intel_vrr_vmax_vtotal(crtc_state); 534 mode->crtc_vblank_end = intel_vrr_vmax_vtotal(crtc_state); 535 mode->crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state); 536 *vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state); 537 } 538 539 void intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state, 540 bool vrr_enable) 541 { 542 struct intel_display *display = to_intel_display(crtc_state); 543 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 544 u8 mode_flags = crtc_state->mode_flags; 545 struct drm_display_mode adjusted_mode; 546 int vmax_vblank_start = 0; 547 unsigned long irqflags; 548 549 intel_crtc_active_timings(&adjusted_mode, &vmax_vblank_start, 550 crtc_state, vrr_enable); 551 552 if (vrr_enable) 553 drm_WARN_ON(display->drm, (mode_flags & I915_MODE_FLAG_VRR) == 0); 554 else 555 mode_flags &= ~I915_MODE_FLAG_VRR; 556 557 /* 558 * Belts and suspenders locking to guarantee everyone sees 100% 559 * consistent state during fastset seamless refresh rate changes. 560 * 561 * vblank_time_lock takes care of all drm_vblank.c stuff, and 562 * uncore.lock takes care of __intel_get_crtc_scanline() which 563 * may get called elsewhere as well. 564 * 565 * TODO maybe just protect everything (including 566 * __intel_get_crtc_scanline()) with vblank_time_lock? 567 * Need to audit everything to make sure it's safe. 568 */ 569 spin_lock_irqsave(&display->drm->vblank_time_lock, irqflags); 570 intel_vblank_section_enter(display); 571 572 drm_calc_timestamping_constants(&crtc->base, &adjusted_mode); 573 574 crtc->vmax_vblank_start = vmax_vblank_start; 575 576 crtc->mode_flags = mode_flags; 577 578 crtc->scanline_offset = intel_crtc_scanline_offset(crtc_state); 579 intel_vblank_section_exit(display); 580 spin_unlock_irqrestore(&display->drm->vblank_time_lock, irqflags); 581 } 582 583 int intel_mode_vdisplay(const struct drm_display_mode *mode) 584 { 585 int vdisplay = mode->crtc_vdisplay; 586 587 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 588 vdisplay = DIV_ROUND_UP(vdisplay, 2); 589 590 return vdisplay; 591 } 592 593 int intel_mode_vblank_start(const struct drm_display_mode *mode) 594 { 595 int vblank_start = mode->crtc_vblank_start; 596 597 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 598 vblank_start = DIV_ROUND_UP(vblank_start, 2); 599 600 return vblank_start; 601 } 602 603 int intel_mode_vblank_end(const struct drm_display_mode *mode) 604 { 605 int vblank_end = mode->crtc_vblank_end; 606 607 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 608 vblank_end /= 2; 609 610 return vblank_end; 611 } 612 613 int intel_mode_vtotal(const struct drm_display_mode *mode) 614 { 615 int vtotal = mode->crtc_vtotal; 616 617 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 618 vtotal /= 2; 619 620 return vtotal; 621 } 622 623 int intel_mode_vblank_delay(const struct drm_display_mode *mode) 624 { 625 return intel_mode_vblank_start(mode) - intel_mode_vdisplay(mode); 626 } 627 628 static const struct intel_crtc_state * 629 pre_commit_crtc_state(const struct intel_crtc_state *old_crtc_state, 630 const struct intel_crtc_state *new_crtc_state) 631 { 632 /* 633 * During fastsets/etc. the transcoder is still 634 * running with the old timings at this point. 635 */ 636 if (intel_crtc_needs_modeset(new_crtc_state)) 637 return new_crtc_state; 638 else 639 return old_crtc_state; 640 } 641 642 const struct intel_crtc_state * 643 intel_pre_commit_crtc_state(struct intel_atomic_state *state, 644 struct intel_crtc *crtc) 645 { 646 const struct intel_crtc_state *old_crtc_state = 647 intel_atomic_get_old_crtc_state(state, crtc); 648 const struct intel_crtc_state *new_crtc_state = 649 intel_atomic_get_new_crtc_state(state, crtc); 650 651 return pre_commit_crtc_state(old_crtc_state, new_crtc_state); 652 } 653 654 void intel_vblank_evade_init(const struct intel_crtc_state *old_crtc_state, 655 const struct intel_crtc_state *new_crtc_state, 656 struct intel_vblank_evade_ctx *evade) 657 { 658 struct intel_display *display = to_intel_display(new_crtc_state); 659 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc); 660 const struct intel_crtc_state *crtc_state; 661 const struct drm_display_mode *adjusted_mode; 662 int vblank_delay; 663 664 evade->crtc = crtc; 665 666 evade->need_vlv_dsi_wa = (display->platform.valleyview || 667 display->platform.cherryview) && 668 intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI); 669 670 /* TODO: maybe just use the active timings here? */ 671 crtc_state = pre_commit_crtc_state(old_crtc_state, new_crtc_state); 672 673 adjusted_mode = &crtc_state->hw.adjusted_mode; 674 675 if (crtc->mode_flags & I915_MODE_FLAG_VRR) { 676 /* timing changes should happen with VRR disabled */ 677 drm_WARN_ON(crtc->base.dev, intel_crtc_needs_modeset(new_crtc_state) || 678 new_crtc_state->update_m_n || new_crtc_state->update_lrr); 679 680 if (intel_vrr_is_push_sent(crtc_state)) 681 evade->vblank_start = intel_vrr_vmin_vblank_start(crtc_state); 682 else 683 evade->vblank_start = intel_vrr_vmax_vblank_start(crtc_state); 684 685 vblank_delay = intel_vrr_vblank_delay(crtc_state); 686 } else { 687 evade->vblank_start = intel_mode_vblank_start(adjusted_mode); 688 689 vblank_delay = intel_mode_vblank_delay(adjusted_mode); 690 } 691 692 /* FIXME needs to be calibrated sensibly */ 693 evade->min = evade->vblank_start - intel_usecs_to_scanlines(adjusted_mode, 694 VBLANK_EVASION_TIME_US); 695 evade->max = evade->vblank_start - 1; 696 697 /* 698 * M/N and TRANS_VTOTAL are double buffered on the transcoder's 699 * undelayed vblank, so with seamless M/N and LRR we must evade 700 * both vblanks. 701 * 702 * DSB execution waits for the transcoder's undelayed vblank, 703 * hence we must kick off the commit before that. 704 */ 705 if (intel_color_uses_dsb(new_crtc_state) || 706 new_crtc_state->update_m_n || new_crtc_state->update_lrr) 707 evade->min -= vblank_delay; 708 } 709 710 /* must be called with vblank interrupt already enabled! */ 711 int intel_vblank_evade(struct intel_vblank_evade_ctx *evade) 712 { 713 struct intel_crtc *crtc = evade->crtc; 714 struct intel_display *display = to_intel_display(crtc); 715 long timeout = msecs_to_jiffies_timeout(1); 716 wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base); 717 DEFINE_WAIT(wait); 718 int scanline; 719 720 if (evade->min <= 0 || evade->max <= 0) 721 return 0; 722 723 for (;;) { 724 /* 725 * prepare_to_wait() has a memory barrier, which guarantees 726 * other CPUs can see the task state update by the time we 727 * read the scanline. 728 */ 729 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); 730 731 scanline = intel_get_crtc_scanline(crtc); 732 if (scanline < evade->min || scanline > evade->max) 733 break; 734 735 if (!timeout) { 736 drm_dbg_kms(display->drm, 737 "Potential atomic update failure on pipe %c\n", 738 pipe_name(crtc->pipe)); 739 break; 740 } 741 742 local_irq_enable(); 743 744 timeout = schedule_timeout(timeout); 745 746 local_irq_disable(); 747 } 748 749 finish_wait(wq, &wait); 750 751 /* 752 * On VLV/CHV DSI the scanline counter would appear to 753 * increment approx. 1/3 of a scanline before start of vblank. 754 * The registers still get latched at start of vblank however. 755 * This means we must not write any registers on the first 756 * line of vblank (since not the whole line is actually in 757 * vblank). And unfortunately we can't use the interrupt to 758 * wait here since it will fire too soon. We could use the 759 * frame start interrupt instead since it will fire after the 760 * critical scanline, but that would require more changes 761 * in the interrupt code. So for now we'll just do the nasty 762 * thing and poll for the bad scanline to pass us by. 763 * 764 * FIXME figure out if BXT+ DSI suffers from this as well 765 */ 766 while (evade->need_vlv_dsi_wa && scanline == evade->vblank_start) 767 scanline = intel_get_crtc_scanline(crtc); 768 769 return scanline; 770 } 771