2 * include/asm-xtensa/atomic.h
4 * Atomic operations that C can't guarantee us. Useful for resource counting..
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 * Copyright (C) 2001 - 2008 Tensilica Inc.
13 #ifndef _XTENSA_ATOMIC_H
14 #define _XTENSA_ATOMIC_H
16 #include <linux/stringify.h>
17 #include <linux/types.h>
20 #include <asm/processor.h>
21 #include <asm/cmpxchg.h>
22 #include <asm/barrier.h>
24 #define ATOMIC_INIT(i) { (i) }
27 * This Xtensa implementation assumes that the right mechanism
28 * for exclusion is for locking interrupts to level EXCM_LEVEL.
30 * Locking interrupts looks like this:
37 * Note that a15 is used here because the register allocation
38 * done by the compiler is not guaranteed and a window overflow
39 * may not occur between the rsil and wsr instructions. By using
40 * a15 in the rsil, the machine is guaranteed to be in a state
41 * where no register reference will cause an overflow.
45 * atomic_read - read atomic variable
46 * @v: pointer of type atomic_t
48 * Atomically reads the value of @v.
50 #define atomic_read(v) READ_ONCE((v)->counter)
53 * atomic_set - set atomic variable
54 * @v: pointer of type atomic_t
57 * Atomically sets the value of @v to @i.
59 #define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
62 #define ATOMIC_OP(op) \
63 static inline void atomic_##op(int i, atomic_t * v) \
68 __asm__ __volatile__( \
69 "1: l32i %1, %3, 0\n" \
70 " wsr %1, scompare1\n" \
71 " " #op " %0, %1, %2\n" \
72 " s32c1i %0, %3, 0\n" \
74 : "=&a" (result), "=&a" (tmp) \
80 #define ATOMIC_OP_RETURN(op) \
81 static inline int atomic_##op##_return(int i, atomic_t * v) \
86 __asm__ __volatile__( \
87 "1: l32i %1, %3, 0\n" \
88 " wsr %1, scompare1\n" \
89 " " #op " %0, %1, %2\n" \
90 " s32c1i %0, %3, 0\n" \
92 " " #op " %0, %0, %2\n" \
93 : "=&a" (result), "=&a" (tmp) \
101 #else /* XCHAL_HAVE_S32C1I */
103 #define ATOMIC_OP(op) \
104 static inline void atomic_##op(int i, atomic_t * v) \
108 __asm__ __volatile__( \
109 " rsil a15, "__stringify(TOPLEVEL)"\n"\
110 " l32i %0, %2, 0\n" \
111 " " #op " %0, %0, %1\n" \
112 " s32i %0, %2, 0\n" \
121 #define ATOMIC_OP_RETURN(op) \
122 static inline int atomic_##op##_return(int i, atomic_t * v) \
126 __asm__ __volatile__( \
127 " rsil a15,"__stringify(TOPLEVEL)"\n" \
128 " l32i %0, %2, 0\n" \
129 " " #op " %0, %0, %1\n" \
130 " s32i %0, %2, 0\n" \
141 #endif /* XCHAL_HAVE_S32C1I */
143 #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op)
153 #undef ATOMIC_OP_RETURN
157 * atomic_sub_and_test - subtract value from variable and test result
158 * @i: integer value to subtract
159 * @v: pointer of type atomic_t
161 * Atomically subtracts @i from @v and returns
162 * true if the result is zero, or false for all
165 #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
168 * atomic_inc - increment atomic variable
169 * @v: pointer of type atomic_t
171 * Atomically increments @v by 1.
173 #define atomic_inc(v) atomic_add(1,(v))
176 * atomic_inc - increment atomic variable
177 * @v: pointer of type atomic_t
179 * Atomically increments @v by 1.
181 #define atomic_inc_return(v) atomic_add_return(1,(v))
184 * atomic_dec - decrement atomic variable
185 * @v: pointer of type atomic_t
187 * Atomically decrements @v by 1.
189 #define atomic_dec(v) atomic_sub(1,(v))
192 * atomic_dec_return - decrement atomic variable
193 * @v: pointer of type atomic_t
195 * Atomically decrements @v by 1.
197 #define atomic_dec_return(v) atomic_sub_return(1,(v))
200 * atomic_dec_and_test - decrement and test
201 * @v: pointer of type atomic_t
203 * Atomically decrements @v by 1 and
204 * returns true if the result is 0, or false for all other
207 #define atomic_dec_and_test(v) (atomic_sub_return(1,(v)) == 0)
210 * atomic_inc_and_test - increment and test
211 * @v: pointer of type atomic_t
213 * Atomically increments @v by 1
214 * and returns true if the result is zero, or false for all
217 #define atomic_inc_and_test(v) (atomic_add_return(1,(v)) == 0)
220 * atomic_add_negative - add and test if negative
221 * @v: pointer of type atomic_t
222 * @i: integer value to add
224 * Atomically adds @i to @v and returns true
225 * if the result is negative, or false when
226 * result is greater than or equal to zero.
228 #define atomic_add_negative(i,v) (atomic_add_return((i),(v)) < 0)
230 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
231 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
234 * __atomic_add_unless - add unless the number is a given value
235 * @v: pointer of type atomic_t
236 * @a: the amount to add to v...
237 * @u: ...unless v is equal to u.
239 * Atomically adds @a to @v, so long as it was not @u.
240 * Returns the old value of @v.
242 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
247 if (unlikely(c == (u)))
249 old = atomic_cmpxchg((v), c, c + (a));
250 if (likely(old == c))
257 #endif /* __KERNEL__ */
259 #endif /* _XTENSA_ATOMIC_H */