2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2015 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.
31 #ifndef _LINUX_BITOPS_H_
32 #define _LINUX_BITOPS_H_
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
38 #define BIT(nr) (1UL << (nr))
40 #define BITS_PER_LONG 64
42 #define BITS_PER_LONG 32
44 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
45 #define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
46 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
47 #define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
48 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
49 #define GENMASK(lo, hi) (((2UL << ((hi) - (lo))) - 1UL) << (lo))
50 #define BITS_PER_BYTE 8
55 return (ffs(mask) - 1);
61 return (fls(mask) - 1);
67 return (ffsl(mask) - 1);
73 return (flsl(mask) - 1);
77 #define ffz(mask) __ffs(~(mask))
79 static inline int get_count_order(unsigned int count)
83 order = fls(count) - 1;
84 if (count & (count - 1))
89 static inline unsigned long
90 find_first_bit(unsigned long *addr, unsigned long size)
95 for (bit = 0; size >= BITS_PER_LONG;
96 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
99 return (bit + __ffsl(*addr));
102 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
111 static inline unsigned long
112 find_first_zero_bit(unsigned long *addr, unsigned long size)
117 for (bit = 0; size >= BITS_PER_LONG;
118 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
121 return (bit + __ffsl(~(*addr)));
124 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
133 static inline unsigned long
134 find_last_bit(unsigned long *addr, unsigned long size)
141 pos = size / BITS_PER_LONG;
142 offs = size % BITS_PER_LONG;
143 bit = BITS_PER_LONG * pos;
146 mask = (*addr) & BITMAP_LAST_WORD_MASK(offs);
148 return (bit + __flsl(mask));
152 bit -= BITS_PER_LONG;
154 return (bit + __flsl(mask));
159 static inline unsigned long
160 find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset)
169 pos = offset / BITS_PER_LONG;
170 offs = offset % BITS_PER_LONG;
171 bit = BITS_PER_LONG * pos;
174 mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs);
176 return (bit + __ffsl(mask));
177 if (size - bit <= BITS_PER_LONG)
179 bit += BITS_PER_LONG;
182 for (size -= bit; size >= BITS_PER_LONG;
183 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
186 return (bit + __ffsl(*addr));
189 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
198 static inline unsigned long
199 find_next_zero_bit(unsigned long *addr, unsigned long size,
200 unsigned long offset)
209 pos = offset / BITS_PER_LONG;
210 offs = offset % BITS_PER_LONG;
211 bit = BITS_PER_LONG * pos;
214 mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs);
216 return (bit + __ffsl(mask));
217 if (size - bit <= BITS_PER_LONG)
219 bit += BITS_PER_LONG;
222 for (size -= bit; size >= BITS_PER_LONG;
223 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
226 return (bit + __ffsl(~(*addr)));
229 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
239 bitmap_zero(unsigned long *addr, int size)
243 len = BITS_TO_LONGS(size) * sizeof(long);
244 memset(addr, 0, len);
248 bitmap_fill(unsigned long *addr, int size)
253 len = (size / BITS_PER_LONG) * sizeof(long);
254 memset(addr, 0xff, len);
255 tail = size & (BITS_PER_LONG - 1);
257 addr[size / BITS_PER_LONG] = BITMAP_LAST_WORD_MASK(tail);
261 bitmap_full(unsigned long *addr, int size)
268 len = size / BITS_PER_LONG;
269 for (i = 0; i < len; i++)
272 tail = size & (BITS_PER_LONG - 1);
274 mask = BITMAP_LAST_WORD_MASK(tail);
275 if ((addr[i] & mask) != mask)
282 bitmap_empty(unsigned long *addr, int size)
289 len = size / BITS_PER_LONG;
290 for (i = 0; i < len; i++)
293 tail = size & (BITS_PER_LONG - 1);
295 mask = BITMAP_LAST_WORD_MASK(tail);
296 if ((addr[i] & mask) != 0)
302 #define __set_bit(i, a) \
303 atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
305 #define set_bit(i, a) \
306 atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
308 #define __clear_bit(i, a) \
309 atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
311 #define clear_bit(i, a) \
312 atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
314 #define test_bit(i, a) \
315 !!(atomic_load_acq_long(&((volatile long *)(a))[BIT_WORD(i)]) & \
319 test_and_clear_bit(long bit, long *var)
323 var += BIT_WORD(bit);
324 bit %= BITS_PER_LONG;
327 val = *(volatile long *)var;
328 } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
330 return !!(val & bit);
334 test_and_set_bit(long bit, long *var)
338 var += BIT_WORD(bit);
339 bit %= BITS_PER_LONG;
342 val = *(volatile long *)var;
343 } while (atomic_cmpset_long(var, val, val | bit) == 0);
345 return !!(val & bit);
349 bitmap_set(unsigned long *map, int start, int nr)
351 unsigned long *p = map + BIT_WORD(start);
352 const int size = start + nr;
353 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
354 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
356 while (nr - bits_to_set >= 0) {
359 bits_to_set = BITS_PER_LONG;
364 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
370 bitmap_clear(unsigned long *map, int start, int nr)
372 unsigned long *p = map + BIT_WORD(start);
373 const int size = start + nr;
374 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
375 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
377 while (nr - bits_to_clear >= 0) {
378 *p &= ~mask_to_clear;
380 bits_to_clear = BITS_PER_LONG;
381 mask_to_clear = ~0UL;
385 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
386 *p &= ~mask_to_clear;
396 static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
407 nbits_reg = 1 << order;
408 index = pos / BITS_PER_LONG;
409 offset = pos - (index * BITS_PER_LONG);
410 nlongs_reg = BITS_TO_LONGS(nbits_reg);
411 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
413 mask = (1UL << (nbitsinlong - 1));
419 for (i = 0; i < nlongs_reg; i++) {
420 if (bitmap[index + i] & mask)
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(unsigned long *bitmap, int bits, int order)
446 for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
447 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
449 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
456 bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
458 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
460 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
465 bitmap_release_region(unsigned long *bitmap, int pos, int order)
467 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
471 #define for_each_set_bit(bit, addr, size) \
472 for ((bit) = find_first_bit((addr), (size)); \
474 (bit) = find_next_bit((addr), (size), (bit) + 1))
476 #endif /* _LINUX_BITOPS_H_ */