xref: /linux-6.15/include/linux/energy_model.h (revision fe968ca2)
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  * 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  */
21 struct em_perf_state {
22 	unsigned long frequency;
23 	unsigned long power;
24 	unsigned long cost;
25 };
26 
27 /**
28  * 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.
37  *
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
42  * field is unused.
43  */
44 struct em_perf_domain {
45 	struct em_perf_state *table;
46 	int nr_perf_states;
47 	int milliwatts;
48 	unsigned long cpus[];
49 };
50 
51 #define em_span_cpus(em) (to_cpumask((em)->cpus))
52 
53 #ifdef CONFIG_ENERGY_MODEL
54 #define EM_MAX_POWER 0xFFFF
55 
56 struct em_data_callback {
57 	/**
58 	 * active_power() - Provide power at the next performance state of
59 	 *		a device
60 	 * @power	: Active power at the performance state
61 	 *		(modified)
62 	 * @freq	: Frequency at the performance state in kHz
63 	 *		(modified)
64 	 * @dev		: Device for which we do this operation (can be a CPU)
65 	 *
66 	 * active_power() must find the lowest performance state of 'dev' above
67 	 * 'freq' and update 'power' and 'freq' to the matching active power
68 	 * and frequency.
69 	 *
70 	 * In case of CPUs, the power is the one of a single CPU in the domain,
71 	 * expressed in milli-Watts or an abstract scale. It is expected to
72 	 * fit in the [0, EM_MAX_POWER] range.
73 	 *
74 	 * Return 0 on success.
75 	 */
76 	int (*active_power)(unsigned long *power, unsigned long *freq,
77 			    struct device *dev);
78 };
79 #define EM_DATA_CB(_active_power_cb) { .active_power = &_active_power_cb }
80 
81 struct em_perf_domain *em_cpu_get(int cpu);
82 struct em_perf_domain *em_pd_get(struct device *dev);
83 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
84 				struct em_data_callback *cb, cpumask_t *span,
85 				bool milliwatts);
86 void em_dev_unregister_perf_domain(struct device *dev);
87 
88 /**
89  * em_cpu_energy() - Estimates the energy consumed by the CPUs of a
90 		performance domain
91  * @pd		: performance domain for which energy has to be estimated
92  * @max_util	: highest utilization among CPUs of the domain
93  * @sum_util	: sum of the utilization of all CPUs in the domain
94  * @allowed_cpu_cap	: maximum allowed CPU capacity for the @pd, which
95 			  might reflect reduced frequency (due to thermal)
96  *
97  * This function must be used only for CPU devices. There is no validation,
98  * i.e. if the EM is a CPU type and has cpumask allocated. It is called from
99  * the scheduler code quite frequently and that is why there is not checks.
100  *
101  * Return: the sum of the energy consumed by the CPUs of the domain assuming
102  * a capacity state satisfying the max utilization of the domain.
103  */
104 static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
105 				unsigned long max_util, unsigned long sum_util,
106 				unsigned long allowed_cpu_cap)
107 {
108 	unsigned long freq, scale_cpu;
109 	struct em_perf_state *ps;
110 	int i, cpu;
111 
112 	if (!sum_util)
113 		return 0;
114 
115 	/*
116 	 * In order to predict the performance state, map the utilization of
117 	 * the most utilized CPU of the performance domain to a requested
118 	 * frequency, like schedutil. Take also into account that the real
119 	 * frequency might be set lower (due to thermal capping). Thus, clamp
120 	 * max utilization to the allowed CPU capacity before calculating
121 	 * effective frequency.
122 	 */
123 	cpu = cpumask_first(to_cpumask(pd->cpus));
124 	scale_cpu = arch_scale_cpu_capacity(cpu);
125 	ps = &pd->table[pd->nr_perf_states - 1];
126 
127 	max_util = map_util_perf(max_util);
128 	max_util = min(max_util, allowed_cpu_cap);
129 	freq = map_util_freq(max_util, ps->frequency, scale_cpu);
130 
131 	/*
132 	 * Find the lowest performance state of the Energy Model above the
133 	 * requested frequency.
134 	 */
135 	for (i = 0; i < pd->nr_perf_states; i++) {
136 		ps = &pd->table[i];
137 		if (ps->frequency >= freq)
138 			break;
139 	}
140 
141 	/*
142 	 * The capacity of a CPU in the domain at the performance state (ps)
143 	 * can be computed as:
144 	 *
145 	 *             ps->freq * scale_cpu
146 	 *   ps->cap = --------------------                          (1)
147 	 *                 cpu_max_freq
148 	 *
149 	 * So, ignoring the costs of idle states (which are not available in
150 	 * the EM), the energy consumed by this CPU at that performance state
151 	 * is estimated as:
152 	 *
153 	 *             ps->power * cpu_util
154 	 *   cpu_nrg = --------------------                          (2)
155 	 *                   ps->cap
156 	 *
157 	 * since 'cpu_util / ps->cap' represents its percentage of busy time.
158 	 *
159 	 *   NOTE: Although the result of this computation actually is in
160 	 *         units of power, it can be manipulated as an energy value
161 	 *         over a scheduling period, since it is assumed to be
162 	 *         constant during that interval.
163 	 *
164 	 * By injecting (1) in (2), 'cpu_nrg' can be re-expressed as a product
165 	 * of two terms:
166 	 *
167 	 *             ps->power * cpu_max_freq   cpu_util
168 	 *   cpu_nrg = ------------------------ * ---------          (3)
169 	 *                    ps->freq            scale_cpu
170 	 *
171 	 * The first term is static, and is stored in the em_perf_state struct
172 	 * as 'ps->cost'.
173 	 *
174 	 * Since all CPUs of the domain have the same micro-architecture, they
175 	 * share the same 'ps->cost', and the same CPU capacity. Hence, the
176 	 * total energy of the domain (which is the simple sum of the energy of
177 	 * all of its CPUs) can be factorized as:
178 	 *
179 	 *            ps->cost * \Sum cpu_util
180 	 *   pd_nrg = ------------------------                       (4)
181 	 *                  scale_cpu
182 	 */
183 	return ps->cost * sum_util / scale_cpu;
184 }
185 
186 /**
187  * em_pd_nr_perf_states() - Get the number of performance states of a perf.
188  *				domain
189  * @pd		: performance domain for which this must be done
190  *
191  * Return: the number of performance states in the performance domain table
192  */
193 static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
194 {
195 	return pd->nr_perf_states;
196 }
197 
198 #else
199 struct em_data_callback {};
200 #define EM_DATA_CB(_active_power_cb) { }
201 
202 static inline
203 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
204 				struct em_data_callback *cb, cpumask_t *span,
205 				bool milliwatts)
206 {
207 	return -EINVAL;
208 }
209 static inline void em_dev_unregister_perf_domain(struct device *dev)
210 {
211 }
212 static inline struct em_perf_domain *em_cpu_get(int cpu)
213 {
214 	return NULL;
215 }
216 static inline struct em_perf_domain *em_pd_get(struct device *dev)
217 {
218 	return NULL;
219 }
220 static inline unsigned long em_cpu_energy(struct em_perf_domain *pd,
221 			unsigned long max_util, unsigned long sum_util,
222 			unsigned long allowed_cpu_cap)
223 {
224 	return 0;
225 }
226 static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
227 {
228 	return 0;
229 }
230 #endif
231 
232 #endif
233