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 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits) 227 { 228 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 229 memset(dst, 0, len); 230 } 231 232 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits) 233 { 234 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 235 memset(dst, 0xff, len); 236 } 237 238 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src, 239 unsigned int nbits) 240 { 241 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 242 memcpy(dst, src, len); 243 } 244 245 /* 246 * Copy bitmap and clear tail bits in last word. 247 */ 248 static inline void bitmap_copy_clear_tail(unsigned long *dst, 249 const unsigned long *src, unsigned int nbits) 250 { 251 bitmap_copy(dst, src, nbits); 252 if (nbits % BITS_PER_LONG) 253 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits); 254 } 255 256 /* 257 * On 32-bit systems bitmaps are represented as u32 arrays internally, and 258 * therefore conversion is not needed when copying data from/to arrays of u32. 259 */ 260 #if BITS_PER_LONG == 64 261 extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, 262 unsigned int nbits); 263 extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, 264 unsigned int nbits); 265 #else 266 #define bitmap_from_arr32(bitmap, buf, nbits) \ 267 bitmap_copy_clear_tail((unsigned long *) (bitmap), \ 268 (const unsigned long *) (buf), (nbits)) 269 #define bitmap_to_arr32(buf, bitmap, nbits) \ 270 bitmap_copy_clear_tail((unsigned long *) (buf), \ 271 (const unsigned long *) (bitmap), (nbits)) 272 #endif 273 274 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1, 275 const unsigned long *src2, unsigned int nbits) 276 { 277 if (small_const_nbits(nbits)) 278 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0; 279 return __bitmap_and(dst, src1, src2, nbits); 280 } 281 282 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, 283 const unsigned long *src2, unsigned int nbits) 284 { 285 if (small_const_nbits(nbits)) 286 *dst = *src1 | *src2; 287 else 288 __bitmap_or(dst, src1, src2, nbits); 289 } 290 291 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1, 292 const unsigned long *src2, unsigned int nbits) 293 { 294 if (small_const_nbits(nbits)) 295 *dst = *src1 ^ *src2; 296 else 297 __bitmap_xor(dst, src1, src2, nbits); 298 } 299 300 static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1, 301 const unsigned long *src2, unsigned int nbits) 302 { 303 if (small_const_nbits(nbits)) 304 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0; 305 return __bitmap_andnot(dst, src1, src2, nbits); 306 } 307 308 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src, 309 unsigned int nbits) 310 { 311 if (small_const_nbits(nbits)) 312 *dst = ~(*src); 313 else 314 __bitmap_complement(dst, src, nbits); 315 } 316 317 #ifdef __LITTLE_ENDIAN 318 #define BITMAP_MEM_ALIGNMENT 8 319 #else 320 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long)) 321 #endif 322 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1) 323 324 static inline int bitmap_equal(const unsigned long *src1, 325 const unsigned long *src2, unsigned int nbits) 326 { 327 if (small_const_nbits(nbits)) 328 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)); 329 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) && 330 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) 331 return !memcmp(src1, src2, nbits / 8); 332 return __bitmap_equal(src1, src2, nbits); 333 } 334 335 /** 336 * bitmap_or_equal - Check whether the or of two bitmaps is equal to a third 337 * @src1: Pointer to bitmap 1 338 * @src2: Pointer to bitmap 2 will be or'ed with bitmap 1 339 * @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2 340 * @nbits: number of bits in each of these bitmaps 341 * 342 * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise 343 */ 344 static inline bool bitmap_or_equal(const unsigned long *src1, 345 const unsigned long *src2, 346 const unsigned long *src3, 347 unsigned int nbits) 348 { 349 if (!small_const_nbits(nbits)) 350 return __bitmap_or_equal(src1, src2, src3, nbits); 351 352 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits)); 353 } 354 355 static inline int bitmap_intersects(const unsigned long *src1, 356 const unsigned long *src2, unsigned int nbits) 357 { 358 if (small_const_nbits(nbits)) 359 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0; 360 else 361 return __bitmap_intersects(src1, src2, nbits); 362 } 363 364 static inline int bitmap_subset(const unsigned long *src1, 365 const unsigned long *src2, unsigned int nbits) 366 { 367 if (small_const_nbits(nbits)) 368 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits)); 369 else 370 return __bitmap_subset(src1, src2, nbits); 371 } 372 373 static inline bool bitmap_empty(const unsigned long *src, unsigned nbits) 374 { 375 if (small_const_nbits(nbits)) 376 return ! (*src & BITMAP_LAST_WORD_MASK(nbits)); 377 378 return find_first_bit(src, nbits) == nbits; 379 } 380 381 static inline bool bitmap_full(const unsigned long *src, unsigned int nbits) 382 { 383 if (small_const_nbits(nbits)) 384 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits)); 385 386 return find_first_zero_bit(src, nbits) == nbits; 387 } 388 389 static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits) 390 { 391 if (small_const_nbits(nbits)) 392 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)); 393 return __bitmap_weight(src, nbits); 394 } 395 396 static __always_inline void bitmap_set(unsigned long *map, unsigned int start, 397 unsigned int nbits) 398 { 399 if (__builtin_constant_p(nbits) && nbits == 1) 400 __set_bit(start, map); 401 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) && 402 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) && 403 __builtin_constant_p(nbits & BITMAP_MEM_MASK) && 404 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) 405 memset((char *)map + start / 8, 0xff, nbits / 8); 406 else 407 __bitmap_set(map, start, nbits); 408 } 409 410 static __always_inline void bitmap_clear(unsigned long *map, unsigned int start, 411 unsigned int nbits) 412 { 413 if (__builtin_constant_p(nbits) && nbits == 1) 414 __clear_bit(start, map); 415 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) && 416 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) && 417 __builtin_constant_p(nbits & BITMAP_MEM_MASK) && 418 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) 419 memset((char *)map + start / 8, 0, nbits / 8); 420 else 421 __bitmap_clear(map, start, nbits); 422 } 423 424 static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src, 425 unsigned int shift, unsigned int nbits) 426 { 427 if (small_const_nbits(nbits)) 428 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift; 429 else 430 __bitmap_shift_right(dst, src, shift, nbits); 431 } 432 433 static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src, 434 unsigned int shift, unsigned int nbits) 435 { 436 if (small_const_nbits(nbits)) 437 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits); 438 else 439 __bitmap_shift_left(dst, src, shift, nbits); 440 } 441 442 static inline void bitmap_replace(unsigned long *dst, 443 const unsigned long *old, 444 const unsigned long *new, 445 const unsigned long *mask, 446 unsigned int nbits) 447 { 448 if (small_const_nbits(nbits)) 449 *dst = (*old & ~(*mask)) | (*new & *mask); 450 else 451 __bitmap_replace(dst, old, new, mask, nbits); 452 } 453 454 static inline void bitmap_next_clear_region(unsigned long *bitmap, 455 unsigned int *rs, unsigned int *re, 456 unsigned int end) 457 { 458 *rs = find_next_zero_bit(bitmap, end, *rs); 459 *re = find_next_bit(bitmap, end, *rs + 1); 460 } 461 462 static inline void bitmap_next_set_region(unsigned long *bitmap, 463 unsigned int *rs, unsigned int *re, 464 unsigned int end) 465 { 466 *rs = find_next_bit(bitmap, end, *rs); 467 *re = find_next_zero_bit(bitmap, end, *rs + 1); 468 } 469 470 /* 471 * Bitmap region iterators. Iterates over the bitmap between [@start, @end). 472 * @rs and @re should be integer variables and will be set to start and end 473 * index of the current clear or set region. 474 */ 475 #define bitmap_for_each_clear_region(bitmap, rs, re, start, end) \ 476 for ((rs) = (start), \ 477 bitmap_next_clear_region((bitmap), &(rs), &(re), (end)); \ 478 (rs) < (re); \ 479 (rs) = (re) + 1, \ 480 bitmap_next_clear_region((bitmap), &(rs), &(re), (end))) 481 482 #define bitmap_for_each_set_region(bitmap, rs, re, start, end) \ 483 for ((rs) = (start), \ 484 bitmap_next_set_region((bitmap), &(rs), &(re), (end)); \ 485 (rs) < (re); \ 486 (rs) = (re) + 1, \ 487 bitmap_next_set_region((bitmap), &(rs), &(re), (end))) 488 489 /** 490 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap. 491 * @n: u64 value 492 * 493 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit 494 * integers in 32-bit environment, and 64-bit integers in 64-bit one. 495 * 496 * There are four combinations of endianness and length of the word in linux 497 * ABIs: LE64, BE64, LE32 and BE32. 498 * 499 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in 500 * bitmaps and therefore don't require any special handling. 501 * 502 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory 503 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the 504 * other hand is represented as an array of 32-bit words and the position of 505 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that 506 * word. For example, bit #42 is located at 10th position of 2nd word. 507 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit 508 * values in memory as it usually does. But for BE we need to swap hi and lo 509 * words manually. 510 * 511 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and 512 * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps 513 * hi and lo words, as is expected by bitmap. 514 */ 515 #if __BITS_PER_LONG == 64 516 #define BITMAP_FROM_U64(n) (n) 517 #else 518 #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \ 519 ((unsigned long) ((u64)(n) >> 32)) 520 #endif 521 522 /** 523 * bitmap_from_u64 - Check and swap words within u64. 524 * @mask: source bitmap 525 * @dst: destination bitmap 526 * 527 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]`` 528 * to read u64 mask, we will get the wrong word. 529 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits, 530 * but we expect the lower 32-bits of u64. 531 */ 532 static inline void bitmap_from_u64(unsigned long *dst, u64 mask) 533 { 534 dst[0] = mask & ULONG_MAX; 535 536 if (sizeof(mask) > sizeof(unsigned long)) 537 dst[1] = mask >> 32; 538 } 539 540 /** 541 * bitmap_get_value8 - get an 8-bit value within a memory region 542 * @map: address to the bitmap memory region 543 * @start: bit offset of the 8-bit value; must be a multiple of 8 544 * 545 * Returns the 8-bit value located at the @start bit offset within the @src 546 * memory region. 547 */ 548 static inline unsigned long bitmap_get_value8(const unsigned long *map, 549 unsigned long start) 550 { 551 const size_t index = BIT_WORD(start); 552 const unsigned long offset = start % BITS_PER_LONG; 553 554 return (map[index] >> offset) & 0xFF; 555 } 556 557 /** 558 * bitmap_set_value8 - set an 8-bit value within a memory region 559 * @map: address to the bitmap memory region 560 * @value: the 8-bit value; values wider than 8 bits may clobber bitmap 561 * @start: bit offset of the 8-bit value; must be a multiple of 8 562 */ 563 static inline void bitmap_set_value8(unsigned long *map, unsigned long value, 564 unsigned long start) 565 { 566 const size_t index = BIT_WORD(start); 567 const unsigned long offset = start % BITS_PER_LONG; 568 569 map[index] &= ~(0xFFUL << offset); 570 map[index] |= value << offset; 571 } 572 573 #endif /* __ASSEMBLY__ */ 574 575 #endif /* __LINUX_BITMAP_H */ 576