bitops.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * PowerPC atomic bit operations.
  4. *
  5. * Merged version by David Gibson <[email protected]>.
  6. * Based on ppc64 versions by: Dave Engebretsen, Todd Inglett, Don
  7. * Reed, Pat McCarthy, Peter Bergner, Anton Blanchard. They
  8. * originally took it from the ppc32 code.
  9. *
  10. * Within a word, bits are numbered LSB first. Lot's of places make
  11. * this assumption by directly testing bits with (val & (1<<nr)).
  12. * This can cause confusion for large (> 1 word) bitmaps on a
  13. * big-endian system because, unlike little endian, the number of each
  14. * bit depends on the word size.
  15. *
  16. * The bitop functions are defined to work on unsigned longs, so for a
  17. * ppc64 system the bits end up numbered:
  18. * |63..............0|127............64|191...........128|255...........192|
  19. * and on ppc32:
  20. * |31.....0|63....32|95....64|127...96|159..128|191..160|223..192|255..224|
  21. *
  22. * There are a few little-endian macros used mostly for filesystem
  23. * bitmaps, these work on similar bit arrays layouts, but
  24. * byte-oriented:
  25. * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
  26. *
  27. * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit
  28. * number field needs to be reversed compared to the big-endian bit
  29. * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).
  30. */
  31. #ifndef _ASM_POWERPC_BITOPS_H
  32. #define _ASM_POWERPC_BITOPS_H
  33. #ifdef __KERNEL__
  34. #ifndef _LINUX_BITOPS_H
  35. #error only <linux/bitops.h> can be included directly
  36. #endif
  37. #include <linux/compiler.h>
  38. #include <asm/asm-compat.h>
  39. #include <asm/synch.h>
  40. /* PPC bit number conversion */
  41. #define PPC_BITLSHIFT(be) (BITS_PER_LONG - 1 - (be))
  42. #define PPC_BIT(bit) (1UL << PPC_BITLSHIFT(bit))
  43. #define PPC_BITMASK(bs, be) ((PPC_BIT(bs) - PPC_BIT(be)) | PPC_BIT(bs))
  44. /* Put a PPC bit into a "normal" bit position */
  45. #define PPC_BITEXTRACT(bits, ppc_bit, dst_bit) \
  46. ((((bits) >> PPC_BITLSHIFT(ppc_bit)) & 1) << (dst_bit))
  47. #define PPC_BITLSHIFT32(be) (32 - 1 - (be))
  48. #define PPC_BIT32(bit) (1UL << PPC_BITLSHIFT32(bit))
  49. #define PPC_BITMASK32(bs, be) ((PPC_BIT32(bs) - PPC_BIT32(be))|PPC_BIT32(bs))
  50. #define PPC_BITLSHIFT8(be) (8 - 1 - (be))
  51. #define PPC_BIT8(bit) (1UL << PPC_BITLSHIFT8(bit))
  52. #define PPC_BITMASK8(bs, be) ((PPC_BIT8(bs) - PPC_BIT8(be))|PPC_BIT8(bs))
  53. #include <asm/barrier.h>
  54. /* Macro for generating the ***_bits() functions */
  55. #define DEFINE_BITOP(fn, op, prefix) \
  56. static inline void fn(unsigned long mask, \
  57. volatile unsigned long *_p) \
  58. { \
  59. unsigned long old; \
  60. unsigned long *p = (unsigned long *)_p; \
  61. __asm__ __volatile__ ( \
  62. prefix \
  63. "1:" PPC_LLARX "%0,0,%3,0\n" \
  64. #op "%I2 %0,%0,%2\n" \
  65. PPC_STLCX "%0,0,%3\n" \
  66. "bne- 1b\n" \
  67. : "=&r" (old), "+m" (*p) \
  68. : "rK" (mask), "r" (p) \
  69. : "cc", "memory"); \
  70. }
  71. DEFINE_BITOP(set_bits, or, "")
  72. DEFINE_BITOP(change_bits, xor, "")
  73. static __always_inline bool is_rlwinm_mask_valid(unsigned long x)
  74. {
  75. if (!x)
  76. return false;
  77. if (x & 1)
  78. x = ~x; // make the mask non-wrapping
  79. x += x & -x; // adding the low set bit results in at most one bit set
  80. return !(x & (x - 1));
  81. }
  82. #define DEFINE_CLROP(fn, prefix) \
  83. static inline void fn(unsigned long mask, volatile unsigned long *_p) \
  84. { \
  85. unsigned long old; \
  86. unsigned long *p = (unsigned long *)_p; \
  87. \
  88. if (IS_ENABLED(CONFIG_PPC32) && \
  89. __builtin_constant_p(mask) && is_rlwinm_mask_valid(~mask)) {\
  90. asm volatile ( \
  91. prefix \
  92. "1:" "lwarx %0,0,%3\n" \
  93. "rlwinm %0,%0,0,%2\n" \
  94. "stwcx. %0,0,%3\n" \
  95. "bne- 1b\n" \
  96. : "=&r" (old), "+m" (*p) \
  97. : "n" (~mask), "r" (p) \
  98. : "cc", "memory"); \
  99. } else { \
  100. asm volatile ( \
  101. prefix \
  102. "1:" PPC_LLARX "%0,0,%3,0\n" \
  103. "andc %0,%0,%2\n" \
  104. PPC_STLCX "%0,0,%3\n" \
  105. "bne- 1b\n" \
  106. : "=&r" (old), "+m" (*p) \
  107. : "r" (mask), "r" (p) \
  108. : "cc", "memory"); \
  109. } \
  110. }
  111. DEFINE_CLROP(clear_bits, "")
  112. DEFINE_CLROP(clear_bits_unlock, PPC_RELEASE_BARRIER)
  113. static inline void arch_set_bit(int nr, volatile unsigned long *addr)
  114. {
  115. set_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
  116. }
  117. static inline void arch_clear_bit(int nr, volatile unsigned long *addr)
  118. {
  119. clear_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
  120. }
  121. static inline void arch_clear_bit_unlock(int nr, volatile unsigned long *addr)
  122. {
  123. clear_bits_unlock(BIT_MASK(nr), addr + BIT_WORD(nr));
  124. }
  125. static inline void arch_change_bit(int nr, volatile unsigned long *addr)
  126. {
  127. change_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
  128. }
  129. /* Like DEFINE_BITOP(), with changes to the arguments to 'op' and the output
  130. * operands. */
  131. #define DEFINE_TESTOP(fn, op, prefix, postfix, eh) \
  132. static inline unsigned long fn( \
  133. unsigned long mask, \
  134. volatile unsigned long *_p) \
  135. { \
  136. unsigned long old, t; \
  137. unsigned long *p = (unsigned long *)_p; \
  138. __asm__ __volatile__ ( \
  139. prefix \
  140. "1:" PPC_LLARX "%0,0,%3,%4\n" \
  141. #op "%I2 %1,%0,%2\n" \
  142. PPC_STLCX "%1,0,%3\n" \
  143. "bne- 1b\n" \
  144. postfix \
  145. : "=&r" (old), "=&r" (t) \
  146. : "rK" (mask), "r" (p), "n" (eh) \
  147. : "cc", "memory"); \
  148. return (old & mask); \
  149. }
  150. DEFINE_TESTOP(test_and_set_bits, or, PPC_ATOMIC_ENTRY_BARRIER,
  151. PPC_ATOMIC_EXIT_BARRIER, 0)
  152. DEFINE_TESTOP(test_and_set_bits_lock, or, "",
  153. PPC_ACQUIRE_BARRIER, IS_ENABLED(CONFIG_PPC64))
  154. DEFINE_TESTOP(test_and_change_bits, xor, PPC_ATOMIC_ENTRY_BARRIER,
  155. PPC_ATOMIC_EXIT_BARRIER, 0)
  156. static inline unsigned long test_and_clear_bits(unsigned long mask, volatile unsigned long *_p)
  157. {
  158. unsigned long old, t;
  159. unsigned long *p = (unsigned long *)_p;
  160. if (IS_ENABLED(CONFIG_PPC32) &&
  161. __builtin_constant_p(mask) && is_rlwinm_mask_valid(~mask)) {
  162. asm volatile (
  163. PPC_ATOMIC_ENTRY_BARRIER
  164. "1:" "lwarx %0,0,%3\n"
  165. "rlwinm %1,%0,0,%2\n"
  166. "stwcx. %1,0,%3\n"
  167. "bne- 1b\n"
  168. PPC_ATOMIC_EXIT_BARRIER
  169. : "=&r" (old), "=&r" (t)
  170. : "n" (~mask), "r" (p)
  171. : "cc", "memory");
  172. } else {
  173. asm volatile (
  174. PPC_ATOMIC_ENTRY_BARRIER
  175. "1:" PPC_LLARX "%0,0,%3,0\n"
  176. "andc %1,%0,%2\n"
  177. PPC_STLCX "%1,0,%3\n"
  178. "bne- 1b\n"
  179. PPC_ATOMIC_EXIT_BARRIER
  180. : "=&r" (old), "=&r" (t)
  181. : "r" (mask), "r" (p)
  182. : "cc", "memory");
  183. }
  184. return (old & mask);
  185. }
  186. static inline int arch_test_and_set_bit(unsigned long nr,
  187. volatile unsigned long *addr)
  188. {
  189. return test_and_set_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0;
  190. }
  191. static inline int arch_test_and_set_bit_lock(unsigned long nr,
  192. volatile unsigned long *addr)
  193. {
  194. return test_and_set_bits_lock(BIT_MASK(nr),
  195. addr + BIT_WORD(nr)) != 0;
  196. }
  197. static inline int arch_test_and_clear_bit(unsigned long nr,
  198. volatile unsigned long *addr)
  199. {
  200. return test_and_clear_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0;
  201. }
  202. static inline int arch_test_and_change_bit(unsigned long nr,
  203. volatile unsigned long *addr)
  204. {
  205. return test_and_change_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0;
  206. }
  207. #ifdef CONFIG_PPC64
  208. static inline unsigned long
  209. clear_bit_unlock_return_word(int nr, volatile unsigned long *addr)
  210. {
  211. unsigned long old, t;
  212. unsigned long *p = (unsigned long *)addr + BIT_WORD(nr);
  213. unsigned long mask = BIT_MASK(nr);
  214. __asm__ __volatile__ (
  215. PPC_RELEASE_BARRIER
  216. "1:" PPC_LLARX "%0,0,%3,0\n"
  217. "andc %1,%0,%2\n"
  218. PPC_STLCX "%1,0,%3\n"
  219. "bne- 1b\n"
  220. : "=&r" (old), "=&r" (t)
  221. : "r" (mask), "r" (p)
  222. : "cc", "memory");
  223. return old;
  224. }
  225. /*
  226. * This is a special function for mm/filemap.c
  227. * Bit 7 corresponds to PG_waiters.
  228. */
  229. #define arch_clear_bit_unlock_is_negative_byte(nr, addr) \
  230. (clear_bit_unlock_return_word(nr, addr) & BIT_MASK(7))
  231. #endif /* CONFIG_PPC64 */
  232. #include <asm-generic/bitops/non-atomic.h>
  233. static inline void arch___clear_bit_unlock(int nr, volatile unsigned long *addr)
  234. {
  235. __asm__ __volatile__(PPC_RELEASE_BARRIER "" ::: "memory");
  236. __clear_bit(nr, addr);
  237. }
  238. /*
  239. * Return the zero-based bit position (LE, not IBM bit numbering) of
  240. * the most significant 1-bit in a double word.
  241. */
  242. #define __ilog2(x) ilog2(x)
  243. #include <asm-generic/bitops/ffz.h>
  244. #include <asm-generic/bitops/builtin-__ffs.h>
  245. #include <asm-generic/bitops/builtin-ffs.h>
  246. /*
  247. * fls: find last (most-significant) bit set.
  248. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  249. */
  250. static __always_inline int fls(unsigned int x)
  251. {
  252. int lz;
  253. if (__builtin_constant_p(x))
  254. return x ? 32 - __builtin_clz(x) : 0;
  255. asm("cntlzw %0,%1" : "=r" (lz) : "r" (x));
  256. return 32 - lz;
  257. }
  258. #include <asm-generic/bitops/builtin-__fls.h>
  259. /*
  260. * 64-bit can do this using one cntlzd (count leading zeroes doubleword)
  261. * instruction; for 32-bit we use the generic version, which does two
  262. * 32-bit fls calls.
  263. */
  264. #ifdef CONFIG_PPC64
  265. static __always_inline int fls64(__u64 x)
  266. {
  267. int lz;
  268. if (__builtin_constant_p(x))
  269. return x ? 64 - __builtin_clzll(x) : 0;
  270. asm("cntlzd %0,%1" : "=r" (lz) : "r" (x));
  271. return 64 - lz;
  272. }
  273. #else
  274. #include <asm-generic/bitops/fls64.h>
  275. #endif
  276. #ifdef CONFIG_PPC64
  277. unsigned int __arch_hweight8(unsigned int w);
  278. unsigned int __arch_hweight16(unsigned int w);
  279. unsigned int __arch_hweight32(unsigned int w);
  280. unsigned long __arch_hweight64(__u64 w);
  281. #include <asm-generic/bitops/const_hweight.h>
  282. #else
  283. #include <asm-generic/bitops/hweight.h>
  284. #endif
  285. /* wrappers that deal with KASAN instrumentation */
  286. #include <asm-generic/bitops/instrumented-atomic.h>
  287. #include <asm-generic/bitops/instrumented-lock.h>
  288. /* Little-endian versions */
  289. #include <asm-generic/bitops/le.h>
  290. /* Bitmap functions for the ext2 filesystem */
  291. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  292. #include <asm-generic/bitops/sched.h>
  293. #endif /* __KERNEL__ */
  294. #endif /* _ASM_POWERPC_BITOPS_H */