1 #include <linux/compiler.h>
2 #include <linux/module.h>
3 #include <linux/types.h>
5 #include <asm/processor.h>
6 #include <asm/cmpxchg.h>
7 #include <asm/atomic.h>
9 static noinline u64 cmpxchg8b(u64 *ptr, u64 old, u64 new)
15 LOCK_PREFIX "cmpxchg8b %1\n"
16 : "+A" (old), "+m" (*ptr)
17 : "b" (low), "c" (high)
22 u64 atomic64_cmpxchg(atomic64_t *ptr, u64 old_val, u64 new_val)
24 return cmpxchg8b(&ptr->counter, old_val, new_val);
26 EXPORT_SYMBOL(atomic64_cmpxchg);
29 * atomic64_xchg - xchg atomic64 variable
30 * @ptr: pointer to type atomic64_t
31 * @new_val: value to assign
33 * Atomically xchgs the value of @ptr to @new_val and returns
36 u64 atomic64_xchg(atomic64_t *ptr, u64 new_val)
39 * Try first with a (possibly incorrect) assumption about
40 * what we have there. We'll do two loops most likely,
41 * but we'll get an ownership MESI transaction straight away
42 * instead of a read transaction followed by a
43 * flush-for-ownership transaction:
45 u64 old_val, real_val = 0;
50 real_val = atomic64_cmpxchg(ptr, old_val, new_val);
52 } while (real_val != old_val);
56 EXPORT_SYMBOL(atomic64_xchg);
59 * atomic64_set - set atomic64 variable
60 * @ptr: pointer to type atomic64_t
61 * @new_val: value to assign
63 * Atomically sets the value of @ptr to @new_val.
65 void atomic64_set(atomic64_t *ptr, u64 new_val)
67 atomic64_xchg(ptr, new_val);
69 EXPORT_SYMBOL(atomic64_read);
72 * atomic64_read - read atomic64 variable
73 * @ptr: pointer to type atomic64_t
75 * Atomically reads the value of @ptr and returns it.
77 u64 atomic64_read(atomic64_t *ptr)
82 "mov %%ebx, %%eax\n\t"
83 "mov %%ecx, %%edx\n\t"
84 LOCK_PREFIX "cmpxchg8b %1\n"
91 EXPORT_SYMBOL(atomic64_read);
94 * atomic64_add_return - add and return
95 * @delta: integer value to add
96 * @ptr: pointer to type atomic64_t
98 * Atomically adds @delta to @ptr and returns @delta + *@ptr
100 noinline u64 atomic64_add_return(u64 delta, atomic64_t *ptr)
103 * Try first with a (possibly incorrect) assumption about
104 * what we have there. We'll do two loops most likely,
105 * but we'll get an ownership MESI transaction straight away
106 * instead of a read transaction followed by a
107 * flush-for-ownership transaction:
109 u64 old_val, new_val, real_val = 0;
113 new_val = old_val + delta;
115 real_val = atomic64_cmpxchg(ptr, old_val, new_val);
117 } while (real_val != old_val);
121 EXPORT_SYMBOL(atomic64_add_return);
123 u64 atomic64_sub_return(u64 delta, atomic64_t *ptr)
125 return atomic64_add_return(-delta, ptr);
127 EXPORT_SYMBOL(atomic64_sub_return);
129 u64 atomic64_inc_return(atomic64_t *ptr)
131 return atomic64_add_return(1, ptr);
133 EXPORT_SYMBOL(atomic64_inc_return);
135 u64 atomic64_dec_return(atomic64_t *ptr)
137 return atomic64_sub_return(1, ptr);
139 EXPORT_SYMBOL(atomic64_dec_return);
142 * atomic64_add - add integer to atomic64 variable
143 * @delta: integer value to add
144 * @ptr: pointer to type atomic64_t
146 * Atomically adds @delta to @ptr.
148 void atomic64_add(u64 delta, atomic64_t *ptr)
150 atomic64_add_return(delta, ptr);
152 EXPORT_SYMBOL(atomic64_add);
155 * atomic64_sub - subtract the atomic64 variable
156 * @delta: integer value to subtract
157 * @ptr: pointer to type atomic64_t
159 * Atomically subtracts @delta from @ptr.
161 void atomic64_sub(u64 delta, atomic64_t *ptr)
163 atomic64_add(-delta, ptr);
165 EXPORT_SYMBOL(atomic64_sub);
168 * atomic64_sub_and_test - subtract value from variable and test result
169 * @delta: integer value to subtract
170 * @ptr: pointer to type atomic64_t
172 * Atomically subtracts @delta from @ptr and returns
173 * true if the result is zero, or false for all
176 int atomic64_sub_and_test(u64 delta, atomic64_t *ptr)
178 u64 old_val = atomic64_sub_return(delta, ptr);
182 EXPORT_SYMBOL(atomic64_sub_and_test);
185 * atomic64_inc - increment atomic64 variable
186 * @ptr: pointer to type atomic64_t
188 * Atomically increments @ptr by 1.
190 void atomic64_inc(atomic64_t *ptr)
192 atomic64_add(1, ptr);
194 EXPORT_SYMBOL(atomic64_inc);
197 * atomic64_dec - decrement atomic64 variable
198 * @ptr: pointer to type atomic64_t
200 * Atomically decrements @ptr by 1.
202 void atomic64_dec(atomic64_t *ptr)
204 atomic64_sub(1, ptr);
206 EXPORT_SYMBOL(atomic64_dec);
209 * atomic64_dec_and_test - decrement and test
210 * @ptr: pointer to type atomic64_t
212 * Atomically decrements @ptr by 1 and
213 * returns true if the result is 0, or false for all other
216 int atomic64_dec_and_test(atomic64_t *ptr)
218 return atomic64_sub_and_test(1, ptr);
220 EXPORT_SYMBOL(atomic64_dec_and_test);
223 * atomic64_inc_and_test - increment and test
224 * @ptr: pointer to type atomic64_t
226 * Atomically increments @ptr by 1
227 * and returns true if the result is zero, or false for all
230 int atomic64_inc_and_test(atomic64_t *ptr)
232 return atomic64_sub_and_test(-1, ptr);
234 EXPORT_SYMBOL(atomic64_inc_and_test);
237 * atomic64_add_negative - add and test if negative
238 * @delta: integer value to add
239 * @ptr: pointer to type atomic64_t
241 * Atomically adds @delta to @ptr and returns true
242 * if the result is negative, or false when
243 * result is greater than or equal to zero.
245 int atomic64_add_negative(u64 delta, atomic64_t *ptr)
247 long long old_val = atomic64_add_return(delta, ptr);
251 EXPORT_SYMBOL(atomic64_add_negative);