2 * drm_irq.c IRQ and vblank support
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
27 #include <drm/drm_vblank.h>
29 #include <linux/export.h>
31 #include "drm_trace.h"
32 #include "drm_internal.h"
35 * DOC: vblank handling
37 * Vertical blanking plays a major role in graphics rendering. To achieve
38 * tear-free display, users must synchronize page flips and/or rendering to
39 * vertical blanking. The DRM API offers ioctls to perform page flips
40 * synchronized to vertical blanking and wait for vertical blanking.
42 * The DRM core handles most of the vertical blanking management logic, which
43 * involves filtering out spurious interrupts, keeping race-free blanking
44 * counters, coping with counter wrap-around and resets and keeping use counts.
45 * It relies on the driver to generate vertical blanking interrupts and
46 * optionally provide a hardware vertical blanking counter.
48 * Drivers must initialize the vertical blanking handling core with a call to
49 * drm_vblank_init(). Minimally, a driver needs to implement
50 * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
51 * drm_crtc_handle_vblank() in it's vblank interrupt handler for working vblank
54 * Vertical blanking interrupts can be enabled by the DRM core or by drivers
55 * themselves (for instance to handle page flipping operations). The DRM core
56 * maintains a vertical blanking use count to ensure that the interrupts are not
57 * disabled while a user still needs them. To increment the use count, drivers
58 * call drm_crtc_vblank_get() and release the vblank reference again with
59 * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
60 * guaranteed to be enabled.
62 * On many hardware disabling the vblank interrupt cannot be done in a race-free
63 * manner, see &drm_driver.vblank_disable_immediate and
64 * &drm_driver.max_vblank_count. In that case the vblank core only disables the
65 * vblanks after a timer has expired, which can be configured through the
66 * ``vblankoffdelay`` module parameter.
69 /* Retry timestamp calculation up to 3 times to satisfy
70 * drm_timestamp_precision before giving up.
72 #define DRM_TIMESTAMP_MAXRETRIES 3
74 /* Threshold in nanoseconds for detection of redundant
75 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
77 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
80 drm_get_last_vbltimestamp(struct drm_device
*dev
, unsigned int pipe
,
81 ktime_t
*tvblank
, bool in_vblank_irq
);
83 static unsigned int drm_timestamp_precision
= 20; /* Default to 20 usecs. */
85 static int drm_vblank_offdelay
= 5000; /* Default to 5000 msecs. */
87 module_param_named(vblankoffdelay
, drm_vblank_offdelay
, int, 0600);
88 module_param_named(timestamp_precision_usec
, drm_timestamp_precision
, int, 0600);
89 MODULE_PARM_DESC(vblankoffdelay
, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
90 MODULE_PARM_DESC(timestamp_precision_usec
, "Max. error on timestamps [usecs]");
92 static void store_vblank(struct drm_device
*dev
, unsigned int pipe
,
94 ktime_t t_vblank
, u32 last
)
96 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
98 assert_spin_locked(&dev
->vblank_time_lock
);
102 write_seqlock(&vblank
->seqlock
);
103 vblank
->time
= t_vblank
;
104 vblank
->count
+= vblank_count_inc
;
105 write_sequnlock(&vblank
->seqlock
);
108 static u32
drm_max_vblank_count(struct drm_device
*dev
, unsigned int pipe
)
110 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
112 return vblank
->max_vblank_count
?: dev
->max_vblank_count
;
116 * "No hw counter" fallback implementation of .get_vblank_counter() hook,
117 * if there is no useable hardware frame counter available.
119 static u32
drm_vblank_no_hw_counter(struct drm_device
*dev
, unsigned int pipe
)
121 WARN_ON_ONCE(drm_max_vblank_count(dev
, pipe
) != 0);
125 static u32
__get_vblank_counter(struct drm_device
*dev
, unsigned int pipe
)
127 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
128 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
133 if (crtc
->funcs
->get_vblank_counter
)
134 return crtc
->funcs
->get_vblank_counter(crtc
);
137 if (dev
->driver
->get_vblank_counter
)
138 return dev
->driver
->get_vblank_counter(dev
, pipe
);
140 return drm_vblank_no_hw_counter(dev
, pipe
);
144 * Reset the stored timestamp for the current vblank count to correspond
145 * to the last vblank occurred.
147 * Only to be called from drm_crtc_vblank_on().
149 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
150 * device vblank fields.
152 static void drm_reset_vblank_timestamp(struct drm_device
*dev
, unsigned int pipe
)
157 int count
= DRM_TIMESTAMP_MAXRETRIES
;
159 spin_lock(&dev
->vblank_time_lock
);
162 * sample the current counter to avoid random jumps
163 * when drm_vblank_enable() applies the diff
166 cur_vblank
= __get_vblank_counter(dev
, pipe
);
167 rc
= drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, false);
168 } while (cur_vblank
!= __get_vblank_counter(dev
, pipe
) && --count
> 0);
171 * Only reinitialize corresponding vblank timestamp if high-precision query
172 * available and didn't fail. Otherwise reinitialize delayed at next vblank
173 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
179 * +1 to make sure user will never see the same
180 * vblank counter value before and after a modeset
182 store_vblank(dev
, pipe
, 1, t_vblank
, cur_vblank
);
184 spin_unlock(&dev
->vblank_time_lock
);
188 * Call back into the driver to update the appropriate vblank counter
189 * (specified by @pipe). Deal with wraparound, if it occurred, and
190 * update the last read value so we can deal with wraparound on the next
193 * Only necessary when going from off->on, to account for frames we
194 * didn't get an interrupt for.
196 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
197 * device vblank fields.
199 static void drm_update_vblank_count(struct drm_device
*dev
, unsigned int pipe
,
202 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
203 u32 cur_vblank
, diff
;
206 int count
= DRM_TIMESTAMP_MAXRETRIES
;
207 int framedur_ns
= vblank
->framedur_ns
;
208 u32 max_vblank_count
= drm_max_vblank_count(dev
, pipe
);
211 * Interrupts were disabled prior to this call, so deal with counter
213 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
214 * here if the register is small or we had vblank interrupts off for
217 * We repeat the hardware vblank counter & timestamp query until
218 * we get consistent results. This to prevent races between gpu
219 * updating its hardware counter while we are retrieving the
220 * corresponding vblank timestamp.
223 cur_vblank
= __get_vblank_counter(dev
, pipe
);
224 rc
= drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, in_vblank_irq
);
225 } while (cur_vblank
!= __get_vblank_counter(dev
, pipe
) && --count
> 0);
227 if (max_vblank_count
) {
228 /* trust the hw counter when it's around */
229 diff
= (cur_vblank
- vblank
->last
) & max_vblank_count
;
230 } else if (rc
&& framedur_ns
) {
231 u64 diff_ns
= ktime_to_ns(ktime_sub(t_vblank
, vblank
->time
));
234 * Figure out how many vblanks we've missed based
235 * on the difference in the timestamps and the
236 * frame/field duration.
238 diff
= DIV_ROUND_CLOSEST_ULL(diff_ns
, framedur_ns
);
240 if (diff
== 0 && in_vblank_irq
)
241 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
242 " diff_ns = %lld, framedur_ns = %d)\n",
243 pipe
, (long long) diff_ns
, framedur_ns
);
245 /* some kind of default for drivers w/o accurate vbl timestamping */
246 diff
= in_vblank_irq
? 1 : 0;
250 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
251 * interval? If so then vblank irqs keep running and it will likely
252 * happen that the hardware vblank counter is not trustworthy as it
253 * might reset at some point in that interval and vblank timestamps
254 * are not trustworthy either in that interval. Iow. this can result
255 * in a bogus diff >> 1 which must be avoided as it would cause
256 * random large forward jumps of the software vblank counter.
258 if (diff
> 1 && (vblank
->inmodeset
& 0x2)) {
259 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
260 " due to pre-modeset.\n", pipe
, diff
);
264 DRM_DEBUG_VBL("updating vblank count on crtc %u:"
265 " current=%llu, diff=%u, hw=%u hw_last=%u\n",
266 pipe
, vblank
->count
, diff
, cur_vblank
, vblank
->last
);
269 WARN_ON_ONCE(cur_vblank
!= vblank
->last
);
274 * Only reinitialize corresponding vblank timestamp if high-precision query
275 * available and didn't fail, or we were called from the vblank interrupt.
276 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
277 * for now, to mark the vblanktimestamp as invalid.
279 if (!rc
&& !in_vblank_irq
)
282 store_vblank(dev
, pipe
, diff
, t_vblank
, cur_vblank
);
285 static u64
drm_vblank_count(struct drm_device
*dev
, unsigned int pipe
)
287 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
289 if (WARN_ON(pipe
>= dev
->num_crtcs
))
292 return vblank
->count
;
296 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
297 * @crtc: which counter to retrieve
299 * This function is similar to drm_crtc_vblank_count() but this function
300 * interpolates to handle a race with vblank interrupts using the high precision
301 * timestamping support.
303 * This is mostly useful for hardware that can obtain the scanout position, but
304 * doesn't have a hardware frame counter.
306 u64
drm_crtc_accurate_vblank_count(struct drm_crtc
*crtc
)
308 struct drm_device
*dev
= crtc
->dev
;
309 unsigned int pipe
= drm_crtc_index(crtc
);
313 WARN_ONCE(drm_debug
& DRM_UT_VBL
&& !dev
->driver
->get_vblank_timestamp
,
314 "This function requires support for accurate vblank timestamps.");
316 spin_lock_irqsave(&dev
->vblank_time_lock
, flags
);
318 drm_update_vblank_count(dev
, pipe
, false);
319 vblank
= drm_vblank_count(dev
, pipe
);
321 spin_unlock_irqrestore(&dev
->vblank_time_lock
, flags
);
325 EXPORT_SYMBOL(drm_crtc_accurate_vblank_count
);
327 static void __disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
329 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
330 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
335 if (crtc
->funcs
->disable_vblank
) {
336 crtc
->funcs
->disable_vblank(crtc
);
341 dev
->driver
->disable_vblank(dev
, pipe
);
345 * Disable vblank irq's on crtc, make sure that last vblank count
346 * of hardware and corresponding consistent software vblank counter
347 * are preserved, even if there are any spurious vblank irq's after
350 void drm_vblank_disable_and_save(struct drm_device
*dev
, unsigned int pipe
)
352 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
353 unsigned long irqflags
;
355 assert_spin_locked(&dev
->vbl_lock
);
357 /* Prevent vblank irq processing while disabling vblank irqs,
358 * so no updates of timestamps or count can happen after we've
359 * disabled. Needed to prevent races in case of delayed irq's.
361 spin_lock_irqsave(&dev
->vblank_time_lock
, irqflags
);
364 * Update vblank count and disable vblank interrupts only if the
365 * interrupts were enabled. This avoids calling the ->disable_vblank()
366 * operation in atomic context with the hardware potentially runtime
369 if (!vblank
->enabled
)
373 * Update the count and timestamp to maintain the
374 * appearance that the counter has been ticking all along until
375 * this time. This makes the count account for the entire time
376 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
378 drm_update_vblank_count(dev
, pipe
, false);
379 __disable_vblank(dev
, pipe
);
380 vblank
->enabled
= false;
383 spin_unlock_irqrestore(&dev
->vblank_time_lock
, irqflags
);
386 static void vblank_disable_fn(struct timer_list
*t
)
388 struct drm_vblank_crtc
*vblank
= from_timer(vblank
, t
, disable_timer
);
389 struct drm_device
*dev
= vblank
->dev
;
390 unsigned int pipe
= vblank
->pipe
;
391 unsigned long irqflags
;
393 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
394 if (atomic_read(&vblank
->refcount
) == 0 && vblank
->enabled
) {
395 DRM_DEBUG("disabling vblank on crtc %u\n", pipe
);
396 drm_vblank_disable_and_save(dev
, pipe
);
398 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
401 void drm_vblank_cleanup(struct drm_device
*dev
)
405 /* Bail if the driver didn't call drm_vblank_init() */
406 if (dev
->num_crtcs
== 0)
409 for (pipe
= 0; pipe
< dev
->num_crtcs
; pipe
++) {
410 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
412 WARN_ON(READ_ONCE(vblank
->enabled
) &&
413 drm_core_check_feature(dev
, DRIVER_MODESET
));
415 del_timer_sync(&vblank
->disable_timer
);
424 * drm_vblank_init - initialize vblank support
426 * @num_crtcs: number of CRTCs supported by @dev
428 * This function initializes vblank support for @num_crtcs display pipelines.
429 * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
430 * drivers with a &drm_driver.release callback.
433 * Zero on success or a negative error code on failure.
435 int drm_vblank_init(struct drm_device
*dev
, unsigned int num_crtcs
)
440 spin_lock_init(&dev
->vbl_lock
);
441 spin_lock_init(&dev
->vblank_time_lock
);
443 dev
->num_crtcs
= num_crtcs
;
445 dev
->vblank
= kcalloc(num_crtcs
, sizeof(*dev
->vblank
), GFP_KERNEL
);
449 for (i
= 0; i
< num_crtcs
; i
++) {
450 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[i
];
454 init_waitqueue_head(&vblank
->queue
);
455 timer_setup(&vblank
->disable_timer
, vblank_disable_fn
, 0);
456 seqlock_init(&vblank
->seqlock
);
459 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
461 /* Driver specific high-precision vblank timestamping supported? */
462 if (dev
->driver
->get_vblank_timestamp
)
463 DRM_INFO("Driver supports precise vblank timestamp query.\n");
465 DRM_INFO("No driver support for vblank timestamp query.\n");
467 /* Must have precise timestamping for reliable vblank instant disable */
468 if (dev
->vblank_disable_immediate
&& !dev
->driver
->get_vblank_timestamp
) {
469 dev
->vblank_disable_immediate
= false;
470 DRM_INFO("Setting vblank_disable_immediate to false because "
471 "get_vblank_timestamp == NULL\n");
480 EXPORT_SYMBOL(drm_vblank_init
);
483 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
484 * @crtc: which CRTC's vblank waitqueue to retrieve
486 * This function returns a pointer to the vblank waitqueue for the CRTC.
487 * Drivers can use this to implement vblank waits using wait_event() and related
490 wait_queue_head_t
*drm_crtc_vblank_waitqueue(struct drm_crtc
*crtc
)
492 return &crtc
->dev
->vblank
[drm_crtc_index(crtc
)].queue
;
494 EXPORT_SYMBOL(drm_crtc_vblank_waitqueue
);
498 * drm_calc_timestamping_constants - calculate vblank timestamp constants
499 * @crtc: drm_crtc whose timestamp constants should be updated.
500 * @mode: display mode containing the scanout timings
502 * Calculate and store various constants which are later needed by vblank and
503 * swap-completion timestamping, e.g, by
504 * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true
505 * scanout timing, so they take things like panel scaling or other adjustments
508 void drm_calc_timestamping_constants(struct drm_crtc
*crtc
,
509 const struct drm_display_mode
*mode
)
511 struct drm_device
*dev
= crtc
->dev
;
512 unsigned int pipe
= drm_crtc_index(crtc
);
513 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
514 int linedur_ns
= 0, framedur_ns
= 0;
515 int dotclock
= mode
->crtc_clock
;
520 if (WARN_ON(pipe
>= dev
->num_crtcs
))
523 /* Valid dotclock? */
525 int frame_size
= mode
->crtc_htotal
* mode
->crtc_vtotal
;
528 * Convert scanline length in pixels and video
529 * dot clock to line duration and frame duration
532 linedur_ns
= div_u64((u64
) mode
->crtc_htotal
* 1000000, dotclock
);
533 framedur_ns
= div_u64((u64
) frame_size
* 1000000, dotclock
);
536 * Fields of interlaced scanout modes are only half a frame duration.
538 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
541 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
544 vblank
->linedur_ns
= linedur_ns
;
545 vblank
->framedur_ns
= framedur_ns
;
546 vblank
->hwmode
= *mode
;
548 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
549 crtc
->base
.id
, mode
->crtc_htotal
,
550 mode
->crtc_vtotal
, mode
->crtc_vdisplay
);
551 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
552 crtc
->base
.id
, dotclock
, framedur_ns
, linedur_ns
);
554 EXPORT_SYMBOL(drm_calc_timestamping_constants
);
557 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
559 * @pipe: index of CRTC whose vblank timestamp to retrieve
560 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
561 * On return contains true maximum error of timestamp
562 * @vblank_time: Pointer to time which should receive the timestamp
564 * True when called from drm_crtc_handle_vblank(). Some drivers
565 * need to apply some workarounds for gpu-specific vblank irq quirks
568 * Implements calculation of exact vblank timestamps from given drm_display_mode
569 * timings and current video scanout position of a CRTC. This can be directly
570 * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver
571 * if &drm_driver.get_scanout_position is implemented.
573 * The current implementation only handles standard video modes. For double scan
574 * and interlaced modes the driver is supposed to adjust the hardware mode
575 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
576 * match the scanout position reported.
578 * Note that atomic drivers must call drm_calc_timestamping_constants() before
579 * enabling a CRTC. The atomic helpers already take care of that in
580 * drm_atomic_helper_update_legacy_modeset_state().
584 * Returns true on success, and false on failure, i.e. when no accurate
585 * timestamp could be acquired.
587 bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device
*dev
,
590 ktime_t
*vblank_time
,
593 struct timespec64 ts_etime
, ts_vblank_time
;
594 ktime_t stime
, etime
;
596 struct drm_crtc
*crtc
;
597 const struct drm_display_mode
*mode
;
598 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
600 int delta_ns
, duration_ns
;
602 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
605 crtc
= drm_crtc_from_index(dev
, pipe
);
607 if (pipe
>= dev
->num_crtcs
|| !crtc
) {
608 DRM_ERROR("Invalid crtc %u\n", pipe
);
612 /* Scanout position query not supported? Should not happen. */
613 if (!dev
->driver
->get_scanout_position
) {
614 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
618 if (drm_drv_uses_atomic_modeset(dev
))
619 mode
= &vblank
->hwmode
;
621 mode
= &crtc
->hwmode
;
623 /* If mode timing undefined, just return as no-op:
624 * Happens during initial modesetting of a crtc.
626 if (mode
->crtc_clock
== 0) {
627 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe
);
628 WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev
));
633 /* Get current scanout position with system timestamp.
634 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
635 * if single query takes longer than max_error nanoseconds.
637 * This guarantees a tight bound on maximum error if
638 * code gets preempted or delayed for some reason.
640 for (i
= 0; i
< DRM_TIMESTAMP_MAXRETRIES
; i
++) {
642 * Get vertical and horizontal scanout position vpos, hpos,
643 * and bounding timestamps stime, etime, pre/post query.
645 vbl_status
= dev
->driver
->get_scanout_position(dev
, pipe
,
651 /* Return as no-op if scanout query unsupported or failed. */
653 DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
658 /* Compute uncertainty in timestamp of scanout position query. */
659 duration_ns
= ktime_to_ns(etime
) - ktime_to_ns(stime
);
661 /* Accept result with < max_error nsecs timing uncertainty. */
662 if (duration_ns
<= *max_error
)
666 /* Noisy system timing? */
667 if (i
== DRM_TIMESTAMP_MAXRETRIES
) {
668 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
669 pipe
, duration_ns
/1000, *max_error
/1000, i
);
672 /* Return upper bound of timestamp precision error. */
673 *max_error
= duration_ns
;
675 /* Convert scanout position into elapsed time at raw_time query
676 * since start of scanout at first display scanline. delta_ns
677 * can be negative if start of scanout hasn't happened yet.
679 delta_ns
= div_s64(1000000LL * (vpos
* mode
->crtc_htotal
+ hpos
),
682 /* Subtract time delta from raw timestamp to get final
683 * vblank_time timestamp for end of vblank.
685 *vblank_time
= ktime_sub_ns(etime
, delta_ns
);
687 if ((drm_debug
& DRM_UT_VBL
) == 0)
690 ts_etime
= ktime_to_timespec64(etime
);
691 ts_vblank_time
= ktime_to_timespec64(*vblank_time
);
693 DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
695 (u64
)ts_etime
.tv_sec
, ts_etime
.tv_nsec
/ 1000,
696 (u64
)ts_vblank_time
.tv_sec
, ts_vblank_time
.tv_nsec
/ 1000,
697 duration_ns
/ 1000, i
);
701 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos
);
704 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
707 * @pipe: index of CRTC whose vblank timestamp to retrieve
708 * @tvblank: Pointer to target time which should receive the timestamp
710 * True when called from drm_crtc_handle_vblank(). Some drivers
711 * need to apply some workarounds for gpu-specific vblank irq quirks
714 * Fetches the system timestamp corresponding to the time of the most recent
715 * vblank interval on specified CRTC. May call into kms-driver to
716 * compute the timestamp with a high-precision GPU specific method.
718 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
719 * call, i.e., it isn't very precisely locked to the true vblank.
722 * True if timestamp is considered to be very precise, false otherwise.
725 drm_get_last_vbltimestamp(struct drm_device
*dev
, unsigned int pipe
,
726 ktime_t
*tvblank
, bool in_vblank_irq
)
730 /* Define requested maximum error on timestamps (nanoseconds). */
731 int max_error
= (int) drm_timestamp_precision
* 1000;
733 /* Query driver if possible and precision timestamping enabled. */
734 if (dev
->driver
->get_vblank_timestamp
&& (max_error
> 0))
735 ret
= dev
->driver
->get_vblank_timestamp(dev
, pipe
, &max_error
,
736 tvblank
, in_vblank_irq
);
738 /* GPU high precision timestamp query unsupported or failed.
739 * Return current monotonic/gettimeofday timestamp as best estimate.
742 *tvblank
= ktime_get();
748 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
749 * @crtc: which counter to retrieve
751 * Fetches the "cooked" vblank count value that represents the number of
752 * vblank events since the system was booted, including lost events due to
753 * modesetting activity. Note that this timer isn't correct against a racing
754 * vblank interrupt (since it only reports the software vblank counter), see
755 * drm_crtc_accurate_vblank_count() for such use-cases.
758 * The software vblank counter.
760 u64
drm_crtc_vblank_count(struct drm_crtc
*crtc
)
762 return drm_vblank_count(crtc
->dev
, drm_crtc_index(crtc
));
764 EXPORT_SYMBOL(drm_crtc_vblank_count
);
767 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
768 * system timestamp corresponding to that vblank counter value.
770 * @pipe: index of CRTC whose counter to retrieve
771 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
773 * Fetches the "cooked" vblank count value that represents the number of
774 * vblank events since the system was booted, including lost events due to
775 * modesetting activity. Returns corresponding system timestamp of the time
776 * of the vblank interval that corresponds to the current vblank counter value.
778 * This is the legacy version of drm_crtc_vblank_count_and_time().
780 static u64
drm_vblank_count_and_time(struct drm_device
*dev
, unsigned int pipe
,
783 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
787 if (WARN_ON(pipe
>= dev
->num_crtcs
)) {
793 seq
= read_seqbegin(&vblank
->seqlock
);
794 vblank_count
= vblank
->count
;
795 *vblanktime
= vblank
->time
;
796 } while (read_seqretry(&vblank
->seqlock
, seq
));
802 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
803 * and the system timestamp corresponding to that vblank counter value
804 * @crtc: which counter to retrieve
805 * @vblanktime: Pointer to time to receive the vblank timestamp.
807 * Fetches the "cooked" vblank count value that represents the number of
808 * vblank events since the system was booted, including lost events due to
809 * modesetting activity. Returns corresponding system timestamp of the time
810 * of the vblank interval that corresponds to the current vblank counter value.
812 u64
drm_crtc_vblank_count_and_time(struct drm_crtc
*crtc
,
815 return drm_vblank_count_and_time(crtc
->dev
, drm_crtc_index(crtc
),
818 EXPORT_SYMBOL(drm_crtc_vblank_count_and_time
);
820 static void send_vblank_event(struct drm_device
*dev
,
821 struct drm_pending_vblank_event
*e
,
822 u64 seq
, ktime_t now
)
824 struct timespec64 tv
;
826 switch (e
->event
.base
.type
) {
827 case DRM_EVENT_VBLANK
:
828 case DRM_EVENT_FLIP_COMPLETE
:
829 tv
= ktime_to_timespec64(now
);
830 e
->event
.vbl
.sequence
= seq
;
832 * e->event is a user space structure, with hardcoded unsigned
833 * 32-bit seconds/microseconds. This is safe as we always use
834 * monotonic timestamps since linux-4.15
836 e
->event
.vbl
.tv_sec
= tv
.tv_sec
;
837 e
->event
.vbl
.tv_usec
= tv
.tv_nsec
/ 1000;
839 case DRM_EVENT_CRTC_SEQUENCE
:
841 e
->event
.seq
.sequence
= seq
;
842 e
->event
.seq
.time_ns
= ktime_to_ns(now
);
845 trace_drm_vblank_event_delivered(e
->base
.file_priv
, e
->pipe
, seq
);
846 drm_send_event_locked(dev
, &e
->base
);
850 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
851 * @crtc: the source CRTC of the vblank event
852 * @e: the event to send
854 * A lot of drivers need to generate vblank events for the very next vblank
855 * interrupt. For example when the page flip interrupt happens when the page
856 * flip gets armed, but not when it actually executes within the next vblank
857 * period. This helper function implements exactly the required vblank arming
860 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
861 * atomic commit must ensure that the next vblank happens at exactly the same
862 * time as the atomic commit is committed to the hardware. This function itself
863 * does **not** protect against the next vblank interrupt racing with either this
864 * function call or the atomic commit operation. A possible sequence could be:
866 * 1. Driver commits new hardware state into vblank-synchronized registers.
867 * 2. A vblank happens, committing the hardware state. Also the corresponding
868 * vblank interrupt is fired off and fully processed by the interrupt
870 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
871 * 4. The event is only send out for the next vblank, which is wrong.
873 * An equivalent race can happen when the driver calls
874 * drm_crtc_arm_vblank_event() before writing out the new hardware state.
876 * The only way to make this work safely is to prevent the vblank from firing
877 * (and the hardware from committing anything else) until the entire atomic
878 * commit sequence has run to completion. If the hardware does not have such a
879 * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
880 * Instead drivers need to manually send out the event from their interrupt
881 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
882 * possible race with the hardware committing the atomic update.
884 * Caller must hold a vblank reference for the event @e, which will be dropped
885 * when the next vblank arrives.
887 void drm_crtc_arm_vblank_event(struct drm_crtc
*crtc
,
888 struct drm_pending_vblank_event
*e
)
890 struct drm_device
*dev
= crtc
->dev
;
891 unsigned int pipe
= drm_crtc_index(crtc
);
893 assert_spin_locked(&dev
->event_lock
);
896 e
->sequence
= drm_crtc_accurate_vblank_count(crtc
) + 1;
897 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
899 EXPORT_SYMBOL(drm_crtc_arm_vblank_event
);
902 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
903 * @crtc: the source CRTC of the vblank event
904 * @e: the event to send
906 * Updates sequence # and timestamp on event for the most recently processed
907 * vblank, and sends it to userspace. Caller must hold event lock.
909 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
910 * situation, especially to send out events for atomic commit operations.
912 void drm_crtc_send_vblank_event(struct drm_crtc
*crtc
,
913 struct drm_pending_vblank_event
*e
)
915 struct drm_device
*dev
= crtc
->dev
;
917 unsigned int pipe
= drm_crtc_index(crtc
);
920 if (dev
->num_crtcs
> 0) {
921 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
928 send_vblank_event(dev
, e
, seq
, now
);
930 EXPORT_SYMBOL(drm_crtc_send_vblank_event
);
932 static int __enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
934 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
935 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
940 if (crtc
->funcs
->enable_vblank
)
941 return crtc
->funcs
->enable_vblank(crtc
);
944 return dev
->driver
->enable_vblank(dev
, pipe
);
947 static int drm_vblank_enable(struct drm_device
*dev
, unsigned int pipe
)
949 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
952 assert_spin_locked(&dev
->vbl_lock
);
954 spin_lock(&dev
->vblank_time_lock
);
956 if (!vblank
->enabled
) {
958 * Enable vblank irqs under vblank_time_lock protection.
959 * All vblank count & timestamp updates are held off
960 * until we are done reinitializing master counter and
961 * timestamps. Filtercode in drm_handle_vblank() will
962 * prevent double-accounting of same vblank interval.
964 ret
= __enable_vblank(dev
, pipe
);
965 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe
, ret
);
967 atomic_dec(&vblank
->refcount
);
969 drm_update_vblank_count(dev
, pipe
, 0);
970 /* drm_update_vblank_count() includes a wmb so we just
971 * need to ensure that the compiler emits the write
972 * to mark the vblank as enabled after the call
973 * to drm_update_vblank_count().
975 WRITE_ONCE(vblank
->enabled
, true);
979 spin_unlock(&dev
->vblank_time_lock
);
984 static int drm_vblank_get(struct drm_device
*dev
, unsigned int pipe
)
986 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
987 unsigned long irqflags
;
993 if (WARN_ON(pipe
>= dev
->num_crtcs
))
996 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
997 /* Going from 0->1 means we have to enable interrupts again */
998 if (atomic_add_return(1, &vblank
->refcount
) == 1) {
999 ret
= drm_vblank_enable(dev
, pipe
);
1001 if (!vblank
->enabled
) {
1002 atomic_dec(&vblank
->refcount
);
1006 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1012 * drm_crtc_vblank_get - get a reference count on vblank events
1013 * @crtc: which CRTC to own
1015 * Acquire a reference count on vblank events to avoid having them disabled
1019 * Zero on success or a negative error code on failure.
1021 int drm_crtc_vblank_get(struct drm_crtc
*crtc
)
1023 return drm_vblank_get(crtc
->dev
, drm_crtc_index(crtc
));
1025 EXPORT_SYMBOL(drm_crtc_vblank_get
);
1027 static void drm_vblank_put(struct drm_device
*dev
, unsigned int pipe
)
1029 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1031 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1034 if (WARN_ON(atomic_read(&vblank
->refcount
) == 0))
1037 /* Last user schedules interrupt disable */
1038 if (atomic_dec_and_test(&vblank
->refcount
)) {
1039 if (drm_vblank_offdelay
== 0)
1041 else if (drm_vblank_offdelay
< 0)
1042 vblank_disable_fn(&vblank
->disable_timer
);
1043 else if (!dev
->vblank_disable_immediate
)
1044 mod_timer(&vblank
->disable_timer
,
1045 jiffies
+ ((drm_vblank_offdelay
* HZ
)/1000));
1050 * drm_crtc_vblank_put - give up ownership of vblank events
1051 * @crtc: which counter to give up
1053 * Release ownership of a given vblank counter, turning off interrupts
1054 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1056 void drm_crtc_vblank_put(struct drm_crtc
*crtc
)
1058 drm_vblank_put(crtc
->dev
, drm_crtc_index(crtc
));
1060 EXPORT_SYMBOL(drm_crtc_vblank_put
);
1063 * drm_wait_one_vblank - wait for one vblank
1067 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1068 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1069 * due to lack of driver support or because the crtc is off.
1071 * This is the legacy version of drm_crtc_wait_one_vblank().
1073 void drm_wait_one_vblank(struct drm_device
*dev
, unsigned int pipe
)
1075 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1079 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1082 ret
= drm_vblank_get(dev
, pipe
);
1083 if (WARN(ret
, "vblank not available on crtc %i, ret=%i\n", pipe
, ret
))
1086 last
= drm_vblank_count(dev
, pipe
);
1088 ret
= wait_event_timeout(vblank
->queue
,
1089 last
!= drm_vblank_count(dev
, pipe
),
1090 msecs_to_jiffies(100));
1092 WARN(ret
== 0, "vblank wait timed out on crtc %i\n", pipe
);
1094 drm_vblank_put(dev
, pipe
);
1096 EXPORT_SYMBOL(drm_wait_one_vblank
);
1099 * drm_crtc_wait_one_vblank - wait for one vblank
1102 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1103 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1104 * due to lack of driver support or because the crtc is off.
1106 void drm_crtc_wait_one_vblank(struct drm_crtc
*crtc
)
1108 drm_wait_one_vblank(crtc
->dev
, drm_crtc_index(crtc
));
1110 EXPORT_SYMBOL(drm_crtc_wait_one_vblank
);
1113 * drm_crtc_vblank_off - disable vblank events on a CRTC
1114 * @crtc: CRTC in question
1116 * Drivers can use this function to shut down the vblank interrupt handling when
1117 * disabling a crtc. This function ensures that the latest vblank frame count is
1118 * stored so that drm_vblank_on can restore it again.
1120 * Drivers must use this function when the hardware vblank counter can get
1121 * reset, e.g. when suspending or disabling the @crtc in general.
1123 void drm_crtc_vblank_off(struct drm_crtc
*crtc
)
1125 struct drm_device
*dev
= crtc
->dev
;
1126 unsigned int pipe
= drm_crtc_index(crtc
);
1127 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1128 struct drm_pending_vblank_event
*e
, *t
;
1131 unsigned long irqflags
;
1134 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1137 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
1139 spin_lock(&dev
->vbl_lock
);
1140 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1141 pipe
, vblank
->enabled
, vblank
->inmodeset
);
1143 /* Avoid redundant vblank disables without previous
1144 * drm_crtc_vblank_on(). */
1145 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
) || !vblank
->inmodeset
)
1146 drm_vblank_disable_and_save(dev
, pipe
);
1148 wake_up(&vblank
->queue
);
1151 * Prevent subsequent drm_vblank_get() from re-enabling
1152 * the vblank interrupt by bumping the refcount.
1154 if (!vblank
->inmodeset
) {
1155 atomic_inc(&vblank
->refcount
);
1156 vblank
->inmodeset
= 1;
1158 spin_unlock(&dev
->vbl_lock
);
1160 /* Send any queued vblank events, lest the natives grow disquiet */
1161 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1163 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1164 if (e
->pipe
!= pipe
)
1166 DRM_DEBUG("Sending premature vblank event on disable: "
1167 "wanted %llu, current %llu\n",
1169 list_del(&e
->base
.link
);
1170 drm_vblank_put(dev
, pipe
);
1171 send_vblank_event(dev
, e
, seq
, now
);
1173 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1175 /* Will be reset by the modeset helpers when re-enabling the crtc by
1176 * calling drm_calc_timestamping_constants(). */
1177 vblank
->hwmode
.crtc_clock
= 0;
1179 EXPORT_SYMBOL(drm_crtc_vblank_off
);
1182 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1183 * @crtc: CRTC in question
1185 * Drivers can use this function to reset the vblank state to off at load time.
1186 * Drivers should use this together with the drm_crtc_vblank_off() and
1187 * drm_crtc_vblank_on() functions. The difference compared to
1188 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1189 * and hence doesn't need to call any driver hooks.
1191 * This is useful for recovering driver state e.g. on driver load, or on resume.
1193 void drm_crtc_vblank_reset(struct drm_crtc
*crtc
)
1195 struct drm_device
*dev
= crtc
->dev
;
1196 unsigned long irqflags
;
1197 unsigned int pipe
= drm_crtc_index(crtc
);
1198 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1200 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1202 * Prevent subsequent drm_vblank_get() from enabling the vblank
1203 * interrupt by bumping the refcount.
1205 if (!vblank
->inmodeset
) {
1206 atomic_inc(&vblank
->refcount
);
1207 vblank
->inmodeset
= 1;
1209 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1211 WARN_ON(!list_empty(&dev
->vblank_event_list
));
1213 EXPORT_SYMBOL(drm_crtc_vblank_reset
);
1216 * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
1217 * @crtc: CRTC in question
1218 * @max_vblank_count: max hardware vblank counter value
1220 * Update the maximum hardware vblank counter value for @crtc
1221 * at runtime. Useful for hardware where the operation of the
1222 * hardware vblank counter depends on the currently active
1223 * display configuration.
1225 * For example, if the hardware vblank counter does not work
1226 * when a specific connector is active the maximum can be set
1227 * to zero. And when that specific connector isn't active the
1228 * maximum can again be set to the appropriate non-zero value.
1230 * If used, must be called before drm_vblank_on().
1232 void drm_crtc_set_max_vblank_count(struct drm_crtc
*crtc
,
1233 u32 max_vblank_count
)
1235 struct drm_device
*dev
= crtc
->dev
;
1236 unsigned int pipe
= drm_crtc_index(crtc
);
1237 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1239 WARN_ON(dev
->max_vblank_count
);
1240 WARN_ON(!READ_ONCE(vblank
->inmodeset
));
1242 vblank
->max_vblank_count
= max_vblank_count
;
1244 EXPORT_SYMBOL(drm_crtc_set_max_vblank_count
);
1247 * drm_crtc_vblank_on - enable vblank events on a CRTC
1248 * @crtc: CRTC in question
1250 * This functions restores the vblank interrupt state captured with
1251 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1252 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1253 * unbalanced and so can also be unconditionally called in driver load code to
1254 * reflect the current hardware state of the crtc.
1256 void drm_crtc_vblank_on(struct drm_crtc
*crtc
)
1258 struct drm_device
*dev
= crtc
->dev
;
1259 unsigned int pipe
= drm_crtc_index(crtc
);
1260 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1261 unsigned long irqflags
;
1263 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1266 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1267 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1268 pipe
, vblank
->enabled
, vblank
->inmodeset
);
1270 /* Drop our private "prevent drm_vblank_get" refcount */
1271 if (vblank
->inmodeset
) {
1272 atomic_dec(&vblank
->refcount
);
1273 vblank
->inmodeset
= 0;
1276 drm_reset_vblank_timestamp(dev
, pipe
);
1279 * re-enable interrupts if there are users left, or the
1280 * user wishes vblank interrupts to be enabled all the time.
1282 if (atomic_read(&vblank
->refcount
) != 0 || drm_vblank_offdelay
== 0)
1283 WARN_ON(drm_vblank_enable(dev
, pipe
));
1284 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1286 EXPORT_SYMBOL(drm_crtc_vblank_on
);
1289 * drm_vblank_restore - estimate missed vblanks and update vblank count.
1293 * Power manamement features can cause frame counter resets between vblank
1294 * disable and enable. Drivers can use this function in their
1295 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1296 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1299 * This function is the legacy version of drm_crtc_vblank_restore().
1301 void drm_vblank_restore(struct drm_device
*dev
, unsigned int pipe
)
1304 struct drm_vblank_crtc
*vblank
;
1307 u32 cur_vblank
, diff
= 1;
1308 int count
= DRM_TIMESTAMP_MAXRETRIES
;
1310 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1313 assert_spin_locked(&dev
->vbl_lock
);
1314 assert_spin_locked(&dev
->vblank_time_lock
);
1316 vblank
= &dev
->vblank
[pipe
];
1317 WARN_ONCE((drm_debug
& DRM_UT_VBL
) && !vblank
->framedur_ns
,
1318 "Cannot compute missed vblanks without frame duration\n");
1319 framedur_ns
= vblank
->framedur_ns
;
1322 cur_vblank
= __get_vblank_counter(dev
, pipe
);
1323 drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, false);
1324 } while (cur_vblank
!= __get_vblank_counter(dev
, pipe
) && --count
> 0);
1326 diff_ns
= ktime_to_ns(ktime_sub(t_vblank
, vblank
->time
));
1328 diff
= DIV_ROUND_CLOSEST_ULL(diff_ns
, framedur_ns
);
1331 DRM_DEBUG_VBL("missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1332 diff
, diff_ns
, framedur_ns
, cur_vblank
- vblank
->last
);
1333 store_vblank(dev
, pipe
, diff
, t_vblank
, cur_vblank
);
1335 EXPORT_SYMBOL(drm_vblank_restore
);
1338 * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
1339 * @crtc: CRTC in question
1341 * Power manamement features can cause frame counter resets between vblank
1342 * disable and enable. Drivers can use this function in their
1343 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1344 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1347 void drm_crtc_vblank_restore(struct drm_crtc
*crtc
)
1349 drm_vblank_restore(crtc
->dev
, drm_crtc_index(crtc
));
1351 EXPORT_SYMBOL(drm_crtc_vblank_restore
);
1353 static void drm_legacy_vblank_pre_modeset(struct drm_device
*dev
,
1356 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1358 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1359 if (!dev
->num_crtcs
)
1362 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1366 * To avoid all the problems that might happen if interrupts
1367 * were enabled/disabled around or between these calls, we just
1368 * have the kernel take a reference on the CRTC (just once though
1369 * to avoid corrupting the count if multiple, mismatch calls occur),
1370 * so that interrupts remain enabled in the interim.
1372 if (!vblank
->inmodeset
) {
1373 vblank
->inmodeset
= 0x1;
1374 if (drm_vblank_get(dev
, pipe
) == 0)
1375 vblank
->inmodeset
|= 0x2;
1379 static void drm_legacy_vblank_post_modeset(struct drm_device
*dev
,
1382 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1383 unsigned long irqflags
;
1385 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1386 if (!dev
->num_crtcs
)
1389 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1392 if (vblank
->inmodeset
) {
1393 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1394 drm_reset_vblank_timestamp(dev
, pipe
);
1395 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1397 if (vblank
->inmodeset
& 0x2)
1398 drm_vblank_put(dev
, pipe
);
1400 vblank
->inmodeset
= 0;
1404 int drm_legacy_modeset_ctl_ioctl(struct drm_device
*dev
, void *data
,
1405 struct drm_file
*file_priv
)
1407 struct drm_modeset_ctl
*modeset
= data
;
1410 /* If drm_vblank_init() hasn't been called yet, just no-op */
1411 if (!dev
->num_crtcs
)
1414 /* KMS drivers handle this internally */
1415 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1418 pipe
= modeset
->crtc
;
1419 if (pipe
>= dev
->num_crtcs
)
1422 switch (modeset
->cmd
) {
1423 case _DRM_PRE_MODESET
:
1424 drm_legacy_vblank_pre_modeset(dev
, pipe
);
1426 case _DRM_POST_MODESET
:
1427 drm_legacy_vblank_post_modeset(dev
, pipe
);
1436 static inline bool vblank_passed(u64 seq
, u64 ref
)
1438 return (seq
- ref
) <= (1 << 23);
1441 static int drm_queue_vblank_event(struct drm_device
*dev
, unsigned int pipe
,
1443 union drm_wait_vblank
*vblwait
,
1444 struct drm_file
*file_priv
)
1446 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1447 struct drm_pending_vblank_event
*e
;
1449 unsigned long flags
;
1453 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1460 e
->event
.base
.type
= DRM_EVENT_VBLANK
;
1461 e
->event
.base
.length
= sizeof(e
->event
.vbl
);
1462 e
->event
.vbl
.user_data
= vblwait
->request
.signal
;
1463 e
->event
.vbl
.crtc_id
= 0;
1464 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1465 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
1467 e
->event
.vbl
.crtc_id
= crtc
->base
.id
;
1470 spin_lock_irqsave(&dev
->event_lock
, flags
);
1473 * drm_crtc_vblank_off() might have been called after we called
1474 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1475 * vblank disable, so no need for further locking. The reference from
1476 * drm_vblank_get() protects against vblank disable from another source.
1478 if (!READ_ONCE(vblank
->enabled
)) {
1483 ret
= drm_event_reserve_init_locked(dev
, file_priv
, &e
->base
,
1489 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1491 DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n",
1492 req_seq
, seq
, pipe
);
1494 trace_drm_vblank_event_queued(file_priv
, pipe
, req_seq
);
1496 e
->sequence
= req_seq
;
1497 if (vblank_passed(seq
, req_seq
)) {
1498 drm_vblank_put(dev
, pipe
);
1499 send_vblank_event(dev
, e
, seq
, now
);
1500 vblwait
->reply
.sequence
= seq
;
1502 /* drm_handle_vblank_events will call drm_vblank_put */
1503 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
1504 vblwait
->reply
.sequence
= req_seq
;
1507 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1512 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1515 drm_vblank_put(dev
, pipe
);
1519 static bool drm_wait_vblank_is_query(union drm_wait_vblank
*vblwait
)
1521 if (vblwait
->request
.sequence
)
1524 return _DRM_VBLANK_RELATIVE
==
1525 (vblwait
->request
.type
& (_DRM_VBLANK_TYPES_MASK
|
1527 _DRM_VBLANK_NEXTONMISS
));
1531 * Widen a 32-bit param to 64-bits.
1533 * \param narrow 32-bit value (missing upper 32 bits)
1534 * \param near 64-bit value that should be 'close' to near
1536 * This function returns a 64-bit value using the lower 32-bits from
1537 * 'narrow' and constructing the upper 32-bits so that the result is
1538 * as close as possible to 'near'.
1541 static u64
widen_32_to_64(u32 narrow
, u64 near
)
1543 return near
+ (s32
) (narrow
- near
);
1546 static void drm_wait_vblank_reply(struct drm_device
*dev
, unsigned int pipe
,
1547 struct drm_wait_vblank_reply
*reply
)
1550 struct timespec64 ts
;
1553 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
1554 * to store the seconds. This is safe as we always use monotonic
1555 * timestamps since linux-4.15.
1557 reply
->sequence
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1558 ts
= ktime_to_timespec64(now
);
1559 reply
->tval_sec
= (u32
)ts
.tv_sec
;
1560 reply
->tval_usec
= ts
.tv_nsec
/ 1000;
1563 int drm_wait_vblank_ioctl(struct drm_device
*dev
, void *data
,
1564 struct drm_file
*file_priv
)
1566 struct drm_crtc
*crtc
;
1567 struct drm_vblank_crtc
*vblank
;
1568 union drm_wait_vblank
*vblwait
= data
;
1571 unsigned int pipe_index
;
1572 unsigned int flags
, pipe
, high_pipe
;
1574 if (!dev
->irq_enabled
)
1577 if (vblwait
->request
.type
& _DRM_VBLANK_SIGNAL
)
1580 if (vblwait
->request
.type
&
1581 ~(_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1582 _DRM_VBLANK_HIGH_CRTC_MASK
)) {
1583 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1584 vblwait
->request
.type
,
1585 (_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1586 _DRM_VBLANK_HIGH_CRTC_MASK
));
1590 flags
= vblwait
->request
.type
& _DRM_VBLANK_FLAGS_MASK
;
1591 high_pipe
= (vblwait
->request
.type
& _DRM_VBLANK_HIGH_CRTC_MASK
);
1593 pipe_index
= high_pipe
>> _DRM_VBLANK_HIGH_CRTC_SHIFT
;
1595 pipe_index
= flags
& _DRM_VBLANK_SECONDARY
? 1 : 0;
1597 /* Convert lease-relative crtc index into global crtc index */
1598 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1600 drm_for_each_crtc(crtc
, dev
) {
1601 if (drm_lease_held(file_priv
, crtc
->base
.id
)) {
1602 if (pipe_index
== 0)
1612 if (pipe
>= dev
->num_crtcs
)
1615 vblank
= &dev
->vblank
[pipe
];
1617 /* If the counter is currently enabled and accurate, short-circuit
1618 * queries to return the cached timestamp of the last vblank.
1620 if (dev
->vblank_disable_immediate
&&
1621 drm_wait_vblank_is_query(vblwait
) &&
1622 READ_ONCE(vblank
->enabled
)) {
1623 drm_wait_vblank_reply(dev
, pipe
, &vblwait
->reply
);
1627 ret
= drm_vblank_get(dev
, pipe
);
1629 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe
, ret
);
1632 seq
= drm_vblank_count(dev
, pipe
);
1634 switch (vblwait
->request
.type
& _DRM_VBLANK_TYPES_MASK
) {
1635 case _DRM_VBLANK_RELATIVE
:
1636 req_seq
= seq
+ vblwait
->request
.sequence
;
1637 vblwait
->request
.sequence
= req_seq
;
1638 vblwait
->request
.type
&= ~_DRM_VBLANK_RELATIVE
;
1640 case _DRM_VBLANK_ABSOLUTE
:
1641 req_seq
= widen_32_to_64(vblwait
->request
.sequence
, seq
);
1648 if ((flags
& _DRM_VBLANK_NEXTONMISS
) &&
1649 vblank_passed(seq
, req_seq
)) {
1651 vblwait
->request
.type
&= ~_DRM_VBLANK_NEXTONMISS
;
1652 vblwait
->request
.sequence
= req_seq
;
1655 if (flags
& _DRM_VBLANK_EVENT
) {
1656 /* must hold on to the vblank ref until the event fires
1657 * drm_vblank_put will be called asynchronously
1659 return drm_queue_vblank_event(dev
, pipe
, req_seq
, vblwait
, file_priv
);
1662 if (req_seq
!= seq
) {
1663 DRM_DEBUG("waiting on vblank count %llu, crtc %u\n",
1665 DRM_WAIT_ON(ret
, vblank
->queue
, 3 * HZ
,
1666 vblank_passed(drm_vblank_count(dev
, pipe
),
1668 !READ_ONCE(vblank
->enabled
));
1671 if (ret
!= -EINTR
) {
1672 drm_wait_vblank_reply(dev
, pipe
, &vblwait
->reply
);
1674 DRM_DEBUG("crtc %d returning %u to client\n",
1675 pipe
, vblwait
->reply
.sequence
);
1677 DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe
);
1681 drm_vblank_put(dev
, pipe
);
1685 static void drm_handle_vblank_events(struct drm_device
*dev
, unsigned int pipe
)
1687 struct drm_pending_vblank_event
*e
, *t
;
1691 assert_spin_locked(&dev
->event_lock
);
1693 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1695 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1696 if (e
->pipe
!= pipe
)
1698 if (!vblank_passed(seq
, e
->sequence
))
1701 DRM_DEBUG("vblank event on %llu, current %llu\n",
1704 list_del(&e
->base
.link
);
1705 drm_vblank_put(dev
, pipe
);
1706 send_vblank_event(dev
, e
, seq
, now
);
1709 trace_drm_vblank_event(pipe
, seq
);
1713 * drm_handle_vblank - handle a vblank event
1715 * @pipe: index of CRTC where this event occurred
1717 * Drivers should call this routine in their vblank interrupt handlers to
1718 * update the vblank counter and send any signals that may be pending.
1720 * This is the legacy version of drm_crtc_handle_vblank().
1722 bool drm_handle_vblank(struct drm_device
*dev
, unsigned int pipe
)
1724 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1725 unsigned long irqflags
;
1728 if (WARN_ON_ONCE(!dev
->num_crtcs
))
1731 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1734 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
1736 /* Need timestamp lock to prevent concurrent execution with
1737 * vblank enable/disable, as this would cause inconsistent
1738 * or corrupted timestamps and vblank counts.
1740 spin_lock(&dev
->vblank_time_lock
);
1742 /* Vblank irq handling disabled. Nothing to do. */
1743 if (!vblank
->enabled
) {
1744 spin_unlock(&dev
->vblank_time_lock
);
1745 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1749 drm_update_vblank_count(dev
, pipe
, true);
1751 spin_unlock(&dev
->vblank_time_lock
);
1753 wake_up(&vblank
->queue
);
1755 /* With instant-off, we defer disabling the interrupt until after
1756 * we finish processing the following vblank after all events have
1757 * been signaled. The disable has to be last (after
1758 * drm_handle_vblank_events) so that the timestamp is always accurate.
1760 disable_irq
= (dev
->vblank_disable_immediate
&&
1761 drm_vblank_offdelay
> 0 &&
1762 !atomic_read(&vblank
->refcount
));
1764 drm_handle_vblank_events(dev
, pipe
);
1766 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1769 vblank_disable_fn(&vblank
->disable_timer
);
1773 EXPORT_SYMBOL(drm_handle_vblank
);
1776 * drm_crtc_handle_vblank - handle a vblank event
1777 * @crtc: where this event occurred
1779 * Drivers should call this routine in their vblank interrupt handlers to
1780 * update the vblank counter and send any signals that may be pending.
1782 * This is the native KMS version of drm_handle_vblank().
1785 * True if the event was successfully handled, false on failure.
1787 bool drm_crtc_handle_vblank(struct drm_crtc
*crtc
)
1789 return drm_handle_vblank(crtc
->dev
, drm_crtc_index(crtc
));
1791 EXPORT_SYMBOL(drm_crtc_handle_vblank
);
1794 * Get crtc VBLANK count.
1796 * \param dev DRM device
1797 * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
1798 * \param file_priv drm file private for the user's open file descriptor
1801 int drm_crtc_get_sequence_ioctl(struct drm_device
*dev
, void *data
,
1802 struct drm_file
*file_priv
)
1804 struct drm_crtc
*crtc
;
1805 struct drm_vblank_crtc
*vblank
;
1807 struct drm_crtc_get_sequence
*get_seq
= data
;
1809 bool vblank_enabled
;
1812 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1815 if (!dev
->irq_enabled
)
1818 crtc
= drm_crtc_find(dev
, file_priv
, get_seq
->crtc_id
);
1822 pipe
= drm_crtc_index(crtc
);
1824 vblank
= &dev
->vblank
[pipe
];
1825 vblank_enabled
= dev
->vblank_disable_immediate
&& READ_ONCE(vblank
->enabled
);
1827 if (!vblank_enabled
) {
1828 ret
= drm_crtc_vblank_get(crtc
);
1830 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe
, ret
);
1834 drm_modeset_lock(&crtc
->mutex
, NULL
);
1836 get_seq
->active
= crtc
->state
->enable
;
1838 get_seq
->active
= crtc
->enabled
;
1839 drm_modeset_unlock(&crtc
->mutex
);
1840 get_seq
->sequence
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1841 get_seq
->sequence_ns
= ktime_to_ns(now
);
1842 if (!vblank_enabled
)
1843 drm_crtc_vblank_put(crtc
);
1848 * Queue a event for VBLANK sequence
1850 * \param dev DRM device
1851 * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
1852 * \param file_priv drm file private for the user's open file descriptor
1855 int drm_crtc_queue_sequence_ioctl(struct drm_device
*dev
, void *data
,
1856 struct drm_file
*file_priv
)
1858 struct drm_crtc
*crtc
;
1859 struct drm_vblank_crtc
*vblank
;
1861 struct drm_crtc_queue_sequence
*queue_seq
= data
;
1863 struct drm_pending_vblank_event
*e
;
1868 unsigned long spin_flags
;
1870 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1873 if (!dev
->irq_enabled
)
1876 crtc
= drm_crtc_find(dev
, file_priv
, queue_seq
->crtc_id
);
1880 flags
= queue_seq
->flags
;
1881 /* Check valid flag bits */
1882 if (flags
& ~(DRM_CRTC_SEQUENCE_RELATIVE
|
1883 DRM_CRTC_SEQUENCE_NEXT_ON_MISS
))
1886 pipe
= drm_crtc_index(crtc
);
1888 vblank
= &dev
->vblank
[pipe
];
1890 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1894 ret
= drm_crtc_vblank_get(crtc
);
1896 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe
, ret
);
1900 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1901 req_seq
= queue_seq
->sequence
;
1903 if (flags
& DRM_CRTC_SEQUENCE_RELATIVE
)
1906 if ((flags
& DRM_CRTC_SEQUENCE_NEXT_ON_MISS
) && vblank_passed(seq
, req_seq
))
1910 e
->event
.base
.type
= DRM_EVENT_CRTC_SEQUENCE
;
1911 e
->event
.base
.length
= sizeof(e
->event
.seq
);
1912 e
->event
.seq
.user_data
= queue_seq
->user_data
;
1914 spin_lock_irqsave(&dev
->event_lock
, spin_flags
);
1917 * drm_crtc_vblank_off() might have been called after we called
1918 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1919 * vblank disable, so no need for further locking. The reference from
1920 * drm_crtc_vblank_get() protects against vblank disable from another source.
1922 if (!READ_ONCE(vblank
->enabled
)) {
1927 ret
= drm_event_reserve_init_locked(dev
, file_priv
, &e
->base
,
1933 e
->sequence
= req_seq
;
1935 if (vblank_passed(seq
, req_seq
)) {
1936 drm_crtc_vblank_put(crtc
);
1937 send_vblank_event(dev
, e
, seq
, now
);
1938 queue_seq
->sequence
= seq
;
1940 /* drm_handle_vblank_events will call drm_vblank_put */
1941 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
1942 queue_seq
->sequence
= req_seq
;
1945 spin_unlock_irqrestore(&dev
->event_lock
, spin_flags
);
1949 spin_unlock_irqrestore(&dev
->event_lock
, spin_flags
);
1950 drm_crtc_vblank_put(crtc
);