1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ARCH_H8300_ATOMIC__
3 #define __ARCH_H8300_ATOMIC__
5 #include <linux/compiler.h>
6 #include <linux/types.h>
7 #include <asm/cmpxchg.h>
8 #include <asm/irqflags.h>
11 * Atomic operations that C can't guarantee us. Useful for
12 * resource counting etc..
15 #define ATOMIC_INIT(i) { (i) }
17 #define atomic_read(v) READ_ONCE((v)->counter)
18 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
20 #define ATOMIC_OP_RETURN(op, c_op) \
21 static inline int atomic_##op##_return(int i, atomic_t *v) \
26 flags = arch_local_irq_save(); \
27 ret = v->counter c_op i; \
28 arch_local_irq_restore(flags); \
32 #define ATOMIC_FETCH_OP(op, c_op) \
33 static inline int atomic_fetch_##op(int i, atomic_t *v) \
38 flags = arch_local_irq_save(); \
41 arch_local_irq_restore(flags); \
45 #define ATOMIC_OP(op, c_op) \
46 static inline void atomic_##op(int i, atomic_t *v) \
50 flags = arch_local_irq_save(); \
52 arch_local_irq_restore(flags); \
55 ATOMIC_OP_RETURN(add, +=)
56 ATOMIC_OP_RETURN(sub, -=)
58 #define ATOMIC_OPS(op, c_op) \
60 ATOMIC_FETCH_OP(op, c_op)
69 #undef ATOMIC_OP_RETURN
72 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
77 flags = arch_local_irq_save();
79 if (likely(ret == old))
81 arch_local_irq_restore(flags);
85 static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
90 flags = arch_local_irq_save();
94 arch_local_irq_restore(flags);
97 #define atomic_fetch_add_unless atomic_fetch_add_unless
99 #endif /* __ARCH_H8300_ATOMIC __ */