ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-s390 / bitops.h
1 #ifndef _S390_BITOPS_H
2 #define _S390_BITOPS_H
3
4 /*
5  *  include/asm-s390/bitops.h
6  *
7  *  S390 version
8  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
9  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
10  *
11  *  Derived from "include/asm-i386/bitops.h"
12  *    Copyright (C) 1992, Linus Torvalds
13  *
14  */
15 #include <linux/config.h>
16 #include <linux/compiler.h>
17
18 /*
19  * 32 bit bitops format:
20  * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
21  * bit 32 is the LSB of *(addr+4). That combined with the
22  * big endian byte order on S390 give the following bit
23  * order in memory:
24  *    1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
25  *    0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
26  * after that follows the next long with bit numbers
27  *    3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
28  *    2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
29  * The reason for this bit ordering is the fact that
30  * in the architecture independent code bits operations
31  * of the form "flags |= (1 << bitnr)" are used INTERMIXED
32  * with operation of the form "set_bit(bitnr, flags)".
33  *
34  * 64 bit bitops format:
35  * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
36  * bit 64 is the LSB of *(addr+8). That combined with the
37  * big endian byte order on S390 give the following bit
38  * order in memory:
39  *    3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
40  *    2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
41  *    1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
42  *    0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
43  * after that follows the next long with bit numbers
44  *    7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
45  *    6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
46  *    5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
47  *    4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
48  * The reason for this bit ordering is the fact that
49  * in the architecture independent code bits operations
50  * of the form "flags |= (1 << bitnr)" are used INTERMIXED
51  * with operation of the form "set_bit(bitnr, flags)".
52  */
53
54 /* set ALIGN_CS to 1 if the SMP safe bit operations should
55  * align the address to 4 byte boundary. It seems to work
56  * without the alignment. 
57  */
58 #ifdef __KERNEL__
59 #define ALIGN_CS 0
60 #else
61 #define ALIGN_CS 1
62 #ifndef CONFIG_SMP
63 #error "bitops won't work without CONFIG_SMP"
64 #endif
65 #endif
66
67 /* bitmap tables from arch/S390/kernel/bitmap.S */
68 extern const char _oi_bitmap[];
69 extern const char _ni_bitmap[];
70 extern const char _zb_findmap[];
71 extern const char _sb_findmap[];
72
73 #ifndef __s390x__
74
75 #define __BITOPS_ALIGN          3
76 #define __BITOPS_WORDSIZE       32
77 #define __BITOPS_OR             "or"
78 #define __BITOPS_AND            "nr"
79 #define __BITOPS_XOR            "xr"
80
81 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string)         \
82         __asm__ __volatile__("   l   %0,0(%4)\n"                        \
83                              "0: lr  %1,%0\n"                           \
84                              __op_string "  %1,%3\n"                    \
85                              "   cs  %0,%1,0(%4)\n"                     \
86                              "   jl  0b"                                \
87                              : "=&d" (__old), "=&d" (__new),            \
88                                "=m" (*(unsigned long *) __addr)         \
89                              : "d" (__val), "a" (__addr),               \
90                                "m" (*(unsigned long *) __addr) : "cc" );
91
92 #else /* __s390x__ */
93
94 #define __BITOPS_ALIGN          7
95 #define __BITOPS_WORDSIZE       64
96 #define __BITOPS_OR             "ogr"
97 #define __BITOPS_AND            "ngr"
98 #define __BITOPS_XOR            "xgr"
99
100 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string)         \
101         __asm__ __volatile__("   lg  %0,0(%4)\n"                        \
102                              "0: lgr %1,%0\n"                           \
103                              __op_string "  %1,%3\n"                    \
104                              "   csg %0,%1,0(%4)\n"                     \
105                              "   jl  0b"                                \
106                              : "=&d" (__old), "=&d" (__new),            \
107                                "=m" (*(unsigned long *) __addr)         \
108                              : "d" (__val), "a" (__addr),               \
109                                "m" (*(unsigned long *) __addr) : "cc" );
110
111 #endif /* __s390x__ */
112
113 #define __BITOPS_BARRIER() __asm__ __volatile__ ( "" : : : "memory" )
114
115 #ifdef CONFIG_SMP
116 /*
117  * SMP safe set_bit routine based on compare and swap (CS)
118  */
119 static inline void set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
120 {
121         unsigned long addr, old, new, mask;
122
123         addr = (unsigned long) ptr;
124 #if ALIGN_CS == 1
125         nr += (addr & __BITOPS_ALIGN) << 3;    /* add alignment to bit number */
126         addr ^= addr & __BITOPS_ALIGN;         /* align address to 8 */
127 #endif
128         /* calculate address for CS */
129         addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
130         /* make OR mask */
131         mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
132         /* Do the atomic update. */
133         __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
134 }
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
143         addr = (unsigned long) ptr;
144 #if ALIGN_CS == 1
145         nr += (addr & __BITOPS_ALIGN) << 3;    /* add alignment to bit number */
146         addr ^= addr & __BITOPS_ALIGN;         /* align address to 8 */
147 #endif
148         /* calculate address for CS */
149         addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
150         /* make AND mask */
151         mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
152         /* Do the atomic update. */
153         __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
154 }
155
156 /*
157  * SMP safe change_bit routine based on compare and swap (CS)
158  */
159 static inline void change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
160 {
161         unsigned long addr, old, new, mask;
162
163         addr = (unsigned long) ptr;
164 #if ALIGN_CS == 1
165         nr += (addr & __BITOPS_ALIGN) << 3;    /* add alignment to bit number */
166         addr ^= addr & __BITOPS_ALIGN;         /* align address to 8 */
167 #endif
168         /* calculate address for CS */
169         addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
170         /* make XOR mask */
171         mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
172         /* Do the atomic update. */
173         __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
174 }
175
176 /*
177  * SMP safe test_and_set_bit routine based on compare and swap (CS)
178  */
179 static inline int
180 test_and_set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
181 {
182         unsigned long addr, old, new, mask;
183
184         addr = (unsigned long) ptr;
185 #if ALIGN_CS == 1
186         nr += (addr & __BITOPS_ALIGN) << 3;    /* add alignment to bit number */
187         addr ^= addr & __BITOPS_ALIGN;         /* align address to 8 */
188 #endif
189         /* calculate address for CS */
190         addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
191         /* make OR/test mask */
192         mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
193         /* Do the atomic update. */
194         __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
195         __BITOPS_BARRIER();
196         return (old & mask) != 0;
197 }
198
199 /*
200  * SMP safe test_and_clear_bit routine based on compare and swap (CS)
201  */
202 static inline int
203 test_and_clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
204 {
205         unsigned long addr, old, new, mask;
206
207         addr = (unsigned long) ptr;
208 #if ALIGN_CS == 1
209         nr += (addr & __BITOPS_ALIGN) << 3;    /* add alignment to bit number */
210         addr ^= addr & __BITOPS_ALIGN;         /* align address to 8 */
211 #endif
212         /* calculate address for CS */
213         addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
214         /* make AND/test mask */
215         mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
216         /* Do the atomic update. */
217         __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
218         __BITOPS_BARRIER();
219         return (old ^ new) != 0;
220 }
221
222 /*
223  * SMP safe test_and_change_bit routine based on compare and swap (CS) 
224  */
225 static inline int
226 test_and_change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
227 {
228         unsigned long addr, old, new, mask;
229
230         addr = (unsigned long) ptr;
231 #if ALIGN_CS == 1
232         nr += (addr & __BITOPS_ALIGN) << 3;  /* add alignment to bit number */
233         addr ^= addr & __BITOPS_ALIGN;       /* align address to 8 */
234 #endif
235         /* calculate address for CS */
236         addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
237         /* make XOR/test mask */
238         mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
239         /* Do the atomic update. */
240         __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
241         __BITOPS_BARRIER();
242         return (old & mask) != 0;
243 }
244 #endif /* CONFIG_SMP */
245
246 /*
247  * fast, non-SMP set_bit routine
248  */
249 static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
250 {
251         unsigned long addr;
252
253         addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
254         asm volatile("oc 0(1,%1),0(%2)"
255                      : "=m" (*(char *) addr)
256                      : "a" (addr), "a" (_oi_bitmap + (nr & 7)),
257                        "m" (*(char *) addr) : "cc" );
258 }
259
260 static inline void 
261 __constant_set_bit(const unsigned long nr, volatile unsigned long *ptr)
262 {
263         unsigned long addr;
264
265         addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
266         switch (nr&7) {
267         case 0:
268                 asm volatile ("oi 0(%1),0x01" : "=m" (*(char *) addr)
269                               : "a" (addr), "m" (*(char *) addr) : "cc" );
270                 break;
271         case 1:
272                 asm volatile ("oi 0(%1),0x02" : "=m" (*(char *) addr)
273                               : "a" (addr), "m" (*(char *) addr) : "cc" );
274                 break;
275         case 2:
276                 asm volatile ("oi 0(%1),0x04" : "=m" (*(char *) addr)
277                               : "a" (addr), "m" (*(char *) addr) : "cc" );
278                 break;
279         case 3:
280                 asm volatile ("oi 0(%1),0x08" : "=m" (*(char *) addr)
281                               : "a" (addr), "m" (*(char *) addr) : "cc" );
282                 break;
283         case 4:
284                 asm volatile ("oi 0(%1),0x10" : "=m" (*(char *) addr)
285                               : "a" (addr), "m" (*(char *) addr) : "cc" );
286                 break;
287         case 5:
288                 asm volatile ("oi 0(%1),0x20" : "=m" (*(char *) addr)
289                               : "a" (addr), "m" (*(char *) addr) : "cc" );
290                 break;
291         case 6:
292                 asm volatile ("oi 0(%1),0x40" : "=m" (*(char *) addr)
293                               : "a" (addr), "m" (*(char *) addr) : "cc" );
294                 break;
295         case 7:
296                 asm volatile ("oi 0(%1),0x80" : "=m" (*(char *) addr)
297                               : "a" (addr), "m" (*(char *) addr) : "cc" );
298                 break;
299         }
300 }
301
302 #define set_bit_simple(nr,addr) \
303 (__builtin_constant_p((nr)) ? \
304  __constant_set_bit((nr),(addr)) : \
305  __set_bit((nr),(addr)) )
306
307 /*
308  * fast, non-SMP clear_bit routine
309  */
310 static inline void 
311 __clear_bit(unsigned long nr, volatile unsigned long *ptr)
312 {
313         unsigned long addr;
314
315         addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
316         asm volatile("nc 0(1,%1),0(%2)"
317                      : "=m" (*(char *) addr)
318                      : "a" (addr), "a" (_ni_bitmap + (nr & 7)),
319                        "m" (*(char *) addr) : "cc" );
320 }
321
322 static inline void 
323 __constant_clear_bit(const unsigned long nr, volatile unsigned long *ptr)
324 {
325         unsigned long addr;
326
327         addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
328         switch (nr&7) {
329         case 0:
330                 asm volatile ("ni 0(%1),0xFE" : "=m" (*(char *) addr)
331                               : "a" (addr), "m" (*(char *) addr) : "cc" );
332                 break;
333         case 1:
334                 asm volatile ("ni 0(%1),0xFD": "=m" (*(char *) addr)
335                               : "a" (addr), "m" (*(char *) addr) : "cc" );
336                 break;
337         case 2:
338                 asm volatile ("ni 0(%1),0xFB" : "=m" (*(char *) addr)
339                               : "a" (addr), "m" (*(char *) addr) : "cc" );
340                 break;
341         case 3:
342                 asm volatile ("ni 0(%1),0xF7" : "=m" (*(char *) addr)
343                               : "a" (addr), "m" (*(char *) addr) : "cc" );
344                 break;
345         case 4:
346                 asm volatile ("ni 0(%1),0xEF" : "=m" (*(char *) addr)
347                               : "a" (addr), "m" (*(char *) addr) : "cc" );
348                 break;
349         case 5:
350                 asm volatile ("ni 0(%1),0xDF" : "=m" (*(char *) addr)
351                               : "a" (addr), "m" (*(char *) addr) : "cc" );
352                 break;
353         case 6:
354                 asm volatile ("ni 0(%1),0xBF" : "=m" (*(char *) addr)
355                               : "a" (addr), "m" (*(char *) addr) : "cc" );
356                 break;
357         case 7:
358                 asm volatile ("ni 0(%1),0x7F" : "=m" (*(char *) addr)
359                               : "a" (addr), "m" (*(char *) addr) : "cc" );
360                 break;
361         }
362 }
363
364 #define clear_bit_simple(nr,addr) \
365 (__builtin_constant_p((nr)) ? \
366  __constant_clear_bit((nr),(addr)) : \
367  __clear_bit((nr),(addr)) )
368
369 /* 
370  * fast, non-SMP change_bit routine 
371  */
372 static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
373 {
374         unsigned long addr;
375
376         addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
377         asm volatile("xc 0(1,%1),0(%2)"
378                      :  "=m" (*(char *) addr)
379                      : "a" (addr), "a" (_oi_bitmap + (nr & 7)),
380                        "m" (*(char *) addr) : "cc" );
381 }
382
383 static inline void 
384 __constant_change_bit(const unsigned long nr, volatile unsigned long *ptr) 
385 {
386         unsigned long addr;
387
388         addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
389         switch (nr&7) {
390         case 0:
391                 asm volatile ("xi 0(%1),0x01" : "=m" (*(char *) addr)
392                               : "a" (addr), "m" (*(char *) addr) : "cc" );
393                 break;
394         case 1:
395                 asm volatile ("xi 0(%1),0x02" : "=m" (*(char *) addr)
396                               : "a" (addr), "m" (*(char *) addr) : "cc" );
397                 break;
398         case 2:
399                 asm volatile ("xi 0(%1),0x04" : "=m" (*(char *) addr)
400                               : "a" (addr), "m" (*(char *) addr) : "cc" );
401                 break;
402         case 3:
403                 asm volatile ("xi 0(%1),0x08" : "=m" (*(char *) addr)
404                               : "a" (addr), "m" (*(char *) addr) : "cc" );
405                 break;
406         case 4:
407                 asm volatile ("xi 0(%1),0x10" : "=m" (*(char *) addr)
408                               : "a" (addr), "m" (*(char *) addr) : "cc" );
409                 break;
410         case 5:
411                 asm volatile ("xi 0(%1),0x20" : "=m" (*(char *) addr)
412                               : "a" (addr), "m" (*(char *) addr) : "cc" );
413                 break;
414         case 6:
415                 asm volatile ("xi 0(%1),0x40" : "=m" (*(char *) addr)
416                               : "a" (addr), "m" (*(char *) addr) : "cc" );
417                 break;
418         case 7:
419                 asm volatile ("xi 0(%1),0x80" : "=m" (*(char *) addr)
420                               : "a" (addr), "m" (*(char *) addr) : "cc" );
421                 break;
422         }
423 }
424
425 #define change_bit_simple(nr,addr) \
426 (__builtin_constant_p((nr)) ? \
427  __constant_change_bit((nr),(addr)) : \
428  __change_bit((nr),(addr)) )
429
430 /*
431  * fast, non-SMP test_and_set_bit routine
432  */
433 static inline int
434 test_and_set_bit_simple(unsigned long nr, volatile unsigned long *ptr)
435 {
436         unsigned long addr;
437         unsigned char ch;
438
439         addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
440         ch = *(unsigned char *) addr;
441         asm volatile("oc 0(1,%1),0(%2)"
442                      : "=m" (*(char *) addr)
443                      : "a" (addr), "a" (_oi_bitmap + (nr & 7)),
444                        "m" (*(char *) addr) : "cc", "memory" );
445         return (ch >> (nr & 7)) & 1;
446 }
447 #define __test_and_set_bit(X,Y)         test_and_set_bit_simple(X,Y)
448
449 /*
450  * fast, non-SMP test_and_clear_bit routine
451  */
452 static inline int
453 test_and_clear_bit_simple(unsigned long nr, volatile unsigned long *ptr)
454 {
455         unsigned long addr;
456         unsigned char ch;
457
458         addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
459         ch = *(unsigned char *) addr;
460         asm volatile("nc 0(1,%1),0(%2)"
461                      : "=m" (*(char *) addr)
462                      : "a" (addr), "a" (_ni_bitmap + (nr & 7)),
463                        "m" (*(char *) addr) : "cc", "memory" );
464         return (ch >> (nr & 7)) & 1;
465 }
466 #define __test_and_clear_bit(X,Y)       test_and_clear_bit_simple(X,Y)
467
468 /*
469  * fast, non-SMP test_and_change_bit routine
470  */
471 static inline int
472 test_and_change_bit_simple(unsigned long nr, volatile unsigned long *ptr)
473 {
474         unsigned long addr;
475         unsigned char ch;
476
477         addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
478         ch = *(unsigned char *) addr;
479         asm volatile("xc 0(1,%1),0(%2)"
480                      : "=m" (*(char *) addr)
481                      : "a" (addr), "a" (_oi_bitmap + (nr & 7)),
482                        "m" (*(char *) addr) : "cc", "memory" );
483         return (ch >> (nr & 7)) & 1;
484 }
485 #define __test_and_change_bit(X,Y)      test_and_change_bit_simple(X,Y)
486
487 #ifdef CONFIG_SMP
488 #define set_bit             set_bit_cs
489 #define clear_bit           clear_bit_cs
490 #define change_bit          change_bit_cs
491 #define test_and_set_bit    test_and_set_bit_cs
492 #define test_and_clear_bit  test_and_clear_bit_cs
493 #define test_and_change_bit test_and_change_bit_cs
494 #else
495 #define set_bit             set_bit_simple
496 #define clear_bit           clear_bit_simple
497 #define change_bit          change_bit_simple
498 #define test_and_set_bit    test_and_set_bit_simple
499 #define test_and_clear_bit  test_and_clear_bit_simple
500 #define test_and_change_bit test_and_change_bit_simple
501 #endif
502
503
504 /*
505  * This routine doesn't need to be atomic.
506  */
507
508 static inline int __test_bit(unsigned long nr, const volatile unsigned long *ptr)
509 {
510         unsigned long addr;
511         unsigned char ch;
512
513         addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
514         ch = *(volatile unsigned char *) addr;
515         return (ch >> (nr & 7)) & 1;
516 }
517
518 static inline int 
519 __constant_test_bit(unsigned long nr, const volatile unsigned long *addr) {
520     return (((volatile char *) addr)
521             [(nr^(__BITOPS_WORDSIZE-8))>>3] & (1<<(nr&7)));
522 }
523
524 #define test_bit(nr,addr) \
525 (__builtin_constant_p((nr)) ? \
526  __constant_test_bit((nr),(addr)) : \
527  __test_bit((nr),(addr)) )
528
529 #ifndef __s390x__
530
531 /*
532  * Find-bit routines..
533  */
534 static inline int
535 find_first_zero_bit(const unsigned long * addr, unsigned int size)
536 {
537         unsigned long cmp, count;
538         unsigned int res;
539
540         if (!size)
541                 return 0;
542         __asm__("   lhi  %1,-1\n"
543                 "   lr   %2,%3\n"
544                 "   slr  %0,%0\n"
545                 "   ahi  %2,31\n"
546                 "   srl  %2,5\n"
547                 "0: c    %1,0(%0,%4)\n"
548                 "   jne  1f\n"
549                 "   ahi  %0,4\n"
550                 "   brct %2,0b\n"
551                 "   lr   %0,%3\n"
552                 "   j    4f\n"
553                 "1: l    %2,0(%0,%4)\n"
554                 "   sll  %0,3\n"
555                 "   lhi  %1,0xff\n"
556                 "   tml  %2,0xffff\n"
557                 "   jno  2f\n"
558                 "   ahi  %0,16\n"
559                 "   srl  %2,16\n"
560                 "2: tml  %2,0x00ff\n"
561                 "   jno  3f\n"
562                 "   ahi  %0,8\n"
563                 "   srl  %2,8\n"
564                 "3: nr   %2,%1\n"
565                 "   ic   %2,0(%2,%5)\n"
566                 "   alr  %0,%2\n"
567                 "4:"
568                 : "=&a" (res), "=&d" (cmp), "=&a" (count)
569                 : "a" (size), "a" (addr), "a" (&_zb_findmap) : "cc" );
570         return (res < size) ? res : size;
571 }
572
573 static inline int
574 find_first_bit(const unsigned long * addr, unsigned int size)
575 {
576         unsigned long cmp, count;
577         unsigned int res;
578
579         if (!size)
580                 return 0;
581         __asm__("   slr  %1,%1\n"
582                 "   lr   %2,%3\n"
583                 "   slr  %0,%0\n"
584                 "   ahi  %2,31\n"
585                 "   srl  %2,5\n"
586                 "0: c    %1,0(%0,%4)\n"
587                 "   jne  1f\n"
588                 "   ahi  %0,4\n"
589                 "   brct %2,0b\n"
590                 "   lr   %0,%3\n"
591                 "   j    4f\n"
592                 "1: l    %2,0(%0,%4)\n"
593                 "   sll  %0,3\n"
594                 "   lhi  %1,0xff\n"
595                 "   tml  %2,0xffff\n"
596                 "   jnz  2f\n"
597                 "   ahi  %0,16\n"
598                 "   srl  %2,16\n"
599                 "2: tml  %2,0x00ff\n"
600                 "   jnz  3f\n"
601                 "   ahi  %0,8\n"
602                 "   srl  %2,8\n"
603                 "3: nr   %2,%1\n"
604                 "   ic   %2,0(%2,%5)\n"
605                 "   alr  %0,%2\n"
606                 "4:"
607                 : "=&a" (res), "=&d" (cmp), "=&a" (count)
608                 : "a" (size), "a" (addr), "a" (&_sb_findmap) : "cc" );
609         return (res < size) ? res : size;
610 }
611
612 static inline int
613 find_next_zero_bit (const unsigned long * addr, int size, int offset)
614 {
615         unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
616         unsigned long bitvec, reg;
617         int set, bit = offset & 31, res;
618
619         if (bit) {
620                 /*
621                  * Look for zero in first word
622                  */
623                 bitvec = (*p) >> bit;
624                 __asm__("   slr  %0,%0\n"
625                         "   lhi  %2,0xff\n"
626                         "   tml  %1,0xffff\n"
627                         "   jno  0f\n"
628                         "   ahi  %0,16\n"
629                         "   srl  %1,16\n"
630                         "0: tml  %1,0x00ff\n"
631                         "   jno  1f\n"
632                         "   ahi  %0,8\n"
633                         "   srl  %1,8\n"
634                         "1: nr   %1,%2\n"
635                         "   ic   %1,0(%1,%3)\n"
636                         "   alr  %0,%1"
637                         : "=&d" (set), "+a" (bitvec), "=&d" (reg)
638                         : "a" (&_zb_findmap) : "cc" );
639                 if (set < (32 - bit))
640                         return set + offset;
641                 offset += 32 - bit;
642                 p++;
643         }
644         /*
645          * No zero yet, search remaining full words for a zero
646          */
647         res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
648         return (offset + res);
649 }
650
651 static inline int
652 find_next_bit (const unsigned long * addr, int size, int offset)
653 {
654         unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
655         unsigned long bitvec, reg;
656         int set, bit = offset & 31, res;
657
658         if (bit) {
659                 /*
660                  * Look for set bit in first word
661                  */
662                 bitvec = (*p) >> bit;
663                 __asm__("   slr  %0,%0\n"
664                         "   lhi  %2,0xff\n"
665                         "   tml  %1,0xffff\n"
666                         "   jnz  0f\n"
667                         "   ahi  %0,16\n"
668                         "   srl  %1,16\n"
669                         "0: tml  %1,0x00ff\n"
670                         "   jnz  1f\n"
671                         "   ahi  %0,8\n"
672                         "   srl  %1,8\n"
673                         "1: nr   %1,%2\n"
674                         "   ic   %1,0(%1,%3)\n"
675                         "   alr  %0,%1"
676                         : "=&d" (set), "+a" (bitvec), "=&d" (reg)
677                         : "a" (&_sb_findmap) : "cc" );
678                 if (set < (32 - bit))
679                         return set + offset;
680                 offset += 32 - bit;
681                 p++;
682         }
683         /*
684          * No set bit yet, search remaining full words for a bit
685          */
686         res = find_first_bit (p, size - 32 * (p - (unsigned long *) addr));
687         return (offset + res);
688 }
689
690 #else /* __s390x__ */
691
692 /*
693  * Find-bit routines..
694  */
695 static inline unsigned long
696 find_first_zero_bit(const unsigned long * addr, unsigned long size)
697 {
698         unsigned long res, cmp, count;
699
700         if (!size)
701                 return 0;
702         __asm__("   lghi  %1,-1\n"
703                 "   lgr   %2,%3\n"
704                 "   slgr  %0,%0\n"
705                 "   aghi  %2,63\n"
706                 "   srlg  %2,%2,6\n"
707                 "0: cg    %1,0(%0,%4)\n"
708                 "   jne   1f\n"
709                 "   aghi  %0,8\n"
710                 "   brct  %2,0b\n"
711                 "   lgr   %0,%3\n"
712                 "   j     5f\n"
713                 "1: lg    %2,0(%0,%4)\n"
714                 "   sllg  %0,%0,3\n"
715                 "   clr   %2,%1\n"
716                 "   jne   2f\n"
717                 "   aghi  %0,32\n"
718                 "   srlg  %2,%2,32\n"
719                 "2: lghi  %1,0xff\n"
720                 "   tmll  %2,0xffff\n"
721                 "   jno   3f\n"
722                 "   aghi  %0,16\n"
723                 "   srl   %2,16\n"
724                 "3: tmll  %2,0x00ff\n"
725                 "   jno   4f\n"
726                 "   aghi  %0,8\n"
727                 "   srl   %2,8\n"
728                 "4: ngr   %2,%1\n"
729                 "   ic    %2,0(%2,%5)\n"
730                 "   algr  %0,%2\n"
731                 "5:"
732                 : "=&a" (res), "=&d" (cmp), "=&a" (count)
733                 : "a" (size), "a" (addr), "a" (&_zb_findmap) : "cc" );
734         return (res < size) ? res : size;
735 }
736
737 static inline unsigned long
738 find_first_bit(const unsigned long * addr, unsigned long size)
739 {
740         unsigned long res, cmp, count;
741
742         if (!size)
743                 return 0;
744         __asm__("   slgr  %1,%1\n"
745                 "   lgr   %2,%3\n"
746                 "   slgr  %0,%0\n"
747                 "   aghi  %2,63\n"
748                 "   srlg  %2,%2,6\n"
749                 "0: cg    %1,0(%0,%4)\n"
750                 "   jne   1f\n"
751                 "   aghi  %0,8\n"
752                 "   brct  %2,0b\n"
753                 "   lgr   %0,%3\n"
754                 "   j     5f\n"
755                 "1: lg    %2,0(%0,%4)\n"
756                 "   sllg  %0,%0,3\n"
757                 "   clr   %2,%1\n"
758                 "   jne   2f\n"
759                 "   aghi  %0,32\n"
760                 "   srlg  %2,%2,32\n"
761                 "2: lghi  %1,0xff\n"
762                 "   tmll  %2,0xffff\n"
763                 "   jnz   3f\n"
764                 "   aghi  %0,16\n"
765                 "   srl   %2,16\n"
766                 "3: tmll  %2,0x00ff\n"
767                 "   jnz   4f\n"
768                 "   aghi  %0,8\n"
769                 "   srl   %2,8\n"
770                 "4: ngr   %2,%1\n"
771                 "   ic    %2,0(%2,%5)\n"
772                 "   algr  %0,%2\n"
773                 "5:"
774                 : "=&a" (res), "=&d" (cmp), "=&a" (count)
775                 : "a" (size), "a" (addr), "a" (&_sb_findmap) : "cc" );
776         return (res < size) ? res : size;
777 }
778
779 static inline unsigned long
780 find_next_zero_bit (const unsigned long * addr, unsigned long size, unsigned long offset)
781 {
782         unsigned long * p = ((unsigned long *) addr) + (offset >> 6);
783         unsigned long bitvec, reg;
784         unsigned long set, bit = offset & 63, res;
785
786         if (bit) {
787                 /*
788                  * Look for zero in first word
789                  */
790                 bitvec = (*p) >> bit;
791                 __asm__("   lhi  %2,-1\n"
792                         "   slgr %0,%0\n"
793                         "   clr  %1,%2\n"
794                         "   jne  0f\n"
795                         "   aghi %0,32\n"
796                         "   srlg %1,%1,32\n"
797                         "0: lghi %2,0xff\n"
798                         "   tmll %1,0xffff\n"
799                         "   jno  1f\n"
800                         "   aghi %0,16\n"
801                         "   srlg %1,%1,16\n"
802                         "1: tmll %1,0x00ff\n"
803                         "   jno  2f\n"
804                         "   aghi %0,8\n"
805                         "   srlg %1,%1,8\n"
806                         "2: ngr  %1,%2\n"
807                         "   ic   %1,0(%1,%3)\n"
808                         "   algr %0,%1"
809                         : "=&d" (set), "+a" (bitvec), "=&d" (reg)
810                         : "a" (&_zb_findmap) : "cc" );
811                 if (set < (64 - bit))
812                         return set + offset;
813                 offset += 64 - bit;
814                 p++;
815         }
816         /*
817          * No zero yet, search remaining full words for a zero
818          */
819         res = find_first_zero_bit (p, size - 64 * (p - (unsigned long *) addr));
820         return (offset + res);
821 }
822
823 static inline unsigned long
824 find_next_bit (const unsigned long * addr, unsigned long size, unsigned long offset)
825 {
826         unsigned long * p = ((unsigned long *) addr) + (offset >> 6);
827         unsigned long bitvec, reg;
828         unsigned long set, bit = offset & 63, res;
829
830         if (bit) {
831                 /*
832                  * Look for zero in first word
833                  */
834                 bitvec = (*p) >> bit;
835                 __asm__("   slgr %0,%0\n"
836                         "   ltr  %1,%1\n"
837                         "   jnz  0f\n"
838                         "   aghi %0,32\n"
839                         "   srlg %1,%1,32\n"
840                         "0: lghi %2,0xff\n"
841                         "   tmll %1,0xffff\n"
842                         "   jnz  1f\n"
843                         "   aghi %0,16\n"
844                         "   srlg %1,%1,16\n"
845                         "1: tmll %1,0x00ff\n"
846                         "   jnz  2f\n"
847                         "   aghi %0,8\n"
848                         "   srlg %1,%1,8\n"
849                         "2: ngr  %1,%2\n"
850                         "   ic   %1,0(%1,%3)\n"
851                         "   algr %0,%1"
852                         : "=&d" (set), "+a" (bitvec), "=&d" (reg)
853                         : "a" (&_sb_findmap) : "cc" );
854                 if (set < (64 - bit))
855                         return set + offset;
856                 offset += 64 - bit;
857                 p++;
858         }
859         /*
860          * No set bit yet, search remaining full words for a bit
861          */
862         res = find_first_bit (p, size - 64 * (p - (unsigned long *) addr));
863         return (offset + res);
864 }
865
866 #endif /* __s390x__ */
867
868 /*
869  * ffz = Find First Zero in word. Undefined if no zero exists,
870  * so code should check against ~0UL first..
871  */
872 static inline unsigned long ffz(unsigned long word)
873 {
874         unsigned long bit = 0;
875
876 #ifdef __s390x__
877         if (likely((word & 0xffffffff) == 0xffffffff)) {
878                 word >>= 32;
879                 bit += 32;
880         }
881 #endif
882         if (likely((word & 0xffff) == 0xffff)) {
883                 word >>= 16;
884                 bit += 16;
885         }
886         if (likely((word & 0xff) == 0xff)) {
887                 word >>= 8;
888                 bit += 8;
889         }
890         return bit + _zb_findmap[word & 0xff];
891 }
892
893 /*
894  * __ffs = find first bit in word. Undefined if no bit exists,
895  * so code should check against 0UL first..
896  */
897 static inline unsigned long __ffs (unsigned long word)
898 {
899         unsigned long bit = 0;
900
901 #ifdef __s390x__
902         if (likely((word & 0xffffffff) == 0)) {
903                 word >>= 32;
904                 bit += 32;
905         }
906 #endif
907         if (likely((word & 0xffff) == 0)) {
908                 word >>= 16;
909                 bit += 16;
910         }
911         if (likely((word & 0xff) == 0)) {
912                 word >>= 8;
913                 bit += 8;
914         }
915         return bit + _sb_findmap[word & 0xff];
916 }
917
918 /*
919  * Every architecture must define this function. It's the fastest
920  * way of searching a 140-bit bitmap where the first 100 bits are
921  * unlikely to be set. It's guaranteed that at least one of the 140
922  * bits is cleared.
923  */
924 static inline int sched_find_first_bit(unsigned long *b)
925 {
926         return find_first_bit(b, 140);
927 }
928
929 /*
930  * ffs: find first bit set. This is defined the same way as
931  * the libc and compiler builtin ffs routines, therefore
932  * differs in spirit from the above ffz (man ffs).
933  */
934 #define ffs(x) generic_ffs(x)
935
936 /*
937  * fls: find last bit set.
938  */
939 #define fls(x) generic_fls(x)
940
941 /*
942  * hweightN: returns the hamming weight (i.e. the number
943  * of bits set) of a N-bit word
944  */
945 #define hweight64(x)                                            \
946 ({                                                              \
947         unsigned long __x = (x);                                \
948         unsigned int __w;                                       \
949         __w = generic_hweight32((unsigned int) __x);            \
950         __w += generic_hweight32((unsigned int) (__x>>32));     \
951         __w;                                                    \
952 })
953 #define hweight32(x) generic_hweight32(x)
954 #define hweight16(x) generic_hweight16(x)
955 #define hweight8(x) generic_hweight8(x)
956
957
958 #ifdef __KERNEL__
959
960 /*
961  * ATTENTION: intel byte ordering convention for ext2 and minix !!
962  * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
963  * bit 32 is the LSB of (addr+4).
964  * That combined with the little endian byte order of Intel gives the
965  * following bit order in memory:
966  *    07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
967  *    23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
968  */
969
970 #define ext2_set_bit(nr, addr)       \
971         test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
972 #define ext2_set_bit_atomic(lock, nr, addr)       \
973         test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
974 #define ext2_clear_bit(nr, addr)     \
975         test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
976 #define ext2_clear_bit_atomic(lock, nr, addr)     \
977         test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
978 #define ext2_test_bit(nr, addr)      \
979         test_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
980
981 #ifndef __s390x__
982
983 static inline int 
984 ext2_find_first_zero_bit(void *vaddr, unsigned int size)
985 {
986         unsigned long cmp, count;
987         unsigned int res;
988
989         if (!size)
990                 return 0;
991         __asm__("   lhi  %1,-1\n"
992                 "   lr   %2,%3\n"
993                 "   ahi  %2,31\n"
994                 "   srl  %2,5\n"
995                 "   slr  %0,%0\n"
996                 "0: cl   %1,0(%0,%4)\n"
997                 "   jne  1f\n"
998                 "   ahi  %0,4\n"
999                 "   brct %2,0b\n"
1000                 "   lr   %0,%3\n"
1001                 "   j    4f\n"
1002                 "1: l    %2,0(%0,%4)\n"
1003                 "   sll  %0,3\n"
1004                 "   ahi  %0,24\n"
1005                 "   lhi  %1,0xff\n"
1006                 "   tmh  %2,0xffff\n"
1007                 "   jo   2f\n"
1008                 "   ahi  %0,-16\n"
1009                 "   srl  %2,16\n"
1010                 "2: tml  %2,0xff00\n"
1011                 "   jo   3f\n"
1012                 "   ahi  %0,-8\n"
1013                 "   srl  %2,8\n"
1014                 "3: nr   %2,%1\n"
1015                 "   ic   %2,0(%2,%5)\n"
1016                 "   alr  %0,%2\n"
1017                 "4:"
1018                 : "=&a" (res), "=&d" (cmp), "=&a" (count)
1019                 : "a" (size), "a" (vaddr), "a" (&_zb_findmap) : "cc" );
1020         return (res < size) ? res : size;
1021 }
1022
1023 static inline int 
1024 ext2_find_next_zero_bit(void *vaddr, unsigned int size, unsigned offset)
1025 {
1026         unsigned long *addr = vaddr;
1027         unsigned long *p = addr + (offset >> 5);
1028         unsigned long word, reg;
1029         unsigned int bit = offset & 31UL, res;
1030
1031         if (offset >= size)
1032                 return size;
1033
1034         if (bit) {
1035                 __asm__("   ic   %0,0(%1)\n"
1036                         "   icm  %0,2,1(%1)\n"
1037                         "   icm  %0,4,2(%1)\n"
1038                         "   icm  %0,8,3(%1)"
1039                         : "=&a" (word) : "a" (p) : "cc" );
1040                 word >>= bit;
1041                 res = bit;
1042                 /* Look for zero in first longword */
1043                 __asm__("   lhi  %2,0xff\n"
1044                         "   tml  %1,0xffff\n"
1045                         "   jno  0f\n"
1046                         "   ahi  %0,16\n"
1047                         "   srl  %1,16\n"
1048                         "0: tml  %1,0x00ff\n"
1049                         "   jno  1f\n"
1050                         "   ahi  %0,8\n"
1051                         "   srl  %1,8\n"
1052                         "1: nr   %1,%2\n"
1053                         "   ic   %1,0(%1,%3)\n"
1054                         "   alr  %0,%1"
1055                         : "+&d" (res), "+&a" (word), "=&d" (reg)
1056                         : "a" (&_zb_findmap) : "cc" );
1057                 if (res < 32)
1058                         return (p - addr)*32 + res;
1059                 p++;
1060         }
1061         /* No zero yet, search remaining full bytes for a zero */
1062         res = ext2_find_first_zero_bit (p, size - 32 * (p - addr));
1063         return (p - addr) * 32 + res;
1064 }
1065
1066 #else /* __s390x__ */
1067
1068 static inline unsigned long
1069 ext2_find_first_zero_bit(void *vaddr, unsigned long size)
1070 {
1071         unsigned long res, cmp, count;
1072
1073         if (!size)
1074                 return 0;
1075         __asm__("   lghi  %1,-1\n"
1076                 "   lgr   %2,%3\n"
1077                 "   aghi  %2,63\n"
1078                 "   srlg  %2,%2,6\n"
1079                 "   slgr  %0,%0\n"
1080                 "0: clg   %1,0(%0,%4)\n"
1081                 "   jne   1f\n"
1082                 "   aghi  %0,8\n"
1083                 "   brct  %2,0b\n"
1084                 "   lgr   %0,%3\n"
1085                 "   j     5f\n"
1086                 "1: cl    %1,0(%0,%4)\n"
1087                 "   jne   2f\n"
1088                 "   aghi  %0,4\n"
1089                 "2: l     %2,0(%0,%4)\n"
1090                 "   sllg  %0,%0,3\n"
1091                 "   aghi  %0,24\n"
1092                 "   lghi  %1,0xff\n"
1093                 "   tmlh  %2,0xffff\n"
1094                 "   jo    3f\n"
1095                 "   aghi  %0,-16\n"
1096                 "   srl   %2,16\n"
1097                 "3: tmll  %2,0xff00\n"
1098                 "   jo    4f\n"
1099                 "   aghi  %0,-8\n"
1100                 "   srl   %2,8\n"
1101                 "4: ngr   %2,%1\n"
1102                 "   ic    %2,0(%2,%5)\n"
1103                 "   algr  %0,%2\n"
1104                 "5:"
1105                 : "=&a" (res), "=&d" (cmp), "=&a" (count)
1106                 : "a" (size), "a" (vaddr), "a" (&_zb_findmap) : "cc" );
1107         return (res < size) ? res : size;
1108 }
1109
1110 static inline unsigned long
1111 ext2_find_next_zero_bit(void *vaddr, unsigned long size, unsigned long offset)
1112 {
1113         unsigned long *addr = vaddr;
1114         unsigned long *p = addr + (offset >> 6);
1115         unsigned long word, reg;
1116         unsigned long bit = offset & 63UL, res;
1117
1118         if (offset >= size)
1119                 return size;
1120
1121         if (bit) {
1122                 __asm__("   lrvg %0,%1" /* load reversed, neat instruction */
1123                         : "=a" (word) : "m" (*p) );
1124                 word >>= bit;
1125                 res = bit;
1126                 /* Look for zero in first 8 byte word */
1127                 __asm__("   lghi %2,0xff\n"
1128                         "   tmll %1,0xffff\n"
1129                         "   jno  2f\n"
1130                         "   ahi  %0,16\n"
1131                         "   srlg %1,%1,16\n"
1132                         "0: tmll %1,0xffff\n"
1133                         "   jno  2f\n"
1134                         "   ahi  %0,16\n"
1135                         "   srlg %1,%1,16\n"
1136                         "1: tmll %1,0xffff\n"
1137                         "   jno  2f\n"
1138                         "   ahi  %0,16\n"
1139                         "   srl  %1,16\n"
1140                         "2: tmll %1,0x00ff\n"
1141                         "   jno  3f\n"
1142                         "   ahi  %0,8\n"
1143                         "   srl  %1,8\n"
1144                         "3: ngr  %1,%2\n"
1145                         "   ic   %1,0(%1,%3)\n"
1146                         "   alr  %0,%1"
1147                         : "+&d" (res), "+a" (word), "=&d" (reg)
1148                         : "a" (&_zb_findmap) : "cc" );
1149                 if (res < 64)
1150                         return (p - addr)*64 + res;
1151                 p++;
1152         }
1153         /* No zero yet, search remaining full bytes for a zero */
1154         res = ext2_find_first_zero_bit (p, size - 64 * (p - addr));
1155         return (p - addr) * 64 + res;
1156 }
1157
1158 #endif /* __s390x__ */
1159
1160 /* Bitmap functions for the minix filesystem.  */
1161 /* FIXME !!! */
1162 #define minix_test_and_set_bit(nr,addr) \
1163         test_and_set_bit(nr,(unsigned long *)addr)
1164 #define minix_set_bit(nr,addr) \
1165         set_bit(nr,(unsigned long *)addr)
1166 #define minix_test_and_clear_bit(nr,addr) \
1167         test_and_clear_bit(nr,(unsigned long *)addr)
1168 #define minix_test_bit(nr,addr) \
1169         test_bit(nr,(unsigned long *)addr)
1170 #define minix_find_first_zero_bit(addr,size) \
1171         find_first_zero_bit(addr,size)
1172
1173 #endif /* __KERNEL__ */
1174
1175 #endif /* _S390_BITOPS_H */