bitops.h 12 KB

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