1 #ifndef _ASM_M32R_ATOMIC_H
2 #define _ASM_M32R_ATOMIC_H
5 * linux/include/asm-m32r/atomic.h
8 * Copyright (C) 2001, 2002 Hitoshi Yamamoto
9 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
12 #include <linux/config.h>
13 #include <asm/system.h>
16 * Atomic operations that C can't guarantee us. Useful for
17 * resource counting etc..
24 #define STORE "unlock"
31 * Make sure gcc doesn't try to be clever and move things around
32 * on us. We need to use _exactly_ the address the user gave us,
33 * not some alias that contains the same information.
35 typedef struct { volatile int counter; } atomic_t;
37 #define ATOMIC_INIT(i) { (i) }
40 * atomic_read - read atomic variable
41 * @v: pointer of type atomic_t
43 * Atomically reads the value of @v.
45 #define atomic_read(v) ((v)->counter)
48 * atomic_set - set atomic variable
49 * @v: pointer of type atomic_t
52 * Atomically sets the value of @v to @i.
54 #define atomic_set(v,i) (((v)->counter) = (i))
57 * atomic_add_return - add integer to atomic variable and return it
58 * @i: integer value to add
59 * @v: pointer of type atomic_t
61 * Atomically adds @i to @v and return (@i + @v).
63 static inline int atomic_add_return(int i, atomic_t *v)
68 local_irq_save(flags);
69 __asm__ __volatile__ (
70 "# atomic_add_return \n\t"
71 DCACHE_CLEAR("%0", "r4", "%1")
76 : "r" (&v->counter), "r" (i)
78 #ifdef CONFIG_CHIP_M32700_TS1
80 #endif /* CONFIG_CHIP_M32700_TS1 */
82 local_irq_restore(flags);
88 * atomic_sub_return - subtract integer from atomic variable and return it
89 * @i: integer value to subtract
90 * @v: pointer of type atomic_t
92 * Atomically subtracts @i from @v and return (@v - @i).
94 static inline int atomic_sub_return(int i, atomic_t *v)
99 local_irq_save(flags);
100 __asm__ __volatile__ (
101 "# atomic_sub_return \n\t"
102 DCACHE_CLEAR("%0", "r4", "%1")
105 STORE" %0, @%1; \n\t"
107 : "r" (&v->counter), "r" (i)
109 #ifdef CONFIG_CHIP_M32700_TS1
111 #endif /* CONFIG_CHIP_M32700_TS1 */
113 local_irq_restore(flags);
119 * atomic_add - add integer to atomic variable
120 * @i: integer value to add
121 * @v: pointer of type atomic_t
123 * Atomically adds @i to @v.
125 #define atomic_add(i,v) ((void) atomic_add_return((i), (v)))
128 * atomic_sub - subtract the atomic variable
129 * @i: integer value to subtract
130 * @v: pointer of type atomic_t
132 * Atomically subtracts @i from @v.
134 #define atomic_sub(i,v) ((void) atomic_sub_return((i), (v)))
137 * atomic_sub_and_test - subtract value from variable and test result
138 * @i: integer value to subtract
139 * @v: pointer of type atomic_t
141 * Atomically subtracts @i from @v and returns
142 * true if the result is zero, or false for all
145 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
148 * atomic_inc_return - increment atomic variable and return it
149 * @v: pointer of type atomic_t
151 * Atomically increments @v by 1 and returns the result.
153 static inline int atomic_inc_return(atomic_t *v)
158 local_irq_save(flags);
159 __asm__ __volatile__ (
160 "# atomic_inc_return \n\t"
161 DCACHE_CLEAR("%0", "r4", "%1")
164 STORE" %0, @%1; \n\t"
168 #ifdef CONFIG_CHIP_M32700_TS1
170 #endif /* CONFIG_CHIP_M32700_TS1 */
172 local_irq_restore(flags);
178 * atomic_dec_return - decrement atomic variable and return it
179 * @v: pointer of type atomic_t
181 * Atomically decrements @v by 1 and returns the result.
183 static inline int atomic_dec_return(atomic_t *v)
188 local_irq_save(flags);
189 __asm__ __volatile__ (
190 "# atomic_dec_return \n\t"
191 DCACHE_CLEAR("%0", "r4", "%1")
194 STORE" %0, @%1; \n\t"
198 #ifdef CONFIG_CHIP_M32700_TS1
200 #endif /* CONFIG_CHIP_M32700_TS1 */
202 local_irq_restore(flags);
208 * atomic_inc - increment atomic variable
209 * @v: pointer of type atomic_t
211 * Atomically increments @v by 1.
213 #define atomic_inc(v) ((void)atomic_inc_return(v))
216 * atomic_dec - decrement atomic variable
217 * @v: pointer of type atomic_t
219 * Atomically decrements @v by 1.
221 #define atomic_dec(v) ((void)atomic_dec_return(v))
224 * atomic_inc_and_test - increment and test
225 * @v: pointer of type atomic_t
227 * Atomically increments @v by 1
228 * and returns true if the result is zero, or false for all
231 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
234 * atomic_dec_and_test - decrement and test
235 * @v: pointer of type atomic_t
237 * Atomically decrements @v by 1 and
238 * returns true if the result is 0, or false for all
241 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
244 * atomic_add_negative - add and test if negative
245 * @v: pointer of type atomic_t
246 * @i: integer value to add
248 * Atomically adds @i to @v and returns true
249 * if the result is negative, or false when
250 * result is greater than or equal to zero.
252 #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
254 static inline void atomic_clear_mask(unsigned long mask, atomic_t *addr)
259 local_irq_save(flags);
260 __asm__ __volatile__ (
261 "# atomic_clear_mask \n\t"
262 DCACHE_CLEAR("%0", "r5", "%1")
265 STORE" %0, @%1; \n\t"
267 : "r" (addr), "r" (~mask)
269 #ifdef CONFIG_CHIP_M32700_TS1
271 #endif /* CONFIG_CHIP_M32700_TS1 */
273 local_irq_restore(flags);
276 static inline void atomic_set_mask(unsigned long mask, atomic_t *addr)
281 local_irq_save(flags);
282 __asm__ __volatile__ (
283 "# atomic_set_mask \n\t"
284 DCACHE_CLEAR("%0", "r5", "%1")
287 STORE" %0, @%1; \n\t"
289 : "r" (addr), "r" (mask)
291 #ifdef CONFIG_CHIP_M32700_TS1
293 #endif /* CONFIG_CHIP_M32700_TS1 */
295 local_irq_restore(flags);
298 /* Atomic operations are already serializing on m32r */
299 #define smp_mb__before_atomic_dec() barrier()
300 #define smp_mb__after_atomic_dec() barrier()
301 #define smp_mb__before_atomic_inc() barrier()
302 #define smp_mb__after_atomic_inc() barrier()
304 #endif /* _ASM_M32R_ATOMIC_H */