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