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