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-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. --- 10 unchanged lines hidden (view full) --- 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)) |
36#ifdef __LP64__ 37#define BITS_PER_LONG 64 38#else 39#define BITS_PER_LONG 32 40#endif |
41#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG)) 42#define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n))) |
43#define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG) |
44#define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1))) |
45#define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
46#define GENMASK(lo, hi) (((2UL << ((hi) - (lo))) - 1UL) << (lo)) |
47#define BITS_PER_BYTE 8 48 49static inline int 50__ffs(int mask) 51{ 52 return (ffs(mask) - 1); 53} 54 --- 36 unchanged lines hidden (view full) --- 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) { |
99 mask = (*addr) & BITMAP_LAST_WORD_MASK(size); |
100 if (mask) 101 bit += __ffsl(mask); 102 else 103 bit += size; 104 } 105 return (bit); 106} 107 --- 5 unchanged lines hidden (view full) --- 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) { |
121 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size); |
122 if (mask) 123 bit += __ffsl(mask); 124 else 125 bit += size; 126 } 127 return (bit); 128} 129 --- 5 unchanged lines hidden (view full) --- 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) { |
143 mask = (*addr) & BITMAP_LAST_WORD_MASK(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)); --- 11 unchanged lines hidden (view full) --- 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) { |
171 mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs); |
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) { |
186 mask = (*addr) & BITMAP_LAST_WORD_MASK(size); |
187 if (mask) 188 bit += __ffsl(mask); 189 else 190 bit += size; 191 } 192 return (bit); 193} 194 --- 8 unchanged lines hidden (view full) --- 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) { |
211 mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs); |
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) { |
226 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size); |
227 if (mask) 228 bit += __ffsl(mask); 229 else 230 bit += size; 231 } 232 return (bit); 233} 234 --- 11 unchanged lines hidden (view full) --- 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) |
254 addr[size / BITS_PER_LONG] = BITMAP_LAST_WORD_MASK(tail); |
255} 256 257static inline int 258bitmap_full(unsigned long *addr, int size) 259{ |
260 unsigned long mask; |
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) { |
271 mask = BITMAP_LAST_WORD_MASK(tail); |
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{ |
281 unsigned long mask; |
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) { |
292 mask = BITMAP_LAST_WORD_MASK(tail); |
293 if ((addr[i] & mask) != 0) 294 return (0); 295 } 296 return (1); 297} 298 |
299#define __set_bit(i, a) \ |
300 atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i)) |
301 302#define set_bit(i, a) \ |
303 atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i)) |
304 305#define __clear_bit(i, a) \ |
306 atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i)) |
307 308#define clear_bit(i, a) \ |
309 atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i)) |
310 311#define test_bit(i, a) \ |
312 !!(atomic_load_acq_long(&((volatile long *)(a))[BIT_WORD(i)]) & \ 313 BIT_MASK(i)) |
314 315static inline long 316test_and_clear_bit(long bit, long *var) 317{ 318 long val; 319 |
320 var += BIT_WORD(bit); 321 bit %= BITS_PER_LONG; |
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 |
335 var += BIT_WORD(bit); 336 bit %= BITS_PER_LONG; |
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 |
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 --- 27 unchanged lines hidden (view full) --- 380 } 381 if (nr) { 382 mask_to_clear &= BITMAP_LAST_WORD_MASK(size); 383 *p &= ~mask_to_clear; 384 } 385} 386 387enum { |
388 REG_OP_ISFREE, 389 REG_OP_ALLOC, 390 REG_OP_RELEASE, |
391}; 392 393static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op) 394{ |
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; |
403 |
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 |
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 } |
420 ret = 1; |
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 |
437static inline int 438bitmap_find_free_region(unsigned long *bitmap, int bits, int order) 439{ |
440 int pos; 441 int end; |
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 |
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 |
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_ */ |