bitops.h 14 KB

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