bitops.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright IBM Corp. 1999,2013
  3. *
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  5. *
  6. * The description below was taken in large parts from the powerpc
  7. * bitops header file:
  8. * Within a word, bits are numbered LSB first. Lot's of places make
  9. * this assumption by directly testing bits with (val & (1<<nr)).
  10. * This can cause confusion for large (> 1 word) bitmaps on a
  11. * big-endian system because, unlike little endian, the number of each
  12. * bit depends on the word size.
  13. *
  14. * The bitop functions are defined to work on unsigned longs, so for an
  15. * s390x system the bits end up numbered:
  16. * |63..............0|127............64|191...........128|255...........196|
  17. * and on s390:
  18. * |31.....0|63....31|95....64|127...96|159..128|191..160|223..192|255..224|
  19. *
  20. * There are a few little-endian macros used mostly for filesystem
  21. * bitmaps, these work on similar bit arrays layouts, but
  22. * byte-oriented:
  23. * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
  24. *
  25. * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit
  26. * number field needs to be reversed compared to the big-endian bit
  27. * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).
  28. *
  29. * We also have special functions which work with an MSB0 encoding:
  30. * on an s390x system the bits are numbered:
  31. * |0..............63|64............127|128...........191|192...........255|
  32. * and on s390:
  33. * |0.....31|31....63|64....95|96...127|128..159|160..191|192..223|224..255|
  34. *
  35. * The main difference is that bit 0-63 (64b) or 0-31 (32b) in the bit
  36. * number field needs to be reversed compared to the LSB0 encoded bit
  37. * fields. This can be achieved by XOR with 0x3f (64b) or 0x1f (32b).
  38. *
  39. */
  40. #ifndef _S390_BITOPS_H
  41. #define _S390_BITOPS_H
  42. #ifndef _LINUX_BITOPS_H
  43. #error only <linux/bitops.h> can be included directly
  44. #endif
  45. #include <linux/typecheck.h>
  46. #include <linux/compiler.h>
  47. #ifndef CONFIG_64BIT
  48. #define __BITOPS_OR "or"
  49. #define __BITOPS_AND "nr"
  50. #define __BITOPS_XOR "xr"
  51. #define __BITOPS_LOOP(__addr, __val, __op_string) \
  52. ({ \
  53. unsigned long __old, __new; \
  54. \
  55. typecheck(unsigned long *, (__addr)); \
  56. asm volatile( \
  57. " l %0,%2\n" \
  58. "0: lr %1,%0\n" \
  59. __op_string " %1,%3\n" \
  60. " cs %0,%1,%2\n" \
  61. " jl 0b" \
  62. : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
  63. : "d" (__val) \
  64. : "cc"); \
  65. __old; \
  66. })
  67. #else /* CONFIG_64BIT */
  68. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  69. #define __BITOPS_OR "laog"
  70. #define __BITOPS_AND "lang"
  71. #define __BITOPS_XOR "laxg"
  72. #define __BITOPS_LOOP(__addr, __val, __op_string) \
  73. ({ \
  74. unsigned long __old; \
  75. \
  76. typecheck(unsigned long *, (__addr)); \
  77. asm volatile( \
  78. __op_string " %0,%2,%1\n" \
  79. : "=d" (__old), "+Q" (*(__addr)) \
  80. : "d" (__val) \
  81. : "cc"); \
  82. __old; \
  83. })
  84. #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  85. #define __BITOPS_OR "ogr"
  86. #define __BITOPS_AND "ngr"
  87. #define __BITOPS_XOR "xgr"
  88. #define __BITOPS_LOOP(__addr, __val, __op_string) \
  89. ({ \
  90. unsigned long __old, __new; \
  91. \
  92. typecheck(unsigned long *, (__addr)); \
  93. asm volatile( \
  94. " lg %0,%2\n" \
  95. "0: lgr %1,%0\n" \
  96. __op_string " %1,%3\n" \
  97. " csg %0,%1,%2\n" \
  98. " jl 0b" \
  99. : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
  100. : "d" (__val) \
  101. : "cc"); \
  102. __old; \
  103. })
  104. #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  105. #endif /* CONFIG_64BIT */
  106. #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
  107. static inline unsigned long *
  108. __bitops_word(unsigned long nr, volatile unsigned long *ptr)
  109. {
  110. unsigned long addr;
  111. addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3);
  112. return (unsigned long *)addr;
  113. }
  114. static inline unsigned char *
  115. __bitops_byte(unsigned long nr, volatile unsigned long *ptr)
  116. {
  117. return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  118. }
  119. static inline void set_bit(unsigned long nr, volatile unsigned long *ptr)
  120. {
  121. unsigned long *addr = __bitops_word(nr, ptr);
  122. unsigned long mask;
  123. #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
  124. if (__builtin_constant_p(nr)) {
  125. unsigned char *caddr = __bitops_byte(nr, ptr);
  126. asm volatile(
  127. "oi %0,%b1\n"
  128. : "+Q" (*caddr)
  129. : "i" (1 << (nr & 7))
  130. : "cc");
  131. return;
  132. }
  133. #endif
  134. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  135. __BITOPS_LOOP(addr, mask, __BITOPS_OR);
  136. }
  137. static inline void clear_bit(unsigned long nr, volatile unsigned long *ptr)
  138. {
  139. unsigned long *addr = __bitops_word(nr, ptr);
  140. unsigned long mask;
  141. #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
  142. if (__builtin_constant_p(nr)) {
  143. unsigned char *caddr = __bitops_byte(nr, ptr);
  144. asm volatile(
  145. "ni %0,%b1\n"
  146. : "+Q" (*caddr)
  147. : "i" (~(1 << (nr & 7)))
  148. : "cc");
  149. return;
  150. }
  151. #endif
  152. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  153. __BITOPS_LOOP(addr, mask, __BITOPS_AND);
  154. }
  155. static inline void change_bit(unsigned long nr, volatile unsigned long *ptr)
  156. {
  157. unsigned long *addr = __bitops_word(nr, ptr);
  158. unsigned long mask;
  159. #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
  160. if (__builtin_constant_p(nr)) {
  161. unsigned char *caddr = __bitops_byte(nr, ptr);
  162. asm volatile(
  163. "xi %0,%b1\n"
  164. : "+Q" (*caddr)
  165. : "i" (1 << (nr & 7))
  166. : "cc");
  167. return;
  168. }
  169. #endif
  170. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  171. __BITOPS_LOOP(addr, mask, __BITOPS_XOR);
  172. }
  173. static inline int
  174. test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
  175. {
  176. unsigned long *addr = __bitops_word(nr, ptr);
  177. unsigned long old, mask;
  178. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  179. old = __BITOPS_LOOP(addr, mask, __BITOPS_OR);
  180. barrier();
  181. return (old & mask) != 0;
  182. }
  183. static inline int
  184. test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
  185. {
  186. unsigned long *addr = __bitops_word(nr, ptr);
  187. unsigned long old, mask;
  188. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  189. old = __BITOPS_LOOP(addr, mask, __BITOPS_AND);
  190. barrier();
  191. return (old & ~mask) != 0;
  192. }
  193. static inline int
  194. test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
  195. {
  196. unsigned long *addr = __bitops_word(nr, ptr);
  197. unsigned long old, mask;
  198. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  199. old = __BITOPS_LOOP(addr, mask, __BITOPS_XOR);
  200. barrier();
  201. return (old & mask) != 0;
  202. }
  203. static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
  204. {
  205. unsigned char *addr = __bitops_byte(nr, ptr);
  206. *addr |= 1 << (nr & 7);
  207. }
  208. static inline void
  209. __clear_bit(unsigned long nr, volatile unsigned long *ptr)
  210. {
  211. unsigned char *addr = __bitops_byte(nr, ptr);
  212. *addr &= ~(1 << (nr & 7));
  213. }
  214. static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
  215. {
  216. unsigned char *addr = __bitops_byte(nr, ptr);
  217. *addr ^= 1 << (nr & 7);
  218. }
  219. static inline int
  220. __test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
  221. {
  222. unsigned char *addr = __bitops_byte(nr, ptr);
  223. unsigned char ch;
  224. ch = *addr;
  225. *addr |= 1 << (nr & 7);
  226. return (ch >> (nr & 7)) & 1;
  227. }
  228. static inline int
  229. __test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
  230. {
  231. unsigned char *addr = __bitops_byte(nr, ptr);
  232. unsigned char ch;
  233. ch = *addr;
  234. *addr &= ~(1 << (nr & 7));
  235. return (ch >> (nr & 7)) & 1;
  236. }
  237. static inline int
  238. __test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
  239. {
  240. unsigned char *addr = __bitops_byte(nr, ptr);
  241. unsigned char ch;
  242. ch = *addr;
  243. *addr ^= 1 << (nr & 7);
  244. return (ch >> (nr & 7)) & 1;
  245. }
  246. static inline int test_bit(unsigned long nr, const volatile unsigned long *ptr)
  247. {
  248. const volatile unsigned char *addr;
  249. addr = ((const volatile unsigned char *)ptr);
  250. addr += (nr ^ (BITS_PER_LONG - 8)) >> 3;
  251. return (*addr >> (nr & 7)) & 1;
  252. }
  253. /*
  254. * Functions which use MSB0 bit numbering.
  255. * On an s390x system the bits are numbered:
  256. * |0..............63|64............127|128...........191|192...........255|
  257. * and on s390:
  258. * |0.....31|31....63|64....95|96...127|128..159|160..191|192..223|224..255|
  259. */
  260. unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size);
  261. unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size,
  262. unsigned long offset);
  263. static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  264. {
  265. return set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  266. }
  267. static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  268. {
  269. return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  270. }
  271. static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  272. {
  273. return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  274. }
  275. static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  276. {
  277. return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  278. }
  279. static inline int test_bit_inv(unsigned long nr,
  280. const volatile unsigned long *ptr)
  281. {
  282. return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  283. }
  284. #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
  285. /**
  286. * __flogr - find leftmost one
  287. * @word - The word to search
  288. *
  289. * Returns the bit number of the most significant bit set,
  290. * where the most significant bit has bit number 0.
  291. * If no bit is set this function returns 64.
  292. */
  293. static inline unsigned char __flogr(unsigned long word)
  294. {
  295. if (__builtin_constant_p(word)) {
  296. unsigned long bit = 0;
  297. if (!word)
  298. return 64;
  299. if (!(word & 0xffffffff00000000UL)) {
  300. word <<= 32;
  301. bit += 32;
  302. }
  303. if (!(word & 0xffff000000000000UL)) {
  304. word <<= 16;
  305. bit += 16;
  306. }
  307. if (!(word & 0xff00000000000000UL)) {
  308. word <<= 8;
  309. bit += 8;
  310. }
  311. if (!(word & 0xf000000000000000UL)) {
  312. word <<= 4;
  313. bit += 4;
  314. }
  315. if (!(word & 0xc000000000000000UL)) {
  316. word <<= 2;
  317. bit += 2;
  318. }
  319. if (!(word & 0x8000000000000000UL)) {
  320. word <<= 1;
  321. bit += 1;
  322. }
  323. return bit;
  324. } else {
  325. register unsigned long bit asm("4") = word;
  326. register unsigned long out asm("5");
  327. asm volatile(
  328. " flogr %[bit],%[bit]\n"
  329. : [bit] "+d" (bit), [out] "=d" (out) : : "cc");
  330. return bit;
  331. }
  332. }
  333. /**
  334. * __ffs - find first bit in word.
  335. * @word: The word to search
  336. *
  337. * Undefined if no bit exists, so code should check against 0 first.
  338. */
  339. static inline unsigned long __ffs(unsigned long word)
  340. {
  341. return __flogr(-word & word) ^ (BITS_PER_LONG - 1);
  342. }
  343. /**
  344. * ffs - find first bit set
  345. * @word: the word to search
  346. *
  347. * This is defined the same way as the libc and
  348. * compiler builtin ffs routines (man ffs).
  349. */
  350. static inline int ffs(int word)
  351. {
  352. unsigned long mask = 2 * BITS_PER_LONG - 1;
  353. unsigned int val = (unsigned int)word;
  354. return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask;
  355. }
  356. /**
  357. * __fls - find last (most-significant) set bit in a long word
  358. * @word: the word to search
  359. *
  360. * Undefined if no set bit exists, so code should check against 0 first.
  361. */
  362. static inline unsigned long __fls(unsigned long word)
  363. {
  364. return __flogr(word) ^ (BITS_PER_LONG - 1);
  365. }
  366. /**
  367. * fls64 - find last set bit in a 64-bit word
  368. * @word: the word to search
  369. *
  370. * This is defined in a similar way as the libc and compiler builtin
  371. * ffsll, but returns the position of the most significant set bit.
  372. *
  373. * fls64(value) returns 0 if value is 0 or the position of the last
  374. * set bit if value is nonzero. The last (most significant) bit is
  375. * at position 64.
  376. */
  377. static inline int fls64(unsigned long word)
  378. {
  379. unsigned long mask = 2 * BITS_PER_LONG - 1;
  380. return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask;
  381. }
  382. /**
  383. * fls - find last (most-significant) bit set
  384. * @word: the word to search
  385. *
  386. * This is defined the same way as ffs.
  387. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  388. */
  389. static inline int fls(int word)
  390. {
  391. return fls64((unsigned int)word);
  392. }
  393. #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
  394. #include <asm-generic/bitops/__ffs.h>
  395. #include <asm-generic/bitops/ffs.h>
  396. #include <asm-generic/bitops/__fls.h>
  397. #include <asm-generic/bitops/fls.h>
  398. #include <asm-generic/bitops/fls64.h>
  399. #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
  400. #include <asm-generic/bitops/ffz.h>
  401. #include <asm-generic/bitops/find.h>
  402. #include <asm-generic/bitops/hweight.h>
  403. #include <asm-generic/bitops/lock.h>
  404. #include <asm-generic/bitops/sched.h>
  405. #include <asm-generic/bitops/le.h>
  406. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  407. #endif /* _S390_BITOPS_H */