1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_ENERGY_MODEL_H
3 #define _LINUX_ENERGY_MODEL_H
4 #include <linux/cpumask.h>
5 #include <linux/device.h>
6 #include <linux/jump_label.h>
7 #include <linux/kobject.h>
8 #include <linux/rcupdate.h>
9 #include <linux/sched/cpufreq.h>
10 #include <linux/sched/topology.h>
11 #include <linux/types.h>
14 * struct em_perf_state - Performance state of a performance domain
15 * @frequency: The frequency in KHz, for consistency with CPUFreq
16 * @power: The power consumed at this level (by 1 CPU or by a registered
17 * device). It can be a total power: static and dynamic.
18 * @cost: The cost coefficient associated with this level, used during
19 * energy calculation. Equal to: power * max_frequency / frequency
21 struct em_perf_state {
22 unsigned long frequency;
28 * struct em_perf_domain - Performance domain
29 * @table: List of performance states, in ascending order
30 * @nr_perf_states: Number of performance states
31 * @milliwatts: Flag indicating the power values are in milli-Watts
32 * or some other scale.
33 * @cpus: Cpumask covering the CPUs of the domain. It's here
34 * for performance reasons to avoid potential cache
35 * misses during energy calculations in the scheduler
36 * and simplifies allocating/freeing that memory region.
38 * In case of CPU device, a "performance domain" represents a group of CPUs
39 * whose performance is scaled together. All CPUs of a performance domain
40 * must have the same micro-architecture. Performance domains often have
41 * a 1-to-1 mapping with CPUFreq policies. In case of other devices the @cpus
44 struct em_perf_domain {
45 struct em_perf_state *table;
51 #define em_span_cpus(em) (to_cpumask((em)->cpus))
53 #ifdef CONFIG_ENERGY_MODEL
54 #define EM_MAX_POWER 0xFFFF
57 * Increase resolution of energy estimation calculations for 64-bit
58 * architectures. The extra resolution improves decision made by EAS for the
59 * task placement when two Performance Domains might provide similar energy
60 * estimation values (w/o better resolution the values could be equal).
62 * We increase resolution only if we have enough bits to allow this increased
63 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
64 * are pretty high and the returns do not justify the increased costs.
67 #define em_scale_power(p) ((p) * 1000)
69 #define em_scale_power(p) (p)
72 struct em_data_callback {
74 * active_power() - Provide power at the next performance state of
76 * @power : Active power at the performance state
78 * @freq : Frequency at the performance state in kHz
80 * @dev : Device for which we do this operation (can be a CPU)
82 * active_power() must find the lowest performance state of 'dev' above
83 * 'freq' and update 'power' and 'freq' to the matching active power
86 * In case of CPUs, the power is the one of a single CPU in the domain,
87 * expressed in milli-Watts or an abstract scale. It is expected to
88 * fit in the [0, EM_MAX_POWER] range.
90 * Return 0 on success.
92 int (*active_power)(unsigned long *power, unsigned long *freq,
95 #define EM_DATA_CB(_active_power_cb) { .active_power = &_active_power_cb }
97 struct em_perf_domain *em_cpu_get(int cpu);
98 struct em_perf_domain *em_pd_get(struct device *dev);
99 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
100 struct em_data_callback *cb, cpumask_t *span,
102 void em_dev_unregister_perf_domain(struct device *dev);
105 * em_cpu_energy() - Estimates the energy consumed by the CPUs of a
107 * @pd : performance domain for which energy has to be estimated
108 * @max_util : highest utilization among CPUs of the domain
109 * @sum_util : sum of the utilization of all CPUs in the domain
110 * @allowed_cpu_cap : maximum allowed CPU capacity for the @pd, which
111 * might reflect reduced frequency (due to thermal)
113 * This function must be used only for CPU devices. There is no validation,
114 * i.e. if the EM is a CPU type and has cpumask allocated. It is called from
115 * the scheduler code quite frequently and that is why there is not checks.
117 * Return: the sum of the energy consumed by the CPUs of the domain assuming
118 * a capacity state satisfying the max utilization of the domain.
120 static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
121 unsigned long max_util, unsigned long sum_util,
122 unsigned long allowed_cpu_cap)
124 unsigned long freq, scale_cpu;
125 struct em_perf_state *ps;
132 * In order to predict the performance state, map the utilization of
133 * the most utilized CPU of the performance domain to a requested
134 * frequency, like schedutil. Take also into account that the real
135 * frequency might be set lower (due to thermal capping). Thus, clamp
136 * max utilization to the allowed CPU capacity before calculating
137 * effective frequency.
139 cpu = cpumask_first(to_cpumask(pd->cpus));
140 scale_cpu = arch_scale_cpu_capacity(cpu);
141 ps = &pd->table[pd->nr_perf_states - 1];
143 max_util = map_util_perf(max_util);
144 max_util = min(max_util, allowed_cpu_cap);
145 freq = map_util_freq(max_util, ps->frequency, scale_cpu);
148 * Find the lowest performance state of the Energy Model above the
149 * requested frequency.
151 for (i = 0; i < pd->nr_perf_states; i++) {
153 if (ps->frequency >= freq)
158 * The capacity of a CPU in the domain at the performance state (ps)
159 * can be computed as:
161 * ps->freq * scale_cpu
162 * ps->cap = -------------------- (1)
165 * So, ignoring the costs of idle states (which are not available in
166 * the EM), the energy consumed by this CPU at that performance state
169 * ps->power * cpu_util
170 * cpu_nrg = -------------------- (2)
173 * since 'cpu_util / ps->cap' represents its percentage of busy time.
175 * NOTE: Although the result of this computation actually is in
176 * units of power, it can be manipulated as an energy value
177 * over a scheduling period, since it is assumed to be
178 * constant during that interval.
180 * By injecting (1) in (2), 'cpu_nrg' can be re-expressed as a product
183 * ps->power * cpu_max_freq cpu_util
184 * cpu_nrg = ------------------------ * --------- (3)
187 * The first term is static, and is stored in the em_perf_state struct
190 * Since all CPUs of the domain have the same micro-architecture, they
191 * share the same 'ps->cost', and the same CPU capacity. Hence, the
192 * total energy of the domain (which is the simple sum of the energy of
193 * all of its CPUs) can be factorized as:
195 * ps->cost * \Sum cpu_util
196 * pd_nrg = ------------------------ (4)
199 return ps->cost * sum_util / scale_cpu;
203 * em_pd_nr_perf_states() - Get the number of performance states of a perf.
205 * @pd : performance domain for which this must be done
207 * Return: the number of performance states in the performance domain table
209 static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
211 return pd->nr_perf_states;
215 struct em_data_callback {};
216 #define EM_DATA_CB(_active_power_cb) { }
219 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
220 struct em_data_callback *cb, cpumask_t *span,
225 static inline void em_dev_unregister_perf_domain(struct device *dev)
228 static inline struct em_perf_domain *em_cpu_get(int cpu)
232 static inline struct em_perf_domain *em_pd_get(struct device *dev)
236 static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
237 unsigned long max_util, unsigned long sum_util,
238 unsigned long allowed_cpu_cap)
242 static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)