]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - arch/s390/include/asm/bitops.h
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 the bits
16 * |63..............0|127............64|191...........128|255...........192|
18 * We also have special functions which work with an MSB0 encoding.
19 * The bits are numbered:
20 * |0..............63|64............127|128...........191|192...........255|
22 * The main difference is that bit 0-63 in the bit number field needs to be
23 * reversed compared to the LSB0 encoded bit fields. This can be achieved by
28 #ifndef _S390_BITOPS_H
29 #define _S390_BITOPS_H
31 #ifndef _LINUX_BITOPS_H
32 #error only <linux/bitops.h> can be included directly
35 #include <linux/typecheck.h>
36 #include <linux/compiler.h>
37 #include <asm/atomic_ops.h>
38 #include <asm/barrier.h>
40 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
42 static inline unsigned long *
43 __bitops_word(unsigned long nr
, volatile unsigned long *ptr
)
47 addr
= (unsigned long)ptr
+ ((nr
^ (nr
& (BITS_PER_LONG
- 1))) >> 3);
48 return (unsigned long *)addr
;
51 static inline unsigned char *
52 __bitops_byte(unsigned long nr
, volatile unsigned long *ptr
)
54 return ((unsigned char *)ptr
) + ((nr
^ (BITS_PER_LONG
- 8)) >> 3);
57 static inline void set_bit(unsigned long nr
, volatile unsigned long *ptr
)
59 unsigned long *addr
= __bitops_word(nr
, ptr
);
62 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
63 if (__builtin_constant_p(nr
)) {
64 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
74 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
75 __atomic64_or(mask
, addr
);
78 static inline void clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
80 unsigned long *addr
= __bitops_word(nr
, ptr
);
83 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
84 if (__builtin_constant_p(nr
)) {
85 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
90 : "i" (~(1 << (nr
& 7)))
95 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
96 __atomic64_and(mask
, addr
);
99 static inline void change_bit(unsigned long nr
, volatile unsigned long *ptr
)
101 unsigned long *addr
= __bitops_word(nr
, ptr
);
104 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
105 if (__builtin_constant_p(nr
)) {
106 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
111 : "i" (1 << (nr
& 7))
116 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
117 __atomic64_xor(mask
, addr
);
121 test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
123 unsigned long *addr
= __bitops_word(nr
, ptr
);
124 unsigned long old
, mask
;
126 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
127 old
= __atomic64_or_barrier(mask
, addr
);
128 return (old
& mask
) != 0;
132 test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
134 unsigned long *addr
= __bitops_word(nr
, ptr
);
135 unsigned long old
, mask
;
137 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
138 old
= __atomic64_and_barrier(mask
, addr
);
139 return (old
& ~mask
) != 0;
143 test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
145 unsigned long *addr
= __bitops_word(nr
, ptr
);
146 unsigned long old
, mask
;
148 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
149 old
= __atomic64_xor_barrier(mask
, addr
);
150 return (old
& mask
) != 0;
153 static inline void __set_bit(unsigned long nr
, volatile unsigned long *ptr
)
155 unsigned char *addr
= __bitops_byte(nr
, ptr
);
157 *addr
|= 1 << (nr
& 7);
161 __clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
163 unsigned char *addr
= __bitops_byte(nr
, ptr
);
165 *addr
&= ~(1 << (nr
& 7));
168 static inline void __change_bit(unsigned long nr
, volatile unsigned long *ptr
)
170 unsigned char *addr
= __bitops_byte(nr
, ptr
);
172 *addr
^= 1 << (nr
& 7);
176 __test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
178 unsigned char *addr
= __bitops_byte(nr
, ptr
);
182 *addr
|= 1 << (nr
& 7);
183 return (ch
>> (nr
& 7)) & 1;
187 __test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
189 unsigned char *addr
= __bitops_byte(nr
, ptr
);
193 *addr
&= ~(1 << (nr
& 7));
194 return (ch
>> (nr
& 7)) & 1;
198 __test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
200 unsigned char *addr
= __bitops_byte(nr
, ptr
);
204 *addr
^= 1 << (nr
& 7);
205 return (ch
>> (nr
& 7)) & 1;
208 static inline int test_bit(unsigned long nr
, const volatile unsigned long *ptr
)
210 const volatile unsigned char *addr
;
212 addr
= ((const volatile unsigned char *)ptr
);
213 addr
+= (nr
^ (BITS_PER_LONG
- 8)) >> 3;
214 return (*addr
>> (nr
& 7)) & 1;
217 static inline int test_and_set_bit_lock(unsigned long nr
,
218 volatile unsigned long *ptr
)
220 if (test_bit(nr
, ptr
))
222 return test_and_set_bit(nr
, ptr
);
225 static inline void clear_bit_unlock(unsigned long nr
,
226 volatile unsigned long *ptr
)
228 smp_mb__before_atomic();
232 static inline void __clear_bit_unlock(unsigned long nr
,
233 volatile unsigned long *ptr
)
236 __clear_bit(nr
, ptr
);
240 * Functions which use MSB0 bit numbering.
241 * The bits are numbered:
242 * |0..............63|64............127|128...........191|192...........255|
244 unsigned long find_first_bit_inv(const unsigned long *addr
, unsigned long size
);
245 unsigned long find_next_bit_inv(const unsigned long *addr
, unsigned long size
,
246 unsigned long offset
);
248 #define for_each_set_bit_inv(bit, addr, size) \
249 for ((bit) = find_first_bit_inv((addr), (size)); \
251 (bit) = find_next_bit_inv((addr), (size), (bit) + 1))
253 static inline void set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
255 return set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
258 static inline void clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
260 return clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
263 static inline void __set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
265 return __set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
268 static inline void __clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
270 return __clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
273 static inline int test_bit_inv(unsigned long nr
,
274 const volatile unsigned long *ptr
)
276 return test_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
279 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
282 * __flogr - find leftmost one
283 * @word - The word to search
285 * Returns the bit number of the most significant bit set,
286 * where the most significant bit has bit number 0.
287 * If no bit is set this function returns 64.
289 static inline unsigned char __flogr(unsigned long word
)
291 if (__builtin_constant_p(word
)) {
292 unsigned long bit
= 0;
296 if (!(word
& 0xffffffff00000000UL
)) {
300 if (!(word
& 0xffff000000000000UL
)) {
304 if (!(word
& 0xff00000000000000UL
)) {
308 if (!(word
& 0xf000000000000000UL
)) {
312 if (!(word
& 0xc000000000000000UL
)) {
316 if (!(word
& 0x8000000000000000UL
)) {
322 register unsigned long bit
asm("4") = word
;
323 register unsigned long out
asm("5");
326 " flogr %[bit],%[bit]\n"
327 : [bit
] "+d" (bit
), [out
] "=d" (out
) : : "cc");
333 * __ffs - find first bit in word.
334 * @word: The word to search
336 * Undefined if no bit exists, so code should check against 0 first.
338 static inline unsigned long __ffs(unsigned long word
)
340 return __flogr(-word
& word
) ^ (BITS_PER_LONG
- 1);
344 * ffs - find first bit set
345 * @word: the word to search
347 * This is defined the same way as the libc and
348 * compiler builtin ffs routines (man ffs).
350 static inline int ffs(int word
)
352 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
353 unsigned int val
= (unsigned int)word
;
355 return (1 + (__flogr(-val
& val
) ^ (BITS_PER_LONG
- 1))) & mask
;
359 * __fls - find last (most-significant) set bit in a long word
360 * @word: the word to search
362 * Undefined if no set bit exists, so code should check against 0 first.
364 static inline unsigned long __fls(unsigned long word
)
366 return __flogr(word
) ^ (BITS_PER_LONG
- 1);
370 * fls64 - find last set bit in a 64-bit word
371 * @word: the word to search
373 * This is defined in a similar way as the libc and compiler builtin
374 * ffsll, but returns the position of the most significant set bit.
376 * fls64(value) returns 0 if value is 0 or the position of the last
377 * set bit if value is nonzero. The last (most significant) bit is
380 static inline int fls64(unsigned long word
)
382 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
384 return (1 + (__flogr(word
) ^ (BITS_PER_LONG
- 1))) & mask
;
388 * fls - find last (most-significant) bit set
389 * @word: the word to search
391 * This is defined the same way as ffs.
392 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
394 static inline int fls(int word
)
396 return fls64((unsigned int)word
);
399 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
401 #include <asm-generic/bitops/__ffs.h>
402 #include <asm-generic/bitops/ffs.h>
403 #include <asm-generic/bitops/__fls.h>
404 #include <asm-generic/bitops/fls.h>
405 #include <asm-generic/bitops/fls64.h>
407 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
409 #include <asm-generic/bitops/ffz.h>
410 #include <asm-generic/bitops/find.h>
411 #include <asm-generic/bitops/hweight.h>
412 #include <asm-generic/bitops/sched.h>
413 #include <asm-generic/bitops/le.h>
414 #include <asm-generic/bitops/ext2-atomic-setbit.h>
416 #endif /* _S390_BITOPS_H */