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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
29 #ifndef _LINUX_BITOPS_H_
30 #define _LINUX_BITOPS_H_
33 #define BITS_PER_LONG 64
35 #define BITS_PER_LONG 32
37 #define BIT_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
38 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
39 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
41 #define BITS_PER_BYTE 8
46 return (ffs(mask) - 1);
52 return (fls(mask) - 1);
58 return (ffsl(mask) - 1);
64 return (flsl(mask) - 1);
68 #define ffz(mask) __ffs(~(mask))
70 static inline int get_count_order(unsigned int count)
74 order = fls(count) - 1;
75 if (count & (count - 1))
80 static inline unsigned long
81 find_first_bit(unsigned long *addr, unsigned long size)
86 for (bit = 0; size >= BITS_PER_LONG;
87 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
90 return (bit + __ffsl(*addr));
93 mask = (*addr) & BIT_MASK(size);
102 static inline unsigned long
103 find_first_zero_bit(unsigned long *addr, unsigned long size)
108 for (bit = 0; size >= BITS_PER_LONG;
109 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
112 return (bit + __ffsl(~(*addr)));
115 mask = ~(*addr) & BIT_MASK(size);
124 static inline unsigned long
125 find_last_bit(unsigned long *addr, unsigned long size)
132 pos = size / BITS_PER_LONG;
133 offs = size % BITS_PER_LONG;
134 bit = BITS_PER_LONG * pos;
137 mask = (*addr) & BIT_MASK(offs);
139 return (bit + __flsl(mask));
143 bit -= BITS_PER_LONG;
145 return (bit + __flsl(mask));
150 static inline unsigned long
151 find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset)
160 pos = offset / BITS_PER_LONG;
161 offs = offset % BITS_PER_LONG;
162 bit = BITS_PER_LONG * pos;
165 mask = (*addr) & ~BIT_MASK(offs);
167 return (bit + __ffsl(mask));
168 bit += BITS_PER_LONG;
171 for (size -= bit; size >= BITS_PER_LONG;
172 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
175 return (bit + __ffsl(*addr));
178 mask = (*addr) & BIT_MASK(size);
187 static inline unsigned long
188 find_next_zero_bit(unsigned long *addr, unsigned long size,
189 unsigned long offset)
198 pos = offset / BITS_PER_LONG;
199 offs = offset % BITS_PER_LONG;
200 bit = BITS_PER_LONG * pos;
203 mask = ~(*addr) & ~BIT_MASK(offs);
205 return (bit + __ffsl(mask));
206 bit += BITS_PER_LONG;
209 for (size -= bit; size >= BITS_PER_LONG;
210 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
213 return (bit + __ffsl(~(*addr)));
216 mask = ~(*addr) & BIT_MASK(size);
226 bitmap_zero(unsigned long *addr, int size)
230 len = BITS_TO_LONGS(size) * sizeof(long);
231 memset(addr, 0, len);
235 bitmap_fill(unsigned long *addr, int size)
240 len = (size / BITS_PER_LONG) * sizeof(long);
241 memset(addr, 0xff, len);
242 tail = size & (BITS_PER_LONG - 1);
244 addr[size / BITS_PER_LONG] = BIT_MASK(tail);
248 bitmap_full(unsigned long *addr, int size)
255 len = size / BITS_PER_LONG;
256 for (i = 0; i < len; i++)
259 tail = size & (BITS_PER_LONG - 1);
261 mask = BIT_MASK(tail);
262 if ((addr[i] & mask) != mask)
269 bitmap_empty(unsigned long *addr, int size)
276 len = size / BITS_PER_LONG;
277 for (i = 0; i < len; i++)
280 tail = size & (BITS_PER_LONG - 1);
282 mask = BIT_MASK(tail);
283 if ((addr[i] & mask) != 0)
289 #define NBLONG (NBBY * sizeof(long))
291 #define set_bit(i, a) \
292 atomic_set_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
294 #define clear_bit(i, a) \
295 atomic_clear_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
297 #define test_bit(i, a) \
298 !!(atomic_load_acq_long(&((volatile long *)(a))[(i)/NBLONG]) & \
299 (1UL << ((i) % NBLONG)))
302 test_and_clear_bit(long bit, long *var)
306 var += bit / (sizeof(long) * NBBY);
307 bit %= sizeof(long) * NBBY;
310 val = *(volatile long *)var;
311 } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
313 return !!(val & bit);
317 test_and_set_bit(long bit, long *var)
321 var += bit / (sizeof(long) * NBBY);
322 bit %= sizeof(long) * NBBY;
325 val = *(volatile long *)var;
326 } while (atomic_cmpset_long(var, val, val | bit) == 0);
328 return !!(val & bit);
332 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
333 #define BITMAP_LAST_WORD_MASK(nbits) \
335 ((nbits) % BITS_PER_LONG) ? \
336 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
341 bitmap_set(unsigned long *map, int start, int nr)
343 unsigned long *p = map + BIT_WORD(start);
344 const int size = start + nr;
345 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
346 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
348 while (nr - bits_to_set >= 0) {
351 bits_to_set = BITS_PER_LONG;
356 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
362 bitmap_clear(unsigned long *map, int start, int nr)
364 unsigned long *p = map + BIT_WORD(start);
365 const int size = start + nr;
366 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
367 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
369 while (nr - bits_to_clear >= 0) {
370 *p &= ~mask_to_clear;
372 bits_to_clear = BITS_PER_LONG;
373 mask_to_clear = ~0UL;
377 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
378 *p &= ~mask_to_clear;
383 REG_OP_ISFREE, /* true if region is all zero bits */
384 REG_OP_ALLOC, /* set all bits in region */
385 REG_OP_RELEASE, /* clear all bits in region */
388 static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
390 int nbits_reg; /* number of bits in region */
391 int index; /* index first long of region in bitmap */
392 int offset; /* bit offset region in bitmap[index] */
393 int nlongs_reg; /* num longs spanned by region in bitmap */
394 int nbitsinlong; /* num bits of region in each spanned long */
395 unsigned long mask; /* bitmask for one long of region */
396 int i; /* scans bitmap by longs */
397 int ret = 0; /* return value */
400 * Either nlongs_reg == 1 (for small orders that fit in one long)
401 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
403 nbits_reg = 1 << order;
404 index = pos / BITS_PER_LONG;
405 offset = pos - (index * BITS_PER_LONG);
406 nlongs_reg = BITS_TO_LONGS(nbits_reg);
407 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
410 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
411 * overflows if nbitsinlong == BITS_PER_LONG.
413 mask = (1UL << (nbitsinlong - 1));
419 for (i = 0; i < nlongs_reg; i++) {
420 if (bitmap[index + i] & mask)
423 ret = 1; /* all bits in region free (zero) */
427 for (i = 0; i < nlongs_reg; i++)
428 bitmap[index + i] |= mask;
432 for (i = 0; i < nlongs_reg; i++)
433 bitmap[index + i] &= ~mask;
441 * bitmap_find_free_region - find a contiguous aligned mem region
442 * @bitmap: array of unsigned longs corresponding to the bitmap
443 * @bits: number of bits in the bitmap
444 * @order: region size (log base 2 of number of bits) to find
446 * Find a region of free (zero) bits in a @bitmap of @bits bits and
447 * allocate them (set them to one). Only consider regions of length
448 * a power (@order) of two, aligned to that power of two, which
449 * makes the search algorithm much faster.
451 * Return the bit offset in bitmap of the allocated region,
452 * or -errno on failure.
455 bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
457 int pos, end; /* scans bitmap by regions of size order */
459 for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
460 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
462 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
469 * bitmap_allocate_region - allocate bitmap region
470 * @bitmap: array of unsigned longs corresponding to the bitmap
471 * @pos: beginning of bit region to allocate
472 * @order: region size (log base 2 of number of bits) to allocate
474 * Allocate (set bits in) a specified region of a bitmap.
476 * Return 0 on success, or %-EBUSY if specified region wasn't
477 * free (not all bits were zero).
481 bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
483 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
485 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
490 * bitmap_release_region - release allocated bitmap region
491 * @bitmap: array of unsigned longs corresponding to the bitmap
492 * @pos: beginning of bit region to release
493 * @order: region size (log base 2 of number of bits) to release
495 * This is the complement to __bitmap_find_free_region() and releases
496 * the found region (by clearing it in the bitmap).
501 bitmap_release_region(unsigned long *bitmap, int pos, int order)
503 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
507 #define for_each_set_bit(bit, addr, size) \
508 for ((bit) = find_first_bit((addr), (size)); \
510 (bit) = find_next_bit((addr), (size), (bit) + 1))
512 #endif /* _LINUX_BITOPS_H_ */