@@ -71,6 +71,31 @@ whose performance is scaled together. Performance domains generally have a
71
71
required to have the same micro-architecture. CPUs in different performance
72
72
domains can have different micro-architectures.
73
73
74
+ To better reflect power variation due to static power (leakage) the EM
75
+ supports runtime modifications of the power values. The mechanism relies on
76
+ RCU to free the modifiable EM perf_state table memory. Its user, the task
77
+ scheduler, also uses RCU to access this memory. The EM framework provides
78
+ API for allocating/freeing the new memory for the modifiable EM table.
79
+ The old memory is freed automatically using RCU callback mechanism when there
80
+ are no owners anymore for the given EM runtime table instance. This is tracked
81
+ using kref mechanism. The device driver which provided the new EM at runtime,
82
+ should call EM API to free it safely when it's no longer needed. The EM
83
+ framework will handle the clean-up when it's possible.
84
+
85
+ The kernel code which want to modify the EM values is protected from concurrent
86
+ access using a mutex. Therefore, the device driver code must run in sleeping
87
+ context when it tries to modify the EM.
88
+
89
+ With the runtime modifiable EM we switch from a 'single and during the entire
90
+ runtime static EM' (system property) design to a 'single EM which can be
91
+ changed during runtime according e.g. to the workload' (system and workload
92
+ property) design.
93
+
94
+ It is possible also to modify the CPU performance values for each EM's
95
+ performance state. Thus, the full power and performance profile (which
96
+ is an exponential curve) can be changed according e.g. to the workload
97
+ or system property.
98
+
74
99
75
100
2. Core APIs
76
101
------------
@@ -175,10 +200,82 @@ CPUfreq governor is in use in case of CPU device. Currently this calculation is
175
200
not provided for other type of devices.
176
201
177
202
More details about the above APIs can be found in ``<linux/energy_model.h> ``
178
- or in Section 2.4
203
+ or in Section 2.5
204
+
205
+
206
+ 2.4 Runtime modifications
207
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
208
+
209
+ Drivers willing to update the EM at runtime should use the following dedicated
210
+ function to allocate a new instance of the modified EM. The API is listed
211
+ below::
212
+
213
+ struct em_perf_table __rcu *em_table_alloc(struct em_perf_domain *pd);
214
+
215
+ This allows to allocate a structure which contains the new EM table with
216
+ also RCU and kref needed by the EM framework. The 'struct em_perf_table'
217
+ contains array 'struct em_perf_state state[]' which is a list of performance
218
+ states in ascending order. That list must be populated by the device driver
219
+ which wants to update the EM. The list of frequencies can be taken from
220
+ existing EM (created during boot). The content in the 'struct em_perf_state'
221
+ must be populated by the driver as well.
222
+
223
+ This is the API which does the EM update, using RCU pointers swap::
224
+
225
+ int em_dev_update_perf_domain(struct device *dev,
226
+ struct em_perf_table __rcu *new_table);
227
+
228
+ Drivers must provide a pointer to the allocated and initialized new EM
229
+ 'struct em_perf_table'. That new EM will be safely used inside the EM framework
230
+ and will be visible to other sub-systems in the kernel (thermal, powercap).
231
+ The main design goal for this API is to be fast and avoid extra calculations
232
+ or memory allocations at runtime. When pre-computed EMs are available in the
233
+ device driver, than it should be possible to simply re-use them with low
234
+ performance overhead.
235
+
236
+ In order to free the EM, provided earlier by the driver (e.g. when the module
237
+ is unloaded), there is a need to call the API::
238
+
239
+ void em_table_free(struct em_perf_table __rcu *table);
240
+
241
+ It will allow the EM framework to safely remove the memory, when there is
242
+ no other sub-system using it, e.g. EAS.
243
+
244
+ To use the power values in other sub-systems (like thermal, powercap) there is
245
+ a need to call API which protects the reader and provide consistency of the EM
246
+ table data::
247
+
248
+ struct em_perf_state *em_perf_state_from_pd(struct em_perf_domain *pd);
249
+
250
+ It returns the 'struct em_perf_state' pointer which is an array of performance
251
+ states in ascending order.
252
+ This function must be called in the RCU read lock section (after the
253
+ rcu_read_lock()). When the EM table is not needed anymore there is a need to
254
+ call rcu_real_unlock(). In this way the EM safely uses the RCU read section
255
+ and protects the users. It also allows the EM framework to manage the memory
256
+ and free it. More details how to use it can be found in Section 3.2 in the
257
+ example driver.
258
+
259
+ There is dedicated API for device drivers to calculate em_perf_state::cost
260
+ values::
261
+
262
+ int em_dev_compute_costs(struct device *dev, struct em_perf_state *table,
263
+ int nr_states);
264
+
265
+ These 'cost' values from EM are used in EAS. The new EM table should be passed
266
+ together with the number of entries and device pointer. When the computation
267
+ of the cost values is done properly the return value from the function is 0.
268
+ The function takes care for right setting of inefficiency for each performance
269
+ state as well. It updates em_perf_state::flags accordingly.
270
+ Then such prepared new EM can be passed to the em_dev_update_perf_domain()
271
+ function, which will allow to use it.
272
+
273
+ More details about the above APIs can be found in ``<linux/energy_model.h> ``
274
+ or in Section 3.2 with an example code showing simple implementation of the
275
+ updating mechanism in a device driver.
179
276
180
277
181
- 2.4 Description details of this API
278
+ 2.5 Description details of this API
182
279
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
183
280
.. kernel-doc :: include/linux/energy_model.h
184
281
:internal:
@@ -187,8 +284,11 @@ or in Section 2.4
187
284
:export:
188
285
189
286
190
- 3. Example driver
191
- -----------------
287
+ 3. Examples
288
+ -----------
289
+
290
+ 3.1 Example driver with EM registration
291
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192
292
193
293
The CPUFreq framework supports dedicated callback for registering
194
294
the EM for a given CPU(s) 'policy' object: cpufreq_driver::register_em().
@@ -242,3 +342,78 @@ EM framework::
242
342
39 static struct cpufreq_driver foo_cpufreq_driver = {
243
343
40 .register_em = foo_cpufreq_register_em,
244
344
41 };
345
+
346
+
347
+ 3.2 Example driver with EM modification
348
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
349
+
350
+ This section provides a simple example of a thermal driver modifying the EM.
351
+ The driver implements a foo_thermal_em_update() function. The driver is woken
352
+ up periodically to check the temperature and modify the EM data::
353
+
354
+ -> drivers/soc/example/example_em_mod.c
355
+
356
+ 01 static void foo_get_new_em(struct foo_context *ctx)
357
+ 02 {
358
+ 03 struct em_perf_table __rcu *em_table;
359
+ 04 struct em_perf_state *table, *new_table;
360
+ 05 struct device *dev = ctx->dev;
361
+ 06 struct em_perf_domain *pd;
362
+ 07 unsigned long freq;
363
+ 08 int i, ret;
364
+ 09
365
+ 10 pd = em_pd_get(dev);
366
+ 11 if (!pd)
367
+ 12 return;
368
+ 13
369
+ 14 em_table = em_table_alloc(pd);
370
+ 15 if (!em_table)
371
+ 16 return;
372
+ 17
373
+ 18 new_table = em_table->state;
374
+ 19
375
+ 20 rcu_read_lock();
376
+ 21 table = em_perf_state_from_pd(pd);
377
+ 22 for (i = 0; i < pd->nr_perf_states; i++) {
378
+ 23 freq = table[i].frequency;
379
+ 24 foo_get_power_perf_values(dev, freq, &new_table[i]);
380
+ 25 }
381
+ 26 rcu_read_unlock();
382
+ 27
383
+ 28 /* Calculate 'cost' values for EAS */
384
+ 29 ret = em_dev_compute_costs(dev, table, pd->nr_perf_states);
385
+ 30 if (ret) {
386
+ 31 dev_warn(dev, "EM: compute costs failed %d\n", ret);
387
+ 32 em_free_table(em_table);
388
+ 33 return;
389
+ 34 }
390
+ 35
391
+ 36 ret = em_dev_update_perf_domain(dev, em_table);
392
+ 37 if (ret) {
393
+ 38 dev_warn(dev, "EM: update failed %d\n", ret);
394
+ 39 em_free_table(em_table);
395
+ 40 return;
396
+ 41 }
397
+ 42
398
+ 43 /*
399
+ 44 * Since it's one-time-update drop the usage counter.
400
+ 45 * The EM framework will later free the table when needed.
401
+ 46 */
402
+ 47 em_table_free(em_table);
403
+ 48 }
404
+ 49
405
+ 50 /*
406
+ 51 * Function called periodically to check the temperature and
407
+ 52 * update the EM if needed
408
+ 53 */
409
+ 54 static void foo_thermal_em_update(struct foo_context *ctx)
410
+ 55 {
411
+ 56 struct device *dev = ctx->dev;
412
+ 57 int cpu;
413
+ 58
414
+ 59 ctx->temperature = foo_get_temp(dev, ctx);
415
+ 60 if (ctx->temperature < FOO_EM_UPDATE_TEMP_THRESHOLD)
416
+ 61 return;
417
+ 62
418
+ 63 foo_get_new_em(ctx);
419
+ 64 }
0 commit comments