1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ARCH_H8300_ATOMIC__
3 #define __ARCH_H8300_ATOMIC__
5 #include <linux/types.h>
6 #include <asm/cmpxchg.h>
9 * Atomic operations that C can't guarantee us. Useful for
10 * resource counting etc..
13 #define ATOMIC_INIT(i) { (i) }
15 #define atomic_read(v) READ_ONCE((v)->counter)
16 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
18 #include <linux/kernel.h>
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 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
73 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
75 #define atomic_inc_return(v) atomic_add_return(1, v)
76 #define atomic_dec_return(v) atomic_sub_return(1, v)
78 #define atomic_inc(v) (void)atomic_inc_return(v)
79 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
81 #define atomic_dec(v) (void)atomic_dec_return(v)
82 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
84 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
89 flags = arch_local_irq_save();
91 if (likely(ret == old))
93 arch_local_irq_restore(flags);
97 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
102 flags = arch_local_irq_save();
106 arch_local_irq_restore(flags);
110 #endif /* __ARCH_H8300_ATOMIC __ */