locking/atomic, arch/avr32: Implement atomic_fetch_{add,sub,and,or,xor}()
[cascardo/linux.git] / arch / avr32 / include / asm / atomic.h
1 /*
2  * Atomic operations that C can't guarantee us.  Useful for
3  * resource counting etc.
4  *
5  * But use these as seldom as possible since they are slower than
6  * regular operations.
7  *
8  * Copyright (C) 2004-2006 Atmel Corporation
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 #ifndef __ASM_AVR32_ATOMIC_H
15 #define __ASM_AVR32_ATOMIC_H
16
17 #include <linux/types.h>
18 #include <asm/cmpxchg.h>
19
20 #define ATOMIC_INIT(i)  { (i) }
21
22 #define atomic_read(v)          READ_ONCE((v)->counter)
23 #define atomic_set(v, i)        WRITE_ONCE(((v)->counter), (i))
24
25 #define ATOMIC_OP_RETURN(op, asm_op, asm_con)                           \
26 static inline int __atomic_##op##_return(int i, atomic_t *v)            \
27 {                                                                       \
28         int result;                                                     \
29                                                                         \
30         asm volatile(                                                   \
31                 "/* atomic_" #op "_return */\n"                         \
32                 "1:     ssrf    5\n"                                    \
33                 "       ld.w    %0, %2\n"                               \
34                 "       " #asm_op "     %0, %3\n"                       \
35                 "       stcond  %1, %0\n"                               \
36                 "       brne    1b"                                     \
37                 : "=&r" (result), "=o" (v->counter)                     \
38                 : "m" (v->counter), #asm_con (i)                        \
39                 : "cc");                                                \
40                                                                         \
41         return result;                                                  \
42 }
43
44 #define ATOMIC_FETCH_OP(op, asm_op, asm_con)                            \
45 static inline int __atomic_fetch_##op(int i, atomic_t *v)               \
46 {                                                                       \
47         int result, val;                                                \
48                                                                         \
49         asm volatile(                                                   \
50                 "/* atomic_fetch_" #op " */\n"                          \
51                 "1:     ssrf    5\n"                                    \
52                 "       ld.w    %0, %3\n"                               \
53                 "       mov     %1, %0\n"                               \
54                 "       " #asm_op "     %1, %4\n"                       \
55                 "       stcond  %2, %1\n"                               \
56                 "       brne    1b"                                     \
57                 : "=&r" (result), "=&r" (val), "=o" (v->counter)        \
58                 : "m" (v->counter), #asm_con (i)                        \
59                 : "cc");                                                \
60                                                                         \
61         return result;                                                  \
62 }
63
64 ATOMIC_OP_RETURN(sub, sub, rKs21)
65 ATOMIC_OP_RETURN(add, add, r)
66 ATOMIC_FETCH_OP (sub, sub, rKs21)
67 ATOMIC_FETCH_OP (add, add, r)
68
69 #define atomic_fetch_or atomic_fetch_or
70
71 #define ATOMIC_OPS(op, asm_op)                                          \
72 ATOMIC_OP_RETURN(op, asm_op, r)                                         \
73 static inline void atomic_##op(int i, atomic_t *v)                      \
74 {                                                                       \
75         (void)__atomic_##op##_return(i, v);                             \
76 }                                                                       \
77 ATOMIC_FETCH_OP(op, asm_op, r)                                          \
78 static inline int atomic_fetch_##op(int i, atomic_t *v)         \
79 {                                                                       \
80         return __atomic_fetch_##op(i, v);                               \
81 }
82
83 ATOMIC_OPS(and, and)
84 ATOMIC_OPS(or, or)
85 ATOMIC_OPS(xor, eor)
86
87 #undef ATOMIC_OPS
88 #undef ATOMIC_FETCH_OP
89 #undef ATOMIC_OP_RETURN
90
91 /*
92  * Probably found the reason why we want to use sub with the signed 21-bit
93  * limit, it uses one less register than the add instruction that can add up to
94  * 32-bit values.
95  *
96  * Both instructions are 32-bit, to use a 16-bit instruction the immediate is
97  * very small; 4 bit.
98  *
99  * sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate.
100  * add 32-bit, type II, adds two register values together.
101  */
102 #define IS_21BIT_CONST(i)                                               \
103         (__builtin_constant_p(i) && ((i) >= -1048575) && ((i) <= 1048576))
104
105 /*
106  * atomic_add_return - add integer to atomic variable
107  * @i: integer value to add
108  * @v: pointer of type atomic_t
109  *
110  * Atomically adds @i to @v. Returns the resulting value.
111  */
112 static inline int atomic_add_return(int i, atomic_t *v)
113 {
114         if (IS_21BIT_CONST(i))
115                 return __atomic_sub_return(-i, v);
116
117         return __atomic_add_return(i, v);
118 }
119
120 static inline int atomic_fetch_add(int i, atomic_t *v)
121 {
122         if (IS_21BIT_CONST(i))
123                 return __atomic_fetch_sub(-i, v);
124
125         return __atomic_fetch_add(i, v);
126 }
127
128 /*
129  * atomic_sub_return - subtract the atomic variable
130  * @i: integer value to subtract
131  * @v: pointer of type atomic_t
132  *
133  * Atomically subtracts @i from @v. Returns the resulting value.
134  */
135 static inline int atomic_sub_return(int i, atomic_t *v)
136 {
137         if (IS_21BIT_CONST(i))
138                 return __atomic_sub_return(i, v);
139
140         return __atomic_add_return(-i, v);
141 }
142
143 static inline int atomic_fetch_sub(int i, atomic_t *v)
144 {
145         if (IS_21BIT_CONST(i))
146                 return __atomic_fetch_sub(i, v);
147
148         return __atomic_fetch_add(-i, v);
149 }
150
151 /*
152  * __atomic_add_unless - add unless the number is a given value
153  * @v: pointer of type atomic_t
154  * @a: the amount to add to v...
155  * @u: ...unless v is equal to u.
156  *
157  * Atomically adds @a to @v, so long as it was not @u.
158  * Returns the old value of @v.
159 */
160 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
161 {
162         int tmp, old = atomic_read(v);
163
164         if (IS_21BIT_CONST(a)) {
165                 asm volatile(
166                         "/* __atomic_sub_unless */\n"
167                         "1:     ssrf    5\n"
168                         "       ld.w    %0, %2\n"
169                         "       cp.w    %0, %4\n"
170                         "       breq    1f\n"
171                         "       sub     %0, %3\n"
172                         "       stcond  %1, %0\n"
173                         "       brne    1b\n"
174                         "1:"
175                         : "=&r"(tmp), "=o"(v->counter)
176                         : "m"(v->counter), "rKs21"(-a), "rKs21"(u)
177                         : "cc", "memory");
178         } else {
179                 asm volatile(
180                         "/* __atomic_add_unless */\n"
181                         "1:     ssrf    5\n"
182                         "       ld.w    %0, %2\n"
183                         "       cp.w    %0, %4\n"
184                         "       breq    1f\n"
185                         "       add     %0, %3\n"
186                         "       stcond  %1, %0\n"
187                         "       brne    1b\n"
188                         "1:"
189                         : "=&r"(tmp), "=o"(v->counter)
190                         : "m"(v->counter), "r"(a), "ir"(u)
191                         : "cc", "memory");
192         }
193
194         return old;
195 }
196
197 #undef IS_21BIT_CONST
198
199 /*
200  * atomic_sub_if_positive - conditionally subtract integer from atomic variable
201  * @i: integer value to subtract
202  * @v: pointer of type atomic_t
203  *
204  * Atomically test @v and subtract @i if @v is greater or equal than @i.
205  * The function returns the old value of @v minus @i.
206  */
207 static inline int atomic_sub_if_positive(int i, atomic_t *v)
208 {
209         int result;
210
211         asm volatile(
212                 "/* atomic_sub_if_positive */\n"
213                 "1:     ssrf    5\n"
214                 "       ld.w    %0, %2\n"
215                 "       sub     %0, %3\n"
216                 "       brlt    1f\n"
217                 "       stcond  %1, %0\n"
218                 "       brne    1b\n"
219                 "1:"
220                 : "=&r"(result), "=o"(v->counter)
221                 : "m"(v->counter), "ir"(i)
222                 : "cc", "memory");
223
224         return result;
225 }
226
227 #define atomic_xchg(v, new)     (xchg(&((v)->counter), new))
228 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
229
230 #define atomic_sub(i, v)        (void)atomic_sub_return(i, v)
231 #define atomic_add(i, v)        (void)atomic_add_return(i, v)
232 #define atomic_dec(v)           atomic_sub(1, (v))
233 #define atomic_inc(v)           atomic_add(1, (v))
234
235 #define atomic_dec_return(v)    atomic_sub_return(1, v)
236 #define atomic_inc_return(v)    atomic_add_return(1, v)
237
238 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
239 #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
240 #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
241 #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
242
243 #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
244
245 #endif /*  __ASM_AVR32_ATOMIC_H */