bitops.h 22 KB

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