bitops.h 9.1 KB

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