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, 1999, 2000 Ralf Baechle (ralf@gnu.org)
7 * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
12 #include <linux/config.h>
13 #include <linux/compiler.h>
14 #include <linux/types.h>
15 #include <asm/byteorder.h> /* sigh ... */
16 #include <asm/cpu-features.h>
18 #if (_MIPS_SZLONG == 32)
20 #define SZLONG_MASK 31UL
23 #define cpu_to_lelongp(x) cpu_to_le32p((__u32 *) (x))
24 #elif (_MIPS_SZLONG == 64)
26 #define SZLONG_MASK 63UL
29 #define cpu_to_lelongp(x) cpu_to_le64p((__u64 *) (x))
34 #include <asm/interrupt.h>
35 #include <asm/sgidefs.h>
39 * clear_bit() doesn't provide any barrier for the compiler.
41 #define smp_mb__before_clear_bit() smp_mb()
42 #define smp_mb__after_clear_bit() smp_mb()
45 * Only disable interrupt for kernel mode stuff to keep usermode stuff
46 * that dares to use kernel include files alive.
49 #define __bi_flags unsigned long flags
50 #define __bi_local_irq_save(x) local_irq_save(x)
51 #define __bi_local_irq_restore(x) local_irq_restore(x)
54 #define __bi_local_irq_save(x)
55 #define __bi_local_irq_restore(x)
56 #endif /* __KERNEL__ */
59 * set_bit - Atomically set a bit in memory
61 * @addr: the address to start counting from
63 * This function is atomic and may not be reordered. See __set_bit()
64 * if you do not require the atomic guarantees.
65 * Note that @nr may be almost arbitrarily large; this function is not
66 * restricted to acting on a single-word quantity.
68 static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
70 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
73 if (cpu_has_llsc && R10000_LLSC_WAR) {
75 "1: " __LL "%0, %1 # set_bit \n"
79 : "=&r" (temp), "=m" (*m)
80 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
81 } else if (cpu_has_llsc) {
83 "1: " __LL "%0, %1 # set_bit \n"
87 : "=&r" (temp), "=m" (*m)
88 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
90 volatile unsigned long *a = addr;
94 a += nr >> SZLONG_LOG;
95 mask = 1UL << (nr & SZLONG_MASK);
96 __bi_local_irq_save(flags);
98 __bi_local_irq_restore(flags);
103 * __set_bit - Set a bit in memory
104 * @nr: the bit to set
105 * @addr: the address to start counting from
107 * Unlike set_bit(), this function is non-atomic and may be reordered.
108 * If it's called on the same region of memory simultaneously, the effect
109 * may be that only one operation succeeds.
111 static inline void __set_bit(unsigned long nr, volatile unsigned long * addr)
113 unsigned long * m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
115 *m |= 1UL << (nr & SZLONG_MASK);
119 * clear_bit - Clears a bit in memory
121 * @addr: Address to start counting from
123 * clear_bit() is atomic and may not be reordered. However, it does
124 * not contain a memory barrier, so if it is used for locking purposes,
125 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
126 * in order to ensure changes are visible on other processors.
128 static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
130 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
133 if (cpu_has_llsc && R10000_LLSC_WAR) {
134 __asm__ __volatile__(
135 "1: " __LL "%0, %1 # clear_bit \n"
139 : "=&r" (temp), "=m" (*m)
140 : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
141 } else if (cpu_has_llsc) {
142 __asm__ __volatile__(
143 "1: " __LL "%0, %1 # clear_bit \n"
147 : "=&r" (temp), "=m" (*m)
148 : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
150 volatile unsigned long *a = addr;
154 a += nr >> SZLONG_LOG;
155 mask = 1UL << (nr & SZLONG_MASK);
156 __bi_local_irq_save(flags);
158 __bi_local_irq_restore(flags);
163 * __clear_bit - Clears a bit in memory
165 * @addr: Address to start counting from
167 * Unlike clear_bit(), this function is non-atomic and may be reordered.
168 * If it's called on the same region of memory simultaneously, the effect
169 * may be that only one operation succeeds.
171 static inline void __clear_bit(unsigned long nr, volatile unsigned long * addr)
173 unsigned long * m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
175 *m &= ~(1UL << (nr & SZLONG_MASK));
179 * change_bit - Toggle a bit in memory
181 * @addr: Address to start counting from
183 * change_bit() is atomic and may not be reordered.
184 * Note that @nr may be almost arbitrarily large; this function is not
185 * restricted to acting on a single-word quantity.
187 static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
189 if (cpu_has_llsc && R10000_LLSC_WAR) {
190 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
193 __asm__ __volatile__(
194 "1: " __LL "%0, %1 # change_bit \n"
198 : "=&r" (temp), "=m" (*m)
199 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
200 } else if (cpu_has_llsc) {
201 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
204 __asm__ __volatile__(
205 "1: " __LL "%0, %1 # change_bit \n"
209 : "=&r" (temp), "=m" (*m)
210 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
212 volatile unsigned long *a = addr;
216 a += nr >> SZLONG_LOG;
217 mask = 1UL << (nr & SZLONG_MASK);
218 __bi_local_irq_save(flags);
220 __bi_local_irq_restore(flags);
225 * __change_bit - Toggle a bit in memory
226 * @nr: the bit to change
227 * @addr: the address to start counting from
229 * Unlike change_bit(), this function is non-atomic and may be reordered.
230 * If it's called on the same region of memory simultaneously, the effect
231 * may be that only one operation succeeds.
233 static inline void __change_bit(unsigned long nr, volatile unsigned long * addr)
235 unsigned long * m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
237 *m ^= 1UL << (nr & SZLONG_MASK);
241 * test_and_set_bit - Set a bit and return its old value
243 * @addr: Address to count from
245 * This operation is atomic and cannot be reordered.
246 * It also implies a memory barrier.
248 static inline int test_and_set_bit(unsigned long nr,
249 volatile unsigned long *addr)
251 if (cpu_has_llsc && R10000_LLSC_WAR) {
252 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
253 unsigned long temp, res;
255 __asm__ __volatile__(
256 "1: " __LL "%0, %1 # test_and_set_bit \n"
264 : "=&r" (temp), "=m" (*m), "=&r" (res)
265 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
269 } else if (cpu_has_llsc) {
270 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
271 unsigned long temp, res;
273 __asm__ __volatile__(
274 " .set noreorder # test_and_set_bit \n"
275 "1: " __LL "%0, %1 \n"
284 : "=&r" (temp), "=m" (*m), "=&r" (res)
285 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
290 volatile unsigned long *a = addr;
295 a += nr >> SZLONG_LOG;
296 mask = 1UL << (nr & SZLONG_MASK);
297 __bi_local_irq_save(flags);
298 retval = (mask & *a) != 0;
300 __bi_local_irq_restore(flags);
307 * __test_and_set_bit - Set a bit and return its old value
309 * @addr: Address to count from
311 * This operation is non-atomic and can be reordered.
312 * If two examples of this operation race, one can appear to succeed
313 * but actually fail. You must protect multiple accesses with a lock.
315 static inline int __test_and_set_bit(unsigned long nr,
316 volatile unsigned long *addr)
318 volatile unsigned long *a = addr;
322 a += nr >> SZLONG_LOG;
323 mask = 1UL << (nr & SZLONG_MASK);
324 retval = (mask & *a) != 0;
331 * test_and_clear_bit - Clear a bit and return its old value
333 * @addr: Address to count from
335 * This operation is atomic and cannot be reordered.
336 * It also implies a memory barrier.
338 static inline int test_and_clear_bit(unsigned long nr,
339 volatile unsigned long *addr)
341 if (cpu_has_llsc && R10000_LLSC_WAR) {
342 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
343 unsigned long temp, res;
345 __asm__ __volatile__(
346 "1: " __LL "%0, %1 # test_and_clear_bit \n"
355 : "=&r" (temp), "=m" (*m), "=&r" (res)
356 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
360 } else if (cpu_has_llsc) {
361 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
362 unsigned long temp, res;
364 __asm__ __volatile__(
365 " .set noreorder # test_and_clear_bit \n"
366 "1: " __LL "%0, %1 \n"
376 : "=&r" (temp), "=m" (*m), "=&r" (res)
377 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
382 volatile unsigned long *a = addr;
387 a += nr >> SZLONG_LOG;
388 mask = 1UL << (nr & SZLONG_MASK);
389 __bi_local_irq_save(flags);
390 retval = (mask & *a) != 0;
392 __bi_local_irq_restore(flags);
399 * __test_and_clear_bit - Clear a bit and return its old value
401 * @addr: Address to count from
403 * This operation is non-atomic and can be reordered.
404 * If two examples of this operation race, one can appear to succeed
405 * but actually fail. You must protect multiple accesses with a lock.
407 static inline int __test_and_clear_bit(unsigned long nr,
408 volatile unsigned long * addr)
410 volatile unsigned long *a = addr;
414 a += (nr >> SZLONG_LOG);
415 mask = 1UL << (nr & SZLONG_MASK);
416 retval = ((mask & *a) != 0);
423 * test_and_change_bit - Change a bit and return its old value
425 * @addr: Address to count from
427 * This operation is atomic and cannot be reordered.
428 * It also implies a memory barrier.
430 static inline int test_and_change_bit(unsigned long nr,
431 volatile unsigned long *addr)
433 if (cpu_has_llsc && R10000_LLSC_WAR) {
434 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
435 unsigned long temp, res;
437 __asm__ __volatile__(
438 "1: " __LL " %0, %1 # test_and_change_bit \n"
446 : "=&r" (temp), "=m" (*m), "=&r" (res)
447 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
451 } else if (cpu_has_llsc) {
452 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
453 unsigned long temp, res;
455 __asm__ __volatile__(
456 " .set noreorder # test_and_change_bit \n"
457 "1: " __LL " %0, %1 \n"
459 " "__SC "\t%2, %1 \n"
466 : "=&r" (temp), "=m" (*m), "=&r" (res)
467 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
472 volatile unsigned long *a = addr;
473 unsigned long mask, retval;
476 a += nr >> SZLONG_LOG;
477 mask = 1UL << (nr & SZLONG_MASK);
478 __bi_local_irq_save(flags);
479 retval = (mask & *a) != 0;
481 __bi_local_irq_restore(flags);
488 * __test_and_change_bit - Change a bit and return its old value
490 * @addr: Address to count from
492 * This operation is non-atomic and can be reordered.
493 * If two examples of this operation race, one can appear to succeed
494 * but actually fail. You must protect multiple accesses with a lock.
496 static inline int __test_and_change_bit(unsigned long nr,
497 volatile unsigned long *addr)
499 volatile unsigned long *a = addr;
503 a += (nr >> SZLONG_LOG);
504 mask = 1UL << (nr & SZLONG_MASK);
505 retval = ((mask & *a) != 0);
512 #undef __bi_local_irq_save
513 #undef __bi_local_irq_restore
516 * test_bit - Determine whether a bit is set
517 * @nr: bit number to test
518 * @addr: Address to start counting from
520 static inline int test_bit(unsigned long nr, const volatile unsigned long *addr)
522 return 1UL & (addr[nr >> SZLONG_LOG] >> (nr & SZLONG_MASK));
526 * ffz - find first zero in word.
527 * @word: The word to search
529 * Undefined if no zero exists, so code should check against ~0UL first.
531 static inline unsigned long ffz(unsigned long word)
537 s = 16; if (word << 16 != 0) s = 0; b += s; word >>= s;
538 s = 8; if (word << 24 != 0) s = 0; b += s; word >>= s;
539 s = 4; if (word << 28 != 0) s = 0; b += s; word >>= s;
540 s = 2; if (word << 30 != 0) s = 0; b += s; word >>= s;
541 s = 1; if (word << 31 != 0) s = 0; b += s;
544 s = 32; if (word << 32 != 0) s = 0; b += s; word >>= s;
545 s = 16; if (word << 48 != 0) s = 0; b += s; word >>= s;
546 s = 8; if (word << 56 != 0) s = 0; b += s; word >>= s;
547 s = 4; if (word << 60 != 0) s = 0; b += s; word >>= s;
548 s = 2; if (word << 62 != 0) s = 0; b += s; word >>= s;
549 s = 1; if (word << 63 != 0) s = 0; b += s;
556 * __ffs - find first bit in word.
557 * @word: The word to search
559 * Undefined if no bit exists, so code should check against 0 first.
561 static inline unsigned long __ffs(unsigned long word)
567 * fls: find last bit set.
570 #define fls(x) generic_fls(x)
573 * find_next_zero_bit - find the first zero bit in a memory region
574 * @addr: The address to base the search on
575 * @offset: The bitnumber to start searching at
576 * @size: The maximum size to search
578 static inline unsigned long find_next_zero_bit(const unsigned long *addr,
579 unsigned long size, unsigned long offset)
581 const unsigned long *p = addr + (offset >> SZLONG_LOG);
582 unsigned long result = offset & ~SZLONG_MASK;
588 offset &= SZLONG_MASK;
591 tmp |= ~0UL >> (_MIPS_SZLONG-offset);
592 if (size < _MIPS_SZLONG)
596 size -= _MIPS_SZLONG;
597 result += _MIPS_SZLONG;
599 while (size & ~SZLONG_MASK) {
602 result += _MIPS_SZLONG;
603 size -= _MIPS_SZLONG;
611 if (tmp == ~0UL) /* Are any bits zero? */
612 return result + size; /* Nope. */
614 return result + ffz(tmp);
617 #define find_first_zero_bit(addr, size) \
618 find_next_zero_bit((addr), (size), 0)
621 * find_next_bit - find the next set bit in a memory region
622 * @addr: The address to base the search on
623 * @offset: The bitnumber to start searching at
624 * @size: The maximum size to search
626 static inline unsigned long find_next_bit(const unsigned long *addr,
627 unsigned long size, unsigned long offset)
629 const unsigned long *p = addr + (offset >> SZLONG_LOG);
630 unsigned long result = offset & ~SZLONG_MASK;
636 offset &= SZLONG_MASK;
639 tmp &= ~0UL << offset;
640 if (size < _MIPS_SZLONG)
644 size -= _MIPS_SZLONG;
645 result += _MIPS_SZLONG;
647 while (size & ~SZLONG_MASK) {
650 result += _MIPS_SZLONG;
651 size -= _MIPS_SZLONG;
658 tmp &= ~0UL >> (_MIPS_SZLONG - size);
659 if (tmp == 0UL) /* Are any bits set? */
660 return result + size; /* Nope. */
662 return result + __ffs(tmp);
666 * find_first_bit - find the first set bit in a memory region
667 * @addr: The address to start the search at
668 * @size: The maximum size to search
670 * Returns the bit-number of the first set bit, not the number of the byte
673 #define find_first_bit(addr, size) \
674 find_next_bit((addr), (size), 0)
679 * Every architecture must define this function. It's the fastest
680 * way of searching a 140-bit bitmap where the first 100 bits are
681 * unlikely to be set. It's guaranteed that at least one of the 140
684 static inline int sched_find_first_bit(const unsigned long *b)
690 return __ffs(b[1]) + 32;
692 return __ffs(b[2]) + 64;
694 return __ffs(b[3]) + 96;
695 return __ffs(b[4]) + 128;
701 return __ffs(b[1]) + 64;
702 return __ffs(b[2]) + 128;
707 * ffs - find first bit set
708 * @x: the word to search
710 * This is defined the same way as
711 * the libc and compiler builtin ffs routines, therefore
712 * differs in spirit from the above ffz (man ffs).
715 #define ffs(x) generic_ffs(x)
718 * hweightN - returns the hamming weight of a N-bit word
719 * @x: the word to weigh
721 * The Hamming Weight of a number is the total number of bits set in it.
724 #define hweight64(x) generic_hweight64(x)
725 #define hweight32(x) generic_hweight32(x)
726 #define hweight16(x) generic_hweight16(x)
727 #define hweight8(x) generic_hweight8(x)
729 static inline int __test_and_set_le_bit(unsigned long nr, unsigned long *addr)
731 unsigned char *ADDR = (unsigned char *) addr;
735 mask = 1 << (nr & 0x07);
736 retval = (mask & *ADDR) != 0;
742 static inline int __test_and_clear_le_bit(unsigned long nr, unsigned long *addr)
744 unsigned char *ADDR = (unsigned char *) addr;
748 mask = 1 << (nr & 0x07);
749 retval = (mask & *ADDR) != 0;
755 static inline int test_le_bit(unsigned long nr, const unsigned long * addr)
757 const unsigned char *ADDR = (const unsigned char *) addr;
761 mask = 1 << (nr & 0x07);
763 return ((mask & *ADDR) != 0);
766 static inline unsigned long find_next_zero_le_bit(unsigned long *addr,
767 unsigned long size, unsigned long offset)
769 unsigned long *p = ((unsigned long *) addr) + (offset >> SZLONG_LOG);
770 unsigned long result = offset & ~SZLONG_MASK;
776 offset &= SZLONG_MASK;
778 tmp = cpu_to_lelongp(p++);
779 tmp |= ~0UL >> (_MIPS_SZLONG-offset); /* bug or feature ? */
780 if (size < _MIPS_SZLONG)
784 size -= _MIPS_SZLONG;
785 result += _MIPS_SZLONG;
787 while (size & ~SZLONG_MASK) {
788 if (~(tmp = cpu_to_lelongp(p++)))
790 result += _MIPS_SZLONG;
791 size -= _MIPS_SZLONG;
795 tmp = cpu_to_lelongp(p);
799 if (tmp == ~0UL) /* Are any bits zero? */
800 return result + size; /* Nope. */
803 return result + ffz(tmp);
806 #define find_first_zero_le_bit(addr, size) \
807 find_next_zero_le_bit((addr), (size), 0)
809 #define ext2_set_bit(nr,addr) \
810 __test_and_set_le_bit((nr),(unsigned long*)addr)
811 #define ext2_clear_bit(nr, addr) \
812 __test_and_clear_le_bit((nr),(unsigned long*)addr)
813 #define ext2_set_bit_atomic(lock, nr, addr) \
817 ret = ext2_set_bit((nr), (addr)); \
822 #define ext2_clear_bit_atomic(lock, nr, addr) \
826 ret = ext2_clear_bit((nr), (addr)); \
830 #define ext2_test_bit(nr, addr) test_le_bit((nr),(unsigned long*)addr)
831 #define ext2_find_first_zero_bit(addr, size) \
832 find_first_zero_le_bit((unsigned long*)addr, size)
833 #define ext2_find_next_zero_bit(addr, size, off) \
834 find_next_zero_le_bit((unsigned long*)addr, size, off)
837 * Bitmap functions for the minix filesystem.
839 * FIXME: These assume that Minix uses the native byte/bitorder.
840 * This limits the Minix filesystem's value for data exchange very much.
842 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
843 #define minix_set_bit(nr,addr) set_bit(nr,addr)
844 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
845 #define minix_test_bit(nr,addr) test_bit(nr,addr)
846 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
848 #endif /* __KERNEL__ */
850 #endif /* _ASM_BITOPS_H */