2 * include/asm-xtensa/bitops.h
4 * Atomic operations that C can't guarantee us.Useful for resource counting etc.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
13 #ifndef _XTENSA_BITOPS_H
14 #define _XTENSA_BITOPS_H
18 #include <asm/processor.h>
19 #include <asm/byteorder.h>
20 #include <asm/system.h>
23 # error SMP not supported on this architecture
26 static __inline__
void set_bit(int nr
, volatile void * addr
)
28 unsigned long mask
= 1 << (nr
& 0x1f);
29 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
32 local_irq_save(flags
);
34 local_irq_restore(flags
);
37 static __inline__
void __set_bit(int nr
, volatile unsigned long * addr
)
39 unsigned long mask
= 1 << (nr
& 0x1f);
40 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
45 static __inline__
void clear_bit(int nr
, volatile void * addr
)
47 unsigned long mask
= 1 << (nr
& 0x1f);
48 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
51 local_irq_save(flags
);
53 local_irq_restore(flags
);
56 static __inline__
void __clear_bit(int nr
, volatile unsigned long *addr
)
58 unsigned long mask
= 1 << (nr
& 0x1f);
59 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
65 * clear_bit() doesn't provide any barrier for the compiler.
68 #define smp_mb__before_clear_bit() barrier()
69 #define smp_mb__after_clear_bit() barrier()
71 static __inline__
void change_bit(int nr
, volatile void * addr
)
73 unsigned long mask
= 1 << (nr
& 0x1f);
74 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
77 local_irq_save(flags
);
79 local_irq_restore(flags
);
82 static __inline__
void __change_bit(int nr
, volatile void * addr
)
84 unsigned long mask
= 1 << (nr
& 0x1f);
85 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
90 static __inline__
int test_and_set_bit(int nr
, volatile void * addr
)
93 unsigned long mask
= 1 << (nr
& 0x1f);
94 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
97 local_irq_save(flags
);
98 retval
= (mask
& *a
) != 0;
100 local_irq_restore(flags
);
105 static __inline__
int __test_and_set_bit(int nr
, volatile void * addr
)
107 unsigned long retval
;
108 unsigned long mask
= 1 << (nr
& 0x1f);
109 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
111 retval
= (mask
& *a
) != 0;
117 static __inline__
int test_and_clear_bit(int nr
, volatile void * addr
)
119 unsigned long retval
;
120 unsigned long mask
= 1 << (nr
& 0x1f);
121 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
124 local_irq_save(flags
);
125 retval
= (mask
& *a
) != 0;
127 local_irq_restore(flags
);
132 static __inline__
int __test_and_clear_bit(int nr
, volatile void * addr
)
134 unsigned long mask
= 1 << (nr
& 0x1f);
135 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
136 unsigned long old
= *a
;
139 return (old
& mask
) != 0;
142 static __inline__
int test_and_change_bit(int nr
, volatile void * addr
)
144 unsigned long retval
;
145 unsigned long mask
= 1 << (nr
& 0x1f);
146 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
149 local_irq_save(flags
);
151 retval
= (mask
& *a
) != 0;
153 local_irq_restore(flags
);
159 * non-atomic version; can be reordered
162 static __inline__
int __test_and_change_bit(int nr
, volatile void *addr
)
164 unsigned long mask
= 1 << (nr
& 0x1f);
165 unsigned long *a
= ((unsigned long *)addr
) + (nr
>> 5);
166 unsigned long old
= *a
;
169 return (old
& mask
) != 0;
172 static __inline__
int test_bit(int nr
, const volatile void *addr
)
174 return 1UL & (((const volatile unsigned int *)addr
)[nr
>>5] >> (nr
&31));
179 static __inline__
int __cntlz (unsigned long x
)
182 asm ("nsau %0, %1" : "=r" (lz
) : "r" (x
));
188 static __inline__
int __cntlz (unsigned long x
)
190 unsigned long sum
, x1
, x2
, x4
, x8
, x16
;
195 x16
= x
& 0xFFFF0000;
197 sum
+= (x16
!= 0) * 16;
198 sum
+= (x8
!= 0) * 8;
199 sum
+= (x4
!= 0) * 4;
208 * ffz: Find first zero in word. Undefined if no zero exists.
209 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
212 static __inline__
int ffz(unsigned long x
)
216 return __cntlz(x
& -x
);
220 * __ffs: Find first bit set in word. Return 0 for bit 0
223 static __inline__
int __ffs(unsigned long x
)
225 return __cntlz(x
& -x
);
229 * ffs: Find first bit set in word. This is defined the same way as
230 * the libc and compiler builtin ffs routines, therefore
231 * differs in spirit from the above ffz (man ffs).
234 static __inline__
int ffs(unsigned long x
)
236 return __cntlz(x
& -x
) + 1;
240 * fls: Find last (most-significant) bit set in word.
241 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
244 static __inline__
int fls (unsigned int x
)
248 #define fls64(x) generic_fls64(x)
250 static __inline__
int
251 find_next_bit(const unsigned long *addr
, int size
, int offset
)
253 const unsigned long *p
= addr
+ (offset
>> 5);
254 unsigned long result
= offset
& ~31UL;
263 tmp
&= ~0UL << offset
;
272 if ((tmp
= *p
++) != 0)
282 tmp
&= ~0UL >> (32 - size
);
283 if (tmp
== 0UL) /* Are any bits set? */
284 return result
+ size
; /* Nope. */
286 return result
+ __ffs(tmp
);
290 * find_first_bit - find the first set bit in a memory region
291 * @addr: The address to start the search at
292 * @size: The maximum size to search
294 * Returns the bit-number of the first set bit, not the number of the byte
298 #define find_first_bit(addr, size) \
299 find_next_bit((addr), (size), 0)
301 static __inline__
int
302 find_next_zero_bit(const unsigned long *addr
, int size
, int offset
)
304 const unsigned long *p
= addr
+ (offset
>> 5);
305 unsigned long result
= offset
& ~31UL;
314 tmp
|= ~0UL >> (32-offset
);
322 while (size
& ~31UL) {
335 return result
+ ffz(tmp
);
338 #define find_first_zero_bit(addr, size) \
339 find_next_zero_bit((addr), (size), 0)
342 # define ext2_set_bit(nr,addr) __test_and_set_bit((nr), (addr))
343 # define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit((nr),(addr))
344 # define ext2_clear_bit(nr,addr) __test_and_clear_bit((nr), (addr))
345 # define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr),(addr))
346 # define ext2_test_bit(nr,addr) test_bit((nr), (addr))
347 # define ext2_find_first_zero_bit(addr, size) find_first_zero_bit((addr),(size))
348 # define ext2_find_next_zero_bit(addr, size, offset) \
349 find_next_zero_bit((addr), (size), (offset))
350 #elif defined(__XTENSA_EB__)
351 # define ext2_set_bit(nr,addr) __test_and_set_bit((nr) ^ 0x18, (addr))
352 # define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit((nr) ^ 0x18, (addr))
353 # define ext2_clear_bit(nr,addr) __test_and_clear_bit((nr) ^ 18, (addr))
354 # define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr)^0x18,(addr))
355 # define ext2_test_bit(nr,addr) test_bit((nr) ^ 0x18, (addr))
356 # define ext2_find_first_zero_bit(addr, size) \
357 ext2_find_next_zero_bit((addr), (size), 0)
359 static __inline__
unsigned long ext2_find_next_zero_bit(void *addr
, unsigned long size
, unsigned long offset
)
361 unsigned long *p
= ((unsigned long *) addr
) + (offset
>> 5);
362 unsigned long result
= offset
& ~31UL;
370 /* We hold the little endian value in tmp, but then the
371 * shift is illegal. So we could keep a big endian value
374 * tmp = __swab32(*(p++));
375 * tmp |= ~0UL >> (32-offset);
377 * but this would decrease preformance, so we change the
381 tmp
|= __swab32(~0UL >> (32-offset
));
389 while(size
& ~31UL) {
400 /* tmp is little endian, so we would have to swab the shift,
401 * see above. But then we have to swab tmp below for ffz, so
402 * we might as well do this here.
404 return result
+ ffz(__swab32(tmp
) | (~0UL << size
));
406 return result
+ ffz(__swab32(tmp
));
410 # error processor byte order undefined!
414 #define hweight32(x) generic_hweight32(x)
415 #define hweight16(x) generic_hweight16(x)
416 #define hweight8(x) generic_hweight8(x)
419 * Find the first bit set in a 140-bit bitmap.
420 * The first 100 bits are unlikely to be set.
423 static inline int sched_find_first_bit(const unsigned long *b
)
428 return __ffs(b
[1]) + 32;
430 return __ffs(b
[2]) + 64;
432 return __ffs(b
[3]) + 96;
433 return __ffs(b
[4]) + 128;
437 /* Bitmap functions for the minix filesystem. */
439 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
440 #define minix_set_bit(nr,addr) set_bit(nr,addr)
441 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
442 #define minix_test_bit(nr,addr) test_bit(nr,addr)
443 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
445 #endif /* __KERNEL__ */
447 #endif /* _XTENSA_BITOPS_H */