14117b021SAkinobu Mita #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
24117b021SAkinobu Mita #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
34117b021SAkinobu Mita 
44117b021SAkinobu Mita #include <asm/types.h>
54117b021SAkinobu Mita 
64117b021SAkinobu Mita #define BITOP_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
74117b021SAkinobu Mita #define BITOP_WORD(nr)		((nr) / BITS_PER_LONG)
84117b021SAkinobu Mita 
94117b021SAkinobu Mita /**
104117b021SAkinobu Mita  * __set_bit - Set a bit in memory
114117b021SAkinobu Mita  * @nr: the bit to set
124117b021SAkinobu Mita  * @addr: the address to start counting from
134117b021SAkinobu Mita  *
144117b021SAkinobu Mita  * Unlike set_bit(), this function is non-atomic and may be reordered.
154117b021SAkinobu Mita  * If it's called on the same region of memory simultaneously, the effect
164117b021SAkinobu Mita  * may be that only one operation succeeds.
174117b021SAkinobu Mita  */
184117b021SAkinobu Mita static inline void __set_bit(int nr, volatile unsigned long *addr)
194117b021SAkinobu Mita {
204117b021SAkinobu Mita 	unsigned long mask = BITOP_MASK(nr);
214117b021SAkinobu Mita 	unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
224117b021SAkinobu Mita 
234117b021SAkinobu Mita 	*p  |= mask;
244117b021SAkinobu Mita }
254117b021SAkinobu Mita 
264117b021SAkinobu Mita static inline void __clear_bit(int nr, volatile unsigned long *addr)
274117b021SAkinobu Mita {
284117b021SAkinobu Mita 	unsigned long mask = BITOP_MASK(nr);
294117b021SAkinobu Mita 	unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
304117b021SAkinobu Mita 
314117b021SAkinobu Mita 	*p &= ~mask;
324117b021SAkinobu Mita }
334117b021SAkinobu Mita 
344117b021SAkinobu Mita /**
354117b021SAkinobu Mita  * __change_bit - Toggle a bit in memory
364117b021SAkinobu Mita  * @nr: the bit to change
374117b021SAkinobu Mita  * @addr: the address to start counting from
384117b021SAkinobu Mita  *
394117b021SAkinobu Mita  * Unlike change_bit(), this function is non-atomic and may be reordered.
404117b021SAkinobu Mita  * If it's called on the same region of memory simultaneously, the effect
414117b021SAkinobu Mita  * may be that only one operation succeeds.
424117b021SAkinobu Mita  */
434117b021SAkinobu Mita static inline void __change_bit(int nr, volatile unsigned long *addr)
444117b021SAkinobu Mita {
454117b021SAkinobu Mita 	unsigned long mask = BITOP_MASK(nr);
464117b021SAkinobu Mita 	unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
474117b021SAkinobu Mita 
484117b021SAkinobu Mita 	*p ^= mask;
494117b021SAkinobu Mita }
504117b021SAkinobu Mita 
514117b021SAkinobu Mita /**
524117b021SAkinobu Mita  * __test_and_set_bit - Set a bit and return its old value
534117b021SAkinobu Mita  * @nr: Bit to set
544117b021SAkinobu Mita  * @addr: Address to count from
554117b021SAkinobu Mita  *
564117b021SAkinobu Mita  * This operation is non-atomic and can be reordered.
574117b021SAkinobu Mita  * If two examples of this operation race, one can appear to succeed
584117b021SAkinobu Mita  * but actually fail.  You must protect multiple accesses with a lock.
594117b021SAkinobu Mita  */
604117b021SAkinobu Mita static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
614117b021SAkinobu Mita {
624117b021SAkinobu Mita 	unsigned long mask = BITOP_MASK(nr);
634117b021SAkinobu Mita 	unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
644117b021SAkinobu Mita 	unsigned long old = *p;
654117b021SAkinobu Mita 
664117b021SAkinobu Mita 	*p = old | mask;
674117b021SAkinobu Mita 	return (old & mask) != 0;
684117b021SAkinobu Mita }
694117b021SAkinobu Mita 
704117b021SAkinobu Mita /**
714117b021SAkinobu Mita  * __test_and_clear_bit - Clear a bit and return its old value
724117b021SAkinobu Mita  * @nr: Bit to clear
734117b021SAkinobu Mita  * @addr: Address to count from
744117b021SAkinobu Mita  *
754117b021SAkinobu Mita  * This operation is non-atomic and can be reordered.
764117b021SAkinobu Mita  * If two examples of this operation race, one can appear to succeed
774117b021SAkinobu Mita  * but actually fail.  You must protect multiple accesses with a lock.
784117b021SAkinobu Mita  */
794117b021SAkinobu Mita static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
804117b021SAkinobu Mita {
814117b021SAkinobu Mita 	unsigned long mask = BITOP_MASK(nr);
824117b021SAkinobu Mita 	unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
834117b021SAkinobu Mita 	unsigned long old = *p;
844117b021SAkinobu Mita 
854117b021SAkinobu Mita 	*p = old & ~mask;
864117b021SAkinobu Mita 	return (old & mask) != 0;
874117b021SAkinobu Mita }
884117b021SAkinobu Mita 
894117b021SAkinobu Mita /* WARNING: non atomic and it can be reordered! */
904117b021SAkinobu Mita static inline int __test_and_change_bit(int nr,
914117b021SAkinobu Mita 					    volatile unsigned long *addr)
924117b021SAkinobu Mita {
934117b021SAkinobu Mita 	unsigned long mask = BITOP_MASK(nr);
944117b021SAkinobu Mita 	unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
954117b021SAkinobu Mita 	unsigned long old = *p;
964117b021SAkinobu Mita 
974117b021SAkinobu Mita 	*p = old ^ mask;
984117b021SAkinobu Mita 	return (old & mask) != 0;
994117b021SAkinobu Mita }
1004117b021SAkinobu Mita 
1014117b021SAkinobu Mita /**
1024117b021SAkinobu Mita  * test_bit - Determine whether a bit is set
1034117b021SAkinobu Mita  * @nr: bit number to test
1044117b021SAkinobu Mita  * @addr: Address to start counting from
1054117b021SAkinobu Mita  */
1064117b021SAkinobu Mita static inline int test_bit(int nr, const volatile unsigned long *addr)
1074117b021SAkinobu Mita {
1084117b021SAkinobu Mita 	return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
1094117b021SAkinobu Mita }
1104117b021SAkinobu Mita 
1114117b021SAkinobu Mita #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */
112