bitfield.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
  4. * Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  5. */
  6. #ifndef _LINUX_BITFIELD_H
  7. #define _LINUX_BITFIELD_H
  8. #include <linux/build_bug.h>
  9. #include <linux/kernel.h>
  10. #include <asm/byteorder.h>
  11. /*
  12. * Bitfield access macros
  13. *
  14. * FIELD_{GET,PREP} macros take as first parameter shifted mask
  15. * from which they extract the base mask and shift amount.
  16. * Mask must be a compilation time constant.
  17. *
  18. * Example:
  19. *
  20. * #define REG_FIELD_A GENMASK(6, 0)
  21. * #define REG_FIELD_B BIT(7)
  22. * #define REG_FIELD_C GENMASK(15, 8)
  23. * #define REG_FIELD_D GENMASK(31, 16)
  24. *
  25. * Get:
  26. * a = FIELD_GET(REG_FIELD_A, reg);
  27. * b = FIELD_GET(REG_FIELD_B, reg);
  28. *
  29. * Set:
  30. * reg = FIELD_PREP(REG_FIELD_A, 1) |
  31. * FIELD_PREP(REG_FIELD_B, 0) |
  32. * FIELD_PREP(REG_FIELD_C, c) |
  33. * FIELD_PREP(REG_FIELD_D, 0x40);
  34. *
  35. * Modify:
  36. * reg &= ~REG_FIELD_C;
  37. * reg |= FIELD_PREP(REG_FIELD_C, c);
  38. */
  39. #define __bf_shf(x) (__builtin_ffsll(x) - 1)
  40. #define __scalar_type_to_unsigned_cases(type) \
  41. unsigned type: (unsigned type)0, \
  42. signed type: (unsigned type)0
  43. #define __unsigned_scalar_typeof(x) typeof( \
  44. _Generic((x), \
  45. char: (unsigned char)0, \
  46. __scalar_type_to_unsigned_cases(char), \
  47. __scalar_type_to_unsigned_cases(short), \
  48. __scalar_type_to_unsigned_cases(int), \
  49. __scalar_type_to_unsigned_cases(long), \
  50. __scalar_type_to_unsigned_cases(long long), \
  51. default: (x)))
  52. #define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
  53. #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
  54. ({ \
  55. BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
  56. _pfx "mask is not constant"); \
  57. BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
  58. BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
  59. ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
  60. _pfx "value too large for the field"); \
  61. BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \
  62. __bf_cast_unsigned(_reg, ~0ull), \
  63. _pfx "type of reg too small for mask"); \
  64. __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
  65. (1ULL << __bf_shf(_mask))); \
  66. })
  67. /**
  68. * FIELD_MAX() - produce the maximum value representable by a field
  69. * @_mask: shifted mask defining the field's length and position
  70. *
  71. * FIELD_MAX() returns the maximum value that can be held in the field
  72. * specified by @_mask.
  73. */
  74. #define FIELD_MAX(_mask) \
  75. ({ \
  76. __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
  77. (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
  78. })
  79. /**
  80. * FIELD_FIT() - check if value fits in the field
  81. * @_mask: shifted mask defining the field's length and position
  82. * @_val: value to test against the field
  83. *
  84. * Return: true if @_val can fit inside @_mask, false if @_val is too big.
  85. */
  86. #define FIELD_FIT(_mask, _val) \
  87. ({ \
  88. __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
  89. !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
  90. })
  91. /**
  92. * FIELD_PREP() - prepare a bitfield element
  93. * @_mask: shifted mask defining the field's length and position
  94. * @_val: value to put in the field
  95. *
  96. * FIELD_PREP() masks and shifts up the value. The result should
  97. * be combined with other fields of the bitfield using logical OR.
  98. */
  99. #define FIELD_PREP(_mask, _val) \
  100. ({ \
  101. __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
  102. ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
  103. })
  104. /**
  105. * FIELD_GET() - extract a bitfield element
  106. * @_mask: shifted mask defining the field's length and position
  107. * @_reg: value of entire bitfield
  108. *
  109. * FIELD_GET() extracts the field specified by @_mask from the
  110. * bitfield passed in as @_reg by masking and shifting it down.
  111. */
  112. #define FIELD_GET(_mask, _reg) \
  113. ({ \
  114. __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
  115. (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
  116. })
  117. extern void __compiletime_error("value doesn't fit into mask")
  118. __field_overflow(void);
  119. extern void __compiletime_error("bad bitfield mask")
  120. __bad_mask(void);
  121. static __always_inline u64 field_multiplier(u64 field)
  122. {
  123. if ((field | (field - 1)) & ((field | (field - 1)) + 1))
  124. __bad_mask();
  125. return field & -field;
  126. }
  127. static __always_inline u64 field_mask(u64 field)
  128. {
  129. return field / field_multiplier(field);
  130. }
  131. #define field_max(field) ((typeof(field))field_mask(field))
  132. #define ____MAKE_OP(type,base,to,from) \
  133. static __always_inline __##type type##_encode_bits(base v, base field) \
  134. { \
  135. if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
  136. __field_overflow(); \
  137. return to((v & field_mask(field)) * field_multiplier(field)); \
  138. } \
  139. static __always_inline __##type type##_replace_bits(__##type old, \
  140. base val, base field) \
  141. { \
  142. return (old & ~to(field)) | type##_encode_bits(val, field); \
  143. } \
  144. static __always_inline void type##p_replace_bits(__##type *p, \
  145. base val, base field) \
  146. { \
  147. *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
  148. } \
  149. static __always_inline base type##_get_bits(__##type v, base field) \
  150. { \
  151. return (from(v) & field)/field_multiplier(field); \
  152. }
  153. #define __MAKE_OP(size) \
  154. ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
  155. ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
  156. ____MAKE_OP(u##size,u##size,,)
  157. ____MAKE_OP(u8,u8,,)
  158. __MAKE_OP(16)
  159. __MAKE_OP(32)
  160. __MAKE_OP(64)
  161. #undef __MAKE_OP
  162. #undef ____MAKE_OP
  163. #endif