xref: /openbmc/linux/kernel/power/energy_model.c (revision 75a3a99a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Energy Model of devices
4  *
5  * Copyright (c) 2018-2021, 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/cpufreq.h>
14 #include <linux/cpumask.h>
15 #include <linux/debugfs.h>
16 #include <linux/energy_model.h>
17 #include <linux/sched/topology.h>
18 #include <linux/slab.h>
19 
20 /*
21  * Mutex serializing the registrations of performance domains and letting
22  * callbacks defined by drivers sleep.
23  */
24 static DEFINE_MUTEX(em_pd_mutex);
25 
26 static bool _is_cpu_device(struct device *dev)
27 {
28 	return (dev->bus == &cpu_subsys);
29 }
30 
31 #ifdef CONFIG_DEBUG_FS
32 static struct dentry *rootdir;
33 
34 static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
35 {
36 	struct dentry *d;
37 	char name[24];
38 
39 	snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
40 
41 	/* Create per-ps directory */
42 	d = debugfs_create_dir(name, pd);
43 	debugfs_create_ulong("frequency", 0444, d, &ps->frequency);
44 	debugfs_create_ulong("power", 0444, d, &ps->power);
45 	debugfs_create_ulong("cost", 0444, d, &ps->cost);
46 	debugfs_create_ulong("inefficient", 0444, d, &ps->flags);
47 }
48 
49 static int em_debug_cpus_show(struct seq_file *s, void *unused)
50 {
51 	seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
52 
53 	return 0;
54 }
55 DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
56 
57 static int em_debug_units_show(struct seq_file *s, void *unused)
58 {
59 	struct em_perf_domain *pd = s->private;
60 	char *units = (pd->flags & EM_PERF_DOMAIN_MILLIWATTS) ?
61 		"milliWatts" : "bogoWatts";
62 
63 	seq_printf(s, "%s\n", units);
64 
65 	return 0;
66 }
67 DEFINE_SHOW_ATTRIBUTE(em_debug_units);
68 
69 static int em_debug_skip_inefficiencies_show(struct seq_file *s, void *unused)
70 {
71 	struct em_perf_domain *pd = s->private;
72 	int enabled = (pd->flags & EM_PERF_DOMAIN_SKIP_INEFFICIENCIES) ? 1 : 0;
73 
74 	seq_printf(s, "%d\n", enabled);
75 
76 	return 0;
77 }
78 DEFINE_SHOW_ATTRIBUTE(em_debug_skip_inefficiencies);
79 
80 static void em_debug_create_pd(struct device *dev)
81 {
82 	struct dentry *d;
83 	int i;
84 
85 	/* Create the directory of the performance domain */
86 	d = debugfs_create_dir(dev_name(dev), rootdir);
87 
88 	if (_is_cpu_device(dev))
89 		debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
90 				    &em_debug_cpus_fops);
91 
92 	debugfs_create_file("units", 0444, d, dev->em_pd, &em_debug_units_fops);
93 	debugfs_create_file("skip-inefficiencies", 0444, d, dev->em_pd,
94 			    &em_debug_skip_inefficiencies_fops);
95 
96 	/* Create a sub-directory for each performance state */
97 	for (i = 0; i < dev->em_pd->nr_perf_states; i++)
98 		em_debug_create_ps(&dev->em_pd->table[i], d);
99 
100 }
101 
102 static void em_debug_remove_pd(struct device *dev)
103 {
104 	struct dentry *debug_dir;
105 
106 	debug_dir = debugfs_lookup(dev_name(dev), rootdir);
107 	debugfs_remove_recursive(debug_dir);
108 }
109 
110 static int __init em_debug_init(void)
111 {
112 	/* Create /sys/kernel/debug/energy_model directory */
113 	rootdir = debugfs_create_dir("energy_model", NULL);
114 
115 	return 0;
116 }
117 fs_initcall(em_debug_init);
118 #else /* CONFIG_DEBUG_FS */
119 static void em_debug_create_pd(struct device *dev) {}
120 static void em_debug_remove_pd(struct device *dev) {}
121 #endif
122 
123 static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
124 				int nr_states, struct em_data_callback *cb,
125 				unsigned long flags)
126 {
127 	unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
128 	struct em_perf_state *table;
129 	int i, ret;
130 	u64 fmax;
131 
132 	table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
133 	if (!table)
134 		return -ENOMEM;
135 
136 	/* Build the list of performance states for this performance domain */
137 	for (i = 0, freq = 0; i < nr_states; i++, freq++) {
138 		/*
139 		 * active_power() is a driver callback which ceils 'freq' to
140 		 * lowest performance state of 'dev' above 'freq' and updates
141 		 * 'power' and 'freq' accordingly.
142 		 */
143 		ret = cb->active_power(dev, &power, &freq);
144 		if (ret) {
145 			dev_err(dev, "EM: invalid perf. state: %d\n",
146 				ret);
147 			goto free_ps_table;
148 		}
149 
150 		/*
151 		 * We expect the driver callback to increase the frequency for
152 		 * higher performance states.
153 		 */
154 		if (freq <= prev_freq) {
155 			dev_err(dev, "EM: non-increasing freq: %lu\n",
156 				freq);
157 			goto free_ps_table;
158 		}
159 
160 		/*
161 		 * The power returned by active_state() is expected to be
162 		 * positive and to fit into 16 bits.
163 		 */
164 		if (!power || power > EM_MAX_POWER) {
165 			dev_err(dev, "EM: invalid power: %lu\n",
166 				power);
167 			goto free_ps_table;
168 		}
169 
170 		table[i].power = power;
171 		table[i].frequency = prev_freq = freq;
172 	}
173 
174 	/* Compute the cost of each performance state. */
175 	fmax = (u64) table[nr_states - 1].frequency;
176 	for (i = nr_states - 1; i >= 0; i--) {
177 		unsigned long power_res, cost;
178 
179 		if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
180 			ret = cb->get_cost(dev, table[i].frequency, &cost);
181 			if (ret || !cost || cost > EM_MAX_POWER) {
182 				dev_err(dev, "EM: invalid cost %lu %d\n",
183 					cost, ret);
184 				goto free_ps_table;
185 			}
186 		} else {
187 			power_res = em_scale_power(table[i].power);
188 			cost = div64_u64(fmax * power_res, table[i].frequency);
189 		}
190 
191 		table[i].cost = cost;
192 
193 		if (table[i].cost >= prev_cost) {
194 			table[i].flags = EM_PERF_STATE_INEFFICIENT;
195 			dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
196 				table[i].frequency);
197 		} else {
198 			prev_cost = table[i].cost;
199 		}
200 	}
201 
202 	pd->table = table;
203 	pd->nr_perf_states = nr_states;
204 
205 	return 0;
206 
207 free_ps_table:
208 	kfree(table);
209 	return -EINVAL;
210 }
211 
212 static int em_create_pd(struct device *dev, int nr_states,
213 			struct em_data_callback *cb, cpumask_t *cpus,
214 			unsigned long flags)
215 {
216 	struct em_perf_domain *pd;
217 	struct device *cpu_dev;
218 	int cpu, ret;
219 
220 	if (_is_cpu_device(dev)) {
221 		pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
222 		if (!pd)
223 			return -ENOMEM;
224 
225 		cpumask_copy(em_span_cpus(pd), cpus);
226 	} else {
227 		pd = kzalloc(sizeof(*pd), GFP_KERNEL);
228 		if (!pd)
229 			return -ENOMEM;
230 	}
231 
232 	ret = em_create_perf_table(dev, pd, nr_states, cb, flags);
233 	if (ret) {
234 		kfree(pd);
235 		return ret;
236 	}
237 
238 	if (_is_cpu_device(dev))
239 		for_each_cpu(cpu, cpus) {
240 			cpu_dev = get_cpu_device(cpu);
241 			cpu_dev->em_pd = pd;
242 		}
243 
244 	dev->em_pd = pd;
245 
246 	return 0;
247 }
248 
249 static void em_cpufreq_update_efficiencies(struct device *dev)
250 {
251 	struct em_perf_domain *pd = dev->em_pd;
252 	struct em_perf_state *table;
253 	struct cpufreq_policy *policy;
254 	int found = 0;
255 	int i;
256 
257 	if (!_is_cpu_device(dev) || !pd)
258 		return;
259 
260 	policy = cpufreq_cpu_get(cpumask_first(em_span_cpus(pd)));
261 	if (!policy) {
262 		dev_warn(dev, "EM: Access to CPUFreq policy failed");
263 		return;
264 	}
265 
266 	table = pd->table;
267 
268 	for (i = 0; i < pd->nr_perf_states; i++) {
269 		if (!(table[i].flags & EM_PERF_STATE_INEFFICIENT))
270 			continue;
271 
272 		if (!cpufreq_table_set_inefficient(policy, table[i].frequency))
273 			found++;
274 	}
275 
276 	if (!found)
277 		return;
278 
279 	/*
280 	 * Efficiencies have been installed in CPUFreq, inefficient frequencies
281 	 * will be skipped. The EM can do the same.
282 	 */
283 	pd->flags |= EM_PERF_DOMAIN_SKIP_INEFFICIENCIES;
284 }
285 
286 /**
287  * em_pd_get() - Return the performance domain for a device
288  * @dev : Device to find the performance domain for
289  *
290  * Returns the performance domain to which @dev belongs, or NULL if it doesn't
291  * exist.
292  */
293 struct em_perf_domain *em_pd_get(struct device *dev)
294 {
295 	if (IS_ERR_OR_NULL(dev))
296 		return NULL;
297 
298 	return dev->em_pd;
299 }
300 EXPORT_SYMBOL_GPL(em_pd_get);
301 
302 /**
303  * em_cpu_get() - Return the performance domain for a CPU
304  * @cpu : CPU to find the performance domain for
305  *
306  * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
307  * exist.
308  */
309 struct em_perf_domain *em_cpu_get(int cpu)
310 {
311 	struct device *cpu_dev;
312 
313 	cpu_dev = get_cpu_device(cpu);
314 	if (!cpu_dev)
315 		return NULL;
316 
317 	return em_pd_get(cpu_dev);
318 }
319 EXPORT_SYMBOL_GPL(em_cpu_get);
320 
321 /**
322  * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
323  * @dev		: Device for which the EM is to register
324  * @nr_states	: Number of performance states to register
325  * @cb		: Callback functions providing the data of the Energy Model
326  * @cpus	: Pointer to cpumask_t, which in case of a CPU device is
327  *		obligatory. It can be taken from i.e. 'policy->cpus'. For other
328  *		type of devices this should be set to NULL.
329  * @milliwatts	: Flag indicating that the power values are in milliWatts or
330  *		in some other scale. It must be set properly.
331  *
332  * Create Energy Model tables for a performance domain using the callbacks
333  * defined in cb.
334  *
335  * The @milliwatts is important to set with correct value. Some kernel
336  * sub-systems might rely on this flag and check if all devices in the EM are
337  * using the same scale.
338  *
339  * If multiple clients register the same performance domain, all but the first
340  * registration will be ignored.
341  *
342  * Return 0 on success
343  */
344 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
345 				struct em_data_callback *cb, cpumask_t *cpus,
346 				bool milliwatts)
347 {
348 	unsigned long cap, prev_cap = 0;
349 	unsigned long flags = 0;
350 	int cpu, ret;
351 
352 	if (!dev || !nr_states || !cb)
353 		return -EINVAL;
354 
355 	/*
356 	 * Use a mutex to serialize the registration of performance domains and
357 	 * let the driver-defined callback functions sleep.
358 	 */
359 	mutex_lock(&em_pd_mutex);
360 
361 	if (dev->em_pd) {
362 		ret = -EEXIST;
363 		goto unlock;
364 	}
365 
366 	if (_is_cpu_device(dev)) {
367 		if (!cpus) {
368 			dev_err(dev, "EM: invalid CPU mask\n");
369 			ret = -EINVAL;
370 			goto unlock;
371 		}
372 
373 		for_each_cpu(cpu, cpus) {
374 			if (em_cpu_get(cpu)) {
375 				dev_err(dev, "EM: exists for CPU%d\n", cpu);
376 				ret = -EEXIST;
377 				goto unlock;
378 			}
379 			/*
380 			 * All CPUs of a domain must have the same
381 			 * micro-architecture since they all share the same
382 			 * table.
383 			 */
384 			cap = arch_scale_cpu_capacity(cpu);
385 			if (prev_cap && prev_cap != cap) {
386 				dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
387 					cpumask_pr_args(cpus));
388 
389 				ret = -EINVAL;
390 				goto unlock;
391 			}
392 			prev_cap = cap;
393 		}
394 	}
395 
396 	if (milliwatts)
397 		flags |= EM_PERF_DOMAIN_MILLIWATTS;
398 	else if (cb->get_cost)
399 		flags |= EM_PERF_DOMAIN_ARTIFICIAL;
400 
401 	ret = em_create_pd(dev, nr_states, cb, cpus, flags);
402 	if (ret)
403 		goto unlock;
404 
405 	dev->em_pd->flags |= flags;
406 
407 	em_cpufreq_update_efficiencies(dev);
408 
409 	em_debug_create_pd(dev);
410 	dev_info(dev, "EM: created perf domain\n");
411 
412 unlock:
413 	mutex_unlock(&em_pd_mutex);
414 	return ret;
415 }
416 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
417 
418 /**
419  * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
420  * @dev		: Device for which the EM is registered
421  *
422  * Unregister the EM for the specified @dev (but not a CPU device).
423  */
424 void em_dev_unregister_perf_domain(struct device *dev)
425 {
426 	if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
427 		return;
428 
429 	if (_is_cpu_device(dev))
430 		return;
431 
432 	/*
433 	 * The mutex separates all register/unregister requests and protects
434 	 * from potential clean-up/setup issues in the debugfs directories.
435 	 * The debugfs directory name is the same as device's name.
436 	 */
437 	mutex_lock(&em_pd_mutex);
438 	em_debug_remove_pd(dev);
439 
440 	kfree(dev->em_pd->table);
441 	kfree(dev->em_pd);
442 	dev->em_pd = NULL;
443 	mutex_unlock(&em_pd_mutex);
444 }
445 EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);
446