bitops.h 12 KB

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