These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / gpu / drm / drm_irq.c
index af9662e..8090989 100644 (file)
@@ -43,8 +43,8 @@
 #include <linux/export.h>
 
 /* Access macro for slots in vblank timestamp ringbuffer. */
-#define vblanktimestamp(dev, crtc, count) \
-       ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
+#define vblanktimestamp(dev, pipe, count) \
+       ((dev)->vblank[pipe].time[(count) % DRM_VBLANKTIME_RBSIZE])
 
 /* Retry timestamp calculation up to 3 times to satisfy
  * drm_timestamp_precision before giving up.
@@ -57,7 +57,7 @@
 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
 
 static bool
-drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
+drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
                          struct timeval *tvblank, unsigned flags);
 
 static unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
@@ -74,13 +74,91 @@ module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
 
+static void store_vblank(struct drm_device *dev, unsigned int pipe,
+                        u32 vblank_count_inc,
+                        struct timeval *t_vblank, u32 last)
+{
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
+       u32 tslot;
+
+       assert_spin_locked(&dev->vblank_time_lock);
+
+       vblank->last = last;
+
+       /* All writers hold the spinlock, but readers are serialized by
+        * the latching of vblank->count below.
+        */
+       tslot = vblank->count + vblank_count_inc;
+       vblanktimestamp(dev, pipe, tslot) = *t_vblank;
+
+       /*
+        * vblank timestamp updates are protected on the write side with
+        * vblank_time_lock, but on the read side done locklessly using a
+        * sequence-lock on the vblank counter. Ensure correct ordering using
+        * memory barrriers. We need the barrier both before and also after the
+        * counter update to synchronize with the next timestamp write.
+        * The read-side barriers for this are in drm_vblank_count_and_time.
+        */
+       smp_wmb();
+       vblank->count += vblank_count_inc;
+       smp_wmb();
+}
+
+/**
+ * drm_reset_vblank_timestamp - reset the last timestamp to the last vblank
+ * @dev: DRM device
+ * @pipe: index of CRTC for which to reset the timestamp
+ *
+ * Reset the stored timestamp for the current vblank count to correspond
+ * to the last vblank occurred.
+ *
+ * Only to be called from drm_vblank_on().
+ *
+ * Note: caller must hold dev->vbl_lock since this reads & writes
+ * device vblank fields.
+ */
+static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
+{
+       u32 cur_vblank;
+       bool rc;
+       struct timeval t_vblank;
+       int count = DRM_TIMESTAMP_MAXRETRIES;
+
+       spin_lock(&dev->vblank_time_lock);
+
+       /*
+        * sample the current counter to avoid random jumps
+        * when drm_vblank_enable() applies the diff
+        */
+       do {
+               cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
+               rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
+       } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
+
+       /*
+        * Only reinitialize corresponding vblank timestamp if high-precision query
+        * available and didn't fail. Otherwise reinitialize delayed at next vblank
+        * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
+        */
+       if (!rc)
+               t_vblank = (struct timeval) {0, 0};
+
+       /*
+        * +1 to make sure user will never see the same
+        * vblank counter value before and after a modeset
+        */
+       store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
+
+       spin_unlock(&dev->vblank_time_lock);
+}
+
 /**
  * drm_update_vblank_count - update the master vblank counter
  * @dev: DRM device
- * @crtc: counter to update
+ * @pipe: counter to update
  *
  * Call back into the driver to update the appropriate vblank counter
- * (specified by @crtc).  Deal with wraparound, if it occurred, and
+ * (specified by @pipe).  Deal with wraparound, if it occurred, and
  * update the last read value so we can deal with wraparound on the next
  * call if necessary.
  *
@@ -90,17 +168,20 @@ module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
  * Note: caller must hold dev->vbl_lock since this reads & writes
  * device vblank fields.
  */
-static void drm_update_vblank_count(struct drm_device *dev, int crtc)
+static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
+                                   unsigned long flags)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
-       u32 cur_vblank, diff, tslot;
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
+       u32 cur_vblank, diff;
        bool rc;
        struct timeval t_vblank;
+       int count = DRM_TIMESTAMP_MAXRETRIES;
+       int framedur_ns = vblank->framedur_ns;
 
        /*
         * Interrupts were disabled prior to this call, so deal with counter
         * wrap if needed.
-        * NOTE!  It's possible we lost a full dev->max_vblank_count events
+        * NOTE!  It's possible we lost a full dev->max_vblank_count + 1 events
         * here if the register is small or we had vblank interrupts off for
         * a long time.
         *
@@ -110,36 +191,113 @@ static void drm_update_vblank_count(struct drm_device *dev, int crtc)
         * corresponding vblank timestamp.
         */
        do {
-               cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
-               rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
-       } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
+               cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
+               rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
+       } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
+
+       if (dev->max_vblank_count != 0) {
+               /* trust the hw counter when it's around */
+               diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
+       } else if (rc && framedur_ns) {
+               const struct timeval *t_old;
+               u64 diff_ns;
 
-       /* Deal with counter wrap */
-       diff = cur_vblank - vblank->last;
-       if (cur_vblank < vblank->last) {
-               diff += dev->max_vblank_count;
+               t_old = &vblanktimestamp(dev, pipe, vblank->count);
+               diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
 
-               DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
-                         crtc, vblank->last, cur_vblank, diff);
+               /*
+                * Figure out how many vblanks we've missed based
+                * on the difference in the timestamps and the
+                * frame/field duration.
+                */
+               diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
+
+               if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
+                       DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
+                                     " diff_ns = %lld, framedur_ns = %d)\n",
+                                     pipe, (long long) diff_ns, framedur_ns);
+       } else {
+               /* some kind of default for drivers w/o accurate vbl timestamping */
+               diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
        }
 
-       DRM_DEBUG("updating vblank count on crtc %d, missed %d\n",
-                 crtc, diff);
+       /*
+        * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
+        * interval? If so then vblank irqs keep running and it will likely
+        * happen that the hardware vblank counter is not trustworthy as it
+        * might reset at some point in that interval and vblank timestamps
+        * are not trustworthy either in that interval. Iow. this can result
+        * in a bogus diff >> 1 which must be avoided as it would cause
+        * random large forward jumps of the software vblank counter.
+        */
+       if (diff > 1 && (vblank->inmodeset & 0x2)) {
+               DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
+                             " due to pre-modeset.\n", pipe, diff);
+               diff = 1;
+       }
 
-       if (diff == 0)
+       /*
+        * FIMXE: Need to replace this hack with proper seqlocks.
+        *
+        * Restrict the bump of the software vblank counter to a safe maximum
+        * value of +1 whenever there is the possibility that concurrent readers
+        * of vblank timestamps could be active at the moment, as the current
+        * implementation of the timestamp caching and updating is not safe
+        * against concurrent readers for calls to store_vblank() with a bump
+        * of anything but +1. A bump != 1 would very likely return corrupted
+        * timestamps to userspace, because the same slot in the cache could
+        * be concurrently written by store_vblank() and read by one of those
+        * readers without the read-retry logic detecting the collision.
+        *
+        * Concurrent readers can exist when we are called from the
+        * drm_vblank_off() or drm_vblank_on() functions and other non-vblank-
+        * irq callers. However, all those calls to us are happening with the
+        * vbl_lock locked to prevent drm_vblank_get(), so the vblank refcount
+        * can't increase while we are executing. Therefore a zero refcount at
+        * this point is safe for arbitrary counter bumps if we are called
+        * outside vblank irq, a non-zero count is not 100% safe. Unfortunately
+        * we must also accept a refcount of 1, as whenever we are called from
+        * drm_vblank_get() -> drm_vblank_enable() the refcount will be 1 and
+        * we must let that one pass through in order to not lose vblank counts
+        * during vblank irq off - which would completely defeat the whole
+        * point of this routine.
+        *
+        * Whenever we are called from vblank irq, we have to assume concurrent
+        * readers exist or can show up any time during our execution, even if
+        * the refcount is currently zero, as vblank irqs are usually only
+        * enabled due to the presence of readers, and because when we are called
+        * from vblank irq we can't hold the vbl_lock to protect us from sudden
+        * bumps in vblank refcount. Therefore also restrict bumps to +1 when
+        * called from vblank irq.
+        */
+       if ((diff > 1) && (atomic_read(&vblank->refcount) > 1 ||
+           (flags & DRM_CALLED_FROM_VBLIRQ))) {
+               DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u "
+                             "refcount %u, vblirq %u\n", pipe, diff,
+                             atomic_read(&vblank->refcount),
+                             (flags & DRM_CALLED_FROM_VBLIRQ) != 0);
+               diff = 1;
+       }
+
+       DRM_DEBUG_VBL("updating vblank count on crtc %u:"
+                     " current=%u, diff=%u, hw=%u hw_last=%u\n",
+                     pipe, vblank->count, diff, cur_vblank, vblank->last);
+
+       if (diff == 0) {
+               WARN_ON_ONCE(cur_vblank != vblank->last);
                return;
+       }
 
-       /* Reinitialize corresponding vblank timestamp if high-precision query
-        * available. Skip this step if query unsupported or failed. Will
-        * reinitialize delayed at next vblank interrupt in that case and
-        * assign 0 for now, to mark the vblanktimestamp as invalid.
+       /*
+        * Only reinitialize corresponding vblank timestamp if high-precision query
+        * available and didn't fail, or we were called from the vblank interrupt.
+        * Otherwise reinitialize delayed at next vblank interrupt and assign 0
+        * for now, to mark the vblanktimestamp as invalid.
         */
-       tslot = atomic_read(&vblank->count) + diff;
-       vblanktimestamp(dev, crtc, tslot) = rc ? t_vblank : (struct timeval) {0, 0};
+       if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
+               t_vblank = (struct timeval) {0, 0};
 
-       smp_mb__before_atomic();
-       atomic_add(diff, &vblank->count);
-       smp_mb__after_atomic();
+       store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
 }
 
 /*
@@ -148,15 +306,10 @@ static void drm_update_vblank_count(struct drm_device *dev, int crtc)
  * are preserved, even if there are any spurious vblank irq's after
  * disable.
  */
-static void vblank_disable_and_save(struct drm_device *dev, int crtc)
+static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        unsigned long irqflags;
-       u32 vblcount;
-       s64 diff_ns;
-       bool vblrc;
-       struct timeval tvblank;
-       int count = DRM_TIMESTAMP_MAXRETRIES;
 
        /* Prevent vblank irq processing while disabling vblank irqs,
         * so no updates of timestamps or count can happen after we've
@@ -164,86 +317,23 @@ static void vblank_disable_and_save(struct drm_device *dev, int crtc)
         */
        spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
 
-       /*
-        * If the vblank interrupt was already disabled update the count
-        * and timestamp to maintain the appearance that the counter
-        * has been ticking all along until this time. This makes the
-        * count account for the entire time between drm_vblank_on() and
-        * drm_vblank_off().
-        *
-        * But only do this if precise vblank timestamps are available.
-        * Otherwise we might read a totally bogus timestamp since drivers
-        * lacking precise timestamp support rely upon sampling the system clock
-        * at vblank interrupt time. Which obviously won't work out well if the
-        * vblank interrupt is disabled.
-        */
-       if (!vblank->enabled &&
-           drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0)) {
-               drm_update_vblank_count(dev, crtc);
-               spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
-               return;
-       }
-
        /*
         * Only disable vblank interrupts if they're enabled. This avoids
         * calling the ->disable_vblank() operation in atomic context with the
         * hardware potentially runtime suspended.
         */
        if (vblank->enabled) {
-               dev->driver->disable_vblank(dev, crtc);
+               dev->driver->disable_vblank(dev, pipe);
                vblank->enabled = false;
        }
 
-       /* No further vblank irq's will be processed after
-        * this point. Get current hardware vblank count and
-        * vblank timestamp, repeat until they are consistent.
-        *
-        * FIXME: There is still a race condition here and in
-        * drm_update_vblank_count() which can cause off-by-one
-        * reinitialization of software vblank counter. If gpu
-        * vblank counter doesn't increment exactly at the leading
-        * edge of a vblank interval, then we can lose 1 count if
-        * we happen to execute between start of vblank and the
-        * delayed gpu counter increment.
-        */
-       do {
-               vblank->last = dev->driver->get_vblank_counter(dev, crtc);
-               vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
-       } while (vblank->last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
-
-       if (!count)
-               vblrc = 0;
-
-       /* Compute time difference to stored timestamp of last vblank
-        * as updated by last invocation of drm_handle_vblank() in vblank irq.
-        */
-       vblcount = atomic_read(&vblank->count);
-       diff_ns = timeval_to_ns(&tvblank) -
-                 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
-
-       /* If there is at least 1 msec difference between the last stored
-        * timestamp and tvblank, then we are currently executing our
-        * disable inside a new vblank interval, the tvblank timestamp
-        * corresponds to this new vblank interval and the irq handler
-        * for this vblank didn't run yet and won't run due to our disable.
-        * Therefore we need to do the job of drm_handle_vblank() and
-        * increment the vblank counter by one to account for this vblank.
-        *
-        * Skip this step if there isn't any high precision timestamp
-        * available. In that case we can't account for this and just
-        * hope for the best.
+       /*
+        * Always update the count and timestamp to maintain the
+        * appearance that the counter has been ticking all along until
+        * this time. This makes the count account for the entire time
+        * between drm_vblank_on() and drm_vblank_off().
         */
-       if (vblrc && (abs64(diff_ns) > 1000000)) {
-               /* Store new timestamp in ringbuffer. */
-               vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
-
-               /* Increment cooked vblank count. This also atomically commits
-                * the timestamp computed above.
-                */
-               smp_mb__before_atomic();
-               atomic_inc(&vblank->count);
-               smp_mb__after_atomic();
-       }
+       drm_update_vblank_count(dev, pipe, 0);
 
        spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
 }
@@ -252,16 +342,16 @@ static void vblank_disable_fn(unsigned long arg)
 {
        struct drm_vblank_crtc *vblank = (void *)arg;
        struct drm_device *dev = vblank->dev;
+       unsigned int pipe = vblank->pipe;
        unsigned long irqflags;
-       int crtc = vblank->crtc;
 
        if (!dev->vblank_disable_allowed)
                return;
 
        spin_lock_irqsave(&dev->vbl_lock, irqflags);
        if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
-               DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
-               vblank_disable_and_save(dev, crtc);
+               DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
+               vblank_disable_and_save(dev, pipe);
        }
        spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
 }
@@ -274,14 +364,14 @@ static void vblank_disable_fn(unsigned long arg)
  */
 void drm_vblank_cleanup(struct drm_device *dev)
 {
-       int crtc;
+       unsigned int pipe;
 
        /* Bail if the driver didn't call drm_vblank_init() */
        if (dev->num_crtcs == 0)
                return;
 
-       for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
-               struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
+               struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
 
                WARN_ON(vblank->enabled &&
                        drm_core_check_feature(dev, DRIVER_MODESET));
@@ -297,17 +387,18 @@ EXPORT_SYMBOL(drm_vblank_cleanup);
 
 /**
  * drm_vblank_init - initialize vblank support
- * @dev: drm_device
- * @num_crtcs: number of crtcs supported by @dev
+ * @dev: DRM device
+ * @num_crtcs: number of CRTCs supported by @dev
  *
  * This function initializes vblank support for @num_crtcs display pipelines.
  *
  * Returns:
  * Zero on success or a negative error code on failure.
  */
-int drm_vblank_init(struct drm_device *dev, int num_crtcs)
+int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
 {
-       int i, ret = -ENOMEM;
+       int ret = -ENOMEM;
+       unsigned int i;
 
        spin_lock_init(&dev->vbl_lock);
        spin_lock_init(&dev->vblank_time_lock);
@@ -322,7 +413,7 @@ int drm_vblank_init(struct drm_device *dev, int num_crtcs)
                struct drm_vblank_crtc *vblank = &dev->vblank[i];
 
                vblank->dev = dev;
-               vblank->crtc = i;
+               vblank->pipe = i;
                init_waitqueue_head(&vblank->queue);
                setup_timer(&vblank->disable_timer, vblank_disable_fn,
                            (unsigned long)vblank);
@@ -336,6 +427,13 @@ int drm_vblank_init(struct drm_device *dev, int num_crtcs)
        else
                DRM_INFO("No driver support for vblank timestamp query.\n");
 
+       /* Must have precise timestamping for reliable vblank instant disable */
+       if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
+               dev->vblank_disable_immediate = false;
+               DRM_INFO("Setting vblank_disable_immediate to false because "
+                        "get_vblank_timestamp == NULL\n");
+       }
+
        dev->vblank_disable_allowed = false;
 
        return 0;
@@ -576,19 +674,27 @@ int drm_control(struct drm_device *dev, void *data,
 void drm_calc_timestamping_constants(struct drm_crtc *crtc,
                                     const struct drm_display_mode *mode)
 {
-       int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
+       struct drm_device *dev = crtc->dev;
+       unsigned int pipe = drm_crtc_index(crtc);
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
+       int linedur_ns = 0, framedur_ns = 0;
        int dotclock = mode->crtc_clock;
 
+       if (!dev->num_crtcs)
+               return;
+
+       if (WARN_ON(pipe >= dev->num_crtcs))
+               return;
+
        /* Valid dotclock? */
        if (dotclock > 0) {
                int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
 
                /*
                 * Convert scanline length in pixels and video
-                * dot clock to line duration, frame duration
-                * and pixel duration in nanoseconds:
+                * dot clock to line duration and frame duration
+                * in nanoseconds:
                 */
-               pixeldur_ns = 1000000 / dotclock;
                linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
                framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
 
@@ -598,33 +704,30 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc,
                if (mode->flags & DRM_MODE_FLAG_INTERLACE)
                        framedur_ns /= 2;
        } else
-               DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
+               DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
                          crtc->base.id);
 
-       crtc->pixeldur_ns = pixeldur_ns;
-       crtc->linedur_ns  = linedur_ns;
-       crtc->framedur_ns = framedur_ns;
+       vblank->linedur_ns  = linedur_ns;
+       vblank->framedur_ns = framedur_ns;
 
-       DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
+       DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
                  crtc->base.id, mode->crtc_htotal,
                  mode->crtc_vtotal, mode->crtc_vdisplay);
-       DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
-                 crtc->base.id, dotclock, framedur_ns,
-                 linedur_ns, pixeldur_ns);
+       DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
+                 crtc->base.id, dotclock, framedur_ns, linedur_ns);
 }
 EXPORT_SYMBOL(drm_calc_timestamping_constants);
 
 /**
  * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  * @dev: DRM device
- * @crtc: Which CRTC's vblank timestamp to retrieve
+ * @pipe: index of CRTC whose vblank timestamp to retrieve
  * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  *             On return contains true maximum error of timestamp
  * @vblank_time: Pointer to struct timeval which should receive the timestamp
  * @flags: Flags to pass to driver:
  *         0 = Default,
  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
- * @refcrtc: CRTC which defines scanout timing
  * @mode: mode which defines the scanout timings
  *
  * Implements calculation of exact vblank timestamps from given drm_display_mode
@@ -660,22 +763,22 @@ EXPORT_SYMBOL(drm_calc_timestamping_constants);
  * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  *
  */
-int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
+int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
+                                         unsigned int pipe,
                                          int *max_error,
                                          struct timeval *vblank_time,
                                          unsigned flags,
-                                         const struct drm_crtc *refcrtc,
                                          const struct drm_display_mode *mode)
 {
        struct timeval tv_etime;
        ktime_t stime, etime;
-       int vbl_status;
+       unsigned int vbl_status;
+       int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
        int vpos, hpos, i;
-       int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
-       bool invbl;
+       int delta_ns, duration_ns;
 
-       if (crtc < 0 || crtc >= dev->num_crtcs) {
-               DRM_ERROR("Invalid crtc %d\n", crtc);
+       if (pipe >= dev->num_crtcs) {
+               DRM_ERROR("Invalid crtc %u\n", pipe);
                return -EINVAL;
        }
 
@@ -685,16 +788,11 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
                return -EIO;
        }
 
-       /* Durations of frames, lines, pixels in nanoseconds. */
-       framedur_ns = refcrtc->framedur_ns;
-       linedur_ns  = refcrtc->linedur_ns;
-       pixeldur_ns = refcrtc->pixeldur_ns;
-
        /* If mode timing undefined, just return as no-op:
         * Happens during initial modesetting of a crtc.
         */
-       if (framedur_ns == 0) {
-               DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
+       if (mode->crtc_clock == 0) {
+               DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
                return -EAGAIN;
        }
 
@@ -710,13 +808,15 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
                 * Get vertical and horizontal scanout position vpos, hpos,
                 * and bounding timestamps stime, etime, pre/post query.
                 */
-               vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
-                                                              &hpos, &stime, &etime);
+               vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
+                                                              &vpos, &hpos,
+                                                              &stime, &etime,
+                                                              mode);
 
                /* Return as no-op if scanout query unsupported or failed. */
                if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
-                       DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
-                                 crtc, vbl_status);
+                       DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
+                                 pipe, vbl_status);
                        return -EIO;
                }
 
@@ -730,8 +830,8 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
 
        /* Noisy system timing? */
        if (i == DRM_TIMESTAMP_MAXRETRIES) {
-               DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
-                         crtc, duration_ns/1000, *max_error/1000, i);
+               DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
+                         pipe, duration_ns/1000, *max_error/1000, i);
        }
 
        /* Return upper bound of timestamp precision error. */
@@ -742,13 +842,15 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
         * within vblank area, counting down the number of lines until
         * start of scanout.
         */
-       invbl = vbl_status & DRM_SCANOUTPOS_IN_VBLANK;
+       if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
+               ret |= DRM_VBLANKTIME_IN_VBLANK;
 
        /* Convert scanout position into elapsed time at raw_time query
         * since start of scanout at first display scanline. delta_ns
         * can be negative if start of scanout hasn't happened yet.
         */
-       delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
+       delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
+                          mode->crtc_clock);
 
        if (!drm_timestamp_monotonic)
                etime = ktime_mono_to_real(etime);
@@ -764,17 +866,13 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
                etime = ktime_sub_ns(etime, delta_ns);
        *vblank_time = ktime_to_timeval(etime);
 
-       DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
-                 crtc, (int)vbl_status, hpos, vpos,
-                 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
-                 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
-                 duration_ns/1000, i);
+       DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
+                     pipe, vbl_status, hpos, vpos,
+                     (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
+                     (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
+                     duration_ns/1000, i);
 
-       vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
-       if (invbl)
-               vbl_status |= DRM_VBLANKTIME_IN_VBLANK;
-
-       return vbl_status;
+       return ret;
 }
 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
 
@@ -790,7 +888,7 @@ static struct timeval get_drm_timestamp(void)
  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  *                             vblank interval
  * @dev: DRM device
- * @crtc: which CRTC's vblank timestamp to retrieve
+ * @pipe: index of CRTC whose vblank timestamp to retrieve
  * @tvblank: Pointer to target struct timeval which should receive the timestamp
  * @flags: Flags to pass to driver:
  *         0 = Default,
@@ -807,7 +905,7 @@ static struct timeval get_drm_timestamp(void)
  * True if timestamp is considered to be very precise, false otherwise.
  */
 static bool
-drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
+drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
                          struct timeval *tvblank, unsigned flags)
 {
        int ret;
@@ -817,7 +915,7 @@ drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
 
        /* Query driver if possible and precision timestamping enabled. */
        if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
-               ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
+               ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
                                                        tvblank, flags);
                if (ret > 0)
                        return true;
@@ -834,7 +932,7 @@ drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
 /**
  * drm_vblank_count - retrieve "cooked" vblank counter value
  * @dev: DRM device
- * @crtc: which counter to retrieve
+ * @pipe: index of CRTC for which to retrieve the counter
  *
  * Fetches the "cooked" vblank count value that represents the number of
  * vblank events since the system was booted, including lost events due to
@@ -845,13 +943,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
  * Returns:
  * The software vblank counter.
  */
-u32 drm_vblank_count(struct drm_device *dev, int crtc)
+u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return 0;
-       return atomic_read(&vblank->count);
+
+       return vblank->count;
 }
 EXPORT_SYMBOL(drm_vblank_count);
 
@@ -875,47 +974,72 @@ u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
 EXPORT_SYMBOL(drm_crtc_vblank_count);
 
 /**
- * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
- * and the system timestamp corresponding to that vblank counter value.
- *
+ * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
+ *     system timestamp corresponding to that vblank counter value.
  * @dev: DRM device
- * @crtc: which counter to retrieve
+ * @pipe: index of CRTC whose counter to retrieve
  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  *
  * Fetches the "cooked" vblank count value that represents the number of
  * vblank events since the system was booted, including lost events due to
  * modesetting activity. Returns corresponding system timestamp of the time
  * of the vblank interval that corresponds to the current vblank counter value.
+ *
+ * This is the legacy version of drm_crtc_vblank_count_and_time().
  */
-u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
+u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
                              struct timeval *vblanktime)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
+       int count = DRM_TIMESTAMP_MAXRETRIES;
        u32 cur_vblank;
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return 0;
 
-       /* Read timestamp from slot of _vblank_time ringbuffer
-        * that corresponds to current vblank count. Retry if
-        * count has incremented during readout. This works like
-        * a seqlock.
+       /*
+        * Vblank timestamps are read lockless. To ensure consistency the vblank
+        * counter is rechecked and ordering is ensured using memory barriers.
+        * This works like a seqlock. The write-side barriers are in store_vblank.
         */
        do {
-               cur_vblank = atomic_read(&vblank->count);
-               *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
+               cur_vblank = vblank->count;
+               smp_rmb();
+               *vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
                smp_rmb();
-       } while (cur_vblank != atomic_read(&vblank->count));
+       } while (cur_vblank != vblank->count && --count > 0);
 
        return cur_vblank;
 }
 EXPORT_SYMBOL(drm_vblank_count_and_time);
 
+/**
+ * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
+ *     and the system timestamp corresponding to that vblank counter value
+ * @crtc: which counter to retrieve
+ * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
+ *
+ * Fetches the "cooked" vblank count value that represents the number of
+ * vblank events since the system was booted, including lost events due to
+ * modesetting activity. Returns corresponding system timestamp of the time
+ * of the vblank interval that corresponds to the current vblank counter value.
+ *
+ * This is the native KMS version of drm_vblank_count_and_time().
+ */
+u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
+                                  struct timeval *vblanktime)
+{
+       return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
+                                        vblanktime);
+}
+EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
+
 static void send_vblank_event(struct drm_device *dev,
                struct drm_pending_vblank_event *e,
                unsigned long seq, struct timeval *now)
 {
-       WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
+       assert_spin_locked(&dev->event_lock);
+
        e->event.sequence = seq;
        e->event.tv_sec = now->tv_sec;
        e->event.tv_usec = now->tv_usec;
@@ -927,10 +1051,61 @@ static void send_vblank_event(struct drm_device *dev,
                                         e->event.sequence);
 }
 
+/**
+ * drm_arm_vblank_event - arm vblank event after pageflip
+ * @dev: DRM device
+ * @pipe: CRTC index
+ * @e: the event to prepare to send
+ *
+ * A lot of drivers need to generate vblank events for the very next vblank
+ * interrupt. For example when the page flip interrupt happens when the page
+ * flip gets armed, but not when it actually executes within the next vblank
+ * period. This helper function implements exactly the required vblank arming
+ * behaviour.
+ *
+ * Caller must hold event lock. Caller must also hold a vblank reference for
+ * the event @e, which will be dropped when the next vblank arrives.
+ *
+ * This is the legacy version of drm_crtc_arm_vblank_event().
+ */
+void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
+                         struct drm_pending_vblank_event *e)
+{
+       assert_spin_locked(&dev->event_lock);
+
+       e->pipe = pipe;
+       e->event.sequence = drm_vblank_count(dev, pipe);
+       list_add_tail(&e->base.link, &dev->vblank_event_list);
+}
+EXPORT_SYMBOL(drm_arm_vblank_event);
+
+/**
+ * drm_crtc_arm_vblank_event - arm vblank event after pageflip
+ * @crtc: the source CRTC of the vblank event
+ * @e: the event to send
+ *
+ * A lot of drivers need to generate vblank events for the very next vblank
+ * interrupt. For example when the page flip interrupt happens when the page
+ * flip gets armed, but not when it actually executes within the next vblank
+ * period. This helper function implements exactly the required vblank arming
+ * behaviour.
+ *
+ * Caller must hold event lock. Caller must also hold a vblank reference for
+ * the event @e, which will be dropped when the next vblank arrives.
+ *
+ * This is the native KMS version of drm_arm_vblank_event().
+ */
+void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
+                              struct drm_pending_vblank_event *e)
+{
+       drm_arm_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
+}
+EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
+
 /**
  * drm_send_vblank_event - helper to send vblank event after pageflip
  * @dev: DRM device
- * @crtc: CRTC in question
+ * @pipe: CRTC index
  * @e: the event to send
  *
  * Updates sequence # and timestamp on event, and sends it to userspace.
@@ -938,20 +1113,20 @@ static void send_vblank_event(struct drm_device *dev,
  *
  * This is the legacy version of drm_crtc_send_vblank_event().
  */
-void drm_send_vblank_event(struct drm_device *dev, int crtc,
-               struct drm_pending_vblank_event *e)
+void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
+                          struct drm_pending_vblank_event *e)
 {
        struct timeval now;
        unsigned int seq;
 
-       if (crtc >= 0) {
-               seq = drm_vblank_count_and_time(dev, crtc, &now);
+       if (dev->num_crtcs > 0) {
+               seq = drm_vblank_count_and_time(dev, pipe, &now);
        } else {
                seq = 0;
 
                now = get_drm_timestamp();
        }
-       e->pipe = crtc;
+       e->pipe = pipe;
        send_vblank_event(dev, e, seq, &now);
 }
 EXPORT_SYMBOL(drm_send_vblank_event);
@@ -976,11 +1151,14 @@ EXPORT_SYMBOL(drm_crtc_send_vblank_event);
 /**
  * drm_vblank_enable - enable the vblank interrupt on a CRTC
  * @dev: DRM device
- * @crtc: CRTC in question
+ * @pipe: CRTC index
+ *
+ * Returns:
+ * Zero on success or a negative error code on failure.
  */
-static int drm_vblank_enable(struct drm_device *dev, int crtc)
+static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        int ret = 0;
 
        assert_spin_locked(&dev->vbl_lock);
@@ -995,13 +1173,13 @@ static int drm_vblank_enable(struct drm_device *dev, int crtc)
                 * timestamps. Filtercode in drm_handle_vblank() will
                 * prevent double-accounting of same vblank interval.
                 */
-               ret = dev->driver->enable_vblank(dev, crtc);
-               DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
+               ret = dev->driver->enable_vblank(dev, pipe);
+               DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
                if (ret)
                        atomic_dec(&vblank->refcount);
                else {
                        vblank->enabled = true;
-                       drm_update_vblank_count(dev, crtc);
+                       drm_update_vblank_count(dev, pipe, 0);
                }
        }
 
@@ -1013,7 +1191,7 @@ static int drm_vblank_enable(struct drm_device *dev, int crtc)
 /**
  * drm_vblank_get - get a reference count on vblank events
  * @dev: DRM device
- * @crtc: which CRTC to own
+ * @pipe: index of CRTC to own
  *
  * Acquire a reference count on vblank events to avoid having them disabled
  * while in use.
@@ -1021,21 +1199,24 @@ static int drm_vblank_enable(struct drm_device *dev, int crtc)
  * This is the legacy version of drm_crtc_vblank_get().
  *
  * Returns:
- * Zero on success, nonzero on failure.
+ * Zero on success or a negative error code on failure.
  */
-int drm_vblank_get(struct drm_device *dev, int crtc)
+int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        unsigned long irqflags;
        int ret = 0;
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (!dev->num_crtcs)
+               return -EINVAL;
+
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return -EINVAL;
 
        spin_lock_irqsave(&dev->vbl_lock, irqflags);
        /* Going from 0->1 means we have to enable interrupts again */
        if (atomic_add_return(1, &vblank->refcount) == 1) {
-               ret = drm_vblank_enable(dev, crtc);
+               ret = drm_vblank_enable(dev, pipe);
        } else {
                if (!vblank->enabled) {
                        atomic_dec(&vblank->refcount);
@@ -1058,7 +1239,7 @@ EXPORT_SYMBOL(drm_vblank_get);
  * This is the native kms version of drm_vblank_get().
  *
  * Returns:
- * Zero on success, nonzero on failure.
+ * Zero on success or a negative error code on failure.
  */
 int drm_crtc_vblank_get(struct drm_crtc *crtc)
 {
@@ -1067,23 +1248,23 @@ int drm_crtc_vblank_get(struct drm_crtc *crtc)
 EXPORT_SYMBOL(drm_crtc_vblank_get);
 
 /**
- * drm_vblank_put - give up ownership of vblank events
+ * drm_vblank_put - release ownership of vblank events
  * @dev: DRM device
- * @crtc: which counter to give up
+ * @pipe: index of CRTC to release
  *
  * Release ownership of a given vblank counter, turning off interrupts
  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  *
  * This is the legacy version of drm_crtc_vblank_put().
  */
-void drm_vblank_put(struct drm_device *dev, int crtc)
+void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
 
-       if (WARN_ON(atomic_read(&vblank->refcount) == 0))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return;
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (WARN_ON(atomic_read(&vblank->refcount) == 0))
                return;
 
        /* Last user schedules interrupt disable */
@@ -1117,30 +1298,34 @@ EXPORT_SYMBOL(drm_crtc_vblank_put);
 /**
  * drm_wait_one_vblank - wait for one vblank
  * @dev: DRM device
- * @crtc: crtc index
+ * @pipe: CRTC index
  *
- * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
- * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
+ * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
+ * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  * due to lack of driver support or because the crtc is off.
  */
-void drm_wait_one_vblank(struct drm_device *dev, int crtc)
+void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
 {
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        int ret;
        u32 last;
 
-       ret = drm_vblank_get(dev, crtc);
-       if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", crtc, ret))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return;
 
-       last = drm_vblank_count(dev, crtc);
+       ret = drm_vblank_get(dev, pipe);
+       if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
+               return;
+
+       last = drm_vblank_count(dev, pipe);
 
-       ret = wait_event_timeout(dev->vblank[crtc].queue,
-                                last != drm_vblank_count(dev, crtc),
+       ret = wait_event_timeout(vblank->queue,
+                                last != drm_vblank_count(dev, pipe),
                                 msecs_to_jiffies(100));
 
-       WARN(ret == 0, "vblank wait timed out on crtc %i\n", crtc);
+       WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
 
-       drm_vblank_put(dev, crtc);
+       drm_vblank_put(dev, pipe);
 }
 EXPORT_SYMBOL(drm_wait_one_vblank);
 
@@ -1161,7 +1346,7 @@ EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
 /**
  * drm_vblank_off - disable vblank events on a CRTC
  * @dev: DRM device
- * @crtc: CRTC in question
+ * @pipe: CRTC index
  *
  * Drivers can use this function to shut down the vblank interrupt handling when
  * disabling a crtc. This function ensures that the latest vblank frame count is
@@ -1172,21 +1357,27 @@ EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  *
  * This is the legacy version of drm_crtc_vblank_off().
  */
-void drm_vblank_off(struct drm_device *dev, int crtc)
+void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        struct drm_pending_vblank_event *e, *t;
        struct timeval now;
        unsigned long irqflags;
        unsigned int seq;
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return;
 
        spin_lock_irqsave(&dev->event_lock, irqflags);
 
        spin_lock(&dev->vbl_lock);
-       vblank_disable_and_save(dev, crtc);
+       DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
+                     pipe, vblank->enabled, vblank->inmodeset);
+
+       /* Avoid redundant vblank disables without previous drm_vblank_on(). */
+       if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
+               vblank_disable_and_save(dev, pipe);
+
        wake_up(&vblank->queue);
 
        /*
@@ -1200,16 +1391,16 @@ void drm_vblank_off(struct drm_device *dev, int crtc)
        spin_unlock(&dev->vbl_lock);
 
        /* Send any queued vblank events, lest the natives grow disquiet */
-       seq = drm_vblank_count_and_time(dev, crtc, &now);
+       seq = drm_vblank_count_and_time(dev, pipe, &now);
 
        list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
-               if (e->pipe != crtc)
+               if (e->pipe != pipe)
                        continue;
-               DRM_DEBUG("Sending premature vblank event on disable: \
-                         wanted %d, current %d\n",
+               DRM_DEBUG("Sending premature vblank event on disable: "
+                         "wanted %d, current %d\n",
                          e->event.sequence, seq);
                list_del(&e->base.link);
-               drm_vblank_put(dev, e->pipe);
+               drm_vblank_put(dev, pipe);
                send_vblank_event(dev, e, seq, &now);
        }
        spin_unlock_irqrestore(&dev->event_lock, irqflags);
@@ -1245,12 +1436,12 @@ EXPORT_SYMBOL(drm_crtc_vblank_off);
  * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  * and hence doesn't need to call any driver hooks.
  */
-void drm_crtc_vblank_reset(struct drm_crtc *drm_crtc)
+void drm_crtc_vblank_reset(struct drm_crtc *crtc)
 {
-       struct drm_device *dev = drm_crtc->dev;
+       struct drm_device *dev = crtc->dev;
        unsigned long irqflags;
-       int crtc = drm_crtc_index(drm_crtc);
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       unsigned int pipe = drm_crtc_index(crtc);
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
 
        spin_lock_irqsave(&dev->vbl_lock, irqflags);
        /*
@@ -1270,7 +1461,7 @@ EXPORT_SYMBOL(drm_crtc_vblank_reset);
 /**
  * drm_vblank_on - enable vblank events on a CRTC
  * @dev: DRM device
- * @crtc: CRTC in question
+ * @pipe: CRTC index
  *
  * This functions restores the vblank interrupt state captured with
  * drm_vblank_off() again. Note that calls to drm_vblank_on() and
@@ -1279,38 +1470,32 @@ EXPORT_SYMBOL(drm_crtc_vblank_reset);
  *
  * This is the legacy version of drm_crtc_vblank_on().
  */
-void drm_vblank_on(struct drm_device *dev, int crtc)
+void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        unsigned long irqflags;
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return;
 
        spin_lock_irqsave(&dev->vbl_lock, irqflags);
+       DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
+                     pipe, vblank->enabled, vblank->inmodeset);
+
        /* Drop our private "prevent drm_vblank_get" refcount */
        if (vblank->inmodeset) {
                atomic_dec(&vblank->refcount);
                vblank->inmodeset = 0;
        }
 
-       /*
-        * sample the current counter to avoid random jumps
-        * when drm_vblank_enable() applies the diff
-        *
-        * -1 to make sure user will never see the same
-        * vblank counter value before and after a modeset
-        */
-       vblank->last =
-               (dev->driver->get_vblank_counter(dev, crtc) - 1) &
-               dev->max_vblank_count;
+       drm_reset_vblank_timestamp(dev, pipe);
+
        /*
         * re-enable interrupts if there are users left, or the
         * user wishes vblank interrupts to be enabled all the time.
         */
-       if (atomic_read(&vblank->refcount) != 0 ||
-           (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0))
-               WARN_ON(drm_vblank_enable(dev, crtc));
+       if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
+               WARN_ON(drm_vblank_enable(dev, pipe));
        spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
 }
 EXPORT_SYMBOL(drm_vblank_on);
@@ -1335,7 +1520,7 @@ EXPORT_SYMBOL(drm_crtc_vblank_on);
 /**
  * drm_vblank_pre_modeset - account for vblanks across mode sets
  * @dev: DRM device
- * @crtc: CRTC in question
+ * @pipe: CRTC index
  *
  * Account for vblank events across mode setting events, which will likely
  * reset the hardware frame counter.
@@ -1355,15 +1540,15 @@ EXPORT_SYMBOL(drm_crtc_vblank_on);
  * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
  * again.
  */
-void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
+void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
 
        /* vblank is not initialized (IRQ not installed ?), or has been freed */
        if (!dev->num_crtcs)
                return;
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return;
 
        /*
@@ -1375,7 +1560,7 @@ void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
         */
        if (!vblank->inmodeset) {
                vblank->inmodeset = 0x1;
-               if (drm_vblank_get(dev, crtc) == 0)
+               if (drm_vblank_get(dev, pipe) == 0)
                        vblank->inmodeset |= 0x2;
        }
 }
@@ -1384,27 +1569,31 @@ EXPORT_SYMBOL(drm_vblank_pre_modeset);
 /**
  * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
  * @dev: DRM device
- * @crtc: CRTC in question
+ * @pipe: CRTC index
  *
  * This function again drops the temporary vblank reference acquired in
  * drm_vblank_pre_modeset.
  */
-void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
+void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        unsigned long irqflags;
 
        /* vblank is not initialized (IRQ not installed ?), or has been freed */
        if (!dev->num_crtcs)
                return;
 
+       if (WARN_ON(pipe >= dev->num_crtcs))
+               return;
+
        if (vblank->inmodeset) {
                spin_lock_irqsave(&dev->vbl_lock, irqflags);
                dev->vblank_disable_allowed = true;
+               drm_reset_vblank_timestamp(dev, pipe);
                spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
 
                if (vblank->inmodeset & 0x2)
-                       drm_vblank_put(dev, crtc);
+                       drm_vblank_put(dev, pipe);
 
                vblank->inmodeset = 0;
        }
@@ -1426,7 +1615,7 @@ int drm_modeset_ctl(struct drm_device *dev, void *data,
                    struct drm_file *file_priv)
 {
        struct drm_modeset_ctl *modeset = data;
-       unsigned int crtc;
+       unsigned int pipe;
 
        /* If drm_vblank_init() hasn't been called yet, just no-op */
        if (!dev->num_crtcs)
@@ -1436,16 +1625,16 @@ int drm_modeset_ctl(struct drm_device *dev, void *data,
        if (drm_core_check_feature(dev, DRIVER_MODESET))
                return 0;
 
-       crtc = modeset->crtc;
-       if (crtc >= dev->num_crtcs)
+       pipe = modeset->crtc;
+       if (pipe >= dev->num_crtcs)
                return -EINVAL;
 
        switch (modeset->cmd) {
        case _DRM_PRE_MODESET:
-               drm_vblank_pre_modeset(dev, crtc);
+               drm_vblank_pre_modeset(dev, pipe);
                break;
        case _DRM_POST_MODESET:
-               drm_vblank_post_modeset(dev, crtc);
+               drm_vblank_post_modeset(dev, pipe);
                break;
        default:
                return -EINVAL;
@@ -1454,7 +1643,7 @@ int drm_modeset_ctl(struct drm_device *dev, void *data,
        return 0;
 }
 
-static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
+static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
                                  union drm_wait_vblank *vblwait,
                                  struct drm_file *file_priv)
 {
@@ -1508,7 +1697,7 @@ static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
                vblwait->reply.sequence = vblwait->request.sequence;
        }
 
-       DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
+       DRM_DEBUG("event on vblank count %d, current %d, crtc %u\n",
                  vblwait->request.sequence, seq, pipe);
 
        trace_drm_vblank_event_queued(current->pid, pipe,
@@ -1557,7 +1746,7 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
        struct drm_vblank_crtc *vblank;
        union drm_wait_vblank *vblwait = data;
        int ret;
-       unsigned int flags, seq, crtc, high_crtc;
+       unsigned int flags, seq, pipe, high_pipe;
 
        if (!dev->irq_enabled)
                return -EINVAL;
@@ -1576,22 +1765,22 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
        }
 
        flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
-       high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
-       if (high_crtc)
-               crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
+       high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
+       if (high_pipe)
+               pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
        else
-               crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
-       if (crtc >= dev->num_crtcs)
+               pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
+       if (pipe >= dev->num_crtcs)
                return -EINVAL;
 
-       vblank = &dev->vblank[crtc];
+       vblank = &dev->vblank[pipe];
 
-       ret = drm_vblank_get(dev, crtc);
+       ret = drm_vblank_get(dev, pipe);
        if (ret) {
                DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
                return ret;
        }
-       seq = drm_vblank_count(dev, crtc);
+       seq = drm_vblank_count(dev, pipe);
 
        switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
        case _DRM_VBLANK_RELATIVE:
@@ -1608,7 +1797,7 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
                /* must hold on to the vblank ref until the event fires
                 * drm_vblank_put will be called asynchronously
                 */
-               return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
+               return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
        }
 
        if ((flags & _DRM_VBLANK_NEXTONMISS) &&
@@ -1616,11 +1805,11 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
                vblwait->request.sequence = seq + 1;
        }
 
-       DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
-                 vblwait->request.sequence, crtc);
+       DRM_DEBUG("waiting on vblank count %d, crtc %u\n",
+                 vblwait->request.sequence, pipe);
        vblank->last_wait = vblwait->request.sequence;
        DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
-                   (((drm_vblank_count(dev, crtc) -
+                   (((drm_vblank_count(dev, pipe) -
                       vblwait->request.sequence) <= (1 << 23)) ||
                     !vblank->enabled ||
                     !dev->irq_enabled));
@@ -1628,7 +1817,7 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
        if (ret != -EINTR) {
                struct timeval now;
 
-               vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
+               vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
                vblwait->reply.tval_sec = now.tv_sec;
                vblwait->reply.tval_usec = now.tv_usec;
 
@@ -1639,11 +1828,11 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
        }
 
 done:
-       drm_vblank_put(dev, crtc);
+       drm_vblank_put(dev, pipe);
        return ret;
 }
 
-static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
+static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
 {
        struct drm_pending_vblank_event *e, *t;
        struct timeval now;
@@ -1651,10 +1840,10 @@ static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
 
        assert_spin_locked(&dev->event_lock);
 
-       seq = drm_vblank_count_and_time(dev, crtc, &now);
+       seq = drm_vblank_count_and_time(dev, pipe, &now);
 
        list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
-               if (e->pipe != crtc)
+               if (e->pipe != pipe)
                        continue;
                if ((seq - e->event.sequence) > (1<<23))
                        continue;
@@ -1663,35 +1852,32 @@ static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
                          e->event.sequence, seq);
 
                list_del(&e->base.link);
-               drm_vblank_put(dev, e->pipe);
+               drm_vblank_put(dev, pipe);
                send_vblank_event(dev, e, seq, &now);
        }
 
-       trace_drm_vblank_event(crtc, seq);
+       trace_drm_vblank_event(pipe, seq);
 }
 
 /**
  * drm_handle_vblank - handle a vblank event
  * @dev: DRM device
- * @crtc: where this event occurred
+ * @pipe: index of CRTC where this event occurred
  *
  * Drivers should call this routine in their vblank interrupt handlers to
  * update the vblank counter and send any signals that may be pending.
  *
  * This is the legacy version of drm_crtc_handle_vblank().
  */
-bool drm_handle_vblank(struct drm_device *dev, int crtc)
+bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
-       u32 vblcount;
-       s64 diff_ns;
-       struct timeval tvblank;
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        unsigned long irqflags;
 
        if (WARN_ON_ONCE(!dev->num_crtcs))
                return false;
 
-       if (WARN_ON(crtc >= dev->num_crtcs))
+       if (WARN_ON(pipe >= dev->num_crtcs))
                return false;
 
        spin_lock_irqsave(&dev->event_lock, irqflags);
@@ -1709,46 +1895,12 @@ bool drm_handle_vblank(struct drm_device *dev, int crtc)
                return false;
        }
 
-       /* Fetch corresponding timestamp for this vblank interval from
-        * driver and store it in proper slot of timestamp ringbuffer.
-        */
-
-       /* Get current timestamp and count. */
-       vblcount = atomic_read(&vblank->count);
-       drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
-
-       /* Compute time difference to timestamp of last vblank */
-       diff_ns = timeval_to_ns(&tvblank) -
-                 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
-
-       /* Update vblank timestamp and count if at least
-        * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
-        * difference between last stored timestamp and current
-        * timestamp. A smaller difference means basically
-        * identical timestamps. Happens if this vblank has
-        * been already processed and this is a redundant call,
-        * e.g., due to spurious vblank interrupts. We need to
-        * ignore those for accounting.
-        */
-       if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
-               /* Store new timestamp in ringbuffer. */
-               vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
-
-               /* Increment cooked vblank count. This also atomically commits
-                * the timestamp computed above.
-                */
-               smp_mb__before_atomic();
-               atomic_inc(&vblank->count);
-               smp_mb__after_atomic();
-       } else {
-               DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
-                         crtc, (int) diff_ns);
-       }
+       drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
 
        spin_unlock(&dev->vblank_time_lock);
 
        wake_up(&vblank->queue);
-       drm_handle_vblank_events(dev, crtc);
+       drm_handle_vblank_events(dev, pipe);
 
        spin_unlock_irqrestore(&dev->event_lock, irqflags);
 
@@ -1773,3 +1925,20 @@ bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
        return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
 }
 EXPORT_SYMBOL(drm_crtc_handle_vblank);
+
+/**
+ * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
+ * @dev: DRM device
+ * @pipe: CRTC for which to read the counter
+ *
+ * Drivers can plug this into the .get_vblank_counter() function if
+ * there is no useable hardware frame counter available.
+ *
+ * Returns:
+ * 0
+ */
+u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
+{
+       return 0;
+}
+EXPORT_SYMBOL(drm_vblank_no_hw_counter);