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 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. FILE_LICENCE ( GPL2_OR_LATER );
  22. /**
  23. * @file
  24. *
  25. * Mellanox bit operations
  26. *
  27. */
  28. /* Datatype used to represent a bit in the Mellanox autogenerated headers */
  29. typedef unsigned char pseudo_bit_t;
  30. /**
  31. * Wrapper structure for pseudo_bit_t structures
  32. *
  33. * This structure provides a wrapper around the autogenerated
  34. * pseudo_bit_t structures. It has the correct size, and also
  35. * encapsulates type information about the underlying pseudo_bit_t
  36. * structure, which allows the MLX_FILL etc. macros to work without
  37. * requiring explicit type information.
  38. */
  39. #define MLX_DECLARE_STRUCT( _structure ) \
  40. _structure { \
  41. union { \
  42. uint8_t bytes[ sizeof ( struct _structure ## _st ) / 8 ]; \
  43. uint32_t dwords[ sizeof ( struct _structure ## _st ) / 32 ]; \
  44. struct _structure ## _st *dummy[0]; \
  45. } __attribute__ (( packed )) u; \
  46. } __attribute__ (( packed ))
  47. /** Get pseudo_bit_t structure type from wrapper structure pointer */
  48. #define MLX_PSEUDO_STRUCT( _ptr ) \
  49. typeof ( *((_ptr)->u.dummy[0]) )
  50. /** Bit offset of a field within a pseudo_bit_t structure */
  51. #define MLX_BIT_OFFSET( _structure_st, _field ) \
  52. offsetof ( _structure_st, _field )
  53. /** Dword offset of a field within a pseudo_bit_t structure */
  54. #define MLX_DWORD_OFFSET( _structure_st, _field ) \
  55. ( MLX_BIT_OFFSET ( _structure_st, _field ) / 32 )
  56. /** Dword bit offset of a field within a pseudo_bit_t structure
  57. *
  58. * Yes, using mod-32 would work, but would lose the check for the
  59. * error of specifying a mismatched field name and dword index.
  60. */
  61. #define MLX_DWORD_BIT_OFFSET( _structure_st, _index, _field ) \
  62. ( MLX_BIT_OFFSET ( _structure_st, _field ) - ( 32 * (_index) ) )
  63. /** Bit width of a field within a pseudo_bit_t structure */
  64. #define MLX_BIT_WIDTH( _structure_st, _field ) \
  65. sizeof ( ( ( _structure_st * ) NULL )->_field )
  66. /** Bit mask for a field within a pseudo_bit_t structure */
  67. #define MLX_BIT_MASK( _structure_st, _field ) \
  68. ( ( ~( ( uint32_t ) 0 ) ) >> \
  69. ( 32 - MLX_BIT_WIDTH ( _structure_st, _field ) ) )
  70. /*
  71. * Assemble native-endian dword from named fields and values
  72. *
  73. */
  74. #define MLX_ASSEMBLE_1( _structure_st, _index, _field, _value ) \
  75. ( (_value) << MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  76. #define MLX_ASSEMBLE_2( _structure_st, _index, _field, _value, ... ) \
  77. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  78. MLX_ASSEMBLE_1 ( _structure_st, _index, __VA_ARGS__ ) )
  79. #define MLX_ASSEMBLE_3( _structure_st, _index, _field, _value, ... ) \
  80. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  81. MLX_ASSEMBLE_2 ( _structure_st, _index, __VA_ARGS__ ) )
  82. #define MLX_ASSEMBLE_4( _structure_st, _index, _field, _value, ... ) \
  83. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  84. MLX_ASSEMBLE_3 ( _structure_st, _index, __VA_ARGS__ ) )
  85. #define MLX_ASSEMBLE_5( _structure_st, _index, _field, _value, ... ) \
  86. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  87. MLX_ASSEMBLE_4 ( _structure_st, _index, __VA_ARGS__ ) )
  88. #define MLX_ASSEMBLE_6( _structure_st, _index, _field, _value, ... ) \
  89. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  90. MLX_ASSEMBLE_5 ( _structure_st, _index, __VA_ARGS__ ) )
  91. #define MLX_ASSEMBLE_7( _structure_st, _index, _field, _value, ... ) \
  92. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  93. MLX_ASSEMBLE_6 ( _structure_st, _index, __VA_ARGS__ ) )
  94. #define MLX_ASSEMBLE_8( _structure_st, _index, _field, _value, ... ) \
  95. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  96. MLX_ASSEMBLE_7 ( _structure_st, _index, __VA_ARGS__ ) )
  97. /*
  98. * Build native-endian (positive) dword bitmasks from named fields
  99. *
  100. */
  101. #define MLX_MASK_1( _structure_st, _index, _field ) \
  102. ( MLX_BIT_MASK ( _structure_st, _field ) << \
  103. MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  104. #define MLX_MASK_2( _structure_st, _index, _field, ... ) \
  105. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  106. MLX_MASK_1 ( _structure_st, _index, __VA_ARGS__ ) )
  107. #define MLX_MASK_3( _structure_st, _index, _field, ... ) \
  108. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  109. MLX_MASK_2 ( _structure_st, _index, __VA_ARGS__ ) )
  110. #define MLX_MASK_4( _structure_st, _index, _field, ... ) \
  111. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  112. MLX_MASK_3 ( _structure_st, _index, __VA_ARGS__ ) )
  113. #define MLX_MASK_5( _structure_st, _index, _field, ... ) \
  114. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  115. MLX_MASK_4 ( _structure_st, _index, __VA_ARGS__ ) )
  116. #define MLX_MASK_6( _structure_st, _index, _field, ... ) \
  117. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  118. MLX_MASK_5 ( _structure_st, _index, __VA_ARGS__ ) )
  119. #define MLX_MASK_7( _structure_st, _index, _field, ... ) \
  120. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  121. MLX_MASK_6 ( _structure_st, _index, __VA_ARGS__ ) )
  122. #define MLX_MASK_8( _structure_st, _index, _field, ... ) \
  123. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  124. MLX_MASK_7 ( _structure_st, _index, __VA_ARGS__ ) )
  125. /*
  126. * Populate big-endian dwords from named fields and values
  127. *
  128. */
  129. #define MLX_FILL( _ptr, _index, _assembled ) \
  130. do { \
  131. uint32_t *__ptr = &(_ptr)->u.dwords[(_index)]; \
  132. uint32_t __assembled = (_assembled); \
  133. *__ptr = cpu_to_be32 ( __assembled ); \
  134. } while ( 0 )
  135. #define MLX_FILL_1( _ptr, _index, ... ) \
  136. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  137. _index, __VA_ARGS__ ) )
  138. #define MLX_FILL_2( _ptr, _index, ... ) \
  139. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_2 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  140. _index, __VA_ARGS__ ) )
  141. #define MLX_FILL_3( _ptr, _index, ... ) \
  142. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_3 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  143. _index, __VA_ARGS__ ) )
  144. #define MLX_FILL_4( _ptr, _index, ... ) \
  145. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_4 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  146. _index, __VA_ARGS__ ) )
  147. #define MLX_FILL_5( _ptr, _index, ... ) \
  148. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_5 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  149. _index, __VA_ARGS__ ) )
  150. #define MLX_FILL_6( _ptr, _index, ... ) \
  151. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_6 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  152. _index, __VA_ARGS__ ) )
  153. #define MLX_FILL_7( _ptr, _index, ... ) \
  154. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_7 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  155. _index, __VA_ARGS__ ) )
  156. #define MLX_FILL_8( _ptr, _index, ... ) \
  157. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_8 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  158. _index, __VA_ARGS__ ) )
  159. /*
  160. * Modify big-endian dword using named field and value
  161. *
  162. */
  163. #define MLX_SET( _ptr, _field, _value ) \
  164. do { \
  165. unsigned int __index = \
  166. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  167. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  168. uint32_t __value = be32_to_cpu ( *__ptr ); \
  169. __value &= ~( MLX_MASK_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  170. __index, _field ) ); \
  171. __value |= MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  172. __index, _field, _value ); \
  173. *__ptr = cpu_to_be32 ( __value ); \
  174. } while ( 0 )
  175. /*
  176. * Extract value of named field
  177. *
  178. */
  179. #define MLX_GET( _ptr, _field ) \
  180. ( { \
  181. unsigned int __index = \
  182. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  183. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  184. uint32_t __value = be32_to_cpu ( *__ptr ); \
  185. __value >>= \
  186. MLX_DWORD_BIT_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), \
  187. __index, _field ); \
  188. __value &= \
  189. MLX_BIT_MASK ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  190. __value; \
  191. } )
  192. /*
  193. * Fill high dword of physical address, if necessary
  194. *
  195. */
  196. #define MLX_FILL_H( _structure_st, _index, _field, _address ) do { \
  197. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) { \
  198. MLX_FILL_1 ( _structure_st, _index, _field, \
  199. ( ( ( uint64_t ) (_address) ) >> 32 ) ); \
  200. } } while ( 0 )
  201. #endif /* _MLX_BITOPS_H */