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