diff mbox series

[21/41] drm/i915: Wrap cmpxchg64 with try_cmpxchg64() helper

Message ID 20210125140136.10494-21-chris@chris-wilson.co.uk (mailing list archive)
State New, archived
Headers show
Series [01/41] drm/i915/selftests: Check for engine-reset errors in the middle of workarounds | expand

Commit Message

Chris Wilson Jan. 25, 2021, 2:01 p.m. UTC
Wrap cmpxchg64 with a try_cmpxchg()-esque helper. Hiding the old-value
dance in the helper allows for cleaner code.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
---
 drivers/gpu/drm/i915/i915_utils.h | 32 +++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Tvrtko Ursulin Jan. 27, 2021, 3:28 p.m. UTC | #1
On 25/01/2021 14:01, Chris Wilson wrote:
> Wrap cmpxchg64 with a try_cmpxchg()-esque helper. Hiding the old-value
> dance in the helper allows for cleaner code.
> 
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> ---
>   drivers/gpu/drm/i915/i915_utils.h | 32 +++++++++++++++++++++++++++++++
>   1 file changed, 32 insertions(+)
> 
> diff --git a/drivers/gpu/drm/i915/i915_utils.h b/drivers/gpu/drm/i915/i915_utils.h
> index abd4dcd9f79c..95ead6bb1ba6 100644
> --- a/drivers/gpu/drm/i915/i915_utils.h
> +++ b/drivers/gpu/drm/i915/i915_utils.h
> @@ -461,4 +461,36 @@ static inline bool timer_expired(const struct timer_list *t)
>    */
>   #define IS_ACTIVE(config) ((config) != 0)
>   
> +#ifndef try_cmpxchg64
> +#if IS_ENABLED(CONFIG_64BIT)
> +#define try_cmpxchg64(_ptr, _pold, _new) try_cmpxchg(_ptr, _pold, _new)
> +#else
> +#define try_cmpxchg64(_ptr, _pold, _new)				\
> +({									\
> +	__typeof__(_ptr) _old = (__typeof__(_ptr))(_pold);		\
> +	__typeof__(*(_ptr)) __old = *_old;				\
> +	__typeof__(*(_ptr)) __cur = cmpxchg64(_ptr, __old, _new);	\
> +	bool success = __cur == __old;					\
> +	if (unlikely(!success))						\
> +		*_old = __cur;						\
> +	likely(success);						\
> +})
> +#endif
> +#endif
> +
> +#ifndef xchg64
> +#if IS_ENABLED(CONFIG_64BIT)
> +#define xchg64(_ptr, _new) xchg(_ptr, _new)
> +#else
> +#define xchg64(_ptr, _new)						\
> +({									\
> +	__typeof__(_ptr) __ptr = (_ptr);				\
> +	__typeof__(*(_ptr)) __old = *__ptr;				\
> +	while (!try_cmpxchg64(__ptr, &__old, (_new)))			\
> +		;							\
> +	__old;								\
> +})
> +#endif
> +#endif
> +
>   #endif /* !__I915_UTILS_H */
> 

Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Regards,

Tvrtko
diff mbox series

Patch

diff --git a/drivers/gpu/drm/i915/i915_utils.h b/drivers/gpu/drm/i915/i915_utils.h
index abd4dcd9f79c..95ead6bb1ba6 100644
--- a/drivers/gpu/drm/i915/i915_utils.h
+++ b/drivers/gpu/drm/i915/i915_utils.h
@@ -461,4 +461,36 @@  static inline bool timer_expired(const struct timer_list *t)
  */
 #define IS_ACTIVE(config) ((config) != 0)
 
+#ifndef try_cmpxchg64
+#if IS_ENABLED(CONFIG_64BIT)
+#define try_cmpxchg64(_ptr, _pold, _new) try_cmpxchg(_ptr, _pold, _new)
+#else
+#define try_cmpxchg64(_ptr, _pold, _new)				\
+({									\
+	__typeof__(_ptr) _old = (__typeof__(_ptr))(_pold);		\
+	__typeof__(*(_ptr)) __old = *_old;				\
+	__typeof__(*(_ptr)) __cur = cmpxchg64(_ptr, __old, _new);	\
+	bool success = __cur == __old;					\
+	if (unlikely(!success))						\
+		*_old = __cur;						\
+	likely(success);						\
+})
+#endif
+#endif
+
+#ifndef xchg64
+#if IS_ENABLED(CONFIG_64BIT)
+#define xchg64(_ptr, _new) xchg(_ptr, _new)
+#else
+#define xchg64(_ptr, _new)						\
+({									\
+	__typeof__(_ptr) __ptr = (_ptr);				\
+	__typeof__(*(_ptr)) __old = *__ptr;				\
+	while (!try_cmpxchg64(__ptr, &__old, (_new)))			\
+		;							\
+	__old;								\
+})
+#endif
+#endif
+
 #endif /* !__I915_UTILS_H */