blob: 6d8d6bc183b7421ddfc367062b1ebb7164245ee3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_SYNC_BITOPS_H
3#define _ASM_X86_SYNC_BITOPS_H
Chris Wright027a8c72006-09-25 23:32:23 -07004
5/*
6 * Copyright 1992, Linus Torvalds.
7 */
8
9/*
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.
13 *
14 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
15 */
16
Jan Beulich547571b2019-03-27 09:15:19 -060017#include <asm/rmwcc.h>
18
Joe Perches26b7fcc2008-03-23 01:03:38 -070019#define ADDR (*(volatile long *)addr)
Chris Wright027a8c72006-09-25 23:32:23 -070020
21/**
22 * sync_set_bit - Atomically set a bit in memory
23 * @nr: the bit to set
24 * @addr: the address to start counting from
25 *
26 * This function is atomic and may not be reordered. See __set_bit()
27 * if you do not require the atomic guarantees.
28 *
Chris Wright027a8c72006-09-25 23:32:23 -070029 * Note that @nr may be almost arbitrarily large; this function is not
30 * restricted to acting on a single-word quantity.
31 */
H. Peter Anvin9b710502013-07-16 15:20:14 -070032static inline void sync_set_bit(long nr, volatile unsigned long *addr)
Chris Wright027a8c72006-09-25 23:32:23 -070033{
Jan Beulich547571b2019-03-27 09:15:19 -060034 asm volatile("lock; " __ASM_SIZE(bts) " %1,%0"
Joe Perches26b7fcc2008-03-23 01:03:38 -070035 : "+m" (ADDR)
36 : "Ir" (nr)
37 : "memory");
Chris Wright027a8c72006-09-25 23:32:23 -070038}
39
40/**
41 * sync_clear_bit - Clears a bit in memory
42 * @nr: Bit to clear
43 * @addr: Address to start counting from
44 *
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,
Peter Zijlstrad00a5692014-03-13 19:00:35 +010047 * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
Chris Wright027a8c72006-09-25 23:32:23 -070048 * in order to ensure changes are visible on other processors.
49 */
H. Peter Anvin9b710502013-07-16 15:20:14 -070050static inline void sync_clear_bit(long nr, volatile unsigned long *addr)
Chris Wright027a8c72006-09-25 23:32:23 -070051{
Jan Beulich547571b2019-03-27 09:15:19 -060052 asm volatile("lock; " __ASM_SIZE(btr) " %1,%0"
Joe Perches26b7fcc2008-03-23 01:03:38 -070053 : "+m" (ADDR)
54 : "Ir" (nr)
55 : "memory");
Chris Wright027a8c72006-09-25 23:32:23 -070056}
57
58/**
59 * sync_change_bit - Toggle a bit in memory
60 * @nr: Bit to change
61 * @addr: Address to start counting from
62 *
Matti Linnanvuori7800c0c2008-03-16 02:47:35 -070063 * sync_change_bit() is atomic and may not be reordered.
Chris Wright027a8c72006-09-25 23:32:23 -070064 * Note that @nr may be almost arbitrarily large; this function is not
65 * restricted to acting on a single-word quantity.
66 */
H. Peter Anvin9b710502013-07-16 15:20:14 -070067static inline void sync_change_bit(long nr, volatile unsigned long *addr)
Chris Wright027a8c72006-09-25 23:32:23 -070068{
Jan Beulich547571b2019-03-27 09:15:19 -060069 asm volatile("lock; " __ASM_SIZE(btc) " %1,%0"
Joe Perches26b7fcc2008-03-23 01:03:38 -070070 : "+m" (ADDR)
71 : "Ir" (nr)
72 : "memory");
Chris Wright027a8c72006-09-25 23:32:23 -070073}
74
75/**
76 * sync_test_and_set_bit - Set a bit and return its old value
77 * @nr: Bit to set
78 * @addr: Address to count from
79 *
80 * This operation is atomic and cannot be reordered.
Chris Wright027a8c72006-09-25 23:32:23 -070081 * It also implies a memory barrier.
82 */
Jan Beulich547571b2019-03-27 09:15:19 -060083static inline bool sync_test_and_set_bit(long nr, volatile unsigned long *addr)
Chris Wright027a8c72006-09-25 23:32:23 -070084{
Jan Beulich547571b2019-03-27 09:15:19 -060085 return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(bts), *addr, c, "Ir", nr);
Chris Wright027a8c72006-09-25 23:32:23 -070086}
87
88/**
89 * sync_test_and_clear_bit - Clear a bit and return its old value
90 * @nr: Bit to clear
91 * @addr: Address to count from
92 *
93 * This operation is atomic and cannot be reordered.
Chris Wright027a8c72006-09-25 23:32:23 -070094 * It also implies a memory barrier.
95 */
H. Peter Anvin9b710502013-07-16 15:20:14 -070096static inline int sync_test_and_clear_bit(long nr, volatile unsigned long *addr)
Chris Wright027a8c72006-09-25 23:32:23 -070097{
Jan Beulich547571b2019-03-27 09:15:19 -060098 return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btr), *addr, c, "Ir", nr);
Chris Wright027a8c72006-09-25 23:32:23 -070099}
100
101/**
102 * sync_test_and_change_bit - Change a bit and return its old value
103 * @nr: Bit to change
104 * @addr: Address to count from
105 *
106 * This operation is atomic and cannot be reordered.
107 * It also implies a memory barrier.
108 */
H. Peter Anvin9b710502013-07-16 15:20:14 -0700109static inline int sync_test_and_change_bit(long nr, volatile unsigned long *addr)
Chris Wright027a8c72006-09-25 23:32:23 -0700110{
Jan Beulich547571b2019-03-27 09:15:19 -0600111 return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btc), *addr, c, "Ir", nr);
Chris Wright027a8c72006-09-25 23:32:23 -0700112}
113
Jeremy Fitzhardingeaa040b22008-03-22 13:27:38 -0700114#define sync_test_bit(nr, addr) test_bit(nr, addr)
Chris Wright027a8c72006-09-25 23:32:23 -0700115
116#undef ADDR
117
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700118#endif /* _ASM_X86_SYNC_BITOPS_H */