1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
24117b021SAkinobu Mita #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
34117b021SAkinobu Mita #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
44117b021SAkinobu Mita 
54117b021SAkinobu Mita #include <asm/types.h>
64117b021SAkinobu Mita 
74117b021SAkinobu Mita /**
8cf3ee3c8SMark Rutland  * arch___set_bit - Set a bit in memory
94117b021SAkinobu Mita  * @nr: the bit to set
104117b021SAkinobu Mita  * @addr: the address to start counting from
114117b021SAkinobu Mita  *
124117b021SAkinobu Mita  * Unlike set_bit(), this function is non-atomic and may be reordered.
134117b021SAkinobu Mita  * If it's called on the same region of memory simultaneously, the effect
144117b021SAkinobu Mita  * may be that only one operation succeeds.
154117b021SAkinobu Mita  */
16cf3ee3c8SMark Rutland static __always_inline void
17cf3ee3c8SMark Rutland arch___set_bit(int nr, volatile unsigned long *addr)
184117b021SAkinobu Mita {
19d05be13bSJiri Slaby 	unsigned long mask = BIT_MASK(nr);
20d05be13bSJiri Slaby 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
214117b021SAkinobu Mita 
224117b021SAkinobu Mita 	*p  |= mask;
234117b021SAkinobu Mita }
24*9248e52fSMark Rutland #define __set_bit arch___set_bit
254117b021SAkinobu Mita 
26cf3ee3c8SMark Rutland static __always_inline void
27cf3ee3c8SMark Rutland arch___clear_bit(int nr, volatile unsigned long *addr)
284117b021SAkinobu Mita {
29d05be13bSJiri Slaby 	unsigned long mask = BIT_MASK(nr);
30d05be13bSJiri Slaby 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
314117b021SAkinobu Mita 
324117b021SAkinobu Mita 	*p &= ~mask;
334117b021SAkinobu Mita }
34*9248e52fSMark Rutland #define __clear_bit arch___clear_bit
354117b021SAkinobu Mita 
364117b021SAkinobu Mita /**
37cf3ee3c8SMark Rutland  * arch___change_bit - Toggle a bit in memory
384117b021SAkinobu Mita  * @nr: the bit to change
394117b021SAkinobu Mita  * @addr: the address to start counting from
404117b021SAkinobu Mita  *
414117b021SAkinobu Mita  * Unlike change_bit(), this function is non-atomic and may be reordered.
424117b021SAkinobu Mita  * If it's called on the same region of memory simultaneously, the effect
434117b021SAkinobu Mita  * may be that only one operation succeeds.
444117b021SAkinobu Mita  */
45cf3ee3c8SMark Rutland static __always_inline
46cf3ee3c8SMark Rutland void arch___change_bit(int nr, volatile unsigned long *addr)
474117b021SAkinobu Mita {
48d05be13bSJiri Slaby 	unsigned long mask = BIT_MASK(nr);
49d05be13bSJiri Slaby 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
504117b021SAkinobu Mita 
514117b021SAkinobu Mita 	*p ^= mask;
524117b021SAkinobu Mita }
53*9248e52fSMark Rutland #define __change_bit arch___change_bit
544117b021SAkinobu Mita 
554117b021SAkinobu Mita /**
56cf3ee3c8SMark Rutland  * arch___test_and_set_bit - Set a bit and return its old value
574117b021SAkinobu Mita  * @nr: Bit to set
584117b021SAkinobu Mita  * @addr: Address to count from
594117b021SAkinobu Mita  *
604117b021SAkinobu Mita  * This operation is non-atomic and can be reordered.
614117b021SAkinobu Mita  * If two examples of this operation race, one can appear to succeed
624117b021SAkinobu Mita  * but actually fail.  You must protect multiple accesses with a lock.
634117b021SAkinobu Mita  */
64cf3ee3c8SMark Rutland static __always_inline int
65cf3ee3c8SMark Rutland arch___test_and_set_bit(int nr, volatile unsigned long *addr)
664117b021SAkinobu Mita {
67d05be13bSJiri Slaby 	unsigned long mask = BIT_MASK(nr);
68d05be13bSJiri Slaby 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
694117b021SAkinobu Mita 	unsigned long old = *p;
704117b021SAkinobu Mita 
714117b021SAkinobu Mita 	*p = old | mask;
724117b021SAkinobu Mita 	return (old & mask) != 0;
734117b021SAkinobu Mita }
74*9248e52fSMark Rutland #define __test_and_set_bit arch___test_and_set_bit
754117b021SAkinobu Mita 
764117b021SAkinobu Mita /**
77cf3ee3c8SMark Rutland  * arch___test_and_clear_bit - Clear a bit and return its old value
784117b021SAkinobu Mita  * @nr: Bit to clear
794117b021SAkinobu Mita  * @addr: Address to count from
804117b021SAkinobu Mita  *
814117b021SAkinobu Mita  * This operation is non-atomic and can be reordered.
824117b021SAkinobu Mita  * If two examples of this operation race, one can appear to succeed
834117b021SAkinobu Mita  * but actually fail.  You must protect multiple accesses with a lock.
844117b021SAkinobu Mita  */
85cf3ee3c8SMark Rutland static __always_inline int
86cf3ee3c8SMark Rutland arch___test_and_clear_bit(int nr, volatile unsigned long *addr)
874117b021SAkinobu Mita {
88d05be13bSJiri Slaby 	unsigned long mask = BIT_MASK(nr);
89d05be13bSJiri Slaby 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
904117b021SAkinobu Mita 	unsigned long old = *p;
914117b021SAkinobu Mita 
924117b021SAkinobu Mita 	*p = old & ~mask;
934117b021SAkinobu Mita 	return (old & mask) != 0;
944117b021SAkinobu Mita }
95*9248e52fSMark Rutland #define __test_and_clear_bit arch___test_and_clear_bit
964117b021SAkinobu Mita 
974117b021SAkinobu Mita /* WARNING: non atomic and it can be reordered! */
98cf3ee3c8SMark Rutland static __always_inline int
99cf3ee3c8SMark Rutland arch___test_and_change_bit(int nr, volatile unsigned long *addr)
1004117b021SAkinobu Mita {
101d05be13bSJiri Slaby 	unsigned long mask = BIT_MASK(nr);
102d05be13bSJiri Slaby 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
1034117b021SAkinobu Mita 	unsigned long old = *p;
1044117b021SAkinobu Mita 
1054117b021SAkinobu Mita 	*p = old ^ mask;
1064117b021SAkinobu Mita 	return (old & mask) != 0;
1074117b021SAkinobu Mita }
108*9248e52fSMark Rutland #define __test_and_change_bit arch___test_and_change_bit
1094117b021SAkinobu Mita 
1104117b021SAkinobu Mita /**
111cf3ee3c8SMark Rutland  * arch_test_bit - Determine whether a bit is set
1124117b021SAkinobu Mita  * @nr: bit number to test
1134117b021SAkinobu Mita  * @addr: Address to start counting from
1144117b021SAkinobu Mita  */
115cf3ee3c8SMark Rutland static __always_inline int
116cf3ee3c8SMark Rutland arch_test_bit(int nr, const volatile unsigned long *addr)
1174117b021SAkinobu Mita {
118d05be13bSJiri Slaby 	return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
1194117b021SAkinobu Mita }
120*9248e52fSMark Rutland #define test_bit arch_test_bit
1214117b021SAkinobu Mita 
1224117b021SAkinobu Mita #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */
123