Merge tag 'acpi-5.18-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-microblaze.git] / include / linux / energy_model.h
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>
12
13 /**
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
20  * @flags:      see "em_perf_state flags" description below.
21  */
22 struct em_perf_state {
23         unsigned long frequency;
24         unsigned long power;
25         unsigned long cost;
26         unsigned long flags;
27 };
28
29 /*
30  * em_perf_state flags:
31  *
32  * EM_PERF_STATE_INEFFICIENT: The performance state is inefficient. There is
33  * in this em_perf_domain, another performance state with a higher frequency
34  * but a lower or equal power cost. Such inefficient states are ignored when
35  * using em_pd_get_efficient_*() functions.
36  */
37 #define EM_PERF_STATE_INEFFICIENT BIT(0)
38
39 /**
40  * struct em_perf_domain - Performance domain
41  * @table:              List of performance states, in ascending order
42  * @nr_perf_states:     Number of performance states
43  * @flags:              See "em_perf_domain flags"
44  * @cpus:               Cpumask covering the CPUs of the domain. It's here
45  *                      for performance reasons to avoid potential cache
46  *                      misses during energy calculations in the scheduler
47  *                      and simplifies allocating/freeing that memory region.
48  *
49  * In case of CPU device, a "performance domain" represents a group of CPUs
50  * whose performance is scaled together. All CPUs of a performance domain
51  * must have the same micro-architecture. Performance domains often have
52  * a 1-to-1 mapping with CPUFreq policies. In case of other devices the @cpus
53  * field is unused.
54  */
55 struct em_perf_domain {
56         struct em_perf_state *table;
57         int nr_perf_states;
58         unsigned long flags;
59         unsigned long cpus[];
60 };
61
62 /*
63  *  em_perf_domain flags:
64  *
65  *  EM_PERF_DOMAIN_MILLIWATTS: The power values are in milli-Watts or some
66  *  other scale.
67  *
68  *  EM_PERF_DOMAIN_SKIP_INEFFICIENCIES: Skip inefficient states when estimating
69  *  energy consumption.
70  */
71 #define EM_PERF_DOMAIN_MILLIWATTS BIT(0)
72 #define EM_PERF_DOMAIN_SKIP_INEFFICIENCIES BIT(1)
73
74 #define em_span_cpus(em) (to_cpumask((em)->cpus))
75
76 #ifdef CONFIG_ENERGY_MODEL
77 #define EM_MAX_POWER 0xFFFF
78
79 /*
80  * Increase resolution of energy estimation calculations for 64-bit
81  * architectures. The extra resolution improves decision made by EAS for the
82  * task placement when two Performance Domains might provide similar energy
83  * estimation values (w/o better resolution the values could be equal).
84  *
85  * We increase resolution only if we have enough bits to allow this increased
86  * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
87  * are pretty high and the returns do not justify the increased costs.
88  */
89 #ifdef CONFIG_64BIT
90 #define em_scale_power(p) ((p) * 1000)
91 #else
92 #define em_scale_power(p) (p)
93 #endif
94
95 struct em_data_callback {
96         /**
97          * active_power() - Provide power at the next performance state of
98          *              a device
99          * @power       : Active power at the performance state
100          *              (modified)
101          * @freq        : Frequency at the performance state in kHz
102          *              (modified)
103          * @dev         : Device for which we do this operation (can be a CPU)
104          *
105          * active_power() must find the lowest performance state of 'dev' above
106          * 'freq' and update 'power' and 'freq' to the matching active power
107          * and frequency.
108          *
109          * In case of CPUs, the power is the one of a single CPU in the domain,
110          * expressed in milli-Watts or an abstract scale. It is expected to
111          * fit in the [0, EM_MAX_POWER] range.
112          *
113          * Return 0 on success.
114          */
115         int (*active_power)(unsigned long *power, unsigned long *freq,
116                             struct device *dev);
117 };
118 #define EM_DATA_CB(_active_power_cb) { .active_power = &_active_power_cb }
119 #define EM_SET_ACTIVE_POWER_CB(em_cb, cb) ((em_cb).active_power = cb)
120
121 struct em_perf_domain *em_cpu_get(int cpu);
122 struct em_perf_domain *em_pd_get(struct device *dev);
123 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
124                                 struct em_data_callback *cb, cpumask_t *span,
125                                 bool milliwatts);
126 void em_dev_unregister_perf_domain(struct device *dev);
127
128 /**
129  * em_pd_get_efficient_state() - Get an efficient performance state from the EM
130  * @pd   : Performance domain for which we want an efficient frequency
131  * @freq : Frequency to map with the EM
132  *
133  * It is called from the scheduler code quite frequently and as a consequence
134  * doesn't implement any check.
135  *
136  * Return: An efficient performance state, high enough to meet @freq
137  * requirement.
138  */
139 static inline
140 struct em_perf_state *em_pd_get_efficient_state(struct em_perf_domain *pd,
141                                                 unsigned long freq)
142 {
143         struct em_perf_state *ps;
144         int i;
145
146         for (i = 0; i < pd->nr_perf_states; i++) {
147                 ps = &pd->table[i];
148                 if (ps->frequency >= freq) {
149                         if (pd->flags & EM_PERF_DOMAIN_SKIP_INEFFICIENCIES &&
150                             ps->flags & EM_PERF_STATE_INEFFICIENT)
151                                 continue;
152                         break;
153                 }
154         }
155
156         return ps;
157 }
158
159 /**
160  * em_cpu_energy() - Estimates the energy consumed by the CPUs of a
161  *              performance domain
162  * @pd          : performance domain for which energy has to be estimated
163  * @max_util    : highest utilization among CPUs of the domain
164  * @sum_util    : sum of the utilization of all CPUs in the domain
165  * @allowed_cpu_cap     : maximum allowed CPU capacity for the @pd, which
166  *                        might reflect reduced frequency (due to thermal)
167  *
168  * This function must be used only for CPU devices. There is no validation,
169  * i.e. if the EM is a CPU type and has cpumask allocated. It is called from
170  * the scheduler code quite frequently and that is why there is not checks.
171  *
172  * Return: the sum of the energy consumed by the CPUs of the domain assuming
173  * a capacity state satisfying the max utilization of the domain.
174  */
175 static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
176                                 unsigned long max_util, unsigned long sum_util,
177                                 unsigned long allowed_cpu_cap)
178 {
179         unsigned long freq, scale_cpu;
180         struct em_perf_state *ps;
181         int cpu;
182
183         if (!sum_util)
184                 return 0;
185
186         /*
187          * In order to predict the performance state, map the utilization of
188          * the most utilized CPU of the performance domain to a requested
189          * frequency, like schedutil. Take also into account that the real
190          * frequency might be set lower (due to thermal capping). Thus, clamp
191          * max utilization to the allowed CPU capacity before calculating
192          * effective frequency.
193          */
194         cpu = cpumask_first(to_cpumask(pd->cpus));
195         scale_cpu = arch_scale_cpu_capacity(cpu);
196         ps = &pd->table[pd->nr_perf_states - 1];
197
198         max_util = map_util_perf(max_util);
199         max_util = min(max_util, allowed_cpu_cap);
200         freq = map_util_freq(max_util, ps->frequency, scale_cpu);
201
202         /*
203          * Find the lowest performance state of the Energy Model above the
204          * requested frequency.
205          */
206         ps = em_pd_get_efficient_state(pd, freq);
207
208         /*
209          * The capacity of a CPU in the domain at the performance state (ps)
210          * can be computed as:
211          *
212          *             ps->freq * scale_cpu
213          *   ps->cap = --------------------                          (1)
214          *                 cpu_max_freq
215          *
216          * So, ignoring the costs of idle states (which are not available in
217          * the EM), the energy consumed by this CPU at that performance state
218          * is estimated as:
219          *
220          *             ps->power * cpu_util
221          *   cpu_nrg = --------------------                          (2)
222          *                   ps->cap
223          *
224          * since 'cpu_util / ps->cap' represents its percentage of busy time.
225          *
226          *   NOTE: Although the result of this computation actually is in
227          *         units of power, it can be manipulated as an energy value
228          *         over a scheduling period, since it is assumed to be
229          *         constant during that interval.
230          *
231          * By injecting (1) in (2), 'cpu_nrg' can be re-expressed as a product
232          * of two terms:
233          *
234          *             ps->power * cpu_max_freq   cpu_util
235          *   cpu_nrg = ------------------------ * ---------          (3)
236          *                    ps->freq            scale_cpu
237          *
238          * The first term is static, and is stored in the em_perf_state struct
239          * as 'ps->cost'.
240          *
241          * Since all CPUs of the domain have the same micro-architecture, they
242          * share the same 'ps->cost', and the same CPU capacity. Hence, the
243          * total energy of the domain (which is the simple sum of the energy of
244          * all of its CPUs) can be factorized as:
245          *
246          *            ps->cost * \Sum cpu_util
247          *   pd_nrg = ------------------------                       (4)
248          *                  scale_cpu
249          */
250         return ps->cost * sum_util / scale_cpu;
251 }
252
253 /**
254  * em_pd_nr_perf_states() - Get the number of performance states of a perf.
255  *                              domain
256  * @pd          : performance domain for which this must be done
257  *
258  * Return: the number of performance states in the performance domain table
259  */
260 static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
261 {
262         return pd->nr_perf_states;
263 }
264
265 #else
266 struct em_data_callback {};
267 #define EM_DATA_CB(_active_power_cb) { }
268 #define EM_SET_ACTIVE_POWER_CB(em_cb, cb) do { } while (0)
269
270 static inline
271 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
272                                 struct em_data_callback *cb, cpumask_t *span,
273                                 bool milliwatts)
274 {
275         return -EINVAL;
276 }
277 static inline void em_dev_unregister_perf_domain(struct device *dev)
278 {
279 }
280 static inline struct em_perf_domain *em_cpu_get(int cpu)
281 {
282         return NULL;
283 }
284 static inline struct em_perf_domain *em_pd_get(struct device *dev)
285 {
286         return NULL;
287 }
288 static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
289                         unsigned long max_util, unsigned long sum_util,
290                         unsigned long allowed_cpu_cap)
291 {
292         return 0;
293 }
294 static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
295 {
296         return 0;
297 }
298 #endif
299
300 #endif