bitops.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright 1995, Russell King.
  3. * Various bits and pieces copyrights include:
  4. * Linus Torvalds (test_bit).
  5. * Big endian support: Copyright 2001, Nicolas Pitre
  6. * reworked by rmk.
  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. #ifndef _LINUX_BITOPS_H
  20. #error only <linux/bitops.h> can be included directly
  21. #endif
  22. #include <linux/compiler.h>
  23. #include <linux/irqflags.h>
  24. #define smp_mb__before_clear_bit() smp_mb()
  25. #define smp_mb__after_clear_bit() smp_mb()
  26. /*
  27. * These functions are the basis of our bit ops.
  28. *
  29. * First, the atomic bitops. These use native endian.
  30. */
  31. static inline void ____atomic_set_bit(unsigned int bit, volatile unsigned long *p)
  32. {
  33. unsigned long flags;
  34. unsigned long mask = 1UL << (bit & 31);
  35. p += bit >> 5;
  36. raw_local_irq_save(flags);
  37. *p |= mask;
  38. raw_local_irq_restore(flags);
  39. }
  40. static inline void ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p)
  41. {
  42. unsigned long flags;
  43. unsigned long mask = 1UL << (bit & 31);
  44. p += bit >> 5;
  45. raw_local_irq_save(flags);
  46. *p &= ~mask;
  47. raw_local_irq_restore(flags);
  48. }
  49. static inline void ____atomic_change_bit(unsigned int bit, volatile unsigned long *p)
  50. {
  51. unsigned long flags;
  52. unsigned long mask = 1UL << (bit & 31);
  53. p += bit >> 5;
  54. raw_local_irq_save(flags);
  55. *p ^= mask;
  56. raw_local_irq_restore(flags);
  57. }
  58. static inline int
  59. ____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p)
  60. {
  61. unsigned long flags;
  62. unsigned int res;
  63. unsigned long mask = 1UL << (bit & 31);
  64. p += bit >> 5;
  65. raw_local_irq_save(flags);
  66. res = *p;
  67. *p = res | mask;
  68. raw_local_irq_restore(flags);
  69. return (res & mask) != 0;
  70. }
  71. static inline int
  72. ____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
  73. {
  74. unsigned long flags;
  75. unsigned int res;
  76. unsigned long mask = 1UL << (bit & 31);
  77. p += bit >> 5;
  78. raw_local_irq_save(flags);
  79. res = *p;
  80. *p = res & ~mask;
  81. raw_local_irq_restore(flags);
  82. return (res & mask) != 0;
  83. }
  84. static inline int
  85. ____atomic_test_and_change_bit(unsigned int bit, volatile unsigned long *p)
  86. {
  87. unsigned long flags;
  88. unsigned int res;
  89. unsigned long mask = 1UL << (bit & 31);
  90. p += bit >> 5;
  91. raw_local_irq_save(flags);
  92. res = *p;
  93. *p = res ^ mask;
  94. raw_local_irq_restore(flags);
  95. return (res & mask) != 0;
  96. }
  97. #include <asm-generic/bitops/non-atomic.h>
  98. /*
  99. * A note about Endian-ness.
  100. * -------------------------
  101. *
  102. * When the ARM is put into big endian mode via CR15, the processor
  103. * merely swaps the order of bytes within words, thus:
  104. *
  105. * ------------ physical data bus bits -----------
  106. * D31 ... D24 D23 ... D16 D15 ... D8 D7 ... D0
  107. * little byte 3 byte 2 byte 1 byte 0
  108. * big byte 0 byte 1 byte 2 byte 3
  109. *
  110. * This means that reading a 32-bit word at address 0 returns the same
  111. * value irrespective of the endian mode bit.
  112. *
  113. * Peripheral devices should be connected with the data bus reversed in
  114. * "Big Endian" mode. ARM Application Note 61 is applicable, and is
  115. * available from http://www.arm.com/.
  116. *
  117. * The following assumes that the data bus connectivity for big endian
  118. * mode has been followed.
  119. *
  120. * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
  121. */
  122. /*
  123. * Native endian assembly bitops. nr = 0 -> word 0 bit 0.
  124. */
  125. extern void _set_bit(int nr, volatile unsigned long * p);
  126. extern void _clear_bit(int nr, volatile unsigned long * p);
  127. extern void _change_bit(int nr, volatile unsigned long * p);
  128. extern int _test_and_set_bit(int nr, volatile unsigned long * p);
  129. extern int _test_and_clear_bit(int nr, volatile unsigned long * p);
  130. extern int _test_and_change_bit(int nr, volatile unsigned long * p);
  131. /*
  132. * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
  133. */
  134. extern int _find_first_zero_bit_le(const void * p, unsigned size);
  135. extern int _find_next_zero_bit_le(const void * p, int size, int offset);
  136. extern int _find_first_bit_le(const unsigned long *p, unsigned size);
  137. extern int _find_next_bit_le(const unsigned long *p, int size, int offset);
  138. /*
  139. * Big endian assembly bitops. nr = 0 -> byte 3 bit 0.
  140. */
  141. extern int _find_first_zero_bit_be(const void * p, unsigned size);
  142. extern int _find_next_zero_bit_be(const void * p, int size, int offset);
  143. extern int _find_first_bit_be(const unsigned long *p, unsigned size);
  144. extern int _find_next_bit_be(const unsigned long *p, int size, int offset);
  145. #ifndef CONFIG_SMP
  146. /*
  147. * The __* form of bitops are non-atomic and may be reordered.
  148. */
  149. #define ATOMIC_BITOP(name,nr,p) \
  150. (__builtin_constant_p(nr) ? ____atomic_##name(nr, p) : _##name(nr,p))
  151. #else
  152. #define ATOMIC_BITOP(name,nr,p) _##name(nr,p)
  153. #endif
  154. /*
  155. * Native endian atomic definitions.
  156. */
  157. #define set_bit(nr,p) ATOMIC_BITOP(set_bit,nr,p)
  158. #define clear_bit(nr,p) ATOMIC_BITOP(clear_bit,nr,p)
  159. #define change_bit(nr,p) ATOMIC_BITOP(change_bit,nr,p)
  160. #define test_and_set_bit(nr,p) ATOMIC_BITOP(test_and_set_bit,nr,p)
  161. #define test_and_clear_bit(nr,p) ATOMIC_BITOP(test_and_clear_bit,nr,p)
  162. #define test_and_change_bit(nr,p) ATOMIC_BITOP(test_and_change_bit,nr,p)
  163. #ifndef __ARMEB__
  164. /*
  165. * These are the little endian, atomic definitions.
  166. */
  167. #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
  168. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
  169. #define find_first_bit(p,sz) _find_first_bit_le(p,sz)
  170. #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
  171. #else
  172. /*
  173. * These are the big endian, atomic definitions.
  174. */
  175. #define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz)
  176. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off)
  177. #define find_first_bit(p,sz) _find_first_bit_be(p,sz)
  178. #define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off)
  179. #endif
  180. #if __LINUX_ARM_ARCH__ < 5
  181. #include <asm-generic/bitops/ffz.h>
  182. #include <asm-generic/bitops/__fls.h>
  183. #include <asm-generic/bitops/__ffs.h>
  184. #include <asm-generic/bitops/fls.h>
  185. #include <asm-generic/bitops/ffs.h>
  186. #else
  187. static inline int constant_fls(int x)
  188. {
  189. int r = 32;
  190. if (!x)
  191. return 0;
  192. if (!(x & 0xffff0000u)) {
  193. x <<= 16;
  194. r -= 16;
  195. }
  196. if (!(x & 0xff000000u)) {
  197. x <<= 8;
  198. r -= 8;
  199. }
  200. if (!(x & 0xf0000000u)) {
  201. x <<= 4;
  202. r -= 4;
  203. }
  204. if (!(x & 0xc0000000u)) {
  205. x <<= 2;
  206. r -= 2;
  207. }
  208. if (!(x & 0x80000000u)) {
  209. x <<= 1;
  210. r -= 1;
  211. }
  212. return r;
  213. }
  214. /*
  215. * On ARMv5 and above those functions can be implemented around
  216. * the clz instruction for much better code efficiency.
  217. */
  218. static inline int fls(int x)
  219. {
  220. int ret;
  221. if (__builtin_constant_p(x))
  222. return constant_fls(x);
  223. asm("clz\t%0, %1" : "=r" (ret) : "r" (x));
  224. ret = 32 - ret;
  225. return ret;
  226. }
  227. #define __fls(x) (fls(x) - 1)
  228. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  229. #define __ffs(x) (ffs(x) - 1)
  230. #define ffz(x) __ffs( ~(x) )
  231. #endif
  232. #include <asm-generic/bitops/fls64.h>
  233. #include <asm-generic/bitops/sched.h>
  234. #include <asm-generic/bitops/hweight.h>
  235. #include <asm-generic/bitops/lock.h>
  236. #ifdef __ARMEB__
  237. static inline int find_first_zero_bit_le(const void *p, unsigned size)
  238. {
  239. return _find_first_zero_bit_le(p, size);
  240. }
  241. #define find_first_zero_bit_le find_first_zero_bit_le
  242. static inline int find_next_zero_bit_le(const void *p, int size, int offset)
  243. {
  244. return _find_next_zero_bit_le(p, size, offset);
  245. }
  246. #define find_next_zero_bit_le find_next_zero_bit_le
  247. static inline int find_next_bit_le(const void *p, int size, int offset)
  248. {
  249. return _find_next_bit_le(p, size, offset);
  250. }
  251. #define find_next_bit_le find_next_bit_le
  252. #endif
  253. #include <asm-generic/bitops/le.h>
  254. /*
  255. * Ext2 is defined to use little-endian byte ordering.
  256. */
  257. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  258. #endif /* __KERNEL__ */
  259. #endif /* _ARM_BITOPS_H */