bitops.h 9.4 KB

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