Skip to contentSkip to conversation

drm: Use raw monotonic clock

PATCH In review Abhilash Jindal 2 participants 2 messages Aug 11, 2015 – Aug 12, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: Use raw monotonic clock

Wall time obtained from do_gettimeofday is susceptible to sudden jumps due to user setting the time or due to NTP.

Raw monotonic time is constantly increasing time and isn't affected by NTP adjustments better suited for comparing two timestamps in short intervals.

---
 drivers/gpu/drm/via/via_drv.h |    2 +-
 drivers/gpu/drm/via/via_irq.c |   14 +++-----------
 2 files changed, 4 insertions(+), 12 deletions(-)
drivers/gpu/drm/via/via_drv.h +1 -1
index ef8c500..7b1b4f2 100644
@@ -74,7 +74,7 @@ typedef struct drm_via_private {
 	volatile uint32_t *last_pause_ptr;
 	volatile uint32_t *hw_addr_ptr;
 	drm_via_ring_buffer_t ring;
-	struct timeval last_vblank;
+	ktime_t last_vblank;
 	int last_vblank_valid;
 	unsigned usec_per_vblank;
 	atomic_t vbl_received;
drivers/gpu/drm/via/via_irq.c +3 -11
index 1319433..5a78117 100644
@@ -88,13 +88,6 @@ static int via_num_unichrome = ARRAY_SIZE(via_unichrome_irqs);
 static int via_irqmap_unichrome[] = {-1, -1, -1, 0, -1, 1};
 
 
-static unsigned time_diff(struct timeval *now, struct timeval *then)
-{
-	return (now->tv_usec >= then->tv_usec) ?
-		now->tv_usec - then->tv_usec :
-		1000000 - (then->tv_usec - now->tv_usec);
-}
-
 u32 via_get_vblank_counter(struct drm_device *dev, int crtc)
 {
 	drm_via_private_t *dev_priv = dev->dev_private;
@@ -110,7 +103,7 @@ irqreturn_t via_driver_irq_handler(int irq, void *arg)
 	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
 	u32 status;
 	int handled = 0;
-	struct timeval cur_vblank;
+	ktime_t cur_vblank;
 	drm_via_irq_t *cur_irq = dev_priv->via_irqs;
 	int i;
 
@@ -118,11 +111,10 @@ irqreturn_t via_driver_irq_handler(int irq, void *arg)
 	if (status & VIA_IRQ_VBLANK_PENDING) {
 		atomic_inc(&dev_priv->vbl_received);
 		if (!(atomic_read(&dev_priv->vbl_received) & 0x0F)) {
-			do_gettimeofday(&cur_vblank);
+			cur_vblank = ktime_get_raw();
 			if (dev_priv->last_vblank_valid) {
 				dev_priv->usec_per_vblank =
-					time_diff(&cur_vblank,
-						  &dev_priv->last_vblank) >> 4;
+				ktime_to_us(ktime_sub(cur_vblank, dev_priv->last_vblank)) >> 4;
 			}
 			dev_priv->last_vblank = cur_vblank;
 			dev_priv->last_vblank_valid = 1;
Signature · 6 lines
1.7.9.5

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw
Quoted 7 lines · Abhilash Jindal

Wall time obtained from do_gettimeofday is susceptible to sudden jumps due to user setting the time or due to NTP.

Raw monotonic time is constantly increasing time and isn't affected by NTP adjustments better suited for comparing two timestamps in short intervals.

Do you actually have a via card supported by this driver, including the corresponding userspace stack? Note that the userspace opengl driver for this has been nuked in upstream mesa years ago ...
-Daniel

Quoted 66 lines
---
 drivers/gpu/drm/via/via_drv.h |    2 +-
 drivers/gpu/drm/via/via_irq.c |   14 +++-----------
 2 files changed, 4 insertions(+), 12 deletions(-)
drivers/gpu/drm/via/via_drv.h +1 -1
index ef8c500..7b1b4f2 100644
@@ -74,7 +74,7 @@ typedef struct drm_via_private {
 	volatile uint32_t *last_pause_ptr;
 	volatile uint32_t *hw_addr_ptr;
 	drm_via_ring_buffer_t ring;
-	struct timeval last_vblank;
+	ktime_t last_vblank;
 	int last_vblank_valid;
 	unsigned usec_per_vblank;
 	atomic_t vbl_received;
drivers/gpu/drm/via/via_irq.c +3 -11
index 1319433..5a78117 100644
@@ -88,13 +88,6 @@ static int via_num_unichrome = ARRAY_SIZE(via_unichrome_irqs);
 static int via_irqmap_unichrome[] = {-1, -1, -1, 0, -1, 1};
 
 
-static unsigned time_diff(struct timeval *now, struct timeval *then)
-{
-	return (now->tv_usec >= then->tv_usec) ?
-		now->tv_usec - then->tv_usec :
-		1000000 - (then->tv_usec - now->tv_usec);
-}
-
 u32 via_get_vblank_counter(struct drm_device *dev, int crtc)
 {
 	drm_via_private_t *dev_priv = dev->dev_private;
@@ -110,7 +103,7 @@ irqreturn_t via_driver_irq_handler(int irq, void *arg)
 	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
 	u32 status;
 	int handled = 0;
-	struct timeval cur_vblank;
+	ktime_t cur_vblank;
 	drm_via_irq_t *cur_irq = dev_priv->via_irqs;
 	int i;
 
@@ -118,11 +111,10 @@ irqreturn_t via_driver_irq_handler(int irq, void *arg)
 	if (status & VIA_IRQ_VBLANK_PENDING) {
 		atomic_inc(&dev_priv->vbl_received);
 		if (!(atomic_read(&dev_priv->vbl_received) & 0x0F)) {
-			do_gettimeofday(&cur_vblank);
+			cur_vblank = ktime_get_raw();
 			if (dev_priv->last_vblank_valid) {
 				dev_priv->usec_per_vblank =
-					time_diff(&cur_vblank,
-						  &dev_priv->last_vblank) >> 4;
+				ktime_to_us(ktime_sub(cur_vblank, dev_priv->last_vblank)) >> 4;
 			}
 			dev_priv->last_vblank = cur_vblank;
 			dev_priv->last_vblank_valid = 1;
Signature · 6 lines
1.7.9.5

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel