bitops.h 12 KB

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