1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_METAG_ATOMIC_LOCK1_H
3 #define __ASM_METAG_ATOMIC_LOCK1_H
5 #define ATOMIC_INIT(i) { (i) }
7 #include <linux/compiler.h>
9 #include <asm/barrier.h>
10 #include <asm/global_lock.h>
12 static inline int atomic_read(const atomic_t *v)
14 return READ_ONCE((v)->counter);
18 * atomic_set needs to be take the lock to protect atomic_add_unless from a
19 * possible race, as it reads the counter twice:
22 * atomic_add_unless(1, 0)
23 * ret = v->counter (non-zero)
24 * if (ret != u) v->counter = 0
25 * v->counter += 1 (counter set to 1)
27 * Making atomic_set take the lock ensures that ordering and logical
28 * consistency is preserved.
30 static inline int atomic_set(atomic_t *v, int i)
34 __global_lock1(flags);
37 __global_unlock1(flags);
41 #define atomic_set_release(v, i) atomic_set((v), (i))
43 #define ATOMIC_OP(op, c_op) \
44 static inline void atomic_##op(int i, atomic_t *v) \
46 unsigned long flags; \
48 __global_lock1(flags); \
51 __global_unlock1(flags); \
54 #define ATOMIC_OP_RETURN(op, c_op) \
55 static inline int atomic_##op##_return(int i, atomic_t *v) \
57 unsigned long result; \
58 unsigned long flags; \
60 __global_lock1(flags); \
61 result = v->counter; \
64 v->counter = result; \
65 __global_unlock1(flags); \
70 #define ATOMIC_FETCH_OP(op, c_op) \
71 static inline int atomic_fetch_##op(int i, atomic_t *v) \
73 unsigned long result; \
74 unsigned long flags; \
76 __global_lock1(flags); \
77 result = v->counter; \
80 __global_unlock1(flags); \
85 #define ATOMIC_OPS(op, c_op) \
87 ATOMIC_OP_RETURN(op, c_op) \
88 ATOMIC_FETCH_OP(op, c_op)
94 #define ATOMIC_OPS(op, c_op) \
96 ATOMIC_FETCH_OP(op, c_op)
103 #undef ATOMIC_FETCH_OP
104 #undef ATOMIC_OP_RETURN
107 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
112 __global_lock1(flags);
118 __global_unlock1(flags);
123 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
125 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
130 __global_lock1(flags);
136 __global_unlock1(flags);
141 static inline int atomic_sub_if_positive(int i, atomic_t *v)
146 __global_lock1(flags);
147 ret = v->counter - 1;
152 __global_unlock1(flags);
157 #endif /* __ASM_METAG_ATOMIC_LOCK1_H */