1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2018, Arm ltd.
6 * Written by: Quentin Perret, Arm ltd.
9 #define pr_fmt(fmt) "energy_model: " fmt
11 #include <linux/cpu.h>
12 #include <linux/cpumask.h>
13 #include <linux/debugfs.h>
14 #include <linux/energy_model.h>
15 #include <linux/sched/topology.h>
16 #include <linux/slab.h>
18 /* Mapping of each CPU to the performance domain to which it belongs. */
19 static DEFINE_PER_CPU(struct em_perf_domain *, em_data);
22 * Mutex serializing the registrations of performance domains and letting
23 * callbacks defined by drivers sleep.
25 static DEFINE_MUTEX(em_pd_mutex);
27 #ifdef CONFIG_DEBUG_FS
28 static struct dentry *rootdir;
30 static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
35 snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
37 /* Create per-ps directory */
38 d = debugfs_create_dir(name, pd);
39 debugfs_create_ulong("frequency", 0444, d, &ps->frequency);
40 debugfs_create_ulong("power", 0444, d, &ps->power);
41 debugfs_create_ulong("cost", 0444, d, &ps->cost);
44 static int em_debug_cpus_show(struct seq_file *s, void *unused)
46 seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
50 DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
52 static void em_debug_create_pd(struct em_perf_domain *pd, int cpu)
58 snprintf(name, sizeof(name), "pd%d", cpu);
60 /* Create the directory of the performance domain */
61 d = debugfs_create_dir(name, rootdir);
63 debugfs_create_file("cpus", 0444, d, pd->cpus, &em_debug_cpus_fops);
65 /* Create a sub-directory for each performance state */
66 for (i = 0; i < pd->nr_perf_states; i++)
67 em_debug_create_ps(&pd->table[i], d);
70 static int __init em_debug_init(void)
72 /* Create /sys/kernel/debug/energy_model directory */
73 rootdir = debugfs_create_dir("energy_model", NULL);
77 core_initcall(em_debug_init);
78 #else /* CONFIG_DEBUG_FS */
79 static void em_debug_create_pd(struct em_perf_domain *pd, int cpu) {}
81 static struct em_perf_domain *em_create_pd(cpumask_t *span, int nr_states,
82 struct em_data_callback *cb)
84 unsigned long opp_eff, prev_opp_eff = ULONG_MAX;
85 unsigned long power, freq, prev_freq = 0;
86 int i, ret, cpu = cpumask_first(span);
87 struct em_perf_state *table;
88 struct em_perf_domain *pd;
91 if (!cb->active_power)
94 pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
98 table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
102 /* Build the list of performance states for this performance domain */
103 for (i = 0, freq = 0; i < nr_states; i++, freq++) {
105 * active_power() is a driver callback which ceils 'freq' to
106 * lowest performance state of 'cpu' above 'freq' and updates
107 * 'power' and 'freq' accordingly.
109 ret = cb->active_power(&power, &freq, cpu);
111 pr_err("pd%d: invalid perf. state: %d\n", cpu, ret);
116 * We expect the driver callback to increase the frequency for
117 * higher performance states.
119 if (freq <= prev_freq) {
120 pr_err("pd%d: non-increasing freq: %lu\n", cpu, freq);
125 * The power returned by active_state() is expected to be
126 * positive, in milli-watts and to fit into 16 bits.
128 if (!power || power > EM_MAX_POWER) {
129 pr_err("pd%d: invalid power: %lu\n", cpu, power);
133 table[i].power = power;
134 table[i].frequency = prev_freq = freq;
137 * The hertz/watts efficiency ratio should decrease as the
138 * frequency grows on sane platforms. But this isn't always
139 * true in practice so warn the user if a higher OPP is more
140 * power efficient than a lower one.
142 opp_eff = freq / power;
143 if (opp_eff >= prev_opp_eff)
144 pr_warn("pd%d: hertz/watts ratio non-monotonically decreasing: em_perf_state %d >= em_perf_state%d\n",
146 prev_opp_eff = opp_eff;
149 /* Compute the cost of each performance state. */
150 fmax = (u64) table[nr_states - 1].frequency;
151 for (i = 0; i < nr_states; i++) {
152 table[i].cost = div64_u64(fmax * table[i].power,
157 pd->nr_perf_states = nr_states;
158 cpumask_copy(to_cpumask(pd->cpus), span);
160 em_debug_create_pd(pd, cpu);
173 * em_cpu_get() - Return the performance domain for a CPU
174 * @cpu : CPU to find the performance domain for
176 * Return: the performance domain to which 'cpu' belongs, or NULL if it doesn't
179 struct em_perf_domain *em_cpu_get(int cpu)
181 return READ_ONCE(per_cpu(em_data, cpu));
183 EXPORT_SYMBOL_GPL(em_cpu_get);
186 * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
187 * @dev : Device for which the EM is to register
188 * @nr_states : Number of performance states to register
189 * @cb : Callback functions providing the data of the Energy Model
190 * @span : Pointer to cpumask_t, which in case of a CPU device is
191 * obligatory. It can be taken from i.e. 'policy->cpus'. For other
192 * type of devices this should be set to NULL.
194 * Create Energy Model tables for a performance domain using the callbacks
197 * If multiple clients register the same performance domain, all but the first
198 * registration will be ignored.
200 * Return 0 on success
202 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
203 struct em_data_callback *cb, cpumask_t *span)
205 unsigned long cap, prev_cap = 0;
206 struct em_perf_domain *pd;
209 if (!dev || !span || !nr_states || !cb)
213 * Use a mutex to serialize the registration of performance domains and
214 * let the driver-defined callback functions sleep.
216 mutex_lock(&em_pd_mutex);
218 for_each_cpu(cpu, span) {
219 /* Make sure we don't register again an existing domain. */
220 if (READ_ONCE(per_cpu(em_data, cpu))) {
226 * All CPUs of a domain must have the same micro-architecture
227 * since they all share the same table.
229 cap = arch_scale_cpu_capacity(cpu);
230 if (prev_cap && prev_cap != cap) {
231 pr_err("CPUs of %*pbl must have the same capacity\n",
232 cpumask_pr_args(span));
239 /* Create the performance domain and add it to the Energy Model. */
240 pd = em_create_pd(span, nr_states, cb);
246 for_each_cpu(cpu, span) {
248 * The per-cpu array can be read concurrently from em_cpu_get().
249 * The barrier enforces the ordering needed to make sure readers
250 * can only access well formed em_perf_domain structs.
252 smp_store_release(per_cpu_ptr(&em_data, cpu), pd);
255 pr_debug("Created perf domain %*pbl\n", cpumask_pr_args(span));
257 mutex_unlock(&em_pd_mutex);
261 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
264 * em_register_perf_domain() - Register the Energy Model of a performance domain
265 * @span : Mask of CPUs in the performance domain
266 * @nr_states : Number of capacity states to register
267 * @cb : Callback functions providing the data of the Energy Model
269 * Create Energy Model tables for a performance domain using the callbacks
272 * If multiple clients register the same performance domain, all but the first
273 * registration will be ignored.
275 * Return 0 on success
277 int em_register_perf_domain(cpumask_t *span, unsigned int nr_states,
278 struct em_data_callback *cb)
280 struct device *cpu_dev;
282 cpu_dev = get_cpu_device(cpumask_first(span));
284 return em_dev_register_perf_domain(cpu_dev, nr_states, cb, span);
286 EXPORT_SYMBOL_GPL(em_register_perf_domain);