drm: Stop using linedur_ns and pixeldur_ns for vblank timestamps
authorVille Syrjälä <ville.syrjala@linux.intel.com>
Mon, 14 Sep 2015 19:43:44 +0000 (22:43 +0300)
committerDaniel Vetter <daniel.vetter@ffwll.ch>
Thu, 24 Sep 2015 18:14:21 +0000 (20:14 +0200)
linedur_ns, and especially pixeldur_ns are becoming rather inaccurate
to be used for the vblank timestamp correction. With 4k@60 the pixel
duration is already below 2ns, so the amount of error due to the
truncation to nanoseconds is introducing quite a bit of error.

We can avoid such problems if we instead calculate the timestamp
delta_ns directly from the dislay timings, avoiding the use of
these intermediate truncated values.

Signed-off-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
Reviewed-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
[danvet: Squash in fixup from Thierry Reding for amdgpu.]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
drivers/gpu/drm/amd/amdgpu/amdgpu_display.c
drivers/gpu/drm/amd/amdgpu/amdgpu_mode.h
drivers/gpu/drm/drm_irq.c
drivers/gpu/drm/i915/i915_irq.c
drivers/gpu/drm/nouveau/nouveau_display.c
drivers/gpu/drm/nouveau/nouveau_display.h
drivers/gpu/drm/radeon/radeon_display.c
drivers/gpu/drm/radeon/radeon_drv.c
drivers/gpu/drm/radeon/radeon_mode.h
drivers/gpu/drm/radeon/radeon_pm.c
include/drm/drmP.h

index e3d70772b53104f1f6a48020088d8391d10985b3..9b34a3410c321f93d062c92b630fb1ff65dc29b4 100644 (file)
@@ -745,7 +745,8 @@ bool amdgpu_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  *
  */
 int amdgpu_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
-                              int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
+                              int *vpos, int *hpos, ktime_t *stime, ktime_t *etime,
+                              const struct drm_display_mode *mode)
 {
        u32 vbl = 0, position = 0;
        int vbl_start, vbl_end, vtotal, ret = 0;
@@ -781,7 +782,7 @@ int amdgpu_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int fl
        }
        else {
                /* No: Fake something reasonable which gives at least ok results. */
-               vbl_start = adev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
+               vbl_start = mode->crtc_vdisplay;
                vbl_end = 0;
        }
 
@@ -797,7 +798,7 @@ int amdgpu_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int fl
 
        /* Inside "upper part" of vblank area? Apply corrective offset if so: */
        if (in_vbl && (*vpos >= vbl_start)) {
-               vtotal = adev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
+               vtotal = mode->crtc_vtotal;
                *vpos = *vpos - vtotal;
        }
 
@@ -819,8 +820,8 @@ int amdgpu_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int fl
         * We only do this if DRM_CALLED_FROM_VBLIRQ.
         */
        if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
-               vbl_start = adev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
-               vtotal = adev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
+               vbl_start = mode->crtc_vdisplay;
+               vtotal = mode->crtc_vtotal;
 
                if (vbl_start - *vpos < vtotal / 100) {
                        *vpos -= vtotal;
index 64efe5b52e6500f840f0ad7edbdc6a9f64db6f66..2b03425f97406487337306a50f76dbbc538c51c1 100644 (file)
@@ -543,7 +543,8 @@ void amdgpu_encoder_set_active_device(struct drm_encoder *encoder);
 int amdgpu_get_crtc_scanoutpos(struct drm_device *dev, int crtc,
                                      unsigned int flags,
                                      int *vpos, int *hpos, ktime_t *stime,
-                                     ktime_t *etime);
+                                     ktime_t *etime,
+                                     const struct drm_display_mode *mode);
 
 int amdgpu_framebuffer_init(struct drm_device *dev,
                             struct amdgpu_framebuffer *rfb,
index 6b2fefde25a812781ed4132af653440481f982bf..9fab3334eb6b97e94c6665e81c37d8f98f923494 100644 (file)
@@ -694,12 +694,11 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
                                          unsigned flags,
                                          const struct drm_display_mode *mode)
 {
-       struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
        struct timeval tv_etime;
        ktime_t stime, etime;
        int vbl_status;
        int vpos, hpos, i;
-       int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
+       int delta_ns, duration_ns;
        bool invbl;
 
        if (pipe >= dev->num_crtcs) {
@@ -713,15 +712,10 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
                return -EIO;
        }
 
-       /* Durations of frames, lines, pixels in nanoseconds. */
-       framedur_ns = vblank->framedur_ns;
-       linedur_ns  = vblank->linedur_ns;
-       pixeldur_ns = vblank->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,8 +732,10 @@ 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)) {
@@ -776,7 +772,8 @@ int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
         * 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);
index 4cbc7221173888da5872edde252c432f62fbb327..ce1e0f5d11dee9d168ba7842730f19d1a182e302 100644 (file)
@@ -648,12 +648,12 @@ static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
 
 static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
                                    unsigned int flags, int *vpos, int *hpos,
-                                   ktime_t *stime, ktime_t *etime)
+                                   ktime_t *stime, ktime_t *etime,
+                                   const struct drm_display_mode *mode)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
        int position;
        int vbl_start, vbl_end, hsync_start, htotal, vtotal;
        bool in_vbl = true;
index 425515feef6e0108f099ca39312206c251183d3f..a82c3cbe31274390dbda44026a57246e7ee6df73 100644 (file)
@@ -133,7 +133,8 @@ nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
 
 int
 nouveau_display_scanoutpos(struct drm_device *dev, int head, unsigned int flags,
-                          int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
+                          int *vpos, int *hpos, ktime_t *stime, ktime_t *etime,
+                          const struct drm_display_mode *mode)
 {
        struct drm_crtc *crtc;
 
index a6213e2425c597cca0168e12188476a1e8baad31..4182d21538c5162f80e522c4591fb8f05a2b2ad6 100644 (file)
@@ -68,7 +68,8 @@ void nouveau_display_resume(struct drm_device *dev, bool runtime);
 int  nouveau_display_vblank_enable(struct drm_device *, int);
 void nouveau_display_vblank_disable(struct drm_device *, int);
 int  nouveau_display_scanoutpos(struct drm_device *, int, unsigned int,
-                               int *, int *, ktime_t *, ktime_t *);
+                               int *, int *, ktime_t *, ktime_t *,
+                               const struct drm_display_mode *);
 int  nouveau_display_vblstamp(struct drm_device *, int, int *,
                              struct timeval *, unsigned);
 
index d2e9e9efc159c053b954aed21840ebe7d91f2739..0503af748d99e3c36714d5dc5321bc248eba6c21 100644 (file)
@@ -323,7 +323,8 @@ void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
         */
        if (update_pending &&
            (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
-                                                              &vpos, &hpos, NULL, NULL)) &&
+                                                              &vpos, &hpos, NULL, NULL,
+                                                              &rdev->mode_info.crtcs[crtc_id]->base.hwmode)) &&
            ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
             (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
                /* crtc didn't flip in this target vblank interval,
@@ -1799,7 +1800,8 @@ bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  *
  */
 int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
-                              int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
+                              int *vpos, int *hpos, ktime_t *stime, ktime_t *etime,
+                              const struct drm_display_mode *mode)
 {
        u32 stat_crtc = 0, vbl = 0, position = 0;
        int vbl_start, vbl_end, vtotal, ret = 0;
@@ -1914,7 +1916,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int fl
        }
        else {
                /* No: Fake something reasonable which gives at least ok results. */
-               vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
+               vbl_start = mode->crtc_vdisplay;
                vbl_end = 0;
        }
 
@@ -1930,7 +1932,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int fl
 
        /* Inside "upper part" of vblank area? Apply corrective offset if so: */
        if (in_vbl && (*vpos >= vbl_start)) {
-               vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
+               vtotal = mode->crtc_vtotal;
                *vpos = *vpos - vtotal;
        }
 
@@ -1952,8 +1954,8 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int fl
         * We only do this if DRM_CALLED_FROM_VBLIRQ.
         */
        if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
-               vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
-               vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
+               vbl_start = mode->crtc_vdisplay;
+               vtotal = mode->crtc_vtotal;
 
                if (vbl_start - *vpos < vtotal / 100) {
                        *vpos -= vtotal;
index 5751446677d382428846b14fc6d37d908bb582b9..e30c1d73b4ca38825949b2cec08091b1f3464cd1 100644 (file)
@@ -126,8 +126,9 @@ struct dma_buf *radeon_gem_prime_export(struct drm_device *dev,
                                        int flags);
 extern int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc,
                                      unsigned int flags,
-                                     int *vpos, int *hpos, ktime_t *stime,
-                                     ktime_t *etime);
+                                     int *vpos, int *hpos,
+                                     ktime_t *stime, ktime_t *etime,
+                                     const struct drm_display_mode *mode);
 extern bool radeon_is_px(struct drm_device *dev);
 extern const struct drm_ioctl_desc radeon_ioctls_kms[];
 extern int radeon_max_kms_ioctl;
index aecc3e3dec0ca093441e3871df414627b51e92ec..2317d04f8a099094cc13bbe7428d573306227559 100644 (file)
@@ -876,8 +876,9 @@ extern void radeon_cursor_reset(struct drm_crtc *crtc);
 
 extern int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc,
                                      unsigned int flags,
-                                     int *vpos, int *hpos, ktime_t *stime,
-                                     ktime_t *etime);
+                                     int *vpos, int *hpos,
+                                     ktime_t *stime, ktime_t *etime,
+                                     const struct drm_display_mode *mode);
 
 extern bool radeon_combios_check_hardcoded_edid(struct radeon_device *rdev);
 extern struct edid *
index 05751f3f84449d40457b3f989f0d7ab874935bbf..10f4c12e439e08f96d725816707c428193a899b5 100644 (file)
@@ -1733,7 +1733,9 @@ static bool radeon_pm_in_vbl(struct radeon_device *rdev)
         */
        for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
                if (rdev->pm.active_crtcs & (1 << crtc)) {
-                       vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
+                       vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0,
+                                                               &vpos, &hpos, NULL, NULL,
+                                                               &rdev->mode_info.crtcs[crtc]->base.hwmode);
                        if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
                            !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
                                in_vbl = false;
index 299886746c827fd443f1860f16a10fddf8caa490..b2a95e7cfeeef4ae805c510b4bba6e3ccb7c00c3 100644 (file)
@@ -482,6 +482,7 @@ struct drm_driver {
         *               scanout position query. Can be NULL to skip timestamp.
         * \param *etime Target location for timestamp taken immediately after
         *               scanout position query. Can be NULL to skip timestamp.
+        * \param mode Current display timings.
         *
         * Returns vpos as a positive number while in active scanout area.
         * Returns vpos as a negative number inside vblank, counting the number
@@ -499,8 +500,9 @@ struct drm_driver {
         */
        int (*get_scanout_position) (struct drm_device *dev, int crtc,
                                     unsigned int flags,
-                                    int *vpos, int *hpos, ktime_t *stime,
-                                    ktime_t *etime);
+                                    int *vpos, int *hpos,
+                                    ktime_t *stime, ktime_t *etime,
+                                    const struct drm_display_mode *mode);
 
        /**
         * Called by \c drm_get_last_vbltimestamp. Should return a precise