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/types.h>
13 #include <asm/assembler.h>
14 #include <asm/system.h>
17 * Atomic operations that C can't guarantee us. Useful for
18 * resource counting etc..
21 #define ATOMIC_INIT(i) { (i) }
24 * atomic_read - read atomic variable
25 * @v: pointer of type atomic_t
27 * Atomically reads the value of @v.
29 #define atomic_read(v) ((v)->counter)
32 * atomic_set - set atomic variable
33 * @v: pointer of type atomic_t
36 * Atomically sets the value of @v to @i.
38 #define atomic_set(v,i) (((v)->counter) = (i))
41 * atomic_add_return - add integer to atomic variable and return it
42 * @i: integer value to add
43 * @v: pointer of type atomic_t
45 * Atomically adds @i to @v and return (@i + @v).
47 static __inline__ int atomic_add_return(int i, atomic_t *v)
52 local_irq_save(flags);
53 __asm__ __volatile__ (
54 "# atomic_add_return \n\t"
55 DCACHE_CLEAR("%0", "r4", "%1")
56 M32R_LOCK" %0, @%1; \n\t"
58 M32R_UNLOCK" %0, @%1; \n\t"
60 : "r" (&v->counter), "r" (i)
62 #ifdef CONFIG_CHIP_M32700_TS1
64 #endif /* CONFIG_CHIP_M32700_TS1 */
66 local_irq_restore(flags);
72 * atomic_sub_return - subtract integer from atomic variable and return it
73 * @i: integer value to subtract
74 * @v: pointer of type atomic_t
76 * Atomically subtracts @i from @v and return (@v - @i).
78 static __inline__ int atomic_sub_return(int i, atomic_t *v)
83 local_irq_save(flags);
84 __asm__ __volatile__ (
85 "# atomic_sub_return \n\t"
86 DCACHE_CLEAR("%0", "r4", "%1")
87 M32R_LOCK" %0, @%1; \n\t"
89 M32R_UNLOCK" %0, @%1; \n\t"
91 : "r" (&v->counter), "r" (i)
93 #ifdef CONFIG_CHIP_M32700_TS1
95 #endif /* CONFIG_CHIP_M32700_TS1 */
97 local_irq_restore(flags);
103 * atomic_add - add integer to atomic variable
104 * @i: integer value to add
105 * @v: pointer of type atomic_t
107 * Atomically adds @i to @v.
109 #define atomic_add(i,v) ((void) atomic_add_return((i), (v)))
112 * atomic_sub - subtract the atomic variable
113 * @i: integer value to subtract
114 * @v: pointer of type atomic_t
116 * Atomically subtracts @i from @v.
118 #define atomic_sub(i,v) ((void) atomic_sub_return((i), (v)))
121 * atomic_sub_and_test - subtract value from variable and test result
122 * @i: integer value to subtract
123 * @v: pointer of type atomic_t
125 * Atomically subtracts @i from @v and returns
126 * true if the result is zero, or false for all
129 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
132 * atomic_inc_return - increment atomic variable and return it
133 * @v: pointer of type atomic_t
135 * Atomically increments @v by 1 and returns the result.
137 static __inline__ int atomic_inc_return(atomic_t *v)
142 local_irq_save(flags);
143 __asm__ __volatile__ (
144 "# atomic_inc_return \n\t"
145 DCACHE_CLEAR("%0", "r4", "%1")
146 M32R_LOCK" %0, @%1; \n\t"
148 M32R_UNLOCK" %0, @%1; \n\t"
152 #ifdef CONFIG_CHIP_M32700_TS1
154 #endif /* CONFIG_CHIP_M32700_TS1 */
156 local_irq_restore(flags);
162 * atomic_dec_return - decrement atomic variable and return it
163 * @v: pointer of type atomic_t
165 * Atomically decrements @v by 1 and returns the result.
167 static __inline__ int atomic_dec_return(atomic_t *v)
172 local_irq_save(flags);
173 __asm__ __volatile__ (
174 "# atomic_dec_return \n\t"
175 DCACHE_CLEAR("%0", "r4", "%1")
176 M32R_LOCK" %0, @%1; \n\t"
178 M32R_UNLOCK" %0, @%1; \n\t"
182 #ifdef CONFIG_CHIP_M32700_TS1
184 #endif /* CONFIG_CHIP_M32700_TS1 */
186 local_irq_restore(flags);
192 * atomic_inc - increment atomic variable
193 * @v: pointer of type atomic_t
195 * Atomically increments @v by 1.
197 #define atomic_inc(v) ((void)atomic_inc_return(v))
200 * atomic_dec - decrement atomic variable
201 * @v: pointer of type atomic_t
203 * Atomically decrements @v by 1.
205 #define atomic_dec(v) ((void)atomic_dec_return(v))
208 * atomic_inc_and_test - increment and test
209 * @v: pointer of type atomic_t
211 * Atomically increments @v by 1
212 * and returns true if the result is zero, or false for all
215 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
218 * atomic_dec_and_test - decrement and test
219 * @v: pointer of type atomic_t
221 * Atomically decrements @v by 1 and
222 * returns true if the result is 0, or false for all
225 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
228 * atomic_add_negative - add and test if negative
229 * @v: pointer of type atomic_t
230 * @i: integer value to add
232 * Atomically adds @i to @v and returns true
233 * if the result is negative, or false when
234 * result is greater than or equal to zero.
236 #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
238 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
239 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
242 * atomic_add_unless - add unless the number is a given value
243 * @v: pointer of type atomic_t
244 * @a: the amount to add to v...
245 * @u: ...unless v is equal to u.
247 * Atomically adds @a to @v, so long as it was not @u.
248 * Returns non-zero if @v was not @u, and zero otherwise.
250 static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
255 if (unlikely(c == (u)))
257 old = atomic_cmpxchg((v), c, c + (a));
258 if (likely(old == c))
265 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
267 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t *addr)
272 local_irq_save(flags);
273 __asm__ __volatile__ (
274 "# atomic_clear_mask \n\t"
275 DCACHE_CLEAR("%0", "r5", "%1")
276 M32R_LOCK" %0, @%1; \n\t"
278 M32R_UNLOCK" %0, @%1; \n\t"
280 : "r" (addr), "r" (~mask)
282 #ifdef CONFIG_CHIP_M32700_TS1
284 #endif /* CONFIG_CHIP_M32700_TS1 */
286 local_irq_restore(flags);
289 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t *addr)
294 local_irq_save(flags);
295 __asm__ __volatile__ (
296 "# atomic_set_mask \n\t"
297 DCACHE_CLEAR("%0", "r5", "%1")
298 M32R_LOCK" %0, @%1; \n\t"
300 M32R_UNLOCK" %0, @%1; \n\t"
302 : "r" (addr), "r" (mask)
304 #ifdef CONFIG_CHIP_M32700_TS1
306 #endif /* CONFIG_CHIP_M32700_TS1 */
308 local_irq_restore(flags);
311 /* Atomic operations are already serializing on m32r */
312 #define smp_mb__before_atomic_dec() barrier()
313 #define smp_mb__after_atomic_dec() barrier()
314 #define smp_mb__before_atomic_inc() barrier()
315 #define smp_mb__after_atomic_inc() barrier()
317 #include <asm-generic/atomic.h>
318 #endif /* _ASM_M32R_ATOMIC_H */