Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/2] fixed mediatek-cpufreq has multi policy concurrency issue
@ 2024-09-13 10:39 Mark Tseng
  2024-09-13 10:39 ` [PATCH v1 1/2] PM / devfreq: mediatek: protect oop in critical session Mark Tseng
  2024-09-13 10:39 ` [PATCH v1 2/2] cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency Mark Tseng
  0 siblings, 2 replies; 6+ messages in thread
From: Mark Tseng @ 2024-09-13 10:39 UTC (permalink / raw)
  To: Rafael J . Wysocki, Viresh Kumar, MyungJoo Ham, Kyungmin Park,
	Chanwoo Choi, Matthias Brugger, AngeloGioacchino Del Regno
  Cc: linux-pm, linux-kernel, linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group, chun-jen.tseng

For multi cluster SoC, the cpufreq->target_index() is re-enter function 
for each policy to change CPU frequency. In the cirtical session must 
use glocal mutex lock to avoid get wrong OPP.

Mark Tseng (2):
  PM / devfreq: mediatek: protect oop in critical session
  cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency

 drivers/cpufreq/mediatek-cpufreq.c | 65 ++++++++++++++++++++++--------
 drivers/devfreq/mtk-cci-devfreq.c  | 30 +++++++-------
 2 files changed, 64 insertions(+), 31 deletions(-)

-- 
2.45.2



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 1/2] PM / devfreq: mediatek: protect oop in critical session
  2024-09-13 10:39 [PATCH v1 0/2] fixed mediatek-cpufreq has multi policy concurrency issue Mark Tseng
@ 2024-09-13 10:39 ` Mark Tseng
  2024-10-01  6:39   ` Viresh Kumar
  2024-09-13 10:39 ` [PATCH v1 2/2] cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency Mark Tseng
  1 sibling, 1 reply; 6+ messages in thread
From: Mark Tseng @ 2024-09-13 10:39 UTC (permalink / raw)
  To: Rafael J . Wysocki, Viresh Kumar, MyungJoo Ham, Kyungmin Park,
	Chanwoo Choi, Matthias Brugger, AngeloGioacchino Del Regno
  Cc: linux-pm, linux-kernel, linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group, chun-jen.tseng

mtk_ccifreq_target() & mtk_ccifreq_opp_notifier() is re-enter funtion
when cpufreq governor is more than one. It should add global mutex to
protect OPP , avoid get wrong frequency & voltage.

Signed-off-by: Mark Tseng <chun-jen.tseng@mediatek.com>
---
 drivers/devfreq/mtk-cci-devfreq.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/devfreq/mtk-cci-devfreq.c b/drivers/devfreq/mtk-cci-devfreq.c
index 7ad5225b0381..293cf81e53fe 100644
--- a/drivers/devfreq/mtk-cci-devfreq.c
+++ b/drivers/devfreq/mtk-cci-devfreq.c
@@ -29,13 +29,14 @@ struct mtk_ccifreq_drv {
 	struct clk *inter_clk;
 	int inter_voltage;
 	unsigned long pre_freq;
-	/* Avoid race condition for regulators between notify and policy */
-	struct mutex reg_lock;
 	struct notifier_block opp_nb;
 	const struct mtk_ccifreq_platform_data *soc_data;
 	int vtrack_max;
 };
 
+/* Avoid race condition for regulators between notify and policy */
+static DEFINE_MUTEX(reg_lock);
+
 static int mtk_ccifreq_set_voltage(struct mtk_ccifreq_drv *drv, int new_voltage)
 {
 	const struct mtk_ccifreq_platform_data *soc_data = drv->soc_data;
@@ -125,22 +126,20 @@ static int mtk_ccifreq_set_voltage(struct mtk_ccifreq_drv *drv, int new_voltage)
 static int mtk_ccifreq_target(struct device *dev, unsigned long *freq,
 			      u32 flags)
 {
-	struct mtk_ccifreq_drv *drv = dev_get_drvdata(dev);
+	struct mtk_ccifreq_drv *drv;
 	struct clk *cci_pll;
 	struct dev_pm_opp *opp;
 	unsigned long opp_rate;
 	int voltage, pre_voltage, inter_voltage, target_voltage, ret;
 
+	mutex_lock(&reg_lock);
+
+	drv = dev_get_drvdata(dev);
 	if (!drv)
 		return -EINVAL;
 
-	if (drv->pre_freq == *freq)
-		return 0;
-
-	mutex_lock(&drv->reg_lock);
-
-	inter_voltage = drv->inter_voltage;
 	cci_pll = clk_get_parent(drv->cci_clk);
+	inter_voltage = drv->inter_voltage;
 
 	opp_rate = *freq;
 	opp = devfreq_recommended_opp(dev, &opp_rate, 1);
@@ -206,7 +205,7 @@ static int mtk_ccifreq_target(struct device *dev, unsigned long *freq,
 	}
 
 	drv->pre_freq = *freq;
-	mutex_unlock(&drv->reg_lock);
+	mutex_unlock(&reg_lock);
 
 	return 0;
 
@@ -214,21 +213,23 @@ static int mtk_ccifreq_target(struct device *dev, unsigned long *freq,
 	mtk_ccifreq_set_voltage(drv, pre_voltage);
 
 out_unlock:
-	mutex_unlock(&drv->reg_lock);
+	mutex_unlock(&reg_lock);
 	return ret;
 }
 
 static int mtk_ccifreq_opp_notifier(struct notifier_block *nb,
 				    unsigned long event, void *data)
 {
-	struct dev_pm_opp *opp = data;
+	struct dev_pm_opp *opp;
 	struct mtk_ccifreq_drv *drv;
 	unsigned long freq, volt;
 
+	mutex_lock(&reg_lock);
+
+	opp = data;
 	drv = container_of(nb, struct mtk_ccifreq_drv, opp_nb);
 
 	if (event == OPP_EVENT_ADJUST_VOLTAGE) {
-		mutex_lock(&drv->reg_lock);
 		freq = dev_pm_opp_get_freq(opp);
 
 		/* current opp item is changed */
@@ -236,8 +237,8 @@ static int mtk_ccifreq_opp_notifier(struct notifier_block *nb,
 			volt = dev_pm_opp_get_voltage(opp);
 			mtk_ccifreq_set_voltage(drv, volt);
 		}
-		mutex_unlock(&drv->reg_lock);
 	}
+	mutex_unlock(&reg_lock);
 
 	return 0;
 }
@@ -262,7 +263,6 @@ static int mtk_ccifreq_probe(struct platform_device *pdev)
 	drv->dev = dev;
 	drv->soc_data = (const struct mtk_ccifreq_platform_data *)
 				of_device_get_match_data(&pdev->dev);
-	mutex_init(&drv->reg_lock);
 	platform_set_drvdata(pdev, drv);
 
 	drv->cci_clk = devm_clk_get(dev, "cci");
-- 
2.45.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v1 2/2] cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency
  2024-09-13 10:39 [PATCH v1 0/2] fixed mediatek-cpufreq has multi policy concurrency issue Mark Tseng
  2024-09-13 10:39 ` [PATCH v1 1/2] PM / devfreq: mediatek: protect oop in critical session Mark Tseng
@ 2024-09-13 10:39 ` Mark Tseng
  2024-10-01  6:43   ` Viresh Kumar
  1 sibling, 1 reply; 6+ messages in thread
From: Mark Tseng @ 2024-09-13 10:39 UTC (permalink / raw)
  To: Rafael J . Wysocki, Viresh Kumar, MyungJoo Ham, Kyungmin Park,
	Chanwoo Choi, Matthias Brugger, AngeloGioacchino Del Regno
  Cc: linux-pm, linux-kernel, linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group, chun-jen.tseng

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@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



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 1/2] PM / devfreq: mediatek: protect oop in critical session
  2024-09-13 10:39 ` [PATCH v1 1/2] PM / devfreq: mediatek: protect oop in critical session Mark Tseng
@ 2024-10-01  6:39   ` Viresh Kumar
  2024-10-01  6:41     ` Viresh Kumar
  0 siblings, 1 reply; 6+ messages in thread
From: Viresh Kumar @ 2024-10-01  6:39 UTC (permalink / raw)
  To: Mark Tseng
  Cc: Rafael J . Wysocki, MyungJoo Ham, Kyungmin Park, Chanwoo Choi,
	Matthias Brugger, AngeloGioacchino Del Regno, linux-pm,
	linux-kernel, linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group

On 13-09-24, 18:39, Mark Tseng wrote:
> mtk_ccifreq_target() & mtk_ccifreq_opp_notifier() is re-enter funtion
> when cpufreq governor is more than one. It should add global mutex to
> protect OPP , avoid get wrong frequency & voltage.

I am not sure I understood the problem well. Can you explain clearly
why the current locking doesn't work with details call chain ?

-- 
viresh


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 1/2] PM / devfreq: mediatek: protect oop in critical session
  2024-10-01  6:39   ` Viresh Kumar
@ 2024-10-01  6:41     ` Viresh Kumar
  0 siblings, 0 replies; 6+ messages in thread
From: Viresh Kumar @ 2024-10-01  6:41 UTC (permalink / raw)
  To: Mark Tseng
  Cc: Rafael J . Wysocki, MyungJoo Ham, Kyungmin Park, Chanwoo Choi,
	Matthias Brugger, AngeloGioacchino Del Regno, linux-pm,
	linux-kernel, linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group

On 01-10-24, 12:09, Viresh Kumar wrote:
> On 13-09-24, 18:39, Mark Tseng wrote:
> > mtk_ccifreq_target() & mtk_ccifreq_opp_notifier() is re-enter funtion
> > when cpufreq governor is more than one. It should add global mutex to
> > protect OPP , avoid get wrong frequency & voltage.
> 
> I am not sure I understood the problem well. Can you explain clearly
> why the current locking doesn't work with details call chain ?

Ended up replying to the wrong email. This comment is for patch 2/2.

-- 
viresh


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 2/2] cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency
  2024-09-13 10:39 ` [PATCH v1 2/2] cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency Mark Tseng
@ 2024-10-01  6:43   ` Viresh Kumar
  0 siblings, 0 replies; 6+ messages in thread
From: Viresh Kumar @ 2024-10-01  6:43 UTC (permalink / raw)
  To: Mark Tseng
  Cc: Rafael J . Wysocki, MyungJoo Ham, Kyungmin Park, Chanwoo Choi,
	Matthias Brugger, AngeloGioacchino Del Regno, linux-pm,
	linux-kernel, linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group

On 13-09-24, 18:39, Mark Tseng wrote:
> 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.

I am not sure I understood the problem well. Can you explain clearly
why the current locking doesn't work with details call chain ?

It is normally okay to have per-policy locks otherwise. Are there any
common resources being used between policies that need locking ?

> 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.

Don't do so much in a single commit. Separate commits for each logical
change so they can be reviewed well. Also don't send cpufreq along
with devfreq changes, unless they are dependent on each other.

-- 
viresh


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-10-01  6:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-13 10:39 [PATCH v1 0/2] fixed mediatek-cpufreq has multi policy concurrency issue Mark Tseng
2024-09-13 10:39 ` [PATCH v1 1/2] PM / devfreq: mediatek: protect oop in critical session Mark Tseng
2024-10-01  6:39   ` Viresh Kumar
2024-10-01  6:41     ` Viresh Kumar
2024-09-13 10:39 ` [PATCH v1 2/2] cpufreq: mediatek: Fixed cpufreq has 2 policy will cause concurrency Mark Tseng
2024-10-01  6:43   ` Viresh Kumar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox