bitops.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. #ifdef __KERNEL__
  14. #ifndef __ASSEMBLY__
  15. #include <linux/types.h>
  16. #include <linux/compiler.h>
  17. /*
  18. * Hardware assisted read-modify-write using ARC700 LLOCK/SCOND insns.
  19. * The Kconfig glue ensures that in SMP, this is only set if the container
  20. * SoC/platform has cross-core coherent LLOCK/SCOND
  21. */
  22. #if defined(CONFIG_ARC_HAS_LLSC)
  23. static inline void set_bit(unsigned long nr, volatile unsigned long *m)
  24. {
  25. unsigned int temp;
  26. m += nr >> 5;
  27. if (__builtin_constant_p(nr))
  28. nr &= 0x1f;
  29. __asm__ __volatile__(
  30. "1: llock %0, [%1] \n"
  31. " bset %0, %0, %2 \n"
  32. " scond %0, [%1] \n"
  33. " bnz 1b \n"
  34. : "=&r"(temp)
  35. : "r"(m), "ir"(nr)
  36. : "cc");
  37. }
  38. static inline void clear_bit(unsigned long nr, volatile unsigned long *m)
  39. {
  40. unsigned int temp;
  41. m += nr >> 5;
  42. if (__builtin_constant_p(nr))
  43. nr &= 0x1f;
  44. __asm__ __volatile__(
  45. "1: llock %0, [%1] \n"
  46. " bclr %0, %0, %2 \n"
  47. " scond %0, [%1] \n"
  48. " bnz 1b \n"
  49. : "=&r"(temp)
  50. : "r"(m), "ir"(nr)
  51. : "cc");
  52. }
  53. static inline void change_bit(unsigned long nr, volatile unsigned long *m)
  54. {
  55. unsigned int temp;
  56. m += nr >> 5;
  57. if (__builtin_constant_p(nr))
  58. nr &= 0x1f;
  59. __asm__ __volatile__(
  60. "1: llock %0, [%1] \n"
  61. " bxor %0, %0, %2 \n"
  62. " scond %0, [%1] \n"
  63. " bnz 1b \n"
  64. : "=&r"(temp)
  65. : "r"(m), "ir"(nr)
  66. : "cc");
  67. }
  68. /*
  69. * Semantically:
  70. * Test the bit
  71. * if clear
  72. * set it and return 0 (old value)
  73. * else
  74. * return 1 (old value).
  75. *
  76. * Since ARC lacks a equivalent h/w primitive, the bit is set unconditionally
  77. * and the old value of bit is returned
  78. */
  79. static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m)
  80. {
  81. unsigned long old, temp;
  82. m += nr >> 5;
  83. if (__builtin_constant_p(nr))
  84. nr &= 0x1f;
  85. __asm__ __volatile__(
  86. "1: llock %0, [%2] \n"
  87. " bset %1, %0, %3 \n"
  88. " scond %1, [%2] \n"
  89. " bnz 1b \n"
  90. : "=&r"(old), "=&r"(temp)
  91. : "r"(m), "ir"(nr)
  92. : "cc");
  93. return (old & (1 << nr)) != 0;
  94. }
  95. static inline int
  96. test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
  97. {
  98. unsigned int old, temp;
  99. m += nr >> 5;
  100. if (__builtin_constant_p(nr))
  101. nr &= 0x1f;
  102. __asm__ __volatile__(
  103. "1: llock %0, [%2] \n"
  104. " bclr %1, %0, %3 \n"
  105. " scond %1, [%2] \n"
  106. " bnz 1b \n"
  107. : "=&r"(old), "=&r"(temp)
  108. : "r"(m), "ir"(nr)
  109. : "cc");
  110. return (old & (1 << nr)) != 0;
  111. }
  112. static inline int
  113. test_and_change_bit(unsigned long nr, volatile unsigned long *m)
  114. {
  115. unsigned int old, temp;
  116. m += nr >> 5;
  117. if (__builtin_constant_p(nr))
  118. nr &= 0x1f;
  119. __asm__ __volatile__(
  120. "1: llock %0, [%2] \n"
  121. " bxor %1, %0, %3 \n"
  122. " scond %1, [%2] \n"
  123. " bnz 1b \n"
  124. : "=&r"(old), "=&r"(temp)
  125. : "r"(m), "ir"(nr)
  126. : "cc");
  127. return (old & (1 << nr)) != 0;
  128. }
  129. #else /* !CONFIG_ARC_HAS_LLSC */
  130. #include <asm/smp.h>
  131. /*
  132. * Non hardware assisted Atomic-R-M-W
  133. * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
  134. *
  135. * There's "significant" micro-optimization in writing our own variants of
  136. * bitops (over generic variants)
  137. *
  138. * (1) The generic APIs have "signed" @nr while we have it "unsigned"
  139. * This avoids extra code to be generated for pointer arithmatic, since
  140. * is "not sure" that index is NOT -ve
  141. * (2) Utilize the fact that ARCompact bit fidding insn (BSET/BCLR/ASL) etc
  142. * only consider bottom 5 bits of @nr, so NO need to mask them off.
  143. * (GCC Quirk: however for constant @nr we still need to do the masking
  144. * at compile time)
  145. */
  146. static inline void set_bit(unsigned long nr, volatile unsigned long *m)
  147. {
  148. unsigned long temp, flags;
  149. m += nr >> 5;
  150. if (__builtin_constant_p(nr))
  151. nr &= 0x1f;
  152. bitops_lock(flags);
  153. temp = *m;
  154. *m = temp | (1UL << nr);
  155. bitops_unlock(flags);
  156. }
  157. static inline void clear_bit(unsigned long nr, volatile unsigned long *m)
  158. {
  159. unsigned long temp, flags;
  160. m += nr >> 5;
  161. if (__builtin_constant_p(nr))
  162. nr &= 0x1f;
  163. bitops_lock(flags);
  164. temp = *m;
  165. *m = temp & ~(1UL << nr);
  166. bitops_unlock(flags);
  167. }
  168. static inline void change_bit(unsigned long nr, volatile unsigned long *m)
  169. {
  170. unsigned long temp, flags;
  171. m += nr >> 5;
  172. if (__builtin_constant_p(nr))
  173. nr &= 0x1f;
  174. bitops_lock(flags);
  175. temp = *m;
  176. *m = temp ^ (1UL << nr);
  177. bitops_unlock(flags);
  178. }
  179. static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m)
  180. {
  181. unsigned long old, flags;
  182. m += nr >> 5;
  183. if (__builtin_constant_p(nr))
  184. nr &= 0x1f;
  185. bitops_lock(flags);
  186. old = *m;
  187. *m = old | (1 << nr);
  188. bitops_unlock(flags);
  189. return (old & (1 << nr)) != 0;
  190. }
  191. static inline int
  192. test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
  193. {
  194. unsigned long old, flags;
  195. m += nr >> 5;
  196. if (__builtin_constant_p(nr))
  197. nr &= 0x1f;
  198. bitops_lock(flags);
  199. old = *m;
  200. *m = old & ~(1 << nr);
  201. bitops_unlock(flags);
  202. return (old & (1 << nr)) != 0;
  203. }
  204. static inline int
  205. test_and_change_bit(unsigned long nr, volatile unsigned long *m)
  206. {
  207. unsigned long old, flags;
  208. m += nr >> 5;
  209. if (__builtin_constant_p(nr))
  210. nr &= 0x1f;
  211. bitops_lock(flags);
  212. old = *m;
  213. *m = old ^ (1 << nr);
  214. bitops_unlock(flags);
  215. return (old & (1 << nr)) != 0;
  216. }
  217. #endif /* CONFIG_ARC_HAS_LLSC */
  218. /***************************************
  219. * Non atomic variants
  220. **************************************/
  221. static inline void __set_bit(unsigned long nr, volatile unsigned long *m)
  222. {
  223. unsigned long temp;
  224. m += nr >> 5;
  225. if (__builtin_constant_p(nr))
  226. nr &= 0x1f;
  227. temp = *m;
  228. *m = temp | (1UL << nr);
  229. }
  230. static inline void __clear_bit(unsigned long nr, volatile unsigned long *m)
  231. {
  232. unsigned long temp;
  233. m += nr >> 5;
  234. if (__builtin_constant_p(nr))
  235. nr &= 0x1f;
  236. temp = *m;
  237. *m = temp & ~(1UL << nr);
  238. }
  239. static inline void __change_bit(unsigned long nr, volatile unsigned long *m)
  240. {
  241. unsigned long temp;
  242. m += nr >> 5;
  243. if (__builtin_constant_p(nr))
  244. nr &= 0x1f;
  245. temp = *m;
  246. *m = temp ^ (1UL << nr);
  247. }
  248. static inline int
  249. __test_and_set_bit(unsigned long nr, volatile unsigned long *m)
  250. {
  251. unsigned long old;
  252. m += nr >> 5;
  253. if (__builtin_constant_p(nr))
  254. nr &= 0x1f;
  255. old = *m;
  256. *m = old | (1 << nr);
  257. return (old & (1 << nr)) != 0;
  258. }
  259. static inline int
  260. __test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
  261. {
  262. unsigned long old;
  263. m += nr >> 5;
  264. if (__builtin_constant_p(nr))
  265. nr &= 0x1f;
  266. old = *m;
  267. *m = old & ~(1 << nr);
  268. return (old & (1 << nr)) != 0;
  269. }
  270. static inline int
  271. __test_and_change_bit(unsigned long nr, volatile unsigned long *m)
  272. {
  273. unsigned long old;
  274. m += nr >> 5;
  275. if (__builtin_constant_p(nr))
  276. nr &= 0x1f;
  277. old = *m;
  278. *m = old ^ (1 << nr);
  279. return (old & (1 << nr)) != 0;
  280. }
  281. /*
  282. * This routine doesn't need to be atomic.
  283. */
  284. static inline int
  285. __constant_test_bit(unsigned int nr, const volatile unsigned long *addr)
  286. {
  287. return ((1UL << (nr & 31)) &
  288. (((const volatile unsigned int *)addr)[nr >> 5])) != 0;
  289. }
  290. static inline int
  291. __test_bit(unsigned int nr, const volatile unsigned long *addr)
  292. {
  293. unsigned long mask;
  294. addr += nr >> 5;
  295. /* ARC700 only considers 5 bits in bit-fiddling insn */
  296. mask = 1 << nr;
  297. return ((mask & *addr) != 0);
  298. }
  299. #define test_bit(nr, addr) (__builtin_constant_p(nr) ? \
  300. __constant_test_bit((nr), (addr)) : \
  301. __test_bit((nr), (addr)))
  302. /*
  303. * Count the number of zeros, starting from MSB
  304. * Helper for fls( ) friends
  305. * This is a pure count, so (1-32) or (0-31) doesn't apply
  306. * It could be 0 to 32, based on num of 0's in there
  307. * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
  308. */
  309. static inline __attribute__ ((const)) int clz(unsigned int x)
  310. {
  311. unsigned int res;
  312. __asm__ __volatile__(
  313. " norm.f %0, %1 \n"
  314. " mov.n %0, 0 \n"
  315. " add.p %0, %0, 1 \n"
  316. : "=r"(res)
  317. : "r"(x)
  318. : "cc");
  319. return res;
  320. }
  321. static inline int constant_fls(int x)
  322. {
  323. int r = 32;
  324. if (!x)
  325. return 0;
  326. if (!(x & 0xffff0000u)) {
  327. x <<= 16;
  328. r -= 16;
  329. }
  330. if (!(x & 0xff000000u)) {
  331. x <<= 8;
  332. r -= 8;
  333. }
  334. if (!(x & 0xf0000000u)) {
  335. x <<= 4;
  336. r -= 4;
  337. }
  338. if (!(x & 0xc0000000u)) {
  339. x <<= 2;
  340. r -= 2;
  341. }
  342. if (!(x & 0x80000000u)) {
  343. x <<= 1;
  344. r -= 1;
  345. }
  346. return r;
  347. }
  348. /*
  349. * fls = Find Last Set in word
  350. * @result: [1-32]
  351. * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
  352. */
  353. static inline __attribute__ ((const)) int fls(unsigned long x)
  354. {
  355. if (__builtin_constant_p(x))
  356. return constant_fls(x);
  357. return 32 - clz(x);
  358. }
  359. /*
  360. * __fls: Similar to fls, but zero based (0-31)
  361. */
  362. static inline __attribute__ ((const)) int __fls(unsigned long x)
  363. {
  364. if (!x)
  365. return 0;
  366. else
  367. return fls(x) - 1;
  368. }
  369. /*
  370. * ffs = Find First Set in word (LSB to MSB)
  371. * @result: [1-32], 0 if all 0's
  372. */
  373. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  374. /*
  375. * __ffs: Similar to ffs, but zero based (0-31)
  376. */
  377. static inline __attribute__ ((const)) int __ffs(unsigned long word)
  378. {
  379. if (!word)
  380. return word;
  381. return ffs(word) - 1;
  382. }
  383. /*
  384. * ffz = Find First Zero in word.
  385. * @return:[0-31], 32 if all 1's
  386. */
  387. #define ffz(x) __ffs(~(x))
  388. /* TODO does this affect uni-processor code */
  389. #define smp_mb__before_clear_bit() barrier()
  390. #define smp_mb__after_clear_bit() barrier()
  391. #include <asm-generic/bitops/hweight.h>
  392. #include <asm-generic/bitops/fls64.h>
  393. #include <asm-generic/bitops/sched.h>
  394. #include <asm-generic/bitops/lock.h>
  395. #include <asm-generic/bitops/find.h>
  396. #include <asm-generic/bitops/le.h>
  397. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  398. #endif /* !__ASSEMBLY__ */
  399. #endif /* __KERNEL__ */
  400. #endif