sbi_bitops.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Atish Patra <atish.patra@wdc.com>
  8. */
  9. #ifndef __SBI_BITOPS_H__
  10. #define __SBI_BITOPS_H__
  11. #include <sbi/sbi_types.h>
  12. #if __SIZEOF_POINTER__ == 8
  13. #define BITS_PER_LONG 64
  14. #elif __SIZEOF_POINTER__ == 4
  15. #define BITS_PER_LONG 32
  16. #else
  17. #error "Unexpected __SIZEOF_POINTER__"
  18. #endif
  19. #define EXTRACT_FIELD(val, which) \
  20. (((val) & (which)) / ((which) & ~((which)-1)))
  21. #define INSERT_FIELD(val, which, fieldval) \
  22. (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))))
  23. #define BITS_TO_LONGS(nbits) (((nbits) + BITS_PER_LONG - 1) / \
  24. BITS_PER_LONG)
  25. #define BIT(nr) (1UL << (nr))
  26. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  27. #define BIT_WORD(bit) ((bit) / BITS_PER_LONG)
  28. #define BIT_WORD_OFFSET(bit) ((bit) & (BITS_PER_LONG - 1))
  29. #define GENMASK(h, l) \
  30. (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
  31. /**
  32. * sbi_ffs - find first (less-significant) set bit in a long word.
  33. * @word: The word to search
  34. *
  35. * Undefined if no bit exists, so code should check against 0 first.
  36. */
  37. static inline int sbi_ffs(unsigned long word)
  38. {
  39. int num = 0;
  40. #if BITS_PER_LONG == 64
  41. if ((word & 0xffffffff) == 0) {
  42. num += 32;
  43. word >>= 32;
  44. }
  45. #endif
  46. if ((word & 0xffff) == 0) {
  47. num += 16;
  48. word >>= 16;
  49. }
  50. if ((word & 0xff) == 0) {
  51. num += 8;
  52. word >>= 8;
  53. }
  54. if ((word & 0xf) == 0) {
  55. num += 4;
  56. word >>= 4;
  57. }
  58. if ((word & 0x3) == 0) {
  59. num += 2;
  60. word >>= 2;
  61. }
  62. if ((word & 0x1) == 0)
  63. num += 1;
  64. return num;
  65. }
  66. /*
  67. * sbi_ffz - find first zero in word.
  68. * @word: The word to search
  69. *
  70. * Undefined if no zero exists, so code should check against ~0UL first.
  71. */
  72. #define sbi_ffz(x) sbi_ffs(~(x))
  73. /**
  74. * sbi_fls - find last (most-significant) set bit in a long word
  75. * @word: the word to search
  76. *
  77. * Undefined if no set bit exists, so code should check against 0 first.
  78. */
  79. static inline unsigned long sbi_fls(unsigned long word)
  80. {
  81. int num = BITS_PER_LONG - 1;
  82. #if BITS_PER_LONG == 64
  83. if (!(word & (~0ul << 32))) {
  84. num -= 32;
  85. word <<= 32;
  86. }
  87. #endif
  88. if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
  89. num -= 16;
  90. word <<= 16;
  91. }
  92. if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
  93. num -= 8;
  94. word <<= 8;
  95. }
  96. if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
  97. num -= 4;
  98. word <<= 4;
  99. }
  100. if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
  101. num -= 2;
  102. word <<= 2;
  103. }
  104. if (!(word & (~0ul << (BITS_PER_LONG-1))))
  105. num -= 1;
  106. return num;
  107. }
  108. #define for_each_set_bit(bit, addr, size) \
  109. for ((bit) = find_first_bit((addr), (size)); \
  110. (bit) < (size); \
  111. (bit) = find_next_bit((addr), (size), (bit) + 1))
  112. /* same as for_each_set_bit() but use bit as value to start with */
  113. #define for_each_set_bit_from(bit, addr, size) \
  114. for ((bit) = find_next_bit((addr), (size), (bit)); \
  115. (bit) < (size); \
  116. (bit) = find_next_bit((addr), (size), (bit) + 1))
  117. #define for_each_clear_bit(bit, addr, size) \
  118. for ((bit) = find_first_zero_bit((addr), (size)); \
  119. (bit) < (size); \
  120. (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
  121. /* same as for_each_clear_bit() but use bit as value to start with */
  122. #define for_each_clear_bit_from(bit, addr, size) \
  123. for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
  124. (bit) < (size); \
  125. (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
  126. unsigned long find_first_bit(const unsigned long *addr,
  127. unsigned long size);
  128. unsigned long find_first_zero_bit(const unsigned long *addr,
  129. unsigned long size);
  130. unsigned long find_last_bit(const unsigned long *addr,
  131. unsigned long size);
  132. unsigned long find_next_bit(const unsigned long *addr,
  133. unsigned long size, unsigned long offset);
  134. unsigned long find_next_zero_bit(const unsigned long *addr,
  135. unsigned long size,
  136. unsigned long offset);
  137. /**
  138. * __set_bit - Set a bit in memory
  139. * @nr: the bit to set
  140. * @addr: the address to start counting from
  141. *
  142. * This function is non-atomic and may be reordered.
  143. */
  144. static inline void __set_bit(int nr, volatile unsigned long *addr)
  145. {
  146. unsigned long mask = BIT_MASK(nr);
  147. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  148. *p |= mask;
  149. }
  150. /**
  151. * __clear_bit - Clear a bit in memory
  152. * @nr: the bit to clear
  153. * @addr: the address to start counting from
  154. *
  155. * This function is non-atomic and may be reordered.
  156. */
  157. static inline void __clear_bit(int nr, volatile unsigned long *addr)
  158. {
  159. unsigned long mask = BIT_MASK(nr);
  160. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  161. *p &= ~mask;
  162. }
  163. /**
  164. * __change_bit - Toggle a bit in memory
  165. * @nr: the bit to change
  166. * @addr: the address to start counting from
  167. *
  168. * This function is non-atomic and may be reordered.
  169. */
  170. static inline void __change_bit(int nr, volatile unsigned long *addr)
  171. {
  172. unsigned long mask = BIT_MASK(nr);
  173. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  174. *p ^= mask;
  175. }
  176. /**
  177. * __test_and_set_bit - Set a bit and return its old value
  178. * @nr: Bit to set
  179. * @addr: Address to count from
  180. *
  181. * This operation is non-atomic and can be reordered.
  182. */
  183. static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
  184. {
  185. unsigned long mask = BIT_MASK(nr);
  186. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  187. unsigned long old = *p;
  188. *p = old | mask;
  189. return (old & mask) != 0;
  190. }
  191. /**
  192. * __test_and_clear_bit - Clear a bit and return its old value
  193. * @nr: Bit to clear
  194. * @addr: Address to count from
  195. *
  196. * This operation is non-atomic and can be reordered.
  197. */
  198. static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
  199. {
  200. unsigned long mask = BIT_MASK(nr);
  201. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  202. unsigned long old = *p;
  203. *p = old & ~mask;
  204. return (old & mask) != 0;
  205. }
  206. /**
  207. * __test_bit - Determine whether a bit is set
  208. * @nr: bit number to test
  209. * @addr: Address to start counting from
  210. *
  211. * This operation is non-atomic and can be reordered.
  212. */
  213. static inline int __test_bit(int nr, const volatile unsigned long *addr)
  214. {
  215. return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
  216. }
  217. #endif