bitops.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #ifdef 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. #elif !defined(CONFIG_ARC_PLAT_EZNPS)
  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. #else /* CONFIG_ARC_PLAT_EZNPS */
  130. #define BIT_OP(op, c_op, asm_op) \
  131. static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
  132. { \
  133. m += nr >> 5; \
  134. \
  135. nr = (1UL << (nr & 0x1f)); \
  136. if (asm_op == CTOP_INST_AAND_DI_R2_R2_R3) \
  137. nr = ~nr; \
  138. \
  139. __asm__ __volatile__( \
  140. " mov r2, %0\n" \
  141. " mov r3, %1\n" \
  142. " .word %2\n" \
  143. : \
  144. : "r"(nr), "r"(m), "i"(asm_op) \
  145. : "r2", "r3", "memory"); \
  146. }
  147. #define TEST_N_BIT_OP(op, c_op, asm_op) \
  148. static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
  149. { \
  150. unsigned long old; \
  151. \
  152. m += nr >> 5; \
  153. \
  154. nr = old = (1UL << (nr & 0x1f)); \
  155. if (asm_op == CTOP_INST_AAND_DI_R2_R2_R3) \
  156. old = ~old; \
  157. \
  158. /* Explicit full memory barrier needed before/after */ \
  159. smp_mb(); \
  160. \
  161. __asm__ __volatile__( \
  162. " mov r2, %0\n" \
  163. " mov r3, %1\n" \
  164. " .word %2\n" \
  165. " mov %0, r2" \
  166. : "+r"(old) \
  167. : "r"(m), "i"(asm_op) \
  168. : "r2", "r3", "memory"); \
  169. \
  170. smp_mb(); \
  171. \
  172. return (old & nr) != 0; \
  173. }
  174. #endif /* CONFIG_ARC_PLAT_EZNPS */
  175. /***************************************
  176. * Non atomic variants
  177. **************************************/
  178. #define __BIT_OP(op, c_op, asm_op) \
  179. static inline void __##op##_bit(unsigned long nr, volatile unsigned long *m) \
  180. { \
  181. unsigned long temp; \
  182. m += nr >> 5; \
  183. \
  184. temp = *m; \
  185. *m = temp c_op (1UL << (nr & 0x1f)); \
  186. }
  187. #define __TEST_N_BIT_OP(op, c_op, asm_op) \
  188. static inline int __test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
  189. { \
  190. unsigned long old; \
  191. m += nr >> 5; \
  192. \
  193. old = *m; \
  194. *m = old c_op (1UL << (nr & 0x1f)); \
  195. \
  196. return (old & (1UL << (nr & 0x1f))) != 0; \
  197. }
  198. #define BIT_OPS(op, c_op, asm_op) \
  199. \
  200. /* set_bit(), clear_bit(), change_bit() */ \
  201. BIT_OP(op, c_op, asm_op) \
  202. \
  203. /* test_and_set_bit(), test_and_clear_bit(), test_and_change_bit() */\
  204. TEST_N_BIT_OP(op, c_op, asm_op) \
  205. \
  206. /* __set_bit(), __clear_bit(), __change_bit() */ \
  207. __BIT_OP(op, c_op, asm_op) \
  208. \
  209. /* __test_and_set_bit(), __test_and_clear_bit(), __test_and_change_bit() */\
  210. __TEST_N_BIT_OP(op, c_op, asm_op)
  211. #ifndef CONFIG_ARC_PLAT_EZNPS
  212. BIT_OPS(set, |, bset)
  213. BIT_OPS(clear, & ~, bclr)
  214. BIT_OPS(change, ^, bxor)
  215. #else
  216. BIT_OPS(set, |, CTOP_INST_AOR_DI_R2_R2_R3)
  217. BIT_OPS(clear, & ~, CTOP_INST_AAND_DI_R2_R2_R3)
  218. BIT_OPS(change, ^, CTOP_INST_AXOR_DI_R2_R2_R3)
  219. #endif
  220. /*
  221. * This routine doesn't need to be atomic.
  222. */
  223. static inline int
  224. test_bit(unsigned int nr, const volatile unsigned long *addr)
  225. {
  226. unsigned long mask;
  227. addr += nr >> 5;
  228. mask = 1UL << (nr & 0x1f);
  229. return ((mask & *addr) != 0);
  230. }
  231. #ifdef CONFIG_ISA_ARCOMPACT
  232. /*
  233. * Count the number of zeros, starting from MSB
  234. * Helper for fls( ) friends
  235. * This is a pure count, so (1-32) or (0-31) doesn't apply
  236. * It could be 0 to 32, based on num of 0's in there
  237. * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
  238. */
  239. static inline __attribute__ ((const)) int clz(unsigned int x)
  240. {
  241. unsigned int res;
  242. __asm__ __volatile__(
  243. " norm.f %0, %1 \n"
  244. " mov.n %0, 0 \n"
  245. " add.p %0, %0, 1 \n"
  246. : "=r"(res)
  247. : "r"(x)
  248. : "cc");
  249. return res;
  250. }
  251. static inline int constant_fls(int x)
  252. {
  253. int r = 32;
  254. if (!x)
  255. return 0;
  256. if (!(x & 0xffff0000u)) {
  257. x <<= 16;
  258. r -= 16;
  259. }
  260. if (!(x & 0xff000000u)) {
  261. x <<= 8;
  262. r -= 8;
  263. }
  264. if (!(x & 0xf0000000u)) {
  265. x <<= 4;
  266. r -= 4;
  267. }
  268. if (!(x & 0xc0000000u)) {
  269. x <<= 2;
  270. r -= 2;
  271. }
  272. if (!(x & 0x80000000u)) {
  273. x <<= 1;
  274. r -= 1;
  275. }
  276. return r;
  277. }
  278. /*
  279. * fls = Find Last Set in word
  280. * @result: [1-32]
  281. * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
  282. */
  283. static inline __attribute__ ((const)) int fls(unsigned long x)
  284. {
  285. if (__builtin_constant_p(x))
  286. return constant_fls(x);
  287. return 32 - clz(x);
  288. }
  289. /*
  290. * __fls: Similar to fls, but zero based (0-31)
  291. */
  292. static inline __attribute__ ((const)) int __fls(unsigned long x)
  293. {
  294. if (!x)
  295. return 0;
  296. else
  297. return fls(x) - 1;
  298. }
  299. /*
  300. * ffs = Find First Set in word (LSB to MSB)
  301. * @result: [1-32], 0 if all 0's
  302. */
  303. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  304. /*
  305. * __ffs: Similar to ffs, but zero based (0-31)
  306. */
  307. static inline __attribute__ ((const)) unsigned long __ffs(unsigned long word)
  308. {
  309. if (!word)
  310. return word;
  311. return ffs(word) - 1;
  312. }
  313. #else /* CONFIG_ISA_ARCV2 */
  314. /*
  315. * fls = Find Last Set in word
  316. * @result: [1-32]
  317. * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
  318. */
  319. static inline __attribute__ ((const)) int fls(unsigned long x)
  320. {
  321. int n;
  322. asm volatile(
  323. " fls.f %0, %1 \n" /* 0:31; 0(Z) if src 0 */
  324. " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
  325. : "=r"(n) /* Early clobber not needed */
  326. : "r"(x)
  327. : "cc");
  328. return n;
  329. }
  330. /*
  331. * __fls: Similar to fls, but zero based (0-31). Also 0 if no bit set
  332. */
  333. static inline __attribute__ ((const)) int __fls(unsigned long x)
  334. {
  335. /* FLS insn has exactly same semantics as the API */
  336. return __builtin_arc_fls(x);
  337. }
  338. /*
  339. * ffs = Find First Set in word (LSB to MSB)
  340. * @result: [1-32], 0 if all 0's
  341. */
  342. static inline __attribute__ ((const)) int ffs(unsigned long x)
  343. {
  344. int n;
  345. asm volatile(
  346. " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
  347. " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
  348. " mov.z %0, 0 \n" /* 31(Z)-> 0 */
  349. : "=r"(n) /* Early clobber not needed */
  350. : "r"(x)
  351. : "cc");
  352. return n;
  353. }
  354. /*
  355. * __ffs: Similar to ffs, but zero based (0-31)
  356. */
  357. static inline __attribute__ ((const)) unsigned long __ffs(unsigned long x)
  358. {
  359. unsigned long n;
  360. asm volatile(
  361. " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
  362. " mov.z %0, 0 \n" /* 31(Z)-> 0 */
  363. : "=r"(n)
  364. : "r"(x)
  365. : "cc");
  366. return n;
  367. }
  368. #endif /* CONFIG_ISA_ARCOMPACT */
  369. /*
  370. * ffz = Find First Zero in word.
  371. * @return:[0-31], 32 if all 1's
  372. */
  373. #define ffz(x) __ffs(~(x))
  374. #include <asm-generic/bitops/hweight.h>
  375. #include <asm-generic/bitops/fls64.h>
  376. #include <asm-generic/bitops/sched.h>
  377. #include <asm-generic/bitops/lock.h>
  378. #include <asm-generic/bitops/find.h>
  379. #include <asm-generic/bitops/le.h>
  380. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  381. #endif /* !__ASSEMBLY__ */
  382. #endif