bitops.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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.c */
  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,-1\n"
  394. " sra %1,5\n"
  395. " jz 1f\n"
  396. "0: c %2,0(%0,%3)\n"
  397. " jne 1f\n"
  398. " la %0,4(%0)\n"
  399. " brct %1,0b\n"
  400. "1:\n"
  401. #else
  402. " aghi %1,-1\n"
  403. " srag %1,%1,6\n"
  404. " jz 1f\n"
  405. "0: cg %2,0(%0,%3)\n"
  406. " jne 1f\n"
  407. " la %0,8(%0)\n"
  408. " brct %1,0b\n"
  409. "1:\n"
  410. #endif
  411. : "+&a" (bytes), "+&d" (size)
  412. : "d" (-1UL), "a" (addr), "m" (*(addrtype *) addr)
  413. : "cc" );
  414. return bytes;
  415. }
  416. /**
  417. * __ffs_word_loop - find byte offset of first long != 0UL
  418. * @addr: pointer to array of unsigned long
  419. * @size: size of the array in bits
  420. */
  421. static inline unsigned long __ffs_word_loop(const unsigned long *addr,
  422. unsigned long size)
  423. {
  424. typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
  425. unsigned long bytes = 0;
  426. asm volatile(
  427. #ifndef __s390x__
  428. " ahi %1,-1\n"
  429. " sra %1,5\n"
  430. " jz 1f\n"
  431. "0: c %2,0(%0,%3)\n"
  432. " jne 1f\n"
  433. " la %0,4(%0)\n"
  434. " brct %1,0b\n"
  435. "1:\n"
  436. #else
  437. " aghi %1,-1\n"
  438. " srag %1,%1,6\n"
  439. " jz 1f\n"
  440. "0: cg %2,0(%0,%3)\n"
  441. " jne 1f\n"
  442. " la %0,8(%0)\n"
  443. " brct %1,0b\n"
  444. "1:\n"
  445. #endif
  446. : "+&a" (bytes), "+&a" (size)
  447. : "d" (0UL), "a" (addr), "m" (*(addrtype *) addr)
  448. : "cc" );
  449. return bytes;
  450. }
  451. /**
  452. * __ffz_word - add number of the first unset bit
  453. * @nr: base value the bit number is added to
  454. * @word: the word that is searched for unset bits
  455. */
  456. static inline unsigned long __ffz_word(unsigned long nr, unsigned long word)
  457. {
  458. #ifdef __s390x__
  459. if ((word & 0xffffffff) == 0xffffffff) {
  460. word >>= 32;
  461. nr += 32;
  462. }
  463. #endif
  464. if ((word & 0xffff) == 0xffff) {
  465. word >>= 16;
  466. nr += 16;
  467. }
  468. if ((word & 0xff) == 0xff) {
  469. word >>= 8;
  470. nr += 8;
  471. }
  472. return nr + _zb_findmap[(unsigned char) word];
  473. }
  474. /**
  475. * __ffs_word - add number of the first set bit
  476. * @nr: base value the bit number is added to
  477. * @word: the word that is searched for set bits
  478. */
  479. static inline unsigned long __ffs_word(unsigned long nr, unsigned long word)
  480. {
  481. #ifdef __s390x__
  482. if ((word & 0xffffffff) == 0) {
  483. word >>= 32;
  484. nr += 32;
  485. }
  486. #endif
  487. if ((word & 0xffff) == 0) {
  488. word >>= 16;
  489. nr += 16;
  490. }
  491. if ((word & 0xff) == 0) {
  492. word >>= 8;
  493. nr += 8;
  494. }
  495. return nr + _sb_findmap[(unsigned char) word];
  496. }
  497. /**
  498. * __load_ulong_be - load big endian unsigned long
  499. * @p: pointer to array of unsigned long
  500. * @offset: byte offset of source value in the array
  501. */
  502. static inline unsigned long __load_ulong_be(const unsigned long *p,
  503. unsigned long offset)
  504. {
  505. p = (unsigned long *)((unsigned long) p + offset);
  506. return *p;
  507. }
  508. /**
  509. * __load_ulong_le - load little endian unsigned long
  510. * @p: pointer to array of unsigned long
  511. * @offset: byte offset of source value in the array
  512. */
  513. static inline unsigned long __load_ulong_le(const unsigned long *p,
  514. unsigned long offset)
  515. {
  516. unsigned long word;
  517. p = (unsigned long *)((unsigned long) p + offset);
  518. #ifndef __s390x__
  519. asm volatile(
  520. " ic %0,0(%1)\n"
  521. " icm %0,2,1(%1)\n"
  522. " icm %0,4,2(%1)\n"
  523. " icm %0,8,3(%1)"
  524. : "=&d" (word) : "a" (p), "m" (*p) : "cc");
  525. #else
  526. asm volatile(
  527. " lrvg %0,%1"
  528. : "=d" (word) : "m" (*p) );
  529. #endif
  530. return word;
  531. }
  532. /*
  533. * The various find bit functions.
  534. */
  535. /*
  536. * ffz - find first zero in word.
  537. * @word: The word to search
  538. *
  539. * Undefined if no zero exists, so code should check against ~0UL first.
  540. */
  541. static inline unsigned long ffz(unsigned long word)
  542. {
  543. return __ffz_word(0, word);
  544. }
  545. /**
  546. * __ffs - find first bit in word.
  547. * @word: The word to search
  548. *
  549. * Undefined if no bit exists, so code should check against 0 first.
  550. */
  551. static inline unsigned long __ffs (unsigned long word)
  552. {
  553. return __ffs_word(0, word);
  554. }
  555. /**
  556. * ffs - find first bit set
  557. * @x: the word to search
  558. *
  559. * This is defined the same way as
  560. * the libc and compiler builtin ffs routines, therefore
  561. * differs in spirit from the above ffz (man ffs).
  562. */
  563. static inline int ffs(int x)
  564. {
  565. if (!x)
  566. return 0;
  567. return __ffs_word(1, x);
  568. }
  569. /**
  570. * find_first_zero_bit - find the first zero bit in a memory region
  571. * @addr: The address to start the search at
  572. * @size: The maximum size to search
  573. *
  574. * Returns the bit-number of the first zero bit, not the number of the byte
  575. * containing a bit.
  576. */
  577. static inline unsigned long find_first_zero_bit(const unsigned long *addr,
  578. unsigned long size)
  579. {
  580. unsigned long bytes, bits;
  581. if (!size)
  582. return 0;
  583. bytes = __ffz_word_loop(addr, size);
  584. bits = __ffz_word(bytes*8, __load_ulong_be(addr, bytes));
  585. return (bits < size) ? bits : size;
  586. }
  587. /**
  588. * find_first_bit - find the first set bit in a memory region
  589. * @addr: The address to start the search at
  590. * @size: The maximum size to search
  591. *
  592. * Returns the bit-number of the first set bit, not the number of the byte
  593. * containing a bit.
  594. */
  595. static inline unsigned long find_first_bit(const unsigned long * addr,
  596. unsigned long size)
  597. {
  598. unsigned long bytes, bits;
  599. if (!size)
  600. return 0;
  601. bytes = __ffs_word_loop(addr, size);
  602. bits = __ffs_word(bytes*8, __load_ulong_be(addr, bytes));
  603. return (bits < size) ? bits : size;
  604. }
  605. /**
  606. * find_next_zero_bit - find the first zero bit in a memory region
  607. * @addr: The address to base the search on
  608. * @offset: The bitnumber to start searching at
  609. * @size: The maximum size to search
  610. */
  611. static inline int find_next_zero_bit (const unsigned long * addr,
  612. unsigned long size,
  613. unsigned long offset)
  614. {
  615. const unsigned long *p;
  616. unsigned long bit, set;
  617. if (offset >= size)
  618. return size;
  619. bit = offset & (__BITOPS_WORDSIZE - 1);
  620. offset -= bit;
  621. size -= offset;
  622. p = addr + offset / __BITOPS_WORDSIZE;
  623. if (bit) {
  624. /*
  625. * __ffz_word returns __BITOPS_WORDSIZE
  626. * if no zero bit is present in the word.
  627. */
  628. set = __ffz_word(bit, *p >> bit);
  629. if (set >= size)
  630. return size + offset;
  631. if (set < __BITOPS_WORDSIZE)
  632. return set + offset;
  633. offset += __BITOPS_WORDSIZE;
  634. size -= __BITOPS_WORDSIZE;
  635. p++;
  636. }
  637. return offset + find_first_zero_bit(p, size);
  638. }
  639. /**
  640. * find_next_bit - find the first set bit in a memory region
  641. * @addr: The address to base the search on
  642. * @offset: The bitnumber to start searching at
  643. * @size: The maximum size to search
  644. */
  645. static inline int find_next_bit (const unsigned long * addr,
  646. unsigned long size,
  647. unsigned long offset)
  648. {
  649. const unsigned long *p;
  650. unsigned long bit, set;
  651. if (offset >= size)
  652. return size;
  653. bit = offset & (__BITOPS_WORDSIZE - 1);
  654. offset -= bit;
  655. size -= offset;
  656. p = addr + offset / __BITOPS_WORDSIZE;
  657. if (bit) {
  658. /*
  659. * __ffs_word returns __BITOPS_WORDSIZE
  660. * if no one bit is present in the word.
  661. */
  662. set = __ffs_word(0, *p & (~0UL << bit));
  663. if (set >= size)
  664. return size + offset;
  665. if (set < __BITOPS_WORDSIZE)
  666. return set + offset;
  667. offset += __BITOPS_WORDSIZE;
  668. size -= __BITOPS_WORDSIZE;
  669. p++;
  670. }
  671. return offset + find_first_bit(p, size);
  672. }
  673. /*
  674. * Every architecture must define this function. It's the fastest
  675. * way of searching a 140-bit bitmap where the first 100 bits are
  676. * unlikely to be set. It's guaranteed that at least one of the 140
  677. * bits is cleared.
  678. */
  679. static inline int sched_find_first_bit(unsigned long *b)
  680. {
  681. return find_first_bit(b, 140);
  682. }
  683. #include <asm-generic/bitops/fls.h>
  684. #include <asm-generic/bitops/__fls.h>
  685. #include <asm-generic/bitops/fls64.h>
  686. #include <asm-generic/bitops/hweight.h>
  687. #include <asm-generic/bitops/lock.h>
  688. /*
  689. * ATTENTION: intel byte ordering convention for ext2 and minix !!
  690. * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
  691. * bit 32 is the LSB of (addr+4).
  692. * That combined with the little endian byte order of Intel gives the
  693. * following bit order in memory:
  694. * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
  695. * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
  696. */
  697. #define ext2_set_bit(nr, addr) \
  698. __test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
  699. #define ext2_set_bit_atomic(lock, nr, addr) \
  700. test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
  701. #define ext2_clear_bit(nr, addr) \
  702. __test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
  703. #define ext2_clear_bit_atomic(lock, nr, addr) \
  704. test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
  705. #define ext2_test_bit(nr, addr) \
  706. test_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
  707. static inline int ext2_find_first_zero_bit(void *vaddr, unsigned int size)
  708. {
  709. unsigned long bytes, bits;
  710. if (!size)
  711. return 0;
  712. bytes = __ffz_word_loop(vaddr, size);
  713. bits = __ffz_word(bytes*8, __load_ulong_le(vaddr, bytes));
  714. return (bits < size) ? bits : size;
  715. }
  716. static inline int ext2_find_next_zero_bit(void *vaddr, unsigned long size,
  717. unsigned long offset)
  718. {
  719. unsigned long *addr = vaddr, *p;
  720. unsigned long bit, set;
  721. if (offset >= size)
  722. return size;
  723. bit = offset & (__BITOPS_WORDSIZE - 1);
  724. offset -= bit;
  725. size -= offset;
  726. p = addr + offset / __BITOPS_WORDSIZE;
  727. if (bit) {
  728. /*
  729. * s390 version of ffz returns __BITOPS_WORDSIZE
  730. * if no zero bit is present in the word.
  731. */
  732. set = __ffz_word(bit, __load_ulong_le(p, 0) >> bit);
  733. if (set >= size)
  734. return size + offset;
  735. if (set < __BITOPS_WORDSIZE)
  736. return set + offset;
  737. offset += __BITOPS_WORDSIZE;
  738. size -= __BITOPS_WORDSIZE;
  739. p++;
  740. }
  741. return offset + ext2_find_first_zero_bit(p, size);
  742. }
  743. static inline unsigned long ext2_find_first_bit(void *vaddr,
  744. unsigned long size)
  745. {
  746. unsigned long bytes, bits;
  747. if (!size)
  748. return 0;
  749. bytes = __ffs_word_loop(vaddr, size);
  750. bits = __ffs_word(bytes*8, __load_ulong_le(vaddr, bytes));
  751. return (bits < size) ? bits : size;
  752. }
  753. static inline int ext2_find_next_bit(void *vaddr, unsigned long size,
  754. unsigned long offset)
  755. {
  756. unsigned long *addr = vaddr, *p;
  757. unsigned long bit, set;
  758. if (offset >= size)
  759. return size;
  760. bit = offset & (__BITOPS_WORDSIZE - 1);
  761. offset -= bit;
  762. size -= offset;
  763. p = addr + offset / __BITOPS_WORDSIZE;
  764. if (bit) {
  765. /*
  766. * s390 version of ffz returns __BITOPS_WORDSIZE
  767. * if no zero bit is present in the word.
  768. */
  769. set = __ffs_word(0, __load_ulong_le(p, 0) & (~0UL << bit));
  770. if (set >= size)
  771. return size + offset;
  772. if (set < __BITOPS_WORDSIZE)
  773. return set + offset;
  774. offset += __BITOPS_WORDSIZE;
  775. size -= __BITOPS_WORDSIZE;
  776. p++;
  777. }
  778. return offset + ext2_find_first_bit(p, size);
  779. }
  780. #include <asm-generic/bitops/minix.h>
  781. #endif /* __KERNEL__ */
  782. #endif /* _S390_BITOPS_H */