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