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) 2000 Silicon Graphics, Inc.
12 #include <linux/types.h>
13 #include <asm/byteorder.h> /* sigh ... */
17 #include <asm/sgidefs.h>
18 #include <asm/system.h>
19 #include <linux/config.h>
22 * clear_bit() doesn't provide any barrier for the compiler.
24 #define smp_mb__before_clear_bit() barrier()
25 #define smp_mb__after_clear_bit() barrier()
28 * Only disable interrupt for kernel mode stuff to keep usermode stuff
29 * that dares to use kernel include files alive.
31 #define __bi_flags unsigned long flags
32 #define __bi_cli() __cli()
33 #define __bi_save_flags(x) __save_flags(x)
34 #define __bi_save_and_cli(x) __save_and_cli(x)
35 #define __bi_restore_flags(x) __restore_flags(x)
39 #define __bi_save_flags(x)
40 #define __bi_save_and_cli(x)
41 #define __bi_restore_flags(x)
42 #endif /* __KERNEL__ */
44 #ifdef CONFIG_CPU_HAS_LLSC
46 #include <asm/mipsregs.h>
49 * These functions for MIPS ISA > 1 are interrupt and SMP proof and
54 * set_bit - Atomically set a bit in memory
56 * @addr: the address to start counting from
58 * This function is atomic and may not be reordered. See __set_bit()
59 * if you do not require the atomic guarantees.
60 * Note that @nr may be almost arbitrarily large; this function is not
61 * restricted to acting on a single-word quantity.
63 static __inline__ void
64 set_bit(int nr, volatile void *addr)
66 unsigned long *m = ((unsigned long *) addr) + (nr >> 5);
70 "1:\tll\t%0, %1\t\t# set_bit\n\t"
74 : "=&r" (temp), "=m" (*m)
75 : "ir" (1UL << (nr & 0x1f)), "m" (*m));
79 * __set_bit - Set a bit in memory
81 * @addr: the address to start counting from
83 * Unlike set_bit(), this function is non-atomic and may be reordered.
84 * If it's called on the same region of memory simultaneously, the effect
85 * may be that only one operation succeeds.
87 static __inline__ void __set_bit(int nr, volatile void * addr)
89 unsigned long * m = ((unsigned long *) addr) + (nr >> 5);
91 *m |= 1UL << (nr & 31);
93 #define PLATFORM__SET_BIT
96 * clear_bit - Clears a bit in memory
98 * @addr: Address to start counting from
100 * clear_bit() is atomic and may not be reordered. However, it does
101 * not contain a memory barrier, so if it is used for locking purposes,
102 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
103 * in order to ensure changes are visible on other processors.
105 static __inline__ void
106 clear_bit(int nr, volatile void *addr)
108 unsigned long *m = ((unsigned long *) addr) + (nr >> 5);
111 __asm__ __volatile__(
112 "1:\tll\t%0, %1\t\t# clear_bit\n\t"
116 : "=&r" (temp), "=m" (*m)
117 : "ir" (~(1UL << (nr & 0x1f))), "m" (*m));
121 * change_bit - Toggle a bit in memory
123 * @addr: Address to start counting from
125 * change_bit() is atomic and may not be reordered.
126 * Note that @nr may be almost arbitrarily large; this function is not
127 * restricted to acting on a single-word quantity.
129 static __inline__ void
130 change_bit(int nr, volatile void *addr)
132 unsigned long *m = ((unsigned long *) addr) + (nr >> 5);
135 __asm__ __volatile__(
136 "1:\tll\t%0, %1\t\t# change_bit\n\t"
140 : "=&r" (temp), "=m" (*m)
141 : "ir" (1UL << (nr & 0x1f)), "m" (*m));
145 * __change_bit - Toggle a bit in memory
146 * @nr: the bit to set
147 * @addr: the address to start counting from
149 * Unlike change_bit(), this function is non-atomic and may be reordered.
150 * If it's called on the same region of memory simultaneously, the effect
151 * may be that only one operation succeeds.
153 static __inline__ void __change_bit(int nr, volatile void * addr)
155 unsigned long * m = ((unsigned long *) addr) + (nr >> 5);
157 *m ^= 1UL << (nr & 31);
161 * test_and_set_bit - Set a bit and return its old value
163 * @addr: Address to count from
165 * This operation is atomic and cannot be reordered.
166 * It also implies a memory barrier.
168 static __inline__ int
169 test_and_set_bit(int nr, volatile void *addr)
171 unsigned long *m = ((unsigned long *) addr) + (nr >> 5);
172 unsigned long temp, res;
174 __asm__ __volatile__(
175 ".set\tnoreorder\t\t# test_and_set_bit\n"
180 " and\t%2, %0, %3\n\t"
182 : "=&r" (temp), "=m" (*m), "=&r" (res)
183 : "r" (1UL << (nr & 0x1f)), "m" (*m)
190 * __test_and_set_bit - Set a bit and return its old value
192 * @addr: Address to count from
194 * This operation is non-atomic and can be reordered.
195 * If two examples of this operation race, one can appear to succeed
196 * but actually fail. You must protect multiple accesses with a lock.
198 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
201 volatile int *a = addr;
204 mask = 1 << (nr & 0x1f);
205 retval = (mask & *a) != 0;
212 * test_and_clear_bit - Clear a bit and return its old value
214 * @addr: Address to count from
216 * This operation is atomic and cannot be reordered.
217 * It also implies a memory barrier.
219 static __inline__ int
220 test_and_clear_bit(int nr, volatile void *addr)
222 unsigned long *m = ((unsigned long *) addr) + (nr >> 5);
223 unsigned long temp, res;
225 __asm__ __volatile__(
226 ".set\tnoreorder\t\t# test_and_clear_bit\n"
232 " and\t%2, %0, %3\n\t"
234 : "=&r" (temp), "=m" (*m), "=&r" (res)
235 : "r" (1UL << (nr & 0x1f)), "m" (*m)
242 * __test_and_clear_bit - Clear a bit and return its old value
244 * @addr: Address to count from
246 * This operation is non-atomic and can be reordered.
247 * If two examples of this operation race, one can appear to succeed
248 * but actually fail. You must protect multiple accesses with a lock.
250 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
253 volatile int *a = addr;
256 mask = 1 << (nr & 0x1f);
257 retval = (mask & *a) != 0;
264 * test_and_change_bit - Change a bit and return its new value
266 * @addr: Address to count from
268 * This operation is atomic and cannot be reordered.
269 * It also implies a memory barrier.
271 static __inline__ int
272 test_and_change_bit(int nr, volatile void *addr)
274 unsigned long *m = ((unsigned long *) addr) + (nr >> 5);
275 unsigned long temp, res;
277 __asm__ __volatile__(
278 ".set\tnoreorder\t\t# test_and_change_bit\n"
280 "xor\t%2, %0, %3\n\t"
283 " and\t%2, %0, %3\n\t"
285 : "=&r" (temp), "=m" (*m), "=&r" (res)
286 : "r" (1UL << (nr & 0x1f)), "m" (*m)
293 * __test_and_change_bit - Change a bit and return its old value
295 * @addr: Address to count from
297 * This operation is non-atomic and can be reordered.
298 * If two examples of this operation race, one can appear to succeed
299 * but actually fail. You must protect multiple accesses with a lock.
301 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
304 volatile int *a = addr;
307 mask = 1 << (nr & 0x1f);
308 retval = (mask & *a) != 0;
317 * set_bit - Atomically set a bit in memory
318 * @nr: the bit to set
319 * @addr: the address to start counting from
321 * This function is atomic and may not be reordered. See __set_bit()
322 * if you do not require the atomic guarantees.
323 * Note that @nr may be almost arbitrarily large; this function is not
324 * restricted to acting on a single-word quantity.
326 static __inline__ void set_bit(int nr, volatile void * addr)
329 volatile int *a = addr;
333 mask = 1 << (nr & 0x1f);
334 __bi_save_and_cli(flags);
336 __bi_restore_flags(flags);
340 * __set_bit - Set a bit in memory
341 * @nr: the bit to set
342 * @addr: the address to start counting from
344 * Unlike set_bit(), this function is non-atomic and may be reordered.
345 * If it's called on the same region of memory simultaneously, the effect
346 * may be that only one operation succeeds.
348 static __inline__ void __set_bit(int nr, volatile void * addr)
351 volatile int *a = addr;
354 mask = 1 << (nr & 0x1f);
359 * clear_bit - Clears a bit in memory
361 * @addr: Address to start counting from
363 * clear_bit() is atomic and may not be reordered. However, it does
364 * not contain a memory barrier, so if it is used for locking purposes,
365 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
366 * in order to ensure changes are visible on other processors.
368 static __inline__ void clear_bit(int nr, volatile void * addr)
371 volatile int *a = addr;
375 mask = 1 << (nr & 0x1f);
376 __bi_save_and_cli(flags);
378 __bi_restore_flags(flags);
382 * change_bit - Toggle a bit in memory
384 * @addr: Address to start counting from
386 * change_bit() is atomic and may not be reordered.
387 * Note that @nr may be almost arbitrarily large; this function is not
388 * restricted to acting on a single-word quantity.
390 static __inline__ void change_bit(int nr, volatile void * addr)
393 volatile int *a = addr;
397 mask = 1 << (nr & 0x1f);
398 __bi_save_and_cli(flags);
400 __bi_restore_flags(flags);
404 * __change_bit - Toggle a bit in memory
405 * @nr: the bit to set
406 * @addr: the address to start counting from
408 * Unlike change_bit(), this function is non-atomic and may be reordered.
409 * If it's called on the same region of memory simultaneously, the effect
410 * may be that only one operation succeeds.
412 static __inline__ void __change_bit(int nr, volatile void * addr)
414 unsigned long * m = ((unsigned long *) addr) + (nr >> 5);
416 *m ^= 1UL << (nr & 31);
420 * test_and_set_bit - Set a bit and return its old value
422 * @addr: Address to count from
424 * This operation is atomic and cannot be reordered.
425 * It also implies a memory barrier.
427 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
430 volatile int *a = addr;
434 mask = 1 << (nr & 0x1f);
435 __bi_save_and_cli(flags);
436 retval = (mask & *a) != 0;
438 __bi_restore_flags(flags);
444 * __test_and_set_bit - Set a bit and return its old value
446 * @addr: Address to count from
448 * This operation is non-atomic and can be reordered.
449 * If two examples of this operation race, one can appear to succeed
450 * but actually fail. You must protect multiple accesses with a lock.
452 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
455 volatile int *a = addr;
458 mask = 1 << (nr & 0x1f);
459 retval = (mask & *a) != 0;
466 * test_and_clear_bit - Clear a bit and return its old value
468 * @addr: Address to count from
470 * This operation is atomic and cannot be reordered.
471 * It also implies a memory barrier.
473 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
476 volatile int *a = addr;
480 mask = 1 << (nr & 0x1f);
481 __bi_save_and_cli(flags);
482 retval = (mask & *a) != 0;
484 __bi_restore_flags(flags);
490 * __test_and_clear_bit - Clear a bit and return its old value
492 * @addr: Address to count from
494 * This operation is non-atomic and can be reordered.
495 * If two examples of this operation race, one can appear to succeed
496 * but actually fail. You must protect multiple accesses with a lock.
498 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
501 volatile int *a = addr;
504 mask = 1 << (nr & 0x1f);
505 retval = (mask & *a) != 0;
512 * test_and_change_bit - Change a bit and return its new value
514 * @addr: Address to count from
516 * This operation is atomic and cannot be reordered.
517 * It also implies a memory barrier.
519 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
522 volatile int *a = addr;
526 mask = 1 << (nr & 0x1f);
527 __bi_save_and_cli(flags);
528 retval = (mask & *a) != 0;
530 __bi_restore_flags(flags);
536 * __test_and_change_bit - Change a bit and return its old value
538 * @addr: Address to count from
540 * This operation is non-atomic and can be reordered.
541 * If two examples of this operation race, one can appear to succeed
542 * but actually fail. You must protect multiple accesses with a lock.
544 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
547 volatile int *a = addr;
550 mask = 1 << (nr & 0x1f);
551 retval = (mask & *a) != 0;
559 #undef __bi_save_flags
560 #undef __bi_restore_flags
565 * test_bit - Determine whether a bit is set
566 * @nr: bit number to test
567 * @addr: Address to start counting from
569 static __inline__ int test_bit(int nr, volatile void *addr)
571 return ((1UL << (nr & 31)) & (((const unsigned int *) addr)[nr >> 5])) != 0;
576 /* Little endian versions. */
579 * find_first_zero_bit - find the first zero bit in a memory region
580 * @addr: The address to start the search at
581 * @size: The maximum size to search
583 * Returns the bit-number of the first zero bit, not the number of the byte
586 static __inline__ int find_first_zero_bit (void *addr, unsigned size)
594 __asm__ (".set\tnoreorder\n\t"
596 "1:\tsubu\t$1,%6,%0\n\t"
600 #if (_MIPS_ISA == _MIPS_ISA_MIPS2 ) || (_MIPS_ISA == _MIPS_ISA_MIPS3 ) || \
601 (_MIPS_ISA == _MIPS_ISA_MIPS4 ) || (_MIPS_ISA == _MIPS_ISA_MIPS5 ) || \
602 (_MIPS_ISA == _MIPS_ISA_MIPS32) || (_MIPS_ISA == _MIPS_ISA_MIPS64)
612 #error "Fix this for big endian"
613 #endif /* __MIPSEB__ */
615 "1:\tand\t%2,$1,%1\n\t"
623 : "=r" (res), "=r" (dummy), "=r" (addr)
624 : "0" ((signed int) 0), "1" ((unsigned int) 0xffffffff),
625 "2" (addr), "r" (size)
632 * find_next_zero_bit - find the first zero bit in a memory region
633 * @addr: The address to base the search on
634 * @offset: The bitnumber to start searching at
635 * @size: The maximum size to search
637 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
639 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
640 int set = 0, bit = offset & 31, res;
645 * Look for zero in first byte
648 #error "Fix this for big endian byte order"
650 __asm__(".set\tnoreorder\n\t"
652 "1:\tand\t$1,%4,%1\n\t"
660 : "=r" (set), "=r" (dummy)
661 : "0" (0), "1" (1 << bit), "r" (*p)
663 if (set < (32 - bit))
669 * No zero yet, search remaining full bytes for a zero
671 res = find_first_zero_bit(p, size - 32 * (p - (unsigned int *) addr));
672 return offset + set + res;
675 #endif /* !(__MIPSEB__) */
678 * ffz - find first zero in word.
679 * @word: The word to search
681 * Undefined if no zero exists, so code should check against ~0UL first.
683 static __inline__ unsigned long ffz(unsigned long word)
686 unsigned int mask = 1;
689 ".set\tnoreorder\n\t"
692 "1:\tand\t$1,%2,%1\n\t"
700 : "=&r" (__res), "=r" (mask)
701 : "r" (word), "1" (mask)
710 * hweightN - returns the hamming weight of a N-bit word
711 * @x: the word to weigh
713 * The Hamming Weight of a number is the total number of bits set in it.
716 #define hweight32(x) generic_hweight32(x)
717 #define hweight16(x) generic_hweight16(x)
718 #define hweight8(x) generic_hweight8(x)
720 #endif /* __KERNEL__ */
724 * find_next_zero_bit - find the first zero bit in a memory region
725 * @addr: The address to base the search on
726 * @offset: The bitnumber to start searching at
727 * @size: The maximum size to search
729 static __inline__ int find_next_zero_bit(void *addr, int size, int offset)
731 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
732 unsigned long result = offset & ~31UL;
741 tmp |= ~0UL >> (32-offset);
749 while (size & ~31UL) {
762 return result + ffz(tmp);
765 /* Linus sez that gcc can optimize the following correctly, we'll see if this
766 * holds on the Sparc as it does for the ALPHA.
769 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
771 * find_first_zero_bit - find the first zero bit in a memory region
772 * @addr: The address to start the search at
773 * @size: The maximum size to search
775 * Returns the bit-number of the first zero bit, not the number of the byte
778 static int find_first_zero_bit (void *addr, unsigned size);
781 #define find_first_zero_bit(addr, size) \
782 find_next_zero_bit((addr), (size), 0)
784 #endif /* (__MIPSEB__) */
786 /* Now for the ext2 filesystem bit operations and helper routines. */
789 static __inline__ int ext2_set_bit(int nr, void * addr)
791 int mask, retval, flags;
792 unsigned char *ADDR = (unsigned char *) addr;
795 mask = 1 << (nr & 0x07);
797 retval = (mask & *ADDR) != 0;
799 restore_flags(flags);
803 static __inline__ int ext2_clear_bit(int nr, void * addr)
805 int mask, retval, flags;
806 unsigned char *ADDR = (unsigned char *) addr;
809 mask = 1 << (nr & 0x07);
811 retval = (mask & *ADDR) != 0;
813 restore_flags(flags);
817 static __inline__ int ext2_test_bit(int nr, const void * addr)
820 const unsigned char *ADDR = (const unsigned char *) addr;
823 mask = 1 << (nr & 0x07);
824 return ((mask & *ADDR) != 0);
827 #define ext2_find_first_zero_bit(addr, size) \
828 ext2_find_next_zero_bit((addr), (size), 0)
830 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
832 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
833 unsigned long result = offset & ~31UL;
841 /* We hold the little endian value in tmp, but then the
842 * shift is illegal. So we could keep a big endian value
845 * tmp = __swab32(*(p++));
846 * tmp |= ~0UL >> (32-offset);
848 * but this would decrease preformance, so we change the
852 tmp |= __swab32(~0UL >> (32-offset));
860 while(size & ~31UL) {
871 /* tmp is little endian, so we would have to swab the shift,
872 * see above. But then we have to swab tmp below for ffz, so
873 * we might as well do this here.
875 return result + ffz(__swab32(tmp) | (~0UL << size));
877 return result + ffz(__swab32(tmp));
879 #else /* !(__MIPSEB__) */
881 /* Native ext2 byte ordering, just collapse using defines. */
882 #define ext2_set_bit(nr, addr) test_and_set_bit((nr), (addr))
883 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr), (addr))
884 #define ext2_test_bit(nr, addr) test_bit((nr), (addr))
885 #define ext2_find_first_zero_bit(addr, size) find_first_zero_bit((addr), (size))
886 #define ext2_find_next_zero_bit(addr, size, offset) \
887 find_next_zero_bit((addr), (size), (offset))
889 #endif /* !(__MIPSEB__) */
892 * Bitmap functions for the minix filesystem.
893 * FIXME: These assume that Minix uses the native byte/bitorder.
894 * This limits the Minix filesystem's value for data exchange very much.
896 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
897 #define minix_set_bit(nr,addr) set_bit(nr,addr)
898 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
899 #define minix_test_bit(nr,addr) test_bit(nr,addr)
900 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
902 #endif /* _ASM_BITOPS_H */