10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "los_config.h"
330d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_CMD_DEBUG
340d163575Sopenharmony_ci#ifdef LOSCFG_CPUP_INCLUDE_IRQ
350d163575Sopenharmony_ci#include "los_cpup_pri.h"
360d163575Sopenharmony_ci#endif
370d163575Sopenharmony_ci#include "los_hwi_pri.h"
380d163575Sopenharmony_ci#include "los_sys_pri.h"
390d163575Sopenharmony_ci#include "shcmd.h"
400d163575Sopenharmony_ci
410d163575Sopenharmony_ci
420d163575Sopenharmony_ci#ifdef LOSCFG_CPUP_INCLUDE_IRQ
430d163575Sopenharmony_ci#define IRQ_CPUP_INFO_SIZE     (sizeof(CPUP_INFO_S) * OS_HWI_MAX_NUM * LOSCFG_KERNEL_CORE_NUM)
440d163575Sopenharmony_ci#define IRQ_CPUP_ALL_INFO_SIZE (3 * IRQ_CPUP_INFO_SIZE)
450d163575Sopenharmony_ci#define IRQ_DATA_SZIE          (sizeof(OsIrqCpupCB) * LOSCFG_KERNEL_CORE_NUM)
460d163575Sopenharmony_ci#define CPUP_PRECISION_MULT    LOS_CPUP_PRECISION_MULT
470d163575Sopenharmony_ci
480d163575Sopenharmony_ciSTATIC VOID ShellCmdHwiInfoShow(OsIrqCpupCB *irqData, CPUP_INFO_S *hwiCpup1s,
490d163575Sopenharmony_ci                                CPUP_INFO_S *hwiCpup10s, CPUP_INFO_S *hwiCpupAll)
500d163575Sopenharmony_ci{
510d163575Sopenharmony_ci    UINT32 intSave;
520d163575Sopenharmony_ci    OsIrqCpupCB *irqDataBase = OsGetIrqCpupArrayBase();
530d163575Sopenharmony_ci
540d163575Sopenharmony_ci    for (UINT32 i = OS_HWI_FORM_EXC_NUM; i < OS_HWI_MAX_NUM + OS_HWI_FORM_EXC_NUM; i++) {
550d163575Sopenharmony_ci        if (!HWI_IS_REGISTED(i)) {
560d163575Sopenharmony_ci            continue;
570d163575Sopenharmony_ci        }
580d163575Sopenharmony_ci
590d163575Sopenharmony_ci        intSave = LOS_IntLock();
600d163575Sopenharmony_ci        (VOID)memcpy_s(irqData, IRQ_DATA_SZIE, &irqDataBase[i * LOSCFG_KERNEL_CORE_NUM], IRQ_DATA_SZIE);
610d163575Sopenharmony_ci        LOS_IntRestore(intSave);
620d163575Sopenharmony_ci
630d163575Sopenharmony_ci        for (UINT32 cpu = 0; cpu < LOSCFG_KERNEL_CORE_NUM; cpu++) {
640d163575Sopenharmony_ci            UINT64 cycles = 0;
650d163575Sopenharmony_ci            UINT64 timeMax = 0;
660d163575Sopenharmony_ci            OsIrqCpupCB *data = &irqData[cpu];
670d163575Sopenharmony_ci            if (data->status == 0) {
680d163575Sopenharmony_ci                continue;
690d163575Sopenharmony_ci            }
700d163575Sopenharmony_ci            UINT32 count = OsGetHwiFormCnt(cpu, i);
710d163575Sopenharmony_ci            if (count != 0) {
720d163575Sopenharmony_ci                if (data->count != 0) {
730d163575Sopenharmony_ci                    cycles = (data->allTime * OS_NS_PER_CYCLE) / (data->count * OS_SYS_NS_PER_US);
740d163575Sopenharmony_ci                }
750d163575Sopenharmony_ci                timeMax = (data->timeMax * OS_NS_PER_CYCLE) / 1000;
760d163575Sopenharmony_ci            }
770d163575Sopenharmony_ci            CHAR *irqName = OsGetHwiFormName(i);
780d163575Sopenharmony_ci            UINT32 index = (i * LOSCFG_KERNEL_CORE_NUM) + cpu;
790d163575Sopenharmony_ci            PRINTK(" %10u:%5u%11u%11llu%10llu%6u.%-2u%8u.%-2u%7u.%-2u%7s %-12s\n", i, cpu, count, cycles, timeMax,
800d163575Sopenharmony_ci                   hwiCpupAll[index].usage / CPUP_PRECISION_MULT, hwiCpupAll[index].usage % CPUP_PRECISION_MULT,
810d163575Sopenharmony_ci                   hwiCpup10s[index].usage / CPUP_PRECISION_MULT, hwiCpup10s[index].usage % CPUP_PRECISION_MULT,
820d163575Sopenharmony_ci                   hwiCpup1s[index].usage / CPUP_PRECISION_MULT, hwiCpup1s[index].usage % CPUP_PRECISION_MULT,
830d163575Sopenharmony_ci                   (g_hwiForm[index].uwParam == IRQF_SHARED) ? "shared" : "normal", (irqName != NULL) ? irqName : "");
840d163575Sopenharmony_ci        }
850d163575Sopenharmony_ci    }
860d163575Sopenharmony_ci}
870d163575Sopenharmony_ci
880d163575Sopenharmony_ciLITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdHwi(INT32 argc, const CHAR **argv)
890d163575Sopenharmony_ci{
900d163575Sopenharmony_ci    UINT32 size;
910d163575Sopenharmony_ci
920d163575Sopenharmony_ci    (VOID)argv;
930d163575Sopenharmony_ci    if (argc > 0) {
940d163575Sopenharmony_ci        PRINTK("\nUsage: hwi\n");
950d163575Sopenharmony_ci        return OS_ERROR;
960d163575Sopenharmony_ci    }
970d163575Sopenharmony_ci
980d163575Sopenharmony_ci    size = IRQ_CPUP_ALL_INFO_SIZE + IRQ_DATA_SZIE;
990d163575Sopenharmony_ci    CHAR *irqCpup = LOS_MemAlloc(m_aucSysMem0, size);
1000d163575Sopenharmony_ci    if (irqCpup == NULL) {
1010d163575Sopenharmony_ci        return OS_ERROR;
1020d163575Sopenharmony_ci    }
1030d163575Sopenharmony_ci
1040d163575Sopenharmony_ci    CPUP_INFO_S *hwiCpupAll = (CPUP_INFO_S *)irqCpup;
1050d163575Sopenharmony_ci    CPUP_INFO_S *hwiCpup10s = (CPUP_INFO_S *)(irqCpup + IRQ_CPUP_INFO_SIZE);
1060d163575Sopenharmony_ci    CPUP_INFO_S *hwiCpup1s = (CPUP_INFO_S *)(irqCpup + 2 * IRQ_CPUP_INFO_SIZE); /* 2: offset */
1070d163575Sopenharmony_ci    OsIrqCpupCB *irqData = (OsIrqCpupCB *)(irqCpup + IRQ_CPUP_ALL_INFO_SIZE);
1080d163575Sopenharmony_ci
1090d163575Sopenharmony_ci    (VOID)LOS_GetAllIrqCpuUsage(CPUP_ALL_TIME, hwiCpupAll, IRQ_CPUP_INFO_SIZE);
1100d163575Sopenharmony_ci    (VOID)LOS_GetAllIrqCpuUsage(CPUP_LAST_TEN_SECONDS, hwiCpup10s, IRQ_CPUP_INFO_SIZE);
1110d163575Sopenharmony_ci    (VOID)LOS_GetAllIrqCpuUsage(CPUP_LAST_ONE_SECONDS, hwiCpup1s, IRQ_CPUP_INFO_SIZE);
1120d163575Sopenharmony_ci
1130d163575Sopenharmony_ci    PRINTK(" InterruptNo  cpu      Count  ATime(us) MTime(us)   CPUUSE  CPUUSE10s  CPUUSE1s   Mode Name\n");
1140d163575Sopenharmony_ci    ShellCmdHwiInfoShow(irqData, hwiCpup1s, hwiCpup10s, hwiCpupAll);
1150d163575Sopenharmony_ci    (VOID)LOS_MemFree(m_aucSysMem0, irqCpup);
1160d163575Sopenharmony_ci    return 0;
1170d163575Sopenharmony_ci}
1180d163575Sopenharmony_ci#else
1190d163575Sopenharmony_ciLITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdHwi(INT32 argc, const CHAR **argv)
1200d163575Sopenharmony_ci{
1210d163575Sopenharmony_ci    UINT32 i;
1220d163575Sopenharmony_ci
1230d163575Sopenharmony_ci    (VOID)argv;
1240d163575Sopenharmony_ci    if (argc > 0) {
1250d163575Sopenharmony_ci        PRINTK("\nUsage: hwi\n");
1260d163575Sopenharmony_ci        return OS_ERROR;
1270d163575Sopenharmony_ci    }
1280d163575Sopenharmony_ci
1290d163575Sopenharmony_ci    PRINTK(" InterruptNo     Count     Name\n");
1300d163575Sopenharmony_ci    for (i = OS_HWI_FORM_EXC_NUM; i < OS_HWI_MAX_NUM + OS_HWI_FORM_EXC_NUM; i++) {
1310d163575Sopenharmony_ci        /* Different cores has different hwi form implementation */
1320d163575Sopenharmony_ci        if (HWI_IS_REGISTED(i) && (OsGetHwiFormName(i) != NULL)) {
1330d163575Sopenharmony_ci            PRINTK(" %8d:%10d:      %-s\n", i, OsGetHwiFormCnt(i), OsGetHwiFormName(i));
1340d163575Sopenharmony_ci        } else if (HWI_IS_REGISTED(i)) {
1350d163575Sopenharmony_ci            PRINTK(" %8d:%10d:\n", i, OsGetHwiFormCnt(i));
1360d163575Sopenharmony_ci        }
1370d163575Sopenharmony_ci    }
1380d163575Sopenharmony_ci    return 0;
1390d163575Sopenharmony_ci}
1400d163575Sopenharmony_ci#endif
1410d163575Sopenharmony_ci
1420d163575Sopenharmony_ciSHELLCMD_ENTRY(hwi_shellcmd, CMD_TYPE_EX, "hwi", 0, (CmdCallBackFunc)OsShellCmdHwi);
1430d163575Sopenharmony_ci
1440d163575Sopenharmony_ci#endif /* LOSCFG_SHELL */
145