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) ACCESS_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) ((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)
149 #undef ATOMIC_OP_RETURN
153 * atomic_sub_and_test - subtract value from variable and test result
154 * @i: integer value to subtract
155 * @v: pointer of type atomic_t
157 * Atomically subtracts @i from @v and returns
158 * true if the result is zero, or false for all
161 #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
164 * atomic_inc - increment atomic variable
165 * @v: pointer of type atomic_t
167 * Atomically increments @v by 1.
169 #define atomic_inc(v) atomic_add(1,(v))
172 * atomic_inc - increment atomic variable
173 * @v: pointer of type atomic_t
175 * Atomically increments @v by 1.
177 #define atomic_inc_return(v) atomic_add_return(1,(v))
180 * atomic_dec - decrement atomic variable
181 * @v: pointer of type atomic_t
183 * Atomically decrements @v by 1.
185 #define atomic_dec(v) atomic_sub(1,(v))
188 * atomic_dec_return - decrement atomic variable
189 * @v: pointer of type atomic_t
191 * Atomically decrements @v by 1.
193 #define atomic_dec_return(v) atomic_sub_return(1,(v))
196 * atomic_dec_and_test - decrement and test
197 * @v: pointer of type atomic_t
199 * Atomically decrements @v by 1 and
200 * returns true if the result is 0, or false for all other
203 #define atomic_dec_and_test(v) (atomic_sub_return(1,(v)) == 0)
206 * atomic_inc_and_test - increment and test
207 * @v: pointer of type atomic_t
209 * Atomically increments @v by 1
210 * and returns true if the result is zero, or false for all
213 #define atomic_inc_and_test(v) (atomic_add_return(1,(v)) == 0)
216 * atomic_add_negative - add and test if negative
217 * @v: pointer of type atomic_t
218 * @i: integer value to add
220 * Atomically adds @i to @v and returns true
221 * if the result is negative, or false when
222 * result is greater than or equal to zero.
224 #define atomic_add_negative(i,v) (atomic_add_return((i),(v)) < 0)
226 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
227 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
230 * __atomic_add_unless - add unless the number is a given value
231 * @v: pointer of type atomic_t
232 * @a: the amount to add to v...
233 * @u: ...unless v is equal to u.
235 * Atomically adds @a to @v, so long as it was not @u.
236 * Returns the old value of @v.
238 static __inline__
int __atomic_add_unless(atomic_t
*v
, int a
, int u
)
243 if (unlikely(c
== (u
)))
245 old
= atomic_cmpxchg((v
), c
, c
+ (a
));
246 if (likely(old
== c
))
254 static inline void atomic_clear_mask(unsigned int mask
, atomic_t
*v
)
256 #if XCHAL_HAVE_S32C1I
260 __asm__
__volatile__(
261 "1: l32i %1, %3, 0\n"
262 " wsr %1, scompare1\n"
264 " s32c1i %0, %3, 0\n"
266 : "=&a" (result
), "=&a" (tmp
)
267 : "a" (~mask
), "a" (v
)
271 unsigned int all_f
= -1;
274 __asm__
__volatile__(
275 " rsil a15,"__stringify(TOPLEVEL
)"\n"
282 : "=&a" (vval
), "=a" (mask
)
283 : "a" (v
), "a" (all_f
), "1" (mask
)
289 static inline void atomic_set_mask(unsigned int mask
, atomic_t
*v
)
291 #if XCHAL_HAVE_S32C1I
295 __asm__
__volatile__(
296 "1: l32i %1, %3, 0\n"
297 " wsr %1, scompare1\n"
299 " s32c1i %0, %3, 0\n"
301 : "=&a" (result
), "=&a" (tmp
)
302 : "a" (mask
), "a" (v
)
308 __asm__
__volatile__(
309 " rsil a15,"__stringify(TOPLEVEL
)"\n"
316 : "a" (mask
), "a" (v
)
322 #endif /* __KERNEL__ */
324 #endif /* _XTENSA_ATOMIC_H */
This page took 0.072004 seconds and 6 git commands to generate.