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