2008-10-22 23:26:29 -06:00
|
|
|
#ifndef _ASM_X86_CMPXCHG_64_H
|
|
|
|
#define _ASM_X86_CMPXCHG_64_H
|
2007-05-08 01:35:02 -06:00
|
|
|
|
|
|
|
#include <asm/alternative.h> /* Provides LOCK_PREFIX */
|
|
|
|
|
2010-07-28 00:29:52 -06:00
|
|
|
static inline void set_64bit(volatile u64 *ptr, u64 val)
|
2007-05-08 01:35:02 -06:00
|
|
|
{
|
|
|
|
*ptr = val;
|
|
|
|
}
|
|
|
|
|
2009-10-09 02:12:46 -06:00
|
|
|
extern void __xchg_wrong_size(void);
|
|
|
|
extern void __cmpxchg_wrong_size(void);
|
|
|
|
|
2007-05-08 01:35:02 -06:00
|
|
|
/*
|
2010-07-28 16:18:35 -06:00
|
|
|
* Note: no "lock" prefix even on SMP: xchg always implies lock anyway.
|
|
|
|
* Since this is generally used to protect other memory information, we
|
|
|
|
* use "asm volatile" and "memory" clobbers to prevent gcc from moving
|
|
|
|
* information around.
|
2007-05-08 01:35:02 -06:00
|
|
|
*/
|
2009-10-09 02:12:46 -06:00
|
|
|
#define __xchg(x, ptr, size) \
|
|
|
|
({ \
|
|
|
|
__typeof(*(ptr)) __x = (x); \
|
|
|
|
switch (size) { \
|
|
|
|
case 1: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u8 *__ptr = (volatile u8 *)(ptr); \
|
|
|
|
asm volatile("xchgb %0,%1" \
|
|
|
|
: "=q" (__x), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "0" (__x) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
case 2: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u16 *__ptr = (volatile u16 *)(ptr); \
|
|
|
|
asm volatile("xchgw %0,%1" \
|
|
|
|
: "=r" (__x), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "0" (__x) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
case 4: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u32 *__ptr = (volatile u32 *)(ptr); \
|
|
|
|
asm volatile("xchgl %0,%1" \
|
|
|
|
: "=r" (__x), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "0" (__x) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
case 8: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u64 *__ptr = (volatile u64 *)(ptr); \
|
2009-10-09 02:12:46 -06:00
|
|
|
asm volatile("xchgq %0,%1" \
|
2010-07-28 16:18:35 -06:00
|
|
|
: "=r" (__x), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "0" (__x) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
default: \
|
|
|
|
__xchg_wrong_size(); \
|
|
|
|
} \
|
|
|
|
__x; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define xchg(ptr, v) \
|
|
|
|
__xchg((v), (ptr), sizeof(*ptr))
|
|
|
|
|
|
|
|
#define __HAVE_ARCH_CMPXCHG 1
|
2007-05-08 01:35:02 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Atomic compare and exchange. Compare OLD with MEM, if identical,
|
|
|
|
* store NEW in MEM. Return the initial value in MEM. Success is
|
|
|
|
* indicated by comparing RETURN with OLD.
|
|
|
|
*/
|
2009-10-09 02:12:46 -06:00
|
|
|
#define __raw_cmpxchg(ptr, old, new, size, lock) \
|
|
|
|
({ \
|
|
|
|
__typeof__(*(ptr)) __ret; \
|
|
|
|
__typeof__(*(ptr)) __old = (old); \
|
|
|
|
__typeof__(*(ptr)) __new = (new); \
|
|
|
|
switch (size) { \
|
|
|
|
case 1: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u8 *__ptr = (volatile u8 *)(ptr); \
|
|
|
|
asm volatile(lock "cmpxchgb %2,%1" \
|
|
|
|
: "=a" (__ret), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "q" (__new), "0" (__old) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
case 2: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u16 *__ptr = (volatile u16 *)(ptr); \
|
|
|
|
asm volatile(lock "cmpxchgw %2,%1" \
|
|
|
|
: "=a" (__ret), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "r" (__new), "0" (__old) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
case 4: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u32 *__ptr = (volatile u32 *)(ptr); \
|
|
|
|
asm volatile(lock "cmpxchgl %2,%1" \
|
|
|
|
: "=a" (__ret), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "r" (__new), "0" (__old) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
case 8: \
|
2010-07-28 16:18:35 -06:00
|
|
|
{ \
|
|
|
|
volatile u64 *__ptr = (volatile u64 *)(ptr); \
|
2010-07-27 18:01:49 -06:00
|
|
|
asm volatile(lock "cmpxchgq %2,%1" \
|
2010-07-28 16:18:35 -06:00
|
|
|
: "=a" (__ret), "+m" (*__ptr) \
|
2010-07-27 18:01:49 -06:00
|
|
|
: "r" (__new), "0" (__old) \
|
2009-10-09 02:12:46 -06:00
|
|
|
: "memory"); \
|
|
|
|
break; \
|
2010-07-28 16:18:35 -06:00
|
|
|
} \
|
2009-10-09 02:12:46 -06:00
|
|
|
default: \
|
|
|
|
__cmpxchg_wrong_size(); \
|
|
|
|
} \
|
|
|
|
__ret; \
|
|
|
|
})
|
2007-05-08 01:35:02 -06:00
|
|
|
|
2009-10-09 02:12:46 -06:00
|
|
|
#define __cmpxchg(ptr, old, new, size) \
|
|
|
|
__raw_cmpxchg((ptr), (old), (new), (size), LOCK_PREFIX)
|
2007-05-08 01:35:02 -06:00
|
|
|
|
2009-10-09 02:12:46 -06:00
|
|
|
#define __sync_cmpxchg(ptr, old, new, size) \
|
|
|
|
__raw_cmpxchg((ptr), (old), (new), (size), "lock; ")
|
2007-05-08 01:35:02 -06:00
|
|
|
|
2009-10-09 02:12:46 -06:00
|
|
|
#define __cmpxchg_local(ptr, old, new, size) \
|
|
|
|
__raw_cmpxchg((ptr), (old), (new), (size), "")
|
2008-06-24 22:19:10 -06:00
|
|
|
|
2009-10-09 02:12:46 -06:00
|
|
|
#define cmpxchg(ptr, old, new) \
|
|
|
|
__cmpxchg((ptr), (old), (new), sizeof(*ptr))
|
|
|
|
|
|
|
|
#define sync_cmpxchg(ptr, old, new) \
|
|
|
|
__sync_cmpxchg((ptr), (old), (new), sizeof(*ptr))
|
|
|
|
|
|
|
|
#define cmpxchg_local(ptr, old, new) \
|
|
|
|
__cmpxchg_local((ptr), (old), (new), sizeof(*ptr))
|
2007-05-08 01:35:02 -06:00
|
|
|
|
2008-02-07 01:16:10 -07:00
|
|
|
#define cmpxchg64(ptr, o, n) \
|
2008-03-23 02:01:52 -06:00
|
|
|
({ \
|
2008-02-07 01:16:10 -07:00
|
|
|
BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
|
|
|
|
cmpxchg((ptr), (o), (n)); \
|
2008-03-23 02:01:52 -06:00
|
|
|
})
|
2009-10-09 02:12:46 -06:00
|
|
|
|
2008-02-07 01:16:10 -07:00
|
|
|
#define cmpxchg64_local(ptr, o, n) \
|
2008-03-23 02:01:52 -06:00
|
|
|
({ \
|
2008-02-07 01:16:10 -07:00
|
|
|
BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
|
|
|
|
cmpxchg_local((ptr), (o), (n)); \
|
2008-03-23 02:01:52 -06:00
|
|
|
})
|
2007-05-08 01:35:02 -06:00
|
|
|
|
2008-10-22 23:26:29 -06:00
|
|
|
#endif /* _ASM_X86_CMPXCHG_64_H */
|