a03583d47b34088f8efec77d060c654f4ea37e70
2 * Based on arch/arm/include/asm/cmpxchg.h
4 * Copyright (C) 2012 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef __ASM_CMPXCHG_H
19 #define __ASM_CMPXCHG_H
21 #include <linux/bug.h>
23 #include <asm/barrier.h>
25 static inline unsigned long __xchg(unsigned long x
, volatile void *ptr
, int size
)
27 unsigned long ret
, tmp
;
31 asm volatile("// __xchg1\n"
33 " stlxrb %w1, %w3, %2\n"
35 : "=&r" (ret
), "=&r" (tmp
), "+Q" (*(u8
*)ptr
)
40 asm volatile("// __xchg2\n"
42 " stlxrh %w1, %w3, %2\n"
44 : "=&r" (ret
), "=&r" (tmp
), "+Q" (*(u16
*)ptr
)
49 asm volatile("// __xchg4\n"
51 " stlxr %w1, %w3, %2\n"
53 : "=&r" (ret
), "=&r" (tmp
), "+Q" (*(u32
*)ptr
)
58 asm volatile("// __xchg8\n"
60 " stlxr %w1, %3, %2\n"
62 : "=&r" (ret
), "=&r" (tmp
), "+Q" (*(u64
*)ptr
)
75 __typeof__(*(ptr)) __ret; \
76 __ret = (__typeof__(*(ptr))) \
77 __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))); \
81 static inline unsigned long __cmpxchg(volatile void *ptr
, unsigned long old
,
82 unsigned long new, int size
)
84 unsigned long oldval
= 0, res
;
89 asm volatile("// __cmpxchg1\n"
94 " stxrb %w0, %w4, %2\n"
96 : "=&r" (res
), "=&r" (oldval
), "+Q" (*(u8
*)ptr
)
97 : "Ir" (old
), "r" (new)
104 asm volatile("// __cmpxchg2\n"
109 " stxrh %w0, %w4, %2\n"
111 : "=&r" (res
), "=&r" (oldval
), "+Q" (*(u16
*)ptr
)
112 : "Ir" (old
), "r" (new)
119 asm volatile("// __cmpxchg4\n"
124 " stxr %w0, %w4, %2\n"
126 : "=&r" (res
), "=&r" (oldval
), "+Q" (*(u32
*)ptr
)
127 : "Ir" (old
), "r" (new)
134 asm volatile("// __cmpxchg8\n"
139 " stxr %w0, %4, %2\n"
141 : "=&r" (res
), "=&r" (oldval
), "+Q" (*(u64
*)ptr
)
142 : "Ir" (old
), "r" (new)
154 static inline unsigned long __cmpxchg_mb(volatile void *ptr
, unsigned long old
,
155 unsigned long new, int size
)
160 ret
= __cmpxchg(ptr
, old
, new, size
);
166 #define cmpxchg(ptr, o, n) \
168 __typeof__(*(ptr)) __ret; \
169 __ret = (__typeof__(*(ptr))) \
170 __cmpxchg_mb((ptr), (unsigned long)(o), (unsigned long)(n), \
175 #define cmpxchg_local(ptr, o, n) \
177 __typeof__(*(ptr)) __ret; \
178 __ret = (__typeof__(*(ptr))) \
179 __cmpxchg((ptr), (unsigned long)(o), \
180 (unsigned long)(n), sizeof(*(ptr))); \
184 #define cmpxchg64(ptr,o,n) cmpxchg((ptr),(o),(n))
185 #define cmpxchg64_local(ptr,o,n) cmpxchg_local((ptr),(o),(n))
187 #endif /* __ASM_CMPXCHG_H */