Merge tag 'rproc-v6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/remoteproc...
[linux-2.6-block.git] / include / linux / bitmap.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __LINUX_BITMAP_H
3#define __LINUX_BITMAP_H
4
5#ifndef __ASSEMBLY__
6
08c5188e 7#include <linux/align.h>
1da177e4 8#include <linux/bitops.h>
d12a8284 9#include <linux/cleanup.h>
6cb42f91 10#include <linux/errno.h>
47d8c156 11#include <linux/find.h>
08c5188e 12#include <linux/limits.h>
c13656b9
BG
13#include <linux/string.h>
14#include <linux/types.h>
aae06fc1 15#include <linux/bitmap-str.h>
1da177e4 16
e829c2e4
BG
17struct device;
18
1da177e4
LT
19/*
20 * bitmaps provide bit arrays that consume one or more unsigned
21 * longs. The bitmap interface and available operations are listed
22 * here, in bitmap.h
23 *
24 * Function implementations generic to all architectures are in
25 * lib/bitmap.c. Functions implementations that are architecture
26 * specific are in various include/asm-<arch>/bitops.h headers
27 * and other arch/<arch> specific files.
28 *
29 * See lib/bitmap.c for more details.
30 */
31
7d7363e4
RD
32/**
33 * DOC: bitmap overview
34 *
1da177e4
LT
35 * The available bitmap operations and their rough meaning in the
36 * case that the bitmap is a single unsigned long are thus:
37 *
41e7b166
RV
38 * The generated code is more efficient when nbits is known at
39 * compile-time and at most BITS_PER_LONG.
08cd3657 40 *
7d7363e4
RD
41 * ::
42 *
43 * bitmap_zero(dst, nbits) *dst = 0UL
44 * bitmap_fill(dst, nbits) *dst = ~0UL
45 * bitmap_copy(dst, src, nbits) *dst = *src
46 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
47 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
48 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
49 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
50 * bitmap_complement(dst, src, nbits) *dst = ~(*src)
51 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
52 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
53 * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
54 * bitmap_empty(src, nbits) Are all bits zero in *src?
55 * bitmap_full(src, nbits) Are all bits set in *src?
56 * bitmap_weight(src, nbits) Hamming Weight: number set bits
24291caf 57 * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap
c1f5204e 58 * bitmap_weight_andnot(src1, src2, nbits) Hamming Weight of andnot'ed bitmap
7d7363e4
RD
59 * bitmap_set(dst, pos, nbits) Set specified bit area
60 * bitmap_clear(dst, pos, nbits) Clear specified bit area
61 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
780d2a9c 62 * bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above
7d7363e4
RD
63 * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
64 * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
20927671 65 * bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
30544ed5 66 * bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask)
de5f8433
AS
67 * bitmap_scatter(dst, src, mask, nbits) *dst = map(dense, sparse)(src)
68 * bitmap_gather(dst, src, mask, nbits) *dst = map(sparse, dense)(src)
7d7363e4
RD
69 * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
70 * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
71 * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
72 * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
73 * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
74 * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
75 * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
76 * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
77 * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
78 * bitmap_release_region(bitmap, pos, order) Free specified bit region
79 * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
c724f193 80 * bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
ba1afa67 81 * bitmap_from_arr64(dst, buf, nbits) Copy nbits from u64[] buf to dst
c724f193 82 * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
0a97953f 83 * bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst
169c474f
WBG
84 * bitmap_get_value8(map, start) Get 8bit value from map at start
85 * bitmap_set_value8(map, value, start) Set 8bit value to map at start
7d7363e4 86 *
334cfa48
AS
87 * Note, bitmap_zero() and bitmap_fill() operate over the region of
88 * unsigned longs, that is, bits behind bitmap till the unsigned long
89 * boundary will be zeroed or filled as well. Consider to use
90 * bitmap_clear() or bitmap_set() to make explicit zeroing or filling
91 * respectively.
1da177e4
LT
92 */
93
7d7363e4
RD
94/**
95 * DOC: bitmap bitops
96 *
97 * Also the following operations in asm/bitops.h apply to bitmaps.::
98 *
99 * set_bit(bit, addr) *addr |= bit
100 * clear_bit(bit, addr) *addr &= ~bit
101 * change_bit(bit, addr) *addr ^= bit
102 * test_bit(bit, addr) Is bit set in *addr?
103 * test_and_set_bit(bit, addr) Set bit and return old value
104 * test_and_clear_bit(bit, addr) Clear bit and return old value
105 * test_and_change_bit(bit, addr) Change bit and return old value
106 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
107 * find_first_bit(addr, nbits) Position first set bit in *addr
0ade34c3
CC
108 * find_next_zero_bit(addr, nbits, bit)
109 * Position next zero bit in *addr >= bit
7d7363e4 110 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
0ade34c3
CC
111 * find_next_and_bit(addr1, addr2, nbits, bit)
112 * Same as find_next_bit, but in
113 * (*addr1 & *addr2)
1da177e4 114 *
1da177e4
LT
115 */
116
7d7363e4
RD
117/**
118 * DOC: declare bitmap
1da177e4
LT
119 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
120 * to declare an array named 'name' of just enough unsigned longs to
121 * contain all bit positions from 0 to 'bits' - 1.
122 */
123
c42b65e3
AS
124/*
125 * Allocation and deallocation of bitmap.
126 * Provided in lib/bitmap.c to avoid circular dependency.
127 */
98635b29
BG
128unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
129unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
7529cc7f
TT
130unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
131unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
98635b29 132void bitmap_free(const unsigned long *bitmap);
c42b65e3 133
d12a8284
BG
134DEFINE_FREE(bitmap, unsigned long *, if (_T) bitmap_free(_T))
135
e829c2e4
BG
136/* Managed variants of the above. */
137unsigned long *devm_bitmap_alloc(struct device *dev,
138 unsigned int nbits, gfp_t flags);
139unsigned long *devm_bitmap_zalloc(struct device *dev,
140 unsigned int nbits, gfp_t flags);
141
1da177e4
LT
142/*
143 * lib/bitmap.c provides these functions:
144 */
145
005f1700
KC
146bool __bitmap_equal(const unsigned long *bitmap1,
147 const unsigned long *bitmap2, unsigned int nbits);
98635b29
BG
148bool __pure __bitmap_or_equal(const unsigned long *src1,
149 const unsigned long *src2,
150 const unsigned long *src3,
151 unsigned int nbits);
152void __bitmap_complement(unsigned long *dst, const unsigned long *src,
153 unsigned int nbits);
154void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
155 unsigned int shift, unsigned int nbits);
156void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
157 unsigned int shift, unsigned int nbits);
158void bitmap_cut(unsigned long *dst, const unsigned long *src,
159 unsigned int first, unsigned int cut, unsigned int nbits);
e2863a78 160bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
98635b29
BG
161 const unsigned long *bitmap2, unsigned int nbits);
162void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
163 const unsigned long *bitmap2, unsigned int nbits);
164void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
165 const unsigned long *bitmap2, unsigned int nbits);
e2863a78 166bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
98635b29
BG
167 const unsigned long *bitmap2, unsigned int nbits);
168void __bitmap_replace(unsigned long *dst,
169 const unsigned long *old, const unsigned long *new,
170 const unsigned long *mask, unsigned int nbits);
005f1700
KC
171bool __bitmap_intersects(const unsigned long *bitmap1,
172 const unsigned long *bitmap2, unsigned int nbits);
173bool __bitmap_subset(const unsigned long *bitmap1,
174 const unsigned long *bitmap2, unsigned int nbits);
4e23eeeb 175unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
24291caf
YN
176unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
177 const unsigned long *bitmap2, unsigned int nbits);
c1f5204e
YN
178unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
179 const unsigned long *bitmap2, unsigned int nbits);
98635b29
BG
180void __bitmap_set(unsigned long *map, unsigned int start, int len);
181void __bitmap_clear(unsigned long *map, unsigned int start, int len);
5e19b013 182
98635b29
BG
183unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
184 unsigned long size,
185 unsigned long start,
186 unsigned int nr,
187 unsigned long align_mask,
188 unsigned long align_offset);
5e19b013
MN
189
190/**
191 * bitmap_find_next_zero_area - find a contiguous aligned zero area
192 * @map: The address to base the search on
193 * @size: The bitmap size in bits
194 * @start: The bitnumber to start searching at
195 * @nr: The number of zeroed bits we're looking for
196 * @align_mask: Alignment mask for zero area
197 *
198 * The @align_mask should be one less than a power of 2; the effect is that
199 * the bit offset of all zero areas this function finds is multiples of that
200 * power of 2. A @align_mask of 0 means no alignment is required.
201 */
202static inline unsigned long
203bitmap_find_next_zero_area(unsigned long *map,
204 unsigned long size,
205 unsigned long start,
206 unsigned int nr,
207 unsigned long align_mask)
208{
209 return bitmap_find_next_zero_area_off(map, size, start, nr,
210 align_mask, 0);
211}
c1a2a962 212
98635b29 213void bitmap_remap(unsigned long *dst, const unsigned long *src,
9814ec13 214 const unsigned long *old, const unsigned long *new, unsigned int nbits);
98635b29 215int bitmap_bitremap(int oldbit,
fb5eeeee 216 const unsigned long *old, const unsigned long *new, int bits);
98635b29 217void bitmap_onto(unsigned long *dst, const unsigned long *orig,
eb569883 218 const unsigned long *relmap, unsigned int bits);
98635b29 219void bitmap_fold(unsigned long *dst, const unsigned long *orig,
b26ad583 220 unsigned int sz, unsigned int nbits);
3aa56885 221
89c1e79e
RV
222#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
223#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
1da177e4 224
8b4daad5 225static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
1da177e4 226{
c8cebc55 227 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
3e7e5baa
AL
228
229 if (small_const_nbits(nbits))
230 *dst = 0;
231 else
232 memset(dst, 0, len);
1da177e4
LT
233}
234
8b4daad5 235static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
1da177e4 236{
c8cebc55 237 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
3e7e5baa
AL
238
239 if (small_const_nbits(nbits))
240 *dst = ~0UL;
241 else
242 memset(dst, 0xff, len);
1da177e4
LT
243}
244
245static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
8b4daad5 246 unsigned int nbits)
1da177e4 247{
c8cebc55 248 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
3e7e5baa
AL
249
250 if (small_const_nbits(nbits))
251 *dst = *src;
252 else
253 memcpy(dst, src, len);
1da177e4
LT
254}
255
c724f193
YN
256/*
257 * Copy bitmap and clear tail bits in last word.
258 */
259static inline void bitmap_copy_clear_tail(unsigned long *dst,
260 const unsigned long *src, unsigned int nbits)
261{
262 bitmap_copy(dst, src, nbits);
263 if (nbits % BITS_PER_LONG)
264 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
265}
266
267/*
e041e0ac
YN
268 * On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64
269 * machines the order of hi and lo parts of numbers match the bitmap structure.
270 * In both cases conversion is not needed when copying data from/to arrays of
271 * u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead
272 * to out-of-bound access. To avoid that, both LE and BE variants of 64-bit
273 * architectures are not using bitmap_copy_clear_tail().
c724f193
YN
274 */
275#if BITS_PER_LONG == 64
98635b29 276void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
c724f193 277 unsigned int nbits);
98635b29 278void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
c724f193
YN
279 unsigned int nbits);
280#else
281#define bitmap_from_arr32(bitmap, buf, nbits) \
282 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
283 (const unsigned long *) (buf), (nbits))
284#define bitmap_to_arr32(buf, bitmap, nbits) \
285 bitmap_copy_clear_tail((unsigned long *) (buf), \
286 (const unsigned long *) (bitmap), (nbits))
287#endif
288
0a97953f 289/*
c1d2ba10
YN
290 * On 64-bit systems bitmaps are represented as u64 arrays internally. So,
291 * the conversion is not needed when copying data from/to arrays of u64.
0a97953f 292 */
c1d2ba10 293#if BITS_PER_LONG == 32
0a97953f
YN
294void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits);
295void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits);
296#else
297#define bitmap_from_arr64(bitmap, buf, nbits) \
298 bitmap_copy_clear_tail((unsigned long *)(bitmap), (const unsigned long *)(buf), (nbits))
299#define bitmap_to_arr64(buf, bitmap, nbits) \
300 bitmap_copy_clear_tail((unsigned long *)(buf), (const unsigned long *)(bitmap), (nbits))
301#endif
302
e2863a78 303static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1,
2f9305eb 304 const unsigned long *src2, unsigned int nbits)
1da177e4 305{
4b0bc0bc 306 if (small_const_nbits(nbits))
7e5f97d1 307 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
f4b0373b 308 return __bitmap_and(dst, src1, src2, nbits);
1da177e4
LT
309}
310
311static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
2f9305eb 312 const unsigned long *src2, unsigned int nbits)
1da177e4 313{
4b0bc0bc 314 if (small_const_nbits(nbits))
1da177e4
LT
315 *dst = *src1 | *src2;
316 else
317 __bitmap_or(dst, src1, src2, nbits);
318}
319
320static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
2f9305eb 321 const unsigned long *src2, unsigned int nbits)
1da177e4 322{
4b0bc0bc 323 if (small_const_nbits(nbits))
1da177e4
LT
324 *dst = *src1 ^ *src2;
325 else
326 __bitmap_xor(dst, src1, src2, nbits);
327}
328
e2863a78 329static inline bool bitmap_andnot(unsigned long *dst, const unsigned long *src1,
2f9305eb 330 const unsigned long *src2, unsigned int nbits)
1da177e4 331{
4b0bc0bc 332 if (small_const_nbits(nbits))
74e76531 333 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
f4b0373b 334 return __bitmap_andnot(dst, src1, src2, nbits);
1da177e4
LT
335}
336
337static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
3d6684f4 338 unsigned int nbits)
1da177e4 339{
4b0bc0bc 340 if (small_const_nbits(nbits))
65b4ee62 341 *dst = ~(*src);
1da177e4
LT
342 else
343 __bitmap_complement(dst, src, nbits);
344}
345
21035965
OS
346#ifdef __LITTLE_ENDIAN
347#define BITMAP_MEM_ALIGNMENT 8
348#else
349#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
350#endif
351#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
352
005f1700
KC
353static inline bool bitmap_equal(const unsigned long *src1,
354 const unsigned long *src2, unsigned int nbits)
1da177e4 355{
4b0bc0bc 356 if (small_const_nbits(nbits))
4b9d314c 357 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
21035965
OS
358 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
359 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
7dd96816 360 return !memcmp(src1, src2, nbits / 8);
4b9d314c 361 return __bitmap_equal(src1, src2, nbits);
1da177e4
LT
362}
363
b9fa6442 364/**
2a7e582f 365 * bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
b9fa6442
TG
366 * @src1: Pointer to bitmap 1
367 * @src2: Pointer to bitmap 2 will be or'ed with bitmap 1
368 * @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2
2a7e582f 369 * @nbits: number of bits in each of these bitmaps
b9fa6442
TG
370 *
371 * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
372 */
373static inline bool bitmap_or_equal(const unsigned long *src1,
374 const unsigned long *src2,
375 const unsigned long *src3,
376 unsigned int nbits)
377{
378 if (!small_const_nbits(nbits))
379 return __bitmap_or_equal(src1, src2, src3, nbits);
380
381 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
382}
383
005f1700
KC
384static inline bool bitmap_intersects(const unsigned long *src1,
385 const unsigned long *src2,
386 unsigned int nbits)
1da177e4 387{
4b0bc0bc 388 if (small_const_nbits(nbits))
1da177e4
LT
389 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
390 else
391 return __bitmap_intersects(src1, src2, nbits);
392}
393
005f1700
KC
394static inline bool bitmap_subset(const unsigned long *src1,
395 const unsigned long *src2, unsigned int nbits)
1da177e4 396{
4b0bc0bc 397 if (small_const_nbits(nbits))
1da177e4
LT
398 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
399 else
400 return __bitmap_subset(src1, src2, nbits);
401}
402
0bb86779 403static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
1da177e4 404{
4b0bc0bc 405 if (small_const_nbits(nbits))
1da177e4 406 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
2afe27c7
YN
407
408 return find_first_bit(src, nbits) == nbits;
1da177e4
LT
409}
410
0bb86779 411static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
1da177e4 412{
4b0bc0bc 413 if (small_const_nbits(nbits))
1da177e4 414 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
2afe27c7
YN
415
416 return find_first_zero_bit(src, nbits) == nbits;
1da177e4
LT
417}
418
4dea97f8 419static __always_inline
4e23eeeb 420unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
1da177e4 421{
4b0bc0bc 422 if (small_const_nbits(nbits))
08cd3657 423 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
1da177e4
LT
424 return __bitmap_weight(src, nbits);
425}
426
24291caf
YN
427static __always_inline
428unsigned long bitmap_weight_and(const unsigned long *src1,
429 const unsigned long *src2, unsigned int nbits)
430{
431 if (small_const_nbits(nbits))
432 return hweight_long(*src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits));
433 return __bitmap_weight_and(src1, src2, nbits);
434}
435
c1f5204e
YN
436static __always_inline
437unsigned long bitmap_weight_andnot(const unsigned long *src1,
438 const unsigned long *src2, unsigned int nbits)
439{
440 if (small_const_nbits(nbits))
441 return hweight_long(*src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits));
442 return __bitmap_weight_andnot(src1, src2, nbits);
443}
444
e5af323c
MW
445static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
446 unsigned int nbits)
447{
448 if (__builtin_constant_p(nbits) && nbits == 1)
449 __set_bit(start, map);
3e7e5baa
AL
450 else if (small_const_nbits(start + nbits))
451 *map |= GENMASK(start + nbits - 1, start);
21035965
OS
452 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
453 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
454 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
455 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
2a98dc02 456 memset((char *)map + start / 8, 0xff, nbits / 8);
e5af323c
MW
457 else
458 __bitmap_set(map, start, nbits);
459}
460
461static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
462 unsigned int nbits)
463{
464 if (__builtin_constant_p(nbits) && nbits == 1)
465 __clear_bit(start, map);
3e7e5baa
AL
466 else if (small_const_nbits(start + nbits))
467 *map &= ~GENMASK(start + nbits - 1, start);
21035965
OS
468 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
469 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
470 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
471 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
2a98dc02 472 memset((char *)map + start / 8, 0, nbits / 8);
e5af323c
MW
473 else
474 __bitmap_clear(map, start, nbits);
475}
476
2fbad299 477static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
d9873969 478 unsigned int shift, unsigned int nbits)
1da177e4 479{
4b0bc0bc 480 if (small_const_nbits(nbits))
2fbad299 481 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
1da177e4 482 else
2fbad299 483 __bitmap_shift_right(dst, src, shift, nbits);
1da177e4
LT
484}
485
dba94c25
RV
486static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
487 unsigned int shift, unsigned int nbits)
1da177e4 488{
4b0bc0bc 489 if (small_const_nbits(nbits))
dba94c25 490 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
1da177e4 491 else
dba94c25 492 __bitmap_shift_left(dst, src, shift, nbits);
1da177e4
LT
493}
494
30544ed5
AS
495static inline void bitmap_replace(unsigned long *dst,
496 const unsigned long *old,
497 const unsigned long *new,
498 const unsigned long *mask,
499 unsigned int nbits)
500{
501 if (small_const_nbits(nbits))
502 *dst = (*old & ~(*mask)) | (*new & *mask);
503 else
504 __bitmap_replace(dst, old, new, mask, nbits);
505}
506
de5f8433
AS
507/**
508 * bitmap_scatter - Scatter a bitmap according to the given mask
509 * @dst: scattered bitmap
510 * @src: gathered bitmap
511 * @mask: mask representing bits to assign to in the scattered bitmap
512 * @nbits: number of bits in each of these bitmaps
513 *
514 * Scatters bitmap with sequential bits according to the given @mask.
515 *
516 * Example:
517 * If @src bitmap = 0x005a, with @mask = 0x1313, @dst will be 0x0302.
518 *
519 * Or in binary form
520 * @src @mask @dst
521 * 0000000001011010 0001001100010011 0000001100000010
522 *
523 * (Bits 0, 1, 2, 3, 4, 5 are copied to the bits 0, 1, 4, 8, 9, 12)
524 *
525 * A more 'visual' description of the operation:
526 * src: 0000000001011010
527 * ||||||
528 * +------+|||||
529 * | +----+||||
530 * | |+----+|||
531 * | || +-+||
532 * | || | ||
533 * mask: ...v..vv...v..vv
534 * ...0..11...0..10
535 * dst: 0000001100000010
536 *
537 * A relationship exists between bitmap_scatter() and bitmap_gather().
538 * bitmap_gather() can be seen as the 'reverse' bitmap_scatter() operation.
539 * See bitmap_scatter() for details related to this relationship.
540 */
541static inline void bitmap_scatter(unsigned long *dst, const unsigned long *src,
542 const unsigned long *mask, unsigned int nbits)
543{
544 unsigned int n = 0;
545 unsigned int bit;
546
547 bitmap_zero(dst, nbits);
548
549 for_each_set_bit(bit, mask, nbits)
550 __assign_bit(bit, dst, test_bit(n++, src));
551}
552
553/**
554 * bitmap_gather - Gather a bitmap according to given mask
555 * @dst: gathered bitmap
556 * @src: scattered bitmap
557 * @mask: mask representing bits to extract from in the scattered bitmap
558 * @nbits: number of bits in each of these bitmaps
559 *
560 * Gathers bitmap with sparse bits according to the given @mask.
561 *
562 * Example:
563 * If @src bitmap = 0x0302, with @mask = 0x1313, @dst will be 0x001a.
564 *
565 * Or in binary form
566 * @src @mask @dst
567 * 0000001100000010 0001001100010011 0000000000011010
568 *
569 * (Bits 0, 1, 4, 8, 9, 12 are copied to the bits 0, 1, 2, 3, 4, 5)
570 *
571 * A more 'visual' description of the operation:
572 * mask: ...v..vv...v..vv
573 * src: 0000001100000010
574 * ^ ^^ ^ 0
575 * | || | 10
576 * | || > 010
577 * | |+--> 1010
578 * | +--> 11010
579 * +----> 011010
580 * dst: 0000000000011010
581 *
582 * A relationship exists between bitmap_gather() and bitmap_scatter(). See
583 * bitmap_scatter() for the bitmap scatter detailed operations.
584 * Suppose scattered computed using bitmap_scatter(scattered, src, mask, n).
585 * The operation bitmap_gather(result, scattered, mask, n) leads to a result
586 * equal or equivalent to src.
587 *
588 * The result can be 'equivalent' because bitmap_scatter() and bitmap_gather()
589 * are not bijective.
590 * The result and src values are equivalent in that sense that a call to
591 * bitmap_scatter(res, src, mask, n) and a call to
592 * bitmap_scatter(res, result, mask, n) will lead to the same res value.
593 */
594static inline void bitmap_gather(unsigned long *dst, const unsigned long *src,
595 const unsigned long *mask, unsigned int nbits)
596{
597 unsigned int n = 0;
598 unsigned int bit;
599
600 bitmap_zero(dst, nbits);
601
602 for_each_set_bit(bit, mask, nbits)
603 __assign_bit(n++, dst, test_bit(bit, src));
604}
605
e837dfde
DZ
606static inline void bitmap_next_set_region(unsigned long *bitmap,
607 unsigned int *rs, unsigned int *re,
608 unsigned int end)
609{
610 *rs = find_next_bit(bitmap, end, *rs);
611 *re = find_next_zero_bit(bitmap, end, *rs + 1);
612}
613
6cb42f91
YN
614/**
615 * bitmap_release_region - release allocated bitmap region
616 * @bitmap: array of unsigned longs corresponding to the bitmap
617 * @pos: beginning of bit region to release
618 * @order: region size (log base 2 of number of bits) to release
619 *
620 * This is the complement to __bitmap_find_free_region() and releases
621 * the found region (by clearing it in the bitmap).
622 */
623static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
624{
625 bitmap_clear(bitmap, pos, BIT(order));
626}
627
628/**
629 * bitmap_allocate_region - allocate bitmap region
630 * @bitmap: array of unsigned longs corresponding to the bitmap
631 * @pos: beginning of bit region to allocate
632 * @order: region size (log base 2 of number of bits) to allocate
633 *
634 * Allocate (set bits in) a specified region of a bitmap.
635 *
636 * Returns: 0 on success, or %-EBUSY if specified region wasn't
637 * free (not all bits were zero).
638 */
639static inline int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
640{
641 unsigned int len = BIT(order);
642
643 if (find_next_bit(bitmap, pos + len, pos) < pos + len)
644 return -EBUSY;
645 bitmap_set(bitmap, pos, len);
646 return 0;
647}
648
649/**
650 * bitmap_find_free_region - find a contiguous aligned mem region
651 * @bitmap: array of unsigned longs corresponding to the bitmap
652 * @bits: number of bits in the bitmap
653 * @order: region size (log base 2 of number of bits) to find
654 *
655 * Find a region of free (zero) bits in a @bitmap of @bits bits and
656 * allocate them (set them to one). Only consider regions of length
657 * a power (@order) of two, aligned to that power of two, which
658 * makes the search algorithm much faster.
659 *
660 * Returns: the bit offset in bitmap of the allocated region,
661 * or -errno on failure.
662 */
663static inline int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
664{
665 unsigned int pos, end; /* scans bitmap by regions of size order */
666
667 for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) {
668 if (!bitmap_allocate_region(bitmap, pos, order))
669 return pos;
670 }
671 return -ENOMEM;
672}
673
404376af 674/**
60ef6900 675 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
404376af 676 * @n: u64 value
60ef6900
YN
677 *
678 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
679 * integers in 32-bit environment, and 64-bit integers in 64-bit one.
680 *
681 * There are four combinations of endianness and length of the word in linux
682 * ABIs: LE64, BE64, LE32 and BE32.
683 *
684 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
685 * bitmaps and therefore don't require any special handling.
686 *
687 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
688 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
689 * other hand is represented as an array of 32-bit words and the position of
690 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
691 * word. For example, bit #42 is located at 10th position of 2nd word.
692 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
693 * values in memory as it usually does. But for BE we need to swap hi and lo
694 * words manually.
695 *
696 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
697 * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
698 * hi and lo words, as is expected by bitmap.
699 */
700#if __BITS_PER_LONG == 64
701#define BITMAP_FROM_U64(n) (n)
702#else
703#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
704 ((unsigned long) ((u64)(n) >> 32))
705#endif
706
404376af 707/**
29dd3288
MS
708 * bitmap_from_u64 - Check and swap words within u64.
709 * @mask: source bitmap
710 * @dst: destination bitmap
711 *
404376af 712 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
29dd3288 713 * to read u64 mask, we will get the wrong word.
404376af 714 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
29dd3288
MS
715 * but we expect the lower 32-bits of u64.
716 */
717static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
718{
0a97953f 719 bitmap_from_arr64(dst, &mask, 64);
29dd3288
MS
720}
721
169c474f
WBG
722/**
723 * bitmap_get_value8 - get an 8-bit value within a memory region
724 * @map: address to the bitmap memory region
725 * @start: bit offset of the 8-bit value; must be a multiple of 8
726 *
727 * Returns the 8-bit value located at the @start bit offset within the @src
728 * memory region.
729 */
730static inline unsigned long bitmap_get_value8(const unsigned long *map,
731 unsigned long start)
732{
733 const size_t index = BIT_WORD(start);
734 const unsigned long offset = start % BITS_PER_LONG;
735
736 return (map[index] >> offset) & 0xFF;
737}
738
739/**
740 * bitmap_set_value8 - set an 8-bit value within a memory region
741 * @map: address to the bitmap memory region
742 * @value: the 8-bit value; values wider than 8 bits may clobber bitmap
743 * @start: bit offset of the 8-bit value; must be a multiple of 8
744 */
745static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
746 unsigned long start)
747{
748 const size_t index = BIT_WORD(start);
749 const unsigned long offset = start % BITS_PER_LONG;
750
751 map[index] &= ~(0xFFUL << offset);
752 map[index] |= value << offset;
753}
754
1da177e4
LT
755#endif /* __ASSEMBLY__ */
756
757#endif /* __LINUX_BITMAP_H */