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,20 @@
ccflags-y += -I$(srctree)/drivers/misc/mediatek/sspm/v1
ifeq ($(CONFIG_MTK_TINYSYS_MCUPM_SUPPORT), y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/mcupm/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/mcupm/$(MTK_PLATFORM)/
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/inc/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/mtk_lpm_trace_event/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/modules/include/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/modules/include/mt6833/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/lpm/modules/debug/mt6833/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/power_gs_v1
obj-y += mt6833.o
obj-y += mtk_lp_plat_apmcu.o
obj-y += mtk_lp_plat_apmcu_mbox.o
obj-y += suspend/mt6833_suspend.o
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <mtk_lp_plat_apmcu.h>
#include "mt6833.h"
#include <suspend/mt6833_suspend.h>
static unsigned int mt6833_lp_pwr_state;
/*
* Please make sure the race condition protection upfront
* when calling mtk_lp_plat_do_mcusys_prepare_pdn()
*/
int mt6833_do_mcusys_prepare_pdn(unsigned int status,
unsigned int *resource_req)
{
mt6833_lp_pwr_state |= (status | PLAT_GIC_MASKED
| PLAT_MCUSYSOFF_PREPARED);
return 0;
}
/*
* Please make sure the race condition protection upfront
* when calling mtk_lp_plat_do_mcusys_prepare_on()
*/
static int __mt6833_do_mcusys_prepare_on(unsigned int clr_status)
{
mt6833_lp_pwr_state &= ~(clr_status | PLAT_GIC_MASKED
| PLAT_MCUSYSOFF_PREPARED);
return 0;
}
int mt6833_do_mcusys_prepare_on(void)
{
unsigned int status = mt6833_lp_pwr_state;
return __mt6833_do_mcusys_prepare_on(status);
}
int mt6833_do_mcusys_prepare_on_ex(unsigned int clr_status)
{
return __mt6833_do_mcusys_prepare_on(clr_status);
}
static int __init mt6833_init(void)
{
mtk_lp_plat_apmcu_init();
mt6833_model_suspend_init();
return 0;
}
late_initcall_sync(mt6833_init);
static int __init mt6833_early_init(void)
{
mtk_lp_plat_apmcu_early_init();
return 0;
}
subsys_initcall(mt6833_early_init);
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MT6833_H__
#define __MT6833_H__
#include <linux/delay.h>
#include <mtk_lpm_type.h>
#include <mt6833_common.h>
int mt6833_do_mcusys_prepare_pdn(unsigned int status,
unsigned int *resource_req);
int mt6833_do_mcusys_prepare_on_ex(unsigned int clr_status);
int mt6833_do_mcusys_prepare_on(void);
#endif
@@ -0,0 +1,30 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MT6833_COMMON_H__
#define __MT6833_COMMON_H__
#include <linux/delay.h>
#include <mtk_lpm_type.h>
/* Platform pwr state */
#define PLAT_XO_UFS_OFF (1<<1L)
#define PLAT_CLKBUF_ENTER_BBLPM (1<<2L)
#define PLAT_SUSPEND (1<<8L)
#define PLAT_MCUSYS_PROTECTED (1<<9L)
#define PLAT_CLUSTER_PROTECTED (1<<10L)
#define PLAT_VCORE_LP_MODE (1<<11L)
#define PLAT_PMIC_VCORE_SRCLKEN0 (1<<12L)
#define PLAT_PMIC_VCORE_SRCLKEN2 (1<<13L)
#define PLAT_MAINPLL_OFF (1<<27L)
#define PLAT_AP_WDT_RST_MODE (1<<28L)
#define PLAT_MCUSYSOFF_PREPARED (1<<29L)
#define PLAT_GIC_MASKED (1<<30L)
#define PLAT_PROTECTED_FAIL (1<<31L)
#endif
@@ -0,0 +1,372 @@
// 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/cpu.h>
#include <linux/pm_qos.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <mtk_lpm.h>
#include <mtk_lp_plat_reg.h>
#include <mtk_lp_plat_apmcu.h>
#include <mtk_lp_plat_apmcu_mbox.h>
void __iomem *cpu_pm_syssram_base;
#define plat_node_ready() (cpu_pm_syssram_base)
#define BOOT_TIME_LIMIT 60
static struct task_struct *mtk_lp_plat_task;
/* qos */
static struct pm_qos_request mtk_lp_plat_qos_req;
#define mtk_lp_plat_qos_init()\
pm_qos_add_request(&mtk_lp_plat_qos_req,\
PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE)
#define mtk_cpu_off_block()\
pm_qos_update_request(&mtk_lp_plat_qos_req, 2)
#define mtk_cpu_off_allow()\
pm_qos_update_request(&mtk_lp_plat_qos_req, PM_QOS_DEFAULT_VALUE)
#define mtk_lp_plat_qos_uninit()\
pm_qos_remove_request(&mtk_lp_plat_qos_req)
#define CHK(cond) WARN_ON(cond)
#define PWR_LVL_BIT(v) BIT(4 * v)
#define PWR_LVL_MASK(v) (0xF << (4 * (v)))
#define PWR_LVL0 (PWR_LVL_BIT(0))
#define PWR_LVL1 (PWR_LVL_BIT(1) | PWR_LVL0)
#define PWR_LVL2 (PWR_LVL_BIT(2) | PWR_LVL1)
#define PWR_LVL3 (PWR_LVL_BIT(3) | PWR_LVL2)
#define CORE_LVL PWR_LVL0
#define CLUSTER_LVL PWR_LVL1
#define MCUSYS_LVL PWR_LVL2
#define BUS26M_LVL PWR_LVL3
#define MAX_PWR_LVL MCUSYS_LVL
struct mtk_lp_device {
unsigned int pwr_lvl_mask;
union {
struct {
u32 core:4;
u32 cluster:4;
u32 mcusys:4;
u32 bus26m:4;
};
u32 lvl;
} pwr_on;
struct mtk_lp_device *parent;
};
static struct mtk_lp_device lp_dev_cpu[NR_CPUS];
static struct mtk_lp_device lp_dev_cluster[nr_cluster_ids];
static struct mtk_lp_device lp_dev_mcusys;
static void _mtk_lp_plat_inc_pwr_cnt(struct mtk_lp_device *dev,
unsigned int lvl)
{
unsigned int lvl_cnt;
lvl_cnt = lvl & dev->pwr_lvl_mask;
if (!lvl_cnt)
return;
/**
* Exclusive read-modify-write is ensured by
* mtk_lp_mod_locker in idle flow
* mtk_lp_plat_qos_req in hot-plug flow
*/
dev->pwr_on.lvl += lvl_cnt;
CHK(dev->pwr_on.core >= nr_cpu_ids);
if (dev->parent)
_mtk_lp_plat_inc_pwr_cnt(dev->parent, lvl);
}
static void _mtk_lp_plat_dec_pwr_cnt(struct mtk_lp_device *dev,
unsigned int lvl)
{
unsigned int lvl_cnt;
lvl_cnt = lvl & dev->pwr_lvl_mask;
if (!lvl_cnt)
return;
/**
* Exclusive read-modify-write is ensured by
* mtk_lp_mod_locker in idle flow
* mtk_lp_plat_qos_req in hot-plug flow
*/
dev->pwr_on.lvl -= lvl_cnt;
CHK((dev->pwr_on.lvl & BIT(31)));
if (dev->parent)
_mtk_lp_plat_dec_pwr_cnt(dev->parent, lvl);
}
#define mtk_lp_plat_set_off_lvl(cpu, lvl) \
do { \
if (likely(cpu < nr_cpu_ids)) \
_mtk_lp_plat_dec_pwr_cnt(&lp_dev_cpu[cpu], lvl); \
} while (0)
#define mtk_lp_plat_clr_off_lvl(cpu, lvl) \
do { \
if (likely(cpu < nr_cpu_ids)) \
_mtk_lp_plat_inc_pwr_cnt(&lp_dev_cpu[cpu], lvl); \
} while (0)
void mtk_lp_plat_set_mcusys_off(int cpu)
{
mtk_lp_plat_set_off_lvl(cpu, MCUSYS_LVL);
}
void mtk_lp_plat_clr_mcusys_off(int cpu)
{
mtk_lp_plat_clr_off_lvl(cpu, MCUSYS_LVL);
}
void mtk_lp_plat_set_cluster_off(int cpu)
{
mtk_lp_plat_set_off_lvl(cpu, CLUSTER_LVL);
}
void mtk_lp_plat_clr_cluster_off(int cpu)
{
mtk_lp_plat_clr_off_lvl(cpu, CLUSTER_LVL);
}
static inline void mtk_lp_dev_get_cpus_online(void)
{
int i;
for_each_online_cpu(i)
mtk_lp_plat_clr_off_lvl(i, MAX_PWR_LVL);
}
static void mtk_lp_dev_set_cpus_off(void)
{
int i;
lp_dev_mcusys.pwr_on.lvl = 0;
for (i = 0; i < nr_cluster_ids; i++)
lp_dev_cluster[i].pwr_on.lvl = 0;
for (i = 0; i < nr_cpu_ids; i++)
lp_dev_cpu[i].pwr_on.lvl = 0;
}
bool mtk_lp_plat_is_mcusys_off(void)
{
return !lp_dev_mcusys.pwr_on.mcusys;
}
bool mtk_lp_plat_is_cluster_off(int cpu)
{
if (cpu < 0)
return false;
if (unlikely(cpu >= nr_cpu_ids || !lp_dev_cpu[cpu].parent))
return false;
return !lp_dev_cpu[cpu].parent->pwr_on.cluster;
}
static int mtk_lp_cpuhp_notify_enter(unsigned int cpu)
{
mtk_cpu_off_block();
return 0;
}
static int mtk_lp_cpuhp_notify_leave(unsigned int cpu)
{
mtk_lp_dev_set_cpus_off();
mtk_lp_dev_get_cpus_online();
mtk_cpu_off_allow();
return 0;
}
static void mtk_lp_plat_cpuhp_init(void)
{
cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN_END, "cpuidle_cb",
mtk_lp_cpuhp_notify_enter,
mtk_lp_cpuhp_notify_leave);
cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "cpuidle_cb",
mtk_lp_cpuhp_notify_leave,
mtk_lp_cpuhp_notify_enter);
}
#define DEPD_COND_TYPE_BOOTIME (1<<0u)
#define DEPD_COND_TYPE_MCU (1<<1u)
static int __mtk_lp_plat_wait_depd_condition(int type, void *arg)
{
struct timespec uptime;
bool mcupm_rdy = false;
bool boot_time_pass = false;
if (type & DEPD_COND_TYPE_MCU) {
mtk_wait_mbox_init_done();
mtk_notify_subsys_ap_ready();
} else
mcupm_rdy = true;
do {
msleep(1000);
if (!mcupm_rdy && mtk_mcupm_is_ready())
mcupm_rdy = true;
if ((type & DEPD_COND_TYPE_BOOTIME) &&
!boot_time_pass) {
get_monotonic_boottime(&uptime);
if ((unsigned int)uptime.tv_sec > BOOT_TIME_LIMIT)
boot_time_pass = true;
}
} while (!(mcupm_rdy && boot_time_pass));
mtk_cpu_off_allow();
mtk_lp_plat_cpuhp_init();
return 0;
}
static int mtk_lp_plat_wait_depd_condition_nonmcu(void *arg)
{
return __mtk_lp_plat_wait_depd_condition(DEPD_COND_TYPE_BOOTIME, arg);
}
static int mtk_lp_plat_wait_depd_condition(void *arg)
{
return __mtk_lp_plat_wait_depd_condition((DEPD_COND_TYPE_BOOTIME
| DEPD_COND_TYPE_MCU), arg);
}
static void __init mtk_lp_plat_pwr_dev_init(void)
{
struct mtk_lp_device *dev;
int i, cluster_id;
for (i = 0; i < nr_cpu_ids; i++) {
dev = &lp_dev_cpu[i];
dev->pwr_lvl_mask = PWR_LVL_MASK(0);
cluster_id = get_physical_cluster_id(i);
if (cluster_id < nr_cluster_ids)
dev->parent = &lp_dev_cluster[cluster_id];
}
for (i = 0; i < nr_cluster_ids; i++) {
dev = &lp_dev_cluster[i];
dev->pwr_lvl_mask = PWR_LVL_MASK(1);
dev->parent = &lp_dev_mcusys;
}
lp_dev_mcusys.pwr_lvl_mask = PWR_LVL_MASK(2);
mtk_lp_dev_get_cpus_online();
}
static int __init mtk_lp_plat_mcusys_ctrl_init(void)
{
struct device_node *node = NULL;
cpu_pm_syssram_base = NULL;
node = of_find_compatible_node(NULL, NULL,
"mediatek,cpupm-sysram");
if (node) {
cpu_pm_syssram_base = of_iomap(node, 0);
of_node_put(node);
}
return plat_node_ready() ? 0 : -1;
}
int __init mtk_lp_plat_apmcu_init(void)
{
struct device_node *node = NULL;
unsigned int is_mcu_mode = 0;
if (!plat_node_ready()) {
mtk_lp_plat_qos_uninit();
return 0;
}
mtk_lp_plat_pwr_dev_init();
node = of_find_compatible_node(NULL, NULL, MTK_LPM_DTS_COMPATIBLE);
if (node) {
const char *method = NULL;
of_property_read_string(node, "cpupm-method", &method);
if (method && !strcmp(method, "mcu"))
is_mcu_mode = 1;
of_node_put(node);
}
if (is_mcu_mode)
mtk_lp_plat_task =
kthread_create(mtk_lp_plat_wait_depd_condition,
NULL, "mtk_lp_plat_wait_rdy");
else
mtk_lp_plat_task =
kthread_create(mtk_lp_plat_wait_depd_condition_nonmcu,
NULL, "mtk_lp_plat_wait_rdy");
if (!IS_ERR(mtk_lp_plat_task))
wake_up_process(mtk_lp_plat_task);
else
pr_notice("Create thread fail @ %s()\n", __func__);
return 0;
}
int __init mtk_lp_plat_apmcu_early_init(void)
{
if (mtk_lp_plat_mcusys_ctrl_init() != 0) {
pr_notice("%s(): Not support\n", __func__);
return 0;
}
mtk_lp_plat_qos_init();
if (cpu_pm_syssram_base) {
memset_io((void __iomem *)
(cpu_pm_syssram_base + LP_PM_SYSRAM_INFO_OFS),
0,
LP_PM_SYSRAM_SIZE - LP_PM_SYSRAM_INFO_OFS);
}
mtk_cpu_off_block();
return 0;
}
@@ -0,0 +1,192 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/workqueue.h>
#include <linux/delay.h>
#ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
#include <sspm_helper.h>
#include <sspm_mbox.h>
#endif
#include <mtk_lp_plat_apmcu_mbox.h>
#ifdef CONFIG_MTK_TINYSYS_MCUPM_SUPPORT
#include "mcupm_driver.h"
#endif
struct mbox_ops {
void (*write)(int id, int *buf, unsigned int len);
void (*read)(int id, int *buf, unsigned int len);
};
static void apmcu_sspm_mailbox_write(int id, int *buf, unsigned int len)
{
#ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
// if (is_sspm_ready())
// sspm_mbox_write(APMCU_SSPM_MBOX_ID, id, (void *)buf, len);
#endif
}
static void apmcu_sspm_mailbox_read(int id, int *buf, unsigned int len)
{
#ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
// if (is_sspm_ready())
// sspm_mbox_read(APMCU_SSPM_MBOX_ID, id, (void *)&buf, len);
#endif
}
static void apmcu_mcupm_mailbox_write(int id, int *buf, unsigned int len)
{
#ifdef CONFIG_MTK_TINYSYS_MCUPM_SUPPORT
mcupm_mbox_write(APMCU_MCUPM_MBOX_ID, id, (void *)buf, len);
#endif
}
static void apmcu_mcupm_mailbox_read(int id, int *buf, unsigned int len)
{
#ifdef CONFIG_MTK_TINYSYS_MCUPM_SUPPORT
mcupm_mbox_read(APMCU_MCUPM_MBOX_ID, id, (void *)buf, len);
#endif
}
static struct mbox_ops mbox[NF_MBOX] = {
[MBOX_SSPM] = {
.write = apmcu_sspm_mailbox_write,
.read = apmcu_sspm_mailbox_read
},
[MBOX_MCUPM] = {
.write = apmcu_mcupm_mailbox_write,
.read = apmcu_mcupm_mailbox_read
},
};
void mtk_set_sspm_lp_cmd(void *buf)
{
mbox[MBOX_SSPM].write(APMCU_SSPM_MBOX_SPM_CMD,
(int *)buf,
APMCU_SSPM_MBOX_SPM_CMD_SIZE);
}
void mtk_clr_sspm_lp_cmd(void)
{
int buf[APMCU_SSPM_MBOX_SPM_CMD_SIZE] = {0};
mbox[MBOX_SSPM].write(APMCU_SSPM_MBOX_SPM_CMD,
(int *)buf,
APMCU_SSPM_MBOX_SPM_CMD_SIZE);
}
static void mtk_mcupm_pwr_ctrl_setting(int dev)
{
mbox[MBOX_MCUPM].write(APMCU_MCUPM_MBOX_PWR_CTRL_EN, &dev, 1);
}
bool mtk_mcupm_cm_is_notified(void)
{
unsigned int en_mask = 0;
mbox[MBOX_MCUPM].read(APMCU_MCUPM_MBOX_PWR_CTRL_EN, &en_mask, 1);
return !!(en_mask & MCUPM_CM_CTRL);
}
EXPORT_SYMBOL(mtk_mcupm_cm_is_notified);
void mtk_set_mcupm_pll_mode(unsigned int mode)
{
if (mode < NF_MCUPM_ARMPLL_MODE)
mbox[MBOX_MCUPM].write(APMCU_MCUPM_MBOX_ARMPLL_MODE, &mode, 1);
}
EXPORT_SYMBOL(mtk_set_mcupm_pll_mode);
int mtk_get_mcupm_pll_mode(void)
{
int mode = 0;
mbox[MBOX_MCUPM].read(APMCU_MCUPM_MBOX_ARMPLL_MODE, &mode, 1);
return mode;
}
EXPORT_SYMBOL(mtk_get_mcupm_pll_mode);
void mtk_set_mcupm_buck_mode(unsigned int mode)
{
if (mode < NF_MCUPM_BUCK_MODE)
mbox[MBOX_MCUPM].write(APMCU_MCUPM_MBOX_BUCK_MODE, &mode, 1);
}
EXPORT_SYMBOL(mtk_set_mcupm_buck_mode);
int mtk_get_mcupm_buck_mode(void)
{
int mode = 0;
mbox[MBOX_MCUPM].read(APMCU_MCUPM_MBOX_BUCK_MODE, &mode, 1);
return mode;
}
EXPORT_SYMBOL(mtk_get_mcupm_buck_mode);
void mtk_set_preferred_cpu_wakeup(int cpu)
{
if (cpu_online(cpu))
mbox[MBOX_MCUPM].write(APMCU_MCUPM_MBOX_WAKEUP_CPU, &cpu, 1);
}
int mtk_get_preferred_cpu_wakeup(void)
{
int cpu = -1;
mbox[MBOX_MCUPM].read(APMCU_MCUPM_MBOX_WAKEUP_CPU, &cpu, 1);
return cpu;
}
bool mtk_mcupm_is_ready(void)
{
int sta = MCUPM_TASK_INIT_FINISH;
mbox[MBOX_MCUPM].read(APMCU_MCUPM_MBOX_TASK_STA, &sta, 1);
return sta == MCUPM_TASK_WAIT || sta == MCUPM_TASK_INIT_FINISH;
}
void mtk_wait_mbox_init_done(void)
{
int sta = MCUPM_TASK_INIT;
while (1) {
mbox[MBOX_MCUPM].read(APMCU_MCUPM_MBOX_TASK_STA, &sta, 1);
if (sta == MCUPM_TASK_INIT)
break;
msleep(1000);
}
mtk_set_mcupm_pll_mode(MCUPM_ARMPLL_OFF);
mtk_set_mcupm_buck_mode(MCUPM_BUCK_OFF_MODE);
mtk_mcupm_pwr_ctrl_setting(
MCUPM_MCUSYS_CTRL |
#ifdef CONFIG_MTK_CM_MGR_LEGACY
MCUPM_CM_CTRL |
#endif
MCUPM_BUCK_CTRL |
MCUPM_ARMPLL_CTRL);
}
void mtk_notify_subsys_ap_ready(void)
{
#ifdef CONFIG_MFD_MT6360_PMU
int ready = 0x6360;
#else
int ready = 1;
#endif
mbox[MBOX_SSPM].write(APMCU_SSPM_MBOX_AP_READY, &ready, 1);
mbox[MBOX_MCUPM].write(APMCU_MCUPM_MBOX_AP_READY, &ready, 1);
}
@@ -0,0 +1,476 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/atomic.h>
#include <linux/cpuidle.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/slab.h>
#include <linux/cpu_pm.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>
#include <linux/syscore_ops.h>
#include <linux/suspend.h>
#include <linux/rtc.h>
#include <linux/hrtimer.h>
#include <linux/timer.h>
#include <linux/completion.h>
#include <linux/jiffies.h>
#include <asm/cpuidle.h>
#include <asm/suspend.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <mt6833_spm_comm.h>
#include <mtk_lpm.h>
#include <mtk_lpm_module.h>
#include <mtk_lpm_call.h>
#include <mtk_lpm_type.h>
#include <mtk_lpm_call_type.h>
#include <mtk_dbg_common_v1.h>
#include <mt-plat/mtk_ccci_common.h>
#include <uapi/linux/sched/types.h>
#include <mtk_cpuidle_status.h>
#include "mt6833.h"
#include "mt6833_suspend.h"
unsigned int mt6833_suspend_status;
struct md_sleep_status before_md_sleep_status;
struct md_sleep_status after_md_sleep_status;
struct cpumask s2idle_cpumask;
static struct cpumask abort_cpumask;
static DEFINE_SPINLOCK(lpm_abort_locker);
struct mtk_lpm_model mt6833_model_suspend;
void __attribute__((weak)) subsys_if_on(void)
{
printk_deferred("[name:spm&]NO %s !!!\n", __func__);
}
void __attribute__((weak)) pll_if_on(void)
{
printk_deferred("[name:spm&]NO %s !!!\n", __func__);
}
void __attribute__((weak)) gpio_dump_regs(void)
{
printk_deferred("[name:spm&]NO %s !!!\n", __func__);
}
void mtk_suspend_gpio_dbg(void)
{
#if !defined(CONFIG_FPGA_EARLY_PORTING)
gpio_dump_regs();
#endif
}
EXPORT_SYMBOL(mtk_suspend_gpio_dbg);
void mtk_suspend_clk_dbg(void){}
EXPORT_SYMBOL(mtk_suspend_clk_dbg);
#define MD_SLEEP_INFO_SMEM_OFFEST (4)
static void get_md_sleep_time(struct md_sleep_status *md_data)
{
/* dump subsystem sleep info */
#if defined(CONFIG_MTK_ECCCI_DRIVER)
u32 *share_mem = NULL;
if (!md_data)
return;
share_mem = (u32 *)get_smem_start_addr(MD_SYS1,
SMEM_USER_LOW_POWER, NULL);
if (share_mem == NULL) {
printk_deferred("[name:spm&][%s:%d] - No MD share mem\n",
__func__, __LINE__);
return;
}
share_mem = share_mem + MD_SLEEP_INFO_SMEM_OFFEST;
memset(md_data, 0, sizeof(struct md_sleep_status));
memcpy(md_data, share_mem, sizeof(struct md_sleep_status));
#else
return;
#endif
}
static void log_md_sleep_info(void)
{
#if BITS_PER_LONG == 64
#define LOG_BUF_SIZE 256
char log_buf[LOG_BUF_SIZE] = { 0 };
int log_size = 0;
if (after_md_sleep_status.sleep_time >= before_md_sleep_status.sleep_time) {
printk_deferred("[name:spm&][SPM] md_slp_duration = %llu (32k)\n",
after_md_sleep_status.sleep_time - before_md_sleep_status.sleep_time);
log_size += scnprintf(log_buf + log_size,
LOG_BUF_SIZE - log_size, "[name:spm&][SPM] ");
log_size += scnprintf(log_buf + log_size,
LOG_BUF_SIZE - log_size, "MD/2G/3G/4G/5G_FR1 = ");
log_size += scnprintf(log_buf + log_size,
LOG_BUF_SIZE - log_size, "%d.%03d/%d.%03d/%d.%03d/%d.%03d/%d.%03d seconds",
(after_md_sleep_status.md_sleep_time -
before_md_sleep_status.md_sleep_time) / 1000000,
(after_md_sleep_status.md_sleep_time -
before_md_sleep_status.md_sleep_time) % 1000000 / 1000,
(after_md_sleep_status.gsm_sleep_time -
before_md_sleep_status.gsm_sleep_time) / 1000000,
(after_md_sleep_status.gsm_sleep_time -
before_md_sleep_status.gsm_sleep_time) % 1000000 / 1000,
(after_md_sleep_status.wcdma_sleep_time -
before_md_sleep_status.wcdma_sleep_time) / 1000000,
(after_md_sleep_status.wcdma_sleep_time -
before_md_sleep_status.wcdma_sleep_time) % 1000000 / 1000,
(after_md_sleep_status.lte_sleep_time -
before_md_sleep_status.lte_sleep_time) / 1000000,
(after_md_sleep_status.lte_sleep_time -
before_md_sleep_status.lte_sleep_time) % 1000000 / 1000,
(after_md_sleep_status.nr_sleep_time -
before_md_sleep_status.nr_sleep_time) / 1000000,
(after_md_sleep_status.nr_sleep_time -
before_md_sleep_status.nr_sleep_time) % 10000000 / 1000);
WARN_ON(strlen(log_buf) >= LOG_BUF_SIZE);
printk_deferred("[name:spm&][SPM] %s", log_buf);
}
#endif
}
static inline int mt6833_suspend_common_enter(unsigned int *susp_status)
{
unsigned int status = PLAT_VCORE_LP_MODE
| PLAT_PMIC_VCORE_SRCLKEN0
| PLAT_SUSPEND;
/* maybe need to stop sspm/mcupm mcdi task here */
if (susp_status)
*susp_status = status;
return 0;
}
static inline int mt6833_suspend_common_resume(unsigned int susp_status)
{
/* Implement suspend common flow here */
return 0;
}
static int __mt6833_suspend_prompt(int type, int cpu,
const struct mtk_lpm_issuer *issuer)
{
int ret = 0;
unsigned int spm_res = 0;
mt6833_suspend_status = 0;
printk_deferred("[name:spm&][%s:%d] - prepare suspend enter\n",
__func__, __LINE__);
ret = mt6833_suspend_common_enter(&mt6833_suspend_status);
if (ret)
goto PLAT_LEAVE_SUSPEND;
/* Legacy SSPM flow, spm sw resource request flow */
mt6833_do_mcusys_prepare_pdn(mt6833_suspend_status, &spm_res);
printk_deferred("[name:spm&][%s:%d] - suspend enter\n",
__func__, __LINE__);
/* Record md sleep time */
get_md_sleep_time(&before_md_sleep_status);
PLAT_LEAVE_SUSPEND:
return ret;
}
static void __mt6833_suspend_reflect(int type, int cpu,
const struct mtk_lpm_issuer *issuer)
{
printk_deferred("[name:spm&][%s:%d] - prepare suspend resume\n",
__func__, __LINE__);
mt6833_suspend_common_resume(mt6833_suspend_status);
mt6833_do_mcusys_prepare_on();
printk_deferred("[name:spm&][%s:%d] - resume\n",
__func__, __LINE__);
/* do not call issuer when prepare fail */
if (mt6833_model_suspend.flag & MTK_LP_PREPARE_FAIL)
return;
if (issuer)
issuer->log(MT_LPM_ISSUER_SUSPEND, "suspend", NULL);
/* show md sleep duration during AP suspend */
get_md_sleep_time(&after_md_sleep_status);
log_md_sleep_info();
}
int mt6833_suspend_system_prompt(int cpu,
const struct mtk_lpm_issuer *issuer)
{
return __mt6833_suspend_prompt(MTK_LPM_SUSPEND_S2IDLE,
cpu, issuer);
}
void mt6833_suspend_system_reflect(int cpu,
const struct mtk_lpm_issuer *issuer)
{
return __mt6833_suspend_reflect(MTK_LPM_SUSPEND_S2IDLE,
cpu, issuer);
}
int mt6833_suspend_s2idle_prompt(int cpu,
const struct mtk_lpm_issuer *issuer)
{
int ret = 0;
cpumask_set_cpu(cpu, &s2idle_cpumask);
if (cpumask_weight(&s2idle_cpumask) == num_online_cpus()) {
#ifdef CONFIG_PM_SLEEP
/* Notice
* Fix the rcu_idle workaround later.
* There are many rcu behaviors in syscore callback.
* In s2idle framework, the rcu enter idle before cpu
* enter idle state. So we need to using RCU_NONIDLE()
* with syscore. But anyway in s2idle, when lastest cpu
* enter idle state means there won't care r/w sync problem
* and RCU_NOIDLE maybe the right solution.
*/
RCU_NONIDLE({
ret = syscore_suspend();
});
#endif
if (ret < 0)
mt6833_model_suspend.flag |= MTK_LP_PREPARE_FAIL;
ret = __mt6833_suspend_prompt(MTK_LPM_SUSPEND_S2IDLE,
cpu, issuer);
}
return ret;
}
int mt6833_suspend_s2idle_prepare_enter(int prompt, int cpu,
const struct mtk_lpm_issuer *issuer)
{
int ret = 0;
if (mt6833_model_suspend.flag & MTK_LP_PREPARE_FAIL)
ret = -1;
return ret;
}
void mt6833_suspend_s2idle_reflect(int cpu,
const struct mtk_lpm_issuer *issuer)
{
if (cpumask_weight(&s2idle_cpumask) == num_online_cpus()) {
__mt6833_suspend_reflect(MTK_LPM_SUSPEND_S2IDLE,
cpu, issuer);
#ifdef CONFIG_PM_SLEEP
/* Notice
* Fix the rcu_idle/timekeeping workaround later.
* There are many rcu behaviors in syscore callback.
* In s2idle framework, the rcu enter idle before cpu
* enter idle state. So we need to using RCU_NONIDLE()
* with syscore.
*/
if (!(mt6833_model_suspend.flag & MTK_LP_PREPARE_FAIL))
RCU_NONIDLE(syscore_resume());
if (mt6833_model_suspend.flag & MTK_LP_PREPARE_FAIL)
mt6833_model_suspend.flag &= (~MTK_LP_PREPARE_FAIL);
#endif
}
cpumask_clear_cpu(cpu, &s2idle_cpumask);
}
#define MT6833_SUSPEND_OP_INIT(_prompt, _enter, _resume, _reflect) ({\
mt6833_model_suspend.op.prompt = _prompt;\
mt6833_model_suspend.op.prepare_enter = _enter;\
mt6833_model_suspend.op.prepare_resume = _resume;\
mt6833_model_suspend.op.reflect = _reflect; })
struct mtk_lpm_model mt6833_model_suspend = {
.flag = MTK_LP_REQ_NONE,
.op = {
.prompt = mt6833_suspend_system_prompt,
.reflect = mt6833_suspend_system_reflect,
}
};
#ifdef CONFIG_PM
#define CPU_NUMBER (NR_CPUS)
struct mtk_lpm_abort_control {
struct task_struct *ts;
int cpu;
};
static struct mtk_lpm_abort_control mtk_lpm_ac[CPU_NUMBER];
static int mtk_lpm_in_suspend;
static struct hrtimer lpm_hrtimer[NR_CPUS];
static enum hrtimer_restart lpm_hrtimer_timeout(struct hrtimer *timer)
{
if (mtk_lpm_in_suspend) {
pr_info("[name:spm&][SPM] wakeup system due to not entering suspend\n");
pm_system_wakeup();
}
return HRTIMER_NORESTART;
}
static int mtk_lpm_monitor_thread(void *data)
{
struct sched_param param = {.sched_priority = 99 };
struct mtk_lpm_abort_control *lpm_ac;
ktime_t kt;
lpm_ac = (struct mtk_lpm_abort_control *)data;
kt = ktime_set(5, 100000);
sched_setscheduler(current, SCHED_FIFO, &param);
allow_signal(SIGKILL);
if (mtk_lpm_in_suspend) {
hrtimer_start(&lpm_hrtimer[lpm_ac->cpu], kt, HRTIMER_MODE_REL);
msleep_interruptible(5000);
hrtimer_cancel(&lpm_hrtimer[lpm_ac->cpu]);
} else {
msleep_interruptible(5000);
}
spin_lock(&lpm_abort_locker);
if (cpumask_test_cpu(lpm_ac->cpu, &abort_cpumask))
cpumask_clear_cpu(lpm_ac->cpu, &abort_cpumask);
spin_unlock(&lpm_abort_locker);
do_exit(0);
}
static int suspend_online_cpus;
static int mt6833_spm_suspend_pm_event(struct notifier_block *notifier,
unsigned long pm_event, void *unused)
{
struct timespec ts;
struct rtc_time tm;
int cpu;
getnstimeofday(&ts);
rtc_time_to_tm(ts.tv_sec, &tm);
switch (pm_event) {
case PM_HIBERNATION_PREPARE:
return NOTIFY_DONE;
case PM_RESTORE_PREPARE:
return NOTIFY_DONE;
case PM_POST_HIBERNATION:
return NOTIFY_DONE;
case PM_SUSPEND_PREPARE:
mtk_s2idle_state_enable(1);
cpu_hotplug_disable();
suspend_online_cpus = num_online_cpus();
cpumask_clear(&abort_cpumask);
mtk_lpm_in_suspend = 1;
get_online_cpus();
for_each_online_cpu(cpu) {
mtk_lpm_ac[cpu].ts = kthread_create(mtk_lpm_monitor_thread,
&mtk_lpm_ac[cpu], "LPM-%d", cpu);
mtk_lpm_ac[cpu].cpu = cpu;
if (!IS_ERR(mtk_lpm_ac[cpu].ts)) {
cpumask_set_cpu(cpu, &abort_cpumask);
kthread_bind(mtk_lpm_ac[cpu].ts, cpu);
wake_up_process(mtk_lpm_ac[cpu].ts);
} else {
pr_info("[name:spm&][SPM] create LPM monitor thread %d fail\n",
cpu);
mtk_lpm_in_suspend = 0;
/* terminate previously created threads */
spin_lock(&lpm_abort_locker);
if (!cpumask_empty(&abort_cpumask)) {
for_each_cpu(cpu, &abort_cpumask) {
send_sig(SIGKILL, mtk_lpm_ac[cpu].ts, 0);
}
}
spin_unlock(&lpm_abort_locker);
put_online_cpus();
return NOTIFY_BAD;
}
}
put_online_cpus();
return NOTIFY_DONE;
case PM_POST_SUSPEND:
cpu_hotplug_enable();
/* make sure the rest of callback proceeds*/
mtk_s2idle_state_enable(0);
mtk_lpm_in_suspend = 0;
spin_lock(&lpm_abort_locker);
if (!cpumask_empty(&abort_cpumask)) {
pr_info("[name:spm&][SPM] check cpumask %*pb\n",
cpumask_pr_args(&abort_cpumask));
for_each_cpu(cpu, &abort_cpumask)
send_sig(SIGKILL, mtk_lpm_ac[cpu].ts, 0);
}
spin_unlock(&lpm_abort_locker);
return NOTIFY_DONE;
}
return NOTIFY_OK;
}
static struct notifier_block mt6833_spm_suspend_pm_notifier_func = {
.notifier_call = mt6833_spm_suspend_pm_event,
.priority = 0,
};
#endif /* CONFIG_PM */
int __init mt6833_model_suspend_init(void)
{
int ret;
int i;
int suspend_type = mtk_lpm_suspend_type_get();
if (suspend_type == MTK_LPM_SUSPEND_S2IDLE) {
MT6833_SUSPEND_OP_INIT(mt6833_suspend_s2idle_prompt,
mt6833_suspend_s2idle_prepare_enter,
NULL,
mt6833_suspend_s2idle_reflect);
mtk_lpm_suspend_registry("s2idle", &mt6833_model_suspend);
mtk_s2idle_state_enable(0);
} else {
MT6833_SUSPEND_OP_INIT(mt6833_suspend_system_prompt,
NULL,
NULL,
mt6833_suspend_system_reflect);
mtk_lpm_suspend_registry("suspend", &mt6833_model_suspend);
}
cpumask_clear(&s2idle_cpumask);
#ifdef CONFIG_PM
ret = register_pm_notifier(&mt6833_spm_suspend_pm_notifier_func);
if (ret) {
pr_debug("[name:spm&][SPM] Failed to register PM notifier.\n");
return ret;
}
for (i = 0; i < CPU_NUMBER; i++) {
hrtimer_init(&lpm_hrtimer[i], CLOCK_MONOTONIC, HRTIMER_MODE_REL);
lpm_hrtimer[i].function = lpm_hrtimer_timeout;
}
#endif /* CONFIG_PM */
#ifdef CONFIG_PM_SLEEP_DEBUG
pm_print_times_enabled = false;
#endif
return 0;
}
@@ -0,0 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MT6833_SUSPEND_H__
#define __MT6833_SUSPEND_H__
int mt6833_model_suspend_init(void);
extern void gpio_dump_regs(void);
extern void pll_if_on(void);
extern void subsys_if_on(void);
#endif