bitops.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. /*
  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. /*
  86. * Explicit full memory barrier needed before/after as
  87. * LLOCK/SCOND themselves don't provide any such semantics
  88. */
  89. smp_mb();
  90. __asm__ __volatile__(
  91. "1: llock %0, [%2] \n"
  92. " bset %1, %0, %3 \n"
  93. " scond %1, [%2] \n"
  94. " bnz 1b \n"
  95. : "=&r"(old), "=&r"(temp)
  96. : "r"(m), "ir"(nr)
  97. : "cc");
  98. smp_mb();
  99. return (old & (1 << nr)) != 0;
  100. }
  101. static inline int
  102. test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
  103. {
  104. unsigned int old, temp;
  105. m += nr >> 5;
  106. if (__builtin_constant_p(nr))
  107. nr &= 0x1f;
  108. smp_mb();
  109. __asm__ __volatile__(
  110. "1: llock %0, [%2] \n"
  111. " bclr %1, %0, %3 \n"
  112. " scond %1, [%2] \n"
  113. " bnz 1b \n"
  114. : "=&r"(old), "=&r"(temp)
  115. : "r"(m), "ir"(nr)
  116. : "cc");
  117. smp_mb();
  118. return (old & (1 << nr)) != 0;
  119. }
  120. static inline int
  121. test_and_change_bit(unsigned long nr, volatile unsigned long *m)
  122. {
  123. unsigned int old, temp;
  124. m += nr >> 5;
  125. if (__builtin_constant_p(nr))
  126. nr &= 0x1f;
  127. smp_mb();
  128. __asm__ __volatile__(
  129. "1: llock %0, [%2] \n"
  130. " bxor %1, %0, %3 \n"
  131. " scond %1, [%2] \n"
  132. " bnz 1b \n"
  133. : "=&r"(old), "=&r"(temp)
  134. : "r"(m), "ir"(nr)
  135. : "cc");
  136. smp_mb();
  137. return (old & (1 << nr)) != 0;
  138. }
  139. #else /* !CONFIG_ARC_HAS_LLSC */
  140. #include <asm/smp.h>
  141. /*
  142. * Non hardware assisted Atomic-R-M-W
  143. * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
  144. *
  145. * There's "significant" micro-optimization in writing our own variants of
  146. * bitops (over generic variants)
  147. *
  148. * (1) The generic APIs have "signed" @nr while we have it "unsigned"
  149. * This avoids extra code to be generated for pointer arithmatic, since
  150. * is "not sure" that index is NOT -ve
  151. * (2) Utilize the fact that ARCompact bit fidding insn (BSET/BCLR/ASL) etc
  152. * only consider bottom 5 bits of @nr, so NO need to mask them off.
  153. * (GCC Quirk: however for constant @nr we still need to do the masking
  154. * at compile time)
  155. */
  156. static inline void set_bit(unsigned long nr, volatile unsigned long *m)
  157. {
  158. unsigned long temp, flags;
  159. m += nr >> 5;
  160. if (__builtin_constant_p(nr))
  161. nr &= 0x1f;
  162. bitops_lock(flags);
  163. temp = *m;
  164. *m = temp | (1UL << nr);
  165. bitops_unlock(flags);
  166. }
  167. static inline void clear_bit(unsigned long nr, volatile unsigned long *m)
  168. {
  169. unsigned long temp, flags;
  170. m += nr >> 5;
  171. if (__builtin_constant_p(nr))
  172. nr &= 0x1f;
  173. bitops_lock(flags);
  174. temp = *m;
  175. *m = temp & ~(1UL << nr);
  176. bitops_unlock(flags);
  177. }
  178. static inline void change_bit(unsigned long nr, volatile unsigned long *m)
  179. {
  180. unsigned long temp, flags;
  181. m += nr >> 5;
  182. if (__builtin_constant_p(nr))
  183. nr &= 0x1f;
  184. bitops_lock(flags);
  185. temp = *m;
  186. *m = temp ^ (1UL << nr);
  187. bitops_unlock(flags);
  188. }
  189. static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m)
  190. {
  191. unsigned long old, flags;
  192. m += nr >> 5;
  193. if (__builtin_constant_p(nr))
  194. nr &= 0x1f;
  195. /*
  196. * spin lock/unlock provide the needed smp_mb() before/after
  197. */
  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_clear_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. static inline int
  218. test_and_change_bit(unsigned long nr, volatile unsigned long *m)
  219. {
  220. unsigned long old, flags;
  221. m += nr >> 5;
  222. if (__builtin_constant_p(nr))
  223. nr &= 0x1f;
  224. bitops_lock(flags);
  225. old = *m;
  226. *m = old ^ (1 << nr);
  227. bitops_unlock(flags);
  228. return (old & (1 << nr)) != 0;
  229. }
  230. #endif /* CONFIG_ARC_HAS_LLSC */
  231. /***************************************
  232. * Non atomic variants
  233. **************************************/
  234. static inline void __set_bit(unsigned long nr, volatile unsigned long *m)
  235. {
  236. unsigned long temp;
  237. m += nr >> 5;
  238. if (__builtin_constant_p(nr))
  239. nr &= 0x1f;
  240. temp = *m;
  241. *m = temp | (1UL << nr);
  242. }
  243. static inline void __clear_bit(unsigned long nr, volatile unsigned long *m)
  244. {
  245. unsigned long temp;
  246. m += nr >> 5;
  247. if (__builtin_constant_p(nr))
  248. nr &= 0x1f;
  249. temp = *m;
  250. *m = temp & ~(1UL << nr);
  251. }
  252. static inline void __change_bit(unsigned long nr, volatile unsigned long *m)
  253. {
  254. unsigned long temp;
  255. m += nr >> 5;
  256. if (__builtin_constant_p(nr))
  257. nr &= 0x1f;
  258. temp = *m;
  259. *m = temp ^ (1UL << nr);
  260. }
  261. static inline int
  262. __test_and_set_bit(unsigned long nr, volatile unsigned long *m)
  263. {
  264. unsigned long old;
  265. m += nr >> 5;
  266. if (__builtin_constant_p(nr))
  267. nr &= 0x1f;
  268. old = *m;
  269. *m = old | (1 << nr);
  270. return (old & (1 << nr)) != 0;
  271. }
  272. static inline int
  273. __test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
  274. {
  275. unsigned long old;
  276. m += nr >> 5;
  277. if (__builtin_constant_p(nr))
  278. nr &= 0x1f;
  279. old = *m;
  280. *m = old & ~(1 << nr);
  281. return (old & (1 << nr)) != 0;
  282. }
  283. static inline int
  284. __test_and_change_bit(unsigned long nr, volatile unsigned long *m)
  285. {
  286. unsigned long old;
  287. m += nr >> 5;
  288. if (__builtin_constant_p(nr))
  289. nr &= 0x1f;
  290. old = *m;
  291. *m = old ^ (1 << nr);
  292. return (old & (1 << nr)) != 0;
  293. }
  294. /*
  295. * This routine doesn't need to be atomic.
  296. */
  297. static inline int
  298. __constant_test_bit(unsigned int nr, const volatile unsigned long *addr)
  299. {
  300. return ((1UL << (nr & 31)) &
  301. (((const volatile unsigned int *)addr)[nr >> 5])) != 0;
  302. }
  303. static inline int
  304. __test_bit(unsigned int nr, const volatile unsigned long *addr)
  305. {
  306. unsigned long mask;
  307. addr += nr >> 5;
  308. /* ARC700 only considers 5 bits in bit-fiddling insn */
  309. mask = 1 << nr;
  310. return ((mask & *addr) != 0);
  311. }
  312. #define test_bit(nr, addr) (__builtin_constant_p(nr) ? \
  313. __constant_test_bit((nr), (addr)) : \
  314. __test_bit((nr), (addr)))
  315. /*
  316. * Count the number of zeros, starting from MSB
  317. * Helper for fls( ) friends
  318. * This is a pure count, so (1-32) or (0-31) doesn't apply
  319. * It could be 0 to 32, based on num of 0's in there
  320. * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
  321. */
  322. static inline __attribute__ ((const)) int clz(unsigned int x)
  323. {
  324. unsigned int res;
  325. __asm__ __volatile__(
  326. " norm.f %0, %1 \n"
  327. " mov.n %0, 0 \n"
  328. " add.p %0, %0, 1 \n"
  329. : "=r"(res)
  330. : "r"(x)
  331. : "cc");
  332. return res;
  333. }
  334. static inline int constant_fls(int x)
  335. {
  336. int r = 32;
  337. if (!x)
  338. return 0;
  339. if (!(x & 0xffff0000u)) {
  340. x <<= 16;
  341. r -= 16;
  342. }
  343. if (!(x & 0xff000000u)) {
  344. x <<= 8;
  345. r -= 8;
  346. }
  347. if (!(x & 0xf0000000u)) {
  348. x <<= 4;
  349. r -= 4;
  350. }
  351. if (!(x & 0xc0000000u)) {
  352. x <<= 2;
  353. r -= 2;
  354. }
  355. if (!(x & 0x80000000u)) {
  356. x <<= 1;
  357. r -= 1;
  358. }
  359. return r;
  360. }
  361. /*
  362. * fls = Find Last Set in word
  363. * @result: [1-32]
  364. * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
  365. */
  366. static inline __attribute__ ((const)) int fls(unsigned long x)
  367. {
  368. if (__builtin_constant_p(x))
  369. return constant_fls(x);
  370. return 32 - clz(x);
  371. }
  372. /*
  373. * __fls: Similar to fls, but zero based (0-31)
  374. */
  375. static inline __attribute__ ((const)) int __fls(unsigned long x)
  376. {
  377. if (!x)
  378. return 0;
  379. else
  380. return fls(x) - 1;
  381. }
  382. /*
  383. * ffs = Find First Set in word (LSB to MSB)
  384. * @result: [1-32], 0 if all 0's
  385. */
  386. #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
  387. /*
  388. * __ffs: Similar to ffs, but zero based (0-31)
  389. */
  390. static inline __attribute__ ((const)) int __ffs(unsigned long word)
  391. {
  392. if (!word)
  393. return word;
  394. return ffs(word) - 1;
  395. }
  396. /*
  397. * ffz = Find First Zero in word.
  398. * @return:[0-31], 32 if all 1's
  399. */
  400. #define ffz(x) __ffs(~(x))
  401. #include <asm-generic/bitops/hweight.h>
  402. #include <asm-generic/bitops/fls64.h>
  403. #include <asm-generic/bitops/sched.h>
  404. #include <asm-generic/bitops/lock.h>
  405. #include <asm-generic/bitops/find.h>
  406. #include <asm-generic/bitops/le.h>
  407. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  408. #endif /* !__ASSEMBLY__ */
  409. #endif