bitops.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef _ASM_BITOPS_H
  9. #define _ASM_BITOPS_H
  10. #ifndef _LINUX_BITOPS_H
  11. #error only <linux/bitops.h> can be included directly
  12. #endif
  13. #ifndef __ASSEMBLY__
  14. #include <linux/types.h>
  15. #include <linux/compiler.h>
  16. #include <asm/barrier.h>
  17. #ifndef CONFIG_ARC_HAS_LLSC
  18. #include <asm/smp.h>
  19. #endif
  20. #if defined(CONFIG_ARC_HAS_LLSC)
  21. /*
  22. * Hardware assisted Atomic-R-M-W
  23. */
  24. #define BIT_OP(op, c_op, asm_op) \
  25. static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
  26. { \
  27. unsigned int temp; \
  28. \
  29. m += nr >> 5; \
  30. \
  31. nr &= 0x1f; \
  32. \
  33. __asm__ __volatile__( \
  34. "1: llock %0, [%1] \n" \
  35. " " #asm_op " %0, %0, %2 \n" \
  36. " scond %0, [%1] \n" \
  37. " bnz 1b \n" \
  38. : "=&r"(temp) /* Early clobber, to prevent reg reuse */ \
  39. : "r"(m), /* Not "m": llock only supports reg direct addr mode */ \
  40. "ir"(nr) \
  41. : "cc"); \
  42. }
  43. /*
  44. * Semantically:
  45. * Test the bit
  46. * if clear
  47. * set it and return 0 (old value)
  48. * else
  49. * return 1 (old value).
  50. *
  51. * Since ARC lacks a equivalent h/w primitive, the bit is set unconditionally
  52. * and the old value of bit is returned
  53. */
  54. #define TEST_N_BIT_OP(op, c_op, asm_op) \
  55. static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
  56. { \
  57. unsigned long old, temp; \
  58. \
  59. m += nr >> 5; \
  60. \
  61. nr &= 0x1f; \
  62. \
  63. /* \
  64. * Explicit full memory barrier needed before/after as \
  65. * LLOCK/SCOND themselves don't provide any such smenatic \
  66. */ \
  67. smp_mb(); \
  68. \
  69. __asm__ __volatile__( \
  70. "1: llock %0, [%2] \n" \
  71. " " #asm_op " %1, %0, %3 \n" \
  72. " scond %1, [%2] \n" \
  73. " bnz 1b \n" \
  74. : "=&r"(old), "=&r"(temp) \
  75. : "r"(m), "ir"(nr) \
  76. : "cc"); \
  77. \
  78. smp_mb(); \
  79. \
  80. return (old & (1 << nr)) != 0; \
  81. }
  82. #else /* !CONFIG_ARC_HAS_LLSC */
  83. /*
  84. * Non hardware assisted Atomic-R-M-W
  85. * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
  86. *
  87. * There's "significant" micro-optimization in writing our own variants of
  88. * bitops (over generic variants)
  89. *
  90. * (1) The generic APIs have "signed" @nr while we have it "unsigned"
  91. * This avoids extra code to be generated for pointer arithmatic, since
  92. * is "not sure" that index is NOT -ve
  93. * (2) Utilize the fact that ARCompact bit fidding insn (BSET/BCLR/ASL) etc
  94. * only consider bottom 5 bits of @nr, so NO need to mask them off.
  95. * (GCC Quirk: however for constant @nr we still need to do the masking
  96. * at compile time)
  97. */
  98. #define BIT_OP(op, c_op, asm_op) \
  99. static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
  100. { \
  101. unsigned long temp, flags; \
  102. m += nr >> 5; \
  103. \
  104. /* \
  105. * spin lock/unlock provide the needed smp_mb() before/after \
  106. */ \
  107. bitops_lock(flags); \
  108. \
  109. temp = *m; \
  110. *m = temp c_op (1UL << (nr & 0x1f)); \
  111. \
  112. bitops_unlock(flags); \
  113. }
  114. #define TEST_N_BIT_OP(op, c_op, asm_op) \
  115. static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
  116. { \
  117. unsigned long old, flags; \
  118. m += nr >> 5; \
  119. \
  120. bitops_lock(flags); \
  121. \
  122. old = *m; \
  123. *m = old c_op (1UL << (nr & 0x1f)); \
  124. \
  125. bitops_unlock(flags); \
  126. \
  127. return (old & (1UL << (nr & 0x1f))) != 0; \
  128. }
  129. #endif /* CONFIG_ARC_HAS_LLSC */
  130. /***************************************
  131. * Non atomic variants
  132. **************************************/
  133. #define __BIT_OP(op, c_op, asm_op) \
  134. static inline void __##op##_bit(unsigned long nr, volatile unsigned long *m) \
  135. { \
  136. unsigned long temp; \
  137. m += nr >> 5; \
  138. \
  139. temp = *m; \
  140. *m = temp c_op (1UL << (nr & 0x1f)); \
  141. }
  142. #define __TEST_N_BIT_OP(op, c_op, asm_op) \
  143. static inline int __test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
  144. { \
  145. unsigned long old; \
  146. m += nr >> 5; \
  147. \
  148. old = *m; \
  149. *m = old c_op (1UL << (nr & 0x1f)); \
  150. \
  151. return (old & (1UL << (nr & 0x1f))) != 0; \
  152. }
  153. #define BIT_OPS(op, c_op, asm_op) \
  154. \
  155. /* set_bit(), clear_bit(), change_bit() */ \
  156. BIT_OP(op, c_op, asm_op) \
  157. \
  158. /* test_and_set_bit(), test_and_clear_bit(), test_and_change_bit() */\
  159. TEST_N_BIT_OP(op, c_op, asm_op) \
  160. \
  161. /* __set_bit(), __clear_bit(), __change_bit() */ \
  162. __BIT_OP(op, c_op, asm_op) \
  163. \
  164. /* __test_and_set_bit(), __test_and_clear_bit(), __test_and_change_bit() */\
  165. __TEST_N_BIT_OP(op, c_op, asm_op)
  166. BIT_OPS(set, |, bset)
  167. BIT_OPS(clear, & ~, bclr)
  168. BIT_OPS(change, ^, bxor)
  169. /*
  170. * This routine doesn't need to be atomic.
  171. */
  172. static inline int
  173. test_bit(unsigned int nr, const volatile unsigned long *addr)
  174. {
  175. unsigned long mask;
  176. addr += nr >> 5;
  177. mask = 1UL << (nr & 0x1f);
  178. return ((mask & *addr) != 0);
  179. }
  180. #ifdef CONFIG_ISA_ARCOMPACT
  181. /*
  182. * Count the number of zeros, starting from MSB
  183. * Helper for fls( ) friends
  184. * This is a pure count, so (1-32) or (0-31) doesn't apply
  185. * It could be 0 to 32, based on num of 0's in there
  186. * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
  187. */
  188. static inline __attribute__ ((const)) int clz(unsigned int x)
  189. {
  190. unsigned int res;
  191. __asm__ __volatile__(
  192. " norm.f %0, %1 \n"
  193. " mov.n %0, 0 \n"
  194. " add.p %0, %0, 1 \n"
  195. : "=r"(res)
  196. : "r"(x)
  197. : "cc");
  198. return res;
  199. }
  200. static inline int constant_fls(int x)
  201. {
  202. int r = 32;
  203. if (!x)
  204. return 0;
  205. if (!(x & 0xffff0000u)) {
  206. x <<= 16;
  207. r -= 16;
  208. }
  209. if (!(x & 0xff000000u)) {
  210. x <<= 8;
  211. r -= 8;
  212. }
  213. if (!(x & 0xf0000000u)) {
  214. x <<= 4;
  215. r -= 4;
  216. }
  217. if (!(x & 0xc0000000u)) {
  218. x <<= 2;
  219. r -= 2;
  220. }
  221. if (!(x & 0x80000000u)) {
  222. x <<= 1;
  223. r -= 1;
  224. }
  225. return r;
  226. }
  227. /*
  228. * fls = Find Last Set in word
  229. * @result: [1-32]
  230. * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
  231. */
  232. static inline __attribute__ ((const)) int fls(unsigned long x)
  233. {
  234. if (__builtin_constant_p(x))
  235. return constant_fls(x);
  236. return 32 - clz(x);
  237. }
  238. /*
  239. * __fls: Similar to fls, but zero based (0-31)
  240. */
  241. static inline __attribute__ ((const)) int __fls(unsigned long x)
  242. {
  243. if (!x)
  244. return 0;
  245. else
  246. return fls(x) - 1;
  247. }
  248. /*
  249. * ffs = Find First Set in word (LSB to MSB)
  250. * @result: [1-32], 0 if all 0's
  251. */
  252. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  253. /*
  254. * __ffs: Similar to ffs, but zero based (0-31)
  255. */
  256. static inline __attribute__ ((const)) unsigned long __ffs(unsigned long word)
  257. {
  258. if (!word)
  259. return word;
  260. return ffs(word) - 1;
  261. }
  262. #else /* CONFIG_ISA_ARCV2 */
  263. /*
  264. * fls = Find Last Set in word
  265. * @result: [1-32]
  266. * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
  267. */
  268. static inline __attribute__ ((const)) int fls(unsigned long x)
  269. {
  270. int n;
  271. asm volatile(
  272. " fls.f %0, %1 \n" /* 0:31; 0(Z) if src 0 */
  273. " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
  274. : "=r"(n) /* Early clobber not needed */
  275. : "r"(x)
  276. : "cc");
  277. return n;
  278. }
  279. /*
  280. * __fls: Similar to fls, but zero based (0-31). Also 0 if no bit set
  281. */
  282. static inline __attribute__ ((const)) int __fls(unsigned long x)
  283. {
  284. /* FLS insn has exactly same semantics as the API */
  285. return __builtin_arc_fls(x);
  286. }
  287. /*
  288. * ffs = Find First Set in word (LSB to MSB)
  289. * @result: [1-32], 0 if all 0's
  290. */
  291. static inline __attribute__ ((const)) int ffs(unsigned long x)
  292. {
  293. int n;
  294. asm volatile(
  295. " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
  296. " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
  297. " mov.z %0, 0 \n" /* 31(Z)-> 0 */
  298. : "=r"(n) /* Early clobber not needed */
  299. : "r"(x)
  300. : "cc");
  301. return n;
  302. }
  303. /*
  304. * __ffs: Similar to ffs, but zero based (0-31)
  305. */
  306. static inline __attribute__ ((const)) unsigned long __ffs(unsigned long x)
  307. {
  308. unsigned long n;
  309. asm volatile(
  310. " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
  311. " mov.z %0, 0 \n" /* 31(Z)-> 0 */
  312. : "=r"(n)
  313. : "r"(x)
  314. : "cc");
  315. return n;
  316. }
  317. #endif /* CONFIG_ISA_ARCOMPACT */
  318. /*
  319. * ffz = Find First Zero in word.
  320. * @return:[0-31], 32 if all 1's
  321. */
  322. #define ffz(x) __ffs(~(x))
  323. #include <asm-generic/bitops/hweight.h>
  324. #include <asm-generic/bitops/fls64.h>
  325. #include <asm-generic/bitops/sched.h>
  326. #include <asm-generic/bitops/lock.h>
  327. #include <asm-generic/bitops/find.h>
  328. #include <asm-generic/bitops/le.h>
  329. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  330. #endif /* !__ASSEMBLY__ */
  331. #endif