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 8 /* 9 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com 10 * 11 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. 12 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 13 * All Rights Reserved. 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining a 16 * copy of this software and associated documentation files (the "Software"), 17 * to deal in the Software without restriction, including without limitation 18 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 19 * and/or sell copies of the Software, and to permit persons to whom the 20 * Software is furnished to do so, subject to the following conditions: 21 * 22 * The above copyright notice and this permission notice (including the next 23 * paragraph) shall be included in all copies or substantial portions of the 24 * Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 29 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 30 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 32 * OTHER DEALINGS IN THE SOFTWARE. 33 */ 34 35 #include <drm/drmP.h> 36 #include "drm_internal.h" 37 38 #include <linux/slab.h> 39 40 #include <linux/export.h> 41 42 /* Access macro for slots in vblank timestamp ringbuffer. */ 43 #define vblanktimestamp(dev, crtc, count) \ 44 ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE]) 45 46 /* Retry timestamp calculation up to 3 times to satisfy 47 * drm_timestamp_precision before giving up. 48 */ 49 #define DRM_TIMESTAMP_MAXRETRIES 3 50 51 /* Threshold in nanoseconds for detection of redundant 52 * vblank irq in drm_handle_vblank(). 1 msec should be ok. 53 */ 54 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000 55 56 static bool 57 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc, 58 struct timeval *tvblank, unsigned flags); 59 60 /** 61 * drm_update_vblank_count - update the master vblank counter 62 * @dev: DRM device 63 * @crtc: counter to update 64 * 65 * Call back into the driver to update the appropriate vblank counter 66 * (specified by @crtc). Deal with wraparound, if it occurred, and 67 * update the last read value so we can deal with wraparound on the next 68 * call if necessary. 69 * 70 * Only necessary when going from off->on, to account for frames we 71 * didn't get an interrupt for. 72 * 73 * Note: caller must hold dev->vbl_lock since this reads & writes 74 * device vblank fields. 75 */ 76 static void drm_update_vblank_count(struct drm_device *dev, int crtc) 77 { 78 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 79 u32 cur_vblank, diff, tslot; 80 bool rc; 81 struct timeval t_vblank; 82 83 /* 84 * Interrupts were disabled prior to this call, so deal with counter 85 * wrap if needed. 86 * NOTE! It's possible we lost a full dev->max_vblank_count events 87 * here if the register is small or we had vblank interrupts off for 88 * a long time. 89 * 90 * We repeat the hardware vblank counter & timestamp query until 91 * we get consistent results. This to prevent races between gpu 92 * updating its hardware counter while we are retrieving the 93 * corresponding vblank timestamp. 94 */ 95 do { 96 cur_vblank = dev->driver->get_vblank_counter(dev, crtc); 97 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0); 98 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc)); 99 100 /* Deal with counter wrap */ 101 diff = cur_vblank - vblank->last; 102 if (cur_vblank < vblank->last) { 103 diff += dev->max_vblank_count; 104 105 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n", 106 crtc, vblank->last, cur_vblank, diff); 107 } 108 109 DRM_DEBUG("updating vblank count on crtc %d, missed %d\n", 110 crtc, diff); 111 112 if (diff == 0) 113 return; 114 115 /* Reinitialize corresponding vblank timestamp if high-precision query 116 * available. Skip this step if query unsupported or failed. Will 117 * reinitialize delayed at next vblank interrupt in that case. 118 */ 119 if (rc) { 120 tslot = atomic_read(&vblank->count) + diff; 121 vblanktimestamp(dev, crtc, tslot) = t_vblank; 122 } 123 124 smp_mb__before_atomic(); 125 atomic_add(diff, &vblank->count); 126 smp_mb__after_atomic(); 127 } 128 129 /* 130 * Disable vblank irq's on crtc, make sure that last vblank count 131 * of hardware and corresponding consistent software vblank counter 132 * are preserved, even if there are any spurious vblank irq's after 133 * disable. 134 */ 135 static void vblank_disable_and_save(struct drm_device *dev, int crtc) 136 { 137 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 138 u32 vblcount; 139 s64 diff_ns; 140 bool vblrc; 141 struct timeval tvblank; 142 int count = DRM_TIMESTAMP_MAXRETRIES; 143 144 /* Prevent vblank irq processing while disabling vblank irqs, 145 * so no updates of timestamps or count can happen after we've 146 * disabled. Needed to prevent races in case of delayed irq's. 147 */ 148 lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE); 149 150 /* 151 * If the vblank interrupt was already disabled update the count 152 * and timestamp to maintain the appearance that the counter 153 * has been ticking all along until this time. This makes the 154 * count account for the entire time between drm_vblank_on() and 155 * drm_vblank_off(). 156 * 157 * But only do this if precise vblank timestamps are available. 158 * Otherwise we might read a totally bogus timestamp since drivers 159 * lacking precise timestamp support rely upon sampling the system clock 160 * at vblank interrupt time. Which obviously won't work out well if the 161 * vblank interrupt is disabled. 162 */ 163 if (!vblank->enabled && 164 drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0)) { 165 drm_update_vblank_count(dev, crtc); 166 lockmgr(&dev->vblank_time_lock, LK_RELEASE); 167 return; 168 } 169 170 dev->driver->disable_vblank(dev, crtc); 171 vblank->enabled = false; 172 173 /* No further vblank irq's will be processed after 174 * this point. Get current hardware vblank count and 175 * vblank timestamp, repeat until they are consistent. 176 * 177 * FIXME: There is still a race condition here and in 178 * drm_update_vblank_count() which can cause off-by-one 179 * reinitialization of software vblank counter. If gpu 180 * vblank counter doesn't increment exactly at the leading 181 * edge of a vblank interval, then we can lose 1 count if 182 * we happen to execute between start of vblank and the 183 * delayed gpu counter increment. 184 */ 185 do { 186 vblank->last = dev->driver->get_vblank_counter(dev, crtc); 187 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0); 188 } while (vblank->last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc); 189 190 if (!count) 191 vblrc = 0; 192 193 /* Compute time difference to stored timestamp of last vblank 194 * as updated by last invocation of drm_handle_vblank() in vblank irq. 195 */ 196 vblcount = atomic_read(&vblank->count); 197 diff_ns = timeval_to_ns(&tvblank) - 198 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount)); 199 200 /* If there is at least 1 msec difference between the last stored 201 * timestamp and tvblank, then we are currently executing our 202 * disable inside a new vblank interval, the tvblank timestamp 203 * corresponds to this new vblank interval and the irq handler 204 * for this vblank didn't run yet and won't run due to our disable. 205 * Therefore we need to do the job of drm_handle_vblank() and 206 * increment the vblank counter by one to account for this vblank. 207 * 208 * Skip this step if there isn't any high precision timestamp 209 * available. In that case we can't account for this and just 210 * hope for the best. 211 */ 212 if (vblrc && (abs64(diff_ns) > 1000000)) { 213 /* Store new timestamp in ringbuffer. */ 214 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank; 215 216 /* Increment cooked vblank count. This also atomically commits 217 * the timestamp computed above. 218 */ 219 smp_mb__before_atomic(); 220 atomic_inc(&vblank->count); 221 smp_mb__after_atomic(); 222 } 223 224 lockmgr(&dev->vblank_time_lock, LK_RELEASE); 225 } 226 227 static void vblank_disable_fn(unsigned long arg) 228 { 229 struct drm_vblank_crtc *vblank = (void *)arg; 230 struct drm_device *dev = vblank->dev; 231 int crtc = vblank->crtc; 232 233 if (!dev->vblank_disable_allowed) 234 return; 235 236 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE); 237 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) { 238 DRM_DEBUG("disabling vblank on crtc %d\n", crtc); 239 vblank_disable_and_save(dev, crtc); 240 } 241 lockmgr(&dev->vbl_lock, LK_RELEASE); 242 } 243 244 /** 245 * drm_vblank_cleanup - cleanup vblank support 246 * @dev: DRM device 247 * 248 * This function cleans up any resources allocated in drm_vblank_init. 249 */ 250 void drm_vblank_cleanup(struct drm_device *dev) 251 { 252 int crtc; 253 254 /* Bail if the driver didn't call drm_vblank_init() */ 255 if (dev->num_crtcs == 0) 256 return; 257 258 for (crtc = 0; crtc < dev->num_crtcs; crtc++) { 259 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 260 261 del_timer_sync(&vblank->disable_timer); 262 263 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE); 264 vblank_disable_and_save(dev, crtc); 265 lockmgr(&dev->vbl_lock, LK_RELEASE); 266 } 267 268 kfree(dev->vblank); 269 270 dev->num_crtcs = 0; 271 } 272 EXPORT_SYMBOL(drm_vblank_cleanup); 273 274 /** 275 * drm_vblank_init - initialize vblank support 276 * @dev: drm_device 277 * @num_crtcs: number of crtcs supported by @dev 278 * 279 * This function initializes vblank support for @num_crtcs display pipelines. 280 * 281 * Returns: 282 * Zero on success or a negative error code on failure. 283 */ 284 int drm_vblank_init(struct drm_device *dev, int num_crtcs) 285 { 286 int i, ret = -ENOMEM; 287 288 lockinit(&dev->vbl_lock, "drmvbl", 0, LK_CANRECURSE); 289 lockinit(&dev->vblank_time_lock, "drmvtl", 0, LK_CANRECURSE); 290 291 dev->num_crtcs = num_crtcs; 292 293 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL); 294 if (!dev->vblank) 295 goto err; 296 297 for (i = 0; i < num_crtcs; i++) { 298 struct drm_vblank_crtc *vblank = &dev->vblank[i]; 299 300 vblank->dev = dev; 301 vblank->crtc = i; 302 init_waitqueue_head(&vblank->queue); 303 setup_timer(&vblank->disable_timer, vblank_disable_fn, 304 (unsigned long)vblank); 305 } 306 307 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n"); 308 309 /* Driver specific high-precision vblank timestamping supported? */ 310 if (dev->driver->get_vblank_timestamp) 311 DRM_INFO("Driver supports precise vblank timestamp query.\n"); 312 else 313 DRM_INFO("No driver support for vblank timestamp query.\n"); 314 315 dev->vblank_disable_allowed = false; 316 317 return 0; 318 319 err: 320 dev->num_crtcs = 0; 321 return ret; 322 } 323 EXPORT_SYMBOL(drm_vblank_init); 324 325 #if 0 326 static void drm_irq_vgaarb_nokms(void *cookie, bool state) 327 { 328 struct drm_device *dev = cookie; 329 330 if (dev->driver->vgaarb_irq) { 331 dev->driver->vgaarb_irq(dev, state); 332 return; 333 } 334 335 if (!dev->irq_enabled) 336 return; 337 338 if (state) { 339 if (dev->driver->irq_uninstall) 340 dev->driver->irq_uninstall(dev); 341 } else { 342 if (dev->driver->irq_preinstall) 343 dev->driver->irq_preinstall(dev); 344 if (dev->driver->irq_postinstall) 345 dev->driver->irq_postinstall(dev); 346 } 347 } 348 #endif 349 350 /** 351 * drm_irq_install - install IRQ handler 352 * @dev: DRM device 353 * @irq: IRQ number to install the handler for 354 * 355 * Initializes the IRQ related data. Installs the handler, calling the driver 356 * irq_preinstall() and irq_postinstall() functions before and after the 357 * installation. 358 * 359 * This is the simplified helper interface provided for drivers with no special 360 * needs. Drivers which need to install interrupt handlers for multiple 361 * interrupts must instead set drm_device->irq_enabled to signal the DRM core 362 * that vblank interrupts are available. 363 * 364 * Returns: 365 * Zero on success or a negative error code on failure. 366 */ 367 int drm_irq_install(struct drm_device *dev, int irq) 368 { 369 int ret; 370 371 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) 372 return -EINVAL; 373 374 if (irq == 0) 375 return -EINVAL; 376 377 /* Driver must have been initialized */ 378 if (!dev->dev_private) 379 return -EINVAL; 380 381 if (dev->irq_enabled) 382 return -EBUSY; 383 dev->irq_enabled = true; 384 385 DRM_DEBUG("irq=%d\n", irq); 386 387 /* Before installing handler */ 388 if (dev->driver->irq_preinstall) 389 dev->driver->irq_preinstall(dev); 390 391 /* Install handler */ 392 ret = -bus_setup_intr(dev->dev, dev->irqr, INTR_MPSAFE, 393 dev->driver->irq_handler, dev, &dev->irqh, &dev->irq_lock); 394 395 if (ret != 0) { 396 dev->irq_enabled = false; 397 return ret; 398 } 399 400 /* After installing handler */ 401 if (dev->driver->irq_postinstall) 402 ret = dev->driver->irq_postinstall(dev); 403 404 if (ret < 0) { 405 dev->irq_enabled = false; 406 bus_teardown_intr(dev->dev, dev->irqr, dev->irqh); 407 } else { 408 dev->irq = irq; 409 } 410 411 return ret; 412 } 413 EXPORT_SYMBOL(drm_irq_install); 414 415 /** 416 * drm_irq_uninstall - uninstall the IRQ handler 417 * @dev: DRM device 418 * 419 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler. 420 * This should only be called by drivers which used drm_irq_install() to set up 421 * their interrupt handler. Other drivers must only reset 422 * drm_device->irq_enabled to false. 423 * 424 * Note that for kernel modesetting drivers it is a bug if this function fails. 425 * The sanity checks are only to catch buggy user modesetting drivers which call 426 * the same function through an ioctl. 427 * 428 * Returns: 429 * Zero on success or a negative error code on failure. 430 */ 431 int drm_irq_uninstall(struct drm_device *dev) 432 { 433 bool irq_enabled; 434 int i; 435 436 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) 437 return -EINVAL; 438 439 irq_enabled = dev->irq_enabled; 440 dev->irq_enabled = false; 441 442 /* 443 * Wake up any waiters so they don't hang. 444 */ 445 if (dev->num_crtcs) { 446 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE); 447 for (i = 0; i < dev->num_crtcs; i++) { 448 struct drm_vblank_crtc *vblank = &dev->vblank[i]; 449 450 wake_up(&vblank->queue); 451 vblank->enabled = false; 452 vblank->last = 453 dev->driver->get_vblank_counter(dev, i); 454 } 455 lockmgr(&dev->vbl_lock, LK_RELEASE); 456 } 457 458 if (!irq_enabled) 459 return -EINVAL; 460 461 DRM_DEBUG("irq=%d\n", dev->irq); 462 463 if (dev->driver->irq_uninstall) 464 dev->driver->irq_uninstall(dev); 465 466 bus_teardown_intr(dev->dev, dev->irqr, dev->irqh); 467 468 return 0; 469 } 470 EXPORT_SYMBOL(drm_irq_uninstall); 471 472 /* 473 * IRQ control ioctl. 474 * 475 * \param inode device inode. 476 * \param file_priv DRM file private. 477 * \param cmd command. 478 * \param arg user argument, pointing to a drm_control structure. 479 * \return zero on success or a negative number on failure. 480 * 481 * Calls irq_install() or irq_uninstall() according to \p arg. 482 */ 483 int drm_control(struct drm_device *dev, void *data, 484 struct drm_file *file_priv) 485 { 486 struct drm_control *ctl = data; 487 int ret = 0, irq; 488 489 /* if we haven't irq we fallback for compatibility reasons - 490 * this used to be a separate function in drm_dma.h 491 */ 492 493 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) 494 return 0; 495 if (drm_core_check_feature(dev, DRIVER_MODESET)) 496 return 0; 497 /* UMS was only ever support on pci devices. */ 498 if (WARN_ON(!dev->pdev)) 499 return -EINVAL; 500 501 switch (ctl->func) { 502 case DRM_INST_HANDLER: 503 irq = dev->irq; 504 505 if (dev->if_version < DRM_IF_VERSION(1, 2) && 506 ctl->irq != irq) 507 return -EINVAL; 508 mutex_lock(&dev->struct_mutex); 509 ret = drm_irq_install(dev, irq); 510 mutex_unlock(&dev->struct_mutex); 511 512 return ret; 513 case DRM_UNINST_HANDLER: 514 mutex_lock(&dev->struct_mutex); 515 ret = drm_irq_uninstall(dev); 516 mutex_unlock(&dev->struct_mutex); 517 518 return ret; 519 default: 520 return -EINVAL; 521 } 522 } 523 524 /** 525 * drm_calc_timestamping_constants - calculate vblank timestamp constants 526 * @crtc: drm_crtc whose timestamp constants should be updated. 527 * @mode: display mode containing the scanout timings 528 * 529 * Calculate and store various constants which are later 530 * needed by vblank and swap-completion timestamping, e.g, 531 * by drm_calc_vbltimestamp_from_scanoutpos(). They are 532 * derived from CRTC's true scanout timing, so they take 533 * things like panel scaling or other adjustments into account. 534 */ 535 void drm_calc_timestamping_constants(struct drm_crtc *crtc, 536 const struct drm_display_mode *mode) 537 { 538 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0; 539 int dotclock = mode->crtc_clock; 540 541 /* Valid dotclock? */ 542 if (dotclock > 0) { 543 int frame_size = mode->crtc_htotal * mode->crtc_vtotal; 544 545 /* 546 * Convert scanline length in pixels and video 547 * dot clock to line duration, frame duration 548 * and pixel duration in nanoseconds: 549 */ 550 pixeldur_ns = 1000000 / dotclock; 551 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock); 552 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock); 553 554 /* 555 * Fields of interlaced scanout modes are only half a frame duration. 556 */ 557 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 558 framedur_ns /= 2; 559 } else 560 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n", 561 crtc->base.id); 562 563 crtc->pixeldur_ns = pixeldur_ns; 564 crtc->linedur_ns = linedur_ns; 565 crtc->framedur_ns = framedur_ns; 566 567 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n", 568 crtc->base.id, mode->crtc_htotal, 569 mode->crtc_vtotal, mode->crtc_vdisplay); 570 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n", 571 crtc->base.id, dotclock, framedur_ns, 572 linedur_ns, pixeldur_ns); 573 } 574 EXPORT_SYMBOL(drm_calc_timestamping_constants); 575 576 /** 577 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper 578 * @dev: DRM device 579 * @crtc: Which CRTC's vblank timestamp to retrieve 580 * @max_error: Desired maximum allowable error in timestamps (nanosecs) 581 * On return contains true maximum error of timestamp 582 * @vblank_time: Pointer to struct timeval which should receive the timestamp 583 * @flags: Flags to pass to driver: 584 * 0 = Default, 585 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler 586 * @refcrtc: CRTC which defines scanout timing 587 * @mode: mode which defines the scanout timings 588 * 589 * Implements calculation of exact vblank timestamps from given drm_display_mode 590 * timings and current video scanout position of a CRTC. This can be called from 591 * within get_vblank_timestamp() implementation of a kms driver to implement the 592 * actual timestamping. 593 * 594 * Should return timestamps conforming to the OML_sync_control OpenML 595 * extension specification. The timestamp corresponds to the end of 596 * the vblank interval, aka start of scanout of topmost-leftmost display 597 * pixel in the following video frame. 598 * 599 * Requires support for optional dev->driver->get_scanout_position() 600 * in kms driver, plus a bit of setup code to provide a drm_display_mode 601 * that corresponds to the true scanout timing. 602 * 603 * The current implementation only handles standard video modes. It 604 * returns as no operation if a doublescan or interlaced video mode is 605 * active. Higher level code is expected to handle this. 606 * 607 * Returns: 608 * Negative value on error, failure or if not supported in current 609 * video mode: 610 * 611 * -EINVAL - Invalid CRTC. 612 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset. 613 * -ENOTSUPP - Function not supported in current display mode. 614 * -EIO - Failed, e.g., due to failed scanout position query. 615 * 616 * Returns or'ed positive status flags on success: 617 * 618 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping. 619 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval. 620 * 621 */ 622 int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc, 623 int *max_error, 624 struct timeval *vblank_time, 625 unsigned flags, 626 const struct drm_crtc *refcrtc, 627 const struct drm_display_mode *mode) 628 { 629 ktime_t stime, etime; 630 struct timeval tv_etime; 631 int vbl_status; 632 int vpos, hpos, i; 633 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns; 634 bool invbl; 635 636 if (crtc < 0 || crtc >= dev->num_crtcs) { 637 DRM_ERROR("Invalid crtc %d\n", crtc); 638 return -EINVAL; 639 } 640 641 /* Scanout position query not supported? Should not happen. */ 642 if (!dev->driver->get_scanout_position) { 643 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n"); 644 return -EIO; 645 } 646 647 /* Durations of frames, lines, pixels in nanoseconds. */ 648 framedur_ns = refcrtc->framedur_ns; 649 linedur_ns = refcrtc->linedur_ns; 650 pixeldur_ns = refcrtc->pixeldur_ns; 651 652 /* If mode timing undefined, just return as no-op: 653 * Happens during initial modesetting of a crtc. 654 */ 655 if (framedur_ns == 0) { 656 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc); 657 return -EAGAIN; 658 } 659 660 /* Get current scanout position with system timestamp. 661 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times 662 * if single query takes longer than max_error nanoseconds. 663 * 664 * This guarantees a tight bound on maximum error if 665 * code gets preempted or delayed for some reason. 666 */ 667 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) { 668 /* 669 * Get vertical and horizontal scanout position vpos, hpos, 670 * and bounding timestamps stime, etime, pre/post query. 671 */ 672 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos, 673 &hpos, &stime, &etime); 674 675 /* 676 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if 677 * CLOCK_REALTIME is requested. 678 */ 679 #if 0 680 if (!drm_timestamp_monotonic) 681 mono_time_offset = ktime_get_monotonic_offset(); 682 #endif 683 684 /* Return as no-op if scanout query unsupported or failed. */ 685 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) { 686 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n", 687 crtc, vbl_status); 688 return -EIO; 689 } 690 691 /* Compute uncertainty in timestamp of scanout position query. */ 692 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime); 693 694 /* Accept result with < max_error nsecs timing uncertainty. */ 695 if (duration_ns <= *max_error) 696 break; 697 } 698 699 /* Noisy system timing? */ 700 if (i == DRM_TIMESTAMP_MAXRETRIES) { 701 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n", 702 crtc, duration_ns/1000, *max_error/1000, i); 703 } 704 705 /* Return upper bound of timestamp precision error. */ 706 *max_error = duration_ns; 707 708 /* Check if in vblank area: 709 * vpos is >=0 in video scanout area, but negative 710 * within vblank area, counting down the number of lines until 711 * start of scanout. 712 */ 713 invbl = vbl_status & DRM_SCANOUTPOS_IN_VBLANK; 714 715 /* Convert scanout position into elapsed time at raw_time query 716 * since start of scanout at first display scanline. delta_ns 717 * can be negative if start of scanout hasn't happened yet. 718 */ 719 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns; 720 721 #if 0 722 if (!drm_timestamp_monotonic) 723 etime = ktime_sub(etime, mono_time_offset); 724 #endif 725 726 /* save this only for debugging purposes */ 727 tv_etime = ktime_to_timeval(etime); 728 /* Subtract time delta from raw timestamp to get final 729 * vblank_time timestamp for end of vblank. 730 */ 731 if (delta_ns < 0) 732 etime = ktime_add_ns(etime, -delta_ns); 733 else 734 etime = ktime_sub_ns(etime, delta_ns); 735 *vblank_time = ktime_to_timeval(etime); 736 737 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n", 738 crtc, (int)vbl_status, hpos, vpos, 739 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec, 740 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec, 741 duration_ns/1000, i); 742 743 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD; 744 if (invbl) 745 vbl_status |= DRM_VBLANKTIME_IN_VBLANK; 746 747 return vbl_status; 748 } 749 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos); 750 751 static struct timeval get_drm_timestamp(void) 752 { 753 ktime_t now; 754 755 now = ktime_get(); 756 #if 0 757 if (!drm_timestamp_monotonic) 758 now = ktime_sub(now, ktime_get_monotonic_offset()); 759 #endif 760 761 return ktime_to_timeval(now); 762 } 763 764 /** 765 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent 766 * vblank interval 767 * @dev: DRM device 768 * @crtc: which CRTC's vblank timestamp to retrieve 769 * @tvblank: Pointer to target struct timeval which should receive the timestamp 770 * @flags: Flags to pass to driver: 771 * 0 = Default, 772 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler 773 * 774 * Fetches the system timestamp corresponding to the time of the most recent 775 * vblank interval on specified CRTC. May call into kms-driver to 776 * compute the timestamp with a high-precision GPU specific method. 777 * 778 * Returns zero if timestamp originates from uncorrected do_gettimeofday() 779 * call, i.e., it isn't very precisely locked to the true vblank. 780 * 781 * Returns: 782 * True if timestamp is considered to be very precise, false otherwise. 783 */ 784 static bool 785 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc, 786 struct timeval *tvblank, unsigned flags) 787 { 788 int ret; 789 790 /* Define requested maximum error on timestamps (nanoseconds). */ 791 int max_error = (int) drm_timestamp_precision * 1000; 792 793 /* Query driver if possible and precision timestamping enabled. */ 794 if (dev->driver->get_vblank_timestamp && (max_error > 0)) { 795 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error, 796 tvblank, flags); 797 if (ret > 0) 798 return true; 799 } 800 801 /* GPU high precision timestamp query unsupported or failed. 802 * Return current monotonic/gettimeofday timestamp as best estimate. 803 */ 804 *tvblank = get_drm_timestamp(); 805 806 return false; 807 } 808 809 /** 810 * drm_vblank_count - retrieve "cooked" vblank counter value 811 * @dev: DRM device 812 * @crtc: which counter to retrieve 813 * 814 * Fetches the "cooked" vblank count value that represents the number of 815 * vblank events since the system was booted, including lost events due to 816 * modesetting activity. 817 * 818 * This is the legacy version of drm_crtc_vblank_count(). 819 * 820 * Returns: 821 * The software vblank counter. 822 */ 823 u32 drm_vblank_count(struct drm_device *dev, int crtc) 824 { 825 if (WARN_ON(crtc >= dev->num_crtcs)) 826 return 0; 827 return atomic_read(&dev->vblank[crtc].count); 828 } 829 EXPORT_SYMBOL(drm_vblank_count); 830 831 /** 832 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value 833 * @crtc: which counter to retrieve 834 * 835 * Fetches the "cooked" vblank count value that represents the number of 836 * vblank events since the system was booted, including lost events due to 837 * modesetting activity. 838 * 839 * This is the native KMS version of drm_vblank_count(). 840 * 841 * Returns: 842 * The software vblank counter. 843 */ 844 u32 drm_crtc_vblank_count(struct drm_crtc *crtc) 845 { 846 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc)); 847 } 848 EXPORT_SYMBOL(drm_crtc_vblank_count); 849 850 /** 851 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value 852 * and the system timestamp corresponding to that vblank counter value. 853 * 854 * @dev: DRM device 855 * @crtc: which counter to retrieve 856 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp. 857 * 858 * Fetches the "cooked" vblank count value that represents the number of 859 * vblank events since the system was booted, including lost events due to 860 * modesetting activity. Returns corresponding system timestamp of the time 861 * of the vblank interval that corresponds to the current vblank counter value. 862 */ 863 u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc, 864 struct timeval *vblanktime) 865 { 866 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 867 u32 cur_vblank; 868 869 if (WARN_ON(crtc >= dev->num_crtcs)) 870 return 0; 871 872 /* Read timestamp from slot of _vblank_time ringbuffer 873 * that corresponds to current vblank count. Retry if 874 * count has incremented during readout. This works like 875 * a seqlock. 876 */ 877 do { 878 cur_vblank = atomic_read(&vblank->count); 879 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank); 880 smp_rmb(); 881 } while (cur_vblank != atomic_read(&vblank->count)); 882 883 return cur_vblank; 884 } 885 EXPORT_SYMBOL(drm_vblank_count_and_time); 886 887 static void send_vblank_event(struct drm_device *dev, 888 struct drm_pending_vblank_event *e, 889 unsigned long seq, struct timeval *now) 890 { 891 #if 0 892 WARN_ON_SMP(!spin_is_locked(&dev->event_lock)); 893 #endif 894 e->event.sequence = seq; 895 e->event.tv_sec = now->tv_sec; 896 e->event.tv_usec = now->tv_usec; 897 898 list_add_tail(&e->base.link, 899 &e->base.file_priv->event_list); 900 wake_up_interruptible(&e->base.file_priv->event_wait); 901 #ifdef __DragonFly__ 902 KNOTE(&e->base.file_priv->dkq.ki_note, 0); 903 #endif 904 #if 0 905 trace_drm_vblank_event_delivered(e->base.pid, e->pipe, 906 e->event.sequence); 907 #endif 908 } 909 910 /** 911 * drm_send_vblank_event - helper to send vblank event after pageflip 912 * @dev: DRM device 913 * @crtc: CRTC in question 914 * @e: the event to send 915 * 916 * Updates sequence # and timestamp on event, and sends it to userspace. 917 * Caller must hold event lock. 918 * 919 * This is the legacy version of drm_crtc_send_vblank_event(). 920 */ 921 void drm_send_vblank_event(struct drm_device *dev, int crtc, 922 struct drm_pending_vblank_event *e) 923 { 924 struct timeval now; 925 unsigned int seq; 926 if (crtc >= 0) { 927 seq = drm_vblank_count_and_time(dev, crtc, &now); 928 } else { 929 seq = 0; 930 931 now = get_drm_timestamp(); 932 } 933 e->pipe = crtc; 934 send_vblank_event(dev, e, seq, &now); 935 } 936 EXPORT_SYMBOL(drm_send_vblank_event); 937 938 /** 939 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip 940 * @crtc: the source CRTC of the vblank event 941 * @e: the event to send 942 * 943 * Updates sequence # and timestamp on event, and sends it to userspace. 944 * Caller must hold event lock. 945 * 946 * This is the native KMS version of drm_send_vblank_event(). 947 */ 948 void drm_crtc_send_vblank_event(struct drm_crtc *crtc, 949 struct drm_pending_vblank_event *e) 950 { 951 drm_send_vblank_event(crtc->dev, drm_crtc_index(crtc), e); 952 } 953 EXPORT_SYMBOL(drm_crtc_send_vblank_event); 954 955 /** 956 * drm_vblank_enable - enable the vblank interrupt on a CRTC 957 * @dev: DRM device 958 * @crtc: CRTC in question 959 */ 960 static int drm_vblank_enable(struct drm_device *dev, int crtc) 961 { 962 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 963 int ret = 0; 964 965 assert_spin_locked(&dev->vbl_lock); 966 967 lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE); 968 969 if (!vblank->enabled) { 970 /* 971 * Enable vblank irqs under vblank_time_lock protection. 972 * All vblank count & timestamp updates are held off 973 * until we are done reinitializing master counter and 974 * timestamps. Filtercode in drm_handle_vblank() will 975 * prevent double-accounting of same vblank interval. 976 */ 977 ret = dev->driver->enable_vblank(dev, crtc); 978 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret); 979 if (ret) 980 atomic_dec(&vblank->refcount); 981 else { 982 vblank->enabled = true; 983 drm_update_vblank_count(dev, crtc); 984 } 985 } 986 987 lockmgr(&dev->vblank_time_lock, LK_RELEASE); 988 989 return ret; 990 } 991 992 /** 993 * drm_vblank_get - get a reference count on vblank events 994 * @dev: DRM device 995 * @crtc: which CRTC to own 996 * 997 * Acquire a reference count on vblank events to avoid having them disabled 998 * while in use. 999 * 1000 * This is the legacy version of drm_crtc_vblank_get(). 1001 * 1002 * Returns: 1003 * Zero on success, nonzero on failure. 1004 */ 1005 int drm_vblank_get(struct drm_device *dev, int crtc) 1006 { 1007 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 1008 int ret = 0; 1009 1010 if (WARN_ON(crtc >= dev->num_crtcs)) 1011 return -EINVAL; 1012 1013 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE); 1014 /* Going from 0->1 means we have to enable interrupts again */ 1015 if (atomic_add_return(1, &vblank->refcount) == 1) { 1016 ret = drm_vblank_enable(dev, crtc); 1017 } else { 1018 if (!vblank->enabled) { 1019 atomic_dec(&vblank->refcount); 1020 ret = -EINVAL; 1021 } 1022 } 1023 lockmgr(&dev->vbl_lock, LK_RELEASE); 1024 1025 return ret; 1026 } 1027 EXPORT_SYMBOL(drm_vblank_get); 1028 1029 /** 1030 * drm_crtc_vblank_get - get a reference count on vblank events 1031 * @crtc: which CRTC to own 1032 * 1033 * Acquire a reference count on vblank events to avoid having them disabled 1034 * while in use. 1035 * 1036 * This is the native kms version of drm_vblank_off(). 1037 * 1038 * Returns: 1039 * Zero on success, nonzero on failure. 1040 */ 1041 int drm_crtc_vblank_get(struct drm_crtc *crtc) 1042 { 1043 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc)); 1044 } 1045 EXPORT_SYMBOL(drm_crtc_vblank_get); 1046 1047 /** 1048 * drm_vblank_put - give up ownership of vblank events 1049 * @dev: DRM device 1050 * @crtc: which counter to give up 1051 * 1052 * Release ownership of a given vblank counter, turning off interrupts 1053 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds. 1054 * 1055 * This is the legacy version of drm_crtc_vblank_put(). 1056 */ 1057 void drm_vblank_put(struct drm_device *dev, int crtc) 1058 { 1059 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 1060 1061 if (WARN_ON(atomic_read(&vblank->refcount) == 0)) 1062 return; 1063 1064 if (WARN_ON(crtc >= dev->num_crtcs)) 1065 return; 1066 1067 /* Last user schedules interrupt disable */ 1068 if (atomic_dec_and_test(&vblank->refcount)) { 1069 if (drm_vblank_offdelay == 0) 1070 return; 1071 else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0) 1072 vblank_disable_fn((unsigned long)vblank); 1073 else 1074 mod_timer(&vblank->disable_timer, 1075 jiffies + ((drm_vblank_offdelay * HZ)/1000)); 1076 } 1077 } 1078 EXPORT_SYMBOL(drm_vblank_put); 1079 1080 /** 1081 * drm_crtc_vblank_put - give up ownership of vblank events 1082 * @crtc: which counter to give up 1083 * 1084 * Release ownership of a given vblank counter, turning off interrupts 1085 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds. 1086 * 1087 * This is the native kms version of drm_vblank_put(). 1088 */ 1089 void drm_crtc_vblank_put(struct drm_crtc *crtc) 1090 { 1091 drm_vblank_put(crtc->dev, drm_crtc_index(crtc)); 1092 } 1093 EXPORT_SYMBOL(drm_crtc_vblank_put); 1094 1095 /** 1096 * drm_wait_one_vblank - wait for one vblank 1097 * @dev: DRM device 1098 * @crtc: crtc index 1099 * 1100 * This waits for one vblank to pass on @crtc, using the irq driver interfaces. 1101 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g. 1102 * due to lack of driver support or because the crtc is off. 1103 */ 1104 void drm_wait_one_vblank(struct drm_device *dev, int crtc) 1105 { 1106 int ret; 1107 u32 last; 1108 1109 ret = drm_vblank_get(dev, crtc); 1110 if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", crtc, ret)) 1111 return; 1112 1113 last = drm_vblank_count(dev, crtc); 1114 1115 ret = wait_event_timeout(dev->vblank[crtc].queue, 1116 last != drm_vblank_count(dev, crtc), 1117 msecs_to_jiffies(100)); 1118 1119 WARN(ret == 0, "vblank wait timed out on crtc %i\n", crtc); 1120 1121 drm_vblank_put(dev, crtc); 1122 } 1123 EXPORT_SYMBOL(drm_wait_one_vblank); 1124 1125 /** 1126 * drm_crtc_wait_one_vblank - wait for one vblank 1127 * @crtc: DRM crtc 1128 * 1129 * This waits for one vblank to pass on @crtc, using the irq driver interfaces. 1130 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g. 1131 * due to lack of driver support or because the crtc is off. 1132 */ 1133 void drm_crtc_wait_one_vblank(struct drm_crtc *crtc) 1134 { 1135 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc)); 1136 } 1137 EXPORT_SYMBOL(drm_crtc_wait_one_vblank); 1138 1139 /** 1140 * drm_vblank_off - disable vblank events on a CRTC 1141 * @dev: DRM device 1142 * @crtc: CRTC in question 1143 * 1144 * Drivers can use this function to shut down the vblank interrupt handling when 1145 * disabling a crtc. This function ensures that the latest vblank frame count is 1146 * stored so that drm_vblank_on() can restore it again. 1147 * 1148 * Drivers must use this function when the hardware vblank counter can get 1149 * reset, e.g. when suspending. 1150 * 1151 * This is the legacy version of drm_crtc_vblank_off(). 1152 */ 1153 void drm_vblank_off(struct drm_device *dev, int crtc) 1154 { 1155 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 1156 struct drm_pending_vblank_event *e, *t; 1157 struct timeval now; 1158 unsigned int seq; 1159 1160 lockmgr(&dev->event_lock, LK_EXCLUSIVE); 1161 1162 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE); 1163 vblank_disable_and_save(dev, crtc); 1164 wake_up(&vblank->queue); 1165 1166 /* 1167 * Prevent subsequent drm_vblank_get() from re-enabling 1168 * the vblank interrupt by bumping the refcount. 1169 */ 1170 if (!vblank->inmodeset) { 1171 atomic_inc(&vblank->refcount); 1172 vblank->inmodeset = 1; 1173 } 1174 lockmgr(&dev->vbl_lock, LK_RELEASE); 1175 1176 /* Send any queued vblank events, lest the natives grow disquiet */ 1177 seq = drm_vblank_count_and_time(dev, crtc, &now); 1178 1179 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { 1180 if (e->pipe != crtc) 1181 continue; 1182 DRM_DEBUG("Sending premature vblank event on disable: \ 1183 wanted %d, current %d\n", 1184 e->event.sequence, seq); 1185 list_del(&e->base.link); 1186 drm_vblank_put(dev, e->pipe); 1187 send_vblank_event(dev, e, seq, &now); 1188 } 1189 lockmgr(&dev->event_lock, LK_RELEASE); 1190 } 1191 EXPORT_SYMBOL(drm_vblank_off); 1192 1193 /** 1194 * drm_crtc_vblank_off - disable vblank events on a CRTC 1195 * @crtc: CRTC in question 1196 * 1197 * Drivers can use this function to shut down the vblank interrupt handling when 1198 * disabling a crtc. This function ensures that the latest vblank frame count is 1199 * stored so that drm_vblank_on can restore it again. 1200 * 1201 * Drivers must use this function when the hardware vblank counter can get 1202 * reset, e.g. when suspending. 1203 * 1204 * This is the native kms version of drm_vblank_off(). 1205 */ 1206 void drm_crtc_vblank_off(struct drm_crtc *crtc) 1207 { 1208 drm_vblank_off(crtc->dev, drm_crtc_index(crtc)); 1209 } 1210 EXPORT_SYMBOL(drm_crtc_vblank_off); 1211 1212 /** 1213 * drm_vblank_on - enable vblank events on a CRTC 1214 * @dev: DRM device 1215 * @crtc: CRTC in question 1216 * 1217 * This functions restores the vblank interrupt state captured with 1218 * drm_vblank_off() again. Note that calls to drm_vblank_on() and 1219 * drm_vblank_off() can be unbalanced and so can also be unconditionally called 1220 * in driver load code to reflect the current hardware state of the crtc. 1221 * 1222 * This is the legacy version of drm_crtc_vblank_on(). 1223 */ 1224 void drm_vblank_on(struct drm_device *dev, int crtc) 1225 { 1226 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 1227 1228 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE); 1229 /* Drop our private "prevent drm_vblank_get" refcount */ 1230 if (vblank->inmodeset) { 1231 atomic_dec(&vblank->refcount); 1232 vblank->inmodeset = 0; 1233 } 1234 1235 /* 1236 * sample the current counter to avoid random jumps 1237 * when drm_vblank_enable() applies the diff 1238 * 1239 * -1 to make sure user will never see the same 1240 * vblank counter value before and after a modeset 1241 */ 1242 vblank->last = 1243 (dev->driver->get_vblank_counter(dev, crtc) - 1) & 1244 dev->max_vblank_count; 1245 /* 1246 * re-enable interrupts if there are users left, or the 1247 * user wishes vblank interrupts to be enabled all the time. 1248 */ 1249 if (atomic_read(&vblank->refcount) != 0 || 1250 (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0)) 1251 WARN_ON(drm_vblank_enable(dev, crtc)); 1252 lockmgr(&dev->vbl_lock, LK_RELEASE); 1253 } 1254 EXPORT_SYMBOL(drm_vblank_on); 1255 1256 /** 1257 * drm_crtc_vblank_on - enable vblank events on a CRTC 1258 * @crtc: CRTC in question 1259 * 1260 * This functions restores the vblank interrupt state captured with 1261 * drm_vblank_off() again. Note that calls to drm_vblank_on() and 1262 * drm_vblank_off() can be unbalanced and so can also be unconditionally called 1263 * in driver load code to reflect the current hardware state of the crtc. 1264 * 1265 * This is the native kms version of drm_vblank_on(). 1266 */ 1267 void drm_crtc_vblank_on(struct drm_crtc *crtc) 1268 { 1269 drm_vblank_on(crtc->dev, drm_crtc_index(crtc)); 1270 } 1271 EXPORT_SYMBOL(drm_crtc_vblank_on); 1272 1273 /** 1274 * drm_vblank_pre_modeset - account for vblanks across mode sets 1275 * @dev: DRM device 1276 * @crtc: CRTC in question 1277 * 1278 * Account for vblank events across mode setting events, which will likely 1279 * reset the hardware frame counter. 1280 * 1281 * This is done by grabbing a temporary vblank reference to ensure that the 1282 * vblank interrupt keeps running across the modeset sequence. With this the 1283 * software-side vblank frame counting will ensure that there are no jumps or 1284 * discontinuities. 1285 * 1286 * Unfortunately this approach is racy and also doesn't work when the vblank 1287 * interrupt stops running, e.g. across system suspend resume. It is therefore 1288 * highly recommended that drivers use the newer drm_vblank_off() and 1289 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when 1290 * using "cooked" software vblank frame counters and not relying on any hardware 1291 * counters. 1292 * 1293 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc 1294 * again. 1295 */ 1296 void drm_vblank_pre_modeset(struct drm_device *dev, int crtc) 1297 { 1298 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 1299 1300 /* vblank is not initialized (IRQ not installed ?), or has been freed */ 1301 if (!dev->num_crtcs) 1302 return; 1303 1304 if (WARN_ON(crtc >= dev->num_crtcs)) 1305 return; 1306 1307 /* 1308 * To avoid all the problems that might happen if interrupts 1309 * were enabled/disabled around or between these calls, we just 1310 * have the kernel take a reference on the CRTC (just once though 1311 * to avoid corrupting the count if multiple, mismatch calls occur), 1312 * so that interrupts remain enabled in the interim. 1313 */ 1314 if (!vblank->inmodeset) { 1315 vblank->inmodeset = 0x1; 1316 if (drm_vblank_get(dev, crtc) == 0) 1317 vblank->inmodeset |= 0x2; 1318 } 1319 } 1320 EXPORT_SYMBOL(drm_vblank_pre_modeset); 1321 1322 /** 1323 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes 1324 * @dev: DRM device 1325 * @crtc: CRTC in question 1326 * 1327 * This function again drops the temporary vblank reference acquired in 1328 * drm_vblank_pre_modeset. 1329 */ 1330 void drm_vblank_post_modeset(struct drm_device *dev, int crtc) 1331 { 1332 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 1333 1334 /* vblank is not initialized (IRQ not installed ?), or has been freed */ 1335 if (!dev->num_crtcs) 1336 return; 1337 1338 if (vblank->inmodeset) { 1339 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE); 1340 dev->vblank_disable_allowed = true; 1341 lockmgr(&dev->vbl_lock, LK_RELEASE); 1342 1343 if (vblank->inmodeset & 0x2) 1344 drm_vblank_put(dev, crtc); 1345 1346 vblank->inmodeset = 0; 1347 } 1348 } 1349 EXPORT_SYMBOL(drm_vblank_post_modeset); 1350 1351 /* 1352 * drm_modeset_ctl - handle vblank event counter changes across mode switch 1353 * @DRM_IOCTL_ARGS: standard ioctl arguments 1354 * 1355 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET 1356 * ioctls around modesetting so that any lost vblank events are accounted for. 1357 * 1358 * Generally the counter will reset across mode sets. If interrupts are 1359 * enabled around this call, we don't have to do anything since the counter 1360 * will have already been incremented. 1361 */ 1362 int drm_modeset_ctl(struct drm_device *dev, void *data, 1363 struct drm_file *file_priv) 1364 { 1365 struct drm_modeset_ctl *modeset = data; 1366 unsigned int crtc; 1367 1368 /* If drm_vblank_init() hasn't been called yet, just no-op */ 1369 if (!dev->num_crtcs) 1370 return 0; 1371 1372 /* KMS drivers handle this internally */ 1373 if (drm_core_check_feature(dev, DRIVER_MODESET)) 1374 return 0; 1375 1376 crtc = modeset->crtc; 1377 if (crtc >= dev->num_crtcs) 1378 return -EINVAL; 1379 1380 switch (modeset->cmd) { 1381 case _DRM_PRE_MODESET: 1382 drm_vblank_pre_modeset(dev, crtc); 1383 break; 1384 case _DRM_POST_MODESET: 1385 drm_vblank_post_modeset(dev, crtc); 1386 break; 1387 default: 1388 return -EINVAL; 1389 } 1390 1391 return 0; 1392 } 1393 1394 #ifdef __DragonFly__ 1395 static void 1396 drm_vblank_event_destroy(struct drm_pending_event *e) 1397 { 1398 kfree(e); 1399 } 1400 #endif 1401 1402 static int drm_queue_vblank_event(struct drm_device *dev, int pipe, 1403 union drm_wait_vblank *vblwait, 1404 struct drm_file *file_priv) 1405 { 1406 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 1407 struct drm_pending_vblank_event *e; 1408 struct timeval now; 1409 unsigned int seq; 1410 int ret; 1411 1412 e = kzalloc(sizeof *e, GFP_KERNEL); 1413 if (e == NULL) { 1414 ret = -ENOMEM; 1415 goto err_put; 1416 } 1417 1418 e->pipe = pipe; 1419 e->base.pid = curproc->p_pid; 1420 e->event.base.type = DRM_EVENT_VBLANK; 1421 e->event.base.length = sizeof e->event; 1422 e->event.user_data = vblwait->request.signal; 1423 e->base.event = &e->event.base; 1424 e->base.file_priv = file_priv; 1425 e->base.destroy = drm_vblank_event_destroy; 1426 1427 lockmgr(&dev->event_lock, LK_EXCLUSIVE); 1428 1429 /* 1430 * drm_vblank_off() might have been called after we called 1431 * drm_vblank_get(). drm_vblank_off() holds event_lock 1432 * around the vblank disable, so no need for further locking. 1433 * The reference from drm_vblank_get() protects against 1434 * vblank disable from another source. 1435 */ 1436 if (!vblank->enabled) { 1437 ret = -EINVAL; 1438 goto err_unlock; 1439 } 1440 1441 if (file_priv->event_space < sizeof e->event) { 1442 ret = -EBUSY; 1443 goto err_unlock; 1444 } 1445 1446 file_priv->event_space -= sizeof e->event; 1447 seq = drm_vblank_count_and_time(dev, pipe, &now); 1448 1449 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) && 1450 (seq - vblwait->request.sequence) <= (1 << 23)) { 1451 vblwait->request.sequence = seq + 1; 1452 vblwait->reply.sequence = vblwait->request.sequence; 1453 } 1454 1455 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n", 1456 vblwait->request.sequence, seq, pipe); 1457 1458 #if 0 1459 trace_drm_vblank_event_queued(current->pid, pipe, 1460 vblwait->request.sequence); 1461 #endif 1462 1463 e->event.sequence = vblwait->request.sequence; 1464 if ((seq - vblwait->request.sequence) <= (1 << 23)) { 1465 drm_vblank_put(dev, pipe); 1466 send_vblank_event(dev, e, seq, &now); 1467 vblwait->reply.sequence = seq; 1468 } else { 1469 /* drm_handle_vblank_events will call drm_vblank_put */ 1470 list_add_tail(&e->base.link, &dev->vblank_event_list); 1471 vblwait->reply.sequence = vblwait->request.sequence; 1472 } 1473 1474 lockmgr(&dev->event_lock, LK_RELEASE); 1475 1476 return 0; 1477 1478 err_unlock: 1479 lockmgr(&dev->event_lock, LK_RELEASE); 1480 kfree(e); 1481 err_put: 1482 drm_vblank_put(dev, pipe); 1483 return ret; 1484 } 1485 1486 /* 1487 * Wait for VBLANK. 1488 * 1489 * \param inode device inode. 1490 * \param file_priv DRM file private. 1491 * \param cmd command. 1492 * \param data user argument, pointing to a drm_wait_vblank structure. 1493 * \return zero on success or a negative number on failure. 1494 * 1495 * This function enables the vblank interrupt on the pipe requested, then 1496 * sleeps waiting for the requested sequence number to occur, and drops 1497 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that 1498 * after a timeout with no further vblank waits scheduled). 1499 */ 1500 int drm_wait_vblank(struct drm_device *dev, void *data, 1501 struct drm_file *file_priv) 1502 { 1503 struct drm_vblank_crtc *vblank; 1504 union drm_wait_vblank *vblwait = data; 1505 int ret; 1506 unsigned int flags, seq, crtc, high_crtc; 1507 1508 if (!dev->irq_enabled) 1509 return -EINVAL; 1510 1511 if (vblwait->request.type & _DRM_VBLANK_SIGNAL) 1512 return -EINVAL; 1513 1514 if (vblwait->request.type & 1515 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | 1516 _DRM_VBLANK_HIGH_CRTC_MASK)) { 1517 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", 1518 vblwait->request.type, 1519 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | 1520 _DRM_VBLANK_HIGH_CRTC_MASK)); 1521 return -EINVAL; 1522 } 1523 1524 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; 1525 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK); 1526 if (high_crtc) 1527 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT; 1528 else 1529 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0; 1530 if (crtc >= dev->num_crtcs) 1531 return -EINVAL; 1532 1533 vblank = &dev->vblank[crtc]; 1534 1535 ret = drm_vblank_get(dev, crtc); 1536 if (ret) { 1537 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret); 1538 return ret; 1539 } 1540 seq = drm_vblank_count(dev, crtc); 1541 1542 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { 1543 case _DRM_VBLANK_RELATIVE: 1544 vblwait->request.sequence += seq; 1545 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; 1546 case _DRM_VBLANK_ABSOLUTE: 1547 break; 1548 default: 1549 ret = -EINVAL; 1550 goto done; 1551 } 1552 1553 if (flags & _DRM_VBLANK_EVENT) { 1554 /* must hold on to the vblank ref until the event fires 1555 * drm_vblank_put will be called asynchronously 1556 */ 1557 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv); 1558 } 1559 1560 if ((flags & _DRM_VBLANK_NEXTONMISS) && 1561 (seq - vblwait->request.sequence) <= (1<<23)) { 1562 vblwait->request.sequence = seq + 1; 1563 } 1564 1565 DRM_DEBUG("waiting on vblank count %d, crtc %d\n", 1566 vblwait->request.sequence, crtc); 1567 vblank->last_wait = vblwait->request.sequence; 1568 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ, 1569 (((drm_vblank_count(dev, crtc) - 1570 vblwait->request.sequence) <= (1 << 23)) || 1571 !vblank->enabled || 1572 !dev->irq_enabled)); 1573 1574 if (ret != -EINTR) { 1575 struct timeval now; 1576 1577 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now); 1578 vblwait->reply.tval_sec = now.tv_sec; 1579 vblwait->reply.tval_usec = now.tv_usec; 1580 1581 DRM_DEBUG("returning %d to client\n", 1582 vblwait->reply.sequence); 1583 } else { 1584 DRM_DEBUG("vblank wait interrupted by signal\n"); 1585 } 1586 1587 done: 1588 drm_vblank_put(dev, crtc); 1589 return ret; 1590 } 1591 1592 static void drm_handle_vblank_events(struct drm_device *dev, int crtc) 1593 { 1594 struct drm_pending_vblank_event *e, *t; 1595 struct timeval now; 1596 unsigned int seq; 1597 1598 1599 seq = drm_vblank_count_and_time(dev, crtc, &now); 1600 1601 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { 1602 if (e->pipe != crtc) 1603 continue; 1604 if ((seq - e->event.sequence) > (1<<23)) 1605 continue; 1606 1607 DRM_DEBUG("vblank event on %d, current %d\n", 1608 e->event.sequence, seq); 1609 1610 list_del(&e->base.link); 1611 drm_vblank_put(dev, e->pipe); 1612 send_vblank_event(dev, e, seq, &now); 1613 } 1614 1615 #if 0 1616 trace_drm_vblank_event(crtc, seq); 1617 #endif 1618 } 1619 1620 /** 1621 * drm_handle_vblank - handle a vblank event 1622 * @dev: DRM device 1623 * @crtc: where this event occurred 1624 * 1625 * Drivers should call this routine in their vblank interrupt handlers to 1626 * update the vblank counter and send any signals that may be pending. 1627 * 1628 * This is the legacy version of drm_crtc_handle_vblank(). 1629 */ 1630 bool drm_handle_vblank(struct drm_device *dev, int crtc) 1631 { 1632 struct drm_vblank_crtc *vblank = &dev->vblank[crtc]; 1633 u32 vblcount; 1634 s64 diff_ns; 1635 struct timeval tvblank; 1636 1637 if (!dev->num_crtcs) 1638 return false; 1639 1640 lockmgr(&dev->event_lock, LK_EXCLUSIVE); 1641 1642 if (WARN_ON(crtc >= dev->num_crtcs)) 1643 return false; 1644 1645 /* Need timestamp lock to prevent concurrent execution with 1646 * vblank enable/disable, as this would cause inconsistent 1647 * or corrupted timestamps and vblank counts. 1648 */ 1649 lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE); 1650 1651 /* Vblank irq handling disabled. Nothing to do. */ 1652 if (!vblank->enabled) { 1653 lockmgr(&dev->vblank_time_lock, LK_RELEASE); 1654 lockmgr(&dev->event_lock, LK_RELEASE); 1655 return false; 1656 } 1657 1658 /* Fetch corresponding timestamp for this vblank interval from 1659 * driver and store it in proper slot of timestamp ringbuffer. 1660 */ 1661 1662 /* Get current timestamp and count. */ 1663 vblcount = atomic_read(&vblank->count); 1664 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ); 1665 1666 /* Compute time difference to timestamp of last vblank */ 1667 diff_ns = timeval_to_ns(&tvblank) - 1668 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount)); 1669 1670 /* Update vblank timestamp and count if at least 1671 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds 1672 * difference between last stored timestamp and current 1673 * timestamp. A smaller difference means basically 1674 * identical timestamps. Happens if this vblank has 1675 * been already processed and this is a redundant call, 1676 * e.g., due to spurious vblank interrupts. We need to 1677 * ignore those for accounting. 1678 */ 1679 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) { 1680 /* Store new timestamp in ringbuffer. */ 1681 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank; 1682 1683 /* Increment cooked vblank count. This also atomically commits 1684 * the timestamp computed above. 1685 */ 1686 smp_mb__before_atomic(); 1687 atomic_inc(&vblank->count); 1688 smp_mb__after_atomic(); 1689 } else { 1690 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n", 1691 crtc, (int) diff_ns); 1692 } 1693 1694 lockmgr(&dev->vblank_time_lock, LK_RELEASE); 1695 1696 wake_up(&vblank->queue); 1697 drm_handle_vblank_events(dev, crtc); 1698 1699 lockmgr(&dev->event_lock, LK_RELEASE); 1700 1701 return true; 1702 } 1703 EXPORT_SYMBOL(drm_handle_vblank); 1704 1705 /** 1706 * drm_crtc_handle_vblank - handle a vblank event 1707 * @crtc: where this event occurred 1708 * 1709 * Drivers should call this routine in their vblank interrupt handlers to 1710 * update the vblank counter and send any signals that may be pending. 1711 * 1712 * This is the native KMS version of drm_handle_vblank(). 1713 * 1714 * Returns: 1715 * True if the event was successfully handled, false on failure. 1716 */ 1717 bool drm_crtc_handle_vblank(struct drm_crtc *crtc) 1718 { 1719 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc)); 1720 } 1721 EXPORT_SYMBOL(drm_crtc_handle_vblank); 1722