energy_model.h 12 KB

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