Skip to contentSkip to conversation

drm/i915: fold wait_for_atomic_us into wait_for_atomic

PATCH In review Daniel Vetter 3 participants 7 messages Mar 27, 2013 – Mar 28, 2013

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

PATCH 1/2 drm/i915: fold wait_for_atomic_us into wait_for_atomic

Since

commit bcf9dcc1e6269fac674e41f25d007ff75f76e840

Author: Chris Wilson <[email protected]>
Date:   Sun Jul 15 09:42:38 2012 +0100

drm/i915: Workaround hang with BSD and forcewake on SandyBridge

and

commit 0cc2764cc4a4bd73df55f8893c871778cf7ddd0f

Author: Ben Widawsky <[email protected]>
Date:   Sat Sep 1 22:59:48 2012 -0700

drm/i915: use cpu_relax() in wait_for_atomic

these two macros are essentially the same, so unify them. We keep the _us version since it's a nice documentation for smaller timeouts.

---
 drivers/gpu/drm/i915/intel_drv.h | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/intel_drv.h +2 -13
index 54bc2ea..c8c1979 100644
@@ -50,21 +50,10 @@
 	ret__;								\
 })
 
-#define wait_for_atomic_us(COND, US) ({ \
-	unsigned long timeout__ = jiffies + usecs_to_jiffies(US);	\
-	int ret__ = 0;							\
-	while (!(COND)) {						\
-		if (time_after(jiffies, timeout__)) {			\
-			ret__ = -ETIMEDOUT;				\
-			break;						\
-		}							\
-		cpu_relax();						\
-	}								\
-	ret__;								\
-})
-
 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
 #define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0)
+#define wait_for_atomic_us(COND, US) _wait_for((COND), \
+					       usecs_to_jiffies((US)), 0)
 
 #define KHz(x) (1000*x)
 #define MHz(x) KHz(1000*x)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 2/2 drm/i915: fix up _wait_for macro

As Thomas Gleixner spotted, it's rather horrible racy:
- We can miss almost a full tick, so need to compensate by 1 jiffy.
- We need to re-check the condition when having timed-out, since a the last check could have been before the timeout expired. E.g. when we've been preempted or a long irq happened.

---
 drivers/gpu/drm/i915/intel_drv.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/intel_drv.h +11 -2
index c8c1979..9dcae4e 100644
@@ -33,12 +33,21 @@
 #include <drm/drm_fb_helper.h>
 #include <drm/drm_dp_helper.h>
 
+/**
+ * _wait_for - magic (register) wait macro
+ *
+ * Does the right thing for modeset paths when run under kdgb or similar atomic
+ * contexts. Note that it's important that we check the condition again after
+ * having timed out, since the timeout could be due to preemption or similar and
+ * we've never had a chance to check the condition before the timeout.
+ */
 #define _wait_for(COND, MS, W) ({ \
-	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS);	\
+	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1;	\
 	int ret__ = 0;							\
 	while (!(COND)) {						\
 		if (time_after(jiffies, timeout__)) {			\
-			ret__ = -ETIMEDOUT;				\
+			if (!(COND))					\
+				ret__ = -ETIMEDOUT;			\
 			break;						\
 		}							\
 		if (W && drm_can_sleep())  {				\
Signature · 1 line
1.7.11.7
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm/i915: fix up _wait_for macro

Quoted 2 lines · Daniel Vetter

As Thomas Gleixner spotted, it's rather horrible racy:
- We can miss almost a full tick, so need to compensate by 1 jiffy.

I have a feeling this is a rather common pattern, so I wonder if [mu]secs_to_jiffies() should do the +1 already for everyone. Or maybe there should be some other macros for specifying timeouts that would do the +1.

Quoted 8 lines

- We need to re-check the condition when having timed-out, since a the last check could have been before the timeout expired. E.g. when we've been preempted or a long irq happened.

Quoted 39 lines
---
 drivers/gpu/drm/i915/intel_drv.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/intel_drv.h +11 -2
index c8c1979..9dcae4e 100644
@@ -33,12 +33,21 @@
 #include <drm/drm_fb_helper.h>
 #include <drm/drm_dp_helper.h>
 
+/**
+ * _wait_for - magic (register) wait macro
+ *
+ * Does the right thing for modeset paths when run under kdgb or similar atomic
+ * contexts. Note that it's important that we check the condition again after
+ * having timed out, since the timeout could be due to preemption or similar and
+ * we've never had a chance to check the condition before the timeout.
+ */
 #define _wait_for(COND, MS, W) ({ \
-	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS);	\
+	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1;	\
 	int ret__ = 0;							\
 	while (!(COND)) {						\
 		if (time_after(jiffies, timeout__)) {			\
-			ret__ = -ETIMEDOUT;				\
+			if (!(COND))					\
+				ret__ = -ETIMEDOUT;			\
 			break;						\
 		}							\
 		if (W && drm_can_sleep())  {				\
Signature · 6 lines
1.7.11.7

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 2 lines
Ville Syrjälä
Intel OTC
#3 lore raw

Re: [PATCH 2/2] drm/i915: fix up _wait_for macro

Quoted 13 lines · Ville Syrjälä
Quoted 2 lines · Daniel Vetter

As Thomas Gleixner spotted, it's rather horrible racy:
- We can miss almost a full tick, so need to compensate by 1 jiffy.

I have a feeling this is a rather common pattern, so I wonder if [mu]secs_to_jiffies() should do the +1 already for everyone. Or maybe there should be some other macros for specifying timeouts that would do the +1.

Quoted 4 lines

- We need to re-check the condition when having timed-out, since a the last check could have been before the timeout expired. E.g. when we've been preempted or a long irq happened.

Quoted 6 lines
Quoted 4 lines

Merged both patches, thanks for the review.
-Daniel

Quoted 44 lines
Quoted 40 lines
---
 drivers/gpu/drm/i915/intel_drv.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/intel_drv.h +11 -2
index c8c1979..9dcae4e 100644
@@ -33,12 +33,21 @@
 #include <drm/drm_fb_helper.h>
 #include <drm/drm_dp_helper.h>
 
+/**
+ * _wait_for - magic (register) wait macro
+ *
+ * Does the right thing for modeset paths when run under kdgb or similar atomic
+ * contexts. Note that it's important that we check the condition again after
+ * having timed out, since the timeout could be due to preemption or similar and
+ * we've never had a chance to check the condition before the timeout.
+ */
 #define _wait_for(COND, MS, W) ({ \
-	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS);	\
+	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1;	\
 	int ret__ = 0;							\
 	while (!(COND)) {						\
 		if (time_after(jiffies, timeout__)) {			\
-			ret__ = -ETIMEDOUT;				\
+			if (!(COND))					\
+				ret__ = -ETIMEDOUT;			\
 			break;						\
 		}							\
 		if (W && drm_can_sleep())  {				\
Signature · 6 lines
1.7.11.7

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 2 lines
Ville Syrjälä
Intel OTC
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#4 lore raw
In reply to Daniel Vetter
Quoted 40 lines · Daniel Vetter

Since

commit bcf9dcc1e6269fac674e41f25d007ff75f76e840

Author: Chris Wilson <[email protected]>
Date:   Sun Jul 15 09:42:38 2012 +0100

drm/i915: Workaround hang with BSD and forcewake on SandyBridge

and

commit 0cc2764cc4a4bd73df55f8893c871778cf7ddd0f

Author: Ben Widawsky <[email protected]>
Date:   Sat Sep 1 22:59:48 2012 -0700

drm/i915: use cpu_relax() in wait_for_atomic

these two macros are essentially the same, so unify them. We keep the _us version since it's a nice documentation for smaller timeouts.

---
 drivers/gpu/drm/i915/intel_drv.h | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/intel_drv.h +0 -7
index 54bc2ea..c8c1979 100644
@@ -50,21 +50,10 @@
 	ret__;								\
 })
 
-#define wait_for_atomic_us(COND, US) ({ \
-	unsigned long timeout__ = jiffies + usecs_to_jiffies(US);	\
-	int ret__ = 0;							\
-	while (!(COND)) {						\
-		if (time_after(jiffies, timeout__)) {			\
-			ret__ = -ETIMEDOUT;				\
-			break;						\

drivers/gpu/drm/i915/intel_drv.h line 56

Quoted 10 lines
drivers/gpu/drm/i915/intel_drv.h +2 -6
-		}							\
-		cpu_relax();						\
-	}								\
-	ret__;								\
-})
-
 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
 #define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0)
+#define wait_for_atomic_us(COND, US) _wait_for((COND), \
+					       usecs_to_jiffies((US)), 0)

usecs_to_jiffies() is wrong here. DIV_ROUND_UP((US), 1000) maybe, or change _wait_for() to take the timeout in jiffies.

Quoted 10 lines
drivers/gpu/drm/i915/intel_drv.h +0 -0
 #define KHz(x) (1000*x)
 #define MHz(x) KHz(1000*x)
Signature · 6 lines
1.7.11.7

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 2 lines
Ville Syrjälä
Intel OTC
#5 lore raw

PATCH 1/1 drm/i915: fold wait_for_atomic_us into wait_for_atomic

Since

commit bcf9dcc1e6269fac674e41f25d007ff75f76e840

Author: Chris Wilson <[email protected]>
Date:   Sun Jul 15 09:42:38 2012 +0100

drm/i915: Workaround hang with BSD and forcewake on SandyBridge

and

commit 0cc2764cc4a4bd73df55f8893c871778cf7ddd0f

Author: Ben Widawsky <[email protected]>
Date:   Sat Sep 1 22:59:48 2012 -0700

drm/i915: use cpu_relax() in wait_for_atomic

these two macros are essentially the same, so unify them. We keep the _us version since it's a nice documentation for smaller timeouts.

v2: Fixup time unit conversion, _wait_for takes ms (Ville).

Cc Jack Winter <[email protected]>
Cc Ville Syrjälä <[email protected]>
---
 drivers/gpu/drm/i915/intel_drv.h | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/intel_drv.h +2 -13
index 54bc2ea..8e76f52f 100644
@@ -50,21 +50,10 @@
 	ret__;								\
 })
 
-#define wait_for_atomic_us(COND, US) ({ \
-	unsigned long timeout__ = jiffies + usecs_to_jiffies(US);	\
-	int ret__ = 0;							\
-	while (!(COND)) {						\
-		if (time_after(jiffies, timeout__)) {			\
-			ret__ = -ETIMEDOUT;				\
-			break;						\
-		}							\
-		cpu_relax();						\
-	}								\
-	ret__;								\
-})
-
 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
 #define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0)
+#define wait_for_atomic_us(COND, US) _wait_for((COND), \
+					       DIV_ROUND_UP((US), 1000), 0)
 
 #define KHz(x) (1000*x)
 #define MHz(x) KHz(1000*x)
Signature · 6 lines
1.7.11.7

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by on v1 1/1
#6 lore raw
Quoted 16 lines · Daniel Vetter

Since

commit bcf9dcc1e6269fac674e41f25d007ff75f76e840

Author: Chris Wilson <[email protected]>
Date:   Sun Jul 15 09:42:38 2012 +0100

drm/i915: Workaround hang with BSD and forcewake on SandyBridge

and

commit 0cc2764cc4a4bd73df55f8893c871778cf7ddd0f

Author: Ben Widawsky <[email protected]>
Date:   Sat Sep 1 22:59:48 2012 -0700

drm/i915: use cpu_relax() in wait_for_atomic

Quoted 8 lines

these two macros are essentially the same, so unify them. We keep the _us version since it's a nice documentation for smaller timeouts.

v2: Fixup time unit conversion, _wait_for takes ms (Ville).

Cc Jack Winter <[email protected]>
Cc Ville Syrjälä <[email protected]>
Quoted 34 lines
---
 drivers/gpu/drm/i915/intel_drv.h | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/intel_drv.h +2 -13
index 54bc2ea..8e76f52f 100644
@@ -50,21 +50,10 @@
 	ret__;								\
 })
 
-#define wait_for_atomic_us(COND, US) ({ \
-	unsigned long timeout__ = jiffies + usecs_to_jiffies(US);	\
-	int ret__ = 0;							\
-	while (!(COND)) {						\
-		if (time_after(jiffies, timeout__)) {			\
-			ret__ = -ETIMEDOUT;				\
-			break;						\
-		}							\
-		cpu_relax();						\
-	}								\
-	ret__;								\
-})
-
 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
 #define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0)
+#define wait_for_atomic_us(COND, US) _wait_for((COND), \
+					       DIV_ROUND_UP((US), 1000), 0)
 
 #define KHz(x) (1000*x)
 #define MHz(x) KHz(1000*x)
Signature · 1 line
1.7.11.7
Signature · 2 lines
Ville Syrjälä
Intel OTC