bitops.h 14 KB

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