bitops.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Bit operations for the Hexagon architecture
  4. *
  5. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  6. */
  7. #ifndef _ASM_BITOPS_H
  8. #define _ASM_BITOPS_H
  9. #include <linux/compiler.h>
  10. #include <asm/byteorder.h>
  11. #include <asm/atomic.h>
  12. #include <asm/barrier.h>
  13. #ifdef __KERNEL__
  14. /*
  15. * The offset calculations for these are based on BITS_PER_LONG == 32
  16. * (i.e. I get to shift by #5-2 (32 bits per long, 4 bytes per access),
  17. * mask by 0x0000001F)
  18. *
  19. * Typically, R10 is clobbered for address, R11 bit nr, and R12 is temp
  20. */
  21. /**
  22. * test_and_clear_bit - clear a bit and return its old value
  23. * @nr: bit number to clear
  24. * @addr: pointer to memory
  25. */
  26. static inline int test_and_clear_bit(int nr, volatile void *addr)
  27. {
  28. int oldval;
  29. __asm__ __volatile__ (
  30. " {R10 = %1; R11 = asr(%2,#5); }\n"
  31. " {R10 += asl(R11,#2); R11 = and(%2,#0x1f)}\n"
  32. "1: R12 = memw_locked(R10);\n"
  33. " { P0 = tstbit(R12,R11); R12 = clrbit(R12,R11); }\n"
  34. " memw_locked(R10,P1) = R12;\n"
  35. " {if (!P1) jump 1b; %0 = mux(P0,#1,#0);}\n"
  36. : "=&r" (oldval)
  37. : "r" (addr), "r" (nr)
  38. : "r10", "r11", "r12", "p0", "p1", "memory"
  39. );
  40. return oldval;
  41. }
  42. /**
  43. * test_and_set_bit - set a bit and return its old value
  44. * @nr: bit number to set
  45. * @addr: pointer to memory
  46. */
  47. static inline int test_and_set_bit(int nr, volatile void *addr)
  48. {
  49. int oldval;
  50. __asm__ __volatile__ (
  51. " {R10 = %1; R11 = asr(%2,#5); }\n"
  52. " {R10 += asl(R11,#2); R11 = and(%2,#0x1f)}\n"
  53. "1: R12 = memw_locked(R10);\n"
  54. " { P0 = tstbit(R12,R11); R12 = setbit(R12,R11); }\n"
  55. " memw_locked(R10,P1) = R12;\n"
  56. " {if (!P1) jump 1b; %0 = mux(P0,#1,#0);}\n"
  57. : "=&r" (oldval)
  58. : "r" (addr), "r" (nr)
  59. : "r10", "r11", "r12", "p0", "p1", "memory"
  60. );
  61. return oldval;
  62. }
  63. /**
  64. * test_and_change_bit - toggle a bit and return its old value
  65. * @nr: bit number to set
  66. * @addr: pointer to memory
  67. */
  68. static inline int test_and_change_bit(int nr, volatile void *addr)
  69. {
  70. int oldval;
  71. __asm__ __volatile__ (
  72. " {R10 = %1; R11 = asr(%2,#5); }\n"
  73. " {R10 += asl(R11,#2); R11 = and(%2,#0x1f)}\n"
  74. "1: R12 = memw_locked(R10);\n"
  75. " { P0 = tstbit(R12,R11); R12 = togglebit(R12,R11); }\n"
  76. " memw_locked(R10,P1) = R12;\n"
  77. " {if (!P1) jump 1b; %0 = mux(P0,#1,#0);}\n"
  78. : "=&r" (oldval)
  79. : "r" (addr), "r" (nr)
  80. : "r10", "r11", "r12", "p0", "p1", "memory"
  81. );
  82. return oldval;
  83. }
  84. /*
  85. * Atomic, but doesn't care about the return value.
  86. * Rewrite later to save a cycle or two.
  87. */
  88. static inline void clear_bit(int nr, volatile void *addr)
  89. {
  90. test_and_clear_bit(nr, addr);
  91. }
  92. static inline void set_bit(int nr, volatile void *addr)
  93. {
  94. test_and_set_bit(nr, addr);
  95. }
  96. static inline void change_bit(int nr, volatile void *addr)
  97. {
  98. test_and_change_bit(nr, addr);
  99. }
  100. /*
  101. * These are allowed to be non-atomic. In fact the generic flavors are
  102. * in non-atomic.h. Would it be better to use intrinsics for this?
  103. *
  104. * OK, writes in our architecture do not invalidate LL/SC, so this has to
  105. * be atomic, particularly for things like slab_lock and slab_unlock.
  106. *
  107. */
  108. static __always_inline void
  109. arch___clear_bit(unsigned long nr, volatile unsigned long *addr)
  110. {
  111. test_and_clear_bit(nr, addr);
  112. }
  113. static __always_inline void
  114. arch___set_bit(unsigned long nr, volatile unsigned long *addr)
  115. {
  116. test_and_set_bit(nr, addr);
  117. }
  118. static __always_inline void
  119. arch___change_bit(unsigned long nr, volatile unsigned long *addr)
  120. {
  121. test_and_change_bit(nr, addr);
  122. }
  123. /* Apparently, at least some of these are allowed to be non-atomic */
  124. static __always_inline bool
  125. arch___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
  126. {
  127. return test_and_clear_bit(nr, addr);
  128. }
  129. static __always_inline bool
  130. arch___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
  131. {
  132. return test_and_set_bit(nr, addr);
  133. }
  134. static __always_inline bool
  135. arch___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
  136. {
  137. return test_and_change_bit(nr, addr);
  138. }
  139. static __always_inline bool
  140. arch_test_bit(unsigned long nr, const volatile unsigned long *addr)
  141. {
  142. int retval;
  143. asm volatile(
  144. "{P0 = tstbit(%1,%2); if (P0.new) %0 = #1; if (!P0.new) %0 = #0;}\n"
  145. : "=&r" (retval)
  146. : "r" (addr[BIT_WORD(nr)]), "r" (nr % BITS_PER_LONG)
  147. : "p0"
  148. );
  149. return retval;
  150. }
  151. static __always_inline bool
  152. arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)
  153. {
  154. int retval;
  155. asm volatile(
  156. "{P0 = tstbit(%1,%2); if (P0.new) %0 = #1; if (!P0.new) %0 = #0;}\n"
  157. : "=&r" (retval)
  158. : "r" (addr[BIT_WORD(nr)]), "r" (nr % BITS_PER_LONG)
  159. : "p0", "memory"
  160. );
  161. return retval;
  162. }
  163. /*
  164. * ffz - find first zero in word.
  165. * @word: The word to search
  166. *
  167. * Undefined if no zero exists, so code should check against ~0UL first.
  168. */
  169. static inline long ffz(int x)
  170. {
  171. int r;
  172. asm("%0 = ct1(%1);\n"
  173. : "=&r" (r)
  174. : "r" (x));
  175. return r;
  176. }
  177. /*
  178. * fls - find last (most-significant) bit set
  179. * @x: the word to search
  180. *
  181. * This is defined the same way as ffs.
  182. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  183. */
  184. static inline int fls(unsigned int x)
  185. {
  186. int r;
  187. asm("{ %0 = cl0(%1);}\n"
  188. "%0 = sub(#32,%0);\n"
  189. : "=&r" (r)
  190. : "r" (x)
  191. : "p0");
  192. return r;
  193. }
  194. /*
  195. * ffs - find first bit set
  196. * @x: the word to search
  197. *
  198. * This is defined the same way as
  199. * the libc and compiler builtin ffs routines, therefore
  200. * differs in spirit from the above ffz (man ffs).
  201. */
  202. static inline int ffs(int x)
  203. {
  204. int r;
  205. asm("{ P0 = cmp.eq(%1,#0); %0 = ct0(%1);}\n"
  206. "{ if (P0) %0 = #0; if (!P0) %0 = add(%0,#1);}\n"
  207. : "=&r" (r)
  208. : "r" (x)
  209. : "p0");
  210. return r;
  211. }
  212. /*
  213. * __ffs - find first bit in word.
  214. * @word: The word to search
  215. *
  216. * Undefined if no bit exists, so code should check against 0 first.
  217. *
  218. * bits_per_long assumed to be 32
  219. * numbering starts at 0 I think (instead of 1 like ffs)
  220. */
  221. static inline unsigned long __ffs(unsigned long word)
  222. {
  223. int num;
  224. asm("%0 = ct0(%1);\n"
  225. : "=&r" (num)
  226. : "r" (word));
  227. return num;
  228. }
  229. /*
  230. * __fls - find last (most-significant) set bit in a long word
  231. * @word: the word to search
  232. *
  233. * Undefined if no set bit exists, so code should check against 0 first.
  234. * bits_per_long assumed to be 32
  235. */
  236. static inline unsigned long __fls(unsigned long word)
  237. {
  238. int num;
  239. asm("%0 = cl0(%1);\n"
  240. "%0 = sub(#31,%0);\n"
  241. : "=&r" (num)
  242. : "r" (word));
  243. return num;
  244. }
  245. #include <asm-generic/bitops/lock.h>
  246. #include <asm-generic/bitops/non-instrumented-non-atomic.h>
  247. #include <asm-generic/bitops/fls64.h>
  248. #include <asm-generic/bitops/sched.h>
  249. #include <asm-generic/bitops/hweight.h>
  250. #include <asm-generic/bitops/le.h>
  251. #include <asm-generic/bitops/ext2-atomic.h>
  252. #endif /* __KERNEL__ */
  253. #endif