1 #ifndef __ARCH_M68K_ATOMIC__
2 #define __ARCH_M68K_ATOMIC__
4 #include <linux/types.h>
5 #include <linux/irqflags.h>
6 #include <asm/cmpxchg.h>
7 #include <asm/barrier.h>
10 * Atomic operations that C can't guarantee us. Useful for
11 * resource counting etc..
15 * We do not have SMP m68k systems, so we don't have to deal with that.
18 #define ATOMIC_INIT(i) { (i) }
20 #define atomic_read(v) READ_ONCE((v)->counter)
21 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
24 * The ColdFire parts cannot do some immediate to memory operations,
25 * so for them we do not specify the "i" asm constraint.
27 #ifdef CONFIG_COLDFIRE
33 #define ATOMIC_OP(op, c_op, asm_op) \
34 static inline void atomic_##op(int i, atomic_t *v) \
36 __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\
39 #ifdef CONFIG_RMW_INSNS
42 * Am I reading these CAS loops right in that %2 is the old value and the first
43 * iteration uses an uninitialized value?
45 * Would it not make sense to add: tmp = atomic_read(v); to avoid this?
48 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
49 static inline int atomic_##op##_return(int i, atomic_t *v) \
53 __asm__ __volatile__( \
55 " " #asm_op "l %3,%1\n" \
58 : "+m" (*v), "=&d" (t), "=&d" (tmp) \
59 : "g" (i), "2" (atomic_read(v))); \
63 #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
64 static inline int atomic_fetch_##op(int i, atomic_t *v) \
68 __asm__ __volatile__( \
70 " " #asm_op "l %3,%1\n" \
73 : "+m" (*v), "=&d" (t), "=&d" (tmp) \
74 : "g" (i), "2" (atomic_read(v))); \
80 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
81 static inline int atomic_##op##_return(int i, atomic_t * v) \
83 unsigned long flags; \
86 local_irq_save(flags); \
87 t = (v->counter c_op i); \
88 local_irq_restore(flags); \
93 #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
94 static inline int atomic_fetch_##op(int i, atomic_t * v) \
96 unsigned long flags; \
99 local_irq_save(flags); \
102 local_irq_restore(flags); \
107 #endif /* CONFIG_RMW_INSNS */
109 #define ATOMIC_OPS(op, c_op, asm_op) \
110 ATOMIC_OP(op, c_op, asm_op) \
111 ATOMIC_OP_RETURN(op, c_op, asm_op) \
112 ATOMIC_FETCH_OP(op, c_op, asm_op)
114 ATOMIC_OPS(add, +=, add)
115 ATOMIC_OPS(sub, -=, sub)
118 #define ATOMIC_OPS(op, c_op, asm_op) \
119 ATOMIC_OP(op, c_op, asm_op) \
120 ATOMIC_FETCH_OP(op, c_op, asm_op)
122 ATOMIC_OPS(and, &=, and)
123 ATOMIC_OPS(or, |=, or)
124 ATOMIC_OPS(xor, ^=, eor)
127 #undef ATOMIC_FETCH_OP
128 #undef ATOMIC_OP_RETURN
131 static inline void atomic_inc(atomic_t *v)
133 __asm__ __volatile__("addql #1,%0" : "+m" (*v));
136 static inline void atomic_dec(atomic_t *v)
138 __asm__ __volatile__("subql #1,%0" : "+m" (*v));
141 static inline int atomic_dec_and_test(atomic_t *v)
144 __asm__ __volatile__("subql #1,%1; seq %0" : "=d" (c), "+m" (*v));
148 static inline int atomic_dec_and_test_lt(atomic_t *v)
151 __asm__ __volatile__(
152 "subql #1,%1; slt %0"
153 : "=d" (c), "=m" (*v)
158 static inline int atomic_inc_and_test(atomic_t *v)
161 __asm__ __volatile__("addql #1,%1; seq %0" : "=d" (c), "+m" (*v));
165 #ifdef CONFIG_RMW_INSNS
167 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
168 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
170 #else /* !CONFIG_RMW_INSNS */
172 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
177 local_irq_save(flags);
178 prev = atomic_read(v);
181 local_irq_restore(flags);
185 static inline int atomic_xchg(atomic_t *v, int new)
190 local_irq_save(flags);
191 prev = atomic_read(v);
193 local_irq_restore(flags);
197 #endif /* !CONFIG_RMW_INSNS */
199 #define atomic_dec_return(v) atomic_sub_return(1, (v))
200 #define atomic_inc_return(v) atomic_add_return(1, (v))
202 static inline int atomic_sub_and_test(int i, atomic_t *v)
205 __asm__ __volatile__("subl %2,%1; seq %0"
206 : "=d" (c), "+m" (*v)
211 static inline int atomic_add_negative(int i, atomic_t *v)
214 __asm__ __volatile__("addl %2,%1; smi %0"
215 : "=d" (c), "+m" (*v)
220 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
225 if (unlikely(c == (u)))
227 old = atomic_cmpxchg((v), c, c + (a));
228 if (likely(old == c))
235 #endif /* __ARCH_M68K_ATOMIC __ */