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