/* ----------------------------------------------------------------------------
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 * Description: LiteOS Perf Hardware Pmu Module Implementation
 * Author: Huawei LiteOS Team
 * Create: 2020-07-29
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --------------------------------------------------------------------------- */

#include "perf_pmu_pri.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

STATIC Pmu *g_perfHw = NULL;//指向性能计数器相关的数据结构或对象

STATIC CHAR *g_eventName[PERF_COUNT_HW_MAX] = {
    [PERF_COUNT_HW_CPU_CYCLES]              = "cycles",
    [PERF_COUNT_HW_INSTRUCTIONS]            = "instructions",
    [PERF_COUNT_HW_ICACHE_REFERENCES]       = "icache",
    [PERF_COUNT_HW_ICACHE_MISSES]           = "icache-misses",
    [PERF_COUNT_HW_DCACHE_REFERENCES]       = "dcache",
    [PERF_COUNT_HW_DCACHE_MISSES]           = "dcache-misses",
    [PERF_COUNT_HW_BRANCH_INSTRUCTIONS]     = "branches",
    [PERF_COUNT_HW_BRANCH_MISSES]           = "branches-misses",
};

/**
 * 1.If config event is PERF_EVENT_TYPE_HW, then map it to the real eventId first, otherwise use the configured
 * eventId directly.
 * 2.Find available counter for each event.
 * 3.Decide whether this hardware pmu need prescaler (once every 64 cycle counts).
 */
STATIC UINT32 OsPerfHwConfig(VOID)//性能计数器的配置函数，用于初始化和配置硬件性能计数器
{
    UINT32 i;
    HwPmu *armPmu = GET_HW_PMU(g_perfHw);

    UINT32 maxCounter = OsGetPmuMaxCounter();//获得最大计数器数量
    UINT32 counter = OsGetPmuCounter0();//获取0核计数器的值
    UINT32 cycleCounter = OsGetPmuCycleCounter();//周期计数器的值
    UINT32 cycleCode = armPmu->mapEvent(PERF_COUNT_HW_CPU_CYCLES, PERF_EVENT_TO_CODE);
    if (cycleCode == PERF_HW_INVAILD_EVENT_TYPE) {
        return LOS_NOK;
    }
    //获取性能事件的列表的和数量并进行遍历
    PerfEvent *events = &g_perfHw->events;
    UINT32 eventNum = events->nr;

    for (i = 0; i < eventNum; i++) {
        Event *event = &(events->per[i]);

        if (!VALID_PERIOD(event->period)) {//检查事件周期是否合法
            PRINT_ERR("Config period: 0x%x invalid, should be in (%#x, %#x)\n", event->period,
                PERIOD_CALC(CCNT_PERIOD_UPPER_BOUND), PERIOD_CALC(CCNT_PERIOD_LOWER_BOUND));
            return LOS_NOK;
        }

        if (g_perfHw->type == PERF_EVENT_TYPE_HW) { /* do map */
        //映射编码
            UINT32 eventId = armPmu->mapEvent(event->eventId, PERF_EVENT_TO_CODE);
            if (eventId == PERF_HW_INVAILD_EVENT_TYPE) {
                return LOS_NOK;
            }
            event->eventId = eventId;
        }
        //函数根据事件编码是否与周期计数器的事件编码相同，来确定该事件使用的计数器。
        //如果相同，则使用周期计数器；否则，使用普通计数器，并递增计数器的值。
        if (event->eventId == cycleCode) {
            event->counter = cycleCounter;
        } else {
            event->counter = counter;
            counter++;
        }

        if (counter >= maxCounter) {//检查计数器是否超过了最大计数器数量
            PRINT_ERR("max events: %u excluding cycle event\n", maxCounter - 1);
            return LOS_NOK;
        }
        //打印结果，设置根据平台支持的分频
        PRINT_DEBUG("Perf Config %u eventId = 0x%x, counter = 0x%x, period = 0x%x\n", i, event->eventId, event->counter,
            event->period);
    }

    armPmu->cntDivided = events->cntDivided & armPmu->canDivided;
    return LOS_OK;
}

STATIC UINT32 OsPerfHwStart(VOID)//用于启动硬件性能计数器
{
    UINT32 i;
    UINT32 cpuid = ArchCurrCpuid();
    HwPmu *armPmu = GET_HW_PMU(g_perfHw);//获取硬件性能计数器的指针

    PerfEvent *events = &g_perfHw->events;
    UINT32 eventNum = events->nr;

    armPmu->clear();//清零计数器

    for (i = 0; i < eventNum; i++) {
        Event *event = &(events->per[i]);
        armPmu->setPeriod(event);//设置事件的计数周期
        armPmu->enable(event);//启用事件的计数器
        event->count[cpuid] = 0;
    }

    armPmu->start();
    return LOS_OK;
}

STATIC UINT32 OsPerfHwStop(VOID)//停止硬件性能计数器
{
    UINT32 i;
    UINT32 cpuid = ArchCurrCpuid();
    HwPmu *armPmu = GET_HW_PMU(g_perfHw);

    PerfEvent *events = &g_perfHw->events;
    UINT32 eventNum = events->nr;

    armPmu->stop();

    for (i = 0; i < eventNum; i++) {
        Event *event = &(events->per[i]);
        UINTPTR value = armPmu->readCnt(event);
        PRINT_DEBUG("perf stop readCnt value = 0x%x\n", value);
        event->count[cpuid] += value;

        /* multiplier of cycle counter */
        UINT32 eventId = armPmu->mapEvent(event->eventId, PERF_CODE_TO_EVENT);
        if ((eventId == PERF_COUNT_HW_CPU_CYCLES) && (armPmu->cntDivided != 0)) {
            //如果该事件的事件ID与周期计数器事件ID相同，并且硬件性能计数器的分频设置armPmu->cntDivided不为0
            PRINT_DEBUG("perf stop is cycle\n");
            //此处将该事件的计数值左移6位，相当于乘以64
            event->count[cpuid] = event->count[cpuid] << 6; /* CCNT counts every 64th cpu cycle */
        }
        PRINT_DEBUG("perf stop eventCount[0x%x] : [%s] = %llu\n", event->eventId, g_eventName[eventId],
            event->count[cpuid]);
    }
    return LOS_OK;
}

STATIC CHAR *OsPerfGetEventName(Event *event)//获取事件名称
{
    UINT32 eventId;
    HwPmu *armPmu = GET_HW_PMU(g_perfHw);//获取PMU上的事件信息
    eventId = armPmu->mapEvent(event->eventId, PERF_CODE_TO_EVENT);//将事件ID映射位对应计数器事件ID
    if (eventId < PERF_COUNT_HW_MAX) {
        return g_eventName[eventId];
    } else {
        return "unknown";
    }
}

UINT32 OsPerfHwInit(HwPmu *hwPmu)//初始化性能计数器
{
    UINT32 ret;
    if (hwPmu == NULL) {
        return LOS_NOK;
    }
    //设置性能计数器的类型，配置函数，启动函数，停止函数，获取事件名称函数
    hwPmu->pmu.type    = PERF_EVENT_TYPE_HW;
    hwPmu->pmu.config  = OsPerfHwConfig;
    hwPmu->pmu.start   = OsPerfHwStart;
    hwPmu->pmu.stop    = OsPerfHwStop;
    hwPmu->pmu.getName = OsPerfGetEventName;
    //将硬件性能计数器的事件数据结构清零
    (VOID)memset_s(&hwPmu->pmu.events, sizeof(PerfEvent), 0, sizeof(PerfEvent));
    ret = OsPerfPmuRegister(&hwPmu->pmu);//注册硬件性能计数器，将其存储在全局变量中

    g_perfHw = OsPerfPmuGet(PERF_EVENT_TYPE_HW);
    return ret;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
