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_units_show(struct seq_file * s,void * unused)55 static 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 }
64 DEFINE_SHOW_ATTRIBUTE(em_debug_units);
65
em_debug_create_pd(struct device * dev)66 static void em_debug_create_pd(struct device *dev)
67 {
68 struct dentry *d;
69 int i;
70
71 /* Create the directory of the performance domain */
72 d = debugfs_create_dir(dev_name(dev), rootdir);
73
74 if (_is_cpu_device(dev))
75 debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
76 &em_debug_cpus_fops);
77
78 debugfs_create_file("units", 0444, d, dev->em_pd, &em_debug_units_fops);
79
80 /* Create a sub-directory for each performance state */
81 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
em_debug_remove_pd(struct device * dev)86 static 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);
92 }
93
em_debug_init(void)94 static 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 }
101 fs_initcall(em_debug_init);
102 #else /* CONFIG_DEBUG_FS */
em_debug_create_pd(struct device * dev)103 static void em_debug_create_pd(struct device *dev) {}
em_debug_remove_pd(struct device * dev)104 static void em_debug_remove_pd(struct device *dev) {}
105 #endif
106
em_create_perf_table(struct device * dev,struct em_perf_domain * pd,int nr_states,struct em_data_callback * cb)107 static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
108 int nr_states, struct em_data_callback *cb)
109 {
110 unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
111 struct em_perf_state *table;
112 int i, ret;
113 u64 fmax;
114
115 table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
116 if (!table)
117 return -ENOMEM;
118
119 /* Build the list of performance states for this performance domain */
120 for (i = 0, freq = 0; i < nr_states; i++, freq++) {
121 /*
122 * active_power() is a driver callback which ceils 'freq' to
123 * lowest performance state of 'dev' above 'freq' and updates
124 * 'power' and 'freq' accordingly.
125 */
126 ret = cb->active_power(&power, &freq, dev);
127 if (ret) {
128 dev_err(dev, "EM: invalid perf. state: %d\n",
129 ret);
130 goto free_ps_table;
131 }
132
133 /*
134 * We expect the driver callback to increase the frequency for
135 * higher performance states.
136 */
137 if (freq <= prev_freq) {
138 dev_err(dev, "EM: non-increasing freq: %lu\n",
139 freq);
140 goto free_ps_table;
141 }
142
143 /*
144 * The power returned by active_state() is expected to be
145 * positive, in milli-watts and to fit into 16 bits.
146 */
147 if (!power || power > EM_MAX_POWER) {
148 dev_err(dev, "EM: invalid power: %lu\n",
149 power);
150 goto free_ps_table;
151 }
152
153 table[i].power = power;
154 table[i].frequency = prev_freq = freq;
155 }
156
157 /* Compute the cost of each performance state. */
158 fmax = (u64) table[nr_states - 1].frequency;
159 for (i = nr_states - 1; i >= 0; 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 if (table[i].cost >= prev_cost) {
165 dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
166 table[i].frequency);
167 } else {
168 prev_cost = table[i].cost;
169 }
170 }
171
172 pd->table = table;
173 pd->nr_perf_states = nr_states;
174
175 return 0;
176
177 free_ps_table:
178 kfree(table);
179 return -EINVAL;
180 }
181
em_create_pd(struct device * dev,int nr_states,struct em_data_callback * cb,cpumask_t * cpus)182 static int em_create_pd(struct device *dev, int nr_states,
183 struct em_data_callback *cb, cpumask_t *cpus)
184 {
185 struct em_perf_domain *pd;
186 struct device *cpu_dev;
187 int cpu, ret;
188
189 if (_is_cpu_device(dev)) {
190 pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
191 if (!pd)
192 return -ENOMEM;
193
194 cpumask_copy(em_span_cpus(pd), cpus);
195 } else {
196 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
197 if (!pd)
198 return -ENOMEM;
199 }
200
201 ret = em_create_perf_table(dev, pd, nr_states, cb);
202 if (ret) {
203 kfree(pd);
204 return ret;
205 }
206
207 if (_is_cpu_device(dev))
208 for_each_cpu(cpu, cpus) {
209 cpu_dev = get_cpu_device(cpu);
210 cpu_dev->em_pd = pd;
211 }
212
213 dev->em_pd = pd;
214
215 return 0;
216 }
217
218 /**
219 * em_pd_get() - Return the performance domain for a device
220 * @dev : Device to find the performance domain for
221 *
222 * Returns the performance domain to which @dev belongs, or NULL if it doesn't
223 * exist.
224 */
em_pd_get(struct device * dev)225 struct em_perf_domain *em_pd_get(struct device *dev)
226 {
227 if (IS_ERR_OR_NULL(dev))
228 return NULL;
229
230 return dev->em_pd;
231 }
232 EXPORT_SYMBOL_GPL(em_pd_get);
233
234 /**
235 * em_cpu_get() - Return the performance domain for a CPU
236 * @cpu : CPU to find the performance domain for
237 *
238 * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
239 * exist.
240 */
em_cpu_get(int cpu)241 struct em_perf_domain *em_cpu_get(int cpu)
242 {
243 struct device *cpu_dev;
244
245 cpu_dev = get_cpu_device(cpu);
246 if (!cpu_dev)
247 return NULL;
248
249 return em_pd_get(cpu_dev);
250 }
251 EXPORT_SYMBOL_GPL(em_cpu_get);
252
253 /**
254 * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
255 * @dev : Device for which the EM is to register
256 * @nr_states : Number of performance states to register
257 * @cb : Callback functions providing the data of the Energy Model
258 * @cpus : Pointer to cpumask_t, which in case of a CPU device is
259 * obligatory. It can be taken from i.e. 'policy->cpus'. For other
260 * type of devices this should be set to NULL.
261 * @milliwatts : Flag indicating that the power values are in milliWatts or
262 * in some other scale. It must be set properly.
263 *
264 * Create Energy Model tables for a performance domain using the callbacks
265 * defined in cb.
266 *
267 * The @milliwatts is important to set with correct value. Some kernel
268 * sub-systems might rely on this flag and check if all devices in the EM are
269 * using the same scale.
270 *
271 * If multiple clients register the same performance domain, all but the first
272 * registration will be ignored.
273 *
274 * Return 0 on success
275 */
em_dev_register_perf_domain(struct device * dev,unsigned int nr_states,struct em_data_callback * cb,cpumask_t * cpus,bool milliwatts)276 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
277 struct em_data_callback *cb, cpumask_t *cpus,
278 bool milliwatts)
279 {
280 unsigned long cap, prev_cap = 0;
281 int cpu, ret;
282
283 if (!dev || !nr_states || !cb)
284 return -EINVAL;
285
286 /*
287 * Use a mutex to serialize the registration of performance domains and
288 * let the driver-defined callback functions sleep.
289 */
290 mutex_lock(&em_pd_mutex);
291
292 if (dev->em_pd) {
293 ret = -EEXIST;
294 goto unlock;
295 }
296
297 if (_is_cpu_device(dev)) {
298 if (!cpus) {
299 dev_err(dev, "EM: invalid CPU mask\n");
300 ret = -EINVAL;
301 goto unlock;
302 }
303
304 for_each_cpu(cpu, cpus) {
305 if (em_cpu_get(cpu)) {
306 dev_err(dev, "EM: exists for CPU%d\n", cpu);
307 ret = -EEXIST;
308 goto unlock;
309 }
310 /*
311 * All CPUs of a domain must have the same
312 * micro-architecture since they all share the same
313 * table.
314 */
315 cap = arch_scale_cpu_capacity(cpu);
316 if (prev_cap && prev_cap != cap) {
317 dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
318 cpumask_pr_args(cpus));
319
320 ret = -EINVAL;
321 goto unlock;
322 }
323 prev_cap = cap;
324 }
325 }
326
327 ret = em_create_pd(dev, nr_states, cb, cpus);
328 if (ret)
329 goto unlock;
330
331 dev->em_pd->milliwatts = milliwatts;
332
333 em_debug_create_pd(dev);
334 dev_info(dev, "EM: created perf domain\n");
335
336 unlock:
337 mutex_unlock(&em_pd_mutex);
338 return ret;
339 }
340 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
341
342 /**
343 * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
344 * @dev : Device for which the EM is registered
345 *
346 * Unregister the EM for the specified @dev (but not a CPU device).
347 */
em_dev_unregister_perf_domain(struct device * dev)348 void em_dev_unregister_perf_domain(struct device *dev)
349 {
350 if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
351 return;
352
353 if (_is_cpu_device(dev))
354 return;
355
356 /*
357 * The mutex separates all register/unregister requests and protects
358 * from potential clean-up/setup issues in the debugfs directories.
359 * The debugfs directory name is the same as device's name.
360 */
361 mutex_lock(&em_pd_mutex);
362 em_debug_remove_pd(dev);
363
364 kfree(dev->em_pd->table);
365 kfree(dev->em_pd);
366 dev->em_pd = NULL;
367 mutex_unlock(&em_pd_mutex);
368 }
369 EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);
370