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