2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #ifndef _ASM_ARC_ATOMIC_H
10 #define _ASM_ARC_ATOMIC_H
14 #include <linux/types.h>
15 #include <linux/compiler.h>
16 #include <asm/cmpxchg.h>
17 #include <asm/barrier.h>
20 #ifndef CONFIG_ARC_PLAT_EZNPS
22 #define atomic_read(v) READ_ONCE((v)->counter)
23 #define ATOMIC_INIT(i) { (i) }
25 #ifdef CONFIG_ARC_HAS_LLSC
27 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
29 #define ATOMIC_OP(op, c_op, asm_op) \
30 static inline void atomic_##op(int i, atomic_t *v) \
34 __asm__ __volatile__( \
35 "1: llock %[val], [%[ctr]] \n" \
36 " " #asm_op " %[val], %[val], %[i] \n" \
37 " scond %[val], [%[ctr]] \n" \
39 : [val] "=&r" (val) /* Early clobber to prevent reg reuse */ \
40 : [ctr] "r" (&v->counter), /* Not "m": llock only supports reg direct addr mode */ \
45 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
46 static inline int atomic_##op##_return(int i, atomic_t *v) \
51 * Explicit full memory barrier needed before/after as \
52 * LLOCK/SCOND thmeselves don't provide any such semantics \
56 __asm__ __volatile__( \
57 "1: llock %[val], [%[ctr]] \n" \
58 " " #asm_op " %[val], %[val], %[i] \n" \
59 " scond %[val], [%[ctr]] \n" \
62 : [ctr] "r" (&v->counter), \
71 #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
72 static inline int atomic_fetch_##op(int i, atomic_t *v) \
74 unsigned int val, orig; \
77 * Explicit full memory barrier needed before/after as \
78 * LLOCK/SCOND thmeselves don't provide any such semantics \
82 __asm__ __volatile__( \
83 "1: llock %[orig], [%[ctr]] \n" \
84 " " #asm_op " %[val], %[orig], %[i] \n" \
85 " scond %[val], [%[ctr]] \n" \
87 : [val] "=&r" (val), \
89 : [ctr] "r" (&v->counter), \
98 #else /* !CONFIG_ARC_HAS_LLSC */
102 /* violating atomic_xxx API locking protocol in UP for optimization sake */
103 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
107 static inline void atomic_set(atomic_t
*v
, int i
)
110 * Independent of hardware support, all of the atomic_xxx() APIs need
111 * to follow the same locking rules to make sure that a "hardware"
112 * atomic insn (e.g. LD) doesn't clobber an "emulated" atomic insn
115 * Thus atomic_set() despite being 1 insn (and seemingly atomic)
116 * requires the locking.
120 atomic_ops_lock(flags
);
121 WRITE_ONCE(v
->counter
, i
);
122 atomic_ops_unlock(flags
);
128 * Non hardware assisted Atomic-R-M-W
129 * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
132 #define ATOMIC_OP(op, c_op, asm_op) \
133 static inline void atomic_##op(int i, atomic_t *v) \
135 unsigned long flags; \
137 atomic_ops_lock(flags); \
139 atomic_ops_unlock(flags); \
142 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
143 static inline int atomic_##op##_return(int i, atomic_t *v) \
145 unsigned long flags; \
146 unsigned long temp; \
149 * spin lock/unlock provides the needed smp_mb() before/after \
151 atomic_ops_lock(flags); \
155 atomic_ops_unlock(flags); \
160 #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
161 static inline int atomic_fetch_##op(int i, atomic_t *v) \
163 unsigned long flags; \
164 unsigned long orig; \
167 * spin lock/unlock provides the needed smp_mb() before/after \
169 atomic_ops_lock(flags); \
172 atomic_ops_unlock(flags); \
177 #endif /* !CONFIG_ARC_HAS_LLSC */
179 #define ATOMIC_OPS(op, c_op, asm_op) \
180 ATOMIC_OP(op, c_op, asm_op) \
181 ATOMIC_OP_RETURN(op, c_op, asm_op) \
182 ATOMIC_FETCH_OP(op, c_op, asm_op)
184 ATOMIC_OPS(add
, +=, add
)
185 ATOMIC_OPS(sub
, -=, sub
)
187 #define atomic_andnot atomic_andnot
190 #define ATOMIC_OPS(op, c_op, asm_op) \
191 ATOMIC_OP(op, c_op, asm_op) \
192 ATOMIC_FETCH_OP(op, c_op, asm_op)
194 ATOMIC_OPS(and, &=, and)
195 ATOMIC_OPS(andnot
, &= ~, bic
)
196 ATOMIC_OPS(or, |=, or)
197 ATOMIC_OPS(xor, ^=, xor)
199 #else /* CONFIG_ARC_PLAT_EZNPS */
201 static inline int atomic_read(const atomic_t
*v
)
205 __asm__
__volatile__(
213 static inline void atomic_set(atomic_t
*v
, int i
)
215 __asm__
__volatile__(
218 : "r"(i
), "r"(&v
->counter
)
222 #define ATOMIC_OP(op, c_op, asm_op) \
223 static inline void atomic_##op(int i, atomic_t *v) \
225 __asm__ __volatile__( \
230 : "r"(i), "r"(&v->counter), "i"(asm_op) \
231 : "r2", "r3", "memory"); \
234 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
235 static inline int atomic_##op##_return(int i, atomic_t *v) \
237 unsigned int temp = i; \
239 /* Explicit full memory barrier needed before/after */ \
242 __asm__ __volatile__( \
248 : "r"(&v->counter), "i"(asm_op) \
249 : "r2", "r3", "memory"); \
258 #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
259 static inline int atomic_fetch_##op(int i, atomic_t *v) \
261 unsigned int temp = i; \
263 /* Explicit full memory barrier needed before/after */ \
266 __asm__ __volatile__( \
272 : "r"(&v->counter), "i"(asm_op) \
273 : "r2", "r3", "memory"); \
280 #define ATOMIC_OPS(op, c_op, asm_op) \
281 ATOMIC_OP(op, c_op, asm_op) \
282 ATOMIC_OP_RETURN(op, c_op, asm_op) \
283 ATOMIC_FETCH_OP(op, c_op, asm_op)
285 ATOMIC_OPS(add
, +=, CTOP_INST_AADD_DI_R2_R2_R3
)
286 #define atomic_sub(i, v) atomic_add(-(i), (v))
287 #define atomic_sub_return(i, v) atomic_add_return(-(i), (v))
290 #define ATOMIC_OPS(op, c_op, asm_op) \
291 ATOMIC_OP(op, c_op, asm_op) \
292 ATOMIC_FETCH_OP(op, c_op, asm_op)
294 ATOMIC_OPS(and, &=, CTOP_INST_AAND_DI_R2_R2_R3
)
295 #define atomic_andnot(mask, v) atomic_and(~(mask), (v))
296 ATOMIC_OPS(or, |=, CTOP_INST_AOR_DI_R2_R2_R3
)
297 ATOMIC_OPS(xor, ^=, CTOP_INST_AXOR_DI_R2_R2_R3
)
299 #endif /* CONFIG_ARC_PLAT_EZNPS */
302 #undef ATOMIC_FETCH_OP
303 #undef ATOMIC_OP_RETURN
307 * __atomic_add_unless - add unless the number is a given value
308 * @v: pointer of type atomic_t
309 * @a: the amount to add to v...
310 * @u: ...unless v is equal to u.
312 * Atomically adds @a to @v, so long as it was not @u.
313 * Returns the old value of @v
315 #define __atomic_add_unless(v, a, u) \
320 * Explicit full memory barrier needed before/after as \
321 * LLOCK/SCOND thmeselves don't provide any such semantics \
325 c = atomic_read(v); \
326 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c)\
334 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
336 #define atomic_inc(v) atomic_add(1, v)
337 #define atomic_dec(v) atomic_sub(1, v)
339 #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
340 #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
341 #define atomic_inc_return(v) atomic_add_return(1, (v))
342 #define atomic_dec_return(v) atomic_sub_return(1, (v))
343 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
345 #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
348 #ifdef CONFIG_GENERIC_ATOMIC64
350 #include <asm-generic/atomic64.h>
352 #else /* Kconfig ensures this is only enabled with needed h/w assist */
355 * ARCv2 supports 64-bit exclusive load (LLOCKD) / store (SCONDD)
356 * - The address HAS to be 64-bit aligned
357 * - There are 2 semantics involved here:
358 * = exclusive implies no interim update between load/store to same addr
359 * = both words are observed/updated together: this is guaranteed even
360 * for regular 64-bit load (LDD) / store (STD). Thus atomic64_set()
361 * is NOT required to use LLOCKD+SCONDD, STD suffices
368 #define ATOMIC64_INIT(a) { (a) }
370 static inline long long atomic64_read(const atomic64_t
*v
)
372 unsigned long long val
;
374 __asm__
__volatile__(
382 static inline void atomic64_set(atomic64_t
*v
, long long a
)
385 * This could have been a simple assignment in "C" but would need
386 * explicit volatile. Otherwise gcc optimizers could elide the store
387 * which borked atomic64 self-test
388 * In the inline asm version, memory clobber needed for exact same
389 * reason, to tell gcc about the store.
391 * This however is not needed for sibling atomic64_add() etc since both
392 * load/store are explicitly done in inline asm. As long as API is used
393 * for each access, gcc has no way to optimize away any load/store
395 __asm__
__volatile__(
398 : "r"(a
), "r"(&v
->counter
)
402 #define ATOMIC64_OP(op, op1, op2) \
403 static inline void atomic64_##op(long long a, atomic64_t *v) \
405 unsigned long long val; \
407 __asm__ __volatile__( \
409 " llockd %0, [%1] \n" \
410 " " #op1 " %L0, %L0, %L2 \n" \
411 " " #op2 " %H0, %H0, %H2 \n" \
412 " scondd %0, [%1] \n" \
415 : "r"(&v->counter), "ir"(a) \
419 #define ATOMIC64_OP_RETURN(op, op1, op2) \
420 static inline long long atomic64_##op##_return(long long a, atomic64_t *v) \
422 unsigned long long val; \
426 __asm__ __volatile__( \
428 " llockd %0, [%1] \n" \
429 " " #op1 " %L0, %L0, %L2 \n" \
430 " " #op2 " %H0, %H0, %H2 \n" \
431 " scondd %0, [%1] \n" \
434 : "r"(&v->counter), "ir"(a) \
435 : "cc"); /* memory clobber comes from smp_mb() */ \
442 #define ATOMIC64_FETCH_OP(op, op1, op2) \
443 static inline long long atomic64_fetch_##op(long long a, atomic64_t *v) \
445 unsigned long long val, orig; \
449 __asm__ __volatile__( \
451 " llockd %0, [%2] \n" \
452 " " #op1 " %L1, %L0, %L3 \n" \
453 " " #op2 " %H1, %H0, %H3 \n" \
454 " scondd %1, [%2] \n" \
456 : "=&r"(orig), "=&r"(val) \
457 : "r"(&v->counter), "ir"(a) \
458 : "cc"); /* memory clobber comes from smp_mb() */ \
465 #define ATOMIC64_OPS(op, op1, op2) \
466 ATOMIC64_OP(op, op1, op2) \
467 ATOMIC64_OP_RETURN(op, op1, op2) \
468 ATOMIC64_FETCH_OP(op, op1, op2)
470 #define atomic64_andnot atomic64_andnot
472 ATOMIC64_OPS(add
, add
.f
, adc
)
473 ATOMIC64_OPS(sub
, sub
.f
, sbc
)
474 ATOMIC64_OPS(and, and, and)
475 ATOMIC64_OPS(andnot
, bic
, bic
)
476 ATOMIC64_OPS(or, or, or)
477 ATOMIC64_OPS(xor, xor, xor)
480 #undef ATOMIC64_FETCH_OP
481 #undef ATOMIC64_OP_RETURN
484 static inline long long
485 atomic64_cmpxchg(atomic64_t
*ptr
, long long expected
, long long new)
491 __asm__
__volatile__(
492 "1: llockd %0, [%1] \n"
493 " brne %L0, %L2, 2f \n"
494 " brne %H0, %H2, 2f \n"
495 " scondd %3, [%1] \n"
499 : "r"(ptr
), "ir"(expected
), "r"(new)
500 : "cc"); /* memory clobber comes from smp_mb() */
507 static inline long long atomic64_xchg(atomic64_t
*ptr
, long long new)
513 __asm__
__volatile__(
514 "1: llockd %0, [%1] \n"
515 " scondd %2, [%1] \n"
520 : "cc"); /* memory clobber comes from smp_mb() */
528 * atomic64_dec_if_positive - decrement by 1 if old value positive
529 * @v: pointer of type atomic64_t
531 * The function returns the old value of *v minus 1, even if
532 * the atomic variable, v, was not decremented.
535 static inline long long atomic64_dec_if_positive(atomic64_t
*v
)
541 __asm__
__volatile__(
542 "1: llockd %0, [%1] \n"
543 " sub.f %L0, %L0, 1 # w0 - 1, set C on borrow\n"
544 " sub.c %H0, %H0, 1 # if C set, w1 - 1\n"
545 " brlt %H0, 0, 2f \n"
546 " scondd %0, [%1] \n"
551 : "cc"); /* memory clobber comes from smp_mb() */
559 * atomic64_add_unless - add unless the number is a given value
560 * @v: pointer of type atomic64_t
561 * @a: the amount to add to v...
562 * @u: ...unless v is equal to u.
564 * if (v != u) { v += a; ret = 1} else {ret = 0}
565 * Returns 1 iff @v was not @u (i.e. if add actually happened)
567 static inline int atomic64_add_unless(atomic64_t
*v
, long long a
, long long u
)
574 __asm__
__volatile__(
575 "1: llockd %0, [%2] \n"
577 " brne %L0, %L4, 2f # continue to add since v != u \n"
578 " breq.d %H0, %H4, 3f # return since v == u \n"
581 " add.f %L0, %L0, %L3 \n"
582 " adc %H0, %H0, %H3 \n"
583 " scondd %0, [%2] \n"
586 : "=&r"(val
), "=&r" (op_done
)
587 : "r"(&v
->counter
), "r"(a
), "r"(u
)
588 : "cc"); /* memory clobber comes from smp_mb() */
595 #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
596 #define atomic64_inc(v) atomic64_add(1LL, (v))
597 #define atomic64_inc_return(v) atomic64_add_return(1LL, (v))
598 #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
599 #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
600 #define atomic64_dec(v) atomic64_sub(1LL, (v))
601 #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v))
602 #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
603 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL)
605 #endif /* !CONFIG_GENERIC_ATOMIC64 */
607 #endif /* !__ASSEMBLY__ */