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