<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/bootloader/bl_console.C $                                 */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2019,2020                        */
/* [+] 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 bl_console.C
 *
 *  @brief Bootloader console functions to write data to lpc console
 */

#include <bootloader/bl_console.H>
#include <sys/task.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <lpc/lpc_const.H>
#include <uart.H>
#include <uartconfig.C>
#include <p10_sbe_hb_structures.H>
#include <util/align.H>
#include <arch/ppc.H>
#include <kernel/timemgr.H>

using namespace CONSOLE;
using namespace UARTREGS;

bool bl_console::console_fail = false;

void computeOpbmErrSev(OpbmErrReg_t i_opbmErrData,
                       ResetLevels &o_resetLevel)
{
    o_resetLevel = RESET_CLEAR;

    // First check the soft errors
    if (i_opbmErrData.rxits || i_opbmErrData.rxicmd || i_opbmErrData.rxiaa ||
        i_opbmErrData.rxopbe || i_opbmErrData.rxicmdb || i_opbmErrData.rxidatab)
    {
        o_resetLevel = RESET_OPB_LPCHC_SOFT;
    }

    // Now look for hard errors that will override soft errors reset level
    if (i_opbmErrData.rxopbt || i_opbmErrData.rxiaddr || i_opbmErrData.rxctgtel)
    {
        o_resetLevel = RESET_OPB_LPCHC_HARD;
    }
}

void computeLpchcErrSev(LpchcErrReg_t i_lpchcErrData,
                        ResetLevels &o_resetLevel)
{
    o_resetLevel = RESET_CLEAR;

    // First check the soft errors
    // All of these errors are set from bad LPC end points. Setting all to soft
    if (i_lpchcErrData.lreset || i_lpchcErrData.syncab ||
        i_lpchcErrData.syncnr || i_lpchcErrData.syncne ||
        i_lpchcErrData.syncto || i_lpchcErrData.tctar || i_lpchcErrData.mctar)
    {
        o_resetLevel = RESET_OPB_LPCHC_SOFT;
    }
}

bool checkForLpcErrors()
{
    uint32_t opbm_buffer = 0;
    uint32_t lpchc_buffer = 0;
    OpbmErrReg_t opbm_err_union;
    opbm_err_union.data32 = 0x0;
    LpchcErrReg_t lpchc_err_union;
    lpchc_err_union.data32 = 0x0;
    ResetLevels l_opbmResetLevel = RESET_CLEAR;
    ResetLevels l_lpchcResetLevel = RESET_CLEAR;

    uint64_t l_pnorEnd = g_blData->blToHbData.lpcBAR + LPC::LPCHC_FW_SPACE +
        PNOR::LPC_TOP_OF_FLASH_OFFSET;

    uint64_t l_mmioAddr = l_pnorEnd - PNOR::TOC_OFFSET_FROM_TOP_OF_FLASH;
    l_mmioAddr = ALIGN_PAGE_DOWN(l_mmioAddr);

    // First do a dummy LPC access (if an LPC error condition exists,
    // an access can be necessary to get the error indicated in the
    // status register. This read will force the error condition to
    // properly be shown in the LPC error status reg
    Bootloader::handleMMIO(l_mmioAddr,
                           reinterpret_cast<uintptr_t>(&opbm_buffer) +
                           getHRMOR(),
                           Bootloader::WORDSIZE,
                           Bootloader::WORDSIZE,
                           Bootloader::READ);

    uint32_t l_opb_status_addr = OPBM_ACCUM_STATUS_REG;
    uint64_t src_addr = g_blData->blToHbData.lpcBAR + LPC::LPCHC_ERR_SPACE +
        l_opb_status_addr;
    Bootloader::handleMMIO(src_addr,
                           reinterpret_cast<uintptr_t>(&opbm_buffer) +
                           getHRMOR(),
                           Bootloader::WORDSIZE, Bootloader::WORDSIZE,
                           Bootloader::READ);

    uint32_t l_lpchc_addr = LPCHC_REG;
    src_addr = g_blData->blToHbData.lpcBAR + LPC::LPCHC_ERR_SPACE + l_lpchc_addr;
    Bootloader::handleMMIO(src_addr,
                           reinterpret_cast<uintptr_t>(&lpchc_buffer) +
                           getHRMOR(),
                           Bootloader::WORDSIZE, Bootloader::WORDSIZE,
                           Bootloader::READ);

    // Mask error bits
    opbm_err_union.data32 = (opbm_buffer & LPC::OPB_ERROR_MASK);
    lpchc_err_union.data32 = (lpchc_buffer & LPCHC_ERROR_MASK);

    if (opbm_err_union.data32 != 0)
    {
        computeOpbmErrSev(opbm_err_union, l_opbmResetLevel);

        if (l_opbmResetLevel != RESET_CLEAR)
        {
            bl_console::console_fail = true;
        }
    }

    if (lpchc_err_union.data32 != 0)
    {
        computeLpchcErrSev(lpchc_err_union, l_lpchcResetLevel);

        if (l_lpchcResetLevel != RESET_CLEAR)
        {
            bl_console::console_fail = true;
        }
    }

    return bl_console::console_fail;
}

static uint64_t convertSecToTicks(const uint64_t i_sec, const uint64_t i_nsec)
{
    // result = ((sec * 10^9 + nsec) * timebaseFreq Hz) / 10^9
    uint64_t timebaseFreq = 512000000ULL;
    uint64_t result = ((i_sec * 1000000000ULL) + i_nsec);
    result *= (timebaseFreq / 1000000);
    result /= 1000;
    return result;
}

// @TODO: RTC: 243863
// Evaluate timing on real hardware and tweak constants as needed
static void simpleDelay(const uint64_t i_sec, const uint64_t i_nsec)
{
    uint64_t delay = convertSecToTicks(i_sec, i_nsec);<commit_msg>Loosen LPC Error Checking in Bootloader<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/bootloader/bl_console.C $                                 */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2019,2020                        */
/* [+] 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 bl_console.C
 *
 *  @brief Bootloader console functions to write data to lpc console
 */

#include <bootloader/bl_console.H>
#include <sys/task.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <lpc/lpc_const.H>
#include <uart.H>
#include <uartconfig.C>
#include <p10_sbe_hb_structures.H>
#include <util/align.H>
#include <arch/ppc.H>
#include <kernel/timemgr.H>

using namespace CONSOLE;
using namespace UARTREGS;

bool bl_console::console_fail = false;

void computeOpbmErrSev(OpbmErrReg_t i_opbmErrData,
                       ResetLevels &o_resetLevel)
{
    o_resetLevel = RESET_CLEAR;

    // First check the soft errors
    if (i_opbmErrData.rxits || i_opbmErrData.rxicmd || i_opbmErrData.rxiaa ||
        i_opbmErrData.rxopbe || i_opbmErrData.rxicmdb || i_opbmErrData.rxidatab)
    {
        o_resetLevel = RESET_OPB_LPCHC_SOFT;
    }

    // Now look for hard errors that will override soft errors reset level
    if (i_opbmErrData.rxopbt || i_opbmErrData.rxiaddr || i_opbmErrData.rxctgtel)
    {
        o_resetLevel = RESET_OPB_LPCHC_HARD;
    }
}

void computeLpchcErrSev(LpchcErrReg_t i_lpchcErrData,
                        ResetLevels &o_resetLevel)
{
    o_resetLevel = RESET_CLEAR;

    // First check the soft errors
    // All of these errors are set from bad LPC end points. Setting all to soft.
    // On Rainier HW, lreset bit pops up when we get to HBBL, but the console
    // works OK despite that bit. The check for that bit was removed from HBBL
    // for now until we check with HW people and figure out why the bit is set.
    if (i_lpchcErrData.syncab ||
        i_lpchcErrData.syncnr || i_lpchcErrData.syncne ||
        i_lpchcErrData.syncto || i_lpchcErrData.tctar || i_lpchcErrData.mctar)
    {
        o_resetLevel = RESET_OPB_LPCHC_SOFT;
    }
}

bool checkForLpcErrors()
{
    uint32_t opbm_buffer = 0;
    uint32_t lpchc_buffer = 0;
    OpbmErrReg_t opbm_err_union;
    opbm_err_union.data32 = 0x0;
    LpchcErrReg_t lpchc_err_union;
    lpchc_err_union.data32 = 0x0;
    ResetLevels l_opbmResetLevel = RESET_CLEAR;
    ResetLevels l_lpchcResetLevel = RESET_CLEAR;

    uint64_t l_pnorEnd = g_blData->blToHbData.lpcBAR + LPC::LPCHC_FW_SPACE +
        PNOR::LPC_TOP_OF_FLASH_OFFSET;

    uint64_t l_mmioAddr = l_pnorEnd - PNOR::TOC_OFFSET_FROM_TOP_OF_FLASH;
    l_mmioAddr = ALIGN_PAGE_DOWN(l_mmioAddr);

    // First do a dummy LPC access (if an LPC error condition exists,
    // an access can be necessary to get the error indicated in the
    // status register. This read will force the error condition to
    // properly be shown in the LPC error status reg
    Bootloader::handleMMIO(l_mmioAddr,
                           reinterpret_cast<uintptr_t>(&opbm_buffer) +
                           getHRMOR(),
                           Bootloader::WORDSIZE,
                           Bootloader::WORDSIZE,
                           Bootloader::READ);

    uint32_t l_opb_status_addr = OPBM_ACCUM_STATUS_REG;
    uint64_t src_addr = g_blData->blToHbData.lpcBAR + LPC::LPCHC_ERR_SPACE +
        l_opb_status_addr;
    Bootloader::handleMMIO(src_addr,
                           reinterpret_cast<uintptr_t>(&opbm_buffer) +
                           getHRMOR(),
                           Bootloader::WORDSIZE, Bootloader::WORDSIZE,
                           Bootloader::READ);

    uint32_t l_lpchc_addr = LPCHC_REG;
    src_addr = g_blData->blToHbData.lpcBAR + LPC::LPCHC_ERR_SPACE + l_lpchc_addr;
    Bootloader::handleMMIO(src_addr,
                           reinterpret_cast<uintptr_t>(&lpchc_buffer) +
                           getHRMOR(),
                           Bootloader::WORDSIZE, Bootloader::WORDSIZE,
                           Bootloader::READ);

    // Mask error bits
    opbm_err_union.data32 = (opbm_buffer & LPC::OPB_ERROR_MASK);
    lpchc_err_union.data32 = (lpchc_buffer & LPCHC_ERROR_MASK);

    if (opbm_err_union.data32 != 0)
    {
        computeOpbmErrSev(opbm_err_union, l_opbmResetLevel);

        if (l_opbmResetLevel != RESET_CLEAR)
        {
            bl_console::console_fail = true;
        }
    }

    if (lpchc_err_union.data32 != 0)
    {
        computeLpchcErrSev(lpchc_err_union, l_lpchcResetLevel);

        if (l_lpchcResetLevel != RESET_CLEAR)
        {
            bl_console::console_fail = true;
        }
    }

    return bl_console::console_fail;
}

static uint64_t convertSecToTicks(const uint64_t i_sec, const uint64_t i_nsec)
{
    // result = ((sec * 10^9 + nsec) * timebaseFreq Hz) / 10^9
    uint64_t timebaseFreq = 512000000ULL;
    uint64_t result = ((i_sec * 1000000000ULL) + i_nsec);
    result *= (timebaseFreq / 1000000);
    result /= 1000;
    return result;
}

static void simpleDelay(const uint64_t i_sec, const uint64_t i_nsec)
{
    uint64_t delay = convertSecToTicks(i_sec, i_nsec);
{'fraction_non_alphanumeric': 0.08342652650413777, 'fraction_numerical': 0.015358234548572281, 'mean_word_length': 1.6908726178535607, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 64, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6245882', 'n_tokens_mistral': 4249, 'n_tokens_neox': 4033, 'n_words': 877}