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