Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
atomic.h
Go to the documentation of this file.
1 /*
2  * include/asm-xtensa/atomic.h
3  *
4  * Atomic operations that C can't guarantee us. Useful for resource counting..
5  *
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
8  * for more details.
9  *
10  * Copyright (C) 2001 - 2005 Tensilica Inc.
11  */
12 
13 #ifndef _XTENSA_ATOMIC_H
14 #define _XTENSA_ATOMIC_H
15 
16 #include <linux/stringify.h>
17 #include <linux/types.h>
18 
19 #ifdef __KERNEL__
20 #include <asm/processor.h>
21 #include <asm/cmpxchg.h>
22 
23 #define ATOMIC_INIT(i) { (i) }
24 
25 /*
26  * This Xtensa implementation assumes that the right mechanism
27  * for exclusion is for locking interrupts to level 1.
28  *
29  * Locking interrupts looks like this:
30  *
31  * rsil a15, 1
32  * <code>
33  * wsr a15, PS
34  * rsync
35  *
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.
41  */
42 
49 #define atomic_read(v) (*(volatile int *)&(v)->counter)
50 
58 #define atomic_set(v,i) ((v)->counter = (i))
59 
67 static inline void atomic_add(int i, atomic_t * v)
68 {
69  unsigned int vval;
70 
71  __asm__ __volatile__(
72  "rsil a15, "__stringify(LOCKLEVEL)"\n\t"
73  "l32i %0, %2, 0 \n\t"
74  "add %0, %0, %1 \n\t"
75  "s32i %0, %2, 0 \n\t"
76  "wsr a15, ps \n\t"
77  "rsync \n"
78  : "=&a" (vval)
79  : "a" (i), "a" (v)
80  : "a15", "memory"
81  );
82 }
83 
91 static inline void atomic_sub(int i, atomic_t *v)
92 {
93  unsigned int vval;
94 
95  __asm__ __volatile__(
96  "rsil a15, "__stringify(LOCKLEVEL)"\n\t"
97  "l32i %0, %2, 0 \n\t"
98  "sub %0, %0, %1 \n\t"
99  "s32i %0, %2, 0 \n\t"
100  "wsr a15, ps \n\t"
101  "rsync \n"
102  : "=&a" (vval)
103  : "a" (i), "a" (v)
104  : "a15", "memory"
105  );
106 }
107 
108 /*
109  * We use atomic_{add|sub}_return to define other functions.
110  */
111 
112 static inline int atomic_add_return(int i, atomic_t * v)
113 {
114  unsigned int vval;
115 
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, ps \n\t"
122  "rsync \n"
123  : "=&a" (vval)
124  : "a" (i), "a" (v)
125  : "a15", "memory"
126  );
127 
128  return vval;
129 }
130 
131 static inline int atomic_sub_return(int i, atomic_t * v)
132 {
133  unsigned int vval;
134 
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, ps \n\t"
141  "rsync \n"
142  : "=&a" (vval)
143  : "a" (i), "a" (v)
144  : "a15", "memory"
145  );
146 
147  return vval;
148 }
149 
159 #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
160 
167 #define atomic_inc(v) atomic_add(1,(v))
168 
175 #define atomic_inc_return(v) atomic_add_return(1,(v))
176 
183 #define atomic_dec(v) atomic_sub(1,(v))
184 
191 #define atomic_dec_return(v) atomic_sub_return(1,(v))
192 
201 #define atomic_dec_and_test(v) (atomic_sub_return(1,(v)) == 0)
202 
211 #define atomic_inc_and_test(v) (atomic_add_return(1,(v)) == 0)
212 
222 #define atomic_add_negative(i,v) (atomic_add_return((i),(v)) < 0)
223 
224 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
225 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
226 
236 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
237 {
238  int c, old;
239  c = atomic_read(v);
240  for (;;) {
241  if (unlikely(c == (u)))
242  break;
243  old = atomic_cmpxchg((v), c, c + (a));
244  if (likely(old == c))
245  break;
246  c = old;
247  }
248  return c;
249 }
250 
251 
252 static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
253 {
254  unsigned int all_f = -1;
255  unsigned int vval;
256 
257  __asm__ __volatile__(
258  "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
259  "l32i %0, %2, 0 \n\t"
260  "xor %1, %4, %3 \n\t"
261  "and %0, %0, %4 \n\t"
262  "s32i %0, %2, 0 \n\t"
263  "wsr a15, ps \n\t"
264  "rsync \n"
265  : "=&a" (vval), "=a" (mask)
266  : "a" (v), "a" (all_f), "1" (mask)
267  : "a15", "memory"
268  );
269 }
270 
271 static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
272 {
273  unsigned int vval;
274 
275  __asm__ __volatile__(
276  "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
277  "l32i %0, %2, 0 \n\t"
278  "or %0, %0, %1 \n\t"
279  "s32i %0, %2, 0 \n\t"
280  "wsr a15, ps \n\t"
281  "rsync \n"
282  : "=&a" (vval)
283  : "a" (mask), "a" (v)
284  : "a15", "memory"
285  );
286 }
287 
288 /* Atomic operations are already serializing */
289 #define smp_mb__before_atomic_dec() barrier()
290 #define smp_mb__after_atomic_dec() barrier()
291 #define smp_mb__before_atomic_inc() barrier()
292 #define smp_mb__after_atomic_inc() barrier()
293 
294 #endif /* __KERNEL__ */
295 
296 #endif /* _XTENSA_ATOMIC_H */
297