1#ifndef _S390_BITOPS_H
2#define _S390_BITOPS_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifdef __KERNEL__
17
18#ifndef _LINUX_BITOPS_H
19#error only <linux/bitops.h> can be included directly
20#endif
21
22#include <linux/compiler.h>
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61extern const char _oi_bitmap[];
62extern const char _ni_bitmap[];
63extern const char _zb_findmap[];
64extern const char _sb_findmap[];
65
66#ifndef __s390x__
67
68#define __BITOPS_ALIGN 3
69#define __BITOPS_WORDSIZE 32
70#define __BITOPS_OR "or"
71#define __BITOPS_AND "nr"
72#define __BITOPS_XOR "xr"
73
74#define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
75 asm volatile( \
76 " l %0,%2\n" \
77 "0: lr %1,%0\n" \
78 __op_string " %1,%3\n" \
79 " cs %0,%1,%2\n" \
80 " jl 0b" \
81 : "=&d" (__old), "=&d" (__new), \
82 "=Q" (*(unsigned long *) __addr) \
83 : "d" (__val), "Q" (*(unsigned long *) __addr) \
84 : "cc");
85
86#else
87
88#define __BITOPS_ALIGN 7
89#define __BITOPS_WORDSIZE 64
90#define __BITOPS_OR "ogr"
91#define __BITOPS_AND "ngr"
92#define __BITOPS_XOR "xgr"
93
94#define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
95 asm volatile( \
96 " lg %0,%2\n" \
97 "0: lgr %1,%0\n" \
98 __op_string " %1,%3\n" \
99 " csg %0,%1,%2\n" \
100 " jl 0b" \
101 : "=&d" (__old), "=&d" (__new), \
102 "=Q" (*(unsigned long *) __addr) \
103 : "d" (__val), "Q" (*(unsigned long *) __addr) \
104 : "cc");
105
106#endif
107
108#define __BITOPS_WORDS(bits) (((bits)+__BITOPS_WORDSIZE-1)/__BITOPS_WORDSIZE)
109#define __BITOPS_BARRIER() asm volatile("" : : : "memory")
110
111#ifdef CONFIG_SMP
112
113
114
115static inline void set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
116{
117 unsigned long addr, old, new, mask;
118
119 addr = (unsigned long) ptr;
120
121 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
122
123 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
124
125 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
126}
127
128
129
130
131static inline void clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
132{
133 unsigned long addr, old, new, mask;
134
135 addr = (unsigned long) ptr;
136
137 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
138
139 mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
140
141 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
142}
143
144
145
146
147static inline void change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
148{
149 unsigned long addr, old, new, mask;
150
151 addr = (unsigned long) ptr;
152
153 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
154
155 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
156
157 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
158}
159
160
161
162
163static inline int
164test_and_set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
165{
166 unsigned long addr, old, new, mask;
167
168 addr = (unsigned long) ptr;
169
170 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
171
172 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
173
174 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
175 __BITOPS_BARRIER();
176 return (old & mask) != 0;
177}
178
179
180
181
182static inline int
183test_and_clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
184{
185 unsigned long addr, old, new, mask;
186
187 addr = (unsigned long) ptr;
188
189 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
190
191 mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
192
193 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
194 __BITOPS_BARRIER();
195 return (old ^ new) != 0;
196}
197
198
199
200
201static inline int
202test_and_change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
203{
204 unsigned long addr, old, new, mask;
205
206 addr = (unsigned long) ptr;
207
208 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
209
210 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
211
212 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
213 __BITOPS_BARRIER();
214 return (old & mask) != 0;
215}
216#endif
217
218
219
220
221static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
222{
223 unsigned long addr;
224
225 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
226 asm volatile(
227 " oc %O0(1,%R0),%1"
228 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
229}
230
231static inline void
232__constant_set_bit(const unsigned long nr, volatile unsigned long *ptr)
233{
234 unsigned long addr;
235
236 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
237 *(unsigned char *) addr |= 1 << (nr & 7);
238}
239
240#define set_bit_simple(nr,addr) \
241(__builtin_constant_p((nr)) ? \
242 __constant_set_bit((nr),(addr)) : \
243 __set_bit((nr),(addr)) )
244
245
246
247
248static inline void
249__clear_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(
255 " nc %O0(1,%R0),%1"
256 : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7]) : "cc" );
257}
258
259static inline void
260__constant_clear_bit(const unsigned long nr, volatile unsigned long *ptr)
261{
262 unsigned long addr;
263
264 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
265 *(unsigned char *) addr &= ~(1 << (nr & 7));
266}
267
268#define clear_bit_simple(nr,addr) \
269(__builtin_constant_p((nr)) ? \
270 __constant_clear_bit((nr),(addr)) : \
271 __clear_bit((nr),(addr)) )
272
273
274
275
276static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
277{
278 unsigned long addr;
279
280 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
281 asm volatile(
282 " xc %O0(1,%R0),%1"
283 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
284}
285
286static inline void
287__constant_change_bit(const unsigned long nr, volatile unsigned long *ptr)
288{
289 unsigned long addr;
290
291 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
292 *(unsigned char *) addr ^= 1 << (nr & 7);
293}
294
295#define change_bit_simple(nr,addr) \
296(__builtin_constant_p((nr)) ? \
297 __constant_change_bit((nr),(addr)) : \
298 __change_bit((nr),(addr)) )
299
300
301
302
303static inline int
304test_and_set_bit_simple(unsigned long nr, volatile unsigned long *ptr)
305{
306 unsigned long addr;
307 unsigned char ch;
308
309 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
310 ch = *(unsigned char *) addr;
311 asm volatile(
312 " oc %O0(1,%R0),%1"
313 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
314 : "cc", "memory");
315 return (ch >> (nr & 7)) & 1;
316}
317#define __test_and_set_bit(X,Y) test_and_set_bit_simple(X,Y)
318
319
320
321
322static inline int
323test_and_clear_bit_simple(unsigned long nr, volatile unsigned long *ptr)
324{
325 unsigned long addr;
326 unsigned char ch;
327
328 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
329 ch = *(unsigned char *) addr;
330 asm volatile(
331 " nc %O0(1,%R0),%1"
332 : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7])
333 : "cc", "memory");
334 return (ch >> (nr & 7)) & 1;
335}
336#define __test_and_clear_bit(X,Y) test_and_clear_bit_simple(X,Y)
337
338
339
340
341static inline int
342test_and_change_bit_simple(unsigned long nr, volatile unsigned long *ptr)
343{
344 unsigned long addr;
345 unsigned char ch;
346
347 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
348 ch = *(unsigned char *) addr;
349 asm volatile(
350 " xc %O0(1,%R0),%1"
351 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
352 : "cc", "memory");
353 return (ch >> (nr & 7)) & 1;
354}
355#define __test_and_change_bit(X,Y) test_and_change_bit_simple(X,Y)
356
357#ifdef CONFIG_SMP
358#define set_bit set_bit_cs
359#define clear_bit clear_bit_cs
360#define change_bit change_bit_cs
361#define test_and_set_bit test_and_set_bit_cs
362#define test_and_clear_bit test_and_clear_bit_cs
363#define test_and_change_bit test_and_change_bit_cs
364#else
365#define set_bit set_bit_simple
366#define clear_bit clear_bit_simple
367#define change_bit change_bit_simple
368#define test_and_set_bit test_and_set_bit_simple
369#define test_and_clear_bit test_and_clear_bit_simple
370#define test_and_change_bit test_and_change_bit_simple
371#endif
372
373
374
375
376
377
378static inline int __test_bit(unsigned long nr, const volatile unsigned long *ptr)
379{
380 unsigned long addr;
381 unsigned char ch;
382
383 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
384 ch = *(volatile unsigned char *) addr;
385 return (ch >> (nr & 7)) & 1;
386}
387
388static inline int
389__constant_test_bit(unsigned long nr, const volatile unsigned long *addr) {
390 return (((volatile char *) addr)
391 [(nr^(__BITOPS_WORDSIZE-8))>>3] & (1<<(nr&7))) != 0;
392}
393
394#define test_bit(nr,addr) \
395(__builtin_constant_p((nr)) ? \
396 __constant_test_bit((nr),(addr)) : \
397 __test_bit((nr),(addr)) )
398
399
400
401
402
403
404
405
406
407
408static inline unsigned long __ffz_word_loop(const unsigned long *addr,
409 unsigned long size)
410{
411 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
412 unsigned long bytes = 0;
413
414 asm volatile(
415#ifndef __s390x__
416 " ahi %1,-1\n"
417 " sra %1,5\n"
418 " jz 1f\n"
419 "0: c %2,0(%0,%3)\n"
420 " jne 1f\n"
421 " la %0,4(%0)\n"
422 " brct %1,0b\n"
423 "1:\n"
424#else
425 " aghi %1,-1\n"
426 " srag %1,%1,6\n"
427 " jz 1f\n"
428 "0: cg %2,0(%0,%3)\n"
429 " jne 1f\n"
430 " la %0,8(%0)\n"
431 " brct %1,0b\n"
432 "1:\n"
433#endif
434 : "+&a" (bytes), "+&d" (size)
435 : "d" (-1UL), "a" (addr), "m" (*(addrtype *) addr)
436 : "cc" );
437 return bytes;
438}
439
440
441
442
443
444
445static inline unsigned long __ffs_word_loop(const unsigned long *addr,
446 unsigned long size)
447{
448 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
449 unsigned long bytes = 0;
450
451 asm volatile(
452#ifndef __s390x__
453 " ahi %1,-1\n"
454 " sra %1,5\n"
455 " jz 1f\n"
456 "0: c %2,0(%0,%3)\n"
457 " jne 1f\n"
458 " la %0,4(%0)\n"
459 " brct %1,0b\n"
460 "1:\n"
461#else
462 " aghi %1,-1\n"
463 " srag %1,%1,6\n"
464 " jz 1f\n"
465 "0: cg %2,0(%0,%3)\n"
466 " jne 1f\n"
467 " la %0,8(%0)\n"
468 " brct %1,0b\n"
469 "1:\n"
470#endif
471 : "+&a" (bytes), "+&a" (size)
472 : "d" (0UL), "a" (addr), "m" (*(addrtype *) addr)
473 : "cc" );
474 return bytes;
475}
476
477
478
479
480
481
482static inline unsigned long __ffz_word(unsigned long nr, unsigned long word)
483{
484#ifdef __s390x__
485 if ((word & 0xffffffff) == 0xffffffff) {
486 word >>= 32;
487 nr += 32;
488 }
489#endif
490 if ((word & 0xffff) == 0xffff) {
491 word >>= 16;
492 nr += 16;
493 }
494 if ((word & 0xff) == 0xff) {
495 word >>= 8;
496 nr += 8;
497 }
498 return nr + _zb_findmap[(unsigned char) word];
499}
500
501
502
503
504
505
506static inline unsigned long __ffs_word(unsigned long nr, unsigned long word)
507{
508#ifdef __s390x__
509 if ((word & 0xffffffff) == 0) {
510 word >>= 32;
511 nr += 32;
512 }
513#endif
514 if ((word & 0xffff) == 0) {
515 word >>= 16;
516 nr += 16;
517 }
518 if ((word & 0xff) == 0) {
519 word >>= 8;
520 nr += 8;
521 }
522 return nr + _sb_findmap[(unsigned char) word];
523}
524
525
526
527
528
529
530
531static inline unsigned long __load_ulong_be(const unsigned long *p,
532 unsigned long offset)
533{
534 p = (unsigned long *)((unsigned long) p + offset);
535 return *p;
536}
537
538
539
540
541
542
543static inline unsigned long __load_ulong_le(const unsigned long *p,
544 unsigned long offset)
545{
546 unsigned long word;
547
548 p = (unsigned long *)((unsigned long) p + offset);
549#ifndef __s390x__
550 asm volatile(
551 " ic %0,%O1(%R1)\n"
552 " icm %0,2,%O1+1(%R1)\n"
553 " icm %0,4,%O1+2(%R1)\n"
554 " icm %0,8,%O1+3(%R1)"
555 : "=&d" (word) : "Q" (*p) : "cc");
556#else
557 asm volatile(
558 " lrvg %0,%1"
559 : "=d" (word) : "m" (*p) );
560#endif
561 return word;
562}
563
564
565
566
567
568
569
570
571
572
573
574static inline unsigned long ffz(unsigned long word)
575{
576 return __ffz_word(0, word);
577}
578
579
580
581
582
583
584
585static inline unsigned long __ffs (unsigned long word)
586{
587 return __ffs_word(0, word);
588}
589
590
591
592
593
594
595
596
597
598static inline int ffs(int x)
599{
600 if (!x)
601 return 0;
602 return __ffs_word(1, x);
603}
604
605
606
607
608
609
610
611
612
613static inline unsigned long find_first_zero_bit(const unsigned long *addr,
614 unsigned long size)
615{
616 unsigned long bytes, bits;
617
618 if (!size)
619 return 0;
620 bytes = __ffz_word_loop(addr, size);
621 bits = __ffz_word(bytes*8, __load_ulong_be(addr, bytes));
622 return (bits < size) ? bits : size;
623}
624
625
626
627
628
629
630
631
632
633static inline unsigned long find_first_bit(const unsigned long * addr,
634 unsigned long size)
635{
636 unsigned long bytes, bits;
637
638 if (!size)
639 return 0;
640 bytes = __ffs_word_loop(addr, size);
641 bits = __ffs_word(bytes*8, __load_ulong_be(addr, bytes));
642 return (bits < size) ? bits : size;
643}
644
645
646
647
648
649
650
651static inline int find_next_zero_bit (const unsigned long * addr,
652 unsigned long size,
653 unsigned long offset)
654{
655 const unsigned long *p;
656 unsigned long bit, set;
657
658 if (offset >= size)
659 return size;
660 bit = offset & (__BITOPS_WORDSIZE - 1);
661 offset -= bit;
662 size -= offset;
663 p = addr + offset / __BITOPS_WORDSIZE;
664 if (bit) {
665
666
667
668
669 set = __ffz_word(bit, *p >> bit);
670 if (set >= size)
671 return size + offset;
672 if (set < __BITOPS_WORDSIZE)
673 return set + offset;
674 offset += __BITOPS_WORDSIZE;
675 size -= __BITOPS_WORDSIZE;
676 p++;
677 }
678 return offset + find_first_zero_bit(p, size);
679}
680
681
682
683
684
685
686
687static inline int find_next_bit (const unsigned long * addr,
688 unsigned long size,
689 unsigned long offset)
690{
691 const unsigned long *p;
692 unsigned long bit, set;
693
694 if (offset >= size)
695 return size;
696 bit = offset & (__BITOPS_WORDSIZE - 1);
697 offset -= bit;
698 size -= offset;
699 p = addr + offset / __BITOPS_WORDSIZE;
700 if (bit) {
701
702
703
704
705 set = __ffs_word(0, *p & (~0UL << bit));
706 if (set >= size)
707 return size + offset;
708 if (set < __BITOPS_WORDSIZE)
709 return set + offset;
710 offset += __BITOPS_WORDSIZE;
711 size -= __BITOPS_WORDSIZE;
712 p++;
713 }
714 return offset + find_first_bit(p, size);
715}
716
717
718
719
720
721
722
723static inline int sched_find_first_bit(unsigned long *b)
724{
725 return find_first_bit(b, 140);
726}
727
728#include <asm-generic/bitops/fls.h>
729#include <asm-generic/bitops/__fls.h>
730#include <asm-generic/bitops/fls64.h>
731
732#include <asm-generic/bitops/hweight.h>
733#include <asm-generic/bitops/lock.h>
734
735
736
737
738
739
740
741
742
743
744
745static inline void __set_bit_le(unsigned long nr, void *addr)
746{
747 __set_bit(nr ^ (__BITOPS_WORDSIZE - 8), addr);
748}
749
750static inline void __clear_bit_le(unsigned long nr, void *addr)
751{
752 __clear_bit(nr ^ (__BITOPS_WORDSIZE - 8), addr);
753}
754
755static inline int __test_and_set_bit_le(unsigned long nr, void *addr)
756{
757 return __test_and_set_bit(nr ^ (__BITOPS_WORDSIZE - 8), addr);
758}
759
760static inline int test_and_set_bit_le(unsigned long nr, void *addr)
761{
762 return test_and_set_bit(nr ^ (__BITOPS_WORDSIZE - 8), addr);
763}
764
765static inline int __test_and_clear_bit_le(unsigned long nr, void *addr)
766{
767 return __test_and_clear_bit(nr ^ (__BITOPS_WORDSIZE - 8), addr);
768}
769
770static inline int test_and_clear_bit_le(unsigned long nr, void *addr)
771{
772 return test_and_clear_bit(nr ^ (__BITOPS_WORDSIZE - 8), addr);
773}
774
775static inline int test_bit_le(unsigned long nr, const void *addr)
776{
777 return test_bit(nr ^ (__BITOPS_WORDSIZE - 8), addr);
778}
779
780static inline int find_first_zero_bit_le(void *vaddr, unsigned int size)
781{
782 unsigned long bytes, bits;
783
784 if (!size)
785 return 0;
786 bytes = __ffz_word_loop(vaddr, size);
787 bits = __ffz_word(bytes*8, __load_ulong_le(vaddr, bytes));
788 return (bits < size) ? bits : size;
789}
790
791static inline int find_next_zero_bit_le(void *vaddr, unsigned long size,
792 unsigned long offset)
793{
794 unsigned long *addr = vaddr, *p;
795 unsigned long bit, set;
796
797 if (offset >= size)
798 return size;
799 bit = offset & (__BITOPS_WORDSIZE - 1);
800 offset -= bit;
801 size -= offset;
802 p = addr + offset / __BITOPS_WORDSIZE;
803 if (bit) {
804
805
806
807
808 set = __ffz_word(bit, __load_ulong_le(p, 0) >> bit);
809 if (set >= size)
810 return size + offset;
811 if (set < __BITOPS_WORDSIZE)
812 return set + offset;
813 offset += __BITOPS_WORDSIZE;
814 size -= __BITOPS_WORDSIZE;
815 p++;
816 }
817 return offset + find_first_zero_bit_le(p, size);
818}
819
820static inline unsigned long find_first_bit_le(void *vaddr, unsigned long size)
821{
822 unsigned long bytes, bits;
823
824 if (!size)
825 return 0;
826 bytes = __ffs_word_loop(vaddr, size);
827 bits = __ffs_word(bytes*8, __load_ulong_le(vaddr, bytes));
828 return (bits < size) ? bits : size;
829}
830
831static inline int find_next_bit_le(void *vaddr, unsigned long size,
832 unsigned long offset)
833{
834 unsigned long *addr = vaddr, *p;
835 unsigned long bit, set;
836
837 if (offset >= size)
838 return size;
839 bit = offset & (__BITOPS_WORDSIZE - 1);
840 offset -= bit;
841 size -= offset;
842 p = addr + offset / __BITOPS_WORDSIZE;
843 if (bit) {
844
845
846
847
848 set = __ffs_word(0, __load_ulong_le(p, 0) & (~0UL << bit));
849 if (set >= size)
850 return size + offset;
851 if (set < __BITOPS_WORDSIZE)
852 return set + offset;
853 offset += __BITOPS_WORDSIZE;
854 size -= __BITOPS_WORDSIZE;
855 p++;
856 }
857 return offset + find_first_bit_le(p, size);
858}
859
860#define ext2_set_bit_atomic(lock, nr, addr) \
861 test_and_set_bit_le(nr, addr)
862#define ext2_clear_bit_atomic(lock, nr, addr) \
863 test_and_clear_bit_le(nr, addr)
864
865
866#endif
867
868#endif
869