bitops.h 9.6 KB

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