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 /** 8*cf3ee3c8SMark 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 */ 16*cf3ee3c8SMark Rutland static __always_inline void 17*cf3ee3c8SMark 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*cf3ee3c8SMark Rutland #define arch___set_bit_uses_plain_access 254117b021SAkinobu Mita 26*cf3ee3c8SMark Rutland static __always_inline void 27*cf3ee3c8SMark 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*cf3ee3c8SMark Rutland #define arch___clear_bit_uses_plain_access 354117b021SAkinobu Mita 364117b021SAkinobu Mita /** 37*cf3ee3c8SMark 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 */ 45*cf3ee3c8SMark Rutland static __always_inline 46*cf3ee3c8SMark 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*cf3ee3c8SMark Rutland #define arch___change_bit_uses_plain_access 544117b021SAkinobu Mita 554117b021SAkinobu Mita /** 56*cf3ee3c8SMark 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 */ 64*cf3ee3c8SMark Rutland static __always_inline int 65*cf3ee3c8SMark 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*cf3ee3c8SMark Rutland #define arch___test_and_set_bit_uses_plain_access 754117b021SAkinobu Mita 764117b021SAkinobu Mita /** 77*cf3ee3c8SMark 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 */ 85*cf3ee3c8SMark Rutland static __always_inline int 86*cf3ee3c8SMark 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*cf3ee3c8SMark Rutland #define arch___test_and_clear_bit_uses_plain_access 964117b021SAkinobu Mita 974117b021SAkinobu Mita /* WARNING: non atomic and it can be reordered! */ 98*cf3ee3c8SMark Rutland static __always_inline int 99*cf3ee3c8SMark 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*cf3ee3c8SMark Rutland #define arch___test_and_change_bit_uses_plain_access 1094117b021SAkinobu Mita 1104117b021SAkinobu Mita /** 111*cf3ee3c8SMark 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 */ 115*cf3ee3c8SMark Rutland static __always_inline int 116*cf3ee3c8SMark 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*cf3ee3c8SMark Rutland #define arch_test_bit_uses_plain_access 121*cf3ee3c8SMark Rutland 122*cf3ee3c8SMark Rutland #include <asm-generic/bitops/instrumented-non-atomic.h> 1234117b021SAkinobu Mita 1244117b021SAkinobu Mita #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */ 125