2 * Copyright IBM Corp. 1999,2013
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6 * The description below was taken in large parts from the powerpc
8 * Within a word, bits are numbered LSB first. Lot's of places make
9 * this assumption by directly testing bits with (val & (1<<nr)).
10 * This can cause confusion for large (> 1 word) bitmaps on a
11 * big-endian system because, unlike little endian, the number of each
12 * bit depends on the word size.
14 * The bitop functions are defined to work on unsigned longs, so for an
15 * s390x system the bits end up numbered:
16 * |63..............0|127............64|191...........128|255...........192|
18 * |31.....0|63....32|95....64|127...96|159..128|191..160|223..192|255..224|
20 * There are a few little-endian macros used mostly for filesystem
21 * bitmaps, these work on similar bit arrays layouts, but
23 * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
25 * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit
26 * number field needs to be reversed compared to the big-endian bit
27 * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).
29 * We also have special functions which work with an MSB0 encoding:
30 * on an s390x system the bits are numbered:
31 * |0..............63|64............127|128...........191|192...........255|
33 * |0.....31|32....63|64....95|96...127|128..159|160..191|192..223|224..255|
35 * The main difference is that bit 0-63 (64b) or 0-31 (32b) in the bit
36 * number field needs to be reversed compared to the LSB0 encoded bit
37 * fields. This can be achieved by XOR with 0x3f (64b) or 0x1f (32b).
41 #ifndef _S390_BITOPS_H
42 #define _S390_BITOPS_H
44 #ifndef _LINUX_BITOPS_H
45 #error only <linux/bitops.h> can be included directly
48 #include <linux/typecheck.h>
49 #include <linux/compiler.h>
50 #include <asm/barrier.h>
52 #define __BITOPS_NO_BARRIER "\n"
56 #define __BITOPS_OR "or"
57 #define __BITOPS_AND "nr"
58 #define __BITOPS_XOR "xr"
59 #define __BITOPS_BARRIER "\n"
61 #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \
63 unsigned long __old, __new; \
65 typecheck(unsigned long *, (__addr)); \
69 __op_string " %1,%3\n" \
72 : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
78 #else /* CONFIG_64BIT */
80 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
82 #define __BITOPS_OR "laog"
83 #define __BITOPS_AND "lang"
84 #define __BITOPS_XOR "laxg"
85 #define __BITOPS_BARRIER "bcr 14,0\n"
87 #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \
89 unsigned long __old; \
91 typecheck(unsigned long *, (__addr)); \
94 __op_string " %0,%2,%1\n" \
96 : "=d" (__old), "+Q" (*(__addr)) \
102 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
104 #define __BITOPS_OR "ogr"
105 #define __BITOPS_AND "ngr"
106 #define __BITOPS_XOR "xgr"
107 #define __BITOPS_BARRIER "\n"
109 #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \
111 unsigned long __old, __new; \
113 typecheck(unsigned long *, (__addr)); \
117 __op_string " %1,%3\n" \
120 : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
126 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
128 #endif /* CONFIG_64BIT */
130 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
132 static inline unsigned long *
133 __bitops_word(unsigned long nr
, volatile unsigned long *ptr
)
137 addr
= (unsigned long)ptr
+ ((nr
^ (nr
& (BITS_PER_LONG
- 1))) >> 3);
138 return (unsigned long *)addr
;
141 static inline unsigned char *
142 __bitops_byte(unsigned long nr
, volatile unsigned long *ptr
)
144 return ((unsigned char *)ptr
) + ((nr
^ (BITS_PER_LONG
- 8)) >> 3);
147 static inline void set_bit(unsigned long nr
, volatile unsigned long *ptr
)
149 unsigned long *addr
= __bitops_word(nr
, ptr
);
152 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
153 if (__builtin_constant_p(nr
)) {
154 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
159 : "i" (1 << (nr
& 7))
164 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
165 __BITOPS_LOOP(addr
, mask
, __BITOPS_OR
, __BITOPS_NO_BARRIER
);
168 static inline void clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
170 unsigned long *addr
= __bitops_word(nr
, ptr
);
173 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
174 if (__builtin_constant_p(nr
)) {
175 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
180 : "i" (~(1 << (nr
& 7)))
185 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
186 __BITOPS_LOOP(addr
, mask
, __BITOPS_AND
, __BITOPS_NO_BARRIER
);
189 static inline void change_bit(unsigned long nr
, volatile unsigned long *ptr
)
191 unsigned long *addr
= __bitops_word(nr
, ptr
);
194 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
195 if (__builtin_constant_p(nr
)) {
196 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
201 : "i" (1 << (nr
& 7))
206 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
207 __BITOPS_LOOP(addr
, mask
, __BITOPS_XOR
, __BITOPS_NO_BARRIER
);
211 test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
213 unsigned long *addr
= __bitops_word(nr
, ptr
);
214 unsigned long old
, mask
;
216 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
217 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_OR
, __BITOPS_BARRIER
);
218 return (old
& mask
) != 0;
222 test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
224 unsigned long *addr
= __bitops_word(nr
, ptr
);
225 unsigned long old
, mask
;
227 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
228 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_AND
, __BITOPS_BARRIER
);
229 return (old
& ~mask
) != 0;
233 test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
235 unsigned long *addr
= __bitops_word(nr
, ptr
);
236 unsigned long old
, mask
;
238 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
239 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_XOR
, __BITOPS_BARRIER
);
240 return (old
& mask
) != 0;
243 static inline void __set_bit(unsigned long nr
, volatile unsigned long *ptr
)
245 unsigned char *addr
= __bitops_byte(nr
, ptr
);
247 *addr
|= 1 << (nr
& 7);
251 __clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
253 unsigned char *addr
= __bitops_byte(nr
, ptr
);
255 *addr
&= ~(1 << (nr
& 7));
258 static inline void __change_bit(unsigned long nr
, volatile unsigned long *ptr
)
260 unsigned char *addr
= __bitops_byte(nr
, ptr
);
262 *addr
^= 1 << (nr
& 7);
266 __test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
268 unsigned char *addr
= __bitops_byte(nr
, ptr
);
272 *addr
|= 1 << (nr
& 7);
273 return (ch
>> (nr
& 7)) & 1;
277 __test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
279 unsigned char *addr
= __bitops_byte(nr
, ptr
);
283 *addr
&= ~(1 << (nr
& 7));
284 return (ch
>> (nr
& 7)) & 1;
288 __test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
290 unsigned char *addr
= __bitops_byte(nr
, ptr
);
294 *addr
^= 1 << (nr
& 7);
295 return (ch
>> (nr
& 7)) & 1;
298 static inline int test_bit(unsigned long nr
, const volatile unsigned long *ptr
)
300 const volatile unsigned char *addr
;
302 addr
= ((const volatile unsigned char *)ptr
);
303 addr
+= (nr
^ (BITS_PER_LONG
- 8)) >> 3;
304 return (*addr
>> (nr
& 7)) & 1;
308 * Functions which use MSB0 bit numbering.
309 * On an s390x system the bits are numbered:
310 * |0..............63|64............127|128...........191|192...........255|
312 * |0.....31|32....63|64....95|96...127|128..159|160..191|192..223|224..255|
314 unsigned long find_first_bit_inv(const unsigned long *addr
, unsigned long size
);
315 unsigned long find_next_bit_inv(const unsigned long *addr
, unsigned long size
,
316 unsigned long offset
);
318 static inline void set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
320 return set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
323 static inline void clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
325 return clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
328 static inline void __set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
330 return __set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
333 static inline void __clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
335 return __clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
338 static inline int test_bit_inv(unsigned long nr
,
339 const volatile unsigned long *ptr
)
341 return test_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
344 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
347 * __flogr - find leftmost one
348 * @word - The word to search
350 * Returns the bit number of the most significant bit set,
351 * where the most significant bit has bit number 0.
352 * If no bit is set this function returns 64.
354 static inline unsigned char __flogr(unsigned long word
)
356 if (__builtin_constant_p(word
)) {
357 unsigned long bit
= 0;
361 if (!(word
& 0xffffffff00000000UL
)) {
365 if (!(word
& 0xffff000000000000UL
)) {
369 if (!(word
& 0xff00000000000000UL
)) {
373 if (!(word
& 0xf000000000000000UL
)) {
377 if (!(word
& 0xc000000000000000UL
)) {
381 if (!(word
& 0x8000000000000000UL
)) {
387 register unsigned long bit
asm("4") = word
;
388 register unsigned long out
asm("5");
391 " flogr %[bit],%[bit]\n"
392 : [bit
] "+d" (bit
), [out
] "=d" (out
) : : "cc");
398 * __ffs - find first bit in word.
399 * @word: The word to search
401 * Undefined if no bit exists, so code should check against 0 first.
403 static inline unsigned long __ffs(unsigned long word
)
405 return __flogr(-word
& word
) ^ (BITS_PER_LONG
- 1);
409 * ffs - find first bit set
410 * @word: the word to search
412 * This is defined the same way as the libc and
413 * compiler builtin ffs routines (man ffs).
415 static inline int ffs(int word
)
417 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
418 unsigned int val
= (unsigned int)word
;
420 return (1 + (__flogr(-val
& val
) ^ (BITS_PER_LONG
- 1))) & mask
;
424 * __fls - find last (most-significant) set bit in a long word
425 * @word: the word to search
427 * Undefined if no set bit exists, so code should check against 0 first.
429 static inline unsigned long __fls(unsigned long word
)
431 return __flogr(word
) ^ (BITS_PER_LONG
- 1);
435 * fls64 - find last set bit in a 64-bit word
436 * @word: the word to search
438 * This is defined in a similar way as the libc and compiler builtin
439 * ffsll, but returns the position of the most significant set bit.
441 * fls64(value) returns 0 if value is 0 or the position of the last
442 * set bit if value is nonzero. The last (most significant) bit is
445 static inline int fls64(unsigned long word
)
447 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
449 return (1 + (__flogr(word
) ^ (BITS_PER_LONG
- 1))) & mask
;
453 * fls - find last (most-significant) bit set
454 * @word: the word to search
456 * This is defined the same way as ffs.
457 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
459 static inline int fls(int word
)
461 return fls64((unsigned int)word
);
464 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
466 #include <asm-generic/bitops/__ffs.h>
467 #include <asm-generic/bitops/ffs.h>
468 #include <asm-generic/bitops/__fls.h>
469 #include <asm-generic/bitops/fls.h>
470 #include <asm-generic/bitops/fls64.h>
472 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
474 #include <asm-generic/bitops/ffz.h>
475 #include <asm-generic/bitops/find.h>
476 #include <asm-generic/bitops/hweight.h>
477 #include <asm-generic/bitops/lock.h>
478 #include <asm-generic/bitops/sched.h>
479 #include <asm-generic/bitops/le.h>
480 #include <asm-generic/bitops/ext2-atomic-setbit.h>
482 #endif /* _S390_BITOPS_H */