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