blob: 5f04a2b35e80b7bc5f55707ef93dbb21a767dd00 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_ENERGY_MODEL_H
3#define _LINUX_ENERGY_MODEL_H
4#include <linux/cpumask.h>
Olivier Deprez157378f2022-04-04 15:47:50 +02005#include <linux/device.h>
David Brazdil0f672f62019-12-10 10:32:29 +00006#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
David Brazdil0f672f62019-12-10 10:32:29 +000013/**
Olivier Deprez157378f2022-04-04 15:47:50 +020014 * 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, in milli-watts (by 1 CPU or
17 by a registered device). It can be a total power: static and
18 dynamic.
David Brazdil0f672f62019-12-10 10:32:29 +000019 * @cost: The cost coefficient associated with this level, used during
20 * energy calculation. Equal to: power * max_frequency / frequency
21 */
Olivier Deprez157378f2022-04-04 15:47:50 +020022struct em_perf_state {
David Brazdil0f672f62019-12-10 10:32:29 +000023 unsigned long frequency;
24 unsigned long power;
25 unsigned long cost;
26};
27
28/**
29 * em_perf_domain - Performance domain
Olivier Deprez157378f2022-04-04 15:47:50 +020030 * @table: List of performance states, in ascending order
31 * @nr_perf_states: Number of performance states
32 * @cpus: Cpumask covering the CPUs of the domain. It's here
33 * for performance reasons to avoid potential cache
34 * misses during energy calculations in the scheduler
35 * and simplifies allocating/freeing that memory region.
David Brazdil0f672f62019-12-10 10:32:29 +000036 *
Olivier Deprez157378f2022-04-04 15:47:50 +020037 * In case of CPU device, a "performance domain" represents a group of CPUs
38 * whose performance is scaled together. All CPUs of a performance domain
39 * must have the same micro-architecture. Performance domains often have
40 * a 1-to-1 mapping with CPUFreq policies. In case of other devices the @cpus
41 * field is unused.
David Brazdil0f672f62019-12-10 10:32:29 +000042 */
43struct em_perf_domain {
Olivier Deprez157378f2022-04-04 15:47:50 +020044 struct em_perf_state *table;
45 int nr_perf_states;
46 unsigned long cpus[];
David Brazdil0f672f62019-12-10 10:32:29 +000047};
48
Olivier Deprez157378f2022-04-04 15:47:50 +020049#define em_span_cpus(em) (to_cpumask((em)->cpus))
50
51#ifdef CONFIG_ENERGY_MODEL
52#define EM_MAX_POWER 0xFFFF
David Brazdil0f672f62019-12-10 10:32:29 +000053
Olivier Deprez0e641232021-09-23 10:07:05 +020054/*
55 * Increase resolution of energy estimation calculations for 64-bit
56 * architectures. The extra resolution improves decision made by EAS for the
57 * task placement when two Performance Domains might provide similar energy
58 * estimation values (w/o better resolution the values could be equal).
59 *
60 * We increase resolution only if we have enough bits to allow this increased
61 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
62 * are pretty high and the returns do not justify the increased costs.
63 */
64#ifdef CONFIG_64BIT
65#define em_scale_power(p) ((p) * 1000)
66#else
67#define em_scale_power(p) (p)
68#endif
69
David Brazdil0f672f62019-12-10 10:32:29 +000070struct em_data_callback {
71 /**
Olivier Deprez157378f2022-04-04 15:47:50 +020072 * active_power() - Provide power at the next performance state of
73 * a device
74 * @power : Active power at the performance state in mW
75 * (modified)
76 * @freq : Frequency at the performance state in kHz
77 * (modified)
78 * @dev : Device for which we do this operation (can be a CPU)
David Brazdil0f672f62019-12-10 10:32:29 +000079 *
Olivier Deprez157378f2022-04-04 15:47:50 +020080 * active_power() must find the lowest performance state of 'dev' above
David Brazdil0f672f62019-12-10 10:32:29 +000081 * 'freq' and update 'power' and 'freq' to the matching active power
82 * and frequency.
83 *
Olivier Deprez157378f2022-04-04 15:47:50 +020084 * In case of CPUs, the power is the one of a single CPU in the domain,
85 * expressed in milli-watts. It is expected to fit in the
86 * [0, EM_MAX_POWER] range.
David Brazdil0f672f62019-12-10 10:32:29 +000087 *
88 * Return 0 on success.
89 */
Olivier Deprez157378f2022-04-04 15:47:50 +020090 int (*active_power)(unsigned long *power, unsigned long *freq,
91 struct device *dev);
David Brazdil0f672f62019-12-10 10:32:29 +000092};
93#define EM_DATA_CB(_active_power_cb) { .active_power = &_active_power_cb }
94
95struct em_perf_domain *em_cpu_get(int cpu);
Olivier Deprez157378f2022-04-04 15:47:50 +020096struct em_perf_domain *em_pd_get(struct device *dev);
97int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
98 struct em_data_callback *cb, cpumask_t *span);
99void em_dev_unregister_perf_domain(struct device *dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000100
101/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200102 * em_cpu_energy() - Estimates the energy consumed by the CPUs of a
103 performance domain
David Brazdil0f672f62019-12-10 10:32:29 +0000104 * @pd : performance domain for which energy has to be estimated
105 * @max_util : highest utilization among CPUs of the domain
106 * @sum_util : sum of the utilization of all CPUs in the domain
107 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200108 * This function must be used only for CPU devices. There is no validation,
109 * i.e. if the EM is a CPU type and has cpumask allocated. It is called from
110 * the scheduler code quite frequently and that is why there is not checks.
111 *
David Brazdil0f672f62019-12-10 10:32:29 +0000112 * Return: the sum of the energy consumed by the CPUs of the domain assuming
113 * a capacity state satisfying the max utilization of the domain.
114 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200115static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
David Brazdil0f672f62019-12-10 10:32:29 +0000116 unsigned long max_util, unsigned long sum_util)
117{
118 unsigned long freq, scale_cpu;
Olivier Deprez157378f2022-04-04 15:47:50 +0200119 struct em_perf_state *ps;
David Brazdil0f672f62019-12-10 10:32:29 +0000120 int i, cpu;
121
122 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200123 * In order to predict the performance state, map the utilization of
124 * the most utilized CPU of the performance domain to a requested
125 * frequency, like schedutil.
David Brazdil0f672f62019-12-10 10:32:29 +0000126 */
127 cpu = cpumask_first(to_cpumask(pd->cpus));
128 scale_cpu = arch_scale_cpu_capacity(cpu);
Olivier Deprez157378f2022-04-04 15:47:50 +0200129 ps = &pd->table[pd->nr_perf_states - 1];
130 freq = map_util_freq(max_util, ps->frequency, scale_cpu);
David Brazdil0f672f62019-12-10 10:32:29 +0000131
132 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200133 * Find the lowest performance state of the Energy Model above the
David Brazdil0f672f62019-12-10 10:32:29 +0000134 * requested frequency.
135 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200136 for (i = 0; i < pd->nr_perf_states; i++) {
137 ps = &pd->table[i];
138 if (ps->frequency >= freq)
David Brazdil0f672f62019-12-10 10:32:29 +0000139 break;
140 }
141
142 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200143 * The capacity of a CPU in the domain at the performance state (ps)
David Brazdil0f672f62019-12-10 10:32:29 +0000144 * can be computed as:
145 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200146 * ps->freq * scale_cpu
147 * ps->cap = -------------------- (1)
David Brazdil0f672f62019-12-10 10:32:29 +0000148 * cpu_max_freq
149 *
150 * So, ignoring the costs of idle states (which are not available in
Olivier Deprez157378f2022-04-04 15:47:50 +0200151 * the EM), the energy consumed by this CPU at that performance state
152 * is estimated as:
David Brazdil0f672f62019-12-10 10:32:29 +0000153 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200154 * ps->power * cpu_util
David Brazdil0f672f62019-12-10 10:32:29 +0000155 * cpu_nrg = -------------------- (2)
Olivier Deprez157378f2022-04-04 15:47:50 +0200156 * ps->cap
David Brazdil0f672f62019-12-10 10:32:29 +0000157 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200158 * since 'cpu_util / ps->cap' represents its percentage of busy time.
David Brazdil0f672f62019-12-10 10:32:29 +0000159 *
160 * NOTE: Although the result of this computation actually is in
161 * units of power, it can be manipulated as an energy value
162 * over a scheduling period, since it is assumed to be
163 * constant during that interval.
164 *
165 * By injecting (1) in (2), 'cpu_nrg' can be re-expressed as a product
166 * of two terms:
167 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200168 * ps->power * cpu_max_freq cpu_util
David Brazdil0f672f62019-12-10 10:32:29 +0000169 * cpu_nrg = ------------------------ * --------- (3)
Olivier Deprez157378f2022-04-04 15:47:50 +0200170 * ps->freq scale_cpu
David Brazdil0f672f62019-12-10 10:32:29 +0000171 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200172 * The first term is static, and is stored in the em_perf_state struct
173 * as 'ps->cost'.
David Brazdil0f672f62019-12-10 10:32:29 +0000174 *
175 * Since all CPUs of the domain have the same micro-architecture, they
Olivier Deprez157378f2022-04-04 15:47:50 +0200176 * share the same 'ps->cost', and the same CPU capacity. Hence, the
David Brazdil0f672f62019-12-10 10:32:29 +0000177 * total energy of the domain (which is the simple sum of the energy of
178 * all of its CPUs) can be factorized as:
179 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200180 * ps->cost * \Sum cpu_util
David Brazdil0f672f62019-12-10 10:32:29 +0000181 * pd_nrg = ------------------------ (4)
182 * scale_cpu
183 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200184 return ps->cost * sum_util / scale_cpu;
David Brazdil0f672f62019-12-10 10:32:29 +0000185}
186
187/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200188 * em_pd_nr_perf_states() - Get the number of performance states of a perf.
189 * domain
David Brazdil0f672f62019-12-10 10:32:29 +0000190 * @pd : performance domain for which this must be done
191 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200192 * Return: the number of performance states in the performance domain table
David Brazdil0f672f62019-12-10 10:32:29 +0000193 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200194static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
David Brazdil0f672f62019-12-10 10:32:29 +0000195{
Olivier Deprez157378f2022-04-04 15:47:50 +0200196 return pd->nr_perf_states;
David Brazdil0f672f62019-12-10 10:32:29 +0000197}
198
199#else
David Brazdil0f672f62019-12-10 10:32:29 +0000200struct em_data_callback {};
201#define EM_DATA_CB(_active_power_cb) { }
202
Olivier Deprez157378f2022-04-04 15:47:50 +0200203static inline
204int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
205 struct em_data_callback *cb, cpumask_t *span)
David Brazdil0f672f62019-12-10 10:32:29 +0000206{
207 return -EINVAL;
208}
Olivier Deprez157378f2022-04-04 15:47:50 +0200209static inline void em_dev_unregister_perf_domain(struct device *dev)
210{
211}
David Brazdil0f672f62019-12-10 10:32:29 +0000212static inline struct em_perf_domain *em_cpu_get(int cpu)
213{
214 return NULL;
215}
Olivier Deprez157378f2022-04-04 15:47:50 +0200216static inline struct em_perf_domain *em_pd_get(struct device *dev)
217{
218 return NULL;
219}
220static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
David Brazdil0f672f62019-12-10 10:32:29 +0000221 unsigned long max_util, unsigned long sum_util)
222{
223 return 0;
224}
Olivier Deprez157378f2022-04-04 15:47:50 +0200225static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
David Brazdil0f672f62019-12-10 10:32:29 +0000226{
227 return 0;
228}
229#endif
230
231#endif