xref: /openbmc/linux/include/asm-generic/atomic.h (revision 34484277b173d0af1bb00532a264940b57920429)
13f7e212dSArnd Bergmann /*
2acac43e2SArun Sharma  * Generic C implementation of atomic counter operations. Usable on
3acac43e2SArun Sharma  * UP systems only. Do not include in machine independent code.
4acac43e2SArun Sharma  *
53f7e212dSArnd Bergmann  * Originally implemented for MN10300.
63f7e212dSArnd Bergmann  *
73f7e212dSArnd Bergmann  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
83f7e212dSArnd Bergmann  * Written by David Howells (dhowells@redhat.com)
93f7e212dSArnd Bergmann  *
103f7e212dSArnd Bergmann  * This program is free software; you can redistribute it and/or
113f7e212dSArnd Bergmann  * modify it under the terms of the GNU General Public Licence
123f7e212dSArnd Bergmann  * as published by the Free Software Foundation; either version
133f7e212dSArnd Bergmann  * 2 of the Licence, or (at your option) any later version.
143f7e212dSArnd Bergmann  */
153f7e212dSArnd Bergmann #ifndef __ASM_GENERIC_ATOMIC_H
163f7e212dSArnd Bergmann #define __ASM_GENERIC_ATOMIC_H
173f7e212dSArnd Bergmann 
18*34484277SDavid Howells #include <asm/cmpxchg.h>
19*34484277SDavid Howells 
203f7e212dSArnd Bergmann #ifdef CONFIG_SMP
217505cb60SMike Frysinger /* Force people to define core atomics */
227505cb60SMike Frysinger # if !defined(atomic_add_return) || !defined(atomic_sub_return) || \
237505cb60SMike Frysinger      !defined(atomic_clear_mask) || !defined(atomic_set_mask)
247505cb60SMike Frysinger #  error "SMP requires a little arch-specific magic"
257505cb60SMike Frysinger # endif
263f7e212dSArnd Bergmann #endif
273f7e212dSArnd Bergmann 
283f7e212dSArnd Bergmann /*
293f7e212dSArnd Bergmann  * Atomic operations that C can't guarantee us.  Useful for
303f7e212dSArnd Bergmann  * resource counting etc..
313f7e212dSArnd Bergmann  */
323f7e212dSArnd Bergmann 
333f7e212dSArnd Bergmann #define ATOMIC_INIT(i)	{ (i) }
343f7e212dSArnd Bergmann 
353f7e212dSArnd Bergmann #ifdef __KERNEL__
363f7e212dSArnd Bergmann 
373f7e212dSArnd Bergmann /**
383f7e212dSArnd Bergmann  * atomic_read - read atomic variable
393f7e212dSArnd Bergmann  * @v: pointer of type atomic_t
403f7e212dSArnd Bergmann  *
4137682177SPeter Fritzsche  * Atomically reads the value of @v.
423f7e212dSArnd Bergmann  */
437505cb60SMike Frysinger #ifndef atomic_read
44f3d46f9dSAnton Blanchard #define atomic_read(v)	(*(volatile int *)&(v)->counter)
457505cb60SMike Frysinger #endif
463f7e212dSArnd Bergmann 
473f7e212dSArnd Bergmann /**
483f7e212dSArnd Bergmann  * atomic_set - set atomic variable
493f7e212dSArnd Bergmann  * @v: pointer of type atomic_t
503f7e212dSArnd Bergmann  * @i: required value
513f7e212dSArnd Bergmann  *
5237682177SPeter Fritzsche  * Atomically sets the value of @v to @i.
533f7e212dSArnd Bergmann  */
543f7e212dSArnd Bergmann #define atomic_set(v, i) (((v)->counter) = (i))
553f7e212dSArnd Bergmann 
56df9ee292SDavid Howells #include <linux/irqflags.h>
573f7e212dSArnd Bergmann #include <asm/system.h>
583f7e212dSArnd Bergmann 
593f7e212dSArnd Bergmann /**
603f7e212dSArnd Bergmann  * atomic_add_return - add integer to atomic variable
613f7e212dSArnd Bergmann  * @i: integer value to add
623f7e212dSArnd Bergmann  * @v: pointer of type atomic_t
633f7e212dSArnd Bergmann  *
643f7e212dSArnd Bergmann  * Atomically adds @i to @v and returns the result
653f7e212dSArnd Bergmann  */
667505cb60SMike Frysinger #ifndef atomic_add_return
673f7e212dSArnd Bergmann static inline int atomic_add_return(int i, atomic_t *v)
683f7e212dSArnd Bergmann {
693f7e212dSArnd Bergmann 	unsigned long flags;
703f7e212dSArnd Bergmann 	int temp;
713f7e212dSArnd Bergmann 
72df9ee292SDavid Howells 	raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */
733f7e212dSArnd Bergmann 	temp = v->counter;
743f7e212dSArnd Bergmann 	temp += i;
753f7e212dSArnd Bergmann 	v->counter = temp;
769e58143dSMichal Simek 	raw_local_irq_restore(flags);
773f7e212dSArnd Bergmann 
783f7e212dSArnd Bergmann 	return temp;
793f7e212dSArnd Bergmann }
807505cb60SMike Frysinger #endif
813f7e212dSArnd Bergmann 
823f7e212dSArnd Bergmann /**
833f7e212dSArnd Bergmann  * atomic_sub_return - subtract integer from atomic variable
843f7e212dSArnd Bergmann  * @i: integer value to subtract
853f7e212dSArnd Bergmann  * @v: pointer of type atomic_t
863f7e212dSArnd Bergmann  *
873f7e212dSArnd Bergmann  * Atomically subtracts @i from @v and returns the result
883f7e212dSArnd Bergmann  */
897505cb60SMike Frysinger #ifndef atomic_sub_return
903f7e212dSArnd Bergmann static inline int atomic_sub_return(int i, atomic_t *v)
913f7e212dSArnd Bergmann {
923f7e212dSArnd Bergmann 	unsigned long flags;
933f7e212dSArnd Bergmann 	int temp;
943f7e212dSArnd Bergmann 
95df9ee292SDavid Howells 	raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */
963f7e212dSArnd Bergmann 	temp = v->counter;
973f7e212dSArnd Bergmann 	temp -= i;
983f7e212dSArnd Bergmann 	v->counter = temp;
999e58143dSMichal Simek 	raw_local_irq_restore(flags);
1003f7e212dSArnd Bergmann 
1013f7e212dSArnd Bergmann 	return temp;
1023f7e212dSArnd Bergmann }
1037505cb60SMike Frysinger #endif
1043f7e212dSArnd Bergmann 
1053f7e212dSArnd Bergmann static inline int atomic_add_negative(int i, atomic_t *v)
1063f7e212dSArnd Bergmann {
1073f7e212dSArnd Bergmann 	return atomic_add_return(i, v) < 0;
1083f7e212dSArnd Bergmann }
1093f7e212dSArnd Bergmann 
1103f7e212dSArnd Bergmann static inline void atomic_add(int i, atomic_t *v)
1113f7e212dSArnd Bergmann {
1123f7e212dSArnd Bergmann 	atomic_add_return(i, v);
1133f7e212dSArnd Bergmann }
1143f7e212dSArnd Bergmann 
1153f7e212dSArnd Bergmann static inline void atomic_sub(int i, atomic_t *v)
1163f7e212dSArnd Bergmann {
1173f7e212dSArnd Bergmann 	atomic_sub_return(i, v);
1183f7e212dSArnd Bergmann }
1193f7e212dSArnd Bergmann 
1203f7e212dSArnd Bergmann static inline void atomic_inc(atomic_t *v)
1213f7e212dSArnd Bergmann {
1223f7e212dSArnd Bergmann 	atomic_add_return(1, v);
1233f7e212dSArnd Bergmann }
1243f7e212dSArnd Bergmann 
1253f7e212dSArnd Bergmann static inline void atomic_dec(atomic_t *v)
1263f7e212dSArnd Bergmann {
1273f7e212dSArnd Bergmann 	atomic_sub_return(1, v);
1283f7e212dSArnd Bergmann }
1293f7e212dSArnd Bergmann 
1303f7e212dSArnd Bergmann #define atomic_dec_return(v)		atomic_sub_return(1, (v))
1313f7e212dSArnd Bergmann #define atomic_inc_return(v)		atomic_add_return(1, (v))
1323f7e212dSArnd Bergmann 
1333f7e212dSArnd Bergmann #define atomic_sub_and_test(i, v)	(atomic_sub_return((i), (v)) == 0)
1343eea44eaSMike Frysinger #define atomic_dec_and_test(v)		(atomic_dec_return(v) == 0)
1353eea44eaSMike Frysinger #define atomic_inc_and_test(v)		(atomic_inc_return(v) == 0)
1363f7e212dSArnd Bergmann 
1378b9d4069SMathieu Lacage #define atomic_xchg(ptr, v)		(xchg(&(ptr)->counter, (v)))
1388b9d4069SMathieu Lacage #define atomic_cmpxchg(v, old, new)	(cmpxchg(&((v)->counter), (old), (new)))
1398b9d4069SMathieu Lacage 
1408b9d4069SMathieu Lacage #define cmpxchg_local(ptr, o, n)				  	       \
1418b9d4069SMathieu Lacage 	((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
1428b9d4069SMathieu Lacage 			(unsigned long)(n), sizeof(*(ptr))))
1438b9d4069SMathieu Lacage 
1448b9d4069SMathieu Lacage #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
1458b9d4069SMathieu Lacage 
146f24219b4SArun Sharma static inline int __atomic_add_unless(atomic_t *v, int a, int u)
1478b9d4069SMathieu Lacage {
1488b9d4069SMathieu Lacage   int c, old;
1498b9d4069SMathieu Lacage   c = atomic_read(v);
1508b9d4069SMathieu Lacage   while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
1518b9d4069SMathieu Lacage     c = old;
152f24219b4SArun Sharma   return c;
1538b9d4069SMathieu Lacage }
1543f7e212dSArnd Bergmann 
155f6081bd3SMike Frysinger /**
156f6081bd3SMike Frysinger  * atomic_clear_mask - Atomically clear bits in atomic variable
157f6081bd3SMike Frysinger  * @mask: Mask of the bits to be cleared
158f6081bd3SMike Frysinger  * @v: pointer of type atomic_t
159f6081bd3SMike Frysinger  *
160f6081bd3SMike Frysinger  * Atomically clears the bits set in @mask from @v
161f6081bd3SMike Frysinger  */
1627505cb60SMike Frysinger #ifndef atomic_clear_mask
163f6081bd3SMike Frysinger static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
1643f7e212dSArnd Bergmann {
1653f7e212dSArnd Bergmann 	unsigned long flags;
1663f7e212dSArnd Bergmann 
1673f7e212dSArnd Bergmann 	mask = ~mask;
1689e58143dSMichal Simek 	raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
169f6081bd3SMike Frysinger 	v->counter &= mask;
1709e58143dSMichal Simek 	raw_local_irq_restore(flags);
1713f7e212dSArnd Bergmann }
1727505cb60SMike Frysinger #endif
1733f7e212dSArnd Bergmann 
17400b3c28bSMike Frysinger /**
17500b3c28bSMike Frysinger  * atomic_set_mask - Atomically set bits in atomic variable
17600b3c28bSMike Frysinger  * @mask: Mask of the bits to be set
17700b3c28bSMike Frysinger  * @v: pointer of type atomic_t
17800b3c28bSMike Frysinger  *
17900b3c28bSMike Frysinger  * Atomically sets the bits set in @mask in @v
18000b3c28bSMike Frysinger  */
1817505cb60SMike Frysinger #ifndef atomic_set_mask
18200b3c28bSMike Frysinger static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
18300b3c28bSMike Frysinger {
18400b3c28bSMike Frysinger 	unsigned long flags;
18500b3c28bSMike Frysinger 
18600b3c28bSMike Frysinger 	raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
18700b3c28bSMike Frysinger 	v->counter |= mask;
18800b3c28bSMike Frysinger 	raw_local_irq_restore(flags);
18900b3c28bSMike Frysinger }
1907505cb60SMike Frysinger #endif
19100b3c28bSMike Frysinger 
1923f7e212dSArnd Bergmann /* Assume that atomic operations are already serializing */
1933f7e212dSArnd Bergmann #define smp_mb__before_atomic_dec()	barrier()
1943f7e212dSArnd Bergmann #define smp_mb__after_atomic_dec()	barrier()
1953f7e212dSArnd Bergmann #define smp_mb__before_atomic_inc()	barrier()
1963f7e212dSArnd Bergmann #define smp_mb__after_atomic_inc()	barrier()
1973f7e212dSArnd Bergmann 
1983f7e212dSArnd Bergmann #endif /* __KERNEL__ */
1993f7e212dSArnd Bergmann #endif /* __ASM_GENERIC_ATOMIC_H */
200