Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 1999,2013 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 4 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, |
| 5 | * |
| 6 | * The description below was taken in large parts from the powerpc |
| 7 | * bitops header file: |
| 8 | * Within a word, bits are numbered LSB first. Lot's of places make |
| 9 | * this assumption by directly testing bits with (val & (1<<nr)). |
| 10 | * This can cause confusion for large (> 1 word) bitmaps on a |
| 11 | * big-endian system because, unlike little endian, the number of each |
| 12 | * bit depends on the word size. |
| 13 | * |
| 14 | * The bitop functions are defined to work on unsigned longs, so for an |
| 15 | * s390x system the bits end up numbered: |
Heiko Carstens | db85eae | 2014-02-12 12:43:40 +0100 | [diff] [blame] | 16 | * |63..............0|127............64|191...........128|255...........192| |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 17 | * and on s390: |
Heiko Carstens | db85eae | 2014-02-12 12:43:40 +0100 | [diff] [blame] | 18 | * |31.....0|63....32|95....64|127...96|159..128|191..160|223..192|255..224| |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 19 | * |
| 20 | * There are a few little-endian macros used mostly for filesystem |
| 21 | * bitmaps, these work on similar bit arrays layouts, but |
| 22 | * byte-oriented: |
| 23 | * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56| |
| 24 | * |
| 25 | * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit |
| 26 | * number field needs to be reversed compared to the big-endian bit |
| 27 | * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b). |
| 28 | * |
| 29 | * We also have special functions which work with an MSB0 encoding: |
| 30 | * on an s390x system the bits are numbered: |
| 31 | * |0..............63|64............127|128...........191|192...........255| |
| 32 | * and on s390: |
Heiko Carstens | db85eae | 2014-02-12 12:43:40 +0100 | [diff] [blame] | 33 | * |0.....31|32....63|64....95|96...127|128..159|160..191|192..223|224..255| |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 34 | * |
| 35 | * The main difference is that bit 0-63 (64b) or 0-31 (32b) in the bit |
| 36 | * number field needs to be reversed compared to the LSB0 encoded bit |
| 37 | * fields. This can be achieved by XOR with 0x3f (64b) or 0x1f (32b). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * |
| 39 | */ |
Heiko Carstens | c406abd | 2006-06-29 14:56:13 +0200 | [diff] [blame] | 40 | |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 41 | #ifndef _S390_BITOPS_H |
| 42 | #define _S390_BITOPS_H |
| 43 | |
Jiri Slaby | 0624517 | 2007-10-18 23:40:26 -0700 | [diff] [blame] | 44 | #ifndef _LINUX_BITOPS_H |
| 45 | #error only <linux/bitops.h> can be included directly |
| 46 | #endif |
| 47 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 48 | #include <linux/typecheck.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/compiler.h> |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 50 | #include <asm/barrier.h> |
| 51 | |
| 52 | #define __BITOPS_NO_BARRIER "\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 54 | #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES |
| 55 | |
| 56 | #define __BITOPS_OR "laog" |
| 57 | #define __BITOPS_AND "lang" |
| 58 | #define __BITOPS_XOR "laxg" |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 59 | #define __BITOPS_BARRIER "bcr 14,0\n" |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 60 | |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 61 | #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 62 | ({ \ |
| 63 | unsigned long __old; \ |
| 64 | \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 65 | typecheck(unsigned long *, (__addr)); \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 66 | asm volatile( \ |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 67 | __barrier \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 68 | __op_string " %0,%2,%1\n" \ |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 69 | __barrier \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 70 | : "=d" (__old), "+Q" (*(__addr)) \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 71 | : "d" (__val) \ |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 72 | : "cc", "memory"); \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 73 | __old; \ |
| 74 | }) |
| 75 | |
| 76 | #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #define __BITOPS_OR "ogr" |
| 79 | #define __BITOPS_AND "ngr" |
| 80 | #define __BITOPS_XOR "xgr" |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 81 | #define __BITOPS_BARRIER "\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 83 | #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 84 | ({ \ |
| 85 | unsigned long __old, __new; \ |
| 86 | \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 87 | typecheck(unsigned long *, (__addr)); \ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 88 | asm volatile( \ |
| 89 | " lg %0,%2\n" \ |
| 90 | "0: lgr %1,%0\n" \ |
| 91 | __op_string " %1,%3\n" \ |
| 92 | " csg %0,%1,%2\n" \ |
| 93 | " jl 0b" \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 94 | : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\ |
| 95 | : "d" (__val) \ |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 96 | : "cc", "memory"); \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 97 | __old; \ |
| 98 | }) |
| 99 | |
| 100 | #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 101 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 102 | #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 104 | static inline unsigned long * |
| 105 | __bitops_word(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 107 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 109 | addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3); |
| 110 | return (unsigned long *)addr; |
| 111 | } |
| 112 | |
| 113 | static inline unsigned char * |
| 114 | __bitops_byte(unsigned long nr, volatile unsigned long *ptr) |
| 115 | { |
| 116 | return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3); |
| 117 | } |
| 118 | |
| 119 | static inline void set_bit(unsigned long nr, volatile unsigned long *ptr) |
| 120 | { |
| 121 | unsigned long *addr = __bitops_word(nr, ptr); |
| 122 | unsigned long mask; |
| 123 | |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 124 | #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES |
| 125 | if (__builtin_constant_p(nr)) { |
| 126 | unsigned char *caddr = __bitops_byte(nr, ptr); |
| 127 | |
| 128 | asm volatile( |
| 129 | "oi %0,%b1\n" |
| 130 | : "+Q" (*caddr) |
| 131 | : "i" (1 << (nr & 7)) |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 132 | : "cc", "memory"); |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | #endif |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 136 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 137 | __BITOPS_LOOP(addr, mask, __BITOPS_OR, __BITOPS_NO_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 140 | static inline void clear_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 142 | unsigned long *addr = __bitops_word(nr, ptr); |
| 143 | unsigned long mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 145 | #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES |
| 146 | if (__builtin_constant_p(nr)) { |
| 147 | unsigned char *caddr = __bitops_byte(nr, ptr); |
| 148 | |
| 149 | asm volatile( |
| 150 | "ni %0,%b1\n" |
| 151 | : "+Q" (*caddr) |
| 152 | : "i" (~(1 << (nr & 7))) |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 153 | : "cc", "memory"); |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | #endif |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 157 | mask = ~(1UL << (nr & (BITS_PER_LONG - 1))); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 158 | __BITOPS_LOOP(addr, mask, __BITOPS_AND, __BITOPS_NO_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 161 | static inline void change_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 163 | unsigned long *addr = __bitops_word(nr, ptr); |
| 164 | unsigned long mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES |
| 167 | if (__builtin_constant_p(nr)) { |
| 168 | unsigned char *caddr = __bitops_byte(nr, ptr); |
| 169 | |
| 170 | asm volatile( |
| 171 | "xi %0,%b1\n" |
| 172 | : "+Q" (*caddr) |
| 173 | : "i" (1 << (nr & 7)) |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 174 | : "cc", "memory"); |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | #endif |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 178 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 179 | __BITOPS_LOOP(addr, mask, __BITOPS_XOR, __BITOPS_NO_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 183 | test_and_set_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 185 | unsigned long *addr = __bitops_word(nr, ptr); |
| 186 | unsigned long old, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 188 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 189 | old = __BITOPS_LOOP(addr, mask, __BITOPS_OR, __BITOPS_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return (old & mask) != 0; |
| 191 | } |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 194 | test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 196 | unsigned long *addr = __bitops_word(nr, ptr); |
| 197 | unsigned long old, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 199 | mask = ~(1UL << (nr & (BITS_PER_LONG - 1))); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 200 | old = __BITOPS_LOOP(addr, mask, __BITOPS_AND, __BITOPS_BARRIER); |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 201 | return (old & ~mask) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 205 | test_and_change_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 207 | unsigned long *addr = __bitops_word(nr, ptr); |
| 208 | unsigned long old, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 210 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 211 | old = __BITOPS_LOOP(addr, mask, __BITOPS_XOR, __BITOPS_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return (old & mask) != 0; |
| 213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr) |
| 216 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 217 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 219 | *addr |= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | __clear_bit(unsigned long nr, volatile unsigned long *ptr) |
| 224 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 225 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 227 | *addr &= ~(1 << (nr & 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr) |
| 231 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 232 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 234 | *addr ^= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 238 | __test_and_set_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 240 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | unsigned char ch; |
| 242 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 243 | ch = *addr; |
| 244 | *addr |= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return (ch >> (nr & 7)) & 1; |
| 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 249 | __test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 251 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | unsigned char ch; |
| 253 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 254 | ch = *addr; |
| 255 | *addr &= ~(1 << (nr & 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return (ch >> (nr & 7)) & 1; |
| 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 260 | __test_and_change_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 262 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | unsigned char ch; |
| 264 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 265 | ch = *addr; |
| 266 | *addr ^= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | return (ch >> (nr & 7)) & 1; |
| 268 | } |
| 269 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 270 | static inline int test_bit(unsigned long nr, const volatile unsigned long *ptr) |
| 271 | { |
| 272 | const volatile unsigned char *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 274 | addr = ((const volatile unsigned char *)ptr); |
| 275 | addr += (nr ^ (BITS_PER_LONG - 8)) >> 3; |
| 276 | return (*addr >> (nr & 7)) & 1; |
| 277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Martin Schwidefsky | afff7e2 | 2005-07-27 11:44:58 -0700 | [diff] [blame] | 279 | /* |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 280 | * Functions which use MSB0 bit numbering. |
| 281 | * On an s390x system the bits are numbered: |
| 282 | * |0..............63|64............127|128...........191|192...........255| |
| 283 | * and on s390: |
Heiko Carstens | db85eae | 2014-02-12 12:43:40 +0100 | [diff] [blame] | 284 | * |0.....31|32....63|64....95|96...127|128..159|160..191|192..223|224..255| |
Martin Schwidefsky | afff7e2 | 2005-07-27 11:44:58 -0700 | [diff] [blame] | 285 | */ |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 286 | unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size); |
| 287 | unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size, |
| 288 | unsigned long offset); |
| 289 | |
| 290 | static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 291 | { |
| 292 | return set_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 293 | } |
| 294 | |
| 295 | static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 296 | { |
| 297 | return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 298 | } |
| 299 | |
| 300 | static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 301 | { |
| 302 | return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 303 | } |
| 304 | |
| 305 | static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 306 | { |
| 307 | return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 308 | } |
| 309 | |
| 310 | static inline int test_bit_inv(unsigned long nr, |
| 311 | const volatile unsigned long *ptr) |
| 312 | { |
| 313 | return test_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 314 | } |
Martin Schwidefsky | afff7e2 | 2005-07-27 11:44:58 -0700 | [diff] [blame] | 315 | |
Heiko Carstens | b1cb7e2 | 2013-09-19 11:05:32 +0200 | [diff] [blame] | 316 | #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES |
| 317 | |
| 318 | /** |
| 319 | * __flogr - find leftmost one |
| 320 | * @word - The word to search |
| 321 | * |
| 322 | * Returns the bit number of the most significant bit set, |
| 323 | * where the most significant bit has bit number 0. |
| 324 | * If no bit is set this function returns 64. |
| 325 | */ |
| 326 | static inline unsigned char __flogr(unsigned long word) |
| 327 | { |
| 328 | if (__builtin_constant_p(word)) { |
| 329 | unsigned long bit = 0; |
| 330 | |
| 331 | if (!word) |
| 332 | return 64; |
| 333 | if (!(word & 0xffffffff00000000UL)) { |
| 334 | word <<= 32; |
| 335 | bit += 32; |
| 336 | } |
| 337 | if (!(word & 0xffff000000000000UL)) { |
| 338 | word <<= 16; |
| 339 | bit += 16; |
| 340 | } |
| 341 | if (!(word & 0xff00000000000000UL)) { |
| 342 | word <<= 8; |
| 343 | bit += 8; |
| 344 | } |
| 345 | if (!(word & 0xf000000000000000UL)) { |
| 346 | word <<= 4; |
| 347 | bit += 4; |
| 348 | } |
| 349 | if (!(word & 0xc000000000000000UL)) { |
| 350 | word <<= 2; |
| 351 | bit += 2; |
| 352 | } |
| 353 | if (!(word & 0x8000000000000000UL)) { |
| 354 | word <<= 1; |
| 355 | bit += 1; |
| 356 | } |
| 357 | return bit; |
| 358 | } else { |
| 359 | register unsigned long bit asm("4") = word; |
| 360 | register unsigned long out asm("5"); |
| 361 | |
| 362 | asm volatile( |
| 363 | " flogr %[bit],%[bit]\n" |
| 364 | : [bit] "+d" (bit), [out] "=d" (out) : : "cc"); |
| 365 | return bit; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * __ffs - find first bit in word. |
| 371 | * @word: The word to search |
| 372 | * |
| 373 | * Undefined if no bit exists, so code should check against 0 first. |
| 374 | */ |
| 375 | static inline unsigned long __ffs(unsigned long word) |
| 376 | { |
| 377 | return __flogr(-word & word) ^ (BITS_PER_LONG - 1); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * ffs - find first bit set |
| 382 | * @word: the word to search |
| 383 | * |
| 384 | * This is defined the same way as the libc and |
| 385 | * compiler builtin ffs routines (man ffs). |
| 386 | */ |
| 387 | static inline int ffs(int word) |
| 388 | { |
| 389 | unsigned long mask = 2 * BITS_PER_LONG - 1; |
| 390 | unsigned int val = (unsigned int)word; |
| 391 | |
| 392 | return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * __fls - find last (most-significant) set bit in a long word |
| 397 | * @word: the word to search |
| 398 | * |
| 399 | * Undefined if no set bit exists, so code should check against 0 first. |
| 400 | */ |
| 401 | static inline unsigned long __fls(unsigned long word) |
| 402 | { |
| 403 | return __flogr(word) ^ (BITS_PER_LONG - 1); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * fls64 - find last set bit in a 64-bit word |
| 408 | * @word: the word to search |
| 409 | * |
| 410 | * This is defined in a similar way as the libc and compiler builtin |
| 411 | * ffsll, but returns the position of the most significant set bit. |
| 412 | * |
| 413 | * fls64(value) returns 0 if value is 0 or the position of the last |
| 414 | * set bit if value is nonzero. The last (most significant) bit is |
| 415 | * at position 64. |
| 416 | */ |
| 417 | static inline int fls64(unsigned long word) |
| 418 | { |
| 419 | unsigned long mask = 2 * BITS_PER_LONG - 1; |
| 420 | |
| 421 | return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * fls - find last (most-significant) bit set |
| 426 | * @word: the word to search |
| 427 | * |
| 428 | * This is defined the same way as ffs. |
| 429 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 430 | */ |
| 431 | static inline int fls(int word) |
| 432 | { |
| 433 | return fls64((unsigned int)word); |
| 434 | } |
| 435 | |
| 436 | #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */ |
| 437 | |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 438 | #include <asm-generic/bitops/__ffs.h> |
| 439 | #include <asm-generic/bitops/ffs.h> |
Alexander van Heukelum | 56a6b1e | 2008-03-15 18:31:49 +0100 | [diff] [blame] | 440 | #include <asm-generic/bitops/__fls.h> |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 441 | #include <asm-generic/bitops/fls.h> |
Akinobu Mita | 7e33db4 | 2006-03-26 01:39:34 -0800 | [diff] [blame] | 442 | #include <asm-generic/bitops/fls64.h> |
Heiko Carstens | b1cb7e2 | 2013-09-19 11:05:32 +0200 | [diff] [blame] | 443 | |
| 444 | #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */ |
| 445 | |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 446 | #include <asm-generic/bitops/ffz.h> |
| 447 | #include <asm-generic/bitops/find.h> |
Akinobu Mita | 7e33db4 | 2006-03-26 01:39:34 -0800 | [diff] [blame] | 448 | #include <asm-generic/bitops/hweight.h> |
Nick Piggin | 2633357 | 2007-10-18 03:06:39 -0700 | [diff] [blame] | 449 | #include <asm-generic/bitops/lock.h> |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 450 | #include <asm-generic/bitops/sched.h> |
Akinobu Mita | 802caab | 2011-05-26 16:26:12 -0700 | [diff] [blame] | 451 | #include <asm-generic/bitops/le.h> |
Akinobu Mita | 148817b | 2011-07-26 16:09:04 -0700 | [diff] [blame] | 452 | #include <asm-generic/bitops/ext2-atomic-setbit.h> |
Akinobu Mita | 50b9b47 | 2011-03-23 16:41:57 -0700 | [diff] [blame] | 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | #endif /* _S390_BITOPS_H */ |