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.

bit_ops.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. This software is available to you under a choice of one of two
  3. licenses. You may choose to be licensed under the terms of the GNU
  4. General Public License (GPL) Version 2, available at
  5. <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
  6. license, available in the LICENSE.TXT file accompanying this
  7. software. These details are also available at
  8. <http://openib.org/license.html>.
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. SOFTWARE.
  17. Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
  18. */
  19. #ifndef __bit_ops_h__
  20. #define __bit_ops_h__
  21. typedef unsigned long MT_offset_t;
  22. typedef unsigned long MT_size_t;
  23. typedef unsigned char pseudo_bit_t;
  24. struct addr_64_st {
  25. __u32 addr_l;
  26. __u32 addr_h;
  27. };
  28. #define MT_BIT_OFFSET(object_struct,reg_path) \
  29. ((MT_offset_t) &( ((struct object_struct *)(0))-> reg_path ))
  30. #define MT_BIT_SIZE(object_struct,reg_path) \
  31. ((MT_size_t) sizeof( ((struct object_struct *)(0))-> reg_path ))
  32. #define MT_BIT_OFFSET_SIZE(object_struct,reg_path) \
  33. MT_BIT_OFFSET(object_struct,reg_path),MT_BIT_SIZE(object_struct,reg_path)
  34. #define MT_BYTE_OFFSET(object_struct,reg_path) \
  35. ((MT_offset_t) (MT_BIT_OFFSET(object_struct,reg_path)/8))
  36. #define MT_BYTE_SIZE(object_struct,reg_path) \
  37. ((MT_size_t) MT_BIT_SIZE(object_struct,reg_path)/8)
  38. #define MT_BYTE_OFFSET_SIZE(object_struct,reg_path) \
  39. MT_BYTE_OFFSET(object_struct,reg_path),MT_BYTE_SIZE(object_struct,reg_path)
  40. #define MT_STRUCT_SIZE(object_struct) (sizeof(struct object_struct) >> 3)
  41. /*****************************************************************************************
  42. * Bit manipulation macros
  43. *****************************************************************************************/
  44. /* MASK generate a bit mask S bits width */
  45. #define MASK32(S) ( ((__u32) ~0L) >> (32-(S)) )
  46. /*
  47. * BITS generate a bit mask with bits O+S..O set (assumes 32 bit integer).
  48. * numbering bits as following: 31........................76543210
  49. */
  50. #define BITS32(O,S) ( MASK32(S) << (O) )
  51. /*
  52. * MT_EXTRACT32 macro extracts S bits from (__u32)W with offset O
  53. * and shifts them O places to the right (right justifies the field extracted).
  54. */
  55. #define MT_EXTRACT32(W,O,S) ( ((W)>>(O)) & MASK32(S) )
  56. /*
  57. * MT_INSERT32 macro inserts S bits with offset O from field F into word W (__u32)
  58. */
  59. #define MT_INSERT32(W,F,O,S) ((W)= ( ( (W) & (~BITS32(O,S)) ) | (((F) & MASK32(S))<<(O)) ))
  60. /*
  61. * MT_EXTRACT_ARRAY32 macro is similar to EXTRACT but works on an array of (__u32),
  62. * thus offset may be larger than 32 (but not size).
  63. */
  64. #define MT_EXTRACT_ARRAY32(A,O,S) MT_EXTRACT32(((__u32*)A)[O >> 5],(O & MASK32(5)),S)
  65. /*
  66. * MT_EXTRACT_ARRAY32_BE macro is similar to EXTRACT but works on an array of (__u32),
  67. * thus offset may be larger than 32 (but not size).
  68. *
  69. * (added by mcb30)
  70. */
  71. #define MT_EXTRACT_ARRAY32_BE(A,O,S) MT_EXTRACT32(be32_to_cpu(((__u32*)A)[O >> 5]),(O & MASK32(5)),S)
  72. /*
  73. * MT_INSERT_ARRAY32 macro is similar to INSERT but works on an array of (__u32),
  74. * thus offset may be larger than 32 (but not size).
  75. */
  76. #define MT_INSERT_ARRAY32(A,F,O,S) MT_INSERT32(((__u32*)A)[O >> 5],F,(O & MASK32(5)),S)
  77. #define INS_FLD(src, a, st, fld) MT_INSERT_ARRAY32(a, src, MT_BIT_OFFSET(st, fld), MT_BIT_SIZE(st, fld))
  78. #define EX_FLD(a, st, fld) MT_EXTRACT_ARRAY32(a, MT_BIT_OFFSET(st, fld), MT_BIT_SIZE(st, fld))
  79. #define EX_FLD_BE(a, st, fld) MT_EXTRACT_ARRAY32_BE(a, MT_BIT_OFFSET(st, fld), MT_BIT_SIZE(st, fld))
  80. /* return the address of the dword holding the field
  81. buf = pointer to buffer where to place the value
  82. st = struct describing the buffer
  83. fld = field in the struct where to insert the value */
  84. #define FLD_DW_ADDR(buf, st, fld) ((__u32 *)((__u32 *)(buf)+(((__u32)(&(((struct st *)(0))->fld))) >> 5)))
  85. /*
  86. val = value to insert
  87. buf = pointer to buffer where to place the value
  88. st = struct describing the buffer
  89. fld = field in the struct where to insert the value */
  90. #define INS_FLD_TO_BE(val, buf, st, fld) \
  91. do { \
  92. *FLD_DW_ADDR(buf, st, fld) = be32_to_cpu(*FLD_DW_ADDR(buf, st, fld)); \
  93. INS_FLD(val, buf, st, fld); \
  94. *FLD_DW_ADDR(buf, st, fld) = cpu_to_be32(*FLD_DW_ADDR(buf, st, fld)); \
  95. } \
  96. while(0)
  97. #define EX_FLD_FROM_BE(buf, st, fld, type) \
  98. ({ \
  99. type field; \
  100. \
  101. *FLD_DW_ADDR(buf, st, fld) = be32_to_cpu(*FLD_DW_ADDR(buf, st, fld)); \
  102. field= EX_FLD(buf, st, fld); \
  103. *FLD_DW_ADDR(buf, st, fld) = cpu_to_be32(*FLD_DW_ADDR(buf, st, fld)); \
  104. \
  105. field; \
  106. })
  107. /* Remaining code Copyright Fen Systems Ltd. 2007 */
  108. /**
  109. * Wrapper structure for pseudo_bit_t structures
  110. *
  111. * This structure provides a wrapper around the autogenerated
  112. * pseudo_bit_t structures. It has the correct size, and also
  113. * encapsulates type information about the underlying pseudo_bit_t
  114. * structure, which allows the MLX_FILL etc. macros to work without
  115. * requiring explicit type information.
  116. */
  117. #define MLX_DECLARE_STRUCT( _structure ) \
  118. _structure { \
  119. union { \
  120. uint8_t bytes[ sizeof ( struct _structure ## _st ) / 8 ]; \
  121. uint32_t dwords[ sizeof ( struct _structure ## _st ) / 32 ]; \
  122. struct _structure ## _st *dummy[0]; \
  123. } u; \
  124. }
  125. /** Get pseudo_bit_t structure type from wrapper structure pointer */
  126. #define MLX_PSEUDO_STRUCT( _ptr ) \
  127. typeof ( *((_ptr)->u.dummy[0]) )
  128. /** Bit offset of a field within a pseudo_bit_t structure */
  129. #define MLX_BIT_OFFSET( _structure_st, _field ) \
  130. offsetof ( _structure_st, _field )
  131. /** Dword offset of a field within a pseudo_bit_t structure */
  132. #define MLX_DWORD_OFFSET( _structure_st, _field ) \
  133. ( MLX_BIT_OFFSET ( _structure_st, _field ) / 32 )
  134. /** Dword bit offset of a field within a pseudo_bit_t structure
  135. *
  136. * Yes, using mod-32 would work, but would lose the check for the
  137. * error of specifying a mismatched field name and dword index.
  138. */
  139. #define MLX_DWORD_BIT_OFFSET( _structure_st, _index, _field ) \
  140. ( MLX_BIT_OFFSET ( _structure_st, _field ) - ( 32 * (_index) ) )
  141. /** Bit width of a field within a pseudo_bit_t structure */
  142. #define MLX_BIT_WIDTH( _structure_st, _field ) \
  143. sizeof ( ( ( _structure_st * ) NULL )->_field )
  144. /** Bit mask for a field within a pseudo_bit_t structure */
  145. #define MLX_BIT_MASK( _structure_st, _field ) \
  146. ( ( ~( ( uint32_t ) 0 ) ) >> \
  147. ( 32 - MLX_BIT_WIDTH ( _structure_st, _field ) ) )
  148. /*
  149. * Assemble native-endian dword from named fields and values
  150. *
  151. */
  152. #define MLX_ASSEMBLE_1( _structure_st, _index, _field, _value ) \
  153. ( (_value) << MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  154. #define MLX_ASSEMBLE_2( _structure_st, _index, _field, _value, ... ) \
  155. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  156. MLX_ASSEMBLE_1 ( _structure_st, _index, __VA_ARGS__ ) )
  157. #define MLX_ASSEMBLE_3( _structure_st, _index, _field, _value, ... ) \
  158. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  159. MLX_ASSEMBLE_2 ( _structure_st, _index, __VA_ARGS__ ) )
  160. #define MLX_ASSEMBLE_4( _structure_st, _index, _field, _value, ... ) \
  161. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  162. MLX_ASSEMBLE_3 ( _structure_st, _index, __VA_ARGS__ ) )
  163. /*
  164. * Build native-endian (positive) dword bitmasks from named fields
  165. *
  166. */
  167. #define MLX_MASK_1( _structure_st, _index, _field ) \
  168. ( MLX_BIT_MASK ( _structure_st, _field ) << \
  169. MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  170. #define MLX_MASK_2( _structure_st, _index, _field, ... ) \
  171. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  172. MLX_MASK_1 ( _structure_st, _index, __VA_ARGS__ ) )
  173. #define MLX_MASK_3( _structure_st, _index, _field, ... ) \
  174. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  175. MLX_MASK_2 ( _structure_st, _index, __VA_ARGS__ ) )
  176. #define MLX_MASK_4( _structure_st, _index, _field, ... ) \
  177. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  178. MLX_MASK_3 ( _structure_st, _index, __VA_ARGS__ ) )
  179. /*
  180. * Populate big-endian dwords from named fields and values
  181. *
  182. */
  183. #define MLX_FILL( _ptr, _index, _assembled ) \
  184. do { \
  185. uint32_t *__ptr = &(_ptr)->u.dwords[(_index)]; \
  186. uint32_t __assembled = (_assembled); \
  187. *__ptr = cpu_to_be32 ( __assembled ); \
  188. } while ( 0 )
  189. #define MLX_FILL_1( _ptr, _index, ... ) \
  190. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  191. _index, __VA_ARGS__ ) )
  192. #define MLX_FILL_2( _ptr, _index, ... ) \
  193. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_2 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  194. _index, __VA_ARGS__ ) )
  195. #define MLX_FILL_3( _ptr, _index, ... ) \
  196. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_3 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  197. _index, __VA_ARGS__ ) )
  198. #define MLX_FILL_4( _ptr, _index, ... ) \
  199. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_4 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  200. _index, __VA_ARGS__ ) )
  201. /*
  202. * Modify big-endian dword using named field and value
  203. *
  204. */
  205. #define MLX_SET( _ptr, _field, _value ) \
  206. do { \
  207. unsigned int __index = \
  208. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  209. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  210. uint32_t __value = be32_to_cpu ( *__ptr ); \
  211. __value &= ~( MLX_MASK_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  212. __index, _field ) ); \
  213. __value |= MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  214. __index, _field, _value ); \
  215. *__ptr = cpu_to_be32 ( __value ); \
  216. } while ( 0 )
  217. /*
  218. * Extract value of named field
  219. *
  220. */
  221. #define MLX_GET( _ptr, _field ) \
  222. ( { \
  223. unsigned int __index = \
  224. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  225. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  226. uint32_t __value = be32_to_cpu ( *__ptr ); \
  227. __value >>= \
  228. MLX_DWORD_BIT_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), \
  229. __index, _field ); \
  230. __value &= \
  231. MLX_BIT_MASK ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  232. __value; \
  233. } )
  234. #endif /* __bit_ops_h__ */