You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mlx_bitops.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifndef _MLX_BITOPS_H
  2. #define _MLX_BITOPS_H
  3. /*
  4. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. FILE_LICENCE ( GPL2_OR_LATER );
  21. /**
  22. * @file
  23. *
  24. * Mellanox bit operations
  25. *
  26. */
  27. /* Datatype used to represent a bit in the Mellanox autogenerated headers */
  28. typedef unsigned char pseudo_bit_t;
  29. /**
  30. * Wrapper structure for pseudo_bit_t structures
  31. *
  32. * This structure provides a wrapper around the autogenerated
  33. * pseudo_bit_t structures. It has the correct size, and also
  34. * encapsulates type information about the underlying pseudo_bit_t
  35. * structure, which allows the MLX_FILL etc. macros to work without
  36. * requiring explicit type information.
  37. */
  38. #define MLX_DECLARE_STRUCT( _structure ) \
  39. _structure { \
  40. union { \
  41. uint8_t bytes[ sizeof ( struct _structure ## _st ) / 8 ]; \
  42. uint32_t dwords[ sizeof ( struct _structure ## _st ) / 32 ]; \
  43. struct _structure ## _st *dummy[0]; \
  44. } u; \
  45. }
  46. /** Get pseudo_bit_t structure type from wrapper structure pointer */
  47. #define MLX_PSEUDO_STRUCT( _ptr ) \
  48. typeof ( *((_ptr)->u.dummy[0]) )
  49. /** Bit offset of a field within a pseudo_bit_t structure */
  50. #define MLX_BIT_OFFSET( _structure_st, _field ) \
  51. offsetof ( _structure_st, _field )
  52. /** Dword offset of a field within a pseudo_bit_t structure */
  53. #define MLX_DWORD_OFFSET( _structure_st, _field ) \
  54. ( MLX_BIT_OFFSET ( _structure_st, _field ) / 32 )
  55. /** Dword bit offset of a field within a pseudo_bit_t structure
  56. *
  57. * Yes, using mod-32 would work, but would lose the check for the
  58. * error of specifying a mismatched field name and dword index.
  59. */
  60. #define MLX_DWORD_BIT_OFFSET( _structure_st, _index, _field ) \
  61. ( MLX_BIT_OFFSET ( _structure_st, _field ) - ( 32 * (_index) ) )
  62. /** Bit width of a field within a pseudo_bit_t structure */
  63. #define MLX_BIT_WIDTH( _structure_st, _field ) \
  64. sizeof ( ( ( _structure_st * ) NULL )->_field )
  65. /** Bit mask for a field within a pseudo_bit_t structure */
  66. #define MLX_BIT_MASK( _structure_st, _field ) \
  67. ( ( ~( ( uint32_t ) 0 ) ) >> \
  68. ( 32 - MLX_BIT_WIDTH ( _structure_st, _field ) ) )
  69. /*
  70. * Assemble native-endian dword from named fields and values
  71. *
  72. */
  73. #define MLX_ASSEMBLE_1( _structure_st, _index, _field, _value ) \
  74. ( (_value) << MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  75. #define MLX_ASSEMBLE_2( _structure_st, _index, _field, _value, ... ) \
  76. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  77. MLX_ASSEMBLE_1 ( _structure_st, _index, __VA_ARGS__ ) )
  78. #define MLX_ASSEMBLE_3( _structure_st, _index, _field, _value, ... ) \
  79. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  80. MLX_ASSEMBLE_2 ( _structure_st, _index, __VA_ARGS__ ) )
  81. #define MLX_ASSEMBLE_4( _structure_st, _index, _field, _value, ... ) \
  82. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  83. MLX_ASSEMBLE_3 ( _structure_st, _index, __VA_ARGS__ ) )
  84. #define MLX_ASSEMBLE_5( _structure_st, _index, _field, _value, ... ) \
  85. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  86. MLX_ASSEMBLE_4 ( _structure_st, _index, __VA_ARGS__ ) )
  87. #define MLX_ASSEMBLE_6( _structure_st, _index, _field, _value, ... ) \
  88. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  89. MLX_ASSEMBLE_5 ( _structure_st, _index, __VA_ARGS__ ) )
  90. #define MLX_ASSEMBLE_7( _structure_st, _index, _field, _value, ... ) \
  91. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  92. MLX_ASSEMBLE_6 ( _structure_st, _index, __VA_ARGS__ ) )
  93. /*
  94. * Build native-endian (positive) dword bitmasks from named fields
  95. *
  96. */
  97. #define MLX_MASK_1( _structure_st, _index, _field ) \
  98. ( MLX_BIT_MASK ( _structure_st, _field ) << \
  99. MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  100. #define MLX_MASK_2( _structure_st, _index, _field, ... ) \
  101. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  102. MLX_MASK_1 ( _structure_st, _index, __VA_ARGS__ ) )
  103. #define MLX_MASK_3( _structure_st, _index, _field, ... ) \
  104. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  105. MLX_MASK_2 ( _structure_st, _index, __VA_ARGS__ ) )
  106. #define MLX_MASK_4( _structure_st, _index, _field, ... ) \
  107. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  108. MLX_MASK_3 ( _structure_st, _index, __VA_ARGS__ ) )
  109. #define MLX_MASK_5( _structure_st, _index, _field, ... ) \
  110. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  111. MLX_MASK_4 ( _structure_st, _index, __VA_ARGS__ ) )
  112. #define MLX_MASK_6( _structure_st, _index, _field, ... ) \
  113. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  114. MLX_MASK_5 ( _structure_st, _index, __VA_ARGS__ ) )
  115. #define MLX_MASK_7( _structure_st, _index, _field, ... ) \
  116. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  117. MLX_MASK_6 ( _structure_st, _index, __VA_ARGS__ ) )
  118. /*
  119. * Populate big-endian dwords from named fields and values
  120. *
  121. */
  122. #define MLX_FILL( _ptr, _index, _assembled ) \
  123. do { \
  124. uint32_t *__ptr = &(_ptr)->u.dwords[(_index)]; \
  125. uint32_t __assembled = (_assembled); \
  126. *__ptr = cpu_to_be32 ( __assembled ); \
  127. } while ( 0 )
  128. #define MLX_FILL_1( _ptr, _index, ... ) \
  129. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  130. _index, __VA_ARGS__ ) )
  131. #define MLX_FILL_2( _ptr, _index, ... ) \
  132. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_2 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  133. _index, __VA_ARGS__ ) )
  134. #define MLX_FILL_3( _ptr, _index, ... ) \
  135. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_3 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  136. _index, __VA_ARGS__ ) )
  137. #define MLX_FILL_4( _ptr, _index, ... ) \
  138. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_4 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  139. _index, __VA_ARGS__ ) )
  140. #define MLX_FILL_5( _ptr, _index, ... ) \
  141. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_5 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  142. _index, __VA_ARGS__ ) )
  143. #define MLX_FILL_6( _ptr, _index, ... ) \
  144. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_6 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  145. _index, __VA_ARGS__ ) )
  146. #define MLX_FILL_7( _ptr, _index, ... ) \
  147. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_7 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  148. _index, __VA_ARGS__ ) )
  149. /*
  150. * Modify big-endian dword using named field and value
  151. *
  152. */
  153. #define MLX_SET( _ptr, _field, _value ) \
  154. do { \
  155. unsigned int __index = \
  156. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  157. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  158. uint32_t __value = be32_to_cpu ( *__ptr ); \
  159. __value &= ~( MLX_MASK_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  160. __index, _field ) ); \
  161. __value |= MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  162. __index, _field, _value ); \
  163. *__ptr = cpu_to_be32 ( __value ); \
  164. } while ( 0 )
  165. /*
  166. * Extract value of named field
  167. *
  168. */
  169. #define MLX_GET( _ptr, _field ) \
  170. ( { \
  171. unsigned int __index = \
  172. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  173. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  174. uint32_t __value = be32_to_cpu ( *__ptr ); \
  175. __value >>= \
  176. MLX_DWORD_BIT_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), \
  177. __index, _field ); \
  178. __value &= \
  179. MLX_BIT_MASK ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  180. __value; \
  181. } )
  182. #endif /* _MLX_BITOPS_H */