bitops.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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_llsc_mb()
  39. #define smp_mb__after_clear_bit() smp_llsc_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. unsigned long res;
  227. if (cpu_has_llsc && R10000_LLSC_WAR) {
  228. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  229. unsigned long temp;
  230. __asm__ __volatile__(
  231. " .set mips3 \n"
  232. "1: " __LL "%0, %1 # test_and_set_bit \n"
  233. " or %2, %0, %3 \n"
  234. " " __SC "%2, %1 \n"
  235. " beqzl %2, 1b \n"
  236. " and %2, %0, %3 \n"
  237. " .set mips0 \n"
  238. : "=&r" (temp), "=m" (*m), "=&r" (res)
  239. : "r" (1UL << bit), "m" (*m)
  240. : "memory");
  241. } else if (cpu_has_llsc) {
  242. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  243. unsigned long temp;
  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. } else {
  262. volatile unsigned long *a = addr;
  263. unsigned long mask;
  264. unsigned long flags;
  265. a += nr >> SZLONG_LOG;
  266. mask = 1UL << bit;
  267. raw_local_irq_save(flags);
  268. res = (mask & *a);
  269. *a |= mask;
  270. raw_local_irq_restore(flags);
  271. }
  272. smp_llsc_mb();
  273. return res != 0;
  274. }
  275. /*
  276. * test_and_clear_bit - Clear a bit and return its old value
  277. * @nr: Bit to clear
  278. * @addr: Address to count from
  279. *
  280. * This operation is atomic and cannot be reordered.
  281. * It also implies a memory barrier.
  282. */
  283. static inline int test_and_clear_bit(unsigned long nr,
  284. volatile unsigned long *addr)
  285. {
  286. unsigned short bit = nr & SZLONG_MASK;
  287. unsigned long res;
  288. if (cpu_has_llsc && R10000_LLSC_WAR) {
  289. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  290. unsigned long temp;
  291. __asm__ __volatile__(
  292. " .set mips3 \n"
  293. "1: " __LL "%0, %1 # test_and_clear_bit \n"
  294. " or %2, %0, %3 \n"
  295. " xor %2, %3 \n"
  296. " " __SC "%2, %1 \n"
  297. " beqzl %2, 1b \n"
  298. " and %2, %0, %3 \n"
  299. " .set mips0 \n"
  300. : "=&r" (temp), "=m" (*m), "=&r" (res)
  301. : "r" (1UL << bit), "m" (*m)
  302. : "memory");
  303. #ifdef CONFIG_CPU_MIPSR2
  304. } else if (__builtin_constant_p(nr)) {
  305. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  306. unsigned long temp;
  307. __asm__ __volatile__(
  308. "1: " __LL "%0, %1 # test_and_clear_bit \n"
  309. " " __EXT "%2, %0, %3, 1 \n"
  310. " " __INS "%0, $0, %3, 1 \n"
  311. " " __SC "%0, %1 \n"
  312. " beqz %0, 2f \n"
  313. " .subsection 2 \n"
  314. "2: b 1b \n"
  315. " .previous \n"
  316. : "=&r" (temp), "=m" (*m), "=&r" (res)
  317. : "ri" (bit), "m" (*m)
  318. : "memory");
  319. #endif
  320. } else if (cpu_has_llsc) {
  321. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  322. unsigned long temp;
  323. __asm__ __volatile__(
  324. " .set push \n"
  325. " .set noreorder \n"
  326. " .set mips3 \n"
  327. "1: " __LL "%0, %1 # test_and_clear_bit \n"
  328. " or %2, %0, %3 \n"
  329. " xor %2, %3 \n"
  330. " " __SC "%2, %1 \n"
  331. " beqz %2, 2f \n"
  332. " and %2, %0, %3 \n"
  333. " .subsection 2 \n"
  334. "2: b 1b \n"
  335. " nop \n"
  336. " .previous \n"
  337. " .set pop \n"
  338. : "=&r" (temp), "=m" (*m), "=&r" (res)
  339. : "r" (1UL << bit), "m" (*m)
  340. : "memory");
  341. } else {
  342. volatile unsigned long *a = addr;
  343. unsigned long mask;
  344. unsigned long flags;
  345. a += nr >> SZLONG_LOG;
  346. mask = 1UL << bit;
  347. raw_local_irq_save(flags);
  348. res = (mask & *a);
  349. *a &= ~mask;
  350. raw_local_irq_restore(flags);
  351. }
  352. smp_llsc_mb();
  353. return res != 0;
  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. unsigned short bit = nr & SZLONG_MASK;
  367. unsigned long res;
  368. if (cpu_has_llsc && R10000_LLSC_WAR) {
  369. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  370. unsigned long temp;
  371. __asm__ __volatile__(
  372. " .set mips3 \n"
  373. "1: " __LL "%0, %1 # test_and_change_bit \n"
  374. " xor %2, %0, %3 \n"
  375. " " __SC "%2, %1 \n"
  376. " beqzl %2, 1b \n"
  377. " and %2, %0, %3 \n"
  378. " .set mips0 \n"
  379. : "=&r" (temp), "=m" (*m), "=&r" (res)
  380. : "r" (1UL << bit), "m" (*m)
  381. : "memory");
  382. } else if (cpu_has_llsc) {
  383. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  384. unsigned long temp;
  385. __asm__ __volatile__(
  386. " .set push \n"
  387. " .set noreorder \n"
  388. " .set mips3 \n"
  389. "1: " __LL "%0, %1 # test_and_change_bit \n"
  390. " xor %2, %0, %3 \n"
  391. " " __SC "\t%2, %1 \n"
  392. " beqz %2, 2f \n"
  393. " and %2, %0, %3 \n"
  394. " .subsection 2 \n"
  395. "2: b 1b \n"
  396. " nop \n"
  397. " .previous \n"
  398. " .set pop \n"
  399. : "=&r" (temp), "=m" (*m), "=&r" (res)
  400. : "r" (1UL << bit), "m" (*m)
  401. : "memory");
  402. } else {
  403. volatile unsigned long *a = addr;
  404. unsigned long mask;
  405. unsigned long flags;
  406. a += nr >> SZLONG_LOG;
  407. mask = 1UL << bit;
  408. raw_local_irq_save(flags);
  409. res = (mask & *a);
  410. *a ^= mask;
  411. raw_local_irq_restore(flags);
  412. }
  413. smp_llsc_mb();
  414. return res != 0;
  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 */