bitops.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * PowerPC atomic bit operations.
  3. *
  4. * Merged version by David Gibson <david@gibson.dropbear.id.au>.
  5. * Based on ppc64 versions by: Dave Engebretsen, Todd Inglett, Don
  6. * Reed, Pat McCarthy, Peter Bergner, Anton Blanchard. They
  7. * originally took it from the ppc32 code.
  8. *
  9. * Within a word, bits are numbered LSB first. Lot's of places make
  10. * this assumption by directly testing bits with (val & (1<<nr)).
  11. * This can cause confusion for large (> 1 word) bitmaps on a
  12. * big-endian system because, unlike little endian, the number of each
  13. * bit depends on the word size.
  14. *
  15. * The bitop functions are defined to work on unsigned longs, so for a
  16. * ppc64 system the bits end up numbered:
  17. * |63..............0|127............64|191...........128|255...........196|
  18. * and on ppc32:
  19. * |31.....0|63....31|95....64|127...96|159..128|191..160|223..192|255..224|
  20. *
  21. * There are a few little-endian macros used mostly for filesystem
  22. * bitmaps, these work on similar bit arrays layouts, but
  23. * byte-oriented:
  24. * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
  25. *
  26. * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit
  27. * number field needs to be reversed compared to the big-endian bit
  28. * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).
  29. *
  30. * This program is free software; you can redistribute it and/or
  31. * modify it under the terms of the GNU General Public License
  32. * as published by the Free Software Foundation; either version
  33. * 2 of the License, or (at your option) any later version.
  34. */
  35. #ifndef _ASM_POWERPC_BITOPS_H
  36. #define _ASM_POWERPC_BITOPS_H
  37. #ifdef __KERNEL__
  38. #ifndef _LINUX_BITOPS_H
  39. #error only <linux/bitops.h> can be included directly
  40. #endif
  41. #include <linux/compiler.h>
  42. #include <asm/asm-compat.h>
  43. #include <asm/synch.h>
  44. /*
  45. * clear_bit doesn't imply a memory barrier
  46. */
  47. #define smp_mb__before_clear_bit() smp_mb()
  48. #define smp_mb__after_clear_bit() smp_mb()
  49. #define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  50. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  51. #define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
  52. static __inline__ void set_bit(int nr, volatile unsigned long *addr)
  53. {
  54. unsigned long old;
  55. unsigned long mask = BITOP_MASK(nr);
  56. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  57. __asm__ __volatile__(
  58. "1:" PPC_LLARX "%0,0,%3 # set_bit\n"
  59. "or %0,%0,%2\n"
  60. PPC405_ERR77(0,%3)
  61. PPC_STLCX "%0,0,%3\n"
  62. "bne- 1b"
  63. : "=&r" (old), "+m" (*p)
  64. : "r" (mask), "r" (p)
  65. : "cc" );
  66. }
  67. static __inline__ void clear_bit(int nr, volatile unsigned long *addr)
  68. {
  69. unsigned long old;
  70. unsigned long mask = BITOP_MASK(nr);
  71. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  72. __asm__ __volatile__(
  73. "1:" PPC_LLARX "%0,0,%3 # clear_bit\n"
  74. "andc %0,%0,%2\n"
  75. PPC405_ERR77(0,%3)
  76. PPC_STLCX "%0,0,%3\n"
  77. "bne- 1b"
  78. : "=&r" (old), "+m" (*p)
  79. : "r" (mask), "r" (p)
  80. : "cc" );
  81. }
  82. static __inline__ void clear_bit_unlock(int nr, volatile unsigned long *addr)
  83. {
  84. unsigned long old;
  85. unsigned long mask = BITOP_MASK(nr);
  86. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  87. __asm__ __volatile__(
  88. LWSYNC_ON_SMP
  89. "1:" PPC_LLARX "%0,0,%3 # clear_bit_unlock\n"
  90. "andc %0,%0,%2\n"
  91. PPC405_ERR77(0,%3)
  92. PPC_STLCX "%0,0,%3\n"
  93. "bne- 1b"
  94. : "=&r" (old), "+m" (*p)
  95. : "r" (mask), "r" (p)
  96. : "cc", "memory");
  97. }
  98. static __inline__ void change_bit(int nr, volatile unsigned long *addr)
  99. {
  100. unsigned long old;
  101. unsigned long mask = BITOP_MASK(nr);
  102. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  103. __asm__ __volatile__(
  104. "1:" PPC_LLARX "%0,0,%3 # change_bit\n"
  105. "xor %0,%0,%2\n"
  106. PPC405_ERR77(0,%3)
  107. PPC_STLCX "%0,0,%3\n"
  108. "bne- 1b"
  109. : "=&r" (old), "+m" (*p)
  110. : "r" (mask), "r" (p)
  111. : "cc" );
  112. }
  113. static __inline__ int test_and_set_bit(unsigned long nr,
  114. volatile unsigned long *addr)
  115. {
  116. unsigned long old, t;
  117. unsigned long mask = BITOP_MASK(nr);
  118. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  119. __asm__ __volatile__(
  120. LWSYNC_ON_SMP
  121. "1:" PPC_LLARX "%0,0,%3 # test_and_set_bit\n"
  122. "or %1,%0,%2 \n"
  123. PPC405_ERR77(0,%3)
  124. PPC_STLCX "%1,0,%3 \n"
  125. "bne- 1b"
  126. ISYNC_ON_SMP
  127. : "=&r" (old), "=&r" (t)
  128. : "r" (mask), "r" (p)
  129. : "cc", "memory");
  130. return (old & mask) != 0;
  131. }
  132. static __inline__ int test_and_set_bit_lock(unsigned long nr,
  133. volatile unsigned long *addr)
  134. {
  135. unsigned long old, t;
  136. unsigned long mask = BITOP_MASK(nr);
  137. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  138. __asm__ __volatile__(
  139. "1:" PPC_LLARX "%0,0,%3 # test_and_set_bit_lock\n"
  140. "or %1,%0,%2 \n"
  141. PPC405_ERR77(0,%3)
  142. PPC_STLCX "%1,0,%3 \n"
  143. "bne- 1b"
  144. ISYNC_ON_SMP
  145. : "=&r" (old), "=&r" (t)
  146. : "r" (mask), "r" (p)
  147. : "cc", "memory");
  148. return (old & mask) != 0;
  149. }
  150. static __inline__ int test_and_clear_bit(unsigned long nr,
  151. volatile unsigned long *addr)
  152. {
  153. unsigned long old, t;
  154. unsigned long mask = BITOP_MASK(nr);
  155. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  156. __asm__ __volatile__(
  157. LWSYNC_ON_SMP
  158. "1:" PPC_LLARX "%0,0,%3 # test_and_clear_bit\n"
  159. "andc %1,%0,%2 \n"
  160. PPC405_ERR77(0,%3)
  161. PPC_STLCX "%1,0,%3 \n"
  162. "bne- 1b"
  163. ISYNC_ON_SMP
  164. : "=&r" (old), "=&r" (t)
  165. : "r" (mask), "r" (p)
  166. : "cc", "memory");
  167. return (old & mask) != 0;
  168. }
  169. static __inline__ int test_and_change_bit(unsigned long nr,
  170. volatile unsigned long *addr)
  171. {
  172. unsigned long old, t;
  173. unsigned long mask = BITOP_MASK(nr);
  174. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  175. __asm__ __volatile__(
  176. LWSYNC_ON_SMP
  177. "1:" PPC_LLARX "%0,0,%3 # test_and_change_bit\n"
  178. "xor %1,%0,%2 \n"
  179. PPC405_ERR77(0,%3)
  180. PPC_STLCX "%1,0,%3 \n"
  181. "bne- 1b"
  182. ISYNC_ON_SMP
  183. : "=&r" (old), "=&r" (t)
  184. : "r" (mask), "r" (p)
  185. : "cc", "memory");
  186. return (old & mask) != 0;
  187. }
  188. static __inline__ void set_bits(unsigned long mask, unsigned long *addr)
  189. {
  190. unsigned long old;
  191. __asm__ __volatile__(
  192. "1:" PPC_LLARX "%0,0,%3 # set_bits\n"
  193. "or %0,%0,%2\n"
  194. PPC_STLCX "%0,0,%3\n"
  195. "bne- 1b"
  196. : "=&r" (old), "+m" (*addr)
  197. : "r" (mask), "r" (addr)
  198. : "cc");
  199. }
  200. #include <asm-generic/bitops/non-atomic.h>
  201. static __inline__ void __clear_bit_unlock(int nr, volatile unsigned long *addr)
  202. {
  203. __asm__ __volatile__(LWSYNC_ON_SMP "" ::: "memory");
  204. __clear_bit(nr, addr);
  205. }
  206. /*
  207. * Return the zero-based bit position (LE, not IBM bit numbering) of
  208. * the most significant 1-bit in a double word.
  209. */
  210. static __inline__ __attribute__((const))
  211. int __ilog2(unsigned long x)
  212. {
  213. int lz;
  214. asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x));
  215. return BITS_PER_LONG - 1 - lz;
  216. }
  217. static inline __attribute__((const))
  218. int __ilog2_u32(u32 n)
  219. {
  220. int bit;
  221. asm ("cntlzw %0,%1" : "=r" (bit) : "r" (n));
  222. return 31 - bit;
  223. }
  224. #ifdef __powerpc64__
  225. static inline __attribute__((const))
  226. int __ilog2_u64(u64 n)
  227. {
  228. int bit;
  229. asm ("cntlzd %0,%1" : "=r" (bit) : "r" (n));
  230. return 63 - bit;
  231. }
  232. #endif
  233. /*
  234. * Determines the bit position of the least significant 0 bit in the
  235. * specified double word. The returned bit position will be
  236. * zero-based, starting from the right side (63/31 - 0).
  237. */
  238. static __inline__ unsigned long ffz(unsigned long x)
  239. {
  240. /* no zero exists anywhere in the 8 byte area. */
  241. if ((x = ~x) == 0)
  242. return BITS_PER_LONG;
  243. /*
  244. * Calculate the bit position of the least signficant '1' bit in x
  245. * (since x has been changed this will actually be the least signficant
  246. * '0' bit in * the original x). Note: (x & -x) gives us a mask that
  247. * is the least significant * (RIGHT-most) 1-bit of the value in x.
  248. */
  249. return __ilog2(x & -x);
  250. }
  251. static __inline__ int __ffs(unsigned long x)
  252. {
  253. return __ilog2(x & -x);
  254. }
  255. /*
  256. * ffs: find first bit set. This is defined the same way as
  257. * the libc and compiler builtin ffs routines, therefore
  258. * differs in spirit from the above ffz (man ffs).
  259. */
  260. static __inline__ int ffs(int x)
  261. {
  262. unsigned long i = (unsigned long)x;
  263. return __ilog2(i & -i) + 1;
  264. }
  265. /*
  266. * fls: find last (most-significant) bit set.
  267. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  268. */
  269. static __inline__ int fls(unsigned int x)
  270. {
  271. int lz;
  272. asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
  273. return 32 - lz;
  274. }
  275. static __inline__ unsigned long __fls(unsigned long x)
  276. {
  277. return __ilog2(x);
  278. }
  279. /*
  280. * 64-bit can do this using one cntlzd (count leading zeroes doubleword)
  281. * instruction; for 32-bit we use the generic version, which does two
  282. * 32-bit fls calls.
  283. */
  284. #ifdef __powerpc64__
  285. static __inline__ int fls64(__u64 x)
  286. {
  287. int lz;
  288. asm ("cntlzd %0,%1" : "=r" (lz) : "r" (x));
  289. return 64 - lz;
  290. }
  291. #else
  292. #include <asm-generic/bitops/fls64.h>
  293. #endif /* __powerpc64__ */
  294. #include <asm-generic/bitops/hweight.h>
  295. #include <asm-generic/bitops/find.h>
  296. /* Little-endian versions */
  297. static __inline__ int test_le_bit(unsigned long nr,
  298. __const__ unsigned long *addr)
  299. {
  300. __const__ unsigned char *tmp = (__const__ unsigned char *) addr;
  301. return (tmp[nr >> 3] >> (nr & 7)) & 1;
  302. }
  303. #define __set_le_bit(nr, addr) \
  304. __set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
  305. #define __clear_le_bit(nr, addr) \
  306. __clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
  307. #define test_and_set_le_bit(nr, addr) \
  308. test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
  309. #define test_and_clear_le_bit(nr, addr) \
  310. test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
  311. #define __test_and_set_le_bit(nr, addr) \
  312. __test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
  313. #define __test_and_clear_le_bit(nr, addr) \
  314. __test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
  315. #define find_first_zero_le_bit(addr, size) generic_find_next_zero_le_bit((addr), (size), 0)
  316. unsigned long generic_find_next_zero_le_bit(const unsigned long *addr,
  317. unsigned long size, unsigned long offset);
  318. unsigned long generic_find_next_le_bit(const unsigned long *addr,
  319. unsigned long size, unsigned long offset);
  320. /* Bitmap functions for the ext2 filesystem */
  321. #define ext2_set_bit(nr,addr) \
  322. __test_and_set_le_bit((nr), (unsigned long*)addr)
  323. #define ext2_clear_bit(nr, addr) \
  324. __test_and_clear_le_bit((nr), (unsigned long*)addr)
  325. #define ext2_set_bit_atomic(lock, nr, addr) \
  326. test_and_set_le_bit((nr), (unsigned long*)addr)
  327. #define ext2_clear_bit_atomic(lock, nr, addr) \
  328. test_and_clear_le_bit((nr), (unsigned long*)addr)
  329. #define ext2_test_bit(nr, addr) test_le_bit((nr),(unsigned long*)addr)
  330. #define ext2_find_first_zero_bit(addr, size) \
  331. find_first_zero_le_bit((unsigned long*)addr, size)
  332. #define ext2_find_next_zero_bit(addr, size, off) \
  333. generic_find_next_zero_le_bit((unsigned long*)addr, size, off)
  334. #define ext2_find_next_bit(addr, size, off) \
  335. generic_find_next_le_bit((unsigned long *)addr, size, off)
  336. /* Bitmap functions for the minix filesystem. */
  337. #define minix_test_and_set_bit(nr,addr) \
  338. __test_and_set_le_bit(nr, (unsigned long *)addr)
  339. #define minix_set_bit(nr,addr) \
  340. __set_le_bit(nr, (unsigned long *)addr)
  341. #define minix_test_and_clear_bit(nr,addr) \
  342. __test_and_clear_le_bit(nr, (unsigned long *)addr)
  343. #define minix_test_bit(nr,addr) \
  344. test_le_bit(nr, (unsigned long *)addr)
  345. #define minix_find_first_zero_bit(addr,size) \
  346. find_first_zero_le_bit((unsigned long *)addr, size)
  347. #include <asm-generic/bitops/sched.h>
  348. #endif /* __KERNEL__ */
  349. #endif /* _ASM_POWERPC_BITOPS_H */