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