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/assembler.h>
14 #include <asm/system.h>
17 * Atomic operations that C can't guarantee us. Useful for
18 * resource counting etc..
22 * Make sure gcc doesn't try to be clever and move things around
23 * on us. We need to use _exactly_ the address the user gave us,
24 * not some alias that contains the same information.
26 typedef struct { volatile int counter; } atomic_t;
28 #define ATOMIC_INIT(i) { (i) }
31 * atomic_read - read atomic variable
32 * @v: pointer of type atomic_t
34 * Atomically reads the value of @v.
36 #define atomic_read(v) ((v)->counter)
39 * atomic_set - set atomic variable
40 * @v: pointer of type atomic_t
43 * Atomically sets the value of @v to @i.
45 #define atomic_set(v,i) (((v)->counter) = (i))
48 * atomic_add_return - add integer to atomic variable and return it
49 * @i: integer value to add
50 * @v: pointer of type atomic_t
52 * Atomically adds @i to @v and return (@i + @v).
54 static __inline__ int atomic_add_return(int i, atomic_t *v)
59 local_irq_save(flags);
60 __asm__ __volatile__ (
61 "# atomic_add_return \n\t"
62 DCACHE_CLEAR("%0", "r4", "%1")
63 M32R_LOCK" %0, @%1; \n\t"
65 M32R_UNLOCK" %0, @%1; \n\t"
67 : "r" (&v->counter), "r" (i)
69 #ifdef CONFIG_CHIP_M32700_TS1
71 #endif /* CONFIG_CHIP_M32700_TS1 */
73 local_irq_restore(flags);
79 * atomic_sub_return - subtract integer from atomic variable and return it
80 * @i: integer value to subtract
81 * @v: pointer of type atomic_t
83 * Atomically subtracts @i from @v and return (@v - @i).
85 static __inline__ int atomic_sub_return(int i, atomic_t *v)
90 local_irq_save(flags);
91 __asm__ __volatile__ (
92 "# atomic_sub_return \n\t"
93 DCACHE_CLEAR("%0", "r4", "%1")
94 M32R_LOCK" %0, @%1; \n\t"
96 M32R_UNLOCK" %0, @%1; \n\t"
98 : "r" (&v->counter), "r" (i)
100 #ifdef CONFIG_CHIP_M32700_TS1
102 #endif /* CONFIG_CHIP_M32700_TS1 */
104 local_irq_restore(flags);
110 * atomic_add - add integer to atomic variable
111 * @i: integer value to add
112 * @v: pointer of type atomic_t
114 * Atomically adds @i to @v.
116 #define atomic_add(i,v) ((void) atomic_add_return((i), (v)))
119 * atomic_sub - subtract the atomic variable
120 * @i: integer value to subtract
121 * @v: pointer of type atomic_t
123 * Atomically subtracts @i from @v.
125 #define atomic_sub(i,v) ((void) atomic_sub_return((i), (v)))
128 * atomic_sub_and_test - subtract value from variable and test result
129 * @i: integer value to subtract
130 * @v: pointer of type atomic_t
132 * Atomically subtracts @i from @v and returns
133 * true if the result is zero, or false for all
136 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
139 * atomic_inc_return - increment atomic variable and return it
140 * @v: pointer of type atomic_t
142 * Atomically increments @v by 1 and returns the result.
144 static __inline__ int atomic_inc_return(atomic_t *v)
149 local_irq_save(flags);
150 __asm__ __volatile__ (
151 "# atomic_inc_return \n\t"
152 DCACHE_CLEAR("%0", "r4", "%1")
153 M32R_LOCK" %0, @%1; \n\t"
155 M32R_UNLOCK" %0, @%1; \n\t"
159 #ifdef CONFIG_CHIP_M32700_TS1
161 #endif /* CONFIG_CHIP_M32700_TS1 */
163 local_irq_restore(flags);
169 * atomic_dec_return - decrement atomic variable and return it
170 * @v: pointer of type atomic_t
172 * Atomically decrements @v by 1 and returns the result.
174 static __inline__ int atomic_dec_return(atomic_t *v)
179 local_irq_save(flags);
180 __asm__ __volatile__ (
181 "# atomic_dec_return \n\t"
182 DCACHE_CLEAR("%0", "r4", "%1")
183 M32R_LOCK" %0, @%1; \n\t"
185 M32R_UNLOCK" %0, @%1; \n\t"
189 #ifdef CONFIG_CHIP_M32700_TS1
191 #endif /* CONFIG_CHIP_M32700_TS1 */
193 local_irq_restore(flags);
199 * atomic_inc - increment atomic variable
200 * @v: pointer of type atomic_t
202 * Atomically increments @v by 1.
204 #define atomic_inc(v) ((void)atomic_inc_return(v))
207 * atomic_dec - decrement atomic variable
208 * @v: pointer of type atomic_t
210 * Atomically decrements @v by 1.
212 #define atomic_dec(v) ((void)atomic_dec_return(v))
215 * atomic_inc_and_test - increment and test
216 * @v: pointer of type atomic_t
218 * Atomically increments @v by 1
219 * and returns true if the result is zero, or false for all
222 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
225 * atomic_dec_and_test - decrement and test
226 * @v: pointer of type atomic_t
228 * Atomically decrements @v by 1 and
229 * returns true if the result is 0, or false for all
232 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
235 * atomic_add_negative - add and test if negative
236 * @v: pointer of type atomic_t
237 * @i: integer value to add
239 * Atomically adds @i to @v and returns true
240 * if the result is negative, or false when
241 * result is greater than or equal to zero.
243 #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
245 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
246 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
249 * atomic_add_unless - add unless the number is a given value
250 * @v: pointer of type atomic_t
251 * @a: the amount to add to v...
252 * @u: ...unless v is equal to u.
254 * Atomically adds @a to @v, so long as it was not @u.
255 * Returns non-zero if @v was not @u, and zero otherwise.
257 #define atomic_add_unless(v, a, u) \
260 c = atomic_read(v); \
261 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != 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 */