^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __ARCH_H8300_ATOMIC__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __ARCH_H8300_ATOMIC__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/cmpxchg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/irqflags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Atomic operations that C can't guarantee us. Useful for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * resource counting etc..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define atomic_read(v) READ_ONCE((v)->counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define ATOMIC_OP_RETURN(op, c_op) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static inline int atomic_##op##_return(int i, atomic_t *v) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) h8300flags flags; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) flags = arch_local_irq_save(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ret = v->counter c_op i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) arch_local_irq_restore(flags); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define ATOMIC_FETCH_OP(op, c_op) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static inline int atomic_fetch_##op(int i, atomic_t *v) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) h8300flags flags; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) flags = arch_local_irq_save(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ret = v->counter; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) v->counter c_op i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) arch_local_irq_restore(flags); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define ATOMIC_OP(op, c_op) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static inline void atomic_##op(int i, atomic_t *v) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) h8300flags flags; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) flags = arch_local_irq_save(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) v->counter c_op i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) arch_local_irq_restore(flags); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ATOMIC_OP_RETURN(add, +=)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ATOMIC_OP_RETURN(sub, -=)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define ATOMIC_OPS(op, c_op) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ATOMIC_OP(op, c_op) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ATOMIC_FETCH_OP(op, c_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ATOMIC_OPS(and, &=)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ATOMIC_OPS(or, |=)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ATOMIC_OPS(xor, ^=)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ATOMIC_OPS(add, +=)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ATOMIC_OPS(sub, -=)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #undef ATOMIC_OPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #undef ATOMIC_OP_RETURN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #undef ATOMIC_OP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) h8300flags flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) flags = arch_local_irq_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ret = v->counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (likely(ret == old))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) v->counter = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) arch_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) h8300flags flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) flags = arch_local_irq_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ret = v->counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (ret != u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) v->counter += a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) arch_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define atomic_fetch_add_unless atomic_fetch_add_unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #endif /* __ARCH_H8300_ATOMIC __ */