Skip to content

Commit 0e0ffa8

Browse files
lukaszluba-armrafaeljw
authored andcommitted
OPP: refactor dev_pm_opp_of_register_em() and update related drivers
The Energy Model framework supports not only CPU devices. Drop the CPU specific interface with cpumask and add struct device. Add also a return value, user might use it. This new interface provides easy way to create a simple Energy Model, which then might be used by e.g. thermal subsystem. Acked-by: Daniel Lezcano <[email protected]> Signed-off-by: Lukasz Luba <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 7b7570a commit 0e0ffa8

File tree

9 files changed

+65
-35
lines changed

9 files changed

+65
-35
lines changed

drivers/cpufreq/cpufreq-dt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ static int cpufreq_init(struct cpufreq_policy *policy)
279279
policy->cpuinfo.transition_latency = transition_latency;
280280
policy->dvfs_possible_from_any_cpu = true;
281281

282-
dev_pm_opp_of_register_em(policy->cpus);
282+
dev_pm_opp_of_register_em(cpu_dev, policy->cpus);
283283

284284
return 0;
285285

drivers/cpufreq/imx6q-cpufreq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
193193
policy->clk = clks[ARM].clk;
194194
cpufreq_generic_init(policy, freq_table, transition_latency);
195195
policy->suspend_freq = max_freq;
196-
dev_pm_opp_of_register_em(policy->cpus);
196+
dev_pm_opp_of_register_em(cpu_dev, policy->cpus);
197197

198198
return 0;
199199
}

drivers/cpufreq/mediatek-cpufreq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ static int mtk_cpufreq_init(struct cpufreq_policy *policy)
448448
policy->driver_data = info;
449449
policy->clk = info->cpu_clk;
450450

451-
dev_pm_opp_of_register_em(policy->cpus);
451+
dev_pm_opp_of_register_em(info->cpu_dev, policy->cpus);
452452

453453
return 0;
454454
}

drivers/cpufreq/omap-cpufreq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static int omap_cpu_init(struct cpufreq_policy *policy)
131131

132132
/* FIXME: what's the actual transition time? */
133133
cpufreq_generic_init(policy, freq_table, 300 * 1000);
134-
dev_pm_opp_of_register_em(policy->cpus);
134+
dev_pm_opp_of_register_em(mpu_dev, policy->cpus);
135135

136136
return 0;
137137
}

drivers/cpufreq/qcom-cpufreq-hw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static int qcom_cpufreq_hw_cpu_init(struct cpufreq_policy *policy)
238238
goto error;
239239
}
240240

241-
dev_pm_opp_of_register_em(policy->cpus);
241+
dev_pm_opp_of_register_em(cpu_dev, policy->cpus);
242242

243243
policy->fast_switch_possible = true;
244244

drivers/cpufreq/scpi-cpufreq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static int scpi_cpufreq_init(struct cpufreq_policy *policy)
167167

168168
policy->fast_switch_possible = false;
169169

170-
dev_pm_opp_of_register_em(policy->cpus);
170+
dev_pm_opp_of_register_em(cpu_dev, policy->cpus);
171171

172172
return 0;
173173

drivers/cpufreq/vexpress-spc-cpufreq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ static int ve_spc_cpufreq_init(struct cpufreq_policy *policy)
450450
policy->freq_table = freq_table[cur_cluster];
451451
policy->cpuinfo.transition_latency = 1000000; /* 1 ms */
452452

453-
dev_pm_opp_of_register_em(policy->cpus);
453+
dev_pm_opp_of_register_em(cpu_dev, policy->cpus);
454454

455455
if (is_bL_switching_enabled())
456456
per_cpu(cpu_last_req_freq, policy->cpu) =

drivers/opp/of.c

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,18 +1205,18 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
12051205

12061206
/*
12071207
* Callback function provided to the Energy Model framework upon registration.
1208-
* This computes the power estimated by @CPU at @kHz if it is the frequency
1208+
* This computes the power estimated by @dev at @kHz if it is the frequency
12091209
* of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
12101210
* (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
12111211
* frequency and @mW to the associated power. The power is estimated as
1212-
* P = C * V^2 * f with C being the CPU's capacitance and V and f respectively
1213-
* the voltage and frequency of the OPP.
1212+
* P = C * V^2 * f with C being the device's capacitance and V and f
1213+
* respectively the voltage and frequency of the OPP.
12141214
*
1215-
* Returns -ENODEV if the CPU device cannot be found, -EINVAL if the power
1216-
* calculation failed because of missing parameters, 0 otherwise.
1215+
* Returns -EINVAL if the power calculation failed because of missing
1216+
* parameters, 0 otherwise.
12171217
*/
1218-
static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
1219-
struct device *cpu_dev)
1218+
static int __maybe_unused _get_power(unsigned long *mW, unsigned long *kHz,
1219+
struct device *dev)
12201220
{
12211221
struct dev_pm_opp *opp;
12221222
struct device_node *np;
@@ -1225,7 +1225,7 @@ static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
12251225
u64 tmp;
12261226
int ret;
12271227

1228-
np = of_node_get(cpu_dev->of_node);
1228+
np = of_node_get(dev->of_node);
12291229
if (!np)
12301230
return -EINVAL;
12311231

@@ -1235,7 +1235,7 @@ static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
12351235
return -EINVAL;
12361236

12371237
Hz = *kHz * 1000;
1238-
opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz);
1238+
opp = dev_pm_opp_find_freq_ceil(dev, &Hz);
12391239
if (IS_ERR(opp))
12401240
return -EINVAL;
12411241

@@ -1255,30 +1255,38 @@ static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
12551255

12561256
/**
12571257
* dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1258-
* @cpus : CPUs for which an Energy Model has to be registered
1258+
* @dev : Device for which an Energy Model has to be registered
1259+
* @cpus : CPUs for which an Energy Model has to be registered. For
1260+
* other type of devices it should be set to NULL.
12591261
*
12601262
* This checks whether the "dynamic-power-coefficient" devicetree property has
12611263
* been specified, and tries to register an Energy Model with it if it has.
1264+
* Having this property means the voltages are known for OPPs and the EM
1265+
* might be calculated.
12621266
*/
1263-
void dev_pm_opp_of_register_em(struct cpumask *cpus)
1267+
int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
12641268
{
1265-
struct em_data_callback em_cb = EM_DATA_CB(_get_cpu_power);
1266-
int ret, nr_opp, cpu = cpumask_first(cpus);
1267-
struct device *cpu_dev;
1269+
struct em_data_callback em_cb = EM_DATA_CB(_get_power);
12681270
struct device_node *np;
1271+
int ret, nr_opp;
12691272
u32 cap;
12701273

1271-
cpu_dev = get_cpu_device(cpu);
1272-
if (!cpu_dev)
1273-
return;
1274+
if (IS_ERR_OR_NULL(dev)) {
1275+
ret = -EINVAL;
1276+
goto failed;
1277+
}
12741278

1275-
nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
1276-
if (nr_opp <= 0)
1277-
return;
1279+
nr_opp = dev_pm_opp_get_opp_count(dev);
1280+
if (nr_opp <= 0) {
1281+
ret = -EINVAL;
1282+
goto failed;
1283+
}
12781284

1279-
np = of_node_get(cpu_dev->of_node);
1280-
if (!np)
1281-
return;
1285+
np = of_node_get(dev->of_node);
1286+
if (!np) {
1287+
ret = -EINVAL;
1288+
goto failed;
1289+
}
12821290

12831291
/*
12841292
* Register an EM only if the 'dynamic-power-coefficient' property is
@@ -1289,9 +1297,20 @@ void dev_pm_opp_of_register_em(struct cpumask *cpus)
12891297
*/
12901298
ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
12911299
of_node_put(np);
1292-
if (ret || !cap)
1293-
return;
1300+
if (ret || !cap) {
1301+
dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1302+
ret = -EINVAL;
1303+
goto failed;
1304+
}
12941305

1295-
em_dev_register_perf_domain(cpu_dev, nr_opp, &em_cb, cpus);
1306+
ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus);
1307+
if (ret)
1308+
goto failed;
1309+
1310+
return 0;
1311+
1312+
failed:
1313+
dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1314+
return ret;
12961315
}
12971316
EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);

include/linux/pm_opp.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifndef __LINUX_OPP_H__
1212
#define __LINUX_OPP_H__
1313

14+
#include <linux/energy_model.h>
1415
#include <linux/err.h>
1516
#include <linux/notifier.h>
1617

@@ -373,7 +374,11 @@ struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev);
373374
struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp);
374375
int of_get_required_opp_performance_state(struct device_node *np, int index);
375376
int dev_pm_opp_of_find_icc_paths(struct device *dev, struct opp_table *opp_table);
376-
void dev_pm_opp_of_register_em(struct cpumask *cpus);
377+
int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus);
378+
static inline void dev_pm_opp_of_unregister_em(struct device *dev)
379+
{
380+
em_dev_unregister_perf_domain(dev);
381+
}
377382
#else
378383
static inline int dev_pm_opp_of_add_table(struct device *dev)
379384
{
@@ -413,7 +418,13 @@ static inline struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
413418
return NULL;
414419
}
415420

416-
static inline void dev_pm_opp_of_register_em(struct cpumask *cpus)
421+
static inline int dev_pm_opp_of_register_em(struct device *dev,
422+
struct cpumask *cpus)
423+
{
424+
return -ENOTSUPP;
425+
}
426+
427+
static inline void dev_pm_opp_of_unregister_em(struct device *dev)
417428
{
418429
}
419430

0 commit comments

Comments
 (0)