bitops.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright IBM Corp. 1999,2013
  4. *
  5. * Author(s): Martin Schwidefsky <[email protected]>,
  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 <linux/types.h>
  36. #include <asm/atomic_ops.h>
  37. #include <asm/barrier.h>
  38. #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
  39. static inline unsigned long *
  40. __bitops_word(unsigned long nr, const volatile unsigned long *ptr)
  41. {
  42. unsigned long addr;
  43. addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3);
  44. return (unsigned long *)addr;
  45. }
  46. static inline unsigned long __bitops_mask(unsigned long nr)
  47. {
  48. return 1UL << (nr & (BITS_PER_LONG - 1));
  49. }
  50. static __always_inline void arch_set_bit(unsigned long nr, volatile unsigned long *ptr)
  51. {
  52. unsigned long *addr = __bitops_word(nr, ptr);
  53. unsigned long mask = __bitops_mask(nr);
  54. __atomic64_or(mask, (long *)addr);
  55. }
  56. static __always_inline void arch_clear_bit(unsigned long nr, volatile unsigned long *ptr)
  57. {
  58. unsigned long *addr = __bitops_word(nr, ptr);
  59. unsigned long mask = __bitops_mask(nr);
  60. __atomic64_and(~mask, (long *)addr);
  61. }
  62. static __always_inline void arch_change_bit(unsigned long nr,
  63. volatile unsigned long *ptr)
  64. {
  65. unsigned long *addr = __bitops_word(nr, ptr);
  66. unsigned long mask = __bitops_mask(nr);
  67. __atomic64_xor(mask, (long *)addr);
  68. }
  69. static inline bool arch_test_and_set_bit(unsigned long nr,
  70. volatile unsigned long *ptr)
  71. {
  72. unsigned long *addr = __bitops_word(nr, ptr);
  73. unsigned long mask = __bitops_mask(nr);
  74. unsigned long old;
  75. old = __atomic64_or_barrier(mask, (long *)addr);
  76. return old & mask;
  77. }
  78. static inline bool arch_test_and_clear_bit(unsigned long nr,
  79. volatile unsigned long *ptr)
  80. {
  81. unsigned long *addr = __bitops_word(nr, ptr);
  82. unsigned long mask = __bitops_mask(nr);
  83. unsigned long old;
  84. old = __atomic64_and_barrier(~mask, (long *)addr);
  85. return old & mask;
  86. }
  87. static inline bool arch_test_and_change_bit(unsigned long nr,
  88. volatile unsigned long *ptr)
  89. {
  90. unsigned long *addr = __bitops_word(nr, ptr);
  91. unsigned long mask = __bitops_mask(nr);
  92. unsigned long old;
  93. old = __atomic64_xor_barrier(mask, (long *)addr);
  94. return old & mask;
  95. }
  96. static __always_inline void
  97. arch___set_bit(unsigned long nr, volatile unsigned long *addr)
  98. {
  99. unsigned long *p = __bitops_word(nr, addr);
  100. unsigned long mask = __bitops_mask(nr);
  101. *p |= mask;
  102. }
  103. static __always_inline void
  104. arch___clear_bit(unsigned long nr, volatile unsigned long *addr)
  105. {
  106. unsigned long *p = __bitops_word(nr, addr);
  107. unsigned long mask = __bitops_mask(nr);
  108. *p &= ~mask;
  109. }
  110. static __always_inline void
  111. arch___change_bit(unsigned long nr, volatile unsigned long *addr)
  112. {
  113. unsigned long *p = __bitops_word(nr, addr);
  114. unsigned long mask = __bitops_mask(nr);
  115. *p ^= mask;
  116. }
  117. static __always_inline bool
  118. arch___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
  119. {
  120. unsigned long *p = __bitops_word(nr, addr);
  121. unsigned long mask = __bitops_mask(nr);
  122. unsigned long old;
  123. old = *p;
  124. *p |= mask;
  125. return old & mask;
  126. }
  127. static __always_inline bool
  128. arch___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
  129. {
  130. unsigned long *p = __bitops_word(nr, addr);
  131. unsigned long mask = __bitops_mask(nr);
  132. unsigned long old;
  133. old = *p;
  134. *p &= ~mask;
  135. return old & mask;
  136. }
  137. static __always_inline bool
  138. arch___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
  139. {
  140. unsigned long *p = __bitops_word(nr, addr);
  141. unsigned long mask = __bitops_mask(nr);
  142. unsigned long old;
  143. old = *p;
  144. *p ^= mask;
  145. return old & mask;
  146. }
  147. #define arch_test_bit generic_test_bit
  148. #define arch_test_bit_acquire generic_test_bit_acquire
  149. static inline bool arch_test_and_set_bit_lock(unsigned long nr,
  150. volatile unsigned long *ptr)
  151. {
  152. if (arch_test_bit(nr, ptr))
  153. return true;
  154. return arch_test_and_set_bit(nr, ptr);
  155. }
  156. static inline void arch_clear_bit_unlock(unsigned long nr,
  157. volatile unsigned long *ptr)
  158. {
  159. smp_mb__before_atomic();
  160. arch_clear_bit(nr, ptr);
  161. }
  162. static inline void arch___clear_bit_unlock(unsigned long nr,
  163. volatile unsigned long *ptr)
  164. {
  165. smp_mb();
  166. arch___clear_bit(nr, ptr);
  167. }
  168. #include <asm-generic/bitops/instrumented-atomic.h>
  169. #include <asm-generic/bitops/instrumented-non-atomic.h>
  170. #include <asm-generic/bitops/instrumented-lock.h>
  171. /*
  172. * Functions which use MSB0 bit numbering.
  173. * The bits are numbered:
  174. * |0..............63|64............127|128...........191|192...........255|
  175. */
  176. unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size);
  177. unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size,
  178. unsigned long offset);
  179. #define for_each_set_bit_inv(bit, addr, size) \
  180. for ((bit) = find_first_bit_inv((addr), (size)); \
  181. (bit) < (size); \
  182. (bit) = find_next_bit_inv((addr), (size), (bit) + 1))
  183. static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  184. {
  185. return set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  186. }
  187. static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  188. {
  189. return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  190. }
  191. static inline bool test_and_clear_bit_inv(unsigned long nr,
  192. volatile unsigned long *ptr)
  193. {
  194. return test_and_clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  195. }
  196. static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  197. {
  198. return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  199. }
  200. static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  201. {
  202. return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  203. }
  204. static inline bool test_bit_inv(unsigned long nr,
  205. const volatile unsigned long *ptr)
  206. {
  207. return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  208. }
  209. /**
  210. * __flogr - find leftmost one
  211. * @word - The word to search
  212. *
  213. * Returns the bit number of the most significant bit set,
  214. * where the most significant bit has bit number 0.
  215. * If no bit is set this function returns 64.
  216. */
  217. static inline unsigned char __flogr(unsigned long word)
  218. {
  219. if (__builtin_constant_p(word)) {
  220. unsigned long bit = 0;
  221. if (!word)
  222. return 64;
  223. if (!(word & 0xffffffff00000000UL)) {
  224. word <<= 32;
  225. bit += 32;
  226. }
  227. if (!(word & 0xffff000000000000UL)) {
  228. word <<= 16;
  229. bit += 16;
  230. }
  231. if (!(word & 0xff00000000000000UL)) {
  232. word <<= 8;
  233. bit += 8;
  234. }
  235. if (!(word & 0xf000000000000000UL)) {
  236. word <<= 4;
  237. bit += 4;
  238. }
  239. if (!(word & 0xc000000000000000UL)) {
  240. word <<= 2;
  241. bit += 2;
  242. }
  243. if (!(word & 0x8000000000000000UL)) {
  244. word <<= 1;
  245. bit += 1;
  246. }
  247. return bit;
  248. } else {
  249. union register_pair rp;
  250. rp.even = word;
  251. asm volatile(
  252. " flogr %[rp],%[rp]\n"
  253. : [rp] "+d" (rp.pair) : : "cc");
  254. return rp.even;
  255. }
  256. }
  257. /**
  258. * __ffs - find first bit in word.
  259. * @word: The word to search
  260. *
  261. * Undefined if no bit exists, so code should check against 0 first.
  262. */
  263. static inline unsigned long __ffs(unsigned long word)
  264. {
  265. return __flogr(-word & word) ^ (BITS_PER_LONG - 1);
  266. }
  267. /**
  268. * ffs - find first bit set
  269. * @word: the word to search
  270. *
  271. * This is defined the same way as the libc and
  272. * compiler builtin ffs routines (man ffs).
  273. */
  274. static inline int ffs(int word)
  275. {
  276. unsigned long mask = 2 * BITS_PER_LONG - 1;
  277. unsigned int val = (unsigned int)word;
  278. return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask;
  279. }
  280. /**
  281. * __fls - find last (most-significant) set bit in a long word
  282. * @word: the word to search
  283. *
  284. * Undefined if no set bit exists, so code should check against 0 first.
  285. */
  286. static inline unsigned long __fls(unsigned long word)
  287. {
  288. return __flogr(word) ^ (BITS_PER_LONG - 1);
  289. }
  290. /**
  291. * fls64 - find last set bit in a 64-bit word
  292. * @word: the word to search
  293. *
  294. * This is defined in a similar way as the libc and compiler builtin
  295. * ffsll, but returns the position of the most significant set bit.
  296. *
  297. * fls64(value) returns 0 if value is 0 or the position of the last
  298. * set bit if value is nonzero. The last (most significant) bit is
  299. * at position 64.
  300. */
  301. static inline int fls64(unsigned long word)
  302. {
  303. unsigned long mask = 2 * BITS_PER_LONG - 1;
  304. return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask;
  305. }
  306. /**
  307. * fls - find last (most-significant) bit set
  308. * @word: the word to search
  309. *
  310. * This is defined the same way as ffs.
  311. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  312. */
  313. static inline int fls(unsigned int word)
  314. {
  315. return fls64(word);
  316. }
  317. #include <asm-generic/bitops/ffz.h>
  318. #include <asm-generic/bitops/hweight.h>
  319. #include <asm-generic/bitops/sched.h>
  320. #include <asm-generic/bitops/le.h>
  321. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  322. #endif /* _S390_BITOPS_H */