forked from Mirrors/opensbi
lib: Support atomic swap instructions
If compiler supports riscv atomic instructions, we should use them instead of legacy gcc built-in macros __sync_lock_test_and_set in atomic exchange functions. Signed-off-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
This commit is contained in:
@@ -50,6 +50,39 @@ long atomic_sub_return(atomic_t *atom, long value)
|
|||||||
return ret - value;
|
return ret - value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define __axchg(ptr, new, size) \
|
||||||
|
({ \
|
||||||
|
__typeof__(ptr) __ptr = (ptr); \
|
||||||
|
__typeof__(new) __new = (new); \
|
||||||
|
__typeof__(*(ptr)) __ret; \
|
||||||
|
switch (size) { \
|
||||||
|
case 4: \
|
||||||
|
__asm__ __volatile__ ( \
|
||||||
|
" amoswap.w.aqrl %0, %2, %1\n" \
|
||||||
|
: "=r" (__ret), "+A" (*__ptr) \
|
||||||
|
: "r" (__new) \
|
||||||
|
: "memory"); \
|
||||||
|
break; \
|
||||||
|
case 8: \
|
||||||
|
__asm__ __volatile__ ( \
|
||||||
|
" amoswap.d.aqrl %0, %2, %1\n" \
|
||||||
|
: "=r" (__ret), "+A" (*__ptr) \
|
||||||
|
: "r" (__new) \
|
||||||
|
: "memory"); \
|
||||||
|
break; \
|
||||||
|
default: \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
__ret; \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define axchg(ptr, x) \
|
||||||
|
({ \
|
||||||
|
__typeof__(*(ptr)) _x_ = (x); \
|
||||||
|
(__typeof__(*(ptr))) __xchg((ptr), _x_, sizeof(*(ptr))); \
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
#define __xchg(ptr, new, size) \
|
#define __xchg(ptr, new, size) \
|
||||||
({ \
|
({ \
|
||||||
__typeof__(ptr) __ptr = (ptr); \
|
__typeof__(ptr) __ptr = (ptr); \
|
||||||
@@ -148,12 +181,7 @@ long arch_atomic_xchg(atomic_t *atom, long newval)
|
|||||||
{
|
{
|
||||||
/* Atomically set new value and return old value. */
|
/* Atomically set new value and return old value. */
|
||||||
#ifdef __riscv_atomic
|
#ifdef __riscv_atomic
|
||||||
/*
|
return axchg(&atom->counter, newval);
|
||||||
* The name of GCC built-in macro __sync_lock_test_and_set()
|
|
||||||
* is misleading. A more appropriate name for GCC built-in
|
|
||||||
* macro would be __sync_val_exchange().
|
|
||||||
*/
|
|
||||||
return __sync_lock_test_and_set(&atom->counter, newval);
|
|
||||||
#else
|
#else
|
||||||
return xchg(&atom->counter, newval);
|
return xchg(&atom->counter, newval);
|
||||||
#endif
|
#endif
|
||||||
@@ -164,12 +192,7 @@ unsigned int atomic_raw_xchg_uint(volatile unsigned int *ptr,
|
|||||||
{
|
{
|
||||||
/* Atomically set new value and return old value. */
|
/* Atomically set new value and return old value. */
|
||||||
#ifdef __riscv_atomic
|
#ifdef __riscv_atomic
|
||||||
/*
|
return axchg(ptr, newval);
|
||||||
* The name of GCC built-in macro __sync_lock_test_and_set()
|
|
||||||
* is misleading. A more appropriate name for GCC built-in
|
|
||||||
* macro would be __sync_val_exchange().
|
|
||||||
*/
|
|
||||||
return __sync_lock_test_and_set(ptr, newval);
|
|
||||||
#else
|
#else
|
||||||
return xchg(ptr, newval);
|
return xchg(ptr, newval);
|
||||||
#endif
|
#endif
|
||||||
@@ -180,12 +203,7 @@ unsigned long atomic_raw_xchg_ulong(volatile unsigned long *ptr,
|
|||||||
{
|
{
|
||||||
/* Atomically set new value and return old value. */
|
/* Atomically set new value and return old value. */
|
||||||
#ifdef __riscv_atomic
|
#ifdef __riscv_atomic
|
||||||
/*
|
return axchg(ptr, newval);
|
||||||
* The name of GCC built-in macro __sync_lock_test_and_set()
|
|
||||||
* is misleading. A more appropriate name for GCC built-in
|
|
||||||
* macro would be __sync_val_exchange().
|
|
||||||
*/
|
|
||||||
return __sync_lock_test_and_set(ptr, newval);
|
|
||||||
#else
|
#else
|
||||||
return xchg(ptr, newval);
|
return xchg(ptr, newval);
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user