cpufreq: scpi: Register an Energy Model
authorQuentin Perret <quentin.perret@arm.com>
Fri, 18 May 2018 10:08:19 +0000 (11:08 +0100)
committerDouglas RAILLARD <douglas.raillard@arm.com>
Tue, 14 Aug 2018 15:32:33 +0000 (16:32 +0100)
The Energy Model framework provides an API to register the active power
of CPUs. This commit calls this API from the scpi-cpufreq driver which can
estimate power using the P = C * V^2 * f equation where C, V, and f
respectively are the capacitance of the CPU and the voltage and frequency
of the OPP.

The CPU capacitance is read from the "dynamic-power-coefficient" DT
binding, and the voltage and frequency values are obtained from PM_OPP.

Signed-off-by: Quentin Perret <quentin.perret@arm.com>
drivers/cpufreq/scpi-cpufreq.c

index 87a98ec77773a965078861a84670e70d37697864..e46d4d109a7148c1f1fcf0bac198706bb6f77b34 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/cpufreq.h>
 #include <linux/cpumask.h>
 #include <linux/cpu_cooling.h>
+#include <linux/energy_model.h>
 #include <linux/export.h>
 #include <linux/module.h>
 #include <linux/of_platform.h>
@@ -96,13 +97,52 @@ scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
        return 0;
 }
 
+static int of_est_power(unsigned long *mW, unsigned long *KHz, int cpu)
+{
+       unsigned long mV, Hz, MHz;
+       struct device *cpu_dev;
+       struct dev_pm_opp *opp;
+       struct device_node *np;
+       u32 cap;
+       u64 tmp;
+
+       cpu_dev = get_cpu_device(cpu);
+        if (!cpu_dev)
+                return -ENODEV;
+
+       np = of_node_get(cpu_dev->of_node);
+       if (!np)
+               return -EINVAL;
+
+       if (of_property_read_u32(np, "dynamic-power-coefficient", &cap))
+               return -EINVAL;
+
+       Hz = *KHz * 1000;
+       opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz);
+       if (IS_ERR(opp))
+               return -EINVAL;
+
+       mV = dev_pm_opp_get_voltage(opp) / 1000;
+       dev_pm_opp_put(opp);
+
+       MHz = Hz / 1000000;
+       tmp = (u64)cap * mV * mV * MHz;
+       do_div(tmp, 1000000000);
+
+       *mW = (unsigned long)tmp;
+       *KHz = Hz / 1000;
+
+       return 0;
+}
+
 static int scpi_cpufreq_init(struct cpufreq_policy *policy)
 {
-       int ret;
+       int ret, nr_opp;
        unsigned int latency;
        struct device *cpu_dev;
        struct scpi_data *priv;
        struct cpufreq_frequency_table *freq_table;
+       struct em_data_callback em_cb = EM_DATA_CB(of_est_power);
 
        cpu_dev = get_cpu_device(policy->cpu);
        if (!cpu_dev) {
@@ -135,6 +175,7 @@ static int scpi_cpufreq_init(struct cpufreq_policy *policy)
                ret = -EPROBE_DEFER;
                goto out_free_opp;
        }
+       nr_opp = ret;
 
        priv = kzalloc(sizeof(*priv), GFP_KERNEL);
        if (!priv) {
@@ -170,6 +211,9 @@ static int scpi_cpufreq_init(struct cpufreq_policy *policy)
        policy->cpuinfo.transition_latency = latency;
 
        policy->fast_switch_possible = false;
+
+       em_register_freq_domain(policy->cpus, nr_opp, &em_cb);
+
        return 0;
 
 out_free_cpufreq_table: