1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc.
| 1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc.
|
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
| 5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
|
6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29#ifndef _LINUX_BITOPS_H_ 30#define _LINUX_BITOPS_H_ 31
| 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29#ifndef _LINUX_BITOPS_H_ 30#define _LINUX_BITOPS_H_ 31
|
| 32#include <sys/types.h> 33#include <sys/systm.h> 34 35#define BIT(nr) (1UL << (nr))
|
32#ifdef __LP64__ 33#define BITS_PER_LONG 64 34#else 35#define BITS_PER_LONG 32 36#endif
| 36#ifdef __LP64__ 37#define BITS_PER_LONG 64 38#else 39#define BITS_PER_LONG 32 40#endif
|
37#define BIT_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
| 41#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG)) 42#define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
|
38#define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
| 43#define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
|
| 44#define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
|
39#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
| 45#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
|
40
| 46#define GENMASK(lo, hi) (((2UL << ((hi) - (lo))) - 1UL) << (lo))
|
41#define BITS_PER_BYTE 8 42 43static inline int 44__ffs(int mask) 45{ 46 return (ffs(mask) - 1); 47} 48 49static inline int 50__fls(int mask) 51{ 52 return (fls(mask) - 1); 53} 54 55static inline int 56__ffsl(long mask) 57{ 58 return (ffsl(mask) - 1); 59} 60 61static inline int 62__flsl(long mask) 63{ 64 return (flsl(mask) - 1); 65} 66 67 68#define ffz(mask) __ffs(~(mask)) 69 70static inline int get_count_order(unsigned int count) 71{ 72 int order; 73 74 order = fls(count) - 1; 75 if (count & (count - 1)) 76 order++; 77 return order; 78} 79 80static inline unsigned long 81find_first_bit(unsigned long *addr, unsigned long size) 82{ 83 long mask; 84 int bit; 85 86 for (bit = 0; size >= BITS_PER_LONG; 87 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 88 if (*addr == 0) 89 continue; 90 return (bit + __ffsl(*addr)); 91 } 92 if (size) {
| 47#define BITS_PER_BYTE 8 48 49static inline int 50__ffs(int mask) 51{ 52 return (ffs(mask) - 1); 53} 54 55static inline int 56__fls(int mask) 57{ 58 return (fls(mask) - 1); 59} 60 61static inline int 62__ffsl(long mask) 63{ 64 return (ffsl(mask) - 1); 65} 66 67static inline int 68__flsl(long mask) 69{ 70 return (flsl(mask) - 1); 71} 72 73 74#define ffz(mask) __ffs(~(mask)) 75 76static inline int get_count_order(unsigned int count) 77{ 78 int order; 79 80 order = fls(count) - 1; 81 if (count & (count - 1)) 82 order++; 83 return order; 84} 85 86static inline unsigned long 87find_first_bit(unsigned long *addr, unsigned long size) 88{ 89 long mask; 90 int bit; 91 92 for (bit = 0; size >= BITS_PER_LONG; 93 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 94 if (*addr == 0) 95 continue; 96 return (bit + __ffsl(*addr)); 97 } 98 if (size) {
|
93 mask = (*addr) & BIT_MASK(size);
| 99 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
|
94 if (mask) 95 bit += __ffsl(mask); 96 else 97 bit += size; 98 } 99 return (bit); 100} 101 102static inline unsigned long 103find_first_zero_bit(unsigned long *addr, unsigned long size) 104{ 105 long mask; 106 int bit; 107 108 for (bit = 0; size >= BITS_PER_LONG; 109 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 110 if (~(*addr) == 0) 111 continue; 112 return (bit + __ffsl(~(*addr))); 113 } 114 if (size) {
| 100 if (mask) 101 bit += __ffsl(mask); 102 else 103 bit += size; 104 } 105 return (bit); 106} 107 108static inline unsigned long 109find_first_zero_bit(unsigned long *addr, unsigned long size) 110{ 111 long mask; 112 int bit; 113 114 for (bit = 0; size >= BITS_PER_LONG; 115 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 116 if (~(*addr) == 0) 117 continue; 118 return (bit + __ffsl(~(*addr))); 119 } 120 if (size) {
|
115 mask = ~(*addr) & BIT_MASK(size);
| 121 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
|
116 if (mask) 117 bit += __ffsl(mask); 118 else 119 bit += size; 120 } 121 return (bit); 122} 123 124static inline unsigned long 125find_last_bit(unsigned long *addr, unsigned long size) 126{ 127 long mask; 128 int offs; 129 int bit; 130 int pos; 131 132 pos = size / BITS_PER_LONG; 133 offs = size % BITS_PER_LONG; 134 bit = BITS_PER_LONG * pos; 135 addr += pos; 136 if (offs) {
| 122 if (mask) 123 bit += __ffsl(mask); 124 else 125 bit += size; 126 } 127 return (bit); 128} 129 130static inline unsigned long 131find_last_bit(unsigned long *addr, unsigned long size) 132{ 133 long mask; 134 int offs; 135 int bit; 136 int pos; 137 138 pos = size / BITS_PER_LONG; 139 offs = size % BITS_PER_LONG; 140 bit = BITS_PER_LONG * pos; 141 addr += pos; 142 if (offs) {
|
137 mask = (*addr) & BIT_MASK(offs);
| 143 mask = (*addr) & BITMAP_LAST_WORD_MASK(offs);
|
138 if (mask) 139 return (bit + __flsl(mask)); 140 } 141 while (--pos) { 142 addr--; 143 bit -= BITS_PER_LONG; 144 if (*addr) 145 return (bit + __flsl(mask)); 146 } 147 return (size); 148} 149 150static inline unsigned long 151find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset) 152{ 153 long mask; 154 int offs; 155 int bit; 156 int pos; 157 158 if (offset >= size) 159 return (size); 160 pos = offset / BITS_PER_LONG; 161 offs = offset % BITS_PER_LONG; 162 bit = BITS_PER_LONG * pos; 163 addr += pos; 164 if (offs) {
| 144 if (mask) 145 return (bit + __flsl(mask)); 146 } 147 while (--pos) { 148 addr--; 149 bit -= BITS_PER_LONG; 150 if (*addr) 151 return (bit + __flsl(mask)); 152 } 153 return (size); 154} 155 156static inline unsigned long 157find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset) 158{ 159 long mask; 160 int offs; 161 int bit; 162 int pos; 163 164 if (offset >= size) 165 return (size); 166 pos = offset / BITS_PER_LONG; 167 offs = offset % BITS_PER_LONG; 168 bit = BITS_PER_LONG * pos; 169 addr += pos; 170 if (offs) {
|
165 mask = (*addr) & ~BIT_MASK(offs);
| 171 mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs);
|
166 if (mask) 167 return (bit + __ffsl(mask)); 168 if (size - bit <= BITS_PER_LONG) 169 return (size); 170 bit += BITS_PER_LONG; 171 addr++; 172 } 173 for (size -= bit; size >= BITS_PER_LONG; 174 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 175 if (*addr == 0) 176 continue; 177 return (bit + __ffsl(*addr)); 178 } 179 if (size) {
| 172 if (mask) 173 return (bit + __ffsl(mask)); 174 if (size - bit <= BITS_PER_LONG) 175 return (size); 176 bit += BITS_PER_LONG; 177 addr++; 178 } 179 for (size -= bit; size >= BITS_PER_LONG; 180 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 181 if (*addr == 0) 182 continue; 183 return (bit + __ffsl(*addr)); 184 } 185 if (size) {
|
180 mask = (*addr) & BIT_MASK(size);
| 186 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
|
181 if (mask) 182 bit += __ffsl(mask); 183 else 184 bit += size; 185 } 186 return (bit); 187} 188 189static inline unsigned long 190find_next_zero_bit(unsigned long *addr, unsigned long size, 191 unsigned long offset) 192{ 193 long mask; 194 int offs; 195 int bit; 196 int pos; 197 198 if (offset >= size) 199 return (size); 200 pos = offset / BITS_PER_LONG; 201 offs = offset % BITS_PER_LONG; 202 bit = BITS_PER_LONG * pos; 203 addr += pos; 204 if (offs) {
| 187 if (mask) 188 bit += __ffsl(mask); 189 else 190 bit += size; 191 } 192 return (bit); 193} 194 195static inline unsigned long 196find_next_zero_bit(unsigned long *addr, unsigned long size, 197 unsigned long offset) 198{ 199 long mask; 200 int offs; 201 int bit; 202 int pos; 203 204 if (offset >= size) 205 return (size); 206 pos = offset / BITS_PER_LONG; 207 offs = offset % BITS_PER_LONG; 208 bit = BITS_PER_LONG * pos; 209 addr += pos; 210 if (offs) {
|
205 mask = ~(*addr) & ~BIT_MASK(offs);
| 211 mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs);
|
206 if (mask) 207 return (bit + __ffsl(mask)); 208 if (size - bit <= BITS_PER_LONG) 209 return (size); 210 bit += BITS_PER_LONG; 211 addr++; 212 } 213 for (size -= bit; size >= BITS_PER_LONG; 214 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 215 if (~(*addr) == 0) 216 continue; 217 return (bit + __ffsl(~(*addr))); 218 } 219 if (size) {
| 212 if (mask) 213 return (bit + __ffsl(mask)); 214 if (size - bit <= BITS_PER_LONG) 215 return (size); 216 bit += BITS_PER_LONG; 217 addr++; 218 } 219 for (size -= bit; size >= BITS_PER_LONG; 220 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) { 221 if (~(*addr) == 0) 222 continue; 223 return (bit + __ffsl(~(*addr))); 224 } 225 if (size) {
|
220 mask = ~(*addr) & BIT_MASK(size);
| 226 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
|
221 if (mask) 222 bit += __ffsl(mask); 223 else 224 bit += size; 225 } 226 return (bit); 227} 228 229static inline void 230bitmap_zero(unsigned long *addr, int size) 231{ 232 int len; 233 234 len = BITS_TO_LONGS(size) * sizeof(long); 235 memset(addr, 0, len); 236} 237 238static inline void 239bitmap_fill(unsigned long *addr, int size) 240{ 241 int tail; 242 int len; 243 244 len = (size / BITS_PER_LONG) * sizeof(long); 245 memset(addr, 0xff, len); 246 tail = size & (BITS_PER_LONG - 1); 247 if (tail)
| 227 if (mask) 228 bit += __ffsl(mask); 229 else 230 bit += size; 231 } 232 return (bit); 233} 234 235static inline void 236bitmap_zero(unsigned long *addr, int size) 237{ 238 int len; 239 240 len = BITS_TO_LONGS(size) * sizeof(long); 241 memset(addr, 0, len); 242} 243 244static inline void 245bitmap_fill(unsigned long *addr, int size) 246{ 247 int tail; 248 int len; 249 250 len = (size / BITS_PER_LONG) * sizeof(long); 251 memset(addr, 0xff, len); 252 tail = size & (BITS_PER_LONG - 1); 253 if (tail)
|
248 addr[size / BITS_PER_LONG] = BIT_MASK(tail);
| 254 addr[size / BITS_PER_LONG] = BITMAP_LAST_WORD_MASK(tail);
|
249} 250 251static inline int 252bitmap_full(unsigned long *addr, int size) 253{
| 255} 256 257static inline int 258bitmap_full(unsigned long *addr, int size) 259{
|
254 long mask;
| 260 unsigned long mask;
|
255 int tail; 256 int len; 257 int i; 258 259 len = size / BITS_PER_LONG; 260 for (i = 0; i < len; i++) 261 if (addr[i] != ~0UL) 262 return (0); 263 tail = size & (BITS_PER_LONG - 1); 264 if (tail) {
| 261 int tail; 262 int len; 263 int i; 264 265 len = size / BITS_PER_LONG; 266 for (i = 0; i < len; i++) 267 if (addr[i] != ~0UL) 268 return (0); 269 tail = size & (BITS_PER_LONG - 1); 270 if (tail) {
|
265 mask = BIT_MASK(tail);
| 271 mask = BITMAP_LAST_WORD_MASK(tail);
|
266 if ((addr[i] & mask) != mask) 267 return (0); 268 } 269 return (1); 270} 271 272static inline int 273bitmap_empty(unsigned long *addr, int size) 274{
| 272 if ((addr[i] & mask) != mask) 273 return (0); 274 } 275 return (1); 276} 277 278static inline int 279bitmap_empty(unsigned long *addr, int size) 280{
|
275 long mask;
| 281 unsigned long mask;
|
276 int tail; 277 int len; 278 int i; 279 280 len = size / BITS_PER_LONG; 281 for (i = 0; i < len; i++) 282 if (addr[i] != 0) 283 return (0); 284 tail = size & (BITS_PER_LONG - 1); 285 if (tail) {
| 282 int tail; 283 int len; 284 int i; 285 286 len = size / BITS_PER_LONG; 287 for (i = 0; i < len; i++) 288 if (addr[i] != 0) 289 return (0); 290 tail = size & (BITS_PER_LONG - 1); 291 if (tail) {
|
286 mask = BIT_MASK(tail);
| 292 mask = BITMAP_LAST_WORD_MASK(tail);
|
287 if ((addr[i] & mask) != 0) 288 return (0); 289 } 290 return (1); 291} 292
| 293 if ((addr[i] & mask) != 0) 294 return (0); 295 } 296 return (1); 297} 298
|
293#define NBLONG (NBBY * sizeof(long)) 294
| |
295#define __set_bit(i, a) \
| 299#define __set_bit(i, a) \
|
296 atomic_set_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
| 300 atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
|
297 298#define set_bit(i, a) \
| 301 302#define set_bit(i, a) \
|
299 atomic_set_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
| 303 atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
|
300 301#define __clear_bit(i, a) \
| 304 305#define __clear_bit(i, a) \
|
302 atomic_clear_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
| 306 atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
|
303 304#define clear_bit(i, a) \
| 307 308#define clear_bit(i, a) \
|
305 atomic_clear_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
| 309 atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
|
306 307#define test_bit(i, a) \
| 310 311#define test_bit(i, a) \
|
308 !!(atomic_load_acq_long(&((volatile long *)(a))[(i)/NBLONG]) & \ 309 (1UL << ((i) % NBLONG)))
| 312 !!(atomic_load_acq_long(&((volatile long *)(a))[BIT_WORD(i)]) & \ 313 BIT_MASK(i))
|
310 311static inline long 312test_and_clear_bit(long bit, long *var) 313{ 314 long val; 315
| 314 315static inline long 316test_and_clear_bit(long bit, long *var) 317{ 318 long val; 319
|
316 var += bit / (sizeof(long) * NBBY); 317 bit %= sizeof(long) * NBBY;
| 320 var += BIT_WORD(bit); 321 bit %= BITS_PER_LONG;
|
318 bit = (1UL << bit); 319 do { 320 val = *(volatile long *)var; 321 } while (atomic_cmpset_long(var, val, val & ~bit) == 0); 322 323 return !!(val & bit); 324} 325 326static inline long 327test_and_set_bit(long bit, long *var) 328{ 329 long val; 330
| 322 bit = (1UL << bit); 323 do { 324 val = *(volatile long *)var; 325 } while (atomic_cmpset_long(var, val, val & ~bit) == 0); 326 327 return !!(val & bit); 328} 329 330static inline long 331test_and_set_bit(long bit, long *var) 332{ 333 long val; 334
|
331 var += bit / (sizeof(long) * NBBY); 332 bit %= sizeof(long) * NBBY;
| 335 var += BIT_WORD(bit); 336 bit %= BITS_PER_LONG;
|
333 bit = (1UL << bit); 334 do { 335 val = *(volatile long *)var; 336 } while (atomic_cmpset_long(var, val, val | bit) == 0); 337 338 return !!(val & bit); 339} 340
| 337 bit = (1UL << bit); 338 do { 339 val = *(volatile long *)var; 340 } while (atomic_cmpset_long(var, val, val | bit) == 0); 341 342 return !!(val & bit); 343} 344
|
341 342#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG)) 343#define BITMAP_LAST_WORD_MASK(nbits) \ 344( \ 345 ((nbits) % BITS_PER_LONG) ? \ 346 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \ 347) 348 349
| |
350static inline void 351bitmap_set(unsigned long *map, int start, int nr) 352{ 353 unsigned long *p = map + BIT_WORD(start); 354 const int size = start + nr; 355 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); 356 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); 357 358 while (nr - bits_to_set >= 0) { 359 *p |= mask_to_set; 360 nr -= bits_to_set; 361 bits_to_set = BITS_PER_LONG; 362 mask_to_set = ~0UL; 363 p++; 364 } 365 if (nr) { 366 mask_to_set &= BITMAP_LAST_WORD_MASK(size); 367 *p |= mask_to_set; 368 } 369} 370 371static inline void 372bitmap_clear(unsigned long *map, int start, int nr) 373{ 374 unsigned long *p = map + BIT_WORD(start); 375 const int size = start + nr; 376 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); 377 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); 378 379 while (nr - bits_to_clear >= 0) { 380 *p &= ~mask_to_clear; 381 nr -= bits_to_clear; 382 bits_to_clear = BITS_PER_LONG; 383 mask_to_clear = ~0UL; 384 p++; 385 } 386 if (nr) { 387 mask_to_clear &= BITMAP_LAST_WORD_MASK(size); 388 *p &= ~mask_to_clear; 389 } 390} 391 392enum {
| 345static inline void 346bitmap_set(unsigned long *map, int start, int nr) 347{ 348 unsigned long *p = map + BIT_WORD(start); 349 const int size = start + nr; 350 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); 351 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); 352 353 while (nr - bits_to_set >= 0) { 354 *p |= mask_to_set; 355 nr -= bits_to_set; 356 bits_to_set = BITS_PER_LONG; 357 mask_to_set = ~0UL; 358 p++; 359 } 360 if (nr) { 361 mask_to_set &= BITMAP_LAST_WORD_MASK(size); 362 *p |= mask_to_set; 363 } 364} 365 366static inline void 367bitmap_clear(unsigned long *map, int start, int nr) 368{ 369 unsigned long *p = map + BIT_WORD(start); 370 const int size = start + nr; 371 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); 372 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); 373 374 while (nr - bits_to_clear >= 0) { 375 *p &= ~mask_to_clear; 376 nr -= bits_to_clear; 377 bits_to_clear = BITS_PER_LONG; 378 mask_to_clear = ~0UL; 379 p++; 380 } 381 if (nr) { 382 mask_to_clear &= BITMAP_LAST_WORD_MASK(size); 383 *p &= ~mask_to_clear; 384 } 385} 386 387enum {
|
393 REG_OP_ISFREE, /* true if region is all zero bits */ 394 REG_OP_ALLOC, /* set all bits in region */ 395 REG_OP_RELEASE, /* clear all bits in region */
| 388 REG_OP_ISFREE, 389 REG_OP_ALLOC, 390 REG_OP_RELEASE,
|
396}; 397 398static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op) 399{
| 391}; 392 393static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op) 394{
|
400 int nbits_reg; /* number of bits in region */ 401 int index; /* index first long of region in bitmap */ 402 int offset; /* bit offset region in bitmap[index] */ 403 int nlongs_reg; /* num longs spanned by region in bitmap */ 404 int nbitsinlong; /* num bits of region in each spanned long */ 405 unsigned long mask; /* bitmask for one long of region */ 406 int i; /* scans bitmap by longs */ 407 int ret = 0; /* return value */
| 395 int nbits_reg; 396 int index; 397 int offset; 398 int nlongs_reg; 399 int nbitsinlong; 400 unsigned long mask; 401 int i; 402 int ret = 0;
|
408
| 403
|
409 /* 410 * Either nlongs_reg == 1 (for small orders that fit in one long) 411 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.) 412 */
| |
413 nbits_reg = 1 << order; 414 index = pos / BITS_PER_LONG; 415 offset = pos - (index * BITS_PER_LONG); 416 nlongs_reg = BITS_TO_LONGS(nbits_reg); 417 nbitsinlong = min(nbits_reg, BITS_PER_LONG); 418
| 404 nbits_reg = 1 << order; 405 index = pos / BITS_PER_LONG; 406 offset = pos - (index * BITS_PER_LONG); 407 nlongs_reg = BITS_TO_LONGS(nbits_reg); 408 nbitsinlong = min(nbits_reg, BITS_PER_LONG); 409
|
419 /* 420 * Can't do "mask = (1UL << nbitsinlong) - 1", as that 421 * overflows if nbitsinlong == BITS_PER_LONG. 422 */
| |
423 mask = (1UL << (nbitsinlong - 1)); 424 mask += mask - 1; 425 mask <<= offset; 426 427 switch (reg_op) { 428 case REG_OP_ISFREE: 429 for (i = 0; i < nlongs_reg; i++) { 430 if (bitmap[index + i] & mask) 431 goto done; 432 }
| 410 mask = (1UL << (nbitsinlong - 1)); 411 mask += mask - 1; 412 mask <<= offset; 413 414 switch (reg_op) { 415 case REG_OP_ISFREE: 416 for (i = 0; i < nlongs_reg; i++) { 417 if (bitmap[index + i] & mask) 418 goto done; 419 }
|
433 ret = 1; /* all bits in region free (zero) */
| 420 ret = 1;
|
434 break; 435 436 case REG_OP_ALLOC: 437 for (i = 0; i < nlongs_reg; i++) 438 bitmap[index + i] |= mask; 439 break; 440 441 case REG_OP_RELEASE: 442 for (i = 0; i < nlongs_reg; i++) 443 bitmap[index + i] &= ~mask; 444 break; 445 } 446done: 447 return ret; 448} 449
| 421 break; 422 423 case REG_OP_ALLOC: 424 for (i = 0; i < nlongs_reg; i++) 425 bitmap[index + i] |= mask; 426 break; 427 428 case REG_OP_RELEASE: 429 for (i = 0; i < nlongs_reg; i++) 430 bitmap[index + i] &= ~mask; 431 break; 432 } 433done: 434 return ret; 435} 436
|
450/** 451 * bitmap_find_free_region - find a contiguous aligned mem region 452 * @bitmap: array of unsigned longs corresponding to the bitmap 453 * @bits: number of bits in the bitmap 454 * @order: region size (log base 2 of number of bits) to find 455 * 456 * Find a region of free (zero) bits in a @bitmap of @bits bits and 457 * allocate them (set them to one). Only consider regions of length 458 * a power (@order) of two, aligned to that power of two, which 459 * makes the search algorithm much faster. 460 * 461 * Return the bit offset in bitmap of the allocated region, 462 * or -errno on failure. 463 */
| |
464static inline int 465bitmap_find_free_region(unsigned long *bitmap, int bits, int order) 466{
| 437static inline int 438bitmap_find_free_region(unsigned long *bitmap, int bits, int order) 439{
|
467 int pos, end; /* scans bitmap by regions of size order */
| 440 int pos; 441 int end;
|
468 469 for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) { 470 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) 471 continue; 472 __reg_op(bitmap, pos, order, REG_OP_ALLOC); 473 return pos; 474 } 475 return -ENOMEM; 476} 477
| 442 443 for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) { 444 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) 445 continue; 446 __reg_op(bitmap, pos, order, REG_OP_ALLOC); 447 return pos; 448 } 449 return -ENOMEM; 450} 451
|
478/** 479 * bitmap_allocate_region - allocate bitmap region 480 * @bitmap: array of unsigned longs corresponding to the bitmap 481 * @pos: beginning of bit region to allocate 482 * @order: region size (log base 2 of number of bits) to allocate 483 * 484 * Allocate (set bits in) a specified region of a bitmap. 485 * 486 * Return 0 on success, or %-EBUSY if specified region wasn't 487 * free (not all bits were zero). 488 */ 489
| |
490static inline int 491bitmap_allocate_region(unsigned long *bitmap, int pos, int order) 492{ 493 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) 494 return -EBUSY; 495 __reg_op(bitmap, pos, order, REG_OP_ALLOC); 496 return 0; 497} 498
| 452static inline int 453bitmap_allocate_region(unsigned long *bitmap, int pos, int order) 454{ 455 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) 456 return -EBUSY; 457 __reg_op(bitmap, pos, order, REG_OP_ALLOC); 458 return 0; 459} 460
|
499/** 500 * bitmap_release_region - release allocated bitmap region 501 * @bitmap: array of unsigned longs corresponding to the bitmap 502 * @pos: beginning of bit region to release 503 * @order: region size (log base 2 of number of bits) to release 504 * 505 * This is the complement to __bitmap_find_free_region() and releases 506 * the found region (by clearing it in the bitmap). 507 * 508 * No return value. 509 */
| |
510static inline void 511bitmap_release_region(unsigned long *bitmap, int pos, int order) 512{ 513 __reg_op(bitmap, pos, order, REG_OP_RELEASE); 514} 515 516 517#define for_each_set_bit(bit, addr, size) \ 518 for ((bit) = find_first_bit((addr), (size)); \ 519 (bit) < (size); \ 520 (bit) = find_next_bit((addr), (size), (bit) + 1)) 521 522#endif /* _LINUX_BITOPS_H_ */
| 461static inline void 462bitmap_release_region(unsigned long *bitmap, int pos, int order) 463{ 464 __reg_op(bitmap, pos, order, REG_OP_RELEASE); 465} 466 467 468#define for_each_set_bit(bit, addr, size) \ 469 for ((bit) = find_first_bit((addr), (size)); \ 470 (bit) < (size); \ 471 (bit) = find_next_bit((addr), (size), (bit) + 1)) 472 473#endif /* _LINUX_BITOPS_H_ */
|