2 * Atomic operations that C can't guarantee us. Useful for
3 * resource counting etc..
5 * But use these as seldom as possible since they are much more slower
6 * than regular operations.
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
12 * Copyright (C) 1996, 97, 99, 2000, 03, 04 by Ralf Baechle
16 * As workaround for the ATOMIC_DEC_AND_LOCK / atomic_dec_and_lock mess in
17 * <linux/spinlock.h> we have to include <linux/spinlock.h> outside the
18 * main big wrapper ...
20 #include <linux/config.h>
21 #include <linux/spinlock.h>
26 #include <asm/cpu-features.h>
29 extern spinlock_t atomic_lock;
31 typedef struct { volatile int counter; } atomic_t;
33 #define ATOMIC_INIT(i) { (i) }
36 * atomic_read - read atomic variable
37 * @v: pointer of type atomic_t
39 * Atomically reads the value of @v.
41 #define atomic_read(v) ((v)->counter)
44 * atomic_set - set atomic variable
45 * @v: pointer of type atomic_t
48 * Atomically sets the value of @v to @i.
50 #define atomic_set(v,i) ((v)->counter = (i))
53 * atomic_add - add integer to atomic variable
54 * @i: integer value to add
55 * @v: pointer of type atomic_t
57 * Atomically adds @i to @v.
59 static __inline__ void atomic_add(int i, atomic_t * v)
61 if (cpu_has_llsc && R10000_LLSC_WAR) {
66 "1: ll %0, %1 # atomic_add \n"
71 : "=&r" (temp), "=m" (v->counter)
72 : "Ir" (i), "m" (v->counter));
73 } else if (cpu_has_llsc) {
78 "1: ll %0, %1 # atomic_add \n"
83 : "=&r" (temp), "=m" (v->counter)
84 : "Ir" (i), "m" (v->counter));
88 spin_lock_irqsave(&atomic_lock, flags);
90 spin_unlock_irqrestore(&atomic_lock, flags);
95 * atomic_sub - subtract the atomic variable
96 * @i: integer value to subtract
97 * @v: pointer of type atomic_t
99 * Atomically subtracts @i from @v.
101 static __inline__ void atomic_sub(int i, atomic_t * v)
103 if (cpu_has_llsc && R10000_LLSC_WAR) {
106 __asm__ __volatile__(
108 "1: ll %0, %1 # atomic_sub \n"
113 : "=&r" (temp), "=m" (v->counter)
114 : "Ir" (i), "m" (v->counter));
115 } else if (cpu_has_llsc) {
118 __asm__ __volatile__(
120 "1: ll %0, %1 # atomic_sub \n"
125 : "=&r" (temp), "=m" (v->counter)
126 : "Ir" (i), "m" (v->counter));
130 spin_lock_irqsave(&atomic_lock, flags);
132 spin_unlock_irqrestore(&atomic_lock, flags);
137 * Same as above, but return the result value
139 static __inline__ int atomic_add_return(int i, atomic_t * v)
141 unsigned long result;
143 if (cpu_has_llsc && R10000_LLSC_WAR) {
146 __asm__ __volatile__(
148 "1: ll %1, %2 # atomic_add_return \n"
149 " addu %0, %1, %3 \n"
152 " addu %0, %1, %3 \n"
155 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
156 : "Ir" (i), "m" (v->counter)
158 } else if (cpu_has_llsc) {
161 __asm__ __volatile__(
163 "1: ll %1, %2 # atomic_add_return \n"
164 " addu %0, %1, %3 \n"
167 " addu %0, %1, %3 \n"
170 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
171 : "Ir" (i), "m" (v->counter)
176 spin_lock_irqsave(&atomic_lock, flags);
180 spin_unlock_irqrestore(&atomic_lock, flags);
186 static __inline__ int atomic_sub_return(int i, atomic_t * v)
188 unsigned long result;
190 if (cpu_has_llsc && R10000_LLSC_WAR) {
193 __asm__ __volatile__(
195 "1: ll %1, %2 # atomic_sub_return \n"
196 " subu %0, %1, %3 \n"
199 " subu %0, %1, %3 \n"
202 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
203 : "Ir" (i), "m" (v->counter)
205 } else if (cpu_has_llsc) {
208 __asm__ __volatile__(
210 "1: ll %1, %2 # atomic_sub_return \n"
211 " subu %0, %1, %3 \n"
214 " subu %0, %1, %3 \n"
217 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
218 : "Ir" (i), "m" (v->counter)
223 spin_lock_irqsave(&atomic_lock, flags);
227 spin_unlock_irqrestore(&atomic_lock, flags);
234 * atomic_sub_if_positive - add integer to atomic variable
235 * @v: pointer of type atomic_t
237 * Atomically test @v and decrement if it is greater than 0.
238 * The function returns the old value of @v minus 1.
240 static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
242 unsigned long result;
244 if (cpu_has_llsc && R10000_LLSC_WAR) {
247 __asm__ __volatile__(
249 "1: ll %1, %2 # atomic_sub_if_positive\n"
250 " subu %0, %1, %3 \n"
257 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
258 : "Ir" (i), "m" (v->counter)
260 } else if (cpu_has_llsc) {
263 __asm__ __volatile__(
265 "1: ll %1, %2 # atomic_sub_if_positive\n"
266 " subu %0, %1, %3 \n"
273 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
274 : "Ir" (i), "m" (v->counter)
279 spin_lock_irqsave(&atomic_lock, flags);
284 spin_unlock_irqrestore(&atomic_lock, flags);
290 #define atomic_dec_return(v) atomic_sub_return(1,(v))
291 #define atomic_inc_return(v) atomic_add_return(1,(v))
294 * atomic_sub_and_test - subtract value from variable and test result
295 * @i: integer value to subtract
296 * @v: pointer of type atomic_t
298 * Atomically subtracts @i from @v and returns
299 * true if the result is zero, or false for all
302 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
305 * atomic_inc_and_test - increment and test
306 * @v: pointer of type atomic_t
308 * Atomically increments @v by 1
309 * and returns true if the result is zero, or false for all
312 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
315 * atomic_dec_and_test - decrement by 1 and test
316 * @v: pointer of type atomic_t
318 * Atomically decrements @v by 1 and
319 * returns true if the result is 0, or false for all other
322 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
325 * atomic_dec_if_positive - decrement by 1 if old value positive
326 * @v: pointer of type atomic_t
328 #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
331 * atomic_inc - increment atomic variable
332 * @v: pointer of type atomic_t
334 * Atomically increments @v by 1.
336 #define atomic_inc(v) atomic_add(1,(v))
339 * atomic_dec - decrement and test
340 * @v: pointer of type atomic_t
342 * Atomically decrements @v by 1.
344 #define atomic_dec(v) atomic_sub(1,(v))
347 * atomic_add_negative - add and test if negative
348 * @v: pointer of type atomic_t
349 * @i: integer value to add
351 * Atomically adds @i to @v and returns true
352 * if the result is negative, or false when
353 * result is greater than or equal to zero.
355 #define atomic_add_negative(i,v) (atomic_add_return(i, (v)) < 0)
359 typedef struct { volatile __s64 counter; } atomic64_t;
361 #define ATOMIC64_INIT(i) { (i) }
364 * atomic64_read - read atomic variable
365 * @v: pointer of type atomic64_t
368 #define atomic64_read(v) ((v)->counter)
371 * atomic64_set - set atomic variable
372 * @v: pointer of type atomic64_t
375 #define atomic64_set(v,i) ((v)->counter = (i))
378 * atomic64_add - add integer to atomic variable
379 * @i: integer value to add
380 * @v: pointer of type atomic64_t
382 * Atomically adds @i to @v.
384 static __inline__ void atomic64_add(long i, atomic64_t * v)
386 if (cpu_has_llsc && R10000_LLSC_WAR) {
389 __asm__ __volatile__(
391 "1: lld %0, %1 # atomic64_add \n"
396 : "=&r" (temp), "=m" (v->counter)
397 : "Ir" (i), "m" (v->counter));
398 } else if (cpu_has_llsc) {
401 __asm__ __volatile__(
403 "1: lld %0, %1 # atomic64_add \n"
408 : "=&r" (temp), "=m" (v->counter)
409 : "Ir" (i), "m" (v->counter));
413 spin_lock_irqsave(&atomic_lock, flags);
415 spin_unlock_irqrestore(&atomic_lock, flags);
420 * atomic64_sub - subtract the atomic variable
421 * @i: integer value to subtract
422 * @v: pointer of type atomic64_t
424 * Atomically subtracts @i from @v.
426 static __inline__ void atomic64_sub(long i, atomic64_t * v)
428 if (cpu_has_llsc && R10000_LLSC_WAR) {
431 __asm__ __volatile__(
433 "1: lld %0, %1 # atomic64_sub \n"
438 : "=&r" (temp), "=m" (v->counter)
439 : "Ir" (i), "m" (v->counter));
440 } else if (cpu_has_llsc) {
443 __asm__ __volatile__(
445 "1: lld %0, %1 # atomic64_sub \n"
450 : "=&r" (temp), "=m" (v->counter)
451 : "Ir" (i), "m" (v->counter));
455 spin_lock_irqsave(&atomic_lock, flags);
457 spin_unlock_irqrestore(&atomic_lock, flags);
462 * Same as above, but return the result value
464 static __inline__ long atomic64_add_return(long i, atomic64_t * v)
466 unsigned long result;
468 if (cpu_has_llsc && R10000_LLSC_WAR) {
471 __asm__ __volatile__(
473 "1: lld %1, %2 # atomic64_add_return \n"
474 " addu %0, %1, %3 \n"
477 " addu %0, %1, %3 \n"
480 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
481 : "Ir" (i), "m" (v->counter)
483 } else if (cpu_has_llsc) {
486 __asm__ __volatile__(
488 "1: lld %1, %2 # atomic64_add_return \n"
489 " addu %0, %1, %3 \n"
492 " addu %0, %1, %3 \n"
495 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
496 : "Ir" (i), "m" (v->counter)
501 spin_lock_irqsave(&atomic_lock, flags);
505 spin_unlock_irqrestore(&atomic_lock, flags);
511 static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
513 unsigned long result;
515 if (cpu_has_llsc && R10000_LLSC_WAR) {
518 __asm__ __volatile__(
520 "1: lld %1, %2 # atomic64_sub_return \n"
521 " subu %0, %1, %3 \n"
524 " subu %0, %1, %3 \n"
527 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
528 : "Ir" (i), "m" (v->counter)
530 } else if (cpu_has_llsc) {
533 __asm__ __volatile__(
535 "1: lld %1, %2 # atomic64_sub_return \n"
536 " subu %0, %1, %3 \n"
539 " subu %0, %1, %3 \n"
542 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
543 : "Ir" (i), "m" (v->counter)
548 spin_lock_irqsave(&atomic_lock, flags);
552 spin_unlock_irqrestore(&atomic_lock, flags);
559 * atomic64_sub_if_positive - add integer to atomic variable
560 * @v: pointer of type atomic64_t
562 * Atomically test @v and decrement if it is greater than 0.
563 * The function returns the old value of @v minus 1.
565 static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
567 unsigned long result;
569 if (cpu_has_llsc && R10000_LLSC_WAR) {
572 __asm__ __volatile__(
574 "1: lld %1, %2 # atomic64_sub_if_positive\n"
575 " dsubu %0, %1, %3 \n"
582 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
583 : "Ir" (i), "m" (v->counter)
585 } else if (cpu_has_llsc) {
588 __asm__ __volatile__(
590 "1: lld %1, %2 # atomic64_sub_if_positive\n"
591 " dsubu %0, %1, %3 \n"
598 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
599 : "Ir" (i), "m" (v->counter)
604 spin_lock_irqsave(&atomic_lock, flags);
609 spin_unlock_irqrestore(&atomic_lock, flags);
615 #define atomic64_dec_return(v) atomic64_sub_return(1,(v))
616 #define atomic64_inc_return(v) atomic64_add_return(1,(v))
619 * atomic64_sub_and_test - subtract value from variable and test result
620 * @i: integer value to subtract
621 * @v: pointer of type atomic64_t
623 * Atomically subtracts @i from @v and returns
624 * true if the result is zero, or false for all
627 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
630 * atomic64_inc_and_test - increment and test
631 * @v: pointer of type atomic64_t
633 * Atomically increments @v by 1
634 * and returns true if the result is zero, or false for all
637 #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
640 * atomic64_dec_and_test - decrement by 1 and test
641 * @v: pointer of type atomic64_t
643 * Atomically decrements @v by 1 and
644 * returns true if the result is 0, or false for all other
647 #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
650 * atomic64_dec_if_positive - decrement by 1 if old value positive
651 * @v: pointer of type atomic64_t
653 #define atomic64_dec_if_positive(v) atomic64_sub_if_positive(1, v)
656 * atomic64_inc - increment atomic variable
657 * @v: pointer of type atomic64_t
659 * Atomically increments @v by 1.
661 #define atomic64_inc(v) atomic64_add(1,(v))
664 * atomic64_dec - decrement and test
665 * @v: pointer of type atomic64_t
667 * Atomically decrements @v by 1.
669 #define atomic64_dec(v) atomic64_sub(1,(v))
672 * atomic64_add_negative - add and test if negative
673 * @v: pointer of type atomic64_t
674 * @i: integer value to add
676 * Atomically adds @i to @v and returns true
677 * if the result is negative, or false when
678 * result is greater than or equal to zero.
680 #define atomic64_add_negative(i,v) (atomic64_add_return(i, (v)) < 0)
682 #endif /* CONFIG_64BIT */
685 * atomic*_return operations are serializing but not the non-*_return
688 #define smp_mb__before_atomic_dec() smp_mb()
689 #define smp_mb__after_atomic_dec() smp_mb()
690 #define smp_mb__before_atomic_inc() smp_mb()
691 #define smp_mb__after_atomic_inc() smp_mb()
693 #endif /* _ASM_ATOMIC_H */