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