summaryrefslogtreecommitdiffstats
path: root/src/import/generic/memory/lib/utils/find.H
blob: 3e67e765ae8f500fc51544814fe388473d172a8d (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/generic/memory/lib/utils/find.H $                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,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 find.H
/// @brief Templates for finding things
///
// *HWP HWP Owner: Andre Marin <aamarin@us.ibm.com>
// *HWP HWP Backup: Stephen Glancy <sglancy@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 3
// *HWP Consumed by: HB:FSP

#ifndef _MSS_FIND_H
#define _MSS_FIND_H

#include <fapi2.H>
#include <vector>
#include <generic/memory/lib/utils/c_str.H>
#include <generic/memory/lib/utils/shared/mss_generic_consts.H>

namespace mss
{
///
/// @brief Helper to find a set of elements based on a fapi2 target
/// @tparam M the target type to be returned
/// @tparam T the fapi2 target type of the argument
/// @param[in] i_target the fapi2 target T
/// @param[in] std::false_type tag dispatch if T != M
/// @param[in] i_state [optional] fapi2 target state (defaults to TARGET_STATE_FUNCTIONAL)
/// @return a vector of M targets.
///
template< fapi2::TargetType M, fapi2::TargetType T >
static inline std::vector< fapi2::Target<M> > find_targets_impl( const fapi2::Target<T>& i_target,
        std::false_type,
        fapi2::TargetState i_state )
{
    return i_target.template getChildren<M>(i_state);
}

///
/// @brief Helper to find a set of elements based on a fapi2 target
/// @tparam M the target type to be returned
/// @tparam T the fapi2 target type of the argument
/// @param[in] i_target the fapi2 target T
/// @param[in] std::true_type tag dispatch if T == M
/// @param[in] i_state [optional] fapi2 target state (defaults to TARGET_STATE_FUNCTIONAL)
/// @return a vector of M targets.
///
template< fapi2::TargetType M, fapi2::TargetType T >
static inline std::vector< fapi2::Target<M> > find_targets_impl( const fapi2::Target<T>& i_target,
        std::true_type,
        fapi2::TargetState i_state )
{
    return std::vector< fapi2::Target<M> > {i_target};
}

///
/// @brief find a set of elements based on a fapi2 target
/// @tparam M the target type to be returned
/// @tparam T the fapi2 target type of the argument
/// @param[in] i_target the fapi2 target T
/// @param[in] std::true_type tag dispatch if T == M
/// @param[in] i_state [optional] fapi2 target state (defaults to TARGET_STATE_FUNCTIONAL)
/// @return a vector of M targets.
/// @note Only works for valid parent-child relationships
/// So calling find_targets<TARGET_TYPE_DIMM>(l_mca) will work here
/// but calling find_targets<TARGET_TYPE_DIMM>(l_mcs) will not work
/// Compiler will freak out and we'll never get a bad relationship/ error at runtime
/// If we do, it's on fapi2
///
template< fapi2::TargetType M, fapi2::TargetType T >
inline std::vector< fapi2::Target<M> > find_targets( const fapi2::Target<T>& i_target,
        fapi2::TargetState i_state = fapi2::TARGET_STATE_FUNCTIONAL )
{
    return find_targets_impl<M>(i_target, std::integral_constant<bool, M == T> {}, i_state);
}

///
/// @brief Helper to find an element based on a fapi2 target
/// @tparam M the target type to be returned
/// @tparam T the fapi2 target type of the argument
/// @param[in] i_target the fapi2 target T
/// @param[in] std::false_type tag dispatch if T != M
/// @return an M target.
/// @note Only works for valid parent-child relationships
///
template< fapi2::TargetType M, fapi2::TargetType T >
static inline fapi2::Target<M> find_target_impl( const fapi2::Target<T>& i_target,
        std::false_type )
{
    return i_target.template getParent<M>();
}

///
/// @brief Helper to find an element based on a fapi2 target
/// @tparam M the target type to be returned
/// @tparam T the fapi2 target type of the argument
/// @param[in] i_target the fapi2 target T
/// @param[in] std::true_type tag dispatch if T == M
/// @return an M target.
/// @note Only works for valid parent-child relationships
///
template< fapi2::TargetType M, fapi2::TargetType T >
static inline fapi2::Target<M> find_target_impl( const fapi2::Target<T>& i_target,
        std::true_type )
{
    return i_target;
}

///
/// @brief find an element based on a fapi2 target
/// @tparam M the target type to be returned
/// @tparam T the fapi2 target type of the argument
/// @param[in] i_target the fapi2 target T
/// @return an M target.
/// @note Only works for valid parent-child relationships
/// Will work for MCA and DIMM
/// Will not work for MCS and DIMM
/// The compiler will let you know if it doesn't work
///
template< fapi2::TargetType M, fapi2::TargetType T >
inline fapi2::Target<M> find_target( const fapi2::Target<T>& i_target)
{
    return find_target_impl<M>(i_target, std::integral_constant<bool, M == T> {});
}

///
/// @brief find the McBIST given a DIMM
/// @param[in] i_target the fapi2 target DIMM
/// @return a McBIST target.
///
template<>
inline fapi2::Target<fapi2::TARGET_TYPE_MCBIST> find_target(const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target)
{
    return i_target.getParent<fapi2::TARGET_TYPE_MCA>().getParent<fapi2::TARGET_TYPE_MCBIST>();
}

///
/// @brief find the PROC_CHIP given a MBA
/// @param[in] i_target the fapi2 target MBA
/// @return a DMI target.
///
template<>
inline fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP> find_target(const fapi2::Target<fapi2::TARGET_TYPE_MBA>& i_target)
{
    return i_target.getParent<fapi2::TARGET_TYPE_MEMBUF_CHIP>()
           .getParent<fapi2::TARGET_TYPE_DMI>()
           .getParent<fapi2::TARGET_TYPE_PROC_CHIP>();
}

///
/// @brief find the PROC_CHIP given a OCMB_CHIP
/// @param[in] i_target the fapi2 target OCMB_CHIP
/// @return a PROC_CHIP target.
///
template<>
inline fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP> find_target(const fapi2::Target<fapi2::TARGET_TYPE_OCMB_CHIP>&
        i_target)
{
    return i_target.getParent<fapi2::TARGET_TYPE_OMI>()
           .getParent<fapi2::TARGET_TYPE_MC>()
           .getParent<fapi2::TARGET_TYPE_PROC_CHIP>();
}

///
/// @brief find the DMI given an MBA
/// @param[in] i_target the fapi2 target MBA
/// @return a DMI target.
///
template<>
inline fapi2::Target<fapi2::TARGET_TYPE_DMI> find_target(const fapi2::Target<fapi2::TARGET_TYPE_MBA>& i_target)
{
    return i_target.getParent<fapi2::TARGET_TYPE_MEMBUF_CHIP>().getParent<fapi2::TARGET_TYPE_DMI>();
}

///
/// @brief find the PROC given a MEMBUF
/// @param[in] i_target the fapi2 target MEMBUF
/// @return a PROC target.
///
template<>
inline fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP> find_target(const fapi2::Target<fapi2::TARGET_TYPE_MEMBUF_CHIP>&
        i_target)
{
    return i_target.getParent<fapi2::TARGET_TYPE_DMI>().getParent<fapi2::TARGET_TYPE_PROC_CHIP>();
}

///
/// @brief find all the OCMB_CHIPs connected to a PROC_CHIP
/// @param[in] i_target a fapi2::Target PROC_CHIP
/// @return a vector of fapi2::TARGET_TYPE_OCMB_CHIP
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_OCMB_CHIP> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_OCMB_CHIP> > l_ocmbs;

    for (const auto& l_mc : i_target.getChildren<fapi2::TARGET_TYPE_MC>(i_state))
    {
        for (const auto& l_omi : l_mc.getChildren<fapi2::TARGET_TYPE_OMI>(i_state))
        {
            auto l_these_ocmbs( l_omi.getChildren<fapi2::TARGET_TYPE_OCMB_CHIP>(i_state) );
            l_ocmbs.insert(l_ocmbs.end(), l_these_ocmbs.begin(), l_these_ocmbs.end());
        }
    }

    return l_ocmbs;
}

///
/// @brief find all the DIMMS connected to an MI
/// @param[in] i_target a fapi2::Target MI
/// @return a vector of fapi2::TARGET_TYPE_DIMM
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_MI>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> > l_dimms;

    for (const auto& l_omi : i_target.getChildren<fapi2::TARGET_TYPE_OMI>(i_state))
    {
        for (const auto& l_ocmb : l_omi.getChildren<fapi2::TARGET_TYPE_OCMB_CHIP>(i_state))
        {
            auto l_these_dimms( l_ocmb.getChildren<fapi2::TARGET_TYPE_DIMM>(i_state) );
            l_dimms.insert(l_dimms.end(), l_these_dimms.begin(), l_these_dimms.end());
        }
    }

    return l_dimms;
}

///
/// @brief find all the OCMB_CHIPs connected to an MI
/// @param[in] i_target a fapi2::Target MI
/// @return a vector of fapi2::TARGET_TYPE_OCMB_CHIP
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_OCMB_CHIP> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_MI>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_OCMB_CHIP> > l_ocmbs;

    for (const auto& l_omi : i_target.getChildren<fapi2::TARGET_TYPE_OMI>(i_state))
    {
        auto l_these_ocmbs( l_omi.getChildren<fapi2::TARGET_TYPE_OCMB_CHIP>(i_state) );
        l_ocmbs.insert(l_ocmbs.end(), l_these_ocmbs.begin(), l_these_ocmbs.end());
    }

    return l_ocmbs;
}

///
/// @brief find all the MEM_PORTs connected to a PROC_CHIP
/// @param[in] i_target a fapi2::Target PROC_CHIP
/// @return a vector of fapi2::TARGET_TYPE_MEM_PORT
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_MEM_PORT> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_MEM_PORT> > l_ports;

    for (const auto& l_mc : i_target.getChildren<fapi2::TARGET_TYPE_MC>(i_state))
    {
        for (const auto& l_omi : l_mc.getChildren<fapi2::TARGET_TYPE_OMI>(i_state))
        {
            for (const auto& l_ocmb : l_omi.getChildren<fapi2::TARGET_TYPE_OCMB_CHIP>(i_state))
            {
                auto l_these_ports( l_ocmb.getChildren<fapi2::TARGET_TYPE_MEM_PORT>(i_state) );
                l_ports.insert(l_ports.end(), l_these_ports.begin(), l_these_ports.end());
            }
        }
    }

    return l_ports;
}

///
/// @brief find all the MBA connected to an DMI
/// @param[in] i_target a fapi2::Target DMI
/// @return a vector of fapi2::TARGET_TYPE_MBA
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_MBA> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_DMI>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_MBA> > l_mbas;

    for (const auto& membuf_chip : i_target.getChildren<fapi2::TARGET_TYPE_MEMBUF_CHIP>(i_state))
    {
        auto l_these_mbas( membuf_chip.getChildren<fapi2::TARGET_TYPE_MBA>(i_state) );
        l_mbas.insert(l_mbas.end(), l_these_mbas.begin(), l_these_mbas.end());
    }

    return l_mbas;
}

///
/// @brief find all the DIMM connected to a centaur
/// @param[in] i_target a fapi2::Target TARGET_TYPE_MEMBUF_CHIP
/// @return a vector of fapi2::TARGET_TYPE_DIMM
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_MEMBUF_CHIP>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> > l_dimms;

    for (const auto& l_mba : i_target.getChildren<fapi2::TARGET_TYPE_MBA>(i_state))
    {
        auto l_these_dimms( l_mba.getChildren<fapi2::TARGET_TYPE_DIMM>(i_state) );
        l_dimms.insert(l_dimms.end(), l_these_dimms.begin(), l_these_dimms.end());
    }

    return l_dimms;
}

///
/// @brief find all the dimm connected to an MCS
/// @param[in] i_target a fapi2::Target MCS
/// @return a vector of fapi2::TARGET_TYPE_DIMM
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_MCS>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> > l_dimms;

    for (const auto& p : i_target.getChildren<fapi2::TARGET_TYPE_MCA>(i_state))
    {
        auto l_these_dimms( p.getChildren<fapi2::TARGET_TYPE_DIMM>(i_state) );
        l_dimms.insert(l_dimms.end(), l_these_dimms.begin(), l_these_dimms.end());
    }

    return l_dimms;
}

///
/// @brief find all the dimms connected to an MCBIST
/// @param[in] i_target a fapi2::Target MCBIST
/// @return a vector of fapi2::TARGET_TYPE_DIMM
///
template<>
inline std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> >
find_targets( const fapi2::Target<fapi2::TARGET_TYPE_MCBIST>& i_target,
              fapi2::TargetState i_state )
{
    std::vector< fapi2::Target<fapi2::TARGET_TYPE_DIMM> > l_dimms;

    for (const auto& p : i_target.getChildren<fapi2::TARGET_TYPE_MCA>(i_state))
    {
        auto l_these_dimms( p.getChildren<fapi2::TARGET_TYPE_DIMM>(i_state) );
        l_dimms.insert(l_dimms.end(), l_these_dimms.begin(), l_these_dimms.end());
    }

    return l_dimms;
}

///
/// @brief find the MCS given a DIMM
/// @param[in] i_target the fapi2 target DIMM
/// @return a MCS target.
///
template<>
inline fapi2::Target<fapi2::TARGET_TYPE_MCS> find_target( const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target)
{
    return i_target.getParent<fapi2::TARGET_TYPE_MCA>().getParent<fapi2::TARGET_TYPE_MCS>();
}

///
/// @brief find a key value from a vector of STL pairs
/// @tparam T  input type
/// @tparam OT the output type to be returned
/// @param[in] i_vector_of_pairs the input vector of pairs
/// @param[in] i_key the "map" key
/// @param[out] o_value the value found from given key
/// @return true if value is found, false otherwise
/// @note VECTOR NEEDS TO BE IN SORTED ORDER
///
template<typename T, typename OT>
bool find_value_from_key(const std::vector<std::pair<T, OT> >& i_vector_of_pairs,
                         const T& i_key,
                         OT& o_value)
{
    // Comparator lambda expression
    const auto compare = [](const std::pair<T, OT>& i_lhs, const T & i_key)
    {
        return (i_lhs.first < i_key);
    };

    // Find iterator to matching key (if it exists)
    const auto l_value_iterator  =  std::lower_bound(i_vector_of_pairs.begin(),
                                    i_vector_of_pairs.end(),
                                    i_key,
                                    compare);

    // Did you find it? Let me know.
    if( (l_value_iterator == i_vector_of_pairs.end()) || (i_key != l_value_iterator->first) )
    {
        // Static cast ensures that the below print will not fail at compile time
        FAPI_INF("Did not find a mapping value to key: %d", static_cast<uint64_t>(i_key));
        return false;
    }

    o_value = l_value_iterator->second;
    return true;

}// find_value_from_key

///
/// @brief find a key value from a vector of STL pairs
/// @tparam T  input type
/// @tparam OT the output type to be returned
/// @param[in] i_vector_of_pairs the input vector of pairs
/// @param[in] i_value the "map" value, the second entry in the pairs
/// @param[out] o_key the first entry in the pair
/// @return true if value is found, false otherwise
///
template<typename T, typename OT>
bool find_key_from_value(const std::vector<std::pair<T, OT> >& i_vector_of_pairs,
                         const OT& i_value,
                         T& o_key)
{
    // Comparator lambda expression
    const auto compare = [&i_value](const std::pair<T, OT>& i_lhs)
    {
        return (i_lhs.second == i_value);
    };

    // Find iterator to matching key (if it exists)
    const auto l_value_iterator  =  std::find_if(i_vector_of_pairs.begin(),
                                    i_vector_of_pairs.end(),
                                    compare);

    // Did you find it? Let me know.
    if( (l_value_iterator == i_vector_of_pairs.end()) || (i_value != l_value_iterator->second) )
    {
        // Static cast ensures that the below print will not fail at compile time
        FAPI_INF("Did not find a mapping key to value: %d", static_cast<uint64_t>(i_value));
        return false;
    }

    o_key = l_value_iterator->first;
    return true;

}// find_value_from_key

///
/// @brief find a key value from a C-style array of STL pairs
/// @tparam T  input type
/// @tparam OT the output type to be returned
/// @tparam N size of the array being passed in
/// @param[in] i_array the input array of pairs
/// @param[in] i_key the "map" key
/// @param[in] o_value the value found from given key
/// @return true if value is found, false otherwise
/// @note To use on short arrays. O(N), simple search
///
template<typename T, typename OT, size_t N>
bool find_value_from_key( const std::pair<T, OT> (&i_array)[N],
                          const T& i_key,
                          OT& o_value)
{
    // TK Use sort and binary search for larger arrays
    for (size_t i = 0; i < N; i++)
    {
        if (i_array[i].first == i_key)
        {
            o_value = i_array[i].second;
            return true;
        }
    }

    FAPI_ERR ("No match found for find_value_from_key");
    return false;
}

///
/// @brief Mapping boilerplate check
/// @tparam T FAPI2 target type
/// @tparam IT map key type
/// @tparam OT map value type
/// @param[in] i_target the FAPI target
/// @param[in] i_map SPD to attribute data mapping
/// @param[in] i_ffdc_code FFDC function code
/// @param[in] i_key Key to query map
/// @param[out] o_output value from key
/// @return FAPI2_RC_SUCCESS iff okay
///
template< fapi2::TargetType T, typename IT, typename OT >
inline fapi2::ReturnCode lookup_table_check(const fapi2::Target<T>& i_target,
        const std::vector<std::pair<IT, OT>>& i_map,
        const generic_ffdc_codes i_ffdc_code,
        const IT i_key,
        OT& o_output)
{
    const bool l_is_val_found = mss::find_value_from_key(i_map, i_key, o_output);

    FAPI_ASSERT( l_is_val_found,
                 fapi2::MSS_LOOKUP_FAILED()
                 .set_KEY(i_key)
                 .set_DATA(o_output)
                 .set_FUNCTION(i_ffdc_code)
                 .set_TARGET(i_target),
                 "Failed to find a mapped value for %d on %s",
                 i_key,
                 mss::spd::c_str(i_target) );
fapi_try_exit:
    return fapi2::current_err;
}

}// mss

#endif
OpenPOWER on IntegriCloud