^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 _ASM_GENERIC_LOCAL64_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _ASM_GENERIC_LOCAL64_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * A signed long type for operations which are atomic for a single CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Usually used in combination with per-cpu variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This is the default implementation, which uses atomic64_t. Which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * rather pointless. The whole point behind local64_t is that some processors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * can perform atomic adds and subtracts in a manner which is atomic wrt IRQs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * running on this CPU. local64_t allows exploitation of such capabilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* Implement in terms of atomics. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #if BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/local.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) local_t a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) } local64_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define LOCAL64_INIT(i) { LOCAL_INIT(i) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define local64_read(l) local_read(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define local64_set(l,i) local_set((&(l)->a),(i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define local64_inc(l) local_inc(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define local64_dec(l) local_dec(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define local64_add(i,l) local_add((i),(&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define local64_sub(i,l) local_sub((i),(&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define local64_sub_and_test(i, l) local_sub_and_test((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define local64_dec_and_test(l) local_dec_and_test(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define local64_inc_and_test(l) local_inc_and_test(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define local64_add_negative(i, l) local_add_negative((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define local64_add_return(i, l) local_add_return((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define local64_sub_return(i, l) local_sub_return((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define local64_inc_return(l) local_inc_return(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define local64_cmpxchg(l, o, n) local_cmpxchg((&(l)->a), (o), (n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define local64_xchg(l, n) local_xchg((&(l)->a), (n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define local64_add_unless(l, _a, u) local_add_unless((&(l)->a), (_a), (u))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define local64_inc_not_zero(l) local_inc_not_zero(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Non-atomic variants, ie. preemption disabled and won't be touched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * in interrupt, etc. Some archs can optimize this case well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define __local64_inc(l) local64_set((l), local64_read(l) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define __local64_dec(l) local64_set((l), local64_read(l) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define __local64_add(i,l) local64_set((l), local64_read(l) + (i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define __local64_sub(i,l) local64_set((l), local64_read(l) - (i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #else /* BITS_PER_LONG != 64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* Don't use typedef: don't want them to be mixed with atomic_t's. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) atomic64_t a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) } local64_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define LOCAL64_INIT(i) { ATOMIC_LONG_INIT(i) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define local64_read(l) atomic64_read(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define local64_set(l,i) atomic64_set((&(l)->a),(i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define local64_inc(l) atomic64_inc(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define local64_dec(l) atomic64_dec(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define local64_add(i,l) atomic64_add((i),(&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define local64_sub(i,l) atomic64_sub((i),(&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define local64_sub_and_test(i, l) atomic64_sub_and_test((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define local64_dec_and_test(l) atomic64_dec_and_test(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define local64_inc_and_test(l) atomic64_inc_and_test(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define local64_add_negative(i, l) atomic64_add_negative((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define local64_add_return(i, l) atomic64_add_return((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define local64_sub_return(i, l) atomic64_sub_return((i), (&(l)->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define local64_inc_return(l) atomic64_inc_return(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define local64_cmpxchg(l, o, n) atomic64_cmpxchg((&(l)->a), (o), (n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define local64_xchg(l, n) atomic64_xchg((&(l)->a), (n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define local64_add_unless(l, _a, u) atomic64_add_unless((&(l)->a), (_a), (u))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define local64_inc_not_zero(l) atomic64_inc_not_zero(&(l)->a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* Non-atomic variants, ie. preemption disabled and won't be touched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * in interrupt, etc. Some archs can optimize this case well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define __local64_inc(l) local64_set((l), local64_read(l) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define __local64_dec(l) local64_set((l), local64_read(l) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define __local64_add(i,l) local64_set((l), local64_read(l) + (i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define __local64_sub(i,l) local64_set((l), local64_read(l) - (i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #endif /* BITS_PER_LONG != 64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #endif /* _ASM_GENERIC_LOCAL64_H */