1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright IBM Corp. 1999, 2011
8 #ifndef __ASM_CMPXCHG_H
9 #define __ASM_CMPXCHG_H
11 #include <linux/mmdebug.h>
12 #include <linux/types.h>
13 #include <linux/bug.h>
15 void __xchg_called_with_bad_pointer(void);
17 static __always_inline unsigned long __xchg(unsigned long x,
18 unsigned long address, int size)
25 shift = (3 ^ (address & 3)) << 3;
26 address ^= address & 3;
34 : "=&d" (old), "+Q" (*(int *) address)
35 : "d" ((x & 0xff) << shift), "d" (~(0xff << shift))
36 : "memory", "cc", "0");
39 shift = (2 ^ (address & 2)) << 3;
40 address ^= address & 2;
48 : "=&d" (old), "+Q" (*(int *) address)
49 : "d" ((x & 0xffff) << shift), "d" (~(0xffff << shift))
50 : "memory", "cc", "0");
57 : "=&d" (old), "+Q" (*(int *) address)
66 : "=&d" (old), "+QS" (*(long *) address)
71 __xchg_called_with_bad_pointer();
75 #define arch_xchg(ptr, x) \
77 __typeof__(*(ptr)) __ret; \
79 __ret = (__typeof__(*(ptr))) \
80 __xchg((unsigned long)(x), (unsigned long)(ptr), \
85 void __cmpxchg_called_with_bad_pointer(void);
87 static __always_inline unsigned long __cmpxchg(unsigned long address,
89 unsigned long new, int size)
91 unsigned long prev, tmp;
96 shift = (3 ^ (address & 3)) << 3;
97 address ^= address & 3;
110 : "=&d" (prev), "=&d" (tmp), "+Q" (*(int *) address)
111 : "d" ((old & 0xff) << shift),
112 "d" ((new & 0xff) << shift),
113 "d" (~(0xff << shift))
115 return prev >> shift;
117 shift = (2 ^ (address & 2)) << 3;
118 address ^= address & 2;
131 : "=&d" (prev), "=&d" (tmp), "+Q" (*(int *) address)
132 : "d" ((old & 0xffff) << shift),
133 "d" ((new & 0xffff) << shift),
134 "d" (~(0xffff << shift))
136 return prev >> shift;
140 : "=&d" (prev), "+Q" (*(int *) address)
141 : "0" (old), "d" (new)
147 : "=&d" (prev), "+QS" (*(long *) address)
148 : "0" (old), "d" (new)
152 __cmpxchg_called_with_bad_pointer();
156 #define arch_cmpxchg(ptr, o, n) \
158 __typeof__(*(ptr)) __ret; \
160 __ret = (__typeof__(*(ptr))) \
161 __cmpxchg((unsigned long)(ptr), (unsigned long)(o), \
162 (unsigned long)(n), sizeof(*(ptr))); \
166 #define arch_cmpxchg64 arch_cmpxchg
167 #define arch_cmpxchg_local arch_cmpxchg
168 #define arch_cmpxchg64_local arch_cmpxchg
170 #define system_has_cmpxchg_double() 1
172 static __always_inline int __cmpxchg_double(unsigned long p1, unsigned long p2,
173 unsigned long o1, unsigned long o2,
174 unsigned long n1, unsigned long n2)
176 union register_pair old = { .even = o1, .odd = o2, };
177 union register_pair new = { .even = n1, .odd = n2, };
181 " cdsg %[old],%[new],%[ptr]\n"
184 : [cc] "=&d" (cc), [old] "+&d" (old.pair)
185 : [new] "d" (new.pair),
186 [ptr] "QS" (*(unsigned long *)p1), "Q" (*(unsigned long *)p2)
191 #define arch_cmpxchg_double(p1, p2, o1, o2, n1, n2) \
193 typeof(p1) __p1 = (p1); \
194 typeof(p2) __p2 = (p2); \
196 BUILD_BUG_ON(sizeof(*(p1)) != sizeof(long)); \
197 BUILD_BUG_ON(sizeof(*(p2)) != sizeof(long)); \
198 VM_BUG_ON((unsigned long)((__p1) + 1) != (unsigned long)(__p2));\
199 __cmpxchg_double((unsigned long)__p1, (unsigned long)__p2, \
200 (unsigned long)(o1), (unsigned long)(o2), \
201 (unsigned long)(n1), (unsigned long)(n2)); \
204 #endif /* __ASM_CMPXCHG_H */