1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_SYNC_BITOPS_H
3 #define _ASM_X86_SYNC_BITOPS_H
6 * Copyright 1992, Linus Torvalds.
10 * These have to be done with inline assembly: that way the bit-setting
11 * is guaranteed to be atomic. All bit operations return 0 if the bit
12 * was cleared before the operation and != 0 if it was not.
14 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
17 #include <asm/rmwcc.h>
19 #define ADDR (*(volatile long *)addr)
22 * sync_set_bit - Atomically set a bit in memory
24 * @addr: the address to start counting from
26 * This function is atomic and may not be reordered. See __set_bit()
27 * if you do not require the atomic guarantees.
29 * Note that @nr may be almost arbitrarily large; this function is not
30 * restricted to acting on a single-word quantity.
32 static inline void sync_set_bit(long nr
, volatile unsigned long *addr
)
34 asm volatile("lock; " __ASM_SIZE(bts
) " %1,%0"
41 * sync_clear_bit - Clears a bit in memory
43 * @addr: Address to start counting from
45 * sync_clear_bit() is atomic and may not be reordered. However, it does
46 * not contain a memory barrier, so if it is used for locking purposes,
47 * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
48 * in order to ensure changes are visible on other processors.
50 static inline void sync_clear_bit(long nr
, volatile unsigned long *addr
)
52 asm volatile("lock; " __ASM_SIZE(btr
) " %1,%0"
59 * sync_change_bit - Toggle a bit in memory
61 * @addr: Address to start counting from
63 * sync_change_bit() is atomic and may not be reordered.
64 * Note that @nr may be almost arbitrarily large; this function is not
65 * restricted to acting on a single-word quantity.
67 static inline void sync_change_bit(long nr
, volatile unsigned long *addr
)
69 asm volatile("lock; " __ASM_SIZE(btc
) " %1,%0"
76 * sync_test_and_set_bit - Set a bit and return its old value
78 * @addr: Address to count from
80 * This operation is atomic and cannot be reordered.
81 * It also implies a memory barrier.
83 static inline bool sync_test_and_set_bit(long nr
, volatile unsigned long *addr
)
85 return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(bts
), *addr
, c
, "Ir", nr
);
89 * sync_test_and_clear_bit - Clear a bit and return its old value
91 * @addr: Address to count from
93 * This operation is atomic and cannot be reordered.
94 * It also implies a memory barrier.
96 static inline int sync_test_and_clear_bit(long nr
, volatile unsigned long *addr
)
98 return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btr
), *addr
, c
, "Ir", nr
);
102 * sync_test_and_change_bit - Change a bit and return its old value
104 * @addr: Address to count from
106 * This operation is atomic and cannot be reordered.
107 * It also implies a memory barrier.
109 static inline int sync_test_and_change_bit(long nr
, volatile unsigned long *addr
)
111 return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btc
), *addr
, c
, "Ir", nr
);
114 #define sync_test_bit(nr, addr) test_bit(nr, addr)
118 #endif /* _ASM_X86_SYNC_BITOPS_H */