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