bitops.h 8.5 KB

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