Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | #ifndef __ARCH_BLACKFIN_ATOMIC__ |
| 2 | #define __ARCH_BLACKFIN_ATOMIC__ |
| 3 | |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 4 | #include <linux/types.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 5 | #include <asm/system.h> /* local_irq_XXX() */ |
| 6 | |
| 7 | /* |
| 8 | * Atomic operations that C can't guarantee us. Useful for |
| 9 | * resource counting etc.. |
| 10 | * |
| 11 | * Generally we do not concern about SMP BFIN systems, so we don't have |
| 12 | * to deal with that. |
| 13 | * |
| 14 | * Tony Kou (tonyko@lineo.ca) Lineo Inc. 2001 |
| 15 | */ |
| 16 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 17 | #define ATOMIC_INIT(i) { (i) } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 18 | #define atomic_set(v, i) (((v)->counter) = i) |
| 19 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 20 | #ifdef CONFIG_SMP |
| 21 | |
| 22 | #define atomic_read(v) __raw_uncached_fetch_asm(&(v)->counter) |
| 23 | |
| 24 | asmlinkage int __raw_uncached_fetch_asm(const volatile int *ptr); |
| 25 | |
| 26 | asmlinkage int __raw_atomic_update_asm(volatile int *ptr, int value); |
| 27 | |
| 28 | asmlinkage int __raw_atomic_clear_asm(volatile int *ptr, int value); |
| 29 | |
| 30 | asmlinkage int __raw_atomic_set_asm(volatile int *ptr, int value); |
| 31 | |
| 32 | asmlinkage int __raw_atomic_xor_asm(volatile int *ptr, int value); |
| 33 | |
| 34 | asmlinkage int __raw_atomic_test_asm(const volatile int *ptr, int value); |
| 35 | |
| 36 | static inline void atomic_add(int i, atomic_t *v) |
| 37 | { |
| 38 | __raw_atomic_update_asm(&v->counter, i); |
| 39 | } |
| 40 | |
| 41 | static inline void atomic_sub(int i, atomic_t *v) |
| 42 | { |
| 43 | __raw_atomic_update_asm(&v->counter, -i); |
| 44 | } |
| 45 | |
| 46 | static inline int atomic_add_return(int i, atomic_t *v) |
| 47 | { |
| 48 | return __raw_atomic_update_asm(&v->counter, i); |
| 49 | } |
| 50 | |
| 51 | static inline int atomic_sub_return(int i, atomic_t *v) |
| 52 | { |
| 53 | return __raw_atomic_update_asm(&v->counter, -i); |
| 54 | } |
| 55 | |
| 56 | static inline void atomic_inc(volatile atomic_t *v) |
| 57 | { |
| 58 | __raw_atomic_update_asm(&v->counter, 1); |
| 59 | } |
| 60 | |
| 61 | static inline void atomic_dec(volatile atomic_t *v) |
| 62 | { |
| 63 | __raw_atomic_update_asm(&v->counter, -1); |
| 64 | } |
| 65 | |
| 66 | static inline void atomic_clear_mask(int mask, atomic_t *v) |
| 67 | { |
| 68 | __raw_atomic_clear_asm(&v->counter, mask); |
| 69 | } |
| 70 | |
| 71 | static inline void atomic_set_mask(int mask, atomic_t *v) |
| 72 | { |
| 73 | __raw_atomic_set_asm(&v->counter, mask); |
| 74 | } |
| 75 | |
| 76 | static inline int atomic_test_mask(int mask, atomic_t *v) |
| 77 | { |
| 78 | return __raw_atomic_test_asm(&v->counter, mask); |
| 79 | } |
| 80 | |
| 81 | /* Atomic operations are already serializing */ |
| 82 | #define smp_mb__before_atomic_dec() barrier() |
| 83 | #define smp_mb__after_atomic_dec() barrier() |
| 84 | #define smp_mb__before_atomic_inc() barrier() |
| 85 | #define smp_mb__after_atomic_inc() barrier() |
| 86 | |
| 87 | #else /* !CONFIG_SMP */ |
| 88 | |
| 89 | #define atomic_read(v) ((v)->counter) |
| 90 | |
| 91 | static inline void atomic_add(int i, atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 92 | { |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 93 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 94 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 95 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 96 | v->counter += i; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 97 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 100 | static inline void atomic_sub(int i, atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 101 | { |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 102 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 103 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 104 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 105 | v->counter -= i; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 106 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 107 | |
| 108 | } |
| 109 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 110 | static inline int atomic_add_return(int i, atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 111 | { |
| 112 | int __temp = 0; |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 113 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 114 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 115 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 116 | v->counter += i; |
| 117 | __temp = v->counter; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 118 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | return __temp; |
| 122 | } |
| 123 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 124 | static inline int atomic_sub_return(int i, atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 125 | { |
| 126 | int __temp = 0; |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 127 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 128 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 129 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 130 | v->counter -= i; |
| 131 | __temp = v->counter; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 132 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 133 | |
| 134 | return __temp; |
| 135 | } |
| 136 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 137 | static inline void atomic_inc(volatile atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 138 | { |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 139 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 140 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 141 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 142 | v->counter++; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 143 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 146 | static inline void atomic_dec(volatile atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 147 | { |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 148 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 149 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 150 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 151 | v->counter--; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 152 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 155 | static inline void atomic_clear_mask(unsigned int mask, atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 156 | { |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 157 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 158 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 159 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 160 | v->counter &= ~mask; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 161 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 164 | static inline void atomic_set_mask(unsigned int mask, atomic_t *v) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 165 | { |
Mike Frysinger | f9ee3ab | 2009-06-08 09:51:30 -0400 | [diff] [blame^] | 166 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 167 | |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 168 | local_irq_save_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 169 | v->counter |= mask; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 170 | local_irq_restore_hw(flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* Atomic operations are already serializing */ |
| 174 | #define smp_mb__before_atomic_dec() barrier() |
| 175 | #define smp_mb__after_atomic_dec() barrier() |
| 176 | #define smp_mb__before_atomic_inc() barrier() |
| 177 | #define smp_mb__after_atomic_inc() barrier() |
| 178 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 179 | #endif /* !CONFIG_SMP */ |
| 180 | |
| 181 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 182 | #define atomic_dec_return(v) atomic_sub_return(1,(v)) |
| 183 | #define atomic_inc_return(v) atomic_add_return(1,(v)) |
| 184 | |
Graf Yang | 6b3087c | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 185 | #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n))) |
| 186 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
| 187 | |
| 188 | #define atomic_add_unless(v, a, u) \ |
| 189 | ({ \ |
| 190 | int c, old; \ |
| 191 | c = atomic_read(v); \ |
| 192 | while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \ |
| 193 | c = old; \ |
| 194 | c != (u); \ |
| 195 | }) |
| 196 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 197 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 198 | /* |
| 199 | * atomic_inc_and_test - increment and test |
| 200 | * @v: pointer of type atomic_t |
| 201 | * |
| 202 | * Atomically increments @v by 1 |
| 203 | * and returns true if the result is zero, or false for all |
| 204 | * other cases. |
| 205 | */ |
| 206 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
| 207 | |
| 208 | #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0) |
| 209 | #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) |
| 210 | |
Arnd Bergmann | 72099ed | 2009-05-13 22:56:29 +0000 | [diff] [blame] | 211 | #include <asm-generic/atomic-long.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 212 | |
| 213 | #endif /* __ARCH_BLACKFIN_ATOMIC __ */ |