1 /* 2 * drm_irq.c IRQ and vblank support 3 * 4 * \author Rickard E. (Rik) Faith <faith@valinux.com> 5 * \author Gareth Hughes <gareth@valinux.com> 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the next 15 * paragraph) shall be included in all copies or substantial portions of the 16 * Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 #include <drm/drm_vblank.h> 28 #include <drm/drmP.h> 29 #include <linux/export.h> 30 31 #include "drm_trace.h" 32 #include "drm_internal.h" 33 34 /** 35 * DOC: vblank handling 36 * 37 * Vertical blanking plays a major role in graphics rendering. To achieve 38 * tear-free display, users must synchronize page flips and/or rendering to 39 * vertical blanking. The DRM API offers ioctls to perform page flips 40 * synchronized to vertical blanking and wait for vertical blanking. 41 * 42 * The DRM core handles most of the vertical blanking management logic, which 43 * involves filtering out spurious interrupts, keeping race-free blanking 44 * counters, coping with counter wrap-around and resets and keeping use counts. 45 * It relies on the driver to generate vertical blanking interrupts and 46 * optionally provide a hardware vertical blanking counter. 47 * 48 * Drivers must initialize the vertical blanking handling core with a call to 49 * drm_vblank_init(). Minimally, a driver needs to implement 50 * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call 51 * drm_crtc_handle_vblank() in it's vblank interrupt handler for working vblank 52 * support. 53 * 54 * Vertical blanking interrupts can be enabled by the DRM core or by drivers 55 * themselves (for instance to handle page flipping operations). The DRM core 56 * maintains a vertical blanking use count to ensure that the interrupts are not 57 * disabled while a user still needs them. To increment the use count, drivers 58 * call drm_crtc_vblank_get() and release the vblank reference again with 59 * drm_crtc_vblank_put(). In between these two calls vblank interrupts are 60 * guaranteed to be enabled. 61 * 62 * On many hardware disabling the vblank interrupt cannot be done in a race-free 63 * manner, see &drm_driver.vblank_disable_immediate and 64 * &drm_driver.max_vblank_count. In that case the vblank core only disables the 65 * vblanks after a timer has expired, which can be configured through the 66 * ``vblankoffdelay`` module parameter. 67 */ 68 69 /* Retry timestamp calculation up to 3 times to satisfy 70 * drm_timestamp_precision before giving up. 71 */ 72 #define DRM_TIMESTAMP_MAXRETRIES 3 73 74 /* Threshold in nanoseconds for detection of redundant 75 * vblank irq in drm_handle_vblank(). 1 msec should be ok. 76 */ 77 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000 78 79 static bool 80 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe, 81 ktime_t *tvblank, bool in_vblank_irq); 82 83 static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */ 84 85 static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */ 86 87 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600); 88 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600); 89 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)"); 90 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]"); 91 92 static void store_vblank(struct drm_device *dev, unsigned int pipe, 93 u32 vblank_count_inc, 94 ktime_t t_vblank, u32 last) 95 { 96 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 97 98 assert_spin_locked(&dev->vblank_time_lock); 99 100 vblank->last = last; 101 102 write_seqlock(&vblank->seqlock); 103 vblank->time = t_vblank; 104 vblank->count += vblank_count_inc; 105 write_sequnlock(&vblank->seqlock); 106 } 107 108 /* 109 * "No hw counter" fallback implementation of .get_vblank_counter() hook, 110 * if there is no useable hardware frame counter available. 111 */ 112 static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe) 113 { 114 WARN_ON_ONCE(dev->max_vblank_count != 0); 115 return 0; 116 } 117 118 static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe) 119 { 120 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 121 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe); 122 123 if (crtc->funcs->get_vblank_counter) 124 return crtc->funcs->get_vblank_counter(crtc); 125 } 126 127 if (dev->driver->get_vblank_counter) 128 return dev->driver->get_vblank_counter(dev, pipe); 129 130 return drm_vblank_no_hw_counter(dev, pipe); 131 } 132 133 /* 134 * Reset the stored timestamp for the current vblank count to correspond 135 * to the last vblank occurred. 136 * 137 * Only to be called from drm_crtc_vblank_on(). 138 * 139 * Note: caller must hold &drm_device.vbl_lock since this reads & writes 140 * device vblank fields. 141 */ 142 static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe) 143 { 144 u32 cur_vblank; 145 bool rc; 146 ktime_t t_vblank; 147 int count = DRM_TIMESTAMP_MAXRETRIES; 148 149 spin_lock(&dev->vblank_time_lock); 150 151 /* 152 * sample the current counter to avoid random jumps 153 * when drm_vblank_enable() applies the diff 154 */ 155 do { 156 cur_vblank = __get_vblank_counter(dev, pipe); 157 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false); 158 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0); 159 160 /* 161 * Only reinitialize corresponding vblank timestamp if high-precision query 162 * available and didn't fail. Otherwise reinitialize delayed at next vblank 163 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid. 164 */ 165 if (!rc) 166 t_vblank = 0; 167 168 /* 169 * +1 to make sure user will never see the same 170 * vblank counter value before and after a modeset 171 */ 172 store_vblank(dev, pipe, 1, t_vblank, cur_vblank); 173 174 spin_unlock(&dev->vblank_time_lock); 175 } 176 177 /* 178 * Call back into the driver to update the appropriate vblank counter 179 * (specified by @pipe). Deal with wraparound, if it occurred, and 180 * update the last read value so we can deal with wraparound on the next 181 * call if necessary. 182 * 183 * Only necessary when going from off->on, to account for frames we 184 * didn't get an interrupt for. 185 * 186 * Note: caller must hold &drm_device.vbl_lock since this reads & writes 187 * device vblank fields. 188 */ 189 static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, 190 bool in_vblank_irq) 191 { 192 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 193 u32 cur_vblank, diff; 194 bool rc; 195 ktime_t t_vblank; 196 int count = DRM_TIMESTAMP_MAXRETRIES; 197 int framedur_ns = vblank->framedur_ns; 198 199 /* 200 * Interrupts were disabled prior to this call, so deal with counter 201 * wrap if needed. 202 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events 203 * here if the register is small or we had vblank interrupts off for 204 * a long time. 205 * 206 * We repeat the hardware vblank counter & timestamp query until 207 * we get consistent results. This to prevent races between gpu 208 * updating its hardware counter while we are retrieving the 209 * corresponding vblank timestamp. 210 */ 211 do { 212 cur_vblank = __get_vblank_counter(dev, pipe); 213 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq); 214 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0); 215 216 if (dev->max_vblank_count != 0) { 217 /* trust the hw counter when it's around */ 218 diff = (cur_vblank - vblank->last) & dev->max_vblank_count; 219 } else if (rc && framedur_ns) { 220 u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time)); 221 222 /* 223 * Figure out how many vblanks we've missed based 224 * on the difference in the timestamps and the 225 * frame/field duration. 226 */ 227 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns); 228 229 if (diff == 0 && in_vblank_irq) 230 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored." 231 " diff_ns = %lld, framedur_ns = %d)\n", 232 pipe, (long long) diff_ns, framedur_ns); 233 } else { 234 /* some kind of default for drivers w/o accurate vbl timestamping */ 235 diff = in_vblank_irq ? 1 : 0; 236 } 237 238 /* 239 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset 240 * interval? If so then vblank irqs keep running and it will likely 241 * happen that the hardware vblank counter is not trustworthy as it 242 * might reset at some point in that interval and vblank timestamps 243 * are not trustworthy either in that interval. Iow. this can result 244 * in a bogus diff >> 1 which must be avoided as it would cause 245 * random large forward jumps of the software vblank counter. 246 */ 247 if (diff > 1 && (vblank->inmodeset & 0x2)) { 248 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u" 249 " due to pre-modeset.\n", pipe, diff); 250 diff = 1; 251 } 252 253 DRM_DEBUG_VBL("updating vblank count on crtc %u:" 254 " current=%llu, diff=%u, hw=%u hw_last=%u\n", 255 pipe, vblank->count, diff, cur_vblank, vblank->last); 256 257 if (diff == 0) { 258 WARN_ON_ONCE(cur_vblank != vblank->last); 259 return; 260 } 261 262 /* 263 * Only reinitialize corresponding vblank timestamp if high-precision query 264 * available and didn't fail, or we were called from the vblank interrupt. 265 * Otherwise reinitialize delayed at next vblank interrupt and assign 0 266 * for now, to mark the vblanktimestamp as invalid. 267 */ 268 if (!rc && !in_vblank_irq) 269 t_vblank = 0; 270 271 store_vblank(dev, pipe, diff, t_vblank, cur_vblank); 272 } 273 274 static u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe) 275 { 276 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 277 278 if (WARN_ON(pipe >= dev->num_crtcs)) 279 return 0; 280 281 return vblank->count; 282 } 283 284 /** 285 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter 286 * @crtc: which counter to retrieve 287 * 288 * This function is similar to drm_crtc_vblank_count() but this function 289 * interpolates to handle a race with vblank interrupts using the high precision 290 * timestamping support. 291 * 292 * This is mostly useful for hardware that can obtain the scanout position, but 293 * doesn't have a hardware frame counter. 294 */ 295 u32 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc) 296 { 297 struct drm_device *dev = crtc->dev; 298 unsigned int pipe = drm_crtc_index(crtc); 299 u32 vblank; 300 unsigned long flags; 301 302 WARN_ONCE(drm_debug & DRM_UT_VBL && !dev->driver->get_vblank_timestamp, 303 "This function requires support for accurate vblank timestamps."); 304 305 spin_lock_irqsave(&dev->vblank_time_lock, flags); 306 307 drm_update_vblank_count(dev, pipe, false); 308 vblank = drm_vblank_count(dev, pipe); 309 310 spin_unlock_irqrestore(&dev->vblank_time_lock, flags); 311 312 return vblank; 313 } 314 EXPORT_SYMBOL(drm_crtc_accurate_vblank_count); 315 316 static void __disable_vblank(struct drm_device *dev, unsigned int pipe) 317 { 318 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 319 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe); 320 321 if (crtc->funcs->disable_vblank) { 322 crtc->funcs->disable_vblank(crtc); 323 return; 324 } 325 } 326 327 dev->driver->disable_vblank(dev, pipe); 328 } 329 330 /* 331 * Disable vblank irq's on crtc, make sure that last vblank count 332 * of hardware and corresponding consistent software vblank counter 333 * are preserved, even if there are any spurious vblank irq's after 334 * disable. 335 */ 336 void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe) 337 { 338 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 339 unsigned long irqflags; 340 341 assert_spin_locked(&dev->vbl_lock); 342 343 /* Prevent vblank irq processing while disabling vblank irqs, 344 * so no updates of timestamps or count can happen after we've 345 * disabled. Needed to prevent races in case of delayed irq's. 346 */ 347 spin_lock_irqsave(&dev->vblank_time_lock, irqflags); 348 349 /* 350 * Only disable vblank interrupts if they're enabled. This avoids 351 * calling the ->disable_vblank() operation in atomic context with the 352 * hardware potentially runtime suspended. 353 */ 354 if (vblank->enabled) { 355 __disable_vblank(dev, pipe); 356 vblank->enabled = false; 357 } 358 359 /* 360 * Always update the count and timestamp to maintain the 361 * appearance that the counter has been ticking all along until 362 * this time. This makes the count account for the entire time 363 * between drm_crtc_vblank_on() and drm_crtc_vblank_off(). 364 */ 365 drm_update_vblank_count(dev, pipe, false); 366 367 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); 368 } 369 370 static void vblank_disable_fn(unsigned long arg) 371 { 372 struct drm_vblank_crtc *vblank = (void *)arg; 373 struct drm_device *dev = vblank->dev; 374 unsigned int pipe = vblank->pipe; 375 unsigned long irqflags; 376 377 spin_lock_irqsave(&dev->vbl_lock, irqflags); 378 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) { 379 DRM_DEBUG("disabling vblank on crtc %u\n", pipe); 380 drm_vblank_disable_and_save(dev, pipe); 381 } 382 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 383 } 384 385 void drm_vblank_cleanup(struct drm_device *dev) 386 { 387 unsigned int pipe; 388 389 /* Bail if the driver didn't call drm_vblank_init() */ 390 if (dev->num_crtcs == 0) 391 return; 392 393 for (pipe = 0; pipe < dev->num_crtcs; pipe++) { 394 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 395 396 WARN_ON(READ_ONCE(vblank->enabled) && 397 drm_core_check_feature(dev, DRIVER_MODESET)); 398 399 del_timer_sync(&vblank->disable_timer); 400 } 401 402 kfree(dev->vblank); 403 404 dev->num_crtcs = 0; 405 } 406 407 /** 408 * drm_vblank_init - initialize vblank support 409 * @dev: DRM device 410 * @num_crtcs: number of CRTCs supported by @dev 411 * 412 * This function initializes vblank support for @num_crtcs display pipelines. 413 * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for 414 * drivers with a &drm_driver.release callback. 415 * 416 * Returns: 417 * Zero on success or a negative error code on failure. 418 */ 419 int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs) 420 { 421 int ret = -ENOMEM; 422 unsigned int i; 423 424 spin_lock_init(&dev->vbl_lock); 425 spin_lock_init(&dev->vblank_time_lock); 426 427 dev->num_crtcs = num_crtcs; 428 429 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL); 430 if (!dev->vblank) 431 goto err; 432 433 for (i = 0; i < num_crtcs; i++) { 434 struct drm_vblank_crtc *vblank = &dev->vblank[i]; 435 436 vblank->dev = dev; 437 vblank->pipe = i; 438 init_waitqueue_head(&vblank->queue); 439 setup_timer(&vblank->disable_timer, vblank_disable_fn, 440 (unsigned long)vblank); 441 seqlock_init(&vblank->seqlock); 442 } 443 444 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n"); 445 446 /* Driver specific high-precision vblank timestamping supported? */ 447 if (dev->driver->get_vblank_timestamp) 448 DRM_INFO("Driver supports precise vblank timestamp query.\n"); 449 else 450 DRM_INFO("No driver support for vblank timestamp query.\n"); 451 452 /* Must have precise timestamping for reliable vblank instant disable */ 453 if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) { 454 dev->vblank_disable_immediate = false; 455 DRM_INFO("Setting vblank_disable_immediate to false because " 456 "get_vblank_timestamp == NULL\n"); 457 } 458 459 return 0; 460 461 err: 462 dev->num_crtcs = 0; 463 return ret; 464 } 465 EXPORT_SYMBOL(drm_vblank_init); 466 467 /** 468 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC 469 * @crtc: which CRTC's vblank waitqueue to retrieve 470 * 471 * This function returns a pointer to the vblank waitqueue for the CRTC. 472 * Drivers can use this to implement vblank waits using wait_event() and related 473 * functions. 474 */ 475 wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc) 476 { 477 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue; 478 } 479 EXPORT_SYMBOL(drm_crtc_vblank_waitqueue); 480 481 482 /** 483 * drm_calc_timestamping_constants - calculate vblank timestamp constants 484 * @crtc: drm_crtc whose timestamp constants should be updated. 485 * @mode: display mode containing the scanout timings 486 * 487 * Calculate and store various constants which are later needed by vblank and 488 * swap-completion timestamping, e.g, by 489 * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true 490 * scanout timing, so they take things like panel scaling or other adjustments 491 * into account. 492 */ 493 void drm_calc_timestamping_constants(struct drm_crtc *crtc, 494 const struct drm_display_mode *mode) 495 { 496 struct drm_device *dev = crtc->dev; 497 unsigned int pipe = drm_crtc_index(crtc); 498 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 499 int linedur_ns = 0, framedur_ns = 0; 500 int dotclock = mode->crtc_clock; 501 502 if (!dev->num_crtcs) 503 return; 504 505 if (WARN_ON(pipe >= dev->num_crtcs)) 506 return; 507 508 /* Valid dotclock? */ 509 if (dotclock > 0) { 510 int frame_size = mode->crtc_htotal * mode->crtc_vtotal; 511 512 /* 513 * Convert scanline length in pixels and video 514 * dot clock to line duration and frame duration 515 * in nanoseconds: 516 */ 517 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock); 518 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock); 519 520 /* 521 * Fields of interlaced scanout modes are only half a frame duration. 522 */ 523 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 524 framedur_ns /= 2; 525 } else 526 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n", 527 crtc->base.id); 528 529 vblank->linedur_ns = linedur_ns; 530 vblank->framedur_ns = framedur_ns; 531 vblank->hwmode = *mode; 532 533 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n", 534 crtc->base.id, mode->crtc_htotal, 535 mode->crtc_vtotal, mode->crtc_vdisplay); 536 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n", 537 crtc->base.id, dotclock, framedur_ns, linedur_ns); 538 } 539 EXPORT_SYMBOL(drm_calc_timestamping_constants); 540 541 /** 542 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper 543 * @dev: DRM device 544 * @pipe: index of CRTC whose vblank timestamp to retrieve 545 * @max_error: Desired maximum allowable error in timestamps (nanosecs) 546 * On return contains true maximum error of timestamp 547 * @vblank_time: Pointer to time which should receive the timestamp 548 * @in_vblank_irq: 549 * True when called from drm_crtc_handle_vblank(). Some drivers 550 * need to apply some workarounds for gpu-specific vblank irq quirks 551 * if flag is set. 552 * 553 * Implements calculation of exact vblank timestamps from given drm_display_mode 554 * timings and current video scanout position of a CRTC. This can be directly 555 * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver 556 * if &drm_driver.get_scanout_position is implemented. 557 * 558 * The current implementation only handles standard video modes. For double scan 559 * and interlaced modes the driver is supposed to adjust the hardware mode 560 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to 561 * match the scanout position reported. 562 * 563 * Note that atomic drivers must call drm_calc_timestamping_constants() before 564 * enabling a CRTC. The atomic helpers already take care of that in 565 * drm_atomic_helper_update_legacy_modeset_state(). 566 * 567 * Returns: 568 * 569 * Returns true on success, and false on failure, i.e. when no accurate 570 * timestamp could be acquired. 571 */ 572 bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, 573 unsigned int pipe, 574 int *max_error, 575 ktime_t *vblank_time, 576 bool in_vblank_irq) 577 { 578 struct timespec64 ts_etime, ts_vblank_time; 579 ktime_t stime, etime; 580 bool vbl_status; 581 struct drm_crtc *crtc; 582 const struct drm_display_mode *mode; 583 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 584 int vpos, hpos, i; 585 int delta_ns, duration_ns; 586 587 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 588 return false; 589 590 crtc = drm_crtc_from_index(dev, pipe); 591 592 if (pipe >= dev->num_crtcs || !crtc) { 593 DRM_ERROR("Invalid crtc %u\n", pipe); 594 return false; 595 } 596 597 /* Scanout position query not supported? Should not happen. */ 598 if (!dev->driver->get_scanout_position) { 599 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n"); 600 return false; 601 } 602 603 if (drm_drv_uses_atomic_modeset(dev)) 604 mode = &vblank->hwmode; 605 else 606 mode = &crtc->hwmode; 607 608 /* If mode timing undefined, just return as no-op: 609 * Happens during initial modesetting of a crtc. 610 */ 611 if (mode->crtc_clock == 0) { 612 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe); 613 WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev)); 614 615 return false; 616 } 617 618 /* Get current scanout position with system timestamp. 619 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times 620 * if single query takes longer than max_error nanoseconds. 621 * 622 * This guarantees a tight bound on maximum error if 623 * code gets preempted or delayed for some reason. 624 */ 625 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) { 626 /* 627 * Get vertical and horizontal scanout position vpos, hpos, 628 * and bounding timestamps stime, etime, pre/post query. 629 */ 630 vbl_status = dev->driver->get_scanout_position(dev, pipe, 631 in_vblank_irq, 632 &vpos, &hpos, 633 &stime, &etime, 634 mode); 635 636 /* Return as no-op if scanout query unsupported or failed. */ 637 if (!vbl_status) { 638 DRM_DEBUG("crtc %u : scanoutpos query failed.\n", 639 pipe); 640 return false; 641 } 642 643 /* Compute uncertainty in timestamp of scanout position query. */ 644 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime); 645 646 /* Accept result with < max_error nsecs timing uncertainty. */ 647 if (duration_ns <= *max_error) 648 break; 649 } 650 651 /* Noisy system timing? */ 652 if (i == DRM_TIMESTAMP_MAXRETRIES) { 653 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n", 654 pipe, duration_ns/1000, *max_error/1000, i); 655 } 656 657 /* Return upper bound of timestamp precision error. */ 658 *max_error = duration_ns; 659 660 /* Convert scanout position into elapsed time at raw_time query 661 * since start of scanout at first display scanline. delta_ns 662 * can be negative if start of scanout hasn't happened yet. 663 */ 664 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos), 665 mode->crtc_clock); 666 667 /* save this only for debugging purposes */ 668 ts_etime = ktime_to_timespec64(etime); 669 ts_vblank_time = ktime_to_timespec64(*vblank_time); 670 /* Subtract time delta from raw timestamp to get final 671 * vblank_time timestamp for end of vblank. 672 */ 673 etime = ktime_sub_ns(etime, delta_ns); 674 *vblank_time = etime; 675 676 DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n", 677 pipe, hpos, vpos, 678 (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000, 679 (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000, 680 duration_ns / 1000, i); 681 682 return true; 683 } 684 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos); 685 686 /** 687 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent 688 * vblank interval 689 * @dev: DRM device 690 * @pipe: index of CRTC whose vblank timestamp to retrieve 691 * @tvblank: Pointer to target time which should receive the timestamp 692 * @in_vblank_irq: 693 * True when called from drm_crtc_handle_vblank(). Some drivers 694 * need to apply some workarounds for gpu-specific vblank irq quirks 695 * if flag is set. 696 * 697 * Fetches the system timestamp corresponding to the time of the most recent 698 * vblank interval on specified CRTC. May call into kms-driver to 699 * compute the timestamp with a high-precision GPU specific method. 700 * 701 * Returns zero if timestamp originates from uncorrected do_gettimeofday() 702 * call, i.e., it isn't very precisely locked to the true vblank. 703 * 704 * Returns: 705 * True if timestamp is considered to be very precise, false otherwise. 706 */ 707 static bool 708 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe, 709 ktime_t *tvblank, bool in_vblank_irq) 710 { 711 bool ret = false; 712 713 /* Define requested maximum error on timestamps (nanoseconds). */ 714 int max_error = (int) drm_timestamp_precision * 1000; 715 716 /* Query driver if possible and precision timestamping enabled. */ 717 if (dev->driver->get_vblank_timestamp && (max_error > 0)) 718 ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error, 719 tvblank, in_vblank_irq); 720 721 /* GPU high precision timestamp query unsupported or failed. 722 * Return current monotonic/gettimeofday timestamp as best estimate. 723 */ 724 if (!ret) 725 *tvblank = ktime_get(); 726 727 return ret; 728 } 729 730 /** 731 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value 732 * @crtc: which counter to retrieve 733 * 734 * Fetches the "cooked" vblank count value that represents the number of 735 * vblank events since the system was booted, including lost events due to 736 * modesetting activity. Note that this timer isn't correct against a racing 737 * vblank interrupt (since it only reports the software vblank counter), see 738 * drm_crtc_accurate_vblank_count() for such use-cases. 739 * 740 * Returns: 741 * The software vblank counter. 742 */ 743 u64 drm_crtc_vblank_count(struct drm_crtc *crtc) 744 { 745 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc)); 746 } 747 EXPORT_SYMBOL(drm_crtc_vblank_count); 748 749 /** 750 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the 751 * system timestamp corresponding to that vblank counter value. 752 * @dev: DRM device 753 * @pipe: index of CRTC whose counter to retrieve 754 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp. 755 * 756 * Fetches the "cooked" vblank count value that represents the number of 757 * vblank events since the system was booted, including lost events due to 758 * modesetting activity. Returns corresponding system timestamp of the time 759 * of the vblank interval that corresponds to the current vblank counter value. 760 * 761 * This is the legacy version of drm_crtc_vblank_count_and_time(). 762 */ 763 static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe, 764 ktime_t *vblanktime) 765 { 766 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 767 u64 vblank_count; 768 unsigned int seq; 769 770 if (WARN_ON(pipe >= dev->num_crtcs)) { 771 *vblanktime = 0; 772 return 0; 773 } 774 775 do { 776 seq = read_seqbegin(&vblank->seqlock); 777 vblank_count = vblank->count; 778 *vblanktime = vblank->time; 779 } while (read_seqretry(&vblank->seqlock, seq)); 780 781 return vblank_count; 782 } 783 784 /** 785 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value 786 * and the system timestamp corresponding to that vblank counter value 787 * @crtc: which counter to retrieve 788 * @vblanktime: Pointer to time to receive the vblank timestamp. 789 * 790 * Fetches the "cooked" vblank count value that represents the number of 791 * vblank events since the system was booted, including lost events due to 792 * modesetting activity. Returns corresponding system timestamp of the time 793 * of the vblank interval that corresponds to the current vblank counter value. 794 */ 795 u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, 796 ktime_t *vblanktime) 797 { 798 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc), 799 vblanktime); 800 } 801 EXPORT_SYMBOL(drm_crtc_vblank_count_and_time); 802 803 static void send_vblank_event(struct drm_device *dev, 804 struct drm_pending_vblank_event *e, 805 u64 seq, ktime_t now) 806 { 807 struct timespec64 tv; 808 809 switch (e->event.base.type) { 810 case DRM_EVENT_VBLANK: 811 case DRM_EVENT_FLIP_COMPLETE: 812 tv = ktime_to_timespec64(now); 813 e->event.vbl.sequence = seq; 814 /* 815 * e->event is a user space structure, with hardcoded unsigned 816 * 32-bit seconds/microseconds. This is safe as we always use 817 * monotonic timestamps since linux-4.15 818 */ 819 e->event.vbl.tv_sec = tv.tv_sec; 820 e->event.vbl.tv_usec = tv.tv_nsec / 1000; 821 break; 822 case DRM_EVENT_CRTC_SEQUENCE: 823 if (seq) 824 e->event.seq.sequence = seq; 825 e->event.seq.time_ns = ktime_to_ns(now); 826 break; 827 } 828 trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq); 829 drm_send_event_locked(dev, &e->base); 830 } 831 832 /** 833 * drm_crtc_arm_vblank_event - arm vblank event after pageflip 834 * @crtc: the source CRTC of the vblank event 835 * @e: the event to send 836 * 837 * A lot of drivers need to generate vblank events for the very next vblank 838 * interrupt. For example when the page flip interrupt happens when the page 839 * flip gets armed, but not when it actually executes within the next vblank 840 * period. This helper function implements exactly the required vblank arming 841 * behaviour. 842 * 843 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an 844 * atomic commit must ensure that the next vblank happens at exactly the same 845 * time as the atomic commit is committed to the hardware. This function itself 846 * does **not** protect against the next vblank interrupt racing with either this 847 * function call or the atomic commit operation. A possible sequence could be: 848 * 849 * 1. Driver commits new hardware state into vblank-synchronized registers. 850 * 2. A vblank happens, committing the hardware state. Also the corresponding 851 * vblank interrupt is fired off and fully processed by the interrupt 852 * handler. 853 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event(). 854 * 4. The event is only send out for the next vblank, which is wrong. 855 * 856 * An equivalent race can happen when the driver calls 857 * drm_crtc_arm_vblank_event() before writing out the new hardware state. 858 * 859 * The only way to make this work safely is to prevent the vblank from firing 860 * (and the hardware from committing anything else) until the entire atomic 861 * commit sequence has run to completion. If the hardware does not have such a 862 * feature (e.g. using a "go" bit), then it is unsafe to use this functions. 863 * Instead drivers need to manually send out the event from their interrupt 864 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no 865 * possible race with the hardware committing the atomic update. 866 * 867 * Caller must hold a vblank reference for the event @e, which will be dropped 868 * when the next vblank arrives. 869 */ 870 void drm_crtc_arm_vblank_event(struct drm_crtc *crtc, 871 struct drm_pending_vblank_event *e) 872 { 873 struct drm_device *dev = crtc->dev; 874 unsigned int pipe = drm_crtc_index(crtc); 875 876 assert_spin_locked(&dev->event_lock); 877 878 e->pipe = pipe; 879 e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1; 880 list_add_tail(&e->base.link, &dev->vblank_event_list); 881 } 882 EXPORT_SYMBOL(drm_crtc_arm_vblank_event); 883 884 /** 885 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip 886 * @crtc: the source CRTC of the vblank event 887 * @e: the event to send 888 * 889 * Updates sequence # and timestamp on event for the most recently processed 890 * vblank, and sends it to userspace. Caller must hold event lock. 891 * 892 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain 893 * situation, especially to send out events for atomic commit operations. 894 */ 895 void drm_crtc_send_vblank_event(struct drm_crtc *crtc, 896 struct drm_pending_vblank_event *e) 897 { 898 struct drm_device *dev = crtc->dev; 899 u64 seq; 900 unsigned int pipe = drm_crtc_index(crtc); 901 ktime_t now; 902 903 if (dev->num_crtcs > 0) { 904 seq = drm_vblank_count_and_time(dev, pipe, &now); 905 } else { 906 seq = 0; 907 908 now = ktime_get(); 909 } 910 e->pipe = pipe; 911 send_vblank_event(dev, e, seq, now); 912 } 913 EXPORT_SYMBOL(drm_crtc_send_vblank_event); 914 915 static int __enable_vblank(struct drm_device *dev, unsigned int pipe) 916 { 917 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 918 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe); 919 920 if (crtc->funcs->enable_vblank) 921 return crtc->funcs->enable_vblank(crtc); 922 } 923 924 return dev->driver->enable_vblank(dev, pipe); 925 } 926 927 static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe) 928 { 929 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 930 int ret = 0; 931 932 assert_spin_locked(&dev->vbl_lock); 933 934 spin_lock(&dev->vblank_time_lock); 935 936 if (!vblank->enabled) { 937 /* 938 * Enable vblank irqs under vblank_time_lock protection. 939 * All vblank count & timestamp updates are held off 940 * until we are done reinitializing master counter and 941 * timestamps. Filtercode in drm_handle_vblank() will 942 * prevent double-accounting of same vblank interval. 943 */ 944 ret = __enable_vblank(dev, pipe); 945 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret); 946 if (ret) { 947 atomic_dec(&vblank->refcount); 948 } else { 949 drm_update_vblank_count(dev, pipe, 0); 950 /* drm_update_vblank_count() includes a wmb so we just 951 * need to ensure that the compiler emits the write 952 * to mark the vblank as enabled after the call 953 * to drm_update_vblank_count(). 954 */ 955 WRITE_ONCE(vblank->enabled, true); 956 } 957 } 958 959 spin_unlock(&dev->vblank_time_lock); 960 961 return ret; 962 } 963 964 static int drm_vblank_get(struct drm_device *dev, unsigned int pipe) 965 { 966 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 967 unsigned long irqflags; 968 int ret = 0; 969 970 if (!dev->num_crtcs) 971 return -EINVAL; 972 973 if (WARN_ON(pipe >= dev->num_crtcs)) 974 return -EINVAL; 975 976 spin_lock_irqsave(&dev->vbl_lock, irqflags); 977 /* Going from 0->1 means we have to enable interrupts again */ 978 if (atomic_add_return(1, &vblank->refcount) == 1) { 979 ret = drm_vblank_enable(dev, pipe); 980 } else { 981 if (!vblank->enabled) { 982 atomic_dec(&vblank->refcount); 983 ret = -EINVAL; 984 } 985 } 986 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 987 988 return ret; 989 } 990 991 /** 992 * drm_crtc_vblank_get - get a reference count on vblank events 993 * @crtc: which CRTC to own 994 * 995 * Acquire a reference count on vblank events to avoid having them disabled 996 * while in use. 997 * 998 * Returns: 999 * Zero on success or a negative error code on failure. 1000 */ 1001 int drm_crtc_vblank_get(struct drm_crtc *crtc) 1002 { 1003 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc)); 1004 } 1005 EXPORT_SYMBOL(drm_crtc_vblank_get); 1006 1007 static void drm_vblank_put(struct drm_device *dev, unsigned int pipe) 1008 { 1009 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1010 1011 if (WARN_ON(pipe >= dev->num_crtcs)) 1012 return; 1013 1014 if (WARN_ON(atomic_read(&vblank->refcount) == 0)) 1015 return; 1016 1017 /* Last user schedules interrupt disable */ 1018 if (atomic_dec_and_test(&vblank->refcount)) { 1019 if (drm_vblank_offdelay == 0) 1020 return; 1021 else if (drm_vblank_offdelay < 0) 1022 vblank_disable_fn((unsigned long)vblank); 1023 else if (!dev->vblank_disable_immediate) 1024 mod_timer(&vblank->disable_timer, 1025 jiffies + ((drm_vblank_offdelay * HZ)/1000)); 1026 } 1027 } 1028 1029 /** 1030 * drm_crtc_vblank_put - give up ownership of vblank events 1031 * @crtc: which counter to give up 1032 * 1033 * Release ownership of a given vblank counter, turning off interrupts 1034 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds. 1035 */ 1036 void drm_crtc_vblank_put(struct drm_crtc *crtc) 1037 { 1038 drm_vblank_put(crtc->dev, drm_crtc_index(crtc)); 1039 } 1040 EXPORT_SYMBOL(drm_crtc_vblank_put); 1041 1042 /** 1043 * drm_wait_one_vblank - wait for one vblank 1044 * @dev: DRM device 1045 * @pipe: CRTC index 1046 * 1047 * This waits for one vblank to pass on @pipe, using the irq driver interfaces. 1048 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g. 1049 * due to lack of driver support or because the crtc is off. 1050 * 1051 * This is the legacy version of drm_crtc_wait_one_vblank(). 1052 */ 1053 void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe) 1054 { 1055 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1056 int ret; 1057 u32 last; 1058 1059 if (WARN_ON(pipe >= dev->num_crtcs)) 1060 return; 1061 1062 ret = drm_vblank_get(dev, pipe); 1063 if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret)) 1064 return; 1065 1066 last = drm_vblank_count(dev, pipe); 1067 1068 ret = wait_event_timeout(vblank->queue, 1069 last != drm_vblank_count(dev, pipe), 1070 msecs_to_jiffies(100)); 1071 1072 WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe); 1073 1074 drm_vblank_put(dev, pipe); 1075 } 1076 EXPORT_SYMBOL(drm_wait_one_vblank); 1077 1078 /** 1079 * drm_crtc_wait_one_vblank - wait for one vblank 1080 * @crtc: DRM crtc 1081 * 1082 * This waits for one vblank to pass on @crtc, using the irq driver interfaces. 1083 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g. 1084 * due to lack of driver support or because the crtc is off. 1085 */ 1086 void drm_crtc_wait_one_vblank(struct drm_crtc *crtc) 1087 { 1088 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc)); 1089 } 1090 EXPORT_SYMBOL(drm_crtc_wait_one_vblank); 1091 1092 /** 1093 * drm_crtc_vblank_off - disable vblank events on a CRTC 1094 * @crtc: CRTC in question 1095 * 1096 * Drivers can use this function to shut down the vblank interrupt handling when 1097 * disabling a crtc. This function ensures that the latest vblank frame count is 1098 * stored so that drm_vblank_on can restore it again. 1099 * 1100 * Drivers must use this function when the hardware vblank counter can get 1101 * reset, e.g. when suspending or disabling the @crtc in general. 1102 */ 1103 void drm_crtc_vblank_off(struct drm_crtc *crtc) 1104 { 1105 struct drm_device *dev = crtc->dev; 1106 unsigned int pipe = drm_crtc_index(crtc); 1107 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1108 struct drm_pending_vblank_event *e, *t; 1109 1110 ktime_t now; 1111 unsigned long irqflags; 1112 u64 seq; 1113 1114 if (WARN_ON(pipe >= dev->num_crtcs)) 1115 return; 1116 1117 spin_lock_irqsave(&dev->event_lock, irqflags); 1118 1119 spin_lock(&dev->vbl_lock); 1120 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n", 1121 pipe, vblank->enabled, vblank->inmodeset); 1122 1123 /* Avoid redundant vblank disables without previous 1124 * drm_crtc_vblank_on(). */ 1125 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset) 1126 drm_vblank_disable_and_save(dev, pipe); 1127 1128 wake_up(&vblank->queue); 1129 1130 /* 1131 * Prevent subsequent drm_vblank_get() from re-enabling 1132 * the vblank interrupt by bumping the refcount. 1133 */ 1134 if (!vblank->inmodeset) { 1135 atomic_inc(&vblank->refcount); 1136 vblank->inmodeset = 1; 1137 } 1138 spin_unlock(&dev->vbl_lock); 1139 1140 /* Send any queued vblank events, lest the natives grow disquiet */ 1141 seq = drm_vblank_count_and_time(dev, pipe, &now); 1142 1143 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { 1144 if (e->pipe != pipe) 1145 continue; 1146 DRM_DEBUG("Sending premature vblank event on disable: " 1147 "wanted %llu, current %llu\n", 1148 e->sequence, seq); 1149 list_del(&e->base.link); 1150 drm_vblank_put(dev, pipe); 1151 send_vblank_event(dev, e, seq, now); 1152 } 1153 spin_unlock_irqrestore(&dev->event_lock, irqflags); 1154 1155 /* Will be reset by the modeset helpers when re-enabling the crtc by 1156 * calling drm_calc_timestamping_constants(). */ 1157 vblank->hwmode.crtc_clock = 0; 1158 } 1159 EXPORT_SYMBOL(drm_crtc_vblank_off); 1160 1161 /** 1162 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC 1163 * @crtc: CRTC in question 1164 * 1165 * Drivers can use this function to reset the vblank state to off at load time. 1166 * Drivers should use this together with the drm_crtc_vblank_off() and 1167 * drm_crtc_vblank_on() functions. The difference compared to 1168 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter 1169 * and hence doesn't need to call any driver hooks. 1170 * 1171 * This is useful for recovering driver state e.g. on driver load, or on resume. 1172 */ 1173 void drm_crtc_vblank_reset(struct drm_crtc *crtc) 1174 { 1175 struct drm_device *dev = crtc->dev; 1176 unsigned long irqflags; 1177 unsigned int pipe = drm_crtc_index(crtc); 1178 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1179 1180 spin_lock_irqsave(&dev->vbl_lock, irqflags); 1181 /* 1182 * Prevent subsequent drm_vblank_get() from enabling the vblank 1183 * interrupt by bumping the refcount. 1184 */ 1185 if (!vblank->inmodeset) { 1186 atomic_inc(&vblank->refcount); 1187 vblank->inmodeset = 1; 1188 } 1189 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 1190 1191 WARN_ON(!list_empty(&dev->vblank_event_list)); 1192 } 1193 EXPORT_SYMBOL(drm_crtc_vblank_reset); 1194 1195 /** 1196 * drm_crtc_vblank_on - enable vblank events on a CRTC 1197 * @crtc: CRTC in question 1198 * 1199 * This functions restores the vblank interrupt state captured with 1200 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note 1201 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be 1202 * unbalanced and so can also be unconditionally called in driver load code to 1203 * reflect the current hardware state of the crtc. 1204 */ 1205 void drm_crtc_vblank_on(struct drm_crtc *crtc) 1206 { 1207 struct drm_device *dev = crtc->dev; 1208 unsigned int pipe = drm_crtc_index(crtc); 1209 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1210 unsigned long irqflags; 1211 1212 if (WARN_ON(pipe >= dev->num_crtcs)) 1213 return; 1214 1215 spin_lock_irqsave(&dev->vbl_lock, irqflags); 1216 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n", 1217 pipe, vblank->enabled, vblank->inmodeset); 1218 1219 /* Drop our private "prevent drm_vblank_get" refcount */ 1220 if (vblank->inmodeset) { 1221 atomic_dec(&vblank->refcount); 1222 vblank->inmodeset = 0; 1223 } 1224 1225 drm_reset_vblank_timestamp(dev, pipe); 1226 1227 /* 1228 * re-enable interrupts if there are users left, or the 1229 * user wishes vblank interrupts to be enabled all the time. 1230 */ 1231 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0) 1232 WARN_ON(drm_vblank_enable(dev, pipe)); 1233 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 1234 } 1235 EXPORT_SYMBOL(drm_crtc_vblank_on); 1236 1237 static void drm_legacy_vblank_pre_modeset(struct drm_device *dev, 1238 unsigned int pipe) 1239 { 1240 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1241 1242 /* vblank is not initialized (IRQ not installed ?), or has been freed */ 1243 if (!dev->num_crtcs) 1244 return; 1245 1246 if (WARN_ON(pipe >= dev->num_crtcs)) 1247 return; 1248 1249 /* 1250 * To avoid all the problems that might happen if interrupts 1251 * were enabled/disabled around or between these calls, we just 1252 * have the kernel take a reference on the CRTC (just once though 1253 * to avoid corrupting the count if multiple, mismatch calls occur), 1254 * so that interrupts remain enabled in the interim. 1255 */ 1256 if (!vblank->inmodeset) { 1257 vblank->inmodeset = 0x1; 1258 if (drm_vblank_get(dev, pipe) == 0) 1259 vblank->inmodeset |= 0x2; 1260 } 1261 } 1262 1263 static void drm_legacy_vblank_post_modeset(struct drm_device *dev, 1264 unsigned int pipe) 1265 { 1266 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1267 unsigned long irqflags; 1268 1269 /* vblank is not initialized (IRQ not installed ?), or has been freed */ 1270 if (!dev->num_crtcs) 1271 return; 1272 1273 if (WARN_ON(pipe >= dev->num_crtcs)) 1274 return; 1275 1276 if (vblank->inmodeset) { 1277 spin_lock_irqsave(&dev->vbl_lock, irqflags); 1278 drm_reset_vblank_timestamp(dev, pipe); 1279 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 1280 1281 if (vblank->inmodeset & 0x2) 1282 drm_vblank_put(dev, pipe); 1283 1284 vblank->inmodeset = 0; 1285 } 1286 } 1287 1288 int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data, 1289 struct drm_file *file_priv) 1290 { 1291 struct drm_modeset_ctl *modeset = data; 1292 unsigned int pipe; 1293 1294 /* If drm_vblank_init() hasn't been called yet, just no-op */ 1295 if (!dev->num_crtcs) 1296 return 0; 1297 1298 /* KMS drivers handle this internally */ 1299 if (!drm_core_check_feature(dev, DRIVER_LEGACY)) 1300 return 0; 1301 1302 pipe = modeset->crtc; 1303 if (pipe >= dev->num_crtcs) 1304 return -EINVAL; 1305 1306 switch (modeset->cmd) { 1307 case _DRM_PRE_MODESET: 1308 drm_legacy_vblank_pre_modeset(dev, pipe); 1309 break; 1310 case _DRM_POST_MODESET: 1311 drm_legacy_vblank_post_modeset(dev, pipe); 1312 break; 1313 default: 1314 return -EINVAL; 1315 } 1316 1317 return 0; 1318 } 1319 1320 static inline bool vblank_passed(u64 seq, u64 ref) 1321 { 1322 return (seq - ref) <= (1 << 23); 1323 } 1324 1325 static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe, 1326 u64 req_seq, 1327 union drm_wait_vblank *vblwait, 1328 struct drm_file *file_priv) 1329 { 1330 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1331 struct drm_pending_vblank_event *e; 1332 ktime_t now; 1333 unsigned long flags; 1334 u64 seq; 1335 int ret; 1336 1337 e = kzalloc(sizeof(*e), GFP_KERNEL); 1338 if (e == NULL) { 1339 ret = -ENOMEM; 1340 goto err_put; 1341 } 1342 1343 e->pipe = pipe; 1344 e->event.base.type = DRM_EVENT_VBLANK; 1345 e->event.base.length = sizeof(e->event.vbl); 1346 e->event.vbl.user_data = vblwait->request.signal; 1347 e->event.vbl.crtc_id = 0; 1348 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 1349 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe); 1350 if (crtc) 1351 e->event.vbl.crtc_id = crtc->base.id; 1352 } 1353 1354 spin_lock_irqsave(&dev->event_lock, flags); 1355 1356 /* 1357 * drm_crtc_vblank_off() might have been called after we called 1358 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the 1359 * vblank disable, so no need for further locking. The reference from 1360 * drm_vblank_get() protects against vblank disable from another source. 1361 */ 1362 if (!READ_ONCE(vblank->enabled)) { 1363 ret = -EINVAL; 1364 goto err_unlock; 1365 } 1366 1367 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base, 1368 &e->event.base); 1369 1370 if (ret) 1371 goto err_unlock; 1372 1373 seq = drm_vblank_count_and_time(dev, pipe, &now); 1374 1375 DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n", 1376 req_seq, seq, pipe); 1377 1378 trace_drm_vblank_event_queued(file_priv, pipe, req_seq); 1379 1380 e->sequence = req_seq; 1381 if (vblank_passed(seq, req_seq)) { 1382 drm_vblank_put(dev, pipe); 1383 send_vblank_event(dev, e, seq, now); 1384 vblwait->reply.sequence = seq; 1385 } else { 1386 /* drm_handle_vblank_events will call drm_vblank_put */ 1387 list_add_tail(&e->base.link, &dev->vblank_event_list); 1388 vblwait->reply.sequence = req_seq; 1389 } 1390 1391 spin_unlock_irqrestore(&dev->event_lock, flags); 1392 1393 return 0; 1394 1395 err_unlock: 1396 spin_unlock_irqrestore(&dev->event_lock, flags); 1397 kfree(e); 1398 err_put: 1399 drm_vblank_put(dev, pipe); 1400 return ret; 1401 } 1402 1403 static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait) 1404 { 1405 if (vblwait->request.sequence) 1406 return false; 1407 1408 return _DRM_VBLANK_RELATIVE == 1409 (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK | 1410 _DRM_VBLANK_EVENT | 1411 _DRM_VBLANK_NEXTONMISS)); 1412 } 1413 1414 /* 1415 * Widen a 32-bit param to 64-bits. 1416 * 1417 * \param narrow 32-bit value (missing upper 32 bits) 1418 * \param near 64-bit value that should be 'close' to near 1419 * 1420 * This function returns a 64-bit value using the lower 32-bits from 1421 * 'narrow' and constructing the upper 32-bits so that the result is 1422 * as close as possible to 'near'. 1423 */ 1424 1425 static u64 widen_32_to_64(u32 narrow, u64 near) 1426 { 1427 return near + (s32) (narrow - near); 1428 } 1429 1430 static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe, 1431 struct drm_wait_vblank_reply *reply) 1432 { 1433 ktime_t now; 1434 struct timespec64 ts; 1435 1436 /* 1437 * drm_wait_vblank_reply is a UAPI structure that uses 'long' 1438 * to store the seconds. This is safe as we always use monotonic 1439 * timestamps since linux-4.15. 1440 */ 1441 reply->sequence = drm_vblank_count_and_time(dev, pipe, &now); 1442 ts = ktime_to_timespec64(now); 1443 reply->tval_sec = (u32)ts.tv_sec; 1444 reply->tval_usec = ts.tv_nsec / 1000; 1445 } 1446 1447 int drm_wait_vblank_ioctl(struct drm_device *dev, void *data, 1448 struct drm_file *file_priv) 1449 { 1450 struct drm_crtc *crtc; 1451 struct drm_vblank_crtc *vblank; 1452 union drm_wait_vblank *vblwait = data; 1453 int ret; 1454 u64 req_seq, seq; 1455 unsigned int pipe_index; 1456 unsigned int flags, pipe, high_pipe; 1457 1458 if (!dev->irq_enabled) 1459 return -EINVAL; 1460 1461 if (vblwait->request.type & _DRM_VBLANK_SIGNAL) 1462 return -EINVAL; 1463 1464 if (vblwait->request.type & 1465 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | 1466 _DRM_VBLANK_HIGH_CRTC_MASK)) { 1467 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", 1468 vblwait->request.type, 1469 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | 1470 _DRM_VBLANK_HIGH_CRTC_MASK)); 1471 return -EINVAL; 1472 } 1473 1474 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; 1475 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK); 1476 if (high_pipe) 1477 pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT; 1478 else 1479 pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0; 1480 1481 /* Convert lease-relative crtc index into global crtc index */ 1482 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 1483 pipe = 0; 1484 drm_for_each_crtc(crtc, dev) { 1485 if (drm_lease_held(file_priv, crtc->base.id)) { 1486 if (pipe_index == 0) 1487 break; 1488 pipe_index--; 1489 } 1490 pipe++; 1491 } 1492 } else { 1493 pipe = pipe_index; 1494 } 1495 1496 if (pipe >= dev->num_crtcs) 1497 return -EINVAL; 1498 1499 vblank = &dev->vblank[pipe]; 1500 1501 /* If the counter is currently enabled and accurate, short-circuit 1502 * queries to return the cached timestamp of the last vblank. 1503 */ 1504 if (dev->vblank_disable_immediate && 1505 drm_wait_vblank_is_query(vblwait) && 1506 READ_ONCE(vblank->enabled)) { 1507 drm_wait_vblank_reply(dev, pipe, &vblwait->reply); 1508 return 0; 1509 } 1510 1511 ret = drm_vblank_get(dev, pipe); 1512 if (ret) { 1513 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret); 1514 return ret; 1515 } 1516 seq = drm_vblank_count(dev, pipe); 1517 1518 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { 1519 case _DRM_VBLANK_RELATIVE: 1520 req_seq = seq + vblwait->request.sequence; 1521 vblwait->request.sequence = req_seq; 1522 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; 1523 break; 1524 case _DRM_VBLANK_ABSOLUTE: 1525 req_seq = widen_32_to_64(vblwait->request.sequence, seq); 1526 break; 1527 default: 1528 ret = -EINVAL; 1529 goto done; 1530 } 1531 1532 if ((flags & _DRM_VBLANK_NEXTONMISS) && 1533 vblank_passed(seq, req_seq)) { 1534 req_seq = seq + 1; 1535 vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS; 1536 vblwait->request.sequence = req_seq; 1537 } 1538 1539 if (flags & _DRM_VBLANK_EVENT) { 1540 /* must hold on to the vblank ref until the event fires 1541 * drm_vblank_put will be called asynchronously 1542 */ 1543 return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv); 1544 } 1545 1546 if (req_seq != seq) { 1547 DRM_DEBUG("waiting on vblank count %llu, crtc %u\n", 1548 req_seq, pipe); 1549 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ, 1550 vblank_passed(drm_vblank_count(dev, pipe), 1551 req_seq) || 1552 !READ_ONCE(vblank->enabled)); 1553 } 1554 1555 if (ret != -EINTR) { 1556 drm_wait_vblank_reply(dev, pipe, &vblwait->reply); 1557 1558 DRM_DEBUG("crtc %d returning %u to client\n", 1559 pipe, vblwait->reply.sequence); 1560 } else { 1561 DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe); 1562 } 1563 1564 done: 1565 drm_vblank_put(dev, pipe); 1566 return ret; 1567 } 1568 1569 static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe) 1570 { 1571 struct drm_pending_vblank_event *e, *t; 1572 ktime_t now; 1573 u64 seq; 1574 1575 assert_spin_locked(&dev->event_lock); 1576 1577 seq = drm_vblank_count_and_time(dev, pipe, &now); 1578 1579 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { 1580 if (e->pipe != pipe) 1581 continue; 1582 if (!vblank_passed(seq, e->sequence)) 1583 continue; 1584 1585 DRM_DEBUG("vblank event on %llu, current %llu\n", 1586 e->sequence, seq); 1587 1588 list_del(&e->base.link); 1589 drm_vblank_put(dev, pipe); 1590 send_vblank_event(dev, e, seq, now); 1591 } 1592 1593 trace_drm_vblank_event(pipe, seq); 1594 } 1595 1596 /** 1597 * drm_handle_vblank - handle a vblank event 1598 * @dev: DRM device 1599 * @pipe: index of CRTC where this event occurred 1600 * 1601 * Drivers should call this routine in their vblank interrupt handlers to 1602 * update the vblank counter and send any signals that may be pending. 1603 * 1604 * This is the legacy version of drm_crtc_handle_vblank(). 1605 */ 1606 bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe) 1607 { 1608 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1609 unsigned long irqflags; 1610 bool disable_irq; 1611 1612 if (WARN_ON_ONCE(!dev->num_crtcs)) 1613 return false; 1614 1615 if (WARN_ON(pipe >= dev->num_crtcs)) 1616 return false; 1617 1618 spin_lock_irqsave(&dev->event_lock, irqflags); 1619 1620 /* Need timestamp lock to prevent concurrent execution with 1621 * vblank enable/disable, as this would cause inconsistent 1622 * or corrupted timestamps and vblank counts. 1623 */ 1624 spin_lock(&dev->vblank_time_lock); 1625 1626 /* Vblank irq handling disabled. Nothing to do. */ 1627 if (!vblank->enabled) { 1628 spin_unlock(&dev->vblank_time_lock); 1629 spin_unlock_irqrestore(&dev->event_lock, irqflags); 1630 return false; 1631 } 1632 1633 drm_update_vblank_count(dev, pipe, true); 1634 1635 spin_unlock(&dev->vblank_time_lock); 1636 1637 wake_up(&vblank->queue); 1638 1639 /* With instant-off, we defer disabling the interrupt until after 1640 * we finish processing the following vblank after all events have 1641 * been signaled. The disable has to be last (after 1642 * drm_handle_vblank_events) so that the timestamp is always accurate. 1643 */ 1644 disable_irq = (dev->vblank_disable_immediate && 1645 drm_vblank_offdelay > 0 && 1646 !atomic_read(&vblank->refcount)); 1647 1648 drm_handle_vblank_events(dev, pipe); 1649 1650 spin_unlock_irqrestore(&dev->event_lock, irqflags); 1651 1652 if (disable_irq) 1653 vblank_disable_fn((unsigned long)vblank); 1654 1655 return true; 1656 } 1657 EXPORT_SYMBOL(drm_handle_vblank); 1658 1659 /** 1660 * drm_crtc_handle_vblank - handle a vblank event 1661 * @crtc: where this event occurred 1662 * 1663 * Drivers should call this routine in their vblank interrupt handlers to 1664 * update the vblank counter and send any signals that may be pending. 1665 * 1666 * This is the native KMS version of drm_handle_vblank(). 1667 * 1668 * Returns: 1669 * True if the event was successfully handled, false on failure. 1670 */ 1671 bool drm_crtc_handle_vblank(struct drm_crtc *crtc) 1672 { 1673 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc)); 1674 } 1675 EXPORT_SYMBOL(drm_crtc_handle_vblank); 1676 1677 /* 1678 * Get crtc VBLANK count. 1679 * 1680 * \param dev DRM device 1681 * \param data user arguement, pointing to a drm_crtc_get_sequence structure. 1682 * \param file_priv drm file private for the user's open file descriptor 1683 */ 1684 1685 int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data, 1686 struct drm_file *file_priv) 1687 { 1688 struct drm_crtc *crtc; 1689 struct drm_vblank_crtc *vblank; 1690 int pipe; 1691 struct drm_crtc_get_sequence *get_seq = data; 1692 ktime_t now; 1693 bool vblank_enabled; 1694 int ret; 1695 1696 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1697 return -EINVAL; 1698 1699 if (!dev->irq_enabled) 1700 return -EINVAL; 1701 1702 crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id); 1703 if (!crtc) 1704 return -ENOENT; 1705 1706 pipe = drm_crtc_index(crtc); 1707 1708 vblank = &dev->vblank[pipe]; 1709 vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled); 1710 1711 if (!vblank_enabled) { 1712 ret = drm_crtc_vblank_get(crtc); 1713 if (ret) { 1714 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret); 1715 return ret; 1716 } 1717 } 1718 drm_modeset_lock(&crtc->mutex, NULL); 1719 if (crtc->state) 1720 get_seq->active = crtc->state->enable; 1721 else 1722 get_seq->active = crtc->enabled; 1723 drm_modeset_unlock(&crtc->mutex); 1724 get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now); 1725 get_seq->sequence_ns = ktime_to_ns(now); 1726 if (!vblank_enabled) 1727 drm_crtc_vblank_put(crtc); 1728 return 0; 1729 } 1730 1731 /* 1732 * Queue a event for VBLANK sequence 1733 * 1734 * \param dev DRM device 1735 * \param data user arguement, pointing to a drm_crtc_queue_sequence structure. 1736 * \param file_priv drm file private for the user's open file descriptor 1737 */ 1738 1739 int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data, 1740 struct drm_file *file_priv) 1741 { 1742 struct drm_crtc *crtc; 1743 struct drm_vblank_crtc *vblank; 1744 int pipe; 1745 struct drm_crtc_queue_sequence *queue_seq = data; 1746 ktime_t now; 1747 struct drm_pending_vblank_event *e; 1748 u32 flags; 1749 u64 seq; 1750 u64 req_seq; 1751 int ret; 1752 unsigned long spin_flags; 1753 1754 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1755 return -EINVAL; 1756 1757 if (!dev->irq_enabled) 1758 return -EINVAL; 1759 1760 crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id); 1761 if (!crtc) 1762 return -ENOENT; 1763 1764 flags = queue_seq->flags; 1765 /* Check valid flag bits */ 1766 if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE| 1767 DRM_CRTC_SEQUENCE_NEXT_ON_MISS)) 1768 return -EINVAL; 1769 1770 pipe = drm_crtc_index(crtc); 1771 1772 vblank = &dev->vblank[pipe]; 1773 1774 e = kzalloc(sizeof(*e), GFP_KERNEL); 1775 if (e == NULL) 1776 return -ENOMEM; 1777 1778 ret = drm_crtc_vblank_get(crtc); 1779 if (ret) { 1780 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret); 1781 goto err_free; 1782 } 1783 1784 seq = drm_vblank_count_and_time(dev, pipe, &now); 1785 req_seq = queue_seq->sequence; 1786 1787 if (flags & DRM_CRTC_SEQUENCE_RELATIVE) 1788 req_seq += seq; 1789 1790 if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && vblank_passed(seq, req_seq)) 1791 req_seq = seq + 1; 1792 1793 e->pipe = pipe; 1794 e->event.base.type = DRM_EVENT_CRTC_SEQUENCE; 1795 e->event.base.length = sizeof(e->event.seq); 1796 e->event.seq.user_data = queue_seq->user_data; 1797 1798 spin_lock_irqsave(&dev->event_lock, spin_flags); 1799 1800 /* 1801 * drm_crtc_vblank_off() might have been called after we called 1802 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the 1803 * vblank disable, so no need for further locking. The reference from 1804 * drm_crtc_vblank_get() protects against vblank disable from another source. 1805 */ 1806 if (!READ_ONCE(vblank->enabled)) { 1807 ret = -EINVAL; 1808 goto err_unlock; 1809 } 1810 1811 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base, 1812 &e->event.base); 1813 1814 if (ret) 1815 goto err_unlock; 1816 1817 e->sequence = req_seq; 1818 1819 if (vblank_passed(seq, req_seq)) { 1820 drm_crtc_vblank_put(crtc); 1821 send_vblank_event(dev, e, seq, now); 1822 queue_seq->sequence = seq; 1823 } else { 1824 /* drm_handle_vblank_events will call drm_vblank_put */ 1825 list_add_tail(&e->base.link, &dev->vblank_event_list); 1826 queue_seq->sequence = req_seq; 1827 } 1828 1829 spin_unlock_irqrestore(&dev->event_lock, spin_flags); 1830 return 0; 1831 1832 err_unlock: 1833 spin_unlock_irqrestore(&dev->event_lock, spin_flags); 1834 drm_crtc_vblank_put(crtc); 1835 err_free: 1836 kfree(e); 1837 return ret; 1838 } 1839