bitfield.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
  3. * Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef _LINUX_BITFIELD_H
  15. #define _LINUX_BITFIELD_H
  16. #include <linux/bug.h>
  17. /*
  18. * Bitfield access macros
  19. *
  20. * FIELD_{GET,PREP} macros take as first parameter shifted mask
  21. * from which they extract the base mask and shift amount.
  22. * Mask must be a compilation time constant.
  23. *
  24. * Example:
  25. *
  26. * #define REG_FIELD_A GENMASK(6, 0)
  27. * #define REG_FIELD_B BIT(7)
  28. * #define REG_FIELD_C GENMASK(15, 8)
  29. * #define REG_FIELD_D GENMASK(31, 16)
  30. *
  31. * Get:
  32. * a = FIELD_GET(REG_FIELD_A, reg);
  33. * b = FIELD_GET(REG_FIELD_B, reg);
  34. *
  35. * Set:
  36. * reg = FIELD_PREP(REG_FIELD_A, 1) |
  37. * FIELD_PREP(REG_FIELD_B, 0) |
  38. * FIELD_PREP(REG_FIELD_C, c) |
  39. * FIELD_PREP(REG_FIELD_D, 0x40);
  40. *
  41. * Modify:
  42. * reg &= ~REG_FIELD_C;
  43. * reg |= FIELD_PREP(REG_FIELD_C, c);
  44. */
  45. #define __bf_shf(x) (__builtin_ffsll(x) - 1)
  46. #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
  47. ({ \
  48. BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
  49. _pfx "mask is not constant"); \
  50. BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
  51. BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
  52. ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
  53. _pfx "value too large for the field"); \
  54. BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \
  55. _pfx "type of reg too small for mask"); \
  56. __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
  57. (1ULL << __bf_shf(_mask))); \
  58. })
  59. /**
  60. * FIELD_FIT() - check if value fits in the field
  61. * @_mask: shifted mask defining the field's length and position
  62. * @_val: value to test against the field
  63. *
  64. * Return: true if @_val can fit inside @_mask, false if @_val is too big.
  65. */
  66. #define FIELD_FIT(_mask, _val) \
  67. ({ \
  68. __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: "); \
  69. !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
  70. })
  71. /**
  72. * FIELD_PREP() - prepare a bitfield element
  73. * @_mask: shifted mask defining the field's length and position
  74. * @_val: value to put in the field
  75. *
  76. * FIELD_PREP() masks and shifts up the value. The result should
  77. * be combined with other fields of the bitfield using logical OR.
  78. */
  79. #define FIELD_PREP(_mask, _val) \
  80. ({ \
  81. __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
  82. ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
  83. })
  84. /**
  85. * FIELD_GET() - extract a bitfield element
  86. * @_mask: shifted mask defining the field's length and position
  87. * @_reg: 32bit value of entire bitfield
  88. *
  89. * FIELD_GET() extracts the field specified by @_mask from the
  90. * bitfield passed in as @_reg by masking and shifting it down.
  91. */
  92. #define FIELD_GET(_mask, _reg) \
  93. ({ \
  94. __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
  95. (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
  96. })
  97. #endif