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
);
109 * "No hw counter" fallback implementation of .get_vblank_counter() hook,
110 * if there is no useable hardware frame counter available.
112 static u32
drm_vblank_no_hw_counter(struct drm_device
*dev
, unsigned int pipe
)
114 WARN_ON_ONCE(dev
->max_vblank_count
!= 0);
118 static u32
__get_vblank_counter(struct drm_device
*dev
, unsigned int pipe
)
120 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
121 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
126 if (crtc
->funcs
->get_vblank_counter
)
127 return crtc
->funcs
->get_vblank_counter(crtc
);
130 if (dev
->driver
->get_vblank_counter
)
131 return dev
->driver
->get_vblank_counter(dev
, pipe
);
133 return drm_vblank_no_hw_counter(dev
, pipe
);
137 * Reset the stored timestamp for the current vblank count to correspond
138 * to the last vblank occurred.
140 * Only to be called from drm_crtc_vblank_on().
142 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
143 * device vblank fields.
145 static void drm_reset_vblank_timestamp(struct drm_device
*dev
, unsigned int pipe
)
150 int count
= DRM_TIMESTAMP_MAXRETRIES
;
152 spin_lock(&dev
->vblank_time_lock
);
155 * sample the current counter to avoid random jumps
156 * when drm_vblank_enable() applies the diff
159 cur_vblank
= __get_vblank_counter(dev
, pipe
);
160 rc
= drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, false);
161 } while (cur_vblank
!= __get_vblank_counter(dev
, pipe
) && --count
> 0);
164 * Only reinitialize corresponding vblank timestamp if high-precision query
165 * available and didn't fail. Otherwise reinitialize delayed at next vblank
166 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
172 * +1 to make sure user will never see the same
173 * vblank counter value before and after a modeset
175 store_vblank(dev
, pipe
, 1, t_vblank
, cur_vblank
);
177 spin_unlock(&dev
->vblank_time_lock
);
181 * Call back into the driver to update the appropriate vblank counter
182 * (specified by @pipe). Deal with wraparound, if it occurred, and
183 * update the last read value so we can deal with wraparound on the next
186 * Only necessary when going from off->on, to account for frames we
187 * didn't get an interrupt for.
189 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
190 * device vblank fields.
192 static void drm_update_vblank_count(struct drm_device
*dev
, unsigned int pipe
,
195 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
196 u32 cur_vblank
, diff
;
199 int count
= DRM_TIMESTAMP_MAXRETRIES
;
200 int framedur_ns
= vblank
->framedur_ns
;
203 * Interrupts were disabled prior to this call, so deal with counter
205 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
206 * here if the register is small or we had vblank interrupts off for
209 * We repeat the hardware vblank counter & timestamp query until
210 * we get consistent results. This to prevent races between gpu
211 * updating its hardware counter while we are retrieving the
212 * corresponding vblank timestamp.
215 cur_vblank
= __get_vblank_counter(dev
, pipe
);
216 rc
= drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, in_vblank_irq
);
217 } while (cur_vblank
!= __get_vblank_counter(dev
, pipe
) && --count
> 0);
219 if (dev
->max_vblank_count
!= 0) {
220 /* trust the hw counter when it's around */
221 diff
= (cur_vblank
- vblank
->last
) & dev
->max_vblank_count
;
222 } else if (rc
&& framedur_ns
) {
223 u64 diff_ns
= ktime_to_ns(ktime_sub(t_vblank
, vblank
->time
));
226 * Figure out how many vblanks we've missed based
227 * on the difference in the timestamps and the
228 * frame/field duration.
230 diff
= DIV_ROUND_CLOSEST_ULL(diff_ns
, framedur_ns
);
232 if (diff
== 0 && in_vblank_irq
)
233 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
234 " diff_ns = %lld, framedur_ns = %d)\n",
235 pipe
, (long long) diff_ns
, framedur_ns
);
237 /* some kind of default for drivers w/o accurate vbl timestamping */
238 diff
= in_vblank_irq
? 1 : 0;
242 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
243 * interval? If so then vblank irqs keep running and it will likely
244 * happen that the hardware vblank counter is not trustworthy as it
245 * might reset at some point in that interval and vblank timestamps
246 * are not trustworthy either in that interval. Iow. this can result
247 * in a bogus diff >> 1 which must be avoided as it would cause
248 * random large forward jumps of the software vblank counter.
250 if (diff
> 1 && (vblank
->inmodeset
& 0x2)) {
251 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
252 " due to pre-modeset.\n", pipe
, diff
);
256 DRM_DEBUG_VBL("updating vblank count on crtc %u:"
257 " current=%llu, diff=%u, hw=%u hw_last=%u\n",
258 pipe
, vblank
->count
, diff
, cur_vblank
, vblank
->last
);
261 WARN_ON_ONCE(cur_vblank
!= vblank
->last
);
266 * Only reinitialize corresponding vblank timestamp if high-precision query
267 * available and didn't fail, or we were called from the vblank interrupt.
268 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
269 * for now, to mark the vblanktimestamp as invalid.
271 if (!rc
&& !in_vblank_irq
)
274 store_vblank(dev
, pipe
, diff
, t_vblank
, cur_vblank
);
277 static u64
drm_vblank_count(struct drm_device
*dev
, unsigned int pipe
)
279 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
281 if (WARN_ON(pipe
>= dev
->num_crtcs
))
284 return vblank
->count
;
288 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
289 * @crtc: which counter to retrieve
291 * This function is similar to drm_crtc_vblank_count() but this function
292 * interpolates to handle a race with vblank interrupts using the high precision
293 * timestamping support.
295 * This is mostly useful for hardware that can obtain the scanout position, but
296 * doesn't have a hardware frame counter.
298 u64
drm_crtc_accurate_vblank_count(struct drm_crtc
*crtc
)
300 struct drm_device
*dev
= crtc
->dev
;
301 unsigned int pipe
= drm_crtc_index(crtc
);
305 WARN_ONCE(drm_debug
& DRM_UT_VBL
&& !dev
->driver
->get_vblank_timestamp
,
306 "This function requires support for accurate vblank timestamps.");
308 spin_lock_irqsave(&dev
->vblank_time_lock
, flags
);
310 drm_update_vblank_count(dev
, pipe
, false);
311 vblank
= drm_vblank_count(dev
, pipe
);
313 spin_unlock_irqrestore(&dev
->vblank_time_lock
, flags
);
317 EXPORT_SYMBOL(drm_crtc_accurate_vblank_count
);
319 static void __disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
321 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
322 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
327 if (crtc
->funcs
->disable_vblank
) {
328 crtc
->funcs
->disable_vblank(crtc
);
333 dev
->driver
->disable_vblank(dev
, pipe
);
337 * Disable vblank irq's on crtc, make sure that last vblank count
338 * of hardware and corresponding consistent software vblank counter
339 * are preserved, even if there are any spurious vblank irq's after
342 void drm_vblank_disable_and_save(struct drm_device
*dev
, unsigned int pipe
)
344 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
345 unsigned long irqflags
;
347 assert_spin_locked(&dev
->vbl_lock
);
349 /* Prevent vblank irq processing while disabling vblank irqs,
350 * so no updates of timestamps or count can happen after we've
351 * disabled. Needed to prevent races in case of delayed irq's.
353 spin_lock_irqsave(&dev
->vblank_time_lock
, irqflags
);
356 * Update vblank count and disable vblank interrupts only if the
357 * interrupts were enabled. This avoids calling the ->disable_vblank()
358 * operation in atomic context with the hardware potentially runtime
361 if (!vblank
->enabled
)
365 * Update the count and timestamp to maintain the
366 * appearance that the counter has been ticking all along until
367 * this time. This makes the count account for the entire time
368 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
370 drm_update_vblank_count(dev
, pipe
, false);
371 __disable_vblank(dev
, pipe
);
372 vblank
->enabled
= false;
375 spin_unlock_irqrestore(&dev
->vblank_time_lock
, irqflags
);
378 static void vblank_disable_fn(struct timer_list
*t
)
380 struct drm_vblank_crtc
*vblank
= from_timer(vblank
, t
, disable_timer
);
381 struct drm_device
*dev
= vblank
->dev
;
382 unsigned int pipe
= vblank
->pipe
;
383 unsigned long irqflags
;
385 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
386 if (atomic_read(&vblank
->refcount
) == 0 && vblank
->enabled
) {
387 DRM_DEBUG("disabling vblank on crtc %u\n", pipe
);
388 drm_vblank_disable_and_save(dev
, pipe
);
390 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
393 void drm_vblank_cleanup(struct drm_device
*dev
)
397 /* Bail if the driver didn't call drm_vblank_init() */
398 if (dev
->num_crtcs
== 0)
401 for (pipe
= 0; pipe
< dev
->num_crtcs
; pipe
++) {
402 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
404 WARN_ON(READ_ONCE(vblank
->enabled
) &&
405 drm_core_check_feature(dev
, DRIVER_MODESET
));
407 del_timer_sync(&vblank
->disable_timer
);
416 * drm_vblank_init - initialize vblank support
418 * @num_crtcs: number of CRTCs supported by @dev
420 * This function initializes vblank support for @num_crtcs display pipelines.
421 * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
422 * drivers with a &drm_driver.release callback.
425 * Zero on success or a negative error code on failure.
427 int drm_vblank_init(struct drm_device
*dev
, unsigned int num_crtcs
)
432 spin_lock_init(&dev
->vbl_lock
);
433 spin_lock_init(&dev
->vblank_time_lock
);
435 dev
->num_crtcs
= num_crtcs
;
437 dev
->vblank
= kcalloc(num_crtcs
, sizeof(*dev
->vblank
), GFP_KERNEL
);
441 for (i
= 0; i
< num_crtcs
; i
++) {
442 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[i
];
446 init_waitqueue_head(&vblank
->queue
);
447 timer_setup(&vblank
->disable_timer
, vblank_disable_fn
, 0);
448 seqlock_init(&vblank
->seqlock
);
451 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
453 /* Driver specific high-precision vblank timestamping supported? */
454 if (dev
->driver
->get_vblank_timestamp
)
455 DRM_INFO("Driver supports precise vblank timestamp query.\n");
457 DRM_INFO("No driver support for vblank timestamp query.\n");
459 /* Must have precise timestamping for reliable vblank instant disable */
460 if (dev
->vblank_disable_immediate
&& !dev
->driver
->get_vblank_timestamp
) {
461 dev
->vblank_disable_immediate
= false;
462 DRM_INFO("Setting vblank_disable_immediate to false because "
463 "get_vblank_timestamp == NULL\n");
472 EXPORT_SYMBOL(drm_vblank_init
);
475 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
476 * @crtc: which CRTC's vblank waitqueue to retrieve
478 * This function returns a pointer to the vblank waitqueue for the CRTC.
479 * Drivers can use this to implement vblank waits using wait_event() and related
482 wait_queue_head_t
*drm_crtc_vblank_waitqueue(struct drm_crtc
*crtc
)
484 return &crtc
->dev
->vblank
[drm_crtc_index(crtc
)].queue
;
486 EXPORT_SYMBOL(drm_crtc_vblank_waitqueue
);
490 * drm_calc_timestamping_constants - calculate vblank timestamp constants
491 * @crtc: drm_crtc whose timestamp constants should be updated.
492 * @mode: display mode containing the scanout timings
494 * Calculate and store various constants which are later needed by vblank and
495 * swap-completion timestamping, e.g, by
496 * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true
497 * scanout timing, so they take things like panel scaling or other adjustments
500 void drm_calc_timestamping_constants(struct drm_crtc
*crtc
,
501 const struct drm_display_mode
*mode
)
503 struct drm_device
*dev
= crtc
->dev
;
504 unsigned int pipe
= drm_crtc_index(crtc
);
505 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
506 int linedur_ns
= 0, framedur_ns
= 0;
507 int dotclock
= mode
->crtc_clock
;
512 if (WARN_ON(pipe
>= dev
->num_crtcs
))
515 /* Valid dotclock? */
517 int frame_size
= mode
->crtc_htotal
* mode
->crtc_vtotal
;
520 * Convert scanline length in pixels and video
521 * dot clock to line duration and frame duration
524 linedur_ns
= div_u64((u64
) mode
->crtc_htotal
* 1000000, dotclock
);
525 framedur_ns
= div_u64((u64
) frame_size
* 1000000, dotclock
);
528 * Fields of interlaced scanout modes are only half a frame duration.
530 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
533 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
536 vblank
->linedur_ns
= linedur_ns
;
537 vblank
->framedur_ns
= framedur_ns
;
538 vblank
->hwmode
= *mode
;
540 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
541 crtc
->base
.id
, mode
->crtc_htotal
,
542 mode
->crtc_vtotal
, mode
->crtc_vdisplay
);
543 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
544 crtc
->base
.id
, dotclock
, framedur_ns
, linedur_ns
);
546 EXPORT_SYMBOL(drm_calc_timestamping_constants
);
549 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
551 * @pipe: index of CRTC whose vblank timestamp to retrieve
552 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
553 * On return contains true maximum error of timestamp
554 * @vblank_time: Pointer to time which should receive the timestamp
556 * True when called from drm_crtc_handle_vblank(). Some drivers
557 * need to apply some workarounds for gpu-specific vblank irq quirks
560 * Implements calculation of exact vblank timestamps from given drm_display_mode
561 * timings and current video scanout position of a CRTC. This can be directly
562 * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver
563 * if &drm_driver.get_scanout_position is implemented.
565 * The current implementation only handles standard video modes. For double scan
566 * and interlaced modes the driver is supposed to adjust the hardware mode
567 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
568 * match the scanout position reported.
570 * Note that atomic drivers must call drm_calc_timestamping_constants() before
571 * enabling a CRTC. The atomic helpers already take care of that in
572 * drm_atomic_helper_update_legacy_modeset_state().
576 * Returns true on success, and false on failure, i.e. when no accurate
577 * timestamp could be acquired.
579 bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device
*dev
,
582 ktime_t
*vblank_time
,
585 struct timespec64 ts_etime
, ts_vblank_time
;
586 ktime_t stime
, etime
;
588 struct drm_crtc
*crtc
;
589 const struct drm_display_mode
*mode
;
590 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
592 int delta_ns
, duration_ns
;
594 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
597 crtc
= drm_crtc_from_index(dev
, pipe
);
599 if (pipe
>= dev
->num_crtcs
|| !crtc
) {
600 DRM_ERROR("Invalid crtc %u\n", pipe
);
604 /* Scanout position query not supported? Should not happen. */
605 if (!dev
->driver
->get_scanout_position
) {
606 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
610 if (drm_drv_uses_atomic_modeset(dev
))
611 mode
= &vblank
->hwmode
;
613 mode
= &crtc
->hwmode
;
615 /* If mode timing undefined, just return as no-op:
616 * Happens during initial modesetting of a crtc.
618 if (mode
->crtc_clock
== 0) {
619 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe
);
620 WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev
));
625 /* Get current scanout position with system timestamp.
626 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
627 * if single query takes longer than max_error nanoseconds.
629 * This guarantees a tight bound on maximum error if
630 * code gets preempted or delayed for some reason.
632 for (i
= 0; i
< DRM_TIMESTAMP_MAXRETRIES
; i
++) {
634 * Get vertical and horizontal scanout position vpos, hpos,
635 * and bounding timestamps stime, etime, pre/post query.
637 vbl_status
= dev
->driver
->get_scanout_position(dev
, pipe
,
643 /* Return as no-op if scanout query unsupported or failed. */
645 DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
650 /* Compute uncertainty in timestamp of scanout position query. */
651 duration_ns
= ktime_to_ns(etime
) - ktime_to_ns(stime
);
653 /* Accept result with < max_error nsecs timing uncertainty. */
654 if (duration_ns
<= *max_error
)
658 /* Noisy system timing? */
659 if (i
== DRM_TIMESTAMP_MAXRETRIES
) {
660 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
661 pipe
, duration_ns
/1000, *max_error
/1000, i
);
664 /* Return upper bound of timestamp precision error. */
665 *max_error
= duration_ns
;
667 /* Convert scanout position into elapsed time at raw_time query
668 * since start of scanout at first display scanline. delta_ns
669 * can be negative if start of scanout hasn't happened yet.
671 delta_ns
= div_s64(1000000LL * (vpos
* mode
->crtc_htotal
+ hpos
),
674 /* Subtract time delta from raw timestamp to get final
675 * vblank_time timestamp for end of vblank.
677 *vblank_time
= ktime_sub_ns(etime
, delta_ns
);
679 if ((drm_debug
& DRM_UT_VBL
) == 0)
682 ts_etime
= ktime_to_timespec64(etime
);
683 ts_vblank_time
= ktime_to_timespec64(*vblank_time
);
685 DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
687 (u64
)ts_etime
.tv_sec
, ts_etime
.tv_nsec
/ 1000,
688 (u64
)ts_vblank_time
.tv_sec
, ts_vblank_time
.tv_nsec
/ 1000,
689 duration_ns
/ 1000, i
);
693 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos
);
696 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
699 * @pipe: index of CRTC whose vblank timestamp to retrieve
700 * @tvblank: Pointer to target time which should receive the timestamp
702 * True when called from drm_crtc_handle_vblank(). Some drivers
703 * need to apply some workarounds for gpu-specific vblank irq quirks
706 * Fetches the system timestamp corresponding to the time of the most recent
707 * vblank interval on specified CRTC. May call into kms-driver to
708 * compute the timestamp with a high-precision GPU specific method.
710 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
711 * call, i.e., it isn't very precisely locked to the true vblank.
714 * True if timestamp is considered to be very precise, false otherwise.
717 drm_get_last_vbltimestamp(struct drm_device
*dev
, unsigned int pipe
,
718 ktime_t
*tvblank
, bool in_vblank_irq
)
722 /* Define requested maximum error on timestamps (nanoseconds). */
723 int max_error
= (int) drm_timestamp_precision
* 1000;
725 /* Query driver if possible and precision timestamping enabled. */
726 if (dev
->driver
->get_vblank_timestamp
&& (max_error
> 0))
727 ret
= dev
->driver
->get_vblank_timestamp(dev
, pipe
, &max_error
,
728 tvblank
, in_vblank_irq
);
730 /* GPU high precision timestamp query unsupported or failed.
731 * Return current monotonic/gettimeofday timestamp as best estimate.
734 *tvblank
= ktime_get();
740 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
741 * @crtc: which counter to retrieve
743 * Fetches the "cooked" vblank count value that represents the number of
744 * vblank events since the system was booted, including lost events due to
745 * modesetting activity. Note that this timer isn't correct against a racing
746 * vblank interrupt (since it only reports the software vblank counter), see
747 * drm_crtc_accurate_vblank_count() for such use-cases.
750 * The software vblank counter.
752 u64
drm_crtc_vblank_count(struct drm_crtc
*crtc
)
754 return drm_vblank_count(crtc
->dev
, drm_crtc_index(crtc
));
756 EXPORT_SYMBOL(drm_crtc_vblank_count
);
759 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
760 * system timestamp corresponding to that vblank counter value.
762 * @pipe: index of CRTC whose counter to retrieve
763 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
765 * Fetches the "cooked" vblank count value that represents the number of
766 * vblank events since the system was booted, including lost events due to
767 * modesetting activity. Returns corresponding system timestamp of the time
768 * of the vblank interval that corresponds to the current vblank counter value.
770 * This is the legacy version of drm_crtc_vblank_count_and_time().
772 static u64
drm_vblank_count_and_time(struct drm_device
*dev
, unsigned int pipe
,
775 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
779 if (WARN_ON(pipe
>= dev
->num_crtcs
)) {
785 seq
= read_seqbegin(&vblank
->seqlock
);
786 vblank_count
= vblank
->count
;
787 *vblanktime
= vblank
->time
;
788 } while (read_seqretry(&vblank
->seqlock
, seq
));
794 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
795 * and the system timestamp corresponding to that vblank counter value
796 * @crtc: which counter to retrieve
797 * @vblanktime: Pointer to time to receive the vblank timestamp.
799 * Fetches the "cooked" vblank count value that represents the number of
800 * vblank events since the system was booted, including lost events due to
801 * modesetting activity. Returns corresponding system timestamp of the time
802 * of the vblank interval that corresponds to the current vblank counter value.
804 u64
drm_crtc_vblank_count_and_time(struct drm_crtc
*crtc
,
807 return drm_vblank_count_and_time(crtc
->dev
, drm_crtc_index(crtc
),
810 EXPORT_SYMBOL(drm_crtc_vblank_count_and_time
);
812 static void send_vblank_event(struct drm_device
*dev
,
813 struct drm_pending_vblank_event
*e
,
814 u64 seq
, ktime_t now
)
816 struct timespec64 tv
;
818 switch (e
->event
.base
.type
) {
819 case DRM_EVENT_VBLANK
:
820 case DRM_EVENT_FLIP_COMPLETE
:
821 tv
= ktime_to_timespec64(now
);
822 e
->event
.vbl
.sequence
= seq
;
824 * e->event is a user space structure, with hardcoded unsigned
825 * 32-bit seconds/microseconds. This is safe as we always use
826 * monotonic timestamps since linux-4.15
828 e
->event
.vbl
.tv_sec
= tv
.tv_sec
;
829 e
->event
.vbl
.tv_usec
= tv
.tv_nsec
/ 1000;
831 case DRM_EVENT_CRTC_SEQUENCE
:
833 e
->event
.seq
.sequence
= seq
;
834 e
->event
.seq
.time_ns
= ktime_to_ns(now
);
837 trace_drm_vblank_event_delivered(e
->base
.file_priv
, e
->pipe
, seq
);
838 drm_send_event_locked(dev
, &e
->base
);
842 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
843 * @crtc: the source CRTC of the vblank event
844 * @e: the event to send
846 * A lot of drivers need to generate vblank events for the very next vblank
847 * interrupt. For example when the page flip interrupt happens when the page
848 * flip gets armed, but not when it actually executes within the next vblank
849 * period. This helper function implements exactly the required vblank arming
852 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
853 * atomic commit must ensure that the next vblank happens at exactly the same
854 * time as the atomic commit is committed to the hardware. This function itself
855 * does **not** protect against the next vblank interrupt racing with either this
856 * function call or the atomic commit operation. A possible sequence could be:
858 * 1. Driver commits new hardware state into vblank-synchronized registers.
859 * 2. A vblank happens, committing the hardware state. Also the corresponding
860 * vblank interrupt is fired off and fully processed by the interrupt
862 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
863 * 4. The event is only send out for the next vblank, which is wrong.
865 * An equivalent race can happen when the driver calls
866 * drm_crtc_arm_vblank_event() before writing out the new hardware state.
868 * The only way to make this work safely is to prevent the vblank from firing
869 * (and the hardware from committing anything else) until the entire atomic
870 * commit sequence has run to completion. If the hardware does not have such a
871 * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
872 * Instead drivers need to manually send out the event from their interrupt
873 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
874 * possible race with the hardware committing the atomic update.
876 * Caller must hold a vblank reference for the event @e acquired by a
877 * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
879 void drm_crtc_arm_vblank_event(struct drm_crtc
*crtc
,
880 struct drm_pending_vblank_event
*e
)
882 struct drm_device
*dev
= crtc
->dev
;
883 unsigned int pipe
= drm_crtc_index(crtc
);
885 assert_spin_locked(&dev
->event_lock
);
888 e
->sequence
= drm_crtc_accurate_vblank_count(crtc
) + 1;
889 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
891 EXPORT_SYMBOL(drm_crtc_arm_vblank_event
);
894 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
895 * @crtc: the source CRTC of the vblank event
896 * @e: the event to send
898 * Updates sequence # and timestamp on event for the most recently processed
899 * vblank, and sends it to userspace. Caller must hold event lock.
901 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
902 * situation, especially to send out events for atomic commit operations.
904 void drm_crtc_send_vblank_event(struct drm_crtc
*crtc
,
905 struct drm_pending_vblank_event
*e
)
907 struct drm_device
*dev
= crtc
->dev
;
909 unsigned int pipe
= drm_crtc_index(crtc
);
912 if (dev
->num_crtcs
> 0) {
913 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
920 send_vblank_event(dev
, e
, seq
, now
);
922 EXPORT_SYMBOL(drm_crtc_send_vblank_event
);
924 static int __enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
926 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
927 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
932 if (crtc
->funcs
->enable_vblank
)
933 return crtc
->funcs
->enable_vblank(crtc
);
936 return dev
->driver
->enable_vblank(dev
, pipe
);
939 static int drm_vblank_enable(struct drm_device
*dev
, unsigned int pipe
)
941 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
944 assert_spin_locked(&dev
->vbl_lock
);
946 spin_lock(&dev
->vblank_time_lock
);
948 if (!vblank
->enabled
) {
950 * Enable vblank irqs under vblank_time_lock protection.
951 * All vblank count & timestamp updates are held off
952 * until we are done reinitializing master counter and
953 * timestamps. Filtercode in drm_handle_vblank() will
954 * prevent double-accounting of same vblank interval.
956 ret
= __enable_vblank(dev
, pipe
);
957 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe
, ret
);
959 atomic_dec(&vblank
->refcount
);
961 drm_update_vblank_count(dev
, pipe
, 0);
962 /* drm_update_vblank_count() includes a wmb so we just
963 * need to ensure that the compiler emits the write
964 * to mark the vblank as enabled after the call
965 * to drm_update_vblank_count().
967 WRITE_ONCE(vblank
->enabled
, true);
971 spin_unlock(&dev
->vblank_time_lock
);
976 static int drm_vblank_get(struct drm_device
*dev
, unsigned int pipe
)
978 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
979 unsigned long irqflags
;
985 if (WARN_ON(pipe
>= dev
->num_crtcs
))
988 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
989 /* Going from 0->1 means we have to enable interrupts again */
990 if (atomic_add_return(1, &vblank
->refcount
) == 1) {
991 ret
= drm_vblank_enable(dev
, pipe
);
993 if (!vblank
->enabled
) {
994 atomic_dec(&vblank
->refcount
);
998 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1004 * drm_crtc_vblank_get - get a reference count on vblank events
1005 * @crtc: which CRTC to own
1007 * Acquire a reference count on vblank events to avoid having them disabled
1011 * Zero on success or a negative error code on failure.
1013 int drm_crtc_vblank_get(struct drm_crtc
*crtc
)
1015 return drm_vblank_get(crtc
->dev
, drm_crtc_index(crtc
));
1017 EXPORT_SYMBOL(drm_crtc_vblank_get
);
1019 static void drm_vblank_put(struct drm_device
*dev
, unsigned int pipe
)
1021 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1023 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1026 if (WARN_ON(atomic_read(&vblank
->refcount
) == 0))
1029 /* Last user schedules interrupt disable */
1030 if (atomic_dec_and_test(&vblank
->refcount
)) {
1031 if (drm_vblank_offdelay
== 0)
1033 else if (drm_vblank_offdelay
< 0)
1034 vblank_disable_fn(&vblank
->disable_timer
);
1035 else if (!dev
->vblank_disable_immediate
)
1036 mod_timer(&vblank
->disable_timer
,
1037 jiffies
+ ((drm_vblank_offdelay
* HZ
)/1000));
1042 * drm_crtc_vblank_put - give up ownership of vblank events
1043 * @crtc: which counter to give up
1045 * Release ownership of a given vblank counter, turning off interrupts
1046 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1048 void drm_crtc_vblank_put(struct drm_crtc
*crtc
)
1050 drm_vblank_put(crtc
->dev
, drm_crtc_index(crtc
));
1052 EXPORT_SYMBOL(drm_crtc_vblank_put
);
1055 * drm_wait_one_vblank - wait for one vblank
1059 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1060 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1061 * due to lack of driver support or because the crtc is off.
1063 * This is the legacy version of drm_crtc_wait_one_vblank().
1065 void drm_wait_one_vblank(struct drm_device
*dev
, unsigned int pipe
)
1067 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1071 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1074 ret
= drm_vblank_get(dev
, pipe
);
1075 if (WARN(ret
, "vblank not available on crtc %i, ret=%i\n", pipe
, ret
))
1078 last
= drm_vblank_count(dev
, pipe
);
1080 ret
= wait_event_timeout(vblank
->queue
,
1081 last
!= drm_vblank_count(dev
, pipe
),
1082 msecs_to_jiffies(100));
1084 WARN(ret
== 0, "vblank wait timed out on crtc %i\n", pipe
);
1086 drm_vblank_put(dev
, pipe
);
1088 EXPORT_SYMBOL(drm_wait_one_vblank
);
1091 * drm_crtc_wait_one_vblank - wait for one vblank
1094 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1095 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1096 * due to lack of driver support or because the crtc is off.
1098 void drm_crtc_wait_one_vblank(struct drm_crtc
*crtc
)
1100 drm_wait_one_vblank(crtc
->dev
, drm_crtc_index(crtc
));
1102 EXPORT_SYMBOL(drm_crtc_wait_one_vblank
);
1105 * drm_crtc_vblank_off - disable vblank events on a CRTC
1106 * @crtc: CRTC in question
1108 * Drivers can use this function to shut down the vblank interrupt handling when
1109 * disabling a crtc. This function ensures that the latest vblank frame count is
1110 * stored so that drm_vblank_on can restore it again.
1112 * Drivers must use this function when the hardware vblank counter can get
1113 * reset, e.g. when suspending or disabling the @crtc in general.
1115 void drm_crtc_vblank_off(struct drm_crtc
*crtc
)
1117 struct drm_device
*dev
= crtc
->dev
;
1118 unsigned int pipe
= drm_crtc_index(crtc
);
1119 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1120 struct drm_pending_vblank_event
*e
, *t
;
1123 unsigned long irqflags
;
1126 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1129 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
1131 spin_lock(&dev
->vbl_lock
);
1132 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1133 pipe
, vblank
->enabled
, vblank
->inmodeset
);
1135 /* Avoid redundant vblank disables without previous
1136 * drm_crtc_vblank_on(). */
1137 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
) || !vblank
->inmodeset
)
1138 drm_vblank_disable_and_save(dev
, pipe
);
1140 wake_up(&vblank
->queue
);
1143 * Prevent subsequent drm_vblank_get() from re-enabling
1144 * the vblank interrupt by bumping the refcount.
1146 if (!vblank
->inmodeset
) {
1147 atomic_inc(&vblank
->refcount
);
1148 vblank
->inmodeset
= 1;
1150 spin_unlock(&dev
->vbl_lock
);
1152 /* Send any queued vblank events, lest the natives grow disquiet */
1153 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1155 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1156 if (e
->pipe
!= pipe
)
1158 DRM_DEBUG("Sending premature vblank event on disable: "
1159 "wanted %llu, current %llu\n",
1161 list_del(&e
->base
.link
);
1162 drm_vblank_put(dev
, pipe
);
1163 send_vblank_event(dev
, e
, seq
, now
);
1165 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1167 /* Will be reset by the modeset helpers when re-enabling the crtc by
1168 * calling drm_calc_timestamping_constants(). */
1169 vblank
->hwmode
.crtc_clock
= 0;
1171 EXPORT_SYMBOL(drm_crtc_vblank_off
);
1174 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1175 * @crtc: CRTC in question
1177 * Drivers can use this function to reset the vblank state to off at load time.
1178 * Drivers should use this together with the drm_crtc_vblank_off() and
1179 * drm_crtc_vblank_on() functions. The difference compared to
1180 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1181 * and hence doesn't need to call any driver hooks.
1183 * This is useful for recovering driver state e.g. on driver load, or on resume.
1185 void drm_crtc_vblank_reset(struct drm_crtc
*crtc
)
1187 struct drm_device
*dev
= crtc
->dev
;
1188 unsigned long irqflags
;
1189 unsigned int pipe
= drm_crtc_index(crtc
);
1190 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1192 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1194 * Prevent subsequent drm_vblank_get() from enabling the vblank
1195 * interrupt by bumping the refcount.
1197 if (!vblank
->inmodeset
) {
1198 atomic_inc(&vblank
->refcount
);
1199 vblank
->inmodeset
= 1;
1201 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1203 WARN_ON(!list_empty(&dev
->vblank_event_list
));
1205 EXPORT_SYMBOL(drm_crtc_vblank_reset
);
1208 * drm_crtc_vblank_on - enable vblank events on a CRTC
1209 * @crtc: CRTC in question
1211 * This functions restores the vblank interrupt state captured with
1212 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1213 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1214 * unbalanced and so can also be unconditionally called in driver load code to
1215 * reflect the current hardware state of the crtc.
1217 void drm_crtc_vblank_on(struct drm_crtc
*crtc
)
1219 struct drm_device
*dev
= crtc
->dev
;
1220 unsigned int pipe
= drm_crtc_index(crtc
);
1221 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1222 unsigned long irqflags
;
1224 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1227 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1228 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1229 pipe
, vblank
->enabled
, vblank
->inmodeset
);
1231 /* Drop our private "prevent drm_vblank_get" refcount */
1232 if (vblank
->inmodeset
) {
1233 atomic_dec(&vblank
->refcount
);
1234 vblank
->inmodeset
= 0;
1237 drm_reset_vblank_timestamp(dev
, pipe
);
1240 * re-enable interrupts if there are users left, or the
1241 * user wishes vblank interrupts to be enabled all the time.
1243 if (atomic_read(&vblank
->refcount
) != 0 || drm_vblank_offdelay
== 0)
1244 WARN_ON(drm_vblank_enable(dev
, pipe
));
1245 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1247 EXPORT_SYMBOL(drm_crtc_vblank_on
);
1250 * drm_vblank_restore - estimate missed vblanks and update vblank count.
1254 * Power manamement features can cause frame counter resets between vblank
1255 * disable and enable. Drivers can use this function in their
1256 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1257 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1260 * This function is the legacy version of drm_crtc_vblank_restore().
1262 void drm_vblank_restore(struct drm_device
*dev
, unsigned int pipe
)
1265 struct drm_vblank_crtc
*vblank
;
1268 u32 cur_vblank
, diff
= 1;
1269 int count
= DRM_TIMESTAMP_MAXRETRIES
;
1271 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1274 assert_spin_locked(&dev
->vbl_lock
);
1275 assert_spin_locked(&dev
->vblank_time_lock
);
1277 vblank
= &dev
->vblank
[pipe
];
1278 WARN_ONCE((drm_debug
& DRM_UT_VBL
) && !vblank
->framedur_ns
,
1279 "Cannot compute missed vblanks without frame duration\n");
1280 framedur_ns
= vblank
->framedur_ns
;
1283 cur_vblank
= __get_vblank_counter(dev
, pipe
);
1284 drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, false);
1285 } while (cur_vblank
!= __get_vblank_counter(dev
, pipe
) && --count
> 0);
1287 diff_ns
= ktime_to_ns(ktime_sub(t_vblank
, vblank
->time
));
1289 diff
= DIV_ROUND_CLOSEST_ULL(diff_ns
, framedur_ns
);
1292 DRM_DEBUG_VBL("missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1293 diff
, diff_ns
, framedur_ns
, cur_vblank
- vblank
->last
);
1294 store_vblank(dev
, pipe
, diff
, t_vblank
, cur_vblank
);
1296 EXPORT_SYMBOL(drm_vblank_restore
);
1299 * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
1300 * @crtc: CRTC in question
1302 * Power manamement features can cause frame counter resets between vblank
1303 * disable and enable. Drivers can use this function in their
1304 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1305 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1308 void drm_crtc_vblank_restore(struct drm_crtc
*crtc
)
1310 drm_vblank_restore(crtc
->dev
, drm_crtc_index(crtc
));
1312 EXPORT_SYMBOL(drm_crtc_vblank_restore
);
1314 static void drm_legacy_vblank_pre_modeset(struct drm_device
*dev
,
1317 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1319 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1320 if (!dev
->num_crtcs
)
1323 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1327 * To avoid all the problems that might happen if interrupts
1328 * were enabled/disabled around or between these calls, we just
1329 * have the kernel take a reference on the CRTC (just once though
1330 * to avoid corrupting the count if multiple, mismatch calls occur),
1331 * so that interrupts remain enabled in the interim.
1333 if (!vblank
->inmodeset
) {
1334 vblank
->inmodeset
= 0x1;
1335 if (drm_vblank_get(dev
, pipe
) == 0)
1336 vblank
->inmodeset
|= 0x2;
1340 static void drm_legacy_vblank_post_modeset(struct drm_device
*dev
,
1343 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1344 unsigned long irqflags
;
1346 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1347 if (!dev
->num_crtcs
)
1350 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1353 if (vblank
->inmodeset
) {
1354 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1355 drm_reset_vblank_timestamp(dev
, pipe
);
1356 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1358 if (vblank
->inmodeset
& 0x2)
1359 drm_vblank_put(dev
, pipe
);
1361 vblank
->inmodeset
= 0;
1365 int drm_legacy_modeset_ctl_ioctl(struct drm_device
*dev
, void *data
,
1366 struct drm_file
*file_priv
)
1368 struct drm_modeset_ctl
*modeset
= data
;
1371 /* If drm_vblank_init() hasn't been called yet, just no-op */
1372 if (!dev
->num_crtcs
)
1375 /* KMS drivers handle this internally */
1376 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1379 pipe
= modeset
->crtc
;
1380 if (pipe
>= dev
->num_crtcs
)
1383 switch (modeset
->cmd
) {
1384 case _DRM_PRE_MODESET
:
1385 drm_legacy_vblank_pre_modeset(dev
, pipe
);
1387 case _DRM_POST_MODESET
:
1388 drm_legacy_vblank_post_modeset(dev
, pipe
);
1397 static inline bool vblank_passed(u64 seq
, u64 ref
)
1399 return (seq
- ref
) <= (1 << 23);
1402 static int drm_queue_vblank_event(struct drm_device
*dev
, unsigned int pipe
,
1404 union drm_wait_vblank
*vblwait
,
1405 struct drm_file
*file_priv
)
1407 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1408 struct drm_pending_vblank_event
*e
;
1410 unsigned long flags
;
1414 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1421 e
->event
.base
.type
= DRM_EVENT_VBLANK
;
1422 e
->event
.base
.length
= sizeof(e
->event
.vbl
);
1423 e
->event
.vbl
.user_data
= vblwait
->request
.signal
;
1424 e
->event
.vbl
.crtc_id
= 0;
1425 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1426 struct drm_crtc
*crtc
= drm_crtc_from_index(dev
, pipe
);
1428 e
->event
.vbl
.crtc_id
= crtc
->base
.id
;
1431 spin_lock_irqsave(&dev
->event_lock
, flags
);
1434 * drm_crtc_vblank_off() might have been called after we called
1435 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1436 * vblank disable, so no need for further locking. The reference from
1437 * drm_vblank_get() protects against vblank disable from another source.
1439 if (!READ_ONCE(vblank
->enabled
)) {
1444 ret
= drm_event_reserve_init_locked(dev
, file_priv
, &e
->base
,
1450 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1452 DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n",
1453 req_seq
, seq
, pipe
);
1455 trace_drm_vblank_event_queued(file_priv
, pipe
, req_seq
);
1457 e
->sequence
= req_seq
;
1458 if (vblank_passed(seq
, req_seq
)) {
1459 drm_vblank_put(dev
, pipe
);
1460 send_vblank_event(dev
, e
, seq
, now
);
1461 vblwait
->reply
.sequence
= seq
;
1463 /* drm_handle_vblank_events will call drm_vblank_put */
1464 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
1465 vblwait
->reply
.sequence
= req_seq
;
1468 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1473 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1476 drm_vblank_put(dev
, pipe
);
1480 static bool drm_wait_vblank_is_query(union drm_wait_vblank
*vblwait
)
1482 if (vblwait
->request
.sequence
)
1485 return _DRM_VBLANK_RELATIVE
==
1486 (vblwait
->request
.type
& (_DRM_VBLANK_TYPES_MASK
|
1488 _DRM_VBLANK_NEXTONMISS
));
1492 * Widen a 32-bit param to 64-bits.
1494 * \param narrow 32-bit value (missing upper 32 bits)
1495 * \param near 64-bit value that should be 'close' to near
1497 * This function returns a 64-bit value using the lower 32-bits from
1498 * 'narrow' and constructing the upper 32-bits so that the result is
1499 * as close as possible to 'near'.
1502 static u64
widen_32_to_64(u32 narrow
, u64 near
)
1504 return near
+ (s32
) (narrow
- near
);
1507 static void drm_wait_vblank_reply(struct drm_device
*dev
, unsigned int pipe
,
1508 struct drm_wait_vblank_reply
*reply
)
1511 struct timespec64 ts
;
1514 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
1515 * to store the seconds. This is safe as we always use monotonic
1516 * timestamps since linux-4.15.
1518 reply
->sequence
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1519 ts
= ktime_to_timespec64(now
);
1520 reply
->tval_sec
= (u32
)ts
.tv_sec
;
1521 reply
->tval_usec
= ts
.tv_nsec
/ 1000;
1524 int drm_wait_vblank_ioctl(struct drm_device
*dev
, void *data
,
1525 struct drm_file
*file_priv
)
1527 struct drm_crtc
*crtc
;
1528 struct drm_vblank_crtc
*vblank
;
1529 union drm_wait_vblank
*vblwait
= data
;
1532 unsigned int pipe_index
;
1533 unsigned int flags
, pipe
, high_pipe
;
1535 if (!dev
->irq_enabled
)
1538 if (vblwait
->request
.type
& _DRM_VBLANK_SIGNAL
)
1541 if (vblwait
->request
.type
&
1542 ~(_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1543 _DRM_VBLANK_HIGH_CRTC_MASK
)) {
1544 DRM_DEBUG("Unsupported type value 0x%x, supported mask 0x%x\n",
1545 vblwait
->request
.type
,
1546 (_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1547 _DRM_VBLANK_HIGH_CRTC_MASK
));
1551 flags
= vblwait
->request
.type
& _DRM_VBLANK_FLAGS_MASK
;
1552 high_pipe
= (vblwait
->request
.type
& _DRM_VBLANK_HIGH_CRTC_MASK
);
1554 pipe_index
= high_pipe
>> _DRM_VBLANK_HIGH_CRTC_SHIFT
;
1556 pipe_index
= flags
& _DRM_VBLANK_SECONDARY
? 1 : 0;
1558 /* Convert lease-relative crtc index into global crtc index */
1559 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1561 drm_for_each_crtc(crtc
, dev
) {
1562 if (drm_lease_held(file_priv
, crtc
->base
.id
)) {
1563 if (pipe_index
== 0)
1573 if (pipe
>= dev
->num_crtcs
)
1576 vblank
= &dev
->vblank
[pipe
];
1578 /* If the counter is currently enabled and accurate, short-circuit
1579 * queries to return the cached timestamp of the last vblank.
1581 if (dev
->vblank_disable_immediate
&&
1582 drm_wait_vblank_is_query(vblwait
) &&
1583 READ_ONCE(vblank
->enabled
)) {
1584 drm_wait_vblank_reply(dev
, pipe
, &vblwait
->reply
);
1588 ret
= drm_vblank_get(dev
, pipe
);
1590 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe
, ret
);
1593 seq
= drm_vblank_count(dev
, pipe
);
1595 switch (vblwait
->request
.type
& _DRM_VBLANK_TYPES_MASK
) {
1596 case _DRM_VBLANK_RELATIVE
:
1597 req_seq
= seq
+ vblwait
->request
.sequence
;
1598 vblwait
->request
.sequence
= req_seq
;
1599 vblwait
->request
.type
&= ~_DRM_VBLANK_RELATIVE
;
1601 case _DRM_VBLANK_ABSOLUTE
:
1602 req_seq
= widen_32_to_64(vblwait
->request
.sequence
, seq
);
1609 if ((flags
& _DRM_VBLANK_NEXTONMISS
) &&
1610 vblank_passed(seq
, req_seq
)) {
1612 vblwait
->request
.type
&= ~_DRM_VBLANK_NEXTONMISS
;
1613 vblwait
->request
.sequence
= req_seq
;
1616 if (flags
& _DRM_VBLANK_EVENT
) {
1617 /* must hold on to the vblank ref until the event fires
1618 * drm_vblank_put will be called asynchronously
1620 return drm_queue_vblank_event(dev
, pipe
, req_seq
, vblwait
, file_priv
);
1623 if (req_seq
!= seq
) {
1624 DRM_DEBUG("waiting on vblank count %llu, crtc %u\n",
1626 DRM_WAIT_ON(ret
, vblank
->queue
, 3 * HZ
,
1627 vblank_passed(drm_vblank_count(dev
, pipe
),
1629 !READ_ONCE(vblank
->enabled
));
1632 if (ret
!= -EINTR
) {
1633 drm_wait_vblank_reply(dev
, pipe
, &vblwait
->reply
);
1635 DRM_DEBUG("crtc %d returning %u to client\n",
1636 pipe
, vblwait
->reply
.sequence
);
1638 DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe
);
1642 drm_vblank_put(dev
, pipe
);
1646 static void drm_handle_vblank_events(struct drm_device
*dev
, unsigned int pipe
)
1648 struct drm_pending_vblank_event
*e
, *t
;
1652 assert_spin_locked(&dev
->event_lock
);
1654 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1656 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1657 if (e
->pipe
!= pipe
)
1659 if (!vblank_passed(seq
, e
->sequence
))
1662 DRM_DEBUG("vblank event on %llu, current %llu\n",
1665 list_del(&e
->base
.link
);
1666 drm_vblank_put(dev
, pipe
);
1667 send_vblank_event(dev
, e
, seq
, now
);
1670 trace_drm_vblank_event(pipe
, seq
);
1674 * drm_handle_vblank - handle a vblank event
1676 * @pipe: index of CRTC where this event occurred
1678 * Drivers should call this routine in their vblank interrupt handlers to
1679 * update the vblank counter and send any signals that may be pending.
1681 * This is the legacy version of drm_crtc_handle_vblank().
1683 bool drm_handle_vblank(struct drm_device
*dev
, unsigned int pipe
)
1685 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1686 unsigned long irqflags
;
1689 if (WARN_ON_ONCE(!dev
->num_crtcs
))
1692 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1695 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
1697 /* Need timestamp lock to prevent concurrent execution with
1698 * vblank enable/disable, as this would cause inconsistent
1699 * or corrupted timestamps and vblank counts.
1701 spin_lock(&dev
->vblank_time_lock
);
1703 /* Vblank irq handling disabled. Nothing to do. */
1704 if (!vblank
->enabled
) {
1705 spin_unlock(&dev
->vblank_time_lock
);
1706 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1710 drm_update_vblank_count(dev
, pipe
, true);
1712 spin_unlock(&dev
->vblank_time_lock
);
1714 wake_up(&vblank
->queue
);
1716 /* With instant-off, we defer disabling the interrupt until after
1717 * we finish processing the following vblank after all events have
1718 * been signaled. The disable has to be last (after
1719 * drm_handle_vblank_events) so that the timestamp is always accurate.
1721 disable_irq
= (dev
->vblank_disable_immediate
&&
1722 drm_vblank_offdelay
> 0 &&
1723 !atomic_read(&vblank
->refcount
));
1725 drm_handle_vblank_events(dev
, pipe
);
1727 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1730 vblank_disable_fn(&vblank
->disable_timer
);
1734 EXPORT_SYMBOL(drm_handle_vblank
);
1737 * drm_crtc_handle_vblank - handle a vblank event
1738 * @crtc: where this event occurred
1740 * Drivers should call this routine in their vblank interrupt handlers to
1741 * update the vblank counter and send any signals that may be pending.
1743 * This is the native KMS version of drm_handle_vblank().
1746 * True if the event was successfully handled, false on failure.
1748 bool drm_crtc_handle_vblank(struct drm_crtc
*crtc
)
1750 return drm_handle_vblank(crtc
->dev
, drm_crtc_index(crtc
));
1752 EXPORT_SYMBOL(drm_crtc_handle_vblank
);
1755 * Get crtc VBLANK count.
1757 * \param dev DRM device
1758 * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
1759 * \param file_priv drm file private for the user's open file descriptor
1762 int drm_crtc_get_sequence_ioctl(struct drm_device
*dev
, void *data
,
1763 struct drm_file
*file_priv
)
1765 struct drm_crtc
*crtc
;
1766 struct drm_vblank_crtc
*vblank
;
1768 struct drm_crtc_get_sequence
*get_seq
= data
;
1770 bool vblank_enabled
;
1773 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1776 if (!dev
->irq_enabled
)
1779 crtc
= drm_crtc_find(dev
, file_priv
, get_seq
->crtc_id
);
1783 pipe
= drm_crtc_index(crtc
);
1785 vblank
= &dev
->vblank
[pipe
];
1786 vblank_enabled
= dev
->vblank_disable_immediate
&& READ_ONCE(vblank
->enabled
);
1788 if (!vblank_enabled
) {
1789 ret
= drm_crtc_vblank_get(crtc
);
1791 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe
, ret
);
1795 drm_modeset_lock(&crtc
->mutex
, NULL
);
1797 get_seq
->active
= crtc
->state
->enable
;
1799 get_seq
->active
= crtc
->enabled
;
1800 drm_modeset_unlock(&crtc
->mutex
);
1801 get_seq
->sequence
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1802 get_seq
->sequence_ns
= ktime_to_ns(now
);
1803 if (!vblank_enabled
)
1804 drm_crtc_vblank_put(crtc
);
1809 * Queue a event for VBLANK sequence
1811 * \param dev DRM device
1812 * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
1813 * \param file_priv drm file private for the user's open file descriptor
1816 int drm_crtc_queue_sequence_ioctl(struct drm_device
*dev
, void *data
,
1817 struct drm_file
*file_priv
)
1819 struct drm_crtc
*crtc
;
1820 struct drm_vblank_crtc
*vblank
;
1822 struct drm_crtc_queue_sequence
*queue_seq
= data
;
1824 struct drm_pending_vblank_event
*e
;
1829 unsigned long spin_flags
;
1831 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1834 if (!dev
->irq_enabled
)
1837 crtc
= drm_crtc_find(dev
, file_priv
, queue_seq
->crtc_id
);
1841 flags
= queue_seq
->flags
;
1842 /* Check valid flag bits */
1843 if (flags
& ~(DRM_CRTC_SEQUENCE_RELATIVE
|
1844 DRM_CRTC_SEQUENCE_NEXT_ON_MISS
))
1847 pipe
= drm_crtc_index(crtc
);
1849 vblank
= &dev
->vblank
[pipe
];
1851 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1855 ret
= drm_crtc_vblank_get(crtc
);
1857 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe
, ret
);
1861 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1862 req_seq
= queue_seq
->sequence
;
1864 if (flags
& DRM_CRTC_SEQUENCE_RELATIVE
)
1867 if ((flags
& DRM_CRTC_SEQUENCE_NEXT_ON_MISS
) && vblank_passed(seq
, req_seq
))
1871 e
->event
.base
.type
= DRM_EVENT_CRTC_SEQUENCE
;
1872 e
->event
.base
.length
= sizeof(e
->event
.seq
);
1873 e
->event
.seq
.user_data
= queue_seq
->user_data
;
1875 spin_lock_irqsave(&dev
->event_lock
, spin_flags
);
1878 * drm_crtc_vblank_off() might have been called after we called
1879 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1880 * vblank disable, so no need for further locking. The reference from
1881 * drm_crtc_vblank_get() protects against vblank disable from another source.
1883 if (!READ_ONCE(vblank
->enabled
)) {
1888 ret
= drm_event_reserve_init_locked(dev
, file_priv
, &e
->base
,
1894 e
->sequence
= req_seq
;
1896 if (vblank_passed(seq
, req_seq
)) {
1897 drm_crtc_vblank_put(crtc
);
1898 send_vblank_event(dev
, e
, seq
, now
);
1899 queue_seq
->sequence
= seq
;
1901 /* drm_handle_vblank_events will call drm_vblank_put */
1902 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
1903 queue_seq
->sequence
= req_seq
;
1906 spin_unlock_irqrestore(&dev
->event_lock
, spin_flags
);
1910 spin_unlock_irqrestore(&dev
->event_lock
, spin_flags
);
1911 drm_crtc_vblank_put(crtc
);