<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/generic/memory/lib/spd/spd_checker.H $             */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2017,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 spd_checker.H
/// @brief SPD functions to check boundaries
///
// *HWP HWP Owner: <NAME> <<EMAIL>>
// *HWP HWP Backup: <NAME> <<EMAIL>>
// *HWP Team: Memory
// *HWP Level: 3
// *HWP Consumed by: HB:FSP

#ifndef _SPD_CHECKER_H_
#define _SPD_CHECKER_H_

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

namespace mss
{

///
/// @brief Selectors for timing limits
/// @note BITS12 stands for a 12 bit range
/// @note BITS16 stands for a 16 bit range
///
enum bit_len
{
    BITS12 = 12,
    BITS16 = 16,
};

///
/// @class bitRangeTraits
/// @brief Traits class to select
/// @tparam T bit_len selector
///
template <bit_len T>
struct bitRangeTraits;

///
/// @class bitRangeTraits
/// @brief Traits class to select - BITS12 specialization
///
template<>
struct bitRangeTraits<BITS12>
{
    enum
    {
        LOWER_BOUND = 0x1,
        UPPER_BOUND = 0xfff,
    };
};

///
/// @class bitRangeTraits
/// @brief Traits class to select - BITS16 specialization
///
template<>
struct bitRangeTraits<BITS16>
{
    enum
    {
        LOWER_BOUND = 0x1,
        UPPER_BOUND = 0xffff,
    };
};

namespace spd
{
namespace check
{

///
/// @brief SPD timing boundary check
/// @tparam BL bit_len selector
/// @tparam T the TargetType
/// @tparam TT defaulted to bitRangeTraits<TB>
/// @param[in] i_target fapi2 target
/// @param[in] i_timing the timing value
/// @param[in] i_ffdc ffdc function code
/// @return FAPI2_RC_SUCCESS iff okay
///
template < bit_len BL, fapi2::TargetType T, typename TT = bitRangeTraits<BL> >
fapi2::ReturnCode max_timing_range(const fapi2::Target<T>& i_target,
                                   const int64_t i_timing,
                                   const generic_ffdc_codes i_ffdc)
{
    FAPI_ASSERT( (i_timing <= TT::UPPER_BOUND) &&
                 (i_timing >= TT::LOWER_BOUND),
                 fapi2::MSS_SPD_TIMING_FAIL()
                 .set_FUNCTION_CODE(i_ffdc)
                 .set_TARGET(i_target),
                 "Failed timing parameter check for %s",
                 spd::c_str(i_target));

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Checks conditional passes and implements traces & exits if it fails
/// @tparam T input data of any size
/// @param[in] i_target fapi2 dimm target
/// @param[in] i_conditional conditional that we are testing against
/// @param[in] i_spd_byte_index current SPD byte
/// @param[in] i_spd_data debug data
/// @param[in] i_err_str error string to print out when conditional fails
/// @return FAPI2_RC_SUCCESS iff okay
///
template< typename T >
inline fapi2::ReturnCode fail_for_invalid_value(const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target,
        const bool i_conditional,
        const size_t i_spd_byte_index,
        const T i_spd_data,
        const char* i_err_str = "")
{
    FAPI_ASSERT(i_conditional,
                fapi2::MSS_BAD_SPD().
                set_VALUE(i_spd_data).
                set_BYTE(i_spd_byte_index).
                set_DIMM_TARGET(i_target),
                "%s %s Byte %d, data 0x%02x, extracted value: 0x%02x.",
                spd::c_str(i_target),
                i_err_str,
                i_spd_byte_index,
                i_spd_data);

    return fapi2::FAPI2_RC_SUCCESS;
fapi_try_exit:
    return fapi2::current_err;

} // fail_for_invalid_value

///
/// @brief Helper function to test if a DRAM generation is valid
/// @param[in] i_dram_gen the DRAM gen from SPD
/// @return true if this DRAM gen is valid, else false
///
static inline bool is_dram_gen_valid(const uint8_t i_dram_gen)
{
    bool l_result = false;

    switch(i_dram_gen)
    {
        case DDR4:
            l_result = true;
            break;

        default:<commit_msg>Generalize byte reading from SPD reading, for exp i2c reuse<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/generic/memory/lib/spd/spd_checker.H $             */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2017,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 spd_checker.H
/// @brief SPD functions to check boundaries
///
// *HWP HWP Owner: <NAME> <<EMAIL>>
// *HWP HWP Backup: <NAME> <<EMAIL>>
// *HWP Team: Memory
// *HWP Level: 3
// *HWP Consumed by: HB:FSP

#ifndef _SPD_CHECKER_H_
#define _SPD_CHECKER_H_

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

namespace mss
{

///
/// @brief Selectors for timing limits
/// @note BITS12 stands for a 12 bit range
/// @note BITS16 stands for a 16 bit range
///
enum bit_len
{
    BITS12 = 12,
    BITS16 = 16,
};

///
/// @class bitRangeTraits
/// @brief Traits class to select
/// @tparam T bit_len selector
///
template <bit_len T>
struct bitRangeTraits;

///
/// @class bitRangeTraits
/// @brief Traits class to select - BITS12 specialization
///
template<>
struct bitRangeTraits<BITS12>
{
    enum
    {
        LOWER_BOUND = 0x1,
        UPPER_BOUND = 0xfff,
    };
};

///
/// @class bitRangeTraits
/// @brief Traits class to select - BITS16 specialization
///
template<>
struct bitRangeTraits<BITS16>
{
    enum
    {
        LOWER_BOUND = 0x1,
        UPPER_BOUND = 0xffff,
    };
};

namespace spd
{
namespace check
{

///
/// @brief SPD timing boundary check
/// @tparam BL bit_len selector
/// @tparam T the TargetType
/// @tparam TT defaulted to bitRangeTraits<TB>
/// @param[in] i_target fapi2 target
/// @param[in] i_timing the timing value
/// @param[in] i_ffdc ffdc function code
/// @return FAPI2_RC_SUCCESS iff okay
///
template < bit_len BL, fapi2::TargetType T, typename TT = bitRangeTraits<BL> >
fapi2::ReturnCode max_timing_range(const fapi2::Target<T>& i_target,
                                   const int64_t i_timing,
                                   const generic_ffdc_codes i_ffdc)
{
    FAPI_ASSERT( (i_timing <= TT::UPPER_BOUND) &&
                 (i_timing >= TT::LOWER_BOUND),
                 fapi2::MSS_SPD_TIMING_FAIL()
                 .set_FUNCTION_CODE(i_ffdc)
                 .set_TARGET(i_target),
                 "Failed timing parameter check for %s",
                 spd::c_str(i_target));

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Helper function to test if a DRAM generation is valid
/// @param[in] i_dram_gen the DRAM gen from SPD
/// @return true if this DRAM gen is valid, else false
///
static inline bool is_dram_gen_valid(const uint8_t i_dram_gen)
{
    bool l_result = false;

    switch(i_dram_gen)
    {
        case DDR4:
            l_result = true;
            break;

        default:
{'fraction_non_alphanumeric': 0.10241498275012322, 'fraction_numerical': 0.012814194184327254, 'mean_word_length': 1.4313443565780015, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2551524', 'n_tokens_mistral': 2886, 'n_tokens_neox': 2756, 'n_words': 784}