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>
18 * Atomic operations that C can't guarantee us. Useful for
19 * resource counting etc..
22 #define ATOMIC_INIT(i) { (i) }
25 * atomic_read - read atomic variable
26 * @v: pointer of type atomic_t
28 * Atomically reads the value of @v.
30 #define atomic_read(v) (*(volatile int *)&(v)->counter)
33 * atomic_set - set atomic variable
34 * @v: pointer of type atomic_t
37 * Atomically sets the value of @v to @i.
39 #define atomic_set(v,i) (((v)->counter) = (i))
42 * atomic_add_return - add integer to atomic variable and return it
43 * @i: integer value to add
44 * @v: pointer of type atomic_t
46 * Atomically adds @i to @v and return (@i + @v).
48 static __inline__
int atomic_add_return(int i
, atomic_t
*v
)
53 local_irq_save(flags
);
54 __asm__
__volatile__ (
55 "# atomic_add_return \n\t"
56 DCACHE_CLEAR("%0", "r4", "%1")
57 M32R_LOCK
" %0, @%1; \n\t"
59 M32R_UNLOCK
" %0, @%1; \n\t"
61 : "r" (&v
->counter
), "r" (i
)
63 #ifdef CONFIG_CHIP_M32700_TS1
65 #endif /* CONFIG_CHIP_M32700_TS1 */
67 local_irq_restore(flags
);
73 * atomic_sub_return - subtract integer from atomic variable and return it
74 * @i: integer value to subtract
75 * @v: pointer of type atomic_t
77 * Atomically subtracts @i from @v and return (@v - @i).
79 static __inline__
int atomic_sub_return(int i
, atomic_t
*v
)
84 local_irq_save(flags
);
85 __asm__
__volatile__ (
86 "# atomic_sub_return \n\t"
87 DCACHE_CLEAR("%0", "r4", "%1")
88 M32R_LOCK
" %0, @%1; \n\t"
90 M32R_UNLOCK
" %0, @%1; \n\t"
92 : "r" (&v
->counter
), "r" (i
)
94 #ifdef CONFIG_CHIP_M32700_TS1
96 #endif /* CONFIG_CHIP_M32700_TS1 */
98 local_irq_restore(flags
);
104 * atomic_add - add integer to atomic variable
105 * @i: integer value to add
106 * @v: pointer of type atomic_t
108 * Atomically adds @i to @v.
110 #define atomic_add(i,v) ((void) atomic_add_return((i), (v)))
113 * atomic_sub - subtract the atomic variable
114 * @i: integer value to subtract
115 * @v: pointer of type atomic_t
117 * Atomically subtracts @i from @v.
119 #define atomic_sub(i,v) ((void) atomic_sub_return((i), (v)))
122 * atomic_sub_and_test - subtract value from variable and test result
123 * @i: integer value to subtract
124 * @v: pointer of type atomic_t
126 * Atomically subtracts @i from @v and returns
127 * true if the result is zero, or false for all
130 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
133 * atomic_inc_return - increment atomic variable and return it
134 * @v: pointer of type atomic_t
136 * Atomically increments @v by 1 and returns the result.
138 static __inline__
int atomic_inc_return(atomic_t
*v
)
143 local_irq_save(flags
);
144 __asm__
__volatile__ (
145 "# atomic_inc_return \n\t"
146 DCACHE_CLEAR("%0", "r4", "%1")
147 M32R_LOCK
" %0, @%1; \n\t"
149 M32R_UNLOCK
" %0, @%1; \n\t"
153 #ifdef CONFIG_CHIP_M32700_TS1
155 #endif /* CONFIG_CHIP_M32700_TS1 */
157 local_irq_restore(flags
);
163 * atomic_dec_return - decrement atomic variable and return it
164 * @v: pointer of type atomic_t
166 * Atomically decrements @v by 1 and returns the result.
168 static __inline__
int atomic_dec_return(atomic_t
*v
)
173 local_irq_save(flags
);
174 __asm__
__volatile__ (
175 "# atomic_dec_return \n\t"
176 DCACHE_CLEAR("%0", "r4", "%1")
177 M32R_LOCK
" %0, @%1; \n\t"
179 M32R_UNLOCK
" %0, @%1; \n\t"
183 #ifdef CONFIG_CHIP_M32700_TS1
185 #endif /* CONFIG_CHIP_M32700_TS1 */
187 local_irq_restore(flags
);
193 * atomic_inc - increment atomic variable
194 * @v: pointer of type atomic_t
196 * Atomically increments @v by 1.
198 #define atomic_inc(v) ((void)atomic_inc_return(v))
201 * atomic_dec - decrement atomic variable
202 * @v: pointer of type atomic_t
204 * Atomically decrements @v by 1.
206 #define atomic_dec(v) ((void)atomic_dec_return(v))
209 * atomic_inc_and_test - increment and test
210 * @v: pointer of type atomic_t
212 * Atomically increments @v by 1
213 * and returns true if the result is zero, or false for all
216 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
219 * atomic_dec_and_test - decrement and test
220 * @v: pointer of type atomic_t
222 * Atomically decrements @v by 1 and
223 * returns true if the result is 0, or false for all
226 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
229 * atomic_add_negative - add and test if negative
230 * @v: pointer of type atomic_t
231 * @i: integer value to add
233 * Atomically adds @i to @v and returns true
234 * if the result is negative, or false when
235 * result is greater than or equal to zero.
237 #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
239 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
240 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
243 * __atomic_add_unless - add unless the number is a given value
244 * @v: pointer of type atomic_t
245 * @a: the amount to add to v...
246 * @u: ...unless v is equal to u.
248 * Atomically adds @a to @v, so long as it was not @u.
249 * Returns the old value of @v.
251 static __inline__
int __atomic_add_unless(atomic_t
*v
, int a
, int u
)
256 if (unlikely(c
== (u
)))
258 old
= atomic_cmpxchg((v
), c
, c
+ (a
));
259 if (likely(old
== c
))
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 #endif /* _ASM_M32R_ATOMIC_H */
This page took 0.036675 seconds and 6 git commands to generate.