xref: /linux/tools/include/asm-generic/bitops/non-atomic.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
19b89a035SMatthew Wilcox /* SPDX-License-Identifier: GPL-2.0 */
29b89a035SMatthew Wilcox #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
39b89a035SMatthew Wilcox #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
49b89a035SMatthew Wilcox 
50e862838SAlexander Lobakin #include <linux/bits.h>
69b89a035SMatthew Wilcox 
79b89a035SMatthew Wilcox /**
8*e69eb9c4SAlexander Lobakin  * ___set_bit - Set a bit in memory
99b89a035SMatthew Wilcox  * @nr: the bit to set
109b89a035SMatthew Wilcox  * @addr: the address to start counting from
119b89a035SMatthew Wilcox  *
129b89a035SMatthew Wilcox  * Unlike set_bit(), this function is non-atomic and may be reordered.
139b89a035SMatthew Wilcox  * If it's called on the same region of memory simultaneously, the effect
149b89a035SMatthew Wilcox  * may be that only one operation succeeds.
159b89a035SMatthew Wilcox  */
160e862838SAlexander Lobakin static __always_inline void
___set_bit(unsigned long nr,volatile unsigned long * addr)17*e69eb9c4SAlexander Lobakin ___set_bit(unsigned long nr, volatile unsigned long *addr)
189b89a035SMatthew Wilcox {
199b89a035SMatthew Wilcox 	unsigned long mask = BIT_MASK(nr);
209b89a035SMatthew Wilcox 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
219b89a035SMatthew Wilcox 
229b89a035SMatthew Wilcox 	*p  |= mask;
239b89a035SMatthew Wilcox }
249b89a035SMatthew Wilcox 
250e862838SAlexander Lobakin static __always_inline void
___clear_bit(unsigned long nr,volatile unsigned long * addr)26*e69eb9c4SAlexander Lobakin ___clear_bit(unsigned long nr, volatile unsigned long *addr)
279b89a035SMatthew Wilcox {
289b89a035SMatthew Wilcox 	unsigned long mask = BIT_MASK(nr);
299b89a035SMatthew Wilcox 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
309b89a035SMatthew Wilcox 
319b89a035SMatthew Wilcox 	*p &= ~mask;
329b89a035SMatthew Wilcox }
339b89a035SMatthew Wilcox 
349b89a035SMatthew Wilcox /**
35*e69eb9c4SAlexander Lobakin  * ___change_bit - Toggle a bit in memory
369b89a035SMatthew Wilcox  * @nr: the bit to change
379b89a035SMatthew Wilcox  * @addr: the address to start counting from
389b89a035SMatthew Wilcox  *
399b89a035SMatthew Wilcox  * Unlike change_bit(), this function is non-atomic and may be reordered.
409b89a035SMatthew Wilcox  * If it's called on the same region of memory simultaneously, the effect
419b89a035SMatthew Wilcox  * may be that only one operation succeeds.
429b89a035SMatthew Wilcox  */
430e862838SAlexander Lobakin static __always_inline void
___change_bit(unsigned long nr,volatile unsigned long * addr)44*e69eb9c4SAlexander Lobakin ___change_bit(unsigned long nr, volatile unsigned long *addr)
459b89a035SMatthew Wilcox {
469b89a035SMatthew Wilcox 	unsigned long mask = BIT_MASK(nr);
479b89a035SMatthew Wilcox 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
489b89a035SMatthew Wilcox 
499b89a035SMatthew Wilcox 	*p ^= mask;
509b89a035SMatthew Wilcox }
519b89a035SMatthew Wilcox 
529b89a035SMatthew Wilcox /**
53*e69eb9c4SAlexander Lobakin  * ___test_and_set_bit - Set a bit and return its old value
549b89a035SMatthew Wilcox  * @nr: Bit to set
559b89a035SMatthew Wilcox  * @addr: Address to count from
569b89a035SMatthew Wilcox  *
579b89a035SMatthew Wilcox  * This operation is non-atomic and can be reordered.
589b89a035SMatthew Wilcox  * If two examples of this operation race, one can appear to succeed
599b89a035SMatthew Wilcox  * but actually fail.  You must protect multiple accesses with a lock.
609b89a035SMatthew Wilcox  */
610e862838SAlexander Lobakin static __always_inline bool
___test_and_set_bit(unsigned long nr,volatile unsigned long * addr)62*e69eb9c4SAlexander Lobakin ___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
639b89a035SMatthew Wilcox {
649b89a035SMatthew Wilcox 	unsigned long mask = BIT_MASK(nr);
659b89a035SMatthew Wilcox 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
669b89a035SMatthew Wilcox 	unsigned long old = *p;
679b89a035SMatthew Wilcox 
689b89a035SMatthew Wilcox 	*p = old | mask;
699b89a035SMatthew Wilcox 	return (old & mask) != 0;
709b89a035SMatthew Wilcox }
719b89a035SMatthew Wilcox 
729b89a035SMatthew Wilcox /**
73*e69eb9c4SAlexander Lobakin  * ___test_and_clear_bit - Clear a bit and return its old value
749b89a035SMatthew Wilcox  * @nr: Bit to clear
759b89a035SMatthew Wilcox  * @addr: Address to count from
769b89a035SMatthew Wilcox  *
779b89a035SMatthew Wilcox  * This operation is non-atomic and can be reordered.
789b89a035SMatthew Wilcox  * If two examples of this operation race, one can appear to succeed
799b89a035SMatthew Wilcox  * but actually fail.  You must protect multiple accesses with a lock.
809b89a035SMatthew Wilcox  */
810e862838SAlexander Lobakin static __always_inline bool
___test_and_clear_bit(unsigned long nr,volatile unsigned long * addr)82*e69eb9c4SAlexander Lobakin ___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
839b89a035SMatthew Wilcox {
849b89a035SMatthew Wilcox 	unsigned long mask = BIT_MASK(nr);
859b89a035SMatthew Wilcox 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
869b89a035SMatthew Wilcox 	unsigned long old = *p;
879b89a035SMatthew Wilcox 
889b89a035SMatthew Wilcox 	*p = old & ~mask;
899b89a035SMatthew Wilcox 	return (old & mask) != 0;
909b89a035SMatthew Wilcox }
919b89a035SMatthew Wilcox 
929b89a035SMatthew Wilcox /* WARNING: non atomic and it can be reordered! */
930e862838SAlexander Lobakin static __always_inline bool
___test_and_change_bit(unsigned long nr,volatile unsigned long * addr)94*e69eb9c4SAlexander Lobakin ___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
959b89a035SMatthew Wilcox {
969b89a035SMatthew Wilcox 	unsigned long mask = BIT_MASK(nr);
979b89a035SMatthew Wilcox 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
989b89a035SMatthew Wilcox 	unsigned long old = *p;
999b89a035SMatthew Wilcox 
1009b89a035SMatthew Wilcox 	*p = old ^ mask;
1019b89a035SMatthew Wilcox 	return (old & mask) != 0;
1029b89a035SMatthew Wilcox }
1039b89a035SMatthew Wilcox 
1049b89a035SMatthew Wilcox /**
105*e69eb9c4SAlexander Lobakin  * _test_bit - Determine whether a bit is set
1069b89a035SMatthew Wilcox  * @nr: bit number to test
1079b89a035SMatthew Wilcox  * @addr: Address to start counting from
1089b89a035SMatthew Wilcox  */
1090e862838SAlexander Lobakin static __always_inline bool
_test_bit(unsigned long nr,const volatile unsigned long * addr)110*e69eb9c4SAlexander Lobakin _test_bit(unsigned long nr, const volatile unsigned long *addr)
1119b89a035SMatthew Wilcox {
1129b89a035SMatthew Wilcox 	return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
1139b89a035SMatthew Wilcox }
1149b89a035SMatthew Wilcox 
1159b89a035SMatthew Wilcox #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */
116