3 * Copyright IBM Corp. 1999
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6 * Derived from "include/asm-i386/bitops.h"
7 * Copyright (C) 1992, Linus Torvalds
11 #ifndef _S390_BITOPS_H
12 #define _S390_BITOPS_H
14 #ifndef _LINUX_BITOPS_H
15 #error only <linux/bitops.h> can be included directly
18 #include <linux/compiler.h>
21 * 32 bit bitops format:
22 * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
23 * bit 32 is the LSB of *(addr+4). That combined with the
24 * big endian byte order on S390 give the following bit
26 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
27 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
28 * after that follows the next long with bit numbers
29 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
30 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
31 * The reason for this bit ordering is the fact that
32 * in the architecture independent code bits operations
33 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
34 * with operation of the form "set_bit(bitnr, flags)".
36 * 64 bit bitops format:
37 * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
38 * bit 64 is the LSB of *(addr+8). That combined with the
39 * big endian byte order on S390 give the following bit
41 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
42 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
43 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
44 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
45 * after that follows the next long with bit numbers
46 * 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
47 * 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
48 * 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
49 * 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
50 * The reason for this bit ordering is the fact that
51 * in the architecture independent code bits operations
52 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
53 * with operation of the form "set_bit(bitnr, flags)".
56 /* bitmap tables from arch/s390/kernel/bitmap.c */
57 extern const char _oi_bitmap[];
58 extern const char _ni_bitmap[];
59 extern const char _zb_findmap[];
60 extern const char _sb_findmap[];
64 #define __BITOPS_ALIGN 3
65 #define __BITOPS_WORDSIZE 32
66 #define __BITOPS_OR "or"
67 #define __BITOPS_AND "nr"
68 #define __BITOPS_XOR "xr"
70 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
74 __op_string " %1,%3\n" \
77 : "=&d" (__old), "=&d" (__new), \
78 "=Q" (*(unsigned long *) __addr) \
79 : "d" (__val), "Q" (*(unsigned long *) __addr) \
82 #else /* CONFIG_64BIT */
84 #define __BITOPS_ALIGN 7
85 #define __BITOPS_WORDSIZE 64
86 #define __BITOPS_OR "ogr"
87 #define __BITOPS_AND "ngr"
88 #define __BITOPS_XOR "xgr"
90 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
94 __op_string " %1,%3\n" \
97 : "=&d" (__old), "=&d" (__new), \
98 "=Q" (*(unsigned long *) __addr) \
99 : "d" (__val), "Q" (*(unsigned long *) __addr) \
102 #endif /* CONFIG_64BIT */
104 #define __BITOPS_WORDS(bits) (((bits)+__BITOPS_WORDSIZE-1)/__BITOPS_WORDSIZE)
105 #define __BITOPS_BARRIER() asm volatile("" : : : "memory")
109 * SMP safe set_bit routine based on compare and swap (CS)
111 static inline void set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
113 unsigned long addr, old, new, mask;
115 addr = (unsigned long) ptr;
116 /* calculate address for CS */
117 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
119 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
120 /* Do the atomic update. */
121 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
125 * SMP safe clear_bit routine based on compare and swap (CS)
127 static inline void clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
129 unsigned long addr, old, new, mask;
131 addr = (unsigned long) ptr;
132 /* calculate address for CS */
133 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
135 mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
136 /* Do the atomic update. */
137 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
141 * SMP safe change_bit routine based on compare and swap (CS)
143 static inline void change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
145 unsigned long addr, old, new, mask;
147 addr = (unsigned long) ptr;
148 /* calculate address for CS */
149 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
151 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
152 /* Do the atomic update. */
153 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
157 * SMP safe test_and_set_bit routine based on compare and swap (CS)
160 test_and_set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
162 unsigned long addr, old, new, mask;
164 addr = (unsigned long) ptr;
165 /* calculate address for CS */
166 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
167 /* make OR/test mask */
168 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
169 /* Do the atomic update. */
170 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
172 return (old & mask) != 0;
176 * SMP safe test_and_clear_bit routine based on compare and swap (CS)
179 test_and_clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
181 unsigned long addr, old, new, mask;
183 addr = (unsigned long) ptr;
184 /* calculate address for CS */
185 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
186 /* make AND/test mask */
187 mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
188 /* Do the atomic update. */
189 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
191 return (old ^ new) != 0;
195 * SMP safe test_and_change_bit routine based on compare and swap (CS)
198 test_and_change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
200 unsigned long addr, old, new, mask;
202 addr = (unsigned long) ptr;
203 /* calculate address for CS */
204 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
205 /* make XOR/test mask */
206 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
207 /* Do the atomic update. */
208 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
210 return (old & mask) != 0;
212 #endif /* CONFIG_SMP */
215 * fast, non-SMP set_bit routine
217 static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
221 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
224 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
228 __constant_set_bit(const unsigned long nr, volatile unsigned long *ptr)
232 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
233 *(unsigned char *) addr |= 1 << (nr & 7);
236 #define set_bit_simple(nr,addr) \
237 (__builtin_constant_p((nr)) ? \
238 __constant_set_bit((nr),(addr)) : \
239 __set_bit((nr),(addr)) )
242 * fast, non-SMP clear_bit routine
245 __clear_bit(unsigned long nr, volatile unsigned long *ptr)
249 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
252 : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7]) : "cc" );
256 __constant_clear_bit(const unsigned long nr, volatile unsigned long *ptr)
260 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
261 *(unsigned char *) addr &= ~(1 << (nr & 7));
264 #define clear_bit_simple(nr,addr) \
265 (__builtin_constant_p((nr)) ? \
266 __constant_clear_bit((nr),(addr)) : \
267 __clear_bit((nr),(addr)) )
270 * fast, non-SMP change_bit routine
272 static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
276 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
279 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
283 __constant_change_bit(const unsigned long nr, volatile unsigned long *ptr)
287 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
288 *(unsigned char *) addr ^= 1 << (nr & 7);
291 #define change_bit_simple(nr,addr) \
292 (__builtin_constant_p((nr)) ? \
293 __constant_change_bit((nr),(addr)) : \
294 __change_bit((nr),(addr)) )
297 * fast, non-SMP test_and_set_bit routine
300 test_and_set_bit_simple(unsigned long nr, volatile unsigned long *ptr)
305 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
306 ch = *(unsigned char *) addr;
309 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
311 return (ch >> (nr & 7)) & 1;
313 #define __test_and_set_bit(X,Y) test_and_set_bit_simple(X,Y)
316 * fast, non-SMP test_and_clear_bit routine
319 test_and_clear_bit_simple(unsigned long nr, volatile unsigned long *ptr)
324 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
325 ch = *(unsigned char *) addr;
328 : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7])
330 return (ch >> (nr & 7)) & 1;
332 #define __test_and_clear_bit(X,Y) test_and_clear_bit_simple(X,Y)
335 * fast, non-SMP test_and_change_bit routine
338 test_and_change_bit_simple(unsigned long nr, volatile unsigned long *ptr)
343 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
344 ch = *(unsigned char *) addr;
347 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
349 return (ch >> (nr & 7)) & 1;
351 #define __test_and_change_bit(X,Y) test_and_change_bit_simple(X,Y)
354 #define set_bit set_bit_cs
355 #define clear_bit clear_bit_cs
356 #define change_bit change_bit_cs
357 #define test_and_set_bit test_and_set_bit_cs
358 #define test_and_clear_bit test_and_clear_bit_cs
359 #define test_and_change_bit test_and_change_bit_cs
361 #define set_bit set_bit_simple
362 #define clear_bit clear_bit_simple
363 #define change_bit change_bit_simple
364 #define test_and_set_bit test_and_set_bit_simple
365 #define test_and_clear_bit test_and_clear_bit_simple
366 #define test_and_change_bit test_and_change_bit_simple
371 * This routine doesn't need to be atomic.
374 static inline int __test_bit(unsigned long nr, const volatile unsigned long *ptr)
379 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
380 ch = *(volatile unsigned char *) addr;
381 return (ch >> (nr & 7)) & 1;
385 __constant_test_bit(unsigned long nr, const volatile unsigned long *addr) {
386 return (((volatile char *) addr)
387 [(nr^(__BITOPS_WORDSIZE-8))>>3] & (1<<(nr&7))) != 0;
390 #define test_bit(nr,addr) \
391 (__builtin_constant_p((nr)) ? \
392 __constant_test_bit((nr),(addr)) : \
393 __test_bit((nr),(addr)) )
396 * Optimized find bit helper functions.
400 * __ffz_word_loop - find byte offset of first long != -1UL
401 * @addr: pointer to array of unsigned long
402 * @size: size of the array in bits
404 static inline unsigned long __ffz_word_loop(const unsigned long *addr,
407 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
408 unsigned long bytes = 0;
424 "0: cg %2,0(%0,%3)\n"
430 : "+&a" (bytes), "+&d" (size)
431 : "d" (-1UL), "a" (addr), "m" (*(addrtype *) addr)
437 * __ffs_word_loop - find byte offset of first long != 0UL
438 * @addr: pointer to array of unsigned long
439 * @size: size of the array in bits
441 static inline unsigned long __ffs_word_loop(const unsigned long *addr,
444 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
445 unsigned long bytes = 0;
461 "0: cg %2,0(%0,%3)\n"
467 : "+&a" (bytes), "+&a" (size)
468 : "d" (0UL), "a" (addr), "m" (*(addrtype *) addr)
474 * __ffz_word - add number of the first unset bit
475 * @nr: base value the bit number is added to
476 * @word: the word that is searched for unset bits
478 static inline unsigned long __ffz_word(unsigned long nr, unsigned long word)
481 if ((word & 0xffffffff) == 0xffffffff) {
486 if ((word & 0xffff) == 0xffff) {
490 if ((word & 0xff) == 0xff) {
494 return nr + _zb_findmap[(unsigned char) word];
498 * __ffs_word - add number of the first set bit
499 * @nr: base value the bit number is added to
500 * @word: the word that is searched for set bits
502 static inline unsigned long __ffs_word(unsigned long nr, unsigned long word)
505 if ((word & 0xffffffff) == 0) {
510 if ((word & 0xffff) == 0) {
514 if ((word & 0xff) == 0) {
518 return nr + _sb_findmap[(unsigned char) word];
523 * __load_ulong_be - load big endian unsigned long
524 * @p: pointer to array of unsigned long
525 * @offset: byte offset of source value in the array
527 static inline unsigned long __load_ulong_be(const unsigned long *p,
528 unsigned long offset)
530 p = (unsigned long *)((unsigned long) p + offset);
535 * __load_ulong_le - load little endian unsigned long
536 * @p: pointer to array of unsigned long
537 * @offset: byte offset of source value in the array
539 static inline unsigned long __load_ulong_le(const unsigned long *p,
540 unsigned long offset)
544 p = (unsigned long *)((unsigned long) p + offset);
548 " icm %0,2,%O1+1(%R1)\n"
549 " icm %0,4,%O1+2(%R1)\n"
550 " icm %0,8,%O1+3(%R1)"
551 : "=&d" (word) : "Q" (*p) : "cc");
555 : "=d" (word) : "m" (*p) );
561 * The various find bit functions.
565 * ffz - find first zero in word.
566 * @word: The word to search
568 * Undefined if no zero exists, so code should check against ~0UL first.
570 static inline unsigned long ffz(unsigned long word)
572 return __ffz_word(0, word);
576 * __ffs - find first bit in word.
577 * @word: The word to search
579 * Undefined if no bit exists, so code should check against 0 first.
581 static inline unsigned long __ffs (unsigned long word)
583 return __ffs_word(0, word);
587 * ffs - find first bit set
588 * @x: the word to search
590 * This is defined the same way as
591 * the libc and compiler builtin ffs routines, therefore
592 * differs in spirit from the above ffz (man ffs).
594 static inline int ffs(int x)
598 return __ffs_word(1, x);
602 * find_first_zero_bit - find the first zero bit in a memory region
603 * @addr: The address to start the search at
604 * @size: The maximum size to search
606 * Returns the bit-number of the first zero bit, not the number of the byte
609 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
612 unsigned long bytes, bits;
616 bytes = __ffz_word_loop(addr, size);
617 bits = __ffz_word(bytes*8, __load_ulong_be(addr, bytes));
618 return (bits < size) ? bits : size;
620 #define find_first_zero_bit find_first_zero_bit
623 * find_first_bit - find the first set bit in a memory region
624 * @addr: The address to start the search at
625 * @size: The maximum size to search
627 * Returns the bit-number of the first set bit, not the number of the byte
630 static inline unsigned long find_first_bit(const unsigned long * addr,
633 unsigned long bytes, bits;
637 bytes = __ffs_word_loop(addr, size);
638 bits = __ffs_word(bytes*8, __load_ulong_be(addr, bytes));
639 return (bits < size) ? bits : size;
641 #define find_first_bit find_first_bit
644 * find_next_zero_bit - find the first zero bit in a memory region
645 * @addr: The address to base the search on
646 * @offset: The bitnumber to start searching at
647 * @size: The maximum size to search
649 static inline int find_next_zero_bit (const unsigned long * addr,
651 unsigned long offset)
653 const unsigned long *p;
654 unsigned long bit, set;
658 bit = offset & (__BITOPS_WORDSIZE - 1);
661 p = addr + offset / __BITOPS_WORDSIZE;
664 * __ffz_word returns __BITOPS_WORDSIZE
665 * if no zero bit is present in the word.
667 set = __ffz_word(bit, *p >> bit);
669 return size + offset;
670 if (set < __BITOPS_WORDSIZE)
672 offset += __BITOPS_WORDSIZE;
673 size -= __BITOPS_WORDSIZE;
676 return offset + find_first_zero_bit(p, size);
678 #define find_next_zero_bit find_next_zero_bit
681 * find_next_bit - find the first set bit in a memory region
682 * @addr: The address to base the search on
683 * @offset: The bitnumber to start searching at
684 * @size: The maximum size to search
686 static inline int find_next_bit (const unsigned long * addr,
688 unsigned long offset)
690 const unsigned long *p;
691 unsigned long bit, set;
695 bit = offset & (__BITOPS_WORDSIZE - 1);
698 p = addr + offset / __BITOPS_WORDSIZE;
701 * __ffs_word returns __BITOPS_WORDSIZE
702 * if no one bit is present in the word.
704 set = __ffs_word(0, *p & (~0UL << bit));
706 return size + offset;
707 if (set < __BITOPS_WORDSIZE)
709 offset += __BITOPS_WORDSIZE;
710 size -= __BITOPS_WORDSIZE;
713 return offset + find_first_bit(p, size);
715 #define find_next_bit find_next_bit
718 * Every architecture must define this function. It's the fastest
719 * way of searching a 140-bit bitmap where the first 100 bits are
720 * unlikely to be set. It's guaranteed that at least one of the 140
723 static inline int sched_find_first_bit(unsigned long *b)
725 return find_first_bit(b, 140);
728 #include <asm-generic/bitops/fls.h>
729 #include <asm-generic/bitops/__fls.h>
730 #include <asm-generic/bitops/fls64.h>
732 #include <asm-generic/bitops/hweight.h>
733 #include <asm-generic/bitops/lock.h>
736 * ATTENTION: intel byte ordering convention for ext2 and minix !!
737 * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
738 * bit 32 is the LSB of (addr+4).
739 * That combined with the little endian byte order of Intel gives the
740 * following bit order in memory:
741 * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
742 * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
745 static inline int find_first_zero_bit_le(void *vaddr, unsigned int size)
747 unsigned long bytes, bits;
751 bytes = __ffz_word_loop(vaddr, size);
752 bits = __ffz_word(bytes*8, __load_ulong_le(vaddr, bytes));
753 return (bits < size) ? bits : size;
755 #define find_first_zero_bit_le find_first_zero_bit_le
757 static inline int find_next_zero_bit_le(void *vaddr, unsigned long size,
758 unsigned long offset)
760 unsigned long *addr = vaddr, *p;
761 unsigned long bit, set;
765 bit = offset & (__BITOPS_WORDSIZE - 1);
768 p = addr + offset / __BITOPS_WORDSIZE;
771 * s390 version of ffz returns __BITOPS_WORDSIZE
772 * if no zero bit is present in the word.
774 set = __ffz_word(bit, __load_ulong_le(p, 0) >> bit);
776 return size + offset;
777 if (set < __BITOPS_WORDSIZE)
779 offset += __BITOPS_WORDSIZE;
780 size -= __BITOPS_WORDSIZE;
783 return offset + find_first_zero_bit_le(p, size);
785 #define find_next_zero_bit_le find_next_zero_bit_le
787 static inline unsigned long find_first_bit_le(void *vaddr, unsigned long size)
789 unsigned long bytes, bits;
793 bytes = __ffs_word_loop(vaddr, size);
794 bits = __ffs_word(bytes*8, __load_ulong_le(vaddr, bytes));
795 return (bits < size) ? bits : size;
797 #define find_first_bit_le find_first_bit_le
799 static inline int find_next_bit_le(void *vaddr, unsigned long size,
800 unsigned long offset)
802 unsigned long *addr = vaddr, *p;
803 unsigned long bit, set;
807 bit = offset & (__BITOPS_WORDSIZE - 1);
810 p = addr + offset / __BITOPS_WORDSIZE;
813 * s390 version of ffz returns __BITOPS_WORDSIZE
814 * if no zero bit is present in the word.
816 set = __ffs_word(0, __load_ulong_le(p, 0) & (~0UL << bit));
818 return size + offset;
819 if (set < __BITOPS_WORDSIZE)
821 offset += __BITOPS_WORDSIZE;
822 size -= __BITOPS_WORDSIZE;
825 return offset + find_first_bit_le(p, size);
827 #define find_next_bit_le find_next_bit_le
829 #include <asm-generic/bitops/le.h>
831 #include <asm-generic/bitops/ext2-atomic-setbit.h>
833 #endif /* _S390_BITOPS_H */