bitops.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* bitops.h: bit operations for the Fujitsu FR-V CPUs
  2. *
  3. * For an explanation of how atomic ops work in this arch, see:
  4. * Documentation/frv/atomic-ops.txt
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #ifndef _ASM_BITOPS_H
  15. #define _ASM_BITOPS_H
  16. #include <linux/compiler.h>
  17. #include <asm/byteorder.h>
  18. #ifdef __KERNEL__
  19. #ifndef _LINUX_BITOPS_H
  20. #error only <linux/bitops.h> can be included directly
  21. #endif
  22. #include <asm-generic/bitops/ffz.h>
  23. /*
  24. * clear_bit() doesn't provide any barrier for the compiler.
  25. */
  26. #define smp_mb__before_clear_bit() barrier()
  27. #define smp_mb__after_clear_bit() barrier()
  28. #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
  29. static inline
  30. unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v)
  31. {
  32. unsigned long old, tmp;
  33. asm volatile(
  34. "0: \n"
  35. " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
  36. " ckeq icc3,cc7 \n"
  37. " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
  38. " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
  39. " and%I3 %1,%3,%2 \n"
  40. " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
  41. " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
  42. " beq icc3,#0,0b \n"
  43. : "+U"(*v), "=&r"(old), "=r"(tmp)
  44. : "NPr"(~mask)
  45. : "memory", "cc7", "cc3", "icc3"
  46. );
  47. return old;
  48. }
  49. static inline
  50. unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v)
  51. {
  52. unsigned long old, tmp;
  53. asm volatile(
  54. "0: \n"
  55. " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
  56. " ckeq icc3,cc7 \n"
  57. " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
  58. " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
  59. " or%I3 %1,%3,%2 \n"
  60. " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
  61. " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
  62. " beq icc3,#0,0b \n"
  63. : "+U"(*v), "=&r"(old), "=r"(tmp)
  64. : "NPr"(mask)
  65. : "memory", "cc7", "cc3", "icc3"
  66. );
  67. return old;
  68. }
  69. static inline
  70. unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v)
  71. {
  72. unsigned long old, tmp;
  73. asm volatile(
  74. "0: \n"
  75. " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
  76. " ckeq icc3,cc7 \n"
  77. " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
  78. " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
  79. " xor%I3 %1,%3,%2 \n"
  80. " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
  81. " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
  82. " beq icc3,#0,0b \n"
  83. : "+U"(*v), "=&r"(old), "=r"(tmp)
  84. : "NPr"(mask)
  85. : "memory", "cc7", "cc3", "icc3"
  86. );
  87. return old;
  88. }
  89. #else
  90. extern unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v);
  91. extern unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v);
  92. extern unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v);
  93. #endif
  94. #define atomic_clear_mask(mask, v) atomic_test_and_ANDNOT_mask((mask), (v))
  95. #define atomic_set_mask(mask, v) atomic_test_and_OR_mask((mask), (v))
  96. static inline int test_and_clear_bit(int nr, volatile void *addr)
  97. {
  98. volatile unsigned long *ptr = addr;
  99. unsigned long mask = 1UL << (nr & 31);
  100. ptr += nr >> 5;
  101. return (atomic_test_and_ANDNOT_mask(mask, ptr) & mask) != 0;
  102. }
  103. static inline int test_and_set_bit(int nr, volatile void *addr)
  104. {
  105. volatile unsigned long *ptr = addr;
  106. unsigned long mask = 1UL << (nr & 31);
  107. ptr += nr >> 5;
  108. return (atomic_test_and_OR_mask(mask, ptr) & mask) != 0;
  109. }
  110. static inline int test_and_change_bit(int nr, volatile void *addr)
  111. {
  112. volatile unsigned long *ptr = addr;
  113. unsigned long mask = 1UL << (nr & 31);
  114. ptr += nr >> 5;
  115. return (atomic_test_and_XOR_mask(mask, ptr) & mask) != 0;
  116. }
  117. static inline void clear_bit(int nr, volatile void *addr)
  118. {
  119. test_and_clear_bit(nr, addr);
  120. }
  121. static inline void set_bit(int nr, volatile void *addr)
  122. {
  123. test_and_set_bit(nr, addr);
  124. }
  125. static inline void change_bit(int nr, volatile void * addr)
  126. {
  127. test_and_change_bit(nr, addr);
  128. }
  129. static inline void __clear_bit(int nr, volatile void * addr)
  130. {
  131. volatile unsigned long *a = addr;
  132. int mask;
  133. a += nr >> 5;
  134. mask = 1 << (nr & 31);
  135. *a &= ~mask;
  136. }
  137. static inline void __set_bit(int nr, volatile void * addr)
  138. {
  139. volatile unsigned long *a = addr;
  140. int mask;
  141. a += nr >> 5;
  142. mask = 1 << (nr & 31);
  143. *a |= mask;
  144. }
  145. static inline void __change_bit(int nr, volatile void *addr)
  146. {
  147. volatile unsigned long *a = addr;
  148. int mask;
  149. a += nr >> 5;
  150. mask = 1 << (nr & 31);
  151. *a ^= mask;
  152. }
  153. static inline int __test_and_clear_bit(int nr, volatile void * addr)
  154. {
  155. volatile unsigned long *a = addr;
  156. int mask, retval;
  157. a += nr >> 5;
  158. mask = 1 << (nr & 31);
  159. retval = (mask & *a) != 0;
  160. *a &= ~mask;
  161. return retval;
  162. }
  163. static inline int __test_and_set_bit(int nr, volatile void * addr)
  164. {
  165. volatile unsigned long *a = addr;
  166. int mask, retval;
  167. a += nr >> 5;
  168. mask = 1 << (nr & 31);
  169. retval = (mask & *a) != 0;
  170. *a |= mask;
  171. return retval;
  172. }
  173. static inline int __test_and_change_bit(int nr, volatile void * addr)
  174. {
  175. volatile unsigned long *a = addr;
  176. int mask, retval;
  177. a += nr >> 5;
  178. mask = 1 << (nr & 31);
  179. retval = (mask & *a) != 0;
  180. *a ^= mask;
  181. return retval;
  182. }
  183. /*
  184. * This routine doesn't need to be atomic.
  185. */
  186. static inline int __constant_test_bit(int nr, const volatile void * addr)
  187. {
  188. return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
  189. }
  190. static inline int __test_bit(int nr, const volatile void * addr)
  191. {
  192. int * a = (int *) addr;
  193. int mask;
  194. a += nr >> 5;
  195. mask = 1 << (nr & 0x1f);
  196. return ((mask & *a) != 0);
  197. }
  198. #define test_bit(nr,addr) \
  199. (__builtin_constant_p(nr) ? \
  200. __constant_test_bit((nr),(addr)) : \
  201. __test_bit((nr),(addr)))
  202. #include <asm-generic/bitops/find.h>
  203. /**
  204. * fls - find last bit set
  205. * @x: the word to search
  206. *
  207. * This is defined the same way as ffs:
  208. * - return 32..1 to indicate bit 31..0 most significant bit set
  209. * - return 0 to indicate no bits set
  210. */
  211. #define fls(x) \
  212. ({ \
  213. int bit; \
  214. \
  215. asm(" subcc %1,gr0,gr0,icc0 \n" \
  216. " ckne icc0,cc4 \n" \
  217. " cscan.p %1,gr0,%0 ,cc4,#1 \n" \
  218. " csub %0,%0,%0 ,cc4,#0 \n" \
  219. " csub %2,%0,%0 ,cc4,#1 \n" \
  220. : "=&r"(bit) \
  221. : "r"(x), "r"(32) \
  222. : "icc0", "cc4" \
  223. ); \
  224. \
  225. bit; \
  226. })
  227. /**
  228. * fls64 - find last bit set in a 64-bit value
  229. * @n: the value to search
  230. *
  231. * This is defined the same way as ffs:
  232. * - return 64..1 to indicate bit 63..0 most significant bit set
  233. * - return 0 to indicate no bits set
  234. */
  235. static inline __attribute__((const))
  236. int fls64(u64 n)
  237. {
  238. union {
  239. u64 ll;
  240. struct { u32 h, l; };
  241. } _;
  242. int bit, x, y;
  243. _.ll = n;
  244. asm(" subcc.p %3,gr0,gr0,icc0 \n"
  245. " subcc %4,gr0,gr0,icc1 \n"
  246. " ckne icc0,cc4 \n"
  247. " ckne icc1,cc5 \n"
  248. " norcr cc4,cc5,cc6 \n"
  249. " csub.p %0,%0,%0 ,cc6,1 \n"
  250. " orcr cc5,cc4,cc4 \n"
  251. " andcr cc4,cc5,cc4 \n"
  252. " cscan.p %3,gr0,%0 ,cc4,0 \n"
  253. " setlos #64,%1 \n"
  254. " cscan.p %4,gr0,%0 ,cc4,1 \n"
  255. " setlos #32,%2 \n"
  256. " csub.p %1,%0,%0 ,cc4,0 \n"
  257. " csub %2,%0,%0 ,cc4,1 \n"
  258. : "=&r"(bit), "=r"(x), "=r"(y)
  259. : "0r"(_.h), "r"(_.l)
  260. : "icc0", "icc1", "cc4", "cc5", "cc6"
  261. );
  262. return bit;
  263. }
  264. /**
  265. * ffs - find first bit set
  266. * @x: the word to search
  267. *
  268. * - return 32..1 to indicate bit 31..0 most least significant bit set
  269. * - return 0 to indicate no bits set
  270. */
  271. static inline __attribute__((const))
  272. int ffs(int x)
  273. {
  274. /* Note: (x & -x) gives us a mask that is the least significant
  275. * (rightmost) 1-bit of the value in x.
  276. */
  277. return fls(x & -x);
  278. }
  279. /**
  280. * __ffs - find first bit set
  281. * @x: the word to search
  282. *
  283. * - return 31..0 to indicate bit 31..0 most least significant bit set
  284. * - if no bits are set in x, the result is undefined
  285. */
  286. static inline __attribute__((const))
  287. int __ffs(unsigned long x)
  288. {
  289. int bit;
  290. asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x & -x));
  291. return 31 - bit;
  292. }
  293. /**
  294. * __fls - find last (most-significant) set bit in a long word
  295. * @word: the word to search
  296. *
  297. * Undefined if no set bit exists, so code should check against 0 first.
  298. */
  299. static inline unsigned long __fls(unsigned long word)
  300. {
  301. unsigned long bit;
  302. asm("scan %1,gr0,%0" : "=r"(bit) : "r"(word));
  303. return bit;
  304. }
  305. /*
  306. * special slimline version of fls() for calculating ilog2_u32()
  307. * - note: no protection against n == 0
  308. */
  309. #define ARCH_HAS_ILOG2_U32
  310. static inline __attribute__((const))
  311. int __ilog2_u32(u32 n)
  312. {
  313. int bit;
  314. asm("scan %1,gr0,%0" : "=r"(bit) : "r"(n));
  315. return 31 - bit;
  316. }
  317. /*
  318. * special slimline version of fls64() for calculating ilog2_u64()
  319. * - note: no protection against n == 0
  320. */
  321. #define ARCH_HAS_ILOG2_U64
  322. static inline __attribute__((const))
  323. int __ilog2_u64(u64 n)
  324. {
  325. union {
  326. u64 ll;
  327. struct { u32 h, l; };
  328. } _;
  329. int bit, x, y;
  330. _.ll = n;
  331. asm(" subcc %3,gr0,gr0,icc0 \n"
  332. " ckeq icc0,cc4 \n"
  333. " cscan.p %3,gr0,%0 ,cc4,0 \n"
  334. " setlos #63,%1 \n"
  335. " cscan.p %4,gr0,%0 ,cc4,1 \n"
  336. " setlos #31,%2 \n"
  337. " csub.p %1,%0,%0 ,cc4,0 \n"
  338. " csub %2,%0,%0 ,cc4,1 \n"
  339. : "=&r"(bit), "=r"(x), "=r"(y)
  340. : "0r"(_.h), "r"(_.l)
  341. : "icc0", "cc4"
  342. );
  343. return bit;
  344. }
  345. #include <asm-generic/bitops/sched.h>
  346. #include <asm-generic/bitops/hweight.h>
  347. #include <asm-generic/bitops/lock.h>
  348. #include <asm-generic/bitops/ext2-non-atomic.h>
  349. #define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit ((nr) ^ 0x18, (addr))
  350. #define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr) ^ 0x18, (addr))
  351. #include <asm-generic/bitops/minix-le.h>
  352. #endif /* __KERNEL__ */
  353. #endif /* _ASM_BITOPS_H */