2 * PowerPC atomic operations
5 #ifndef _ASM_PPC_ATOMIC_H_
6 #define _ASM_PPC_ATOMIC_H_
8 typedef struct { volatile int counter; } atomic_t;
12 #define ATOMIC_INIT(i) { (i) }
14 #define atomic_read(v) ((v)->counter)
15 #define atomic_set(v,i) (((v)->counter) = (i))
17 extern void atomic_clear_mask(unsigned long mask, unsigned long *addr);
18 extern void atomic_set_mask(unsigned long mask, unsigned long *addr);
21 #define SMP_ISYNC "\n\tisync"
26 /* Erratum #77 on the 405 means we need a sync or dcbt before every stwcx.
27 * The old ATOMIC_SYNC_FIX covered some but not all of this.
29 #ifdef CONFIG_IBM405_ERR77
30 #define PPC405_ERR77(ra,rb) "dcbt " #ra "," #rb ";"
32 #define PPC405_ERR77(ra,rb)
35 static __inline__ void atomic_add(int a, atomic_t *v)
40 "1: lwarx %0,0,%3 # atomic_add\n\
45 : "=&r" (t), "=m" (v->counter)
46 : "r" (a), "r" (&v->counter), "m" (v->counter)
50 static __inline__ int atomic_add_return(int a, atomic_t *v)
55 "1: lwarx %0,0,%2 # atomic_add_return\n\
62 : "r" (a), "r" (&v->counter)
68 static __inline__ void atomic_sub(int a, atomic_t *v)
73 "1: lwarx %0,0,%3 # atomic_sub\n\
78 : "=&r" (t), "=m" (v->counter)
79 : "r" (a), "r" (&v->counter), "m" (v->counter)
83 static __inline__ int atomic_sub_return(int a, atomic_t *v)
88 "1: lwarx %0,0,%2 # atomic_sub_return\n\
95 : "r" (a), "r" (&v->counter)
101 static __inline__ void atomic_inc(atomic_t *v)
105 __asm__ __volatile__(
106 "1: lwarx %0,0,%2 # atomic_inc\n\
111 : "=&r" (t), "=m" (v->counter)
112 : "r" (&v->counter), "m" (v->counter)
116 static __inline__ int atomic_inc_return(atomic_t *v)
120 __asm__ __volatile__(
121 "1: lwarx %0,0,%1 # atomic_inc_return\n\
134 static __inline__ void atomic_dec(atomic_t *v)
138 __asm__ __volatile__(
139 "1: lwarx %0,0,%2 # atomic_dec\n\
144 : "=&r" (t), "=m" (v->counter)
145 : "r" (&v->counter), "m" (v->counter)
149 static __inline__ int atomic_dec_return(atomic_t *v)
153 __asm__ __volatile__(
154 "1: lwarx %0,0,%1 # atomic_dec_return\n\
167 #define atomic_sub_and_test(a, v) (atomic_sub_return((a), (v)) == 0)
168 #define atomic_dec_and_test(v) (atomic_dec_return((v)) == 0)
171 * Atomically test *v and decrement if it is greater than 0.
172 * The function returns the old value of *v minus 1.
174 static __inline__ int atomic_dec_if_positive(atomic_t *v)
178 __asm__ __volatile__(
179 "1: lwarx %0,0,%1 # atomic_dec_if_positive\n\
194 #define smp_mb__before_atomic_dec() smp_mb()
195 #define smp_mb__after_atomic_dec() smp_mb()
196 #define smp_mb__before_atomic_inc() smp_mb()
197 #define smp_mb__after_atomic_inc() smp_mb()
199 #endif /* __KERNEL__ */
200 #endif /* _ASM_PPC_ATOMIC_H_ */