1 #ifndef _ASM_X86_BITOPS_H
2 #define _ASM_X86_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #ifndef _LINUX_BITOPS_H
9 #error only <linux/bitops.h> can be included directly
12 #include <linux/compiler.h>
13 #include <asm/alternative.h>
16 * These have to be done with inline assembly: that way the bit-setting
17 * is guaranteed to be atomic. All bit operations return 0 if the bit
18 * was cleared before the operation and != 0 if it was not.
20 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
23 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
24 /* Technically wrong, but this avoids compilation errors on some gcc
26 #define ADDR "=m" (*(volatile long *) addr)
27 #define BIT_ADDR "=m" (((volatile int *) addr)[nr >> 5])
29 #define ADDR "+m" (*(volatile long *) addr)
30 #define BIT_ADDR "+m" (((volatile int *) addr)[nr >> 5])
32 #define BASE_ADDR "m" (*(volatile int *) addr)
35 * set_bit - Atomically set a bit in memory
37 * @addr: the address to start counting from
39 * This function is atomic and may not be reordered. See __set_bit()
40 * if you do not require the atomic guarantees.
42 * Note: there are no guarantees that this function will not be reordered
43 * on non x86 architectures, so if you are writing portable code,
44 * make sure not to rely on its reordering guarantees.
46 * Note that @nr may be almost arbitrarily large; this function is not
47 * restricted to acting on a single-word quantity.
49 static inline void set_bit(int nr
, volatile void *addr
)
51 asm volatile(LOCK_PREFIX
"bts %1,%0"
53 : "Ir" (nr
) : "memory");
57 * __set_bit - Set a bit in memory
59 * @addr: the address to start counting from
61 * Unlike set_bit(), this function is non-atomic and may be reordered.
62 * If it's called on the same region of memory simultaneously, the effect
63 * may be that only one operation succeeds.
65 static inline void __set_bit(int nr
, volatile void *addr
)
67 asm volatile("bts %1,%0"
69 : "Ir" (nr
) : "memory");
74 * clear_bit - Clears a bit in memory
76 * @addr: Address to start counting from
78 * clear_bit() is atomic and may not be reordered. However, it does
79 * not contain a memory barrier, so if it is used for locking purposes,
80 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
81 * in order to ensure changes are visible on other processors.
83 static inline void clear_bit(int nr
, volatile void *addr
)
85 asm volatile(LOCK_PREFIX
"btr %1,%2"
86 : BIT_ADDR
: "Ir" (nr
), BASE_ADDR
);
90 * clear_bit_unlock - Clears a bit in memory
92 * @addr: Address to start counting from
94 * clear_bit() is atomic and implies release semantics before the memory
95 * operation. It can be used for an unlock.
97 static inline void clear_bit_unlock(unsigned nr
, volatile void *addr
)
103 static inline void __clear_bit(int nr
, volatile void *addr
)
105 asm volatile("btr %1,%2" : BIT_ADDR
: "Ir" (nr
), BASE_ADDR
);
109 * __clear_bit_unlock - Clears a bit in memory
111 * @addr: Address to start counting from
113 * __clear_bit() is non-atomic and implies release semantics before the memory
114 * operation. It can be used for an unlock if no other CPUs can concurrently
115 * modify other bits in the word.
117 * No memory barrier is required here, because x86 cannot reorder stores past
118 * older loads. Same principle as spin_unlock.
120 static inline void __clear_bit_unlock(unsigned nr
, volatile void *addr
)
123 __clear_bit(nr
, addr
);
126 #define smp_mb__before_clear_bit() barrier()
127 #define smp_mb__after_clear_bit() barrier()
130 * __change_bit - Toggle a bit in memory
131 * @nr: the bit to change
132 * @addr: the address to start counting from
134 * Unlike change_bit(), this function is non-atomic and may be reordered.
135 * If it's called on the same region of memory simultaneously, the effect
136 * may be that only one operation succeeds.
138 static inline void __change_bit(int nr
, volatile void *addr
)
140 asm volatile("btc %1,%2" : BIT_ADDR
: "Ir" (nr
), BASE_ADDR
);
144 * change_bit - Toggle a bit in memory
146 * @addr: Address to start counting from
148 * change_bit() is atomic and may not be reordered.
149 * Note that @nr may be almost arbitrarily large; this function is not
150 * restricted to acting on a single-word quantity.
152 static inline void change_bit(int nr
, volatile void *addr
)
154 asm volatile(LOCK_PREFIX
"btc %1,%2"
155 : BIT_ADDR
: "Ir" (nr
), BASE_ADDR
);
159 * test_and_set_bit - Set a bit and return its old value
161 * @addr: Address to count from
163 * This operation is atomic and cannot be reordered.
164 * It also implies a memory barrier.
166 static inline int test_and_set_bit(int nr
, volatile void *addr
)
170 asm volatile(LOCK_PREFIX
"bts %2,%1\n\t"
172 : "=r" (oldbit
), ADDR
173 : "Ir" (nr
) : "memory");
179 * test_and_set_bit_lock - Set a bit and return its old value for lock
181 * @addr: Address to count from
183 * This is the same as test_and_set_bit on x86.
185 static inline int test_and_set_bit_lock(int nr
, volatile void *addr
)
187 return test_and_set_bit(nr
, addr
);
191 * __test_and_set_bit - Set a bit and return its old value
193 * @addr: Address to count from
195 * This operation is non-atomic and can be reordered.
196 * If two examples of this operation race, one can appear to succeed
197 * but actually fail. You must protect multiple accesses with a lock.
199 static inline int __test_and_set_bit(int nr
, volatile void *addr
)
203 asm volatile("bts %2,%3\n\t"
205 : "=r" (oldbit
), BIT_ADDR
206 : "Ir" (nr
), BASE_ADDR
);
211 * test_and_clear_bit - Clear a bit and return its old value
213 * @addr: Address to count from
215 * This operation is atomic and cannot be reordered.
216 * It also implies a memory barrier.
218 static inline int test_and_clear_bit(int nr
, volatile void *addr
)
222 asm volatile(LOCK_PREFIX
"btr %2,%1\n\t"
224 : "=r" (oldbit
), ADDR
225 : "Ir" (nr
) : "memory");
231 * __test_and_clear_bit - Clear a bit and return its old value
233 * @addr: Address to count from
235 * This operation is non-atomic and can be reordered.
236 * If two examples of this operation race, one can appear to succeed
237 * but actually fail. You must protect multiple accesses with a lock.
239 static inline int __test_and_clear_bit(int nr
, volatile void *addr
)
243 asm volatile("btr %2,%3\n\t"
245 : "=r" (oldbit
), BIT_ADDR
246 : "Ir" (nr
), BASE_ADDR
);
250 /* WARNING: non atomic and it can be reordered! */
251 static inline int __test_and_change_bit(int nr
, volatile void *addr
)
255 asm volatile("btc %2,%3\n\t"
257 : "=r" (oldbit
), BIT_ADDR
258 : "Ir" (nr
), BASE_ADDR
);
264 * test_and_change_bit - Change a bit and return its old value
266 * @addr: Address to count from
268 * This operation is atomic and cannot be reordered.
269 * It also implies a memory barrier.
271 static inline int test_and_change_bit(int nr
, volatile void *addr
)
275 asm volatile(LOCK_PREFIX
"btc %2,%1\n\t"
277 : "=r" (oldbit
), ADDR
278 : "Ir" (nr
) : "memory");
283 static inline int constant_test_bit(int nr
, const volatile void *addr
)
285 return ((1UL << (nr
% BITS_PER_LONG
)) &
286 (((unsigned long *)addr
)[nr
/ BITS_PER_LONG
])) != 0;
289 static inline int variable_test_bit(int nr
, volatile const void *addr
)
293 asm volatile("bt %2,%3\n\t"
296 : "m" (((volatile const int *)addr
)[nr
>> 5]),
297 "Ir" (nr
), BASE_ADDR
);
302 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
304 * test_bit - Determine whether a bit is set
305 * @nr: bit number to test
306 * @addr: Address to start counting from
308 static int test_bit(int nr
, const volatile unsigned long *addr
);
311 #define test_bit(nr,addr) \
312 (__builtin_constant_p(nr) ? \
313 constant_test_bit((nr),(addr)) : \
314 variable_test_bit((nr),(addr)))
321 # include "bitops_32.h"
323 # include "bitops_64.h"
326 #endif /* _ASM_X86_BITOPS_H */