bitops.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 1995, Russell King.
  3. *
  4. * Based on the arm32 version by RMK (and others). Their copyrights apply to
  5. * Those parts.
  6. * Modified for arm26 by Ian Molton on 25/11/04
  7. *
  8. * bit 0 is the LSB of an "unsigned long" quantity.
  9. *
  10. * Please note that the code in this file should never be included
  11. * from user space. Many of these are not implemented in assembler
  12. * since they would be too costly. Also, they require privileged
  13. * instructions (which are not available from user mode) to ensure
  14. * that they are atomic.
  15. */
  16. #ifndef __ASM_ARM_BITOPS_H
  17. #define __ASM_ARM_BITOPS_H
  18. #ifdef __KERNEL__
  19. #include <linux/compiler.h>
  20. #include <asm/system.h>
  21. #define smp_mb__before_clear_bit() do { } while (0)
  22. #define smp_mb__after_clear_bit() do { } while (0)
  23. /*
  24. * These functions are the basis of our bit ops.
  25. *
  26. * First, the atomic bitops. These use native endian.
  27. */
  28. static inline void ____atomic_set_bit(unsigned int bit, volatile unsigned long *p)
  29. {
  30. unsigned long flags;
  31. unsigned long mask = 1UL << (bit & 31);
  32. p += bit >> 5;
  33. local_irq_save(flags);
  34. *p |= mask;
  35. local_irq_restore(flags);
  36. }
  37. static inline void ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p)
  38. {
  39. unsigned long flags;
  40. unsigned long mask = 1UL << (bit & 31);
  41. p += bit >> 5;
  42. local_irq_save(flags);
  43. *p &= ~mask;
  44. local_irq_restore(flags);
  45. }
  46. static inline void ____atomic_change_bit(unsigned int bit, volatile unsigned long *p)
  47. {
  48. unsigned long flags;
  49. unsigned long mask = 1UL << (bit & 31);
  50. p += bit >> 5;
  51. local_irq_save(flags);
  52. *p ^= mask;
  53. local_irq_restore(flags);
  54. }
  55. static inline int
  56. ____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p)
  57. {
  58. unsigned long flags;
  59. unsigned int res;
  60. unsigned long mask = 1UL << (bit & 31);
  61. p += bit >> 5;
  62. local_irq_save(flags);
  63. res = *p;
  64. *p = res | mask;
  65. local_irq_restore(flags);
  66. return res & mask;
  67. }
  68. static inline int
  69. ____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
  70. {
  71. unsigned long flags;
  72. unsigned int res;
  73. unsigned long mask = 1UL << (bit & 31);
  74. p += bit >> 5;
  75. local_irq_save(flags);
  76. res = *p;
  77. *p = res & ~mask;
  78. local_irq_restore(flags);
  79. return res & mask;
  80. }
  81. static inline int
  82. ____atomic_test_and_change_bit(unsigned int bit, volatile unsigned long *p)
  83. {
  84. unsigned long flags;
  85. unsigned int res;
  86. unsigned long mask = 1UL << (bit & 31);
  87. p += bit >> 5;
  88. local_irq_save(flags);
  89. res = *p;
  90. *p = res ^ mask;
  91. local_irq_restore(flags);
  92. return res & mask;
  93. }
  94. #include <asm-generic/bitops/non-atomic.h>
  95. /*
  96. * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
  97. */
  98. extern void _set_bit_le(int nr, volatile unsigned long * p);
  99. extern void _clear_bit_le(int nr, volatile unsigned long * p);
  100. extern void _change_bit_le(int nr, volatile unsigned long * p);
  101. extern int _test_and_set_bit_le(int nr, volatile unsigned long * p);
  102. extern int _test_and_clear_bit_le(int nr, volatile unsigned long * p);
  103. extern int _test_and_change_bit_le(int nr, volatile unsigned long * p);
  104. extern int _find_first_zero_bit_le(const unsigned long * p, unsigned size);
  105. extern int _find_next_zero_bit_le(void * p, int size, int offset);
  106. extern int _find_first_bit_le(const unsigned long *p, unsigned size);
  107. extern int _find_next_bit_le(const unsigned long *p, int size, int offset);
  108. /*
  109. * The __* form of bitops are non-atomic and may be reordered.
  110. */
  111. #define ATOMIC_BITOP_LE(name,nr,p) \
  112. (__builtin_constant_p(nr) ? \
  113. ____atomic_##name(nr, p) : \
  114. _##name##_le(nr,p))
  115. #define NONATOMIC_BITOP(name,nr,p) \
  116. (____nonatomic_##name(nr, p))
  117. /*
  118. * These are the little endian, atomic definitions.
  119. */
  120. #define set_bit(nr,p) ATOMIC_BITOP_LE(set_bit,nr,p)
  121. #define clear_bit(nr,p) ATOMIC_BITOP_LE(clear_bit,nr,p)
  122. #define change_bit(nr,p) ATOMIC_BITOP_LE(change_bit,nr,p)
  123. #define test_and_set_bit(nr,p) ATOMIC_BITOP_LE(test_and_set_bit,nr,p)
  124. #define test_and_clear_bit(nr,p) ATOMIC_BITOP_LE(test_and_clear_bit,nr,p)
  125. #define test_and_change_bit(nr,p) ATOMIC_BITOP_LE(test_and_change_bit,nr,p)
  126. #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
  127. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
  128. #define find_first_bit(p,sz) _find_first_bit_le(p,sz)
  129. #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
  130. #define WORD_BITOFF_TO_LE(x) ((x))
  131. #include <asm-generic/bitops/ffz.h>
  132. #include <asm-generic/bitops/__ffs.h>
  133. #include <asm-generic/bitops/fls.h>
  134. #include <asm-generic/bitops/fls64.h>
  135. #include <asm-generic/bitops/ffs.h>
  136. #include <asm-generic/bitops/sched.h>
  137. #include <asm-generic/bitops/hweight.h>
  138. /*
  139. * Ext2 is defined to use little-endian byte ordering.
  140. * These do not need to be atomic.
  141. */
  142. #define ext2_set_bit(nr,p) \
  143. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  144. #define ext2_set_bit_atomic(lock,nr,p) \
  145. test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  146. #define ext2_clear_bit(nr,p) \
  147. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  148. #define ext2_clear_bit_atomic(lock,nr,p) \
  149. test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  150. #define ext2_test_bit(nr,p) \
  151. test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  152. #define ext2_find_first_zero_bit(p,sz) \
  153. _find_first_zero_bit_le(p,sz)
  154. #define ext2_find_next_zero_bit(p,sz,off) \
  155. _find_next_zero_bit_le(p,sz,off)
  156. /*
  157. * Minix is defined to use little-endian byte ordering.
  158. * These do not need to be atomic.
  159. */
  160. #define minix_set_bit(nr,p) \
  161. __set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  162. #define minix_test_bit(nr,p) \
  163. test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  164. #define minix_test_and_set_bit(nr,p) \
  165. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  166. #define minix_test_and_clear_bit(nr,p) \
  167. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  168. #define minix_find_first_zero_bit(p,sz) \
  169. _find_first_zero_bit_le((unsigned long *)(p),sz)
  170. #endif /* __KERNEL__ */
  171. #endif /* _ARM_BITOPS_H */