1 /* 64-bit atomic xchg() and cmpxchg() definitions.
3 * Copyright (C) 1996, 1997, 2000 David S. Miller (davem@redhat.com)
6 #ifndef __ARCH_SPARC64_CMPXCHG__
7 #define __ARCH_SPARC64_CMPXCHG__
9 static inline unsigned long xchg32(__volatile__ unsigned int *m, unsigned int val)
11 unsigned long tmp1, tmp2;
18 " bne,a,pn %%icc, 1b\n"
20 : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
26 static inline unsigned long xchg64(__volatile__ unsigned long *m, unsigned long val)
28 unsigned long tmp1, tmp2;
33 " casx [%4], %2, %0\n"
35 " bne,a,pn %%xcc, 1b\n"
37 : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
44 ({ __typeof__(*(ptr)) __ret; \
45 __ret = (__typeof__(*(ptr))) \
46 __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))); \
50 void __xchg_called_with_bad_pointer(void);
52 static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
57 return xchg32(ptr, x);
59 return xchg64(ptr, x);
61 __xchg_called_with_bad_pointer();
66 * Atomic compare and exchange. Compare OLD with MEM, if identical,
67 * store NEW in MEM. Return the initial value in MEM. Success is
68 * indicated by comparing RETURN with OLD.
71 #include <asm-generic/cmpxchg-local.h>
73 static inline unsigned long
74 __cmpxchg_u32(volatile int *m, int old, int new)
76 __asm__ __volatile__("cas [%2], %3, %0"
78 : "0" (new), "r" (m), "r" (old)
84 static inline unsigned long
85 __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
87 __asm__ __volatile__("casx [%2], %3, %0"
89 : "0" (new), "r" (m), "r" (old)
95 /* This function doesn't exist, so you'll get a linker error
96 if something tries to do an invalid cmpxchg(). */
97 void __cmpxchg_called_with_bad_pointer(void);
99 static inline unsigned long
100 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
104 return __cmpxchg_u32(ptr, old, new);
106 return __cmpxchg_u64(ptr, old, new);
108 __cmpxchg_called_with_bad_pointer();
112 #define cmpxchg(ptr,o,n) \
114 __typeof__(*(ptr)) _o_ = (o); \
115 __typeof__(*(ptr)) _n_ = (n); \
116 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
117 (unsigned long)_n_, sizeof(*(ptr))); \
121 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
125 static inline unsigned long __cmpxchg_local(volatile void *ptr,
127 unsigned long new, int size)
131 case 8: return __cmpxchg(ptr, old, new, size);
133 return __cmpxchg_local_generic(ptr, old, new, size);
139 #define cmpxchg_local(ptr, o, n) \
140 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
141 (unsigned long)(n), sizeof(*(ptr))))
142 #define cmpxchg64_local(ptr, o, n) \
144 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
145 cmpxchg_local((ptr), (o), (n)); \
147 #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
149 #endif /* __ARCH_SPARC64_CMPXCHG__ */