Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ASM_SH_ATOMIC_H |
| 2 | #define __ASM_SH_ATOMIC_H |
| 3 | |
| 4 | /* |
| 5 | * Atomic operations that C can't guarantee us. Useful for |
| 6 | * resource counting etc.. |
| 7 | * |
| 8 | */ |
| 9 | |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 10 | #include <linux/compiler.h> |
| 11 | #include <linux/types.h> |
David Howells | e839ca5 | 2012-03-28 18:30:03 +0100 | [diff] [blame] | 12 | #include <asm/cmpxchg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | #define ATOMIC_INIT(i) ( (atomic_t) { (i) } ) |
| 15 | |
Anton Blanchard | f3d46f9 | 2010-05-17 14:33:53 +1000 | [diff] [blame] | 16 | #define atomic_read(v) (*(volatile int *)&(v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #define atomic_set(v,i) ((v)->counter = (i)) |
| 18 | |
Stuart Menefy | 1efe4ce | 2007-11-30 16:12:36 +0900 | [diff] [blame] | 19 | #if defined(CONFIG_GUSA_RB) |
| 20 | #include <asm/atomic-grb.h> |
| 21 | #elif defined(CONFIG_CPU_SH4A) |
Paul Mundt | ec723fbe | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 22 | #include <asm/atomic-llsc.h> |
Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 23 | #else |
Paul Mundt | ec723fbe | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 24 | #include <asm/atomic-irq.h> |
Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 25 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 28 | #define atomic_dec_return(v) atomic_sub_return(1, (v)) |
| 29 | #define atomic_inc_return(v) atomic_add_return(1, (v)) |
| 30 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
| 31 | #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0) |
| 32 | #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 34 | #define atomic_inc(v) atomic_add(1, (v)) |
| 35 | #define atomic_dec(v) atomic_sub(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 37 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
| 38 | #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) |
| 39 | |
| 40 | /** |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 41 | * __atomic_add_unless - add unless the number is a given value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * @v: pointer of type atomic_t |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 43 | * @a: the amount to add to v... |
| 44 | * @u: ...unless v is equal to u. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 46 | * Atomically adds @a to @v, so long as it was not @u. |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 47 | * Returns the old value of @v. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 49 | static inline int __atomic_add_unless(atomic_t *v, int a, int u) |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 50 | { |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 51 | int c, old; |
| 52 | c = atomic_read(v); |
| 53 | for (;;) { |
| 54 | if (unlikely(c == (u))) |
| 55 | break; |
| 56 | old = atomic_cmpxchg((v), c, c + (a)); |
| 57 | if (likely(old == c)) |
| 58 | break; |
| 59 | c = old; |
| 60 | } |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 61 | |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 62 | return c; |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 63 | } |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 64 | |
Paul Mundt | 1c8db71 | 2009-10-18 15:36:02 +0900 | [diff] [blame] | 65 | #define smp_mb__before_atomic_dec() smp_mb() |
| 66 | #define smp_mb__after_atomic_dec() smp_mb() |
| 67 | #define smp_mb__before_atomic_inc() smp_mb() |
| 68 | #define smp_mb__after_atomic_inc() smp_mb() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #endif /* __ASM_SH_ATOMIC_H */ |