2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _ASM_ATOMIC_H_
33 #define _ASM_ATOMIC_H_
35 #include <linux/compiler.h>
36 #include <sys/types.h>
37 #include <machine/atomic.h>
38 #define ATOMIC_INIT(x) { .counter = (x) }
44 /*------------------------------------------------------------------------*
45 * 32-bit atomic operations
46 *------------------------------------------------------------------------*/
48 #define atomic_add(i, v) atomic_add_return((i), (v))
49 #define atomic_sub(i, v) atomic_sub_return((i), (v))
50 #define atomic_inc_return(v) atomic_add_return(1, (v))
51 #define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
52 #define atomic_add_and_test(i, v) (atomic_add_return((i), (v)) == 0)
53 #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
54 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
55 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
56 #define atomic_dec_return(v) atomic_sub_return(1, (v))
57 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
60 atomic_add_return(int i, atomic_t *v)
62 return i + atomic_fetchadd_int(&v->counter, i);
66 atomic_sub_return(int i, atomic_t *v)
68 return atomic_fetchadd_int(&v->counter, -i) - i;
72 atomic_set(atomic_t *v, int i)
74 WRITE_ONCE(v->counter, i);
78 atomic_set_release(atomic_t *v, int i)
80 atomic_store_rel_int(&v->counter, i);
84 atomic_set_mask(unsigned int mask, atomic_t *v)
86 atomic_set_int(&v->counter, mask);
90 atomic_read(const atomic_t *v)
92 return READ_ONCE(v->counter);
96 atomic_inc(atomic_t *v)
98 return atomic_fetchadd_int(&v->counter, 1) + 1;
102 atomic_dec(atomic_t *v)
104 return atomic_fetchadd_int(&v->counter, -1) - 1;
108 atomic_add_unless(atomic_t *v, int a, int u)
110 int c = atomic_read(v);
113 if (unlikely(c == u))
115 if (likely(atomic_fcmpset_int(&v->counter, &c, c + a)))
122 atomic_fetch_add_unless(atomic_t *v, int a, int u)
124 int c = atomic_read(v);
127 if (unlikely(c == u))
129 if (likely(atomic_fcmpset_int(&v->counter, &c, c + a)))
136 atomic_clear_mask(unsigned int mask, atomic_t *v)
138 atomic_clear_int(&v->counter, mask);
142 atomic_xchg(atomic_t *v, int i)
144 return (atomic_swap_int(&v->counter, i));
148 atomic_cmpxchg(atomic_t *v, int old, int new)
153 if (atomic_fcmpset_int(&v->counter, &ret, new))
161 #if defined(__amd64__) || defined(__arm64__) || defined(__i386__)
162 #define LINUXKPI_ATOMIC_8(...) __VA_ARGS__
163 #define LINUXKPI_ATOMIC_16(...) __VA_ARGS__
165 #define LINUXKPI_ATOMIC_8(...)
166 #define LINUXKPI_ATOMIC_16(...)
169 #if !(defined(i386) || (defined(__mips__) && !(defined(__mips_n32) || \
170 defined(__mips_n64))) || (defined(__powerpc__) && \
171 !defined(__powerpc64__)))
172 #define LINUXKPI_ATOMIC_64(...) __VA_ARGS__
174 #define LINUXKPI_ATOMIC_64(...)
177 #define cmpxchg(ptr, old, new) ({ \
179 __typeof(*(ptr)) val; \
184 } __ret = { .val = (old) }, __new = { .val = (new) }; \
187 LINUXKPI_ATOMIC_8(sizeof(__ret.val) == 1 ||) \
188 LINUXKPI_ATOMIC_16(sizeof(__ret.val) == 2 ||) \
189 LINUXKPI_ATOMIC_64(sizeof(__ret.val) == 8 ||) \
190 sizeof(__ret.val) == 4); \
192 switch (sizeof(__ret.val)) { \
195 while (!atomic_fcmpset_8((volatile u8 *)(ptr), \
196 __ret.u8, __new.u8[0]) && __ret.val == (old)) \
200 LINUXKPI_ATOMIC_16( \
202 while (!atomic_fcmpset_16((volatile u16 *)(ptr), \
203 __ret.u16, __new.u16[0]) && __ret.val == (old)) \
208 while (!atomic_fcmpset_32((volatile u32 *)(ptr), \
209 __ret.u32, __new.u32[0]) && __ret.val == (old)) \
212 LINUXKPI_ATOMIC_64( \
214 while (!atomic_fcmpset_64((volatile u64 *)(ptr), \
215 __ret.u64, __new.u64[0]) && __ret.val == (old)) \
223 #define cmpxchg_relaxed(...) cmpxchg(__VA_ARGS__)
225 #define xchg(ptr, new) ({ \
227 __typeof(*(ptr)) val; \
232 } __ret, __new = { .val = (new) }; \
235 LINUXKPI_ATOMIC_8(sizeof(__ret.val) == 1 ||) \
236 LINUXKPI_ATOMIC_16(sizeof(__ret.val) == 2 ||) \
237 LINUXKPI_ATOMIC_64(sizeof(__ret.val) == 8 ||) \
238 sizeof(__ret.val) == 4); \
240 switch (sizeof(__ret.val)) { \
243 __ret.val = READ_ONCE(*ptr); \
244 while (!atomic_fcmpset_8((volatile u8 *)(ptr), \
245 __ret.u8, __new.u8[0])) \
249 LINUXKPI_ATOMIC_16( \
251 __ret.val = READ_ONCE(*ptr); \
252 while (!atomic_fcmpset_16((volatile u16 *)(ptr), \
253 __ret.u16, __new.u16[0])) \
258 __ret.u32[0] = atomic_swap_32((volatile u32 *)(ptr), \
261 LINUXKPI_ATOMIC_64( \
263 __ret.u64[0] = atomic_swap_64((volatile u64 *)(ptr), \
272 atomic_dec_if_positive(atomic_t *v)
277 old = atomic_read(v);
280 if (unlikely(retval < 0))
282 if (likely(atomic_fcmpset_int(&v->counter, &old, retval)))
288 #define LINUX_ATOMIC_OP(op, c_op) \
289 static inline void atomic_##op(int i, atomic_t *v) \
294 while ((old = atomic_cmpxchg(v, c, c c_op i)) != c) \
298 #define LINUX_ATOMIC_FETCH_OP(op, c_op) \
299 static inline int atomic_fetch_##op(int i, atomic_t *v) \
304 while ((old = atomic_cmpxchg(v, c, c c_op i)) != c) \
310 LINUX_ATOMIC_OP(or, |)
311 LINUX_ATOMIC_OP(and, &)
312 LINUX_ATOMIC_OP(andnot, &~)
313 LINUX_ATOMIC_OP(xor, ^)
315 LINUX_ATOMIC_FETCH_OP(or, |)
316 LINUX_ATOMIC_FETCH_OP(and, &)
317 LINUX_ATOMIC_FETCH_OP(andnot, &~)
318 LINUX_ATOMIC_FETCH_OP(xor, ^)
320 #endif /* _ASM_ATOMIC_H_ */