1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_BITMAP_H 3 #define __LINUX_BITMAP_H 4 5 #ifndef __ASSEMBLY__ 6 7 #include <linux/align.h> 8 #include <linux/bitops.h> 9 #include <linux/cleanup.h> 10 #include <linux/errno.h> 11 #include <linux/find.h> 12 #include <linux/limits.h> 13 #include <linux/string.h> 14 #include <linux/types.h> 15 #include <linux/bitmap-str.h> 16 17 struct device; 18 19 /* 20 * bitmaps provide bit arrays that consume one or more unsigned 21 * longs. The bitmap interface and available operations are listed 22 * here, in bitmap.h 23 * 24 * Function implementations generic to all architectures are in 25 * lib/bitmap.c. Functions implementations that are architecture 26 * specific are in various include/asm-<arch>/bitops.h headers 27 * and other arch/<arch> specific files. 28 * 29 * See lib/bitmap.c for more details. 30 */ 31 32 /** 33 * DOC: bitmap overview 34 * 35 * The available bitmap operations and their rough meaning in the 36 * case that the bitmap is a single unsigned long are thus: 37 * 38 * The generated code is more efficient when nbits is known at 39 * compile-time and at most BITS_PER_LONG. 40 * 41 * :: 42 * 43 * bitmap_zero(dst, nbits) *dst = 0UL 44 * bitmap_fill(dst, nbits) *dst = ~0UL 45 * bitmap_copy(dst, src, nbits) *dst = *src 46 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2 47 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2 48 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2 49 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2) 50 * bitmap_complement(dst, src, nbits) *dst = ~(*src) 51 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal? 52 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap? 53 * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2? 54 * bitmap_empty(src, nbits) Are all bits zero in *src? 55 * bitmap_full(src, nbits) Are all bits set in *src? 56 * bitmap_weight(src, nbits) Hamming Weight: number set bits 57 * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap 58 * bitmap_set(dst, pos, nbits) Set specified bit area 59 * bitmap_clear(dst, pos, nbits) Clear specified bit area 60 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area 61 * bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above 62 * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n 63 * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n 64 * bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest 65 * bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask) 66 * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src) 67 * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit) 68 * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap 69 * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz 70 * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf 71 * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf 72 * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf 73 * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf 74 * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region 75 * bitmap_release_region(bitmap, pos, order) Free specified bit region 76 * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region 77 * bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst 78 * bitmap_from_arr64(dst, buf, nbits) Copy nbits from u64[] buf to dst 79 * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst 80 * bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst 81 * bitmap_get_value8(map, start) Get 8bit value from map at start 82 * bitmap_set_value8(map, value, start) Set 8bit value to map at start 83 * 84 * Note, bitmap_zero() and bitmap_fill() operate over the region of 85 * unsigned longs, that is, bits behind bitmap till the unsigned long 86 * boundary will be zeroed or filled as well. Consider to use 87 * bitmap_clear() or bitmap_set() to make explicit zeroing or filling 88 * respectively. 89 */ 90 91 /** 92 * DOC: bitmap bitops 93 * 94 * Also the following operations in asm/bitops.h apply to bitmaps.:: 95 * 96 * set_bit(bit, addr) *addr |= bit 97 * clear_bit(bit, addr) *addr &= ~bit 98 * change_bit(bit, addr) *addr ^= bit 99 * test_bit(bit, addr) Is bit set in *addr? 100 * test_and_set_bit(bit, addr) Set bit and return old value 101 * test_and_clear_bit(bit, addr) Clear bit and return old value 102 * test_and_change_bit(bit, addr) Change bit and return old value 103 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr 104 * find_first_bit(addr, nbits) Position first set bit in *addr 105 * find_next_zero_bit(addr, nbits, bit) 106 * Position next zero bit in *addr >= bit 107 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit 108 * find_next_and_bit(addr1, addr2, nbits, bit) 109 * Same as find_next_bit, but in 110 * (*addr1 & *addr2) 111 * 112 */ 113 114 /** 115 * DOC: declare bitmap 116 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used 117 * to declare an array named 'name' of just enough unsigned longs to 118 * contain all bit positions from 0 to 'bits' - 1. 119 */ 120 121 /* 122 * Allocation and deallocation of bitmap. 123 * Provided in lib/bitmap.c to avoid circular dependency. 124 */ 125 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags); 126 unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags); 127 unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node); 128 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node); 129 void bitmap_free(const unsigned long *bitmap); 130 131 DEFINE_FREE(bitmap, unsigned long *, if (_T) bitmap_free(_T)) 132 133 /* Managed variants of the above. */ 134 unsigned long *devm_bitmap_alloc(struct device *dev, 135 unsigned int nbits, gfp_t flags); 136 unsigned long *devm_bitmap_zalloc(struct device *dev, 137 unsigned int nbits, gfp_t flags); 138 139 /* 140 * lib/bitmap.c provides these functions: 141 */ 142 143 bool __bitmap_equal(const unsigned long *bitmap1, 144 const unsigned long *bitmap2, unsigned int nbits); 145 bool __pure __bitmap_or_equal(const unsigned long *src1, 146 const unsigned long *src2, 147 const unsigned long *src3, 148 unsigned int nbits); 149 void __bitmap_complement(unsigned long *dst, const unsigned long *src, 150 unsigned int nbits); 151 void __bitmap_shift_right(unsigned long *dst, const unsigned long *src, 152 unsigned int shift, unsigned int nbits); 153 void __bitmap_shift_left(unsigned long *dst, const unsigned long *src, 154 unsigned int shift, unsigned int nbits); 155 void bitmap_cut(unsigned long *dst, const unsigned long *src, 156 unsigned int first, unsigned int cut, unsigned int nbits); 157 bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, 158 const unsigned long *bitmap2, unsigned int nbits); 159 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, 160 const unsigned long *bitmap2, unsigned int nbits); 161 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, 162 const unsigned long *bitmap2, unsigned int nbits); 163 bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, 164 const unsigned long *bitmap2, unsigned int nbits); 165 void __bitmap_replace(unsigned long *dst, 166 const unsigned long *old, const unsigned long *new, 167 const unsigned long *mask, unsigned int nbits); 168 bool __bitmap_intersects(const unsigned long *bitmap1, 169 const unsigned long *bitmap2, unsigned int nbits); 170 bool __bitmap_subset(const unsigned long *bitmap1, 171 const unsigned long *bitmap2, unsigned int nbits); 172 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits); 173 unsigned int __bitmap_weight_and(const unsigned long *bitmap1, 174 const unsigned long *bitmap2, unsigned int nbits); 175 void __bitmap_set(unsigned long *map, unsigned int start, int len); 176 void __bitmap_clear(unsigned long *map, unsigned int start, int len); 177 178 unsigned long bitmap_find_next_zero_area_off(unsigned long *map, 179 unsigned long size, 180 unsigned long start, 181 unsigned int nr, 182 unsigned long align_mask, 183 unsigned long align_offset); 184 185 /** 186 * bitmap_find_next_zero_area - find a contiguous aligned zero area 187 * @map: The address to base the search on 188 * @size: The bitmap size in bits 189 * @start: The bitnumber to start searching at 190 * @nr: The number of zeroed bits we're looking for 191 * @align_mask: Alignment mask for zero area 192 * 193 * The @align_mask should be one less than a power of 2; the effect is that 194 * the bit offset of all zero areas this function finds is multiples of that 195 * power of 2. A @align_mask of 0 means no alignment is required. 196 */ 197 static inline unsigned long 198 bitmap_find_next_zero_area(unsigned long *map, 199 unsigned long size, 200 unsigned long start, 201 unsigned int nr, 202 unsigned long align_mask) 203 { 204 return bitmap_find_next_zero_area_off(map, size, start, nr, 205 align_mask, 0); 206 } 207 208 void bitmap_remap(unsigned long *dst, const unsigned long *src, 209 const unsigned long *old, const unsigned long *new, unsigned int nbits); 210 int bitmap_bitremap(int oldbit, 211 const unsigned long *old, const unsigned long *new, int bits); 212 void bitmap_onto(unsigned long *dst, const unsigned long *orig, 213 const unsigned long *relmap, unsigned int bits); 214 void bitmap_fold(unsigned long *dst, const unsigned long *orig, 215 unsigned int sz, unsigned int nbits); 216 217 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) 218 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) 219 220 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits) 221 { 222 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 223 224 if (small_const_nbits(nbits)) 225 *dst = 0; 226 else 227 memset(dst, 0, len); 228 } 229 230 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits) 231 { 232 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 233 234 if (small_const_nbits(nbits)) 235 *dst = ~0UL; 236 else 237 memset(dst, 0xff, len); 238 } 239 240 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src, 241 unsigned int nbits) 242 { 243 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 244 245 if (small_const_nbits(nbits)) 246 *dst = *src; 247 else 248 memcpy(dst, src, len); 249 } 250 251 /* 252 * Copy bitmap and clear tail bits in last word. 253 */ 254 static inline void bitmap_copy_clear_tail(unsigned long *dst, 255 const unsigned long *src, unsigned int nbits) 256 { 257 bitmap_copy(dst, src, nbits); 258 if (nbits % BITS_PER_LONG) 259 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits); 260 } 261 262 /* 263 * On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64 264 * machines the order of hi and lo parts of numbers match the bitmap structure. 265 * In both cases conversion is not needed when copying data from/to arrays of 266 * u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead 267 * to out-of-bound access. To avoid that, both LE and BE variants of 64-bit 268 * architectures are not using bitmap_copy_clear_tail(). 269 */ 270 #if BITS_PER_LONG == 64 271 void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, 272 unsigned int nbits); 273 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, 274 unsigned int nbits); 275 #else 276 #define bitmap_from_arr32(bitmap, buf, nbits) \ 277 bitmap_copy_clear_tail((unsigned long *) (bitmap), \ 278 (const unsigned long *) (buf), (nbits)) 279 #define bitmap_to_arr32(buf, bitmap, nbits) \ 280 bitmap_copy_clear_tail((unsigned long *) (buf), \ 281 (const unsigned long *) (bitmap), (nbits)) 282 #endif 283 284 /* 285 * On 64-bit systems bitmaps are represented as u64 arrays internally. So, 286 * the conversion is not needed when copying data from/to arrays of u64. 287 */ 288 #if BITS_PER_LONG == 32 289 void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits); 290 void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits); 291 #else 292 #define bitmap_from_arr64(bitmap, buf, nbits) \ 293 bitmap_copy_clear_tail((unsigned long *)(bitmap), (const unsigned long *)(buf), (nbits)) 294 #define bitmap_to_arr64(buf, bitmap, nbits) \ 295 bitmap_copy_clear_tail((unsigned long *)(buf), (const unsigned long *)(bitmap), (nbits)) 296 #endif 297 298 static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1, 299 const unsigned long *src2, unsigned int nbits) 300 { 301 if (small_const_nbits(nbits)) 302 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0; 303 return __bitmap_and(dst, src1, src2, nbits); 304 } 305 306 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, 307 const unsigned long *src2, unsigned int nbits) 308 { 309 if (small_const_nbits(nbits)) 310 *dst = *src1 | *src2; 311 else 312 __bitmap_or(dst, src1, src2, nbits); 313 } 314 315 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1, 316 const unsigned long *src2, unsigned int nbits) 317 { 318 if (small_const_nbits(nbits)) 319 *dst = *src1 ^ *src2; 320 else 321 __bitmap_xor(dst, src1, src2, nbits); 322 } 323 324 static inline bool bitmap_andnot(unsigned long *dst, const unsigned long *src1, 325 const unsigned long *src2, unsigned int nbits) 326 { 327 if (small_const_nbits(nbits)) 328 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0; 329 return __bitmap_andnot(dst, src1, src2, nbits); 330 } 331 332 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src, 333 unsigned int nbits) 334 { 335 if (small_const_nbits(nbits)) 336 *dst = ~(*src); 337 else 338 __bitmap_complement(dst, src, nbits); 339 } 340 341 #ifdef __LITTLE_ENDIAN 342 #define BITMAP_MEM_ALIGNMENT 8 343 #else 344 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long)) 345 #endif 346 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1) 347 348 static inline bool bitmap_equal(const unsigned long *src1, 349 const unsigned long *src2, unsigned int nbits) 350 { 351 if (small_const_nbits(nbits)) 352 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)); 353 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) && 354 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) 355 return !memcmp(src1, src2, nbits / 8); 356 return __bitmap_equal(src1, src2, nbits); 357 } 358 359 /** 360 * bitmap_or_equal - Check whether the or of two bitmaps is equal to a third 361 * @src1: Pointer to bitmap 1 362 * @src2: Pointer to bitmap 2 will be or'ed with bitmap 1 363 * @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2 364 * @nbits: number of bits in each of these bitmaps 365 * 366 * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise 367 */ 368 static inline bool bitmap_or_equal(const unsigned long *src1, 369 const unsigned long *src2, 370 const unsigned long *src3, 371 unsigned int nbits) 372 { 373 if (!small_const_nbits(nbits)) 374 return __bitmap_or_equal(src1, src2, src3, nbits); 375 376 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits)); 377 } 378 379 static inline bool bitmap_intersects(const unsigned long *src1, 380 const unsigned long *src2, 381 unsigned int nbits) 382 { 383 if (small_const_nbits(nbits)) 384 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0; 385 else 386 return __bitmap_intersects(src1, src2, nbits); 387 } 388 389 static inline bool bitmap_subset(const unsigned long *src1, 390 const unsigned long *src2, unsigned int nbits) 391 { 392 if (small_const_nbits(nbits)) 393 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits)); 394 else 395 return __bitmap_subset(src1, src2, nbits); 396 } 397 398 static inline bool bitmap_empty(const unsigned long *src, unsigned nbits) 399 { 400 if (small_const_nbits(nbits)) 401 return ! (*src & BITMAP_LAST_WORD_MASK(nbits)); 402 403 return find_first_bit(src, nbits) == nbits; 404 } 405 406 static inline bool bitmap_full(const unsigned long *src, unsigned int nbits) 407 { 408 if (small_const_nbits(nbits)) 409 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits)); 410 411 return find_first_zero_bit(src, nbits) == nbits; 412 } 413 414 static __always_inline 415 unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits) 416 { 417 if (small_const_nbits(nbits)) 418 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)); 419 return __bitmap_weight(src, nbits); 420 } 421 422 static __always_inline 423 unsigned long bitmap_weight_and(const unsigned long *src1, 424 const unsigned long *src2, unsigned int nbits) 425 { 426 if (small_const_nbits(nbits)) 427 return hweight_long(*src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)); 428 return __bitmap_weight_and(src1, src2, nbits); 429 } 430 431 static __always_inline void bitmap_set(unsigned long *map, unsigned int start, 432 unsigned int nbits) 433 { 434 if (__builtin_constant_p(nbits) && nbits == 1) 435 __set_bit(start, map); 436 else if (small_const_nbits(start + nbits)) 437 *map |= GENMASK(start + nbits - 1, start); 438 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) && 439 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) && 440 __builtin_constant_p(nbits & BITMAP_MEM_MASK) && 441 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) 442 memset((char *)map + start / 8, 0xff, nbits / 8); 443 else 444 __bitmap_set(map, start, nbits); 445 } 446 447 static __always_inline void bitmap_clear(unsigned long *map, unsigned int start, 448 unsigned int nbits) 449 { 450 if (__builtin_constant_p(nbits) && nbits == 1) 451 __clear_bit(start, map); 452 else if (small_const_nbits(start + nbits)) 453 *map &= ~GENMASK(start + nbits - 1, start); 454 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) && 455 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) && 456 __builtin_constant_p(nbits & BITMAP_MEM_MASK) && 457 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) 458 memset((char *)map + start / 8, 0, nbits / 8); 459 else 460 __bitmap_clear(map, start, nbits); 461 } 462 463 static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src, 464 unsigned int shift, unsigned int nbits) 465 { 466 if (small_const_nbits(nbits)) 467 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift; 468 else 469 __bitmap_shift_right(dst, src, shift, nbits); 470 } 471 472 static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src, 473 unsigned int shift, unsigned int nbits) 474 { 475 if (small_const_nbits(nbits)) 476 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits); 477 else 478 __bitmap_shift_left(dst, src, shift, nbits); 479 } 480 481 static inline void bitmap_replace(unsigned long *dst, 482 const unsigned long *old, 483 const unsigned long *new, 484 const unsigned long *mask, 485 unsigned int nbits) 486 { 487 if (small_const_nbits(nbits)) 488 *dst = (*old & ~(*mask)) | (*new & *mask); 489 else 490 __bitmap_replace(dst, old, new, mask, nbits); 491 } 492 493 static inline void bitmap_next_set_region(unsigned long *bitmap, 494 unsigned int *rs, unsigned int *re, 495 unsigned int end) 496 { 497 *rs = find_next_bit(bitmap, end, *rs); 498 *re = find_next_zero_bit(bitmap, end, *rs + 1); 499 } 500 501 /** 502 * bitmap_release_region - release allocated bitmap region 503 * @bitmap: array of unsigned longs corresponding to the bitmap 504 * @pos: beginning of bit region to release 505 * @order: region size (log base 2 of number of bits) to release 506 * 507 * This is the complement to __bitmap_find_free_region() and releases 508 * the found region (by clearing it in the bitmap). 509 */ 510 static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order) 511 { 512 bitmap_clear(bitmap, pos, BIT(order)); 513 } 514 515 /** 516 * bitmap_allocate_region - allocate bitmap region 517 * @bitmap: array of unsigned longs corresponding to the bitmap 518 * @pos: beginning of bit region to allocate 519 * @order: region size (log base 2 of number of bits) to allocate 520 * 521 * Allocate (set bits in) a specified region of a bitmap. 522 * 523 * Returns: 0 on success, or %-EBUSY if specified region wasn't 524 * free (not all bits were zero). 525 */ 526 static inline int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order) 527 { 528 unsigned int len = BIT(order); 529 530 if (find_next_bit(bitmap, pos + len, pos) < pos + len) 531 return -EBUSY; 532 bitmap_set(bitmap, pos, len); 533 return 0; 534 } 535 536 /** 537 * bitmap_find_free_region - find a contiguous aligned mem region 538 * @bitmap: array of unsigned longs corresponding to the bitmap 539 * @bits: number of bits in the bitmap 540 * @order: region size (log base 2 of number of bits) to find 541 * 542 * Find a region of free (zero) bits in a @bitmap of @bits bits and 543 * allocate them (set them to one). Only consider regions of length 544 * a power (@order) of two, aligned to that power of two, which 545 * makes the search algorithm much faster. 546 * 547 * Returns: the bit offset in bitmap of the allocated region, 548 * or -errno on failure. 549 */ 550 static inline int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order) 551 { 552 unsigned int pos, end; /* scans bitmap by regions of size order */ 553 554 for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) { 555 if (!bitmap_allocate_region(bitmap, pos, order)) 556 return pos; 557 } 558 return -ENOMEM; 559 } 560 561 /** 562 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap. 563 * @n: u64 value 564 * 565 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit 566 * integers in 32-bit environment, and 64-bit integers in 64-bit one. 567 * 568 * There are four combinations of endianness and length of the word in linux 569 * ABIs: LE64, BE64, LE32 and BE32. 570 * 571 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in 572 * bitmaps and therefore don't require any special handling. 573 * 574 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory 575 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the 576 * other hand is represented as an array of 32-bit words and the position of 577 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that 578 * word. For example, bit #42 is located at 10th position of 2nd word. 579 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit 580 * values in memory as it usually does. But for BE we need to swap hi and lo 581 * words manually. 582 * 583 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and 584 * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps 585 * hi and lo words, as is expected by bitmap. 586 */ 587 #if __BITS_PER_LONG == 64 588 #define BITMAP_FROM_U64(n) (n) 589 #else 590 #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \ 591 ((unsigned long) ((u64)(n) >> 32)) 592 #endif 593 594 /** 595 * bitmap_from_u64 - Check and swap words within u64. 596 * @mask: source bitmap 597 * @dst: destination bitmap 598 * 599 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]`` 600 * to read u64 mask, we will get the wrong word. 601 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits, 602 * but we expect the lower 32-bits of u64. 603 */ 604 static inline void bitmap_from_u64(unsigned long *dst, u64 mask) 605 { 606 bitmap_from_arr64(dst, &mask, 64); 607 } 608 609 /** 610 * bitmap_get_value8 - get an 8-bit value within a memory region 611 * @map: address to the bitmap memory region 612 * @start: bit offset of the 8-bit value; must be a multiple of 8 613 * 614 * Returns the 8-bit value located at the @start bit offset within the @src 615 * memory region. 616 */ 617 static inline unsigned long bitmap_get_value8(const unsigned long *map, 618 unsigned long start) 619 { 620 const size_t index = BIT_WORD(start); 621 const unsigned long offset = start % BITS_PER_LONG; 622 623 return (map[index] >> offset) & 0xFF; 624 } 625 626 /** 627 * bitmap_set_value8 - set an 8-bit value within a memory region 628 * @map: address to the bitmap memory region 629 * @value: the 8-bit value; values wider than 8 bits may clobber bitmap 630 * @start: bit offset of the 8-bit value; must be a multiple of 8 631 */ 632 static inline void bitmap_set_value8(unsigned long *map, unsigned long value, 633 unsigned long start) 634 { 635 const size_t index = BIT_WORD(start); 636 const unsigned long offset = start % BITS_PER_LONG; 637 638 map[index] &= ~(0xFFUL << offset); 639 map[index] |= value << offset; 640 } 641 642 #endif /* __ASSEMBLY__ */ 643 644 #endif /* __LINUX_BITMAP_H */ 645