/* ----------------------------------------------------------------------------
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 * Description: LiteOS Perf Pmu Manager Implementation Private HeadFile
 * 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.
 * --------------------------------------------------------------------------- */

#ifndef _PERF_PMU_PRI_H
#define _PERF_PMU_PRI_H

#include "los_perf_pri.h"

#ifdef LOSCFG_COMPAT_LINUX
#include "linux/hrtimer.h"
#endif

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

typedef struct {
    Pmu pmu;
    BOOL canDivided;
    UINT32 cntDivided;
    VOID (*enable)(Event *event);
    VOID (*disable)(Event *event);
    VOID (*start)(VOID);
    VOID (*stop)(VOID);
    VOID (*clear)(VOID);
    VOID (*setPeriod)(Event *event);
    UINTPTR (*readCnt)(Event *event);
    UINT32 (*mapEvent)(UINT32 eventType, BOOL reverse);
} HwPmu;//硬件性能计数器结构体，包括硬件计数器、计数器是否能被分频、分频系数、启用、禁用、开始、停止、清除、设置周期、读取计数器值和事件映射等函数指针

typedef struct {
    Pmu pmu;
    union {
        struct { /* trace event */
            BOOL enable;
        };
#ifdef LOSCFG_COMPAT_LINUX
        struct { /* timer event */
            struct hrtimer hrtimer;
            union ktime time;
            union ktime cfgTime;
        };
#endif
    };
} SwPmu;//软件性能计数器结构体，包括软件计数器、是否启用标志位、定时器配置时间

#define GET_HW_PMU(item)                    LOS_DL_LIST_ENTRY(item, HwPmu, pmu)

#define TIMER_PERIOD_LOWER_BOUND_US         100//定时器最小周期

#define CCNT_FULL                           0xFFFFFFFF//计数器最大值
#define CCNT_PERIOD_LOWER_BOUND             0x00000000//最小周期
#define CCNT_PERIOD_UPPER_BOUND             0xFFFFFF00//最大周期
#define PERIOD_CALC(p)                      (CCNT_FULL - (p))//计算给定周期对应的计数器值
#define VALID_PERIOD(p)                     ((PERIOD_CALC(p) > CCNT_PERIOD_LOWER_BOUND) \
                                            && (PERIOD_CALC(p) < CCNT_PERIOD_UPPER_BOUND))//判断给定周期是否合法

#define PERF_HW_INVAILD_EVENT_TYPE          0xFFFFFFFF//无效的事件类型

#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

extern UINT32 OsPerfPmuRegister(Pmu *pmu);//注册硬件性能计数器
extern VOID OsPerfPmuRm(UINT32 type);//删除硬件性能计数器
extern Pmu *OsPerfPmuGet(UINT32 type);//获取指定类型的硬件性能计数器

extern UINT32 OsHwPmuInit(VOID);//初始化硬件性能计数器
extern UINT32 OsSwPmuInit(VOID);//初始化软件性能计数器
extern UINT32 OsTimedPmuInit(VOID);//初始化定时器性能计数器

extern UINT32 OsGetPmuCounter0(VOID);//获取0核计数器值
extern UINT32 OsGetPmuMaxCounter(VOID);//获取最大计数器数
extern UINT32 OsGetPmuCycleCounter(VOID);//获取周期性计数器值
extern UINT32 OsPerfHwInit(HwPmu *hwPmu);//初始化硬件性能计数器

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

#endif /* _PERF_PMU_PRI_H */
