xref: /OK3568_Linux_fs/kernel/arch/riscv/include/asm/bitops.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2012 Regents of the University of California
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #ifndef _ASM_RISCV_BITOPS_H
7*4882a593Smuzhiyun #define _ASM_RISCV_BITOPS_H
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef _LINUX_BITOPS_H
10*4882a593Smuzhiyun #error "Only <linux/bitops.h> can be included directly"
11*4882a593Smuzhiyun #endif /* _LINUX_BITOPS_H */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/compiler.h>
14*4882a593Smuzhiyun #include <linux/irqflags.h>
15*4882a593Smuzhiyun #include <asm/barrier.h>
16*4882a593Smuzhiyun #include <asm/bitsperlong.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <asm-generic/bitops/__ffs.h>
19*4882a593Smuzhiyun #include <asm-generic/bitops/ffz.h>
20*4882a593Smuzhiyun #include <asm-generic/bitops/fls.h>
21*4882a593Smuzhiyun #include <asm-generic/bitops/__fls.h>
22*4882a593Smuzhiyun #include <asm-generic/bitops/fls64.h>
23*4882a593Smuzhiyun #include <asm-generic/bitops/find.h>
24*4882a593Smuzhiyun #include <asm-generic/bitops/sched.h>
25*4882a593Smuzhiyun #include <asm-generic/bitops/ffs.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <asm-generic/bitops/hweight.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #if (BITS_PER_LONG == 64)
30*4882a593Smuzhiyun #define __AMO(op)	"amo" #op ".d"
31*4882a593Smuzhiyun #elif (BITS_PER_LONG == 32)
32*4882a593Smuzhiyun #define __AMO(op)	"amo" #op ".w"
33*4882a593Smuzhiyun #else
34*4882a593Smuzhiyun #error "Unexpected BITS_PER_LONG"
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define __test_and_op_bit_ord(op, mod, nr, addr, ord)		\
38*4882a593Smuzhiyun ({								\
39*4882a593Smuzhiyun 	unsigned long __res, __mask;				\
40*4882a593Smuzhiyun 	__mask = BIT_MASK(nr);					\
41*4882a593Smuzhiyun 	__asm__ __volatile__ (					\
42*4882a593Smuzhiyun 		__AMO(op) #ord " %0, %2, %1"			\
43*4882a593Smuzhiyun 		: "=r" (__res), "+A" (addr[BIT_WORD(nr)])	\
44*4882a593Smuzhiyun 		: "r" (mod(__mask))				\
45*4882a593Smuzhiyun 		: "memory");					\
46*4882a593Smuzhiyun 	((__res & __mask) != 0);				\
47*4882a593Smuzhiyun })
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #define __op_bit_ord(op, mod, nr, addr, ord)			\
50*4882a593Smuzhiyun 	__asm__ __volatile__ (					\
51*4882a593Smuzhiyun 		__AMO(op) #ord " zero, %1, %0"			\
52*4882a593Smuzhiyun 		: "+A" (addr[BIT_WORD(nr)])			\
53*4882a593Smuzhiyun 		: "r" (mod(BIT_MASK(nr)))			\
54*4882a593Smuzhiyun 		: "memory");
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define __test_and_op_bit(op, mod, nr, addr) 			\
57*4882a593Smuzhiyun 	__test_and_op_bit_ord(op, mod, nr, addr, .aqrl)
58*4882a593Smuzhiyun #define __op_bit(op, mod, nr, addr)				\
59*4882a593Smuzhiyun 	__op_bit_ord(op, mod, nr, addr, )
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* Bitmask modifiers */
62*4882a593Smuzhiyun #define __NOP(x)	(x)
63*4882a593Smuzhiyun #define __NOT(x)	(~(x))
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /**
66*4882a593Smuzhiyun  * test_and_set_bit - Set a bit and return its old value
67*4882a593Smuzhiyun  * @nr: Bit to set
68*4882a593Smuzhiyun  * @addr: Address to count from
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * This operation may be reordered on other architectures than x86.
71*4882a593Smuzhiyun  */
test_and_set_bit(int nr,volatile unsigned long * addr)72*4882a593Smuzhiyun static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	return __test_and_op_bit(or, __NOP, nr, addr);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun  * test_and_clear_bit - Clear a bit and return its old value
79*4882a593Smuzhiyun  * @nr: Bit to clear
80*4882a593Smuzhiyun  * @addr: Address to count from
81*4882a593Smuzhiyun  *
82*4882a593Smuzhiyun  * This operation can be reordered on other architectures other than x86.
83*4882a593Smuzhiyun  */
test_and_clear_bit(int nr,volatile unsigned long * addr)84*4882a593Smuzhiyun static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	return __test_and_op_bit(and, __NOT, nr, addr);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun  * test_and_change_bit - Change a bit and return its old value
91*4882a593Smuzhiyun  * @nr: Bit to change
92*4882a593Smuzhiyun  * @addr: Address to count from
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * This operation is atomic and cannot be reordered.
95*4882a593Smuzhiyun  * It also implies a memory barrier.
96*4882a593Smuzhiyun  */
test_and_change_bit(int nr,volatile unsigned long * addr)97*4882a593Smuzhiyun static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	return __test_and_op_bit(xor, __NOP, nr, addr);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /**
103*4882a593Smuzhiyun  * set_bit - Atomically set a bit in memory
104*4882a593Smuzhiyun  * @nr: the bit to set
105*4882a593Smuzhiyun  * @addr: the address to start counting from
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * Note: there are no guarantees that this function will not be reordered
108*4882a593Smuzhiyun  * on non x86 architectures, so if you are writing portable code,
109*4882a593Smuzhiyun  * make sure not to rely on its reordering guarantees.
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  * Note that @nr may be almost arbitrarily large; this function is not
112*4882a593Smuzhiyun  * restricted to acting on a single-word quantity.
113*4882a593Smuzhiyun  */
set_bit(int nr,volatile unsigned long * addr)114*4882a593Smuzhiyun static inline void set_bit(int nr, volatile unsigned long *addr)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	__op_bit(or, __NOP, nr, addr);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun  * clear_bit - Clears a bit in memory
121*4882a593Smuzhiyun  * @nr: Bit to clear
122*4882a593Smuzhiyun  * @addr: Address to start counting from
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Note: there are no guarantees that this function will not be reordered
125*4882a593Smuzhiyun  * on non x86 architectures, so if you are writing portable code,
126*4882a593Smuzhiyun  * make sure not to rely on its reordering guarantees.
127*4882a593Smuzhiyun  */
clear_bit(int nr,volatile unsigned long * addr)128*4882a593Smuzhiyun static inline void clear_bit(int nr, volatile unsigned long *addr)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	__op_bit(and, __NOT, nr, addr);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /**
134*4882a593Smuzhiyun  * change_bit - Toggle a bit in memory
135*4882a593Smuzhiyun  * @nr: Bit to change
136*4882a593Smuzhiyun  * @addr: Address to start counting from
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  * change_bit()  may be reordered on other architectures than x86.
139*4882a593Smuzhiyun  * Note that @nr may be almost arbitrarily large; this function is not
140*4882a593Smuzhiyun  * restricted to acting on a single-word quantity.
141*4882a593Smuzhiyun  */
change_bit(int nr,volatile unsigned long * addr)142*4882a593Smuzhiyun static inline void change_bit(int nr, volatile unsigned long *addr)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	__op_bit(xor, __NOP, nr, addr);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /**
148*4882a593Smuzhiyun  * test_and_set_bit_lock - Set a bit and return its old value, for lock
149*4882a593Smuzhiyun  * @nr: Bit to set
150*4882a593Smuzhiyun  * @addr: Address to count from
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * This operation is atomic and provides acquire barrier semantics.
153*4882a593Smuzhiyun  * It can be used to implement bit locks.
154*4882a593Smuzhiyun  */
test_and_set_bit_lock(unsigned long nr,volatile unsigned long * addr)155*4882a593Smuzhiyun static inline int test_and_set_bit_lock(
156*4882a593Smuzhiyun 	unsigned long nr, volatile unsigned long *addr)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	return __test_and_op_bit_ord(or, __NOP, nr, addr, .aq);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /**
162*4882a593Smuzhiyun  * clear_bit_unlock - Clear a bit in memory, for unlock
163*4882a593Smuzhiyun  * @nr: the bit to set
164*4882a593Smuzhiyun  * @addr: the address to start counting from
165*4882a593Smuzhiyun  *
166*4882a593Smuzhiyun  * This operation is atomic and provides release barrier semantics.
167*4882a593Smuzhiyun  */
clear_bit_unlock(unsigned long nr,volatile unsigned long * addr)168*4882a593Smuzhiyun static inline void clear_bit_unlock(
169*4882a593Smuzhiyun 	unsigned long nr, volatile unsigned long *addr)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	__op_bit_ord(and, __NOT, nr, addr, .rl);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun  * __clear_bit_unlock - Clear a bit in memory, for unlock
176*4882a593Smuzhiyun  * @nr: the bit to set
177*4882a593Smuzhiyun  * @addr: the address to start counting from
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * This operation is like clear_bit_unlock, however it is not atomic.
180*4882a593Smuzhiyun  * It does provide release barrier semantics so it can be used to unlock
181*4882a593Smuzhiyun  * a bit lock, however it would only be used if no other CPU can modify
182*4882a593Smuzhiyun  * any bits in the memory until the lock is released (a good example is
183*4882a593Smuzhiyun  * if the bit lock itself protects access to the other bits in the word).
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * On RISC-V systems there seems to be no benefit to taking advantage of the
186*4882a593Smuzhiyun  * non-atomic property here: it's a lot more instructions and we still have to
187*4882a593Smuzhiyun  * provide release semantics anyway.
188*4882a593Smuzhiyun  */
__clear_bit_unlock(unsigned long nr,volatile unsigned long * addr)189*4882a593Smuzhiyun static inline void __clear_bit_unlock(
190*4882a593Smuzhiyun 	unsigned long nr, volatile unsigned long *addr)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	clear_bit_unlock(nr, addr);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun #undef __test_and_op_bit
196*4882a593Smuzhiyun #undef __op_bit
197*4882a593Smuzhiyun #undef __NOP
198*4882a593Smuzhiyun #undef __NOT
199*4882a593Smuzhiyun #undef __AMO
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun #include <asm-generic/bitops/non-atomic.h>
202*4882a593Smuzhiyun #include <asm-generic/bitops/le.h>
203*4882a593Smuzhiyun #include <asm-generic/bitops/ext2-atomic.h>
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun #endif /* _ASM_RISCV_BITOPS_H */
206