xref: /OK3568_Linux_fs/u-boot/arch/nios2/include/asm/bitops/non-atomic.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
2*4882a593Smuzhiyun #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include <asm/types.h>
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /**
7*4882a593Smuzhiyun  * __set_bit - Set a bit in memory
8*4882a593Smuzhiyun  * @nr: the bit to set
9*4882a593Smuzhiyun  * @addr: the address to start counting from
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Unlike set_bit(), this function is non-atomic and may be reordered.
12*4882a593Smuzhiyun  * If it's called on the same region of memory simultaneously, the effect
13*4882a593Smuzhiyun  * may be that only one operation succeeds.
14*4882a593Smuzhiyun  */
__set_bit(int nr,volatile unsigned long * addr)15*4882a593Smuzhiyun static inline void __set_bit(int nr, volatile unsigned long *addr)
16*4882a593Smuzhiyun {
17*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
18*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun 	*p  |= mask;
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun 
__clear_bit(int nr,volatile unsigned long * addr)23*4882a593Smuzhiyun static inline void __clear_bit(int nr, volatile unsigned long *addr)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
26*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	*p &= ~mask;
29*4882a593Smuzhiyun }
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /**
32*4882a593Smuzhiyun  * __change_bit - Toggle a bit in memory
33*4882a593Smuzhiyun  * @nr: the bit to change
34*4882a593Smuzhiyun  * @addr: the address to start counting from
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * Unlike change_bit(), this function is non-atomic and may be reordered.
37*4882a593Smuzhiyun  * If it's called on the same region of memory simultaneously, the effect
38*4882a593Smuzhiyun  * may be that only one operation succeeds.
39*4882a593Smuzhiyun  */
__change_bit(int nr,volatile unsigned long * addr)40*4882a593Smuzhiyun static inline void __change_bit(int nr, volatile unsigned long *addr)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
43*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	*p ^= mask;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /**
49*4882a593Smuzhiyun  * __test_and_set_bit - Set a bit and return its old value
50*4882a593Smuzhiyun  * @nr: Bit to set
51*4882a593Smuzhiyun  * @addr: Address to count from
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * This operation is non-atomic and can be reordered.
54*4882a593Smuzhiyun  * If two examples of this operation race, one can appear to succeed
55*4882a593Smuzhiyun  * but actually fail.  You must protect multiple accesses with a lock.
56*4882a593Smuzhiyun  */
__test_and_set_bit(int nr,volatile unsigned long * addr)57*4882a593Smuzhiyun static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
60*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
61*4882a593Smuzhiyun 	unsigned long old = *p;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	*p = old | mask;
64*4882a593Smuzhiyun 	return (old & mask) != 0;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun  * __test_and_clear_bit - Clear a bit and return its old value
69*4882a593Smuzhiyun  * @nr: Bit to clear
70*4882a593Smuzhiyun  * @addr: Address to count from
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * This operation is non-atomic and can be reordered.
73*4882a593Smuzhiyun  * If two examples of this operation race, one can appear to succeed
74*4882a593Smuzhiyun  * but actually fail.  You must protect multiple accesses with a lock.
75*4882a593Smuzhiyun  */
__test_and_clear_bit(int nr,volatile unsigned long * addr)76*4882a593Smuzhiyun static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
79*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
80*4882a593Smuzhiyun 	unsigned long old = *p;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	*p = old & ~mask;
83*4882a593Smuzhiyun 	return (old & mask) != 0;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* WARNING: non atomic and it can be reordered! */
__test_and_change_bit(int nr,volatile unsigned long * addr)87*4882a593Smuzhiyun static inline int __test_and_change_bit(int nr,
88*4882a593Smuzhiyun 					    volatile unsigned long *addr)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
91*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
92*4882a593Smuzhiyun 	unsigned long old = *p;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	*p = old ^ mask;
95*4882a593Smuzhiyun 	return (old & mask) != 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun  * test_bit - Determine whether a bit is set
100*4882a593Smuzhiyun  * @nr: bit number to test
101*4882a593Smuzhiyun  * @addr: Address to start counting from
102*4882a593Smuzhiyun  */
test_bit(int nr,const volatile unsigned long * addr)103*4882a593Smuzhiyun static inline int test_bit(int nr, const volatile unsigned long *addr)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */
109