Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

@@ -0,0 +1,32 @@
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/inc/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/mtk_lpm_fs/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/modules/include/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/modules/include/mt6779/
BUILD_MTK_LPM_DBG_MODUDLE_NAME=mtk-lpm-dbg-$(strip $(subst ",, $(CONFIG_MTK_LPM_MODULE_NAME)))
obj-$(CONFIG_MTK_LPM_MT6779) += ${BUILD_MTK_LPM_DBG_MODUDLE_NAME}.o
ifeq ($(CONFIG_MTK_LPM_DBG_COMMON),$(filter $(CONFIG_MTK_LPM_DBG_COMMON),m y))
ccflags-y += -DMTK_LPM_DBG_COMMON
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += ../common/mtk_dbg_fs_common.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += ../common/mtk_lpm_timer.o
endif
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mt6779_dbg.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mt6779_logger.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mt6779_dbg_fs.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mt6779_dbg_idle_fs.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mt6779_dbg_spm_fs.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_cpupm_dbg.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_cpuidle_status.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_cpuidle_cpc.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_idle_procfs.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_idle_procfs_state.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_idle_procfs_cpc.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_idle_procfs_profile.o
${BUILD_MTK_LPM_DBG_MODUDLE_NAME}-y += mtk_idle_procfs_control.o
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <mtk_dbg_common_v1.h>
#include <mtk_lpm_module.h>
#include <mt6779_dbg_fs_common.h>
static int __init mt6779_dbg_init(void)
{
#ifdef MTK_LPM_DBG_COMMON
mtk_dbg_common_fs_init();
#endif
mtk_cpupm_dbg_init();
mt6779_dbg_fs_init();
return 0;
}
static void __exit mt6779_dbg_exit(void)
{
#ifdef MTK_LPM_DBG_COMMON
mtk_dbg_common_fs_exit();
#endif
mt6779_dbg_fs_exit();
mtk_cpupm_dbg_exit();
}
module_init(mt6779_dbg_init);
module_exit(mt6779_dbg_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("MT6779 Low Power debug KO");
MODULE_AUTHOR("MediaTek Inc.");
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <mtk_dbg_common_v1.h>
#include <mtk_lpm_module.h>
#include <mt6779_dbg_fs_common.h>
void mt6779_dbg_fs_exit(void)
{
mt6779_dbg_idle_fs_deinit();
mt6779_dbg_spm_fs_deinit();
}
int mt6779_dbg_fs_init(void)
{
mt6779_logger_init();
mt6779_dbg_idle_fs_init();
mt6779_dbg_spm_fs_init();
pr_info("%s %d: finish", __func__, __LINE__);
return 0;
}
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MT6779_DBG_FS_COMMON_H__
#define __MT6779_DBG_FS_COMMON_H__
/* Determine for user name handle */
#define MT_LP_RQ_USER_NAME_LEN (4)
#define MT_LP_RQ_USER_CHAR_U (8)
#define MT_LP_RQ_USER_CHAR_MASK (0xFF)
/* Determine for resource usage id */
#define MT_LP_RQ_ID_ALL_USAGE (-1)
int mt6779_dbg_idle_fs_init(void);
int mt6779_dbg_idle_fs_deinit(void);
int mt6779_dbg_spm_fs_init(void);
int mt6779_dbg_spm_fs_deinit(void);
int mt6779_logger_init(void);
int mt6779_dbg_fs_init(void);
void mt6779_dbg_fs_exit(void);
int mtk_cpupm_dbg_init(void);
void mtk_cpupm_dbg_exit(void);
#endif /* __MTK_LP_PLAT_FS_H__ */
@@ -0,0 +1,290 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/console.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <mtk_dbg_common_v1.h>
#include <mtk_lpm_module.h>
#include <mtk_resource_constraint_v1.h>
#include <mtk_lpm_sysfs.h>
#include <mtk_suspend_sysfs.h>
#include <mtk_spm_sysfs.h>
#include <mt6779_pwr_ctrl.h>
#include <mt6779_dbg_fs_common.h>
#include <mt6779_cond.h>
#include <mt6779_spm_comm.h>
#define MT_LP_BUS26M_NODE "/sys/kernel/debug/cpuidle/IdleBus26m_state"
#define MT_LP_SYSPLL_NODE "/sys/kernel/debug/cpuidle/IdleSyspll_state"
#define MT_LP_DRAM_NODE "/sys/kernel/debug/cpuidle/IdleDram_state"
#undef mtk_idle_log
#define mtk_idle_log(fmt, args...) \
do { \
int l = scnprintf(p, sz, fmt, ##args); \
p += l; \
sz -= l; \
} while (0)
static char *mt6779_spm_cond_cg_str[PLAT_SPM_COND_MAX] = {
[PLAT_SPM_COND_MTCMOS_0] = "MTCMOS_0",
[PLAT_SPM_COND_MTCMOS_1] = "MTCMOS_1",
[PLAT_SPM_COND_CG_INFRA_0] = "INFRA_0",
[PLAT_SPM_COND_CG_INFRA_1] = "INFRA_1",
[PLAT_SPM_COND_CG_INFRA_2] = "INFRA_2",
[PLAT_SPM_COND_CG_INFRA_3] = "INFRA_3",
[PLAT_SPM_COND_CG_MMSYS_0] = "MMSYS_0",
[PLAT_SPM_COND_CG_MMSYS_1] = "MMSYS_1",
};
static char *mt6779_spm_cond_pll_str[PLAT_SPM_COND_PLL_MAX] = {
[PLAT_SPM_COND_UNIVPLL] = "UNIVPLL",
[PLAT_SPM_COND_MFGPLL] = "MFGPLL",
[PLAT_SPM_COND_MSDCPLL] = "MSDCPLL",
[PLAT_SPM_COND_TVPLL] = "TVPLL",
[PLAT_SPM_COND_MMPLL] = "MMPLL",
};
static ssize_t mt6779_idle_dram_read(char *ToUserBuf,
size_t sz, void *priv)
{
char *p = ToUserBuf;
uint32_t block, b;
int i;
block = mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_BLOCK,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_DRAM, 0);
mtk_idle_log("enable=%lu. count=%lu, is_cond_check = %lu\n\n",
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_SWITCH,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_DRAM, 0)
& MT_SPM_RC_VALID_SW,
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_CNT,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_DRAM, 0),
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_CHECK,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_DRAM, 0));
mtk_idle_log("is_cond_block = 0x%08x\n", block);
for (i = 0, b = block >> SPM_COND_BLOCKED_CG_IDX;
i < PLAT_SPM_COND_MAX; i++)
mtk_idle_log("%8s = 0x%08lx\n", mt6779_spm_cond_cg_str[i],
((b >> i) & 0x1) ?
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_BLOCK_DETAIL,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_DRAM, i) : 0);
for (i = 0, b = block >> SPM_COND_BLOCKED_PLL_IDX;
i < PLAT_SPM_COND_PLL_MAX; i++)
mtk_idle_log("%8s = %d\n", mt6779_spm_cond_pll_str[i],
((b >> i) & 0x1));
mtk_idle_log("*************** idle command help ****************\n");
mtk_idle_log("echo cond_check 0/1 > %s\n", MT_LP_DRAM_NODE);
return p - ToUserBuf;
}
static ssize_t mt6779_idle_dram_write(char *FromUserBuf,
size_t sz, void *priv)
{
char cmd[128];
int parm;
if (sscanf(FromUserBuf, "%127s %x", cmd, &parm) == 2) {
if (!strcmp(cmd, "cond_check")) {
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_CHECK,
MT_LPM_SMC_ACT_SET,
MT_RM_CONSTRAINT_ID_DRAM, !!parm);
}
return sz;
} else if ((!kstrtoint(FromUserBuf, 10, &parm)) == 1) {
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_SWITCH,
MT_LPM_SMC_ACT_SET,
MT_RM_CONSTRAINT_ID_DRAM, !!parm);
return sz;
}
return -EINVAL;
}
static const struct mtk_lp_sysfs_op mt6779_idle_dram_fops = {
.fs_read = mt6779_idle_dram_read,
.fs_write = mt6779_idle_dram_write,
};
static ssize_t mt6779_idle_syspll_read(char *ToUserBuf,
size_t sz, void *priv)
{
char *p = ToUserBuf;
uint32_t block, b;
int i;
block = mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_BLOCK,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_SYSPLL, 0);
mtk_idle_log("enable=%lu. count=%lu, is_cond_check = %lu\n\n",
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_SWITCH,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_SYSPLL, 0)
& MT_SPM_RC_VALID_SW,
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_CNT,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_SYSPLL, 0),
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_CHECK,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_SYSPLL, 0));
mtk_idle_log("is_cond_block = 0x%08x\n", block);
for (i = 0, b = block >> SPM_COND_BLOCKED_CG_IDX;
i < PLAT_SPM_COND_MAX; i++)
mtk_idle_log("%8s = 0x%08lx\n", mt6779_spm_cond_cg_str[i],
((b >> i) & 0x1) ?
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_BLOCK_DETAIL,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_SYSPLL, i) : 0);
for (i = 0, b = block >> SPM_COND_BLOCKED_PLL_IDX;
i < PLAT_SPM_COND_PLL_MAX; i++)
mtk_idle_log("%8s = %d\n", mt6779_spm_cond_pll_str[i],
((b >> i) & 0x1));
mtk_idle_log("*************** idle command help ****************\n");
mtk_idle_log("echo cond_check 0/1 > %s\n", MT_LP_SYSPLL_NODE);
return p - ToUserBuf;
}
static ssize_t mt6779_idle_syspll_write(char *FromUserBuf,
size_t sz, void *priv)
{
char cmd[128];
int parm;
if (sscanf(FromUserBuf, "%127s %x", cmd, &parm) == 2) {
if (!strcmp(cmd, "cond_check")) {
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_CHECK,
MT_LPM_SMC_ACT_SET,
MT_RM_CONSTRAINT_ID_SYSPLL, !!parm);
}
return sz;
} else if ((!kstrtoint(FromUserBuf, 10, &parm)) == 1) {
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_SWITCH,
MT_LPM_SMC_ACT_SET,
MT_RM_CONSTRAINT_ID_SYSPLL, !!parm);
return sz;
}
return -EINVAL;
}
static const struct mtk_lp_sysfs_op mt6779_idle_syspll_fops = {
.fs_read = mt6779_idle_syspll_read,
.fs_write = mt6779_idle_syspll_write,
};
static ssize_t mt6779_idle_bus26m_read(char *ToUserBuf,
size_t sz, void *priv)
{
char *p = ToUserBuf;
uint32_t block, b;
int i;
block = mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_BLOCK,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_BUS26M, 0);
mtk_idle_log("enable=%lu. count=%lu, is_cond_check = %lu\n\n",
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_SWITCH,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_BUS26M, 0)
& MT_SPM_RC_VALID_SW,
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_CNT,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_BUS26M, 0),
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_CHECK,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_BUS26M, 0));
mtk_idle_log("is_cond_block = 0x%08X\n", block);
for (i = 0, b = block >> SPM_COND_BLOCKED_CG_IDX;
i < PLAT_SPM_COND_MAX; i++)
mtk_idle_log("%8s = 0x%08lX\n", mt6779_spm_cond_cg_str[i],
((b >> i) & 0x1) ?
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_BLOCK_DETAIL,
MT_LPM_SMC_ACT_GET,
MT_RM_CONSTRAINT_ID_BUS26M, i) : 0);
for (i = 0, b = block >> SPM_COND_BLOCKED_PLL_IDX;
i < PLAT_SPM_COND_PLL_MAX; i++)
mtk_idle_log("%8s = %d\n", mt6779_spm_cond_pll_str[i],
((b >> i) & 0x1));
mtk_idle_log("*************** idle command help ****************\n");
mtk_idle_log("echo cond_check 0/1 > %s\n", MT_LP_BUS26M_NODE);
return p - ToUserBuf;
}
static ssize_t mt6779_idle_bus26m_write(char *FromUserBuf,
size_t sz, void *priv)
{
char cmd[128];
int parm;
if (sscanf(FromUserBuf, "%127s %x", cmd, &parm) == 2) {
if (!strcmp(cmd, "cond_check")) {
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_COND_CHECK,
MT_LPM_SMC_ACT_SET,
MT_RM_CONSTRAINT_ID_BUS26M, !!parm);
}
return sz;
} else if ((!kstrtoint(FromUserBuf, 10, &parm)) == 1) {
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_RC_SWITCH,
MT_LPM_SMC_ACT_SET,
MT_RM_CONSTRAINT_ID_BUS26M, !!parm);
return sz;
}
return -EINVAL;
}
static const struct mtk_lp_sysfs_op mt6779_idle_bus26m_fops = {
.fs_read = mt6779_idle_bus26m_read,
.fs_write = mt6779_idle_bus26m_write,
};
int mt6779_dbg_idle_fs_init(void)
{
/* enable resource constraint condition block latch */
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_BLOCK_LATCH,
MT_LPM_SMC_ACT_SET, 0, 0);
mtk_lpm_sysfs_root_entry_create();
mtk_lpm_sysfs_entry_node_add("IdleDram_state"
, 0644, &mt6779_idle_dram_fops, NULL);
mtk_lpm_sysfs_entry_node_add("IdleSyspll_state"
, 0644, &mt6779_idle_syspll_fops, NULL);
mtk_lpm_sysfs_entry_node_add("IdleBus26m_state"
, 0644, &mt6779_idle_bus26m_fops, NULL);
return 0;
}
int mt6779_dbg_idle_fs_deinit(void)
{
/* disable resource contraint condition block latch */
mtk_lpm_smc_spm_dbg(MT_SPM_DBG_SMC_UID_BLOCK_LATCH,
MT_LPM_SMC_ACT_CLR, 0, 0);
return 0;
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,321 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/io.h>
#include <aee.h>
#include <mtk_lpm.h>
#include <mt6779_spm_comm.h>
#include <mt6779_spm_reg.h>
#include <mt6779_pwr_ctrl.h>
#include <mt6779_pcm_def.h>
#include <mtk_dbg_common_v1.h>
#define BYPASS_LOG
#ifndef BYPASS_LOG
static struct mt6779_spm_wake_status mt6779_wake;
#endif
void __iomem *mt6779_spm_base;
const char *mt6779_wakesrc_str[32] = {
[0] = " R12_PCM_TIMER_EVENT",
[1] = " R12_SPM_TWAM_IRQ_B",
[2] = " R12_KP_IRQ_B",
[3] = " R12_APWDT_EVENT_B",
[4] = " R12_APXGPT1_EVENT_B",
[5] = " R12_CONN2AP_SPM_WAKEUP_B",
[6] = " R12_EINT_EVENT_B",
[7] = " R12_CONN_WDT_IRQ_B",
[8] = " R12_CCIF0_EVENT_B",
[9] = " R12_LOWBATTERY_IRQ_B",
[10] = " R12_SC_SSPM2SPM_WAKEUP",
[11] = " R12_SC_SCP2SPM_WAKEUP",
[12] = " R12_SC_ADSP2SPM_WAKEUP",
[13] = " R12_PCM_WDT_EVENT_B",
[14] = " R12_USBX_CDSC_B",
[15] = " R12_USBX_POWERDWN_B",
[16] = " R12_SYS_TIMER_EVENT_B",
[17] = " R12_EINT_EVENT_SECURE_B",
[18] = " R12_CCIF1_EVENT_B",
[19] = " R12_UART0_IRQ_B",
[20] = " R12_AFE_IRQ_MCU_B",
[21] = " R12_THERMAL_CTRL_EVENT_B",
[22] = " R12_SYS_CIRQ_IRQ_B",
[23] = " R12_MD2AP_PEER_WAKEUP_EVENT",
[24] = " R12_CSYSPWREQ_B",
[25] = " R12_MD1_WDT_B",
[26] = " R12_AP2AP_PEER_WAKEUP_EVENT",
[27] = " R12_SEJ_EVENT_B",
[28] = " R12_SPM_CPU_WAKEUP_EVENT",
[29] = " R12_CPU_IRQOUT",
[30] = " R12_CPU_WFI",
[31] = " R12_MCUSYS_IDLE_TO_EMI_ALL",
};
#define plat_mmio_read(offset) __raw_readl(mt6779_spm_base + offset)
int mt6779_get_wakeup_status(struct mt6779_spm_wake_status *wakesta)
{
if (!wakesta || !mt6779_spm_base)
return -EINVAL;
wakesta->r12 = plat_mmio_read(SPM_SW_RSV_0);
wakesta->r12_ext = plat_mmio_read(SPM_WAKEUP_STA);
wakesta->raw_sta = plat_mmio_read(SPM_WAKEUP_STA);
wakesta->raw_ext_sta = plat_mmio_read(SPM_WAKEUP_EXT_STA);
wakesta->md32pcm_wakeup_sta = plat_mmio_read(MD32PCM_WAKEUP_STA);
wakesta->md32pcm_event_sta = plat_mmio_read(MD32PCM_EVENT_STA);
wakesta->src_req = plat_mmio_read(SPM_SRC_REQ);
/* backup of SPM_WAKEUP_MISC */
wakesta->wake_misc = plat_mmio_read(SPM_SW_RSV_5);
/* get sleep time */
/* backup of PCM_TIMER_OUT */
wakesta->timer_out = plat_mmio_read(SPM_SW_RSV_6);
/* get other SYS and co-clock status */
wakesta->r13 = plat_mmio_read(PCM_REG13_DATA);
wakesta->idle_sta = plat_mmio_read(SUBSYS_IDLE_STA);
wakesta->req_sta0 = plat_mmio_read(SRC_REQ_STA_0);
wakesta->req_sta1 = plat_mmio_read(SRC_REQ_STA_1);
wakesta->req_sta2 = plat_mmio_read(SRC_REQ_STA_2);
wakesta->req_sta3 = plat_mmio_read(SRC_REQ_STA_3);
wakesta->req_sta4 = plat_mmio_read(SRC_REQ_STA_4);
/* get debug flag for PCM execution check */
wakesta->debug_flag = plat_mmio_read(PCM_WDT_LATCH_SPARE_0);
wakesta->debug_flag1 = plat_mmio_read(PCM_WDT_LATCH_SPARE_1);
/* get backup SW flag status */
wakesta->b_sw_flag0 = plat_mmio_read(SPM_SW_RSV_7);
wakesta->b_sw_flag1 = plat_mmio_read(SPM_SW_RSV_8);
/* get ISR status */
wakesta->isr = plat_mmio_read(SPM_IRQ_STA);
/* get SW flag status */
wakesta->sw_flag0 = plat_mmio_read(SPM_SW_FLAG_0);
wakesta->sw_flag1 = plat_mmio_read(SPM_SW_FLAG_1);
/* get CLK SETTLE */
wakesta->clk_settle = plat_mmio_read(SPM_CLK_SETTLE);
/* check abort */
wakesta->is_abort = wakesta->debug_flag & DEBUG_ABORT_MASK;
wakesta->is_abort |= wakesta->debug_flag1 & DEBUG_ABORT_MASK_1;
return 0;
}
#ifdef BYPASS_LOG
int mt6779_show_log_message(int type, const char *prefix, void *data)
{
return 0;
}
#else
int mt6779_show_log_message(int type, const char *prefix, void *data)
{
#define LOG_BUF_SIZE 256
#define LOG_BUF_OUT_SZ 768
int i;
char buf[LOG_BUF_SIZE] = { 0 };
char log_buf[LOG_BUF_OUT_SZ] = { 0 };
char *local_ptr;
int log_size = 0;
unsigned int wr = WR_UNKNOWN;
const char *scenario = prefix ?: "UNKNOWN";
mt6779_get_wakeup_status(&mt6779_wake);
/* Disable rcu lock checking */
rcu_irq_enter_irqson();
if (mt6779_wake.is_abort != 0) {
/* add size check for vcoredvfs */
aee_sram_printk("SPM ABORT (%s), r13 = 0x%x, ",
scenario, mt6779_wake.r13);
log_size += scnprintf(log_buf + log_size,
LOG_BUF_OUT_SZ - log_size,
"[SPM] ABORT (%s), r13 = 0x%x, ",
scenario, mt6779_wake.r13);
aee_sram_printk(" debug_flag = 0x%x 0x%x\n",
mt6779_wake.debug_flag, mt6779_wake.debug_flag1);
log_size += scnprintf(log_buf + log_size,
LOG_BUF_OUT_SZ - log_size,
" debug_flag = 0x%x 0x%x\n",
mt6779_wake.debug_flag, mt6779_wake.debug_flag1);
aee_sram_printk(" sw_flag = 0x%x 0x%x\n",
mt6779_wake.sw_flag0, mt6779_wake.sw_flag1);
log_size += scnprintf(log_buf + log_size,
LOG_BUF_OUT_SZ - log_size,
" sw_flag = 0x%x 0x%x\n",
mt6779_wake.sw_flag0, mt6779_wake.sw_flag1);
aee_sram_printk(" b_sw_flag = 0x%x 0x%x\n",
mt6779_wake.b_sw_flag0, mt6779_wake.b_sw_flag1);
log_size += scnprintf(log_buf + log_size,
LOG_BUF_OUT_SZ - log_size,
" b_sw_flag = 0x%x 0x%x\n",
mt6779_wake.b_sw_flag0, mt6779_wake.b_sw_flag1);
wr = WR_ABORT;
}
if (mt6779_wake.r12 & R12_PCM_TIMER_EVENT) {
if (mt6779_wake.wake_misc & WAKE_MISC_PCM_TIMER_EVENT) {
local_ptr = " PCM_TIMER";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_PCM_TIMER;
}
}
if (mt6779_wake.r12 & R12_SPM_TWAM_IRQ_B) {
if (mt6779_wake.wake_misc & WAKE_MISC_DVFSRC_IRQ) {
local_ptr = " DVFSRC";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_DVFSRC;
}
if (mt6779_wake.wake_misc & WAKE_MISC_TWAM_IRQ_B) {
local_ptr = " TWAM";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_TWAM;
}
if (mt6779_wake.wake_misc & WAKE_MISC_PMSR_IRQ_B_SET0) {
local_ptr = " PMSR";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_PMSR;
}
if (mt6779_wake.wake_misc & WAKE_MISC_PMSR_IRQ_B_SET1) {
local_ptr = " PMSR";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_PMSR;
}
if (mt6779_wake.wake_misc & WAKE_MISC_SPM_ACK_CHK_WAKEUP_0) {
local_ptr = " SPM_ACK_CHK";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_SPM_ACK_CHK;
}
if (mt6779_wake.wake_misc & WAKE_MISC_SPM_ACK_CHK_WAKEUP_1) {
local_ptr = " SPM_ACK_CHK";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_SPM_ACK_CHK;
}
if (mt6779_wake.wake_misc & WAKE_MISC_SPM_ACK_CHK_WAKEUP_2) {
local_ptr = " SPM_ACK_CHK";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_SPM_ACK_CHK;
}
if (mt6779_wake.wake_misc & WAKE_MISC_SPM_ACK_CHK_WAKEUP_3) {
local_ptr = " SPM_ACK_CHK";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_SPM_ACK_CHK;
}
if (mt6779_wake.wake_misc & WAKE_MISC_SPM_ACK_CHK_WAKEUP_ALL) {
local_ptr = " SPM_ACK_CHK";
if ((strlen(buf) + strlen(local_ptr)) < LOG_BUF_SIZE)
strncat(buf, local_ptr, strlen(local_ptr));
wr = WR_SPM_ACK_CHK;
}
}
for (i = 1; i < 32; i++) {
if (mt6779_wake.r12 & (1U << i)) {
if ((strlen(buf) + strlen(mt6779_wakesrc_str[i])) <
LOG_BUF_SIZE)
strncat(buf, mt6779_wakesrc_str[i],
strlen(mt6779_wakesrc_str[i]));
wr = WR_WAKE_SRC;
}
}
WARN_ON(strlen(buf) >= LOG_BUF_SIZE);
log_size += scnprintf(log_buf + log_size, LOG_BUF_OUT_SZ - log_size,
"%s wake up by %s, timer_out = %u, r13 = 0x%x, debug_flag = 0x%x 0x%x, ",
scenario, buf, mt6779_wake.timer_out, mt6779_wake.r13,
mt6779_wake.debug_flag, mt6779_wake.debug_flag1);
log_size += scnprintf(log_buf + log_size, LOG_BUF_OUT_SZ - log_size,
"r12 = 0x%x, r12_ext = 0x%x, raw_sta = 0x%x 0x%x 0x%x, idle_sta = 0x%x, ",
mt6779_wake.r12, mt6779_wake.r12_ext, mt6779_wake.raw_sta,
mt6779_wake.md32pcm_wakeup_sta, mt6779_wake.md32pcm_event_sta,
mt6779_wake.idle_sta);
log_size += scnprintf(log_buf + log_size, LOG_BUF_OUT_SZ - log_size,
" req_sta = 0x%x 0x%x 0x%x 0x%x 0x%x, isr = 0x%x, ",
mt6779_wake.req_sta0, mt6779_wake.req_sta1,
mt6779_wake.req_sta2, mt6779_wake.req_sta3,
mt6779_wake.req_sta4, mt6779_wake.isr);
log_size += scnprintf(log_buf + log_size,
LOG_BUF_OUT_SZ - log_size,
"raw_ext_sta = 0x%x, wake_misc = 0x%x, pcm_flag = 0x%x 0x%x 0x%x 0x%x, req = 0x%x, ",
mt6779_wake.raw_ext_sta,
mt6779_wake.wake_misc,
mt6779_wake.sw_flag0,
mt6779_wake.sw_flag1, mt6779_wake.b_sw_flag0,
mt6779_wake.b_sw_flag0,
mt6779_wake.src_req);
log_size += scnprintf(log_buf + log_size,
LOG_BUF_OUT_SZ - log_size,
" clk_settle = 0x%x, ", mt6779_wake.clk_settle);
WARN_ON(log_size >= LOG_BUF_OUT_SZ);
pr_info("[name:spm&][SPM] %s", log_buf);
/* Eable rcu lock checking */
rcu_irq_exit_irqson();
return wr;
}
#endif
struct mtk_lpm_issuer mt6779_issuer = {
.log = mt6779_show_log_message,
};
int mt6779_logger_init(void)
{
struct device_node *node = NULL;
node = of_find_compatible_node(NULL, NULL, "mediatek,spm");
if (node) {
mt6779_spm_base = of_iomap(node, 0);
of_node_put(node);
}
if (mt6779_spm_base)
mtk_lp_issuer_register(&mt6779_issuer);
else
pr_info("[name:mtk_lpm][P] - Don't register the issue by error! (%s:%d)\n",
__func__, __LINE__);
return 0;
}
EXPORT_SYMBOL(mt6779_logger_init);
@@ -0,0 +1,296 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/pm_qos.h>
#include <mtk_lpm.h>
#include <mtk_lp_plat_reg.h>
#include "mtk_cpupm_dbg.h"
#include "mtk_cpuidle_cpc.h"
static DEFINE_SPINLOCK(cpc_prof_spin_lock);
/* profile latecny */
#define PROF_DEV_NAME_SIZE 36
#define get_core_type_index(cpu) ((cpu > 3) ? CPU_TYPE_B : CPU_TYPE_L)
struct mtk_cpc_lat_data {
char name[PROF_DEV_NAME_SIZE];
unsigned int on_sum;
unsigned int on_max;
unsigned int off_sum;
unsigned int off_max;
unsigned int on_cnt;
unsigned int off_cnt;
};
struct mtk_cpc_device {
union {
struct mtk_cpc_lat_data p[DEV_TYPE_NUM];
struct {
struct mtk_cpc_lat_data cpu[NF_CPU_TYPE];
struct mtk_cpc_lat_data cluster;
struct mtk_cpc_lat_data mcusys;
};
};
bool prof_en;
};
struct mtk_cpc_device cpc;
static void mtk_cpc_clr_lat(void)
{
unsigned int size;
unsigned int ofs;
int i;
ofs = sizeof(cpc.p[0].name);
size = sizeof(struct mtk_cpc_lat_data) - ofs;
for (i = 0; i < DEV_TYPE_NUM; i++)
memset((char *)&cpc.p[i] + ofs, 0, size);
}
static void mtk_cpc_cal_lat(void)
{
}
static void mtk_cpc_get_cpu_lat(int cpu, unsigned int *on, unsigned int *off)
{
unsigned int lat;
lat = mtk_cpupm_mcusys_read(CPC_CPU_LATENCY(cpu));
*on = (lat >> 16) & 0xFFFF;
*off = lat & 0xFFFF;
}
static void mtk_cpc_get_cpusys_lat(unsigned int *on, unsigned int *off)
{
unsigned int lat_on;
unsigned int lat_off;
lat_on = mtk_cpupm_mcusys_read(CPC_CLUSTER_ON_LATENCY);
lat_off = mtk_cpupm_mcusys_read(CPC_CLUSTER_OFF_LATENCY);
*on = lat_on & 0xFFFF;
*off = lat_off & 0xFFFF;
}
static void mtk_cpc_get_mcusys_lat(unsigned int *on, unsigned int *off)
{
unsigned int lat;
lat = mtk_cpupm_mcusys_read(CPC_MCUSYS_LATENCY);
*on = (lat >> 16) & 0xFFFF;
*off = lat & 0xFFFF;
}
#define __mtk_cpc_record_lat(sum, max, lat) \
do { \
if (lat > max) \
max = lat; \
(sum) += (lat); \
} while (0)
static void mtk_cpc_on_record_lat(struct mtk_cpc_lat_data *lat,
unsigned int ticks)
{
unsigned long flags;
if (ticks == 0)
return;
spin_lock_irqsave(&cpc_prof_spin_lock, flags);
__mtk_cpc_record_lat(lat->on_sum, lat->on_max, ticks);
lat->on_cnt++;
spin_unlock_irqrestore(&cpc_prof_spin_lock, flags);
}
static void mtk_cpc_off_record_lat(struct mtk_cpc_lat_data *lat,
unsigned int ticks)
{
unsigned long flags;
if (ticks == 0)
return;
spin_lock_irqsave(&cpc_prof_spin_lock, flags);
__mtk_cpc_record_lat(lat->off_sum, lat->off_max, ticks);
lat->off_cnt++;
spin_unlock_irqrestore(&cpc_prof_spin_lock, flags);
}
static bool mtk_cpc_did_cluster_pwr_off(void)
{
static unsigned int last_cnt;
unsigned int cnt = mtk_cpupm_mcusys_read(CPC_DORMANT_COUNTER);
/**
* Cluster off count
* bit[0:15] : memory retention
* bit[16:31] : memory off
*/
if ((cnt & 0x7FFF) == 0)
cnt = ((cnt >> 16) & 0x7FFF);
else
cnt = cnt & 0x7FFF;
cnt += mtk_cpupm_syssram_read(SYSRAM_CPC_CPUSYS_CNT_BACKUP);
if (last_cnt == cnt)
return false;
last_cnt = cnt;
return true;
}
static bool mtk_cpc_did_mcusys_pwr_off(void)
{
static unsigned int last_cnt;
unsigned int cnt = mtk_cpupm_syssram_read(SYSRAM_CPC_MCUSYS_CNT_BACKUP);
cnt += mtk_cpupm_syssram_read(SYSRAM_MCUSYS_CNT);
if (last_cnt == cnt)
return false;
last_cnt = cnt;
return true;
}
static void mtk_cpc_save_latency(int cpu)
{
unsigned int on, off;
struct mtk_cpc_lat_data *lat;
lat = &cpc.cpu[get_core_type_index(cpu)];
mtk_cpc_get_cpu_lat(cpu, &on, &off);
mtk_cpc_on_record_lat(lat, on);
mtk_cpc_off_record_lat(lat, off);
if (mtk_cpc_did_mcusys_pwr_off()) {
lat = &cpc.mcusys;
mtk_cpc_get_mcusys_lat(&on, &off);
mtk_cpc_on_record_lat(lat, on);
mtk_cpc_off_record_lat(lat, off);
} else if (!mtk_cpc_did_cluster_pwr_off()) {
return;
}
lat = &cpc.cluster;
mtk_cpc_get_cpusys_lat(&on, &off);
mtk_cpc_on_record_lat(lat, on);
mtk_cpc_off_record_lat(lat, off);
}
void mtk_cpc_prof_lat_dump(struct seq_file *m)
{
struct mtk_cpc_lat_data *lat;
int i;
for (i = 0; i < DEV_TYPE_NUM; i++) {
lat = &cpc.p[i];
seq_printf(m, "%s\n", lat->name);
if (lat->off_cnt) {
seq_printf(m, "\toff : avg = %2dus, max = %3dus, cnt = %d\n",
cpc_tick_to_us(lat->off_sum / lat->off_cnt),
cpc_tick_to_us(lat->off_max),
lat->off_cnt);
} else {
seq_puts(m, "\toff : None\n");
}
if (lat->on_cnt) {
seq_printf(m, "\ton : avg = %2dus, max = %3dus, cnt = %d\n",
cpc_tick_to_us(lat->on_sum / lat->on_cnt),
cpc_tick_to_us(lat->on_max),
lat->on_cnt);
} else {
seq_puts(m, "\ton : None\n");
}
}
}
void mtk_cpc_prof_start(void)
{
mtk_cpupm_block();
cpc_prof_en();
mtk_cpc_clr_lat();
cpc.prof_en = true;
mtk_cpupm_allow();
}
void mtk_cpc_prof_stop(void)
{
mtk_cpupm_block();
cpc.prof_en = false;
mtk_cpc_cal_lat();
cpc_prof_dis();
mtk_cpupm_allow();
}
int mtk_cpc_notify(struct notifier_block *nb,
unsigned long action, void *data)
{
struct mtk_lpm_nb_data *nb_data = (struct mtk_lpm_nb_data *)data;
if (!cpc.prof_en)
return NOTIFY_OK;
if (action & MTK_LPM_NB_BEFORE_REFLECT)
mtk_cpc_save_latency(nb_data->cpu);
return NOTIFY_OK;
}
struct notifier_block mtk_cpc_nb = {
.notifier_call = mtk_cpc_notify,
};
int __init mtk_cpc_init(void)
{
cpc.prof_en = false;
snprintf(cpc.cpu[CPU_TYPE_L].name, PROF_DEV_NAME_SIZE, "cpu_L");
snprintf(cpc.cpu[CPU_TYPE_B].name, PROF_DEV_NAME_SIZE, "cpu_B");
snprintf(cpc.cluster.name, PROF_DEV_NAME_SIZE, "cluster");
snprintf(cpc.mcusys.name, PROF_DEV_NAME_SIZE, "mcusys");
mtk_lpm_notifier_register(&mtk_cpc_nb);
return 0;
}
void __exit mtk_cpc_exit(void)
{
mtk_lpm_notifier_unregister(&mtk_cpc_nb);
}
@@ -0,0 +1,95 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MTK_CPUIDLE_CPC_H__
#define __MTK_CPUIDLE_CPC_H__
#include <linux/seq_file.h>
#include <mtk_lpm_module.h>
enum {
CPU_TYPE_L,
CPU_TYPE_B,
NF_CPU_TYPE
};
enum {
CPC_SMC_EVENT_DUMP_TRACE_DATA,
CPC_SMC_EVENT_GIC_DPG_SET,
CPC_SMC_EVENT_CPC_CONFIG,
CPC_SMC_EVENT_READ_CONFIG,
NF_CPC_SMC_EVENT
};
enum {
CPC_SMC_CONFIG_PROF,
CPC_SMC_CONFIG_AUTO_OFF,
CPC_SMC_CONFIG_AUTO_OFF_THRES,
CPC_SMC_CONFIG_CNT_CLR,
NF_CPC_SMC_CONFIG
};
/**
* number of profile type :
* - (number of cpu type) + cluster + mcusys
*/
#define DEV_TYPE_NUM (NF_CPU_TYPE + 2)
/* smc */
#define mtk_cpc_smc(act, arg1, arg2)\
mtk_lpm_smc_cpu_pm(CPC_COMMAND, act, arg1, arg2)
/* configuration */
#define cpc_prof_en()\
mtk_cpc_smc(CPC_SMC_EVENT_CPC_CONFIG\
, CPC_SMC_CONFIG_PROF, 1)
#define cpc_prof_dis()\
mtk_cpc_smc(CPC_SMC_EVENT_CPC_CONFIG\
, CPC_SMC_CONFIG_PROF, 0)
#define cpc_auto_off_en()\
mtk_cpc_smc(CPC_SMC_EVENT_CPC_CONFIG\
, CPC_SMC_CONFIG_AUTO_OFF, 1)
#define cpc_auto_off_dis()\
mtk_cpc_smc(CPC_SMC_EVENT_CPC_CONFIG\
, CPC_SMC_CONFIG_AUTO_OFF, 0)
#define cpc_set_auto_off_thres(us)\
mtk_cpc_smc(CPC_SMC_EVENT_CPC_CONFIG\
, CPC_SMC_CONFIG_AUTO_OFF_THRES, us)
#define cpc_cluster_cnt_clr()\
mtk_cpc_smc(CPC_SMC_EVENT_CPC_CONFIG\
, CPC_SMC_CONFIG_CNT_CLR, 0)
/* status */
#define cpc_get_prof_sta()\
mtk_cpc_smc(CPC_SMC_EVENT_READ_CONFIG\
, CPC_SMC_CONFIG_PROF, 0)
#define cpc_get_auto_off_sta()\
mtk_cpc_smc(CPC_SMC_EVENT_READ_CONFIG\
, CPC_SMC_CONFIG_AUTO_OFF, 0)
#define cpc_get_auto_off_thres()\
mtk_cpc_smc(CPC_SMC_EVENT_READ_CONFIG\
, CPC_SMC_CONFIG_AUTO_OFF_THRES, 0)
#define cpc_tick_to_us(val) ((val) / 13)
void mtk_cpc_prof_start(void);
void mtk_cpc_prof_stop(void);
void mtk_cpc_prof_lat_dump(struct seq_file *m);
int __init mtk_cpc_init(void);
void __exit mtk_cpc_exit(void);
#endif /* __MTK_CPUIDLE_CPC_H__ */
@@ -0,0 +1,612 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/cpuidle.h>
#include <linux/sched/clock.h>
#include <linux/cpu.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/pm_qos.h>
#include <linux/tick.h>
#include <linux/timer.h>
#include <mboot_params.h>
#include <mtk_lpm.h>
#include <mtk_lp_plat_reg.h>
#include "mtk_cpupm_dbg.h"
#include "mtk_cpuidle_status.h"
#include "mtk_cpuidle_cpc.h"
#define DUMP_INTERVAL sec_to_ns(5)
static u64 last_dump_ns;
/* stress test */
static unsigned int timer_interval = 10 * 1000;
static struct task_struct *stress_tsk[NR_CPUS];
/* CPU off state : core, cluster and mcusys */
#define CPU_OFF_MAX_LV (3)
/* all core idle profiling */
struct all_cpu_idle_data {
unsigned long long last_ns;
unsigned long long dur_ns;
unsigned int cnt;
unsigned int bp; /* basis points : 0.01% */
};
struct all_cpu_idle {
unsigned long long start_us;
struct all_cpu_idle_data lv[CPU_OFF_MAX_LV];
};
static struct all_cpu_idle all_core_off;
/* idle ratio profiling */
struct mtk_cpuidle_ratio {
unsigned long long start_us;
unsigned long long idle_time_ns[CPUIDLE_STATE_MAX];
unsigned int bp[CPUIDLE_STATE_MAX];
};
/* idle latency profiling */
struct mtk_cpuidle_prof {
unsigned int cnt;
unsigned int max;
union {
u64 avg;
u64 sum;
};
};
/* idle information */
struct mtk_cpuidle_info {
u64 enter_time_ns;
int idle_index;
int cnt[CPUIDLE_STATE_MAX];
};
/* mtk cpu idle status */
struct mtk_cpuidle_device {
struct mtk_cpuidle_ratio ratio;
struct mtk_cpuidle_info info;
struct mtk_cpuidle_prof prof;
struct hrtimer timer;
int cpu;
int state_count;
bool tmr_running;
};
static DEFINE_PER_CPU(struct mtk_cpuidle_device, mtk_cpuidle_dev);
/* mtk cpu idle configuration */
struct mtk_cpuidle_control {
bool tmr_en;
bool prof_en;
bool log_en;
bool stress_en;
};
static struct mtk_cpuidle_control mtk_cpuidle_ctrl;
static int mtk_cpuidle_stress_task(void *arg)
{
while (mtk_cpuidle_ctrl.stress_en)
usleep_range(timer_interval - 10, timer_interval + 10);
return 0;
}
static void mtk_cpuidle_stress_start(void)
{
int i;
char name[20] = {0};
int ret = 0;
if (mtk_cpuidle_ctrl.stress_en)
return;
mtk_cpuidle_ctrl.stress_en = true;
for_each_online_cpu(i) {
ret = scnprintf(name, sizeof(name), "mtk_cpupm_stress_%d", i);
stress_tsk[i] =
kthread_create(mtk_cpuidle_stress_task, NULL, name);
if (!IS_ERR(stress_tsk[i])) {
kthread_bind(stress_tsk[i], i);
wake_up_process(stress_tsk[i]);
}
}
}
static void mtk_cpuidle_stress_stop(void)
{
mtk_cpuidle_ctrl.stress_en = false;
msleep(20);
}
void mtk_cpuidle_set_stress_test(bool en)
{
if (en)
mtk_cpuidle_stress_start();
else
mtk_cpuidle_stress_stop();
}
bool mtk_cpuidle_get_stress_status(void)
{
return mtk_cpuidle_ctrl.stress_en;
}
void mtk_cpuidle_set_stress_time(unsigned int val)
{
timer_interval = clamp_val(val, 100, 20000);
}
unsigned int mtk_cpuidle_get_stress_time(void)
{
return timer_interval;
}
void mtk_cpuidle_ctrl_timer_en(bool enable)
{
mtk_cpupm_block();
mtk_cpuidle_ctrl.tmr_en = enable;
mtk_cpupm_allow();
}
bool mtk_cpuidle_ctrl_timer_sta_get(void)
{
return mtk_cpuidle_ctrl.tmr_en;
}
void mtk_cpuidle_ctrl_log_en(bool enable)
{
mtk_cpupm_block();
mtk_cpuidle_ctrl.log_en = enable;
mtk_cpupm_allow();
}
bool mtk_cpuidle_ctrl_log_sta_get(void)
{
return mtk_cpuidle_ctrl.log_en;
}
static void _mtk_cpuidle_prof_ratio_start(void *data)
{
int i, cpu;
struct mtk_cpuidle_device *mtk_idle;
cpu = get_cpu();
mtk_idle = &per_cpu(mtk_cpuidle_dev, cpu);
put_cpu();
if (unlikely(!mtk_idle))
return;
mtk_idle->ratio.start_us = div64_u64(sched_clock(), 1000);
for (i = 0; i < mtk_idle->state_count; i++)
mtk_idle->ratio.idle_time_ns[i] = 0;
}
void mtk_cpuidle_prof_ratio_start(void)
{
int i;
if (mtk_cpuidle_ctrl.prof_en)
return;
mtk_cpupm_block();
for (i = 0; i < CPU_OFF_MAX_LV; i++) {
all_core_off.lv[i].dur_ns = 0;
all_core_off.lv[i].cnt = 0;
}
all_core_off.start_us = div64_u64(sched_clock(), 1000);
on_each_cpu(_mtk_cpuidle_prof_ratio_start, NULL, 1);
mtk_cpuidle_ctrl.prof_en = true;
mtk_cpupm_allow();
}
static void _mtk_cpuidle_prof_ratio_stop(void *stop_time)
{
int i, cpu;
uint64_t dur_us;
struct mtk_cpuidle_device *mtk_idle;
cpu = get_cpu();
mtk_idle = &per_cpu(mtk_cpuidle_dev, cpu);
put_cpu();
if (unlikely(!mtk_idle))
return;
dur_us = div64_u64(sched_clock(), 1000) -
mtk_idle->ratio.start_us;
for (i = 0; i < mtk_idle->state_count; i++)
mtk_idle->ratio.bp[i] = (unsigned int)div64_u64(
mtk_idle->ratio.idle_time_ns[i] * 10,
dur_us);
}
void mtk_cpuidle_prof_ratio_stop(void)
{
int i;
uint64_t time_us;
if (!mtk_cpuidle_ctrl.prof_en)
return;
mtk_cpupm_block();
on_each_cpu(_mtk_cpuidle_prof_ratio_stop, NULL, 1);
mtk_cpuidle_ctrl.prof_en = false;
time_us = div64_u64(sched_clock(), 1000) - all_core_off.start_us;
for (i = 0; i < CPU_OFF_MAX_LV; i++)
all_core_off.lv[i].bp = (unsigned int)div64_u64(
all_core_off.lv[i].dur_ns * 10, time_us);
mtk_cpupm_allow();
}
void mtk_cpuidle_prof_ratio_dump(struct seq_file *m)
{
int i, cpu;
struct mtk_cpuidle_device *mtk_idle;
seq_printf(m, "%6s%12s%12s%12s\n",
"",
"cpu_off",
"cluster_off",
"mcusys_off");
for_each_possible_cpu(cpu) {
mtk_idle = &per_cpu(mtk_cpuidle_dev, cpu);
if (!mtk_idle)
continue;
seq_printf(m, "cpu%d: ", cpu);
for (i = 1; i < mtk_idle->state_count; i++) {
seq_printf(m, "%5s%3u.%02u%%",
"",
mtk_idle->ratio.bp[i] / 100,
mtk_idle->ratio.bp[i] % 100);
}
seq_puts(m, "\n");
}
seq_puts(m, "All core off ratio :\n");
for (i = 0; i < CPU_OFF_MAX_LV; i++)
seq_printf(m, "\t C_%d = %3u.%02u%%\n",
i + 1,
all_core_off.lv[i].bp / 100,
all_core_off.lv[i].bp % 100);
}
void mtk_cpuidle_state_enable(bool en)
{
struct cpuidle_driver *drv = NULL;
int i, cpu;
mtk_cpupm_block();
for_each_possible_cpu(cpu) {
//drv = cpuidle_get_cpu_driver(per_cpu(cpuidle_devices, cpu));
if (!drv)
continue;
for (i = drv->state_count - 1; i > 0; i--)
mtk_cpuidle_set_param(drv, i, IDLE_PARAM_EN, en);
}
mtk_cpupm_allow();
}
static bool mtk_cpuidle_need_dump(unsigned int idx)
{
u64 curr_ns;
bool dump = false;
if (!mtk_cpuidle_ctrl.log_en || !idx)
return false;
curr_ns = sched_clock();
if (curr_ns - last_dump_ns > DUMP_INTERVAL) {
last_dump_ns = curr_ns;
dump = true;
}
return dump;
}
static unsigned int mtk_cpuidle_get_cluster_off_cnt(void)
{
unsigned int cnt = mtk_cpupm_mcusys_read(CPC_DORMANT_COUNTER);
/**
* Cluster off count
* bit[0:15] : memory retention
* bit[16:31] : memory off
*/
if ((cnt & 0x7FFF) == 0)
cnt = ((cnt >> 16) & 0x7FFF);
else
cnt = cnt & 0x7FFF;
cnt += mtk_cpupm_syssram_read(SYSRAM_CPC_CPUSYS_CNT_BACKUP);
cpc_cluster_cnt_clr();
mtk_cpupm_syssram_write(SYSRAM_CPC_CPUSYS_CNT_BACKUP, 0);
mtk_cpupm_syssram_write(SYSRAM_CPUSYS_CNT,
mtk_cpupm_syssram_read(SYSRAM_CPUSYS_CNT) + cnt);
return cnt;
}
static unsigned int mtk_cpuidle_get_mcusys_off_cnt(void)
{
unsigned int cnt = mtk_cpupm_syssram_read(SYSRAM_CPC_MCUSYS_CNT_BACKUP);
mtk_cpupm_syssram_write(SYSRAM_CPC_MCUSYS_CNT_BACKUP, 0);
mtk_cpupm_syssram_write(SYSRAM_MCUSYS_CNT,
mtk_cpupm_syssram_read(SYSRAM_MCUSYS_CNT) + cnt);
return cnt;
}
static void mtk_cpuidle_dump_info(void)
{
struct mtk_cpuidle_device *mtk_idle;
int cpu, idx, ofs;
unsigned int avail_cpu_mask = 0;
for_each_possible_cpu(cpu) {
if (cpu_online(cpu))
avail_cpu_mask |= (1 << cpu);
mtk_idle = &per_cpu(mtk_cpuidle_dev, cpu);
if (!mtk_idle)
continue;
ofs = SYSRAM_RECENT_CPU_CNT(cpu);
mtk_cpupm_syssram_write(ofs, 0);
for (idx = 1; idx < mtk_idle->state_count ; idx++) {
mtk_cpupm_syssram_write(ofs, mtk_cpupm_syssram_read(ofs)
+ mtk_idle->info.cnt[idx]);
mtk_idle->info.cnt[idx] = 0;
}
}
mtk_cpupm_syssram_write(SYSRAM_RECENT_CPUSYS_CNT,
mtk_cpuidle_get_cluster_off_cnt());
mtk_cpupm_syssram_write(SYSRAM_RECENT_MCUSYS_CNT,
mtk_cpuidle_get_mcusys_off_cnt());
mtk_cpupm_syssram_write(SYSRAM_CPU_ONLINE, avail_cpu_mask);
mtk_cpupm_syssram_write(SYSRAM_RECENT_CNT_TS_H,
(unsigned int)((last_dump_ns >> 32) & 0xFFFFFFFF));
mtk_cpupm_syssram_write(SYSRAM_RECENT_CNT_TS_L,
(unsigned int)(last_dump_ns & 0xFFFFFFFF));
}
static enum hrtimer_restart mtk_cpuidle_hrtimer_func(struct hrtimer *timer)
{
return HRTIMER_NORESTART;
}
static void mtk_cpuidle_set_timer(struct mtk_cpuidle_device *mtk_idle)
{
int index;
int limit_sleep_us;
struct cpuidle_driver *drv;
if (!mtk_cpuidle_ctrl.tmr_en)
return;
index = mtk_idle->info.idle_index;
/* Only support WFI/CPU_OFF state */
if (index > 1)
return;
drv = cpuidle_get_driver();
if (unlikely(!drv))
return;
if (index + 1 >= mtk_idle->state_count)
//|| !tick_nohz_tick_stopped())
return;
limit_sleep_us = 4 * max_t(unsigned int,
get_residency(drv, index),
get_residency(drv, index + 1));
if (index != 0)
tick_broadcast_exit();
//RCU_NONIDLE(hrtimer_start(&mtk_idle->timer,
// ns_to_ktime(limit_sleep_us * NSEC_PER_USEC),
// HRTIMER_MODE_REL_PINNED));
mtk_idle->tmr_running = true;
if (index != 0)
tick_broadcast_enter();
}
static void mtk_cpuidle_cancel_timer(struct mtk_cpuidle_device *mtk_idle)
{
if (!mtk_cpuidle_ctrl.tmr_en)
return;
if (mtk_idle->tmr_running) {
mtk_idle->tmr_running = true;
//RCU_NONIDLE(hrtimer_try_to_cancel(&mtk_idle->timer));
}
}
static void mtk_cpuidle_set_last_off_ts(int idx)
{
int i;
uint64_t curr_ns = sched_clock();
if (!idx)
return;
if (idx > CPU_OFF_MAX_LV)
idx = CPU_OFF_MAX_LV;
for (i = 0; i < idx; i++) {
all_core_off.lv[i].cnt++;
if (all_core_off.lv[i].cnt == num_online_cpus())
all_core_off.lv[i].last_ns = curr_ns;
}
}
static void mtk_cpuidle_save_idle_time(int idx)
{
int i;
uint64_t curr_ns = sched_clock();
if (!idx)
return;
if (idx > CPU_OFF_MAX_LV)
idx = CPU_OFF_MAX_LV;
for (i = 0; i < idx; i++) {
if (all_core_off.lv[i].cnt == num_online_cpus())
all_core_off.lv[i].dur_ns += (curr_ns
- all_core_off.lv[i].last_ns);
all_core_off.lv[i].cnt--;
}
}
static int mtk_cpuidle_status_update(struct notifier_block *nb,
unsigned long action, void *data)
{
struct mtk_cpuidle_device *mtk_idle;
struct mtk_lpm_nb_data *nb_data = (struct mtk_lpm_nb_data *)data;
if (action & MTK_LPM_NB_BEFORE_REFLECT) {
/* prevent race conditions by mtk_lp_mod_locker */
if (mtk_cpuidle_ctrl.prof_en)
mtk_cpuidle_save_idle_time(nb_data->index);
if (mtk_cpuidle_need_dump(nb_data->index))
mtk_cpuidle_dump_info();
} else if (action & MTK_LPM_NB_RESUME) {
//aee_rr_rec_mcdi_val(nb_data->cpu,
// (nb_data->index << 16) | 0x0);
mtk_idle = &per_cpu(mtk_cpuidle_dev, nb_data->cpu);
mtk_idle->info.idle_index = -1;
mtk_idle->info.cnt[nb_data->index]++;
if (mtk_cpuidle_ctrl.prof_en)
mtk_idle->ratio.idle_time_ns[nb_data->index] +=
(sched_clock() - mtk_idle->info.enter_time_ns);
mtk_cpuidle_cancel_timer(mtk_idle);
} else if (action & MTK_LPM_NB_AFTER_PROMPT) {
/* prevent race conditions by mtk_lp_mod_locker */
if (mtk_cpuidle_ctrl.prof_en)
mtk_cpuidle_set_last_off_ts(nb_data->index);
} else if (action & MTK_LPM_NB_PREPARE) {
mtk_idle = &per_cpu(mtk_cpuidle_dev, nb_data->cpu);
mtk_idle->info.idle_index = nb_data->index;
mtk_idle->info.enter_time_ns = sched_clock();
mtk_cpuidle_set_timer(mtk_idle);
//aee_rr_rec_mcdi_val(nb_data->cpu,
// (nb_data->index << 16) | 0xff);
}
return NOTIFY_OK;
}
struct notifier_block mtk_cpuidle_status_nb = {
.notifier_call = mtk_cpuidle_status_update,
};
static void mtk_cpuidle_init_per_cpu(void *info)
{
struct mtk_cpuidle_device *mtk_idle;
int cpu;
cpu = get_cpu();
mtk_idle = &per_cpu(mtk_cpuidle_dev, cpu);
put_cpu();
hrtimer_init(&mtk_idle->timer,
CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mtk_idle->cpu = cpu;
mtk_idle->timer.function = mtk_cpuidle_hrtimer_func;
mtk_idle->state_count = mtk_cpupm_get_idle_state_count(cpu);
}
int __init mtk_cpuidle_status_init(void)
{
mtk_cpuidle_ctrl.tmr_en = true;
mtk_cpuidle_ctrl.prof_en = false;
mtk_cpuidle_ctrl.log_en = true;
mtk_cpuidle_ctrl.stress_en = false;
mtk_cpupm_block();
on_each_cpu(mtk_cpuidle_init_per_cpu, NULL, 0);
mtk_cpupm_allow();
mtk_lpm_notifier_register(&mtk_cpuidle_status_nb);
return 0;
}
void __exit mtk_cpuidle_status_exit(void)
{
mtk_lpm_notifier_unregister(&mtk_cpuidle_status_nb);
}
@@ -0,0 +1,69 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MTK_CPUIDLE_STATUS_H__
#define __MTK_CPUIDLE_STATUS_H__
#include <linux/seq_file.h>
#define sec_to_ns(v) ((v) * 1000 * 1000 * 1000ULL)
enum {
IDLE_PARAM_LAT,
IDLE_PARAM_RES,
IDLE_PARAM_EN,
NF_IDLE_PARAM
};
#define get_residency(drv, idx)\
((drv)->states[idx].target_residency)
#define get_latency(drv, idx)\
((drv)->states[idx].exit_latency)
#define get_disabled(drv, idx)\
((drv)->states[idx].disabled)
#define mtk_cpuidle_get_param(drv, state, param) \
({ \
int val = -1; \
if (param == IDLE_PARAM_LAT) \
val = (int)get_latency(drv, state); \
else if (param == IDLE_PARAM_RES) \
val = (int)get_residency(drv, state); \
else if (param == IDLE_PARAM_EN) \
val = get_disabled(drv, state) ? 0 : 1 ; \
val; \
})
#define mtk_cpuidle_set_param(drv, state, param, val) \
do { \
if (param == IDLE_PARAM_LAT) \
get_latency(drv, state) = val; \
else if (param == IDLE_PARAM_RES) \
get_residency(drv, state) = val; \
else if (param == IDLE_PARAM_EN) \
get_disabled(drv, state) = !val ; \
} while (0)
void mtk_cpuidle_set_stress_test(bool en);
bool mtk_cpuidle_get_stress_status(void);
void mtk_cpuidle_set_stress_time(unsigned int val);
unsigned int mtk_cpuidle_get_stress_time(void);
void mtk_cpuidle_prof_ratio_start(void);
void mtk_cpuidle_prof_ratio_stop(void);
void mtk_cpuidle_prof_ratio_dump(struct seq_file *m);
void mtk_cpuidle_ctrl_timer_en(bool enable);
bool mtk_cpuidle_ctrl_timer_sta_get(void);
void mtk_cpuidle_ctrl_log_en(bool enable);
bool mtk_cpuidle_ctrl_log_sta_get(void);
void mtk_cpuidle_state_enable(bool en);
int __init mtk_cpuidle_status_init(void);
void __exit mtk_cpuidle_status_exit(void);
#endif /* __MTK_CPUIDLE_STATUS_H__ */
@@ -0,0 +1,152 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cpuidle.h>
#include <linux/pm_qos.h>
#include "mtk_cpuidle_status.h"
#include "mtk_cpuidle_cpc.h"
#include "mtk_idle_procfs.h"
static void __iomem *cpupm_mcusys_base_addr;
static void __iomem *cpupm_syssram_base_addr;
static struct pm_qos_request cpuidle_dbg_qos_req;
int mtk_cpupm_mcusys_write(int ofs, unsigned int val)
{
if (!cpupm_mcusys_base_addr)
return -EADDRNOTAVAIL;
if ((ofs % 4) != 0)
return -EINVAL;
__raw_writel(val, cpupm_mcusys_base_addr + ofs);
mb(); /* make sure register access in order */
return 0;
}
unsigned int mtk_cpupm_mcusys_read(int ofs)
{
if (!cpupm_mcusys_base_addr)
return 0;
if ((ofs % 4) != 0)
return 0;
return __raw_readl(cpupm_mcusys_base_addr + ofs);
}
int mtk_cpupm_syssram_write(int ofs, unsigned int val)
{
if (!cpupm_syssram_base_addr)
return -EADDRNOTAVAIL;
if ((ofs % 4) != 0)
return -EINVAL;
__raw_writel(val, cpupm_syssram_base_addr + ofs);
mb(); /* make sure register access in order */
return 0;
}
unsigned int mtk_cpupm_syssram_read(int ofs)
{
if (!cpupm_syssram_base_addr)
return 0;
if ((ofs % 4) != 0)
return 0;
return __raw_readl(cpupm_syssram_base_addr + ofs);
}
void mtk_cpupm_block(void)
{
pm_qos_update_request(&cpuidle_dbg_qos_req, 2);
}
void mtk_cpupm_allow(void)
{
pm_qos_update_request(&cpuidle_dbg_qos_req, PM_QOS_DEFAULT_VALUE);
}
int mtk_cpupm_get_idle_state_count(int cpu)
{
struct device_node *state_node, *cpu_node;
int i, state_count, start_idx = 1;
cpu_node = of_cpu_device_node_get(cpu);
if (!cpu_node)
return start_idx;
for (i = 0; ; i++) {
state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
if (!state_node)
break;
of_node_put(state_node);
}
of_node_put(cpu_node);
state_count = i + start_idx;
if (state_count > CPUIDLE_STATE_MAX)
state_count = CPUIDLE_STATE_MAX;
return state_count;
}
static void __init mtk_cpupm_node_init(void)
{
struct device_node *node = NULL;
cpupm_mcusys_base_addr = NULL;
cpupm_syssram_base_addr = NULL;
node = of_find_compatible_node(NULL, NULL,
"mediatek,mcusys-ctrl");
if (node) {
cpupm_mcusys_base_addr = of_iomap(node, 0);
of_node_put(node);
}
node = of_find_compatible_node(NULL, NULL,
"mediatek,cpupm-sysram");
if (node) {
cpupm_syssram_base_addr = of_iomap(node, 0);
of_node_put(node);
}
}
int mtk_cpupm_dbg_init(void)
{
pm_qos_add_request(&cpuidle_dbg_qos_req,
PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
mtk_cpupm_node_init();
mtk_cpuidle_status_init();
mtk_cpc_init();
mtk_idle_procfs_init();
return 0;
}
void mtk_cpupm_dbg_exit(void)
{
mtk_idle_procfs_exit();
mtk_cpuidle_status_exit();
mtk_cpc_exit();
pm_qos_remove_request(&cpuidle_dbg_qos_req);
}
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MTK_CPUPM_DBG_H__
#define __MTK_CPUPM_DBG_H__
int mtk_cpupm_mcusys_write(int ofs, unsigned int val);
unsigned int mtk_cpupm_mcusys_read(int ofs);
int mtk_cpupm_syssram_write(int ofs, unsigned int val);
unsigned int mtk_cpupm_syssram_read(int ofs);
void mtk_cpupm_block(void);
void mtk_cpupm_allow(void);
int mtk_cpupm_get_idle_state_count(int cpu);
#endif /* __MTK_CPUPM_DBG_H__ */
@@ -0,0 +1,130 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/seq_file.h>
#include <mtk_lp_plat_reg.h>
#include "mtk_cpupm_dbg.h"
#include "mtk_cpuidle_status.h"
#include "mtk_idle_procfs.h"
#define MTK_IDLE_PROC_FS_NAME "cpuidle"
static int idle_proc_info_show(struct seq_file *m, void *v)
{
int cpu;
unsigned long long ts, rem;
ts = mtk_cpupm_syssram_read(SYSRAM_RECENT_CNT_TS_H);
ts = (ts << 32) | mtk_cpupm_syssram_read(SYSRAM_RECENT_CNT_TS_L);
ts = div64_u64_rem(ts, 1000 * 1000 * 1000, &rem);
seq_puts(m, "\n========= Power off count =========\n");
seq_puts(m, "\n-- Within 5 seconds before dumped --\n");
seq_printf(m, "\nLast dump timestamp = %llu.%09llu\n",
ts, rem);
seq_printf(m, "%8s", "cpu:");
for_each_possible_cpu(cpu)
seq_printf(m, " %d", mtk_cpupm_syssram_read(
SYSRAM_RECENT_CPU_CNT(cpu)));
seq_puts(m, "\n");
seq_printf(m, "%8s %d\n",
"cluster:",
mtk_cpupm_syssram_read(SYSRAM_RECENT_CPUSYS_CNT));
seq_printf(m, "%8s %d\n",
"mcusys:",
mtk_cpupm_syssram_read(SYSRAM_RECENT_MCUSYS_CNT));
seq_printf(m, "%8s 0x%x\n",
"online:",
mtk_cpupm_syssram_read(SYSRAM_CPU_ONLINE));
seq_puts(m, "\n---- Total ----\n");
seq_printf(m, "%8s %d\n",
"cluster:",
mtk_cpupm_syssram_read(SYSRAM_CPUSYS_CNT)
+ mtk_cpupm_syssram_read(SYSRAM_RECENT_CPUSYS_CNT));
seq_printf(m, "%8s %d\n",
"mcusys:",
mtk_cpupm_syssram_read(SYSRAM_MCUSYS_CNT)
+ mtk_cpupm_syssram_read(SYSRAM_RECENT_MCUSYS_CNT));
return 0;
}
static ssize_t idle_proc_info_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
return count;
}
static int idle_proc_enable_show(struct seq_file *m, void *v)
{
seq_printf(m, "%s()\n", __func__);
return 0;
}
static ssize_t idle_proc_enable_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
mtk_cpuidle_state_enable(!!enabled);
free:
mtk_idle_procfs_free(buf);
return ret;
}
PROC_FOPS(info);
PROC_FOPS(enable);
int __init mtk_idle_procfs_init(void)
{
int i;
struct proc_dir_entry *dir = NULL;
const struct idle_proc_entry entries[] = {
PROC_ENTRY(info),
PROC_ENTRY(enable)
};
dir = proc_mkdir(MTK_IDLE_PROC_FS_NAME, NULL);
if (!dir) {
pr_notice("fail to create procfs @ %s()\n", __func__);
return -ENOMEM;
}
for (i = 0; i < ARRAY_SIZE(entries); i++)
PROC_CREATE_NODE(dir, entries[i]);
mtk_idle_procfs_state_dir_init(dir);
mtk_idle_procfs_cpc_dir_init(dir);
mtk_idle_procfs_profile_dir_init(dir);
mtk_idle_procfs_control_dir_init(dir);
return 0;
}
void __exit mtk_idle_procfs_exit(void)
{
remove_proc_subtree(MTK_IDLE_PROC_FS_NAME, NULL);
}
@@ -0,0 +1,75 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MTK_IDLE_PROCFS__
#define __MTK_IDLE_PROCFS__
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
struct idle_proc_entry {
const char *name;
const struct file_operations *fops;
};
#define PROC_FOPS(name) \
static int idle_proc_ ## name ## _open(struct inode *inode, struct file *file) \
{ \
return single_open(file, idle_proc_ ## name ## _show, PDE_DATA(inode));\
} \
static const struct file_operations idle_proc_ ## name ## _fops = { \
.owner = THIS_MODULE, \
.open = idle_proc_ ## name ## _open, \
.read = seq_read, \
.llseek = seq_lseek, \
.release = single_release, \
.write = idle_proc_ ## name ## _write, \
}
#define PROC_ENTRY(name) {__stringify(name), &idle_proc_ ## name ## _fops}
#define PROC_CREATE_NODE(dir, entry) \
do { \
if (!proc_create(entry.name, 0644, dir, entry.fops)) \
pr_notice("%s(), create procfs cpuidle node %s failed\n", \
__func__, entry.name); \
} while (0)
#define mtk_idle_procfs_alloc_from_user(buf, userbuf, count) \
do { \
buf = (char *)__get_free_page(GFP_USER); \
\
if (!buf) \
break; \
\
if (count >= PAGE_SIZE \
|| copy_from_user(buf, userbuf, count)) { \
free_page((unsigned long)buf); \
buf = NULL; \
break; \
} \
\
buf[count] = '\0'; \
\
} while (0)
#define mtk_idle_procfs_free(buf) \
do { \
if (!buf) \
break; \
\
free_page((unsigned long)buf); \
} while (0)
void __init mtk_idle_procfs_cpc_dir_init(struct proc_dir_entry *parent);
void __init mtk_idle_procfs_state_dir_init(struct proc_dir_entry *parent);
void __init mtk_idle_procfs_profile_dir_init(struct proc_dir_entry *parent);
void __init mtk_idle_procfs_control_dir_init(struct proc_dir_entry *parent);
int __init mtk_idle_procfs_init(void);
void __exit mtk_idle_procfs_exit(void);
#endif /* __MTK_IDLE_PROCFS__ */
@@ -0,0 +1,315 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/seq_file.h>
#include <mtk_lp_plat_apmcu_mbox.h>
#include "mtk_cpuidle_status.h"
#include "mtk_idle_procfs.h"
static int idle_proc_log_show(struct seq_file *m, void *v)
{
seq_printf(m, "CPU idle log : %s\n",
mtk_cpuidle_ctrl_log_sta_get() ?
"Enable" : "Disable");
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [0|1] > /proc/cpuidle/control/log\n");
return 0;
}
static ssize_t idle_proc_log_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
mtk_cpuidle_ctrl_log_en(!!enabled);
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_timer_show(struct seq_file *m, void *v)
{
seq_printf(m, "Limit sleep timer : %s\n",
mtk_cpuidle_ctrl_timer_sta_get() ?
"Enable" : "Disable");
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [0|1] > /proc/cpuidle/control/timer\n");
return 0;
}
static ssize_t idle_proc_timer_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
mtk_cpuidle_ctrl_timer_en(!!enabled);
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_stress_show(struct seq_file *m, void *v)
{
seq_printf(m, "CPU idle stress : %s\n",
mtk_cpuidle_get_stress_status() ?
"Enable" : "Disable");
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [0|1] > /proc/cpuidle/control/stress\n");
return 0;
}
static ssize_t idle_proc_stress_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
mtk_cpuidle_set_stress_test(!!enabled);
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_stress_time_show(struct seq_file *m, void *v)
{
seq_printf(m, "CPU idle stress interval time : %u\n",
mtk_cpuidle_get_stress_time());
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [us] > /proc/cpuidle/control/stress_time\n");
return 0;
}
static ssize_t idle_proc_stress_time_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int interval_us;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &interval_us) != 0) {
ret = -EINVAL;
goto free;
}
mtk_cpuidle_set_stress_time(interval_us);
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_buck_mode_show(struct seq_file *m, void *v)
{
int mode;
struct mode_param {
int id;
char *str;
};
const struct mode_param table[] = {
{MCUPM_BUCK_NORMAL_MODE, "on"},
{MCUPM_BUCK_LP_MODE, "low power"},
{MCUPM_BUCK_OFF_MODE, "off"},
{NF_MCUPM_BUCK_MODE, "unknown"},
};
mode = mtk_get_mcupm_buck_mode();
if (mode < 0 || mode > NF_MCUPM_BUCK_MODE)
mode = NF_MCUPM_BUCK_MODE;
seq_printf(m, "Vproc/Vproc_sram buck mode : %s\n",
table[mode].str);
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [mode] > /proc/cpuidle/control/buck_mode\n");
seq_puts(m, "mode:\n");
for (mode = 0; mode < NF_MCUPM_BUCK_MODE; mode++)
seq_printf(m, "\t%d: %s\n",
table[mode].id,
table[mode].str);
return 0;
}
static ssize_t idle_proc_buck_mode_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int mode;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &mode) != 0) {
ret = -EINVAL;
goto free;
}
if (mode < NF_MCUPM_BUCK_MODE)
mtk_set_mcupm_buck_mode(mode);
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_armpll_mode_show(struct seq_file *m, void *v)
{
int mode;
struct mode_param {
int id;
char *str;
};
const struct mode_param table[] = {
{MCUPM_ARMPLL_ON, "on"},
{MCUPM_ARMPLL_GATING, "gating"},
{MCUPM_ARMPLL_OFF, "off"},
{NF_MCUPM_ARMPLL_MODE, "unknown"},
};
mode = mtk_get_mcupm_pll_mode();
if (mode < 0 || mode > NF_MCUPM_ARMPLL_MODE)
mode = NF_MCUPM_ARMPLL_MODE;
seq_printf(m, "armpll mode : %s\n",
table[mode].str);
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [mode] > /proc/cpuidle/control/armpll_mode\n");
seq_puts(m, "mode:\n");
for (mode = 0; mode < NF_MCUPM_ARMPLL_MODE; mode++)
seq_printf(m, "\t%d: %s\n",
table[mode].id,
table[mode].str);
return 0;
}
static ssize_t idle_proc_armpll_mode_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int mode;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &mode) != 0) {
ret = -EINVAL;
goto free;
}
if (mode < NF_MCUPM_ARMPLL_MODE)
mtk_set_mcupm_pll_mode(mode);
free:
mtk_idle_procfs_free(buf);
return ret;
}
PROC_FOPS(log);
PROC_FOPS(timer);
PROC_FOPS(stress);
PROC_FOPS(stress_time);
PROC_FOPS(buck_mode);
PROC_FOPS(armpll_mode);
void __init mtk_idle_procfs_control_dir_init(struct proc_dir_entry *parent)
{
int i;
struct proc_dir_entry *dir = NULL;
const struct idle_proc_entry entries[] = {
PROC_ENTRY(log),
PROC_ENTRY(timer),
PROC_ENTRY(stress),
PROC_ENTRY(stress_time),
PROC_ENTRY(buck_mode),
PROC_ENTRY(armpll_mode)
};
dir = proc_mkdir("control", parent);
if (!dir) {
pr_notice("fail to create procfs @ %s()\n", __func__);
return;
}
for (i = 0; i < ARRAY_SIZE(entries); i++)
PROC_CREATE_NODE(dir, entries[i]);
}
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/seq_file.h>
#include "mtk_cpuidle_cpc.h"
#include "mtk_idle_procfs.h"
static int idle_proc_auto_off_show(struct seq_file *m, void *v)
{
seq_printf(m, "auto_off mode : %s\n",
cpc_get_auto_off_sta() ? "Enabled" : "Disabled");
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [0|1] > /proc/cpuidle/cpc/auto_off\n");
return 0;
}
static ssize_t idle_proc_auto_off_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
if (enabled)
cpc_auto_off_en();
else
cpc_auto_off_dis();
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_auto_off_thres_show(struct seq_file *m, void *v)
{
seq_printf(m, "auto_off threshold = %u us\n",
(unsigned int)cpc_get_auto_off_thres());
seq_puts(m, "\n======== Command Usage ========\n");
seq_puts(m, "echo [us] > /proc/cpuidle/cpc/auto_off_thres\n");
return 0;
}
static ssize_t idle_proc_auto_off_thres_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int threshold_us;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &threshold_us) != 0) {
ret = -EINVAL;
goto free;
}
cpc_set_auto_off_thres(threshold_us);
free:
mtk_idle_procfs_free(buf);
return ret;
}
PROC_FOPS(auto_off);
PROC_FOPS(auto_off_thres);
void __init mtk_idle_procfs_cpc_dir_init(struct proc_dir_entry *parent)
{
int i;
struct proc_dir_entry *dir = NULL;
const struct idle_proc_entry entries[] = {
PROC_ENTRY(auto_off),
PROC_ENTRY(auto_off_thres)
};
dir = proc_mkdir("cpc", parent);
if (!dir) {
pr_notice("fail to create procfs @ %s()\n", __func__);
return;
}
for (i = 0; i < ARRAY_SIZE(entries); i++)
PROC_CREATE_NODE(dir, entries[i]);
}
@@ -0,0 +1,172 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/seq_file.h>
#include "mtk_cpuidle_cpc.h"
#include "mtk_cpuidle_status.h"
#include "mtk_idle_procfs.h"
static int idle_proc_kernel_show(struct seq_file *m, void *v)
{
seq_printf(m, "%s\n", __func__);
return 0;
}
static ssize_t idle_proc_kernel_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
/* Start/Stop kernel profile */
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_target_state_show(struct seq_file *m, void *v)
{
seq_printf(m, "%s\n", __func__);
return 0;
}
static ssize_t idle_proc_target_state_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
int state;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtoint(buf, 10, &state) != 0) {
ret = -EINVAL;
goto free;
}
/* Set target state */
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_ratio_show(struct seq_file *m, void *v)
{
mtk_cpuidle_prof_ratio_dump(m);
return 0;
}
static ssize_t idle_proc_ratio_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
if (enabled)
mtk_cpuidle_prof_ratio_start();
else
mtk_cpuidle_prof_ratio_stop();
free:
mtk_idle_procfs_free(buf);
return ret;
}
static int idle_proc_cpc_show(struct seq_file *m, void *v)
{
mtk_cpc_prof_lat_dump(m);
return 0;
}
static ssize_t idle_proc_cpc_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
ssize_t ret = count;
unsigned int enabled;
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
if (kstrtouint(buf, 10, &enabled) != 0) {
ret = -EINVAL;
goto free;
}
if (enabled)
mtk_cpc_prof_start();
else
mtk_cpc_prof_stop();
free:
mtk_idle_procfs_free(buf);
return ret;
}
PROC_FOPS(kernel);
PROC_FOPS(target_state);
PROC_FOPS(ratio);
PROC_FOPS(cpc);
void __init mtk_idle_procfs_profile_dir_init(struct proc_dir_entry *parent)
{
int i;
struct proc_dir_entry *dir = NULL;
const struct idle_proc_entry entries[] = {
PROC_ENTRY(kernel),
PROC_ENTRY(target_state),
PROC_ENTRY(ratio),
PROC_ENTRY(cpc)
};
dir = proc_mkdir("profile", parent);
if (!dir) {
pr_notice("fail to create procfs @ %s()\n", __func__);
return;
}
for (i = 0; i < ARRAY_SIZE(entries); i++)
PROC_CREATE_NODE(dir, entries[i]);
}
@@ -0,0 +1,218 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/cpuidle.h>
#include <linux/seq_file.h>
#include "mtk_cpuidle_status.h"
#include "mtk_idle_procfs.h"
#define ALL_LITTLR_CPU_ID (10)
#define ALL_BIG_CPU_ID (20)
#define ALL_CPU_ID (100)
#define CPU_L_MASK (0x3f)
#define CPU_B_MASK (0xc0)
#define ALL_CPU_MASK (0xff)
static void idle_proc_state_uasge_print(struct seq_file *m, char *node_name)
{
seq_puts(m, "\n======== Command Usage ========\n");
seq_printf(m, "%s > /proc/cpuidle/state/%s\n",
"echo [cpu_id] [state_index] [val(dec)]",
node_name);
seq_puts(m, "\t cpu_id: 0~7 -> cpu number\n");
seq_printf(m, "\t %3d -> all little CPU\n", ALL_LITTLR_CPU_ID);
seq_printf(m, "\t %3d -> all big CPU\n", ALL_BIG_CPU_ID);
seq_printf(m, "\t %3d -> all CPU\n", ALL_CPU_ID);
seq_puts(m, "\t state_index: >0 (index 0 can't be modified)\n");
seq_puts(m, "\n");
}
static void idle_proc_state_param_show(struct seq_file *m, int param)
{
struct cpuidle_driver *drv = NULL;
int i, cpu, desc = 0;
for_each_possible_cpu(cpu) {
//drv = cpuidle_get_cpu_driver(per_cpu(cpuidle_devices, cpu));
if (!drv)
continue;
if (desc == 0) {
seq_printf(m, "%-12s:", "state_index");
for (i = 0; i < drv->state_count; i++)
seq_printf(m, "%7d ", i);
seq_puts(m, "\n");
desc = 1;
}
seq_printf(m, "%11s%d:", "cpu", cpu);
for (i = 0; i < drv->state_count; i++)
seq_printf(m, "%7d ",
mtk_cpuidle_get_param(drv, i, param));
seq_puts(m, "\n");
}
}
static int idle_proc_state_param_setting(char *cmd, int param)
{
struct cpuidle_driver *drv = NULL;
char *args;
unsigned int cpu_mask, state_idx, val;
int cpu, i;
struct cmd_param {
unsigned int id;
unsigned int mask;
};
const struct cmd_param table[] = {
{ALL_LITTLR_CPU_ID, CPU_L_MASK},
{ALL_BIG_CPU_ID, CPU_B_MASK},
{ALL_CPU_ID, ALL_CPU_MASK},
};
args = strsep(&cmd, " ");
if (!args || kstrtoint(args, 10, &cpu) != 0)
return -EINVAL;
args = strsep(&cmd, " ");
if (!args || kstrtouint(args, 10, &state_idx) != 0)
return -EINVAL;
args = strsep(&cmd, " ");
if (!args || kstrtouint(args, 10, &val) != 0)
return -EINVAL;
if (!state_idx)
return -EINVAL;
cpu_mask = 0;
if (cpu >= 0 && cpu < nr_cpu_ids) {
cpu_mask = (1 << cpu);
} else {
for (i = 0; i < ARRAY_SIZE(table); i++) {
if (cpu == table[i].id) {
cpu_mask = table[i].mask;
break;
}
}
}
for_each_possible_cpu(cpu) {
if ((cpu_mask & (1 << cpu)) == 0)
continue;
//drv = cpuidle_get_cpu_driver(per_cpu(cpuidle_devices, cpu));
if (!drv)
continue;
mtk_cpuidle_set_param(drv, state_idx, param, val);
}
return 0;
}
static int idle_proc_latency_show(struct seq_file *m, void *v)
{
seq_puts(m, "==== CPU idle state: Exit latency ====\n");
idle_proc_state_param_show(m, IDLE_PARAM_LAT);
idle_proc_state_uasge_print(m, "latency");
return 0;
}
static ssize_t idle_proc_latency_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
idle_proc_state_param_setting(buf, IDLE_PARAM_LAT);
mtk_idle_procfs_free(buf);
return count;
}
static int idle_proc_residency_show(struct seq_file *m, void *v)
{
seq_puts(m, "==== CPU idle state: Target residency ====\n");
idle_proc_state_param_show(m, IDLE_PARAM_RES);
idle_proc_state_uasge_print(m, "residency");
return 0;
}
static ssize_t idle_proc_residency_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
idle_proc_state_param_setting(buf, IDLE_PARAM_RES);
mtk_idle_procfs_free(buf);
return count;
}
static int idle_proc_enabled_show(struct seq_file *m, void *v)
{
seq_puts(m, "==== CPU idle state: Enabled ====\n");
idle_proc_state_param_show(m, IDLE_PARAM_EN);
idle_proc_state_uasge_print(m, "enabled");
return 0;
}
static ssize_t idle_proc_enabled_write(struct file *filp,
const char __user *userbuf, size_t count, loff_t *f_pos)
{
char *buf;
mtk_idle_procfs_alloc_from_user(buf, userbuf, count);
if (!buf)
return -EINVAL;
idle_proc_state_param_setting(buf, IDLE_PARAM_EN);
mtk_idle_procfs_free(buf);
return count;
}
PROC_FOPS(latency);
PROC_FOPS(residency);
PROC_FOPS(enabled);
void __init mtk_idle_procfs_state_dir_init(struct proc_dir_entry *parent)
{
int i;
struct proc_dir_entry *dir = NULL;
const struct idle_proc_entry entries[] = {
PROC_ENTRY(latency),
PROC_ENTRY(residency),
PROC_ENTRY(enabled)
};
dir = proc_mkdir("state", parent);
if (!dir) {
pr_notice("fail to create procfs @ %s()\n", __func__);
return;
}
for (i = 0; i < ARRAY_SIZE(entries); i++)
PROC_CREATE_NODE(dir, entries[i]);
}