diff mbox series

[V3,9/9] cpufreq: scmi: Use .register_em() to register with energy model

Message ID 8158488baa1ea1aebd09c8d256db7420051d05ac.1628742634.git.viresh.kumar@linaro.org
State Accepted
Commit 37f188318ea3f1da75b32df3b1a19f45d9840652
Headers show
Series Add callback to register with energy model | expand

Commit Message

Viresh Kumar Aug. 12, 2021, 4:35 a.m. UTC
Set the newly added .register_em() callback to register with the EM
after the cpufreq policy is properly initialized.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

---
 drivers/cpufreq/scmi-cpufreq.c | 65 ++++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 23 deletions(-)

-- 
2.31.1.272.g89b43f80a514

Comments

Sudeep Holla Aug. 26, 2021, 11:27 a.m. UTC | #1
On Thu, Aug 12, 2021 at 10:05:22AM +0530, Viresh Kumar wrote:
> Set the newly added .register_em() callback to register with the EM

> after the cpufreq policy is properly initialized.

> 


Acked-by: Sudeep Holla <sudeep.holla@arm.com>


-- 
Regards,
Sudeep
Lukasz Luba Aug. 31, 2021, 4:17 p.m. UTC | #2
Hi Viresh,

My apologies for delay I was on holidays. I've seen the pull requests,
so it's too late, but just for history...

I have tested this new callback with hacked juno FW which works in
the per-cpu perf requests. There are no issues.

Some debug prints:

[    3.110072] cpu cpu0: cpumask weight(opp_shared_cpus)=4 weight 
policy->cpus=1
[    3.117666] cpu cpu0: Empty OPP table
[    3.121418] cpu cpu0: OPP table empty
[    3.131367] cpu cpu0: EM: created perf domain
[    3.137848] cpu cpu1: cpumask weight(opp_shared_cpus)=2 weight 
policy->cpus=1
[    3.145220] cpu cpu1: Empty OPP table
[    3.148961] cpu cpu1: OPP table empty
[    3.158193] cpu cpu1: EM: created perf domain
[    3.164325] cpu cpu2: cpumask weight(opp_shared_cpus)=2 weight 
policy->cpus=1
[    3.173430] cpu cpu3: cpumask weight(opp_shared_cpus)=4 weight 
policy->cpus=1
[    3.181947] cpu cpu4: cpumask weight(opp_shared_cpus)=4 weight 
policy->cpus=1
[    3.190620] cpu cpu5: cpumask weight(opp_shared_cpus)=4 weight 
policy->cpus=1

root@sqwt-ubuntu:~# grep . /sys/kernel/debug/energy_model/cpu0/ps\:*/*
/sys/kernel/debug/energy_model/cpu0/ps:450000/cost:79
/sys/kernel/debug/energy_model/cpu0/ps:450000/frequency:450000
/sys/kernel/debug/energy_model/cpu0/ps:450000/power:42
/sys/kernel/debug/energy_model/cpu0/ps:575000/cost:85
/sys/kernel/debug/energy_model/cpu0/ps:575000/frequency:575000
/sys/kernel/debug/energy_model/cpu0/ps:575000/power:58
/sys/kernel/debug/energy_model/cpu0/ps:700000/cost:95
/sys/kernel/debug/energy_model/cpu0/ps:700000/frequency:700000
/sys/kernel/debug/energy_model/cpu0/ps:700000/power:79
/sys/kernel/debug/energy_model/cpu0/ps:775000/cost:106
/sys/kernel/debug/energy_model/cpu0/ps:775000/frequency:775000
/sys/kernel/debug/energy_model/cpu0/ps:775000/power:97
/sys/kernel/debug/energy_model/cpu0/ps:850000/cost:119
/sys/kernel/debug/energy_model/cpu0/ps:850000/frequency:850000
/sys/kernel/debug/energy_model/cpu0/ps:850000/power:119
root@sqwt-ubuntu:~# cat /sys/kernel/debug/energy_model/cpu0/cpus
0,3-5
root@sqwt-ubuntu:~# cat /sys/kernel/debug/energy_model/cpu1/cpus
1-2
root@sqwt-ubuntu:~# grep . /sys/kernel/debug/energy_model/cpu1/ps\:*/*
/sys/kernel/debug/energy_model/cpu1/ps:1100000/cost:583
/sys/kernel/debug/energy_model/cpu1/ps:1100000/frequency:1100000
/sys/kernel/debug/energy_model/cpu1/ps:1100000/power:583
/sys/kernel/debug/energy_model/cpu1/ps:450000/cost:391
/sys/kernel/debug/energy_model/cpu1/ps:450000/frequency:450000
/sys/kernel/debug/energy_model/cpu1/ps:450000/power:160
/sys/kernel/debug/energy_model/cpu1/ps:625000/cost:420
/sys/kernel/debug/energy_model/cpu1/ps:625000/frequency:625000
/sys/kernel/debug/energy_model/cpu1/ps:625000/power:239
/sys/kernel/debug/energy_model/cpu1/ps:800000/cost:471
/sys/kernel/debug/energy_model/cpu1/ps:800000/frequency:800000
/sys/kernel/debug/energy_model/cpu1/ps:800000/power:343
/sys/kernel/debug/energy_model/cpu1/ps:950000/cost:525
/sys/kernel/debug/energy_model/cpu1/ps:950000/frequency:950000
/sys/kernel/debug/energy_model/cpu1/ps:950000/power:454
root@sqwt-ubuntu:~#

Regards,
Lukasz
diff mbox series

Patch

diff --git a/drivers/cpufreq/scmi-cpufreq.c b/drivers/cpufreq/scmi-cpufreq.c
index 75f818d04b48..1e0cd4d165f0 100644
--- a/drivers/cpufreq/scmi-cpufreq.c
+++ b/drivers/cpufreq/scmi-cpufreq.c
@@ -22,7 +22,9 @@ 
 
 struct scmi_data {
 	int domain_id;
+	int nr_opp;
 	struct device *cpu_dev;
+	cpumask_var_t opp_shared_cpus;
 };
 
 static struct scmi_protocol_handle *ph;
@@ -123,9 +125,6 @@  static int scmi_cpufreq_init(struct cpufreq_policy *policy)
 	struct device *cpu_dev;
 	struct scmi_data *priv;
 	struct cpufreq_frequency_table *freq_table;
-	struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
-	cpumask_var_t opp_shared_cpus;
-	bool power_scale_mw;
 
 	cpu_dev = get_cpu_device(policy->cpu);
 	if (!cpu_dev) {
@@ -133,9 +132,15 @@  static int scmi_cpufreq_init(struct cpufreq_policy *policy)
 		return -ENODEV;
 	}
 
-	if (!zalloc_cpumask_var(&opp_shared_cpus, GFP_KERNEL))
+	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+	if (!priv)
 		return -ENOMEM;
 
+	if (!zalloc_cpumask_var(&priv->opp_shared_cpus, GFP_KERNEL)) {
+		ret = -ENOMEM;
+		goto out_free_priv;
+	}
+
 	/* Obtain CPUs that share SCMI performance controls */
 	ret = scmi_get_sharing_cpus(cpu_dev, policy->cpus);
 	if (ret) {
@@ -148,14 +153,14 @@  static int scmi_cpufreq_init(struct cpufreq_policy *policy)
 	 * The OPP 'sharing cpus' info may come from DT through an empty opp
 	 * table and opp-shared.
 	 */
-	ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, opp_shared_cpus);
-	if (ret || !cpumask_weight(opp_shared_cpus)) {
+	ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
+	if (ret || !cpumask_weight(priv->opp_shared_cpus)) {
 		/*
 		 * Either opp-table is not set or no opp-shared was found.
 		 * Use the CPU mask from SCMI to designate CPUs sharing an OPP
 		 * table.
 		 */
-		cpumask_copy(opp_shared_cpus, policy->cpus);
+		cpumask_copy(priv->opp_shared_cpus, policy->cpus);
 	}
 
 	 /*
@@ -180,7 +185,7 @@  static int scmi_cpufreq_init(struct cpufreq_policy *policy)
 			goto out_free_opp;
 		}
 
-		ret = dev_pm_opp_set_sharing_cpus(cpu_dev, opp_shared_cpus);
+		ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
 		if (ret) {
 			dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
 				__func__, ret);
@@ -188,21 +193,13 @@  static int scmi_cpufreq_init(struct cpufreq_policy *policy)
 			goto out_free_opp;
 		}
 
-		power_scale_mw = perf_ops->power_scale_mw_get(ph);
-		em_dev_register_perf_domain(cpu_dev, nr_opp, &em_cb,
-					    opp_shared_cpus, power_scale_mw);
-	}
-
-	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
-	if (!priv) {
-		ret = -ENOMEM;
-		goto out_free_opp;
+		priv->nr_opp = nr_opp;
 	}
 
 	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
 	if (ret) {
 		dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
-		goto out_free_priv;
+		goto out_free_opp;
 	}
 
 	priv->cpu_dev = cpu_dev;
@@ -223,17 +220,16 @@  static int scmi_cpufreq_init(struct cpufreq_policy *policy)
 	policy->fast_switch_possible =
 		perf_ops->fast_switch_possible(ph, cpu_dev);
 
-	free_cpumask_var(opp_shared_cpus);
 	return 0;
 
-out_free_priv:
-	kfree(priv);
-
 out_free_opp:
 	dev_pm_opp_remove_all_dynamic(cpu_dev);
 
 out_free_cpumask:
-	free_cpumask_var(opp_shared_cpus);
+	free_cpumask_var(priv->opp_shared_cpus);
+
+out_free_priv:
+	kfree(priv);
 
 	return ret;
 }
@@ -244,11 +240,33 @@  static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
 
 	dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
 	dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
+	free_cpumask_var(priv->opp_shared_cpus);
 	kfree(priv);
 
 	return 0;
 }
 
+static void scmi_cpufreq_register_em(struct cpufreq_policy *policy)
+{
+	struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
+	bool power_scale_mw = perf_ops->power_scale_mw_get(ph);
+	struct scmi_data *priv = policy->driver_data;
+
+	/*
+	 * This callback will be called for each policy, but we don't need to
+	 * register with EM every time. Despite not being part of the same
+	 * policy, some CPUs may still share their perf-domains, and a CPU from
+	 * another policy may already have registered with EM on behalf of CPUs
+	 * of this policy.
+	 */
+	if (!priv->nr_opp)
+		return;
+
+	em_dev_register_perf_domain(get_cpu_device(policy->cpu), priv->nr_opp,
+				    &em_cb, priv->opp_shared_cpus,
+				    power_scale_mw);
+}
+
 static struct cpufreq_driver scmi_cpufreq_driver = {
 	.name	= "scmi",
 	.flags	= CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
@@ -261,6 +279,7 @@  static struct cpufreq_driver scmi_cpufreq_driver = {
 	.get	= scmi_cpufreq_get_rate,
 	.init	= scmi_cpufreq_init,
 	.exit	= scmi_cpufreq_exit,
+	.register_em	= scmi_cpufreq_register_em,
 };
 
 static int scmi_cpufreq_probe(struct scmi_device *sdev)