bitops.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_IA64_BITOPS_H
  3. #define _ASM_IA64_BITOPS_H
  4. /*
  5. * Copyright (C) 1998-2003 Hewlett-Packard Co
  6. * David Mosberger-Tang <[email protected]>
  7. *
  8. * 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia64
  9. * O(1) scheduler patch
  10. */
  11. #ifndef _LINUX_BITOPS_H
  12. #error only <linux/bitops.h> can be included directly
  13. #endif
  14. #include <linux/compiler.h>
  15. #include <linux/types.h>
  16. #include <asm/intrinsics.h>
  17. #include <asm/barrier.h>
  18. /**
  19. * set_bit - Atomically set a bit in memory
  20. * @nr: the bit to set
  21. * @addr: the address to start counting from
  22. *
  23. * This function is atomic and may not be reordered. See __set_bit()
  24. * if you do not require the atomic guarantees.
  25. * Note that @nr may be almost arbitrarily large; this function is not
  26. * restricted to acting on a single-word quantity.
  27. *
  28. * The address must be (at least) "long" aligned.
  29. * Note that there are driver (e.g., eepro100) which use these operations to
  30. * operate on hw-defined data-structures, so we can't easily change these
  31. * operations to force a bigger alignment.
  32. *
  33. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  34. */
  35. static __inline__ void
  36. set_bit (int nr, volatile void *addr)
  37. {
  38. __u32 bit, old, new;
  39. volatile __u32 *m;
  40. CMPXCHG_BUGCHECK_DECL
  41. m = (volatile __u32 *) addr + (nr >> 5);
  42. bit = 1 << (nr & 31);
  43. do {
  44. CMPXCHG_BUGCHECK(m);
  45. old = *m;
  46. new = old | bit;
  47. } while (cmpxchg_acq(m, old, new) != old);
  48. }
  49. /**
  50. * arch___set_bit - Set a bit in memory
  51. * @nr: the bit to set
  52. * @addr: the address to start counting from
  53. *
  54. * Unlike set_bit(), this function is non-atomic and may be reordered.
  55. * If it's called on the same region of memory simultaneously, the effect
  56. * may be that only one operation succeeds.
  57. */
  58. static __always_inline void
  59. arch___set_bit(unsigned long nr, volatile unsigned long *addr)
  60. {
  61. *((__u32 *) addr + (nr >> 5)) |= (1 << (nr & 31));
  62. }
  63. /**
  64. * clear_bit - Clears a bit in memory
  65. * @nr: Bit to clear
  66. * @addr: Address to start counting from
  67. *
  68. * clear_bit() is atomic and may not be reordered. However, it does
  69. * not contain a memory barrier, so if it is used for locking purposes,
  70. * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
  71. * in order to ensure changes are visible on other processors.
  72. */
  73. static __inline__ void
  74. clear_bit (int nr, volatile void *addr)
  75. {
  76. __u32 mask, old, new;
  77. volatile __u32 *m;
  78. CMPXCHG_BUGCHECK_DECL
  79. m = (volatile __u32 *) addr + (nr >> 5);
  80. mask = ~(1 << (nr & 31));
  81. do {
  82. CMPXCHG_BUGCHECK(m);
  83. old = *m;
  84. new = old & mask;
  85. } while (cmpxchg_acq(m, old, new) != old);
  86. }
  87. /**
  88. * clear_bit_unlock - Clears a bit in memory with release
  89. * @nr: Bit to clear
  90. * @addr: Address to start counting from
  91. *
  92. * clear_bit_unlock() is atomic and may not be reordered. It does
  93. * contain a memory barrier suitable for unlock type operations.
  94. */
  95. static __inline__ void
  96. clear_bit_unlock (int nr, volatile void *addr)
  97. {
  98. __u32 mask, old, new;
  99. volatile __u32 *m;
  100. CMPXCHG_BUGCHECK_DECL
  101. m = (volatile __u32 *) addr + (nr >> 5);
  102. mask = ~(1 << (nr & 31));
  103. do {
  104. CMPXCHG_BUGCHECK(m);
  105. old = *m;
  106. new = old & mask;
  107. } while (cmpxchg_rel(m, old, new) != old);
  108. }
  109. /**
  110. * __clear_bit_unlock - Non-atomically clears a bit in memory with release
  111. * @nr: Bit to clear
  112. * @addr: Address to start counting from
  113. *
  114. * Similarly to clear_bit_unlock, the implementation uses a store
  115. * with release semantics. See also arch_spin_unlock().
  116. */
  117. static __inline__ void
  118. __clear_bit_unlock(int nr, void *addr)
  119. {
  120. __u32 * const m = (__u32 *) addr + (nr >> 5);
  121. __u32 const new = *m & ~(1 << (nr & 31));
  122. ia64_st4_rel_nta(m, new);
  123. }
  124. /**
  125. * arch___clear_bit - Clears a bit in memory (non-atomic version)
  126. * @nr: the bit to clear
  127. * @addr: the address to start counting from
  128. *
  129. * Unlike clear_bit(), this function is non-atomic and may be reordered.
  130. * If it's called on the same region of memory simultaneously, the effect
  131. * may be that only one operation succeeds.
  132. */
  133. static __always_inline void
  134. arch___clear_bit(unsigned long nr, volatile unsigned long *addr)
  135. {
  136. *((__u32 *) addr + (nr >> 5)) &= ~(1 << (nr & 31));
  137. }
  138. /**
  139. * change_bit - Toggle a bit in memory
  140. * @nr: Bit to toggle
  141. * @addr: Address to start counting from
  142. *
  143. * change_bit() is atomic and may not be reordered.
  144. * Note that @nr may be almost arbitrarily large; this function is not
  145. * restricted to acting on a single-word quantity.
  146. */
  147. static __inline__ void
  148. change_bit (int nr, volatile void *addr)
  149. {
  150. __u32 bit, old, new;
  151. volatile __u32 *m;
  152. CMPXCHG_BUGCHECK_DECL
  153. m = (volatile __u32 *) addr + (nr >> 5);
  154. bit = (1 << (nr & 31));
  155. do {
  156. CMPXCHG_BUGCHECK(m);
  157. old = *m;
  158. new = old ^ bit;
  159. } while (cmpxchg_acq(m, old, new) != old);
  160. }
  161. /**
  162. * arch___change_bit - Toggle a bit in memory
  163. * @nr: the bit to toggle
  164. * @addr: the address to start counting from
  165. *
  166. * Unlike change_bit(), this function is non-atomic and may be reordered.
  167. * If it's called on the same region of memory simultaneously, the effect
  168. * may be that only one operation succeeds.
  169. */
  170. static __always_inline void
  171. arch___change_bit(unsigned long nr, volatile unsigned long *addr)
  172. {
  173. *((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31));
  174. }
  175. /**
  176. * test_and_set_bit - Set a bit and return its old value
  177. * @nr: Bit to set
  178. * @addr: Address to count from
  179. *
  180. * This operation is atomic and cannot be reordered.
  181. * It also implies the acquisition side of the memory barrier.
  182. */
  183. static __inline__ int
  184. test_and_set_bit (int nr, volatile void *addr)
  185. {
  186. __u32 bit, old, new;
  187. volatile __u32 *m;
  188. CMPXCHG_BUGCHECK_DECL
  189. m = (volatile __u32 *) addr + (nr >> 5);
  190. bit = 1 << (nr & 31);
  191. do {
  192. CMPXCHG_BUGCHECK(m);
  193. old = *m;
  194. new = old | bit;
  195. } while (cmpxchg_acq(m, old, new) != old);
  196. return (old & bit) != 0;
  197. }
  198. /**
  199. * test_and_set_bit_lock - Set a bit and return its old value for lock
  200. * @nr: Bit to set
  201. * @addr: Address to count from
  202. *
  203. * This is the same as test_and_set_bit on ia64
  204. */
  205. #define test_and_set_bit_lock test_and_set_bit
  206. /**
  207. * arch___test_and_set_bit - Set a bit and return its old value
  208. * @nr: Bit to set
  209. * @addr: Address to count from
  210. *
  211. * This operation is non-atomic and can be reordered.
  212. * If two examples of this operation race, one can appear to succeed
  213. * but actually fail. You must protect multiple accesses with a lock.
  214. */
  215. static __always_inline bool
  216. arch___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
  217. {
  218. __u32 *p = (__u32 *) addr + (nr >> 5);
  219. __u32 m = 1 << (nr & 31);
  220. int oldbitset = (*p & m) != 0;
  221. *p |= m;
  222. return oldbitset;
  223. }
  224. /**
  225. * test_and_clear_bit - Clear a bit and return its old value
  226. * @nr: Bit to clear
  227. * @addr: Address to count from
  228. *
  229. * This operation is atomic and cannot be reordered.
  230. * It also implies the acquisition side of the memory barrier.
  231. */
  232. static __inline__ int
  233. test_and_clear_bit (int nr, volatile void *addr)
  234. {
  235. __u32 mask, old, new;
  236. volatile __u32 *m;
  237. CMPXCHG_BUGCHECK_DECL
  238. m = (volatile __u32 *) addr + (nr >> 5);
  239. mask = ~(1 << (nr & 31));
  240. do {
  241. CMPXCHG_BUGCHECK(m);
  242. old = *m;
  243. new = old & mask;
  244. } while (cmpxchg_acq(m, old, new) != old);
  245. return (old & ~mask) != 0;
  246. }
  247. /**
  248. * arch___test_and_clear_bit - Clear a bit and return its old value
  249. * @nr: Bit to clear
  250. * @addr: Address to count from
  251. *
  252. * This operation is non-atomic and can be reordered.
  253. * If two examples of this operation race, one can appear to succeed
  254. * but actually fail. You must protect multiple accesses with a lock.
  255. */
  256. static __always_inline bool
  257. arch___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
  258. {
  259. __u32 *p = (__u32 *) addr + (nr >> 5);
  260. __u32 m = 1 << (nr & 31);
  261. int oldbitset = (*p & m) != 0;
  262. *p &= ~m;
  263. return oldbitset;
  264. }
  265. /**
  266. * test_and_change_bit - Change a bit and return its old value
  267. * @nr: Bit to change
  268. * @addr: Address to count from
  269. *
  270. * This operation is atomic and cannot be reordered.
  271. * It also implies the acquisition side of the memory barrier.
  272. */
  273. static __inline__ int
  274. test_and_change_bit (int nr, volatile void *addr)
  275. {
  276. __u32 bit, old, new;
  277. volatile __u32 *m;
  278. CMPXCHG_BUGCHECK_DECL
  279. m = (volatile __u32 *) addr + (nr >> 5);
  280. bit = (1 << (nr & 31));
  281. do {
  282. CMPXCHG_BUGCHECK(m);
  283. old = *m;
  284. new = old ^ bit;
  285. } while (cmpxchg_acq(m, old, new) != old);
  286. return (old & bit) != 0;
  287. }
  288. /**
  289. * arch___test_and_change_bit - Change a bit and return its old value
  290. * @nr: Bit to change
  291. * @addr: Address to count from
  292. *
  293. * This operation is non-atomic and can be reordered.
  294. */
  295. static __always_inline bool
  296. arch___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
  297. {
  298. __u32 old, bit = (1 << (nr & 31));
  299. __u32 *m = (__u32 *) addr + (nr >> 5);
  300. old = *m;
  301. *m = old ^ bit;
  302. return (old & bit) != 0;
  303. }
  304. #define arch_test_bit generic_test_bit
  305. #define arch_test_bit_acquire generic_test_bit_acquire
  306. /**
  307. * ffz - find the first zero bit in a long word
  308. * @x: The long word to find the bit in
  309. *
  310. * Returns the bit-number (0..63) of the first (least significant) zero bit.
  311. * Undefined if no zero exists, so code should check against ~0UL first...
  312. */
  313. static inline unsigned long
  314. ffz (unsigned long x)
  315. {
  316. unsigned long result;
  317. result = ia64_popcnt(x & (~x - 1));
  318. return result;
  319. }
  320. /**
  321. * __ffs - find first bit in word.
  322. * @x: The word to search
  323. *
  324. * Undefined if no bit exists, so code should check against 0 first.
  325. */
  326. static __inline__ unsigned long
  327. __ffs (unsigned long x)
  328. {
  329. unsigned long result;
  330. result = ia64_popcnt((x-1) & ~x);
  331. return result;
  332. }
  333. #ifdef __KERNEL__
  334. /*
  335. * Return bit number of last (most-significant) bit set. Undefined
  336. * for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3).
  337. */
  338. static inline unsigned long
  339. ia64_fls (unsigned long x)
  340. {
  341. long double d = x;
  342. long exp;
  343. exp = ia64_getf_exp(d);
  344. return exp - 0xffff;
  345. }
  346. /*
  347. * Find the last (most significant) bit set. Returns 0 for x==0 and
  348. * bits are numbered from 1..32 (e.g., fls(9) == 4).
  349. */
  350. static inline int fls(unsigned int t)
  351. {
  352. unsigned long x = t & 0xffffffffu;
  353. if (!x)
  354. return 0;
  355. x |= x >> 1;
  356. x |= x >> 2;
  357. x |= x >> 4;
  358. x |= x >> 8;
  359. x |= x >> 16;
  360. return ia64_popcnt(x);
  361. }
  362. /*
  363. * Find the last (most significant) bit set. Undefined for x==0.
  364. * Bits are numbered from 0..63 (e.g., __fls(9) == 3).
  365. */
  366. static inline unsigned long
  367. __fls (unsigned long x)
  368. {
  369. x |= x >> 1;
  370. x |= x >> 2;
  371. x |= x >> 4;
  372. x |= x >> 8;
  373. x |= x >> 16;
  374. x |= x >> 32;
  375. return ia64_popcnt(x) - 1;
  376. }
  377. #include <asm-generic/bitops/fls64.h>
  378. #include <asm-generic/bitops/builtin-ffs.h>
  379. /*
  380. * hweightN: returns the hamming weight (i.e. the number
  381. * of bits set) of a N-bit word
  382. */
  383. static __inline__ unsigned long __arch_hweight64(unsigned long x)
  384. {
  385. unsigned long result;
  386. result = ia64_popcnt(x);
  387. return result;
  388. }
  389. #define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful))
  390. #define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful))
  391. #define __arch_hweight8(x) ((unsigned int) __arch_hweight64((x) & 0xfful))
  392. #include <asm-generic/bitops/const_hweight.h>
  393. #endif /* __KERNEL__ */
  394. #ifdef __KERNEL__
  395. #include <asm-generic/bitops/non-instrumented-non-atomic.h>
  396. #include <asm-generic/bitops/le.h>
  397. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  398. #include <asm-generic/bitops/sched.h>
  399. #endif /* __KERNEL__ */
  400. #endif /* _ASM_IA64_BITOPS_H */