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 <linux/export.h>
28 #include <linux/moduleparam.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_drv.h>
32 #include <drm/drm_framebuffer.h>
33 #include <drm/drm_managed.h>
34 #include <drm/drm_modeset_helper_vtables.h>
35 #include <drm/drm_print.h>
36 #include <drm/drm_vblank.h>
38 #include "drm_internal.h"
39 #include "drm_trace.h"
42 * DOC: vblank handling
44 * From the computer's perspective, every time the monitor displays
45 * a new frame the scanout engine has "scanned out" the display image
46 * from top to bottom, one row of pixels at a time. The current row
47 * of pixels is referred to as the current scanline.
49 * In addition to the display's visible area, there's usually a couple of
50 * extra scanlines which aren't actually displayed on the screen.
51 * These extra scanlines don't contain image data and are occasionally used
52 * for features like audio and infoframes. The region made up of these
53 * scanlines is referred to as the vertical blanking region, or vblank for
56 * For historical reference, the vertical blanking period was designed to
57 * give the electron gun (on CRTs) enough time to move back to the top of
58 * the screen to start scanning out the next frame. Similar for horizontal
59 * blanking periods. They were designed to give the electron gun enough
60 * time to move back to the other side of the screen to start scanning the
66 * physical → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
71 * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓|
72 * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| ← Scanline,
73 * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓| updates the
83 * vertical |⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽| ← display
84 * blanking ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
85 * region → ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
86 * ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
87 * start of → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
90 * "Physical top of display" is the reference point for the high-precision/
91 * corrected timestamp.
93 * On a lot of display hardware, programming needs to take effect during the
94 * vertical blanking period so that settings like gamma, the image buffer
95 * buffer to be scanned out, etc. can safely be changed without showing
96 * any visual artifacts on the screen. In some unforgiving hardware, some of
97 * this programming has to both start and end in the same vblank. To help
98 * with the timing of the hardware programming, an interrupt is usually
99 * available to notify the driver when it can start the updating of registers.
100 * The interrupt is in this context named the vblank interrupt.
102 * The vblank interrupt may be fired at different points depending on the
103 * hardware. Some hardware implementations will fire the interrupt when the
104 * new frame start, other implementations will fire the interrupt at different
107 * Vertical blanking plays a major role in graphics rendering. To achieve
108 * tear-free display, users must synchronize page flips and/or rendering to
109 * vertical blanking. The DRM API offers ioctls to perform page flips
110 * synchronized to vertical blanking and wait for vertical blanking.
112 * The DRM core handles most of the vertical blanking management logic, which
113 * involves filtering out spurious interrupts, keeping race-free blanking
114 * counters, coping with counter wrap-around and resets and keeping use counts.
115 * It relies on the driver to generate vertical blanking interrupts and
116 * optionally provide a hardware vertical blanking counter.
118 * Drivers must initialize the vertical blanking handling core with a call to
119 * drm_vblank_init(). Minimally, a driver needs to implement
120 * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
121 * drm_crtc_handle_vblank() in its vblank interrupt handler for working vblank
124 * Vertical blanking interrupts can be enabled by the DRM core or by drivers
125 * themselves (for instance to handle page flipping operations). The DRM core
126 * maintains a vertical blanking use count to ensure that the interrupts are not
127 * disabled while a user still needs them. To increment the use count, drivers
128 * call drm_crtc_vblank_get() and release the vblank reference again with
129 * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
130 * guaranteed to be enabled.
132 * On many hardware disabling the vblank interrupt cannot be done in a race-free
133 * manner, see &drm_driver.vblank_disable_immediate and
134 * &drm_driver.max_vblank_count. In that case the vblank core only disables the
135 * vblanks after a timer has expired, which can be configured through the
136 * ``vblankoffdelay`` module parameter.
138 * Drivers for hardware without support for vertical-blanking interrupts
139 * must not call drm_vblank_init(). For such drivers, atomic helpers will
140 * automatically generate fake vblank events as part of the display update.
141 * This functionality also can be controlled by the driver by enabling and
142 * disabling struct drm_crtc_state.no_vblank.
145 /* Retry timestamp calculation up to 3 times to satisfy
146 * drm_timestamp_precision before giving up.
148 #define DRM_TIMESTAMP_MAXRETRIES 3
150 /* Threshold in nanoseconds for detection of redundant
151 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
153 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
156 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
157 ktime_t *tvblank, bool in_vblank_irq);
159 static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
161 static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
163 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
164 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
165 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
166 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
168 static void store_vblank(struct drm_device *dev, unsigned int pipe,
169 u32 vblank_count_inc,
170 ktime_t t_vblank, u32 last)
172 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
174 assert_spin_locked(&dev->vblank_time_lock);
178 write_seqlock(&vblank->seqlock);
179 vblank->time = t_vblank;
180 atomic64_add(vblank_count_inc, &vblank->count);
181 write_sequnlock(&vblank->seqlock);
184 static u32 drm_max_vblank_count(struct drm_device *dev, unsigned int pipe)
186 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
188 return vblank->max_vblank_count ?: dev->max_vblank_count;
192 * "No hw counter" fallback implementation of .get_vblank_counter() hook,
193 * if there is no useable hardware frame counter available.
195 static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
197 WARN_ON_ONCE(drm_max_vblank_count(dev, pipe) != 0);
201 static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
203 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
204 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
209 if (crtc->funcs->get_vblank_counter)
210 return crtc->funcs->get_vblank_counter(crtc);
211 } else if (dev->driver->get_vblank_counter) {
212 return dev->driver->get_vblank_counter(dev, pipe);
215 return drm_vblank_no_hw_counter(dev, pipe);
219 * Reset the stored timestamp for the current vblank count to correspond
220 * to the last vblank occurred.
222 * Only to be called from drm_crtc_vblank_on().
224 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
225 * device vblank fields.
227 static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
232 int count = DRM_TIMESTAMP_MAXRETRIES;
234 spin_lock(&dev->vblank_time_lock);
237 * sample the current counter to avoid random jumps
238 * when drm_vblank_enable() applies the diff
241 cur_vblank = __get_vblank_counter(dev, pipe);
242 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
243 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
246 * Only reinitialize corresponding vblank timestamp if high-precision query
247 * available and didn't fail. Otherwise reinitialize delayed at next vblank
248 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
254 * +1 to make sure user will never see the same
255 * vblank counter value before and after a modeset
257 store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
259 spin_unlock(&dev->vblank_time_lock);
263 * Call back into the driver to update the appropriate vblank counter
264 * (specified by @pipe). Deal with wraparound, if it occurred, and
265 * update the last read value so we can deal with wraparound on the next
268 * Only necessary when going from off->on, to account for frames we
269 * didn't get an interrupt for.
271 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
272 * device vblank fields.
274 static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
277 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
278 u32 cur_vblank, diff;
281 int count = DRM_TIMESTAMP_MAXRETRIES;
282 int framedur_ns = vblank->framedur_ns;
283 u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
286 * Interrupts were disabled prior to this call, so deal with counter
288 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
289 * here if the register is small or we had vblank interrupts off for
292 * We repeat the hardware vblank counter & timestamp query until
293 * we get consistent results. This to prevent races between gpu
294 * updating its hardware counter while we are retrieving the
295 * corresponding vblank timestamp.
298 cur_vblank = __get_vblank_counter(dev, pipe);
299 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
300 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
302 if (max_vblank_count) {
303 /* trust the hw counter when it's around */
304 diff = (cur_vblank - vblank->last) & max_vblank_count;
305 } else if (rc && framedur_ns) {
306 u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
309 * Figure out how many vblanks we've missed based
310 * on the difference in the timestamps and the
311 * frame/field duration.
314 DRM_DEBUG_VBL("crtc %u: Calculating number of vblanks."
315 " diff_ns = %lld, framedur_ns = %d)\n",
316 pipe, (long long) diff_ns, framedur_ns);
318 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
320 if (diff == 0 && in_vblank_irq)
321 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored\n",
324 /* some kind of default for drivers w/o accurate vbl timestamping */
325 diff = in_vblank_irq ? 1 : 0;
329 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
330 * interval? If so then vblank irqs keep running and it will likely
331 * happen that the hardware vblank counter is not trustworthy as it
332 * might reset at some point in that interval and vblank timestamps
333 * are not trustworthy either in that interval. Iow. this can result
334 * in a bogus diff >> 1 which must be avoided as it would cause
335 * random large forward jumps of the software vblank counter.
337 if (diff > 1 && (vblank->inmodeset & 0x2)) {
338 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
339 " due to pre-modeset.\n", pipe, diff);
343 DRM_DEBUG_VBL("updating vblank count on crtc %u:"
344 " current=%llu, diff=%u, hw=%u hw_last=%u\n",
345 pipe, (unsigned long long)atomic64_read(&vblank->count),
346 diff, cur_vblank, vblank->last);
349 WARN_ON_ONCE(cur_vblank != vblank->last);
354 * Only reinitialize corresponding vblank timestamp if high-precision query
355 * available and didn't fail, or we were called from the vblank interrupt.
356 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
357 * for now, to mark the vblanktimestamp as invalid.
359 if (!rc && !in_vblank_irq)
362 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
365 static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
367 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
370 if (WARN_ON(pipe >= dev->num_crtcs))
373 count = atomic64_read(&vblank->count);
376 * This read barrier corresponds to the implicit write barrier of the
377 * write seqlock in store_vblank(). Note that this is the only place
378 * where we need an explicit barrier, since all other access goes
379 * through drm_vblank_count_and_time(), which already has the required
380 * read barrier curtesy of the read seqlock.
388 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
389 * @crtc: which counter to retrieve
391 * This function is similar to drm_crtc_vblank_count() but this function
392 * interpolates to handle a race with vblank interrupts using the high precision
393 * timestamping support.
395 * This is mostly useful for hardware that can obtain the scanout position, but
396 * doesn't have a hardware frame counter.
398 u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
400 struct drm_device *dev = crtc->dev;
401 unsigned int pipe = drm_crtc_index(crtc);
405 WARN_ONCE(drm_debug_enabled(DRM_UT_VBL) &&
406 !crtc->funcs->get_vblank_timestamp,
407 "This function requires support for accurate vblank timestamps.");
409 spin_lock_irqsave(&dev->vblank_time_lock, flags);
411 drm_update_vblank_count(dev, pipe, false);
412 vblank = drm_vblank_count(dev, pipe);
414 spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
418 EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
420 static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
422 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
423 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
428 if (crtc->funcs->disable_vblank)
429 crtc->funcs->disable_vblank(crtc);
431 dev->driver->disable_vblank(dev, pipe);
436 * Disable vblank irq's on crtc, make sure that last vblank count
437 * of hardware and corresponding consistent software vblank counter
438 * are preserved, even if there are any spurious vblank irq's after
441 void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
443 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
444 unsigned long irqflags;
446 assert_spin_locked(&dev->vbl_lock);
448 /* Prevent vblank irq processing while disabling vblank irqs,
449 * so no updates of timestamps or count can happen after we've
450 * disabled. Needed to prevent races in case of delayed irq's.
452 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
455 * Update vblank count and disable vblank interrupts only if the
456 * interrupts were enabled. This avoids calling the ->disable_vblank()
457 * operation in atomic context with the hardware potentially runtime
460 if (!vblank->enabled)
464 * Update the count and timestamp to maintain the
465 * appearance that the counter has been ticking all along until
466 * this time. This makes the count account for the entire time
467 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
469 drm_update_vblank_count(dev, pipe, false);
470 __disable_vblank(dev, pipe);
471 vblank->enabled = false;
474 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
477 static void vblank_disable_fn(struct timer_list *t)
479 struct drm_vblank_crtc *vblank = from_timer(vblank, t, disable_timer);
480 struct drm_device *dev = vblank->dev;
481 unsigned int pipe = vblank->pipe;
482 unsigned long irqflags;
484 spin_lock_irqsave(&dev->vbl_lock, irqflags);
485 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
486 DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
487 drm_vblank_disable_and_save(dev, pipe);
489 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
492 static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
496 for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
497 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
499 WARN_ON(READ_ONCE(vblank->enabled) &&
500 drm_core_check_feature(dev, DRIVER_MODESET));
502 del_timer_sync(&vblank->disable_timer);
507 * drm_vblank_init - initialize vblank support
509 * @num_crtcs: number of CRTCs supported by @dev
511 * This function initializes vblank support for @num_crtcs display pipelines.
512 * Cleanup is handled automatically through a cleanup function added with
516 * Zero on success or a negative error code on failure.
518 int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
523 spin_lock_init(&dev->vbl_lock);
524 spin_lock_init(&dev->vblank_time_lock);
526 dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
530 dev->num_crtcs = num_crtcs;
532 ret = drmm_add_action(dev, drm_vblank_init_release, NULL);
536 for (i = 0; i < num_crtcs; i++) {
537 struct drm_vblank_crtc *vblank = &dev->vblank[i];
541 init_waitqueue_head(&vblank->queue);
542 timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
543 seqlock_init(&vblank->seqlock);
546 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
550 EXPORT_SYMBOL(drm_vblank_init);
553 * drm_dev_has_vblank - test if vblanking has been initialized for
557 * Drivers may call this function to test if vblank support is
558 * initialized for a device. For most hardware this means that vblanking
559 * can also be enabled.
561 * Atomic helpers use this function to initialize
562 * &drm_crtc_state.no_vblank. See also drm_atomic_helper_check_modeset().
565 * True if vblanking has been initialized for the given device, false
568 bool drm_dev_has_vblank(const struct drm_device *dev)
570 return dev->num_crtcs != 0;
572 EXPORT_SYMBOL(drm_dev_has_vblank);
575 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
576 * @crtc: which CRTC's vblank waitqueue to retrieve
578 * This function returns a pointer to the vblank waitqueue for the CRTC.
579 * Drivers can use this to implement vblank waits using wait_event() and related
582 wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
584 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
586 EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
590 * drm_calc_timestamping_constants - calculate vblank timestamp constants
591 * @crtc: drm_crtc whose timestamp constants should be updated.
592 * @mode: display mode containing the scanout timings
594 * Calculate and store various constants which are later needed by vblank and
595 * swap-completion timestamping, e.g, by
596 * drm_crtc_vblank_helper_get_vblank_timestamp(). They are derived from
597 * CRTC's true scanout timing, so they take things like panel scaling or
598 * other adjustments into account.
600 void drm_calc_timestamping_constants(struct drm_crtc *crtc,
601 const struct drm_display_mode *mode)
603 struct drm_device *dev = crtc->dev;
604 unsigned int pipe = drm_crtc_index(crtc);
605 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
606 int linedur_ns = 0, framedur_ns = 0;
607 int dotclock = mode->crtc_clock;
612 if (WARN_ON(pipe >= dev->num_crtcs))
615 /* Valid dotclock? */
617 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
620 * Convert scanline length in pixels and video
621 * dot clock to line duration and frame duration
624 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
625 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
628 * Fields of interlaced scanout modes are only half a frame duration.
630 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
633 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
636 vblank->linedur_ns = linedur_ns;
637 vblank->framedur_ns = framedur_ns;
638 vblank->hwmode = *mode;
640 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
641 crtc->base.id, mode->crtc_htotal,
642 mode->crtc_vtotal, mode->crtc_vdisplay);
643 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
644 crtc->base.id, dotclock, framedur_ns, linedur_ns);
646 EXPORT_SYMBOL(drm_calc_timestamping_constants);
649 * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
651 * @crtc: CRTC whose vblank timestamp to retrieve
652 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
653 * On return contains true maximum error of timestamp
654 * @vblank_time: Pointer to time which should receive the timestamp
656 * True when called from drm_crtc_handle_vblank(). Some drivers
657 * need to apply some workarounds for gpu-specific vblank irq quirks
659 * @get_scanout_position:
660 * Callback function to retrieve the scanout position. See
661 * @struct drm_crtc_helper_funcs.get_scanout_position.
663 * Implements calculation of exact vblank timestamps from given drm_display_mode
664 * timings and current video scanout position of a CRTC.
666 * The current implementation only handles standard video modes. For double scan
667 * and interlaced modes the driver is supposed to adjust the hardware mode
668 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
669 * match the scanout position reported.
671 * Note that atomic drivers must call drm_calc_timestamping_constants() before
672 * enabling a CRTC. The atomic helpers already take care of that in
673 * drm_atomic_helper_update_legacy_modeset_state().
677 * Returns true on success, and false on failure, i.e. when no accurate
678 * timestamp could be acquired.
681 drm_crtc_vblank_helper_get_vblank_timestamp_internal(
682 struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time,
684 drm_vblank_get_scanout_position_func get_scanout_position)
686 struct drm_device *dev = crtc->dev;
687 unsigned int pipe = crtc->index;
688 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
689 struct timespec64 ts_etime, ts_vblank_time;
690 ktime_t stime, etime;
692 const struct drm_display_mode *mode;
694 int delta_ns, duration_ns;
696 if (pipe >= dev->num_crtcs) {
697 DRM_ERROR("Invalid crtc %u\n", pipe);
701 /* Scanout position query not supported? Should not happen. */
702 if (!get_scanout_position) {
703 DRM_ERROR("Called from CRTC w/o get_scanout_position()!?\n");
707 if (drm_drv_uses_atomic_modeset(dev))
708 mode = &vblank->hwmode;
710 mode = &crtc->hwmode;
712 /* If mode timing undefined, just return as no-op:
713 * Happens during initial modesetting of a crtc.
715 if (mode->crtc_clock == 0) {
716 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
717 WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev));
721 /* Get current scanout position with system timestamp.
722 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
723 * if single query takes longer than max_error nanoseconds.
725 * This guarantees a tight bound on maximum error if
726 * code gets preempted or delayed for some reason.
728 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
730 * Get vertical and horizontal scanout position vpos, hpos,
731 * and bounding timestamps stime, etime, pre/post query.
733 vbl_status = get_scanout_position(crtc, in_vblank_irq,
738 /* Return as no-op if scanout query unsupported or failed. */
740 DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
745 /* Compute uncertainty in timestamp of scanout position query. */
746 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
748 /* Accept result with < max_error nsecs timing uncertainty. */
749 if (duration_ns <= *max_error)
753 /* Noisy system timing? */
754 if (i == DRM_TIMESTAMP_MAXRETRIES) {
755 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
756 pipe, duration_ns/1000, *max_error/1000, i);
759 /* Return upper bound of timestamp precision error. */
760 *max_error = duration_ns;
762 /* Convert scanout position into elapsed time at raw_time query
763 * since start of scanout at first display scanline. delta_ns
764 * can be negative if start of scanout hasn't happened yet.
766 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
769 /* Subtract time delta from raw timestamp to get final
770 * vblank_time timestamp for end of vblank.
772 *vblank_time = ktime_sub_ns(etime, delta_ns);
774 if (!drm_debug_enabled(DRM_UT_VBL))
777 ts_etime = ktime_to_timespec64(etime);
778 ts_vblank_time = ktime_to_timespec64(*vblank_time);
780 DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
782 (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
783 (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
784 duration_ns / 1000, i);
788 EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal);
791 * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
793 * @crtc: CRTC whose vblank timestamp to retrieve
794 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
795 * On return contains true maximum error of timestamp
796 * @vblank_time: Pointer to time which should receive the timestamp
798 * True when called from drm_crtc_handle_vblank(). Some drivers
799 * need to apply some workarounds for gpu-specific vblank irq quirks
802 * Implements calculation of exact vblank timestamps from given drm_display_mode
803 * timings and current video scanout position of a CRTC. This can be directly
804 * used as the &drm_crtc_funcs.get_vblank_timestamp implementation of a kms
805 * driver if &drm_crtc_helper_funcs.get_scanout_position is implemented.
807 * The current implementation only handles standard video modes. For double scan
808 * and interlaced modes the driver is supposed to adjust the hardware mode
809 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
810 * match the scanout position reported.
812 * Note that atomic drivers must call drm_calc_timestamping_constants() before
813 * enabling a CRTC. The atomic helpers already take care of that in
814 * drm_atomic_helper_update_legacy_modeset_state().
818 * Returns true on success, and false on failure, i.e. when no accurate
819 * timestamp could be acquired.
821 bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
823 ktime_t *vblank_time,
826 return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
827 crtc, max_error, vblank_time, in_vblank_irq,
828 crtc->helper_private->get_scanout_position);
830 EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);
833 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
836 * @pipe: index of CRTC whose vblank timestamp to retrieve
837 * @tvblank: Pointer to target time which should receive the timestamp
839 * True when called from drm_crtc_handle_vblank(). Some drivers
840 * need to apply some workarounds for gpu-specific vblank irq quirks
843 * Fetches the system timestamp corresponding to the time of the most recent
844 * vblank interval on specified CRTC. May call into kms-driver to
845 * compute the timestamp with a high-precision GPU specific method.
847 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
848 * call, i.e., it isn't very precisely locked to the true vblank.
851 * True if timestamp is considered to be very precise, false otherwise.
854 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
855 ktime_t *tvblank, bool in_vblank_irq)
857 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
860 /* Define requested maximum error on timestamps (nanoseconds). */
861 int max_error = (int) drm_timestamp_precision * 1000;
863 /* Query driver if possible and precision timestamping enabled. */
864 if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {
865 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
867 ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,
868 tvblank, in_vblank_irq);
871 /* GPU high precision timestamp query unsupported or failed.
872 * Return current monotonic/gettimeofday timestamp as best estimate.
875 *tvblank = ktime_get();
881 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
882 * @crtc: which counter to retrieve
884 * Fetches the "cooked" vblank count value that represents the number of
885 * vblank events since the system was booted, including lost events due to
886 * modesetting activity. Note that this timer isn't correct against a racing
887 * vblank interrupt (since it only reports the software vblank counter), see
888 * drm_crtc_accurate_vblank_count() for such use-cases.
890 * Note that for a given vblank counter value drm_crtc_handle_vblank()
891 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
892 * provide a barrier: Any writes done before calling
893 * drm_crtc_handle_vblank() will be visible to callers of the later
894 * functions, iff the vblank count is the same or a later one.
896 * See also &drm_vblank_crtc.count.
899 * The software vblank counter.
901 u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
903 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
905 EXPORT_SYMBOL(drm_crtc_vblank_count);
908 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
909 * system timestamp corresponding to that vblank counter value.
911 * @pipe: index of CRTC whose counter to retrieve
912 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
914 * Fetches the "cooked" vblank count value that represents the number of
915 * vblank events since the system was booted, including lost events due to
916 * modesetting activity. Returns corresponding system timestamp of the time
917 * of the vblank interval that corresponds to the current vblank counter value.
919 * This is the legacy version of drm_crtc_vblank_count_and_time().
921 static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
924 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
928 if (WARN_ON(pipe >= dev->num_crtcs)) {
934 seq = read_seqbegin(&vblank->seqlock);
935 vblank_count = atomic64_read(&vblank->count);
936 *vblanktime = vblank->time;
937 } while (read_seqretry(&vblank->seqlock, seq));
943 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
944 * and the system timestamp corresponding to that vblank counter value
945 * @crtc: which counter to retrieve
946 * @vblanktime: Pointer to time to receive the vblank timestamp.
948 * Fetches the "cooked" vblank count value that represents the number of
949 * vblank events since the system was booted, including lost events due to
950 * modesetting activity. Returns corresponding system timestamp of the time
951 * of the vblank interval that corresponds to the current vblank counter value.
953 * Note that for a given vblank counter value drm_crtc_handle_vblank()
954 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
955 * provide a barrier: Any writes done before calling
956 * drm_crtc_handle_vblank() will be visible to callers of the later
957 * functions, iff the vblank count is the same or a later one.
959 * See also &drm_vblank_crtc.count.
961 u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
964 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
967 EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
969 static void send_vblank_event(struct drm_device *dev,
970 struct drm_pending_vblank_event *e,
971 u64 seq, ktime_t now)
973 struct timespec64 tv;
975 switch (e->event.base.type) {
976 case DRM_EVENT_VBLANK:
977 case DRM_EVENT_FLIP_COMPLETE:
978 tv = ktime_to_timespec64(now);
979 e->event.vbl.sequence = seq;
981 * e->event is a user space structure, with hardcoded unsigned
982 * 32-bit seconds/microseconds. This is safe as we always use
983 * monotonic timestamps since linux-4.15
985 e->event.vbl.tv_sec = tv.tv_sec;
986 e->event.vbl.tv_usec = tv.tv_nsec / 1000;
988 case DRM_EVENT_CRTC_SEQUENCE:
990 e->event.seq.sequence = seq;
991 e->event.seq.time_ns = ktime_to_ns(now);
994 trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
995 drm_send_event_locked(dev, &e->base);
999 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1000 * @crtc: the source CRTC of the vblank event
1001 * @e: the event to send
1003 * A lot of drivers need to generate vblank events for the very next vblank
1004 * interrupt. For example when the page flip interrupt happens when the page
1005 * flip gets armed, but not when it actually executes within the next vblank
1006 * period. This helper function implements exactly the required vblank arming
1009 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
1010 * atomic commit must ensure that the next vblank happens at exactly the same
1011 * time as the atomic commit is committed to the hardware. This function itself
1012 * does **not** protect against the next vblank interrupt racing with either this
1013 * function call or the atomic commit operation. A possible sequence could be:
1015 * 1. Driver commits new hardware state into vblank-synchronized registers.
1016 * 2. A vblank happens, committing the hardware state. Also the corresponding
1017 * vblank interrupt is fired off and fully processed by the interrupt
1019 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
1020 * 4. The event is only send out for the next vblank, which is wrong.
1022 * An equivalent race can happen when the driver calls
1023 * drm_crtc_arm_vblank_event() before writing out the new hardware state.
1025 * The only way to make this work safely is to prevent the vblank from firing
1026 * (and the hardware from committing anything else) until the entire atomic
1027 * commit sequence has run to completion. If the hardware does not have such a
1028 * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
1029 * Instead drivers need to manually send out the event from their interrupt
1030 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
1031 * possible race with the hardware committing the atomic update.
1033 * Caller must hold a vblank reference for the event @e acquired by a
1034 * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
1036 void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1037 struct drm_pending_vblank_event *e)
1039 struct drm_device *dev = crtc->dev;
1040 unsigned int pipe = drm_crtc_index(crtc);
1042 assert_spin_locked(&dev->event_lock);
1045 e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
1046 list_add_tail(&e->base.link, &dev->vblank_event_list);
1048 EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1051 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1052 * @crtc: the source CRTC of the vblank event
1053 * @e: the event to send
1055 * Updates sequence # and timestamp on event for the most recently processed
1056 * vblank, and sends it to userspace. Caller must hold event lock.
1058 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
1059 * situation, especially to send out events for atomic commit operations.
1061 void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1062 struct drm_pending_vblank_event *e)
1064 struct drm_device *dev = crtc->dev;
1066 unsigned int pipe = drm_crtc_index(crtc);
1069 if (dev->num_crtcs > 0) {
1070 seq = drm_vblank_count_and_time(dev, pipe, &now);
1077 send_vblank_event(dev, e, seq, now);
1079 EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1081 static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
1083 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1084 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1089 if (crtc->funcs->enable_vblank)
1090 return crtc->funcs->enable_vblank(crtc);
1091 } else if (dev->driver->enable_vblank) {
1092 return dev->driver->enable_vblank(dev, pipe);
1098 static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1100 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1103 assert_spin_locked(&dev->vbl_lock);
1105 spin_lock(&dev->vblank_time_lock);
1107 if (!vblank->enabled) {
1109 * Enable vblank irqs under vblank_time_lock protection.
1110 * All vblank count & timestamp updates are held off
1111 * until we are done reinitializing master counter and
1112 * timestamps. Filtercode in drm_handle_vblank() will
1113 * prevent double-accounting of same vblank interval.
1115 ret = __enable_vblank(dev, pipe);
1116 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
1118 atomic_dec(&vblank->refcount);
1120 drm_update_vblank_count(dev, pipe, 0);
1121 /* drm_update_vblank_count() includes a wmb so we just
1122 * need to ensure that the compiler emits the write
1123 * to mark the vblank as enabled after the call
1124 * to drm_update_vblank_count().
1126 WRITE_ONCE(vblank->enabled, true);
1130 spin_unlock(&dev->vblank_time_lock);
1135 static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
1137 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1138 unsigned long irqflags;
1141 if (!dev->num_crtcs)
1144 if (WARN_ON(pipe >= dev->num_crtcs))
1147 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1148 /* Going from 0->1 means we have to enable interrupts again */
1149 if (atomic_add_return(1, &vblank->refcount) == 1) {
1150 ret = drm_vblank_enable(dev, pipe);
1152 if (!vblank->enabled) {
1153 atomic_dec(&vblank->refcount);
1157 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1163 * drm_crtc_vblank_get - get a reference count on vblank events
1164 * @crtc: which CRTC to own
1166 * Acquire a reference count on vblank events to avoid having them disabled
1170 * Zero on success or a negative error code on failure.
1172 int drm_crtc_vblank_get(struct drm_crtc *crtc)
1174 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1176 EXPORT_SYMBOL(drm_crtc_vblank_get);
1178 static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1180 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1182 if (WARN_ON(pipe >= dev->num_crtcs))
1185 if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1188 /* Last user schedules interrupt disable */
1189 if (atomic_dec_and_test(&vblank->refcount)) {
1190 if (drm_vblank_offdelay == 0)
1192 else if (drm_vblank_offdelay < 0)
1193 vblank_disable_fn(&vblank->disable_timer);
1194 else if (!dev->vblank_disable_immediate)
1195 mod_timer(&vblank->disable_timer,
1196 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1201 * drm_crtc_vblank_put - give up ownership of vblank events
1202 * @crtc: which counter to give up
1204 * Release ownership of a given vblank counter, turning off interrupts
1205 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1207 void drm_crtc_vblank_put(struct drm_crtc *crtc)
1209 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1211 EXPORT_SYMBOL(drm_crtc_vblank_put);
1214 * drm_wait_one_vblank - wait for one vblank
1218 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1219 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1220 * due to lack of driver support or because the crtc is off.
1222 * This is the legacy version of drm_crtc_wait_one_vblank().
1224 void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1226 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1230 if (WARN_ON(pipe >= dev->num_crtcs))
1233 ret = drm_vblank_get(dev, pipe);
1234 if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1237 last = drm_vblank_count(dev, pipe);
1239 ret = wait_event_timeout(vblank->queue,
1240 last != drm_vblank_count(dev, pipe),
1241 msecs_to_jiffies(100));
1243 WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1245 drm_vblank_put(dev, pipe);
1247 EXPORT_SYMBOL(drm_wait_one_vblank);
1250 * drm_crtc_wait_one_vblank - wait for one vblank
1253 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1254 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1255 * due to lack of driver support or because the crtc is off.
1257 void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1259 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1261 EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1264 * drm_crtc_vblank_off - disable vblank events on a CRTC
1265 * @crtc: CRTC in question
1267 * Drivers can use this function to shut down the vblank interrupt handling when
1268 * disabling a crtc. This function ensures that the latest vblank frame count is
1269 * stored so that drm_vblank_on can restore it again.
1271 * Drivers must use this function when the hardware vblank counter can get
1272 * reset, e.g. when suspending or disabling the @crtc in general.
1274 void drm_crtc_vblank_off(struct drm_crtc *crtc)
1276 struct drm_device *dev = crtc->dev;
1277 unsigned int pipe = drm_crtc_index(crtc);
1278 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1279 struct drm_pending_vblank_event *e, *t;
1282 unsigned long irqflags;
1285 if (WARN_ON(pipe >= dev->num_crtcs))
1288 spin_lock_irqsave(&dev->event_lock, irqflags);
1290 spin_lock(&dev->vbl_lock);
1291 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1292 pipe, vblank->enabled, vblank->inmodeset);
1294 /* Avoid redundant vblank disables without previous
1295 * drm_crtc_vblank_on(). */
1296 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1297 drm_vblank_disable_and_save(dev, pipe);
1299 wake_up(&vblank->queue);
1302 * Prevent subsequent drm_vblank_get() from re-enabling
1303 * the vblank interrupt by bumping the refcount.
1305 if (!vblank->inmodeset) {
1306 atomic_inc(&vblank->refcount);
1307 vblank->inmodeset = 1;
1309 spin_unlock(&dev->vbl_lock);
1311 /* Send any queued vblank events, lest the natives grow disquiet */
1312 seq = drm_vblank_count_and_time(dev, pipe, &now);
1314 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1315 if (e->pipe != pipe)
1317 DRM_DEBUG("Sending premature vblank event on disable: "
1318 "wanted %llu, current %llu\n",
1320 list_del(&e->base.link);
1321 drm_vblank_put(dev, pipe);
1322 send_vblank_event(dev, e, seq, now);
1324 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1326 /* Will be reset by the modeset helpers when re-enabling the crtc by
1327 * calling drm_calc_timestamping_constants(). */
1328 vblank->hwmode.crtc_clock = 0;
1330 EXPORT_SYMBOL(drm_crtc_vblank_off);
1333 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1334 * @crtc: CRTC in question
1336 * Drivers can use this function to reset the vblank state to off at load time.
1337 * Drivers should use this together with the drm_crtc_vblank_off() and
1338 * drm_crtc_vblank_on() functions. The difference compared to
1339 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1340 * and hence doesn't need to call any driver hooks.
1342 * This is useful for recovering driver state e.g. on driver load, or on resume.
1344 void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1346 struct drm_device *dev = crtc->dev;
1347 unsigned long irqflags;
1348 unsigned int pipe = drm_crtc_index(crtc);
1349 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1351 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1353 * Prevent subsequent drm_vblank_get() from enabling the vblank
1354 * interrupt by bumping the refcount.
1356 if (!vblank->inmodeset) {
1357 atomic_inc(&vblank->refcount);
1358 vblank->inmodeset = 1;
1360 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1362 WARN_ON(!list_empty(&dev->vblank_event_list));
1364 EXPORT_SYMBOL(drm_crtc_vblank_reset);
1367 * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
1368 * @crtc: CRTC in question
1369 * @max_vblank_count: max hardware vblank counter value
1371 * Update the maximum hardware vblank counter value for @crtc
1372 * at runtime. Useful for hardware where the operation of the
1373 * hardware vblank counter depends on the currently active
1374 * display configuration.
1376 * For example, if the hardware vblank counter does not work
1377 * when a specific connector is active the maximum can be set
1378 * to zero. And when that specific connector isn't active the
1379 * maximum can again be set to the appropriate non-zero value.
1381 * If used, must be called before drm_vblank_on().
1383 void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
1384 u32 max_vblank_count)
1386 struct drm_device *dev = crtc->dev;
1387 unsigned int pipe = drm_crtc_index(crtc);
1388 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1390 WARN_ON(dev->max_vblank_count);
1391 WARN_ON(!READ_ONCE(vblank->inmodeset));
1393 vblank->max_vblank_count = max_vblank_count;
1395 EXPORT_SYMBOL(drm_crtc_set_max_vblank_count);
1398 * drm_crtc_vblank_on - enable vblank events on a CRTC
1399 * @crtc: CRTC in question
1401 * This functions restores the vblank interrupt state captured with
1402 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1403 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1404 * unbalanced and so can also be unconditionally called in driver load code to
1405 * reflect the current hardware state of the crtc.
1407 void drm_crtc_vblank_on(struct drm_crtc *crtc)
1409 struct drm_device *dev = crtc->dev;
1410 unsigned int pipe = drm_crtc_index(crtc);
1411 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1412 unsigned long irqflags;
1414 if (WARN_ON(pipe >= dev->num_crtcs))
1417 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1418 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1419 pipe, vblank->enabled, vblank->inmodeset);
1421 /* Drop our private "prevent drm_vblank_get" refcount */
1422 if (vblank->inmodeset) {
1423 atomic_dec(&vblank->refcount);
1424 vblank->inmodeset = 0;
1427 drm_reset_vblank_timestamp(dev, pipe);
1430 * re-enable interrupts if there are users left, or the
1431 * user wishes vblank interrupts to be enabled all the time.
1433 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1434 WARN_ON(drm_vblank_enable(dev, pipe));
1435 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1437 EXPORT_SYMBOL(drm_crtc_vblank_on);
1440 * drm_vblank_restore - estimate missed vblanks and update vblank count.
1444 * Power manamement features can cause frame counter resets between vblank
1445 * disable and enable. Drivers can use this function in their
1446 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1447 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1450 * This function is the legacy version of drm_crtc_vblank_restore().
1452 void drm_vblank_restore(struct drm_device *dev, unsigned int pipe)
1455 struct drm_vblank_crtc *vblank;
1458 u32 cur_vblank, diff = 1;
1459 int count = DRM_TIMESTAMP_MAXRETRIES;
1461 if (WARN_ON(pipe >= dev->num_crtcs))
1464 assert_spin_locked(&dev->vbl_lock);
1465 assert_spin_locked(&dev->vblank_time_lock);
1467 vblank = &dev->vblank[pipe];
1468 WARN_ONCE(drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
1469 "Cannot compute missed vblanks without frame duration\n");
1470 framedur_ns = vblank->framedur_ns;
1473 cur_vblank = __get_vblank_counter(dev, pipe);
1474 drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
1475 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
1477 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
1479 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
1482 DRM_DEBUG_VBL("missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1483 diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
1484 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
1486 EXPORT_SYMBOL(drm_vblank_restore);
1489 * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
1490 * @crtc: CRTC in question
1492 * Power manamement features can cause frame counter resets between vblank
1493 * disable and enable. Drivers can use this function in their
1494 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1495 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1498 void drm_crtc_vblank_restore(struct drm_crtc *crtc)
1500 drm_vblank_restore(crtc->dev, drm_crtc_index(crtc));
1502 EXPORT_SYMBOL(drm_crtc_vblank_restore);
1504 static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
1507 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1509 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1510 if (!dev->num_crtcs)
1513 if (WARN_ON(pipe >= dev->num_crtcs))
1517 * To avoid all the problems that might happen if interrupts
1518 * were enabled/disabled around or between these calls, we just
1519 * have the kernel take a reference on the CRTC (just once though
1520 * to avoid corrupting the count if multiple, mismatch calls occur),
1521 * so that interrupts remain enabled in the interim.
1523 if (!vblank->inmodeset) {
1524 vblank->inmodeset = 0x1;
1525 if (drm_vblank_get(dev, pipe) == 0)
1526 vblank->inmodeset |= 0x2;
1530 static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
1533 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1534 unsigned long irqflags;
1536 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1537 if (!dev->num_crtcs)
1540 if (WARN_ON(pipe >= dev->num_crtcs))
1543 if (vblank->inmodeset) {
1544 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1545 drm_reset_vblank_timestamp(dev, pipe);
1546 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1548 if (vblank->inmodeset & 0x2)
1549 drm_vblank_put(dev, pipe);
1551 vblank->inmodeset = 0;
1555 int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
1556 struct drm_file *file_priv)
1558 struct drm_modeset_ctl *modeset = data;
1561 /* If drm_vblank_init() hasn't been called yet, just no-op */
1562 if (!dev->num_crtcs)
1565 /* KMS drivers handle this internally */
1566 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1569 pipe = modeset->crtc;
1570 if (pipe >= dev->num_crtcs)
1573 switch (modeset->cmd) {
1574 case _DRM_PRE_MODESET:
1575 drm_legacy_vblank_pre_modeset(dev, pipe);
1577 case _DRM_POST_MODESET:
1578 drm_legacy_vblank_post_modeset(dev, pipe);
1587 static inline bool vblank_passed(u64 seq, u64 ref)
1589 return (seq - ref) <= (1 << 23);
1592 static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
1594 union drm_wait_vblank *vblwait,
1595 struct drm_file *file_priv)
1597 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1598 struct drm_pending_vblank_event *e;
1600 unsigned long flags;
1604 e = kzalloc(sizeof(*e), GFP_KERNEL);
1611 e->event.base.type = DRM_EVENT_VBLANK;
1612 e->event.base.length = sizeof(e->event.vbl);
1613 e->event.vbl.user_data = vblwait->request.signal;
1614 e->event.vbl.crtc_id = 0;
1615 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1616 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1618 e->event.vbl.crtc_id = crtc->base.id;
1621 spin_lock_irqsave(&dev->event_lock, flags);
1624 * drm_crtc_vblank_off() might have been called after we called
1625 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1626 * vblank disable, so no need for further locking. The reference from
1627 * drm_vblank_get() protects against vblank disable from another source.
1629 if (!READ_ONCE(vblank->enabled)) {
1634 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1640 seq = drm_vblank_count_and_time(dev, pipe, &now);
1642 DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n",
1643 req_seq, seq, pipe);
1645 trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
1647 e->sequence = req_seq;
1648 if (vblank_passed(seq, req_seq)) {
1649 drm_vblank_put(dev, pipe);
1650 send_vblank_event(dev, e, seq, now);
1651 vblwait->reply.sequence = seq;
1653 /* drm_handle_vblank_events will call drm_vblank_put */
1654 list_add_tail(&e->base.link, &dev->vblank_event_list);
1655 vblwait->reply.sequence = req_seq;
1658 spin_unlock_irqrestore(&dev->event_lock, flags);
1663 spin_unlock_irqrestore(&dev->event_lock, flags);
1666 drm_vblank_put(dev, pipe);
1670 static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
1672 if (vblwait->request.sequence)
1675 return _DRM_VBLANK_RELATIVE ==
1676 (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
1678 _DRM_VBLANK_NEXTONMISS));
1682 * Widen a 32-bit param to 64-bits.
1684 * \param narrow 32-bit value (missing upper 32 bits)
1685 * \param near 64-bit value that should be 'close' to near
1687 * This function returns a 64-bit value using the lower 32-bits from
1688 * 'narrow' and constructing the upper 32-bits so that the result is
1689 * as close as possible to 'near'.
1692 static u64 widen_32_to_64(u32 narrow, u64 near)
1694 return near + (s32) (narrow - near);
1697 static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
1698 struct drm_wait_vblank_reply *reply)
1701 struct timespec64 ts;
1704 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
1705 * to store the seconds. This is safe as we always use monotonic
1706 * timestamps since linux-4.15.
1708 reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1709 ts = ktime_to_timespec64(now);
1710 reply->tval_sec = (u32)ts.tv_sec;
1711 reply->tval_usec = ts.tv_nsec / 1000;
1714 int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
1715 struct drm_file *file_priv)
1717 struct drm_crtc *crtc;
1718 struct drm_vblank_crtc *vblank;
1719 union drm_wait_vblank *vblwait = data;
1722 unsigned int pipe_index;
1723 unsigned int flags, pipe, high_pipe;
1725 if (!dev->irq_enabled)
1728 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1731 if (vblwait->request.type &
1732 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1733 _DRM_VBLANK_HIGH_CRTC_MASK)) {
1734 DRM_DEBUG("Unsupported type value 0x%x, supported mask 0x%x\n",
1735 vblwait->request.type,
1736 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1737 _DRM_VBLANK_HIGH_CRTC_MASK));
1741 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1742 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1744 pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1746 pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1748 /* Convert lease-relative crtc index into global crtc index */
1749 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1751 drm_for_each_crtc(crtc, dev) {
1752 if (drm_lease_held(file_priv, crtc->base.id)) {
1753 if (pipe_index == 0)
1763 if (pipe >= dev->num_crtcs)
1766 vblank = &dev->vblank[pipe];
1768 /* If the counter is currently enabled and accurate, short-circuit
1769 * queries to return the cached timestamp of the last vblank.
1771 if (dev->vblank_disable_immediate &&
1772 drm_wait_vblank_is_query(vblwait) &&
1773 READ_ONCE(vblank->enabled)) {
1774 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
1778 ret = drm_vblank_get(dev, pipe);
1780 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1783 seq = drm_vblank_count(dev, pipe);
1785 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1786 case _DRM_VBLANK_RELATIVE:
1787 req_seq = seq + vblwait->request.sequence;
1788 vblwait->request.sequence = req_seq;
1789 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1791 case _DRM_VBLANK_ABSOLUTE:
1792 req_seq = widen_32_to_64(vblwait->request.sequence, seq);
1799 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1800 vblank_passed(seq, req_seq)) {
1802 vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
1803 vblwait->request.sequence = req_seq;
1806 if (flags & _DRM_VBLANK_EVENT) {
1807 /* must hold on to the vblank ref until the event fires
1808 * drm_vblank_put will be called asynchronously
1810 return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
1813 if (req_seq != seq) {
1816 DRM_DEBUG("waiting on vblank count %llu, crtc %u\n",
1818 wait = wait_event_interruptible_timeout(vblank->queue,
1819 vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
1820 !READ_ONCE(vblank->enabled),
1821 msecs_to_jiffies(3000));
1829 /* interrupted by signal */
1838 if (ret != -EINTR) {
1839 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
1841 DRM_DEBUG("crtc %d returning %u to client\n",
1842 pipe, vblwait->reply.sequence);
1844 DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
1848 drm_vblank_put(dev, pipe);
1852 static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1854 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1855 bool high_prec = false;
1856 struct drm_pending_vblank_event *e, *t;
1860 assert_spin_locked(&dev->event_lock);
1862 seq = drm_vblank_count_and_time(dev, pipe, &now);
1864 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1865 if (e->pipe != pipe)
1867 if (!vblank_passed(seq, e->sequence))
1870 DRM_DEBUG("vblank event on %llu, current %llu\n",
1873 list_del(&e->base.link);
1874 drm_vblank_put(dev, pipe);
1875 send_vblank_event(dev, e, seq, now);
1878 if (crtc && crtc->funcs->get_vblank_timestamp)
1881 trace_drm_vblank_event(pipe, seq, now, high_prec);
1885 * drm_handle_vblank - handle a vblank event
1887 * @pipe: index of CRTC where this event occurred
1889 * Drivers should call this routine in their vblank interrupt handlers to
1890 * update the vblank counter and send any signals that may be pending.
1892 * This is the legacy version of drm_crtc_handle_vblank().
1894 bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1896 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1897 unsigned long irqflags;
1900 if (WARN_ON_ONCE(!dev->num_crtcs))
1903 if (WARN_ON(pipe >= dev->num_crtcs))
1906 spin_lock_irqsave(&dev->event_lock, irqflags);
1908 /* Need timestamp lock to prevent concurrent execution with
1909 * vblank enable/disable, as this would cause inconsistent
1910 * or corrupted timestamps and vblank counts.
1912 spin_lock(&dev->vblank_time_lock);
1914 /* Vblank irq handling disabled. Nothing to do. */
1915 if (!vblank->enabled) {
1916 spin_unlock(&dev->vblank_time_lock);
1917 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1921 drm_update_vblank_count(dev, pipe, true);
1923 spin_unlock(&dev->vblank_time_lock);
1925 wake_up(&vblank->queue);
1927 /* With instant-off, we defer disabling the interrupt until after
1928 * we finish processing the following vblank after all events have
1929 * been signaled. The disable has to be last (after
1930 * drm_handle_vblank_events) so that the timestamp is always accurate.
1932 disable_irq = (dev->vblank_disable_immediate &&
1933 drm_vblank_offdelay > 0 &&
1934 !atomic_read(&vblank->refcount));
1936 drm_handle_vblank_events(dev, pipe);
1938 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1941 vblank_disable_fn(&vblank->disable_timer);
1945 EXPORT_SYMBOL(drm_handle_vblank);
1948 * drm_crtc_handle_vblank - handle a vblank event
1949 * @crtc: where this event occurred
1951 * Drivers should call this routine in their vblank interrupt handlers to
1952 * update the vblank counter and send any signals that may be pending.
1954 * This is the native KMS version of drm_handle_vblank().
1956 * Note that for a given vblank counter value drm_crtc_handle_vblank()
1957 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
1958 * provide a barrier: Any writes done before calling
1959 * drm_crtc_handle_vblank() will be visible to callers of the later
1960 * functions, iff the vblank count is the same or a later one.
1962 * See also &drm_vblank_crtc.count.
1965 * True if the event was successfully handled, false on failure.
1967 bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1969 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1971 EXPORT_SYMBOL(drm_crtc_handle_vblank);
1974 * Get crtc VBLANK count.
1976 * \param dev DRM device
1977 * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
1978 * \param file_priv drm file private for the user's open file descriptor
1981 int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
1982 struct drm_file *file_priv)
1984 struct drm_crtc *crtc;
1985 struct drm_vblank_crtc *vblank;
1987 struct drm_crtc_get_sequence *get_seq = data;
1989 bool vblank_enabled;
1992 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1995 if (!dev->irq_enabled)
1998 crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
2002 pipe = drm_crtc_index(crtc);
2004 vblank = &dev->vblank[pipe];
2005 vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
2007 if (!vblank_enabled) {
2008 ret = drm_crtc_vblank_get(crtc);
2010 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
2014 drm_modeset_lock(&crtc->mutex, NULL);
2016 get_seq->active = crtc->state->enable;
2018 get_seq->active = crtc->enabled;
2019 drm_modeset_unlock(&crtc->mutex);
2020 get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
2021 get_seq->sequence_ns = ktime_to_ns(now);
2022 if (!vblank_enabled)
2023 drm_crtc_vblank_put(crtc);
2028 * Queue a event for VBLANK sequence
2030 * \param dev DRM device
2031 * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
2032 * \param file_priv drm file private for the user's open file descriptor
2035 int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
2036 struct drm_file *file_priv)
2038 struct drm_crtc *crtc;
2039 struct drm_vblank_crtc *vblank;
2041 struct drm_crtc_queue_sequence *queue_seq = data;
2043 struct drm_pending_vblank_event *e;
2048 unsigned long spin_flags;
2050 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2053 if (!dev->irq_enabled)
2056 crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
2060 flags = queue_seq->flags;
2061 /* Check valid flag bits */
2062 if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
2063 DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
2066 pipe = drm_crtc_index(crtc);
2068 vblank = &dev->vblank[pipe];
2070 e = kzalloc(sizeof(*e), GFP_KERNEL);
2074 ret = drm_crtc_vblank_get(crtc);
2076 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
2080 seq = drm_vblank_count_and_time(dev, pipe, &now);
2081 req_seq = queue_seq->sequence;
2083 if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
2086 if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && vblank_passed(seq, req_seq))
2090 e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
2091 e->event.base.length = sizeof(e->event.seq);
2092 e->event.seq.user_data = queue_seq->user_data;
2094 spin_lock_irqsave(&dev->event_lock, spin_flags);
2097 * drm_crtc_vblank_off() might have been called after we called
2098 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
2099 * vblank disable, so no need for further locking. The reference from
2100 * drm_crtc_vblank_get() protects against vblank disable from another source.
2102 if (!READ_ONCE(vblank->enabled)) {
2107 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
2113 e->sequence = req_seq;
2115 if (vblank_passed(seq, req_seq)) {
2116 drm_crtc_vblank_put(crtc);
2117 send_vblank_event(dev, e, seq, now);
2118 queue_seq->sequence = seq;
2120 /* drm_handle_vblank_events will call drm_vblank_put */
2121 list_add_tail(&e->base.link, &dev->vblank_event_list);
2122 queue_seq->sequence = req_seq;
2125 spin_unlock_irqrestore(&dev->event_lock, spin_flags);
2129 spin_unlock_irqrestore(&dev->event_lock, spin_flags);
2130 drm_crtc_vblank_put(crtc);