summaryrefslogtreecommitdiffstats
path: root/src/import/generic/memory/lib/utils/mss_field.H
blob: d6bdeaaedb2e0b972fa1218071737e1ca28a3b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/generic/memory/lib/utils/mss_field.H $             */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2018,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file mss_field.H
/// @brief API for data fields and operations
///

// *HWP HWP Owner: Andre Marin <aamarin@us.ibm.com>
// *HWP HWP Backup: Louis Stermole <stermole@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 2
// *HWP Consumed by: HB:FSP

#ifndef _MSS_FIELD_H_
#define _MSS_FIELD_H_

#ifdef __PPE__
    #include <mss_generic_check.H>
#else
    #include <generic/memory/lib/utils/shared/mss_generic_consts.H>
    #include <generic/memory/lib/utils/mss_generic_check.H>
#endif

//Macro
#ifdef __PPE__
    #define TARGIDFORMAT "0x%08X"
    #define TARGTID i_target.get()
#else
    #define TARGIDFORMAT "%s"
    #define TARGTID spd::c_str(i_target)
#endif

namespace mss
{

///
/// @brief endian fields for use as a template selecor
///
enum class endian
{
    BIG,
    LITTLE,
};

///
/// @class field_t
/// @brief data structure for byte fields
/// @tparam E endian type for this field
/// @note holds byte index, start bit, and bit length of a decoded field
///
template< endian E >
class field_t
{
    private:

        const size_t iv_byte;
        const size_t iv_start;
        const size_t iv_length;

    public:

        // default ctor deleted
        field_t() = delete;

        ///
        /// @brief ctor
        /// @param[in] i_byte_index
        /// @param[in] i_start_bit
        /// @param[in] i_bit_length
        ///
        constexpr field_t(const size_t i_byte_index,
                          const size_t i_start_bit,
                          const size_t i_bit_length)
            : iv_byte(i_byte_index),
              iv_start(i_start_bit),
              iv_length(i_bit_length)
        {}

        ///
        /// @brief default dtor
        ///
        ~field_t() = default;

        ///
        /// @brief Byte index getter
        /// @return the byte index for this field
        ///
        const size_t get_byte(const std::vector<uint8_t>& i_data) const;

        ///
        /// @brief Starting bit getter
        /// @return the starting bit position for this field
        ///
        constexpr size_t get_start() const
        {
            return iv_start;
        }

        ///
        /// @brief bit length getter
        /// @return the bit length of this field
        ///
        constexpr size_t get_length() const
        {
            return iv_length;
        }

};// field_t

///
/// @brief Byte index getter - big endian specialization
/// @return the byte index for this field
///
template<>
inline const size_t field_t<mss::endian::BIG>::get_byte(const std::vector<uint8_t>& i_data) const
{
    return ( i_data.size() - 1 ) - iv_byte;
}

///
/// @brief Byte index getter - big endian specialization
/// @return the byte index for this field
///
template<>
inline const size_t field_t<mss::endian::LITTLE>::get_byte(const std::vector<uint8_t>& i_data) const
{
    return iv_byte;
}

///
/// @brief Checks input field against a custom conditional
/// @tparam T field input type
/// @tparam F Callable object type
/// @param[in] i_field Extracted field
/// @param[in] i_comparison_val value we are comparing against
/// @param[in] i_op comparison operator function object
/// @return boolean true or false
///
template < typename T, typename F >
inline bool conditional(const T i_field,
                        const size_t i_comparison_val,
                        const F i_op)
{
    return i_op(i_field, i_comparison_val);
}

///
/// @brief Helper function to extract byte information
/// @tparam E endian type
/// @tparam F the field to extract
/// @tparam T the fapi2 target type
/// @tparam IT data input type
/// @tparam OT data output type
/// @tparam FFDC ffdc code type
/// @param[in] i_target the fapi2 target
/// @param[in] i_data the data
/// @param[in] i_ffdc_codes FFDC code
/// @param[out] o_value raw value for this field
/// @return FAPI2_RC_SUCCESS iff okay
///
template< mss::endian E,
          const mss::field_t<E>& F,
          fapi2::TargetType T,
          typename IT,
          typename OT,
          typename FFDC >
inline fapi2::ReturnCode get_field(const fapi2::Target<T>& i_target,
                                   const std::vector<IT>& i_data,
                                   const FFDC i_ffdc_codes,
                                   OT& o_value)
{
    // Initializes the output value to 0, that way, we won't have any stale data in o_value
    o_value = 0;

    const size_t BYTE = F.get_byte(i_data);

    FAPI_ASSERT( BYTE < i_data.size(),
                 fapi2::MSS_OUT_OF_BOUNDS_INDEXING()
                 .set_INDEX(BYTE)
                 .set_LIST_SIZE(i_data.size())
                 .set_FUNCTION(i_ffdc_codes)
                 .set_TARGET(i_target),
                 "Out of bounds indexing (with %d) on a list of size %d for " TARGIDFORMAT,
                 BYTE,
                 i_data.size(),
                 TARGTID);

    {
        // Extracting desired bits
        const fapi2::buffer<OT> l_buffer(i_data[BYTE]);
        l_buffer.template extractToRight<F.get_start(), F.get_length()>(o_value);

        FAPI_DBG(TARGIDFORMAT " data[%d] = 0x%02x. Field with start bit %d, bit len %d, has data 0x%02x.",
                 TARGTID,
                 BYTE,
                 i_data[BYTE],
                 F.get_start(),
                 F.get_length(),
                 o_value);
    }

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}
#ifndef __PPE__

///
/// @brief Helper function to set byte field information
/// @tparam E endian type
/// @tparam F the field to extract
/// @tparam T the fapi2 target type
/// @tparam IT data input type
/// @tparam OT data output type
/// @tparam FFDC ffdc code type
/// @param[in] i_target the fapi2 target
/// @param[in] i_setting the setting to set
/// @param[in] i_ffdc_codes FFDC code
/// @param[in,out] io_data the data to modify
/// @return FAPI2_RC_SUCCESS iff okay
///
template< mss::endian E,
          const mss::field_t<E>& F,
          fapi2::TargetType T,
          typename IT,
          typename OT,
          typename FFDC >
inline fapi2::ReturnCode set_field(const fapi2::Target<T>& i_target,
                                   const IT i_setting,
                                   const FFDC i_ffdc_codes,
                                   std::vector<OT>& io_data)
{
    const size_t BYTE = F.get_byte(io_data);

    FAPI_ASSERT( BYTE < io_data.size(),
                 fapi2::MSS_OUT_OF_BOUNDS_INDEXING()
                 .set_INDEX(BYTE)
                 .set_LIST_SIZE(io_data.size())
                 .set_FUNCTION(i_ffdc_codes)
                 .set_TARGET(i_target),
                 "Out of bounds indexing (with %d) on a list of size %d for %s",
                 BYTE,
                 io_data.size(),
                 spd::c_str(i_target));

    {
        // Insert desired setting
        fapi2::buffer<OT> l_buffer(io_data[BYTE]);
        l_buffer.template insertFromRight<F.get_start(), F.get_length()>(i_setting);

        io_data[BYTE] = static_cast<OT>(l_buffer);

        FAPI_DBG("%s data[%d] = 0x%02x. Field with start bit %d, bit len %d, has data 0x%02x.",
                 spd::c_str(i_target),
                 BYTE,
                 io_data[BYTE],
                 F.get_start(),
                 F.get_length(),
                 i_setting);
    }

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}
#endif

///
/// @brief byte field reader
/// @tparam E endian type
/// @tparam F the byte field to read
/// @tparam TT traits associated with F - required
/// @tparam T the fapi2 target type
/// @tparam IT data input type
/// @tparam OT data output type
/// @param[in] i_target the dimm target
/// @param[in] i_data the data
/// @param[in] i_ffdc_codes FFDC code
/// @param[out] o_value raw value for this field
/// @return FAPI2_RC_SUCCESS iff okay
///
template< mss::endian E,
          const mss::field_t<E>& F,
          typename TT,
          fapi2::TargetType T,
          typename IT,
          typename OT,
          typename FFDC >
inline fapi2::ReturnCode get_field( const fapi2::Target<T>& i_target,
                                    const std::vector<IT>& i_data,
                                    const FFDC i_ffdc_codes,
                                    OT& o_value )
{
    IT l_temp = 0;
    FAPI_TRY( (get_field<E, F>(i_target, i_data, i_ffdc_codes, l_temp)),
              "Failed get_field() for " TARGIDFORMAT, TARGTID );

    // Test if retrieved data seems valid
    FAPI_TRY( check::invalid_value(i_target,
                                   conditional( l_temp,
                                           TT::COMPARISON_VAL,
                                           typename TT::template COMPARISON_OP<IT>() ),
                                   F.get_byte(i_data),
                                   l_temp,
                                   i_ffdc_codes),
              "Failed fail_for_invalid_value() for " TARGIDFORMAT, TARGTID );


    // Output should only change if data check passes
    o_value = static_cast<OT>(l_temp);
    FAPI_ASSERT( o_value == l_temp,
                 fapi2::MSS_CONVERSION_ERROR()
                 .set_ORIGINAL_VAL(l_temp)
                 .set_CONVERTED_VAL(o_value)
                 .set_TARGET(i_target)
                 .set_FUNCTION(i_ffdc_codes),
                 "Conversion error between original %d to converted %d value for " TARGIDFORMAT,
                 l_temp, o_value, TARGTID);

    FAPI_DBG("%s: 0x%02x for %s",
             TT::FIELD_STR,
             o_value,
             spd::c_str(i_target));


fapi_try_exit:
    return fapi2::current_err;
}

#ifndef __PPE__
///
/// @brief byte field writer
/// @tparam E endian type
/// @tparam F the byte field to read
/// @tparam TT traits associated with writer
/// @tparam T the fapi2 target type
/// @tparam IT data input type
/// @tparam OT data output type
/// @tparam FFDC ffdc code type
/// @param[in] i_target the dimm target
/// @param[in] i_setting value to set for field
/// @param[in] i_ffdc_codes FFDC code
/// @param[in,out] io_data the data to modify
/// @return FAPI2_RC_SUCCESS iff okay
///
template< mss::endian E,
          const mss::field_t<E>& F,
          typename TT,
          fapi2::TargetType T,
          typename IT,
          typename OT,
          typename FFDC >
inline fapi2::ReturnCode set_field( const fapi2::Target<T>& i_target,
                                    const IT i_setting,
                                    const FFDC i_ffdc_codes,
                                    std::vector<OT>& io_data )
{
    const size_t BYTE = F.get_byte(io_data);

    // Test if the data we want to set is valid for this field
    FAPI_TRY( check::invalid_value(i_target,
                                   conditional( i_setting,
                                           TT::COMPARISON_VAL,
                                           typename TT::template COMPARISON_OP<IT>() ),
                                   BYTE,
                                   i_setting,
                                   i_ffdc_codes),
              "Failed fail_for_invalid_value() for %s", spd::c_str(i_target) );

    FAPI_TRY( (set_field<E, F>(i_target, i_setting, i_ffdc_codes, io_data)),
              "Failed set_field() for %s", spd::c_str(i_target) );

    FAPI_DBG("%s: Set value of 0x%02x. Data for buffer at byte %d, is now 0x%02x for %s",
             TT::FIELD_STR,
             i_setting,
             BYTE,
             io_data[BYTE],
             spd::c_str(i_target));

fapi_try_exit:
    return fapi2::current_err;
}
#endif

}// mss

#endif
OpenPOWER on IntegriCloud