1 #ifndef _ASM_POWERPC_ATOMIC_H_
2 #define _ASM_POWERPC_ATOMIC_H_
5 * PowerPC atomic operations
8 typedef struct { volatile int counter; } atomic_t;
11 #include <asm/synch.h>
13 #define ATOMIC_INIT(i) { (i) }
15 #define atomic_read(v) ((v)->counter)
16 #define atomic_set(v,i) (((v)->counter) = (i))
18 /* Erratum #77 on the 405 means we need a sync or dcbt before every stwcx.
19 * The old ATOMIC_SYNC_FIX covered some but not all of this.
21 #ifdef CONFIG_IBM405_ERR77
22 #define PPC405_ERR77(ra,rb) "dcbt " #ra "," #rb ";"
24 #define PPC405_ERR77(ra,rb)
27 static __inline__ void atomic_add(int a, atomic_t *v)
32 "1: lwarx %0,0,%3 # atomic_add\n\
37 : "=&r" (t), "=m" (v->counter)
38 : "r" (a), "r" (&v->counter), "m" (v->counter)
42 static __inline__ int atomic_add_return(int a, atomic_t *v)
48 "1: lwarx %0,0,%2 # atomic_add_return\n\
55 : "r" (a), "r" (&v->counter)
61 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
63 static __inline__ void atomic_sub(int a, atomic_t *v)
68 "1: lwarx %0,0,%3 # atomic_sub\n\
73 : "=&r" (t), "=m" (v->counter)
74 : "r" (a), "r" (&v->counter), "m" (v->counter)
78 static __inline__ int atomic_sub_return(int a, atomic_t *v)
84 "1: lwarx %0,0,%2 # atomic_sub_return\n\
91 : "r" (a), "r" (&v->counter)
97 static __inline__ void atomic_inc(atomic_t *v)
101 __asm__ __volatile__(
102 "1: lwarx %0,0,%2 # atomic_inc\n\
107 : "=&r" (t), "=m" (v->counter)
108 : "r" (&v->counter), "m" (v->counter)
112 static __inline__ int atomic_inc_return(atomic_t *v)
116 __asm__ __volatile__(
118 "1: lwarx %0,0,%1 # atomic_inc_return\n\
132 * atomic_inc_and_test - increment and test
133 * @v: pointer of type atomic_t
135 * Atomically increments @v by 1
136 * and returns true if the result is zero, or false for all
139 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
141 static __inline__ void atomic_dec(atomic_t *v)
145 __asm__ __volatile__(
146 "1: lwarx %0,0,%2 # atomic_dec\n\
151 : "=&r" (t), "=m" (v->counter)
152 : "r" (&v->counter), "m" (v->counter)
156 static __inline__ int atomic_dec_return(atomic_t *v)
160 __asm__ __volatile__(
162 "1: lwarx %0,0,%1 # atomic_dec_return\n\
175 #define atomic_sub_and_test(a, v) (atomic_sub_return((a), (v)) == 0)
176 #define atomic_dec_and_test(v) (atomic_dec_return((v)) == 0)
179 * Atomically test *v and decrement if it is greater than 0.
180 * The function returns the old value of *v minus 1.
182 static __inline__ int atomic_dec_if_positive(atomic_t *v)
186 __asm__ __volatile__(
188 "1: lwarx %0,0,%1 # atomic_dec_if_positive\n\
203 #define smp_mb__before_atomic_dec() smp_mb()
204 #define smp_mb__after_atomic_dec() smp_mb()
205 #define smp_mb__before_atomic_inc() smp_mb()
206 #define smp_mb__after_atomic_inc() smp_mb()
208 #endif /* __KERNEL__ */
209 #endif /* _ASM_POWERPC_ATOMIC_H_ */