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