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