1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H
4 #define __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H
6 #include <linux/bits.h>
7 #include <asm/barrier.h>
9 #ifndef _LINUX_BITOPS_H
10 #error only <linux/bitops.h> can be included directly
14 * Generic definitions for bit operations, should not be used in regular code
19 * generic___set_bit - Set a bit in memory
21 * @addr: the address to start counting from
23 * Unlike set_bit(), this function is non-atomic and may be reordered.
24 * If it's called on the same region of memory simultaneously, the effect
25 * may be that only one operation succeeds.
27 static __always_inline
void
28 generic___set_bit(unsigned long nr
, volatile unsigned long *addr
)
30 unsigned long mask
= BIT_MASK(nr
);
31 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
36 static __always_inline
void
37 generic___clear_bit(unsigned long nr
, volatile unsigned long *addr
)
39 unsigned long mask
= BIT_MASK(nr
);
40 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
46 * generic___change_bit - Toggle a bit in memory
47 * @nr: the bit to change
48 * @addr: the address to start counting from
50 * Unlike change_bit(), this function is non-atomic and may be reordered.
51 * If it's called on the same region of memory simultaneously, the effect
52 * may be that only one operation succeeds.
54 static __always_inline
void
55 generic___change_bit(unsigned long nr
, volatile unsigned long *addr
)
57 unsigned long mask
= BIT_MASK(nr
);
58 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
64 * generic___test_and_set_bit - Set a bit and return its old value
66 * @addr: Address to count from
68 * This operation is non-atomic and can be reordered.
69 * If two examples of this operation race, one can appear to succeed
70 * but actually fail. You must protect multiple accesses with a lock.
72 static __always_inline
bool
73 generic___test_and_set_bit(unsigned long nr
, volatile unsigned long *addr
)
75 unsigned long mask
= BIT_MASK(nr
);
76 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
77 unsigned long old
= *p
;
80 return (old
& mask
) != 0;
84 * generic___test_and_clear_bit - Clear a bit and return its old value
86 * @addr: Address to count from
88 * This operation is non-atomic and can be reordered.
89 * If two examples of this operation race, one can appear to succeed
90 * but actually fail. You must protect multiple accesses with a lock.
92 static __always_inline
bool
93 generic___test_and_clear_bit(unsigned long nr
, volatile unsigned long *addr
)
95 unsigned long mask
= BIT_MASK(nr
);
96 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
97 unsigned long old
= *p
;
100 return (old
& mask
) != 0;
103 /* WARNING: non atomic and it can be reordered! */
104 static __always_inline
bool
105 generic___test_and_change_bit(unsigned long nr
, volatile unsigned long *addr
)
107 unsigned long mask
= BIT_MASK(nr
);
108 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
109 unsigned long old
= *p
;
112 return (old
& mask
) != 0;
116 * generic_test_bit - Determine whether a bit is set
117 * @nr: bit number to test
118 * @addr: Address to start counting from
120 static __always_inline
bool
121 generic_test_bit(unsigned long nr
, const volatile unsigned long *addr
)
124 * Unlike the bitops with the '__' prefix above, this one *is* atomic,
125 * so `volatile` must always stay here with no cast-aways. See
126 * `Documentation/atomic_bitops.txt` for the details.
128 return 1UL & (addr
[BIT_WORD(nr
)] >> (nr
& (BITS_PER_LONG
-1)));
132 * generic_test_bit_acquire - Determine, with acquire semantics, whether a bit is set
133 * @nr: bit number to test
134 * @addr: Address to start counting from
136 static __always_inline
bool
137 generic_test_bit_acquire(unsigned long nr
, const volatile unsigned long *addr
)
139 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
140 return 1UL & (smp_load_acquire(p
) >> (nr
& (BITS_PER_LONG
-1)));
144 * const_*() definitions provide good compile-time optimizations when
145 * the passed arguments can be resolved at compile time.
147 #define const___set_bit generic___set_bit
148 #define const___clear_bit generic___clear_bit
149 #define const___change_bit generic___change_bit
150 #define const___test_and_set_bit generic___test_and_set_bit
151 #define const___test_and_clear_bit generic___test_and_clear_bit
152 #define const___test_and_change_bit generic___test_and_change_bit
153 #define const_test_bit_acquire generic_test_bit_acquire
156 * const_test_bit - Determine whether a bit is set
157 * @nr: bit number to test
158 * @addr: Address to start counting from
160 * A version of generic_test_bit() which discards the `volatile` qualifier to
161 * allow a compiler to optimize code harder. Non-atomic and to be called only
162 * for testing compile-time constants, e.g. by the corresponding macros, not
163 * directly from "regular" code.
165 static __always_inline
bool
166 const_test_bit(unsigned long nr
, const volatile unsigned long *addr
)
168 const unsigned long *p
= (const unsigned long *)addr
+ BIT_WORD(nr
);
169 unsigned long mask
= BIT_MASK(nr
);
170 unsigned long val
= *p
;
172 return !!(val
& mask
);
175 #endif /* __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H */