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 * bitmap_read(map, start, nbits) Read an nbits-sized value from
81 * map at start
82 * bitmap_write(map, value, start, nbits) Write an nbits-sized value to
83 * map at start
84 *
85 * Note, bitmap_zero() and bitmap_fill() operate over the region of
86 * unsigned longs, that is, bits behind bitmap till the unsigned long
87 * boundary will be zeroed or filled as well. Consider to use
88 * bitmap_clear() or bitmap_set() to make explicit zeroing or filling
89 * respectively.
90 */
91
92 /**
93 * DOC: bitmap bitops
94 *
95 * Also the following operations in asm/bitops.h apply to bitmaps.::
96 *
97 * set_bit(bit, addr) *addr |= bit
98 * clear_bit(bit, addr) *addr &= ~bit
99 * change_bit(bit, addr) *addr ^= bit
100 * test_bit(bit, addr) Is bit set in *addr?
101 * test_and_set_bit(bit, addr) Set bit and return old value
102 * test_and_clear_bit(bit, addr) Clear bit and return old value
103 * test_and_change_bit(bit, addr) Change bit and return old value
104 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
105 * find_first_bit(addr, nbits) Position first set bit in *addr
106 * find_next_zero_bit(addr, nbits, bit)
107 * Position next zero bit in *addr >= bit
108 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
109 * find_next_and_bit(addr1, addr2, nbits, bit)
110 * Same as find_next_bit, but in
111 * (*addr1 & *addr2)
112 *
113 */
114
115 /**
116 * DOC: declare bitmap
117 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
118 * to declare an array named 'name' of just enough unsigned longs to
119 * contain all bit positions from 0 to 'bits' - 1.
120 */
121
122 /*
123 * Allocation and deallocation of bitmap.
124 * Provided in lib/bitmap.c to avoid circular dependency.
125 */
126 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
127 unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
128 unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
129 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
130 void bitmap_free(const unsigned long *bitmap);
131
132 /* Managed variants of the above. */
133 unsigned long *devm_bitmap_alloc(struct device *dev,
134 unsigned int nbits, gfp_t flags);
135 unsigned long *devm_bitmap_zalloc(struct device *dev,
136 unsigned int nbits, gfp_t flags);
137
138 /*
139 * lib/bitmap.c provides these functions:
140 */
141
142 bool __bitmap_equal(const unsigned long *bitmap1,
143 const unsigned long *bitmap2, unsigned int nbits);
144 bool __pure __bitmap_or_equal(const unsigned long *src1,
145 const unsigned long *src2,
146 const unsigned long *src3,
147 unsigned int nbits);
148 void __bitmap_complement(unsigned long *dst, const unsigned long *src,
149 unsigned int nbits);
150 void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
151 unsigned int shift, unsigned int nbits);
152 void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
153 unsigned int shift, unsigned int nbits);
154 void bitmap_cut(unsigned long *dst, const unsigned long *src,
155 unsigned int first, unsigned int cut, unsigned int nbits);
156 bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
157 const unsigned long *bitmap2, unsigned int nbits);
158 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
159 const unsigned long *bitmap2, unsigned int nbits);
160 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
161 const unsigned long *bitmap2, unsigned int nbits);
162 bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
163 const unsigned long *bitmap2, unsigned int nbits);
164 void __bitmap_replace(unsigned long *dst,
165 const unsigned long *old, const unsigned long *new,
166 const unsigned long *mask, unsigned int nbits);
167 bool __bitmap_intersects(const unsigned long *bitmap1,
168 const unsigned long *bitmap2, unsigned int nbits);
169 bool __bitmap_subset(const unsigned long *bitmap1,
170 const unsigned long *bitmap2, unsigned int nbits);
171 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
172 unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
173 const unsigned long *bitmap2, unsigned int nbits);
174 void __bitmap_set(unsigned long *map, unsigned int start, int len);
175 void __bitmap_clear(unsigned long *map, unsigned int start, int len);
176
177 unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
178 unsigned long size,
179 unsigned long start,
180 unsigned int nr,
181 unsigned long align_mask,
182 unsigned long align_offset);
183
184 /**
185 * bitmap_find_next_zero_area - find a contiguous aligned zero area
186 * @map: The address to base the search on
187 * @size: The bitmap size in bits
188 * @start: The bitnumber to start searching at
189 * @nr: The number of zeroed bits we're looking for
190 * @align_mask: Alignment mask for zero area
191 *
192 * The @align_mask should be one less than a power of 2; the effect is that
193 * the bit offset of all zero areas this function finds is multiples of that
194 * power of 2. A @align_mask of 0 means no alignment is required.
195 */
196 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)197 bitmap_find_next_zero_area(unsigned long *map,
198 unsigned long size,
199 unsigned long start,
200 unsigned int nr,
201 unsigned long align_mask)
202 {
203 return bitmap_find_next_zero_area_off(map, size, start, nr,
204 align_mask, 0);
205 }
206
207 int bitmap_parse(const char *buf, unsigned int buflen,
208 unsigned long *dst, int nbits);
209 int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
210 unsigned long *dst, int nbits);
211 int bitmap_parselist(const char *buf, unsigned long *maskp,
212 int nmaskbits);
213 int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
214 unsigned long *dst, int nbits);
215 void bitmap_remap(unsigned long *dst, const unsigned long *src,
216 const unsigned long *old, const unsigned long *new, unsigned int nbits);
217 int bitmap_bitremap(int oldbit,
218 const unsigned long *old, const unsigned long *new, int bits);
219 void bitmap_onto(unsigned long *dst, const unsigned long *orig,
220 const unsigned long *relmap, unsigned int bits);
221 void bitmap_fold(unsigned long *dst, const unsigned long *orig,
222 unsigned int sz, unsigned int nbits);
223 int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
224 void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
225 int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
226
227 #ifdef __BIG_ENDIAN
228 void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
229 #else
230 #define bitmap_copy_le bitmap_copy
231 #endif
232 int bitmap_print_to_pagebuf(bool list, char *buf,
233 const unsigned long *maskp, int nmaskbits);
234
235 extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
236 int nmaskbits, loff_t off, size_t count);
237
238 extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
239 int nmaskbits, loff_t off, size_t count);
240
241 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
242 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
243
244 #define bitmap_size(nbits) (ALIGN(nbits, BITS_PER_LONG) / BITS_PER_BYTE)
245
bitmap_zero(unsigned long * dst,unsigned int nbits)246 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
247 {
248 unsigned int len = bitmap_size(nbits);
249
250 if (small_const_nbits(nbits))
251 *dst = 0;
252 else
253 memset(dst, 0, len);
254 }
255
bitmap_fill(unsigned long * dst,unsigned int nbits)256 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
257 {
258 unsigned int len = bitmap_size(nbits);
259
260 if (small_const_nbits(nbits))
261 *dst = ~0UL;
262 else
263 memset(dst, 0xff, len);
264 }
265
bitmap_copy(unsigned long * dst,const unsigned long * src,unsigned int nbits)266 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
267 unsigned int nbits)
268 {
269 unsigned int len = bitmap_size(nbits);
270
271 if (small_const_nbits(nbits))
272 *dst = *src;
273 else
274 memcpy(dst, src, len);
275 }
276
277 /*
278 * Copy bitmap and clear tail bits in last word.
279 */
bitmap_copy_clear_tail(unsigned long * dst,const unsigned long * src,unsigned int nbits)280 static inline void bitmap_copy_clear_tail(unsigned long *dst,
281 const unsigned long *src, unsigned int nbits)
282 {
283 bitmap_copy(dst, src, nbits);
284 if (nbits % BITS_PER_LONG)
285 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
286 }
287
bitmap_copy_and_extend(unsigned long * to,const unsigned long * from,unsigned int count,unsigned int size)288 static inline void bitmap_copy_and_extend(unsigned long *to,
289 const unsigned long *from,
290 unsigned int count, unsigned int size)
291 {
292 unsigned int copy = BITS_TO_LONGS(count);
293
294 memcpy(to, from, copy * sizeof(long));
295 if (count % BITS_PER_LONG)
296 to[copy - 1] &= BITMAP_LAST_WORD_MASK(count);
297 memset(to + copy, 0, bitmap_size(size) - copy * sizeof(long));
298 }
299
300 /*
301 * On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64
302 * machines the order of hi and lo parts of numbers match the bitmap structure.
303 * In both cases conversion is not needed when copying data from/to arrays of
304 * u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead
305 * to out-of-bound access. To avoid that, both LE and BE variants of 64-bit
306 * architectures are not using bitmap_copy_clear_tail().
307 */
308 #if BITS_PER_LONG == 64
309 void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
310 unsigned int nbits);
311 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
312 unsigned int nbits);
313 #else
314 #define bitmap_from_arr32(bitmap, buf, nbits) \
315 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
316 (const unsigned long *) (buf), (nbits))
317 #define bitmap_to_arr32(buf, bitmap, nbits) \
318 bitmap_copy_clear_tail((unsigned long *) (buf), \
319 (const unsigned long *) (bitmap), (nbits))
320 #endif
321
322 /*
323 * On 64-bit systems bitmaps are represented as u64 arrays internally. So,
324 * the conversion is not needed when copying data from/to arrays of u64.
325 */
326 #if BITS_PER_LONG == 32
327 void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits);
328 void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits);
329 #else
330 #define bitmap_from_arr64(bitmap, buf, nbits) \
331 bitmap_copy_clear_tail((unsigned long *)(bitmap), (const unsigned long *)(buf), (nbits))
332 #define bitmap_to_arr64(buf, bitmap, nbits) \
333 bitmap_copy_clear_tail((unsigned long *)(buf), (const unsigned long *)(bitmap), (nbits))
334 #endif
335
bitmap_and(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,unsigned int nbits)336 static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1,
337 const unsigned long *src2, unsigned int nbits)
338 {
339 if (small_const_nbits(nbits))
340 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
341 return __bitmap_and(dst, src1, src2, nbits);
342 }
343
bitmap_or(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,unsigned int nbits)344 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
345 const unsigned long *src2, unsigned int nbits)
346 {
347 if (small_const_nbits(nbits))
348 *dst = *src1 | *src2;
349 else
350 __bitmap_or(dst, src1, src2, nbits);
351 }
352
bitmap_xor(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,unsigned int nbits)353 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
354 const unsigned long *src2, unsigned int nbits)
355 {
356 if (small_const_nbits(nbits))
357 *dst = *src1 ^ *src2;
358 else
359 __bitmap_xor(dst, src1, src2, nbits);
360 }
361
bitmap_andnot(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,unsigned int nbits)362 static inline bool bitmap_andnot(unsigned long *dst, const unsigned long *src1,
363 const unsigned long *src2, unsigned int nbits)
364 {
365 if (small_const_nbits(nbits))
366 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
367 return __bitmap_andnot(dst, src1, src2, nbits);
368 }
369
bitmap_complement(unsigned long * dst,const unsigned long * src,unsigned int nbits)370 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
371 unsigned int nbits)
372 {
373 if (small_const_nbits(nbits))
374 *dst = ~(*src);
375 else
376 __bitmap_complement(dst, src, nbits);
377 }
378
379 #ifdef __LITTLE_ENDIAN
380 #define BITMAP_MEM_ALIGNMENT 8
381 #else
382 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
383 #endif
384 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
385
bitmap_equal(const unsigned long * src1,const unsigned long * src2,unsigned int nbits)386 static inline bool bitmap_equal(const unsigned long *src1,
387 const unsigned long *src2, unsigned int nbits)
388 {
389 if (small_const_nbits(nbits))
390 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
391 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
392 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
393 return !memcmp(src1, src2, nbits / 8);
394 return __bitmap_equal(src1, src2, nbits);
395 }
396
397 /**
398 * bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
399 * @src1: Pointer to bitmap 1
400 * @src2: Pointer to bitmap 2 will be or'ed with bitmap 1
401 * @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2
402 * @nbits: number of bits in each of these bitmaps
403 *
404 * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
405 */
bitmap_or_equal(const unsigned long * src1,const unsigned long * src2,const unsigned long * src3,unsigned int nbits)406 static inline bool bitmap_or_equal(const unsigned long *src1,
407 const unsigned long *src2,
408 const unsigned long *src3,
409 unsigned int nbits)
410 {
411 if (!small_const_nbits(nbits))
412 return __bitmap_or_equal(src1, src2, src3, nbits);
413
414 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
415 }
416
bitmap_intersects(const unsigned long * src1,const unsigned long * src2,unsigned int nbits)417 static inline bool bitmap_intersects(const unsigned long *src1,
418 const unsigned long *src2,
419 unsigned int nbits)
420 {
421 if (small_const_nbits(nbits))
422 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
423 else
424 return __bitmap_intersects(src1, src2, nbits);
425 }
426
bitmap_subset(const unsigned long * src1,const unsigned long * src2,unsigned int nbits)427 static inline bool bitmap_subset(const unsigned long *src1,
428 const unsigned long *src2, unsigned int nbits)
429 {
430 if (small_const_nbits(nbits))
431 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
432 else
433 return __bitmap_subset(src1, src2, nbits);
434 }
435
bitmap_empty(const unsigned long * src,unsigned nbits)436 static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
437 {
438 if (small_const_nbits(nbits))
439 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
440
441 return find_first_bit(src, nbits) == nbits;
442 }
443
bitmap_full(const unsigned long * src,unsigned int nbits)444 static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
445 {
446 if (small_const_nbits(nbits))
447 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
448
449 return find_first_zero_bit(src, nbits) == nbits;
450 }
451
452 static __always_inline
bitmap_weight(const unsigned long * src,unsigned int nbits)453 unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
454 {
455 if (small_const_nbits(nbits))
456 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
457 return __bitmap_weight(src, nbits);
458 }
459
460 static __always_inline
bitmap_weight_and(const unsigned long * src1,const unsigned long * src2,unsigned int nbits)461 unsigned long bitmap_weight_and(const unsigned long *src1,
462 const unsigned long *src2, unsigned int nbits)
463 {
464 if (small_const_nbits(nbits))
465 return hweight_long(*src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits));
466 return __bitmap_weight_and(src1, src2, nbits);
467 }
468
bitmap_set(unsigned long * map,unsigned int start,unsigned int nbits)469 static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
470 unsigned int nbits)
471 {
472 if (__builtin_constant_p(nbits) && nbits == 1)
473 __set_bit(start, map);
474 else if (small_const_nbits(start + nbits))
475 *map |= GENMASK(start + nbits - 1, start);
476 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
477 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
478 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
479 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
480 memset((char *)map + start / 8, 0xff, nbits / 8);
481 else
482 __bitmap_set(map, start, nbits);
483 }
484
bitmap_clear(unsigned long * map,unsigned int start,unsigned int nbits)485 static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
486 unsigned int nbits)
487 {
488 if (__builtin_constant_p(nbits) && nbits == 1)
489 __clear_bit(start, map);
490 else if (small_const_nbits(start + nbits))
491 *map &= ~GENMASK(start + nbits - 1, start);
492 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
493 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
494 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
495 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
496 memset((char *)map + start / 8, 0, nbits / 8);
497 else
498 __bitmap_clear(map, start, nbits);
499 }
500
bitmap_shift_right(unsigned long * dst,const unsigned long * src,unsigned int shift,unsigned int nbits)501 static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
502 unsigned int shift, unsigned int nbits)
503 {
504 if (small_const_nbits(nbits))
505 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
506 else
507 __bitmap_shift_right(dst, src, shift, nbits);
508 }
509
bitmap_shift_left(unsigned long * dst,const unsigned long * src,unsigned int shift,unsigned int nbits)510 static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
511 unsigned int shift, unsigned int nbits)
512 {
513 if (small_const_nbits(nbits))
514 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
515 else
516 __bitmap_shift_left(dst, src, shift, nbits);
517 }
518
bitmap_replace(unsigned long * dst,const unsigned long * old,const unsigned long * new,const unsigned long * mask,unsigned int nbits)519 static inline void bitmap_replace(unsigned long *dst,
520 const unsigned long *old,
521 const unsigned long *new,
522 const unsigned long *mask,
523 unsigned int nbits)
524 {
525 if (small_const_nbits(nbits))
526 *dst = (*old & ~(*mask)) | (*new & *mask);
527 else
528 __bitmap_replace(dst, old, new, mask, nbits);
529 }
530
bitmap_next_set_region(unsigned long * bitmap,unsigned int * rs,unsigned int * re,unsigned int end)531 static inline void bitmap_next_set_region(unsigned long *bitmap,
532 unsigned int *rs, unsigned int *re,
533 unsigned int end)
534 {
535 *rs = find_next_bit(bitmap, end, *rs);
536 *re = find_next_zero_bit(bitmap, end, *rs + 1);
537 }
538
539 /**
540 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
541 * @n: u64 value
542 *
543 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
544 * integers in 32-bit environment, and 64-bit integers in 64-bit one.
545 *
546 * There are four combinations of endianness and length of the word in linux
547 * ABIs: LE64, BE64, LE32 and BE32.
548 *
549 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
550 * bitmaps and therefore don't require any special handling.
551 *
552 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
553 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
554 * other hand is represented as an array of 32-bit words and the position of
555 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
556 * word. For example, bit #42 is located at 10th position of 2nd word.
557 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
558 * values in memory as it usually does. But for BE we need to swap hi and lo
559 * words manually.
560 *
561 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
562 * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
563 * hi and lo words, as is expected by bitmap.
564 */
565 #if __BITS_PER_LONG == 64
566 #define BITMAP_FROM_U64(n) (n)
567 #else
568 #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
569 ((unsigned long) ((u64)(n) >> 32))
570 #endif
571
572 /**
573 * bitmap_from_u64 - Check and swap words within u64.
574 * @mask: source bitmap
575 * @dst: destination bitmap
576 *
577 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
578 * to read u64 mask, we will get the wrong word.
579 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
580 * but we expect the lower 32-bits of u64.
581 */
bitmap_from_u64(unsigned long * dst,u64 mask)582 static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
583 {
584 bitmap_from_arr64(dst, &mask, 64);
585 }
586
587 /**
588 * bitmap_get_value8 - get an 8-bit value within a memory region
589 * @map: address to the bitmap memory region
590 * @start: bit offset of the 8-bit value; must be a multiple of 8
591 *
592 * Returns the 8-bit value located at the @start bit offset within the @src
593 * memory region.
594 */
bitmap_get_value8(const unsigned long * map,unsigned long start)595 static inline unsigned long bitmap_get_value8(const unsigned long *map,
596 unsigned long start)
597 {
598 const size_t index = BIT_WORD(start);
599 const unsigned long offset = start % BITS_PER_LONG;
600
601 return (map[index] >> offset) & 0xFF;
602 }
603
604 /**
605 * bitmap_set_value8 - set an 8-bit value within a memory region
606 * @map: address to the bitmap memory region
607 * @value: the 8-bit value; values wider than 8 bits may clobber bitmap
608 * @start: bit offset of the 8-bit value; must be a multiple of 8
609 */
bitmap_set_value8(unsigned long * map,unsigned long value,unsigned long start)610 static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
611 unsigned long start)
612 {
613 const size_t index = BIT_WORD(start);
614 const unsigned long offset = start % BITS_PER_LONG;
615
616 map[index] &= ~(0xFFUL << offset);
617 map[index] |= value << offset;
618 }
619
620 /**
621 * bitmap_read - read a value of n-bits from the memory region
622 * @map: address to the bitmap memory region
623 * @start: bit offset of the n-bit value
624 * @nbits: size of value in bits, nonzero, up to BITS_PER_LONG
625 *
626 * Returns: value of @nbits bits located at the @start bit offset within the
627 * @map memory region. For @nbits = 0 and @nbits > BITS_PER_LONG the return
628 * value is undefined.
629 */
bitmap_read(const unsigned long * map,unsigned long start,unsigned long nbits)630 static inline unsigned long bitmap_read(const unsigned long *map,
631 unsigned long start,
632 unsigned long nbits)
633 {
634 size_t index = BIT_WORD(start);
635 unsigned long offset = start % BITS_PER_LONG;
636 unsigned long space = BITS_PER_LONG - offset;
637 unsigned long value_low, value_high;
638
639 if (unlikely(!nbits || nbits > BITS_PER_LONG))
640 return 0;
641
642 if (space >= nbits)
643 return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);
644
645 value_low = map[index] & BITMAP_FIRST_WORD_MASK(start);
646 value_high = map[index + 1] & BITMAP_LAST_WORD_MASK(start + nbits);
647 return (value_low >> offset) | (value_high << space);
648 }
649
650 /**
651 * bitmap_write - write n-bit value within a memory region
652 * @map: address to the bitmap memory region
653 * @value: value to write, clamped to nbits
654 * @start: bit offset of the n-bit value
655 * @nbits: size of value in bits, nonzero, up to BITS_PER_LONG.
656 *
657 * bitmap_write() behaves as-if implemented as @nbits calls of __assign_bit(),
658 * i.e. bits beyond @nbits are ignored:
659 *
660 * for (bit = 0; bit < nbits; bit++)
661 * __assign_bit(start + bit, bitmap, val & BIT(bit));
662 *
663 * For @nbits == 0 and @nbits > BITS_PER_LONG no writes are performed.
664 */
bitmap_write(unsigned long * map,unsigned long value,unsigned long start,unsigned long nbits)665 static inline void bitmap_write(unsigned long *map, unsigned long value,
666 unsigned long start, unsigned long nbits)
667 {
668 size_t index;
669 unsigned long offset;
670 unsigned long space;
671 unsigned long mask;
672 bool fit;
673
674 if (unlikely(!nbits || nbits > BITS_PER_LONG))
675 return;
676
677 mask = BITMAP_LAST_WORD_MASK(nbits);
678 value &= mask;
679 offset = start % BITS_PER_LONG;
680 space = BITS_PER_LONG - offset;
681 fit = space >= nbits;
682 index = BIT_WORD(start);
683
684 map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
685 map[index] |= value << offset;
686 if (fit)
687 return;
688
689 map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
690 map[index + 1] |= (value >> space);
691 }
692
693 #endif /* __ASSEMBLY__ */
694
695 #endif /* __LINUX_BITMAP_H */
696