1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright IBM Corp. 1999,2013 4 * 5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, 6 * 7 * The description below was taken in large parts from the powerpc 8 * bitops header file: 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. 14 * 15 * The bitop functions are defined to work on unsigned longs, so the bits 16 * end up numbered: 17 * |63..............0|127............64|191...........128|255...........192| 18 * 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| 22 * 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 25 * XOR with 0x3f. 26 * 27 */ 28 29 #ifndef _S390_BITOPS_H 30 #define _S390_BITOPS_H 31 32 #ifndef _LINUX_BITOPS_H 33 #error only <linux/bitops.h> can be included directly 34 #endif 35 36 #include <linux/typecheck.h> 37 #include <linux/compiler.h> 38 #include <linux/types.h> 39 #include <asm/atomic_ops.h> 40 #include <asm/barrier.h> 41 42 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG) 43 44 static inline unsigned long * 45 __bitops_word(unsigned long nr, volatile unsigned long *ptr) 46 { 47 unsigned long addr; 48 49 addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3); 50 return (unsigned long *)addr; 51 } 52 53 static inline unsigned char * 54 __bitops_byte(unsigned long nr, volatile unsigned long *ptr) 55 { 56 return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3); 57 } 58 59 static __always_inline void arch_set_bit(unsigned long nr, volatile unsigned long *ptr) 60 { 61 unsigned long *addr = __bitops_word(nr, ptr); 62 unsigned long mask; 63 64 mask = 1UL << (nr & (BITS_PER_LONG - 1)); 65 __atomic64_or(mask, (long *)addr); 66 } 67 68 static __always_inline void arch_clear_bit(unsigned long nr, volatile unsigned long *ptr) 69 { 70 unsigned long *addr = __bitops_word(nr, ptr); 71 unsigned long mask; 72 73 mask = ~(1UL << (nr & (BITS_PER_LONG - 1))); 74 __atomic64_and(mask, (long *)addr); 75 } 76 77 static __always_inline void arch_change_bit(unsigned long nr, 78 volatile unsigned long *ptr) 79 { 80 unsigned long *addr = __bitops_word(nr, ptr); 81 unsigned long mask; 82 83 mask = 1UL << (nr & (BITS_PER_LONG - 1)); 84 __atomic64_xor(mask, (long *)addr); 85 } 86 87 static inline bool arch_test_and_set_bit(unsigned long nr, 88 volatile unsigned long *ptr) 89 { 90 unsigned long *addr = __bitops_word(nr, ptr); 91 unsigned long old, mask; 92 93 mask = 1UL << (nr & (BITS_PER_LONG - 1)); 94 old = __atomic64_or_barrier(mask, (long *)addr); 95 return (old & mask) != 0; 96 } 97 98 static inline bool arch_test_and_clear_bit(unsigned long nr, 99 volatile unsigned long *ptr) 100 { 101 unsigned long *addr = __bitops_word(nr, ptr); 102 unsigned long old, mask; 103 104 mask = ~(1UL << (nr & (BITS_PER_LONG - 1))); 105 old = __atomic64_and_barrier(mask, (long *)addr); 106 return (old & ~mask) != 0; 107 } 108 109 static inline bool arch_test_and_change_bit(unsigned long nr, 110 volatile unsigned long *ptr) 111 { 112 unsigned long *addr = __bitops_word(nr, ptr); 113 unsigned long old, mask; 114 115 mask = 1UL << (nr & (BITS_PER_LONG - 1)); 116 old = __atomic64_xor_barrier(mask, (long *)addr); 117 return (old & mask) != 0; 118 } 119 120 static inline void arch___set_bit(unsigned long nr, volatile unsigned long *ptr) 121 { 122 unsigned char *addr = __bitops_byte(nr, ptr); 123 124 *addr |= 1 << (nr & 7); 125 } 126 127 static inline void arch___clear_bit(unsigned long nr, 128 volatile unsigned long *ptr) 129 { 130 unsigned char *addr = __bitops_byte(nr, ptr); 131 132 *addr &= ~(1 << (nr & 7)); 133 } 134 135 static inline void arch___change_bit(unsigned long nr, 136 volatile unsigned long *ptr) 137 { 138 unsigned char *addr = __bitops_byte(nr, ptr); 139 140 *addr ^= 1 << (nr & 7); 141 } 142 143 static inline bool arch___test_and_set_bit(unsigned long nr, 144 volatile unsigned long *ptr) 145 { 146 unsigned char *addr = __bitops_byte(nr, ptr); 147 unsigned char ch; 148 149 ch = *addr; 150 *addr |= 1 << (nr & 7); 151 return (ch >> (nr & 7)) & 1; 152 } 153 154 static inline bool arch___test_and_clear_bit(unsigned long nr, 155 volatile unsigned long *ptr) 156 { 157 unsigned char *addr = __bitops_byte(nr, ptr); 158 unsigned char ch; 159 160 ch = *addr; 161 *addr &= ~(1 << (nr & 7)); 162 return (ch >> (nr & 7)) & 1; 163 } 164 165 static inline bool arch___test_and_change_bit(unsigned long nr, 166 volatile unsigned long *ptr) 167 { 168 unsigned char *addr = __bitops_byte(nr, ptr); 169 unsigned char ch; 170 171 ch = *addr; 172 *addr ^= 1 << (nr & 7); 173 return (ch >> (nr & 7)) & 1; 174 } 175 176 static inline bool arch_test_bit(unsigned long nr, 177 const volatile unsigned long *ptr) 178 { 179 const volatile unsigned char *addr; 180 181 addr = ((const volatile unsigned char *)ptr); 182 addr += (nr ^ (BITS_PER_LONG - 8)) >> 3; 183 return (*addr >> (nr & 7)) & 1; 184 } 185 186 static inline bool arch_test_and_set_bit_lock(unsigned long nr, 187 volatile unsigned long *ptr) 188 { 189 if (arch_test_bit(nr, ptr)) 190 return 1; 191 return arch_test_and_set_bit(nr, ptr); 192 } 193 194 static inline void arch_clear_bit_unlock(unsigned long nr, 195 volatile unsigned long *ptr) 196 { 197 smp_mb__before_atomic(); 198 arch_clear_bit(nr, ptr); 199 } 200 201 static inline void arch___clear_bit_unlock(unsigned long nr, 202 volatile unsigned long *ptr) 203 { 204 smp_mb(); 205 arch___clear_bit(nr, ptr); 206 } 207 208 #include <asm-generic/bitops/instrumented-atomic.h> 209 #include <asm-generic/bitops/instrumented-non-atomic.h> 210 #include <asm-generic/bitops/instrumented-lock.h> 211 212 /* 213 * Functions which use MSB0 bit numbering. 214 * The bits are numbered: 215 * |0..............63|64............127|128...........191|192...........255| 216 */ 217 unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size); 218 unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size, 219 unsigned long offset); 220 221 #define for_each_set_bit_inv(bit, addr, size) \ 222 for ((bit) = find_first_bit_inv((addr), (size)); \ 223 (bit) < (size); \ 224 (bit) = find_next_bit_inv((addr), (size), (bit) + 1)) 225 226 static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr) 227 { 228 return set_bit(nr ^ (BITS_PER_LONG - 1), ptr); 229 } 230 231 static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) 232 { 233 return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); 234 } 235 236 static inline bool test_and_clear_bit_inv(unsigned long nr, 237 volatile unsigned long *ptr) 238 { 239 return test_and_clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); 240 } 241 242 static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr) 243 { 244 return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr); 245 } 246 247 static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) 248 { 249 return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); 250 } 251 252 static inline bool test_bit_inv(unsigned long nr, 253 const volatile unsigned long *ptr) 254 { 255 return test_bit(nr ^ (BITS_PER_LONG - 1), ptr); 256 } 257 258 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES 259 260 /** 261 * __flogr - find leftmost one 262 * @word - The word to search 263 * 264 * Returns the bit number of the most significant bit set, 265 * where the most significant bit has bit number 0. 266 * If no bit is set this function returns 64. 267 */ 268 static inline unsigned char __flogr(unsigned long word) 269 { 270 if (__builtin_constant_p(word)) { 271 unsigned long bit = 0; 272 273 if (!word) 274 return 64; 275 if (!(word & 0xffffffff00000000UL)) { 276 word <<= 32; 277 bit += 32; 278 } 279 if (!(word & 0xffff000000000000UL)) { 280 word <<= 16; 281 bit += 16; 282 } 283 if (!(word & 0xff00000000000000UL)) { 284 word <<= 8; 285 bit += 8; 286 } 287 if (!(word & 0xf000000000000000UL)) { 288 word <<= 4; 289 bit += 4; 290 } 291 if (!(word & 0xc000000000000000UL)) { 292 word <<= 2; 293 bit += 2; 294 } 295 if (!(word & 0x8000000000000000UL)) { 296 word <<= 1; 297 bit += 1; 298 } 299 return bit; 300 } else { 301 register unsigned long bit asm("4") = word; 302 register unsigned long out asm("5"); 303 304 asm volatile( 305 " flogr %[bit],%[bit]\n" 306 : [bit] "+d" (bit), [out] "=d" (out) : : "cc"); 307 return bit; 308 } 309 } 310 311 /** 312 * __ffs - find first bit in word. 313 * @word: The word to search 314 * 315 * Undefined if no bit exists, so code should check against 0 first. 316 */ 317 static inline unsigned long __ffs(unsigned long word) 318 { 319 return __flogr(-word & word) ^ (BITS_PER_LONG - 1); 320 } 321 322 /** 323 * ffs - find first bit set 324 * @word: the word to search 325 * 326 * This is defined the same way as the libc and 327 * compiler builtin ffs routines (man ffs). 328 */ 329 static inline int ffs(int word) 330 { 331 unsigned long mask = 2 * BITS_PER_LONG - 1; 332 unsigned int val = (unsigned int)word; 333 334 return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask; 335 } 336 337 /** 338 * __fls - find last (most-significant) set bit in a long word 339 * @word: the word to search 340 * 341 * Undefined if no set bit exists, so code should check against 0 first. 342 */ 343 static inline unsigned long __fls(unsigned long word) 344 { 345 return __flogr(word) ^ (BITS_PER_LONG - 1); 346 } 347 348 /** 349 * fls64 - find last set bit in a 64-bit word 350 * @word: the word to search 351 * 352 * This is defined in a similar way as the libc and compiler builtin 353 * ffsll, but returns the position of the most significant set bit. 354 * 355 * fls64(value) returns 0 if value is 0 or the position of the last 356 * set bit if value is nonzero. The last (most significant) bit is 357 * at position 64. 358 */ 359 static inline int fls64(unsigned long word) 360 { 361 unsigned long mask = 2 * BITS_PER_LONG - 1; 362 363 return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask; 364 } 365 366 /** 367 * fls - find last (most-significant) bit set 368 * @word: the word to search 369 * 370 * This is defined the same way as ffs. 371 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 372 */ 373 static inline int fls(unsigned int word) 374 { 375 return fls64(word); 376 } 377 378 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */ 379 380 #include <asm-generic/bitops/__ffs.h> 381 #include <asm-generic/bitops/ffs.h> 382 #include <asm-generic/bitops/__fls.h> 383 #include <asm-generic/bitops/fls.h> 384 #include <asm-generic/bitops/fls64.h> 385 386 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */ 387 388 #include <asm-generic/bitops/ffz.h> 389 #include <asm-generic/bitops/find.h> 390 #include <asm-generic/bitops/hweight.h> 391 #include <asm-generic/bitops/sched.h> 392 #include <asm-generic/bitops/le.h> 393 #include <asm-generic/bitops/ext2-atomic-setbit.h> 394 395 #endif /* _S390_BITOPS_H */ 396