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/cmpxchg.h>
15 #include <asm/dcache_clear.h>
16 #include <asm/barrier.h>
19 * Atomic operations that C can't guarantee us. Useful for
20 * resource counting etc..
23 #define ATOMIC_INIT(i) { (i) }
26 * atomic_read - read atomic variable
27 * @v: pointer of type atomic_t
29 * Atomically reads the value of @v.
31 #define atomic_read(v) (*(volatile int *)&(v)->counter)
34 * atomic_set - set atomic variable
35 * @v: pointer of type atomic_t
38 * Atomically sets the value of @v to @i.
40 #define atomic_set(v,i) (((v)->counter) = (i))
43 * atomic_add_return - add integer to atomic variable and return it
44 * @i: integer value to add
45 * @v: pointer of type atomic_t
47 * Atomically adds @i to @v and return (@i + @v).
49 static __inline__ int atomic_add_return(int i, atomic_t *v)
54 local_irq_save(flags);
55 __asm__ __volatile__ (
56 "# atomic_add_return \n\t"
57 DCACHE_CLEAR("%0", "r4", "%1")
58 M32R_LOCK" %0, @%1; \n\t"
60 M32R_UNLOCK" %0, @%1; \n\t"
62 : "r" (&v->counter), "r" (i)
64 #ifdef CONFIG_CHIP_M32700_TS1
66 #endif /* CONFIG_CHIP_M32700_TS1 */
68 local_irq_restore(flags);
74 * atomic_sub_return - subtract integer from atomic variable and return it
75 * @i: integer value to subtract
76 * @v: pointer of type atomic_t
78 * Atomically subtracts @i from @v and return (@v - @i).
80 static __inline__ int atomic_sub_return(int i, atomic_t *v)
85 local_irq_save(flags);
86 __asm__ __volatile__ (
87 "# atomic_sub_return \n\t"
88 DCACHE_CLEAR("%0", "r4", "%1")
89 M32R_LOCK" %0, @%1; \n\t"
91 M32R_UNLOCK" %0, @%1; \n\t"
93 : "r" (&v->counter), "r" (i)
95 #ifdef CONFIG_CHIP_M32700_TS1
97 #endif /* CONFIG_CHIP_M32700_TS1 */
99 local_irq_restore(flags);
105 * atomic_add - add integer to atomic variable
106 * @i: integer value to add
107 * @v: pointer of type atomic_t
109 * Atomically adds @i to @v.
111 #define atomic_add(i,v) ((void) atomic_add_return((i), (v)))
114 * atomic_sub - subtract the atomic variable
115 * @i: integer value to subtract
116 * @v: pointer of type atomic_t
118 * Atomically subtracts @i from @v.
120 #define atomic_sub(i,v) ((void) atomic_sub_return((i), (v)))
123 * atomic_sub_and_test - subtract value from variable and test result
124 * @i: integer value to subtract
125 * @v: pointer of type atomic_t
127 * Atomically subtracts @i from @v and returns
128 * true if the result is zero, or false for all
131 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
134 * atomic_inc_return - increment atomic variable and return it
135 * @v: pointer of type atomic_t
137 * Atomically increments @v by 1 and returns the result.
139 static __inline__ int atomic_inc_return(atomic_t *v)
144 local_irq_save(flags);
145 __asm__ __volatile__ (
146 "# atomic_inc_return \n\t"
147 DCACHE_CLEAR("%0", "r4", "%1")
148 M32R_LOCK" %0, @%1; \n\t"
150 M32R_UNLOCK" %0, @%1; \n\t"
154 #ifdef CONFIG_CHIP_M32700_TS1
156 #endif /* CONFIG_CHIP_M32700_TS1 */
158 local_irq_restore(flags);
164 * atomic_dec_return - decrement atomic variable and return it
165 * @v: pointer of type atomic_t
167 * Atomically decrements @v by 1 and returns the result.
169 static __inline__ int atomic_dec_return(atomic_t *v)
174 local_irq_save(flags);
175 __asm__ __volatile__ (
176 "# atomic_dec_return \n\t"
177 DCACHE_CLEAR("%0", "r4", "%1")
178 M32R_LOCK" %0, @%1; \n\t"
180 M32R_UNLOCK" %0, @%1; \n\t"
184 #ifdef CONFIG_CHIP_M32700_TS1
186 #endif /* CONFIG_CHIP_M32700_TS1 */
188 local_irq_restore(flags);
194 * atomic_inc - increment atomic variable
195 * @v: pointer of type atomic_t
197 * Atomically increments @v by 1.
199 #define atomic_inc(v) ((void)atomic_inc_return(v))
202 * atomic_dec - decrement atomic variable
203 * @v: pointer of type atomic_t
205 * Atomically decrements @v by 1.
207 #define atomic_dec(v) ((void)atomic_dec_return(v))
210 * atomic_inc_and_test - increment and test
211 * @v: pointer of type atomic_t
213 * Atomically increments @v by 1
214 * and returns true if the result is zero, or false for all
217 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
220 * atomic_dec_and_test - decrement and test
221 * @v: pointer of type atomic_t
223 * Atomically decrements @v by 1 and
224 * returns true if the result is 0, or false for all
227 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
230 * atomic_add_negative - add and test if negative
231 * @v: pointer of type atomic_t
232 * @i: integer value to add
234 * Atomically adds @i to @v and returns true
235 * if the result is negative, or false when
236 * result is greater than or equal to zero.
238 #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
240 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
241 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
244 * __atomic_add_unless - add unless the number is a given value
245 * @v: pointer of type atomic_t
246 * @a: the amount to add to v...
247 * @u: ...unless v is equal to u.
249 * Atomically adds @a to @v, so long as it was not @u.
250 * Returns the old value of @v.
252 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
257 if (unlikely(c == (u)))
259 old = atomic_cmpxchg((v), c, c + (a));
260 if (likely(old == c))
268 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t *addr)
273 local_irq_save(flags);
274 __asm__ __volatile__ (
275 "# atomic_clear_mask \n\t"
276 DCACHE_CLEAR("%0", "r5", "%1")
277 M32R_LOCK" %0, @%1; \n\t"
279 M32R_UNLOCK" %0, @%1; \n\t"
281 : "r" (addr), "r" (~mask)
283 #ifdef CONFIG_CHIP_M32700_TS1
285 #endif /* CONFIG_CHIP_M32700_TS1 */
287 local_irq_restore(flags);
290 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t *addr)
295 local_irq_save(flags);
296 __asm__ __volatile__ (
297 "# atomic_set_mask \n\t"
298 DCACHE_CLEAR("%0", "r5", "%1")
299 M32R_LOCK" %0, @%1; \n\t"
301 M32R_UNLOCK" %0, @%1; \n\t"
303 : "r" (addr), "r" (mask)
305 #ifdef CONFIG_CHIP_M32700_TS1
307 #endif /* CONFIG_CHIP_M32700_TS1 */
309 local_irq_restore(flags);
312 #endif /* _ASM_M32R_ATOMIC_H */