xref: /linux/arch/x86/include/asm/sync_bitops.h (revision 547571b5)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_SYNC_BITOPS_H
31965aae3SH. Peter Anvin #define _ASM_X86_SYNC_BITOPS_H
4bb898558SAl Viro 
5bb898558SAl Viro /*
6bb898558SAl Viro  * Copyright 1992, Linus Torvalds.
7bb898558SAl Viro  */
8bb898558SAl Viro 
9bb898558SAl Viro /*
10bb898558SAl Viro  * These have to be done with inline assembly: that way the bit-setting
11bb898558SAl Viro  * is guaranteed to be atomic. All bit operations return 0 if the bit
12bb898558SAl Viro  * was cleared before the operation and != 0 if it was not.
13bb898558SAl Viro  *
14bb898558SAl Viro  * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
15bb898558SAl Viro  */
16bb898558SAl Viro 
17*547571b5SJan Beulich #include <asm/rmwcc.h>
18*547571b5SJan Beulich 
19bb898558SAl Viro #define ADDR (*(volatile long *)addr)
20bb898558SAl Viro 
21bb898558SAl Viro /**
22bb898558SAl Viro  * sync_set_bit - Atomically set a bit in memory
23bb898558SAl Viro  * @nr: the bit to set
24bb898558SAl Viro  * @addr: the address to start counting from
25bb898558SAl Viro  *
26bb898558SAl Viro  * This function is atomic and may not be reordered.  See __set_bit()
27bb898558SAl Viro  * if you do not require the atomic guarantees.
28bb898558SAl Viro  *
29bb898558SAl Viro  * Note that @nr may be almost arbitrarily large; this function is not
30bb898558SAl Viro  * restricted to acting on a single-word quantity.
31bb898558SAl Viro  */
sync_set_bit(long nr,volatile unsigned long * addr)329b710506SH. Peter Anvin static inline void sync_set_bit(long nr, volatile unsigned long *addr)
33bb898558SAl Viro {
34*547571b5SJan Beulich 	asm volatile("lock; " __ASM_SIZE(bts) " %1,%0"
35bb898558SAl Viro 		     : "+m" (ADDR)
36bb898558SAl Viro 		     : "Ir" (nr)
37bb898558SAl Viro 		     : "memory");
38bb898558SAl Viro }
39bb898558SAl Viro 
40bb898558SAl Viro /**
41bb898558SAl Viro  * sync_clear_bit - Clears a bit in memory
42bb898558SAl Viro  * @nr: Bit to clear
43bb898558SAl Viro  * @addr: Address to start counting from
44bb898558SAl Viro  *
45bb898558SAl Viro  * sync_clear_bit() is atomic and may not be reordered.  However, it does
46bb898558SAl Viro  * not contain a memory barrier, so if it is used for locking purposes,
47d00a5692SPeter Zijlstra  * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
48bb898558SAl Viro  * in order to ensure changes are visible on other processors.
49bb898558SAl Viro  */
sync_clear_bit(long nr,volatile unsigned long * addr)509b710506SH. Peter Anvin static inline void sync_clear_bit(long nr, volatile unsigned long *addr)
51bb898558SAl Viro {
52*547571b5SJan Beulich 	asm volatile("lock; " __ASM_SIZE(btr) " %1,%0"
53bb898558SAl Viro 		     : "+m" (ADDR)
54bb898558SAl Viro 		     : "Ir" (nr)
55bb898558SAl Viro 		     : "memory");
56bb898558SAl Viro }
57bb898558SAl Viro 
58bb898558SAl Viro /**
59bb898558SAl Viro  * sync_change_bit - Toggle a bit in memory
60bb898558SAl Viro  * @nr: Bit to change
61bb898558SAl Viro  * @addr: Address to start counting from
62bb898558SAl Viro  *
63bb898558SAl Viro  * sync_change_bit() is atomic and may not be reordered.
64bb898558SAl Viro  * Note that @nr may be almost arbitrarily large; this function is not
65bb898558SAl Viro  * restricted to acting on a single-word quantity.
66bb898558SAl Viro  */
sync_change_bit(long nr,volatile unsigned long * addr)679b710506SH. Peter Anvin static inline void sync_change_bit(long nr, volatile unsigned long *addr)
68bb898558SAl Viro {
69*547571b5SJan Beulich 	asm volatile("lock; " __ASM_SIZE(btc) " %1,%0"
70bb898558SAl Viro 		     : "+m" (ADDR)
71bb898558SAl Viro 		     : "Ir" (nr)
72bb898558SAl Viro 		     : "memory");
73bb898558SAl Viro }
74bb898558SAl Viro 
75bb898558SAl Viro /**
76bb898558SAl Viro  * sync_test_and_set_bit - Set a bit and return its old value
77bb898558SAl Viro  * @nr: Bit to set
78bb898558SAl Viro  * @addr: Address to count from
79bb898558SAl Viro  *
80bb898558SAl Viro  * This operation is atomic and cannot be reordered.
81bb898558SAl Viro  * It also implies a memory barrier.
82bb898558SAl Viro  */
sync_test_and_set_bit(long nr,volatile unsigned long * addr)83*547571b5SJan Beulich static inline bool sync_test_and_set_bit(long nr, volatile unsigned long *addr)
84bb898558SAl Viro {
85*547571b5SJan Beulich 	return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(bts), *addr, c, "Ir", nr);
86bb898558SAl Viro }
87bb898558SAl Viro 
88bb898558SAl Viro /**
89bb898558SAl Viro  * sync_test_and_clear_bit - Clear a bit and return its old value
90bb898558SAl Viro  * @nr: Bit to clear
91bb898558SAl Viro  * @addr: Address to count from
92bb898558SAl Viro  *
93bb898558SAl Viro  * This operation is atomic and cannot be reordered.
94bb898558SAl Viro  * It also implies a memory barrier.
95bb898558SAl Viro  */
sync_test_and_clear_bit(long nr,volatile unsigned long * addr)969b710506SH. Peter Anvin static inline int sync_test_and_clear_bit(long nr, volatile unsigned long *addr)
97bb898558SAl Viro {
98*547571b5SJan Beulich 	return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btr), *addr, c, "Ir", nr);
99bb898558SAl Viro }
100bb898558SAl Viro 
101bb898558SAl Viro /**
102bb898558SAl Viro  * sync_test_and_change_bit - Change a bit and return its old value
103bb898558SAl Viro  * @nr: Bit to change
104bb898558SAl Viro  * @addr: Address to count from
105bb898558SAl Viro  *
106bb898558SAl Viro  * This operation is atomic and cannot be reordered.
107bb898558SAl Viro  * It also implies a memory barrier.
108bb898558SAl Viro  */
sync_test_and_change_bit(long nr,volatile unsigned long * addr)1099b710506SH. Peter Anvin static inline int sync_test_and_change_bit(long nr, volatile unsigned long *addr)
110bb898558SAl Viro {
111*547571b5SJan Beulich 	return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btc), *addr, c, "Ir", nr);
112bb898558SAl Viro }
113bb898558SAl Viro 
114bb898558SAl Viro #define sync_test_bit(nr, addr) test_bit(nr, addr)
115bb898558SAl Viro 
116bb898558SAl Viro #undef ADDR
117bb898558SAl Viro 
1181965aae3SH. Peter Anvin #endif /* _ASM_X86_SYNC_BITOPS_H */
119