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 - 2007 Tensilica Inc.
13 #ifndef _XTENSA_BITOPS_H
14 #define _XTENSA_BITOPS_H
18 #ifndef _LINUX_BITOPS_H
19 #error only <linux/bitops.h> can be included directly
22 #include <asm/processor.h>
23 #include <asm/byteorder.h>
24 #include <asm/barrier.h>
26 #include <asm-generic/bitops/non-atomic.h>
30 static inline unsigned long __cntlz (unsigned long x
)
33 asm ("nsau %0, %1" : "=r" (lz
) : "r" (x
));
38 * ffz: Find first zero in word. Undefined if no zero exists.
39 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
42 static inline int ffz(unsigned long x
)
44 return 31 - __cntlz(~x
& -~x
);
48 * __ffs: Find first bit set in word. Return 0 for bit 0
51 static inline unsigned long __ffs(unsigned long x
)
53 return 31 - __cntlz(x
& -x
);
57 * ffs: Find first bit set in word. This is defined the same way as
58 * the libc and compiler builtin ffs routines, therefore
59 * differs in spirit from the above ffz (man ffs).
62 static inline int ffs(unsigned long x
)
64 return 32 - __cntlz(x
& -x
);
68 * fls: Find last (most-significant) bit set in word.
69 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
72 static inline int fls (unsigned int x
)
74 return 32 - __cntlz(x
);
78 * __fls - find last (most-significant) set bit in a long word
79 * @word: the word to search
81 * Undefined if no set bit exists, so code should check against 0 first.
83 static inline unsigned long __fls(unsigned long word
)
85 return 31 - __cntlz(word
);
89 /* Use the generic implementation if we don't have the nsa/nsau instructions. */
91 # include <asm-generic/bitops/ffs.h>
92 # include <asm-generic/bitops/__ffs.h>
93 # include <asm-generic/bitops/ffz.h>
94 # include <asm-generic/bitops/fls.h>
95 # include <asm-generic/bitops/__fls.h>
99 #include <asm-generic/bitops/fls64.h>
101 #if XCHAL_HAVE_S32C1I
103 static inline void set_bit(unsigned int bit
, volatile unsigned long *p
)
105 unsigned long tmp
, value
;
106 unsigned long mask
= 1UL << (bit
& 31);
110 __asm__
__volatile__(
111 "1: l32i %1, %3, 0\n"
112 " wsr %1, scompare1\n"
114 " s32c1i %0, %3, 0\n"
116 : "=&a" (tmp
), "=&a" (value
)
117 : "a" (mask
), "a" (p
)
121 static inline void clear_bit(unsigned int bit
, volatile unsigned long *p
)
123 unsigned long tmp
, value
;
124 unsigned long mask
= 1UL << (bit
& 31);
128 __asm__
__volatile__(
129 "1: l32i %1, %3, 0\n"
130 " wsr %1, scompare1\n"
132 " s32c1i %0, %3, 0\n"
134 : "=&a" (tmp
), "=&a" (value
)
135 : "a" (~mask
), "a" (p
)
139 static inline void change_bit(unsigned int bit
, volatile unsigned long *p
)
141 unsigned long tmp
, value
;
142 unsigned long mask
= 1UL << (bit
& 31);
146 __asm__
__volatile__(
147 "1: l32i %1, %3, 0\n"
148 " wsr %1, scompare1\n"
150 " s32c1i %0, %3, 0\n"
152 : "=&a" (tmp
), "=&a" (value
)
153 : "a" (mask
), "a" (p
)
158 test_and_set_bit(unsigned int bit
, volatile unsigned long *p
)
160 unsigned long tmp
, value
;
161 unsigned long mask
= 1UL << (bit
& 31);
165 __asm__
__volatile__(
166 "1: l32i %1, %3, 0\n"
167 " wsr %1, scompare1\n"
169 " s32c1i %0, %3, 0\n"
171 : "=&a" (tmp
), "=&a" (value
)
172 : "a" (mask
), "a" (p
)
179 test_and_clear_bit(unsigned int bit
, volatile unsigned long *p
)
181 unsigned long tmp
, value
;
182 unsigned long mask
= 1UL << (bit
& 31);
186 __asm__
__volatile__(
187 "1: l32i %1, %3, 0\n"
188 " wsr %1, scompare1\n"
190 " s32c1i %0, %3, 0\n"
192 : "=&a" (tmp
), "=&a" (value
)
193 : "a" (~mask
), "a" (p
)
200 test_and_change_bit(unsigned int bit
, volatile unsigned long *p
)
202 unsigned long tmp
, value
;
203 unsigned long mask
= 1UL << (bit
& 31);
207 __asm__
__volatile__(
208 "1: l32i %1, %3, 0\n"
209 " wsr %1, scompare1\n"
211 " s32c1i %0, %3, 0\n"
213 : "=&a" (tmp
), "=&a" (value
)
214 : "a" (mask
), "a" (p
)
222 #include <asm-generic/bitops/atomic.h>
224 #endif /* XCHAL_HAVE_S32C1I */
226 #include <asm-generic/bitops/find.h>
227 #include <asm-generic/bitops/le.h>
229 #include <asm-generic/bitops/ext2-atomic-setbit.h>
231 #include <asm-generic/bitops/hweight.h>
232 #include <asm-generic/bitops/lock.h>
233 #include <asm-generic/bitops/sched.h>
235 #endif /* __KERNEL__ */
237 #endif /* _XTENSA_BITOPS_H */