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 | |
| 10 | typedef struct { volatile int counter; } atomic_t; |
| 11 | |
| 12 | #define ATOMIC_INIT(i) ( (atomic_t) { (i) } ) |
| 13 | |
| 14 | #define atomic_read(v) ((v)->counter) |
| 15 | #define atomic_set(v,i) ((v)->counter = (i)) |
| 16 | |
Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 17 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/system.h> |
| 19 | |
Stuart Menefy | 1efe4ce | 2007-11-30 16:12:36 +0900 | [diff] [blame] | 20 | #if defined(CONFIG_GUSA_RB) |
| 21 | #include <asm/atomic-grb.h> |
| 22 | #elif defined(CONFIG_CPU_SH4A) |
Paul Mundt | ec723fb | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 23 | #include <asm/atomic-llsc.h> |
Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 24 | #else |
Paul Mundt | ec723fb | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 25 | #include <asm/atomic-irq.h> |
Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 26 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #define atomic_dec_return(v) atomic_sub_return(1,(v)) |
| 31 | #define atomic_inc_return(v) atomic_add_return(1,(v)) |
| 32 | |
| 33 | /* |
| 34 | * atomic_inc_and_test - increment and test |
| 35 | * @v: pointer of type atomic_t |
| 36 | * |
| 37 | * Atomically increments @v by 1 |
| 38 | * and returns true if the result is zero, or false for all |
| 39 | * other cases. |
| 40 | */ |
| 41 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
| 42 | |
| 43 | #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0) |
| 44 | #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) |
| 45 | |
| 46 | #define atomic_inc(v) atomic_add(1,(v)) |
| 47 | #define atomic_dec(v) atomic_sub(1,(v)) |
| 48 | |
Stuart Menefy | 1efe4ce | 2007-11-30 16:12:36 +0900 | [diff] [blame] | 49 | #ifndef CONFIG_GUSA_RB |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 50 | static inline int atomic_cmpxchg(atomic_t *v, int old, int new) |
| 51 | { |
| 52 | int ret; |
| 53 | unsigned long flags; |
| 54 | |
| 55 | local_irq_save(flags); |
| 56 | ret = v->counter; |
| 57 | if (likely(ret == old)) |
| 58 | v->counter = new; |
| 59 | local_irq_restore(flags); |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 64 | static inline int atomic_add_unless(atomic_t *v, int a, int u) |
| 65 | { |
| 66 | int ret; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | local_irq_save(flags); |
| 70 | ret = v->counter; |
| 71 | if (ret != u) |
| 72 | v->counter += a; |
| 73 | local_irq_restore(flags); |
| 74 | |
| 75 | return ret != u; |
| 76 | } |
Stuart Menefy | 1efe4ce | 2007-11-30 16:12:36 +0900 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 80 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* Atomic operations are already serializing on SH */ |
| 83 | #define smp_mb__before_atomic_dec() barrier() |
| 84 | #define smp_mb__after_atomic_dec() barrier() |
| 85 | #define smp_mb__before_atomic_inc() barrier() |
| 86 | #define smp_mb__after_atomic_inc() barrier() |
| 87 | |
Christoph Lameter | d3cb487 | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 88 | #include <asm-generic/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #endif /* __ASM_SH_ATOMIC_H */ |