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