xref: /openbmc/linux/include/asm-generic/local64.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21996bda2SPeter Zijlstra #ifndef _ASM_GENERIC_LOCAL64_H
31996bda2SPeter Zijlstra #define _ASM_GENERIC_LOCAL64_H
41996bda2SPeter Zijlstra 
51996bda2SPeter Zijlstra #include <linux/percpu.h>
61996bda2SPeter Zijlstra #include <asm/types.h>
71996bda2SPeter Zijlstra 
81996bda2SPeter Zijlstra /*
91996bda2SPeter Zijlstra  * A signed long type for operations which are atomic for a single CPU.
101996bda2SPeter Zijlstra  * Usually used in combination with per-cpu variables.
111996bda2SPeter Zijlstra  *
121996bda2SPeter Zijlstra  * This is the default implementation, which uses atomic64_t.  Which is
131996bda2SPeter Zijlstra  * rather pointless.  The whole point behind local64_t is that some processors
141996bda2SPeter Zijlstra  * can perform atomic adds and subtracts in a manner which is atomic wrt IRQs
151996bda2SPeter Zijlstra  * running on this CPU.  local64_t allows exploitation of such capabilities.
161996bda2SPeter Zijlstra  */
171996bda2SPeter Zijlstra 
181996bda2SPeter Zijlstra /* Implement in terms of atomics. */
191996bda2SPeter Zijlstra 
201996bda2SPeter Zijlstra #if BITS_PER_LONG == 64
211996bda2SPeter Zijlstra 
221996bda2SPeter Zijlstra #include <asm/local.h>
231996bda2SPeter Zijlstra 
241996bda2SPeter Zijlstra typedef struct {
251996bda2SPeter Zijlstra 	local_t a;
261996bda2SPeter Zijlstra } local64_t;
271996bda2SPeter Zijlstra 
281996bda2SPeter Zijlstra #define LOCAL64_INIT(i)	{ LOCAL_INIT(i) }
291996bda2SPeter Zijlstra 
301996bda2SPeter Zijlstra #define local64_read(l)		local_read(&(l)->a)
311996bda2SPeter Zijlstra #define local64_set(l,i)	local_set((&(l)->a),(i))
321996bda2SPeter Zijlstra #define local64_inc(l)		local_inc(&(l)->a)
331996bda2SPeter Zijlstra #define local64_dec(l)		local_dec(&(l)->a)
341996bda2SPeter Zijlstra #define local64_add(i,l)	local_add((i),(&(l)->a))
351996bda2SPeter Zijlstra #define local64_sub(i,l)	local_sub((i),(&(l)->a))
361996bda2SPeter Zijlstra 
371996bda2SPeter Zijlstra #define local64_sub_and_test(i, l) local_sub_and_test((i), (&(l)->a))
381996bda2SPeter Zijlstra #define local64_dec_and_test(l) local_dec_and_test(&(l)->a)
391996bda2SPeter Zijlstra #define local64_inc_and_test(l) local_inc_and_test(&(l)->a)
401996bda2SPeter Zijlstra #define local64_add_negative(i, l) local_add_negative((i), (&(l)->a))
411996bda2SPeter Zijlstra #define local64_add_return(i, l) local_add_return((i), (&(l)->a))
421996bda2SPeter Zijlstra #define local64_sub_return(i, l) local_sub_return((i), (&(l)->a))
431996bda2SPeter Zijlstra #define local64_inc_return(l)	local_inc_return(&(l)->a)
441996bda2SPeter Zijlstra 
local64_cmpxchg(local64_t * l,s64 old,s64 new)45*8fc4fddaSUros Bizjak static inline s64 local64_cmpxchg(local64_t *l, s64 old, s64 new)
46*8fc4fddaSUros Bizjak {
47*8fc4fddaSUros Bizjak 	return local_cmpxchg(&l->a, old, new);
48*8fc4fddaSUros Bizjak }
49*8fc4fddaSUros Bizjak 
local64_try_cmpxchg(local64_t * l,s64 * old,s64 new)50*8fc4fddaSUros Bizjak static inline bool local64_try_cmpxchg(local64_t *l, s64 *old, s64 new)
51*8fc4fddaSUros Bizjak {
52*8fc4fddaSUros Bizjak 	return local_try_cmpxchg(&l->a, (long *)old, new);
53*8fc4fddaSUros Bizjak }
54*8fc4fddaSUros Bizjak 
551996bda2SPeter Zijlstra #define local64_xchg(l, n)	local_xchg((&(l)->a), (n))
561996bda2SPeter Zijlstra #define local64_add_unless(l, _a, u) local_add_unless((&(l)->a), (_a), (u))
571996bda2SPeter Zijlstra #define local64_inc_not_zero(l)	local_inc_not_zero(&(l)->a)
581996bda2SPeter Zijlstra 
591996bda2SPeter Zijlstra /* Non-atomic variants, ie. preemption disabled and won't be touched
601996bda2SPeter Zijlstra  * in interrupt, etc.  Some archs can optimize this case well. */
611996bda2SPeter Zijlstra #define __local64_inc(l)	local64_set((l), local64_read(l) + 1)
621996bda2SPeter Zijlstra #define __local64_dec(l)	local64_set((l), local64_read(l) - 1)
631996bda2SPeter Zijlstra #define __local64_add(i,l)	local64_set((l), local64_read(l) + (i))
641996bda2SPeter Zijlstra #define __local64_sub(i,l)	local64_set((l), local64_read(l) - (i))
651996bda2SPeter Zijlstra 
661996bda2SPeter Zijlstra #else /* BITS_PER_LONG != 64 */
671996bda2SPeter Zijlstra 
6860063497SArun Sharma #include <linux/atomic.h>
691996bda2SPeter Zijlstra 
701996bda2SPeter Zijlstra /* Don't use typedef: don't want them to be mixed with atomic_t's. */
711996bda2SPeter Zijlstra typedef struct {
721996bda2SPeter Zijlstra 	atomic64_t a;
731996bda2SPeter Zijlstra } local64_t;
741996bda2SPeter Zijlstra 
751996bda2SPeter Zijlstra #define LOCAL64_INIT(i)	{ ATOMIC_LONG_INIT(i) }
761996bda2SPeter Zijlstra 
771996bda2SPeter Zijlstra #define local64_read(l)		atomic64_read(&(l)->a)
781996bda2SPeter Zijlstra #define local64_set(l,i)	atomic64_set((&(l)->a),(i))
791996bda2SPeter Zijlstra #define local64_inc(l)		atomic64_inc(&(l)->a)
801996bda2SPeter Zijlstra #define local64_dec(l)		atomic64_dec(&(l)->a)
811996bda2SPeter Zijlstra #define local64_add(i,l)	atomic64_add((i),(&(l)->a))
821996bda2SPeter Zijlstra #define local64_sub(i,l)	atomic64_sub((i),(&(l)->a))
831996bda2SPeter Zijlstra 
841996bda2SPeter Zijlstra #define local64_sub_and_test(i, l) atomic64_sub_and_test((i), (&(l)->a))
851996bda2SPeter Zijlstra #define local64_dec_and_test(l) atomic64_dec_and_test(&(l)->a)
861996bda2SPeter Zijlstra #define local64_inc_and_test(l) atomic64_inc_and_test(&(l)->a)
871996bda2SPeter Zijlstra #define local64_add_negative(i, l) atomic64_add_negative((i), (&(l)->a))
881996bda2SPeter Zijlstra #define local64_add_return(i, l) atomic64_add_return((i), (&(l)->a))
891996bda2SPeter Zijlstra #define local64_sub_return(i, l) atomic64_sub_return((i), (&(l)->a))
901996bda2SPeter Zijlstra #define local64_inc_return(l)	atomic64_inc_return(&(l)->a)
911996bda2SPeter Zijlstra 
921996bda2SPeter Zijlstra #define local64_cmpxchg(l, o, n) atomic64_cmpxchg((&(l)->a), (o), (n))
93*8fc4fddaSUros Bizjak #define local64_try_cmpxchg(l, po, n) atomic64_try_cmpxchg((&(l)->a), (po), (n))
941996bda2SPeter Zijlstra #define local64_xchg(l, n)	atomic64_xchg((&(l)->a), (n))
951996bda2SPeter Zijlstra #define local64_add_unless(l, _a, u) atomic64_add_unless((&(l)->a), (_a), (u))
961996bda2SPeter Zijlstra #define local64_inc_not_zero(l)	atomic64_inc_not_zero(&(l)->a)
971996bda2SPeter Zijlstra 
981996bda2SPeter Zijlstra /* Non-atomic variants, ie. preemption disabled and won't be touched
991996bda2SPeter Zijlstra  * in interrupt, etc.  Some archs can optimize this case well. */
1001996bda2SPeter Zijlstra #define __local64_inc(l)	local64_set((l), local64_read(l) + 1)
1011996bda2SPeter Zijlstra #define __local64_dec(l)	local64_set((l), local64_read(l) - 1)
1021996bda2SPeter Zijlstra #define __local64_add(i,l)	local64_set((l), local64_read(l) + (i))
1031996bda2SPeter Zijlstra #define __local64_sub(i,l)	local64_set((l), local64_read(l) - (i))
1041996bda2SPeter Zijlstra 
1051996bda2SPeter Zijlstra #endif /* BITS_PER_LONG != 64 */
1061996bda2SPeter Zijlstra 
1071996bda2SPeter Zijlstra #endif /* _ASM_GENERIC_LOCAL64_H */
108