bitops.h 15 KB

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