mm: convert ptlock_alloc() to use ptdescs
[linux-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>
47d8c156 9#include <linux/find.h>
08c5188e 10#include <linux/limits.h>
c13656b9
BG
11#include <linux/string.h>
12#include <linux/types.h>
1da177e4 13
e829c2e4
BG
14struct device;
15
1da177e4
LT
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
7d7363e4
RD
29/**
30 * DOC: bitmap overview
31 *
1da177e4
LT
32 * The available bitmap operations and their rough meaning in the
33 * case that the bitmap is a single unsigned long are thus:
34 *
41e7b166
RV
35 * The generated code is more efficient when nbits is known at
36 * compile-time and at most BITS_PER_LONG.
08cd3657 37 *
7d7363e4
RD
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
24291caf 54 * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap
7d7363e4
RD
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
780d2a9c 58 * bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above
7d7363e4
RD
59 * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
60 * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
20927671 61 * bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
30544ed5 62 * bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask)
7d7363e4
RD
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
c724f193 74 * bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
ba1afa67 75 * bitmap_from_arr64(dst, buf, nbits) Copy nbits from u64[] buf to dst
c724f193 76 * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
0a97953f 77 * bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst
169c474f
WBG
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
7d7363e4 80 *
334cfa48
AS
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.
1da177e4
LT
86 */
87
7d7363e4
RD
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
0ade34c3
CC
102 * find_next_zero_bit(addr, nbits, bit)
103 * Position next zero bit in *addr >= bit
7d7363e4 104 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
0ade34c3
CC
105 * find_next_and_bit(addr1, addr2, nbits, bit)
106 * Same as find_next_bit, but in
107 * (*addr1 & *addr2)
1da177e4 108 *
1da177e4
LT
109 */
110
7d7363e4
RD
111/**
112 * DOC: declare bitmap
1da177e4
LT
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
c42b65e3
AS
118/*
119 * Allocation and deallocation of bitmap.
120 * Provided in lib/bitmap.c to avoid circular dependency.
121 */
98635b29
BG
122unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
123unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
7529cc7f
TT
124unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
125unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
98635b29 126void bitmap_free(const unsigned long *bitmap);
c42b65e3 127
e829c2e4
BG
128/* Managed variants of the above. */
129unsigned long *devm_bitmap_alloc(struct device *dev,
130 unsigned int nbits, gfp_t flags);
131unsigned long *devm_bitmap_zalloc(struct device *dev,
132 unsigned int nbits, gfp_t flags);
133
1da177e4
LT
134/*
135 * lib/bitmap.c provides these functions:
136 */
137
005f1700
KC
138bool __bitmap_equal(const unsigned long *bitmap1,
139 const unsigned long *bitmap2, unsigned int nbits);
98635b29
BG
140bool __pure __bitmap_or_equal(const unsigned long *src1,
141 const unsigned long *src2,
142 const unsigned long *src3,
143 unsigned int nbits);
144void __bitmap_complement(unsigned long *dst, const unsigned long *src,
145 unsigned int nbits);
146void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
147 unsigned int shift, unsigned int nbits);
148void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
149 unsigned int shift, unsigned int nbits);
150void bitmap_cut(unsigned long *dst, const unsigned long *src,
151 unsigned int first, unsigned int cut, unsigned int nbits);
e2863a78 152bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
98635b29
BG
153 const unsigned long *bitmap2, unsigned int nbits);
154void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
155 const unsigned long *bitmap2, unsigned int nbits);
156void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
157 const unsigned long *bitmap2, unsigned int nbits);
e2863a78 158bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
98635b29
BG
159 const unsigned long *bitmap2, unsigned int nbits);
160void __bitmap_replace(unsigned long *dst,
161 const unsigned long *old, const unsigned long *new,
162 const unsigned long *mask, unsigned int nbits);
005f1700
KC
163bool __bitmap_intersects(const unsigned long *bitmap1,
164 const unsigned long *bitmap2, unsigned int nbits);
165bool __bitmap_subset(const unsigned long *bitmap1,
166 const unsigned long *bitmap2, unsigned int nbits);
4e23eeeb 167unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
24291caf
YN
168unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
169 const unsigned long *bitmap2, unsigned int nbits);
98635b29
BG
170void __bitmap_set(unsigned long *map, unsigned int start, int len);
171void __bitmap_clear(unsigned long *map, unsigned int start, int len);
5e19b013 172
98635b29
BG
173unsigned 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);
5e19b013
MN
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 */
192static inline unsigned long
193bitmap_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}
c1a2a962 202
98635b29 203int bitmap_parse(const char *buf, unsigned int buflen,
01a3ee2b 204 unsigned long *dst, int nbits);
98635b29 205int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
1da177e4 206 unsigned long *dst, int nbits);
98635b29 207int bitmap_parselist(const char *buf, unsigned long *maskp,
1da177e4 208 int nmaskbits);
98635b29 209int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
4b060420 210 unsigned long *dst, int nbits);
98635b29 211void bitmap_remap(unsigned long *dst, const unsigned long *src,
9814ec13 212 const unsigned long *old, const unsigned long *new, unsigned int nbits);
98635b29 213int bitmap_bitremap(int oldbit,
fb5eeeee 214 const unsigned long *old, const unsigned long *new, int bits);
98635b29 215void bitmap_onto(unsigned long *dst, const unsigned long *orig,
eb569883 216 const unsigned long *relmap, unsigned int bits);
98635b29 217void bitmap_fold(unsigned long *dst, const unsigned long *orig,
b26ad583 218 unsigned int sz, unsigned int nbits);
98635b29
BG
219int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
220void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
221int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
3aa56885 222
e8f24278 223#ifdef __BIG_ENDIAN
98635b29 224void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
e8f24278
RV
225#else
226#define bitmap_copy_le bitmap_copy
227#endif
98635b29 228int bitmap_print_to_pagebuf(bool list, char *buf,
5aaba363 229 const unsigned long *maskp, int nmaskbits);
1da177e4 230
1fae5629
TT
231extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
232 int nmaskbits, loff_t off, size_t count);
233
234extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
235 int nmaskbits, loff_t off, size_t count);
236
89c1e79e
RV
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)))
1da177e4 239
8b4daad5 240static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
1da177e4 241{
c8cebc55 242 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
3e7e5baa
AL
243
244 if (small_const_nbits(nbits))
245 *dst = 0;
246 else
247 memset(dst, 0, len);
1da177e4
LT
248}
249
8b4daad5 250static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
1da177e4 251{
c8cebc55 252 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
3e7e5baa
AL
253
254 if (small_const_nbits(nbits))
255 *dst = ~0UL;
256 else
257 memset(dst, 0xff, len);
1da177e4
LT
258}
259
260static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
8b4daad5 261 unsigned int nbits)
1da177e4 262{
c8cebc55 263 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
3e7e5baa
AL
264
265 if (small_const_nbits(nbits))
266 *dst = *src;
267 else
268 memcpy(dst, src, len);
1da177e4
LT
269}
270
c724f193
YN
271/*
272 * Copy bitmap and clear tail bits in last word.
273 */
274static inline void bitmap_copy_clear_tail(unsigned long *dst,
275 const unsigned long *src, unsigned int nbits)
276{
277 bitmap_copy(dst, src, nbits);
278 if (nbits % BITS_PER_LONG)
279 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
280}
281
282/*
e041e0ac
YN
283 * On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64
284 * machines the order of hi and lo parts of numbers match the bitmap structure.
285 * In both cases conversion is not needed when copying data from/to arrays of
286 * u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead
287 * to out-of-bound access. To avoid that, both LE and BE variants of 64-bit
288 * architectures are not using bitmap_copy_clear_tail().
c724f193
YN
289 */
290#if BITS_PER_LONG == 64
98635b29 291void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
c724f193 292 unsigned int nbits);
98635b29 293void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
c724f193
YN
294 unsigned int nbits);
295#else
296#define bitmap_from_arr32(bitmap, buf, nbits) \
297 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
298 (const unsigned long *) (buf), (nbits))
299#define bitmap_to_arr32(buf, bitmap, nbits) \
300 bitmap_copy_clear_tail((unsigned long *) (buf), \
301 (const unsigned long *) (bitmap), (nbits))
302#endif
303
0a97953f 304/*
c1d2ba10
YN
305 * On 64-bit systems bitmaps are represented as u64 arrays internally. So,
306 * the conversion is not needed when copying data from/to arrays of u64.
0a97953f 307 */
c1d2ba10 308#if BITS_PER_LONG == 32
0a97953f
YN
309void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits);
310void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits);
311#else
312#define bitmap_from_arr64(bitmap, buf, nbits) \
313 bitmap_copy_clear_tail((unsigned long *)(bitmap), (const unsigned long *)(buf), (nbits))
314#define bitmap_to_arr64(buf, bitmap, nbits) \
315 bitmap_copy_clear_tail((unsigned long *)(buf), (const unsigned long *)(bitmap), (nbits))
316#endif
317
e2863a78 318static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1,
2f9305eb 319 const unsigned long *src2, unsigned int nbits)
1da177e4 320{
4b0bc0bc 321 if (small_const_nbits(nbits))
7e5f97d1 322 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
f4b0373b 323 return __bitmap_and(dst, src1, src2, nbits);
1da177e4
LT
324}
325
326static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
2f9305eb 327 const unsigned long *src2, unsigned int nbits)
1da177e4 328{
4b0bc0bc 329 if (small_const_nbits(nbits))
1da177e4
LT
330 *dst = *src1 | *src2;
331 else
332 __bitmap_or(dst, src1, src2, nbits);
333}
334
335static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
2f9305eb 336 const unsigned long *src2, unsigned int nbits)
1da177e4 337{
4b0bc0bc 338 if (small_const_nbits(nbits))
1da177e4
LT
339 *dst = *src1 ^ *src2;
340 else
341 __bitmap_xor(dst, src1, src2, nbits);
342}
343
e2863a78 344static inline bool bitmap_andnot(unsigned long *dst, const unsigned long *src1,
2f9305eb 345 const unsigned long *src2, unsigned int nbits)
1da177e4 346{
4b0bc0bc 347 if (small_const_nbits(nbits))
74e76531 348 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
f4b0373b 349 return __bitmap_andnot(dst, src1, src2, nbits);
1da177e4
LT
350}
351
352static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
3d6684f4 353 unsigned int nbits)
1da177e4 354{
4b0bc0bc 355 if (small_const_nbits(nbits))
65b4ee62 356 *dst = ~(*src);
1da177e4
LT
357 else
358 __bitmap_complement(dst, src, nbits);
359}
360
21035965
OS
361#ifdef __LITTLE_ENDIAN
362#define BITMAP_MEM_ALIGNMENT 8
363#else
364#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
365#endif
366#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
367
005f1700
KC
368static inline bool bitmap_equal(const unsigned long *src1,
369 const unsigned long *src2, unsigned int nbits)
1da177e4 370{
4b0bc0bc 371 if (small_const_nbits(nbits))
4b9d314c 372 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
21035965
OS
373 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
374 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
7dd96816 375 return !memcmp(src1, src2, nbits / 8);
4b9d314c 376 return __bitmap_equal(src1, src2, nbits);
1da177e4
LT
377}
378
b9fa6442 379/**
2a7e582f 380 * bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
b9fa6442
TG
381 * @src1: Pointer to bitmap 1
382 * @src2: Pointer to bitmap 2 will be or'ed with bitmap 1
383 * @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2
2a7e582f 384 * @nbits: number of bits in each of these bitmaps
b9fa6442
TG
385 *
386 * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
387 */
388static inline bool bitmap_or_equal(const unsigned long *src1,
389 const unsigned long *src2,
390 const unsigned long *src3,
391 unsigned int nbits)
392{
393 if (!small_const_nbits(nbits))
394 return __bitmap_or_equal(src1, src2, src3, nbits);
395
396 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
397}
398
005f1700
KC
399static inline bool bitmap_intersects(const unsigned long *src1,
400 const unsigned long *src2,
401 unsigned int nbits)
1da177e4 402{
4b0bc0bc 403 if (small_const_nbits(nbits))
1da177e4
LT
404 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
405 else
406 return __bitmap_intersects(src1, src2, nbits);
407}
408
005f1700
KC
409static inline bool bitmap_subset(const unsigned long *src1,
410 const unsigned long *src2, unsigned int nbits)
1da177e4 411{
4b0bc0bc 412 if (small_const_nbits(nbits))
1da177e4
LT
413 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
414 else
415 return __bitmap_subset(src1, src2, nbits);
416}
417
0bb86779 418static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
1da177e4 419{
4b0bc0bc 420 if (small_const_nbits(nbits))
1da177e4 421 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
2afe27c7
YN
422
423 return find_first_bit(src, nbits) == nbits;
1da177e4
LT
424}
425
0bb86779 426static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
1da177e4 427{
4b0bc0bc 428 if (small_const_nbits(nbits))
1da177e4 429 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
2afe27c7
YN
430
431 return find_first_zero_bit(src, nbits) == nbits;
1da177e4
LT
432}
433
4dea97f8 434static __always_inline
4e23eeeb 435unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
1da177e4 436{
4b0bc0bc 437 if (small_const_nbits(nbits))
08cd3657 438 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
1da177e4
LT
439 return __bitmap_weight(src, nbits);
440}
441
24291caf
YN
442static __always_inline
443unsigned long bitmap_weight_and(const unsigned long *src1,
444 const unsigned long *src2, unsigned int nbits)
445{
446 if (small_const_nbits(nbits))
447 return hweight_long(*src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits));
448 return __bitmap_weight_and(src1, src2, nbits);
449}
450
e5af323c
MW
451static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
452 unsigned int nbits)
453{
454 if (__builtin_constant_p(nbits) && nbits == 1)
455 __set_bit(start, map);
3e7e5baa
AL
456 else if (small_const_nbits(start + nbits))
457 *map |= GENMASK(start + nbits - 1, start);
21035965
OS
458 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
459 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
460 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
461 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
2a98dc02 462 memset((char *)map + start / 8, 0xff, nbits / 8);
e5af323c
MW
463 else
464 __bitmap_set(map, start, nbits);
465}
466
467static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
468 unsigned int nbits)
469{
470 if (__builtin_constant_p(nbits) && nbits == 1)
471 __clear_bit(start, map);
3e7e5baa
AL
472 else if (small_const_nbits(start + nbits))
473 *map &= ~GENMASK(start + nbits - 1, start);
21035965
OS
474 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
475 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
476 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
477 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
2a98dc02 478 memset((char *)map + start / 8, 0, nbits / 8);
e5af323c
MW
479 else
480 __bitmap_clear(map, start, nbits);
481}
482
2fbad299 483static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
d9873969 484 unsigned int shift, unsigned int nbits)
1da177e4 485{
4b0bc0bc 486 if (small_const_nbits(nbits))
2fbad299 487 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
1da177e4 488 else
2fbad299 489 __bitmap_shift_right(dst, src, shift, nbits);
1da177e4
LT
490}
491
dba94c25
RV
492static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
493 unsigned int shift, unsigned int nbits)
1da177e4 494{
4b0bc0bc 495 if (small_const_nbits(nbits))
dba94c25 496 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
1da177e4 497 else
dba94c25 498 __bitmap_shift_left(dst, src, shift, nbits);
1da177e4
LT
499}
500
30544ed5
AS
501static inline void bitmap_replace(unsigned long *dst,
502 const unsigned long *old,
503 const unsigned long *new,
504 const unsigned long *mask,
505 unsigned int nbits)
506{
507 if (small_const_nbits(nbits))
508 *dst = (*old & ~(*mask)) | (*new & *mask);
509 else
510 __bitmap_replace(dst, old, new, mask, nbits);
511}
512
e837dfde
DZ
513static inline void bitmap_next_set_region(unsigned long *bitmap,
514 unsigned int *rs, unsigned int *re,
515 unsigned int end)
516{
517 *rs = find_next_bit(bitmap, end, *rs);
518 *re = find_next_zero_bit(bitmap, end, *rs + 1);
519}
520
404376af 521/**
60ef6900 522 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
404376af 523 * @n: u64 value
60ef6900
YN
524 *
525 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
526 * integers in 32-bit environment, and 64-bit integers in 64-bit one.
527 *
528 * There are four combinations of endianness and length of the word in linux
529 * ABIs: LE64, BE64, LE32 and BE32.
530 *
531 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
532 * bitmaps and therefore don't require any special handling.
533 *
534 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
535 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
536 * other hand is represented as an array of 32-bit words and the position of
537 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
538 * word. For example, bit #42 is located at 10th position of 2nd word.
539 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
540 * values in memory as it usually does. But for BE we need to swap hi and lo
541 * words manually.
542 *
543 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
544 * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
545 * hi and lo words, as is expected by bitmap.
546 */
547#if __BITS_PER_LONG == 64
548#define BITMAP_FROM_U64(n) (n)
549#else
550#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
551 ((unsigned long) ((u64)(n) >> 32))
552#endif
553
404376af 554/**
29dd3288
MS
555 * bitmap_from_u64 - Check and swap words within u64.
556 * @mask: source bitmap
557 * @dst: destination bitmap
558 *
404376af 559 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
29dd3288 560 * to read u64 mask, we will get the wrong word.
404376af 561 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
29dd3288
MS
562 * but we expect the lower 32-bits of u64.
563 */
564static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
565{
0a97953f 566 bitmap_from_arr64(dst, &mask, 64);
29dd3288
MS
567}
568
169c474f
WBG
569/**
570 * bitmap_get_value8 - get an 8-bit value within a memory region
571 * @map: address to the bitmap memory region
572 * @start: bit offset of the 8-bit value; must be a multiple of 8
573 *
574 * Returns the 8-bit value located at the @start bit offset within the @src
575 * memory region.
576 */
577static inline unsigned long bitmap_get_value8(const unsigned long *map,
578 unsigned long start)
579{
580 const size_t index = BIT_WORD(start);
581 const unsigned long offset = start % BITS_PER_LONG;
582
583 return (map[index] >> offset) & 0xFF;
584}
585
586/**
587 * bitmap_set_value8 - set an 8-bit value within a memory region
588 * @map: address to the bitmap memory region
589 * @value: the 8-bit value; values wider than 8 bits may clobber bitmap
590 * @start: bit offset of the 8-bit value; must be a multiple of 8
591 */
592static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
593 unsigned long start)
594{
595 const size_t index = BIT_WORD(start);
596 const unsigned long offset = start % BITS_PER_LONG;
597
598 map[index] &= ~(0xFFUL << offset);
599 map[index] |= value << offset;
600}
601
1da177e4
LT
602#endif /* __ASSEMBLY__ */
603
604#endif /* __LINUX_BITMAP_H */