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>
18 #include <asm/processor.h>
19 #include <asm/cmpxchg.h>
20 #include <asm/barrier.h>
22 #define ATOMIC_INIT(i) { (i) }
25 * This Xtensa implementation assumes that the right mechanism
26 * for exclusion is for locking interrupts to level EXCM_LEVEL.
28 * Locking interrupts looks like this:
35 * Note that a15 is used here because the register allocation
36 * done by the compiler is not guaranteed and a window overflow
37 * may not occur between the rsil and wsr instructions. By using
38 * a15 in the rsil, the machine is guaranteed to be in a state
39 * where no register reference will cause an overflow.
43 * atomic_read - read atomic variable
44 * @v: pointer of type atomic_t
46 * Atomically reads the value of @v.
48 #define atomic_read(v) READ_ONCE((v)->counter)
51 * atomic_set - set atomic variable
52 * @v: pointer of type atomic_t
55 * Atomically sets the value of @v to @i.
57 #define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
59 #if XCHAL_HAVE_EXCLUSIVE
60 #define ATOMIC_OP(op) \
61 static inline void atomic_##op(int i, atomic_t *v) \
66 __asm__ __volatile__( \
68 " " #op " %0, %1, %2\n" \
72 : "=&a" (result), "=&a" (tmp) \
78 #define ATOMIC_OP_RETURN(op) \
79 static inline int atomic_##op##_return(int i, atomic_t *v) \
84 __asm__ __volatile__( \
86 " " #op " %0, %1, %2\n" \
90 " " #op " %0, %1, %2\n" \
91 : "=&a" (result), "=&a" (tmp) \
99 #define ATOMIC_FETCH_OP(op) \
100 static inline int atomic_fetch_##op(int i, atomic_t *v) \
105 __asm__ __volatile__( \
106 "1: l32ex %1, %3\n" \
107 " " #op " %0, %1, %2\n" \
111 : "=&a" (result), "=&a" (tmp) \
119 #elif XCHAL_HAVE_S32C1I
120 #define ATOMIC_OP(op) \
121 static inline void atomic_##op(int i, atomic_t * v) \
126 __asm__ __volatile__( \
127 "1: l32i %1, %3, 0\n" \
128 " wsr %1, scompare1\n" \
129 " " #op " %0, %1, %2\n" \
130 " s32c1i %0, %3, 0\n" \
131 " bne %0, %1, 1b\n" \
132 : "=&a" (result), "=&a" (tmp) \
138 #define ATOMIC_OP_RETURN(op) \
139 static inline int atomic_##op##_return(int i, atomic_t * v) \
144 __asm__ __volatile__( \
145 "1: l32i %1, %3, 0\n" \
146 " wsr %1, scompare1\n" \
147 " " #op " %0, %1, %2\n" \
148 " s32c1i %0, %3, 0\n" \
149 " bne %0, %1, 1b\n" \
150 " " #op " %0, %0, %2\n" \
151 : "=&a" (result), "=&a" (tmp) \
159 #define ATOMIC_FETCH_OP(op) \
160 static inline int atomic_fetch_##op(int i, atomic_t * v) \
165 __asm__ __volatile__( \
166 "1: l32i %1, %3, 0\n" \
167 " wsr %1, scompare1\n" \
168 " " #op " %0, %1, %2\n" \
169 " s32c1i %0, %3, 0\n" \
170 " bne %0, %1, 1b\n" \
171 : "=&a" (result), "=&a" (tmp) \
179 #else /* XCHAL_HAVE_S32C1I */
181 #define ATOMIC_OP(op) \
182 static inline void atomic_##op(int i, atomic_t * v) \
186 __asm__ __volatile__( \
187 " rsil a15, "__stringify(TOPLEVEL)"\n"\
188 " l32i %0, %2, 0\n" \
189 " " #op " %0, %0, %1\n" \
190 " s32i %0, %2, 0\n" \
199 #define ATOMIC_OP_RETURN(op) \
200 static inline int atomic_##op##_return(int i, atomic_t * v) \
204 __asm__ __volatile__( \
205 " rsil a15,"__stringify(TOPLEVEL)"\n" \
206 " l32i %0, %2, 0\n" \
207 " " #op " %0, %0, %1\n" \
208 " s32i %0, %2, 0\n" \
219 #define ATOMIC_FETCH_OP(op) \
220 static inline int atomic_fetch_##op(int i, atomic_t * v) \
222 unsigned int tmp, vval; \
224 __asm__ __volatile__( \
225 " rsil a15,"__stringify(TOPLEVEL)"\n" \
226 " l32i %0, %3, 0\n" \
227 " " #op " %1, %0, %2\n" \
228 " s32i %1, %3, 0\n" \
231 : "=&a" (vval), "=&a" (tmp) \
239 #endif /* XCHAL_HAVE_S32C1I */
241 #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_FETCH_OP(op) ATOMIC_OP_RETURN(op)
247 #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_FETCH_OP(op)
254 #undef ATOMIC_FETCH_OP
255 #undef ATOMIC_OP_RETURN
258 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
259 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
261 #endif /* _XTENSA_ATOMIC_H */