bitops.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999
  4. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  5. *
  6. * Derived from "include/asm-i386/bitops.h"
  7. * Copyright (C) 1992, Linus Torvalds
  8. *
  9. */
  10. #ifndef _S390_BITOPS_H
  11. #define _S390_BITOPS_H
  12. #ifndef _LINUX_BITOPS_H
  13. #error only <linux/bitops.h> can be included directly
  14. #endif
  15. #include <linux/compiler.h>
  16. /*
  17. * 32 bit bitops format:
  18. * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
  19. * bit 32 is the LSB of *(addr+4). That combined with the
  20. * big endian byte order on S390 give the following bit
  21. * order in memory:
  22. * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
  23. * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
  24. * after that follows the next long with bit numbers
  25. * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
  26. * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
  27. * The reason for this bit ordering is the fact that
  28. * in the architecture independent code bits operations
  29. * of the form "flags |= (1 << bitnr)" are used INTERMIXED
  30. * with operation of the form "set_bit(bitnr, flags)".
  31. *
  32. * 64 bit bitops format:
  33. * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
  34. * bit 64 is the LSB of *(addr+8). That combined with the
  35. * big endian byte order on S390 give the following bit
  36. * order in memory:
  37. * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
  38. * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
  39. * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
  40. * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
  41. * after that follows the next long with bit numbers
  42. * 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
  43. * 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
  44. * 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
  45. * 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
  46. * The reason for this bit ordering is the fact that
  47. * in the architecture independent code bits operations
  48. * of the form "flags |= (1 << bitnr)" are used INTERMIXED
  49. * with operation of the form "set_bit(bitnr, flags)".
  50. */
  51. /* bitmap tables from arch/s390/kernel/bitmap.c */
  52. extern const char _oi_bitmap[];
  53. extern const char _ni_bitmap[];
  54. extern const char _zb_findmap[];
  55. extern const char _sb_findmap[];
  56. #ifndef CONFIG_64BIT
  57. #define __BITOPS_OR "or"
  58. #define __BITOPS_AND "nr"
  59. #define __BITOPS_XOR "xr"
  60. #define __BITOPS_LOOP(__addr, __val, __op_string) \
  61. ({ \
  62. unsigned long __old, __new; \
  63. \
  64. asm volatile( \
  65. " l %0,%2\n" \
  66. "0: lr %1,%0\n" \
  67. __op_string " %1,%3\n" \
  68. " cs %0,%1,%2\n" \
  69. " jl 0b" \
  70. : "=&d" (__old), "=&d" (__new), \
  71. "=Q" (*(unsigned long *) __addr) \
  72. : "d" (__val), "Q" (*(unsigned long *) __addr) \
  73. : "cc"); \
  74. __old; \
  75. })
  76. #else /* CONFIG_64BIT */
  77. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  78. #define __BITOPS_OR "laog"
  79. #define __BITOPS_AND "lang"
  80. #define __BITOPS_XOR "laxg"
  81. #define __BITOPS_LOOP(__addr, __val, __op_string) \
  82. ({ \
  83. unsigned long __old; \
  84. \
  85. asm volatile( \
  86. __op_string " %0,%2,%1\n" \
  87. : "=d" (__old), "+Q" (*(unsigned long *)__addr) \
  88. : "d" (__val) \
  89. : "cc"); \
  90. __old; \
  91. })
  92. #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  93. #define __BITOPS_OR "ogr"
  94. #define __BITOPS_AND "ngr"
  95. #define __BITOPS_XOR "xgr"
  96. #define __BITOPS_LOOP(__addr, __val, __op_string) \
  97. ({ \
  98. unsigned long __old, __new; \
  99. \
  100. asm volatile( \
  101. " lg %0,%2\n" \
  102. "0: lgr %1,%0\n" \
  103. __op_string " %1,%3\n" \
  104. " csg %0,%1,%2\n" \
  105. " jl 0b" \
  106. : "=&d" (__old), "=&d" (__new), \
  107. "=Q" (*(unsigned long *) __addr) \
  108. : "d" (__val), "Q" (*(unsigned long *) __addr) \
  109. : "cc"); \
  110. __old; \
  111. })
  112. #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  113. #endif /* CONFIG_64BIT */
  114. #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
  115. #ifdef CONFIG_SMP
  116. /*
  117. * SMP safe set_bit routine based on compare and swap (CS)
  118. */
  119. static inline void set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  120. {
  121. unsigned long addr, mask;
  122. addr = (unsigned long) ptr;
  123. /* calculate address for CS */
  124. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  125. /* make OR mask */
  126. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  127. /* Do the atomic update. */
  128. __BITOPS_LOOP(addr, mask, __BITOPS_OR);
  129. }
  130. /*
  131. * SMP safe clear_bit routine based on compare and swap (CS)
  132. */
  133. static inline void clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  134. {
  135. unsigned long addr, mask;
  136. addr = (unsigned long) ptr;
  137. /* calculate address for CS */
  138. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  139. /* make AND mask */
  140. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  141. /* Do the atomic update. */
  142. __BITOPS_LOOP(addr, mask, __BITOPS_AND);
  143. }
  144. /*
  145. * SMP safe change_bit routine based on compare and swap (CS)
  146. */
  147. static inline void change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  148. {
  149. unsigned long addr, mask;
  150. addr = (unsigned long) ptr;
  151. /* calculate address for CS */
  152. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  153. /* make XOR mask */
  154. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  155. /* Do the atomic update. */
  156. __BITOPS_LOOP(addr, mask, __BITOPS_XOR);
  157. }
  158. /*
  159. * SMP safe test_and_set_bit routine based on compare and swap (CS)
  160. */
  161. static inline int
  162. test_and_set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  163. {
  164. unsigned long addr, old, mask;
  165. addr = (unsigned long) ptr;
  166. /* calculate address for CS */
  167. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  168. /* make OR/test mask */
  169. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  170. /* Do the atomic update. */
  171. old = __BITOPS_LOOP(addr, mask, __BITOPS_OR);
  172. barrier();
  173. return (old & mask) != 0;
  174. }
  175. /*
  176. * SMP safe test_and_clear_bit routine based on compare and swap (CS)
  177. */
  178. static inline int
  179. test_and_clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  180. {
  181. unsigned long addr, old, mask;
  182. addr = (unsigned long) ptr;
  183. /* calculate address for CS */
  184. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  185. /* make AND/test mask */
  186. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  187. /* Do the atomic update. */
  188. old = __BITOPS_LOOP(addr, mask, __BITOPS_AND);
  189. barrier();
  190. return (old & ~mask) != 0;
  191. }
  192. /*
  193. * SMP safe test_and_change_bit routine based on compare and swap (CS)
  194. */
  195. static inline int
  196. test_and_change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  197. {
  198. unsigned long addr, old, mask;
  199. addr = (unsigned long) ptr;
  200. /* calculate address for CS */
  201. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  202. /* make XOR/test mask */
  203. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  204. /* Do the atomic update. */
  205. old = __BITOPS_LOOP(addr, mask, __BITOPS_XOR);
  206. barrier();
  207. return (old & mask) != 0;
  208. }
  209. #endif /* CONFIG_SMP */
  210. /*
  211. * fast, non-SMP set_bit routine
  212. */
  213. static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
  214. {
  215. unsigned long addr;
  216. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  217. asm volatile(
  218. " oc %O0(1,%R0),%1"
  219. : "+Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc");
  220. }
  221. static inline void
  222. __constant_set_bit(const unsigned long nr, volatile unsigned long *ptr)
  223. {
  224. unsigned long addr;
  225. addr = ((unsigned long) ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  226. *(unsigned char *) addr |= 1 << (nr & 7);
  227. }
  228. #define set_bit_simple(nr,addr) \
  229. (__builtin_constant_p((nr)) ? \
  230. __constant_set_bit((nr),(addr)) : \
  231. __set_bit((nr),(addr)) )
  232. /*
  233. * fast, non-SMP clear_bit routine
  234. */
  235. static inline void
  236. __clear_bit(unsigned long nr, volatile unsigned long *ptr)
  237. {
  238. unsigned long addr;
  239. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  240. asm volatile(
  241. " nc %O0(1,%R0),%1"
  242. : "+Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7]) : "cc");
  243. }
  244. static inline void
  245. __constant_clear_bit(const unsigned long nr, volatile unsigned long *ptr)
  246. {
  247. unsigned long addr;
  248. addr = ((unsigned long) ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  249. *(unsigned char *) addr &= ~(1 << (nr & 7));
  250. }
  251. #define clear_bit_simple(nr,addr) \
  252. (__builtin_constant_p((nr)) ? \
  253. __constant_clear_bit((nr),(addr)) : \
  254. __clear_bit((nr),(addr)) )
  255. /*
  256. * fast, non-SMP change_bit routine
  257. */
  258. static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
  259. {
  260. unsigned long addr;
  261. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  262. asm volatile(
  263. " xc %O0(1,%R0),%1"
  264. : "+Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc");
  265. }
  266. static inline void
  267. __constant_change_bit(const unsigned long nr, volatile unsigned long *ptr)
  268. {
  269. unsigned long addr;
  270. addr = ((unsigned long) ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  271. *(unsigned char *) addr ^= 1 << (nr & 7);
  272. }
  273. #define change_bit_simple(nr,addr) \
  274. (__builtin_constant_p((nr)) ? \
  275. __constant_change_bit((nr),(addr)) : \
  276. __change_bit((nr),(addr)) )
  277. /*
  278. * fast, non-SMP test_and_set_bit routine
  279. */
  280. static inline int
  281. test_and_set_bit_simple(unsigned long nr, volatile unsigned long *ptr)
  282. {
  283. unsigned long addr;
  284. unsigned char ch;
  285. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  286. ch = *(unsigned char *) addr;
  287. asm volatile(
  288. " oc %O0(1,%R0),%1"
  289. : "+Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
  290. : "cc", "memory");
  291. return (ch >> (nr & 7)) & 1;
  292. }
  293. #define __test_and_set_bit(X,Y) test_and_set_bit_simple(X,Y)
  294. /*
  295. * fast, non-SMP test_and_clear_bit routine
  296. */
  297. static inline int
  298. test_and_clear_bit_simple(unsigned long nr, volatile unsigned long *ptr)
  299. {
  300. unsigned long addr;
  301. unsigned char ch;
  302. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  303. ch = *(unsigned char *) addr;
  304. asm volatile(
  305. " nc %O0(1,%R0),%1"
  306. : "+Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7])
  307. : "cc", "memory");
  308. return (ch >> (nr & 7)) & 1;
  309. }
  310. #define __test_and_clear_bit(X,Y) test_and_clear_bit_simple(X,Y)
  311. /*
  312. * fast, non-SMP test_and_change_bit routine
  313. */
  314. static inline int
  315. test_and_change_bit_simple(unsigned long nr, volatile unsigned long *ptr)
  316. {
  317. unsigned long addr;
  318. unsigned char ch;
  319. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  320. ch = *(unsigned char *) addr;
  321. asm volatile(
  322. " xc %O0(1,%R0),%1"
  323. : "+Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
  324. : "cc", "memory");
  325. return (ch >> (nr & 7)) & 1;
  326. }
  327. #define __test_and_change_bit(X,Y) test_and_change_bit_simple(X,Y)
  328. #ifdef CONFIG_SMP
  329. #define set_bit set_bit_cs
  330. #define clear_bit clear_bit_cs
  331. #define change_bit change_bit_cs
  332. #define test_and_set_bit test_and_set_bit_cs
  333. #define test_and_clear_bit test_and_clear_bit_cs
  334. #define test_and_change_bit test_and_change_bit_cs
  335. #else
  336. #define set_bit set_bit_simple
  337. #define clear_bit clear_bit_simple
  338. #define change_bit change_bit_simple
  339. #define test_and_set_bit test_and_set_bit_simple
  340. #define test_and_clear_bit test_and_clear_bit_simple
  341. #define test_and_change_bit test_and_change_bit_simple
  342. #endif
  343. /*
  344. * This routine doesn't need to be atomic.
  345. */
  346. static inline int __test_bit(unsigned long nr, const volatile unsigned long *ptr)
  347. {
  348. unsigned long addr;
  349. unsigned char ch;
  350. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  351. ch = *(volatile unsigned char *) addr;
  352. return (ch >> (nr & 7)) & 1;
  353. }
  354. static inline int
  355. __constant_test_bit(unsigned long nr, const volatile unsigned long *addr) {
  356. return (((volatile char *) addr)
  357. [(nr^(BITS_PER_LONG-8))>>3] & (1<<(nr&7))) != 0;
  358. }
  359. #define test_bit(nr,addr) \
  360. (__builtin_constant_p((nr)) ? \
  361. __constant_test_bit((nr),(addr)) : \
  362. __test_bit((nr),(addr)) )
  363. /*
  364. * Optimized find bit helper functions.
  365. */
  366. /**
  367. * __ffz_word_loop - find byte offset of first long != -1UL
  368. * @addr: pointer to array of unsigned long
  369. * @size: size of the array in bits
  370. */
  371. static inline unsigned long __ffz_word_loop(const unsigned long *addr,
  372. unsigned long size)
  373. {
  374. typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
  375. unsigned long bytes = 0;
  376. asm volatile(
  377. #ifndef CONFIG_64BIT
  378. " ahi %1,-1\n"
  379. " sra %1,5\n"
  380. " jz 1f\n"
  381. "0: c %2,0(%0,%3)\n"
  382. " jne 1f\n"
  383. " la %0,4(%0)\n"
  384. " brct %1,0b\n"
  385. "1:\n"
  386. #else
  387. " aghi %1,-1\n"
  388. " srag %1,%1,6\n"
  389. " jz 1f\n"
  390. "0: cg %2,0(%0,%3)\n"
  391. " jne 1f\n"
  392. " la %0,8(%0)\n"
  393. " brct %1,0b\n"
  394. "1:\n"
  395. #endif
  396. : "+&a" (bytes), "+&d" (size)
  397. : "d" (-1UL), "a" (addr), "m" (*(addrtype *) addr)
  398. : "cc" );
  399. return bytes;
  400. }
  401. /**
  402. * __ffs_word_loop - find byte offset of first long != 0UL
  403. * @addr: pointer to array of unsigned long
  404. * @size: size of the array in bits
  405. */
  406. static inline unsigned long __ffs_word_loop(const unsigned long *addr,
  407. unsigned long size)
  408. {
  409. typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
  410. unsigned long bytes = 0;
  411. asm volatile(
  412. #ifndef CONFIG_64BIT
  413. " ahi %1,-1\n"
  414. " sra %1,5\n"
  415. " jz 1f\n"
  416. "0: c %2,0(%0,%3)\n"
  417. " jne 1f\n"
  418. " la %0,4(%0)\n"
  419. " brct %1,0b\n"
  420. "1:\n"
  421. #else
  422. " aghi %1,-1\n"
  423. " srag %1,%1,6\n"
  424. " jz 1f\n"
  425. "0: cg %2,0(%0,%3)\n"
  426. " jne 1f\n"
  427. " la %0,8(%0)\n"
  428. " brct %1,0b\n"
  429. "1:\n"
  430. #endif
  431. : "+&a" (bytes), "+&a" (size)
  432. : "d" (0UL), "a" (addr), "m" (*(addrtype *) addr)
  433. : "cc" );
  434. return bytes;
  435. }
  436. /**
  437. * __ffz_word - add number of the first unset bit
  438. * @nr: base value the bit number is added to
  439. * @word: the word that is searched for unset bits
  440. */
  441. static inline unsigned long __ffz_word(unsigned long nr, unsigned long word)
  442. {
  443. #ifdef CONFIG_64BIT
  444. if ((word & 0xffffffff) == 0xffffffff) {
  445. word >>= 32;
  446. nr += 32;
  447. }
  448. #endif
  449. if ((word & 0xffff) == 0xffff) {
  450. word >>= 16;
  451. nr += 16;
  452. }
  453. if ((word & 0xff) == 0xff) {
  454. word >>= 8;
  455. nr += 8;
  456. }
  457. return nr + _zb_findmap[(unsigned char) word];
  458. }
  459. /**
  460. * __ffs_word - add number of the first set bit
  461. * @nr: base value the bit number is added to
  462. * @word: the word that is searched for set bits
  463. */
  464. static inline unsigned long __ffs_word(unsigned long nr, unsigned long word)
  465. {
  466. #ifdef CONFIG_64BIT
  467. if ((word & 0xffffffff) == 0) {
  468. word >>= 32;
  469. nr += 32;
  470. }
  471. #endif
  472. if ((word & 0xffff) == 0) {
  473. word >>= 16;
  474. nr += 16;
  475. }
  476. if ((word & 0xff) == 0) {
  477. word >>= 8;
  478. nr += 8;
  479. }
  480. return nr + _sb_findmap[(unsigned char) word];
  481. }
  482. /**
  483. * __load_ulong_be - load big endian unsigned long
  484. * @p: pointer to array of unsigned long
  485. * @offset: byte offset of source value in the array
  486. */
  487. static inline unsigned long __load_ulong_be(const unsigned long *p,
  488. unsigned long offset)
  489. {
  490. p = (unsigned long *)((unsigned long) p + offset);
  491. return *p;
  492. }
  493. /**
  494. * __load_ulong_le - load little endian unsigned long
  495. * @p: pointer to array of unsigned long
  496. * @offset: byte offset of source value in the array
  497. */
  498. static inline unsigned long __load_ulong_le(const unsigned long *p,
  499. unsigned long offset)
  500. {
  501. unsigned long word;
  502. p = (unsigned long *)((unsigned long) p + offset);
  503. #ifndef CONFIG_64BIT
  504. asm volatile(
  505. " ic %0,%O1(%R1)\n"
  506. " icm %0,2,%O1+1(%R1)\n"
  507. " icm %0,4,%O1+2(%R1)\n"
  508. " icm %0,8,%O1+3(%R1)"
  509. : "=&d" (word) : "Q" (*p) : "cc");
  510. #else
  511. asm volatile(
  512. " lrvg %0,%1"
  513. : "=d" (word) : "m" (*p) );
  514. #endif
  515. return word;
  516. }
  517. /*
  518. * The various find bit functions.
  519. */
  520. /*
  521. * ffz - find first zero in word.
  522. * @word: The word to search
  523. *
  524. * Undefined if no zero exists, so code should check against ~0UL first.
  525. */
  526. static inline unsigned long ffz(unsigned long word)
  527. {
  528. return __ffz_word(0, word);
  529. }
  530. /**
  531. * __ffs - find first bit in word.
  532. * @word: The word to search
  533. *
  534. * Undefined if no bit exists, so code should check against 0 first.
  535. */
  536. static inline unsigned long __ffs (unsigned long word)
  537. {
  538. return __ffs_word(0, word);
  539. }
  540. /**
  541. * ffs - find first bit set
  542. * @x: the word to search
  543. *
  544. * This is defined the same way as
  545. * the libc and compiler builtin ffs routines, therefore
  546. * differs in spirit from the above ffz (man ffs).
  547. */
  548. static inline int ffs(int x)
  549. {
  550. if (!x)
  551. return 0;
  552. return __ffs_word(1, x);
  553. }
  554. /**
  555. * find_first_zero_bit - find the first zero bit in a memory region
  556. * @addr: The address to start the search at
  557. * @size: The maximum size to search
  558. *
  559. * Returns the bit-number of the first zero bit, not the number of the byte
  560. * containing a bit.
  561. */
  562. static inline unsigned long find_first_zero_bit(const unsigned long *addr,
  563. unsigned long size)
  564. {
  565. unsigned long bytes, bits;
  566. if (!size)
  567. return 0;
  568. bytes = __ffz_word_loop(addr, size);
  569. bits = __ffz_word(bytes*8, __load_ulong_be(addr, bytes));
  570. return (bits < size) ? bits : size;
  571. }
  572. #define find_first_zero_bit find_first_zero_bit
  573. /**
  574. * find_first_bit - find the first set bit in a memory region
  575. * @addr: The address to start the search at
  576. * @size: The maximum size to search
  577. *
  578. * Returns the bit-number of the first set bit, not the number of the byte
  579. * containing a bit.
  580. */
  581. static inline unsigned long find_first_bit(const unsigned long * addr,
  582. unsigned long size)
  583. {
  584. unsigned long bytes, bits;
  585. if (!size)
  586. return 0;
  587. bytes = __ffs_word_loop(addr, size);
  588. bits = __ffs_word(bytes*8, __load_ulong_be(addr, bytes));
  589. return (bits < size) ? bits : size;
  590. }
  591. #define find_first_bit find_first_bit
  592. /*
  593. * Big endian variant whichs starts bit counting from left using
  594. * the flogr (find leftmost one) instruction.
  595. */
  596. static inline unsigned long __flo_word(unsigned long nr, unsigned long val)
  597. {
  598. register unsigned long bit asm("2") = val;
  599. register unsigned long out asm("3");
  600. asm volatile (
  601. " .insn rre,0xb9830000,%[bit],%[bit]\n"
  602. : [bit] "+d" (bit), [out] "=d" (out) : : "cc");
  603. return nr + bit;
  604. }
  605. /*
  606. * 64 bit special left bitops format:
  607. * order in memory:
  608. * 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
  609. * 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
  610. * 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
  611. * 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
  612. * after that follows the next long with bit numbers
  613. * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
  614. * 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
  615. * 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
  616. * 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
  617. * The reason for this bit ordering is the fact that
  618. * the hardware sets bits in a bitmap starting at bit 0
  619. * and we don't want to scan the bitmap from the 'wrong
  620. * end'.
  621. */
  622. static inline unsigned long find_first_bit_left(const unsigned long *addr,
  623. unsigned long size)
  624. {
  625. unsigned long bytes, bits;
  626. if (!size)
  627. return 0;
  628. bytes = __ffs_word_loop(addr, size);
  629. bits = __flo_word(bytes * 8, __load_ulong_be(addr, bytes));
  630. return (bits < size) ? bits : size;
  631. }
  632. static inline int find_next_bit_left(const unsigned long *addr,
  633. unsigned long size,
  634. unsigned long offset)
  635. {
  636. const unsigned long *p;
  637. unsigned long bit, set;
  638. if (offset >= size)
  639. return size;
  640. bit = offset & (BITS_PER_LONG - 1);
  641. offset -= bit;
  642. size -= offset;
  643. p = addr + offset / BITS_PER_LONG;
  644. if (bit) {
  645. set = __flo_word(0, *p & (~0UL >> bit));
  646. if (set >= size)
  647. return size + offset;
  648. if (set < BITS_PER_LONG)
  649. return set + offset;
  650. offset += BITS_PER_LONG;
  651. size -= BITS_PER_LONG;
  652. p++;
  653. }
  654. return offset + find_first_bit_left(p, size);
  655. }
  656. #define for_each_set_bit_left(bit, addr, size) \
  657. for ((bit) = find_first_bit_left((addr), (size)); \
  658. (bit) < (size); \
  659. (bit) = find_next_bit_left((addr), (size), (bit) + 1))
  660. /* same as for_each_set_bit() but use bit as value to start with */
  661. #define for_each_set_bit_left_cont(bit, addr, size) \
  662. for ((bit) = find_next_bit_left((addr), (size), (bit)); \
  663. (bit) < (size); \
  664. (bit) = find_next_bit_left((addr), (size), (bit) + 1))
  665. /**
  666. * find_next_zero_bit - find the first zero bit in a memory region
  667. * @addr: The address to base the search on
  668. * @offset: The bitnumber to start searching at
  669. * @size: The maximum size to search
  670. */
  671. static inline int find_next_zero_bit (const unsigned long * addr,
  672. unsigned long size,
  673. unsigned long offset)
  674. {
  675. const unsigned long *p;
  676. unsigned long bit, set;
  677. if (offset >= size)
  678. return size;
  679. bit = offset & (BITS_PER_LONG - 1);
  680. offset -= bit;
  681. size -= offset;
  682. p = addr + offset / BITS_PER_LONG;
  683. if (bit) {
  684. /*
  685. * __ffz_word returns BITS_PER_LONG
  686. * if no zero bit is present in the word.
  687. */
  688. set = __ffz_word(bit, *p >> bit);
  689. if (set >= size)
  690. return size + offset;
  691. if (set < BITS_PER_LONG)
  692. return set + offset;
  693. offset += BITS_PER_LONG;
  694. size -= BITS_PER_LONG;
  695. p++;
  696. }
  697. return offset + find_first_zero_bit(p, size);
  698. }
  699. #define find_next_zero_bit find_next_zero_bit
  700. /**
  701. * find_next_bit - find the first set bit in a memory region
  702. * @addr: The address to base the search on
  703. * @offset: The bitnumber to start searching at
  704. * @size: The maximum size to search
  705. */
  706. static inline int find_next_bit (const unsigned long * addr,
  707. unsigned long size,
  708. unsigned long offset)
  709. {
  710. const unsigned long *p;
  711. unsigned long bit, set;
  712. if (offset >= size)
  713. return size;
  714. bit = offset & (BITS_PER_LONG - 1);
  715. offset -= bit;
  716. size -= offset;
  717. p = addr + offset / BITS_PER_LONG;
  718. if (bit) {
  719. /*
  720. * __ffs_word returns BITS_PER_LONG
  721. * if no one bit is present in the word.
  722. */
  723. set = __ffs_word(0, *p & (~0UL << bit));
  724. if (set >= size)
  725. return size + offset;
  726. if (set < BITS_PER_LONG)
  727. return set + offset;
  728. offset += BITS_PER_LONG;
  729. size -= BITS_PER_LONG;
  730. p++;
  731. }
  732. return offset + find_first_bit(p, size);
  733. }
  734. #define find_next_bit find_next_bit
  735. /*
  736. * Every architecture must define this function. It's the fastest
  737. * way of searching a 140-bit bitmap where the first 100 bits are
  738. * unlikely to be set. It's guaranteed that at least one of the 140
  739. * bits is cleared.
  740. */
  741. static inline int sched_find_first_bit(unsigned long *b)
  742. {
  743. return find_first_bit(b, 140);
  744. }
  745. #include <asm-generic/bitops/fls.h>
  746. #include <asm-generic/bitops/__fls.h>
  747. #include <asm-generic/bitops/fls64.h>
  748. #include <asm-generic/bitops/hweight.h>
  749. #include <asm-generic/bitops/lock.h>
  750. /*
  751. * ATTENTION: intel byte ordering convention for ext2 and minix !!
  752. * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
  753. * bit 32 is the LSB of (addr+4).
  754. * That combined with the little endian byte order of Intel gives the
  755. * following bit order in memory:
  756. * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
  757. * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
  758. */
  759. static inline int find_first_zero_bit_le(void *vaddr, unsigned int size)
  760. {
  761. unsigned long bytes, bits;
  762. if (!size)
  763. return 0;
  764. bytes = __ffz_word_loop(vaddr, size);
  765. bits = __ffz_word(bytes*8, __load_ulong_le(vaddr, bytes));
  766. return (bits < size) ? bits : size;
  767. }
  768. #define find_first_zero_bit_le find_first_zero_bit_le
  769. static inline int find_next_zero_bit_le(void *vaddr, unsigned long size,
  770. unsigned long offset)
  771. {
  772. unsigned long *addr = vaddr, *p;
  773. unsigned long bit, set;
  774. if (offset >= size)
  775. return size;
  776. bit = offset & (BITS_PER_LONG - 1);
  777. offset -= bit;
  778. size -= offset;
  779. p = addr + offset / BITS_PER_LONG;
  780. if (bit) {
  781. /*
  782. * s390 version of ffz returns BITS_PER_LONG
  783. * if no zero bit is present in the word.
  784. */
  785. set = __ffz_word(bit, __load_ulong_le(p, 0) >> bit);
  786. if (set >= size)
  787. return size + offset;
  788. if (set < BITS_PER_LONG)
  789. return set + offset;
  790. offset += BITS_PER_LONG;
  791. size -= BITS_PER_LONG;
  792. p++;
  793. }
  794. return offset + find_first_zero_bit_le(p, size);
  795. }
  796. #define find_next_zero_bit_le find_next_zero_bit_le
  797. static inline unsigned long find_first_bit_le(void *vaddr, unsigned long size)
  798. {
  799. unsigned long bytes, bits;
  800. if (!size)
  801. return 0;
  802. bytes = __ffs_word_loop(vaddr, size);
  803. bits = __ffs_word(bytes*8, __load_ulong_le(vaddr, bytes));
  804. return (bits < size) ? bits : size;
  805. }
  806. #define find_first_bit_le find_first_bit_le
  807. static inline int find_next_bit_le(void *vaddr, unsigned long size,
  808. unsigned long offset)
  809. {
  810. unsigned long *addr = vaddr, *p;
  811. unsigned long bit, set;
  812. if (offset >= size)
  813. return size;
  814. bit = offset & (BITS_PER_LONG - 1);
  815. offset -= bit;
  816. size -= offset;
  817. p = addr + offset / BITS_PER_LONG;
  818. if (bit) {
  819. /*
  820. * s390 version of ffz returns BITS_PER_LONG
  821. * if no zero bit is present in the word.
  822. */
  823. set = __ffs_word(0, __load_ulong_le(p, 0) & (~0UL << bit));
  824. if (set >= size)
  825. return size + offset;
  826. if (set < BITS_PER_LONG)
  827. return set + offset;
  828. offset += BITS_PER_LONG;
  829. size -= BITS_PER_LONG;
  830. p++;
  831. }
  832. return offset + find_first_bit_le(p, size);
  833. }
  834. #define find_next_bit_le find_next_bit_le
  835. #include <asm-generic/bitops/le.h>
  836. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  837. #endif /* _S390_BITOPS_H */