blob: 9ee6ccc18424f99672aff34bd5ad2960bc169590 [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>
5#include <linux/jump_label.h>
6#include <linux/kobject.h>
7#include <linux/rcupdate.h>
8#include <linux/sched/cpufreq.h>
9#include <linux/sched/topology.h>
10#include <linux/types.h>
11
12#ifdef CONFIG_ENERGY_MODEL
13/**
14 * em_cap_state - Capacity state of a performance domain
15 * @frequency: The CPU frequency in KHz, for consistency with CPUFreq
16 * @power: The power consumed by 1 CPU at this level, in milli-watts
17 * @cost: The cost coefficient associated with this level, used during
18 * energy calculation. Equal to: power * max_frequency / frequency
19 */
20struct em_cap_state {
21 unsigned long frequency;
22 unsigned long power;
23 unsigned long cost;
24};
25
26/**
27 * em_perf_domain - Performance domain
28 * @table: List of capacity states, in ascending order
29 * @nr_cap_states: Number of capacity states
30 * @cpus: Cpumask covering the CPUs of the domain
31 *
32 * A "performance domain" represents a group of CPUs whose performance is
33 * scaled together. All CPUs of a performance domain must have the same
34 * micro-architecture. Performance domains often have a 1-to-1 mapping with
35 * CPUFreq policies.
36 */
37struct em_perf_domain {
38 struct em_cap_state *table;
39 int nr_cap_states;
40 unsigned long cpus[0];
41};
42
43#define EM_CPU_MAX_POWER 0xFFFF
44
Olivier Deprez0e641232021-09-23 10:07:05 +020045/*
46 * Increase resolution of energy estimation calculations for 64-bit
47 * architectures. The extra resolution improves decision made by EAS for the
48 * task placement when two Performance Domains might provide similar energy
49 * estimation values (w/o better resolution the values could be equal).
50 *
51 * We increase resolution only if we have enough bits to allow this increased
52 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
53 * are pretty high and the returns do not justify the increased costs.
54 */
55#ifdef CONFIG_64BIT
56#define em_scale_power(p) ((p) * 1000)
57#else
58#define em_scale_power(p) (p)
59#endif
60
David Brazdil0f672f62019-12-10 10:32:29 +000061struct em_data_callback {
62 /**
63 * active_power() - Provide power at the next capacity state of a CPU
64 * @power : Active power at the capacity state in mW (modified)
65 * @freq : Frequency at the capacity state in kHz (modified)
66 * @cpu : CPU for which we do this operation
67 *
68 * active_power() must find the lowest capacity state of 'cpu' above
69 * 'freq' and update 'power' and 'freq' to the matching active power
70 * and frequency.
71 *
72 * The power is the one of a single CPU in the domain, expressed in
73 * milli-watts. It is expected to fit in the [0, EM_CPU_MAX_POWER]
74 * range.
75 *
76 * Return 0 on success.
77 */
78 int (*active_power)(unsigned long *power, unsigned long *freq, int cpu);
79};
80#define EM_DATA_CB(_active_power_cb) { .active_power = &_active_power_cb }
81
82struct em_perf_domain *em_cpu_get(int cpu);
83int em_register_perf_domain(cpumask_t *span, unsigned int nr_states,
84 struct em_data_callback *cb);
85
86/**
87 * em_pd_energy() - Estimates the energy consumed by the CPUs of a perf. domain
88 * @pd : performance domain for which energy has to be estimated
89 * @max_util : highest utilization among CPUs of the domain
90 * @sum_util : sum of the utilization of all CPUs in the domain
91 *
92 * Return: the sum of the energy consumed by the CPUs of the domain assuming
93 * a capacity state satisfying the max utilization of the domain.
94 */
95static inline unsigned long em_pd_energy(struct em_perf_domain *pd,
96 unsigned long max_util, unsigned long sum_util)
97{
98 unsigned long freq, scale_cpu;
99 struct em_cap_state *cs;
100 int i, cpu;
101
102 /*
103 * In order to predict the capacity state, map the utilization of the
104 * most utilized CPU of the performance domain to a requested frequency,
105 * like schedutil.
106 */
107 cpu = cpumask_first(to_cpumask(pd->cpus));
108 scale_cpu = arch_scale_cpu_capacity(cpu);
109 cs = &pd->table[pd->nr_cap_states - 1];
110 freq = map_util_freq(max_util, cs->frequency, scale_cpu);
111
112 /*
113 * Find the lowest capacity state of the Energy Model above the
114 * requested frequency.
115 */
116 for (i = 0; i < pd->nr_cap_states; i++) {
117 cs = &pd->table[i];
118 if (cs->frequency >= freq)
119 break;
120 }
121
122 /*
123 * The capacity of a CPU in the domain at that capacity state (cs)
124 * can be computed as:
125 *
126 * cs->freq * scale_cpu
127 * cs->cap = -------------------- (1)
128 * cpu_max_freq
129 *
130 * So, ignoring the costs of idle states (which are not available in
131 * the EM), the energy consumed by this CPU at that capacity state is
132 * estimated as:
133 *
134 * cs->power * cpu_util
135 * cpu_nrg = -------------------- (2)
136 * cs->cap
137 *
138 * since 'cpu_util / cs->cap' represents its percentage of busy time.
139 *
140 * NOTE: Although the result of this computation actually is in
141 * units of power, it can be manipulated as an energy value
142 * over a scheduling period, since it is assumed to be
143 * constant during that interval.
144 *
145 * By injecting (1) in (2), 'cpu_nrg' can be re-expressed as a product
146 * of two terms:
147 *
148 * cs->power * cpu_max_freq cpu_util
149 * cpu_nrg = ------------------------ * --------- (3)
150 * cs->freq scale_cpu
151 *
152 * The first term is static, and is stored in the em_cap_state struct
153 * as 'cs->cost'.
154 *
155 * Since all CPUs of the domain have the same micro-architecture, they
156 * share the same 'cs->cost', and the same CPU capacity. Hence, the
157 * total energy of the domain (which is the simple sum of the energy of
158 * all of its CPUs) can be factorized as:
159 *
160 * cs->cost * \Sum cpu_util
161 * pd_nrg = ------------------------ (4)
162 * scale_cpu
163 */
164 return cs->cost * sum_util / scale_cpu;
165}
166
167/**
168 * em_pd_nr_cap_states() - Get the number of capacity states of a perf. domain
169 * @pd : performance domain for which this must be done
170 *
171 * Return: the number of capacity states in the performance domain table
172 */
173static inline int em_pd_nr_cap_states(struct em_perf_domain *pd)
174{
175 return pd->nr_cap_states;
176}
177
178#else
179struct em_perf_domain {};
180struct em_data_callback {};
181#define EM_DATA_CB(_active_power_cb) { }
182
183static inline int em_register_perf_domain(cpumask_t *span,
184 unsigned int nr_states, struct em_data_callback *cb)
185{
186 return -EINVAL;
187}
188static inline struct em_perf_domain *em_cpu_get(int cpu)
189{
190 return NULL;
191}
192static inline unsigned long em_pd_energy(struct em_perf_domain *pd,
193 unsigned long max_util, unsigned long sum_util)
194{
195 return 0;
196}
197static inline int em_pd_nr_cap_states(struct em_perf_domain *pd)
198{
199 return 0;
200}
201#endif
202
203#endif