bitops.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 1994 - 1997, 99, 2000, 06, 07 Ralf Baechle ([email protected])
  7. * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
  8. */
  9. #ifndef _ASM_BITOPS_H
  10. #define _ASM_BITOPS_H
  11. #ifndef _LINUX_BITOPS_H
  12. #error only <linux/bitops.h> can be included directly
  13. #endif
  14. #include <linux/bits.h>
  15. #include <linux/compiler.h>
  16. #include <linux/types.h>
  17. #include <asm/asm.h>
  18. #include <asm/barrier.h>
  19. #include <asm/byteorder.h> /* sigh ... */
  20. #include <asm/compiler.h>
  21. #include <asm/cpu-features.h>
  22. #include <asm/sgidefs.h>
  23. #define __bit_op(mem, insn, inputs...) do { \
  24. unsigned long __temp; \
  25. \
  26. asm volatile( \
  27. " .set push \n" \
  28. " .set " MIPS_ISA_LEVEL " \n" \
  29. " " __SYNC(full, loongson3_war) " \n" \
  30. "1: " __stringify(LONG_LL) " %0, %1 \n" \
  31. " " insn " \n" \
  32. " " __stringify(LONG_SC) " %0, %1 \n" \
  33. " " __stringify(SC_BEQZ) " %0, 1b \n" \
  34. " .set pop \n" \
  35. : "=&r"(__temp), "+" GCC_OFF_SMALL_ASM()(mem) \
  36. : inputs \
  37. : __LLSC_CLOBBER); \
  38. } while (0)
  39. #define __test_bit_op(mem, ll_dst, insn, inputs...) ({ \
  40. unsigned long __orig, __temp; \
  41. \
  42. asm volatile( \
  43. " .set push \n" \
  44. " .set " MIPS_ISA_LEVEL " \n" \
  45. " " __SYNC(full, loongson3_war) " \n" \
  46. "1: " __stringify(LONG_LL) " " ll_dst ", %2\n" \
  47. " " insn " \n" \
  48. " " __stringify(LONG_SC) " %1, %2 \n" \
  49. " " __stringify(SC_BEQZ) " %1, 1b \n" \
  50. " .set pop \n" \
  51. : "=&r"(__orig), "=&r"(__temp), \
  52. "+" GCC_OFF_SMALL_ASM()(mem) \
  53. : inputs \
  54. : __LLSC_CLOBBER); \
  55. \
  56. __orig; \
  57. })
  58. /*
  59. * These are the "slower" versions of the functions and are in bitops.c.
  60. * These functions call raw_local_irq_{save,restore}().
  61. */
  62. void __mips_set_bit(unsigned long nr, volatile unsigned long *addr);
  63. void __mips_clear_bit(unsigned long nr, volatile unsigned long *addr);
  64. void __mips_change_bit(unsigned long nr, volatile unsigned long *addr);
  65. int __mips_test_and_set_bit_lock(unsigned long nr,
  66. volatile unsigned long *addr);
  67. int __mips_test_and_clear_bit(unsigned long nr,
  68. volatile unsigned long *addr);
  69. int __mips_test_and_change_bit(unsigned long nr,
  70. volatile unsigned long *addr);
  71. /*
  72. * set_bit - Atomically set a bit in memory
  73. * @nr: the bit to set
  74. * @addr: the address to start counting from
  75. *
  76. * This function is atomic and may not be reordered. See __set_bit()
  77. * if you do not require the atomic guarantees.
  78. * Note that @nr may be almost arbitrarily large; this function is not
  79. * restricted to acting on a single-word quantity.
  80. */
  81. static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
  82. {
  83. volatile unsigned long *m = &addr[BIT_WORD(nr)];
  84. int bit = nr % BITS_PER_LONG;
  85. if (!kernel_uses_llsc) {
  86. __mips_set_bit(nr, addr);
  87. return;
  88. }
  89. if ((MIPS_ISA_REV >= 2) && __builtin_constant_p(bit) && (bit >= 16)) {
  90. __bit_op(*m, __stringify(LONG_INS) " %0, %3, %2, 1", "i"(bit), "r"(~0));
  91. return;
  92. }
  93. __bit_op(*m, "or\t%0, %2", "ir"(BIT(bit)));
  94. }
  95. /*
  96. * clear_bit - Clears a bit in memory
  97. * @nr: Bit to clear
  98. * @addr: Address to start counting from
  99. *
  100. * clear_bit() is atomic and may not be reordered. However, it does
  101. * not contain a memory barrier, so if it is used for locking purposes,
  102. * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
  103. * in order to ensure changes are visible on other processors.
  104. */
  105. static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
  106. {
  107. volatile unsigned long *m = &addr[BIT_WORD(nr)];
  108. int bit = nr % BITS_PER_LONG;
  109. if (!kernel_uses_llsc) {
  110. __mips_clear_bit(nr, addr);
  111. return;
  112. }
  113. if ((MIPS_ISA_REV >= 2) && __builtin_constant_p(bit)) {
  114. __bit_op(*m, __stringify(LONG_INS) " %0, $0, %2, 1", "i"(bit));
  115. return;
  116. }
  117. __bit_op(*m, "and\t%0, %2", "ir"(~BIT(bit)));
  118. }
  119. /*
  120. * clear_bit_unlock - Clears a bit in memory
  121. * @nr: Bit to clear
  122. * @addr: Address to start counting from
  123. *
  124. * clear_bit() is atomic and implies release semantics before the memory
  125. * operation. It can be used for an unlock.
  126. */
  127. static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
  128. {
  129. smp_mb__before_atomic();
  130. clear_bit(nr, addr);
  131. }
  132. /*
  133. * change_bit - Toggle a bit in memory
  134. * @nr: Bit to change
  135. * @addr: Address to start counting from
  136. *
  137. * change_bit() is atomic and may not be reordered.
  138. * Note that @nr may be almost arbitrarily large; this function is not
  139. * restricted to acting on a single-word quantity.
  140. */
  141. static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
  142. {
  143. volatile unsigned long *m = &addr[BIT_WORD(nr)];
  144. int bit = nr % BITS_PER_LONG;
  145. if (!kernel_uses_llsc) {
  146. __mips_change_bit(nr, addr);
  147. return;
  148. }
  149. __bit_op(*m, "xor\t%0, %2", "ir"(BIT(bit)));
  150. }
  151. /*
  152. * test_and_set_bit_lock - Set a bit and return its old value
  153. * @nr: Bit to set
  154. * @addr: Address to count from
  155. *
  156. * This operation is atomic and implies acquire ordering semantics
  157. * after the memory operation.
  158. */
  159. static inline int test_and_set_bit_lock(unsigned long nr,
  160. volatile unsigned long *addr)
  161. {
  162. volatile unsigned long *m = &addr[BIT_WORD(nr)];
  163. int bit = nr % BITS_PER_LONG;
  164. unsigned long res, orig;
  165. if (!kernel_uses_llsc) {
  166. res = __mips_test_and_set_bit_lock(nr, addr);
  167. } else {
  168. orig = __test_bit_op(*m, "%0",
  169. "or\t%1, %0, %3",
  170. "ir"(BIT(bit)));
  171. res = (orig & BIT(bit)) != 0;
  172. }
  173. smp_llsc_mb();
  174. return res;
  175. }
  176. /*
  177. * test_and_set_bit - Set a bit and return its old value
  178. * @nr: Bit to set
  179. * @addr: Address to count from
  180. *
  181. * This operation is atomic and cannot be reordered.
  182. * It also implies a memory barrier.
  183. */
  184. static inline int test_and_set_bit(unsigned long nr,
  185. volatile unsigned long *addr)
  186. {
  187. smp_mb__before_atomic();
  188. return test_and_set_bit_lock(nr, addr);
  189. }
  190. /*
  191. * test_and_clear_bit - Clear a bit and return its old value
  192. * @nr: Bit to clear
  193. * @addr: Address to count from
  194. *
  195. * This operation is atomic and cannot be reordered.
  196. * It also implies a memory barrier.
  197. */
  198. static inline int test_and_clear_bit(unsigned long nr,
  199. volatile unsigned long *addr)
  200. {
  201. volatile unsigned long *m = &addr[BIT_WORD(nr)];
  202. int bit = nr % BITS_PER_LONG;
  203. unsigned long res, orig;
  204. smp_mb__before_atomic();
  205. if (!kernel_uses_llsc) {
  206. res = __mips_test_and_clear_bit(nr, addr);
  207. } else if ((MIPS_ISA_REV >= 2) && __builtin_constant_p(nr)) {
  208. res = __test_bit_op(*m, "%1",
  209. __stringify(LONG_EXT) " %0, %1, %3, 1;"
  210. __stringify(LONG_INS) " %1, $0, %3, 1",
  211. "i"(bit));
  212. } else {
  213. orig = __test_bit_op(*m, "%0",
  214. "or\t%1, %0, %3;"
  215. "xor\t%1, %1, %3",
  216. "ir"(BIT(bit)));
  217. res = (orig & BIT(bit)) != 0;
  218. }
  219. smp_llsc_mb();
  220. return res;
  221. }
  222. /*
  223. * test_and_change_bit - Change a bit and return its old value
  224. * @nr: Bit to change
  225. * @addr: Address to count from
  226. *
  227. * This operation is atomic and cannot be reordered.
  228. * It also implies a memory barrier.
  229. */
  230. static inline int test_and_change_bit(unsigned long nr,
  231. volatile unsigned long *addr)
  232. {
  233. volatile unsigned long *m = &addr[BIT_WORD(nr)];
  234. int bit = nr % BITS_PER_LONG;
  235. unsigned long res, orig;
  236. smp_mb__before_atomic();
  237. if (!kernel_uses_llsc) {
  238. res = __mips_test_and_change_bit(nr, addr);
  239. } else {
  240. orig = __test_bit_op(*m, "%0",
  241. "xor\t%1, %0, %3",
  242. "ir"(BIT(bit)));
  243. res = (orig & BIT(bit)) != 0;
  244. }
  245. smp_llsc_mb();
  246. return res;
  247. }
  248. #undef __bit_op
  249. #undef __test_bit_op
  250. #include <asm-generic/bitops/non-atomic.h>
  251. /*
  252. * __clear_bit_unlock - Clears a bit in memory
  253. * @nr: Bit to clear
  254. * @addr: Address to start counting from
  255. *
  256. * __clear_bit() is non-atomic and implies release semantics before the memory
  257. * operation. It can be used for an unlock if no other CPUs can concurrently
  258. * modify other bits in the word.
  259. */
  260. static inline void __clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
  261. {
  262. smp_mb__before_llsc();
  263. __clear_bit(nr, addr);
  264. nudge_writes();
  265. }
  266. /*
  267. * Return the bit position (0..63) of the most significant 1 bit in a word
  268. * Returns -1 if no 1 bit exists
  269. */
  270. static __always_inline unsigned long __fls(unsigned long word)
  271. {
  272. int num;
  273. if (BITS_PER_LONG == 32 && !__builtin_constant_p(word) &&
  274. __builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
  275. __asm__(
  276. " .set push \n"
  277. " .set "MIPS_ISA_LEVEL" \n"
  278. " clz %0, %1 \n"
  279. " .set pop \n"
  280. : "=r" (num)
  281. : "r" (word));
  282. return 31 - num;
  283. }
  284. if (BITS_PER_LONG == 64 && !__builtin_constant_p(word) &&
  285. __builtin_constant_p(cpu_has_mips64) && cpu_has_mips64) {
  286. __asm__(
  287. " .set push \n"
  288. " .set "MIPS_ISA_LEVEL" \n"
  289. " dclz %0, %1 \n"
  290. " .set pop \n"
  291. : "=r" (num)
  292. : "r" (word));
  293. return 63 - num;
  294. }
  295. num = BITS_PER_LONG - 1;
  296. #if BITS_PER_LONG == 64
  297. if (!(word & (~0ul << 32))) {
  298. num -= 32;
  299. word <<= 32;
  300. }
  301. #endif
  302. if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
  303. num -= 16;
  304. word <<= 16;
  305. }
  306. if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
  307. num -= 8;
  308. word <<= 8;
  309. }
  310. if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
  311. num -= 4;
  312. word <<= 4;
  313. }
  314. if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
  315. num -= 2;
  316. word <<= 2;
  317. }
  318. if (!(word & (~0ul << (BITS_PER_LONG-1))))
  319. num -= 1;
  320. return num;
  321. }
  322. /*
  323. * __ffs - find first bit in word.
  324. * @word: The word to search
  325. *
  326. * Returns 0..SZLONG-1
  327. * Undefined if no bit exists, so code should check against 0 first.
  328. */
  329. static __always_inline unsigned long __ffs(unsigned long word)
  330. {
  331. return __fls(word & -word);
  332. }
  333. /*
  334. * fls - find last bit set.
  335. * @word: The word to search
  336. *
  337. * This is defined the same way as ffs.
  338. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  339. */
  340. static inline int fls(unsigned int x)
  341. {
  342. int r;
  343. if (!__builtin_constant_p(x) &&
  344. __builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
  345. __asm__(
  346. " .set push \n"
  347. " .set "MIPS_ISA_LEVEL" \n"
  348. " clz %0, %1 \n"
  349. " .set pop \n"
  350. : "=r" (x)
  351. : "r" (x));
  352. return 32 - x;
  353. }
  354. r = 32;
  355. if (!x)
  356. return 0;
  357. if (!(x & 0xffff0000u)) {
  358. x <<= 16;
  359. r -= 16;
  360. }
  361. if (!(x & 0xff000000u)) {
  362. x <<= 8;
  363. r -= 8;
  364. }
  365. if (!(x & 0xf0000000u)) {
  366. x <<= 4;
  367. r -= 4;
  368. }
  369. if (!(x & 0xc0000000u)) {
  370. x <<= 2;
  371. r -= 2;
  372. }
  373. if (!(x & 0x80000000u)) {
  374. x <<= 1;
  375. r -= 1;
  376. }
  377. return r;
  378. }
  379. #include <asm-generic/bitops/fls64.h>
  380. /*
  381. * ffs - find first bit set.
  382. * @word: The word to search
  383. *
  384. * This is defined the same way as
  385. * the libc and compiler builtin ffs routines, therefore
  386. * differs in spirit from the below ffz (man ffs).
  387. */
  388. static inline int ffs(int word)
  389. {
  390. if (!word)
  391. return 0;
  392. return fls(word & -word);
  393. }
  394. #include <asm-generic/bitops/ffz.h>
  395. #ifdef __KERNEL__
  396. #include <asm-generic/bitops/sched.h>
  397. #include <asm/arch_hweight.h>
  398. #include <asm-generic/bitops/const_hweight.h>
  399. #include <asm-generic/bitops/le.h>
  400. #include <asm-generic/bitops/ext2-atomic.h>
  401. #endif /* __KERNEL__ */
  402. #endif /* _ASM_BITOPS_H */