bitops.h 6.7 KB

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