[PATCH v1 2/2] cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency
Mark Tseng
chun-jen.tseng at mediatek.com
Fri Sep 13 03:39:33 PDT 2024
mtk_cpufreq_set_target() is re-enter function but the mutex lock decalre
in mtk_cpu_dvfs_info structure for each policy. It should change to
global variable for critical session avoid policy get wrong OPP.
SoC with CCI architecture should set transition_delay to 10 ms
because cpufreq need to call devfreq notifier in async mode. if delay
less than 10ms may get wrong OPP-level in CCI driver.
Add CPUFREQ_ASYNC_NOTIFICATION flages for cpufreq policy because some of
process will get CPU frequency by cpufreq sysfs node. It may get wrong
frequency then call cpufreq_out_of_sync() to fixed frequency.
Signed-off-by: Mark Tseng <chun-jen.tseng at mediatek.com>
---
drivers/cpufreq/mediatek-cpufreq.c | 65 ++++++++++++++++++++++--------
1 file changed, 49 insertions(+), 16 deletions(-)
diff --git a/drivers/cpufreq/mediatek-cpufreq.c b/drivers/cpufreq/mediatek-cpufreq.c
index 663f61565cf7..3303b6d72ea7 100644
--- a/drivers/cpufreq/mediatek-cpufreq.c
+++ b/drivers/cpufreq/mediatek-cpufreq.c
@@ -49,8 +49,6 @@ struct mtk_cpu_dvfs_info {
bool need_voltage_tracking;
int vproc_on_boot;
int pre_vproc;
- /* Avoid race condition for regulators between notify and policy */
- struct mutex reg_lock;
struct notifier_block opp_nb;
unsigned int opp_cpu;
unsigned long current_freq;
@@ -59,6 +57,9 @@ struct mtk_cpu_dvfs_info {
bool ccifreq_bound;
};
+/* Avoid race condition for regulators between notify and policy */
+static DEFINE_MUTEX(mtk_policy_lock);
+
static struct platform_device *cpufreq_pdev;
static LIST_HEAD(dvfs_info_list);
@@ -200,20 +201,29 @@ static bool is_ccifreq_ready(struct mtk_cpu_dvfs_info *info)
static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
unsigned int index)
{
- struct cpufreq_frequency_table *freq_table = policy->freq_table;
- struct clk *cpu_clk = policy->clk;
- struct clk *armpll = clk_get_parent(cpu_clk);
- struct mtk_cpu_dvfs_info *info = policy->driver_data;
- struct device *cpu_dev = info->cpu_dev;
+ struct cpufreq_frequency_table *freq_table;
+ struct clk *cpu_clk;
+ struct clk *armpll;
+ struct mtk_cpu_dvfs_info *info;
+ struct device *cpu_dev;
struct dev_pm_opp *opp;
long freq_hz, pre_freq_hz;
int vproc, pre_vproc, inter_vproc, target_vproc, ret;
+ struct cpufreq_freqs freqs;
- inter_vproc = info->intermediate_voltage;
+ mutex_lock(&mtk_policy_lock);
- pre_freq_hz = clk_get_rate(cpu_clk);
+ freq_table = policy->freq_table;
+ cpu_clk = policy->clk;
+ armpll = clk_get_parent(cpu_clk);
+ info = policy->driver_data;
+ cpu_dev = info->cpu_dev;
+ inter_vproc = info->intermediate_voltage;
+ pre_freq_hz = policy->cur * 1000;
- mutex_lock(&info->reg_lock);
+ freqs.old = policy->cur;
+ freqs.new = freq_table[index].frequency;
+ cpufreq_freq_transition_begin(policy, &freqs);
if (unlikely(info->pre_vproc <= 0))
pre_vproc = regulator_get_voltage(info->proc_reg);
@@ -308,7 +318,8 @@ static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
info->current_freq = freq_hz;
out:
- mutex_unlock(&info->reg_lock);
+ cpufreq_freq_transition_end(policy, &freqs, false);
+ mutex_unlock(&mtk_policy_lock);
return ret;
}
@@ -316,19 +327,20 @@ static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
static int mtk_cpufreq_opp_notifier(struct notifier_block *nb,
unsigned long event, void *data)
{
- struct dev_pm_opp *opp = data;
+ struct dev_pm_opp *opp;
struct dev_pm_opp *new_opp;
struct mtk_cpu_dvfs_info *info;
unsigned long freq, volt;
struct cpufreq_policy *policy;
int ret = 0;
+ mutex_lock(&mtk_policy_lock);
+ opp = data;
info = container_of(nb, struct mtk_cpu_dvfs_info, opp_nb);
if (event == OPP_EVENT_ADJUST_VOLTAGE) {
freq = dev_pm_opp_get_freq(opp);
- mutex_lock(&info->reg_lock);
if (info->current_freq == freq) {
volt = dev_pm_opp_get_voltage(opp);
ret = mtk_cpufreq_set_voltage(info, volt);
@@ -336,7 +348,6 @@ static int mtk_cpufreq_opp_notifier(struct notifier_block *nb,
dev_err(info->cpu_dev,
"failed to scale voltage: %d\n", ret);
}
- mutex_unlock(&info->reg_lock);
} else if (event == OPP_EVENT_DISABLE) {
freq = dev_pm_opp_get_freq(opp);
@@ -361,6 +372,7 @@ static int mtk_cpufreq_opp_notifier(struct notifier_block *nb,
}
}
}
+ mutex_unlock(&mtk_policy_lock);
return notifier_from_errno(ret);
}
@@ -495,7 +507,6 @@ static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
info->intermediate_voltage = dev_pm_opp_get_voltage(opp);
dev_pm_opp_put(opp);
- mutex_init(&info->reg_lock);
info->current_freq = clk_get_rate(info->cpu_clk);
info->opp_cpu = cpu;
@@ -597,6 +608,9 @@ static int mtk_cpufreq_init(struct cpufreq_policy *policy)
policy->driver_data = info;
policy->clk = info->cpu_clk;
+ if (info->soc_data->ccifreq_supported)
+ policy->transition_delay_us = 10000;
+
return 0;
}
@@ -607,13 +621,32 @@ static void mtk_cpufreq_exit(struct cpufreq_policy *policy)
dev_pm_opp_free_cpufreq_table(info->cpu_dev, &policy->freq_table);
}
+static unsigned int mtk_cpufreq_get(unsigned int cpu)
+{
+ struct mtk_cpu_dvfs_info *info;
+ unsigned long current_freq;
+
+ mutex_lock(&mtk_policy_lock);
+ info = mtk_cpu_dvfs_info_lookup(cpu);
+ if (!info) {
+ mutex_unlock(&mtk_policy_lock);
+ return 0;
+ }
+
+ current_freq = info->current_freq / 1000;
+ mutex_unlock(&mtk_policy_lock);
+
+ return current_freq;
+}
+
static struct cpufreq_driver mtk_cpufreq_driver = {
.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK |
CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
+ CPUFREQ_ASYNC_NOTIFICATION |
CPUFREQ_IS_COOLING_DEV,
.verify = cpufreq_generic_frequency_table_verify,
.target_index = mtk_cpufreq_set_target,
- .get = cpufreq_generic_get,
+ .get = mtk_cpufreq_get,
.init = mtk_cpufreq_init,
.exit = mtk_cpufreq_exit,
.register_em = cpufreq_register_em_with_opp,
--
2.45.2
More information about the Linux-mediatek
mailing list