bitops.h 10 KB

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