1 // SPDX-License-Identifier: GPL-2.0
3 * Energy Model of devices
5 * Copyright (c) 2018-2020, Arm ltd.
6 * Written by: Quentin Perret, Arm ltd.
7 * Improvements provided by: Lukasz Luba, Arm ltd.
10 #define pr_fmt(fmt) "energy_model: " fmt
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>
20 * Mutex serializing the registrations of performance domains and letting
21 * callbacks defined by drivers sleep.
23 static DEFINE_MUTEX(em_pd_mutex);
25 static bool _is_cpu_device(struct device *dev)
27 return (dev->bus == &cpu_subsys);
30 #ifdef CONFIG_DEBUG_FS
31 static struct dentry *rootdir;
33 static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
38 snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
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);
47 static int em_debug_cpus_show(struct seq_file *s, void *unused)
49 seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
53 DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
55 static int em_debug_units_show(struct seq_file *s, void *unused)
57 struct em_perf_domain *pd = s->private;
58 char *units = pd->milliwatts ? "milliWatts" : "bogoWatts";
60 seq_printf(s, "%s\n", units);
64 DEFINE_SHOW_ATTRIBUTE(em_debug_units);
66 static void em_debug_create_pd(struct device *dev)
71 /* Create the directory of the performance domain */
72 d = debugfs_create_dir(dev_name(dev), rootdir);
74 if (_is_cpu_device(dev))
75 debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
78 debugfs_create_file("units", 0444, d, dev->em_pd, &em_debug_units_fops);
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);
86 static void em_debug_remove_pd(struct device *dev)
88 debugfs_lookup_and_remove(dev_name(dev), rootdir);
91 static int __init em_debug_init(void)
93 /* Create /sys/kernel/debug/energy_model directory */
94 rootdir = debugfs_create_dir("energy_model", NULL);
98 fs_initcall(em_debug_init);
99 #else /* CONFIG_DEBUG_FS */
100 static void em_debug_create_pd(struct device *dev) {}
101 static void em_debug_remove_pd(struct device *dev) {}
104 static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
105 int nr_states, struct em_data_callback *cb)
107 unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
108 struct em_perf_state *table;
112 table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
116 /* Build the list of performance states for this performance domain */
117 for (i = 0, freq = 0; i < nr_states; i++, freq++) {
119 * active_power() is a driver callback which ceils 'freq' to
120 * lowest performance state of 'dev' above 'freq' and updates
121 * 'power' and 'freq' accordingly.
123 ret = cb->active_power(&power, &freq, dev);
125 dev_err(dev, "EM: invalid perf. state: %d\n",
131 * We expect the driver callback to increase the frequency for
132 * higher performance states.
134 if (freq <= prev_freq) {
135 dev_err(dev, "EM: non-increasing freq: %lu\n",
141 * The power returned by active_state() is expected to be
142 * positive and to fit into 16 bits.
144 if (!power || power > EM_MAX_POWER) {
145 dev_err(dev, "EM: invalid power: %lu\n",
150 table[i].power = power;
151 table[i].frequency = prev_freq = freq;
154 /* Compute the cost of each performance state. */
155 fmax = (u64) table[nr_states - 1].frequency;
156 for (i = nr_states - 1; i >= 0; i--) {
157 unsigned long power_res = em_scale_power(table[i].power);
159 table[i].cost = div64_u64(fmax * power_res,
161 if (table[i].cost >= prev_cost) {
162 dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
165 prev_cost = table[i].cost;
170 pd->nr_perf_states = nr_states;
179 static int em_create_pd(struct device *dev, int nr_states,
180 struct em_data_callback *cb, cpumask_t *cpus)
182 struct em_perf_domain *pd;
183 struct device *cpu_dev;
186 if (_is_cpu_device(dev)) {
187 pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
191 cpumask_copy(em_span_cpus(pd), cpus);
193 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
198 ret = em_create_perf_table(dev, pd, nr_states, cb);
204 if (_is_cpu_device(dev))
205 for_each_cpu(cpu, cpus) {
206 cpu_dev = get_cpu_device(cpu);
216 * em_pd_get() - Return the performance domain for a device
217 * @dev : Device to find the performance domain for
219 * Returns the performance domain to which @dev belongs, or NULL if it doesn't
222 struct em_perf_domain *em_pd_get(struct device *dev)
224 if (IS_ERR_OR_NULL(dev))
229 EXPORT_SYMBOL_GPL(em_pd_get);
232 * em_cpu_get() - Return the performance domain for a CPU
233 * @cpu : CPU to find the performance domain for
235 * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
238 struct em_perf_domain *em_cpu_get(int cpu)
240 struct device *cpu_dev;
242 cpu_dev = get_cpu_device(cpu);
246 return em_pd_get(cpu_dev);
248 EXPORT_SYMBOL_GPL(em_cpu_get);
251 * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
252 * @dev : Device for which the EM is to register
253 * @nr_states : Number of performance states to register
254 * @cb : Callback functions providing the data of the Energy Model
255 * @cpus : Pointer to cpumask_t, which in case of a CPU device is
256 * obligatory. It can be taken from i.e. 'policy->cpus'. For other
257 * type of devices this should be set to NULL.
258 * @milliwatts : Flag indicating that the power values are in milliWatts or
259 * in some other scale. It must be set properly.
261 * Create Energy Model tables for a performance domain using the callbacks
264 * The @milliwatts is important to set with correct value. Some kernel
265 * sub-systems might rely on this flag and check if all devices in the EM are
266 * using the same scale.
268 * If multiple clients register the same performance domain, all but the first
269 * registration will be ignored.
271 * Return 0 on success
273 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
274 struct em_data_callback *cb, cpumask_t *cpus,
277 unsigned long cap, prev_cap = 0;
280 if (!dev || !nr_states || !cb)
284 * Use a mutex to serialize the registration of performance domains and
285 * let the driver-defined callback functions sleep.
287 mutex_lock(&em_pd_mutex);
294 if (_is_cpu_device(dev)) {
296 dev_err(dev, "EM: invalid CPU mask\n");
301 for_each_cpu(cpu, cpus) {
302 if (em_cpu_get(cpu)) {
303 dev_err(dev, "EM: exists for CPU%d\n", cpu);
308 * All CPUs of a domain must have the same
309 * micro-architecture since they all share the same
312 cap = arch_scale_cpu_capacity(cpu);
313 if (prev_cap && prev_cap != cap) {
314 dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
315 cpumask_pr_args(cpus));
324 ret = em_create_pd(dev, nr_states, cb, cpus);
328 dev->em_pd->milliwatts = milliwatts;
330 em_debug_create_pd(dev);
331 dev_info(dev, "EM: created perf domain\n");
334 mutex_unlock(&em_pd_mutex);
337 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
340 * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
341 * @dev : Device for which the EM is registered
343 * Unregister the EM for the specified @dev (but not a CPU device).
345 void em_dev_unregister_perf_domain(struct device *dev)
347 if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
350 if (_is_cpu_device(dev))
354 * The mutex separates all register/unregister requests and protects
355 * from potential clean-up/setup issues in the debugfs directories.
356 * The debugfs directory name is the same as device's name.
358 mutex_lock(&em_pd_mutex);
359 em_debug_remove_pd(dev);
361 kfree(dev->em_pd->table);
364 mutex_unlock(&em_pd_mutex);
366 EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);