]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/sparc/include/asm/cmpxchg_64.h
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
10 __cmpxchg_u32(volatile int *m
, int old
, int new)
12 __asm__
__volatile__("cas [%2], %3, %0"
14 : "0" (new), "r" (m
), "r" (old
)
20 static inline unsigned long xchg32(__volatile__
unsigned int *m
, unsigned int val
)
22 unsigned long tmp1
, tmp2
;
29 " bne,a,pn %%icc, 1b\n"
31 : "=&r" (val
), "=&r" (tmp1
), "=&r" (tmp2
)
37 static inline unsigned long xchg64(__volatile__
unsigned long *m
, unsigned long val
)
39 unsigned long tmp1
, tmp2
;
44 " casx [%4], %2, %0\n"
46 " bne,a,pn %%xcc, 1b\n"
48 : "=&r" (val
), "=&r" (tmp1
), "=&r" (tmp2
)
54 #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
56 void __xchg_called_with_bad_pointer(void);
59 * Use 4 byte cas instruction to achieve 2 byte xchg. Main logic
60 * here is to get the bit shift of the byte we are interested in.
61 * The XOR is handy for reversing the bits for big-endian byte order.
63 static inline unsigned long
64 xchg16(__volatile__
unsigned short *m
, unsigned short val
)
66 unsigned long maddr
= (unsigned long)m
;
67 int bit_shift
= (((unsigned long)m
& 2) ^ 2) << 3;
68 unsigned int mask
= 0xffff << bit_shift
;
69 unsigned int *ptr
= (unsigned int *) (maddr
& ~2);
70 unsigned int old32
, new32
, load32
;
72 /* Read the old value */
77 new32
= (load32
& (~mask
)) | val
<< bit_shift
;
78 load32
= __cmpxchg_u32(ptr
, old32
, new32
);
79 } while (load32
!= old32
);
81 return (load32
& mask
) >> bit_shift
;
84 static inline unsigned long __xchg(unsigned long x
, __volatile__
void * ptr
,
89 return xchg16(ptr
, x
);
91 return xchg32(ptr
, x
);
93 return xchg64(ptr
, x
);
95 __xchg_called_with_bad_pointer();
100 * Atomic compare and exchange. Compare OLD with MEM, if identical,
101 * store NEW in MEM. Return the initial value in MEM. Success is
102 * indicated by comparing RETURN with OLD.
105 #include <asm-generic/cmpxchg-local.h>
108 static inline unsigned long
109 __cmpxchg_u64(volatile long *m
, unsigned long old
, unsigned long new)
111 __asm__
__volatile__("casx [%2], %3, %0"
113 : "0" (new), "r" (m
), "r" (old
)
120 * Use 4 byte cas instruction to achieve 1 byte cmpxchg. Main logic
121 * here is to get the bit shift of the byte we are interested in.
122 * The XOR is handy for reversing the bits for big-endian byte order
124 static inline unsigned long
125 __cmpxchg_u8(volatile unsigned char *m
, unsigned char old
, unsigned char new)
127 unsigned long maddr
= (unsigned long)m
;
128 int bit_shift
= (((unsigned long)m
& 3) ^ 3) << 3;
129 unsigned int mask
= 0xff << bit_shift
;
130 unsigned int *ptr
= (unsigned int *) (maddr
& ~3);
131 unsigned int old32
, new32
, load
;
132 unsigned int load32
= *ptr
;
135 new32
= (load32
& ~mask
) | (new << bit_shift
);
136 old32
= (load32
& ~mask
) | (old
<< bit_shift
);
137 load32
= __cmpxchg_u32(ptr
, old32
, new32
);
140 load
= (load32
& mask
) >> bit_shift
;
141 } while (load
== old
);
146 /* This function doesn't exist, so you'll get a linker error
147 if something tries to do an invalid cmpxchg(). */
148 void __cmpxchg_called_with_bad_pointer(void);
150 static inline unsigned long
151 __cmpxchg(volatile void *ptr
, unsigned long old
, unsigned long new, int size
)
155 return __cmpxchg_u8(ptr
, old
, new);
157 return __cmpxchg_u32(ptr
, old
, new);
159 return __cmpxchg_u64(ptr
, old
, new);
161 __cmpxchg_called_with_bad_pointer();
165 #define cmpxchg(ptr,o,n) \
167 __typeof__(*(ptr)) _o_ = (o); \
168 __typeof__(*(ptr)) _n_ = (n); \
169 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
170 (unsigned long)_n_, sizeof(*(ptr))); \
174 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
178 static inline unsigned long __cmpxchg_local(volatile void *ptr
,
180 unsigned long new, int size
)
184 case 8: return __cmpxchg(ptr
, old
, new, size
);
186 return __cmpxchg_local_generic(ptr
, old
, new, size
);
192 #define cmpxchg_local(ptr, o, n) \
193 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
194 (unsigned long)(n), sizeof(*(ptr))))
195 #define cmpxchg64_local(ptr, o, n) \
197 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
198 cmpxchg_local((ptr), (o), (n)); \
200 #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
202 #endif /* __ARCH_SPARC64_CMPXCHG__ */