bitops.h 22 KB

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