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