drm: Clean up drm_calc_vbltimestamp_from_scanoutpos() vbl_status
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_irq.c
index 22d207e211e719b76b17c2a470d8323bb73ae6ba..aad4f1d63a2ea382b5893073bc99b337320b780d 100644 (file)
@@ -74,11 +74,11 @@ 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, int crtc,
+static void store_vblank(struct drm_device *dev, unsigned int pipe,
                         u32 vblank_count_inc,
                         struct timeval *t_vblank)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        u32 tslot;
 
        assert_spin_locked(&dev->vblank_time_lock);
@@ -88,7 +88,7 @@ static void store_vblank(struct drm_device *dev, int crtc,
                 * the latching of vblank->count below.
                 */
                tslot = vblank->count + vblank_count_inc;
-               vblanktimestamp(dev, crtc, tslot) = *t_vblank;
+               vblanktimestamp(dev, pipe, tslot) = *t_vblank;
        }
 
        /*
@@ -110,7 +110,7 @@ static void store_vblank(struct drm_device *dev, int crtc,
  * @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.
  *
@@ -120,12 +120,14 @@ static void store_vblank(struct drm_device *dev, int crtc,
  * Note: caller must hold dev->vbl_lock since this reads & writes
  * device vblank fields.
  */
-static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe)
+static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
+                                   unsigned long flags)
 {
        struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        u32 cur_vblank, diff;
        bool rc;
        struct timeval t_vblank;
+       int count = DRM_TIMESTAMP_MAXRETRIES;
 
        /*
         * Interrupts were disabled prior to this call, so deal with counter
@@ -141,8 +143,8 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe)
         */
        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));
+               rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
+       } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
 
        /* Deal with counter wrap */
        diff = cur_vblank - vblank->last;
@@ -207,7 +209,7 @@ static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
         */
        if (!vblank->enabled &&
            drm_get_last_vbltimestamp(dev, pipe, &tvblank, 0)) {
-               drm_update_vblank_count(dev, pipe);
+               drm_update_vblank_count(dev, pipe, 0);
                spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
                return;
        }
@@ -603,7 +605,8 @@ 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_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
+       int linedur_ns = 0, framedur_ns = 0;
        int dotclock = mode->crtc_clock;
 
        /* Valid dotclock? */
@@ -612,10 +615,9 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc,
 
                /*
                 * 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);
 
@@ -628,16 +630,14 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc,
                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 %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
                  crtc->base.id, mode->crtc_htotal,
                  mode->crtc_vtotal, mode->crtc_vdisplay);
-       DRM_DEBUG("crtc %u: 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);
 
@@ -651,7 +651,6 @@ EXPORT_SYMBOL(drm_calc_timestamping_constants);
  * @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
@@ -692,15 +691,14 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
                                          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 (pipe >= dev->num_crtcs) {
                DRM_ERROR("Invalid crtc %u\n", pipe);
@@ -713,15 +711,10 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
                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) {
+       if (mode->crtc_clock == 0) {
                DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
                return -EAGAIN;
        }
@@ -738,12 +731,14 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
                 * Get vertical and horizontal scanout position vpos, hpos,
                 * and bounding timestamps stime, etime, pre/post query.
                 */
-               vbl_status = dev->driver->get_scanout_position(dev, pipe, 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 %u : scanoutpos query failed [%d].\n",
+                       DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
                                  pipe, vbl_status);
                        return -EIO;
                }
@@ -770,13 +765,15 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
         * 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);
@@ -792,17 +789,13 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
                etime = ktime_sub_ns(etime, delta_ns);
        *vblank_time = ktime_to_timeval(etime);
 
-       DRM_DEBUG("crtc %u : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
-                 pipe, (int)vbl_status, hpos, vpos,
+       DRM_DEBUG("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);
 
@@ -919,6 +912,7 @@ u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
                              struct timeval *vblanktime)
 {
        struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
+       int count = DRM_TIMESTAMP_MAXRETRIES;
        u32 cur_vblank;
 
        if (WARN_ON(pipe >= dev->num_crtcs))
@@ -934,7 +928,7 @@ u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
                smp_rmb();
                *vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
                smp_rmb();
-       } while (cur_vblank != vblank->count);
+       } while (cur_vblank != vblank->count && --count > 0);
 
        return cur_vblank;
 }
@@ -1033,7 +1027,7 @@ static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
                        atomic_dec(&vblank->refcount);
                else {
                        vblank->enabled = true;
-                       drm_update_vblank_count(dev, pipe);
+                       drm_update_vblank_count(dev, pipe, 0);
                }
        }
 
@@ -1154,8 +1148,8 @@ EXPORT_SYMBOL(drm_crtc_vblank_put);
  * @dev: DRM device
  * @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, unsigned int pipe)
@@ -1288,8 +1282,8 @@ void drm_crtc_vblank_reset(struct drm_crtc *drm_crtc)
 {
        struct drm_device *dev = drm_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(drm_crtc);
+       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
 
        spin_lock_irqsave(&dev->vbl_lock, irqflags);
        /*