blob: b1d92f13ef9602ca8b54a14d30b3335b735350e2 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001#ifndef __ARCH_BLACKFIN_ATOMIC__
2#define __ARCH_BLACKFIN_ATOMIC__
3
Matthew Wilcoxea4354672009-01-06 14:40:39 -08004#include <linux/types.h>
Bryan Wu1394f032007-05-06 14:50:22 -07005#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 Wu1394f032007-05-06 14:50:22 -070017#define ATOMIC_INIT(i) { (i) }
Bryan Wu1394f032007-05-06 14:50:22 -070018#define atomic_set(v, i) (((v)->counter) = i)
19
Graf Yang6b3087c2009-01-07 23:14:39 +080020#ifdef CONFIG_SMP
21
22#define atomic_read(v) __raw_uncached_fetch_asm(&(v)->counter)
23
24asmlinkage int __raw_uncached_fetch_asm(const volatile int *ptr);
25
26asmlinkage int __raw_atomic_update_asm(volatile int *ptr, int value);
27
28asmlinkage int __raw_atomic_clear_asm(volatile int *ptr, int value);
29
30asmlinkage int __raw_atomic_set_asm(volatile int *ptr, int value);
31
32asmlinkage int __raw_atomic_xor_asm(volatile int *ptr, int value);
33
34asmlinkage int __raw_atomic_test_asm(const volatile int *ptr, int value);
35
36static inline void atomic_add(int i, atomic_t *v)
37{
38 __raw_atomic_update_asm(&v->counter, i);
39}
40
41static inline void atomic_sub(int i, atomic_t *v)
42{
43 __raw_atomic_update_asm(&v->counter, -i);
44}
45
46static inline int atomic_add_return(int i, atomic_t *v)
47{
48 return __raw_atomic_update_asm(&v->counter, i);
49}
50
51static inline int atomic_sub_return(int i, atomic_t *v)
52{
53 return __raw_atomic_update_asm(&v->counter, -i);
54}
55
56static inline void atomic_inc(volatile atomic_t *v)
57{
58 __raw_atomic_update_asm(&v->counter, 1);
59}
60
61static inline void atomic_dec(volatile atomic_t *v)
62{
63 __raw_atomic_update_asm(&v->counter, -1);
64}
65
66static inline void atomic_clear_mask(int mask, atomic_t *v)
67{
68 __raw_atomic_clear_asm(&v->counter, mask);
69}
70
71static inline void atomic_set_mask(int mask, atomic_t *v)
72{
73 __raw_atomic_set_asm(&v->counter, mask);
74}
75
76static 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
91static inline void atomic_add(int i, atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -070092{
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -040093 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -070094
Yi Li6a01f232009-01-07 23:14:39 +080095 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -070096 v->counter += i;
Yi Li6a01f232009-01-07 23:14:39 +080097 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -070098}
99
Graf Yang6b3087c2009-01-07 23:14:39 +0800100static inline void atomic_sub(int i, atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -0700101{
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -0400102 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700103
Yi Li6a01f232009-01-07 23:14:39 +0800104 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700105 v->counter -= i;
Yi Li6a01f232009-01-07 23:14:39 +0800106 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700107
108}
109
Graf Yang6b3087c2009-01-07 23:14:39 +0800110static inline int atomic_add_return(int i, atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -0700111{
112 int __temp = 0;
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -0400113 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700114
Yi Li6a01f232009-01-07 23:14:39 +0800115 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700116 v->counter += i;
117 __temp = v->counter;
Yi Li6a01f232009-01-07 23:14:39 +0800118 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700119
120
121 return __temp;
122}
123
Graf Yang6b3087c2009-01-07 23:14:39 +0800124static inline int atomic_sub_return(int i, atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -0700125{
126 int __temp = 0;
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -0400127 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700128
Yi Li6a01f232009-01-07 23:14:39 +0800129 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700130 v->counter -= i;
131 __temp = v->counter;
Yi Li6a01f232009-01-07 23:14:39 +0800132 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700133
134 return __temp;
135}
136
Graf Yang6b3087c2009-01-07 23:14:39 +0800137static inline void atomic_inc(volatile atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -0700138{
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -0400139 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700140
Yi Li6a01f232009-01-07 23:14:39 +0800141 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700142 v->counter++;
Yi Li6a01f232009-01-07 23:14:39 +0800143 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700144}
145
Graf Yang6b3087c2009-01-07 23:14:39 +0800146static inline void atomic_dec(volatile atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -0700147{
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -0400148 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700149
Yi Li6a01f232009-01-07 23:14:39 +0800150 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700151 v->counter--;
Yi Li6a01f232009-01-07 23:14:39 +0800152 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700153}
154
Graf Yang6b3087c2009-01-07 23:14:39 +0800155static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -0700156{
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -0400157 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700158
Yi Li6a01f232009-01-07 23:14:39 +0800159 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700160 v->counter &= ~mask;
Yi Li6a01f232009-01-07 23:14:39 +0800161 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700162}
163
Graf Yang6b3087c2009-01-07 23:14:39 +0800164static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
Bryan Wu1394f032007-05-06 14:50:22 -0700165{
Mike Frysingerf9ee3ab2009-06-08 09:51:30 -0400166 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700167
Yi Li6a01f232009-01-07 23:14:39 +0800168 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700169 v->counter |= mask;
Yi Li6a01f232009-01-07 23:14:39 +0800170 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700171}
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 Yang6b3087c2009-01-07 23:14:39 +0800179#endif /* !CONFIG_SMP */
180
181#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
Bryan Wu1394f032007-05-06 14:50:22 -0700182#define atomic_dec_return(v) atomic_sub_return(1,(v))
183#define atomic_inc_return(v) atomic_add_return(1,(v))
184
Graf Yang6b3087c2009-01-07 23:14:39 +0800185#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 Wu1394f032007-05-06 14:50:22 -0700198/*
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 Bergmann72099ed2009-05-13 22:56:29 +0000211#include <asm-generic/atomic-long.h>
Bryan Wu1394f032007-05-06 14:50:22 -0700212
213#endif /* __ARCH_BLACKFIN_ATOMIC __ */