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