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 - 2005 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/system.h>
23 #define ATOMIC_INIT(i) { (i) }
26 * This Xtensa implementation assumes that the right mechanism
27 * for exclusion is for locking interrupts to level 1.
29 * Locking interrupts looks like this:
36 * Note that a15 is used here because the register allocation
37 * done by the compiler is not guaranteed and a window overflow
38 * may not occur between the rsil and wsr instructions. By using
39 * a15 in the rsil, the machine is guaranteed to be in a state
40 * where no register reference will cause an overflow.
44 * atomic_read - read atomic variable
45 * @v: pointer of type atomic_t
47 * Atomically reads the value of @v.
49 #define atomic_read(v) ((v)->counter)
52 * atomic_set - set atomic variable
53 * @v: pointer of type atomic_t
56 * Atomically sets the value of @v to @i.
58 #define atomic_set(v,i) ((v)->counter = (i))
61 * atomic_add - add integer to atomic variable
62 * @i: integer value to add
63 * @v: pointer of type atomic_t
65 * Atomically adds @i to @v.
67 static inline void atomic_add(int i, atomic_t * v)
72 "rsil a15, "__stringify(LOCKLEVEL)"\n\t"
76 "wsr a15, "__stringify(PS)" \n\t"
85 * atomic_sub - subtract the atomic variable
86 * @i: integer value to subtract
87 * @v: pointer of type atomic_t
89 * Atomically subtracts @i from @v.
91 static inline void atomic_sub(int i, atomic_t *v)
96 "rsil a15, "__stringify(LOCKLEVEL)"\n\t"
100 "wsr a15, "__stringify(PS)" \n\t"
109 * We use atomic_{add|sub}_return to define other functions.
112 static inline int atomic_add_return(int i, atomic_t * v)
116 __asm__ __volatile__(
117 "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
118 "l32i %0, %2, 0 \n\t"
119 "add %0, %0, %1 \n\t"
120 "s32i %0, %2, 0 \n\t"
121 "wsr a15, "__stringify(PS)" \n\t"
131 static inline int atomic_sub_return(int i, atomic_t * v)
135 __asm__ __volatile__(
136 "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
137 "l32i %0, %2, 0 \n\t"
138 "sub %0, %0, %1 \n\t"
139 "s32i %0, %2, 0 \n\t"
140 "wsr a15, "__stringify(PS)" \n\t"
151 * atomic_sub_and_test - subtract value from variable and test result
152 * @i: integer value to subtract
153 * @v: pointer of type atomic_t
155 * Atomically subtracts @i from @v and returns
156 * true if the result is zero, or false for all
159 #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
162 * atomic_inc - increment atomic variable
163 * @v: pointer of type atomic_t
165 * Atomically increments @v by 1.
167 #define atomic_inc(v) atomic_add(1,(v))
170 * atomic_inc - increment atomic variable
171 * @v: pointer of type atomic_t
173 * Atomically increments @v by 1.
175 #define atomic_inc_return(v) atomic_add_return(1,(v))
178 * atomic_dec - decrement atomic variable
179 * @v: pointer of type atomic_t
181 * Atomically decrements @v by 1.
183 #define atomic_dec(v) atomic_sub(1,(v))
186 * atomic_dec_return - decrement atomic variable
187 * @v: pointer of type atomic_t
189 * Atomically decrements @v by 1.
191 #define atomic_dec_return(v) atomic_sub_return(1,(v))
194 * atomic_dec_and_test - decrement and test
195 * @v: pointer of type atomic_t
197 * Atomically decrements @v by 1 and
198 * returns true if the result is 0, or false for all other
201 #define atomic_dec_and_test(v) (atomic_sub_return(1,(v)) == 0)
204 * atomic_inc_and_test - increment and test
205 * @v: pointer of type atomic_t
207 * Atomically increments @v by 1
208 * and returns true if the result is zero, or false for all
211 #define atomic_inc_and_test(v) (atomic_add_return(1,(v)) == 0)
214 * atomic_add_negative - add and test if negative
215 * @v: pointer of type atomic_t
216 * @i: integer value to add
218 * Atomically adds @i to @v and returns true
219 * if the result is negative, or false when
220 * result is greater than or equal to zero.
222 #define atomic_add_negative(i,v) (atomic_add_return((i),(v)) < 0)
224 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
225 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
228 * atomic_add_unless - add unless the number is a given value
229 * @v: pointer of type atomic_t
230 * @a: the amount to add to v...
231 * @u: ...unless v is equal to u.
233 * Atomically adds @a to @v, so long as it was not @u.
234 * Returns non-zero if @v was not @u, and zero otherwise.
236 static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
241 if (unlikely(c == (u)))
243 old = atomic_cmpxchg((v), c, c + (a));
244 if (likely(old == c))
251 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
253 static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
255 unsigned int all_f = -1;
258 __asm__ __volatile__(
259 "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
260 "l32i %0, %2, 0 \n\t"
261 "xor %1, %4, %3 \n\t"
262 "and %0, %0, %4 \n\t"
263 "s32i %0, %2, 0 \n\t"
264 "wsr a15, "__stringify(PS)" \n\t"
266 : "=&a" (vval), "=a" (mask)
267 : "a" (v), "a" (all_f), "1" (mask)
272 static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
276 __asm__ __volatile__(
277 "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
278 "l32i %0, %2, 0 \n\t"
280 "s32i %0, %2, 0 \n\t"
281 "wsr a15, "__stringify(PS)" \n\t"
284 : "a" (mask), "a" (v)
289 /* Atomic operations are already serializing */
290 #define smp_mb__before_atomic_dec() barrier()
291 #define smp_mb__after_atomic_dec() barrier()
292 #define smp_mb__before_atomic_inc() barrier()
293 #define smp_mb__after_atomic_inc() barrier()
295 #include <asm-generic/atomic-long.h>
296 #endif /* __KERNEL__ */
298 #endif /* _XTENSA_ATOMIC_H */