bitops.h 9.4 KB

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