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/asm.h> 40 41 #define arch___set_bit generic___set_bit 42 #define arch___clear_bit generic___clear_bit 43 #define arch___change_bit generic___change_bit 44 #define arch___test_and_set_bit generic___test_and_set_bit 45 #define arch___test_and_clear_bit generic___test_and_clear_bit 46 #define arch___test_and_change_bit generic___test_and_change_bit 47 #define arch_test_bit_acquire generic_test_bit_acquire 48 49 static __always_inline bool arch_test_bit(unsigned long nr, const volatile unsigned long *ptr) 50 { 51 #ifdef __HAVE_ASM_FLAG_OUTPUTS__ 52 const volatile unsigned char *addr; 53 unsigned long mask; 54 int cc; 55 56 if (__builtin_constant_p(nr)) { 57 addr = (const volatile unsigned char *)ptr; 58 addr += (nr ^ (BITS_PER_LONG - BITS_PER_BYTE)) / BITS_PER_BYTE; 59 mask = 1UL << (nr & (BITS_PER_BYTE - 1)); 60 asm volatile( 61 " tm %[addr],%[mask]\n" 62 : "=@cc" (cc) 63 : [addr] "R" (*addr), [mask] "I" (mask) 64 ); 65 return cc == 3; 66 } 67 #endif 68 return generic_test_bit(nr, ptr); 69 } 70 71 #include <asm-generic/bitops/atomic.h> 72 #include <asm-generic/bitops/non-instrumented-non-atomic.h> 73 #include <asm-generic/bitops/lock.h> 74 75 /* 76 * Functions which use MSB0 bit numbering. 77 * The bits are numbered: 78 * |0..............63|64............127|128...........191|192...........255| 79 */ 80 unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size); 81 unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size, 82 unsigned long offset); 83 84 #define for_each_set_bit_inv(bit, addr, size) \ 85 for ((bit) = find_first_bit_inv((addr), (size)); \ 86 (bit) < (size); \ 87 (bit) = find_next_bit_inv((addr), (size), (bit) + 1)) 88 89 static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr) 90 { 91 return set_bit(nr ^ (BITS_PER_LONG - 1), ptr); 92 } 93 94 static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) 95 { 96 return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); 97 } 98 99 static inline bool test_and_clear_bit_inv(unsigned long nr, 100 volatile unsigned long *ptr) 101 { 102 return test_and_clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); 103 } 104 105 static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr) 106 { 107 return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr); 108 } 109 110 static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) 111 { 112 return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); 113 } 114 115 static inline bool test_bit_inv(unsigned long nr, 116 const volatile unsigned long *ptr) 117 { 118 return test_bit(nr ^ (BITS_PER_LONG - 1), ptr); 119 } 120 121 /** 122 * __flogr - find leftmost one 123 * @word - The word to search 124 * 125 * Returns the bit number of the most significant bit set, 126 * where the most significant bit has bit number 0. 127 * If no bit is set this function returns 64. 128 */ 129 static inline unsigned char __flogr(unsigned long word) 130 { 131 if (__builtin_constant_p(word)) { 132 unsigned long bit = 0; 133 134 if (!word) 135 return 64; 136 if (!(word & 0xffffffff00000000UL)) { 137 word <<= 32; 138 bit += 32; 139 } 140 if (!(word & 0xffff000000000000UL)) { 141 word <<= 16; 142 bit += 16; 143 } 144 if (!(word & 0xff00000000000000UL)) { 145 word <<= 8; 146 bit += 8; 147 } 148 if (!(word & 0xf000000000000000UL)) { 149 word <<= 4; 150 bit += 4; 151 } 152 if (!(word & 0xc000000000000000UL)) { 153 word <<= 2; 154 bit += 2; 155 } 156 if (!(word & 0x8000000000000000UL)) { 157 word <<= 1; 158 bit += 1; 159 } 160 return bit; 161 } else { 162 union register_pair rp; 163 164 rp.even = word; 165 asm volatile( 166 " flogr %[rp],%[rp]\n" 167 : [rp] "+d" (rp.pair) : : "cc"); 168 return rp.even; 169 } 170 } 171 172 /** 173 * __ffs - find first bit in word. 174 * @word: The word to search 175 * 176 * Undefined if no bit exists, so code should check against 0 first. 177 */ 178 static inline unsigned long __ffs(unsigned long word) 179 { 180 return __flogr(-word & word) ^ (BITS_PER_LONG - 1); 181 } 182 183 /** 184 * ffs - find first bit set 185 * @word: the word to search 186 * 187 * This is defined the same way as the libc and 188 * compiler builtin ffs routines (man ffs). 189 */ 190 static inline int ffs(int word) 191 { 192 unsigned long mask = 2 * BITS_PER_LONG - 1; 193 unsigned int val = (unsigned int)word; 194 195 return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask; 196 } 197 198 /** 199 * __fls - find last (most-significant) set bit in a long word 200 * @word: the word to search 201 * 202 * Undefined if no set bit exists, so code should check against 0 first. 203 */ 204 static inline unsigned long __fls(unsigned long word) 205 { 206 return __flogr(word) ^ (BITS_PER_LONG - 1); 207 } 208 209 /** 210 * fls64 - find last set bit in a 64-bit word 211 * @word: the word to search 212 * 213 * This is defined in a similar way as the libc and compiler builtin 214 * ffsll, but returns the position of the most significant set bit. 215 * 216 * fls64(value) returns 0 if value is 0 or the position of the last 217 * set bit if value is nonzero. The last (most significant) bit is 218 * at position 64. 219 */ 220 static inline int fls64(unsigned long word) 221 { 222 unsigned long mask = 2 * BITS_PER_LONG - 1; 223 224 return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask; 225 } 226 227 /** 228 * fls - find last (most-significant) bit set 229 * @word: the word to search 230 * 231 * This is defined the same way as ffs. 232 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 233 */ 234 static inline int fls(unsigned int word) 235 { 236 return fls64(word); 237 } 238 239 #include <asm/arch_hweight.h> 240 #include <asm-generic/bitops/const_hweight.h> 241 #include <asm-generic/bitops/ffz.h> 242 #include <asm-generic/bitops/sched.h> 243 #include <asm-generic/bitops/le.h> 244 #include <asm-generic/bitops/ext2-atomic-setbit.h> 245 246 #endif /* _S390_BITOPS_H */ 247