1 // SPDX-License-Identifier: GPL-2.0
3 * Energy Model of devices
5 * Copyright (c) 2018-2021, 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/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>
21 * Mutex serializing the registrations of performance domains and letting
22 * callbacks defined by drivers sleep.
24 static DEFINE_MUTEX(em_pd_mutex);
26 static bool _is_cpu_device(struct device *dev)
28 return (dev->bus == &cpu_subsys);
31 #ifdef CONFIG_DEBUG_FS
32 static struct dentry *rootdir;
34 static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
39 snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
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);
49 static int em_debug_cpus_show(struct seq_file *s, void *unused)
51 seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
55 DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
57 static int em_debug_flags_show(struct seq_file *s, void *unused)
59 struct em_perf_domain *pd = s->private;
61 seq_printf(s, "%#lx\n", pd->flags);
65 DEFINE_SHOW_ATTRIBUTE(em_debug_flags);
67 static void em_debug_create_pd(struct device *dev)
72 /* Create the directory of the performance domain */
73 d = debugfs_create_dir(dev_name(dev), rootdir);
75 if (_is_cpu_device(dev))
76 debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
79 debugfs_create_file("flags", 0444, d, dev->em_pd,
80 &em_debug_flags_fops);
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);
88 static void em_debug_remove_pd(struct device *dev)
90 struct dentry *debug_dir;
92 debug_dir = debugfs_lookup(dev_name(dev), rootdir);
93 debugfs_remove_recursive(debug_dir);
96 static int __init em_debug_init(void)
98 /* Create /sys/kernel/debug/energy_model directory */
99 rootdir = debugfs_create_dir("energy_model", NULL);
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) {}
109 static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
110 int nr_states, struct em_data_callback *cb,
113 unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
114 struct em_perf_state *table;
118 table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
122 /* Build the list of performance states for this performance domain */
123 for (i = 0, freq = 0; i < nr_states; i++, freq++) {
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.
129 ret = cb->active_power(dev, &power, &freq);
131 dev_err(dev, "EM: invalid perf. state: %d\n",
137 * We expect the driver callback to increase the frequency for
138 * higher performance states.
140 if (freq <= prev_freq) {
141 dev_err(dev, "EM: non-increasing freq: %lu\n",
147 * The power returned by active_state() is expected to be
148 * positive and be in range.
150 if (!power || power > EM_MAX_POWER) {
151 dev_err(dev, "EM: invalid power: %lu\n",
156 table[i].power = power;
157 table[i].frequency = prev_freq = freq;
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;
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",
173 power_res = table[i].power;
174 cost = div64_u64(fmax * power_res, table[i].frequency);
177 table[i].cost = cost;
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",
184 prev_cost = table[i].cost;
189 pd->nr_perf_states = nr_states;
198 static int em_create_pd(struct device *dev, int nr_states,
199 struct em_data_callback *cb, cpumask_t *cpus,
202 struct em_perf_domain *pd;
203 struct device *cpu_dev;
204 int cpu, ret, num_cpus;
206 if (_is_cpu_device(dev)) {
207 num_cpus = cpumask_weight(cpus);
209 /* Prevent max possible energy calculation to not overflow */
210 if (num_cpus > EM_MAX_NUM_CPUS) {
211 dev_err(dev, "EM: too many CPUs, overflow possible\n");
215 pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
219 cpumask_copy(em_span_cpus(pd), cpus);
221 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
226 ret = em_create_perf_table(dev, pd, nr_states, cb, flags);
232 if (_is_cpu_device(dev))
233 for_each_cpu(cpu, cpus) {
234 cpu_dev = get_cpu_device(cpu);
243 static void em_cpufreq_update_efficiencies(struct device *dev)
245 struct em_perf_domain *pd = dev->em_pd;
246 struct em_perf_state *table;
247 struct cpufreq_policy *policy;
251 if (!_is_cpu_device(dev) || !pd)
254 policy = cpufreq_cpu_get(cpumask_first(em_span_cpus(pd)));
256 dev_warn(dev, "EM: Access to CPUFreq policy failed");
262 for (i = 0; i < pd->nr_perf_states; i++) {
263 if (!(table[i].flags & EM_PERF_STATE_INEFFICIENT))
266 if (!cpufreq_table_set_inefficient(policy, table[i].frequency))
270 cpufreq_cpu_put(policy);
276 * Efficiencies have been installed in CPUFreq, inefficient frequencies
277 * will be skipped. The EM can do the same.
279 pd->flags |= EM_PERF_DOMAIN_SKIP_INEFFICIENCIES;
283 * em_pd_get() - Return the performance domain for a device
284 * @dev : Device to find the performance domain for
286 * Returns the performance domain to which @dev belongs, or NULL if it doesn't
289 struct em_perf_domain *em_pd_get(struct device *dev)
291 if (IS_ERR_OR_NULL(dev))
296 EXPORT_SYMBOL_GPL(em_pd_get);
299 * em_cpu_get() - Return the performance domain for a CPU
300 * @cpu : CPU to find the performance domain for
302 * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
305 struct em_perf_domain *em_cpu_get(int cpu)
307 struct device *cpu_dev;
309 cpu_dev = get_cpu_device(cpu);
313 return em_pd_get(cpu_dev);
315 EXPORT_SYMBOL_GPL(em_cpu_get);
318 * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
319 * @dev : Device for which the EM is to register
320 * @nr_states : Number of performance states to register
321 * @cb : Callback functions providing the data of the Energy Model
322 * @cpus : Pointer to cpumask_t, which in case of a CPU device is
323 * obligatory. It can be taken from i.e. 'policy->cpus'. For other
324 * type of devices this should be set to NULL.
325 * @microwatts : Flag indicating that the power values are in micro-Watts or
326 * in some other scale. It must be set properly.
328 * Create Energy Model tables for a performance domain using the callbacks
331 * The @microwatts is important to set with correct value. Some kernel
332 * sub-systems might rely on this flag and check if all devices in the EM are
333 * using the same scale.
335 * If multiple clients register the same performance domain, all but the first
336 * registration will be ignored.
338 * Return 0 on success
340 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
341 struct em_data_callback *cb, cpumask_t *cpus,
344 unsigned long cap, prev_cap = 0;
345 unsigned long flags = 0;
348 if (!dev || !nr_states || !cb)
352 * Use a mutex to serialize the registration of performance domains and
353 * let the driver-defined callback functions sleep.
355 mutex_lock(&em_pd_mutex);
362 if (_is_cpu_device(dev)) {
364 dev_err(dev, "EM: invalid CPU mask\n");
369 for_each_cpu(cpu, cpus) {
370 if (em_cpu_get(cpu)) {
371 dev_err(dev, "EM: exists for CPU%d\n", cpu);
376 * All CPUs of a domain must have the same
377 * micro-architecture since they all share the same
380 cap = arch_scale_cpu_capacity(cpu);
381 if (prev_cap && prev_cap != cap) {
382 dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
383 cpumask_pr_args(cpus));
393 flags |= EM_PERF_DOMAIN_MICROWATTS;
394 else if (cb->get_cost)
395 flags |= EM_PERF_DOMAIN_ARTIFICIAL;
397 ret = em_create_pd(dev, nr_states, cb, cpus, flags);
401 dev->em_pd->flags |= flags;
403 em_cpufreq_update_efficiencies(dev);
405 em_debug_create_pd(dev);
406 dev_info(dev, "EM: created perf domain\n");
409 mutex_unlock(&em_pd_mutex);
412 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
415 * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
416 * @dev : Device for which the EM is registered
418 * Unregister the EM for the specified @dev (but not a CPU device).
420 void em_dev_unregister_perf_domain(struct device *dev)
422 if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
425 if (_is_cpu_device(dev))
429 * The mutex separates all register/unregister requests and protects
430 * from potential clean-up/setup issues in the debugfs directories.
431 * The debugfs directory name is the same as device's name.
433 mutex_lock(&em_pd_mutex);
434 em_debug_remove_pd(dev);
436 kfree(dev->em_pd->table);
439 mutex_unlock(&em_pd_mutex);
441 EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);