[PATCH 1/3] drm: Widen vblank count to 64 bits. Change vblank time precision to ns

Daniel Vetter daniel at ffwll.ch
Thu Jul 6 07:19:48 UTC 2017


On Wed, Jul 05, 2017 at 03:10:11PM -0700, Keith Packard wrote:
> This modifies the datatypes used by the vblank code to provide both 64
> bits of vblank count and to increase the resolution of the vblank
> timestamp from microseconds to nanoseconds.
> 
> The driver interfaces have also been changed to return 64-bits of
> vblank count; fortunately all of the code necessary to widen that value
> was already included to handle devices returning fewer than 32-bits.

Extending the reported/sw vblank counter to u64 makes sense imo, but do we
have to extend the driver interfaces too? If there's no 64 bit hw vblank
currently I think I'd be good to postpone that part, simply because I'm
too lazy to audit all the drivers for correctly setting max_vblank_count
after your change :-)

Other thought on this, since you bother to change all the types: Afaik
both timespec and timeval suffer from the 32bit issues. If we bother with
changing everything I think it'd be neat to switch all internal interfaces
over to ktime, and only convert to the userspace types once when we
generate the event. I think that's how cool hackers are supposed to do it,
but not fully sure.

Otherwise looks all good, but haven't yet carefully hunted for fumbles in
review before the above is clear.
-Daniel

> This will provide the necessary datatypes for the Vulkan API.
> 
> Signed-off-by: Keith Packard <keithp at keithp.com>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu.h      |   2 +-
>  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c  |   2 +-
>  drivers/gpu/drm/drm_vblank.c             | 159 ++++++++++++++++++-------------
>  drivers/gpu/drm/exynos/exynos_drm_crtc.c |   2 +-
>  drivers/gpu/drm/gma500/psb_drv.h         |   2 +-
>  drivers/gpu/drm/gma500/psb_irq.c         |   2 +-
>  drivers/gpu/drm/gma500/psb_irq.h         |   2 +-
>  drivers/gpu/drm/i915/i915_irq.c          |   4 +-
>  drivers/gpu/drm/mga/mga_drv.h            |   2 +-
>  drivers/gpu/drm/mga/mga_irq.c            |   2 +-
>  drivers/gpu/drm/msm/mdp/mdp5/mdp5_kms.c  |   2 +-
>  drivers/gpu/drm/r128/r128_drv.h          |   2 +-
>  drivers/gpu/drm/r128/r128_irq.c          |   2 +-
>  drivers/gpu/drm/radeon/radeon_drv.c      |   2 +-
>  drivers/gpu/drm/radeon/radeon_kms.c      |   2 +-
>  drivers/gpu/drm/tegra/dc.c               |   2 +-
>  drivers/gpu/drm/via/via_drv.h            |   2 +-
>  drivers/gpu/drm/via/via_irq.c            |   5 +-
>  drivers/gpu/drm/vmwgfx/vmwgfx_drv.h      |   2 +-
>  drivers/gpu/drm/vmwgfx/vmwgfx_kms.c      |   2 +-
>  include/drm/drmP.h                       |   2 +-
>  include/drm/drm_crtc.h                   |   2 +-
>  include/drm/drm_drv.h                    |   4 +-
>  include/drm/drm_vblank.h                 |  18 ++--
>  24 files changed, 130 insertions(+), 98 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
> index e0adad590ecb..860f5e194864 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
> @@ -1979,7 +1979,7 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
>  int amdgpu_suspend(struct amdgpu_device *adev);
>  int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon);
>  int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon);
> -u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe);
> +u64 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe);
>  int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe);
>  void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe);
>  long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd,
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
> index 12497a40ef92..f8c814c9c91a 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c
> @@ -922,7 +922,7 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
>   * Gets the frame count on the requested crtc (all asics).
>   * Returns frame count on success, -EINVAL on failure.
>   */
> -u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
> +u64 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
>  {
>  	struct amdgpu_device *adev = dev->dev_private;
>  	int vpos, hpos, stat;
> diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c
> index 463e4d81fb0d..f55f997c0b8f 100644
> --- a/drivers/gpu/drm/drm_vblank.c
> +++ b/drivers/gpu/drm/drm_vblank.c
> @@ -43,7 +43,7 @@
>  
>  static bool
>  drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
> -			  struct timeval *tvblank, bool in_vblank_irq);
> +			  struct timespec *tvblank, bool in_vblank_irq);
>  
>  static unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
>  
> @@ -63,8 +63,8 @@ MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
>  MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
>  
>  static void store_vblank(struct drm_device *dev, unsigned int pipe,
> -			 u32 vblank_count_inc,
> -			 struct timeval *t_vblank, u32 last)
> +			 u64 vblank_count_inc,
> +			 struct timespec *t_vblank, u64 last)
>  {
>  	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
>  
> @@ -82,13 +82,13 @@ static void store_vblank(struct drm_device *dev, unsigned int pipe,
>   * "No hw counter" fallback implementation of .get_vblank_counter() hook,
>   * if there is no useable hardware frame counter available.
>   */
> -static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
> +static u64 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	WARN_ON_ONCE(dev->max_vblank_count != 0);
>  	return 0;
>  }
>  
> -static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +static u64 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
>  		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
> @@ -114,9 +114,9 @@ static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>   */
>  static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
>  {
> -	u32 cur_vblank;
> +	u64 cur_vblank;
>  	bool rc;
> -	struct timeval t_vblank;
> +	struct timespec t_vblank;
>  	int count = DRM_TIMESTAMP_MAXRETRIES;
>  
>  	spin_lock(&dev->vblank_time_lock);
> @@ -136,7 +136,7 @@ static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe
>  	 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
>  	 */
>  	if (!rc)
> -		t_vblank = (struct timeval) {0, 0};
> +		t_vblank = (struct timespec) {0, 0};
>  
>  	/*
>  	 * +1 to make sure user will never see the same
> @@ -163,9 +163,9 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
>  				    bool in_vblank_irq)
>  {
>  	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
> -	u32 cur_vblank, diff;
> +	u64 cur_vblank, diff;
>  	bool rc;
> -	struct timeval t_vblank;
> +	struct timespec t_vblank;
>  	int count = DRM_TIMESTAMP_MAXRETRIES;
>  	int framedur_ns = vblank->framedur_ns;
>  
> @@ -190,11 +190,11 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
>  		/* 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;
> +		const struct timespec *t_old;
>  		u64 diff_ns;
>  
>  		t_old = &vblank->time;
> -		diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
> +		diff_ns = timespec_to_ns(&t_vblank) - timespec_to_ns(t_old);
>  
>  		/*
>  		 * Figure out how many vblanks we've missed based
> @@ -222,13 +222,13 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
>  	 * 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"
> +		DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%llu"
>  			      " due to pre-modeset.\n", pipe, diff);
>  		diff = 1;
>  	}
>  
>  	DRM_DEBUG_VBL("updating vblank count on crtc %u:"
> -		      " current=%u, diff=%u, hw=%u hw_last=%u\n",
> +		      " current=%llu, diff=%llu, hw=%llu hw_last=%llu\n",
>  		      pipe, vblank->count, diff, cur_vblank, vblank->last);
>  
>  	if (diff == 0) {
> @@ -243,7 +243,7 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
>  	 * for now, to mark the vblanktimestamp as invalid.
>  	 */
>  	if (!rc && in_vblank_irq)
> -		t_vblank = (struct timeval) {0, 0};
> +		t_vblank = (struct timespec) {0, 0};
>  
>  	store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
>  }
> @@ -567,10 +567,10 @@ EXPORT_SYMBOL(drm_calc_timestamping_constants);
>  bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
>  					   unsigned int pipe,
>  					   int *max_error,
> -					   struct timeval *vblank_time,
> +					   struct timespec *vblank_time,
>  					   bool in_vblank_irq)
>  {
> -	struct timeval tv_etime;
> +	struct timespec tv_etime;
>  	ktime_t stime, etime;
>  	bool vbl_status;
>  	struct drm_crtc *crtc;
> @@ -663,29 +663,29 @@ bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
>  		etime = ktime_mono_to_real(etime);
>  
>  	/* save this only for debugging purposes */
> -	tv_etime = ktime_to_timeval(etime);
> +	tv_etime = ktime_to_timespec(etime);
>  	/* Subtract time delta from raw timestamp to get final
>  	 * vblank_time timestamp for end of vblank.
>  	 */
>  	etime = ktime_sub_ns(etime, delta_ns);
> -	*vblank_time = ktime_to_timeval(etime);
> +	*vblank_time = ktime_to_timespec(etime);
>  
>  	DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
>  		      pipe, hpos, vpos,
> -		      (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
> -		      (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
> +		      (long)tv_etime.tv_sec, (long)tv_etime.tv_nsec,
> +		      (long)vblank_time->tv_sec, (long)vblank_time->tv_nsec,
>  		      duration_ns/1000, i);
>  
>  	return true;
>  }
>  EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
>  
> -static struct timeval get_drm_timestamp(void)
> +static struct timespec get_drm_timestamp(void)
>  {
>  	ktime_t now;
>  
>  	now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
> -	return ktime_to_timeval(now);
> +	return ktime_to_timespec(now);
>  }
>  
>  /**
> @@ -711,7 +711,7 @@ static struct timeval get_drm_timestamp(void)
>   */
>  static bool
>  drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
> -			  struct timeval *tvblank, bool in_vblank_irq)
> +			  struct timespec *tvblank, bool in_vblank_irq)
>  {
>  	bool ret = false;
>  
> @@ -743,7 +743,7 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
>   * Returns:
>   * The software vblank counter.
>   */
> -u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
> +u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
>  {
>  	return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
>  }
> @@ -763,15 +763,15 @@ EXPORT_SYMBOL(drm_crtc_vblank_count);
>   *
>   * This is the legacy version of drm_crtc_vblank_count_and_time().
>   */
> -static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
> -				     struct timeval *vblanktime)
> +static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
> +				     struct timespec *vblanktime)
>  {
>  	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
> -	u32 vblank_count;
> +	u64 vblank_count;
>  	unsigned int seq;
>  
>  	if (WARN_ON(pipe >= dev->num_crtcs)) {
> -		*vblanktime = (struct timeval) { 0 };
> +		*vblanktime = (struct timespec) { 0 };
>  		return 0;
>  	}
>  
> @@ -795,8 +795,8 @@ static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
>   * modesetting activity. Returns corresponding system timestamp of the time
>   * of the vblank interval that corresponds to the current vblank counter value.
>   */
> -u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
> -				   struct timeval *vblanktime)
> +u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
> +				   struct timespec *vblanktime)
>  {
>  	return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
>  					 vblanktime);
> @@ -805,11 +805,11 @@ 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)
> +		u64 seq, struct timespec *now)
>  {
>  	e->event.sequence = seq;
>  	e->event.tv_sec = now->tv_sec;
> -	e->event.tv_usec = now->tv_usec;
> +	e->event.tv_usec = now->tv_nsec / 1000;
>  
>  	trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe,
>  					 e->event.sequence);
> @@ -864,7 +864,7 @@ void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
>  	assert_spin_locked(&dev->event_lock);
>  
>  	e->pipe = pipe;
> -	e->event.sequence = drm_vblank_count(dev, pipe);
> +	e->sequence = drm_vblank_count(dev, pipe);
>  	e->event.crtc_id = crtc->base.id;
>  	list_add_tail(&e->base.link, &dev->vblank_event_list);
>  }
> @@ -885,8 +885,9 @@ void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
>  				struct drm_pending_vblank_event *e)
>  {
>  	struct drm_device *dev = crtc->dev;
> -	unsigned int seq, pipe = drm_crtc_index(crtc);
> -	struct timeval now;
> +	u64 seq;
> +	unsigned int pipe = drm_crtc_index(crtc);
> +	struct timespec now;
>  
>  	if (dev->num_crtcs > 0) {
>  		seq = drm_vblank_count_and_time(dev, pipe, &now);
> @@ -1124,9 +1125,9 @@ void drm_crtc_vblank_off(struct drm_crtc *crtc)
>  	unsigned int pipe = drm_crtc_index(crtc);
>  	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
>  	struct drm_pending_vblank_event *e, *t;
> -	struct timeval now;
> +	struct timespec now;
>  	unsigned long irqflags;
> -	unsigned int seq;
> +	u64 seq;
>  
>  	if (WARN_ON(pipe >= dev->num_crtcs))
>  		return;
> @@ -1161,8 +1162,8 @@ void drm_crtc_vblank_off(struct drm_crtc *crtc)
>  		if (e->pipe != pipe)
>  			continue;
>  		DRM_DEBUG("Sending premature vblank event on disable: "
> -			  "wanted %u, current %u\n",
> -			  e->event.sequence, seq);
> +			  "wanted %llu current %llu\n",
> +			  e->sequence, seq);
>  		list_del(&e->base.link);
>  		drm_vblank_put(dev, pipe);
>  		send_vblank_event(dev, e, seq, &now);
> @@ -1331,20 +1332,21 @@ int drm_legacy_modeset_ctl(struct drm_device *dev, void *data,
>  	return 0;
>  }
>  
> -static inline bool vblank_passed(u32 seq, u32 ref)
> +static inline bool vblank_passed(u64 seq, u64 ref)
>  {
>  	return (seq - ref) <= (1 << 23);
>  }
>  
>  static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
> +				  u64 req_seq,
>  				  union drm_wait_vblank *vblwait,
>  				  struct drm_file *file_priv)
>  {
>  	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
>  	struct drm_pending_vblank_event *e;
> -	struct timeval now;
> +	struct timespec now;
>  	unsigned long flags;
> -	unsigned int seq;
> +	u64 seq;
>  	int ret;
>  
>  	e = kzalloc(sizeof(*e), GFP_KERNEL);
> @@ -1379,21 +1381,20 @@ static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
>  
>  	seq = drm_vblank_count_and_time(dev, pipe, &now);
>  
> -	DRM_DEBUG("event on vblank count %u, current %u, crtc %u\n",
> -		  vblwait->request.sequence, seq, pipe);
> +	DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n",
> +		  req_seq, seq, pipe);
>  
> -	trace_drm_vblank_event_queued(file_priv, pipe,
> -				      vblwait->request.sequence);
> +	trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
>  
> -	e->event.sequence = vblwait->request.sequence;
> -	if (vblank_passed(seq, vblwait->request.sequence)) {
> +	e->sequence = req_seq;
> +	if (vblank_passed(seq, req_seq)) {
>  		drm_vblank_put(dev, pipe);
>  		send_vblank_event(dev, e, seq, &now);
>  		vblwait->reply.sequence = seq;
>  	} else {
>  		/* drm_handle_vblank_events will call drm_vblank_put */
>  		list_add_tail(&e->base.link, &dev->vblank_event_list);
> -		vblwait->reply.sequence = vblwait->request.sequence;
> +		vblwait->reply.sequence = req_seq;
>  	}
>  
>  	spin_unlock_irqrestore(&dev->event_lock, flags);
> @@ -1420,6 +1421,27 @@ static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
>  }
>  
>  /*
> + * Widen a 32-bit param to 64-bits.
> + *
> + * \param narrow 32-bit value (missing upper 32 bits)
> + * \param near 64-bit value that should be 'close' to near
> + *
> + * This function returns a 64-bit value using the lower 32-bits from
> + * 'narrow' and constructing the upper 32-bits so that the result is
> + * as close as possible to 'near'.
> + */
> + 
> +static u64 widen_32_to_64(u32 narrow, u64 near)
> +{
> +	u64 wide = narrow | (near & 0xffffffff00000000ULL);
> +	if ((int64_t) (wide - near) > 0x80000000LL)
> +		wide -= 0x100000000ULL;
> +	else if ((int64_t) (near - wide) > 0x80000000LL)
> +		wide += 0x100000000ULL;
> +	return wide;
> +}
> +
> +/*
>   * Wait for VBLANK.
>   *
>   * \param inode device inode.
> @@ -1439,6 +1461,7 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
>  	struct drm_vblank_crtc *vblank;
>  	union drm_wait_vblank *vblwait = data;
>  	int ret;
> +	u64 req_seq;
>  	unsigned int flags, seq, pipe, high_pipe;
>  
>  	if (!dev->irq_enabled)
> @@ -1474,12 +1497,12 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
>  	if (dev->vblank_disable_immediate &&
>  	    drm_wait_vblank_is_query(vblwait) &&
>  	    READ_ONCE(vblank->enabled)) {
> -		struct timeval now;
> +		struct timespec 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;
> +		vblwait->reply.tval_usec = now.tv_nsec / 1000;
>  		return 0;
>  	}
>  
> @@ -1492,9 +1515,11 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
>  
>  	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
>  	case _DRM_VBLANK_RELATIVE:
> -		vblwait->request.sequence += seq;
> +		req_seq = seq + vblwait->request.sequence;
>  		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
> +		break;
>  	case _DRM_VBLANK_ABSOLUTE:
> +		req_seq = widen_32_to_64(vblwait->request.sequence, seq);
>  		break;
>  	default:
>  		ret = -EINVAL;
> @@ -1502,31 +1527,31 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
>  	}
>  
>  	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
> -	    vblank_passed(seq, vblwait->request.sequence))
> -		vblwait->request.sequence = seq + 1;
> +	    vblank_passed(seq, req_seq))
> +		req_seq = seq + 1;
>  
>  	if (flags & _DRM_VBLANK_EVENT) {
>  		/* must hold on to the vblank ref until the event fires
>  		 * drm_vblank_put will be called asynchronously
>  		 */
> -		return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
> +		return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
>  	}
>  
> -	if (vblwait->request.sequence != seq) {
> -		DRM_DEBUG("waiting on vblank count %u, crtc %u\n",
> -			  vblwait->request.sequence, pipe);
> +	if (req_seq != seq) {
> +		DRM_DEBUG("waiting on vblank count %llu, crtc %u\n",
> +			  req_seq, pipe);
>  		DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
>  			    vblank_passed(drm_vblank_count(dev, pipe),
> -					  vblwait->request.sequence) ||
> +					  req_seq) ||
>  			    !READ_ONCE(vblank->enabled));
>  	}
>  
>  	if (ret != -EINTR) {
> -		struct timeval now;
> +		struct timespec 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;
> +		vblwait->reply.tval_usec = now.tv_nsec / 1000;
>  
>  		DRM_DEBUG("crtc %d returning %u to client\n",
>  			  pipe, vblwait->reply.sequence);
> @@ -1542,8 +1567,8 @@ int drm_wait_vblank(struct drm_device *dev, void *data,
>  static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
>  {
>  	struct drm_pending_vblank_event *e, *t;
> -	struct timeval now;
> -	unsigned int seq;
> +	struct timespec now;
> +	u64 seq;
>  
>  	assert_spin_locked(&dev->event_lock);
>  
> @@ -1552,11 +1577,11 @@ static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
>  	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
>  		if (e->pipe != pipe)
>  			continue;
> -		if (!vblank_passed(seq, e->event.sequence))
> +		if (!vblank_passed(seq, e->sequence))
>  			continue;
>  
> -		DRM_DEBUG("vblank event on %u, current %u\n",
> -			  e->event.sequence, seq);
> +		DRM_DEBUG("vblank event on %llu, current %llu\n",
> +			  e->sequence, seq);
>  
>  		list_del(&e->base.link);
>  		drm_vblank_put(dev, pipe);
> diff --git a/drivers/gpu/drm/exynos/exynos_drm_crtc.c b/drivers/gpu/drm/exynos/exynos_drm_crtc.c
> index d72777f6411a..110beca116f8 100644
> --- a/drivers/gpu/drm/exynos/exynos_drm_crtc.c
> +++ b/drivers/gpu/drm/exynos/exynos_drm_crtc.c
> @@ -132,7 +132,7 @@ static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
>  		exynos_crtc->ops->disable_vblank(exynos_crtc);
>  }
>  
> -static u32 exynos_drm_crtc_get_vblank_counter(struct drm_crtc *crtc)
> +static u64 exynos_drm_crtc_get_vblank_counter(struct drm_crtc *crtc)
>  {
>  	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
>  
> diff --git a/drivers/gpu/drm/gma500/psb_drv.h b/drivers/gpu/drm/gma500/psb_drv.h
> index 83667087d6e5..6e3959da8ec2 100644
> --- a/drivers/gpu/drm/gma500/psb_drv.h
> +++ b/drivers/gpu/drm/gma500/psb_drv.h
> @@ -699,7 +699,7 @@ psb_enable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask);
>  void
>  psb_disable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask);
>  
> -extern u32 psb_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
> +extern u64 psb_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
>  
>  /* framebuffer.c */
>  extern int psbfb_probed(struct drm_device *dev);
> diff --git a/drivers/gpu/drm/gma500/psb_irq.c b/drivers/gpu/drm/gma500/psb_irq.c
> index 78eb10902809..efd2124de971 100644
> --- a/drivers/gpu/drm/gma500/psb_irq.c
> +++ b/drivers/gpu/drm/gma500/psb_irq.c
> @@ -622,7 +622,7 @@ void mdfld_disable_te(struct drm_device *dev, int pipe)
>  /* Called from drm generic code, passed a 'crtc', which
>   * we use as a pipe index
>   */
> -u32 psb_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +u64 psb_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	uint32_t high_frame = PIPEAFRAMEHIGH;
>  	uint32_t low_frame = PIPEAFRAMEPIXEL;
> diff --git a/drivers/gpu/drm/gma500/psb_irq.h b/drivers/gpu/drm/gma500/psb_irq.h
> index e6a81a8c9f35..4ab8af0607a4 100644
> --- a/drivers/gpu/drm/gma500/psb_irq.h
> +++ b/drivers/gpu/drm/gma500/psb_irq.h
> @@ -40,7 +40,7 @@ void psb_irq_turn_on_dpst(struct drm_device *dev);
>  void psb_irq_turn_off_dpst(struct drm_device *dev);
>  int  psb_enable_vblank(struct drm_device *dev, unsigned int pipe);
>  void psb_disable_vblank(struct drm_device *dev, unsigned int pipe);
> -u32  psb_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
> +u64  psb_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
>  
>  int mdfld_enable_te(struct drm_device *dev, int pipe);
>  void mdfld_disable_te(struct drm_device *dev, int pipe);
> diff --git a/drivers/gpu/drm/i915/i915_irq.c b/drivers/gpu/drm/i915/i915_irq.c
> index 7b7f55a28eec..97c928c823e2 100644
> --- a/drivers/gpu/drm/i915/i915_irq.c
> +++ b/drivers/gpu/drm/i915/i915_irq.c
> @@ -715,7 +715,7 @@ static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
>  /* Called from drm generic code, passed a 'crtc', which
>   * we use as a pipe index
>   */
> -static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +static u64 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	struct drm_i915_private *dev_priv = to_i915(dev);
>  	i915_reg_t high_frame, low_frame;
> @@ -765,7 +765,7 @@ static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
>  }
>  
> -static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +static u64 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	struct drm_i915_private *dev_priv = to_i915(dev);
>  
> diff --git a/drivers/gpu/drm/mga/mga_drv.h b/drivers/gpu/drm/mga/mga_drv.h
> index 45cf363d25ad..46adff5d1fc6 100644
> --- a/drivers/gpu/drm/mga/mga_drv.h
> +++ b/drivers/gpu/drm/mga/mga_drv.h
> @@ -185,7 +185,7 @@ extern int mga_warp_init(drm_mga_private_t *dev_priv);
>  				/* mga_irq.c */
>  extern int mga_enable_vblank(struct drm_device *dev, unsigned int pipe);
>  extern void mga_disable_vblank(struct drm_device *dev, unsigned int pipe);
> -extern u32 mga_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
> +extern u64 mga_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
>  extern int mga_driver_fence_wait(struct drm_device *dev, unsigned int *sequence);
>  extern int mga_driver_vblank_wait(struct drm_device *dev, unsigned int *sequence);
>  extern irqreturn_t mga_driver_irq_handler(int irq, void *arg);
> diff --git a/drivers/gpu/drm/mga/mga_irq.c b/drivers/gpu/drm/mga/mga_irq.c
> index 693ba708cfed..a361db778f6a 100644
> --- a/drivers/gpu/drm/mga/mga_irq.c
> +++ b/drivers/gpu/drm/mga/mga_irq.c
> @@ -35,7 +35,7 @@
>  #include <drm/mga_drm.h>
>  #include "mga_drv.h"
>  
> -u32 mga_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +u64 mga_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	const drm_mga_private_t *const dev_priv =
>  		(drm_mga_private_t *) dev->dev_private;
> diff --git a/drivers/gpu/drm/msm/mdp/mdp5/mdp5_kms.c b/drivers/gpu/drm/msm/mdp/mdp5/mdp5_kms.c
> index e2b3346ead48..678f2c03a93a 100644
> --- a/drivers/gpu/drm/msm/mdp/mdp5/mdp5_kms.c
> +++ b/drivers/gpu/drm/msm/mdp/mdp5/mdp5_kms.c
> @@ -587,7 +587,7 @@ static bool mdp5_get_scanoutpos(struct drm_device *dev, unsigned int pipe,
>  	return true;
>  }
>  
> -static u32 mdp5_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +static u64 mdp5_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	struct msm_drm_private *priv = dev->dev_private;
>  	struct drm_crtc *crtc;
> diff --git a/drivers/gpu/drm/r128/r128_drv.h b/drivers/gpu/drm/r128/r128_drv.h
> index 09143b840482..3479198774dc 100644
> --- a/drivers/gpu/drm/r128/r128_drv.h
> +++ b/drivers/gpu/drm/r128/r128_drv.h
> @@ -156,7 +156,7 @@ extern int r128_do_cleanup_cce(struct drm_device *dev);
>  
>  extern int r128_enable_vblank(struct drm_device *dev, unsigned int pipe);
>  extern void r128_disable_vblank(struct drm_device *dev, unsigned int pipe);
> -extern u32 r128_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
> +extern u64 r128_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
>  extern irqreturn_t r128_driver_irq_handler(int irq, void *arg);
>  extern void r128_driver_irq_preinstall(struct drm_device *dev);
>  extern int r128_driver_irq_postinstall(struct drm_device *dev);
> diff --git a/drivers/gpu/drm/r128/r128_irq.c b/drivers/gpu/drm/r128/r128_irq.c
> index 9730f4918944..141d4dfc30b1 100644
> --- a/drivers/gpu/drm/r128/r128_irq.c
> +++ b/drivers/gpu/drm/r128/r128_irq.c
> @@ -34,7 +34,7 @@
>  #include <drm/r128_drm.h>
>  #include "r128_drv.h"
>  
> -u32 r128_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +u64 r128_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	const drm_r128_private_t *dev_priv = dev->dev_private;
>  
> diff --git a/drivers/gpu/drm/radeon/radeon_drv.c b/drivers/gpu/drm/radeon/radeon_drv.c
> index b23c771f4216..4e2b3fa4293a 100644
> --- a/drivers/gpu/drm/radeon/radeon_drv.c
> +++ b/drivers/gpu/drm/radeon/radeon_drv.c
> @@ -112,7 +112,7 @@ void radeon_driver_postclose_kms(struct drm_device *dev,
>  int radeon_suspend_kms(struct drm_device *dev, bool suspend,
>  		       bool fbcon, bool freeze);
>  int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon);
> -u32 radeon_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe);
> +u64 radeon_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe);
>  int radeon_enable_vblank_kms(struct drm_device *dev, unsigned int pipe);
>  void radeon_disable_vblank_kms(struct drm_device *dev, unsigned int pipe);
>  void radeon_driver_irq_preinstall_kms(struct drm_device *dev);
> diff --git a/drivers/gpu/drm/radeon/radeon_kms.c b/drivers/gpu/drm/radeon/radeon_kms.c
> index dfee8f7d94ae..bf6c3bad36c7 100644
> --- a/drivers/gpu/drm/radeon/radeon_kms.c
> +++ b/drivers/gpu/drm/radeon/radeon_kms.c
> @@ -772,7 +772,7 @@ void radeon_driver_postclose_kms(struct drm_device *dev,
>   * Gets the frame count on the requested crtc (all asics).
>   * Returns frame count on success, -EINVAL on failure.
>   */
> -u32 radeon_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
> +u64 radeon_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
>  {
>  	int vpos, hpos, stat;
>  	u32 count;
> diff --git a/drivers/gpu/drm/tegra/dc.c b/drivers/gpu/drm/tegra/dc.c
> index 95b373f739f2..9f060ce156b4 100644
> --- a/drivers/gpu/drm/tegra/dc.c
> +++ b/drivers/gpu/drm/tegra/dc.c
> @@ -909,7 +909,7 @@ static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
>  	return 0;
>  }
>  
> -static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
> +static u64 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
>  {
>  	struct tegra_dc *dc = to_tegra_dc(crtc);
>  
> diff --git a/drivers/gpu/drm/via/via_drv.h b/drivers/gpu/drm/via/via_drv.h
> index 9873942ca8f4..019769a54a70 100644
> --- a/drivers/gpu/drm/via/via_drv.h
> +++ b/drivers/gpu/drm/via/via_drv.h
> @@ -140,7 +140,7 @@ extern int via_init_context(struct drm_device *dev, int context);
>  extern int via_final_context(struct drm_device *dev, int context);
>  
>  extern int via_do_cleanup_map(struct drm_device *dev);
> -extern u32 via_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
> +extern u64 via_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
>  extern int via_enable_vblank(struct drm_device *dev, unsigned int pipe);
>  extern void via_disable_vblank(struct drm_device *dev, unsigned int pipe);
>  
> diff --git a/drivers/gpu/drm/via/via_irq.c b/drivers/gpu/drm/via/via_irq.c
> index ea8172c747a2..a151c72d148a 100644
> --- a/drivers/gpu/drm/via/via_irq.c
> +++ b/drivers/gpu/drm/via/via_irq.c
> @@ -95,7 +95,7 @@ static unsigned time_diff(struct timeval *now, struct timeval *then)
>  		1000000 - (then->tv_usec - now->tv_usec);
>  }
>  
> -u32 via_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +u64 via_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	drm_via_private_t *dev_priv = dev->dev_private;
>  
> @@ -317,6 +317,9 @@ int via_driver_irq_postinstall(struct drm_device *dev)
>  	if (!dev_priv)
>  		return -EINVAL;
>  
> +	if (dev->driver->get_vblank_counter)
> +		dev->max_vblank_count = 0xffffffff;
> +
>  	status = VIA_READ(VIA_REG_INTERRUPT);
>  	VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_GLOBAL
>  		  | dev_priv->irq_enable_mask);
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
> index 130d51c5ec6a..adc9d2ae37a4 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
> @@ -918,7 +918,7 @@ void vmw_kms_idle_workqueues(struct vmw_master *vmaster);
>  bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
>  				uint32_t pitch,
>  				uint32_t height);
> -u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
> +u64 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
>  int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe);
>  void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe);
>  int vmw_kms_present(struct vmw_private *dev_priv,
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
> index a8876b070168..135f5c3dbb6c 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
> @@ -1944,7 +1944,7 @@ bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
>  /**
>   * Function called by DRM code called with vbl_lock held.
>   */
> -u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
> +u64 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
>  {
>  	return 0;
>  }
> diff --git a/include/drm/drmP.h b/include/drm/drmP.h
> index 39df16af7a4a..e50cf152f565 100644
> --- a/include/drm/drmP.h
> +++ b/include/drm/drmP.h
> @@ -403,7 +403,7 @@ struct drm_device {
>  	spinlock_t vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
>  	spinlock_t vbl_lock;
>  
> -	u32 max_vblank_count;           /**< size of vblank counter register */
> +	u64 max_vblank_count;           /**< size of vblank counter register */
>  
>  	/**
>  	 * List of events
> diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h
> index 629a5fe075b3..e866f0007d8a 100644
> --- a/include/drm/drm_crtc.h
> +++ b/include/drm/drm_crtc.h
> @@ -689,7 +689,7 @@ struct drm_crtc_funcs {
>  	 *
>  	 * Raw vblank counter value.
>  	 */
> -	u32 (*get_vblank_counter)(struct drm_crtc *crtc);
> +	u64 (*get_vblank_counter)(struct drm_crtc *crtc);
>  
>  	/**
>  	 * @enable_vblank:
> diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h
> index d855f9ae41a8..e575802fbe4c 100644
> --- a/include/drm/drm_drv.h
> +++ b/include/drm/drm_drv.h
> @@ -196,7 +196,7 @@ struct drm_driver {
>  	 *
>  	 * Raw vblank counter value.
>  	 */
> -	u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
> +	u64 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
>  
>  	/**
>  	 * @enable_vblank:
> @@ -325,7 +325,7 @@ struct drm_driver {
>  	 */
>  	bool (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
>  				     int *max_error,
> -				     struct timeval *vblank_time,
> +				     struct timespec *vblank_time,
>  				     bool in_vblank_irq);
>  
>  	/**
> diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h
> index 4cde47332dfa..68e99177fff3 100644
> --- a/include/drm/drm_vblank.h
> +++ b/include/drm/drm_vblank.h
> @@ -48,6 +48,10 @@ struct drm_pending_vblank_event {
>  	 */
>  	unsigned int pipe;
>  	/**
> +	 * @sequence: frame event should be triggered at
> +	 */
> +	u64 sequence;
> +	/**
>  	 * @event: Actual event which will be sent to userspace.
>  	 */
>  	struct drm_event_vblank event;
> @@ -88,11 +92,11 @@ struct drm_vblank_crtc {
>  	/**
>  	 * @count: Current software vblank counter.
>  	 */
> -	u32 count;
> +	u64 count;
>  	/**
>  	 * @time: Vblank timestamp corresponding to @count.
>  	 */
> -	struct timeval time;
> +	struct timespec time;
>  
>  	/**
>  	 * @refcount: Number of users/waiters of the vblank interrupt. Only when
> @@ -103,7 +107,7 @@ struct drm_vblank_crtc {
>  	/**
>  	 * @last: Protected by &drm_device.vbl_lock, used for wraparound handling.
>  	 */
> -	u32 last;
> +	u64 last;
>  	/**
>  	 * @inmodeset: Tracks whether the vblank is disabled due to a modeset.
>  	 * For legacy driver bit 2 additionally tracks whether an additional
> @@ -152,9 +156,9 @@ struct drm_vblank_crtc {
>  };
>  
>  int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
> -u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
> -u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
> -				   struct timeval *vblanktime);
> +u64 drm_crtc_vblank_count(struct drm_crtc *crtc);
> +u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
> +				   struct timespec *vblanktime);
>  void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
>  			       struct drm_pending_vblank_event *e);
>  void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
> @@ -173,7 +177,7 @@ u32 drm_accurate_vblank_count(struct drm_crtc *crtc);
>  
>  bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
>  					   unsigned int pipe, int *max_error,
> -					   struct timeval *vblank_time,
> +					   struct timespec *vblank_time,
>  					   bool in_vblank_irq);
>  void drm_calc_timestamping_constants(struct drm_crtc *crtc,
>  				     const struct drm_display_mode *mode);
> -- 
> 2.11.0
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch


More information about the dri-devel mailing list