]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/riscv/include/asm/bitops.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 Regents of the University of California
6 #ifndef _ASM_RISCV_BITOPS_H
7 #define _ASM_RISCV_BITOPS_H
9 #ifndef _LINUX_BITOPS_H
10 #error "Only <linux/bitops.h> can be included directly"
11 #endif /* _LINUX_BITOPS_H */
13 #include <linux/compiler.h>
14 #include <linux/irqflags.h>
15 #include <asm/barrier.h>
16 #include <asm/bitsperlong.h>
18 #ifndef smp_mb__before_clear_bit
19 #define smp_mb__before_clear_bit() smp_mb()
20 #define smp_mb__after_clear_bit() smp_mb()
21 #endif /* smp_mb__before_clear_bit */
23 #include <asm-generic/bitops/__ffs.h>
24 #include <asm-generic/bitops/ffz.h>
25 #include <asm-generic/bitops/fls.h>
26 #include <asm-generic/bitops/__fls.h>
27 #include <asm-generic/bitops/fls64.h>
28 #include <asm-generic/bitops/find.h>
29 #include <asm-generic/bitops/sched.h>
30 #include <asm-generic/bitops/ffs.h>
32 #include <asm-generic/bitops/hweight.h>
34 #if (BITS_PER_LONG == 64)
35 #define __AMO(op) "amo" #op ".d"
36 #elif (BITS_PER_LONG == 32)
37 #define __AMO(op) "amo" #op ".w"
39 #error "Unexpected BITS_PER_LONG"
42 #define __test_and_op_bit_ord(op, mod, nr, addr, ord) \
44 unsigned long __res, __mask; \
45 __mask = BIT_MASK(nr); \
46 __asm__ __volatile__ ( \
47 __AMO(op) #ord " %0, %2, %1" \
48 : "=r" (__res), "+A" (addr[BIT_WORD(nr)]) \
51 ((__res & __mask) != 0); \
54 #define __op_bit_ord(op, mod, nr, addr, ord) \
55 __asm__ __volatile__ ( \
56 __AMO(op) #ord " zero, %1, %0" \
57 : "+A" (addr[BIT_WORD(nr)]) \
58 : "r" (mod(BIT_MASK(nr))) \
61 #define __test_and_op_bit(op, mod, nr, addr) \
62 __test_and_op_bit_ord(op, mod, nr, addr, .aqrl)
63 #define __op_bit(op, mod, nr, addr) \
64 __op_bit_ord(op, mod, nr, addr, )
66 /* Bitmask modifiers */
68 #define __NOT(x) (~(x))
71 * test_and_set_bit - Set a bit and return its old value
73 * @addr: Address to count from
75 * This operation may be reordered on other architectures than x86.
77 static inline int test_and_set_bit(int nr
, volatile unsigned long *addr
)
79 return __test_and_op_bit(or, __NOP
, nr
, addr
);
83 * test_and_clear_bit - Clear a bit and return its old value
85 * @addr: Address to count from
87 * This operation can be reordered on other architectures other than x86.
89 static inline int test_and_clear_bit(int nr
, volatile unsigned long *addr
)
91 return __test_and_op_bit(and, __NOT
, nr
, addr
);
95 * test_and_change_bit - Change a bit and return its old value
97 * @addr: Address to count from
99 * This operation is atomic and cannot be reordered.
100 * It also implies a memory barrier.
102 static inline int test_and_change_bit(int nr
, volatile unsigned long *addr
)
104 return __test_and_op_bit(xor, __NOP
, nr
, addr
);
108 * set_bit - Atomically set a bit in memory
109 * @nr: the bit to set
110 * @addr: the address to start counting from
112 * Note: there are no guarantees that this function will not be reordered
113 * on non x86 architectures, so if you are writing portable code,
114 * make sure not to rely on its reordering guarantees.
116 * Note that @nr may be almost arbitrarily large; this function is not
117 * restricted to acting on a single-word quantity.
119 static inline void set_bit(int nr
, volatile unsigned long *addr
)
121 __op_bit(or, __NOP
, nr
, addr
);
125 * clear_bit - Clears a bit in memory
127 * @addr: Address to start counting from
129 * Note: there are no guarantees that this function will not be reordered
130 * on non x86 architectures, so if you are writing portable code,
131 * make sure not to rely on its reordering guarantees.
133 static inline void clear_bit(int nr
, volatile unsigned long *addr
)
135 __op_bit(and, __NOT
, nr
, addr
);
139 * change_bit - Toggle a bit in memory
141 * @addr: Address to start counting from
143 * change_bit() may be reordered on other architectures than x86.
144 * Note that @nr may be almost arbitrarily large; this function is not
145 * restricted to acting on a single-word quantity.
147 static inline void change_bit(int nr
, volatile unsigned long *addr
)
149 __op_bit(xor, __NOP
, nr
, addr
);
153 * test_and_set_bit_lock - Set a bit and return its old value, for lock
155 * @addr: Address to count from
157 * This operation is atomic and provides acquire barrier semantics.
158 * It can be used to implement bit locks.
160 static inline int test_and_set_bit_lock(
161 unsigned long nr
, volatile unsigned long *addr
)
163 return __test_and_op_bit_ord(or, __NOP
, nr
, addr
, .aq
);
167 * clear_bit_unlock - Clear a bit in memory, for unlock
168 * @nr: the bit to set
169 * @addr: the address to start counting from
171 * This operation is atomic and provides release barrier semantics.
173 static inline void clear_bit_unlock(
174 unsigned long nr
, volatile unsigned long *addr
)
176 __op_bit_ord(and, __NOT
, nr
, addr
, .rl
);
180 * __clear_bit_unlock - Clear a bit in memory, for unlock
181 * @nr: the bit to set
182 * @addr: the address to start counting from
184 * This operation is like clear_bit_unlock, however it is not atomic.
185 * It does provide release barrier semantics so it can be used to unlock
186 * a bit lock, however it would only be used if no other CPU can modify
187 * any bits in the memory until the lock is released (a good example is
188 * if the bit lock itself protects access to the other bits in the word).
190 * On RISC-V systems there seems to be no benefit to taking advantage of the
191 * non-atomic property here: it's a lot more instructions and we still have to
192 * provide release semantics anyway.
194 static inline void __clear_bit_unlock(
195 unsigned long nr
, volatile unsigned long *addr
)
197 clear_bit_unlock(nr
, addr
);
200 #undef __test_and_op_bit
206 #include <asm-generic/bitops/non-atomic.h>
207 #include <asm-generic/bitops/le.h>
208 #include <asm-generic/bitops/ext2-atomic.h>
210 #endif /* _ASM_RISCV_BITOPS_H */