bitops.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <asm/system.h>
  24. #define smp_mb__before_clear_bit() mb()
  25. #define smp_mb__after_clear_bit() 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;
  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;
  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;
  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. * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
  124. */
  125. extern void _set_bit_le(int nr, volatile unsigned long * p);
  126. extern void _clear_bit_le(int nr, volatile unsigned long * p);
  127. extern void _change_bit_le(int nr, volatile unsigned long * p);
  128. extern int _test_and_set_bit_le(int nr, volatile unsigned long * p);
  129. extern int _test_and_clear_bit_le(int nr, volatile unsigned long * p);
  130. extern int _test_and_change_bit_le(int nr, volatile unsigned long * p);
  131. extern int _find_first_zero_bit_le(const void * p, unsigned size);
  132. extern int _find_next_zero_bit_le(const void * p, int size, int offset);
  133. extern int _find_first_bit_le(const unsigned long *p, unsigned size);
  134. extern int _find_next_bit_le(const unsigned long *p, int size, int offset);
  135. /*
  136. * Big endian assembly bitops. nr = 0 -> byte 3 bit 0.
  137. */
  138. extern void _set_bit_be(int nr, volatile unsigned long * p);
  139. extern void _clear_bit_be(int nr, volatile unsigned long * p);
  140. extern void _change_bit_be(int nr, volatile unsigned long * p);
  141. extern int _test_and_set_bit_be(int nr, volatile unsigned long * p);
  142. extern int _test_and_clear_bit_be(int nr, volatile unsigned long * p);
  143. extern int _test_and_change_bit_be(int nr, volatile unsigned long * p);
  144. extern int _find_first_zero_bit_be(const void * p, unsigned size);
  145. extern int _find_next_zero_bit_be(const void * p, int size, int offset);
  146. extern int _find_first_bit_be(const unsigned long *p, unsigned size);
  147. extern int _find_next_bit_be(const unsigned long *p, int size, int offset);
  148. #ifndef CONFIG_SMP
  149. /*
  150. * The __* form of bitops are non-atomic and may be reordered.
  151. */
  152. #define ATOMIC_BITOP_LE(name,nr,p) \
  153. (__builtin_constant_p(nr) ? \
  154. ____atomic_##name(nr, p) : \
  155. _##name##_le(nr,p))
  156. #define ATOMIC_BITOP_BE(name,nr,p) \
  157. (__builtin_constant_p(nr) ? \
  158. ____atomic_##name(nr, p) : \
  159. _##name##_be(nr,p))
  160. #else
  161. #define ATOMIC_BITOP_LE(name,nr,p) _##name##_le(nr,p)
  162. #define ATOMIC_BITOP_BE(name,nr,p) _##name##_be(nr,p)
  163. #endif
  164. #define NONATOMIC_BITOP(name,nr,p) \
  165. (____nonatomic_##name(nr, p))
  166. #ifndef __ARMEB__
  167. /*
  168. * These are the little endian, atomic definitions.
  169. */
  170. #define set_bit(nr,p) ATOMIC_BITOP_LE(set_bit,nr,p)
  171. #define clear_bit(nr,p) ATOMIC_BITOP_LE(clear_bit,nr,p)
  172. #define change_bit(nr,p) ATOMIC_BITOP_LE(change_bit,nr,p)
  173. #define test_and_set_bit(nr,p) ATOMIC_BITOP_LE(test_and_set_bit,nr,p)
  174. #define test_and_clear_bit(nr,p) ATOMIC_BITOP_LE(test_and_clear_bit,nr,p)
  175. #define test_and_change_bit(nr,p) ATOMIC_BITOP_LE(test_and_change_bit,nr,p)
  176. #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
  177. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
  178. #define find_first_bit(p,sz) _find_first_bit_le(p,sz)
  179. #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
  180. #define WORD_BITOFF_TO_LE(x) ((x))
  181. #else
  182. /*
  183. * These are the big endian, atomic definitions.
  184. */
  185. #define set_bit(nr,p) ATOMIC_BITOP_BE(set_bit,nr,p)
  186. #define clear_bit(nr,p) ATOMIC_BITOP_BE(clear_bit,nr,p)
  187. #define change_bit(nr,p) ATOMIC_BITOP_BE(change_bit,nr,p)
  188. #define test_and_set_bit(nr,p) ATOMIC_BITOP_BE(test_and_set_bit,nr,p)
  189. #define test_and_clear_bit(nr,p) ATOMIC_BITOP_BE(test_and_clear_bit,nr,p)
  190. #define test_and_change_bit(nr,p) ATOMIC_BITOP_BE(test_and_change_bit,nr,p)
  191. #define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz)
  192. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off)
  193. #define find_first_bit(p,sz) _find_first_bit_be(p,sz)
  194. #define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off)
  195. #define WORD_BITOFF_TO_LE(x) ((x) ^ 0x18)
  196. #endif
  197. #if __LINUX_ARM_ARCH__ < 5
  198. #include <asm-generic/bitops/ffz.h>
  199. #include <asm-generic/bitops/__fls.h>
  200. #include <asm-generic/bitops/__ffs.h>
  201. #include <asm-generic/bitops/fls.h>
  202. #include <asm-generic/bitops/ffs.h>
  203. #else
  204. static inline int constant_fls(int x)
  205. {
  206. int r = 32;
  207. if (!x)
  208. return 0;
  209. if (!(x & 0xffff0000u)) {
  210. x <<= 16;
  211. r -= 16;
  212. }
  213. if (!(x & 0xff000000u)) {
  214. x <<= 8;
  215. r -= 8;
  216. }
  217. if (!(x & 0xf0000000u)) {
  218. x <<= 4;
  219. r -= 4;
  220. }
  221. if (!(x & 0xc0000000u)) {
  222. x <<= 2;
  223. r -= 2;
  224. }
  225. if (!(x & 0x80000000u)) {
  226. x <<= 1;
  227. r -= 1;
  228. }
  229. return r;
  230. }
  231. /*
  232. * On ARMv5 and above those functions can be implemented around
  233. * the clz instruction for much better code efficiency.
  234. */
  235. static inline int fls(int x)
  236. {
  237. int ret;
  238. if (__builtin_constant_p(x))
  239. return constant_fls(x);
  240. asm("clz\t%0, %1" : "=r" (ret) : "r" (x) : "cc");
  241. ret = 32 - ret;
  242. return ret;
  243. }
  244. #define __fls(x) (fls(x) - 1)
  245. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  246. #define __ffs(x) (ffs(x) - 1)
  247. #define ffz(x) __ffs( ~(x) )
  248. #endif
  249. #include <asm-generic/bitops/fls64.h>
  250. #include <asm-generic/bitops/sched.h>
  251. #include <asm-generic/bitops/hweight.h>
  252. #include <asm-generic/bitops/lock.h>
  253. /*
  254. * Ext2 is defined to use little-endian byte ordering.
  255. * These do not need to be atomic.
  256. */
  257. #define ext2_set_bit(nr,p) \
  258. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  259. #define ext2_set_bit_atomic(lock,nr,p) \
  260. test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  261. #define ext2_clear_bit(nr,p) \
  262. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  263. #define ext2_clear_bit_atomic(lock,nr,p) \
  264. test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  265. #define ext2_test_bit(nr,p) \
  266. test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  267. #define ext2_find_first_zero_bit(p,sz) \
  268. _find_first_zero_bit_le(p,sz)
  269. #define ext2_find_next_zero_bit(p,sz,off) \
  270. _find_next_zero_bit_le(p,sz,off)
  271. #define ext2_find_next_bit(p, sz, off) \
  272. _find_next_bit_le(p, sz, off)
  273. /*
  274. * Minix is defined to use little-endian byte ordering.
  275. * These do not need to be atomic.
  276. */
  277. #define minix_set_bit(nr,p) \
  278. __set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  279. #define minix_test_bit(nr,p) \
  280. test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  281. #define minix_test_and_set_bit(nr,p) \
  282. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  283. #define minix_test_and_clear_bit(nr,p) \
  284. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  285. #define minix_find_first_zero_bit(p,sz) \
  286. _find_first_zero_bit_le(p,sz)
  287. #endif /* __KERNEL__ */
  288. #endif /* _ARM_BITOPS_H */