2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (c) 1994 - 1997, 99, 2000, 06, 07 Ralf Baechle (ralf@linux-mips.org)
7 * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
12 #ifndef _LINUX_BITOPS_H
13 #error only <linux/bitops.h> can be included directly
16 #include <linux/compiler.h>
17 #include <linux/irqflags.h>
18 #include <linux/types.h>
19 #include <asm/barrier.h>
20 #include <asm/byteorder.h> /* sigh ... */
21 #include <asm/cpu-features.h>
22 #include <asm/sgidefs.h>
25 #if _MIPS_SZLONG == 32
27 #define SZLONG_MASK 31UL
32 #elif _MIPS_SZLONG == 64
34 #define SZLONG_MASK 63UL
42 * clear_bit() doesn't provide any barrier for the compiler.
44 #define smp_mb__before_clear_bit() smp_mb__before_llsc()
45 #define smp_mb__after_clear_bit() smp_llsc_mb()
48 * set_bit - Atomically set a bit in memory
50 * @addr: the address to start counting from
52 * This function is atomic and may not be reordered. See __set_bit()
53 * if you do not require the atomic guarantees.
54 * Note that @nr may be almost arbitrarily large; this function is not
55 * restricted to acting on a single-word quantity.
57 static inline void set_bit(unsigned long nr
, volatile unsigned long *addr
)
59 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
60 unsigned short bit
= nr
& SZLONG_MASK
;
63 if (kernel_uses_llsc
&& R10000_LLSC_WAR
) {
66 "1: " __LL
"%0, %1 # set_bit \n"
71 : "=&r" (temp
), "=m" (*m
)
72 : "ir" (1UL << bit
), "m" (*m
));
73 #ifdef CONFIG_CPU_MIPSR2
74 } else if (kernel_uses_llsc
&& __builtin_constant_p(bit
)) {
77 " " __LL
"%0, %1 # set_bit \n"
78 " " __INS
"%0, %3, %2, 1 \n"
80 : "=&r" (temp
), "+m" (*m
)
81 : "ir" (bit
), "r" (~0));
82 } while (unlikely(!temp
));
83 #endif /* CONFIG_CPU_MIPSR2 */
84 } else if (kernel_uses_llsc
) {
88 " " __LL
"%0, %1 # set_bit \n"
92 : "=&r" (temp
), "+m" (*m
)
94 } while (unlikely(!temp
));
96 volatile unsigned long *a
= addr
;
100 a
+= nr
>> SZLONG_LOG
;
102 raw_local_irq_save(flags
);
104 raw_local_irq_restore(flags
);
109 * clear_bit - Clears a bit in memory
111 * @addr: Address to start counting from
113 * clear_bit() is atomic and may not be reordered. However, it does
114 * not contain a memory barrier, so if it is used for locking purposes,
115 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
116 * in order to ensure changes are visible on other processors.
118 static inline void clear_bit(unsigned long nr
, volatile unsigned long *addr
)
120 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
121 unsigned short bit
= nr
& SZLONG_MASK
;
124 if (kernel_uses_llsc
&& R10000_LLSC_WAR
) {
125 __asm__
__volatile__(
127 "1: " __LL
"%0, %1 # clear_bit \n"
132 : "=&r" (temp
), "+m" (*m
)
133 : "ir" (~(1UL << bit
)));
134 #ifdef CONFIG_CPU_MIPSR2
135 } else if (kernel_uses_llsc
&& __builtin_constant_p(bit
)) {
137 __asm__
__volatile__(
138 " " __LL
"%0, %1 # clear_bit \n"
139 " " __INS
"%0, $0, %2, 1 \n"
141 : "=&r" (temp
), "+m" (*m
)
143 } while (unlikely(!temp
));
144 #endif /* CONFIG_CPU_MIPSR2 */
145 } else if (kernel_uses_llsc
) {
147 __asm__
__volatile__(
149 " " __LL
"%0, %1 # clear_bit \n"
153 : "=&r" (temp
), "+m" (*m
)
154 : "ir" (~(1UL << bit
)));
155 } while (unlikely(!temp
));
157 volatile unsigned long *a
= addr
;
161 a
+= nr
>> SZLONG_LOG
;
163 raw_local_irq_save(flags
);
165 raw_local_irq_restore(flags
);
170 * clear_bit_unlock - Clears a bit in memory
172 * @addr: Address to start counting from
174 * clear_bit() is atomic and implies release semantics before the memory
175 * operation. It can be used for an unlock.
177 static inline void clear_bit_unlock(unsigned long nr
, volatile unsigned long *addr
)
179 smp_mb__before_clear_bit();
184 * change_bit - Toggle a bit in memory
186 * @addr: Address to start counting from
188 * change_bit() is atomic and may not be reordered.
189 * Note that @nr may be almost arbitrarily large; this function is not
190 * restricted to acting on a single-word quantity.
192 static inline void change_bit(unsigned long nr
, volatile unsigned long *addr
)
194 unsigned short bit
= nr
& SZLONG_MASK
;
196 if (kernel_uses_llsc
&& R10000_LLSC_WAR
) {
197 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
200 __asm__
__volatile__(
202 "1: " __LL
"%0, %1 # change_bit \n"
207 : "=&r" (temp
), "+m" (*m
)
208 : "ir" (1UL << bit
));
209 } else if (kernel_uses_llsc
) {
210 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
214 __asm__
__volatile__(
216 " " __LL
"%0, %1 # change_bit \n"
220 : "=&r" (temp
), "+m" (*m
)
221 : "ir" (1UL << bit
));
222 } while (unlikely(!temp
));
224 volatile unsigned long *a
= addr
;
228 a
+= nr
>> SZLONG_LOG
;
230 raw_local_irq_save(flags
);
232 raw_local_irq_restore(flags
);
237 * test_and_set_bit - Set a bit and return its old value
239 * @addr: Address to count from
241 * This operation is atomic and cannot be reordered.
242 * It also implies a memory barrier.
244 static inline int test_and_set_bit(unsigned long nr
,
245 volatile unsigned long *addr
)
247 unsigned short bit
= nr
& SZLONG_MASK
;
250 smp_mb__before_llsc();
252 if (kernel_uses_llsc
&& R10000_LLSC_WAR
) {
253 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
256 __asm__
__volatile__(
258 "1: " __LL
"%0, %1 # test_and_set_bit \n"
264 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
267 } else if (kernel_uses_llsc
) {
268 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
272 __asm__
__volatile__(
274 " " __LL
"%0, %1 # test_and_set_bit \n"
278 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
281 } while (unlikely(!res
));
283 res
= temp
& (1UL << bit
);
285 volatile unsigned long *a
= addr
;
289 a
+= nr
>> SZLONG_LOG
;
291 raw_local_irq_save(flags
);
294 raw_local_irq_restore(flags
);
303 * test_and_set_bit_lock - Set a bit and return its old value
305 * @addr: Address to count from
307 * This operation is atomic and implies acquire ordering semantics
308 * after the memory operation.
310 static inline int test_and_set_bit_lock(unsigned long nr
,
311 volatile unsigned long *addr
)
313 unsigned short bit
= nr
& SZLONG_MASK
;
316 if (kernel_uses_llsc
&& R10000_LLSC_WAR
) {
317 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
320 __asm__
__volatile__(
322 "1: " __LL
"%0, %1 # test_and_set_bit \n"
328 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
331 } else if (kernel_uses_llsc
) {
332 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
336 __asm__
__volatile__(
338 " " __LL
"%0, %1 # test_and_set_bit \n"
342 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
345 } while (unlikely(!res
));
347 res
= temp
& (1UL << bit
);
349 volatile unsigned long *a
= addr
;
353 a
+= nr
>> SZLONG_LOG
;
355 raw_local_irq_save(flags
);
358 raw_local_irq_restore(flags
);
366 * test_and_clear_bit - Clear a bit and return its old value
368 * @addr: Address to count from
370 * This operation is atomic and cannot be reordered.
371 * It also implies a memory barrier.
373 static inline int test_and_clear_bit(unsigned long nr
,
374 volatile unsigned long *addr
)
376 unsigned short bit
= nr
& SZLONG_MASK
;
379 smp_mb__before_llsc();
381 if (kernel_uses_llsc
&& R10000_LLSC_WAR
) {
382 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
385 __asm__
__volatile__(
387 "1: " __LL
"%0, %1 # test_and_clear_bit \n"
394 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
397 #ifdef CONFIG_CPU_MIPSR2
398 } else if (kernel_uses_llsc
&& __builtin_constant_p(nr
)) {
399 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
403 __asm__
__volatile__(
404 " " __LL
"%0, %1 # test_and_clear_bit \n"
405 " " __EXT
"%2, %0, %3, 1 \n"
406 " " __INS
"%0, $0, %3, 1 \n"
408 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
411 } while (unlikely(!temp
));
413 } else if (kernel_uses_llsc
) {
414 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
418 __asm__
__volatile__(
420 " " __LL
"%0, %1 # test_and_clear_bit \n"
425 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
428 } while (unlikely(!res
));
430 res
= temp
& (1UL << bit
);
432 volatile unsigned long *a
= addr
;
436 a
+= nr
>> SZLONG_LOG
;
438 raw_local_irq_save(flags
);
441 raw_local_irq_restore(flags
);
450 * test_and_change_bit - Change a bit and return its old value
452 * @addr: Address to count from
454 * This operation is atomic and cannot be reordered.
455 * It also implies a memory barrier.
457 static inline int test_and_change_bit(unsigned long nr
,
458 volatile unsigned long *addr
)
460 unsigned short bit
= nr
& SZLONG_MASK
;
463 smp_mb__before_llsc();
465 if (kernel_uses_llsc
&& R10000_LLSC_WAR
) {
466 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
469 __asm__
__volatile__(
471 "1: " __LL
"%0, %1 # test_and_change_bit \n"
477 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
480 } else if (kernel_uses_llsc
) {
481 unsigned long *m
= ((unsigned long *) addr
) + (nr
>> SZLONG_LOG
);
485 __asm__
__volatile__(
487 " " __LL
"%0, %1 # test_and_change_bit \n"
489 " " __SC
"\t%2, %1 \n"
491 : "=&r" (temp
), "+m" (*m
), "=&r" (res
)
494 } while (unlikely(!res
));
496 res
= temp
& (1UL << bit
);
498 volatile unsigned long *a
= addr
;
502 a
+= nr
>> SZLONG_LOG
;
504 raw_local_irq_save(flags
);
507 raw_local_irq_restore(flags
);
515 #include <asm-generic/bitops/non-atomic.h>
518 * __clear_bit_unlock - Clears a bit in memory
520 * @addr: Address to start counting from
522 * __clear_bit() is non-atomic and implies release semantics before the memory
523 * operation. It can be used for an unlock if no other CPUs can concurrently
524 * modify other bits in the word.
526 static inline void __clear_bit_unlock(unsigned long nr
, volatile unsigned long *addr
)
529 __clear_bit(nr
, addr
);
533 * Return the bit position (0..63) of the most significant 1 bit in a word
534 * Returns -1 if no 1 bit exists
536 static inline unsigned long __fls(unsigned long word
)
540 if (BITS_PER_LONG
== 32 &&
541 __builtin_constant_p(cpu_has_clo_clz
) && cpu_has_clo_clz
) {
553 if (BITS_PER_LONG
== 64 &&
554 __builtin_constant_p(cpu_has_mips64
) && cpu_has_mips64
) {
566 num
= BITS_PER_LONG
- 1;
568 #if BITS_PER_LONG == 64
569 if (!(word
& (~0ul << 32))) {
574 if (!(word
& (~0ul << (BITS_PER_LONG
-16)))) {
578 if (!(word
& (~0ul << (BITS_PER_LONG
-8)))) {
582 if (!(word
& (~0ul << (BITS_PER_LONG
-4)))) {
586 if (!(word
& (~0ul << (BITS_PER_LONG
-2)))) {
590 if (!(word
& (~0ul << (BITS_PER_LONG
-1))))
596 * __ffs - find first bit in word.
597 * @word: The word to search
599 * Returns 0..SZLONG-1
600 * Undefined if no bit exists, so code should check against 0 first.
602 static inline unsigned long __ffs(unsigned long word
)
604 return __fls(word
& -word
);
608 * fls - find last bit set.
609 * @word: The word to search
611 * This is defined the same way as ffs.
612 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
614 static inline int fls(int x
)
618 if (__builtin_constant_p(cpu_has_clo_clz
) && cpu_has_clo_clz
) {
619 __asm__("clz %0, %1" : "=r" (x
) : "r" (x
));
627 if (!(x
& 0xffff0000u
)) {
631 if (!(x
& 0xff000000u
)) {
635 if (!(x
& 0xf0000000u
)) {
639 if (!(x
& 0xc0000000u
)) {
643 if (!(x
& 0x80000000u
)) {
650 #include <asm-generic/bitops/fls64.h>
653 * ffs - find first bit set.
654 * @word: The word to search
656 * This is defined the same way as
657 * the libc and compiler builtin ffs routines, therefore
658 * differs in spirit from the above ffz (man ffs).
660 static inline int ffs(int word
)
665 return fls(word
& -word
);
668 #include <asm-generic/bitops/ffz.h>
669 #include <asm-generic/bitops/find.h>
673 #include <asm-generic/bitops/sched.h>
675 #include <asm/arch_hweight.h>
676 #include <asm-generic/bitops/const_hweight.h>
678 #include <asm-generic/bitops/le.h>
679 #include <asm-generic/bitops/ext2-atomic.h>
681 #endif /* __KERNEL__ */
683 #endif /* _ASM_BITOPS_H */