Lines Matching refs:vblank
2 * drm_irq.c IRQ and vblank support
43 * DOC: vblank handling
54 * scanlines is referred to as the vertical blanking region, or vblank for
98 * this programming has to both start and end in the same vblank. To help
101 * The interrupt is in this context named the vblank interrupt.
103 * The vblank interrupt may be fired at different points depending on the
122 * drm_crtc_handle_vblank() in its vblank interrupt handler for working vblank
129 * call drm_crtc_vblank_get() and release the vblank reference again with
130 * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
133 * On many hardware disabling the vblank interrupt cannot be done in a race-free
135 * &drm_driver.max_vblank_count. In that case the vblank core only disables the
141 * automatically generate fake vblank events as part of the display update.
152 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
166 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
173 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
177 vblank->last = last;
179 write_seqlock(&vblank->seqlock);
180 vblank->time = t_vblank;
181 atomic64_add(vblank_count_inc, &vblank->count);
182 write_sequnlock(&vblank->seqlock);
187 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
189 return vblank->max_vblank_count ?: dev->max_vblank_count;
223 * Reset the stored timestamp for the current vblank count to correspond
224 * to the last vblank occurred.
229 * device vblank fields.
250 * Only reinitialize corresponding vblank timestamp if high-precision query
251 * available and didn't fail. Otherwise reinitialize delayed at next vblank
259 * vblank counter value before and after a modeset
267 * Call back into the driver to update the appropriate vblank counter
276 * device vblank fields.
281 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
286 int framedur_ns = vblank->framedur_ns;
293 * here if the register is small or we had vblank interrupts off for
296 * We repeat the hardware vblank counter & timestamp query until
299 * corresponding vblank timestamp.
308 diff = (cur_vblank - vblank->last) & max_vblank_count;
310 u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
334 * interval? If so then vblank irqs keep running and it will likely
335 * happen that the hardware vblank counter is not trustworthy as it
336 * might reset at some point in that interval and vblank timestamps
339 * random large forward jumps of the software vblank counter.
341 if (diff > 1 && (vblank->inmodeset & 0x2)) {
343 "clamping vblank bump to 1 on crtc %u: diffr=%u"
348 drm_dbg_vbl(dev, "updating vblank count on crtc %u:"
350 pipe, (unsigned long long)atomic64_read(&vblank->count),
351 diff, cur_vblank, vblank->last);
354 drm_WARN_ON_ONCE(dev, cur_vblank != vblank->last);
359 * Only reinitialize corresponding vblank timestamp if high-precision query
360 * available and didn't fail, or we were called from the vblank interrupt.
361 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
372 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
378 count = atomic64_read(&vblank->count);
393 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
397 * interpolates to handle a race with vblank interrupts using the high precision
407 u64 vblank;
412 "This function requires support for accurate vblank timestamps.");
417 vblank = drm_vblank_count(dev, pipe);
421 return vblank;
444 * Disable vblank irq's on crtc, make sure that last vblank count
445 * of hardware and corresponding consistent software vblank counter
446 * are preserved, even if there are any spurious vblank irq's after
451 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
456 /* Prevent vblank irq processing while disabling vblank irqs,
463 * Update vblank count and disable vblank interrupts only if the
468 if (!vblank->enabled)
479 vblank->enabled = false;
487 struct drm_vblank_crtc *vblank = from_timer(vblank, t, disable_timer);
488 struct drm_device *dev = vblank->dev;
489 unsigned int pipe = vblank->pipe;
493 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
494 drm_dbg_core(dev, "disabling vblank on crtc %u\n", pipe);
502 struct drm_vblank_crtc *vblank = ptr;
504 drm_WARN_ON(dev, READ_ONCE(vblank->enabled) &&
507 drm_vblank_destroy_worker(vblank);
508 del_timer_sync(&vblank->disable_timer);
512 * drm_vblank_init - initialize vblank support
516 * This function initializes vblank support for @num_crtcs display pipelines.
531 dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
532 if (!dev->vblank)
538 struct drm_vblank_crtc *vblank = &dev->vblank[i];
540 vblank->dev = dev;
541 vblank->pipe = i;
542 init_waitqueue_head(&vblank->queue);
543 timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
544 seqlock_init(&vblank->seqlock);
547 vblank);
551 ret = drm_vblank_worker_init(vblank);
565 * Drivers may call this function to test if vblank support is
583 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
584 * @crtc: which CRTC's vblank waitqueue to retrieve
586 * This function returns a pointer to the vblank waitqueue for the CRTC.
587 * Drivers can use this to implement vblank waits using wait_event() and related
592 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
598 * drm_calc_timestamping_constants - calculate vblank timestamp constants
602 * Calculate and store various constants which are later needed by vblank and
613 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
645 vblank->linedur_ns = linedur_ns;
646 vblank->framedur_ns = framedur_ns;
647 drm_mode_copy(&vblank->hwmode, mode);
659 * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
661 * @crtc: CRTC whose vblank timestamp to retrieve
667 * need to apply some workarounds for gpu-specific vblank irq quirks
673 * Implements calculation of exact vblank timestamps from given drm_display_mode
698 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
718 mode = &vblank->hwmode;
783 * vblank_time timestamp for end of vblank.
805 * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
807 * @crtc: CRTC whose vblank timestamp to retrieve
813 * need to apply some workarounds for gpu-specific vblank irq quirks
816 * Implements calculation of exact vblank timestamps from given drm_display_mode
848 * recent vblank interval
849 * @crtc: CRTC whose vblank timestamp to retrieve
853 * need to apply some workarounds for gpu-specific vblank irq quirks
857 * vblank interval on specified CRTC. May call into kms-driver to
861 * call, i.e., it isn't very precisely locked to the true vblank.
900 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
903 * Fetches the "cooked" vblank count value that represents the number of
904 * vblank events since the system was booted, including lost events due to
906 * vblank interrupt (since it only reports the software vblank counter), see
909 * Note that for a given vblank counter value drm_crtc_handle_vblank()
913 * functions, if the vblank count is the same or a later one.
918 * The software vblank counter.
927 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
928 * system timestamp corresponding to that vblank counter value.
931 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
933 * Fetches the "cooked" vblank count value that represents the number of
934 * vblank events since the system was booted, including lost events due to
936 * of the vblank interval that corresponds to the current vblank counter value.
943 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
953 seq = read_seqbegin(&vblank->seqlock);
954 vblank_count = atomic64_read(&vblank->count);
955 *vblanktime = vblank->time;
956 } while (read_seqretry(&vblank->seqlock, seq));
962 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
963 * and the system timestamp corresponding to that vblank counter value
965 * @vblanktime: Pointer to time to receive the vblank timestamp.
967 * Fetches the "cooked" vblank count value that represents the number of
968 * vblank events since the system was booted, including lost events due to
970 * of the vblank interval that corresponds to the current vblank counter value.
972 * Note that for a given vblank counter value drm_crtc_handle_vblank()
976 * functions, if the vblank count is the same or a later one.
989 * drm_crtc_next_vblank_start - calculate the time of the next vblank
990 * @crtc: the crtc for which to calculate next vblank time
991 * @vblanktime: pointer to time to receive the next vblank timestamp.
993 * Calculate the expected time of the start of the next vblank period,
994 * based on time of previous vblank and frame duration
999 struct drm_vblank_crtc *vblank;
1006 vblank = &crtc->dev->vblank[pipe];
1007 mode = &vblank->hwmode;
1009 if (!vblank->framedur_ns || !vblank->linedur_ns)
1016 (u64)vblank->framedur_ns * mode->crtc_vblank_start,
1061 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1062 * @crtc: the source CRTC of the vblank event
1065 * A lot of drivers need to generate vblank events for the very next vblank
1067 * flip gets armed, but not when it actually executes within the next vblank
1068 * period. This helper function implements exactly the required vblank arming
1072 * atomic commit must ensure that the next vblank happens at exactly the same
1074 * does **not** protect against the next vblank interrupt racing with either this
1077 * 1. Driver commits new hardware state into vblank-synchronized registers.
1078 * 2. A vblank happens, committing the hardware state. Also the corresponding
1079 * vblank interrupt is fired off and fully processed by the interrupt
1082 * 4. The event is only send out for the next vblank, which is wrong.
1087 * The only way to make this work safely is to prevent the vblank from firing
1095 * Caller must hold a vblank reference for the event @e acquired by a
1096 * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
1113 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1114 * @crtc: the source CRTC of the vblank event
1118 * vblank, and sends it to userspace. Caller must hold event lock.
1165 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1172 if (!vblank->enabled) {
1174 * Enable vblank irqs under vblank_time_lock protection.
1175 * All vblank count & timestamp updates are held off
1178 * prevent double-accounting of same vblank interval.
1181 drm_dbg_core(dev, "enabling vblank on crtc %u, ret: %d\n",
1184 atomic_dec(&vblank->refcount);
1189 * to mark the vblank as enabled after the call
1192 WRITE_ONCE(vblank->enabled, true);
1203 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1215 if (atomic_add_return(1, &vblank->refcount) == 1) {
1218 if (!vblank->enabled) {
1219 atomic_dec(&vblank->refcount);
1229 * drm_crtc_vblank_get - get a reference count on vblank events
1232 * Acquire a reference count on vblank events to avoid having them disabled
1246 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1251 if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
1255 if (atomic_dec_and_test(&vblank->refcount)) {
1259 vblank_disable_fn(&vblank->disable_timer);
1261 mod_timer(&vblank->disable_timer,
1267 * drm_crtc_vblank_put - give up ownership of vblank events
1270 * Release ownership of a given vblank counter, turning off interrupts
1280 * drm_wait_one_vblank - wait for one vblank
1284 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1285 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1292 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1300 if (drm_WARN(dev, ret, "vblank not available on crtc %i, ret=%i\n",
1306 ret = wait_event_timeout(vblank->queue,
1310 drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1317 * drm_crtc_wait_one_vblank - wait for one vblank
1320 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1321 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1331 * drm_crtc_vblank_off - disable vblank events on a CRTC
1334 * Drivers can use this function to shut down the vblank interrupt handling when
1335 * disabling a crtc. This function ensures that the latest vblank frame count is
1338 * Drivers must use this function when the hardware vblank counter can get
1345 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1354 * Grab event_lock early to prevent vblank work from being scheduled
1355 * while we're in the middle of shutting down vblank interrupts
1360 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1361 pipe, vblank->enabled, vblank->inmodeset);
1363 /* Avoid redundant vblank disables without previous
1365 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1368 wake_up(&vblank->queue);
1372 * the vblank interrupt by bumping the refcount.
1374 if (!vblank->inmodeset) {
1375 atomic_inc(&vblank->refcount);
1376 vblank->inmodeset = 1;
1380 /* Send any queued vblank events, lest the natives grow disquiet */
1386 drm_dbg_core(dev, "Sending premature vblank event on disable: "
1394 /* Cancel any leftover pending vblank work */
1395 drm_vblank_cancel_pending_works(vblank);
1401 vblank->hwmode.crtc_clock = 0;
1403 /* Wait for any vblank work that's still executing to finish */
1404 drm_vblank_flush_worker(vblank);
1409 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1412 * Drivers can use this function to reset the vblank state to off at load time.
1415 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1424 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1428 * Prevent subsequent drm_vblank_get() from enabling the vblank
1431 if (!vblank->inmodeset) {
1432 atomic_inc(&vblank->refcount);
1433 vblank->inmodeset = 1;
1438 drm_WARN_ON(dev, !list_empty(&vblank->pending_work));
1443 * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
1445 * @max_vblank_count: max hardware vblank counter value
1447 * Update the maximum hardware vblank counter value for @crtc
1449 * hardware vblank counter depends on the currently active
1452 * For example, if the hardware vblank counter does not work
1464 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1467 drm_WARN_ON(dev, !READ_ONCE(vblank->inmodeset));
1469 vblank->max_vblank_count = max_vblank_count;
1474 * drm_crtc_vblank_on - enable vblank events on a CRTC
1477 * This functions restores the vblank interrupt state captured with
1487 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1493 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1494 pipe, vblank->enabled, vblank->inmodeset);
1497 if (vblank->inmodeset) {
1498 atomic_dec(&vblank->refcount);
1499 vblank->inmodeset = 0;
1506 * user wishes vblank interrupts to be enabled all the time.
1508 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1517 struct drm_vblank_crtc *vblank;
1530 vblank = &dev->vblank[pipe];
1532 drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
1534 framedur_ns = vblank->framedur_ns;
1541 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
1548 diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
1549 vblank->last = (cur_vblank - diff) & max_vblank_count;
1553 * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
1556 * Power manamement features can cause frame counter resets between vblank
1560 * vblank counter.
1565 * time-stamping functions are race-free against vblank hardware counter
1580 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1582 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1596 if (!vblank->inmodeset) {
1597 vblank->inmodeset = 0x1;
1599 vblank->inmodeset |= 0x2;
1606 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1608 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1615 if (vblank->inmodeset) {
1620 if (vblank->inmodeset & 0x2)
1623 vblank->inmodeset = 0;
1664 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1693 * vblank disable, so no need for further locking. The reference from
1694 * drm_vblank_get() protects against vblank disable from another source.
1696 if (!READ_ONCE(vblank->enabled)) {
1709 drm_dbg_core(dev, "event on vblank count %llu, current %llu, crtc %u\n",
1794 struct drm_vblank_crtc *vblank;
1843 vblank = &dev->vblank[pipe];
1846 * queries to return the cached timestamp of the last vblank.
1850 READ_ONCE(vblank->enabled)) {
1858 "crtc %d failed to acquire vblank counter, %d\n",
1886 /* must hold on to the vblank ref until the event fires
1895 drm_dbg_core(dev, "waiting on vblank count %llu, crtc %u\n",
1897 wait = wait_event_interruptible_timeout(vblank->queue,
1899 !READ_ONCE(vblank->enabled),
1923 drm_dbg_core(dev, "crtc %d vblank wait interrupted by signal\n",
1950 drm_dbg_core(dev, "vblank event on %llu, current %llu\n",
1965 * drm_handle_vblank - handle a vblank event
1969 * Drivers should call this routine in their vblank interrupt handlers to
1970 * update the vblank counter and send any signals that may be pending.
1976 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1989 * vblank enable/disable, as this would cause inconsistent
1990 * or corrupted timestamps and vblank counts.
1995 if (!vblank->enabled) {
2005 wake_up(&vblank->queue);
2008 * we finish processing the following vblank after all events have
2014 !atomic_read(&vblank->refcount));
2017 drm_handle_vblank_works(vblank);
2022 vblank_disable_fn(&vblank->disable_timer);
2029 * drm_crtc_handle_vblank - handle a vblank event
2032 * Drivers should call this routine in their vblank interrupt handlers to
2033 * update the vblank counter and send any signals that may be pending.
2037 * Note that for a given vblank counter value drm_crtc_handle_vblank()
2041 * functions, if the vblank count is the same or a later one.
2066 struct drm_vblank_crtc *vblank;
2085 vblank = &dev->vblank[pipe];
2086 vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
2092 "crtc %d failed to acquire vblank counter, %d\n",
2122 struct drm_vblank_crtc *vblank;
2150 vblank = &dev->vblank[pipe];
2159 "crtc %d failed to acquire vblank counter, %d\n",
2183 * vblank disable, so no need for further locking. The reference from
2184 * drm_crtc_vblank_get() protects against vblank disable from another source.
2186 if (!READ_ONCE(vblank->enabled)) {