2 * Copyright 1995, Russell King.
3 * Various bits and pieces copyrights include:
4 * Linus Torvalds (test_bit).
5 * Big endian support: Copyright 2001, Nicolas Pitre
8 * bit 0 is the LSB of an "unsigned long" quantity.
10 * Please note that the code in this file should never be included
11 * from user space. Many of these are not implemented in assembler
12 * since they would be too costly. Also, they require privileged
13 * instructions (which are not available from user mode) to ensure
14 * that they are atomic.
17 #ifndef __ASM_ARM_BITOPS_H
18 #define __ASM_ARM_BITOPS_H
22 #ifndef _LINUX_BITOPS_H
23 #error only <linux/bitops.h> can be included directly
26 #include <linux/compiler.h>
27 #include <linux/irqflags.h>
28 #include <asm/barrier.h>
31 * These functions are the basis of our bit ops.
33 * First, the atomic bitops. These use native endian.
35 static inline void ____atomic_set_bit(unsigned int bit
, volatile unsigned long *p
)
38 unsigned long mask
= BIT_MASK(bit
);
42 raw_local_irq_save(flags
);
44 raw_local_irq_restore(flags
);
47 static inline void ____atomic_clear_bit(unsigned int bit
, volatile unsigned long *p
)
50 unsigned long mask
= BIT_MASK(bit
);
54 raw_local_irq_save(flags
);
56 raw_local_irq_restore(flags
);
59 static inline void ____atomic_change_bit(unsigned int bit
, volatile unsigned long *p
)
62 unsigned long mask
= BIT_MASK(bit
);
66 raw_local_irq_save(flags
);
68 raw_local_irq_restore(flags
);
72 ____atomic_test_and_set_bit(unsigned int bit
, volatile unsigned long *p
)
76 unsigned long mask
= BIT_MASK(bit
);
80 raw_local_irq_save(flags
);
83 raw_local_irq_restore(flags
);
85 return (res
& mask
) != 0;
89 ____atomic_test_and_clear_bit(unsigned int bit
, volatile unsigned long *p
)
93 unsigned long mask
= BIT_MASK(bit
);
97 raw_local_irq_save(flags
);
100 raw_local_irq_restore(flags
);
102 return (res
& mask
) != 0;
106 ____atomic_test_and_change_bit(unsigned int bit
, volatile unsigned long *p
)
110 unsigned long mask
= BIT_MASK(bit
);
114 raw_local_irq_save(flags
);
117 raw_local_irq_restore(flags
);
119 return (res
& mask
) != 0;
122 #include <asm-generic/bitops/non-atomic.h>
125 * A note about Endian-ness.
126 * -------------------------
128 * When the ARM is put into big endian mode via CR15, the processor
129 * merely swaps the order of bytes within words, thus:
131 * ------------ physical data bus bits -----------
132 * D31 ... D24 D23 ... D16 D15 ... D8 D7 ... D0
133 * little byte 3 byte 2 byte 1 byte 0
134 * big byte 0 byte 1 byte 2 byte 3
136 * This means that reading a 32-bit word at address 0 returns the same
137 * value irrespective of the endian mode bit.
139 * Peripheral devices should be connected with the data bus reversed in
140 * "Big Endian" mode. ARM Application Note 61 is applicable, and is
141 * available from http://www.arm.com/.
143 * The following assumes that the data bus connectivity for big endian
144 * mode has been followed.
146 * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
150 * Native endian assembly bitops. nr = 0 -> word 0 bit 0.
152 extern void _set_bit(int nr
, volatile unsigned long * p
);
153 extern void _clear_bit(int nr
, volatile unsigned long * p
);
154 extern void _change_bit(int nr
, volatile unsigned long * p
);
155 extern int _test_and_set_bit(int nr
, volatile unsigned long * p
);
156 extern int _test_and_clear_bit(int nr
, volatile unsigned long * p
);
157 extern int _test_and_change_bit(int nr
, volatile unsigned long * p
);
160 * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
162 extern int _find_first_zero_bit_le(const void * p
, unsigned size
);
163 extern int _find_next_zero_bit_le(const void * p
, int size
, int offset
);
164 extern int _find_first_bit_le(const unsigned long *p
, unsigned size
);
165 extern int _find_next_bit_le(const unsigned long *p
, int size
, int offset
);
168 * Big endian assembly bitops. nr = 0 -> byte 3 bit 0.
170 extern int _find_first_zero_bit_be(const void * p
, unsigned size
);
171 extern int _find_next_zero_bit_be(const void * p
, int size
, int offset
);
172 extern int _find_first_bit_be(const unsigned long *p
, unsigned size
);
173 extern int _find_next_bit_be(const unsigned long *p
, int size
, int offset
);
177 * The __* form of bitops are non-atomic and may be reordered.
179 #define ATOMIC_BITOP(name,nr,p) \
180 (__builtin_constant_p(nr) ? ____atomic_##name(nr, p) : _##name(nr,p))
182 #define ATOMIC_BITOP(name,nr,p) _##name(nr,p)
186 * Native endian atomic definitions.
188 #define set_bit(nr,p) ATOMIC_BITOP(set_bit,nr,p)
189 #define clear_bit(nr,p) ATOMIC_BITOP(clear_bit,nr,p)
190 #define change_bit(nr,p) ATOMIC_BITOP(change_bit,nr,p)
191 #define test_and_set_bit(nr,p) ATOMIC_BITOP(test_and_set_bit,nr,p)
192 #define test_and_clear_bit(nr,p) ATOMIC_BITOP(test_and_clear_bit,nr,p)
193 #define test_and_change_bit(nr,p) ATOMIC_BITOP(test_and_change_bit,nr,p)
197 * These are the little endian, atomic definitions.
199 #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
200 #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
201 #define find_first_bit(p,sz) _find_first_bit_le(p,sz)
202 #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
206 * These are the big endian, atomic definitions.
208 #define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz)
209 #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off)
210 #define find_first_bit(p,sz) _find_first_bit_be(p,sz)
211 #define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off)
215 #if __LINUX_ARM_ARCH__ < 5
217 #include <asm-generic/bitops/ffz.h>
218 #include <asm-generic/bitops/__fls.h>
219 #include <asm-generic/bitops/__ffs.h>
220 #include <asm-generic/bitops/fls.h>
221 #include <asm-generic/bitops/ffs.h>
225 static inline int constant_fls(int x
)
231 if (!(x
& 0xffff0000u
)) {
235 if (!(x
& 0xff000000u
)) {
239 if (!(x
& 0xf0000000u
)) {
243 if (!(x
& 0xc0000000u
)) {
247 if (!(x
& 0x80000000u
)) {
255 * On ARMv5 and above those functions can be implemented around the
256 * clz instruction for much better code efficiency. __clz returns
257 * the number of leading zeros, zero input will return 32, and
258 * 0x80000000 will return 0.
260 static inline unsigned int __clz(unsigned int x
)
264 asm("clz\t%0, %1" : "=r" (ret
) : "r" (x
));
270 * fls() returns zero if the input is zero, otherwise returns the bit
271 * position of the last set bit, where the LSB is 1 and MSB is 32.
273 static inline int fls(int x
)
275 if (__builtin_constant_p(x
))
276 return constant_fls(x
);
278 return 32 - __clz(x
);
282 * __fls() returns the bit position of the last bit set, where the
283 * LSB is 0 and MSB is 31. Zero input is undefined.
285 static inline unsigned long __fls(unsigned long x
)
291 * ffs() returns zero if the input was zero, otherwise returns the bit
292 * position of the first set bit, where the LSB is 1 and MSB is 32.
294 static inline int ffs(int x
)
300 * __ffs() returns the bit position of the first bit set, where the
301 * LSB is 0 and MSB is 31. Zero input is undefined.
303 static inline unsigned long __ffs(unsigned long x
)
308 #define ffz(x) __ffs( ~(x) )
312 #include <asm-generic/bitops/fls64.h>
314 #include <asm-generic/bitops/sched.h>
315 #include <asm-generic/bitops/hweight.h>
316 #include <asm-generic/bitops/lock.h>
320 static inline int find_first_zero_bit_le(const void *p
, unsigned size
)
322 return _find_first_zero_bit_le(p
, size
);
324 #define find_first_zero_bit_le find_first_zero_bit_le
326 static inline int find_next_zero_bit_le(const void *p
, int size
, int offset
)
328 return _find_next_zero_bit_le(p
, size
, offset
);
330 #define find_next_zero_bit_le find_next_zero_bit_le
332 static inline int find_next_bit_le(const void *p
, int size
, int offset
)
334 return _find_next_bit_le(p
, size
, offset
);
336 #define find_next_bit_le find_next_bit_le
340 #include <asm-generic/bitops/le.h>
343 * Ext2 is defined to use little-endian byte ordering.
345 #include <asm-generic/bitops/ext2-atomic-setbit.h>
347 #endif /* __KERNEL__ */
349 #endif /* _ARM_BITOPS_H */