bitops.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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) != 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. #define WORD_BITOFF_TO_LE(x) ((x))
  172. #else
  173. /*
  174. * These are the big endian, atomic definitions.
  175. */
  176. #define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz)
  177. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off)
  178. #define find_first_bit(p,sz) _find_first_bit_be(p,sz)
  179. #define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off)
  180. #define WORD_BITOFF_TO_LE(x) ((x) ^ 0x18)
  181. #endif
  182. #if __LINUX_ARM_ARCH__ < 5
  183. #include <asm-generic/bitops/ffz.h>
  184. #include <asm-generic/bitops/__fls.h>
  185. #include <asm-generic/bitops/__ffs.h>
  186. #include <asm-generic/bitops/fls.h>
  187. #include <asm-generic/bitops/ffs.h>
  188. #else
  189. static inline int constant_fls(int x)
  190. {
  191. int r = 32;
  192. if (!x)
  193. return 0;
  194. if (!(x & 0xffff0000u)) {
  195. x <<= 16;
  196. r -= 16;
  197. }
  198. if (!(x & 0xff000000u)) {
  199. x <<= 8;
  200. r -= 8;
  201. }
  202. if (!(x & 0xf0000000u)) {
  203. x <<= 4;
  204. r -= 4;
  205. }
  206. if (!(x & 0xc0000000u)) {
  207. x <<= 2;
  208. r -= 2;
  209. }
  210. if (!(x & 0x80000000u)) {
  211. x <<= 1;
  212. r -= 1;
  213. }
  214. return r;
  215. }
  216. /*
  217. * On ARMv5 and above those functions can be implemented around
  218. * the clz instruction for much better code efficiency.
  219. */
  220. static inline int fls(int x)
  221. {
  222. int ret;
  223. if (__builtin_constant_p(x))
  224. return constant_fls(x);
  225. asm("clz\t%0, %1" : "=r" (ret) : "r" (x));
  226. ret = 32 - ret;
  227. return ret;
  228. }
  229. #define __fls(x) (fls(x) - 1)
  230. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  231. #define __ffs(x) (ffs(x) - 1)
  232. #define ffz(x) __ffs( ~(x) )
  233. #endif
  234. #include <asm-generic/bitops/fls64.h>
  235. #include <asm-generic/bitops/sched.h>
  236. #include <asm-generic/bitops/hweight.h>
  237. #include <asm-generic/bitops/lock.h>
  238. static inline void __set_bit_le(int nr, void *addr)
  239. {
  240. __set_bit(WORD_BITOFF_TO_LE(nr), addr);
  241. }
  242. static inline void __clear_bit_le(int nr, void *addr)
  243. {
  244. __clear_bit(WORD_BITOFF_TO_LE(nr), addr);
  245. }
  246. static inline int __test_and_set_bit_le(int nr, void *addr)
  247. {
  248. return __test_and_set_bit(WORD_BITOFF_TO_LE(nr), addr);
  249. }
  250. static inline int test_and_set_bit_le(int nr, void *addr)
  251. {
  252. return test_and_set_bit(WORD_BITOFF_TO_LE(nr), addr);
  253. }
  254. static inline int __test_and_clear_bit_le(int nr, void *addr)
  255. {
  256. return __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), addr);
  257. }
  258. static inline int test_and_clear_bit_le(int nr, void *addr)
  259. {
  260. return test_and_clear_bit(WORD_BITOFF_TO_LE(nr), addr);
  261. }
  262. static inline int test_bit_le(int nr, const void *addr)
  263. {
  264. return test_bit(WORD_BITOFF_TO_LE(nr), addr);
  265. }
  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. static inline int find_next_zero_bit_le(const void *p, int size, int offset)
  271. {
  272. return _find_next_zero_bit_le(p, size, offset);
  273. }
  274. static inline int find_next_bit_le(const void *p, int size, int offset)
  275. {
  276. return _find_next_bit_le(p, size, offset);
  277. }
  278. /*
  279. * Ext2 is defined to use little-endian byte ordering.
  280. * These do not need to be atomic.
  281. */
  282. #define ext2_set_bit __test_and_set_bit_le
  283. #define ext2_set_bit_atomic(lock, nr, p) \
  284. test_and_set_bit_le(nr, p)
  285. #define ext2_clear_bit __test_and_clear_bit_le
  286. #define ext2_clear_bit_atomic(lock, nr, p) \
  287. test_and_clear_bit_le(nr, p)
  288. #define ext2_test_bit test_bit_le
  289. #define ext2_find_first_zero_bit find_first_zero_bit_le
  290. #define ext2_find_next_zero_bit find_next_zero_bit_le
  291. #define ext2_find_next_bit find_next_bit_le
  292. /*
  293. * Minix is defined to use little-endian byte ordering.
  294. * These do not need to be atomic.
  295. */
  296. #define minix_set_bit __set_bit_le
  297. #define minix_test_bit test_bit_le
  298. #define minix_test_and_set_bit __test_and_set_bit_le
  299. #define minix_test_and_clear_bit __test_and_clear_bit_le
  300. #define minix_find_first_zero_bit find_first_zero_bit_le
  301. #endif /* __KERNEL__ */
  302. #endif /* _ARM_BITOPS_H */