2 * drm_irq.c IRQ and vblank support
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
9 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
11 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
12 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
13 * All Rights Reserved.
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
22 * The above copyright notice and this permission notice (including the next
23 * paragraph) shall be included in all copies or substantial portions of the
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 * OTHER DEALINGS IN THE SOFTWARE.
36 #include "drm_trace.h"
37 #include "drm_internal.h"
39 #include <linux/slab.h>
41 #include <linux/export.h>
43 /* Access macro for slots in vblank timestamp ringbuffer. */
44 #define vblanktimestamp(dev, pipe, count) \
45 ((dev)->vblank[pipe].time[(count) % DRM_VBLANKTIME_RBSIZE])
47 /* Retry timestamp calculation up to 3 times to satisfy
48 * drm_timestamp_precision before giving up.
50 #define DRM_TIMESTAMP_MAXRETRIES 3
52 /* Threshold in nanoseconds for detection of redundant
53 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
55 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
58 drm_get_last_vbltimestamp(struct drm_device
*dev
, unsigned int pipe
,
59 struct timeval
*tvblank
, unsigned flags
);
61 unsigned int drm_timestamp_precision
= 20; /* Default to 20 usecs. */
64 * Default to use monotonic timestamps for wait-for-vblank and page-flip
67 unsigned int drm_timestamp_monotonic
= 1;
69 int drm_vblank_offdelay
= 5000; /* Default to 5000 msecs. */
71 module_param_named(vblankoffdelay
, drm_vblank_offdelay
, int, 0600);
72 module_param_named(timestamp_precision_usec
, drm_timestamp_precision
, int, 0600);
73 module_param_named(timestamp_monotonic
, drm_timestamp_monotonic
, int, 0600);
74 MODULE_PARM_DESC(vblankoffdelay
, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
75 MODULE_PARM_DESC(timestamp_precision_usec
, "Max. error on timestamps [usecs]");
76 MODULE_PARM_DESC(timestamp_monotonic
, "Use monotonic timestamps");
78 static void store_vblank(struct drm_device
*dev
, unsigned int pipe
,
80 struct timeval
*t_vblank
, u32 last
)
82 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
85 assert_spin_locked(&dev
->vblank_time_lock
);
89 /* All writers hold the spinlock, but readers are serialized by
90 * the latching of vblank->count below.
92 tslot
= vblank
->count
+ vblank_count_inc
;
93 vblanktimestamp(dev
, pipe
, tslot
) = *t_vblank
;
96 * vblank timestamp updates are protected on the write side with
97 * vblank_time_lock, but on the read side done locklessly using a
98 * sequence-lock on the vblank counter. Ensure correct ordering using
99 * memory barrriers. We need the barrier both before and also after the
100 * counter update to synchronize with the next timestamp write.
101 * The read-side barriers for this are in drm_vblank_count_and_time.
104 vblank
->count
+= vblank_count_inc
;
109 * drm_reset_vblank_timestamp - reset the last timestamp to the last vblank
111 * @pipe: index of CRTC for which to reset the timestamp
113 * Reset the stored timestamp for the current vblank count to correspond
114 * to the last vblank occurred.
116 * Only to be called from drm_vblank_on().
118 * Note: caller must hold dev->vbl_lock since this reads & writes
119 * device vblank fields.
121 static void drm_reset_vblank_timestamp(struct drm_device
*dev
, unsigned int pipe
)
125 struct timeval t_vblank
;
126 int count
= DRM_TIMESTAMP_MAXRETRIES
;
128 lockmgr(&dev
->vblank_time_lock
, LK_EXCLUSIVE
);
131 * sample the current counter to avoid random jumps
132 * when drm_vblank_enable() applies the diff
135 cur_vblank
= dev
->driver
->get_vblank_counter(dev
, pipe
);
136 rc
= drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, 0);
137 } while (cur_vblank
!= dev
->driver
->get_vblank_counter(dev
, pipe
) && --count
> 0);
140 * Only reinitialize corresponding vblank timestamp if high-precision query
141 * available and didn't fail. Otherwise reinitialize delayed at next vblank
142 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
145 t_vblank
= (struct timeval
) {0, 0};
148 * +1 to make sure user will never see the same
149 * vblank counter value before and after a modeset
151 store_vblank(dev
, pipe
, 1, &t_vblank
, cur_vblank
);
153 lockmgr(&dev
->vblank_time_lock
, LK_RELEASE
);
157 * drm_update_vblank_count - update the master vblank counter
159 * @pipe: counter to update
161 * Call back into the driver to update the appropriate vblank counter
162 * (specified by @pipe). Deal with wraparound, if it occurred, and
163 * update the last read value so we can deal with wraparound on the next
166 * Only necessary when going from off->on, to account for frames we
167 * didn't get an interrupt for.
169 * Note: caller must hold dev->vbl_lock since this reads & writes
170 * device vblank fields.
172 static void drm_update_vblank_count(struct drm_device
*dev
, unsigned int pipe
,
175 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
176 u32 cur_vblank
, diff
;
178 struct timeval t_vblank
;
179 int count
= DRM_TIMESTAMP_MAXRETRIES
;
180 int framedur_ns
= vblank
->framedur_ns
;
183 * Interrupts were disabled prior to this call, so deal with counter
185 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
186 * here if the register is small or we had vblank interrupts off for
189 * We repeat the hardware vblank counter & timestamp query until
190 * we get consistent results. This to prevent races between gpu
191 * updating its hardware counter while we are retrieving the
192 * corresponding vblank timestamp.
195 cur_vblank
= dev
->driver
->get_vblank_counter(dev
, pipe
);
196 rc
= drm_get_last_vbltimestamp(dev
, pipe
, &t_vblank
, flags
);
197 } while (cur_vblank
!= dev
->driver
->get_vblank_counter(dev
, pipe
) && --count
> 0);
199 if (dev
->max_vblank_count
!= 0) {
200 /* trust the hw counter when it's around */
201 diff
= (cur_vblank
- vblank
->last
) & dev
->max_vblank_count
;
202 } else if (rc
&& framedur_ns
) {
203 const struct timeval
*t_old
;
206 t_old
= &vblanktimestamp(dev
, pipe
, vblank
->count
);
207 diff_ns
= timeval_to_ns(&t_vblank
) - timeval_to_ns(t_old
);
210 * Figure out how many vblanks we've missed based
211 * on the difference in the timestamps and the
212 * frame/field duration.
214 diff
= DIV_ROUND_CLOSEST_ULL(diff_ns
, framedur_ns
);
216 if (diff
== 0 && flags
& DRM_CALLED_FROM_VBLIRQ
)
217 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
218 " diff_ns = %lld, framedur_ns = %d)\n",
219 pipe
, (long long) diff_ns
, framedur_ns
);
221 /* some kind of default for drivers w/o accurate vbl timestamping */
222 diff
= (flags
& DRM_CALLED_FROM_VBLIRQ
) != 0;
226 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
227 * interval? If so then vblank irqs keep running and it will likely
228 * happen that the hardware vblank counter is not trustworthy as it
229 * might reset at some point in that interval and vblank timestamps
230 * are not trustworthy either in that interval. Iow. this can result
231 * in a bogus diff >> 1 which must be avoided as it would cause
232 * random large forward jumps of the software vblank counter.
234 if (diff
> 1 && (vblank
->inmodeset
& 0x2)) {
235 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
236 " due to pre-modeset.\n", pipe
, diff
);
241 * FIMXE: Need to replace this hack with proper seqlocks.
243 * Restrict the bump of the software vblank counter to a safe maximum
244 * value of +1 whenever there is the possibility that concurrent readers
245 * of vblank timestamps could be active at the moment, as the current
246 * implementation of the timestamp caching and updating is not safe
247 * against concurrent readers for calls to store_vblank() with a bump
248 * of anything but +1. A bump != 1 would very likely return corrupted
249 * timestamps to userspace, because the same slot in the cache could
250 * be concurrently written by store_vblank() and read by one of those
251 * readers without the read-retry logic detecting the collision.
253 * Concurrent readers can exist when we are called from the
254 * drm_vblank_off() or drm_vblank_on() functions and other non-vblank-
255 * irq callers. However, all those calls to us are happening with the
256 * vbl_lock locked to prevent drm_vblank_get(), so the vblank refcount
257 * can't increase while we are executing. Therefore a zero refcount at
258 * this point is safe for arbitrary counter bumps if we are called
259 * outside vblank irq, a non-zero count is not 100% safe. Unfortunately
260 * we must also accept a refcount of 1, as whenever we are called from
261 * drm_vblank_get() -> drm_vblank_enable() the refcount will be 1 and
262 * we must let that one pass through in order to not lose vblank counts
263 * during vblank irq off - which would completely defeat the whole
264 * point of this routine.
266 * Whenever we are called from vblank irq, we have to assume concurrent
267 * readers exist or can show up any time during our execution, even if
268 * the refcount is currently zero, as vblank irqs are usually only
269 * enabled due to the presence of readers, and because when we are called
270 * from vblank irq we can't hold the vbl_lock to protect us from sudden
271 * bumps in vblank refcount. Therefore also restrict bumps to +1 when
272 * called from vblank irq.
274 if ((diff
> 1) && (atomic_read(&vblank
->refcount
) > 1 ||
275 (flags
& DRM_CALLED_FROM_VBLIRQ
))) {
276 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u "
277 "refcount %u, vblirq %u\n", pipe
, diff
,
278 atomic_read(&vblank
->refcount
),
279 (flags
& DRM_CALLED_FROM_VBLIRQ
) != 0);
283 DRM_DEBUG_VBL("updating vblank count on crtc %u:"
284 " current=%u, diff=%u, hw=%u hw_last=%u\n",
285 pipe
, vblank
->count
, diff
, cur_vblank
, vblank
->last
);
288 WARN_ON_ONCE(cur_vblank
!= vblank
->last
);
293 * Only reinitialize corresponding vblank timestamp if high-precision query
294 * available and didn't fail, or we were called from the vblank interrupt.
295 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
296 * for now, to mark the vblanktimestamp as invalid.
298 if (!rc
&& (flags
& DRM_CALLED_FROM_VBLIRQ
) == 0)
299 t_vblank
= (struct timeval
) {0, 0};
301 store_vblank(dev
, pipe
, diff
, &t_vblank
, cur_vblank
);
305 * Disable vblank irq's on crtc, make sure that last vblank count
306 * of hardware and corresponding consistent software vblank counter
307 * are preserved, even if there are any spurious vblank irq's after
310 static void vblank_disable_and_save(struct drm_device
*dev
, unsigned int pipe
)
312 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
313 unsigned long irqflags
;
315 /* Prevent vblank irq processing while disabling vblank irqs,
316 * so no updates of timestamps or count can happen after we've
317 * disabled. Needed to prevent races in case of delayed irq's.
319 spin_lock_irqsave(&dev
->vblank_time_lock
, irqflags
);
322 * Only disable vblank interrupts if they're enabled. This avoids
323 * calling the ->disable_vblank() operation in atomic context with the
324 * hardware potentially runtime suspended.
326 if (vblank
->enabled
) {
327 dev
->driver
->disable_vblank(dev
, pipe
);
328 vblank
->enabled
= false;
332 * Always update the count and timestamp to maintain the
333 * appearance that the counter has been ticking all along until
334 * this time. This makes the count account for the entire time
335 * between drm_vblank_on() and drm_vblank_off().
337 drm_update_vblank_count(dev
, pipe
, 0);
339 spin_unlock_irqrestore(&dev
->vblank_time_lock
, irqflags
);
342 static void vblank_disable_fn(unsigned long arg
)
344 struct drm_vblank_crtc
*vblank
= (void *)arg
;
345 struct drm_device
*dev
= vblank
->dev
;
346 unsigned int pipe
= vblank
->pipe
;
347 unsigned long irqflags
;
349 if (!dev
->vblank_disable_allowed
)
352 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
353 if (atomic_read(&vblank
->refcount
) == 0 && vblank
->enabled
) {
354 DRM_DEBUG_VBLANK("disabling vblank on crtc %u\n", pipe
);
355 vblank_disable_and_save(dev
, pipe
);
357 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
361 * drm_vblank_cleanup - cleanup vblank support
364 * This function cleans up any resources allocated in drm_vblank_init.
366 void drm_vblank_cleanup(struct drm_device
*dev
)
370 /* Bail if the driver didn't call drm_vblank_init() */
371 if (dev
->num_crtcs
== 0)
374 for (pipe
= 0; pipe
< dev
->num_crtcs
; pipe
++) {
375 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
377 WARN_ON(vblank
->enabled
&&
378 drm_core_check_feature(dev
, DRIVER_MODESET
));
380 del_timer_sync(&vblank
->disable_timer
);
387 EXPORT_SYMBOL(drm_vblank_cleanup
);
390 * drm_vblank_init - initialize vblank support
392 * @num_crtcs: number of CRTCs supported by @dev
394 * This function initializes vblank support for @num_crtcs display pipelines.
397 * Zero on success or a negative error code on failure.
399 int drm_vblank_init(struct drm_device
*dev
, unsigned int num_crtcs
)
404 lockinit(&dev
->vbl_lock
, "drmvbl", 0, LK_CANRECURSE
);
405 lockinit(&dev
->vblank_time_lock
, "drmvtl", 0, LK_CANRECURSE
);
407 dev
->num_crtcs
= num_crtcs
;
409 dev
->vblank
= kcalloc(num_crtcs
, sizeof(*dev
->vblank
), GFP_KERNEL
);
413 for (i
= 0; i
< num_crtcs
; i
++) {
414 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[i
];
418 init_waitqueue_head(&vblank
->queue
);
419 setup_timer(&vblank
->disable_timer
, vblank_disable_fn
,
420 (unsigned long)vblank
);
423 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
425 /* Driver specific high-precision vblank timestamping supported? */
426 if (dev
->driver
->get_vblank_timestamp
)
427 DRM_INFO("Driver supports precise vblank timestamp query.\n");
429 DRM_INFO("No driver support for vblank timestamp query.\n");
431 /* Must have precise timestamping for reliable vblank instant disable */
432 if (dev
->vblank_disable_immediate
&& !dev
->driver
->get_vblank_timestamp
) {
433 dev
->vblank_disable_immediate
= false;
434 DRM_INFO("Setting vblank_disable_immediate to false because "
435 "get_vblank_timestamp == NULL\n");
438 dev
->vblank_disable_allowed
= false;
446 EXPORT_SYMBOL(drm_vblank_init
);
449 static void drm_irq_vgaarb_nokms(void *cookie
, bool state
)
451 struct drm_device
*dev
= cookie
;
453 if (dev
->driver
->vgaarb_irq
) {
454 dev
->driver
->vgaarb_irq(dev
, state
);
458 if (!dev
->irq_enabled
)
462 if (dev
->driver
->irq_uninstall
)
463 dev
->driver
->irq_uninstall(dev
);
465 if (dev
->driver
->irq_preinstall
)
466 dev
->driver
->irq_preinstall(dev
);
467 if (dev
->driver
->irq_postinstall
)
468 dev
->driver
->irq_postinstall(dev
);
474 * drm_irq_install - install IRQ handler
476 * @irq: IRQ number to install the handler for
478 * Initializes the IRQ related data. Installs the handler, calling the driver
479 * irq_preinstall() and irq_postinstall() functions before and after the
482 * This is the simplified helper interface provided for drivers with no special
483 * needs. Drivers which need to install interrupt handlers for multiple
484 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
485 * that vblank interrupts are available.
488 * Zero on success or a negative error code on failure.
490 int drm_irq_install(struct drm_device
*dev
, int irq
)
494 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
500 /* Driver must have been initialized */
501 if (!dev
->dev_private
)
504 if (dev
->irq_enabled
)
506 dev
->irq_enabled
= true;
508 DRM_DEBUG("irq=%d\n", irq
);
510 /* Before installing handler */
511 if (dev
->driver
->irq_preinstall
)
512 dev
->driver
->irq_preinstall(dev
);
514 /* Install handler */
515 ret
= -bus_setup_intr(dev
->dev
->bsddev
, dev
->irqr
, INTR_MPSAFE
,
516 dev
->driver
->irq_handler
, dev
, &dev
->irqh
, &dev
->irq_lock
);
519 dev
->irq_enabled
= false;
523 /* After installing handler */
524 if (dev
->driver
->irq_postinstall
)
525 ret
= dev
->driver
->irq_postinstall(dev
);
528 dev
->irq_enabled
= false;
529 bus_teardown_intr(dev
->dev
->bsddev
, dev
->irqr
, dev
->irqh
);
536 EXPORT_SYMBOL(drm_irq_install
);
539 * drm_irq_uninstall - uninstall the IRQ handler
542 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
543 * This should only be called by drivers which used drm_irq_install() to set up
544 * their interrupt handler. Other drivers must only reset
545 * drm_device->irq_enabled to false.
547 * Note that for kernel modesetting drivers it is a bug if this function fails.
548 * The sanity checks are only to catch buggy user modesetting drivers which call
549 * the same function through an ioctl.
552 * Zero on success or a negative error code on failure.
554 int drm_irq_uninstall(struct drm_device
*dev
)
556 unsigned long irqflags
;
560 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
563 irq_enabled
= dev
->irq_enabled
;
564 dev
->irq_enabled
= false;
567 * Wake up any waiters so they don't hang. This is just to paper over
568 * isssues for UMS drivers which aren't in full control of their
569 * vblank/irq handling. KMS drivers must ensure that vblanks are all
570 * disabled when uninstalling the irq handler.
572 if (dev
->num_crtcs
) {
573 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
574 for (i
= 0; i
< dev
->num_crtcs
; i
++) {
575 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[i
];
577 if (!vblank
->enabled
)
580 WARN_ON(drm_core_check_feature(dev
, DRIVER_MODESET
));
582 vblank_disable_and_save(dev
, i
);
583 wake_up(&vblank
->queue
);
585 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
591 DRM_DEBUG("irq=%d\n", dev
->irq
);
593 if (dev
->driver
->irq_uninstall
)
594 dev
->driver
->irq_uninstall(dev
);
596 bus_teardown_intr(dev
->dev
->bsddev
, dev
->irqr
, dev
->irqh
);
600 EXPORT_SYMBOL(drm_irq_uninstall
);
605 * \param inode device inode.
606 * \param file_priv DRM file private.
607 * \param cmd command.
608 * \param arg user argument, pointing to a drm_control structure.
609 * \return zero on success or a negative number on failure.
611 * Calls irq_install() or irq_uninstall() according to \p arg.
613 int drm_control(struct drm_device
*dev
, void *data
,
614 struct drm_file
*file_priv
)
616 struct drm_control
*ctl
= data
;
619 /* if we haven't irq we fallback for compatibility reasons -
620 * this used to be a separate function in drm_dma.h
623 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
625 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
627 /* UMS was only ever support on pci devices. */
628 if (WARN_ON(!dev
->pdev
))
632 case DRM_INST_HANDLER
:
635 if (dev
->if_version
< DRM_IF_VERSION(1, 2) &&
638 mutex_lock(&dev
->struct_mutex
);
639 ret
= drm_irq_install(dev
, irq
);
640 mutex_unlock(&dev
->struct_mutex
);
643 case DRM_UNINST_HANDLER
:
644 mutex_lock(&dev
->struct_mutex
);
645 ret
= drm_irq_uninstall(dev
);
646 mutex_unlock(&dev
->struct_mutex
);
655 * drm_calc_timestamping_constants - calculate vblank timestamp constants
656 * @crtc: drm_crtc whose timestamp constants should be updated.
657 * @mode: display mode containing the scanout timings
659 * Calculate and store various constants which are later
660 * needed by vblank and swap-completion timestamping, e.g,
661 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
662 * derived from CRTC's true scanout timing, so they take
663 * things like panel scaling or other adjustments into account.
665 void drm_calc_timestamping_constants(struct drm_crtc
*crtc
,
666 const struct drm_display_mode
*mode
)
668 struct drm_device
*dev
= crtc
->dev
;
669 unsigned int pipe
= drm_crtc_index(crtc
);
670 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
671 int linedur_ns
= 0, framedur_ns
= 0;
672 int dotclock
= mode
->crtc_clock
;
677 if (WARN_ON(pipe
>= dev
->num_crtcs
))
680 /* Valid dotclock? */
682 int frame_size
= mode
->crtc_htotal
* mode
->crtc_vtotal
;
685 * Convert scanline length in pixels and video
686 * dot clock to line duration and frame duration
689 linedur_ns
= div_u64((u64
) mode
->crtc_htotal
* 1000000, dotclock
);
690 framedur_ns
= div_u64((u64
) frame_size
* 1000000, dotclock
);
693 * Fields of interlaced scanout modes are only half a frame duration.
695 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
698 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
701 vblank
->linedur_ns
= linedur_ns
;
702 vblank
->framedur_ns
= framedur_ns
;
704 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
705 crtc
->base
.id
, mode
->crtc_htotal
,
706 mode
->crtc_vtotal
, mode
->crtc_vdisplay
);
707 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
708 crtc
->base
.id
, dotclock
, framedur_ns
, linedur_ns
);
710 EXPORT_SYMBOL(drm_calc_timestamping_constants
);
713 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
715 * @pipe: index of CRTC whose vblank timestamp to retrieve
716 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
717 * On return contains true maximum error of timestamp
718 * @vblank_time: Pointer to struct timeval which should receive the timestamp
719 * @flags: Flags to pass to driver:
721 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
722 * @mode: mode which defines the scanout timings
724 * Implements calculation of exact vblank timestamps from given drm_display_mode
725 * timings and current video scanout position of a CRTC. This can be called from
726 * within get_vblank_timestamp() implementation of a kms driver to implement the
727 * actual timestamping.
729 * Should return timestamps conforming to the OML_sync_control OpenML
730 * extension specification. The timestamp corresponds to the end of
731 * the vblank interval, aka start of scanout of topmost-leftmost display
732 * pixel in the following video frame.
734 * Requires support for optional dev->driver->get_scanout_position()
735 * in kms driver, plus a bit of setup code to provide a drm_display_mode
736 * that corresponds to the true scanout timing.
738 * The current implementation only handles standard video modes. It
739 * returns as no operation if a doublescan or interlaced video mode is
740 * active. Higher level code is expected to handle this.
743 * Negative value on error, failure or if not supported in current
746 * -EINVAL - Invalid CRTC.
747 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
748 * -ENOTSUPP - Function not supported in current display mode.
749 * -EIO - Failed, e.g., due to failed scanout position query.
751 * Returns or'ed positive status flags on success:
753 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
754 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
757 int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device
*dev
,
760 struct timeval
*vblank_time
,
762 const struct drm_display_mode
*mode
)
764 struct timeval tv_etime
;
765 ktime_t stime
, etime
;
766 unsigned int vbl_status
;
767 int ret
= DRM_VBLANKTIME_SCANOUTPOS_METHOD
;
769 int delta_ns
, duration_ns
;
771 if (pipe
>= dev
->num_crtcs
) {
772 DRM_ERROR("Invalid crtc %u\n", pipe
);
776 /* Scanout position query not supported? Should not happen. */
777 if (!dev
->driver
->get_scanout_position
) {
778 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
782 /* If mode timing undefined, just return as no-op:
783 * Happens during initial modesetting of a crtc.
785 if (mode
->crtc_clock
== 0) {
786 DRM_DEBUG_VBLANK("crtc %u: Noop due to uninitialized mode.\n", pipe
);
790 /* Get current scanout position with system timestamp.
791 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
792 * if single query takes longer than max_error nanoseconds.
794 * This guarantees a tight bound on maximum error if
795 * code gets preempted or delayed for some reason.
797 for (i
= 0; i
< DRM_TIMESTAMP_MAXRETRIES
; i
++) {
799 * Get vertical and horizontal scanout position vpos, hpos,
800 * and bounding timestamps stime, etime, pre/post query.
802 vbl_status
= dev
->driver
->get_scanout_position(dev
, pipe
, flags
,
807 /* Return as no-op if scanout query unsupported or failed. */
808 if (!(vbl_status
& DRM_SCANOUTPOS_VALID
)) {
809 DRM_DEBUG_VBLANK("crtc %u : scanoutpos query failed [0x%x].\n",
814 /* Compute uncertainty in timestamp of scanout position query. */
815 duration_ns
= ktime_to_ns(etime
) - ktime_to_ns(stime
);
817 /* Accept result with < max_error nsecs timing uncertainty. */
818 if (duration_ns
<= *max_error
)
822 /* Noisy system timing? */
823 if (i
== DRM_TIMESTAMP_MAXRETRIES
) {
824 DRM_DEBUG_VBLANK("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
825 pipe
, duration_ns
/1000, *max_error
/1000, i
);
828 /* Return upper bound of timestamp precision error. */
829 *max_error
= duration_ns
;
831 /* Check if in vblank area:
832 * vpos is >=0 in video scanout area, but negative
833 * within vblank area, counting down the number of lines until
836 if (vbl_status
& DRM_SCANOUTPOS_IN_VBLANK
)
837 ret
|= DRM_VBLANKTIME_IN_VBLANK
;
839 /* Convert scanout position into elapsed time at raw_time query
840 * since start of scanout at first display scanline. delta_ns
841 * can be negative if start of scanout hasn't happened yet.
843 delta_ns
= div_s64(1000000LL * (vpos
* mode
->crtc_htotal
+ hpos
),
846 if (!drm_timestamp_monotonic
)
847 etime
= ktime_mono_to_real(etime
);
849 /* save this only for debugging purposes */
850 tv_etime
= ktime_to_timeval(etime
);
851 /* Subtract time delta from raw timestamp to get final
852 * vblank_time timestamp for end of vblank.
855 etime
= ktime_add_ns(etime
, -delta_ns
);
857 etime
= ktime_sub_ns(etime
, delta_ns
);
858 *vblank_time
= ktime_to_timeval(etime
);
860 DRM_DEBUG_VBLANK("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
861 pipe
, vbl_status
, hpos
, vpos
,
862 (long)tv_etime
.tv_sec
, (long)tv_etime
.tv_usec
,
863 (long)vblank_time
->tv_sec
, (long)vblank_time
->tv_usec
,
864 duration_ns
/1000, i
);
868 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos
);
870 static struct timeval
get_drm_timestamp(void)
874 now
= drm_timestamp_monotonic
? ktime_get() : ktime_get_real();
875 return ktime_to_timeval(now
);
879 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
882 * @pipe: index of CRTC whose vblank timestamp to retrieve
883 * @tvblank: Pointer to target struct timeval which should receive the timestamp
884 * @flags: Flags to pass to driver:
886 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
888 * Fetches the system timestamp corresponding to the time of the most recent
889 * vblank interval on specified CRTC. May call into kms-driver to
890 * compute the timestamp with a high-precision GPU specific method.
892 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
893 * call, i.e., it isn't very precisely locked to the true vblank.
896 * True if timestamp is considered to be very precise, false otherwise.
899 drm_get_last_vbltimestamp(struct drm_device
*dev
, unsigned int pipe
,
900 struct timeval
*tvblank
, unsigned flags
)
904 /* Define requested maximum error on timestamps (nanoseconds). */
905 int max_error
= (int) drm_timestamp_precision
* 1000;
907 /* Query driver if possible and precision timestamping enabled. */
908 if (dev
->driver
->get_vblank_timestamp
&& (max_error
> 0)) {
909 ret
= dev
->driver
->get_vblank_timestamp(dev
, pipe
, &max_error
,
915 /* GPU high precision timestamp query unsupported or failed.
916 * Return current monotonic/gettimeofday timestamp as best estimate.
918 *tvblank
= get_drm_timestamp();
924 * drm_vblank_count - retrieve "cooked" vblank counter value
926 * @pipe: index of CRTC for which to retrieve the counter
928 * Fetches the "cooked" vblank count value that represents the number of
929 * vblank events since the system was booted, including lost events due to
930 * modesetting activity.
932 * This is the legacy version of drm_crtc_vblank_count().
935 * The software vblank counter.
937 u32
drm_vblank_count(struct drm_device
*dev
, unsigned int pipe
)
939 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
941 if (WARN_ON(pipe
>= dev
->num_crtcs
))
944 return vblank
->count
;
946 EXPORT_SYMBOL(drm_vblank_count
);
949 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
950 * @crtc: which counter to retrieve
952 * Fetches the "cooked" vblank count value that represents the number of
953 * vblank events since the system was booted, including lost events due to
954 * modesetting activity.
956 * This is the native KMS version of drm_vblank_count().
959 * The software vblank counter.
961 u32
drm_crtc_vblank_count(struct drm_crtc
*crtc
)
963 return drm_vblank_count(crtc
->dev
, drm_crtc_index(crtc
));
965 EXPORT_SYMBOL(drm_crtc_vblank_count
);
968 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
969 * system timestamp corresponding to that vblank counter value.
971 * @pipe: index of CRTC whose counter to retrieve
972 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
974 * Fetches the "cooked" vblank count value that represents the number of
975 * vblank events since the system was booted, including lost events due to
976 * modesetting activity. Returns corresponding system timestamp of the time
977 * of the vblank interval that corresponds to the current vblank counter value.
979 * This is the legacy version of drm_crtc_vblank_count_and_time().
981 u32
drm_vblank_count_and_time(struct drm_device
*dev
, unsigned int pipe
,
982 struct timeval
*vblanktime
)
984 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
985 int count
= DRM_TIMESTAMP_MAXRETRIES
;
988 vblanktime
->tv_sec
= 0; /* silence gcc warning */
989 vblanktime
->tv_usec
= 0; /* silence gcc warning */
990 if (WARN_ON(pipe
>= dev
->num_crtcs
))
994 * Vblank timestamps are read lockless. To ensure consistency the vblank
995 * counter is rechecked and ordering is ensured using memory barriers.
996 * This works like a seqlock. The write-side barriers are in store_vblank.
999 cur_vblank
= vblank
->count
;
1001 *vblanktime
= vblanktimestamp(dev
, pipe
, cur_vblank
);
1003 } while (cur_vblank
!= vblank
->count
&& --count
> 0);
1007 EXPORT_SYMBOL(drm_vblank_count_and_time
);
1010 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
1011 * and the system timestamp corresponding to that vblank counter value
1012 * @crtc: which counter to retrieve
1013 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
1015 * Fetches the "cooked" vblank count value that represents the number of
1016 * vblank events since the system was booted, including lost events due to
1017 * modesetting activity. Returns corresponding system timestamp of the time
1018 * of the vblank interval that corresponds to the current vblank counter value.
1020 * This is the native KMS version of drm_vblank_count_and_time().
1022 u32
drm_crtc_vblank_count_and_time(struct drm_crtc
*crtc
,
1023 struct timeval
*vblanktime
)
1025 return drm_vblank_count_and_time(crtc
->dev
, drm_crtc_index(crtc
),
1028 EXPORT_SYMBOL(drm_crtc_vblank_count_and_time
);
1030 static void send_vblank_event(struct drm_device
*dev
,
1031 struct drm_pending_vblank_event
*e
,
1032 unsigned long seq
, struct timeval
*now
)
1034 assert_spin_locked(&dev
->event_lock
);
1036 e
->event
.sequence
= seq
;
1037 e
->event
.tv_sec
= now
->tv_sec
;
1038 e
->event
.tv_usec
= now
->tv_usec
;
1040 list_add_tail(&e
->base
.link
,
1041 &e
->base
.file_priv
->event_list
);
1042 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
1043 #ifdef __DragonFly__
1044 KNOTE(&e
->base
.file_priv
->dkq
.ki_note
, 0);
1046 trace_drm_vblank_event_delivered(e
->base
.pid
, e
->pipe
,
1051 * drm_arm_vblank_event - arm vblank event after pageflip
1054 * @e: the event to prepare to send
1056 * A lot of drivers need to generate vblank events for the very next vblank
1057 * interrupt. For example when the page flip interrupt happens when the page
1058 * flip gets armed, but not when it actually executes within the next vblank
1059 * period. This helper function implements exactly the required vblank arming
1062 * Caller must hold event lock. Caller must also hold a vblank reference for
1063 * the event @e, which will be dropped when the next vblank arrives.
1065 * This is the legacy version of drm_crtc_arm_vblank_event().
1067 void drm_arm_vblank_event(struct drm_device
*dev
, unsigned int pipe
,
1068 struct drm_pending_vblank_event
*e
)
1070 assert_spin_locked(&dev
->event_lock
);
1073 e
->event
.sequence
= drm_vblank_count(dev
, pipe
);
1074 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
1076 EXPORT_SYMBOL(drm_arm_vblank_event
);
1079 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1080 * @crtc: the source CRTC of the vblank event
1081 * @e: the event to send
1083 * A lot of drivers need to generate vblank events for the very next vblank
1084 * interrupt. For example when the page flip interrupt happens when the page
1085 * flip gets armed, but not when it actually executes within the next vblank
1086 * period. This helper function implements exactly the required vblank arming
1089 * Caller must hold event lock. Caller must also hold a vblank reference for
1090 * the event @e, which will be dropped when the next vblank arrives.
1092 * This is the native KMS version of drm_arm_vblank_event().
1094 void drm_crtc_arm_vblank_event(struct drm_crtc
*crtc
,
1095 struct drm_pending_vblank_event
*e
)
1097 drm_arm_vblank_event(crtc
->dev
, drm_crtc_index(crtc
), e
);
1099 EXPORT_SYMBOL(drm_crtc_arm_vblank_event
);
1102 * drm_send_vblank_event - helper to send vblank event after pageflip
1105 * @e: the event to send
1107 * Updates sequence # and timestamp on event, and sends it to userspace.
1108 * Caller must hold event lock.
1110 * This is the legacy version of drm_crtc_send_vblank_event().
1112 void drm_send_vblank_event(struct drm_device
*dev
, unsigned int pipe
,
1113 struct drm_pending_vblank_event
*e
)
1118 if (dev
->num_crtcs
> 0) {
1119 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1123 now
= get_drm_timestamp();
1126 send_vblank_event(dev
, e
, seq
, &now
);
1128 EXPORT_SYMBOL(drm_send_vblank_event
);
1131 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1132 * @crtc: the source CRTC of the vblank event
1133 * @e: the event to send
1135 * Updates sequence # and timestamp on event, and sends it to userspace.
1136 * Caller must hold event lock.
1138 * This is the native KMS version of drm_send_vblank_event().
1140 void drm_crtc_send_vblank_event(struct drm_crtc
*crtc
,
1141 struct drm_pending_vblank_event
*e
)
1143 drm_send_vblank_event(crtc
->dev
, drm_crtc_index(crtc
), e
);
1145 EXPORT_SYMBOL(drm_crtc_send_vblank_event
);
1148 * drm_vblank_enable - enable the vblank interrupt on a CRTC
1153 * Zero on success or a negative error code on failure.
1155 static int drm_vblank_enable(struct drm_device
*dev
, unsigned int pipe
)
1157 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1160 assert_spin_locked(&dev
->vbl_lock
);
1162 lockmgr(&dev
->vblank_time_lock
, LK_EXCLUSIVE
);
1164 if (!vblank
->enabled
) {
1166 * Enable vblank irqs under vblank_time_lock protection.
1167 * All vblank count & timestamp updates are held off
1168 * until we are done reinitializing master counter and
1169 * timestamps. Filtercode in drm_handle_vblank() will
1170 * prevent double-accounting of same vblank interval.
1172 ret
= dev
->driver
->enable_vblank(dev
, pipe
);
1173 DRM_DEBUG_VBLANK("enabling vblank on crtc %u, ret: %d\n", pipe
, ret
);
1175 atomic_dec(&vblank
->refcount
);
1177 vblank
->enabled
= true;
1178 drm_update_vblank_count(dev
, pipe
, 0);
1182 lockmgr(&dev
->vblank_time_lock
, LK_RELEASE
);
1188 * drm_vblank_get - get a reference count on vblank events
1190 * @pipe: index of CRTC to own
1192 * Acquire a reference count on vblank events to avoid having them disabled
1195 * This is the legacy version of drm_crtc_vblank_get().
1198 * Zero on success or a negative error code on failure.
1200 int drm_vblank_get(struct drm_device
*dev
, unsigned int pipe
)
1202 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1203 unsigned long irqflags
;
1206 if (!dev
->num_crtcs
)
1209 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1212 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1213 /* Going from 0->1 means we have to enable interrupts again */
1214 if (atomic_add_return(1, &vblank
->refcount
) == 1) {
1215 ret
= drm_vblank_enable(dev
, pipe
);
1217 if (!vblank
->enabled
) {
1218 atomic_dec(&vblank
->refcount
);
1222 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1226 EXPORT_SYMBOL(drm_vblank_get
);
1229 * drm_crtc_vblank_get - get a reference count on vblank events
1230 * @crtc: which CRTC to own
1232 * Acquire a reference count on vblank events to avoid having them disabled
1235 * This is the native kms version of drm_vblank_get().
1238 * Zero on success or a negative error code on failure.
1240 int drm_crtc_vblank_get(struct drm_crtc
*crtc
)
1242 return drm_vblank_get(crtc
->dev
, drm_crtc_index(crtc
));
1244 EXPORT_SYMBOL(drm_crtc_vblank_get
);
1247 * drm_vblank_put - release ownership of vblank events
1249 * @pipe: index of CRTC to release
1251 * Release ownership of a given vblank counter, turning off interrupts
1252 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1254 * This is the legacy version of drm_crtc_vblank_put().
1256 void drm_vblank_put(struct drm_device
*dev
, unsigned int pipe
)
1258 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1260 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1263 if (WARN_ON(atomic_read(&vblank
->refcount
) == 0))
1266 /* Last user schedules interrupt disable */
1267 if (atomic_dec_and_test(&vblank
->refcount
)) {
1268 if (drm_vblank_offdelay
== 0)
1270 else if (dev
->vblank_disable_immediate
|| drm_vblank_offdelay
< 0)
1271 vblank_disable_fn((unsigned long)vblank
);
1273 mod_timer(&vblank
->disable_timer
,
1274 jiffies
+ ((drm_vblank_offdelay
* HZ
)/1000));
1277 EXPORT_SYMBOL(drm_vblank_put
);
1280 * drm_crtc_vblank_put - give up ownership of vblank events
1281 * @crtc: which counter to give up
1283 * Release ownership of a given vblank counter, turning off interrupts
1284 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1286 * This is the native kms version of drm_vblank_put().
1288 void drm_crtc_vblank_put(struct drm_crtc
*crtc
)
1290 drm_vblank_put(crtc
->dev
, drm_crtc_index(crtc
));
1292 EXPORT_SYMBOL(drm_crtc_vblank_put
);
1295 * drm_wait_one_vblank - wait for one vblank
1299 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1300 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1301 * due to lack of driver support or because the crtc is off.
1303 void drm_wait_one_vblank(struct drm_device
*dev
, unsigned int pipe
)
1305 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1309 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1312 ret
= drm_vblank_get(dev
, pipe
);
1313 if (WARN(ret
, "vblank not available on crtc %i, ret=%i\n", pipe
, ret
))
1316 last
= drm_vblank_count(dev
, pipe
);
1318 ret
= wait_event_timeout(vblank
->queue
,
1319 last
!= drm_vblank_count(dev
, pipe
),
1320 msecs_to_jiffies(100));
1322 WARN(ret
== 0, "vblank wait timed out on crtc %i\n", pipe
);
1324 drm_vblank_put(dev
, pipe
);
1326 EXPORT_SYMBOL(drm_wait_one_vblank
);
1329 * drm_crtc_wait_one_vblank - wait for one vblank
1332 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1333 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1334 * due to lack of driver support or because the crtc is off.
1336 void drm_crtc_wait_one_vblank(struct drm_crtc
*crtc
)
1338 drm_wait_one_vblank(crtc
->dev
, drm_crtc_index(crtc
));
1340 EXPORT_SYMBOL(drm_crtc_wait_one_vblank
);
1343 * drm_vblank_off - disable vblank events on a CRTC
1347 * Drivers can use this function to shut down the vblank interrupt handling when
1348 * disabling a crtc. This function ensures that the latest vblank frame count is
1349 * stored so that drm_vblank_on() can restore it again.
1351 * Drivers must use this function when the hardware vblank counter can get
1352 * reset, e.g. when suspending.
1354 * This is the legacy version of drm_crtc_vblank_off().
1356 void drm_vblank_off(struct drm_device
*dev
, unsigned int pipe
)
1358 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1359 struct drm_pending_vblank_event
*e
, *t
;
1361 unsigned long irqflags
;
1364 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1367 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
1369 lockmgr(&dev
->vbl_lock
, LK_EXCLUSIVE
);
1370 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1371 pipe
, vblank
->enabled
, vblank
->inmodeset
);
1373 /* Avoid redundant vblank disables without previous drm_vblank_on(). */
1374 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
) || !vblank
->inmodeset
)
1375 vblank_disable_and_save(dev
, pipe
);
1377 wake_up(&vblank
->queue
);
1380 * Prevent subsequent drm_vblank_get() from re-enabling
1381 * the vblank interrupt by bumping the refcount.
1383 if (!vblank
->inmodeset
) {
1384 atomic_inc(&vblank
->refcount
);
1385 vblank
->inmodeset
= 1;
1387 lockmgr(&dev
->vbl_lock
, LK_RELEASE
);
1389 /* Send any queued vblank events, lest the natives grow disquiet */
1390 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1392 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1393 if (e
->pipe
!= pipe
)
1395 DRM_DEBUG_VBLANK("Sending premature vblank event on disable: \
1396 wanted %d, current %d\n",
1397 e
->event
.sequence
, seq
);
1398 list_del(&e
->base
.link
);
1399 drm_vblank_put(dev
, pipe
);
1400 send_vblank_event(dev
, e
, seq
, &now
);
1402 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1404 EXPORT_SYMBOL(drm_vblank_off
);
1407 * drm_crtc_vblank_off - disable vblank events on a CRTC
1408 * @crtc: CRTC in question
1410 * Drivers can use this function to shut down the vblank interrupt handling when
1411 * disabling a crtc. This function ensures that the latest vblank frame count is
1412 * stored so that drm_vblank_on can restore it again.
1414 * Drivers must use this function when the hardware vblank counter can get
1415 * reset, e.g. when suspending.
1417 * This is the native kms version of drm_vblank_off().
1419 void drm_crtc_vblank_off(struct drm_crtc
*crtc
)
1421 drm_vblank_off(crtc
->dev
, drm_crtc_index(crtc
));
1423 EXPORT_SYMBOL(drm_crtc_vblank_off
);
1426 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1427 * @crtc: CRTC in question
1429 * Drivers can use this function to reset the vblank state to off at load time.
1430 * Drivers should use this together with the drm_crtc_vblank_off() and
1431 * drm_crtc_vblank_on() functions. The difference compared to
1432 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1433 * and hence doesn't need to call any driver hooks.
1435 void drm_crtc_vblank_reset(struct drm_crtc
*crtc
)
1437 struct drm_device
*dev
= crtc
->dev
;
1438 unsigned long irqflags
;
1439 unsigned int pipe
= drm_crtc_index(crtc
);
1440 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1442 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1444 * Prevent subsequent drm_vblank_get() from enabling the vblank
1445 * interrupt by bumping the refcount.
1447 if (!vblank
->inmodeset
) {
1448 atomic_inc(&vblank
->refcount
);
1449 vblank
->inmodeset
= 1;
1451 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1453 WARN_ON(!list_empty(&dev
->vblank_event_list
));
1455 EXPORT_SYMBOL(drm_crtc_vblank_reset
);
1458 * drm_vblank_on - enable vblank events on a CRTC
1462 * This functions restores the vblank interrupt state captured with
1463 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1464 * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1465 * in driver load code to reflect the current hardware state of the crtc.
1467 * This is the legacy version of drm_crtc_vblank_on().
1469 void drm_vblank_on(struct drm_device
*dev
, unsigned int pipe
)
1471 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1472 unsigned long irqflags
;
1474 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1477 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1478 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1479 pipe
, vblank
->enabled
, vblank
->inmodeset
);
1481 /* Drop our private "prevent drm_vblank_get" refcount */
1482 if (vblank
->inmodeset
) {
1483 atomic_dec(&vblank
->refcount
);
1484 vblank
->inmodeset
= 0;
1487 drm_reset_vblank_timestamp(dev
, pipe
);
1490 * re-enable interrupts if there are users left, or the
1491 * user wishes vblank interrupts to be enabled all the time.
1493 if (atomic_read(&vblank
->refcount
) != 0 || drm_vblank_offdelay
== 0)
1494 WARN_ON(drm_vblank_enable(dev
, pipe
));
1495 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1497 EXPORT_SYMBOL(drm_vblank_on
);
1500 * drm_crtc_vblank_on - enable vblank events on a CRTC
1501 * @crtc: CRTC in question
1503 * This functions restores the vblank interrupt state captured with
1504 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1505 * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1506 * in driver load code to reflect the current hardware state of the crtc.
1508 * This is the native kms version of drm_vblank_on().
1510 void drm_crtc_vblank_on(struct drm_crtc
*crtc
)
1512 drm_vblank_on(crtc
->dev
, drm_crtc_index(crtc
));
1514 EXPORT_SYMBOL(drm_crtc_vblank_on
);
1517 * drm_vblank_pre_modeset - account for vblanks across mode sets
1521 * Account for vblank events across mode setting events, which will likely
1522 * reset the hardware frame counter.
1524 * This is done by grabbing a temporary vblank reference to ensure that the
1525 * vblank interrupt keeps running across the modeset sequence. With this the
1526 * software-side vblank frame counting will ensure that there are no jumps or
1529 * Unfortunately this approach is racy and also doesn't work when the vblank
1530 * interrupt stops running, e.g. across system suspend resume. It is therefore
1531 * highly recommended that drivers use the newer drm_vblank_off() and
1532 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1533 * using "cooked" software vblank frame counters and not relying on any hardware
1536 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1539 void drm_vblank_pre_modeset(struct drm_device
*dev
, unsigned int pipe
)
1541 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1543 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1544 if (!dev
->num_crtcs
)
1547 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1551 * To avoid all the problems that might happen if interrupts
1552 * were enabled/disabled around or between these calls, we just
1553 * have the kernel take a reference on the CRTC (just once though
1554 * to avoid corrupting the count if multiple, mismatch calls occur),
1555 * so that interrupts remain enabled in the interim.
1557 if (!vblank
->inmodeset
) {
1558 vblank
->inmodeset
= 0x1;
1559 if (drm_vblank_get(dev
, pipe
) == 0)
1560 vblank
->inmodeset
|= 0x2;
1563 EXPORT_SYMBOL(drm_vblank_pre_modeset
);
1566 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1570 * This function again drops the temporary vblank reference acquired in
1571 * drm_vblank_pre_modeset.
1573 void drm_vblank_post_modeset(struct drm_device
*dev
, unsigned int pipe
)
1575 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1576 unsigned long irqflags
;
1578 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1579 if (!dev
->num_crtcs
)
1582 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1585 if (vblank
->inmodeset
) {
1586 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1587 dev
->vblank_disable_allowed
= true;
1588 drm_reset_vblank_timestamp(dev
, pipe
);
1589 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1591 if (vblank
->inmodeset
& 0x2)
1592 drm_vblank_put(dev
, pipe
);
1594 vblank
->inmodeset
= 0;
1597 EXPORT_SYMBOL(drm_vblank_post_modeset
);
1600 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1601 * @DRM_IOCTL_ARGS: standard ioctl arguments
1603 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1604 * ioctls around modesetting so that any lost vblank events are accounted for.
1606 * Generally the counter will reset across mode sets. If interrupts are
1607 * enabled around this call, we don't have to do anything since the counter
1608 * will have already been incremented.
1610 int drm_modeset_ctl(struct drm_device
*dev
, void *data
,
1611 struct drm_file
*file_priv
)
1613 struct drm_modeset_ctl
*modeset
= data
;
1616 /* If drm_vblank_init() hasn't been called yet, just no-op */
1617 if (!dev
->num_crtcs
)
1620 /* KMS drivers handle this internally */
1621 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
1624 pipe
= modeset
->crtc
;
1625 if (pipe
>= dev
->num_crtcs
)
1628 switch (modeset
->cmd
) {
1629 case _DRM_PRE_MODESET
:
1630 drm_vblank_pre_modeset(dev
, pipe
);
1632 case _DRM_POST_MODESET
:
1633 drm_vblank_post_modeset(dev
, pipe
);
1642 #ifdef __DragonFly__
1644 * The Linux layer version of kfree() is a macro and can't be called
1645 * directly via a function pointer
1648 drm_vblank_event_destroy(struct drm_pending_event
*e
)
1654 static int drm_queue_vblank_event(struct drm_device
*dev
, unsigned int pipe
,
1655 union drm_wait_vblank
*vblwait
,
1656 struct drm_file
*file_priv
)
1658 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1659 struct drm_pending_vblank_event
*e
;
1661 unsigned long flags
;
1665 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1672 e
->base
.pid
= curproc
->p_pid
;
1673 e
->event
.base
.type
= DRM_EVENT_VBLANK
;
1674 e
->event
.base
.length
= sizeof(e
->event
);
1675 e
->event
.user_data
= vblwait
->request
.signal
;
1676 e
->base
.event
= &e
->event
.base
;
1677 e
->base
.file_priv
= file_priv
;
1678 #ifdef __DragonFly__
1679 e
->base
.destroy
= drm_vblank_event_destroy
;
1681 e
->base
.destroy
= (void (*) (struct drm_pending_event
*)) kfree
;
1684 spin_lock_irqsave(&dev
->event_lock
, flags
);
1687 * drm_vblank_off() might have been called after we called
1688 * drm_vblank_get(). drm_vblank_off() holds event_lock
1689 * around the vblank disable, so no need for further locking.
1690 * The reference from drm_vblank_get() protects against
1691 * vblank disable from another source.
1693 if (!vblank
->enabled
) {
1698 if (file_priv
->event_space
< sizeof(e
->event
)) {
1703 file_priv
->event_space
-= sizeof(e
->event
);
1704 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1706 if ((vblwait
->request
.type
& _DRM_VBLANK_NEXTONMISS
) &&
1707 (seq
- vblwait
->request
.sequence
) <= (1 << 23)) {
1708 vblwait
->request
.sequence
= seq
+ 1;
1709 vblwait
->reply
.sequence
= vblwait
->request
.sequence
;
1712 DRM_DEBUG_VBLANK("event on vblank count %d, current %d, crtc %u\n",
1713 vblwait
->request
.sequence
, seq
, pipe
);
1715 trace_drm_vblank_event_queued(current
->pid
, pipe
,
1716 vblwait
->request
.sequence
);
1718 e
->event
.sequence
= vblwait
->request
.sequence
;
1719 if ((seq
- vblwait
->request
.sequence
) <= (1 << 23)) {
1720 drm_vblank_put(dev
, pipe
);
1721 send_vblank_event(dev
, e
, seq
, &now
);
1722 vblwait
->reply
.sequence
= seq
;
1724 /* drm_handle_vblank_events will call drm_vblank_put */
1725 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
1726 vblwait
->reply
.sequence
= vblwait
->request
.sequence
;
1729 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1734 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1737 drm_vblank_put(dev
, pipe
);
1744 * \param inode device inode.
1745 * \param file_priv DRM file private.
1746 * \param cmd command.
1747 * \param data user argument, pointing to a drm_wait_vblank structure.
1748 * \return zero on success or a negative number on failure.
1750 * This function enables the vblank interrupt on the pipe requested, then
1751 * sleeps waiting for the requested sequence number to occur, and drops
1752 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
1753 * after a timeout with no further vblank waits scheduled).
1755 int drm_wait_vblank(struct drm_device
*dev
, void *data
,
1756 struct drm_file
*file_priv
)
1758 struct drm_vblank_crtc
*vblank
;
1759 union drm_wait_vblank
*vblwait
= data
;
1761 unsigned int flags
, seq
, pipe
, high_pipe
;
1763 if (!dev
->irq_enabled
)
1766 if (vblwait
->request
.type
& _DRM_VBLANK_SIGNAL
)
1769 if (vblwait
->request
.type
&
1770 ~(_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1771 _DRM_VBLANK_HIGH_CRTC_MASK
)) {
1772 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1773 vblwait
->request
.type
,
1774 (_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1775 _DRM_VBLANK_HIGH_CRTC_MASK
));
1779 flags
= vblwait
->request
.type
& _DRM_VBLANK_FLAGS_MASK
;
1780 high_pipe
= (vblwait
->request
.type
& _DRM_VBLANK_HIGH_CRTC_MASK
);
1782 pipe
= high_pipe
>> _DRM_VBLANK_HIGH_CRTC_SHIFT
;
1784 pipe
= flags
& _DRM_VBLANK_SECONDARY
? 1 : 0;
1785 if (pipe
>= dev
->num_crtcs
)
1788 vblank
= &dev
->vblank
[pipe
];
1790 ret
= drm_vblank_get(dev
, pipe
);
1792 DRM_DEBUG_VBLANK("failed to acquire vblank counter, %d\n", ret
);
1795 seq
= drm_vblank_count(dev
, pipe
);
1797 switch (vblwait
->request
.type
& _DRM_VBLANK_TYPES_MASK
) {
1798 case _DRM_VBLANK_RELATIVE
:
1799 vblwait
->request
.sequence
+= seq
;
1800 vblwait
->request
.type
&= ~_DRM_VBLANK_RELATIVE
;
1801 case _DRM_VBLANK_ABSOLUTE
:
1808 if (flags
& _DRM_VBLANK_EVENT
) {
1809 /* must hold on to the vblank ref until the event fires
1810 * drm_vblank_put will be called asynchronously
1812 return drm_queue_vblank_event(dev
, pipe
, vblwait
, file_priv
);
1815 if ((flags
& _DRM_VBLANK_NEXTONMISS
) &&
1816 (seq
- vblwait
->request
.sequence
) <= (1<<23)) {
1817 vblwait
->request
.sequence
= seq
+ 1;
1820 DRM_DEBUG_VBLANK("waiting on vblank count %d, crtc %u\n",
1821 vblwait
->request
.sequence
, pipe
);
1822 vblank
->last_wait
= vblwait
->request
.sequence
;
1823 DRM_WAIT_ON(ret
, vblank
->queue
, 3 * HZ
,
1824 (((drm_vblank_count(dev
, pipe
) -
1825 vblwait
->request
.sequence
) <= (1 << 23)) ||
1827 !dev
->irq_enabled
));
1829 if (ret
!= -EINTR
) {
1832 vblwait
->reply
.sequence
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1833 vblwait
->reply
.tval_sec
= now
.tv_sec
;
1834 vblwait
->reply
.tval_usec
= now
.tv_usec
;
1836 DRM_DEBUG_VBLANK("returning %d to client\n",
1837 vblwait
->reply
.sequence
);
1839 DRM_DEBUG_VBLANK("vblank wait interrupted by signal\n");
1843 drm_vblank_put(dev
, pipe
);
1847 static void drm_handle_vblank_events(struct drm_device
*dev
, unsigned int pipe
)
1849 struct drm_pending_vblank_event
*e
, *t
;
1853 assert_spin_locked(&dev
->event_lock
);
1855 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1857 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1858 if (e
->pipe
!= pipe
)
1860 if ((seq
- e
->event
.sequence
) > (1<<23))
1863 DRM_DEBUG_VBLANK("vblank event on %d, current %d\n",
1864 e
->event
.sequence
, seq
);
1866 list_del(&e
->base
.link
);
1867 drm_vblank_put(dev
, pipe
);
1868 send_vblank_event(dev
, e
, seq
, &now
);
1871 trace_drm_vblank_event(pipe
, seq
);
1875 * drm_handle_vblank - handle a vblank event
1877 * @pipe: index of CRTC where this event occurred
1879 * Drivers should call this routine in their vblank interrupt handlers to
1880 * update the vblank counter and send any signals that may be pending.
1882 * This is the legacy version of drm_crtc_handle_vblank().
1884 bool drm_handle_vblank(struct drm_device
*dev
, unsigned int pipe
)
1886 struct drm_vblank_crtc
*vblank
= &dev
->vblank
[pipe
];
1887 unsigned long irqflags
;
1889 if (WARN_ON_ONCE(!dev
->num_crtcs
))
1892 if (WARN_ON(pipe
>= dev
->num_crtcs
))
1895 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
1897 /* Need timestamp lock to prevent concurrent execution with
1898 * vblank enable/disable, as this would cause inconsistent
1899 * or corrupted timestamps and vblank counts.
1901 lockmgr(&dev
->vblank_time_lock
, LK_EXCLUSIVE
);
1903 /* Vblank irq handling disabled. Nothing to do. */
1904 if (!vblank
->enabled
) {
1905 lockmgr(&dev
->vblank_time_lock
, LK_RELEASE
);
1906 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1910 drm_update_vblank_count(dev
, pipe
, DRM_CALLED_FROM_VBLIRQ
);
1912 lockmgr(&dev
->vblank_time_lock
, LK_RELEASE
);
1914 wake_up(&vblank
->queue
);
1915 drm_handle_vblank_events(dev
, pipe
);
1917 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
1921 EXPORT_SYMBOL(drm_handle_vblank
);
1924 * drm_crtc_handle_vblank - handle a vblank event
1925 * @crtc: where this event occurred
1927 * Drivers should call this routine in their vblank interrupt handlers to
1928 * update the vblank counter and send any signals that may be pending.
1930 * This is the native KMS version of drm_handle_vblank().
1933 * True if the event was successfully handled, false on failure.
1935 bool drm_crtc_handle_vblank(struct drm_crtc
*crtc
)
1937 return drm_handle_vblank(crtc
->dev
, drm_crtc_index(crtc
));
1939 EXPORT_SYMBOL(drm_crtc_handle_vblank
);
1942 * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
1944 * @pipe: CRTC for which to read the counter
1946 * Drivers can plug this into the .get_vblank_counter() function if
1947 * there is no useable hardware frame counter available.
1952 u32
drm_vblank_no_hw_counter(struct drm_device
*dev
, unsigned int pipe
)
1956 EXPORT_SYMBOL(drm_vblank_no_hw_counter
);