2 * Based on arch/arm/include/asm/atomic.h
4 * Copyright (C) 1996 Russell King.
5 * Copyright (C) 2002 Deep Blue Solutions Ltd.
6 * Copyright (C) 2012 ARM Ltd.
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.
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.
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/>.
21 #ifndef __ASM_ATOMIC_LSE_H
22 #define __ASM_ATOMIC_LSE_H
24 #ifndef __ARM64_IN_ATOMIC_IMPL
25 #error "please don't include this file directly"
28 #define __LL_SC_ATOMIC(op) __LL_SC_CALL(atomic_##op)
29 #define ATOMIC_OP(op, asm_op) \
30 static inline void atomic_##op(int i, atomic_t *v) \
32 register int w0 asm ("w0") = i; \
33 register atomic_t *x1 asm ("x1") = v; \
35 asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC(op), \
36 " " #asm_op " %w[i], %[v]\n") \
37 : [i] "+r" (w0), [v] "+Q" (v->counter) \
39 : __LL_SC_CLOBBERS); \
42 ATOMIC_OP(andnot, stclr)
49 #define ATOMIC_FETCH_OP(name, mb, op, asm_op, cl...) \
50 static inline int atomic_fetch_##op##name(int i, atomic_t *v) \
52 register int w0 asm ("w0") = i; \
53 register atomic_t *x1 asm ("x1") = v; \
55 asm volatile(ARM64_LSE_ATOMIC_INSN( \
57 __LL_SC_ATOMIC(fetch_##op##name), \
59 " " #asm_op #mb " %w[i], %w[i], %[v]") \
60 : [i] "+r" (w0), [v] "+Q" (v->counter) \
62 : __LL_SC_CLOBBERS, ##cl); \
67 #define ATOMIC_FETCH_OPS(op, asm_op) \
68 ATOMIC_FETCH_OP(_relaxed, , op, asm_op) \
69 ATOMIC_FETCH_OP(_acquire, a, op, asm_op, "memory") \
70 ATOMIC_FETCH_OP(_release, l, op, asm_op, "memory") \
71 ATOMIC_FETCH_OP( , al, op, asm_op, "memory")
73 ATOMIC_FETCH_OPS(andnot, ldclr)
74 ATOMIC_FETCH_OPS(or, ldset)
75 ATOMIC_FETCH_OPS(xor, ldeor)
76 ATOMIC_FETCH_OPS(add, ldadd)
78 #undef ATOMIC_FETCH_OP
79 #undef ATOMIC_FETCH_OPS
81 #define ATOMIC_OP_ADD_RETURN(name, mb, cl...) \
82 static inline int atomic_add_return##name(int i, atomic_t *v) \
84 register int w0 asm ("w0") = i; \
85 register atomic_t *x1 asm ("x1") = v; \
87 asm volatile(ARM64_LSE_ATOMIC_INSN( \
90 __LL_SC_ATOMIC(add_return##name), \
92 " ldadd" #mb " %w[i], w30, %[v]\n" \
93 " add %w[i], %w[i], w30") \
94 : [i] "+r" (w0), [v] "+Q" (v->counter) \
96 : __LL_SC_CLOBBERS, ##cl); \
101 ATOMIC_OP_ADD_RETURN(_relaxed, )
102 ATOMIC_OP_ADD_RETURN(_acquire, a, "memory")
103 ATOMIC_OP_ADD_RETURN(_release, l, "memory")
104 ATOMIC_OP_ADD_RETURN( , al, "memory")
106 #undef ATOMIC_OP_ADD_RETURN
108 static inline void atomic_and(int i, atomic_t *v)
110 register int w0 asm ("w0") = i;
111 register atomic_t *x1 asm ("x1") = v;
113 asm volatile(ARM64_LSE_ATOMIC_INSN(
118 " mvn %w[i], %w[i]\n"
119 " stclr %w[i], %[v]")
120 : [i] "+r" (w0), [v] "+Q" (v->counter)
125 #define ATOMIC_FETCH_OP_AND(name, mb, cl...) \
126 static inline int atomic_fetch_and##name(int i, atomic_t *v) \
128 register int w0 asm ("w0") = i; \
129 register atomic_t *x1 asm ("x1") = v; \
131 asm volatile(ARM64_LSE_ATOMIC_INSN( \
134 __LL_SC_ATOMIC(fetch_and##name), \
136 " mvn %w[i], %w[i]\n" \
137 " ldclr" #mb " %w[i], %w[i], %[v]") \
138 : [i] "+r" (w0), [v] "+Q" (v->counter) \
140 : __LL_SC_CLOBBERS, ##cl); \
145 ATOMIC_FETCH_OP_AND(_relaxed, )
146 ATOMIC_FETCH_OP_AND(_acquire, a, "memory")
147 ATOMIC_FETCH_OP_AND(_release, l, "memory")
148 ATOMIC_FETCH_OP_AND( , al, "memory")
150 #undef ATOMIC_FETCH_OP_AND
152 static inline void atomic_sub(int i, atomic_t *v)
154 register int w0 asm ("w0") = i;
155 register atomic_t *x1 asm ("x1") = v;
157 asm volatile(ARM64_LSE_ATOMIC_INSN(
162 " neg %w[i], %w[i]\n"
163 " stadd %w[i], %[v]")
164 : [i] "+r" (w0), [v] "+Q" (v->counter)
169 #define ATOMIC_OP_SUB_RETURN(name, mb, cl...) \
170 static inline int atomic_sub_return##name(int i, atomic_t *v) \
172 register int w0 asm ("w0") = i; \
173 register atomic_t *x1 asm ("x1") = v; \
175 asm volatile(ARM64_LSE_ATOMIC_INSN( \
178 __LL_SC_ATOMIC(sub_return##name) \
181 " neg %w[i], %w[i]\n" \
182 " ldadd" #mb " %w[i], w30, %[v]\n" \
183 " add %w[i], %w[i], w30") \
184 : [i] "+r" (w0), [v] "+Q" (v->counter) \
186 : __LL_SC_CLOBBERS , ##cl); \
191 ATOMIC_OP_SUB_RETURN(_relaxed, )
192 ATOMIC_OP_SUB_RETURN(_acquire, a, "memory")
193 ATOMIC_OP_SUB_RETURN(_release, l, "memory")
194 ATOMIC_OP_SUB_RETURN( , al, "memory")
196 #undef ATOMIC_OP_SUB_RETURN
198 #define ATOMIC_FETCH_OP_SUB(name, mb, cl...) \
199 static inline int atomic_fetch_sub##name(int i, atomic_t *v) \
201 register int w0 asm ("w0") = i; \
202 register atomic_t *x1 asm ("x1") = v; \
204 asm volatile(ARM64_LSE_ATOMIC_INSN( \
207 __LL_SC_ATOMIC(fetch_sub##name), \
209 " neg %w[i], %w[i]\n" \
210 " ldadd" #mb " %w[i], %w[i], %[v]") \
211 : [i] "+r" (w0), [v] "+Q" (v->counter) \
213 : __LL_SC_CLOBBERS, ##cl); \
218 ATOMIC_FETCH_OP_SUB(_relaxed, )
219 ATOMIC_FETCH_OP_SUB(_acquire, a, "memory")
220 ATOMIC_FETCH_OP_SUB(_release, l, "memory")
221 ATOMIC_FETCH_OP_SUB( , al, "memory")
223 #undef ATOMIC_FETCH_OP_SUB
224 #undef __LL_SC_ATOMIC
226 #define __LL_SC_ATOMIC64(op) __LL_SC_CALL(atomic64_##op)
227 #define ATOMIC64_OP(op, asm_op) \
228 static inline void atomic64_##op(long i, atomic64_t *v) \
230 register long x0 asm ("x0") = i; \
231 register atomic64_t *x1 asm ("x1") = v; \
233 asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC64(op), \
234 " " #asm_op " %[i], %[v]\n") \
235 : [i] "+r" (x0), [v] "+Q" (v->counter) \
237 : __LL_SC_CLOBBERS); \
240 ATOMIC64_OP(andnot, stclr)
241 ATOMIC64_OP(or, stset)
242 ATOMIC64_OP(xor, steor)
243 ATOMIC64_OP(add, stadd)
247 #define ATOMIC64_FETCH_OP(name, mb, op, asm_op, cl...) \
248 static inline long atomic64_fetch_##op##name(long i, atomic64_t *v) \
250 register long x0 asm ("x0") = i; \
251 register atomic64_t *x1 asm ("x1") = v; \
253 asm volatile(ARM64_LSE_ATOMIC_INSN( \
255 __LL_SC_ATOMIC64(fetch_##op##name), \
257 " " #asm_op #mb " %[i], %[i], %[v]") \
258 : [i] "+r" (x0), [v] "+Q" (v->counter) \
260 : __LL_SC_CLOBBERS, ##cl); \
265 #define ATOMIC64_FETCH_OPS(op, asm_op) \
266 ATOMIC64_FETCH_OP(_relaxed, , op, asm_op) \
267 ATOMIC64_FETCH_OP(_acquire, a, op, asm_op, "memory") \
268 ATOMIC64_FETCH_OP(_release, l, op, asm_op, "memory") \
269 ATOMIC64_FETCH_OP( , al, op, asm_op, "memory")
271 ATOMIC64_FETCH_OPS(andnot, ldclr)
272 ATOMIC64_FETCH_OPS(or, ldset)
273 ATOMIC64_FETCH_OPS(xor, ldeor)
274 ATOMIC64_FETCH_OPS(add, ldadd)
276 #undef ATOMIC64_FETCH_OP
277 #undef ATOMIC64_FETCH_OPS
279 #define ATOMIC64_OP_ADD_RETURN(name, mb, cl...) \
280 static inline long atomic64_add_return##name(long i, atomic64_t *v) \
282 register long x0 asm ("x0") = i; \
283 register atomic64_t *x1 asm ("x1") = v; \
285 asm volatile(ARM64_LSE_ATOMIC_INSN( \
288 __LL_SC_ATOMIC64(add_return##name), \
290 " ldadd" #mb " %[i], x30, %[v]\n" \
291 " add %[i], %[i], x30") \
292 : [i] "+r" (x0), [v] "+Q" (v->counter) \
294 : __LL_SC_CLOBBERS, ##cl); \
299 ATOMIC64_OP_ADD_RETURN(_relaxed, )
300 ATOMIC64_OP_ADD_RETURN(_acquire, a, "memory")
301 ATOMIC64_OP_ADD_RETURN(_release, l, "memory")
302 ATOMIC64_OP_ADD_RETURN( , al, "memory")
304 #undef ATOMIC64_OP_ADD_RETURN
306 static inline void atomic64_and(long i, atomic64_t *v)
308 register long x0 asm ("x0") = i;
309 register atomic64_t *x1 asm ("x1") = v;
311 asm volatile(ARM64_LSE_ATOMIC_INSN(
314 __LL_SC_ATOMIC64(and),
318 : [i] "+r" (x0), [v] "+Q" (v->counter)
323 #define ATOMIC64_FETCH_OP_AND(name, mb, cl...) \
324 static inline long atomic64_fetch_and##name(long i, atomic64_t *v) \
326 register long x0 asm ("w0") = i; \
327 register atomic64_t *x1 asm ("x1") = v; \
329 asm volatile(ARM64_LSE_ATOMIC_INSN( \
332 __LL_SC_ATOMIC64(fetch_and##name), \
334 " mvn %[i], %[i]\n" \
335 " ldclr" #mb " %[i], %[i], %[v]") \
336 : [i] "+r" (x0), [v] "+Q" (v->counter) \
338 : __LL_SC_CLOBBERS, ##cl); \
343 ATOMIC64_FETCH_OP_AND(_relaxed, )
344 ATOMIC64_FETCH_OP_AND(_acquire, a, "memory")
345 ATOMIC64_FETCH_OP_AND(_release, l, "memory")
346 ATOMIC64_FETCH_OP_AND( , al, "memory")
348 #undef ATOMIC64_FETCH_OP_AND
350 static inline void atomic64_sub(long i, atomic64_t *v)
352 register long x0 asm ("x0") = i;
353 register atomic64_t *x1 asm ("x1") = v;
355 asm volatile(ARM64_LSE_ATOMIC_INSN(
358 __LL_SC_ATOMIC64(sub),
362 : [i] "+r" (x0), [v] "+Q" (v->counter)
367 #define ATOMIC64_OP_SUB_RETURN(name, mb, cl...) \
368 static inline long atomic64_sub_return##name(long i, atomic64_t *v) \
370 register long x0 asm ("x0") = i; \
371 register atomic64_t *x1 asm ("x1") = v; \
373 asm volatile(ARM64_LSE_ATOMIC_INSN( \
376 __LL_SC_ATOMIC64(sub_return##name) \
379 " neg %[i], %[i]\n" \
380 " ldadd" #mb " %[i], x30, %[v]\n" \
381 " add %[i], %[i], x30") \
382 : [i] "+r" (x0), [v] "+Q" (v->counter) \
384 : __LL_SC_CLOBBERS, ##cl); \
389 ATOMIC64_OP_SUB_RETURN(_relaxed, )
390 ATOMIC64_OP_SUB_RETURN(_acquire, a, "memory")
391 ATOMIC64_OP_SUB_RETURN(_release, l, "memory")
392 ATOMIC64_OP_SUB_RETURN( , al, "memory")
394 #undef ATOMIC64_OP_SUB_RETURN
396 #define ATOMIC64_FETCH_OP_SUB(name, mb, cl...) \
397 static inline long atomic64_fetch_sub##name(long i, atomic64_t *v) \
399 register long x0 asm ("w0") = i; \
400 register atomic64_t *x1 asm ("x1") = v; \
402 asm volatile(ARM64_LSE_ATOMIC_INSN( \
405 __LL_SC_ATOMIC64(fetch_sub##name), \
407 " neg %[i], %[i]\n" \
408 " ldadd" #mb " %[i], %[i], %[v]") \
409 : [i] "+r" (x0), [v] "+Q" (v->counter) \
411 : __LL_SC_CLOBBERS, ##cl); \
416 ATOMIC64_FETCH_OP_SUB(_relaxed, )
417 ATOMIC64_FETCH_OP_SUB(_acquire, a, "memory")
418 ATOMIC64_FETCH_OP_SUB(_release, l, "memory")
419 ATOMIC64_FETCH_OP_SUB( , al, "memory")
421 #undef ATOMIC64_FETCH_OP_SUB
423 static inline long atomic64_dec_if_positive(atomic64_t *v)
425 register long x0 asm ("x0") = (long)v;
427 asm volatile(ARM64_LSE_ATOMIC_INSN(
430 __LL_SC_ATOMIC64(dec_if_positive)
438 " subs %[ret], x30, #1\n"
440 " casal x30, %[ret], %[v]\n"
441 " sub x30, x30, #1\n"
442 " sub x30, x30, %[ret]\n"
445 : [ret] "+&r" (x0), [v] "+Q" (v->counter)
447 : __LL_SC_CLOBBERS, "cc", "memory");
452 #undef __LL_SC_ATOMIC64
454 #define __LL_SC_CMPXCHG(op) __LL_SC_CALL(__cmpxchg_case_##op)
456 #define __CMPXCHG_CASE(w, sz, name, mb, cl...) \
457 static inline unsigned long __cmpxchg_case_##name(volatile void *ptr, \
461 register unsigned long x0 asm ("x0") = (unsigned long)ptr; \
462 register unsigned long x1 asm ("x1") = old; \
463 register unsigned long x2 asm ("x2") = new; \
465 asm volatile(ARM64_LSE_ATOMIC_INSN( \
468 __LL_SC_CMPXCHG(name) \
471 " mov " #w "30, %" #w "[old]\n" \
472 " cas" #mb #sz "\t" #w "30, %" #w "[new], %[v]\n" \
473 " mov %" #w "[ret], " #w "30") \
474 : [ret] "+r" (x0), [v] "+Q" (*(unsigned long *)ptr) \
475 : [old] "r" (x1), [new] "r" (x2) \
476 : __LL_SC_CLOBBERS, ##cl); \
481 __CMPXCHG_CASE(w, b, 1, )
482 __CMPXCHG_CASE(w, h, 2, )
483 __CMPXCHG_CASE(w, , 4, )
484 __CMPXCHG_CASE(x, , 8, )
485 __CMPXCHG_CASE(w, b, acq_1, a, "memory")
486 __CMPXCHG_CASE(w, h, acq_2, a, "memory")
487 __CMPXCHG_CASE(w, , acq_4, a, "memory")
488 __CMPXCHG_CASE(x, , acq_8, a, "memory")
489 __CMPXCHG_CASE(w, b, rel_1, l, "memory")
490 __CMPXCHG_CASE(w, h, rel_2, l, "memory")
491 __CMPXCHG_CASE(w, , rel_4, l, "memory")
492 __CMPXCHG_CASE(x, , rel_8, l, "memory")
493 __CMPXCHG_CASE(w, b, mb_1, al, "memory")
494 __CMPXCHG_CASE(w, h, mb_2, al, "memory")
495 __CMPXCHG_CASE(w, , mb_4, al, "memory")
496 __CMPXCHG_CASE(x, , mb_8, al, "memory")
498 #undef __LL_SC_CMPXCHG
499 #undef __CMPXCHG_CASE
501 #define __LL_SC_CMPXCHG_DBL(op) __LL_SC_CALL(__cmpxchg_double##op)
503 #define __CMPXCHG_DBL(name, mb, cl...) \
504 static inline long __cmpxchg_double##name(unsigned long old1, \
505 unsigned long old2, \
506 unsigned long new1, \
507 unsigned long new2, \
508 volatile void *ptr) \
510 unsigned long oldval1 = old1; \
511 unsigned long oldval2 = old2; \
512 register unsigned long x0 asm ("x0") = old1; \
513 register unsigned long x1 asm ("x1") = old2; \
514 register unsigned long x2 asm ("x2") = new1; \
515 register unsigned long x3 asm ("x3") = new2; \
516 register unsigned long x4 asm ("x4") = (unsigned long)ptr; \
518 asm volatile(ARM64_LSE_ATOMIC_INSN( \
523 __LL_SC_CMPXCHG_DBL(name), \
525 " casp" #mb "\t%[old1], %[old2], %[new1], %[new2], %[v]\n"\
526 " eor %[old1], %[old1], %[oldval1]\n" \
527 " eor %[old2], %[old2], %[oldval2]\n" \
528 " orr %[old1], %[old1], %[old2]") \
529 : [old1] "+r" (x0), [old2] "+r" (x1), \
530 [v] "+Q" (*(unsigned long *)ptr) \
531 : [new1] "r" (x2), [new2] "r" (x3), [ptr] "r" (x4), \
532 [oldval1] "r" (oldval1), [oldval2] "r" (oldval2) \
533 : __LL_SC_CLOBBERS, ##cl); \
539 __CMPXCHG_DBL(_mb, al, "memory")
541 #undef __LL_SC_CMPXCHG_DBL
544 #endif /* __ASM_ATOMIC_LSE_H */