5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
37 #if !defined(__AROS__)
38 #include "drm_trace.h"
40 #include <linux/interrupt.h> /* For task queue support */
41 #include <linux/slab.h>
43 #include <linux/vgaarb.h>
47 #include <proto/oop.h>
51 #if !defined(__AROS__)
52 /* Access macro for slots in vblank timestamp ringbuffer. */
53 #define vblanktimestamp(dev, crtc, count) ( \
54 (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
55 ((count) % DRM_VBLANKTIME_RBSIZE)])
57 /* Retry timestamp calculation up to 3 times to satisfy
58 * drm_timestamp_precision before giving up.
60 #define DRM_TIMESTAMP_MAXRETRIES 3
62 /* Threshold in nanoseconds for detection of redundant
63 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
65 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
68 * Get interrupt from bus id.
70 * \param inode device inode.
71 * \param file_priv DRM file private.
73 * \param arg user argument, pointing to a drm_irq_busid structure.
74 * \return zero on success or a negative number on failure.
76 * Finds the PCI device with the specified bus id and gets its IRQ number.
77 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
78 * to that of the device that this DRM instance attached to.
80 int drm_irq_by_busid(struct drm_device
*dev
, void *data
,
81 struct drm_file
*file_priv
)
83 struct drm_irq_busid
*p
= data
;
85 if (!dev
->driver
->bus
->irq_by_busid
)
88 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
91 return dev
->driver
->bus
->irq_by_busid(dev
, p
);
95 * Clear vblank timestamp buffer for a crtc.
97 static void clear_vblank_timestamps(struct drm_device
*dev
, int crtc
)
99 memset(&dev
->_vblank_time
[crtc
* DRM_VBLANKTIME_RBSIZE
], 0,
100 DRM_VBLANKTIME_RBSIZE
* sizeof(struct timeval
));
104 * Disable vblank irq's on crtc, make sure that last vblank count
105 * of hardware and corresponding consistent software vblank counter
106 * are preserved, even if there are any spurious vblank irq's after
109 static void vblank_disable_and_save(struct drm_device
*dev
, int crtc
)
111 unsigned long irqflags
;
115 struct timeval tvblank
;
117 /* Prevent vblank irq processing while disabling vblank irqs,
118 * so no updates of timestamps or count can happen after we've
119 * disabled. Needed to prevent races in case of delayed irq's.
120 * Disable preemption, so vblank_time_lock is held as short as
121 * possible, even under a kernel with PREEMPT_RT patches.
124 spin_lock_irqsave(&dev
->vblank_time_lock
, irqflags
);
126 dev
->driver
->disable_vblank(dev
, crtc
);
127 dev
->vblank_enabled
[crtc
] = 0;
129 /* No further vblank irq's will be processed after
130 * this point. Get current hardware vblank count and
131 * vblank timestamp, repeat until they are consistent.
133 * FIXME: There is still a race condition here and in
134 * drm_update_vblank_count() which can cause off-by-one
135 * reinitialization of software vblank counter. If gpu
136 * vblank counter doesn't increment exactly at the leading
137 * edge of a vblank interval, then we can lose 1 count if
138 * we happen to execute between start of vblank and the
139 * delayed gpu counter increment.
142 dev
->last_vblank
[crtc
] = dev
->driver
->get_vblank_counter(dev
, crtc
);
143 vblrc
= drm_get_last_vbltimestamp(dev
, crtc
, &tvblank
, 0);
144 } while (dev
->last_vblank
[crtc
] != dev
->driver
->get_vblank_counter(dev
, crtc
));
146 /* Compute time difference to stored timestamp of last vblank
147 * as updated by last invocation of drm_handle_vblank() in vblank irq.
149 vblcount
= atomic_read(&dev
->_vblank_count
[crtc
]);
150 diff_ns
= timeval_to_ns(&tvblank
) -
151 timeval_to_ns(&vblanktimestamp(dev
, crtc
, vblcount
));
153 /* If there is at least 1 msec difference between the last stored
154 * timestamp and tvblank, then we are currently executing our
155 * disable inside a new vblank interval, the tvblank timestamp
156 * corresponds to this new vblank interval and the irq handler
157 * for this vblank didn't run yet and won't run due to our disable.
158 * Therefore we need to do the job of drm_handle_vblank() and
159 * increment the vblank counter by one to account for this vblank.
161 * Skip this step if there isn't any high precision timestamp
162 * available. In that case we can't account for this and just
165 if ((vblrc
> 0) && (abs64(diff_ns
) > 1000000)) {
166 atomic_inc(&dev
->_vblank_count
[crtc
]);
167 smp_mb__after_atomic_inc();
170 /* Invalidate all timestamps while vblank irq's are off. */
171 clear_vblank_timestamps(dev
, crtc
);
173 spin_unlock_irqrestore(&dev
->vblank_time_lock
, irqflags
);
177 static void vblank_disable_fn(unsigned long arg
)
179 struct drm_device
*dev
= (struct drm_device
*)arg
;
180 unsigned long irqflags
;
183 if (!dev
->vblank_disable_allowed
)
186 for (i
= 0; i
< dev
->num_crtcs
; i
++) {
187 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
188 if (atomic_read(&dev
->vblank_refcount
[i
]) == 0 &&
189 dev
->vblank_enabled
[i
]) {
190 DRM_DEBUG("disabling vblank on crtc %d\n", i
);
191 vblank_disable_and_save(dev
, i
);
193 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
197 void drm_vblank_cleanup(struct drm_device
*dev
)
199 /* Bail if the driver didn't call drm_vblank_init() */
200 if (dev
->num_crtcs
== 0)
203 del_timer(&dev
->vblank_disable_timer
);
205 vblank_disable_fn((unsigned long)dev
);
207 kfree(dev
->vbl_queue
);
208 kfree(dev
->_vblank_count
);
209 kfree(dev
->vblank_refcount
);
210 kfree(dev
->vblank_enabled
);
211 kfree(dev
->last_vblank
);
212 kfree(dev
->last_vblank_wait
);
213 kfree(dev
->vblank_inmodeset
);
214 kfree(dev
->_vblank_time
);
218 EXPORT_SYMBOL(drm_vblank_cleanup
);
220 int drm_vblank_init(struct drm_device
*dev
, int num_crtcs
)
222 int i
, ret
= -ENOMEM
;
224 setup_timer(&dev
->vblank_disable_timer
, vblank_disable_fn
,
226 spin_lock_init(&dev
->vbl_lock
);
227 spin_lock_init(&dev
->vblank_time_lock
);
229 dev
->num_crtcs
= num_crtcs
;
231 dev
->vbl_queue
= kmalloc(sizeof(wait_queue_head_t
) * num_crtcs
,
236 dev
->_vblank_count
= kmalloc(sizeof(atomic_t
) * num_crtcs
, GFP_KERNEL
);
237 if (!dev
->_vblank_count
)
240 dev
->vblank_refcount
= kmalloc(sizeof(atomic_t
) * num_crtcs
,
242 if (!dev
->vblank_refcount
)
245 dev
->vblank_enabled
= kcalloc(num_crtcs
, sizeof(int), GFP_KERNEL
);
246 if (!dev
->vblank_enabled
)
249 dev
->last_vblank
= kcalloc(num_crtcs
, sizeof(u32
), GFP_KERNEL
);
250 if (!dev
->last_vblank
)
253 dev
->last_vblank_wait
= kcalloc(num_crtcs
, sizeof(u32
), GFP_KERNEL
);
254 if (!dev
->last_vblank_wait
)
257 dev
->vblank_inmodeset
= kcalloc(num_crtcs
, sizeof(int), GFP_KERNEL
);
258 if (!dev
->vblank_inmodeset
)
261 dev
->_vblank_time
= kcalloc(num_crtcs
* DRM_VBLANKTIME_RBSIZE
,
262 sizeof(struct timeval
), GFP_KERNEL
);
263 if (!dev
->_vblank_time
)
266 DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
268 /* Driver specific high-precision vblank timestamping supported? */
269 if (dev
->driver
->get_vblank_timestamp
)
270 DRM_INFO("Driver supports precise vblank timestamp query.\n");
272 DRM_INFO("No driver support for vblank timestamp query.\n");
274 /* Zero per-crtc vblank stuff */
275 for (i
= 0; i
< num_crtcs
; i
++) {
276 init_waitqueue_head(&dev
->vbl_queue
[i
]);
277 atomic_set(&dev
->_vblank_count
[i
], 0);
278 atomic_set(&dev
->vblank_refcount
[i
], 0);
281 dev
->vblank_disable_allowed
= 0;
285 drm_vblank_cleanup(dev
);
288 EXPORT_SYMBOL(drm_vblank_init
);
290 static void drm_irq_vgaarb_nokms(void *cookie
, bool state
)
292 struct drm_device
*dev
= cookie
;
294 if (dev
->driver
->vgaarb_irq
) {
295 dev
->driver
->vgaarb_irq(dev
, state
);
299 if (!dev
->irq_enabled
)
303 if (dev
->driver
->irq_uninstall
)
304 dev
->driver
->irq_uninstall(dev
);
306 if (dev
->driver
->irq_preinstall
)
307 dev
->driver
->irq_preinstall(dev
);
308 if (dev
->driver
->irq_postinstall
)
309 dev
->driver
->irq_postinstall(dev
);
315 * Install IRQ handler.
317 * \param dev DRM device.
319 * Initializes the IRQ related data. Installs the handler, calling the driver
320 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
321 * before and after the installation.
323 #if !defined(__AROS__)
324 int drm_irq_install(struct drm_device
*dev
)
327 unsigned long sh_flags
= 0;
330 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
333 if (drm_dev_to_irq(dev
) == 0)
336 mutex_lock(&dev
->struct_mutex
);
338 /* Driver must have been initialized */
339 if (!dev
->dev_private
) {
340 mutex_unlock(&dev
->struct_mutex
);
344 if (dev
->irq_enabled
) {
345 mutex_unlock(&dev
->struct_mutex
);
348 dev
->irq_enabled
= 1;
349 mutex_unlock(&dev
->struct_mutex
);
351 DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev
));
353 /* Before installing handler */
354 if (dev
->driver
->irq_preinstall
)
355 dev
->driver
->irq_preinstall(dev
);
357 /* Install handler */
358 if (drm_core_check_feature(dev
, DRIVER_IRQ_SHARED
))
359 sh_flags
= IRQF_SHARED
;
362 irqname
= dev
->devname
;
364 irqname
= dev
->driver
->name
;
366 ret
= request_irq(drm_dev_to_irq(dev
), dev
->driver
->irq_handler
,
367 sh_flags
, irqname
, dev
);
370 mutex_lock(&dev
->struct_mutex
);
371 dev
->irq_enabled
= 0;
372 mutex_unlock(&dev
->struct_mutex
);
376 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
377 vga_client_register(dev
->pdev
, (void *)dev
, drm_irq_vgaarb_nokms
, NULL
);
379 /* After installing handler */
380 if (dev
->driver
->irq_postinstall
)
381 ret
= dev
->driver
->irq_postinstall(dev
);
384 mutex_lock(&dev
->struct_mutex
);
385 dev
->irq_enabled
= 0;
386 mutex_unlock(&dev
->struct_mutex
);
387 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
388 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
389 free_irq(drm_dev_to_irq(dev
), dev
);
394 EXPORT_SYMBOL(drm_irq_install
);
396 static AROS_INTH1(interrupt_handler
, struct drm_device
*, dev
)
399 if (dev
->driver
->irq_handler
)
400 dev
->driver
->irq_handler(dev
);
405 int drm_irq_install(struct drm_device
*dev
)
410 ObtainSemaphore(&dev
->struct_mutex
.semaphore
);
411 if (dev
->irq_enabled
) {
414 dev
->irq_enabled
= 1;
415 ReleaseSemaphore(&dev
->struct_mutex
.semaphore
);
417 if (dev
->driver
->irq_preinstall
)
418 dev
->driver
->irq_preinstall(dev
);
420 dev
->IntHandler
.is_Node
.ln_Type
= NT_INTERRUPT
;
421 dev
->IntHandler
.is_Node
.ln_Pri
= 10;
422 dev
->IntHandler
.is_Node
.ln_Name
= "Gallium3D INT Handler";
423 dev
->IntHandler
.is_Code
= (VOID_FUNC
)interrupt_handler
;
424 dev
->IntHandler
.is_Data
= dev
;
426 OOP_GetAttr((OOP_Object
*)dev
->pdev
->oopdev
, aHidd_PCIDevice_INTLine
, &INTLine
);
427 DRM_DEBUG("INTLine: %d\n", INTLine
);
429 AddIntServer(INTB_KERNEL
+ INTLine
, &dev
->IntHandler
);
431 if (dev
->driver
->irq_postinstall
)
433 retval
= dev
->driver
->irq_postinstall(dev
);
436 ObtainSemaphore(&dev
->struct_mutex
.semaphore
);
437 dev
->irq_enabled
= 0;
438 ReleaseSemaphore(&dev
->struct_mutex
.semaphore
);
446 #if !defined(__AROS__)
448 * Uninstall the IRQ handler.
450 * \param dev DRM device.
452 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
454 int drm_irq_uninstall(struct drm_device
*dev
)
456 unsigned long irqflags
;
459 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
462 mutex_lock(&dev
->struct_mutex
);
463 irq_enabled
= dev
->irq_enabled
;
464 dev
->irq_enabled
= 0;
465 mutex_unlock(&dev
->struct_mutex
);
468 * Wake up any waiters so they don't hang.
470 if (dev
->num_crtcs
) {
471 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
472 for (i
= 0; i
< dev
->num_crtcs
; i
++) {
473 DRM_WAKEUP(&dev
->vbl_queue
[i
]);
474 dev
->vblank_enabled
[i
] = 0;
475 dev
->last_vblank
[i
] =
476 dev
->driver
->get_vblank_counter(dev
, i
);
478 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
484 DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev
));
486 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
487 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
489 if (dev
->driver
->irq_uninstall
)
490 dev
->driver
->irq_uninstall(dev
);
492 free_irq(drm_dev_to_irq(dev
), dev
);
496 EXPORT_SYMBOL(drm_irq_uninstall
);
498 int drm_irq_uninstall(struct drm_device
*dev
)
501 int retval
= -EINVAL
;
504 ObtainSemaphore(&dev
->struct_mutex
.semaphore
);
505 irq_enabled
= dev
->irq_enabled
;
506 dev
->irq_enabled
= 0;
507 ReleaseSemaphore(&dev
->struct_mutex
.semaphore
);
512 if (dev
->driver
->irq_uninstall
)
513 dev
->driver
->irq_uninstall(dev
);
515 OOP_GetAttr((OOP_Object
*)dev
->pdev
->oopdev
, aHidd_PCIDevice_INTLine
, &INTLine
);
516 DRM_DEBUG("INTLine: %d\n", INTLine
);
518 RemIntServer(INTB_KERNEL
+ INTLine
, &dev
->IntHandler
);
524 #if !defined(__AROS__)
528 * \param inode device inode.
529 * \param file_priv DRM file private.
530 * \param cmd command.
531 * \param arg user argument, pointing to a drm_control structure.
532 * \return zero on success or a negative number on failure.
534 * Calls irq_install() or irq_uninstall() according to \p arg.
536 int drm_control(struct drm_device
*dev
, void *data
,
537 struct drm_file
*file_priv
)
539 struct drm_control
*ctl
= data
;
541 /* if we haven't irq we fallback for compatibility reasons -
542 * this used to be a separate function in drm_dma.h
547 case DRM_INST_HANDLER
:
548 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
550 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
552 if (dev
->if_version
< DRM_IF_VERSION(1, 2) &&
553 ctl
->irq
!= drm_dev_to_irq(dev
))
555 return drm_irq_install(dev
);
556 case DRM_UNINST_HANDLER
:
557 if (!drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
))
559 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
561 return drm_irq_uninstall(dev
);
568 * drm_calc_timestamping_constants - Calculate and
569 * store various constants which are later needed by
570 * vblank and swap-completion timestamping, e.g, by
571 * drm_calc_vbltimestamp_from_scanoutpos().
572 * They are derived from crtc's true scanout timing,
573 * so they take things like panel scaling or other
574 * adjustments into account.
576 * @crtc drm_crtc whose timestamp constants should be updated.
579 void drm_calc_timestamping_constants(struct drm_crtc
*crtc
)
581 s64 linedur_ns
= 0, pixeldur_ns
= 0, framedur_ns
= 0;
584 /* Dot clock in Hz: */
585 dotclock
= (u64
) crtc
->hwmode
.clock
* 1000;
587 /* Fields of interlaced scanout modes are only halve a frame duration.
588 * Double the dotclock to get halve the frame-/line-/pixelduration.
590 if (crtc
->hwmode
.flags
& DRM_MODE_FLAG_INTERLACE
)
593 /* Valid dotclock? */
595 /* Convert scanline length in pixels and video dot clock to
596 * line duration, frame duration and pixel duration in
599 pixeldur_ns
= (s64
) div64_u64(1000000000, dotclock
);
600 linedur_ns
= (s64
) div64_u64(((u64
) crtc
->hwmode
.crtc_htotal
*
601 1000000000), dotclock
);
602 framedur_ns
= (s64
) crtc
->hwmode
.crtc_vtotal
* linedur_ns
;
604 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
607 crtc
->pixeldur_ns
= pixeldur_ns
;
608 crtc
->linedur_ns
= linedur_ns
;
609 crtc
->framedur_ns
= framedur_ns
;
611 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
612 crtc
->base
.id
, crtc
->hwmode
.crtc_htotal
,
613 crtc
->hwmode
.crtc_vtotal
, crtc
->hwmode
.crtc_vdisplay
);
614 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
615 crtc
->base
.id
, (int) dotclock
/1000, (int) framedur_ns
,
616 (int) linedur_ns
, (int) pixeldur_ns
);
618 EXPORT_SYMBOL(drm_calc_timestamping_constants
);
621 * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
622 * drivers. Implements calculation of exact vblank timestamps from
623 * given drm_display_mode timings and current video scanout position
624 * of a crtc. This can be called from within get_vblank_timestamp()
625 * implementation of a kms driver to implement the actual timestamping.
627 * Should return timestamps conforming to the OML_sync_control OpenML
628 * extension specification. The timestamp corresponds to the end of
629 * the vblank interval, aka start of scanout of topmost-leftmost display
630 * pixel in the following video frame.
632 * Requires support for optional dev->driver->get_scanout_position()
633 * in kms driver, plus a bit of setup code to provide a drm_display_mode
634 * that corresponds to the true scanout timing.
636 * The current implementation only handles standard video modes. It
637 * returns as no operation if a doublescan or interlaced video mode is
638 * active. Higher level code is expected to handle this.
641 * @crtc: Which crtc's vblank timestamp to retrieve.
642 * @max_error: Desired maximum allowable error in timestamps (nanosecs).
643 * On return contains true maximum error of timestamp.
644 * @vblank_time: Pointer to struct timeval which should receive the timestamp.
645 * @flags: Flags to pass to driver:
647 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
648 * @refcrtc: drm_crtc* of crtc which defines scanout timing.
650 * Returns negative value on error, failure or if not supported in current
653 * -EINVAL - Invalid crtc.
654 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
655 * -ENOTSUPP - Function not supported in current display mode.
656 * -EIO - Failed, e.g., due to failed scanout position query.
658 * Returns or'ed positive status flags on success:
660 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
661 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
664 int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device
*dev
, int crtc
,
666 struct timeval
*vblank_time
,
668 struct drm_crtc
*refcrtc
)
670 struct timeval stime
, raw_time
;
671 struct drm_display_mode
*mode
;
672 int vbl_status
, vtotal
, vdisplay
;
674 s64 framedur_ns
, linedur_ns
, pixeldur_ns
, delta_ns
, duration_ns
;
677 if (crtc
< 0 || crtc
>= dev
->num_crtcs
) {
678 DRM_ERROR("Invalid crtc %d\n", crtc
);
682 /* Scanout position query not supported? Should not happen. */
683 if (!dev
->driver
->get_scanout_position
) {
684 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
688 mode
= &refcrtc
->hwmode
;
689 vtotal
= mode
->crtc_vtotal
;
690 vdisplay
= mode
->crtc_vdisplay
;
692 /* Durations of frames, lines, pixels in nanoseconds. */
693 framedur_ns
= refcrtc
->framedur_ns
;
694 linedur_ns
= refcrtc
->linedur_ns
;
695 pixeldur_ns
= refcrtc
->pixeldur_ns
;
697 /* If mode timing undefined, just return as no-op:
698 * Happens during initial modesetting of a crtc.
700 if (vtotal
<= 0 || vdisplay
<= 0 || framedur_ns
== 0) {
701 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc
);
705 /* Get current scanout position with system timestamp.
706 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
707 * if single query takes longer than max_error nanoseconds.
709 * This guarantees a tight bound on maximum error if
710 * code gets preempted or delayed for some reason.
712 for (i
= 0; i
< DRM_TIMESTAMP_MAXRETRIES
; i
++) {
713 /* Disable preemption to make it very likely to
714 * succeed in the first iteration even on PREEMPT_RT kernel.
718 /* Get system timestamp before query. */
719 do_gettimeofday(&stime
);
721 /* Get vertical and horizontal scanout pos. vpos, hpos. */
722 vbl_status
= dev
->driver
->get_scanout_position(dev
, crtc
, &vpos
, &hpos
);
724 /* Get system timestamp after query. */
725 do_gettimeofday(&raw_time
);
729 /* Return as no-op if scanout query unsupported or failed. */
730 if (!(vbl_status
& DRM_SCANOUTPOS_VALID
)) {
731 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
736 duration_ns
= timeval_to_ns(&raw_time
) - timeval_to_ns(&stime
);
738 /* Accept result with < max_error nsecs timing uncertainty. */
739 if (duration_ns
<= (s64
) *max_error
)
743 /* Noisy system timing? */
744 if (i
== DRM_TIMESTAMP_MAXRETRIES
) {
745 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
746 crtc
, (int) duration_ns
/1000, *max_error
/1000, i
);
749 /* Return upper bound of timestamp precision error. */
750 *max_error
= (int) duration_ns
;
752 /* Check if in vblank area:
753 * vpos is >=0 in video scanout area, but negative
754 * within vblank area, counting down the number of lines until
757 invbl
= vbl_status
& DRM_SCANOUTPOS_INVBL
;
759 /* Convert scanout position into elapsed time at raw_time query
760 * since start of scanout at first display scanline. delta_ns
761 * can be negative if start of scanout hasn't happened yet.
763 delta_ns
= (s64
) vpos
* linedur_ns
+ (s64
) hpos
* pixeldur_ns
;
765 /* Is vpos outside nominal vblank area, but less than
766 * 1/100 of a frame height away from start of vblank?
767 * If so, assume this isn't a massively delayed vblank
768 * interrupt, but a vblank interrupt that fired a few
769 * microseconds before true start of vblank. Compensate
770 * by adding a full frame duration to the final timestamp.
771 * Happens, e.g., on ATI R500, R600.
773 * We only do this if DRM_CALLED_FROM_VBLIRQ.
775 if ((flags
& DRM_CALLED_FROM_VBLIRQ
) && !invbl
&&
776 ((vdisplay
- vpos
) < vtotal
/ 100)) {
777 delta_ns
= delta_ns
- framedur_ns
;
779 /* Signal this correction as "applied". */
783 /* Subtract time delta from raw timestamp to get final
784 * vblank_time timestamp for end of vblank.
786 *vblank_time
= ns_to_timeval(timeval_to_ns(&raw_time
) - delta_ns
);
788 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
789 crtc
, (int)vbl_status
, hpos
, vpos
,
790 (long)raw_time
.tv_sec
, (long)raw_time
.tv_usec
,
791 (long)vblank_time
->tv_sec
, (long)vblank_time
->tv_usec
,
792 (int)duration_ns
/1000, i
);
794 vbl_status
= DRM_VBLANKTIME_SCANOUTPOS_METHOD
;
796 vbl_status
|= DRM_VBLANKTIME_INVBL
;
800 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos
);
803 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
807 * @crtc: which crtc's vblank timestamp to retrieve
808 * @tvblank: Pointer to target struct timeval which should receive the timestamp
809 * @flags: Flags to pass to driver:
811 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
813 * Fetches the system timestamp corresponding to the time of the most recent
814 * vblank interval on specified crtc. May call into kms-driver to
815 * compute the timestamp with a high-precision GPU specific method.
817 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
818 * call, i.e., it isn't very precisely locked to the true vblank.
820 * Returns non-zero if timestamp is considered to be very precise.
822 u32
drm_get_last_vbltimestamp(struct drm_device
*dev
, int crtc
,
823 struct timeval
*tvblank
, unsigned flags
)
827 /* Define requested maximum error on timestamps (nanoseconds). */
828 int max_error
= (int) drm_timestamp_precision
* 1000;
830 /* Query driver if possible and precision timestamping enabled. */
831 if (dev
->driver
->get_vblank_timestamp
&& (max_error
> 0)) {
832 ret
= dev
->driver
->get_vblank_timestamp(dev
, crtc
, &max_error
,
838 /* GPU high precision timestamp query unsupported or failed.
839 * Return gettimeofday timestamp as best estimate.
841 do_gettimeofday(tvblank
);
845 EXPORT_SYMBOL(drm_get_last_vbltimestamp
);
848 * drm_vblank_count - retrieve "cooked" vblank counter value
850 * @crtc: which counter to retrieve
852 * Fetches the "cooked" vblank count value that represents the number of
853 * vblank events since the system was booted, including lost events due to
854 * modesetting activity.
856 u32
drm_vblank_count(struct drm_device
*dev
, int crtc
)
858 return atomic_read(&dev
->_vblank_count
[crtc
]);
860 EXPORT_SYMBOL(drm_vblank_count
);
863 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
864 * and the system timestamp corresponding to that vblank counter value.
867 * @crtc: which counter to retrieve
868 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
870 * Fetches the "cooked" vblank count value that represents the number of
871 * vblank events since the system was booted, including lost events due to
872 * modesetting activity. Returns corresponding system timestamp of the time
873 * of the vblank interval that corresponds to the current value vblank counter
876 u32
drm_vblank_count_and_time(struct drm_device
*dev
, int crtc
,
877 struct timeval
*vblanktime
)
881 /* Read timestamp from slot of _vblank_time ringbuffer
882 * that corresponds to current vblank count. Retry if
883 * count has incremented during readout. This works like
887 cur_vblank
= atomic_read(&dev
->_vblank_count
[crtc
]);
888 *vblanktime
= vblanktimestamp(dev
, crtc
, cur_vblank
);
890 } while (cur_vblank
!= atomic_read(&dev
->_vblank_count
[crtc
]));
894 EXPORT_SYMBOL(drm_vblank_count_and_time
);
897 * drm_update_vblank_count - update the master vblank counter
899 * @crtc: counter to update
901 * Call back into the driver to update the appropriate vblank counter
902 * (specified by @crtc). Deal with wraparound, if it occurred, and
903 * update the last read value so we can deal with wraparound on the next
906 * Only necessary when going from off->on, to account for frames we
907 * didn't get an interrupt for.
909 * Note: caller must hold dev->vbl_lock since this reads & writes
910 * device vblank fields.
912 static void drm_update_vblank_count(struct drm_device
*dev
, int crtc
)
914 u32 cur_vblank
, diff
, tslot
, rc
;
915 struct timeval t_vblank
;
918 * Interrupts were disabled prior to this call, so deal with counter
920 * NOTE! It's possible we lost a full dev->max_vblank_count events
921 * here if the register is small or we had vblank interrupts off for
924 * We repeat the hardware vblank counter & timestamp query until
925 * we get consistent results. This to prevent races between gpu
926 * updating its hardware counter while we are retrieving the
927 * corresponding vblank timestamp.
930 cur_vblank
= dev
->driver
->get_vblank_counter(dev
, crtc
);
931 rc
= drm_get_last_vbltimestamp(dev
, crtc
, &t_vblank
, 0);
932 } while (cur_vblank
!= dev
->driver
->get_vblank_counter(dev
, crtc
));
934 /* Deal with counter wrap */
935 diff
= cur_vblank
- dev
->last_vblank
[crtc
];
936 if (cur_vblank
< dev
->last_vblank
[crtc
]) {
937 diff
+= dev
->max_vblank_count
;
939 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
940 crtc
, dev
->last_vblank
[crtc
], cur_vblank
, diff
);
943 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
946 /* Reinitialize corresponding vblank timestamp if high-precision query
947 * available. Skip this step if query unsupported or failed. Will
948 * reinitialize delayed at next vblank interrupt in that case.
951 tslot
= atomic_read(&dev
->_vblank_count
[crtc
]) + diff
;
952 vblanktimestamp(dev
, crtc
, tslot
) = t_vblank
;
955 smp_mb__before_atomic_inc();
956 atomic_add(diff
, &dev
->_vblank_count
[crtc
]);
957 smp_mb__after_atomic_inc();
961 * drm_vblank_get - get a reference count on vblank events
963 * @crtc: which CRTC to own
965 * Acquire a reference count on vblank events to avoid having them disabled
969 * Zero on success, nonzero on failure.
971 int drm_vblank_get(struct drm_device
*dev
, int crtc
)
973 unsigned long irqflags
, irqflags2
;
976 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
977 /* Going from 0->1 means we have to enable interrupts again */
978 if (atomic_add_return(1, &dev
->vblank_refcount
[crtc
]) == 1) {
979 /* Disable preemption while holding vblank_time_lock. Do
980 * it explicitely to guard against PREEMPT_RT kernel.
983 spin_lock_irqsave(&dev
->vblank_time_lock
, irqflags2
);
984 if (!dev
->vblank_enabled
[crtc
]) {
985 /* Enable vblank irqs under vblank_time_lock protection.
986 * All vblank count & timestamp updates are held off
987 * until we are done reinitializing master counter and
988 * timestamps. Filtercode in drm_handle_vblank() will
989 * prevent double-accounting of same vblank interval.
991 ret
= dev
->driver
->enable_vblank(dev
, crtc
);
992 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
995 atomic_dec(&dev
->vblank_refcount
[crtc
]);
997 dev
->vblank_enabled
[crtc
] = 1;
998 drm_update_vblank_count(dev
, crtc
);
1001 spin_unlock_irqrestore(&dev
->vblank_time_lock
, irqflags2
);
1004 if (!dev
->vblank_enabled
[crtc
]) {
1005 atomic_dec(&dev
->vblank_refcount
[crtc
]);
1009 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1013 EXPORT_SYMBOL(drm_vblank_get
);
1016 * drm_vblank_put - give up ownership of vblank events
1018 * @crtc: which counter to give up
1020 * Release ownership of a given vblank counter, turning off interrupts
1021 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1023 void drm_vblank_put(struct drm_device
*dev
, int crtc
)
1025 BUG_ON(atomic_read(&dev
->vblank_refcount
[crtc
]) == 0);
1027 /* Last user schedules interrupt disable */
1028 if (atomic_dec_and_test(&dev
->vblank_refcount
[crtc
]) &&
1029 (drm_vblank_offdelay
> 0))
1030 mod_timer(&dev
->vblank_disable_timer
,
1031 jiffies
+ ((drm_vblank_offdelay
* DRM_HZ
)/1000));
1033 EXPORT_SYMBOL(drm_vblank_put
);
1035 void drm_vblank_off(struct drm_device
*dev
, int crtc
)
1037 struct drm_pending_vblank_event
*e
, *t
;
1039 unsigned long irqflags
;
1042 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1043 vblank_disable_and_save(dev
, crtc
);
1044 DRM_WAKEUP(&dev
->vbl_queue
[crtc
]);
1046 /* Send any queued vblank events, lest the natives grow disquiet */
1047 seq
= drm_vblank_count_and_time(dev
, crtc
, &now
);
1048 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1049 if (e
->pipe
!= crtc
)
1051 DRM_DEBUG("Sending premature vblank event on disable: \
1052 wanted %d, current %d\n",
1053 e
->event
.sequence
, seq
);
1055 e
->event
.sequence
= seq
;
1056 e
->event
.tv_sec
= now
.tv_sec
;
1057 e
->event
.tv_usec
= now
.tv_usec
;
1058 drm_vblank_put(dev
, e
->pipe
);
1059 list_move_tail(&e
->base
.link
, &e
->base
.file_priv
->event_list
);
1060 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
1061 trace_drm_vblank_event_delivered(e
->base
.pid
, e
->pipe
,
1065 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1067 EXPORT_SYMBOL(drm_vblank_off
);
1070 * drm_vblank_pre_modeset - account for vblanks across mode sets
1072 * @crtc: CRTC in question
1073 * @post: post or pre mode set?
1075 * Account for vblank events across mode setting events, which will likely
1076 * reset the hardware frame counter.
1078 void drm_vblank_pre_modeset(struct drm_device
*dev
, int crtc
)
1080 /* vblank is not initialized (IRQ not installed ?) */
1081 if (!dev
->num_crtcs
)
1084 * To avoid all the problems that might happen if interrupts
1085 * were enabled/disabled around or between these calls, we just
1086 * have the kernel take a reference on the CRTC (just once though
1087 * to avoid corrupting the count if multiple, mismatch calls occur),
1088 * so that interrupts remain enabled in the interim.
1090 if (!dev
->vblank_inmodeset
[crtc
]) {
1091 dev
->vblank_inmodeset
[crtc
] = 0x1;
1092 if (drm_vblank_get(dev
, crtc
) == 0)
1093 dev
->vblank_inmodeset
[crtc
] |= 0x2;
1096 EXPORT_SYMBOL(drm_vblank_pre_modeset
);
1098 void drm_vblank_post_modeset(struct drm_device
*dev
, int crtc
)
1100 unsigned long irqflags
;
1102 if (dev
->vblank_inmodeset
[crtc
]) {
1103 spin_lock_irqsave(&dev
->vbl_lock
, irqflags
);
1104 dev
->vblank_disable_allowed
= 1;
1105 spin_unlock_irqrestore(&dev
->vbl_lock
, irqflags
);
1107 if (dev
->vblank_inmodeset
[crtc
] & 0x2)
1108 drm_vblank_put(dev
, crtc
);
1110 dev
->vblank_inmodeset
[crtc
] = 0;
1113 EXPORT_SYMBOL(drm_vblank_post_modeset
);
1116 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1117 * @DRM_IOCTL_ARGS: standard ioctl arguments
1119 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1120 * ioctls around modesetting so that any lost vblank events are accounted for.
1122 * Generally the counter will reset across mode sets. If interrupts are
1123 * enabled around this call, we don't have to do anything since the counter
1124 * will have already been incremented.
1126 int drm_modeset_ctl(struct drm_device
*dev
, void *data
,
1127 struct drm_file
*file_priv
)
1129 struct drm_modeset_ctl
*modeset
= data
;
1133 /* If drm_vblank_init() hasn't been called yet, just no-op */
1134 if (!dev
->num_crtcs
)
1137 crtc
= modeset
->crtc
;
1138 if (crtc
>= dev
->num_crtcs
) {
1143 switch (modeset
->cmd
) {
1144 case _DRM_PRE_MODESET
:
1145 drm_vblank_pre_modeset(dev
, crtc
);
1147 case _DRM_POST_MODESET
:
1148 drm_vblank_post_modeset(dev
, crtc
);
1159 static int drm_queue_vblank_event(struct drm_device
*dev
, int pipe
,
1160 union drm_wait_vblank
*vblwait
,
1161 struct drm_file
*file_priv
)
1163 struct drm_pending_vblank_event
*e
;
1165 unsigned long flags
;
1169 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
1176 e
->base
.pid
= current
->pid
;
1177 e
->event
.base
.type
= DRM_EVENT_VBLANK
;
1178 e
->event
.base
.length
= sizeof e
->event
;
1179 e
->event
.user_data
= vblwait
->request
.signal
;
1180 e
->base
.event
= &e
->event
.base
;
1181 e
->base
.file_priv
= file_priv
;
1182 e
->base
.destroy
= (void (*) (struct drm_pending_event
*)) kfree
;
1184 spin_lock_irqsave(&dev
->event_lock
, flags
);
1186 if (file_priv
->event_space
< sizeof e
->event
) {
1191 file_priv
->event_space
-= sizeof e
->event
;
1192 seq
= drm_vblank_count_and_time(dev
, pipe
, &now
);
1194 if ((vblwait
->request
.type
& _DRM_VBLANK_NEXTONMISS
) &&
1195 (seq
- vblwait
->request
.sequence
) <= (1 << 23)) {
1196 vblwait
->request
.sequence
= seq
+ 1;
1197 vblwait
->reply
.sequence
= vblwait
->request
.sequence
;
1200 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1201 vblwait
->request
.sequence
, seq
, pipe
);
1203 trace_drm_vblank_event_queued(current
->pid
, pipe
,
1204 vblwait
->request
.sequence
);
1206 e
->event
.sequence
= vblwait
->request
.sequence
;
1207 if ((seq
- vblwait
->request
.sequence
) <= (1 << 23)) {
1208 e
->event
.sequence
= seq
;
1209 e
->event
.tv_sec
= now
.tv_sec
;
1210 e
->event
.tv_usec
= now
.tv_usec
;
1211 drm_vblank_put(dev
, pipe
);
1212 list_add_tail(&e
->base
.link
, &e
->base
.file_priv
->event_list
);
1213 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
1214 vblwait
->reply
.sequence
= seq
;
1215 trace_drm_vblank_event_delivered(current
->pid
, pipe
,
1216 vblwait
->request
.sequence
);
1218 list_add_tail(&e
->base
.link
, &dev
->vblank_event_list
);
1219 vblwait
->reply
.sequence
= vblwait
->request
.sequence
;
1222 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1227 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1230 drm_vblank_put(dev
, pipe
);
1237 * \param inode device inode.
1238 * \param file_priv DRM file private.
1239 * \param cmd command.
1240 * \param data user argument, pointing to a drm_wait_vblank structure.
1241 * \return zero on success or a negative number on failure.
1243 * This function enables the vblank interrupt on the pipe requested, then
1244 * sleeps waiting for the requested sequence number to occur, and drops
1245 * the vblank interrupt refcount afterwards. (vblank irq disable follows that
1246 * after a timeout with no further vblank waits scheduled).
1248 int drm_wait_vblank(struct drm_device
*dev
, void *data
,
1249 struct drm_file
*file_priv
)
1251 union drm_wait_vblank
*vblwait
= data
;
1253 unsigned int flags
, seq
, crtc
, high_crtc
;
1255 if ((!drm_dev_to_irq(dev
)) || (!dev
->irq_enabled
))
1258 if (vblwait
->request
.type
& _DRM_VBLANK_SIGNAL
)
1261 if (vblwait
->request
.type
&
1262 ~(_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1263 _DRM_VBLANK_HIGH_CRTC_MASK
)) {
1264 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1265 vblwait
->request
.type
,
1266 (_DRM_VBLANK_TYPES_MASK
| _DRM_VBLANK_FLAGS_MASK
|
1267 _DRM_VBLANK_HIGH_CRTC_MASK
));
1271 flags
= vblwait
->request
.type
& _DRM_VBLANK_FLAGS_MASK
;
1272 high_crtc
= (vblwait
->request
.type
& _DRM_VBLANK_HIGH_CRTC_MASK
);
1274 crtc
= high_crtc
>> _DRM_VBLANK_HIGH_CRTC_SHIFT
;
1276 crtc
= flags
& _DRM_VBLANK_SECONDARY
? 1 : 0;
1277 if (crtc
>= dev
->num_crtcs
)
1280 ret
= drm_vblank_get(dev
, crtc
);
1282 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret
);
1285 seq
= drm_vblank_count(dev
, crtc
);
1287 switch (vblwait
->request
.type
& _DRM_VBLANK_TYPES_MASK
) {
1288 case _DRM_VBLANK_RELATIVE
:
1289 vblwait
->request
.sequence
+= seq
;
1290 vblwait
->request
.type
&= ~_DRM_VBLANK_RELATIVE
;
1291 case _DRM_VBLANK_ABSOLUTE
:
1298 if (flags
& _DRM_VBLANK_EVENT
)
1299 return drm_queue_vblank_event(dev
, crtc
, vblwait
, file_priv
);
1301 if ((flags
& _DRM_VBLANK_NEXTONMISS
) &&
1302 (seq
- vblwait
->request
.sequence
) <= (1<<23)) {
1303 vblwait
->request
.sequence
= seq
+ 1;
1306 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1307 vblwait
->request
.sequence
, crtc
);
1308 dev
->last_vblank_wait
[crtc
] = vblwait
->request
.sequence
;
1309 DRM_WAIT_ON(ret
, dev
->vbl_queue
[crtc
], 3 * DRM_HZ
,
1310 (((drm_vblank_count(dev
, crtc
) -
1311 vblwait
->request
.sequence
) <= (1 << 23)) ||
1312 !dev
->irq_enabled
));
1314 if (ret
!= -EINTR
) {
1317 vblwait
->reply
.sequence
= drm_vblank_count_and_time(dev
, crtc
, &now
);
1318 vblwait
->reply
.tval_sec
= now
.tv_sec
;
1319 vblwait
->reply
.tval_usec
= now
.tv_usec
;
1321 DRM_DEBUG("returning %d to client\n",
1322 vblwait
->reply
.sequence
);
1324 DRM_DEBUG("vblank wait interrupted by signal\n");
1328 drm_vblank_put(dev
, crtc
);
1332 void drm_handle_vblank_events(struct drm_device
*dev
, int crtc
)
1334 struct drm_pending_vblank_event
*e
, *t
;
1336 unsigned long flags
;
1339 seq
= drm_vblank_count_and_time(dev
, crtc
, &now
);
1341 spin_lock_irqsave(&dev
->event_lock
, flags
);
1343 list_for_each_entry_safe(e
, t
, &dev
->vblank_event_list
, base
.link
) {
1344 if (e
->pipe
!= crtc
)
1346 if ((seq
- e
->event
.sequence
) > (1<<23))
1349 DRM_DEBUG("vblank event on %d, current %d\n",
1350 e
->event
.sequence
, seq
);
1352 e
->event
.sequence
= seq
;
1353 e
->event
.tv_sec
= now
.tv_sec
;
1354 e
->event
.tv_usec
= now
.tv_usec
;
1355 drm_vblank_put(dev
, e
->pipe
);
1356 list_move_tail(&e
->base
.link
, &e
->base
.file_priv
->event_list
);
1357 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
1358 trace_drm_vblank_event_delivered(e
->base
.pid
, e
->pipe
,
1362 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1364 trace_drm_vblank_event(crtc
, seq
);
1368 * drm_handle_vblank - handle a vblank event
1370 * @crtc: where this event occurred
1372 * Drivers should call this routine in their vblank interrupt handlers to
1373 * update the vblank counter and send any signals that may be pending.
1375 bool drm_handle_vblank(struct drm_device
*dev
, int crtc
)
1379 struct timeval tvblank
;
1380 unsigned long irqflags
;
1382 if (!dev
->num_crtcs
)
1385 /* Need timestamp lock to prevent concurrent execution with
1386 * vblank enable/disable, as this would cause inconsistent
1387 * or corrupted timestamps and vblank counts.
1389 spin_lock_irqsave(&dev
->vblank_time_lock
, irqflags
);
1391 /* Vblank irq handling disabled. Nothing to do. */
1392 if (!dev
->vblank_enabled
[crtc
]) {
1393 spin_unlock_irqrestore(&dev
->vblank_time_lock
, irqflags
);
1397 /* Fetch corresponding timestamp for this vblank interval from
1398 * driver and store it in proper slot of timestamp ringbuffer.
1401 /* Get current timestamp and count. */
1402 vblcount
= atomic_read(&dev
->_vblank_count
[crtc
]);
1403 drm_get_last_vbltimestamp(dev
, crtc
, &tvblank
, DRM_CALLED_FROM_VBLIRQ
);
1405 /* Compute time difference to timestamp of last vblank */
1406 diff_ns
= timeval_to_ns(&tvblank
) -
1407 timeval_to_ns(&vblanktimestamp(dev
, crtc
, vblcount
));
1409 /* Update vblank timestamp and count if at least
1410 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1411 * difference between last stored timestamp and current
1412 * timestamp. A smaller difference means basically
1413 * identical timestamps. Happens if this vblank has
1414 * been already processed and this is a redundant call,
1415 * e.g., due to spurious vblank interrupts. We need to
1416 * ignore those for accounting.
1418 if (abs64(diff_ns
) > DRM_REDUNDANT_VBLIRQ_THRESH_NS
) {
1419 /* Store new timestamp in ringbuffer. */
1420 vblanktimestamp(dev
, crtc
, vblcount
+ 1) = tvblank
;
1422 /* Increment cooked vblank count. This also atomically commits
1423 * the timestamp computed above.
1425 smp_mb__before_atomic_inc();
1426 atomic_inc(&dev
->_vblank_count
[crtc
]);
1427 smp_mb__after_atomic_inc();
1429 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1430 crtc
, (int) diff_ns
);
1433 DRM_WAKEUP(&dev
->vbl_queue
[crtc
]);
1434 drm_handle_vblank_events(dev
, crtc
);
1436 spin_unlock_irqrestore(&dev
->vblank_time_lock
, irqflags
);
1439 EXPORT_SYMBOL(drm_handle_vblank
);