energy-model.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =======================
  3. Energy Model of devices
  4. =======================
  5. 1. Overview
  6. -----------
  7. The Energy Model (EM) framework serves as an interface between drivers knowing
  8. the power consumed by devices at various performance levels, and the kernel
  9. subsystems willing to use that information to make energy-aware decisions.
  10. The source of the information about the power consumed by devices can vary greatly
  11. from one platform to another. These power costs can be estimated using
  12. devicetree data in some cases. In others, the firmware will know better.
  13. Alternatively, userspace might be best positioned. And so on. In order to avoid
  14. each and every client subsystem to re-implement support for each and every
  15. possible source of information on its own, the EM framework intervenes as an
  16. abstraction layer which standardizes the format of power cost tables in the
  17. kernel, hence enabling to avoid redundant work.
  18. The power values might be expressed in micro-Watts or in an 'abstract scale'.
  19. Multiple subsystems might use the EM and it is up to the system integrator to
  20. check that the requirements for the power value scale types are met. An example
  21. can be found in the Energy-Aware Scheduler documentation
  22. Documentation/scheduler/sched-energy.rst. For some subsystems like thermal or
  23. powercap power values expressed in an 'abstract scale' might cause issues.
  24. These subsystems are more interested in estimation of power used in the past,
  25. thus the real micro-Watts might be needed. An example of these requirements can
  26. be found in the Intelligent Power Allocation in
  27. Documentation/driver-api/thermal/power_allocator.rst.
  28. Kernel subsystems might implement automatic detection to check whether EM
  29. registered devices have inconsistent scale (based on EM internal flag).
  30. Important thing to keep in mind is that when the power values are expressed in
  31. an 'abstract scale' deriving real energy in micro-Joules would not be possible.
  32. The figure below depicts an example of drivers (Arm-specific here, but the
  33. approach is applicable to any architecture) providing power costs to the EM
  34. framework, and interested clients reading the data from it::
  35. +---------------+ +-----------------+ +---------------+
  36. | Thermal (IPA) | | Scheduler (EAS) | | Other |
  37. +---------------+ +-----------------+ +---------------+
  38. | | em_cpu_energy() |
  39. | | em_cpu_get() |
  40. +---------+ | +---------+
  41. | | |
  42. v v v
  43. +---------------------+
  44. | Energy Model |
  45. | Framework |
  46. +---------------------+
  47. ^ ^ ^
  48. | | | em_dev_register_perf_domain()
  49. +----------+ | +---------+
  50. | | |
  51. +---------------+ +---------------+ +--------------+
  52. | cpufreq-dt | | arm_scmi | | Other |
  53. +---------------+ +---------------+ +--------------+
  54. ^ ^ ^
  55. | | |
  56. +--------------+ +---------------+ +--------------+
  57. | Device Tree | | Firmware | | ? |
  58. +--------------+ +---------------+ +--------------+
  59. In case of CPU devices the EM framework manages power cost tables per
  60. 'performance domain' in the system. A performance domain is a group of CPUs
  61. whose performance is scaled together. Performance domains generally have a
  62. 1-to-1 mapping with CPUFreq policies. All CPUs in a performance domain are
  63. required to have the same micro-architecture. CPUs in different performance
  64. domains can have different micro-architectures.
  65. To better reflect power variation due to static power (leakage) the EM
  66. supports runtime modifications of the power values. The mechanism relies on
  67. RCU to free the modifiable EM perf_state table memory. Its user, the task
  68. scheduler, also uses RCU to access this memory. The EM framework provides
  69. API for allocating/freeing the new memory for the modifiable EM table.
  70. The old memory is freed automatically using RCU callback mechanism when there
  71. are no owners anymore for the given EM runtime table instance. This is tracked
  72. using kref mechanism. The device driver which provided the new EM at runtime,
  73. should call EM API to free it safely when it's no longer needed. The EM
  74. framework will handle the clean-up when it's possible.
  75. The kernel code which want to modify the EM values is protected from concurrent
  76. access using a mutex. Therefore, the device driver code must run in sleeping
  77. context when it tries to modify the EM.
  78. With the runtime modifiable EM we switch from a 'single and during the entire
  79. runtime static EM' (system property) design to a 'single EM which can be
  80. changed during runtime according e.g. to the workload' (system and workload
  81. property) design.
  82. It is possible also to modify the CPU performance values for each EM's
  83. performance state. Thus, the full power and performance profile (which
  84. is an exponential curve) can be changed according e.g. to the workload
  85. or system property.
  86. 2. Core APIs
  87. ------------
  88. 2.1 Config options
  89. ^^^^^^^^^^^^^^^^^^
  90. CONFIG_ENERGY_MODEL must be enabled to use the EM framework.
  91. 2.2 Registration of performance domains
  92. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  93. Registration of 'advanced' EM
  94. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. The 'advanced' EM gets its name due to the fact that the driver is allowed
  96. to provide more precised power model. It's not limited to some implemented math
  97. formula in the framework (like it is in 'simple' EM case). It can better reflect
  98. the real power measurements performed for each performance state. Thus, this
  99. registration method should be preferred in case considering EM static power
  100. (leakage) is important.
  101. Drivers are expected to register performance domains into the EM framework by
  102. calling the following API::
  103. int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
  104. struct em_data_callback *cb, cpumask_t *cpus, bool microwatts);
  105. Drivers must provide a callback function returning <frequency, power> tuples
  106. for each performance state. The callback function provided by the driver is free
  107. to fetch data from any relevant location (DT, firmware, ...), and by any mean
  108. deemed necessary. Only for CPU devices, drivers must specify the CPUs of the
  109. performance domains using cpumask. For other devices than CPUs the last
  110. argument must be set to NULL.
  111. The last argument 'microwatts' is important to set with correct value. Kernel
  112. subsystems which use EM might rely on this flag to check if all EM devices use
  113. the same scale. If there are different scales, these subsystems might decide
  114. to return warning/error, stop working or panic.
  115. See Section 3. for an example of driver implementing this
  116. callback, or Section 2.4 for further documentation on this API
  117. Registration of EM using DT
  118. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  119. The EM can also be registered using OPP framework and information in DT
  120. "operating-points-v2". Each OPP entry in DT can be extended with a property
  121. "opp-microwatt" containing micro-Watts power value. This OPP DT property
  122. allows a platform to register EM power values which are reflecting total power
  123. (static + dynamic). These power values might be coming directly from
  124. experiments and measurements.
  125. Registration of 'artificial' EM
  126. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127. There is an option to provide a custom callback for drivers missing detailed
  128. knowledge about power value for each performance state. The callback
  129. .get_cost() is optional and provides the 'cost' values used by the EAS.
  130. This is useful for platforms that only provide information on relative
  131. efficiency between CPU types, where one could use the information to
  132. create an abstract power model. But even an abstract power model can
  133. sometimes be hard to fit in, given the input power value size restrictions.
  134. The .get_cost() allows to provide the 'cost' values which reflect the
  135. efficiency of the CPUs. This would allow to provide EAS information which
  136. has different relation than what would be forced by the EM internal
  137. formulas calculating 'cost' values. To register an EM for such platform, the
  138. driver must set the flag 'microwatts' to 0, provide .get_power() callback
  139. and provide .get_cost() callback. The EM framework would handle such platform
  140. properly during registration. A flag EM_PERF_DOMAIN_ARTIFICIAL is set for such
  141. platform. Special care should be taken by other frameworks which are using EM
  142. to test and treat this flag properly.
  143. Registration of 'simple' EM
  144. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  145. The 'simple' EM is registered using the framework helper function
  146. cpufreq_register_em_with_opp(). It implements a power model which is tight to
  147. math formula::
  148. Power = C * V^2 * f
  149. The EM which is registered using this method might not reflect correctly the
  150. physics of a real device, e.g. when static power (leakage) is important.
  151. 2.3 Accessing performance domains
  152. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  153. There are two API functions which provide the access to the energy model:
  154. em_cpu_get() which takes CPU id as an argument and em_pd_get() with device
  155. pointer as an argument. It depends on the subsystem which interface it is
  156. going to use, but in case of CPU devices both functions return the same
  157. performance domain.
  158. Subsystems interested in the energy model of a CPU can retrieve it using the
  159. em_cpu_get() API. The energy model tables are allocated once upon creation of
  160. the performance domains, and kept in memory untouched.
  161. The energy consumed by a performance domain can be estimated using the
  162. em_cpu_energy() API. The estimation is performed assuming that the schedutil
  163. CPUfreq governor is in use in case of CPU device. Currently this calculation is
  164. not provided for other type of devices.
  165. More details about the above APIs can be found in ``<linux/energy_model.h>``
  166. or in Section 2.5
  167. 2.4 Runtime modifications
  168. ^^^^^^^^^^^^^^^^^^^^^^^^^
  169. Drivers willing to update the EM at runtime should use the following dedicated
  170. function to allocate a new instance of the modified EM. The API is listed
  171. below::
  172. struct em_perf_table __rcu *em_table_alloc(struct em_perf_domain *pd);
  173. This allows to allocate a structure which contains the new EM table with
  174. also RCU and kref needed by the EM framework. The 'struct em_perf_table'
  175. contains array 'struct em_perf_state state[]' which is a list of performance
  176. states in ascending order. That list must be populated by the device driver
  177. which wants to update the EM. The list of frequencies can be taken from
  178. existing EM (created during boot). The content in the 'struct em_perf_state'
  179. must be populated by the driver as well.
  180. This is the API which does the EM update, using RCU pointers swap::
  181. int em_dev_update_perf_domain(struct device *dev,
  182. struct em_perf_table __rcu *new_table);
  183. Drivers must provide a pointer to the allocated and initialized new EM
  184. 'struct em_perf_table'. That new EM will be safely used inside the EM framework
  185. and will be visible to other sub-systems in the kernel (thermal, powercap).
  186. The main design goal for this API is to be fast and avoid extra calculations
  187. or memory allocations at runtime. When pre-computed EMs are available in the
  188. device driver, than it should be possible to simply re-use them with low
  189. performance overhead.
  190. In order to free the EM, provided earlier by the driver (e.g. when the module
  191. is unloaded), there is a need to call the API::
  192. void em_table_free(struct em_perf_table __rcu *table);
  193. It will allow the EM framework to safely remove the memory, when there is
  194. no other sub-system using it, e.g. EAS.
  195. To use the power values in other sub-systems (like thermal, powercap) there is
  196. a need to call API which protects the reader and provide consistency of the EM
  197. table data::
  198. struct em_perf_state *em_perf_state_from_pd(struct em_perf_domain *pd);
  199. It returns the 'struct em_perf_state' pointer which is an array of performance
  200. states in ascending order.
  201. This function must be called in the RCU read lock section (after the
  202. rcu_read_lock()). When the EM table is not needed anymore there is a need to
  203. call rcu_real_unlock(). In this way the EM safely uses the RCU read section
  204. and protects the users. It also allows the EM framework to manage the memory
  205. and free it. More details how to use it can be found in Section 3.2 in the
  206. example driver.
  207. There is dedicated API for device drivers to calculate em_perf_state::cost
  208. values::
  209. int em_dev_compute_costs(struct device *dev, struct em_perf_state *table,
  210. int nr_states);
  211. These 'cost' values from EM are used in EAS. The new EM table should be passed
  212. together with the number of entries and device pointer. When the computation
  213. of the cost values is done properly the return value from the function is 0.
  214. The function takes care for right setting of inefficiency for each performance
  215. state as well. It updates em_perf_state::flags accordingly.
  216. Then such prepared new EM can be passed to the em_dev_update_perf_domain()
  217. function, which will allow to use it.
  218. More details about the above APIs can be found in ``<linux/energy_model.h>``
  219. or in Section 3.2 with an example code showing simple implementation of the
  220. updating mechanism in a device driver.
  221. 2.5 Description details of this API
  222. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  223. .. kernel-doc:: include/linux/energy_model.h
  224. :internal:
  225. .. kernel-doc:: kernel/power/energy_model.c
  226. :export:
  227. 3. Examples
  228. -----------
  229. 3.1 Example driver with EM registration
  230. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  231. The CPUFreq framework supports dedicated callback for registering
  232. the EM for a given CPU(s) 'policy' object: cpufreq_driver::register_em().
  233. That callback has to be implemented properly for a given driver,
  234. because the framework would call it at the right time during setup.
  235. This section provides a simple example of a CPUFreq driver registering a
  236. performance domain in the Energy Model framework using the (fake) 'foo'
  237. protocol. The driver implements an est_power() function to be provided to the
  238. EM framework::
  239. -> drivers/cpufreq/foo_cpufreq.c
  240. 01 static int est_power(struct device *dev, unsigned long *mW,
  241. 02 unsigned long *KHz)
  242. 03 {
  243. 04 long freq, power;
  244. 05
  245. 06 /* Use the 'foo' protocol to ceil the frequency */
  246. 07 freq = foo_get_freq_ceil(dev, *KHz);
  247. 08 if (freq < 0);
  248. 09 return freq;
  249. 10
  250. 11 /* Estimate the power cost for the dev at the relevant freq. */
  251. 12 power = foo_estimate_power(dev, freq);
  252. 13 if (power < 0);
  253. 14 return power;
  254. 15
  255. 16 /* Return the values to the EM framework */
  256. 17 *mW = power;
  257. 18 *KHz = freq;
  258. 19
  259. 20 return 0;
  260. 21 }
  261. 22
  262. 23 static void foo_cpufreq_register_em(struct cpufreq_policy *policy)
  263. 24 {
  264. 25 struct em_data_callback em_cb = EM_DATA_CB(est_power);
  265. 26 struct device *cpu_dev;
  266. 27 int nr_opp;
  267. 28
  268. 29 cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
  269. 30
  270. 31 /* Find the number of OPPs for this policy */
  271. 32 nr_opp = foo_get_nr_opp(policy);
  272. 33
  273. 34 /* And register the new performance domain */
  274. 35 em_dev_register_perf_domain(cpu_dev, nr_opp, &em_cb, policy->cpus,
  275. 36 true);
  276. 37 }
  277. 38
  278. 39 static struct cpufreq_driver foo_cpufreq_driver = {
  279. 40 .register_em = foo_cpufreq_register_em,
  280. 41 };
  281. 3.2 Example driver with EM modification
  282. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  283. This section provides a simple example of a thermal driver modifying the EM.
  284. The driver implements a foo_thermal_em_update() function. The driver is woken
  285. up periodically to check the temperature and modify the EM data::
  286. -> drivers/soc/example/example_em_mod.c
  287. 01 static void foo_get_new_em(struct foo_context *ctx)
  288. 02 {
  289. 03 struct em_perf_table __rcu *em_table;
  290. 04 struct em_perf_state *table, *new_table;
  291. 05 struct device *dev = ctx->dev;
  292. 06 struct em_perf_domain *pd;
  293. 07 unsigned long freq;
  294. 08 int i, ret;
  295. 09
  296. 10 pd = em_pd_get(dev);
  297. 11 if (!pd)
  298. 12 return;
  299. 13
  300. 14 em_table = em_table_alloc(pd);
  301. 15 if (!em_table)
  302. 16 return;
  303. 17
  304. 18 new_table = em_table->state;
  305. 19
  306. 20 rcu_read_lock();
  307. 21 table = em_perf_state_from_pd(pd);
  308. 22 for (i = 0; i < pd->nr_perf_states; i++) {
  309. 23 freq = table[i].frequency;
  310. 24 foo_get_power_perf_values(dev, freq, &new_table[i]);
  311. 25 }
  312. 26 rcu_read_unlock();
  313. 27
  314. 28 /* Calculate 'cost' values for EAS */
  315. 29 ret = em_dev_compute_costs(dev, table, pd->nr_perf_states);
  316. 30 if (ret) {
  317. 31 dev_warn(dev, "EM: compute costs failed %d\n", ret);
  318. 32 em_free_table(em_table);
  319. 33 return;
  320. 34 }
  321. 35
  322. 36 ret = em_dev_update_perf_domain(dev, em_table);
  323. 37 if (ret) {
  324. 38 dev_warn(dev, "EM: update failed %d\n", ret);
  325. 39 em_free_table(em_table);
  326. 40 return;
  327. 41 }
  328. 42
  329. 43 /*
  330. 44 * Since it's one-time-update drop the usage counter.
  331. 45 * The EM framework will later free the table when needed.
  332. 46 */
  333. 47 em_table_free(em_table);
  334. 48 }
  335. 49
  336. 50 /*
  337. 51 * Function called periodically to check the temperature and
  338. 52 * update the EM if needed
  339. 53 */
  340. 54 static void foo_thermal_em_update(struct foo_context *ctx)
  341. 55 {
  342. 56 struct device *dev = ctx->dev;
  343. 57 int cpu;
  344. 58
  345. 59 ctx->temperature = foo_get_temp(dev, ctx);
  346. 60 if (ctx->temperature < FOO_EM_UPDATE_TEMP_THRESHOLD)
  347. 61 return;
  348. 62
  349. 63 foo_get_new_em(ctx);
  350. 64 }