blob: 0f4530b3a8cd9d961dc52b08750e7b44fcfdedd6 [file] [log] [blame]
Quentin Perret27871f72018-12-03 09:56:16 +00001// SPDX-License-Identifier: GPL-2.0
2/*
Lukasz Luba1bc138c2020-06-10 11:12:23 +01003 * Energy Model of devices
Quentin Perret27871f72018-12-03 09:56:16 +00004 *
Lukasz Luba1bc138c2020-06-10 11:12:23 +01005 * Copyright (c) 2018-2020, Arm ltd.
Quentin Perret27871f72018-12-03 09:56:16 +00006 * Written by: Quentin Perret, Arm ltd.
Lukasz Luba1bc138c2020-06-10 11:12:23 +01007 * Improvements provided by: Lukasz Luba, Arm ltd.
Quentin Perret27871f72018-12-03 09:56:16 +00008 */
9
10#define pr_fmt(fmt) "energy_model: " fmt
11
12#include <linux/cpu.h>
13#include <linux/cpumask.h>
Quentin Perret9cac42d2019-01-22 16:42:47 +000014#include <linux/debugfs.h>
Quentin Perret27871f72018-12-03 09:56:16 +000015#include <linux/energy_model.h>
16#include <linux/sched/topology.h>
17#include <linux/slab.h>
18
Quentin Perret27871f72018-12-03 09:56:16 +000019/*
20 * Mutex serializing the registrations of performance domains and letting
21 * callbacks defined by drivers sleep.
22 */
23static DEFINE_MUTEX(em_pd_mutex);
24
Lukasz Luba1bc138c2020-06-10 11:12:23 +010025static bool _is_cpu_device(struct device *dev)
26{
27 return (dev->bus == &cpu_subsys);
28}
29
Quentin Perret9cac42d2019-01-22 16:42:47 +000030#ifdef CONFIG_DEBUG_FS
31static struct dentry *rootdir;
32
Lukasz Luba521b5122020-05-27 10:58:47 +010033static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
Quentin Perret9cac42d2019-01-22 16:42:47 +000034{
35 struct dentry *d;
36 char name[24];
37
Lukasz Luba521b5122020-05-27 10:58:47 +010038 snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
Quentin Perret9cac42d2019-01-22 16:42:47 +000039
Lukasz Luba521b5122020-05-27 10:58:47 +010040 /* Create per-ps directory */
Quentin Perret9cac42d2019-01-22 16:42:47 +000041 d = debugfs_create_dir(name, pd);
Lukasz Luba521b5122020-05-27 10:58:47 +010042 debugfs_create_ulong("frequency", 0444, d, &ps->frequency);
43 debugfs_create_ulong("power", 0444, d, &ps->power);
44 debugfs_create_ulong("cost", 0444, d, &ps->cost);
Quentin Perret9cac42d2019-01-22 16:42:47 +000045}
46
47static int em_debug_cpus_show(struct seq_file *s, void *unused)
48{
49 seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
50
51 return 0;
52}
53DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
54
Lukasz Lubac250d502020-11-05 12:50:01 +000055static int em_debug_units_show(struct seq_file *s, void *unused)
56{
57 struct em_perf_domain *pd = s->private;
58 char *units = pd->milliwatts ? "milliWatts" : "bogoWatts";
59
60 seq_printf(s, "%s\n", units);
61
62 return 0;
63}
64DEFINE_SHOW_ATTRIBUTE(em_debug_units);
65
Lukasz Luba1bc138c2020-06-10 11:12:23 +010066static void em_debug_create_pd(struct device *dev)
Quentin Perret9cac42d2019-01-22 16:42:47 +000067{
68 struct dentry *d;
Quentin Perret9cac42d2019-01-22 16:42:47 +000069 int i;
70
Quentin Perret9cac42d2019-01-22 16:42:47 +000071 /* Create the directory of the performance domain */
Lukasz Luba1bc138c2020-06-10 11:12:23 +010072 d = debugfs_create_dir(dev_name(dev), rootdir);
Quentin Perret9cac42d2019-01-22 16:42:47 +000073
Lukasz Luba1bc138c2020-06-10 11:12:23 +010074 if (_is_cpu_device(dev))
75 debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
76 &em_debug_cpus_fops);
Quentin Perret9cac42d2019-01-22 16:42:47 +000077
Lukasz Lubac250d502020-11-05 12:50:01 +000078 debugfs_create_file("units", 0444, d, dev->em_pd, &em_debug_units_fops);
79
Lukasz Luba521b5122020-05-27 10:58:47 +010080 /* Create a sub-directory for each performance state */
Lukasz Luba1bc138c2020-06-10 11:12:23 +010081 for (i = 0; i < dev->em_pd->nr_perf_states; i++)
82 em_debug_create_ps(&dev->em_pd->table[i], d);
83
84}
85
86static void em_debug_remove_pd(struct device *dev)
87{
88 struct dentry *debug_dir;
89
90 debug_dir = debugfs_lookup(dev_name(dev), rootdir);
91 debugfs_remove_recursive(debug_dir);
Quentin Perret9cac42d2019-01-22 16:42:47 +000092}
93
94static int __init em_debug_init(void)
95{
96 /* Create /sys/kernel/debug/energy_model directory */
97 rootdir = debugfs_create_dir("energy_model", NULL);
98
99 return 0;
100}
Lukasz Lubafb9d62b2021-03-23 14:56:08 +0000101fs_initcall(em_debug_init);
Quentin Perret9cac42d2019-01-22 16:42:47 +0000102#else /* CONFIG_DEBUG_FS */
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100103static void em_debug_create_pd(struct device *dev) {}
104static void em_debug_remove_pd(struct device *dev) {}
Quentin Perret9cac42d2019-01-22 16:42:47 +0000105#endif
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100106
107static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
108 int nr_states, struct em_data_callback *cb)
Quentin Perret27871f72018-12-03 09:56:16 +0000109{
110 unsigned long opp_eff, prev_opp_eff = ULONG_MAX;
111 unsigned long power, freq, prev_freq = 0;
Lukasz Luba521b5122020-05-27 10:58:47 +0100112 struct em_perf_state *table;
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100113 int i, ret;
Quentin Perret27871f72018-12-03 09:56:16 +0000114 u64 fmax;
115
Quentin Perret27871f72018-12-03 09:56:16 +0000116 table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
117 if (!table)
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100118 return -ENOMEM;
Quentin Perret27871f72018-12-03 09:56:16 +0000119
Lukasz Luba521b5122020-05-27 10:58:47 +0100120 /* Build the list of performance states for this performance domain */
Quentin Perret27871f72018-12-03 09:56:16 +0000121 for (i = 0, freq = 0; i < nr_states; i++, freq++) {
122 /*
123 * active_power() is a driver callback which ceils 'freq' to
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100124 * lowest performance state of 'dev' above 'freq' and updates
Quentin Perret27871f72018-12-03 09:56:16 +0000125 * 'power' and 'freq' accordingly.
126 */
Lukasz Lubad0351cc2020-05-27 10:58:49 +0100127 ret = cb->active_power(&power, &freq, dev);
Quentin Perret27871f72018-12-03 09:56:16 +0000128 if (ret) {
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100129 dev_err(dev, "EM: invalid perf. state: %d\n",
130 ret);
Lukasz Luba521b5122020-05-27 10:58:47 +0100131 goto free_ps_table;
Quentin Perret27871f72018-12-03 09:56:16 +0000132 }
133
134 /*
135 * We expect the driver callback to increase the frequency for
Lukasz Luba521b5122020-05-27 10:58:47 +0100136 * higher performance states.
Quentin Perret27871f72018-12-03 09:56:16 +0000137 */
138 if (freq <= prev_freq) {
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100139 dev_err(dev, "EM: non-increasing freq: %lu\n",
140 freq);
Lukasz Luba521b5122020-05-27 10:58:47 +0100141 goto free_ps_table;
Quentin Perret27871f72018-12-03 09:56:16 +0000142 }
143
144 /*
145 * The power returned by active_state() is expected to be
Lukasz Lubaf2c90b12020-11-03 09:05:59 +0000146 * positive and to fit into 16 bits.
Quentin Perret27871f72018-12-03 09:56:16 +0000147 */
Lukasz Luba7d9895c2020-05-27 10:58:48 +0100148 if (!power || power > EM_MAX_POWER) {
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100149 dev_err(dev, "EM: invalid power: %lu\n",
150 power);
Lukasz Luba521b5122020-05-27 10:58:47 +0100151 goto free_ps_table;
Quentin Perret27871f72018-12-03 09:56:16 +0000152 }
153
154 table[i].power = power;
155 table[i].frequency = prev_freq = freq;
156
157 /*
158 * The hertz/watts efficiency ratio should decrease as the
159 * frequency grows on sane platforms. But this isn't always
160 * true in practice so warn the user if a higher OPP is more
161 * power efficient than a lower one.
162 */
163 opp_eff = freq / power;
164 if (opp_eff >= prev_opp_eff)
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100165 dev_dbg(dev, "EM: hertz/watts ratio non-monotonically decreasing: em_perf_state %d >= em_perf_state%d\n",
166 i, i - 1);
Quentin Perret27871f72018-12-03 09:56:16 +0000167 prev_opp_eff = opp_eff;
168 }
169
Lukasz Luba521b5122020-05-27 10:58:47 +0100170 /* Compute the cost of each performance state. */
Quentin Perret27871f72018-12-03 09:56:16 +0000171 fmax = (u64) table[nr_states - 1].frequency;
172 for (i = 0; i < nr_states; i++) {
173 table[i].cost = div64_u64(fmax * table[i].power,
174 table[i].frequency);
175 }
176
177 pd->table = table;
Lukasz Luba521b5122020-05-27 10:58:47 +0100178 pd->nr_perf_states = nr_states;
Quentin Perret27871f72018-12-03 09:56:16 +0000179
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100180 return 0;
Quentin Perret27871f72018-12-03 09:56:16 +0000181
Lukasz Luba521b5122020-05-27 10:58:47 +0100182free_ps_table:
Quentin Perret27871f72018-12-03 09:56:16 +0000183 kfree(table);
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100184 return -EINVAL;
Quentin Perret27871f72018-12-03 09:56:16 +0000185}
186
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100187static int em_create_pd(struct device *dev, int nr_states,
188 struct em_data_callback *cb, cpumask_t *cpus)
189{
190 struct em_perf_domain *pd;
191 struct device *cpu_dev;
192 int cpu, ret;
193
194 if (_is_cpu_device(dev)) {
195 pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
196 if (!pd)
197 return -ENOMEM;
198
199 cpumask_copy(em_span_cpus(pd), cpus);
200 } else {
201 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
202 if (!pd)
203 return -ENOMEM;
204 }
205
206 ret = em_create_perf_table(dev, pd, nr_states, cb);
207 if (ret) {
208 kfree(pd);
209 return ret;
210 }
211
212 if (_is_cpu_device(dev))
213 for_each_cpu(cpu, cpus) {
214 cpu_dev = get_cpu_device(cpu);
215 cpu_dev->em_pd = pd;
216 }
217
218 dev->em_pd = pd;
219
220 return 0;
221}
222
223/**
224 * em_pd_get() - Return the performance domain for a device
225 * @dev : Device to find the performance domain for
226 *
227 * Returns the performance domain to which @dev belongs, or NULL if it doesn't
228 * exist.
229 */
230struct em_perf_domain *em_pd_get(struct device *dev)
231{
232 if (IS_ERR_OR_NULL(dev))
233 return NULL;
234
235 return dev->em_pd;
236}
237EXPORT_SYMBOL_GPL(em_pd_get);
238
Quentin Perret27871f72018-12-03 09:56:16 +0000239/**
240 * em_cpu_get() - Return the performance domain for a CPU
241 * @cpu : CPU to find the performance domain for
242 *
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100243 * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
Quentin Perret27871f72018-12-03 09:56:16 +0000244 * exist.
245 */
246struct em_perf_domain *em_cpu_get(int cpu)
247{
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100248 struct device *cpu_dev;
249
250 cpu_dev = get_cpu_device(cpu);
251 if (!cpu_dev)
252 return NULL;
253
254 return em_pd_get(cpu_dev);
Quentin Perret27871f72018-12-03 09:56:16 +0000255}
256EXPORT_SYMBOL_GPL(em_cpu_get);
257
258/**
Lukasz Luba7d9895c2020-05-27 10:58:48 +0100259 * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
260 * @dev : Device for which the EM is to register
Lukasz Luba521b5122020-05-27 10:58:47 +0100261 * @nr_states : Number of performance states to register
Quentin Perret27871f72018-12-03 09:56:16 +0000262 * @cb : Callback functions providing the data of the Energy Model
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100263 * @cpus : Pointer to cpumask_t, which in case of a CPU device is
Lukasz Luba7d9895c2020-05-27 10:58:48 +0100264 * obligatory. It can be taken from i.e. 'policy->cpus'. For other
265 * type of devices this should be set to NULL.
Lukasz Lubac250d502020-11-05 12:50:01 +0000266 * @milliwatts : Flag indicating that the power values are in milliWatts or
267 * in some other scale. It must be set properly.
Quentin Perret27871f72018-12-03 09:56:16 +0000268 *
269 * Create Energy Model tables for a performance domain using the callbacks
270 * defined in cb.
271 *
Lukasz Lubac250d502020-11-05 12:50:01 +0000272 * The @milliwatts is important to set with correct value. Some kernel
273 * sub-systems might rely on this flag and check if all devices in the EM are
274 * using the same scale.
275 *
Quentin Perret27871f72018-12-03 09:56:16 +0000276 * If multiple clients register the same performance domain, all but the first
277 * registration will be ignored.
278 *
279 * Return 0 on success
280 */
Lukasz Luba7d9895c2020-05-27 10:58:48 +0100281int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
Lukasz Lubac250d502020-11-05 12:50:01 +0000282 struct em_data_callback *cb, cpumask_t *cpus,
283 bool milliwatts)
Quentin Perret27871f72018-12-03 09:56:16 +0000284{
285 unsigned long cap, prev_cap = 0;
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100286 int cpu, ret;
Quentin Perret27871f72018-12-03 09:56:16 +0000287
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100288 if (!dev || !nr_states || !cb)
Quentin Perret27871f72018-12-03 09:56:16 +0000289 return -EINVAL;
290
291 /*
292 * Use a mutex to serialize the registration of performance domains and
293 * let the driver-defined callback functions sleep.
294 */
295 mutex_lock(&em_pd_mutex);
296
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100297 if (dev->em_pd) {
298 ret = -EEXIST;
Quentin Perret27871f72018-12-03 09:56:16 +0000299 goto unlock;
300 }
301
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100302 if (_is_cpu_device(dev)) {
303 if (!cpus) {
304 dev_err(dev, "EM: invalid CPU mask\n");
305 ret = -EINVAL;
306 goto unlock;
307 }
308
309 for_each_cpu(cpu, cpus) {
310 if (em_cpu_get(cpu)) {
311 dev_err(dev, "EM: exists for CPU%d\n", cpu);
312 ret = -EEXIST;
313 goto unlock;
314 }
315 /*
316 * All CPUs of a domain must have the same
317 * micro-architecture since they all share the same
318 * table.
319 */
320 cap = arch_scale_cpu_capacity(cpu);
321 if (prev_cap && prev_cap != cap) {
322 dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
323 cpumask_pr_args(cpus));
324
325 ret = -EINVAL;
326 goto unlock;
327 }
328 prev_cap = cap;
329 }
Quentin Perret27871f72018-12-03 09:56:16 +0000330 }
331
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100332 ret = em_create_pd(dev, nr_states, cb, cpus);
333 if (ret)
334 goto unlock;
335
Lukasz Lubac250d502020-11-05 12:50:01 +0000336 dev->em_pd->milliwatts = milliwatts;
337
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100338 em_debug_create_pd(dev);
339 dev_info(dev, "EM: created perf domain\n");
340
Quentin Perret27871f72018-12-03 09:56:16 +0000341unlock:
342 mutex_unlock(&em_pd_mutex);
Quentin Perret27871f72018-12-03 09:56:16 +0000343 return ret;
344}
Lukasz Luba7d9895c2020-05-27 10:58:48 +0100345EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
346
347/**
Lukasz Luba1bc138c2020-06-10 11:12:23 +0100348 * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
349 * @dev : Device for which the EM is registered
350 *
351 * Unregister the EM for the specified @dev (but not a CPU device).
352 */
353void em_dev_unregister_perf_domain(struct device *dev)
354{
355 if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
356 return;
357
358 if (_is_cpu_device(dev))
359 return;
360
361 /*
362 * The mutex separates all register/unregister requests and protects
363 * from potential clean-up/setup issues in the debugfs directories.
364 * The debugfs directory name is the same as device's name.
365 */
366 mutex_lock(&em_pd_mutex);
367 em_debug_remove_pd(dev);
368
369 kfree(dev->em_pd->table);
370 kfree(dev->em_pd);
371 dev->em_pd = NULL;
372 mutex_unlock(&em_pd_mutex);
373}
374EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);