bitops.h 23 KB

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