bitops.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include <linux/bitops.h>
  10. #include <linux/irqflags.h>
  11. #include <linux/export.h>
  12. /**
  13. * __mips_set_bit - Atomically set a bit in memory. This is called by
  14. * set_bit() if it cannot find a faster solution.
  15. * @nr: the bit to set
  16. * @addr: the address to start counting from
  17. */
  18. void __mips_set_bit(unsigned long nr, volatile unsigned long *addr)
  19. {
  20. unsigned long *a = (unsigned long *)addr;
  21. unsigned bit = nr & SZLONG_MASK;
  22. unsigned long mask;
  23. unsigned long flags;
  24. a += nr >> SZLONG_LOG;
  25. mask = 1UL << bit;
  26. raw_local_irq_save(flags);
  27. *a |= mask;
  28. raw_local_irq_restore(flags);
  29. }
  30. EXPORT_SYMBOL(__mips_set_bit);
  31. /**
  32. * __mips_clear_bit - Clears a bit in memory. This is called by clear_bit() if
  33. * it cannot find a faster solution.
  34. * @nr: Bit to clear
  35. * @addr: Address to start counting from
  36. */
  37. void __mips_clear_bit(unsigned long nr, volatile unsigned long *addr)
  38. {
  39. unsigned long *a = (unsigned long *)addr;
  40. unsigned bit = nr & SZLONG_MASK;
  41. unsigned long mask;
  42. unsigned long flags;
  43. a += nr >> SZLONG_LOG;
  44. mask = 1UL << bit;
  45. raw_local_irq_save(flags);
  46. *a &= ~mask;
  47. raw_local_irq_restore(flags);
  48. }
  49. EXPORT_SYMBOL(__mips_clear_bit);
  50. /**
  51. * __mips_change_bit - Toggle a bit in memory. This is called by change_bit()
  52. * if it cannot find a faster solution.
  53. * @nr: Bit to change
  54. * @addr: Address to start counting from
  55. */
  56. void __mips_change_bit(unsigned long nr, volatile unsigned long *addr)
  57. {
  58. unsigned long *a = (unsigned long *)addr;
  59. unsigned bit = nr & SZLONG_MASK;
  60. unsigned long mask;
  61. unsigned long flags;
  62. a += nr >> SZLONG_LOG;
  63. mask = 1UL << bit;
  64. raw_local_irq_save(flags);
  65. *a ^= mask;
  66. raw_local_irq_restore(flags);
  67. }
  68. EXPORT_SYMBOL(__mips_change_bit);
  69. /**
  70. * __mips_test_and_set_bit - Set a bit and return its old value. This is
  71. * called by test_and_set_bit() if it cannot find a faster solution.
  72. * @nr: Bit to set
  73. * @addr: Address to count from
  74. */
  75. int __mips_test_and_set_bit(unsigned long nr,
  76. volatile unsigned long *addr)
  77. {
  78. unsigned long *a = (unsigned long *)addr;
  79. unsigned bit = nr & SZLONG_MASK;
  80. unsigned long mask;
  81. unsigned long flags;
  82. int res;
  83. a += nr >> SZLONG_LOG;
  84. mask = 1UL << bit;
  85. raw_local_irq_save(flags);
  86. res = (mask & *a) != 0;
  87. *a |= mask;
  88. raw_local_irq_restore(flags);
  89. return res;
  90. }
  91. EXPORT_SYMBOL(__mips_test_and_set_bit);
  92. /**
  93. * __mips_test_and_set_bit_lock - Set a bit and return its old value. This is
  94. * called by test_and_set_bit_lock() if it cannot find a faster solution.
  95. * @nr: Bit to set
  96. * @addr: Address to count from
  97. */
  98. int __mips_test_and_set_bit_lock(unsigned long nr,
  99. volatile unsigned long *addr)
  100. {
  101. unsigned long *a = (unsigned long *)addr;
  102. unsigned bit = nr & SZLONG_MASK;
  103. unsigned long mask;
  104. unsigned long flags;
  105. int res;
  106. a += nr >> SZLONG_LOG;
  107. mask = 1UL << bit;
  108. raw_local_irq_save(flags);
  109. res = (mask & *a) != 0;
  110. *a |= mask;
  111. raw_local_irq_restore(flags);
  112. return res;
  113. }
  114. EXPORT_SYMBOL(__mips_test_and_set_bit_lock);
  115. /**
  116. * __mips_test_and_clear_bit - Clear a bit and return its old value. This is
  117. * called by test_and_clear_bit() if it cannot find a faster solution.
  118. * @nr: Bit to clear
  119. * @addr: Address to count from
  120. */
  121. int __mips_test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
  122. {
  123. unsigned long *a = (unsigned long *)addr;
  124. unsigned bit = nr & SZLONG_MASK;
  125. unsigned long mask;
  126. unsigned long flags;
  127. int res;
  128. a += nr >> SZLONG_LOG;
  129. mask = 1UL << bit;
  130. raw_local_irq_save(flags);
  131. res = (mask & *a) != 0;
  132. *a &= ~mask;
  133. raw_local_irq_restore(flags);
  134. return res;
  135. }
  136. EXPORT_SYMBOL(__mips_test_and_clear_bit);
  137. /**
  138. * __mips_test_and_change_bit - Change a bit and return its old value. This is
  139. * called by test_and_change_bit() if it cannot find a faster solution.
  140. * @nr: Bit to change
  141. * @addr: Address to count from
  142. */
  143. int __mips_test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
  144. {
  145. unsigned long *a = (unsigned long *)addr;
  146. unsigned bit = nr & SZLONG_MASK;
  147. unsigned long mask;
  148. unsigned long flags;
  149. int res;
  150. a += nr >> SZLONG_LOG;
  151. mask = 1UL << bit;
  152. raw_local_irq_save(flags);
  153. res = (mask & *a) != 0;
  154. *a ^= mask;
  155. raw_local_irq_restore(flags);
  156. return res;
  157. }
  158. EXPORT_SYMBOL(__mips_test_and_change_bit);