Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2021 Intel Corporation
3 : : * Copyright(c) 2021 Arm Limited
4 : : */
5 : :
6 : : #include <stdlib.h>
7 : :
8 : : #include <rte_memcpy.h>
9 : : #include <rte_stdatomic.h>
10 : :
11 : : #include "cppc_cpufreq.h"
12 : : #include "power_common.h"
13 : :
14 : : /* macros used for rounding frequency to nearest 100000 */
15 : : #define FREQ_ROUNDING_DELTA 50000
16 : : #define ROUND_FREQ_TO_N_100000 100000
17 : :
18 : : /* the unit of highest_perf and nominal_perf differs on different arm platforms.
19 : : * For highest_perf, it maybe 300 or 3000000, both means 3.0GHz.
20 : : */
21 : : #define UNIT_DIFF 10000
22 : :
23 : : #define POWER_GOVERNOR_USERSPACE "userspace"
24 : : #define POWER_SYSFILE_SETSPEED \
25 : : "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
26 : : #define POWER_SYSFILE_SCALING_MAX_FREQ \
27 : : "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
28 : : #define POWER_SYSFILE_SCALING_MIN_FREQ \
29 : : "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_min_freq"
30 : : #define POWER_SYSFILE_HIGHEST_PERF \
31 : : "/sys/devices/system/cpu/cpu%u/acpi_cppc/highest_perf"
32 : : #define POWER_SYSFILE_NOMINAL_PERF \
33 : : "/sys/devices/system/cpu/cpu%u/acpi_cppc/nominal_perf"
34 : : #define POWER_SYSFILE_SYS_MAX \
35 : : "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq"
36 : :
37 : : #define POWER_CPPC_DRIVER "cppc_cpufreq"
38 : : #define BUS_FREQ 100000
39 : :
40 : : /**
41 : : * Power info per lcore.
42 : : */
43 : : struct __rte_cache_aligned cppc_power_info {
44 : : unsigned int lcore_id; /**< Logical core id */
45 : : RTE_ATOMIC(uint32_t) state; /**< Power in use state */
46 : : FILE *f; /**< FD of scaling_setspeed */
47 : : char governor_ori[32]; /**< Original governor name */
48 : : uint32_t curr_idx; /**< Freq index in freqs array */
49 : : uint32_t highest_perf; /**< system wide max freq */
50 : : uint32_t nominal_perf; /**< system wide nominal freq */
51 : : uint16_t turbo_available; /**< Turbo Boost available */
52 : : uint16_t turbo_enable; /**< Turbo Boost enable/disable */
53 : : uint32_t nb_freqs; /**< number of available freqs */
54 : : uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
55 : : };
56 : :
57 : : static struct cppc_power_info lcore_power_info[RTE_MAX_LCORE];
58 : :
59 : : /**
60 : : * It is to set specific freq for specific logical core, according to the index
61 : : * of supported frequencies.
62 : : */
63 : : static int
64 : 0 : set_freq_internal(struct cppc_power_info *pi, uint32_t idx)
65 : : {
66 : : char buf[16];
67 : :
68 [ # # # # ]: 0 : if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
69 : 0 : POWER_LOG(ERR, "Invalid frequency index %u, which "
70 : : "should be less than %u", idx, pi->nb_freqs);
71 : 0 : return -1;
72 : : }
73 : :
74 : : /* Check if it is the same as current */
75 [ # # ]: 0 : if (idx == pi->curr_idx)
76 : : return 0;
77 : :
78 : : POWER_DEBUG_LOG("Frequency[%u] %u to be set for lcore %u",
79 : : idx, pi->freqs[idx], pi->lcore_id);
80 : :
81 : 0 : snprintf(buf, sizeof(buf), "%u", pi->freqs[idx]);
82 [ # # ]: 0 : if (write_core_sysfs_s(pi->f, buf) != 0) {
83 : 0 : POWER_LOG(ERR, "Fail to write new frequency for "
84 : : "lcore %u", pi->lcore_id);
85 : 0 : return -1;
86 : : }
87 : 0 : pi->curr_idx = idx;
88 : :
89 : 0 : return 1;
90 : : }
91 : :
92 : : /**
93 : : * It is to check the current scaling governor by reading sys file, and then
94 : : * set it into 'userspace' if it is not by writing the sys file. The original
95 : : * governor will be saved for rolling back.
96 : : */
97 : : static int
98 : : power_set_governor_userspace(struct cppc_power_info *pi)
99 : : {
100 : 0 : return power_set_governor(pi->lcore_id, POWER_GOVERNOR_USERSPACE,
101 : 0 : pi->governor_ori, sizeof(pi->governor_ori));
102 : : }
103 : :
104 : : static int
105 : 0 : power_check_turbo(struct cppc_power_info *pi)
106 : : {
107 : 0 : FILE *f_nom = NULL, *f_max = NULL, *f_cmax = NULL;
108 : : int ret = -1;
109 : 0 : uint32_t nominal_perf = 0, highest_perf = 0, cpuinfo_max_freq = 0;
110 : :
111 : 0 : open_core_sysfs_file(&f_max, "r", POWER_SYSFILE_HIGHEST_PERF,
112 : : pi->lcore_id);
113 [ # # ]: 0 : if (f_max == NULL) {
114 : 0 : POWER_LOG(ERR, "failed to open %s",
115 : : POWER_SYSFILE_HIGHEST_PERF);
116 : 0 : goto err;
117 : : }
118 : :
119 : 0 : open_core_sysfs_file(&f_nom, "r", POWER_SYSFILE_NOMINAL_PERF,
120 : : pi->lcore_id);
121 [ # # ]: 0 : if (f_nom == NULL) {
122 : 0 : POWER_LOG(ERR, "failed to open %s",
123 : : POWER_SYSFILE_NOMINAL_PERF);
124 : 0 : goto err;
125 : : }
126 : :
127 : 0 : open_core_sysfs_file(&f_cmax, "r", POWER_SYSFILE_SYS_MAX,
128 : : pi->lcore_id);
129 [ # # ]: 0 : if (f_cmax == NULL) {
130 : 0 : POWER_LOG(ERR, "failed to open %s",
131 : : POWER_SYSFILE_SYS_MAX);
132 : 0 : goto err;
133 : : }
134 : :
135 : 0 : ret = read_core_sysfs_u32(f_max, &highest_perf);
136 [ # # ]: 0 : if (ret < 0) {
137 : 0 : POWER_LOG(ERR, "Failed to read %s",
138 : : POWER_SYSFILE_HIGHEST_PERF);
139 : 0 : goto err;
140 : : }
141 : :
142 : 0 : ret = read_core_sysfs_u32(f_nom, &nominal_perf);
143 [ # # ]: 0 : if (ret < 0) {
144 : 0 : POWER_LOG(ERR, "Failed to read %s",
145 : : POWER_SYSFILE_NOMINAL_PERF);
146 : 0 : goto err;
147 : : }
148 : :
149 : 0 : ret = read_core_sysfs_u32(f_cmax, &cpuinfo_max_freq);
150 [ # # ]: 0 : if (ret < 0) {
151 : 0 : POWER_LOG(ERR, "Failed to read %s",
152 : : POWER_SYSFILE_SYS_MAX);
153 : 0 : goto err;
154 : : }
155 : :
156 : 0 : pi->highest_perf = highest_perf;
157 : 0 : pi->nominal_perf = nominal_perf;
158 : :
159 [ # # # # ]: 0 : if ((highest_perf > nominal_perf) && ((cpuinfo_max_freq == highest_perf)
160 [ # # ]: 0 : || cpuinfo_max_freq == highest_perf * UNIT_DIFF)) {
161 : 0 : pi->turbo_available = 1;
162 : 0 : pi->turbo_enable = 1;
163 : 0 : ret = 0;
164 : : POWER_DEBUG_LOG("Lcore %u can do Turbo Boost! highest perf %u, "
165 : : "nominal perf %u",
166 : : pi->lcore_id, highest_perf, nominal_perf);
167 : : } else {
168 : 0 : pi->turbo_available = 0;
169 : 0 : pi->turbo_enable = 0;
170 : : POWER_DEBUG_LOG("Lcore %u Turbo not available! highest perf %u, "
171 : : "nominal perf %u",
172 : : pi->lcore_id, highest_perf, nominal_perf);
173 : : }
174 : :
175 : 0 : err:
176 [ # # ]: 0 : if (f_max != NULL)
177 : 0 : fclose(f_max);
178 [ # # ]: 0 : if (f_nom != NULL)
179 : 0 : fclose(f_nom);
180 [ # # ]: 0 : if (f_cmax != NULL)
181 : 0 : fclose(f_cmax);
182 : :
183 : 0 : return ret;
184 : : }
185 : :
186 : : /**
187 : : * It is to get the available frequencies of the specific lcore by reading the
188 : : * sys file.
189 : : */
190 : : static int
191 : 0 : power_get_available_freqs(struct cppc_power_info *pi)
192 : : {
193 : 0 : FILE *f_min = NULL, *f_max = NULL;
194 : : int ret = -1;
195 : 0 : uint32_t scaling_min_freq = 0, scaling_max_freq = 0, nominal_perf = 0;
196 : : uint32_t i, num_freqs = 0;
197 : :
198 : 0 : open_core_sysfs_file(&f_max, "r", POWER_SYSFILE_SCALING_MAX_FREQ,
199 : : pi->lcore_id);
200 [ # # ]: 0 : if (f_max == NULL) {
201 : 0 : POWER_LOG(ERR, "failed to open %s",
202 : : POWER_SYSFILE_SCALING_MAX_FREQ);
203 : 0 : goto out;
204 : : }
205 : :
206 : 0 : open_core_sysfs_file(&f_min, "r", POWER_SYSFILE_SCALING_MIN_FREQ,
207 : : pi->lcore_id);
208 [ # # ]: 0 : if (f_min == NULL) {
209 : 0 : POWER_LOG(ERR, "failed to open %s",
210 : : POWER_SYSFILE_SCALING_MIN_FREQ);
211 : 0 : goto out;
212 : : }
213 : :
214 : 0 : ret = read_core_sysfs_u32(f_max, &scaling_max_freq);
215 [ # # ]: 0 : if (ret < 0) {
216 : 0 : POWER_LOG(ERR, "Failed to read %s",
217 : : POWER_SYSFILE_SCALING_MAX_FREQ);
218 : 0 : goto out;
219 : : }
220 : :
221 : 0 : ret = read_core_sysfs_u32(f_min, &scaling_min_freq);
222 [ # # ]: 0 : if (ret < 0) {
223 : 0 : POWER_LOG(ERR, "Failed to read %s",
224 : : POWER_SYSFILE_SCALING_MIN_FREQ);
225 : 0 : goto out;
226 : : }
227 : :
228 : 0 : power_check_turbo(pi);
229 : :
230 [ # # ]: 0 : if (scaling_max_freq < scaling_min_freq)
231 : 0 : goto out;
232 : :
233 : : /* If turbo is available then there is one extra freq bucket
234 : : * to store the sys max freq which value is scaling_max_freq
235 : : */
236 : 0 : nominal_perf = (pi->nominal_perf < UNIT_DIFF) ?
237 [ # # ]: 0 : pi->nominal_perf * UNIT_DIFF : pi->nominal_perf;
238 : 0 : num_freqs = (nominal_perf - scaling_min_freq) / BUS_FREQ + 1 +
239 : 0 : pi->turbo_available;
240 [ # # ]: 0 : if (num_freqs >= RTE_MAX_LCORE_FREQS) {
241 : 0 : POWER_LOG(ERR, "Too many available frequencies: %d",
242 : : num_freqs);
243 : 0 : goto out;
244 : : }
245 : :
246 : : /* Generate the freq bucket array. */
247 [ # # ]: 0 : for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
248 [ # # # # ]: 0 : if ((i == 0) && pi->turbo_available)
249 : 0 : pi->freqs[pi->nb_freqs++] = scaling_max_freq;
250 : : else
251 : 0 : pi->freqs[pi->nb_freqs++] =
252 : 0 : nominal_perf - (i - pi->turbo_available) * BUS_FREQ;
253 : : }
254 : :
255 : : ret = 0;
256 : :
257 : : POWER_DEBUG_LOG("%d frequency(s) of lcore %u are available",
258 : : num_freqs, pi->lcore_id);
259 : :
260 : 0 : out:
261 [ # # ]: 0 : if (f_min != NULL)
262 : 0 : fclose(f_min);
263 [ # # ]: 0 : if (f_max != NULL)
264 : 0 : fclose(f_max);
265 : :
266 : 0 : return ret;
267 : : }
268 : :
269 : : /**
270 : : * It is to fopen the sys file for the future setting the lcore frequency.
271 : : */
272 : : static int
273 : 0 : power_init_for_setting_freq(struct cppc_power_info *pi)
274 : : {
275 : 0 : FILE *f = NULL;
276 : : char buf[BUFSIZ];
277 : : uint32_t i, freq;
278 : : int ret;
279 : :
280 : 0 : open_core_sysfs_file(&f, "rw+", POWER_SYSFILE_SETSPEED, pi->lcore_id);
281 [ # # ]: 0 : if (f == NULL) {
282 : 0 : POWER_LOG(ERR, "failed to open %s",
283 : : POWER_SYSFILE_SETSPEED);
284 : 0 : goto err;
285 : : }
286 : :
287 : 0 : ret = read_core_sysfs_s(f, buf, sizeof(buf));
288 [ # # ]: 0 : if (ret < 0) {
289 : 0 : POWER_LOG(ERR, "Failed to read %s",
290 : : POWER_SYSFILE_SETSPEED);
291 : 0 : goto err;
292 : : }
293 : :
294 : 0 : freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
295 : :
296 : : /* convert the frequency to nearest 100000 value
297 : : * Ex: if freq=1396789 then freq_conv=1400000
298 : : * Ex: if freq=800030 then freq_conv=800000
299 : : */
300 : : unsigned int freq_conv = 0;
301 : 0 : freq_conv = (freq + FREQ_ROUNDING_DELTA)
302 : : / ROUND_FREQ_TO_N_100000;
303 : 0 : freq_conv = freq_conv * ROUND_FREQ_TO_N_100000;
304 : :
305 [ # # ]: 0 : for (i = 0; i < pi->nb_freqs; i++) {
306 [ # # ]: 0 : if (freq_conv == pi->freqs[i]) {
307 : 0 : pi->curr_idx = i;
308 : 0 : pi->f = f;
309 : 0 : return 0;
310 : : }
311 : : }
312 : :
313 : 0 : err:
314 [ # # ]: 0 : if (f != NULL)
315 : 0 : fclose(f);
316 : :
317 : : return -1;
318 : : }
319 : :
320 : : int
321 : 0 : power_cppc_cpufreq_check_supported(void)
322 : : {
323 : 0 : return cpufreq_check_scaling_driver(POWER_CPPC_DRIVER);
324 : : }
325 : :
326 : : int
327 : 0 : power_cppc_cpufreq_init(unsigned int lcore_id)
328 : : {
329 : : struct cppc_power_info *pi;
330 : : uint32_t exp_state;
331 : :
332 [ # # ]: 0 : if (!power_cppc_cpufreq_check_supported()) {
333 : 0 : POWER_LOG(ERR, "%s driver is not supported",
334 : : POWER_CPPC_DRIVER);
335 : 0 : return -1;
336 : : }
337 : :
338 : 0 : pi = &lcore_power_info[lcore_id];
339 : : exp_state = POWER_IDLE;
340 : : /* The power in use state works as a guard variable between
341 : : * the CPU frequency control initialization and exit process.
342 : : * The ACQUIRE memory ordering here pairs with the RELEASE
343 : : * ordering below as lock to make sure the frequency operations
344 : : * in the critical section are done under the correct state.
345 : : */
346 [ # # ]: 0 : if (!rte_atomic_compare_exchange_strong_explicit(&(pi->state), &exp_state,
347 : : POWER_ONGOING,
348 : : rte_memory_order_acquire, rte_memory_order_relaxed)) {
349 : 0 : POWER_LOG(INFO, "Power management of lcore %u is "
350 : : "in use", lcore_id);
351 : 0 : return -1;
352 : : }
353 : :
354 [ # # ]: 0 : if (power_get_lcore_mapped_cpu_id(lcore_id, &pi->lcore_id) < 0) {
355 : 0 : POWER_LOG(ERR, "Cannot get CPU ID mapped for lcore %u", lcore_id);
356 : 0 : return -1;
357 : : }
358 : :
359 : : /* Check and set the governor */
360 [ # # ]: 0 : if (power_set_governor_userspace(pi) < 0) {
361 : 0 : POWER_LOG(ERR, "Cannot set governor of lcore %u to "
362 : : "userspace", lcore_id);
363 : 0 : goto fail;
364 : : }
365 : :
366 : : /* Get the available frequencies */
367 [ # # ]: 0 : if (power_get_available_freqs(pi) < 0) {
368 : 0 : POWER_LOG(ERR, "Cannot get available frequencies of "
369 : : "lcore %u", lcore_id);
370 : 0 : goto fail;
371 : : }
372 : :
373 : : /* Init for setting lcore frequency */
374 [ # # ]: 0 : if (power_init_for_setting_freq(pi) < 0) {
375 : 0 : POWER_LOG(ERR, "Cannot init for setting frequency for "
376 : : "lcore %u", lcore_id);
377 : 0 : goto fail;
378 : : }
379 : :
380 : : /* Set freq to max by default */
381 [ # # ]: 0 : if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
382 : 0 : POWER_LOG(ERR, "Cannot set frequency of lcore %u "
383 : : "to max", lcore_id);
384 : 0 : goto fail;
385 : : }
386 : :
387 : 0 : POWER_LOG(INFO, "Initialized successfully for lcore %u "
388 : : "power management", lcore_id);
389 : :
390 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_USED, rte_memory_order_release);
391 : :
392 : 0 : return 0;
393 : :
394 : 0 : fail:
395 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_UNKNOWN, rte_memory_order_release);
396 : 0 : return -1;
397 : : }
398 : :
399 : : /**
400 : : * It is to check the governor and then set the original governor back if
401 : : * needed by writing the sys file.
402 : : */
403 : : static int
404 : : power_set_governor_original(struct cppc_power_info *pi)
405 : : {
406 : 0 : return power_set_governor(pi->lcore_id, pi->governor_ori, NULL, 0);
407 : : }
408 : :
409 : : int
410 : 0 : power_cppc_cpufreq_exit(unsigned int lcore_id)
411 : : {
412 : : struct cppc_power_info *pi;
413 : : uint32_t exp_state;
414 : :
415 : : pi = &lcore_power_info[lcore_id];
416 : : exp_state = POWER_USED;
417 : : /* The power in use state works as a guard variable between
418 : : * the CPU frequency control initialization and exit process.
419 : : * The ACQUIRE memory ordering here pairs with the RELEASE
420 : : * ordering below as lock to make sure the frequency operations
421 : : * in the critical section are done under the correct state.
422 : : */
423 [ # # ]: 0 : if (!rte_atomic_compare_exchange_strong_explicit(&(pi->state), &exp_state,
424 : : POWER_ONGOING,
425 : : rte_memory_order_acquire, rte_memory_order_relaxed)) {
426 : 0 : POWER_LOG(INFO, "Power management of lcore %u is "
427 : : "not used", lcore_id);
428 : 0 : return -1;
429 : : }
430 : :
431 : : /* Close FD of setting freq */
432 : 0 : fclose(pi->f);
433 : 0 : pi->f = NULL;
434 : :
435 : : /* Set the governor back to the original */
436 [ # # ]: 0 : if (power_set_governor_original(pi) < 0) {
437 : 0 : POWER_LOG(ERR, "Cannot set the governor of %u back "
438 : : "to the original", lcore_id);
439 : 0 : goto fail;
440 : : }
441 : :
442 : 0 : POWER_LOG(INFO, "Power management of lcore %u has exited from "
443 : : "'userspace' mode and been set back to the "
444 : : "original", lcore_id);
445 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_IDLE, rte_memory_order_release);
446 : :
447 : 0 : return 0;
448 : :
449 : : fail:
450 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_UNKNOWN, rte_memory_order_release);
451 : :
452 : 0 : return -1;
453 : : }
454 : :
455 : : uint32_t
456 : 0 : power_cppc_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
457 : : {
458 : : struct cppc_power_info *pi;
459 : :
460 [ # # ]: 0 : if (freqs == NULL) {
461 : 0 : POWER_LOG(ERR, "NULL buffer supplied");
462 : 0 : return 0;
463 : : }
464 : :
465 : : pi = &lcore_power_info[lcore_id];
466 [ # # ]: 0 : if (num < pi->nb_freqs) {
467 : 0 : POWER_LOG(ERR, "Buffer size is not enough");
468 : 0 : return 0;
469 : : }
470 [ # # ]: 0 : rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
471 : :
472 : 0 : return pi->nb_freqs;
473 : : }
474 : :
475 : : uint32_t
476 : 0 : power_cppc_cpufreq_get_freq(unsigned int lcore_id)
477 : : {
478 : 0 : return lcore_power_info[lcore_id].curr_idx;
479 : : }
480 : :
481 : : int
482 : 0 : power_cppc_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
483 : : {
484 : 0 : return set_freq_internal(&(lcore_power_info[lcore_id]), index);
485 : : }
486 : :
487 : : int
488 : 0 : power_cppc_cpufreq_freq_down(unsigned int lcore_id)
489 : : {
490 : : struct cppc_power_info *pi;
491 : :
492 : 0 : pi = &lcore_power_info[lcore_id];
493 [ # # ]: 0 : if (pi->curr_idx + 1 == pi->nb_freqs)
494 : : return 0;
495 : :
496 : : /* Frequencies in the array are from high to low. */
497 : 0 : return set_freq_internal(pi, pi->curr_idx + 1);
498 : : }
499 : :
500 : : int
501 : 0 : power_cppc_cpufreq_freq_up(unsigned int lcore_id)
502 : : {
503 : : struct cppc_power_info *pi;
504 : :
505 : 0 : pi = &lcore_power_info[lcore_id];
506 [ # # # # ]: 0 : if (pi->curr_idx == 0 || (pi->curr_idx == 1 &&
507 [ # # # # ]: 0 : pi->turbo_available && !pi->turbo_enable))
508 : : return 0;
509 : :
510 : : /* Frequencies in the array are from high to low. */
511 : 0 : return set_freq_internal(pi, pi->curr_idx - 1);
512 : : }
513 : :
514 : : int
515 : 0 : power_cppc_cpufreq_freq_max(unsigned int lcore_id)
516 : : {
517 : : /* Frequencies in the array are from high to low. */
518 [ # # ]: 0 : if (lcore_power_info[lcore_id].turbo_available) {
519 [ # # ]: 0 : if (lcore_power_info[lcore_id].turbo_enable)
520 : : /* Set to Turbo */
521 : 0 : return set_freq_internal(
522 : : &lcore_power_info[lcore_id], 0);
523 : : else
524 : : /* Set to max non-turbo */
525 : 0 : return set_freq_internal(
526 : : &lcore_power_info[lcore_id], 1);
527 : : } else
528 : 0 : return set_freq_internal(&lcore_power_info[lcore_id], 0);
529 : : }
530 : :
531 : : int
532 : 0 : power_cppc_cpufreq_freq_min(unsigned int lcore_id)
533 : : {
534 : : struct cppc_power_info *pi;
535 : :
536 : 0 : pi = &lcore_power_info[lcore_id];
537 : :
538 : : /* Frequencies in the array are from high to low. */
539 : 0 : return set_freq_internal(pi, pi->nb_freqs - 1);
540 : : }
541 : :
542 : : int
543 : 0 : power_cppc_turbo_status(unsigned int lcore_id)
544 : : {
545 : : struct cppc_power_info *pi;
546 : :
547 : : pi = &lcore_power_info[lcore_id];
548 : :
549 : 0 : return pi->turbo_enable;
550 : : }
551 : :
552 : : int
553 : 0 : power_cppc_enable_turbo(unsigned int lcore_id)
554 : : {
555 : : struct cppc_power_info *pi;
556 : :
557 : : pi = &lcore_power_info[lcore_id];
558 : :
559 [ # # ]: 0 : if (pi->turbo_available)
560 : 0 : pi->turbo_enable = 1;
561 : : else {
562 : 0 : pi->turbo_enable = 0;
563 : 0 : POWER_LOG(ERR,
564 : : "Failed to enable turbo on lcore %u",
565 : : lcore_id);
566 : 0 : return -1;
567 : : }
568 : :
569 : : /* TODO: must set to max once enabling Turbo? Considering add condition:
570 : : * if ((pi->turbo_available) && (pi->curr_idx <= 1))
571 : : */
572 : : /* Max may have changed, so call to max function */
573 [ # # ]: 0 : if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
574 : 0 : POWER_LOG(ERR,
575 : : "Failed to set frequency of lcore %u to max",
576 : : lcore_id);
577 : 0 : return -1;
578 : : }
579 : :
580 : : return 0;
581 : : }
582 : :
583 : : int
584 : 0 : power_cppc_disable_turbo(unsigned int lcore_id)
585 : : {
586 : : struct cppc_power_info *pi;
587 : :
588 : : pi = &lcore_power_info[lcore_id];
589 : :
590 : 0 : pi->turbo_enable = 0;
591 : :
592 [ # # # # ]: 0 : if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
593 : : /* Try to set freq to max by default coming out of turbo */
594 [ # # ]: 0 : if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
595 : 0 : POWER_LOG(ERR,
596 : : "Failed to set frequency of lcore %u to max",
597 : : lcore_id);
598 : 0 : return -1;
599 : : }
600 : : }
601 : :
602 : : return 0;
603 : : }
604 : :
605 : : int
606 : 0 : power_cppc_get_capabilities(unsigned int lcore_id,
607 : : struct rte_power_core_capabilities *caps)
608 : : {
609 : : struct cppc_power_info *pi;
610 : :
611 [ # # ]: 0 : if (caps == NULL) {
612 : 0 : POWER_LOG(ERR, "Invalid argument");
613 : 0 : return -1;
614 : : }
615 : :
616 : : pi = &lcore_power_info[lcore_id];
617 : 0 : caps->capabilities = 0;
618 : 0 : caps->turbo = !!(pi->turbo_available);
619 : :
620 : 0 : return 0;
621 : : }
622 : :
623 : : static struct rte_power_cpufreq_ops cppc_ops = {
624 : : .name = "cppc",
625 : : .init = power_cppc_cpufreq_init,
626 : : .exit = power_cppc_cpufreq_exit,
627 : : .check_env_support = power_cppc_cpufreq_check_supported,
628 : : .get_avail_freqs = power_cppc_cpufreq_freqs,
629 : : .get_freq = power_cppc_cpufreq_get_freq,
630 : : .set_freq = power_cppc_cpufreq_set_freq,
631 : : .freq_down = power_cppc_cpufreq_freq_down,
632 : : .freq_up = power_cppc_cpufreq_freq_up,
633 : : .freq_max = power_cppc_cpufreq_freq_max,
634 : : .freq_min = power_cppc_cpufreq_freq_min,
635 : : .turbo_status = power_cppc_turbo_status,
636 : : .enable_turbo = power_cppc_enable_turbo,
637 : : .disable_turbo = power_cppc_disable_turbo,
638 : : .get_caps = power_cppc_get_capabilities
639 : : };
640 : :
641 : 305 : RTE_POWER_REGISTER_CPUFREQ_OPS(cppc_ops);
|