bitops.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #ifndef _ASM_RISCV_BITOPS_H
  6. #define _ASM_RISCV_BITOPS_H
  7. #ifndef _LINUX_BITOPS_H
  8. #error "Only <linux/bitops.h> can be included directly"
  9. #endif /* _LINUX_BITOPS_H */
  10. #include <linux/compiler.h>
  11. #include <linux/irqflags.h>
  12. #include <asm/barrier.h>
  13. #include <asm/bitsperlong.h>
  14. #if !defined(CONFIG_RISCV_ISA_ZBB) || defined(NO_ALTERNATIVE)
  15. #include <asm-generic/bitops/__ffs.h>
  16. #include <asm-generic/bitops/__fls.h>
  17. #include <asm-generic/bitops/ffs.h>
  18. #include <asm-generic/bitops/fls.h>
  19. #else
  20. #define __HAVE_ARCH___FFS
  21. #define __HAVE_ARCH___FLS
  22. #define __HAVE_ARCH_FFS
  23. #define __HAVE_ARCH_FLS
  24. #include <asm-generic/bitops/__ffs.h>
  25. #include <asm-generic/bitops/__fls.h>
  26. #include <asm-generic/bitops/ffs.h>
  27. #include <asm-generic/bitops/fls.h>
  28. #include <asm/alternative-macros.h>
  29. #include <asm/hwcap.h>
  30. #if (BITS_PER_LONG == 64)
  31. #define CTZW "ctzw "
  32. #define CLZW "clzw "
  33. #elif (BITS_PER_LONG == 32)
  34. #define CTZW "ctz "
  35. #define CLZW "clz "
  36. #else
  37. #error "Unexpected BITS_PER_LONG"
  38. #endif
  39. static __always_inline unsigned long variable__ffs(unsigned long word)
  40. {
  41. asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
  42. RISCV_ISA_EXT_ZBB, 1)
  43. : : : : legacy);
  44. asm volatile (".option push\n"
  45. ".option arch,+zbb\n"
  46. "ctz %0, %1\n"
  47. ".option pop\n"
  48. : "=r" (word) : "r" (word) :);
  49. return word;
  50. legacy:
  51. return generic___ffs(word);
  52. }
  53. /**
  54. * __ffs - find first set bit in a long word
  55. * @word: The word to search
  56. *
  57. * Undefined if no set bit exists, so code should check against 0 first.
  58. */
  59. #define __ffs(word) \
  60. (__builtin_constant_p(word) ? \
  61. (unsigned long)__builtin_ctzl(word) : \
  62. variable__ffs(word))
  63. static __always_inline unsigned long variable__fls(unsigned long word)
  64. {
  65. asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
  66. RISCV_ISA_EXT_ZBB, 1)
  67. : : : : legacy);
  68. asm volatile (".option push\n"
  69. ".option arch,+zbb\n"
  70. "clz %0, %1\n"
  71. ".option pop\n"
  72. : "=r" (word) : "r" (word) :);
  73. return BITS_PER_LONG - 1 - word;
  74. legacy:
  75. return generic___fls(word);
  76. }
  77. /**
  78. * __fls - find last set bit in a long word
  79. * @word: the word to search
  80. *
  81. * Undefined if no set bit exists, so code should check against 0 first.
  82. */
  83. #define __fls(word) \
  84. (__builtin_constant_p(word) ? \
  85. (unsigned long)(BITS_PER_LONG - 1 - __builtin_clzl(word)) : \
  86. variable__fls(word))
  87. static __always_inline int variable_ffs(int x)
  88. {
  89. asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
  90. RISCV_ISA_EXT_ZBB, 1)
  91. : : : : legacy);
  92. if (!x)
  93. return 0;
  94. asm volatile (".option push\n"
  95. ".option arch,+zbb\n"
  96. CTZW "%0, %1\n"
  97. ".option pop\n"
  98. : "=r" (x) : "r" (x) :);
  99. return x + 1;
  100. legacy:
  101. return generic_ffs(x);
  102. }
  103. /**
  104. * ffs - find first set bit in a word
  105. * @x: the word to search
  106. *
  107. * This is defined the same way as the libc and compiler builtin ffs routines.
  108. *
  109. * ffs(value) returns 0 if value is 0 or the position of the first set bit if
  110. * value is nonzero. The first (least significant) bit is at position 1.
  111. */
  112. #define ffs(x) (__builtin_constant_p(x) ? __builtin_ffs(x) : variable_ffs(x))
  113. static __always_inline int variable_fls(unsigned int x)
  114. {
  115. asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
  116. RISCV_ISA_EXT_ZBB, 1)
  117. : : : : legacy);
  118. if (!x)
  119. return 0;
  120. asm volatile (".option push\n"
  121. ".option arch,+zbb\n"
  122. CLZW "%0, %1\n"
  123. ".option pop\n"
  124. : "=r" (x) : "r" (x) :);
  125. return 32 - x;
  126. legacy:
  127. return generic_fls(x);
  128. }
  129. /**
  130. * fls - find last set bit in a word
  131. * @x: the word to search
  132. *
  133. * This is defined in a similar way as ffs, but returns the position of the most
  134. * significant set bit.
  135. *
  136. * fls(value) returns 0 if value is 0 or the position of the last set bit if
  137. * value is nonzero. The last (most significant) bit is at position 32.
  138. */
  139. #define fls(x) \
  140. ({ \
  141. typeof(x) x_ = (x); \
  142. __builtin_constant_p(x_) ? \
  143. ((x_ != 0) ? (32 - __builtin_clz(x_)) : 0) \
  144. : \
  145. variable_fls(x_); \
  146. })
  147. #endif /* !defined(CONFIG_RISCV_ISA_ZBB) || defined(NO_ALTERNATIVE) */
  148. #include <asm-generic/bitops/ffz.h>
  149. #include <asm-generic/bitops/fls64.h>
  150. #include <asm-generic/bitops/sched.h>
  151. #include <asm/arch_hweight.h>
  152. #include <asm-generic/bitops/const_hweight.h>
  153. #if (BITS_PER_LONG == 64)
  154. #define __AMO(op) "amo" #op ".d"
  155. #elif (BITS_PER_LONG == 32)
  156. #define __AMO(op) "amo" #op ".w"
  157. #else
  158. #error "Unexpected BITS_PER_LONG"
  159. #endif
  160. #define __test_and_op_bit_ord(op, mod, nr, addr, ord) \
  161. ({ \
  162. unsigned long __res, __mask; \
  163. __mask = BIT_MASK(nr); \
  164. __asm__ __volatile__ ( \
  165. __AMO(op) #ord " %0, %2, %1" \
  166. : "=r" (__res), "+A" (addr[BIT_WORD(nr)]) \
  167. : "r" (mod(__mask)) \
  168. : "memory"); \
  169. ((__res & __mask) != 0); \
  170. })
  171. #define __op_bit_ord(op, mod, nr, addr, ord) \
  172. __asm__ __volatile__ ( \
  173. __AMO(op) #ord " zero, %1, %0" \
  174. : "+A" (addr[BIT_WORD(nr)]) \
  175. : "r" (mod(BIT_MASK(nr))) \
  176. : "memory");
  177. #define __test_and_op_bit(op, mod, nr, addr) \
  178. __test_and_op_bit_ord(op, mod, nr, addr, .aqrl)
  179. #define __op_bit(op, mod, nr, addr) \
  180. __op_bit_ord(op, mod, nr, addr, )
  181. /* Bitmask modifiers */
  182. #define __NOP(x) (x)
  183. #define __NOT(x) (~(x))
  184. /**
  185. * arch_test_and_set_bit - Set a bit and return its old value
  186. * @nr: Bit to set
  187. * @addr: Address to count from
  188. *
  189. * This operation may be reordered on other architectures than x86.
  190. */
  191. static inline int arch_test_and_set_bit(int nr, volatile unsigned long *addr)
  192. {
  193. return __test_and_op_bit(or, __NOP, nr, addr);
  194. }
  195. /**
  196. * arch_test_and_clear_bit - Clear a bit and return its old value
  197. * @nr: Bit to clear
  198. * @addr: Address to count from
  199. *
  200. * This operation can be reordered on other architectures other than x86.
  201. */
  202. static inline int arch_test_and_clear_bit(int nr, volatile unsigned long *addr)
  203. {
  204. return __test_and_op_bit(and, __NOT, nr, addr);
  205. }
  206. /**
  207. * arch_test_and_change_bit - Change a bit and return its old value
  208. * @nr: Bit to change
  209. * @addr: Address to count from
  210. *
  211. * This operation is atomic and cannot be reordered.
  212. * It also implies a memory barrier.
  213. */
  214. static inline int arch_test_and_change_bit(int nr, volatile unsigned long *addr)
  215. {
  216. return __test_and_op_bit(xor, __NOP, nr, addr);
  217. }
  218. /**
  219. * arch_set_bit - Atomically set a bit in memory
  220. * @nr: the bit to set
  221. * @addr: the address to start counting from
  222. *
  223. * Note: there are no guarantees that this function will not be reordered
  224. * on non x86 architectures, so if you are writing portable code,
  225. * make sure not to rely on its reordering guarantees.
  226. *
  227. * Note that @nr may be almost arbitrarily large; this function is not
  228. * restricted to acting on a single-word quantity.
  229. */
  230. static inline void arch_set_bit(int nr, volatile unsigned long *addr)
  231. {
  232. __op_bit(or, __NOP, nr, addr);
  233. }
  234. /**
  235. * arch_clear_bit - Clears a bit in memory
  236. * @nr: Bit to clear
  237. * @addr: Address to start counting from
  238. *
  239. * Note: there are no guarantees that this function will not be reordered
  240. * on non x86 architectures, so if you are writing portable code,
  241. * make sure not to rely on its reordering guarantees.
  242. */
  243. static inline void arch_clear_bit(int nr, volatile unsigned long *addr)
  244. {
  245. __op_bit(and, __NOT, nr, addr);
  246. }
  247. /**
  248. * arch_change_bit - Toggle a bit in memory
  249. * @nr: Bit to change
  250. * @addr: Address to start counting from
  251. *
  252. * change_bit() may be reordered on other architectures than x86.
  253. * Note that @nr may be almost arbitrarily large; this function is not
  254. * restricted to acting on a single-word quantity.
  255. */
  256. static inline void arch_change_bit(int nr, volatile unsigned long *addr)
  257. {
  258. __op_bit(xor, __NOP, nr, addr);
  259. }
  260. /**
  261. * arch_test_and_set_bit_lock - Set a bit and return its old value, for lock
  262. * @nr: Bit to set
  263. * @addr: Address to count from
  264. *
  265. * This operation is atomic and provides acquire barrier semantics.
  266. * It can be used to implement bit locks.
  267. */
  268. static inline int arch_test_and_set_bit_lock(
  269. unsigned long nr, volatile unsigned long *addr)
  270. {
  271. return __test_and_op_bit_ord(or, __NOP, nr, addr, .aq);
  272. }
  273. /**
  274. * arch_clear_bit_unlock - Clear a bit in memory, for unlock
  275. * @nr: the bit to set
  276. * @addr: the address to start counting from
  277. *
  278. * This operation is atomic and provides release barrier semantics.
  279. */
  280. static inline void arch_clear_bit_unlock(
  281. unsigned long nr, volatile unsigned long *addr)
  282. {
  283. __op_bit_ord(and, __NOT, nr, addr, .rl);
  284. }
  285. /**
  286. * arch___clear_bit_unlock - Clear a bit in memory, for unlock
  287. * @nr: the bit to set
  288. * @addr: the address to start counting from
  289. *
  290. * This operation is like clear_bit_unlock, however it is not atomic.
  291. * It does provide release barrier semantics so it can be used to unlock
  292. * a bit lock, however it would only be used if no other CPU can modify
  293. * any bits in the memory until the lock is released (a good example is
  294. * if the bit lock itself protects access to the other bits in the word).
  295. *
  296. * On RISC-V systems there seems to be no benefit to taking advantage of the
  297. * non-atomic property here: it's a lot more instructions and we still have to
  298. * provide release semantics anyway.
  299. */
  300. static inline void arch___clear_bit_unlock(
  301. unsigned long nr, volatile unsigned long *addr)
  302. {
  303. arch_clear_bit_unlock(nr, addr);
  304. }
  305. static inline bool arch_xor_unlock_is_negative_byte(unsigned long mask,
  306. volatile unsigned long *addr)
  307. {
  308. unsigned long res;
  309. __asm__ __volatile__ (
  310. __AMO(xor) ".rl %0, %2, %1"
  311. : "=r" (res), "+A" (*addr)
  312. : "r" (__NOP(mask))
  313. : "memory");
  314. return (res & BIT(7)) != 0;
  315. }
  316. #undef __test_and_op_bit
  317. #undef __op_bit
  318. #undef __NOP
  319. #undef __NOT
  320. #undef __AMO
  321. #include <asm-generic/bitops/instrumented-atomic.h>
  322. #include <asm-generic/bitops/instrumented-lock.h>
  323. #include <asm-generic/bitops/non-atomic.h>
  324. #include <asm-generic/bitops/le.h>
  325. #include <asm-generic/bitops/ext2-atomic.h>
  326. #endif /* _ASM_RISCV_BITOPS_H */