ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-arm26 / bitops.h
1 /*
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
6  *  reworked by rmk.
7  *
8  * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
9  *
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.
15  */
16
17 #ifndef __ASM_ARM_BITOPS_H
18 #define __ASM_ARM_BITOPS_H
19
20 #ifdef __KERNEL__
21
22 #include <asm/system.h>
23
24 #define smp_mb__before_clear_bit()      do { } while (0)
25 #define smp_mb__after_clear_bit()       do { } while (0)
26
27 /*
28  * These functions are the basis of our bit ops.
29  * First, the atomic bitops.
30  *
31  * The endian issue for these functions is handled by the macros below.
32  */
33 static inline void
34 ____atomic_set_bit(unsigned int bit, volatile unsigned long *p)
35 {
36         unsigned long flags;
37         unsigned long mask = 1UL << (bit & 31);
38
39         p += bit >> 5;
40
41         local_irq_save(flags);
42         *p |= mask;
43         local_irq_restore(flags);
44 }
45
46 static inline void
47 ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p)
48 {
49         unsigned long flags;
50         unsigned long mask = 1UL << (bit & 31);
51
52         p += bit >> 5;
53
54         local_irq_save(flags);
55         *p &= ~mask;
56         local_irq_restore(flags);
57 }
58
59 static inline void
60 ____atomic_change_bit(unsigned int bit, volatile unsigned long *p)
61 {
62         unsigned long flags;
63         unsigned long mask = 1UL << (bit & 31);
64
65         p += bit >> 5;
66
67         local_irq_save(flags);
68         *p ^= mask;
69         local_irq_restore(flags);
70 }
71
72 static inline int
73 ____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p)
74 {
75         unsigned long flags;
76         unsigned int res;
77         unsigned long mask = 1UL << (bit & 31);
78
79         p += bit >> 5;
80
81         local_irq_save(flags);
82         res = *p;
83         *p = res | mask;
84         local_irq_restore(flags);
85
86         return res & mask;
87 }
88
89 static inline int
90 ____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
91 {
92         unsigned long flags;
93         unsigned int res;
94         unsigned long mask = 1UL << (bit & 31);
95
96         p += bit >> 5;
97
98         local_irq_save(flags);
99         res = *p;
100         *p = res & ~mask;
101         local_irq_restore(flags);
102
103         return res & mask;
104 }
105
106 static inline int
107 ____atomic_test_and_change_bit_mask(unsigned int bit, volatile unsigned long *p)
108 {
109         unsigned long flags;
110         unsigned int res;
111         unsigned long mask = 1UL << (bit & 31);
112
113         p += bit >> 5;
114
115         local_irq_save(flags);
116         res = *p;
117         *p = res ^ mask;
118         local_irq_restore(flags);
119
120         return res & mask;
121 }
122
123 /*
124  * Now the non-atomic variants.  We let the compiler handle all
125  * optimisations for these.  These are all _native_ endian.
126  */
127 static inline void __set_bit(int nr, volatile unsigned long *p)
128 {
129         p[nr >> 5] |= (1UL << (nr & 31));
130 }
131
132 static inline void __clear_bit(int nr, volatile unsigned long *p)
133 {
134         p[nr >> 5] &= ~(1UL << (nr & 31));
135 }
136
137 static inline void __change_bit(int nr, volatile unsigned long *p)
138 {
139         p[nr >> 5] ^= (1UL << (nr & 31));
140 }
141
142 static inline int __test_and_set_bit(int nr, volatile unsigned long *p)
143 {
144         unsigned long oldval, mask = 1UL << (nr & 31);
145
146         p += nr >> 5;
147
148         oldval = *p;
149         *p = oldval | mask;
150         return oldval & mask;
151 }
152
153 static inline int __test_and_clear_bit(int nr, volatile unsigned long *p)
154 {
155         unsigned long oldval, mask = 1UL << (nr & 31);
156
157         p += nr >> 5;
158
159         oldval = *p;
160         *p = oldval & ~mask;
161
162         return oldval & mask;
163 }
164
165 static inline int __test_and_change_bit(int nr, volatile unsigned long *p)
166 {
167         unsigned long oldval, mask = 1UL << (nr & 31);
168
169         p += nr >> 5;
170
171         oldval = *p;
172         *p = oldval ^ mask;
173
174         return oldval & mask;
175 }
176
177 /*
178  * This routine doesn't need to be atomic.
179  */
180 static inline int __test_bit(int nr, const unsigned long * p)
181 {
182         return p[nr >> 5] & (1UL << (nr & 31));
183 }
184
185 /*
186  *  A note about Endian-ness.
187  *  -------------------------
188  *
189  *          ------------ physical data bus bits -----------
190  *          D31 ... D24  D23 ... D16  D15 ... D8  D7 ... D0
191  *            byte 3       byte 2       byte 1      byte 0
192  *
193  * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
194  */
195
196 /*
197  * Little endian assembly bitops.  nr = 0 -> byte 0 bit 0.
198  */
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);
207
208 /*
209  * The __* form of bitops are non-atomic and may be reordered.
210  */
211 #define ATOMIC_BITOP_LE(name,nr,p)              \
212         (__builtin_constant_p(nr) ?             \
213          ____atomic_##name(nr, p) :             \
214          _##name##_le(nr,p))
215
216 #define ATOMIC_BITOP_BE(name,nr,p)              \
217         (__builtin_constant_p(nr) ?             \
218          ____atomic_##name(nr, p) :             \
219          _##name##_be(nr,p))
220
221 #define NONATOMIC_BITOP(name,nr,p)              \
222         (____nonatomic_##name(nr, p))
223
224 /*
225  * These are the little endian, atomic definitions.
226  */
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)
236
237 #define WORD_BITOFF_TO_LE(x)            ((x))
238
239 /*
240  * ffz = Find First Zero in word. Undefined if no zero exists,
241  * so code should check against ~0UL first..
242  */
243 static inline unsigned long ffz(unsigned long word)
244 {
245         int k;
246
247         word = ~word;
248         k = 31;
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; }
254         return k;
255 }
256
257 /*
258  * ffz = Find First Zero in word. Undefined if no zero exists,
259  * so code should check against ~0UL first..
260  */
261 static inline unsigned long __ffs(unsigned long word)
262 {
263         int k;
264
265         k = 31;
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; }
271         return k;
272 }
273
274 /*
275  * fls: find last bit set.
276  */
277
278 #define fls(x) generic_fls(x)
279
280 /*
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).
284  */
285
286 #define ffs(x) generic_ffs(x)
287
288 /*
289  * Find first bit set in a 168-bit bitmap, where the first
290  * 128 bits are unlikely to be set.
291  */
292 static inline int sched_find_first_bit(unsigned long *b)
293 {
294         unsigned long v;
295         unsigned int off;
296
297         for (off = 0; v = b[off], off < 4; off++) {
298                 if (unlikely(v))
299                         break;
300         }
301         return __ffs(v) + off * 32;
302 }
303
304 /*
305  * hweightN: returns the hamming weight (i.e. the number
306  * of bits set) of a N-bit word
307  */
308
309 #define hweight32(x) generic_hweight32(x)
310 #define hweight16(x) generic_hweight16(x)
311 #define hweight8(x) generic_hweight8(x)
312
313 /*
314  * Ext2 is defined to use little-endian byte ordering.
315  * These do not need to be atomic.
316  */
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)
331
332 /*
333  * Minix is defined to use little-endian byte ordering.
334  * These do not need to be atomic.
335  */
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)
346
347 #endif /* __KERNEL__ */
348
349 #endif /* _ARM_BITOPS_H */