ARM: rk: build resource.img with logo_kernel.bmp
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / include / asm / atomic.h
1 /*
2  * Based on arch/arm/include/asm/atomic.h
3  *
4  * Copyright (C) 1996 Russell King.
5  * Copyright (C) 2002 Deep Blue Solutions Ltd.
6  * Copyright (C) 2012 ARM Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 #ifndef __ASM_ATOMIC_H
21 #define __ASM_ATOMIC_H
22
23 #include <linux/compiler.h>
24 #include <linux/types.h>
25
26 #include <asm/barrier.h>
27 #include <asm/cmpxchg.h>
28
29 #define ATOMIC_INIT(i)  { (i) }
30
31 #ifdef __KERNEL__
32
33 /*
34  * On ARM, ordinary assignment (str instruction) doesn't clear the local
35  * strex/ldrex monitor on some implementations. The reason we can use it for
36  * atomic_set() is the clrex or dummy strex done on every exception return.
37  */
38 #define atomic_read(v)  (*(volatile int *)&(v)->counter)
39 #define atomic_set(v,i) (((v)->counter) = (i))
40
41 /*
42  * AArch64 UP and SMP safe atomic ops.  We use load exclusive and
43  * store exclusive to ensure that these are atomic.  We may loop
44  * to ensure that the update happens.
45  */
46 static inline void atomic_add(int i, atomic_t *v)
47 {
48         unsigned long tmp;
49         int result;
50
51         asm volatile("// atomic_add\n"
52 "1:     ldxr    %w0, %2\n"
53 "       add     %w0, %w0, %w3\n"
54 "       stxr    %w1, %w0, %2\n"
55 "       cbnz    %w1, 1b"
56         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
57         : "Ir" (i));
58 }
59
60 static inline int atomic_add_return(int i, atomic_t *v)
61 {
62         unsigned long tmp;
63         int result;
64
65         asm volatile("// atomic_add_return\n"
66 "1:     ldaxr   %w0, %2\n"
67 "       add     %w0, %w0, %w3\n"
68 "       stlxr   %w1, %w0, %2\n"
69 "       cbnz    %w1, 1b"
70         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
71         : "Ir" (i)
72         : "memory");
73
74         return result;
75 }
76
77 static inline void atomic_sub(int i, atomic_t *v)
78 {
79         unsigned long tmp;
80         int result;
81
82         asm volatile("// atomic_sub\n"
83 "1:     ldxr    %w0, %2\n"
84 "       sub     %w0, %w0, %w3\n"
85 "       stxr    %w1, %w0, %2\n"
86 "       cbnz    %w1, 1b"
87         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
88         : "Ir" (i));
89 }
90
91 static inline int atomic_sub_return(int i, atomic_t *v)
92 {
93         unsigned long tmp;
94         int result;
95
96         asm volatile("// atomic_sub_return\n"
97 "1:     ldaxr   %w0, %2\n"
98 "       sub     %w0, %w0, %w3\n"
99 "       stlxr   %w1, %w0, %2\n"
100 "       cbnz    %w1, 1b"
101         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
102         : "Ir" (i)
103         : "memory");
104
105         return result;
106 }
107
108 static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
109 {
110         unsigned long tmp;
111         int oldval;
112
113         asm volatile("// atomic_cmpxchg\n"
114 "1:     ldaxr   %w1, %2\n"
115 "       cmp     %w1, %w3\n"
116 "       b.ne    2f\n"
117 "       stlxr   %w0, %w4, %2\n"
118 "       cbnz    %w0, 1b\n"
119 "2:"
120         : "=&r" (tmp), "=&r" (oldval), "+Q" (ptr->counter)
121         : "Ir" (old), "r" (new)
122         : "cc");
123
124         return oldval;
125 }
126
127 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
128 {
129         unsigned long tmp, tmp2;
130
131         asm volatile("// atomic_clear_mask\n"
132 "1:     ldxr    %0, %2\n"
133 "       bic     %0, %0, %3\n"
134 "       stxr    %w1, %0, %2\n"
135 "       cbnz    %w1, 1b"
136         : "=&r" (tmp), "=&r" (tmp2), "+Q" (*addr)
137         : "Ir" (mask)
138         : "cc");
139 }
140
141 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
142
143 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
144 {
145         int c, old;
146
147         c = atomic_read(v);
148         while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
149                 c = old;
150         return c;
151 }
152
153 #define atomic_inc(v)           atomic_add(1, v)
154 #define atomic_dec(v)           atomic_sub(1, v)
155
156 #define atomic_inc_and_test(v)  (atomic_add_return(1, v) == 0)
157 #define atomic_dec_and_test(v)  (atomic_sub_return(1, v) == 0)
158 #define atomic_inc_return(v)    (atomic_add_return(1, v))
159 #define atomic_dec_return(v)    (atomic_sub_return(1, v))
160 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
161
162 #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
163
164 #define smp_mb__before_atomic_dec()     smp_mb()
165 #define smp_mb__after_atomic_dec()      smp_mb()
166 #define smp_mb__before_atomic_inc()     smp_mb()
167 #define smp_mb__after_atomic_inc()      smp_mb()
168
169 /*
170  * 64-bit atomic operations.
171  */
172 #define ATOMIC64_INIT(i) { (i) }
173
174 #define atomic64_read(v)        (*(volatile long *)&(v)->counter)
175 #define atomic64_set(v,i)       (((v)->counter) = (i))
176
177 static inline void atomic64_add(u64 i, atomic64_t *v)
178 {
179         long result;
180         unsigned long tmp;
181
182         asm volatile("// atomic64_add\n"
183 "1:     ldxr    %0, %2\n"
184 "       add     %0, %0, %3\n"
185 "       stxr    %w1, %0, %2\n"
186 "       cbnz    %w1, 1b"
187         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
188         : "Ir" (i));
189 }
190
191 static inline long atomic64_add_return(long i, atomic64_t *v)
192 {
193         long result;
194         unsigned long tmp;
195
196         asm volatile("// atomic64_add_return\n"
197 "1:     ldaxr   %0, %2\n"
198 "       add     %0, %0, %3\n"
199 "       stlxr   %w1, %0, %2\n"
200 "       cbnz    %w1, 1b"
201         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
202         : "Ir" (i)
203         : "memory");
204
205         return result;
206 }
207
208 static inline void atomic64_sub(u64 i, atomic64_t *v)
209 {
210         long result;
211         unsigned long tmp;
212
213         asm volatile("// atomic64_sub\n"
214 "1:     ldxr    %0, %2\n"
215 "       sub     %0, %0, %3\n"
216 "       stxr    %w1, %0, %2\n"
217 "       cbnz    %w1, 1b"
218         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
219         : "Ir" (i));
220 }
221
222 static inline long atomic64_sub_return(long i, atomic64_t *v)
223 {
224         long result;
225         unsigned long tmp;
226
227         asm volatile("// atomic64_sub_return\n"
228 "1:     ldaxr   %0, %2\n"
229 "       sub     %0, %0, %3\n"
230 "       stlxr   %w1, %0, %2\n"
231 "       cbnz    %w1, 1b"
232         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
233         : "Ir" (i)
234         : "memory");
235
236         return result;
237 }
238
239 static inline long atomic64_cmpxchg(atomic64_t *ptr, long old, long new)
240 {
241         long oldval;
242         unsigned long res;
243
244         asm volatile("// atomic64_cmpxchg\n"
245 "1:     ldaxr   %1, %2\n"
246 "       cmp     %1, %3\n"
247 "       b.ne    2f\n"
248 "       stlxr   %w0, %4, %2\n"
249 "       cbnz    %w0, 1b\n"
250 "2:"
251         : "=&r" (res), "=&r" (oldval), "+Q" (ptr->counter)
252         : "Ir" (old), "r" (new)
253         : "cc");
254
255         return oldval;
256 }
257
258 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
259
260 static inline long atomic64_dec_if_positive(atomic64_t *v)
261 {
262         long result;
263         unsigned long tmp;
264
265         asm volatile("// atomic64_dec_if_positive\n"
266 "1:     ldaxr   %0, %2\n"
267 "       subs    %0, %0, #1\n"
268 "       b.mi    2f\n"
269 "       stlxr   %w1, %0, %2\n"
270 "       cbnz    %w1, 1b\n"
271 "2:"
272         : "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
273         :
274         : "cc", "memory");
275
276         return result;
277 }
278
279 static inline int atomic64_add_unless(atomic64_t *v, long a, long u)
280 {
281         long c, old;
282
283         c = atomic64_read(v);
284         while (c != u && (old = atomic64_cmpxchg((v), c, c + a)) != c)
285                 c = old;
286
287         return c != u;
288 }
289
290 #define atomic64_add_negative(a, v)     (atomic64_add_return((a), (v)) < 0)
291 #define atomic64_inc(v)                 atomic64_add(1LL, (v))
292 #define atomic64_inc_return(v)          atomic64_add_return(1LL, (v))
293 #define atomic64_inc_and_test(v)        (atomic64_inc_return(v) == 0)
294 #define atomic64_sub_and_test(a, v)     (atomic64_sub_return((a), (v)) == 0)
295 #define atomic64_dec(v)                 atomic64_sub(1LL, (v))
296 #define atomic64_dec_return(v)          atomic64_sub_return(1LL, (v))
297 #define atomic64_dec_and_test(v)        (atomic64_dec_return((v)) == 0)
298 #define atomic64_inc_not_zero(v)        atomic64_add_unless((v), 1LL, 0LL)
299
300 #endif
301 #endif