bitops.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include <linux/compiler.h>
  20. #include <asm/system.h>
  21. #define smp_mb__before_clear_bit() mb()
  22. #define smp_mb__after_clear_bit() mb()
  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. /*
  95. * Now the non-atomic variants. We let the compiler handle all
  96. * optimisations for these. These are all _native_ endian.
  97. */
  98. static inline void __set_bit(int nr, volatile unsigned long *p)
  99. {
  100. p[nr >> 5] |= (1UL << (nr & 31));
  101. }
  102. static inline void __clear_bit(int nr, volatile unsigned long *p)
  103. {
  104. p[nr >> 5] &= ~(1UL << (nr & 31));
  105. }
  106. static inline void __change_bit(int nr, volatile unsigned long *p)
  107. {
  108. p[nr >> 5] ^= (1UL << (nr & 31));
  109. }
  110. static inline int __test_and_set_bit(int nr, volatile unsigned long *p)
  111. {
  112. unsigned long oldval, mask = 1UL << (nr & 31);
  113. p += nr >> 5;
  114. oldval = *p;
  115. *p = oldval | mask;
  116. return oldval & mask;
  117. }
  118. static inline int __test_and_clear_bit(int nr, volatile unsigned long *p)
  119. {
  120. unsigned long oldval, mask = 1UL << (nr & 31);
  121. p += nr >> 5;
  122. oldval = *p;
  123. *p = oldval & ~mask;
  124. return oldval & mask;
  125. }
  126. static inline int __test_and_change_bit(int nr, volatile unsigned long *p)
  127. {
  128. unsigned long oldval, mask = 1UL << (nr & 31);
  129. p += nr >> 5;
  130. oldval = *p;
  131. *p = oldval ^ mask;
  132. return oldval & mask;
  133. }
  134. /*
  135. * This routine doesn't need to be atomic.
  136. */
  137. static inline int __test_bit(int nr, const volatile unsigned long * p)
  138. {
  139. return (p[nr >> 5] >> (nr & 31)) & 1UL;
  140. }
  141. /*
  142. * A note about Endian-ness.
  143. * -------------------------
  144. *
  145. * When the ARM is put into big endian mode via CR15, the processor
  146. * merely swaps the order of bytes within words, thus:
  147. *
  148. * ------------ physical data bus bits -----------
  149. * D31 ... D24 D23 ... D16 D15 ... D8 D7 ... D0
  150. * little byte 3 byte 2 byte 1 byte 0
  151. * big byte 0 byte 1 byte 2 byte 3
  152. *
  153. * This means that reading a 32-bit word at address 0 returns the same
  154. * value irrespective of the endian mode bit.
  155. *
  156. * Peripheral devices should be connected with the data bus reversed in
  157. * "Big Endian" mode. ARM Application Note 61 is applicable, and is
  158. * available from http://www.arm.com/.
  159. *
  160. * The following assumes that the data bus connectivity for big endian
  161. * mode has been followed.
  162. *
  163. * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
  164. */
  165. /*
  166. * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
  167. */
  168. extern void _set_bit_le(int nr, volatile unsigned long * p);
  169. extern void _clear_bit_le(int nr, volatile unsigned long * p);
  170. extern void _change_bit_le(int nr, volatile unsigned long * p);
  171. extern int _test_and_set_bit_le(int nr, volatile unsigned long * p);
  172. extern int _test_and_clear_bit_le(int nr, volatile unsigned long * p);
  173. extern int _test_and_change_bit_le(int nr, volatile unsigned long * p);
  174. extern int _find_first_zero_bit_le(const void * p, unsigned size);
  175. extern int _find_next_zero_bit_le(const void * p, int size, int offset);
  176. extern int _find_first_bit_le(const unsigned long *p, unsigned size);
  177. extern int _find_next_bit_le(const unsigned long *p, int size, int offset);
  178. /*
  179. * Big endian assembly bitops. nr = 0 -> byte 3 bit 0.
  180. */
  181. extern void _set_bit_be(int nr, volatile unsigned long * p);
  182. extern void _clear_bit_be(int nr, volatile unsigned long * p);
  183. extern void _change_bit_be(int nr, volatile unsigned long * p);
  184. extern int _test_and_set_bit_be(int nr, volatile unsigned long * p);
  185. extern int _test_and_clear_bit_be(int nr, volatile unsigned long * p);
  186. extern int _test_and_change_bit_be(int nr, volatile unsigned long * p);
  187. extern int _find_first_zero_bit_be(const void * p, unsigned size);
  188. extern int _find_next_zero_bit_be(const void * p, int size, int offset);
  189. extern int _find_first_bit_be(const unsigned long *p, unsigned size);
  190. extern int _find_next_bit_be(const unsigned long *p, int size, int offset);
  191. #ifndef CONFIG_SMP
  192. /*
  193. * The __* form of bitops are non-atomic and may be reordered.
  194. */
  195. #define ATOMIC_BITOP_LE(name,nr,p) \
  196. (__builtin_constant_p(nr) ? \
  197. ____atomic_##name(nr, p) : \
  198. _##name##_le(nr,p))
  199. #define ATOMIC_BITOP_BE(name,nr,p) \
  200. (__builtin_constant_p(nr) ? \
  201. ____atomic_##name(nr, p) : \
  202. _##name##_be(nr,p))
  203. #else
  204. #define ATOMIC_BITOP_LE(name,nr,p) _##name##_le(nr,p)
  205. #define ATOMIC_BITOP_BE(name,nr,p) _##name##_be(nr,p)
  206. #endif
  207. #define NONATOMIC_BITOP(name,nr,p) \
  208. (____nonatomic_##name(nr, p))
  209. #ifndef __ARMEB__
  210. /*
  211. * These are the little endian, atomic definitions.
  212. */
  213. #define set_bit(nr,p) ATOMIC_BITOP_LE(set_bit,nr,p)
  214. #define clear_bit(nr,p) ATOMIC_BITOP_LE(clear_bit,nr,p)
  215. #define change_bit(nr,p) ATOMIC_BITOP_LE(change_bit,nr,p)
  216. #define test_and_set_bit(nr,p) ATOMIC_BITOP_LE(test_and_set_bit,nr,p)
  217. #define test_and_clear_bit(nr,p) ATOMIC_BITOP_LE(test_and_clear_bit,nr,p)
  218. #define test_and_change_bit(nr,p) ATOMIC_BITOP_LE(test_and_change_bit,nr,p)
  219. #define test_bit(nr,p) __test_bit(nr,p)
  220. #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
  221. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
  222. #define find_first_bit(p,sz) _find_first_bit_le(p,sz)
  223. #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
  224. #define WORD_BITOFF_TO_LE(x) ((x))
  225. #else
  226. /*
  227. * These are the big endian, atomic definitions.
  228. */
  229. #define set_bit(nr,p) ATOMIC_BITOP_BE(set_bit,nr,p)
  230. #define clear_bit(nr,p) ATOMIC_BITOP_BE(clear_bit,nr,p)
  231. #define change_bit(nr,p) ATOMIC_BITOP_BE(change_bit,nr,p)
  232. #define test_and_set_bit(nr,p) ATOMIC_BITOP_BE(test_and_set_bit,nr,p)
  233. #define test_and_clear_bit(nr,p) ATOMIC_BITOP_BE(test_and_clear_bit,nr,p)
  234. #define test_and_change_bit(nr,p) ATOMIC_BITOP_BE(test_and_change_bit,nr,p)
  235. #define test_bit(nr,p) __test_bit(nr,p)
  236. #define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz)
  237. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off)
  238. #define find_first_bit(p,sz) _find_first_bit_be(p,sz)
  239. #define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off)
  240. #define WORD_BITOFF_TO_LE(x) ((x) ^ 0x18)
  241. #endif
  242. #if __LINUX_ARM_ARCH__ < 5
  243. /*
  244. * ffz = Find First Zero in word. Undefined if no zero exists,
  245. * so code should check against ~0UL first..
  246. */
  247. static inline unsigned long ffz(unsigned long word)
  248. {
  249. int k;
  250. word = ~word;
  251. k = 31;
  252. if (word & 0x0000ffff) { k -= 16; word <<= 16; }
  253. if (word & 0x00ff0000) { k -= 8; word <<= 8; }
  254. if (word & 0x0f000000) { k -= 4; word <<= 4; }
  255. if (word & 0x30000000) { k -= 2; word <<= 2; }
  256. if (word & 0x40000000) { k -= 1; }
  257. return k;
  258. }
  259. /*
  260. * ffz = Find First Zero in word. Undefined if no zero exists,
  261. * so code should check against ~0UL first..
  262. */
  263. static inline unsigned long __ffs(unsigned long word)
  264. {
  265. int k;
  266. k = 31;
  267. if (word & 0x0000ffff) { k -= 16; word <<= 16; }
  268. if (word & 0x00ff0000) { k -= 8; word <<= 8; }
  269. if (word & 0x0f000000) { k -= 4; word <<= 4; }
  270. if (word & 0x30000000) { k -= 2; word <<= 2; }
  271. if (word & 0x40000000) { k -= 1; }
  272. return k;
  273. }
  274. /*
  275. * fls: find last bit set.
  276. */
  277. #define fls(x) generic_fls(x)
  278. #define fls64(x) generic_fls64(x)
  279. /*
  280. * ffs: find first bit set. This is defined the same way as
  281. * the libc and compiler builtin ffs routines, therefore
  282. * differs in spirit from the above ffz (man ffs).
  283. */
  284. #define ffs(x) generic_ffs(x)
  285. #else
  286. /*
  287. * On ARMv5 and above those functions can be implemented around
  288. * the clz instruction for much better code efficiency.
  289. */
  290. #define fls(x) \
  291. ( __builtin_constant_p(x) ? generic_fls(x) : \
  292. ({ int __r; asm("clz\t%0, %1" : "=r"(__r) : "r"(x) : "cc"); 32-__r; }) )
  293. #define fls64(x) generic_fls64(x)
  294. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  295. #define __ffs(x) (ffs(x) - 1)
  296. #define ffz(x) __ffs( ~(x) )
  297. #endif
  298. /*
  299. * Find first bit set in a 168-bit bitmap, where the first
  300. * 128 bits are unlikely to be set.
  301. */
  302. static inline int sched_find_first_bit(const unsigned long *b)
  303. {
  304. unsigned long v;
  305. unsigned int off;
  306. for (off = 0; v = b[off], off < 4; off++) {
  307. if (unlikely(v))
  308. break;
  309. }
  310. return __ffs(v) + off * 32;
  311. }
  312. /*
  313. * hweightN: returns the hamming weight (i.e. the number
  314. * of bits set) of a N-bit word
  315. */
  316. #define hweight32(x) generic_hweight32(x)
  317. #define hweight16(x) generic_hweight16(x)
  318. #define hweight8(x) generic_hweight8(x)
  319. /*
  320. * Ext2 is defined to use little-endian byte ordering.
  321. * These do not need to be atomic.
  322. */
  323. #define ext2_set_bit(nr,p) \
  324. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  325. #define ext2_set_bit_atomic(lock,nr,p) \
  326. test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  327. #define ext2_clear_bit(nr,p) \
  328. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  329. #define ext2_clear_bit_atomic(lock,nr,p) \
  330. test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  331. #define ext2_test_bit(nr,p) \
  332. __test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  333. #define ext2_find_first_zero_bit(p,sz) \
  334. _find_first_zero_bit_le(p,sz)
  335. #define ext2_find_next_zero_bit(p,sz,off) \
  336. _find_next_zero_bit_le(p,sz,off)
  337. /*
  338. * Minix is defined to use little-endian byte ordering.
  339. * These do not need to be atomic.
  340. */
  341. #define minix_set_bit(nr,p) \
  342. __set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  343. #define minix_test_bit(nr,p) \
  344. __test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  345. #define minix_test_and_set_bit(nr,p) \
  346. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  347. #define minix_test_and_clear_bit(nr,p) \
  348. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  349. #define minix_find_first_zero_bit(p,sz) \
  350. _find_first_zero_bit_le(p,sz)
  351. #endif /* __KERNEL__ */
  352. #endif /* _ARM_BITOPS_H */