1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright 1995, Russell King.
4 * Various bits and pieces copyrights include:
5 * Linus Torvalds (test_bit).
6 * Big endian support: Copyright 2001, Nicolas Pitre
9 * bit 0 is the LSB of an "unsigned long" quantity.
11 * Please note that the code in this file should never be included
12 * from user space. Many of these are not implemented in assembler
13 * since they would be too costly. Also, they require privileged
14 * instructions (which are not available from user mode) to ensure
15 * that they are atomic.
18 #ifndef __ASM_ARM_BITOPS_H
19 #define __ASM_ARM_BITOPS_H
23 #ifndef _LINUX_BITOPS_H
24 #error only <linux/bitops.h> can be included directly
27 #include <linux/compiler.h>
28 #include <linux/irqflags.h>
29 #include <asm/barrier.h>
32 * These functions are the basis of our bit ops.
34 * First, the atomic bitops. These use native endian.
36 static inline void ____atomic_set_bit(unsigned int bit
, volatile unsigned long *p
)
39 unsigned long mask
= BIT_MASK(bit
);
43 raw_local_irq_save(flags
);
45 raw_local_irq_restore(flags
);
48 static inline void ____atomic_clear_bit(unsigned int bit
, volatile unsigned long *p
)
51 unsigned long mask
= BIT_MASK(bit
);
55 raw_local_irq_save(flags
);
57 raw_local_irq_restore(flags
);
60 static inline void ____atomic_change_bit(unsigned int bit
, volatile unsigned long *p
)
63 unsigned long mask
= BIT_MASK(bit
);
67 raw_local_irq_save(flags
);
69 raw_local_irq_restore(flags
);
73 ____atomic_test_and_set_bit(unsigned int bit
, volatile unsigned long *p
)
77 unsigned long mask
= BIT_MASK(bit
);
81 raw_local_irq_save(flags
);
84 raw_local_irq_restore(flags
);
86 return (res
& mask
) != 0;
90 ____atomic_test_and_clear_bit(unsigned int bit
, volatile unsigned long *p
)
94 unsigned long mask
= BIT_MASK(bit
);
98 raw_local_irq_save(flags
);
101 raw_local_irq_restore(flags
);
103 return (res
& mask
) != 0;
107 ____atomic_test_and_change_bit(unsigned int bit
, volatile unsigned long *p
)
111 unsigned long mask
= BIT_MASK(bit
);
115 raw_local_irq_save(flags
);
118 raw_local_irq_restore(flags
);
120 return (res
& mask
) != 0;
123 #include <asm-generic/bitops/non-atomic.h>
126 * A note about Endian-ness.
127 * -------------------------
129 * When the ARM is put into big endian mode via CR15, the processor
130 * merely swaps the order of bytes within words, thus:
132 * ------------ physical data bus bits -----------
133 * D31 ... D24 D23 ... D16 D15 ... D8 D7 ... D0
134 * little byte 3 byte 2 byte 1 byte 0
135 * big byte 0 byte 1 byte 2 byte 3
137 * This means that reading a 32-bit word at address 0 returns the same
138 * value irrespective of the endian mode bit.
140 * Peripheral devices should be connected with the data bus reversed in
141 * "Big Endian" mode. ARM Application Note 61 is applicable, and is
142 * available from http://www.arm.com/.
144 * The following assumes that the data bus connectivity for big endian
145 * mode has been followed.
147 * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
151 * Native endian assembly bitops. nr = 0 -> word 0 bit 0.
153 extern void _set_bit(int nr
, volatile unsigned long * p
);
154 extern void _clear_bit(int nr
, volatile unsigned long * p
);
155 extern void _change_bit(int nr
, volatile unsigned long * p
);
156 extern int _test_and_set_bit(int nr
, volatile unsigned long * p
);
157 extern int _test_and_clear_bit(int nr
, volatile unsigned long * p
);
158 extern int _test_and_change_bit(int nr
, volatile unsigned long * p
);
161 * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
163 unsigned long _find_first_zero_bit_le(const unsigned long *p
, unsigned long size
);
164 unsigned long _find_next_zero_bit_le(const unsigned long *p
,
165 unsigned long size
, unsigned long offset
);
166 unsigned long _find_first_bit_le(const unsigned long *p
, unsigned long size
);
167 unsigned long _find_next_bit_le(const unsigned long *p
, unsigned long size
, unsigned long offset
);
170 * Big endian assembly bitops. nr = 0 -> byte 3 bit 0.
172 unsigned long _find_first_zero_bit_be(const unsigned long *p
, unsigned long size
);
173 unsigned long _find_next_zero_bit_be(const unsigned long *p
,
174 unsigned long size
, unsigned long offset
);
175 unsigned long _find_first_bit_be(const unsigned long *p
, unsigned long size
);
176 unsigned long _find_next_bit_be(const unsigned long *p
, unsigned long size
, unsigned long offset
);
180 * The __* form of bitops are non-atomic and may be reordered.
182 #define ATOMIC_BITOP(name,nr,p) \
183 (__builtin_constant_p(nr) ? ____atomic_##name(nr, p) : _##name(nr,p))
185 #define ATOMIC_BITOP(name,nr,p) _##name(nr,p)
189 * Native endian atomic definitions.
191 #define set_bit(nr,p) ATOMIC_BITOP(set_bit,nr,p)
192 #define clear_bit(nr,p) ATOMIC_BITOP(clear_bit,nr,p)
193 #define change_bit(nr,p) ATOMIC_BITOP(change_bit,nr,p)
194 #define test_and_set_bit(nr,p) ATOMIC_BITOP(test_and_set_bit,nr,p)
195 #define test_and_clear_bit(nr,p) ATOMIC_BITOP(test_and_clear_bit,nr,p)
196 #define test_and_change_bit(nr,p) ATOMIC_BITOP(test_and_change_bit,nr,p)
200 * These are the little endian, atomic definitions.
202 #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
203 #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
204 #define find_first_bit(p,sz) _find_first_bit_le(p,sz)
205 #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
209 * These are the big endian, atomic definitions.
211 #define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz)
212 #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off)
213 #define find_first_bit(p,sz) _find_first_bit_be(p,sz)
214 #define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off)
218 #if __LINUX_ARM_ARCH__ < 5
220 #include <asm-generic/bitops/__fls.h>
221 #include <asm-generic/bitops/__ffs.h>
222 #include <asm-generic/bitops/fls.h>
223 #include <asm-generic/bitops/ffs.h>
228 * On ARMv5 and above, the gcc built-ins may rely on the clz instruction
229 * and produce optimal inlined code in all cases. On ARMv7 it is even
230 * better by also using the rbit instruction.
232 #include <asm-generic/bitops/builtin-__fls.h>
233 #include <asm-generic/bitops/builtin-__ffs.h>
234 #include <asm-generic/bitops/builtin-fls.h>
235 #include <asm-generic/bitops/builtin-ffs.h>
239 #include <asm-generic/bitops/ffz.h>
241 #include <asm-generic/bitops/fls64.h>
243 #include <asm-generic/bitops/sched.h>
244 #include <asm-generic/bitops/hweight.h>
245 #include <asm-generic/bitops/lock.h>
249 static inline int find_first_zero_bit_le(const void *p
, unsigned size
)
251 return _find_first_zero_bit_le(p
, size
);
253 #define find_first_zero_bit_le find_first_zero_bit_le
255 static inline int find_next_zero_bit_le(const void *p
, int size
, int offset
)
257 return _find_next_zero_bit_le(p
, size
, offset
);
259 #define find_next_zero_bit_le find_next_zero_bit_le
261 static inline int find_next_bit_le(const void *p
, int size
, int offset
)
263 return _find_next_bit_le(p
, size
, offset
);
265 #define find_next_bit_le find_next_bit_le
269 #include <asm-generic/bitops/le.h>
272 * Ext2 is defined to use little-endian byte ordering.
274 #include <asm-generic/bitops/ext2-atomic-setbit.h>
276 #endif /* __KERNEL__ */
278 #endif /* _ARM_BITOPS_H */