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 <asm/assembler.h>
13 #include <asm/system.h>
16 * Atomic operations that C can't guarantee us. Useful for
17 * resource counting etc..
21 * Make sure gcc doesn't try to be clever and move things around
22 * on us. We need to use _exactly_ the address the user gave us,
23 * not some alias that contains the same information.
25 typedef struct { volatile int counter; } atomic_t;
27 #define ATOMIC_INIT(i) { (i) }
30 * atomic_read - read atomic variable
31 * @v: pointer of type atomic_t
33 * Atomically reads the value of @v.
35 #define atomic_read(v) ((v)->counter)
38 * atomic_set - set atomic variable
39 * @v: pointer of type atomic_t
42 * Atomically sets the value of @v to @i.
44 #define atomic_set(v,i) (((v)->counter) = (i))
47 * atomic_add_return - add integer to atomic variable and return it
48 * @i: integer value to add
49 * @v: pointer of type atomic_t
51 * Atomically adds @i to @v and return (@i + @v).
53 static __inline__ int atomic_add_return(int i, atomic_t *v)
58 local_irq_save(flags);
59 __asm__ __volatile__ (
60 "# atomic_add_return \n\t"
61 DCACHE_CLEAR("%0", "r4", "%1")
62 M32R_LOCK" %0, @%1; \n\t"
64 M32R_UNLOCK" %0, @%1; \n\t"
66 : "r" (&v->counter), "r" (i)
68 #ifdef CONFIG_CHIP_M32700_TS1
70 #endif /* CONFIG_CHIP_M32700_TS1 */
72 local_irq_restore(flags);
78 * atomic_sub_return - subtract integer from atomic variable and return it
79 * @i: integer value to subtract
80 * @v: pointer of type atomic_t
82 * Atomically subtracts @i from @v and return (@v - @i).
84 static __inline__ int atomic_sub_return(int i, atomic_t *v)
89 local_irq_save(flags);
90 __asm__ __volatile__ (
91 "# atomic_sub_return \n\t"
92 DCACHE_CLEAR("%0", "r4", "%1")
93 M32R_LOCK" %0, @%1; \n\t"
95 M32R_UNLOCK" %0, @%1; \n\t"
97 : "r" (&v->counter), "r" (i)
99 #ifdef CONFIG_CHIP_M32700_TS1
101 #endif /* CONFIG_CHIP_M32700_TS1 */
103 local_irq_restore(flags);
109 * atomic_add - add integer to atomic variable
110 * @i: integer value to add
111 * @v: pointer of type atomic_t
113 * Atomically adds @i to @v.
115 #define atomic_add(i,v) ((void) atomic_add_return((i), (v)))
118 * atomic_sub - subtract the atomic variable
119 * @i: integer value to subtract
120 * @v: pointer of type atomic_t
122 * Atomically subtracts @i from @v.
124 #define atomic_sub(i,v) ((void) atomic_sub_return((i), (v)))
127 * atomic_sub_and_test - subtract value from variable and test result
128 * @i: integer value to subtract
129 * @v: pointer of type atomic_t
131 * Atomically subtracts @i from @v and returns
132 * true if the result is zero, or false for all
135 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
138 * atomic_inc_return - increment atomic variable and return it
139 * @v: pointer of type atomic_t
141 * Atomically increments @v by 1 and returns the result.
143 static __inline__ int atomic_inc_return(atomic_t *v)
148 local_irq_save(flags);
149 __asm__ __volatile__ (
150 "# atomic_inc_return \n\t"
151 DCACHE_CLEAR("%0", "r4", "%1")
152 M32R_LOCK" %0, @%1; \n\t"
154 M32R_UNLOCK" %0, @%1; \n\t"
158 #ifdef CONFIG_CHIP_M32700_TS1
160 #endif /* CONFIG_CHIP_M32700_TS1 */
162 local_irq_restore(flags);
168 * atomic_dec_return - decrement atomic variable and return it
169 * @v: pointer of type atomic_t
171 * Atomically decrements @v by 1 and returns the result.
173 static __inline__ int atomic_dec_return(atomic_t *v)
178 local_irq_save(flags);
179 __asm__ __volatile__ (
180 "# atomic_dec_return \n\t"
181 DCACHE_CLEAR("%0", "r4", "%1")
182 M32R_LOCK" %0, @%1; \n\t"
184 M32R_UNLOCK" %0, @%1; \n\t"
188 #ifdef CONFIG_CHIP_M32700_TS1
190 #endif /* CONFIG_CHIP_M32700_TS1 */
192 local_irq_restore(flags);
198 * atomic_inc - increment atomic variable
199 * @v: pointer of type atomic_t
201 * Atomically increments @v by 1.
203 #define atomic_inc(v) ((void)atomic_inc_return(v))
206 * atomic_dec - decrement atomic variable
207 * @v: pointer of type atomic_t
209 * Atomically decrements @v by 1.
211 #define atomic_dec(v) ((void)atomic_dec_return(v))
214 * atomic_inc_and_test - increment and test
215 * @v: pointer of type atomic_t
217 * Atomically increments @v by 1
218 * and returns true if the result is zero, or false for all
221 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
224 * atomic_dec_and_test - decrement and test
225 * @v: pointer of type atomic_t
227 * Atomically decrements @v by 1 and
228 * returns true if the result is 0, or false for all
231 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
234 * atomic_add_negative - add and test if negative
235 * @v: pointer of type atomic_t
236 * @i: integer value to add
238 * Atomically adds @i to @v and returns true
239 * if the result is negative, or false when
240 * result is greater than or equal to zero.
242 #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
244 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
245 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
248 * atomic_add_unless - add unless the number is a given value
249 * @v: pointer of type atomic_t
250 * @a: the amount to add to v...
251 * @u: ...unless v is equal to u.
253 * Atomically adds @a to @v, so long as it was not @u.
254 * Returns non-zero if @v was not @u, and zero otherwise.
256 #define atomic_add_unless(v, a, u) \
259 c = atomic_read(v); \
260 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
264 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
266 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t *addr)
271 local_irq_save(flags);
272 __asm__ __volatile__ (
273 "# atomic_clear_mask \n\t"
274 DCACHE_CLEAR("%0", "r5", "%1")
275 M32R_LOCK" %0, @%1; \n\t"
277 M32R_UNLOCK" %0, @%1; \n\t"
279 : "r" (addr), "r" (~mask)
281 #ifdef CONFIG_CHIP_M32700_TS1
283 #endif /* CONFIG_CHIP_M32700_TS1 */
285 local_irq_restore(flags);
288 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t *addr)
293 local_irq_save(flags);
294 __asm__ __volatile__ (
295 "# atomic_set_mask \n\t"
296 DCACHE_CLEAR("%0", "r5", "%1")
297 M32R_LOCK" %0, @%1; \n\t"
299 M32R_UNLOCK" %0, @%1; \n\t"
301 : "r" (addr), "r" (mask)
303 #ifdef CONFIG_CHIP_M32700_TS1
305 #endif /* CONFIG_CHIP_M32700_TS1 */
307 local_irq_restore(flags);
310 /* Atomic operations are already serializing on m32r */
311 #define smp_mb__before_atomic_dec() barrier()
312 #define smp_mb__after_atomic_dec() barrier()
313 #define smp_mb__before_atomic_inc() barrier()
314 #define smp_mb__after_atomic_inc() barrier()
316 #include <asm-generic/atomic.h>
317 #endif /* _ASM_M32R_ATOMIC_H */