1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright IBM Corp. 1999,2013
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * The description below was taken in large parts from the powerpc
9 * Within a word, bits are numbered LSB first. Lot's of places make
10 * this assumption by directly testing bits with (val & (1<<nr)).
11 * This can cause confusion for large (> 1 word) bitmaps on a
12 * big-endian system because, unlike little endian, the number of each
13 * bit depends on the word size.
15 * The bitop functions are defined to work on unsigned longs, so the bits
17 * |63..............0|127............64|191...........128|255...........192|
19 * We also have special functions which work with an MSB0 encoding.
20 * The bits are numbered:
21 * |0..............63|64............127|128...........191|192...........255|
23 * The main difference is that bit 0-63 in the bit number field needs to be
24 * reversed compared to the LSB0 encoded bit fields. This can be achieved by
29 #ifndef _S390_BITOPS_H
30 #define _S390_BITOPS_H
32 #ifndef _LINUX_BITOPS_H
33 #error only <linux/bitops.h> can be included directly
36 #include <linux/typecheck.h>
37 #include <linux/compiler.h>
38 #include <asm/atomic_ops.h>
39 #include <asm/barrier.h>
41 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
43 static inline unsigned long *
44 __bitops_word(unsigned long nr
, volatile unsigned long *ptr
)
48 addr
= (unsigned long)ptr
+ ((nr
^ (nr
& (BITS_PER_LONG
- 1))) >> 3);
49 return (unsigned long *)addr
;
52 static inline unsigned char *
53 __bitops_byte(unsigned long nr
, volatile unsigned long *ptr
)
55 return ((unsigned char *)ptr
) + ((nr
^ (BITS_PER_LONG
- 8)) >> 3);
58 static inline void set_bit(unsigned long nr
, volatile unsigned long *ptr
)
60 unsigned long *addr
= __bitops_word(nr
, ptr
);
63 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
64 if (__builtin_constant_p(nr
)) {
65 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
75 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
76 __atomic64_or(mask
, (long *)addr
);
79 static inline void clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
81 unsigned long *addr
= __bitops_word(nr
, ptr
);
84 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
85 if (__builtin_constant_p(nr
)) {
86 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
91 : "i" (~(1 << (nr
& 7)))
96 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
97 __atomic64_and(mask
, (long *)addr
);
100 static inline void change_bit(unsigned long nr
, volatile unsigned long *ptr
)
102 unsigned long *addr
= __bitops_word(nr
, ptr
);
105 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
106 if (__builtin_constant_p(nr
)) {
107 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
112 : "i" (1 << (nr
& 7))
117 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
118 __atomic64_xor(mask
, (long *)addr
);
122 test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
124 unsigned long *addr
= __bitops_word(nr
, ptr
);
125 unsigned long old
, mask
;
127 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
128 old
= __atomic64_or_barrier(mask
, (long *)addr
);
129 return (old
& mask
) != 0;
133 test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
135 unsigned long *addr
= __bitops_word(nr
, ptr
);
136 unsigned long old
, mask
;
138 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
139 old
= __atomic64_and_barrier(mask
, (long *)addr
);
140 return (old
& ~mask
) != 0;
144 test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
146 unsigned long *addr
= __bitops_word(nr
, ptr
);
147 unsigned long old
, mask
;
149 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
150 old
= __atomic64_xor_barrier(mask
, (long *)addr
);
151 return (old
& mask
) != 0;
154 static inline void __set_bit(unsigned long nr
, volatile unsigned long *ptr
)
156 unsigned char *addr
= __bitops_byte(nr
, ptr
);
158 *addr
|= 1 << (nr
& 7);
162 __clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
164 unsigned char *addr
= __bitops_byte(nr
, ptr
);
166 *addr
&= ~(1 << (nr
& 7));
169 static inline void __change_bit(unsigned long nr
, volatile unsigned long *ptr
)
171 unsigned char *addr
= __bitops_byte(nr
, ptr
);
173 *addr
^= 1 << (nr
& 7);
177 __test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
179 unsigned char *addr
= __bitops_byte(nr
, ptr
);
183 *addr
|= 1 << (nr
& 7);
184 return (ch
>> (nr
& 7)) & 1;
188 __test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
190 unsigned char *addr
= __bitops_byte(nr
, ptr
);
194 *addr
&= ~(1 << (nr
& 7));
195 return (ch
>> (nr
& 7)) & 1;
199 __test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
201 unsigned char *addr
= __bitops_byte(nr
, ptr
);
205 *addr
^= 1 << (nr
& 7);
206 return (ch
>> (nr
& 7)) & 1;
209 static inline int test_bit(unsigned long nr
, const volatile unsigned long *ptr
)
211 const volatile unsigned char *addr
;
213 addr
= ((const volatile unsigned char *)ptr
);
214 addr
+= (nr
^ (BITS_PER_LONG
- 8)) >> 3;
215 return (*addr
>> (nr
& 7)) & 1;
218 static inline int test_and_set_bit_lock(unsigned long nr
,
219 volatile unsigned long *ptr
)
221 if (test_bit(nr
, ptr
))
223 return test_and_set_bit(nr
, ptr
);
226 static inline void clear_bit_unlock(unsigned long nr
,
227 volatile unsigned long *ptr
)
229 smp_mb__before_atomic();
233 static inline void __clear_bit_unlock(unsigned long nr
,
234 volatile unsigned long *ptr
)
237 __clear_bit(nr
, ptr
);
241 * Functions which use MSB0 bit numbering.
242 * The bits are numbered:
243 * |0..............63|64............127|128...........191|192...........255|
245 unsigned long find_first_bit_inv(const unsigned long *addr
, unsigned long size
);
246 unsigned long find_next_bit_inv(const unsigned long *addr
, unsigned long size
,
247 unsigned long offset
);
249 #define for_each_set_bit_inv(bit, addr, size) \
250 for ((bit) = find_first_bit_inv((addr), (size)); \
252 (bit) = find_next_bit_inv((addr), (size), (bit) + 1))
254 static inline void set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
256 return set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
259 static inline void clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
261 return clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
264 static inline int test_and_clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
266 return test_and_clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
269 static inline void __set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
271 return __set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
274 static inline void __clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
276 return __clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
279 static inline int test_bit_inv(unsigned long nr
,
280 const volatile unsigned long *ptr
)
282 return test_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
285 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
288 * __flogr - find leftmost one
289 * @word - The word to search
291 * Returns the bit number of the most significant bit set,
292 * where the most significant bit has bit number 0.
293 * If no bit is set this function returns 64.
295 static inline unsigned char __flogr(unsigned long word
)
297 if (__builtin_constant_p(word
)) {
298 unsigned long bit
= 0;
302 if (!(word
& 0xffffffff00000000UL
)) {
306 if (!(word
& 0xffff000000000000UL
)) {
310 if (!(word
& 0xff00000000000000UL
)) {
314 if (!(word
& 0xf000000000000000UL
)) {
318 if (!(word
& 0xc000000000000000UL
)) {
322 if (!(word
& 0x8000000000000000UL
)) {
328 register unsigned long bit
asm("4") = word
;
329 register unsigned long out
asm("5");
332 " flogr %[bit],%[bit]\n"
333 : [bit
] "+d" (bit
), [out
] "=d" (out
) : : "cc");
339 * __ffs - find first bit in word.
340 * @word: The word to search
342 * Undefined if no bit exists, so code should check against 0 first.
344 static inline unsigned long __ffs(unsigned long word
)
346 return __flogr(-word
& word
) ^ (BITS_PER_LONG
- 1);
350 * ffs - find first bit set
351 * @word: the word to search
353 * This is defined the same way as the libc and
354 * compiler builtin ffs routines (man ffs).
356 static inline int ffs(int word
)
358 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
359 unsigned int val
= (unsigned int)word
;
361 return (1 + (__flogr(-val
& val
) ^ (BITS_PER_LONG
- 1))) & mask
;
365 * __fls - find last (most-significant) set bit in a long word
366 * @word: the word to search
368 * Undefined if no set bit exists, so code should check against 0 first.
370 static inline unsigned long __fls(unsigned long word
)
372 return __flogr(word
) ^ (BITS_PER_LONG
- 1);
376 * fls64 - find last set bit in a 64-bit word
377 * @word: the word to search
379 * This is defined in a similar way as the libc and compiler builtin
380 * ffsll, but returns the position of the most significant set bit.
382 * fls64(value) returns 0 if value is 0 or the position of the last
383 * set bit if value is nonzero. The last (most significant) bit is
386 static inline int fls64(unsigned long word
)
388 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
390 return (1 + (__flogr(word
) ^ (BITS_PER_LONG
- 1))) & mask
;
394 * fls - find last (most-significant) bit set
395 * @word: the word to search
397 * This is defined the same way as ffs.
398 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
400 static inline int fls(unsigned int word
)
405 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
407 #include <asm-generic/bitops/__ffs.h>
408 #include <asm-generic/bitops/ffs.h>
409 #include <asm-generic/bitops/__fls.h>
410 #include <asm-generic/bitops/fls.h>
411 #include <asm-generic/bitops/fls64.h>
413 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
415 #include <asm-generic/bitops/ffz.h>
416 #include <asm-generic/bitops/find.h>
417 #include <asm-generic/bitops/hweight.h>
418 #include <asm-generic/bitops/sched.h>
419 #include <asm-generic/bitops/le.h>
420 #include <asm-generic/bitops/ext2-atomic-setbit.h>
422 #endif /* _S390_BITOPS_H */