bitops.h 9.4 KB

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