bitops.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 (ralf@linux-mips.org)
  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/compiler.h>
  15. #include <linux/types.h>
  16. #include <asm/barrier.h>
  17. #include <asm/byteorder.h> /* sigh ... */
  18. #include <asm/cpu-features.h>
  19. #include <asm/sgidefs.h>
  20. #include <asm/war.h>
  21. #if _MIPS_SZLONG == 32
  22. #define SZLONG_LOG 5
  23. #define SZLONG_MASK 31UL
  24. #define __LL "ll "
  25. #define __SC "sc "
  26. #define __INS "ins "
  27. #define __EXT "ext "
  28. #elif _MIPS_SZLONG == 64
  29. #define SZLONG_LOG 6
  30. #define SZLONG_MASK 63UL
  31. #define __LL "lld "
  32. #define __SC "scd "
  33. #define __INS "dins "
  34. #define __EXT "dext "
  35. #endif
  36. /*
  37. * clear_bit() doesn't provide any barrier for the compiler.
  38. */
  39. #define smp_mb__before_clear_bit() smp_mb__before_llsc()
  40. #define smp_mb__after_clear_bit() smp_llsc_mb()
  41. /*
  42. * These are the "slower" versions of the functions and are in bitops.c.
  43. * These functions call raw_local_irq_{save,restore}().
  44. */
  45. void __mips_set_bit(unsigned long nr, volatile unsigned long *addr);
  46. void __mips_clear_bit(unsigned long nr, volatile unsigned long *addr);
  47. void __mips_change_bit(unsigned long nr, volatile unsigned long *addr);
  48. int __mips_test_and_set_bit(unsigned long nr,
  49. volatile unsigned long *addr);
  50. int __mips_test_and_set_bit_lock(unsigned long nr,
  51. volatile unsigned long *addr);
  52. int __mips_test_and_clear_bit(unsigned long nr,
  53. volatile unsigned long *addr);
  54. int __mips_test_and_change_bit(unsigned long nr,
  55. volatile unsigned long *addr);
  56. /*
  57. * set_bit - Atomically set a bit in memory
  58. * @nr: the bit to set
  59. * @addr: the address to start counting from
  60. *
  61. * This function is atomic and may not be reordered. See __set_bit()
  62. * if you do not require the atomic guarantees.
  63. * Note that @nr may be almost arbitrarily large; this function is not
  64. * restricted to acting on a single-word quantity.
  65. */
  66. static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
  67. {
  68. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  69. int bit = nr & SZLONG_MASK;
  70. unsigned long temp;
  71. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  72. __asm__ __volatile__(
  73. " .set mips3 \n"
  74. "1: " __LL "%0, %1 # set_bit \n"
  75. " or %0, %2 \n"
  76. " " __SC "%0, %1 \n"
  77. " beqzl %0, 1b \n"
  78. " .set mips0 \n"
  79. : "=&r" (temp), "=m" (*m)
  80. : "ir" (1UL << bit), "m" (*m));
  81. #ifdef CONFIG_CPU_MIPSR2
  82. } else if (kernel_uses_llsc && __builtin_constant_p(bit)) {
  83. do {
  84. __asm__ __volatile__(
  85. " " __LL "%0, %1 # set_bit \n"
  86. " " __INS "%0, %3, %2, 1 \n"
  87. " " __SC "%0, %1 \n"
  88. : "=&r" (temp), "+m" (*m)
  89. : "ir" (bit), "r" (~0));
  90. } while (unlikely(!temp));
  91. #endif /* CONFIG_CPU_MIPSR2 */
  92. } else if (kernel_uses_llsc) {
  93. do {
  94. __asm__ __volatile__(
  95. " .set mips3 \n"
  96. " " __LL "%0, %1 # set_bit \n"
  97. " or %0, %2 \n"
  98. " " __SC "%0, %1 \n"
  99. " .set mips0 \n"
  100. : "=&r" (temp), "+m" (*m)
  101. : "ir" (1UL << bit));
  102. } while (unlikely(!temp));
  103. } else
  104. __mips_set_bit(nr, addr);
  105. }
  106. /*
  107. * clear_bit - Clears a bit in memory
  108. * @nr: Bit to clear
  109. * @addr: Address to start counting from
  110. *
  111. * clear_bit() is atomic and may not be reordered. However, it does
  112. * not contain a memory barrier, so if it is used for locking purposes,
  113. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  114. * in order to ensure changes are visible on other processors.
  115. */
  116. static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
  117. {
  118. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  119. int bit = nr & SZLONG_MASK;
  120. unsigned long temp;
  121. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  122. __asm__ __volatile__(
  123. " .set mips3 \n"
  124. "1: " __LL "%0, %1 # clear_bit \n"
  125. " and %0, %2 \n"
  126. " " __SC "%0, %1 \n"
  127. " beqzl %0, 1b \n"
  128. " .set mips0 \n"
  129. : "=&r" (temp), "+m" (*m)
  130. : "ir" (~(1UL << bit)));
  131. #ifdef CONFIG_CPU_MIPSR2
  132. } else if (kernel_uses_llsc && __builtin_constant_p(bit)) {
  133. do {
  134. __asm__ __volatile__(
  135. " " __LL "%0, %1 # clear_bit \n"
  136. " " __INS "%0, $0, %2, 1 \n"
  137. " " __SC "%0, %1 \n"
  138. : "=&r" (temp), "+m" (*m)
  139. : "ir" (bit));
  140. } while (unlikely(!temp));
  141. #endif /* CONFIG_CPU_MIPSR2 */
  142. } else if (kernel_uses_llsc) {
  143. do {
  144. __asm__ __volatile__(
  145. " .set mips3 \n"
  146. " " __LL "%0, %1 # clear_bit \n"
  147. " and %0, %2 \n"
  148. " " __SC "%0, %1 \n"
  149. " .set mips0 \n"
  150. : "=&r" (temp), "+m" (*m)
  151. : "ir" (~(1UL << bit)));
  152. } while (unlikely(!temp));
  153. } else
  154. __mips_clear_bit(nr, addr);
  155. }
  156. /*
  157. * clear_bit_unlock - Clears a bit in memory
  158. * @nr: Bit to clear
  159. * @addr: Address to start counting from
  160. *
  161. * clear_bit() is atomic and implies release semantics before the memory
  162. * operation. It can be used for an unlock.
  163. */
  164. static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
  165. {
  166. smp_mb__before_clear_bit();
  167. clear_bit(nr, addr);
  168. }
  169. /*
  170. * change_bit - Toggle a bit in memory
  171. * @nr: Bit to change
  172. * @addr: Address to start counting from
  173. *
  174. * change_bit() is atomic and may not be reordered.
  175. * Note that @nr may be almost arbitrarily large; this function is not
  176. * restricted to acting on a single-word quantity.
  177. */
  178. static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
  179. {
  180. int bit = nr & SZLONG_MASK;
  181. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  182. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  183. unsigned long temp;
  184. __asm__ __volatile__(
  185. " .set mips3 \n"
  186. "1: " __LL "%0, %1 # change_bit \n"
  187. " xor %0, %2 \n"
  188. " " __SC "%0, %1 \n"
  189. " beqzl %0, 1b \n"
  190. " .set mips0 \n"
  191. : "=&r" (temp), "+m" (*m)
  192. : "ir" (1UL << bit));
  193. } else if (kernel_uses_llsc) {
  194. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  195. unsigned long temp;
  196. do {
  197. __asm__ __volatile__(
  198. " .set mips3 \n"
  199. " " __LL "%0, %1 # change_bit \n"
  200. " xor %0, %2 \n"
  201. " " __SC "%0, %1 \n"
  202. " .set mips0 \n"
  203. : "=&r" (temp), "+m" (*m)
  204. : "ir" (1UL << bit));
  205. } while (unlikely(!temp));
  206. } else
  207. __mips_change_bit(nr, addr);
  208. }
  209. /*
  210. * test_and_set_bit - Set a bit and return its old value
  211. * @nr: Bit to set
  212. * @addr: Address to count from
  213. *
  214. * This operation is atomic and cannot be reordered.
  215. * It also implies a memory barrier.
  216. */
  217. static inline int test_and_set_bit(unsigned long nr,
  218. volatile unsigned long *addr)
  219. {
  220. int bit = nr & SZLONG_MASK;
  221. unsigned long res;
  222. smp_mb__before_llsc();
  223. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  224. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  225. unsigned long temp;
  226. __asm__ __volatile__(
  227. " .set mips3 \n"
  228. "1: " __LL "%0, %1 # test_and_set_bit \n"
  229. " or %2, %0, %3 \n"
  230. " " __SC "%2, %1 \n"
  231. " beqzl %2, 1b \n"
  232. " and %2, %0, %3 \n"
  233. " .set mips0 \n"
  234. : "=&r" (temp), "+m" (*m), "=&r" (res)
  235. : "r" (1UL << bit)
  236. : "memory");
  237. } else if (kernel_uses_llsc) {
  238. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  239. unsigned long temp;
  240. do {
  241. __asm__ __volatile__(
  242. " .set mips3 \n"
  243. " " __LL "%0, %1 # test_and_set_bit \n"
  244. " or %2, %0, %3 \n"
  245. " " __SC "%2, %1 \n"
  246. " .set mips0 \n"
  247. : "=&r" (temp), "+m" (*m), "=&r" (res)
  248. : "r" (1UL << bit)
  249. : "memory");
  250. } while (unlikely(!res));
  251. res = temp & (1UL << bit);
  252. } else
  253. res = __mips_test_and_set_bit(nr, addr);
  254. smp_llsc_mb();
  255. return res != 0;
  256. }
  257. /*
  258. * test_and_set_bit_lock - Set a bit and return its old value
  259. * @nr: Bit to set
  260. * @addr: Address to count from
  261. *
  262. * This operation is atomic and implies acquire ordering semantics
  263. * after the memory operation.
  264. */
  265. static inline int test_and_set_bit_lock(unsigned long nr,
  266. volatile unsigned long *addr)
  267. {
  268. int bit = nr & SZLONG_MASK;
  269. unsigned long res;
  270. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  271. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  272. unsigned long temp;
  273. __asm__ __volatile__(
  274. " .set mips3 \n"
  275. "1: " __LL "%0, %1 # test_and_set_bit \n"
  276. " or %2, %0, %3 \n"
  277. " " __SC "%2, %1 \n"
  278. " beqzl %2, 1b \n"
  279. " and %2, %0, %3 \n"
  280. " .set mips0 \n"
  281. : "=&r" (temp), "+m" (*m), "=&r" (res)
  282. : "r" (1UL << bit)
  283. : "memory");
  284. } else if (kernel_uses_llsc) {
  285. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  286. unsigned long temp;
  287. do {
  288. __asm__ __volatile__(
  289. " .set mips3 \n"
  290. " " __LL "%0, %1 # test_and_set_bit \n"
  291. " or %2, %0, %3 \n"
  292. " " __SC "%2, %1 \n"
  293. " .set mips0 \n"
  294. : "=&r" (temp), "+m" (*m), "=&r" (res)
  295. : "r" (1UL << bit)
  296. : "memory");
  297. } while (unlikely(!res));
  298. res = temp & (1UL << bit);
  299. } else
  300. res = __mips_test_and_set_bit_lock(nr, addr);
  301. smp_llsc_mb();
  302. return res != 0;
  303. }
  304. /*
  305. * test_and_clear_bit - Clear a bit and return its old value
  306. * @nr: Bit to clear
  307. * @addr: Address to count from
  308. *
  309. * This operation is atomic and cannot be reordered.
  310. * It also implies a memory barrier.
  311. */
  312. static inline int test_and_clear_bit(unsigned long nr,
  313. volatile unsigned long *addr)
  314. {
  315. int bit = nr & SZLONG_MASK;
  316. unsigned long res;
  317. smp_mb__before_llsc();
  318. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  319. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  320. unsigned long temp;
  321. __asm__ __volatile__(
  322. " .set mips3 \n"
  323. "1: " __LL "%0, %1 # test_and_clear_bit \n"
  324. " or %2, %0, %3 \n"
  325. " xor %2, %3 \n"
  326. " " __SC "%2, %1 \n"
  327. " beqzl %2, 1b \n"
  328. " and %2, %0, %3 \n"
  329. " .set mips0 \n"
  330. : "=&r" (temp), "+m" (*m), "=&r" (res)
  331. : "r" (1UL << bit)
  332. : "memory");
  333. #ifdef CONFIG_CPU_MIPSR2
  334. } else if (kernel_uses_llsc && __builtin_constant_p(nr)) {
  335. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  336. unsigned long temp;
  337. do {
  338. __asm__ __volatile__(
  339. " " __LL "%0, %1 # test_and_clear_bit \n"
  340. " " __EXT "%2, %0, %3, 1 \n"
  341. " " __INS "%0, $0, %3, 1 \n"
  342. " " __SC "%0, %1 \n"
  343. : "=&r" (temp), "+m" (*m), "=&r" (res)
  344. : "ir" (bit)
  345. : "memory");
  346. } while (unlikely(!temp));
  347. #endif
  348. } else if (kernel_uses_llsc) {
  349. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  350. unsigned long temp;
  351. do {
  352. __asm__ __volatile__(
  353. " .set mips3 \n"
  354. " " __LL "%0, %1 # test_and_clear_bit \n"
  355. " or %2, %0, %3 \n"
  356. " xor %2, %3 \n"
  357. " " __SC "%2, %1 \n"
  358. " .set mips0 \n"
  359. : "=&r" (temp), "+m" (*m), "=&r" (res)
  360. : "r" (1UL << bit)
  361. : "memory");
  362. } while (unlikely(!res));
  363. res = temp & (1UL << bit);
  364. } else
  365. res = __mips_test_and_clear_bit(nr, addr);
  366. smp_llsc_mb();
  367. return res != 0;
  368. }
  369. /*
  370. * test_and_change_bit - Change a bit and return its old value
  371. * @nr: Bit to change
  372. * @addr: Address to count from
  373. *
  374. * This operation is atomic and cannot be reordered.
  375. * It also implies a memory barrier.
  376. */
  377. static inline int test_and_change_bit(unsigned long nr,
  378. volatile unsigned long *addr)
  379. {
  380. int bit = nr & SZLONG_MASK;
  381. unsigned long res;
  382. smp_mb__before_llsc();
  383. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  384. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  385. unsigned long temp;
  386. __asm__ __volatile__(
  387. " .set mips3 \n"
  388. "1: " __LL "%0, %1 # test_and_change_bit \n"
  389. " xor %2, %0, %3 \n"
  390. " " __SC "%2, %1 \n"
  391. " beqzl %2, 1b \n"
  392. " and %2, %0, %3 \n"
  393. " .set mips0 \n"
  394. : "=&r" (temp), "+m" (*m), "=&r" (res)
  395. : "r" (1UL << bit)
  396. : "memory");
  397. } else if (kernel_uses_llsc) {
  398. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  399. unsigned long temp;
  400. do {
  401. __asm__ __volatile__(
  402. " .set mips3 \n"
  403. " " __LL "%0, %1 # test_and_change_bit \n"
  404. " xor %2, %0, %3 \n"
  405. " " __SC "\t%2, %1 \n"
  406. " .set mips0 \n"
  407. : "=&r" (temp), "+m" (*m), "=&r" (res)
  408. : "r" (1UL << bit)
  409. : "memory");
  410. } while (unlikely(!res));
  411. res = temp & (1UL << bit);
  412. } else
  413. res = __mips_test_and_change_bit(nr, addr);
  414. smp_llsc_mb();
  415. return res != 0;
  416. }
  417. #include <asm-generic/bitops/non-atomic.h>
  418. /*
  419. * __clear_bit_unlock - Clears a bit in memory
  420. * @nr: Bit to clear
  421. * @addr: Address to start counting from
  422. *
  423. * __clear_bit() is non-atomic and implies release semantics before the memory
  424. * operation. It can be used for an unlock if no other CPUs can concurrently
  425. * modify other bits in the word.
  426. */
  427. static inline void __clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
  428. {
  429. smp_mb();
  430. __clear_bit(nr, addr);
  431. }
  432. /*
  433. * Return the bit position (0..63) of the most significant 1 bit in a word
  434. * Returns -1 if no 1 bit exists
  435. */
  436. static inline unsigned long __fls(unsigned long word)
  437. {
  438. int num;
  439. if (BITS_PER_LONG == 32 &&
  440. __builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
  441. __asm__(
  442. " .set push \n"
  443. " .set mips32 \n"
  444. " clz %0, %1 \n"
  445. " .set pop \n"
  446. : "=r" (num)
  447. : "r" (word));
  448. return 31 - num;
  449. }
  450. if (BITS_PER_LONG == 64 &&
  451. __builtin_constant_p(cpu_has_mips64) && cpu_has_mips64) {
  452. __asm__(
  453. " .set push \n"
  454. " .set mips64 \n"
  455. " dclz %0, %1 \n"
  456. " .set pop \n"
  457. : "=r" (num)
  458. : "r" (word));
  459. return 63 - num;
  460. }
  461. num = BITS_PER_LONG - 1;
  462. #if BITS_PER_LONG == 64
  463. if (!(word & (~0ul << 32))) {
  464. num -= 32;
  465. word <<= 32;
  466. }
  467. #endif
  468. if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
  469. num -= 16;
  470. word <<= 16;
  471. }
  472. if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
  473. num -= 8;
  474. word <<= 8;
  475. }
  476. if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
  477. num -= 4;
  478. word <<= 4;
  479. }
  480. if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
  481. num -= 2;
  482. word <<= 2;
  483. }
  484. if (!(word & (~0ul << (BITS_PER_LONG-1))))
  485. num -= 1;
  486. return num;
  487. }
  488. /*
  489. * __ffs - find first bit in word.
  490. * @word: The word to search
  491. *
  492. * Returns 0..SZLONG-1
  493. * Undefined if no bit exists, so code should check against 0 first.
  494. */
  495. static inline unsigned long __ffs(unsigned long word)
  496. {
  497. return __fls(word & -word);
  498. }
  499. /*
  500. * fls - find last bit set.
  501. * @word: The word to search
  502. *
  503. * This is defined the same way as ffs.
  504. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  505. */
  506. static inline int fls(int x)
  507. {
  508. int r;
  509. if (__builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
  510. __asm__("clz %0, %1" : "=r" (x) : "r" (x));
  511. return 32 - x;
  512. }
  513. r = 32;
  514. if (!x)
  515. return 0;
  516. if (!(x & 0xffff0000u)) {
  517. x <<= 16;
  518. r -= 16;
  519. }
  520. if (!(x & 0xff000000u)) {
  521. x <<= 8;
  522. r -= 8;
  523. }
  524. if (!(x & 0xf0000000u)) {
  525. x <<= 4;
  526. r -= 4;
  527. }
  528. if (!(x & 0xc0000000u)) {
  529. x <<= 2;
  530. r -= 2;
  531. }
  532. if (!(x & 0x80000000u)) {
  533. x <<= 1;
  534. r -= 1;
  535. }
  536. return r;
  537. }
  538. #include <asm-generic/bitops/fls64.h>
  539. /*
  540. * ffs - find first bit set.
  541. * @word: The word to search
  542. *
  543. * This is defined the same way as
  544. * the libc and compiler builtin ffs routines, therefore
  545. * differs in spirit from the above ffz (man ffs).
  546. */
  547. static inline int ffs(int word)
  548. {
  549. if (!word)
  550. return 0;
  551. return fls(word & -word);
  552. }
  553. #include <asm-generic/bitops/ffz.h>
  554. #include <asm-generic/bitops/find.h>
  555. #ifdef __KERNEL__
  556. #include <asm-generic/bitops/sched.h>
  557. #include <asm/arch_hweight.h>
  558. #include <asm-generic/bitops/const_hweight.h>
  559. #include <asm-generic/bitops/le.h>
  560. #include <asm-generic/bitops/ext2-atomic.h>
  561. #endif /* __KERNEL__ */
  562. #endif /* _ASM_BITOPS_H */