1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 *    conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 *    of conditions and the following disclaimer in the documentation and/or other materials
12 *    provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 *    to endorse or promote products derived from this software without specific prior written
16 *    permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "los_debugtools.h"
32#include "securec.h"
33#include "los_debug.h"
34#include "los_memory.h"
35#include "los_arch.h"
36#include "los_interrupt.h"
37#include "los_arch_interrupt.h"
38
39#if (LOSCFG_DEBUG_TOOLS == 1)
40
41#if (LOSCFG_CPUP_INCLUDE_IRQ == 1) && (LOSCFG_BASE_CORE_SWTMR == 1)
42#include "los_cpup.h"
43
44#define IRQ_CPUP_INFO_SIZE     (sizeof(CPUP_INFO_S) * LOSCFG_PLATFORM_HWI_LIMIT)
45#define IRQ_CPUP_ALL_INFO_SIZE (IRQ_CPUP_INFO_SIZE + IRQ_CPUP_INFO_SIZE)
46#define IRQ_DATA_SIZE          sizeof(OsIrqCpupCB)
47#define CPUP_PRECISION_MULT    LOS_CPUP_PRECISION_MULT
48
49STATIC VOID ShellCmdHwiInfoShow(OsIrqCpupCB *irqData, CPUP_INFO_S *hwiCpup1s,
50                                CPUP_INFO_S *hwiCpup10s)
51{
52    UINT32 i;
53    UINT32 intSave;
54    UINT32 count;
55    UINT64 cycles = 0;
56    UINT64 timeMax = 0;
57    CHAR *irqName = NULL;
58
59    OsIrqCpupCB *irqDataBase = OsGetIrqCpupArrayBase();
60    if (irqDataBase == NULL) {
61        PRINT_ERR("get hwi info error\n");
62        return;
63    }
64
65    for (i = 0; i < LOSCFG_PLATFORM_HWI_LIMIT; i++) {
66        if ((OsHwiIsCreated(i) != TRUE) || (OsGetHwiFormCnt(i) == 0)) {
67            continue;
68        }
69
70        intSave = LOS_IntLock();
71        (VOID)memcpy_s(irqData, IRQ_DATA_SIZE, &irqDataBase[i], IRQ_DATA_SIZE);
72        LOS_IntRestore(intSave);
73
74        if (irqData->status == 0) {
75            continue;
76        }
77
78        count = OsGetHwiFormCnt(i);
79        if (count != 0) {
80            if (irqData->count != 0) {
81                cycles = (irqData->allTime * OS_NS_PER_CYCLE) / (irqData->count * OS_SYS_NS_PER_US);
82            }
83            timeMax = (irqData->timeMax * OS_NS_PER_CYCLE) / OS_SYS_NS_PER_US;
84        }
85        irqName = OsGetHwiFormName(i);
86
87        PRINTK(" %10d:%11u%11llu%10llu%9u.%-2u%9u.%-2u %-12s\n", i - OS_SYS_VECTOR_CNT, count, cycles, timeMax,
88               hwiCpup1s[i].uwUsage / CPUP_PRECISION_MULT, hwiCpup1s[i].uwUsage % CPUP_PRECISION_MULT,
89               hwiCpup10s[i].uwUsage / CPUP_PRECISION_MULT, hwiCpup10s[i].uwUsage % CPUP_PRECISION_MULT,
90               (irqName != NULL) ? irqName : NULL);
91    }
92}
93
94STATIC VOID HwiInfoDump(VOID)
95{
96    UINT32 size;
97
98    size = IRQ_CPUP_ALL_INFO_SIZE + IRQ_DATA_SIZE;
99    CHAR *irqCpup = LOS_MemAlloc(m_aucSysMem0, size);
100    if (irqCpup == NULL) {
101        return;
102    }
103
104    CPUP_INFO_S *hwiCpup10s = (CPUP_INFO_S *)(irqCpup);
105    CPUP_INFO_S *hwiCpup1s = (CPUP_INFO_S *)(hwiCpup10s + IRQ_CPUP_INFO_SIZE);
106    OsIrqCpupCB *irqData = (OsIrqCpupCB *)(irqCpup + IRQ_CPUP_ALL_INFO_SIZE);
107
108    (VOID)LOS_GetAllIrqCpuUsage(CPUP_IN_1S, hwiCpup1s);
109    (VOID)LOS_GetAllIrqCpuUsage(CPUP_IN_10S, hwiCpup10s);
110
111    PRINTK(" InterruptNo      Count  ATime(us)  MTime(us)  CPUUSE1s   CPUUSE10s  Name\n");
112    ShellCmdHwiInfoShow(irqData, hwiCpup1s, hwiCpup10s);
113    (VOID)LOS_MemFree(m_aucSysMem0, irqCpup);
114    return;
115}
116#else
117STATIC VOID HwiInfoDump(VOID)
118{
119    INT32 i;
120
121    PRINTK(" InterruptNo     Count     Name\n");
122    for (i = 0; i < LOSCFG_PLATFORM_HWI_LIMIT; i++) {
123        if ((OsHwiIsCreated(i) != TRUE) || (OsGetHwiFormCnt(i) == 0)) {
124            continue;
125        }
126
127        if (OsGetHwiFormName(i) != NULL) {
128            PRINTK(" %8d:%10d      %-s\n", i - OS_SYS_VECTOR_CNT, OsGetHwiFormCnt(i), OsGetHwiFormName(i));
129        } else {
130            PRINTK(" %8d:%10d\n", i - OS_SYS_VECTOR_CNT, OsGetHwiFormCnt(i));
131        }
132    }
133    return;
134}
135#endif
136
137UINT32 OsShellCmdHwiDump(INT32 argc, const CHAR **argv)
138{
139    (VOID)argv;
140
141    if (argc > 1) {
142        PRINT_ERR("\nUsage:hwi\n");
143        return LOS_NOK;
144    }
145
146    HwiInfoDump();
147    return LOS_OK;
148}
149#endif /* LOSCFG_STACK_DUMP == 1 */
150