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 addr; bit 32 is the LSB of (addr+1).
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 priviledged
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 #include <asm/system.h>
24 #define smp_mb__before_clear_bit() do { } while (0)
25 #define smp_mb__after_clear_bit() do { } while (0)
28 * These functions are the basis of our bit ops.
29 * First, the atomic bitops.
31 * The endian issue for these functions is handled by the macros below.
34 ____atomic_set_bit(unsigned int bit, volatile unsigned long *p)
37 unsigned long mask = 1UL << (bit & 31);
41 local_irq_save(flags);
43 local_irq_restore(flags);
47 ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p)
50 unsigned long mask = 1UL << (bit & 31);
54 local_irq_save(flags);
56 local_irq_restore(flags);
60 ____atomic_change_bit(unsigned int bit, volatile unsigned long *p)
63 unsigned long mask = 1UL << (bit & 31);
67 local_irq_save(flags);
69 local_irq_restore(flags);
73 ____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p)
77 unsigned long mask = 1UL << (bit & 31);
81 local_irq_save(flags);
84 local_irq_restore(flags);
90 ____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
94 unsigned long mask = 1UL << (bit & 31);
98 local_irq_save(flags);
101 local_irq_restore(flags);
107 ____atomic_test_and_change_bit_mask(unsigned int bit, volatile unsigned long *p)
111 unsigned long mask = 1UL << (bit & 31);
115 local_irq_save(flags);
118 local_irq_restore(flags);
124 * Now the non-atomic variants. We let the compiler handle all
125 * optimisations for these. These are all _native_ endian.
127 static inline void __set_bit(int nr, volatile unsigned long *p)
129 p[nr >> 5] |= (1UL << (nr & 31));
132 static inline void __clear_bit(int nr, volatile unsigned long *p)
134 p[nr >> 5] &= ~(1UL << (nr & 31));
137 static inline void __change_bit(int nr, volatile unsigned long *p)
139 p[nr >> 5] ^= (1UL << (nr & 31));
142 static inline int __test_and_set_bit(int nr, volatile unsigned long *p)
144 unsigned long oldval, mask = 1UL << (nr & 31);
150 return oldval & mask;
153 static inline int __test_and_clear_bit(int nr, volatile unsigned long *p)
155 unsigned long oldval, mask = 1UL << (nr & 31);
162 return oldval & mask;
165 static inline int __test_and_change_bit(int nr, volatile unsigned long *p)
167 unsigned long oldval, mask = 1UL << (nr & 31);
174 return oldval & mask;
178 * This routine doesn't need to be atomic.
180 static inline int __test_bit(int nr, const unsigned long * p)
182 return p[nr >> 5] & (1UL << (nr & 31));
186 * A note about Endian-ness.
187 * -------------------------
189 * ------------ physical data bus bits -----------
190 * D31 ... D24 D23 ... D16 D15 ... D8 D7 ... D0
191 * byte 3 byte 2 byte 1 byte 0
193 * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
197 * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
199 extern void _set_bit_le(int nr, volatile unsigned long * p);
200 extern void _clear_bit_le(int nr, volatile unsigned long * p);
201 extern void _change_bit_le(int nr, volatile unsigned long * p);
202 extern int _test_and_set_bit_le(int nr, volatile unsigned long * p);
203 extern int _test_and_clear_bit_le(int nr, volatile unsigned long * p);
204 extern int _test_and_change_bit_le(int nr, volatile unsigned long * p);
205 extern int _find_first_zero_bit_le(void * p, unsigned size);
206 extern int _find_next_zero_bit_le(void * p, int size, int offset);
209 * The __* form of bitops are non-atomic and may be reordered.
211 #define ATOMIC_BITOP_LE(name,nr,p) \
212 (__builtin_constant_p(nr) ? \
213 ____atomic_##name(nr, p) : \
216 #define ATOMIC_BITOP_BE(name,nr,p) \
217 (__builtin_constant_p(nr) ? \
218 ____atomic_##name(nr, p) : \
221 #define NONATOMIC_BITOP(name,nr,p) \
222 (____nonatomic_##name(nr, p))
225 * These are the little endian, atomic definitions.
227 #define set_bit(nr,p) ATOMIC_BITOP_LE(set_bit,nr,p)
228 #define clear_bit(nr,p) ATOMIC_BITOP_LE(clear_bit,nr,p)
229 #define change_bit(nr,p) ATOMIC_BITOP_LE(change_bit,nr,p)
230 #define test_and_set_bit(nr,p) ATOMIC_BITOP_LE(test_and_set_bit,nr,p)
231 #define test_and_clear_bit(nr,p) ATOMIC_BITOP_LE(test_and_clear_bit,nr,p)
232 #define test_and_change_bit(nr,p) ATOMIC_BITOP_LE(test_and_change_bit,nr,p)
233 #define test_bit(nr,p) __test_bit(nr,p)
234 #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
235 #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
237 #define WORD_BITOFF_TO_LE(x) ((x))
240 * ffz = Find First Zero in word. Undefined if no zero exists,
241 * so code should check against ~0UL first..
243 static inline unsigned long ffz(unsigned long word)
249 if (word & 0x0000ffff) { k -= 16; word <<= 16; }
250 if (word & 0x00ff0000) { k -= 8; word <<= 8; }
251 if (word & 0x0f000000) { k -= 4; word <<= 4; }
252 if (word & 0x30000000) { k -= 2; word <<= 2; }
253 if (word & 0x40000000) { k -= 1; }
258 * ffz = Find First Zero in word. Undefined if no zero exists,
259 * so code should check against ~0UL first..
261 static inline unsigned long __ffs(unsigned long word)
266 if (word & 0x0000ffff) { k -= 16; word <<= 16; }
267 if (word & 0x00ff0000) { k -= 8; word <<= 8; }
268 if (word & 0x0f000000) { k -= 4; word <<= 4; }
269 if (word & 0x30000000) { k -= 2; word <<= 2; }
270 if (word & 0x40000000) { k -= 1; }
275 * fls: find last bit set.
278 #define fls(x) generic_fls(x)
281 * ffs: find first bit set. This is defined the same way as
282 * the libc and compiler builtin ffs routines, therefore
283 * differs in spirit from the above ffz (man ffs).
286 #define ffs(x) generic_ffs(x)
289 * Find first bit set in a 168-bit bitmap, where the first
290 * 128 bits are unlikely to be set.
292 static inline int sched_find_first_bit(unsigned long *b)
297 for (off = 0; v = b[off], off < 4; off++) {
301 return __ffs(v) + off * 32;
305 * hweightN: returns the hamming weight (i.e. the number
306 * of bits set) of a N-bit word
309 #define hweight32(x) generic_hweight32(x)
310 #define hweight16(x) generic_hweight16(x)
311 #define hweight8(x) generic_hweight8(x)
314 * Ext2 is defined to use little-endian byte ordering.
315 * These do not need to be atomic.
317 #define ext2_set_bit(nr,p) \
318 __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)p)
319 #define ext2_set_bit_atomic(lock,nr,p) \
320 test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
321 #define ext2_clear_bit(nr,p) \
322 __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)p)
323 #define ext2_clear_bit_atomic(lock,nr,p) \
324 test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
325 #define ext2_test_bit(nr,p) \
326 __test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)p)
327 #define ext2_find_first_zero_bit(p,sz) \
328 _find_first_zero_bit_le(p,sz)
329 #define ext2_find_next_zero_bit(p,sz,off) \
330 _find_next_zero_bit_le(p,sz,off)
333 * Minix is defined to use little-endian byte ordering.
334 * These do not need to be atomic.
336 #define minix_set_bit(nr,p) \
337 __set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)p)
338 #define minix_test_bit(nr,p) \
339 __test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)p)
340 #define minix_test_and_set_bit(nr,p) \
341 __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)p)
342 #define minix_test_and_clear_bit(nr,p) \
343 __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)p)
344 #define minix_find_first_zero_bit(p,sz) \
345 _find_first_zero_bit_le(p,sz)
347 #endif /* __KERNEL__ */
349 #endif /* _ARM_BITOPS_H */