1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Energy Model of devices
4 *
5 * Copyright (c) 2018-2020, Arm ltd.
6 * Written by: Quentin Perret, Arm ltd.
7 * Improvements provided by: Lukasz Luba, Arm ltd.
8 */
9
10 #define pr_fmt(fmt) "energy_model: " fmt
11
12 #include <linux/cpu.h>
13 #include <linux/cpumask.h>
14 #include <linux/debugfs.h>
15 #include <linux/energy_model.h>
16 #include <linux/sched/topology.h>
17 #include <linux/slab.h>
18
19 /*
20 * Mutex serializing the registrations of performance domains and letting
21 * callbacks defined by drivers sleep.
22 */
23 static DEFINE_MUTEX(em_pd_mutex);
24
_is_cpu_device(struct device * dev)25 static bool _is_cpu_device(struct device *dev)
26 {
27 return (dev->bus == &cpu_subsys);
28 }
29
30 #ifdef CONFIG_DEBUG_FS
31 static struct dentry *rootdir;
32
em_debug_create_ps(struct em_perf_state * ps,struct dentry * pd)33 static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
34 {
35 struct dentry *d;
36 char name[24];
37
38 snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
39
40 /* Create per-ps directory */
41 d = debugfs_create_dir(name, pd);
42 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);
45 }
46
em_debug_cpus_show(struct seq_file * s,void * unused)47 static 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 }
53 DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
54
em_debug_create_pd(struct device * dev)55 static void em_debug_create_pd(struct device *dev)
56 {
57 struct dentry *d;
58 int i;
59
60 /* Create the directory of the performance domain */
61 d = debugfs_create_dir(dev_name(dev), rootdir);
62
63 if (_is_cpu_device(dev))
64 debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
65 &em_debug_cpus_fops);
66
67 /* Create a sub-directory for each performance state */
68 for (i = 0; i < dev->em_pd->nr_perf_states; i++)
69 em_debug_create_ps(&dev->em_pd->table[i], d);
70
71 }
72
em_debug_remove_pd(struct device * dev)73 static void em_debug_remove_pd(struct device *dev)
74 {
75 struct dentry *debug_dir;
76
77 debug_dir = debugfs_lookup(dev_name(dev), rootdir);
78 debugfs_remove_recursive(debug_dir);
79 }
80
em_debug_init(void)81 static int __init em_debug_init(void)
82 {
83 /* Create /sys/kernel/debug/energy_model directory */
84 rootdir = debugfs_create_dir("energy_model", NULL);
85
86 return 0;
87 }
88 fs_initcall(em_debug_init);
89 #else /* CONFIG_DEBUG_FS */
em_debug_create_pd(struct device * dev)90 static void em_debug_create_pd(struct device *dev) {}
em_debug_remove_pd(struct device * dev)91 static void em_debug_remove_pd(struct device *dev) {}
92 #endif
93
em_create_perf_table(struct device * dev,struct em_perf_domain * pd,int nr_states,struct em_data_callback * cb)94 static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
95 int nr_states, struct em_data_callback *cb)
96 {
97 unsigned long opp_eff, prev_opp_eff = ULONG_MAX;
98 unsigned long power, freq, prev_freq = 0;
99 struct em_perf_state *table;
100 int i, ret;
101 u64 fmax;
102
103 table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
104 if (!table)
105 return -ENOMEM;
106
107 /* Build the list of performance states for this performance domain */
108 for (i = 0, freq = 0; i < nr_states; i++, freq++) {
109 /*
110 * active_power() is a driver callback which ceils 'freq' to
111 * lowest performance state of 'dev' above 'freq' and updates
112 * 'power' and 'freq' accordingly.
113 */
114 ret = cb->active_power(&power, &freq, dev);
115 if (ret) {
116 dev_err(dev, "EM: invalid perf. state: %d\n",
117 ret);
118 goto free_ps_table;
119 }
120
121 /*
122 * We expect the driver callback to increase the frequency for
123 * higher performance states.
124 */
125 if (freq <= prev_freq) {
126 dev_err(dev, "EM: non-increasing freq: %lu\n",
127 freq);
128 goto free_ps_table;
129 }
130
131 /*
132 * The power returned by active_state() is expected to be
133 * positive, in milli-watts and to fit into 16 bits.
134 */
135 if (!power || power > EM_MAX_POWER) {
136 dev_err(dev, "EM: invalid power: %lu\n",
137 power);
138 goto free_ps_table;
139 }
140
141 table[i].power = power;
142 table[i].frequency = prev_freq = freq;
143
144 /*
145 * The hertz/watts efficiency ratio should decrease as the
146 * frequency grows on sane platforms. But this isn't always
147 * true in practice so warn the user if a higher OPP is more
148 * power efficient than a lower one.
149 */
150 opp_eff = freq / power;
151 if (opp_eff >= prev_opp_eff)
152 dev_dbg(dev, "EM: hertz/watts ratio non-monotonically decreasing: em_perf_state %d >= em_perf_state%d\n",
153 i, i - 1);
154 prev_opp_eff = opp_eff;
155 }
156
157 /* Compute the cost of each performance state. */
158 fmax = (u64) table[nr_states - 1].frequency;
159 for (i = 0; i < nr_states; i++) {
160 unsigned long power_res = em_scale_power(table[i].power);
161
162 table[i].cost = div64_u64(fmax * power_res,
163 table[i].frequency);
164 }
165
166 pd->table = table;
167 pd->nr_perf_states = nr_states;
168
169 return 0;
170
171 free_ps_table:
172 kfree(table);
173 return -EINVAL;
174 }
175
em_create_pd(struct device * dev,int nr_states,struct em_data_callback * cb,cpumask_t * cpus)176 static int em_create_pd(struct device *dev, int nr_states,
177 struct em_data_callback *cb, cpumask_t *cpus)
178 {
179 struct em_perf_domain *pd;
180 struct device *cpu_dev;
181 int cpu, ret;
182
183 if (_is_cpu_device(dev)) {
184 pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
185 if (!pd)
186 return -ENOMEM;
187
188 cpumask_copy(em_span_cpus(pd), cpus);
189 } else {
190 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
191 if (!pd)
192 return -ENOMEM;
193 }
194
195 ret = em_create_perf_table(dev, pd, nr_states, cb);
196 if (ret) {
197 kfree(pd);
198 return ret;
199 }
200
201 if (_is_cpu_device(dev))
202 for_each_cpu(cpu, cpus) {
203 cpu_dev = get_cpu_device(cpu);
204 cpu_dev->em_pd = pd;
205 }
206
207 dev->em_pd = pd;
208
209 return 0;
210 }
211
212 /**
213 * em_pd_get() - Return the performance domain for a device
214 * @dev : Device to find the performance domain for
215 *
216 * Returns the performance domain to which @dev belongs, or NULL if it doesn't
217 * exist.
218 */
em_pd_get(struct device * dev)219 struct em_perf_domain *em_pd_get(struct device *dev)
220 {
221 if (IS_ERR_OR_NULL(dev))
222 return NULL;
223
224 return dev->em_pd;
225 }
226 EXPORT_SYMBOL_GPL(em_pd_get);
227
228 /**
229 * em_cpu_get() - Return the performance domain for a CPU
230 * @cpu : CPU to find the performance domain for
231 *
232 * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
233 * exist.
234 */
em_cpu_get(int cpu)235 struct em_perf_domain *em_cpu_get(int cpu)
236 {
237 struct device *cpu_dev;
238
239 cpu_dev = get_cpu_device(cpu);
240 if (!cpu_dev)
241 return NULL;
242
243 return em_pd_get(cpu_dev);
244 }
245 EXPORT_SYMBOL_GPL(em_cpu_get);
246
247 /**
248 * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
249 * @dev : Device for which the EM is to register
250 * @nr_states : Number of performance states to register
251 * @cb : Callback functions providing the data of the Energy Model
252 * @cpus : Pointer to cpumask_t, which in case of a CPU device is
253 * obligatory. It can be taken from i.e. 'policy->cpus'. For other
254 * type of devices this should be set to NULL.
255 *
256 * Create Energy Model tables for a performance domain using the callbacks
257 * defined in cb.
258 *
259 * If multiple clients register the same performance domain, all but the first
260 * registration will be ignored.
261 *
262 * Return 0 on success
263 */
em_dev_register_perf_domain(struct device * dev,unsigned int nr_states,struct em_data_callback * cb,cpumask_t * cpus)264 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
265 struct em_data_callback *cb, cpumask_t *cpus)
266 {
267 unsigned long cap, prev_cap = 0;
268 int cpu, ret;
269
270 if (!dev || !nr_states || !cb)
271 return -EINVAL;
272
273 /*
274 * Use a mutex to serialize the registration of performance domains and
275 * let the driver-defined callback functions sleep.
276 */
277 mutex_lock(&em_pd_mutex);
278
279 if (dev->em_pd) {
280 ret = -EEXIST;
281 goto unlock;
282 }
283
284 if (_is_cpu_device(dev)) {
285 if (!cpus) {
286 dev_err(dev, "EM: invalid CPU mask\n");
287 ret = -EINVAL;
288 goto unlock;
289 }
290
291 for_each_cpu(cpu, cpus) {
292 if (em_cpu_get(cpu)) {
293 dev_err(dev, "EM: exists for CPU%d\n", cpu);
294 ret = -EEXIST;
295 goto unlock;
296 }
297 /*
298 * All CPUs of a domain must have the same
299 * micro-architecture since they all share the same
300 * table.
301 */
302 cap = arch_scale_cpu_capacity(cpu);
303 if (prev_cap && prev_cap != cap) {
304 dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
305 cpumask_pr_args(cpus));
306
307 ret = -EINVAL;
308 goto unlock;
309 }
310 prev_cap = cap;
311 }
312 }
313
314 ret = em_create_pd(dev, nr_states, cb, cpus);
315 if (ret)
316 goto unlock;
317
318 em_debug_create_pd(dev);
319 dev_info(dev, "EM: created perf domain\n");
320
321 unlock:
322 mutex_unlock(&em_pd_mutex);
323 return ret;
324 }
325 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
326
327 /**
328 * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
329 * @dev : Device for which the EM is registered
330 *
331 * Unregister the EM for the specified @dev (but not a CPU device).
332 */
em_dev_unregister_perf_domain(struct device * dev)333 void em_dev_unregister_perf_domain(struct device *dev)
334 {
335 if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
336 return;
337
338 if (_is_cpu_device(dev))
339 return;
340
341 /*
342 * The mutex separates all register/unregister requests and protects
343 * from potential clean-up/setup issues in the debugfs directories.
344 * The debugfs directory name is the same as device's name.
345 */
346 mutex_lock(&em_pd_mutex);
347 em_debug_remove_pd(dev);
348
349 kfree(dev->em_pd->table);
350 kfree(dev->em_pd);
351 dev->em_pd = NULL;
352 mutex_unlock(&em_pd_mutex);
353 }
354 EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);
355