bitops.h 6.7 KB

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