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