aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/drm_irq.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /drivers/gpu/drm/drm_irq.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'drivers/gpu/drm/drm_irq.c')
-rw-r--r--drivers/gpu/drm/drm_irq.c644
1 files changed, 596 insertions, 48 deletions
diff --git a/drivers/gpu/drm/drm_irq.c b/drivers/gpu/drm/drm_irq.c
index 9d3a5030b6e1..2022a5c966bb 100644
--- a/drivers/gpu/drm/drm_irq.c
+++ b/drivers/gpu/drm/drm_irq.c
@@ -40,6 +40,22 @@
40#include <linux/slab.h> 40#include <linux/slab.h>
41 41
42#include <linux/vgaarb.h> 42#include <linux/vgaarb.h>
43
44/* Access macro for slots in vblank timestamp ringbuffer. */
45#define vblanktimestamp(dev, crtc, count) ( \
46 (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
47 ((count) % DRM_VBLANKTIME_RBSIZE)])
48
49/* Retry timestamp calculation up to 3 times to satisfy
50 * drm_timestamp_precision before giving up.
51 */
52#define DRM_TIMESTAMP_MAXRETRIES 3
53
54/* Threshold in nanoseconds for detection of redundant
55 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
56 */
57#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
58
43/** 59/**
44 * Get interrupt from bus id. 60 * Get interrupt from bus id.
45 * 61 *
@@ -58,23 +74,96 @@ int drm_irq_by_busid(struct drm_device *dev, void *data,
58{ 74{
59 struct drm_irq_busid *p = data; 75 struct drm_irq_busid *p = data;
60 76
61 if (drm_core_check_feature(dev, DRIVER_USE_PLATFORM_DEVICE)) 77 if (!dev->driver->bus->irq_by_busid)
62 return -EINVAL; 78 return -EINVAL;
63 79
64 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) 80 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
65 return -EINVAL; 81 return -EINVAL;
66 82
67 if ((p->busnum >> 8) != drm_get_pci_domain(dev) || 83 return dev->driver->bus->irq_by_busid(dev, p);
68 (p->busnum & 0xff) != dev->pdev->bus->number || 84}
69 p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
70 return -EINVAL;
71 85
72 p->irq = dev->pdev->irq; 86/*
87 * Clear vblank timestamp buffer for a crtc.
88 */
89static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
90{
91 memset(&dev->_vblank_time[crtc * DRM_VBLANKTIME_RBSIZE], 0,
92 DRM_VBLANKTIME_RBSIZE * sizeof(struct timeval));
93}
73 94
74 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum, 95/*
75 p->irq); 96 * Disable vblank irq's on crtc, make sure that last vblank count
97 * of hardware and corresponding consistent software vblank counter
98 * are preserved, even if there are any spurious vblank irq's after
99 * disable.
100 */
101static void vblank_disable_and_save(struct drm_device *dev, int crtc)
102{
103 unsigned long irqflags;
104 u32 vblcount;
105 s64 diff_ns;
106 int vblrc;
107 struct timeval tvblank;
108
109 /* Prevent vblank irq processing while disabling vblank irqs,
110 * so no updates of timestamps or count can happen after we've
111 * disabled. Needed to prevent races in case of delayed irq's.
112 * Disable preemption, so vblank_time_lock is held as short as
113 * possible, even under a kernel with PREEMPT_RT patches.
114 */
115 preempt_disable();
116 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
76 117
77 return 0; 118 dev->driver->disable_vblank(dev, crtc);
119 dev->vblank_enabled[crtc] = 0;
120
121 /* No further vblank irq's will be processed after
122 * this point. Get current hardware vblank count and
123 * vblank timestamp, repeat until they are consistent.
124 *
125 * FIXME: There is still a race condition here and in
126 * drm_update_vblank_count() which can cause off-by-one
127 * reinitialization of software vblank counter. If gpu
128 * vblank counter doesn't increment exactly at the leading
129 * edge of a vblank interval, then we can lose 1 count if
130 * we happen to execute between start of vblank and the
131 * delayed gpu counter increment.
132 */
133 do {
134 dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
135 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
136 } while (dev->last_vblank[crtc] != dev->driver->get_vblank_counter(dev, crtc));
137
138 /* Compute time difference to stored timestamp of last vblank
139 * as updated by last invocation of drm_handle_vblank() in vblank irq.
140 */
141 vblcount = atomic_read(&dev->_vblank_count[crtc]);
142 diff_ns = timeval_to_ns(&tvblank) -
143 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
144
145 /* If there is at least 1 msec difference between the last stored
146 * timestamp and tvblank, then we are currently executing our
147 * disable inside a new vblank interval, the tvblank timestamp
148 * corresponds to this new vblank interval and the irq handler
149 * for this vblank didn't run yet and won't run due to our disable.
150 * Therefore we need to do the job of drm_handle_vblank() and
151 * increment the vblank counter by one to account for this vblank.
152 *
153 * Skip this step if there isn't any high precision timestamp
154 * available. In that case we can't account for this and just
155 * hope for the best.
156 */
157 if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
158 atomic_inc(&dev->_vblank_count[crtc]);
159 smp_mb__after_atomic_inc();
160 }
161
162 /* Invalidate all timestamps while vblank irq's are off. */
163 clear_vblank_timestamps(dev, crtc);
164
165 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
166 preempt_enable();
78} 167}
79 168
80static void vblank_disable_fn(unsigned long arg) 169static void vblank_disable_fn(unsigned long arg)
@@ -91,10 +180,7 @@ static void vblank_disable_fn(unsigned long arg)
91 if (atomic_read(&dev->vblank_refcount[i]) == 0 && 180 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
92 dev->vblank_enabled[i]) { 181 dev->vblank_enabled[i]) {
93 DRM_DEBUG("disabling vblank on crtc %d\n", i); 182 DRM_DEBUG("disabling vblank on crtc %d\n", i);
94 dev->last_vblank[i] = 183 vblank_disable_and_save(dev, i);
95 dev->driver->get_vblank_counter(dev, i);
96 dev->driver->disable_vblank(dev, i);
97 dev->vblank_enabled[i] = 0;
98 } 184 }
99 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 185 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
100 } 186 }
@@ -117,6 +203,7 @@ void drm_vblank_cleanup(struct drm_device *dev)
117 kfree(dev->last_vblank); 203 kfree(dev->last_vblank);
118 kfree(dev->last_vblank_wait); 204 kfree(dev->last_vblank_wait);
119 kfree(dev->vblank_inmodeset); 205 kfree(dev->vblank_inmodeset);
206 kfree(dev->_vblank_time);
120 207
121 dev->num_crtcs = 0; 208 dev->num_crtcs = 0;
122} 209}
@@ -129,6 +216,8 @@ int drm_vblank_init(struct drm_device *dev, int num_crtcs)
129 setup_timer(&dev->vblank_disable_timer, vblank_disable_fn, 216 setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
130 (unsigned long)dev); 217 (unsigned long)dev);
131 spin_lock_init(&dev->vbl_lock); 218 spin_lock_init(&dev->vbl_lock);
219 spin_lock_init(&dev->vblank_time_lock);
220
132 dev->num_crtcs = num_crtcs; 221 dev->num_crtcs = num_crtcs;
133 222
134 dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs, 223 dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs,
@@ -161,6 +250,19 @@ int drm_vblank_init(struct drm_device *dev, int num_crtcs)
161 if (!dev->vblank_inmodeset) 250 if (!dev->vblank_inmodeset)
162 goto err; 251 goto err;
163 252
253 dev->_vblank_time = kcalloc(num_crtcs * DRM_VBLANKTIME_RBSIZE,
254 sizeof(struct timeval), GFP_KERNEL);
255 if (!dev->_vblank_time)
256 goto err;
257
258 DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
259
260 /* Driver specific high-precision vblank timestamping supported? */
261 if (dev->driver->get_vblank_timestamp)
262 DRM_INFO("Driver supports precise vblank timestamp query.\n");
263 else
264 DRM_INFO("No driver support for vblank timestamp query.\n");
265
164 /* Zero per-crtc vblank stuff */ 266 /* Zero per-crtc vblank stuff */
165 for (i = 0; i < num_crtcs; i++) { 267 for (i = 0; i < num_crtcs; i++) {
166 init_waitqueue_head(&dev->vbl_queue[i]); 268 init_waitqueue_head(&dev->vbl_queue[i]);
@@ -279,7 +381,7 @@ EXPORT_SYMBOL(drm_irq_install);
279 * 381 *
280 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq. 382 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
281 */ 383 */
282int drm_irq_uninstall(struct drm_device * dev) 384int drm_irq_uninstall(struct drm_device *dev)
283{ 385{
284 unsigned long irqflags; 386 unsigned long irqflags;
285 int irq_enabled, i; 387 int irq_enabled, i;
@@ -335,7 +437,9 @@ int drm_control(struct drm_device *dev, void *data,
335{ 437{
336 struct drm_control *ctl = data; 438 struct drm_control *ctl = data;
337 439
338 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */ 440 /* if we haven't irq we fallback for compatibility reasons -
441 * this used to be a separate function in drm_dma.h
442 */
339 443
340 444
341 switch (ctl->func) { 445 switch (ctl->func) {
@@ -360,6 +464,286 @@ int drm_control(struct drm_device *dev, void *data,
360} 464}
361 465
362/** 466/**
467 * drm_calc_timestamping_constants - Calculate and
468 * store various constants which are later needed by
469 * vblank and swap-completion timestamping, e.g, by
470 * drm_calc_vbltimestamp_from_scanoutpos().
471 * They are derived from crtc's true scanout timing,
472 * so they take things like panel scaling or other
473 * adjustments into account.
474 *
475 * @crtc drm_crtc whose timestamp constants should be updated.
476 *
477 */
478void drm_calc_timestamping_constants(struct drm_crtc *crtc)
479{
480 s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
481 u64 dotclock;
482
483 /* Dot clock in Hz: */
484 dotclock = (u64) crtc->hwmode.clock * 1000;
485
486 /* Fields of interlaced scanout modes are only halve a frame duration.
487 * Double the dotclock to get halve the frame-/line-/pixelduration.
488 */
489 if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
490 dotclock *= 2;
491
492 /* Valid dotclock? */
493 if (dotclock > 0) {
494 /* Convert scanline length in pixels and video dot clock to
495 * line duration, frame duration and pixel duration in
496 * nanoseconds:
497 */
498 pixeldur_ns = (s64) div64_u64(1000000000, dotclock);
499 linedur_ns = (s64) div64_u64(((u64) crtc->hwmode.crtc_htotal *
500 1000000000), dotclock);
501 framedur_ns = (s64) crtc->hwmode.crtc_vtotal * linedur_ns;
502 } else
503 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
504 crtc->base.id);
505
506 crtc->pixeldur_ns = pixeldur_ns;
507 crtc->linedur_ns = linedur_ns;
508 crtc->framedur_ns = framedur_ns;
509
510 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
511 crtc->base.id, crtc->hwmode.crtc_htotal,
512 crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
513 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
514 crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
515 (int) linedur_ns, (int) pixeldur_ns);
516}
517EXPORT_SYMBOL(drm_calc_timestamping_constants);
518
519/**
520 * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
521 * drivers. Implements calculation of exact vblank timestamps from
522 * given drm_display_mode timings and current video scanout position
523 * of a crtc. This can be called from within get_vblank_timestamp()
524 * implementation of a kms driver to implement the actual timestamping.
525 *
526 * Should return timestamps conforming to the OML_sync_control OpenML
527 * extension specification. The timestamp corresponds to the end of
528 * the vblank interval, aka start of scanout of topmost-leftmost display
529 * pixel in the following video frame.
530 *
531 * Requires support for optional dev->driver->get_scanout_position()
532 * in kms driver, plus a bit of setup code to provide a drm_display_mode
533 * that corresponds to the true scanout timing.
534 *
535 * The current implementation only handles standard video modes. It
536 * returns as no operation if a doublescan or interlaced video mode is
537 * active. Higher level code is expected to handle this.
538 *
539 * @dev: DRM device.
540 * @crtc: Which crtc's vblank timestamp to retrieve.
541 * @max_error: Desired maximum allowable error in timestamps (nanosecs).
542 * On return contains true maximum error of timestamp.
543 * @vblank_time: Pointer to struct timeval which should receive the timestamp.
544 * @flags: Flags to pass to driver:
545 * 0 = Default.
546 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
547 * @refcrtc: drm_crtc* of crtc which defines scanout timing.
548 *
549 * Returns negative value on error, failure or if not supported in current
550 * video mode:
551 *
552 * -EINVAL - Invalid crtc.
553 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
554 * -ENOTSUPP - Function not supported in current display mode.
555 * -EIO - Failed, e.g., due to failed scanout position query.
556 *
557 * Returns or'ed positive status flags on success:
558 *
559 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
560 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
561 *
562 */
563int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
564 int *max_error,
565 struct timeval *vblank_time,
566 unsigned flags,
567 struct drm_crtc *refcrtc)
568{
569 struct timeval stime, raw_time;
570 struct drm_display_mode *mode;
571 int vbl_status, vtotal, vdisplay;
572 int vpos, hpos, i;
573 s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
574 bool invbl;
575
576 if (crtc < 0 || crtc >= dev->num_crtcs) {
577 DRM_ERROR("Invalid crtc %d\n", crtc);
578 return -EINVAL;
579 }
580
581 /* Scanout position query not supported? Should not happen. */
582 if (!dev->driver->get_scanout_position) {
583 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
584 return -EIO;
585 }
586
587 mode = &refcrtc->hwmode;
588 vtotal = mode->crtc_vtotal;
589 vdisplay = mode->crtc_vdisplay;
590
591 /* Durations of frames, lines, pixels in nanoseconds. */
592 framedur_ns = refcrtc->framedur_ns;
593 linedur_ns = refcrtc->linedur_ns;
594 pixeldur_ns = refcrtc->pixeldur_ns;
595
596 /* If mode timing undefined, just return as no-op:
597 * Happens during initial modesetting of a crtc.
598 */
599 if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
600 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
601 return -EAGAIN;
602 }
603
604 /* Get current scanout position with system timestamp.
605 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
606 * if single query takes longer than max_error nanoseconds.
607 *
608 * This guarantees a tight bound on maximum error if
609 * code gets preempted or delayed for some reason.
610 */
611 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
612 /* Disable preemption to make it very likely to
613 * succeed in the first iteration even on PREEMPT_RT kernel.
614 */
615 preempt_disable();
616
617 /* Get system timestamp before query. */
618 do_gettimeofday(&stime);
619
620 /* Get vertical and horizontal scanout pos. vpos, hpos. */
621 vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
622
623 /* Get system timestamp after query. */
624 do_gettimeofday(&raw_time);
625
626 preempt_enable();
627
628 /* Return as no-op if scanout query unsupported or failed. */
629 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
630 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
631 crtc, vbl_status);
632 return -EIO;
633 }
634
635 duration_ns = timeval_to_ns(&raw_time) - timeval_to_ns(&stime);
636
637 /* Accept result with < max_error nsecs timing uncertainty. */
638 if (duration_ns <= (s64) *max_error)
639 break;
640 }
641
642 /* Noisy system timing? */
643 if (i == DRM_TIMESTAMP_MAXRETRIES) {
644 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
645 crtc, (int) duration_ns/1000, *max_error/1000, i);
646 }
647
648 /* Return upper bound of timestamp precision error. */
649 *max_error = (int) duration_ns;
650
651 /* Check if in vblank area:
652 * vpos is >=0 in video scanout area, but negative
653 * within vblank area, counting down the number of lines until
654 * start of scanout.
655 */
656 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
657
658 /* Convert scanout position into elapsed time at raw_time query
659 * since start of scanout at first display scanline. delta_ns
660 * can be negative if start of scanout hasn't happened yet.
661 */
662 delta_ns = (s64) vpos * linedur_ns + (s64) hpos * pixeldur_ns;
663
664 /* Is vpos outside nominal vblank area, but less than
665 * 1/100 of a frame height away from start of vblank?
666 * If so, assume this isn't a massively delayed vblank
667 * interrupt, but a vblank interrupt that fired a few
668 * microseconds before true start of vblank. Compensate
669 * by adding a full frame duration to the final timestamp.
670 * Happens, e.g., on ATI R500, R600.
671 *
672 * We only do this if DRM_CALLED_FROM_VBLIRQ.
673 */
674 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
675 ((vdisplay - vpos) < vtotal / 100)) {
676 delta_ns = delta_ns - framedur_ns;
677
678 /* Signal this correction as "applied". */
679 vbl_status |= 0x8;
680 }
681
682 /* Subtract time delta from raw timestamp to get final
683 * vblank_time timestamp for end of vblank.
684 */
685 *vblank_time = ns_to_timeval(timeval_to_ns(&raw_time) - delta_ns);
686
687 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
688 crtc, (int)vbl_status, hpos, vpos,
689 (long)raw_time.tv_sec, (long)raw_time.tv_usec,
690 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
691 (int)duration_ns/1000, i);
692
693 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
694 if (invbl)
695 vbl_status |= DRM_VBLANKTIME_INVBL;
696
697 return vbl_status;
698}
699EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
700
701/**
702 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
703 * vblank interval.
704 *
705 * @dev: DRM device
706 * @crtc: which crtc's vblank timestamp to retrieve
707 * @tvblank: Pointer to target struct timeval which should receive the timestamp
708 * @flags: Flags to pass to driver:
709 * 0 = Default.
710 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
711 *
712 * Fetches the system timestamp corresponding to the time of the most recent
713 * vblank interval on specified crtc. May call into kms-driver to
714 * compute the timestamp with a high-precision GPU specific method.
715 *
716 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
717 * call, i.e., it isn't very precisely locked to the true vblank.
718 *
719 * Returns non-zero if timestamp is considered to be very precise.
720 */
721u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
722 struct timeval *tvblank, unsigned flags)
723{
724 int ret = 0;
725
726 /* Define requested maximum error on timestamps (nanoseconds). */
727 int max_error = (int) drm_timestamp_precision * 1000;
728
729 /* Query driver if possible and precision timestamping enabled. */
730 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
731 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
732 tvblank, flags);
733 if (ret > 0)
734 return (u32) ret;
735 }
736
737 /* GPU high precision timestamp query unsupported or failed.
738 * Return gettimeofday timestamp as best estimate.
739 */
740 do_gettimeofday(tvblank);
741
742 return 0;
743}
744EXPORT_SYMBOL(drm_get_last_vbltimestamp);
745
746/**
363 * drm_vblank_count - retrieve "cooked" vblank counter value 747 * drm_vblank_count - retrieve "cooked" vblank counter value
364 * @dev: DRM device 748 * @dev: DRM device
365 * @crtc: which counter to retrieve 749 * @crtc: which counter to retrieve
@@ -375,6 +759,40 @@ u32 drm_vblank_count(struct drm_device *dev, int crtc)
375EXPORT_SYMBOL(drm_vblank_count); 759EXPORT_SYMBOL(drm_vblank_count);
376 760
377/** 761/**
762 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
763 * and the system timestamp corresponding to that vblank counter value.
764 *
765 * @dev: DRM device
766 * @crtc: which counter to retrieve
767 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
768 *
769 * Fetches the "cooked" vblank count value that represents the number of
770 * vblank events since the system was booted, including lost events due to
771 * modesetting activity. Returns corresponding system timestamp of the time
772 * of the vblank interval that corresponds to the current value vblank counter
773 * value.
774 */
775u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
776 struct timeval *vblanktime)
777{
778 u32 cur_vblank;
779
780 /* Read timestamp from slot of _vblank_time ringbuffer
781 * that corresponds to current vblank count. Retry if
782 * count has incremented during readout. This works like
783 * a seqlock.
784 */
785 do {
786 cur_vblank = atomic_read(&dev->_vblank_count[crtc]);
787 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
788 smp_rmb();
789 } while (cur_vblank != atomic_read(&dev->_vblank_count[crtc]));
790
791 return cur_vblank;
792}
793EXPORT_SYMBOL(drm_vblank_count_and_time);
794
795/**
378 * drm_update_vblank_count - update the master vblank counter 796 * drm_update_vblank_count - update the master vblank counter
379 * @dev: DRM device 797 * @dev: DRM device
380 * @crtc: counter to update 798 * @crtc: counter to update
@@ -392,7 +810,8 @@ EXPORT_SYMBOL(drm_vblank_count);
392 */ 810 */
393static void drm_update_vblank_count(struct drm_device *dev, int crtc) 811static void drm_update_vblank_count(struct drm_device *dev, int crtc)
394{ 812{
395 u32 cur_vblank, diff; 813 u32 cur_vblank, diff, tslot, rc;
814 struct timeval t_vblank;
396 815
397 /* 816 /*
398 * Interrupts were disabled prior to this call, so deal with counter 817 * Interrupts were disabled prior to this call, so deal with counter
@@ -400,8 +819,18 @@ static void drm_update_vblank_count(struct drm_device *dev, int crtc)
400 * NOTE! It's possible we lost a full dev->max_vblank_count events 819 * NOTE! It's possible we lost a full dev->max_vblank_count events
401 * here if the register is small or we had vblank interrupts off for 820 * here if the register is small or we had vblank interrupts off for
402 * a long time. 821 * a long time.
822 *
823 * We repeat the hardware vblank counter & timestamp query until
824 * we get consistent results. This to prevent races between gpu
825 * updating its hardware counter while we are retrieving the
826 * corresponding vblank timestamp.
403 */ 827 */
404 cur_vblank = dev->driver->get_vblank_counter(dev, crtc); 828 do {
829 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
830 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
831 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
832
833 /* Deal with counter wrap */
405 diff = cur_vblank - dev->last_vblank[crtc]; 834 diff = cur_vblank - dev->last_vblank[crtc];
406 if (cur_vblank < dev->last_vblank[crtc]) { 835 if (cur_vblank < dev->last_vblank[crtc]) {
407 diff += dev->max_vblank_count; 836 diff += dev->max_vblank_count;
@@ -413,7 +842,18 @@ static void drm_update_vblank_count(struct drm_device *dev, int crtc)
413 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n", 842 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
414 crtc, diff); 843 crtc, diff);
415 844
845 /* Reinitialize corresponding vblank timestamp if high-precision query
846 * available. Skip this step if query unsupported or failed. Will
847 * reinitialize delayed at next vblank interrupt in that case.
848 */
849 if (rc) {
850 tslot = atomic_read(&dev->_vblank_count[crtc]) + diff;
851 vblanktimestamp(dev, crtc, tslot) = t_vblank;
852 }
853
854 smp_mb__before_atomic_inc();
416 atomic_add(diff, &dev->_vblank_count[crtc]); 855 atomic_add(diff, &dev->_vblank_count[crtc]);
856 smp_mb__after_atomic_inc();
417} 857}
418 858
419/** 859/**
@@ -429,15 +869,27 @@ static void drm_update_vblank_count(struct drm_device *dev, int crtc)
429 */ 869 */
430int drm_vblank_get(struct drm_device *dev, int crtc) 870int drm_vblank_get(struct drm_device *dev, int crtc)
431{ 871{
432 unsigned long irqflags; 872 unsigned long irqflags, irqflags2;
433 int ret = 0; 873 int ret = 0;
434 874
435 spin_lock_irqsave(&dev->vbl_lock, irqflags); 875 spin_lock_irqsave(&dev->vbl_lock, irqflags);
436 /* Going from 0->1 means we have to enable interrupts again */ 876 /* Going from 0->1 means we have to enable interrupts again */
437 if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) { 877 if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) {
878 /* Disable preemption while holding vblank_time_lock. Do
879 * it explicitely to guard against PREEMPT_RT kernel.
880 */
881 preempt_disable();
882 spin_lock_irqsave(&dev->vblank_time_lock, irqflags2);
438 if (!dev->vblank_enabled[crtc]) { 883 if (!dev->vblank_enabled[crtc]) {
884 /* Enable vblank irqs under vblank_time_lock protection.
885 * All vblank count & timestamp updates are held off
886 * until we are done reinitializing master counter and
887 * timestamps. Filtercode in drm_handle_vblank() will
888 * prevent double-accounting of same vblank interval.
889 */
439 ret = dev->driver->enable_vblank(dev, crtc); 890 ret = dev->driver->enable_vblank(dev, crtc);
440 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret); 891 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
892 crtc, ret);
441 if (ret) 893 if (ret)
442 atomic_dec(&dev->vblank_refcount[crtc]); 894 atomic_dec(&dev->vblank_refcount[crtc]);
443 else { 895 else {
@@ -445,6 +897,8 @@ int drm_vblank_get(struct drm_device *dev, int crtc)
445 drm_update_vblank_count(dev, crtc); 897 drm_update_vblank_count(dev, crtc);
446 } 898 }
447 } 899 }
900 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
901 preempt_enable();
448 } else { 902 } else {
449 if (!dev->vblank_enabled[crtc]) { 903 if (!dev->vblank_enabled[crtc]) {
450 atomic_dec(&dev->vblank_refcount[crtc]); 904 atomic_dec(&dev->vblank_refcount[crtc]);
@@ -463,27 +917,50 @@ EXPORT_SYMBOL(drm_vblank_get);
463 * @crtc: which counter to give up 917 * @crtc: which counter to give up
464 * 918 *
465 * Release ownership of a given vblank counter, turning off interrupts 919 * Release ownership of a given vblank counter, turning off interrupts
466 * if possible. 920 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
467 */ 921 */
468void drm_vblank_put(struct drm_device *dev, int crtc) 922void drm_vblank_put(struct drm_device *dev, int crtc)
469{ 923{
470 BUG_ON (atomic_read (&dev->vblank_refcount[crtc]) == 0); 924 BUG_ON(atomic_read(&dev->vblank_refcount[crtc]) == 0);
471 925
472 /* Last user schedules interrupt disable */ 926 /* Last user schedules interrupt disable */
473 if (atomic_dec_and_test(&dev->vblank_refcount[crtc])) 927 if (atomic_dec_and_test(&dev->vblank_refcount[crtc]) &&
474 mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ); 928 (drm_vblank_offdelay > 0))
929 mod_timer(&dev->vblank_disable_timer,
930 jiffies + ((drm_vblank_offdelay * DRM_HZ)/1000));
475} 931}
476EXPORT_SYMBOL(drm_vblank_put); 932EXPORT_SYMBOL(drm_vblank_put);
477 933
478void drm_vblank_off(struct drm_device *dev, int crtc) 934void drm_vblank_off(struct drm_device *dev, int crtc)
479{ 935{
936 struct drm_pending_vblank_event *e, *t;
937 struct timeval now;
480 unsigned long irqflags; 938 unsigned long irqflags;
939 unsigned int seq;
481 940
482 spin_lock_irqsave(&dev->vbl_lock, irqflags); 941 spin_lock_irqsave(&dev->vbl_lock, irqflags);
483 dev->driver->disable_vblank(dev, crtc); 942 vblank_disable_and_save(dev, crtc);
484 DRM_WAKEUP(&dev->vbl_queue[crtc]); 943 DRM_WAKEUP(&dev->vbl_queue[crtc]);
485 dev->vblank_enabled[crtc] = 0; 944
486 dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc); 945 /* Send any queued vblank events, lest the natives grow disquiet */
946 seq = drm_vblank_count_and_time(dev, crtc, &now);
947 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
948 if (e->pipe != crtc)
949 continue;
950 DRM_DEBUG("Sending premature vblank event on disable: \
951 wanted %d, current %d\n",
952 e->event.sequence, seq);
953
954 e->event.sequence = seq;
955 e->event.tv_sec = now.tv_sec;
956 e->event.tv_usec = now.tv_usec;
957 drm_vblank_put(dev, e->pipe);
958 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
959 wake_up_interruptible(&e->base.file_priv->event_wait);
960 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
961 e->event.sequence);
962 }
963
487 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 964 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
488} 965}
489EXPORT_SYMBOL(drm_vblank_off); 966EXPORT_SYMBOL(drm_vblank_off);
@@ -549,7 +1026,8 @@ int drm_modeset_ctl(struct drm_device *dev, void *data,
549 struct drm_file *file_priv) 1026 struct drm_file *file_priv)
550{ 1027{
551 struct drm_modeset_ctl *modeset = data; 1028 struct drm_modeset_ctl *modeset = data;
552 int crtc, ret = 0; 1029 int ret = 0;
1030 unsigned int crtc;
553 1031
554 /* If drm_vblank_init() hasn't been called yet, just no-op */ 1032 /* If drm_vblank_init() hasn't been called yet, just no-op */
555 if (!dev->num_crtcs) 1033 if (!dev->num_crtcs)
@@ -585,10 +1063,13 @@ static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
585 struct timeval now; 1063 struct timeval now;
586 unsigned long flags; 1064 unsigned long flags;
587 unsigned int seq; 1065 unsigned int seq;
1066 int ret;
588 1067
589 e = kzalloc(sizeof *e, GFP_KERNEL); 1068 e = kzalloc(sizeof *e, GFP_KERNEL);
590 if (e == NULL) 1069 if (e == NULL) {
591 return -ENOMEM; 1070 ret = -ENOMEM;
1071 goto err_put;
1072 }
592 1073
593 e->pipe = pipe; 1074 e->pipe = pipe;
594 e->base.pid = current->pid; 1075 e->base.pid = current->pid;
@@ -599,17 +1080,16 @@ static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
599 e->base.file_priv = file_priv; 1080 e->base.file_priv = file_priv;
600 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree; 1081 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
601 1082
602 do_gettimeofday(&now);
603 spin_lock_irqsave(&dev->event_lock, flags); 1083 spin_lock_irqsave(&dev->event_lock, flags);
604 1084
605 if (file_priv->event_space < sizeof e->event) { 1085 if (file_priv->event_space < sizeof e->event) {
606 spin_unlock_irqrestore(&dev->event_lock, flags); 1086 ret = -EBUSY;
607 kfree(e); 1087 goto err_unlock;
608 return -ENOMEM;
609 } 1088 }
610 1089
611 file_priv->event_space -= sizeof e->event; 1090 file_priv->event_space -= sizeof e->event;
612 seq = drm_vblank_count(dev, pipe); 1091 seq = drm_vblank_count_and_time(dev, pipe, &now);
1092
613 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) && 1093 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
614 (seq - vblwait->request.sequence) <= (1 << 23)) { 1094 (seq - vblwait->request.sequence) <= (1 << 23)) {
615 vblwait->request.sequence = seq + 1; 1095 vblwait->request.sequence = seq + 1;
@@ -624,20 +1104,30 @@ static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
624 1104
625 e->event.sequence = vblwait->request.sequence; 1105 e->event.sequence = vblwait->request.sequence;
626 if ((seq - vblwait->request.sequence) <= (1 << 23)) { 1106 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1107 e->event.sequence = seq;
627 e->event.tv_sec = now.tv_sec; 1108 e->event.tv_sec = now.tv_sec;
628 e->event.tv_usec = now.tv_usec; 1109 e->event.tv_usec = now.tv_usec;
629 drm_vblank_put(dev, e->pipe); 1110 drm_vblank_put(dev, pipe);
630 list_add_tail(&e->base.link, &e->base.file_priv->event_list); 1111 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
631 wake_up_interruptible(&e->base.file_priv->event_wait); 1112 wake_up_interruptible(&e->base.file_priv->event_wait);
1113 vblwait->reply.sequence = seq;
632 trace_drm_vblank_event_delivered(current->pid, pipe, 1114 trace_drm_vblank_event_delivered(current->pid, pipe,
633 vblwait->request.sequence); 1115 vblwait->request.sequence);
634 } else { 1116 } else {
635 list_add_tail(&e->base.link, &dev->vblank_event_list); 1117 list_add_tail(&e->base.link, &dev->vblank_event_list);
1118 vblwait->reply.sequence = vblwait->request.sequence;
636 } 1119 }
637 1120
638 spin_unlock_irqrestore(&dev->event_lock, flags); 1121 spin_unlock_irqrestore(&dev->event_lock, flags);
639 1122
640 return 0; 1123 return 0;
1124
1125err_unlock:
1126 spin_unlock_irqrestore(&dev->event_lock, flags);
1127 kfree(e);
1128err_put:
1129 drm_vblank_put(dev, pipe);
1130 return ret;
641} 1131}
642 1132
643/** 1133/**
@@ -659,7 +1149,7 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
659{ 1149{
660 union drm_wait_vblank *vblwait = data; 1150 union drm_wait_vblank *vblwait = data;
661 int ret = 0; 1151 int ret = 0;
662 unsigned int flags, seq, crtc; 1152 unsigned int flags, seq, crtc, high_crtc;
663 1153
664 if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled)) 1154 if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled))
665 return -EINVAL; 1155 return -EINVAL;
@@ -668,16 +1158,21 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
668 return -EINVAL; 1158 return -EINVAL;
669 1159
670 if (vblwait->request.type & 1160 if (vblwait->request.type &
671 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) { 1161 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1162 _DRM_VBLANK_HIGH_CRTC_MASK)) {
672 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", 1163 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
673 vblwait->request.type, 1164 vblwait->request.type,
674 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)); 1165 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1166 _DRM_VBLANK_HIGH_CRTC_MASK));
675 return -EINVAL; 1167 return -EINVAL;
676 } 1168 }
677 1169
678 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; 1170 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
679 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0; 1171 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
680 1172 if (high_crtc)
1173 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1174 else
1175 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
681 if (crtc >= dev->num_crtcs) 1176 if (crtc >= dev->num_crtcs)
682 return -EINVAL; 1177 return -EINVAL;
683 1178
@@ -718,11 +1213,10 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
718 if (ret != -EINTR) { 1213 if (ret != -EINTR) {
719 struct timeval now; 1214 struct timeval now;
720 1215
721 do_gettimeofday(&now); 1216 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
722
723 vblwait->reply.tval_sec = now.tv_sec; 1217 vblwait->reply.tval_sec = now.tv_sec;
724 vblwait->reply.tval_usec = now.tv_usec; 1218 vblwait->reply.tval_usec = now.tv_usec;
725 vblwait->reply.sequence = drm_vblank_count(dev, crtc); 1219
726 DRM_DEBUG("returning %d to client\n", 1220 DRM_DEBUG("returning %d to client\n",
727 vblwait->reply.sequence); 1221 vblwait->reply.sequence);
728 } else { 1222 } else {
@@ -741,8 +1235,7 @@ void drm_handle_vblank_events(struct drm_device *dev, int crtc)
741 unsigned long flags; 1235 unsigned long flags;
742 unsigned int seq; 1236 unsigned int seq;
743 1237
744 do_gettimeofday(&now); 1238 seq = drm_vblank_count_and_time(dev, crtc, &now);
745 seq = drm_vblank_count(dev, crtc);
746 1239
747 spin_lock_irqsave(&dev->event_lock, flags); 1240 spin_lock_irqsave(&dev->event_lock, flags);
748 1241
@@ -778,13 +1271,68 @@ void drm_handle_vblank_events(struct drm_device *dev, int crtc)
778 * Drivers should call this routine in their vblank interrupt handlers to 1271 * Drivers should call this routine in their vblank interrupt handlers to
779 * update the vblank counter and send any signals that may be pending. 1272 * update the vblank counter and send any signals that may be pending.
780 */ 1273 */
781void drm_handle_vblank(struct drm_device *dev, int crtc) 1274bool drm_handle_vblank(struct drm_device *dev, int crtc)
782{ 1275{
1276 u32 vblcount;
1277 s64 diff_ns;
1278 struct timeval tvblank;
1279 unsigned long irqflags;
1280
783 if (!dev->num_crtcs) 1281 if (!dev->num_crtcs)
784 return; 1282 return false;
1283
1284 /* Need timestamp lock to prevent concurrent execution with
1285 * vblank enable/disable, as this would cause inconsistent
1286 * or corrupted timestamps and vblank counts.
1287 */
1288 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
1289
1290 /* Vblank irq handling disabled. Nothing to do. */
1291 if (!dev->vblank_enabled[crtc]) {
1292 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1293 return false;
1294 }
1295
1296 /* Fetch corresponding timestamp for this vblank interval from
1297 * driver and store it in proper slot of timestamp ringbuffer.
1298 */
1299
1300 /* Get current timestamp and count. */
1301 vblcount = atomic_read(&dev->_vblank_count[crtc]);
1302 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1303
1304 /* Compute time difference to timestamp of last vblank */
1305 diff_ns = timeval_to_ns(&tvblank) -
1306 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1307
1308 /* Update vblank timestamp and count if at least
1309 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1310 * difference between last stored timestamp and current
1311 * timestamp. A smaller difference means basically
1312 * identical timestamps. Happens if this vblank has
1313 * been already processed and this is a redundant call,
1314 * e.g., due to spurious vblank interrupts. We need to
1315 * ignore those for accounting.
1316 */
1317 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
1318 /* Store new timestamp in ringbuffer. */
1319 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
1320
1321 /* Increment cooked vblank count. This also atomically commits
1322 * the timestamp computed above.
1323 */
1324 smp_mb__before_atomic_inc();
1325 atomic_inc(&dev->_vblank_count[crtc]);
1326 smp_mb__after_atomic_inc();
1327 } else {
1328 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1329 crtc, (int) diff_ns);
1330 }
785 1331
786 atomic_inc(&dev->_vblank_count[crtc]);
787 DRM_WAKEUP(&dev->vbl_queue[crtc]); 1332 DRM_WAKEUP(&dev->vbl_queue[crtc]);
788 drm_handle_vblank_events(dev, crtc); 1333 drm_handle_vblank_events(dev, crtc);
1334
1335 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
1336 return true;
789} 1337}
790EXPORT_SYMBOL(drm_handle_vblank); 1338EXPORT_SYMBOL(drm_handle_vblank);