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 #define atomic_read(v) ((v)->counter)
22 #ifdef CONFIG_ARC_HAS_LLSC
24 #define atomic_set(v, i) (((v)->counter) = (i))
26 #ifdef CONFIG_ISA_ARCV2
27 #define PREFETCHW " prefetchw [%1] \n"
32 #define ATOMIC_OP(op, c_op, asm_op) \
33 static inline void atomic_##op(int i, atomic_t *v) \
37 __asm__ __volatile__( \
40 " llock %0, [%1] \n" \
41 " " #asm_op " %0, %0, %2 \n" \
42 " scond %0, [%1] \n" \
44 : "=&r"(temp) /* Early clobber, to prevent reg reuse */ \
45 : "r"(&v->counter), "ir"(i) \
49 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
50 static inline int atomic_##op##_return(int i, atomic_t *v) \
55 * Explicit full memory barrier needed before/after as \
56 * LLOCK/SCOND thmeselves don't provide any such semantics \
60 __asm__ __volatile__( \
63 " llock %0, [%1] \n" \
64 " " #asm_op " %0, %0, %2 \n" \
65 " scond %0, [%1] \n" \
68 : "r"(&v->counter), "ir"(i) \
76 #else /* !CONFIG_ARC_HAS_LLSC */
80 /* violating atomic_xxx API locking protocol in UP for optimization sake */
81 #define atomic_set(v, i) (((v)->counter) = (i))
85 static inline void atomic_set(atomic_t *v, int i)
88 * Independent of hardware support, all of the atomic_xxx() APIs need
89 * to follow the same locking rules to make sure that a "hardware"
90 * atomic insn (e.g. LD) doesn't clobber an "emulated" atomic insn
93 * Thus atomic_set() despite being 1 insn (and seemingly atomic)
94 * requires the locking.
98 atomic_ops_lock(flags);
100 atomic_ops_unlock(flags);
106 * Non hardware assisted Atomic-R-M-W
107 * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
110 #define ATOMIC_OP(op, c_op, asm_op) \
111 static inline void atomic_##op(int i, atomic_t *v) \
113 unsigned long flags; \
115 atomic_ops_lock(flags); \
117 atomic_ops_unlock(flags); \
120 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
121 static inline int atomic_##op##_return(int i, atomic_t *v) \
123 unsigned long flags; \
124 unsigned long temp; \
127 * spin lock/unlock provides the needed smp_mb() before/after \
129 atomic_ops_lock(flags); \
133 atomic_ops_unlock(flags); \
138 #endif /* !CONFIG_ARC_HAS_LLSC */
140 #define ATOMIC_OPS(op, c_op, asm_op) \
141 ATOMIC_OP(op, c_op, asm_op) \
142 ATOMIC_OP_RETURN(op, c_op, asm_op)
144 ATOMIC_OPS(add, +=, add)
145 ATOMIC_OPS(sub, -=, sub)
146 ATOMIC_OP(and, &=, and)
148 #define atomic_clear_mask(mask, v) atomic_and(~(mask), (v))
151 #undef ATOMIC_OP_RETURN
155 * __atomic_add_unless - add unless the number is a given value
156 * @v: pointer of type atomic_t
157 * @a: the amount to add to v...
158 * @u: ...unless v is equal to u.
160 * Atomically adds @a to @v, so long as it was not @u.
161 * Returns the old value of @v
163 #define __atomic_add_unless(v, a, u) \
168 * Explicit full memory barrier needed before/after as \
169 * LLOCK/SCOND thmeselves don't provide any such semantics \
173 c = atomic_read(v); \
174 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c)\
182 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
184 #define atomic_inc(v) atomic_add(1, v)
185 #define atomic_dec(v) atomic_sub(1, v)
187 #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
188 #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
189 #define atomic_inc_return(v) atomic_add_return(1, (v))
190 #define atomic_dec_return(v) atomic_sub_return(1, (v))
191 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
193 #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
195 #define ATOMIC_INIT(i) { (i) }
197 #include <asm-generic/atomic64.h>