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