sbi_bitops.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. * ffs - Find first bit set
  33. * @x: the word to search
  34. *
  35. * This is defined the same way as
  36. * the libc and compiler builtin ffs routines, therefore
  37. * differs in spirit from the above ffz (man ffs).
  38. */
  39. static inline int ffs(int x)
  40. {
  41. int r = 1;
  42. if (!x)
  43. return 0;
  44. if (!(x & 0xffff)) {
  45. x >>= 16;
  46. r += 16;
  47. }
  48. if (!(x & 0xff)) {
  49. x >>= 8;
  50. r += 8;
  51. }
  52. if (!(x & 0xf)) {
  53. x >>= 4;
  54. r += 4;
  55. }
  56. if (!(x & 3)) {
  57. x >>= 2;
  58. r += 2;
  59. }
  60. if (!(x & 1))
  61. r += 1;
  62. return r;
  63. }
  64. /**
  65. * __ffs - find first bit in word.
  66. * @word: The word to search
  67. *
  68. * Undefined if no bit exists, so code should check against 0 first.
  69. */
  70. static inline int __ffs(unsigned long word)
  71. {
  72. int num = 0;
  73. #if BITS_PER_LONG == 64
  74. if ((word & 0xffffffff) == 0) {
  75. num += 32;
  76. word >>= 32;
  77. }
  78. #endif
  79. if ((word & 0xffff) == 0) {
  80. num += 16;
  81. word >>= 16;
  82. }
  83. if ((word & 0xff) == 0) {
  84. num += 8;
  85. word >>= 8;
  86. }
  87. if ((word & 0xf) == 0) {
  88. num += 4;
  89. word >>= 4;
  90. }
  91. if ((word & 0x3) == 0) {
  92. num += 2;
  93. word >>= 2;
  94. }
  95. if ((word & 0x1) == 0)
  96. num += 1;
  97. return num;
  98. }
  99. /*
  100. * ffz - find first zero in word.
  101. * @word: The word to search
  102. *
  103. * Undefined if no zero exists, so code should check against ~0UL first.
  104. */
  105. #define ffz(x) __ffs(~(x))
  106. /**
  107. * fls - find last (most-significant) bit set
  108. * @x: the word to search
  109. *
  110. * This is defined the same way as ffs.
  111. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  112. */
  113. static inline int fls(int x)
  114. {
  115. int r = 32;
  116. if (!x)
  117. return 0;
  118. if (!(x & 0xffff0000u)) {
  119. x <<= 16;
  120. r -= 16;
  121. }
  122. if (!(x & 0xff000000u)) {
  123. x <<= 8;
  124. r -= 8;
  125. }
  126. if (!(x & 0xf0000000u)) {
  127. x <<= 4;
  128. r -= 4;
  129. }
  130. if (!(x & 0xc0000000u)) {
  131. x <<= 2;
  132. r -= 2;
  133. }
  134. if (!(x & 0x80000000u))
  135. r -= 1;
  136. return r;
  137. }
  138. /**
  139. * __fls - find last (most-significant) set bit in a long word
  140. * @word: the word to search
  141. *
  142. * Undefined if no set bit exists, so code should check against 0 first.
  143. */
  144. static inline unsigned long __fls(unsigned long word)
  145. {
  146. int num = BITS_PER_LONG - 1;
  147. #if BITS_PER_LONG == 64
  148. if (!(word & (~0ul << 32))) {
  149. num -= 32;
  150. word <<= 32;
  151. }
  152. #endif
  153. if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
  154. num -= 16;
  155. word <<= 16;
  156. }
  157. if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
  158. num -= 8;
  159. word <<= 8;
  160. }
  161. if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
  162. num -= 4;
  163. word <<= 4;
  164. }
  165. if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
  166. num -= 2;
  167. word <<= 2;
  168. }
  169. if (!(word & (~0ul << (BITS_PER_LONG-1))))
  170. num -= 1;
  171. return num;
  172. }
  173. #define for_each_set_bit(bit, addr, size) \
  174. for ((bit) = find_first_bit((addr), (size)); \
  175. (bit) < (size); \
  176. (bit) = find_next_bit((addr), (size), (bit) + 1))
  177. /* same as for_each_set_bit() but use bit as value to start with */
  178. #define for_each_set_bit_from(bit, addr, size) \
  179. for ((bit) = find_next_bit((addr), (size), (bit)); \
  180. (bit) < (size); \
  181. (bit) = find_next_bit((addr), (size), (bit) + 1))
  182. #define for_each_clear_bit(bit, addr, size) \
  183. for ((bit) = find_first_zero_bit((addr), (size)); \
  184. (bit) < (size); \
  185. (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
  186. /* same as for_each_clear_bit() but use bit as value to start with */
  187. #define for_each_clear_bit_from(bit, addr, size) \
  188. for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
  189. (bit) < (size); \
  190. (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
  191. unsigned long find_first_bit(const unsigned long *addr,
  192. unsigned long size);
  193. unsigned long find_first_zero_bit(const unsigned long *addr,
  194. unsigned long size);
  195. unsigned long find_last_bit(const unsigned long *addr,
  196. unsigned long size);
  197. unsigned long find_next_bit(const unsigned long *addr,
  198. unsigned long size, unsigned long offset);
  199. unsigned long find_next_zero_bit(const unsigned long *addr,
  200. unsigned long size,
  201. unsigned long offset);
  202. /**
  203. * __set_bit - Set a bit in memory
  204. * @nr: the bit to set
  205. * @addr: the address to start counting from
  206. *
  207. * This function is non-atomic and may be reordered.
  208. */
  209. static inline void __set_bit(int nr, volatile unsigned long *addr)
  210. {
  211. unsigned long mask = BIT_MASK(nr);
  212. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  213. *p |= mask;
  214. }
  215. /**
  216. * __clear_bit - Clear a bit in memory
  217. * @nr: the bit to clear
  218. * @addr: the address to start counting from
  219. *
  220. * This function is non-atomic and may be reordered.
  221. */
  222. static inline void __clear_bit(int nr, volatile unsigned long *addr)
  223. {
  224. unsigned long mask = BIT_MASK(nr);
  225. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  226. *p &= ~mask;
  227. }
  228. /**
  229. * __change_bit - Toggle a bit in memory
  230. * @nr: the bit to change
  231. * @addr: the address to start counting from
  232. *
  233. * This function is non-atomic and may be reordered.
  234. */
  235. static inline void __change_bit(int nr, volatile unsigned long *addr)
  236. {
  237. unsigned long mask = BIT_MASK(nr);
  238. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  239. *p ^= mask;
  240. }
  241. /**
  242. * __test_and_set_bit - Set a bit and return its old value
  243. * @nr: Bit to set
  244. * @addr: Address to count from
  245. *
  246. * This operation is non-atomic and can be reordered.
  247. */
  248. static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
  249. {
  250. unsigned long mask = BIT_MASK(nr);
  251. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  252. unsigned long old = *p;
  253. *p = old | mask;
  254. return (old & mask) != 0;
  255. }
  256. /**
  257. * __test_and_clear_bit - Clear a bit and return its old value
  258. * @nr: Bit to clear
  259. * @addr: Address to count from
  260. *
  261. * This operation is non-atomic and can be reordered.
  262. */
  263. static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
  264. {
  265. unsigned long mask = BIT_MASK(nr);
  266. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  267. unsigned long old = *p;
  268. *p = old & ~mask;
  269. return (old & mask) != 0;
  270. }
  271. /**
  272. * __test_bit - Determine whether a bit is set
  273. * @nr: bit number to test
  274. * @addr: Address to start counting from
  275. *
  276. * This operation is non-atomic and can be reordered.
  277. */
  278. static inline int __test_bit(int nr, const volatile unsigned long *addr)
  279. {
  280. return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
  281. }
  282. #endif