1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
17 #include <rte_memcpy.h>
18 #include <rte_memory.h>
19 #include <rte_string_fns.h>
21 #include "power_pstate_cpufreq.h"
22 #include "power_common.h"
25 #ifdef RTE_LIBRTE_POWER_DEBUG
26 #define POWER_DEBUG_TRACE(fmt, args...) do { \
27 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
30 #define POWER_DEBUG_TRACE(fmt, args...)
33 #define FOPEN_OR_ERR_RET(f, retval) do { \
35 RTE_LOG(ERR, POWER, "File not opened\n"); \
40 #define FOPS_OR_NULL_GOTO(ret, label) do { \
41 if ((ret) == NULL) { \
42 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
47 #define FOPS_OR_ERR_GOTO(ret, label) do { \
49 RTE_LOG(ERR, POWER, "File operations failed\n"); \
54 /* macros used for rounding frequency to nearest 100000 */
55 #define FREQ_ROUNDING_DELTA 50000
56 #define ROUND_FREQ_TO_N_100000 100000
58 #define POWER_CONVERT_TO_DECIMAL 10
59 #define BUS_FREQ 100000
61 #define POWER_GOVERNOR_PERF "performance"
62 #define POWER_SYSFILE_GOVERNOR \
63 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
64 #define POWER_SYSFILE_MAX_FREQ \
65 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
66 #define POWER_SYSFILE_MIN_FREQ \
67 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_min_freq"
68 #define POWER_SYSFILE_CUR_FREQ \
69 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
70 #define POWER_SYSFILE_BASE_MAX_FREQ \
71 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq"
72 #define POWER_SYSFILE_BASE_MIN_FREQ \
73 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_min_freq"
74 #define POWER_SYSFILE_BASE_FREQ \
75 "/sys/devices/system/cpu/cpu%u/cpufreq/base_frequency"
76 #define POWER_PSTATE_DRIVER "intel_pstate"
77 #define POWER_MSR_PATH "/dev/cpu/%u/msr"
82 #define PLATFORM_INFO 0x0CE
83 #define NON_TURBO_MASK 0xFF00
84 #define NON_TURBO_OFFSET 0x8
94 struct pstate_power_info {
95 unsigned int lcore_id; /**< Logical core id */
96 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
97 uint32_t nb_freqs; /**< number of available freqs */
98 FILE *f_cur_min; /**< FD of scaling_min */
99 FILE *f_cur_max; /**< FD of scaling_max */
100 char governor_ori[32]; /**< Original governor name */
101 uint32_t curr_idx; /**< Freq index in freqs array */
102 uint32_t non_turbo_max_ratio; /**< Non Turbo Max ratio */
103 uint32_t sys_max_freq; /**< system wide max freq */
104 uint32_t core_base_freq; /**< core base freq */
105 uint32_t state; /**< Power in use state */
106 uint16_t turbo_available; /**< Turbo Boost available */
107 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
108 uint16_t priority_core; /**< High Performance core */
109 } __rte_cache_aligned;
112 static struct pstate_power_info lcore_power_info[RTE_MAX_LCORE];
115 * It is to read the specific MSR.
119 power_rdmsr(int msr, uint64_t *val, unsigned int lcore_id)
122 char fullpath[PATH_MAX];
124 snprintf(fullpath, sizeof(fullpath), POWER_MSR_PATH, lcore_id);
126 fd = open(fullpath, O_RDONLY);
129 RTE_LOG(ERR, POWER, "Error opening '%s': %s\n", fullpath,
134 ret = pread(fd, val, sizeof(uint64_t), msr);
137 RTE_LOG(ERR, POWER, "Error reading '%s': %s\n", fullpath,
142 POWER_DEBUG_TRACE("MSR Path %s, offset 0x%X for lcore %u\n",
143 fullpath, msr, lcore_id);
145 POWER_DEBUG_TRACE("Ret value %d, content is 0x%"PRIx64"\n", ret, *val);
152 * It is to fopen the sys file for the future setting the lcore frequency.
155 power_init_for_setting_freq(struct pstate_power_info *pi)
157 FILE *f_min, *f_max, *f_base = NULL, *f_base_max;
158 char fullpath_min[PATH_MAX];
159 char fullpath_max[PATH_MAX];
160 char fullpath_base[PATH_MAX];
161 char fullpath_base_max[PATH_MAX];
162 char buf_base[BUFSIZ];
165 uint32_t base_ratio = 0;
166 uint32_t base_max_ratio = 0;
167 uint64_t max_non_turbo = 0;
170 snprintf(fullpath_base_max,
171 sizeof(fullpath_base_max),
172 POWER_SYSFILE_BASE_MAX_FREQ,
174 f_base_max = fopen(fullpath_base_max, "r");
175 FOPEN_OR_ERR_RET(f_base_max, -1);
176 if (f_base_max != NULL) {
177 s_base_max = fgets(buf_base, sizeof(buf_base), f_base_max);
178 FOPS_OR_NULL_GOTO(s_base_max, out);
180 buf_base[BUFSIZ-1] = '\0';
181 if (strlen(buf_base))
182 /* Strip off terminating '\n' */
183 strtok(buf_base, "\n");
186 strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
190 snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ,
192 f_min = fopen(fullpath_min, "rw+");
193 FOPEN_OR_ERR_RET(f_min, -1);
195 snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ,
197 f_max = fopen(fullpath_max, "rw+");
200 FOPEN_OR_ERR_RET(f_max, -1);
202 pi->f_cur_min = f_min;
203 pi->f_cur_max = f_max;
205 snprintf(fullpath_base, sizeof(fullpath_base), POWER_SYSFILE_BASE_FREQ,
208 f_base = fopen(fullpath_base, "r");
209 if (f_base == NULL) {
210 /* No sysfs base_frequency, that's OK, continue without */
213 s_base = fgets(buf_base, sizeof(buf_base), f_base);
214 FOPS_OR_NULL_GOTO(s_base, out);
216 buf_base[BUFSIZ-1] = '\0';
217 if (strlen(buf_base))
218 /* Strip off terminating '\n' */
219 strtok(buf_base, "\n");
221 base_ratio = strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
226 /* Add MSR read to detect turbo status */
228 if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) < 0) {
233 max_non_turbo = (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
235 POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n", max_non_turbo);
237 pi->non_turbo_max_ratio = max_non_turbo;
240 * If base_frequency is reported as greater than the maximum
241 * turbo frequency, that's a known issue with some kernels.
242 * Set base_frequency to max_non_turbo as a workaround.
244 if (base_ratio > base_max_ratio) {
245 /* base_ratio is greater than max turbo. Kernel bug. */
246 pi->priority_core = 0;
251 * If base_frequency is reported as greater than the maximum
252 * non-turbo frequency, then mark it as a high priority core.
254 if (base_ratio > max_non_turbo)
255 pi->priority_core = 1;
257 pi->priority_core = 0;
258 pi->core_base_freq = base_ratio * BUS_FREQ;
267 set_freq_internal(struct pstate_power_info *pi, uint32_t idx)
269 uint32_t target_freq = 0;
271 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
272 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
273 "should be less than %u\n", idx, pi->nb_freqs);
277 /* Check if it is the same as current */
278 if (idx == pi->curr_idx)
281 /* Because Intel Pstate Driver only allow user change min/max hint
282 * User need change the min/max as same value.
284 if (fseek(pi->f_cur_min, 0, SEEK_SET) < 0) {
285 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
286 "for setting frequency for lcore %u\n",
291 if (fseek(pi->f_cur_max, 0, SEEK_SET) < 0) {
292 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
293 "for setting frequency for lcore %u\n",
298 /* Turbo is available and enabled, first freq bucket is sys max freq */
299 if (pi->turbo_available && idx == 0) {
300 if (pi->turbo_enable)
301 target_freq = pi->sys_max_freq;
303 RTE_LOG(ERR, POWER, "Turbo is off, frequency can't be scaled up more %u\n",
308 target_freq = pi->freqs[idx];
310 /* Decrease freq, the min freq should be updated first */
311 if (idx > pi->curr_idx) {
313 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
314 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
315 "lcore %u\n", pi->lcore_id);
319 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
320 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
321 "lcore %u\n", pi->lcore_id);
325 POWER_DEBUG_TRACE("Frequency '%u' to be set for lcore %u\n",
326 target_freq, pi->lcore_id);
328 fflush(pi->f_cur_min);
329 fflush(pi->f_cur_max);
333 /* Increase freq, the max freq should be updated first */
334 if (idx < pi->curr_idx) {
336 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
337 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
338 "lcore %u\n", pi->lcore_id);
342 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
343 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
344 "lcore %u\n", pi->lcore_id);
348 POWER_DEBUG_TRACE("Frequency '%u' to be set for lcore %u\n",
349 target_freq, pi->lcore_id);
351 fflush(pi->f_cur_max);
352 fflush(pi->f_cur_min);
361 * It is to check the current scaling governor by reading sys file, and then
362 * set it into 'performance' if it is not by writing the sys file. The original
363 * governor will be saved for rolling back.
366 power_set_governor_performance(struct pstate_power_info *pi)
371 char fullpath[PATH_MAX];
375 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
377 f = fopen(fullpath, "rw+");
378 FOPEN_OR_ERR_RET(f, ret);
380 s = fgets(buf, sizeof(buf), f);
381 FOPS_OR_NULL_GOTO(s, out);
382 /* Strip off terminating '\n' */
385 /* Check if current governor is performance */
386 if (strncmp(buf, POWER_GOVERNOR_PERF,
387 sizeof(POWER_GOVERNOR_PERF)) == 0) {
389 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
390 "already performance\n", pi->lcore_id);
393 /* Save the original governor */
394 strlcpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
396 /* Write 'performance' to the governor */
397 val = fseek(f, 0, SEEK_SET);
398 FOPS_OR_ERR_GOTO(val, out);
400 val = fputs(POWER_GOVERNOR_PERF, f);
401 FOPS_OR_ERR_GOTO(val, out);
403 /* We need to flush to see if the fputs succeeds */
405 FOPS_OR_ERR_GOTO(val, out);
408 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
409 "set to performance successfully\n", pi->lcore_id);
417 * It is to check the governor and then set the original governor back if
418 * needed by writing the sys file.
421 power_set_governor_original(struct pstate_power_info *pi)
426 char fullpath[PATH_MAX];
430 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
432 f = fopen(fullpath, "rw+");
433 FOPEN_OR_ERR_RET(f, ret);
435 s = fgets(buf, sizeof(buf), f);
436 FOPS_OR_NULL_GOTO(s, out);
438 /* Check if the governor to be set is the same as current */
439 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
441 POWER_DEBUG_TRACE("Power management governor of lcore %u "
442 "has already been set to %s\n",
443 pi->lcore_id, pi->governor_ori);
447 /* Write back the original governor */
448 val = fseek(f, 0, SEEK_SET);
449 FOPS_OR_ERR_GOTO(val, out);
451 val = fputs(pi->governor_ori, f);
452 FOPS_OR_ERR_GOTO(val, out);
455 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
456 "has been set back to %s successfully\n",
457 pi->lcore_id, pi->governor_ori);
465 * It is to get the available frequencies of the specific lcore by reading the
469 power_get_available_freqs(struct pstate_power_info *pi)
474 char buf_min[BUFSIZ];
475 char buf_max[BUFSIZ];
476 char fullpath_min[PATH_MAX];
477 char fullpath_max[PATH_MAX];
479 uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
480 uint32_t i, num_freqs = 0;
482 snprintf(fullpath_max, sizeof(fullpath_max),
483 POWER_SYSFILE_BASE_MAX_FREQ,
485 snprintf(fullpath_min, sizeof(fullpath_min),
486 POWER_SYSFILE_BASE_MIN_FREQ,
489 f_min = fopen(fullpath_min, "r");
490 FOPEN_OR_ERR_RET(f_min, ret);
492 f_max = fopen(fullpath_max, "r");
496 FOPEN_OR_ERR_RET(f_max, ret);
498 s_min = fgets(buf_min, sizeof(buf_min), f_min);
499 FOPS_OR_NULL_GOTO(s_min, out);
501 s_max = fgets(buf_max, sizeof(buf_max), f_max);
502 FOPS_OR_NULL_GOTO(s_max, out);
505 /* Strip the line break if there is */
506 p_min = strchr(buf_min, '\n');
510 p_max = strchr(buf_max, '\n');
514 sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
515 sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
517 if (sys_max_freq < sys_min_freq)
520 pi->sys_max_freq = sys_max_freq;
522 if (pi->priority_core == 1)
523 base_max_freq = pi->core_base_freq;
525 base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
527 POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
532 if (base_max_freq < sys_max_freq)
533 pi->turbo_available = 1;
535 pi->turbo_available = 0;
537 /* If turbo is available then there is one extra freq bucket
538 * to store the sys max freq which value is base_max +1
540 num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
543 /* Generate the freq bucket array.
544 * If turbo is available the freq bucket[0] value is base_max +1
545 * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
547 * If turbo is not available bucket[0] is base_max and so on
549 for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
550 if ((i == 0) && pi->turbo_available)
551 pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
553 pi->freqs[pi->nb_freqs++] =
554 base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
559 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
560 num_freqs, pi->lcore_id);
570 power_get_cur_idx(struct pstate_power_info *pi)
575 char buf_cur[BUFSIZ];
576 char fullpath_cur[PATH_MAX];
578 uint32_t sys_cur_freq = 0;
581 snprintf(fullpath_cur, sizeof(fullpath_cur),
582 POWER_SYSFILE_CUR_FREQ,
584 f_cur = fopen(fullpath_cur, "r");
585 FOPEN_OR_ERR_RET(f_cur, ret);
587 /* initialize the cur_idx to matching current frequency freq index */
588 s_cur = fgets(buf_cur, sizeof(buf_cur), f_cur);
589 FOPS_OR_NULL_GOTO(s_cur, fail);
591 p_cur = strchr(buf_cur, '\n');
594 sys_cur_freq = strtoul(buf_cur, &p_cur, POWER_CONVERT_TO_DECIMAL);
596 /* convert the frequency to nearest 100000 value
597 * Ex: if sys_cur_freq=1396789 then freq_conv=1400000
598 * Ex: if sys_cur_freq=800030 then freq_conv=800000
599 * Ex: if sys_cur_freq=800030 then freq_conv=800000
601 unsigned int freq_conv = 0;
602 freq_conv = (sys_cur_freq + FREQ_ROUNDING_DELTA)
603 / ROUND_FREQ_TO_N_100000;
604 freq_conv = freq_conv * ROUND_FREQ_TO_N_100000;
606 for (i = 0; i < pi->nb_freqs; i++) {
607 if (freq_conv == pi->freqs[i]) {
621 power_pstate_cpufreq_check_supported(void)
623 return cpufreq_check_scaling_driver(POWER_PSTATE_DRIVER);
627 power_pstate_cpufreq_init(unsigned int lcore_id)
629 struct pstate_power_info *pi;
632 if (lcore_id >= RTE_MAX_LCORE) {
633 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
634 lcore_id, RTE_MAX_LCORE - 1U);
638 pi = &lcore_power_info[lcore_id];
639 exp_state = POWER_IDLE;
640 /* The power in use state works as a guard variable between
641 * the CPU frequency control initialization and exit process.
642 * The ACQUIRE memory ordering here pairs with the RELEASE
643 * ordering below as lock to make sure the frequency operations
644 * in the critical section are done under the correct state.
646 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
648 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
649 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
650 "in use\n", lcore_id);
654 pi->lcore_id = lcore_id;
655 /* Check and set the governor */
656 if (power_set_governor_performance(pi) < 0) {
657 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
658 "performance\n", lcore_id);
661 /* Init for setting lcore frequency */
662 if (power_init_for_setting_freq(pi) < 0) {
663 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
664 "lcore %u\n", lcore_id);
668 /* Get the available frequencies */
669 if (power_get_available_freqs(pi) < 0) {
670 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
671 "lcore %u\n", lcore_id);
675 if (power_get_cur_idx(pi) < 0) {
676 RTE_LOG(ERR, POWER, "Cannot get current frequency "
677 "index of lcore %u\n", lcore_id);
681 /* Set freq to max by default */
682 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
683 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
684 "to max\n", lcore_id);
688 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
689 "power management\n", lcore_id);
690 exp_state = POWER_ONGOING;
691 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
692 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
697 exp_state = POWER_ONGOING;
698 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
699 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
705 power_pstate_cpufreq_exit(unsigned int lcore_id)
707 struct pstate_power_info *pi;
710 if (lcore_id >= RTE_MAX_LCORE) {
711 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
712 lcore_id, RTE_MAX_LCORE - 1U);
715 pi = &lcore_power_info[lcore_id];
717 exp_state = POWER_USED;
718 /* The power in use state works as a guard variable between
719 * the CPU frequency control initialization and exit process.
720 * The ACQUIRE memory ordering here pairs with the RELEASE
721 * ordering below as lock to make sure the frequency operations
722 * in the critical section are under done the correct state.
724 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
726 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
727 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
728 "not used\n", lcore_id);
732 /* Close FD of setting freq */
733 fclose(pi->f_cur_min);
734 fclose(pi->f_cur_max);
735 pi->f_cur_min = NULL;
736 pi->f_cur_max = NULL;
738 /* Set the governor back to the original */
739 if (power_set_governor_original(pi) < 0) {
740 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
741 "to the original\n", lcore_id);
745 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
746 "'performance' mode and been set back to the "
747 "original\n", lcore_id);
748 exp_state = POWER_ONGOING;
749 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
750 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
755 exp_state = POWER_ONGOING;
756 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
757 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
764 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
766 struct pstate_power_info *pi;
768 if (lcore_id >= RTE_MAX_LCORE) {
769 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
774 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
778 pi = &lcore_power_info[lcore_id];
779 if (num < pi->nb_freqs) {
780 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
783 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
789 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
791 if (lcore_id >= RTE_MAX_LCORE) {
792 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
793 return RTE_POWER_INVALID_FREQ_INDEX;
796 return lcore_power_info[lcore_id].curr_idx;
801 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
803 if (lcore_id >= RTE_MAX_LCORE) {
804 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
808 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
812 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
814 struct pstate_power_info *pi;
816 if (lcore_id >= RTE_MAX_LCORE) {
817 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
821 pi = &lcore_power_info[lcore_id];
822 if (pi->curr_idx == 0 ||
823 (pi->curr_idx == 1 && pi->turbo_available && !pi->turbo_enable))
826 /* Frequencies in the array are from high to low. */
827 return set_freq_internal(pi, pi->curr_idx - 1);
831 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
833 struct pstate_power_info *pi;
835 if (lcore_id >= RTE_MAX_LCORE) {
836 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
840 pi = &lcore_power_info[lcore_id];
841 if (pi->curr_idx + 1 == pi->nb_freqs)
844 /* Frequencies in the array are from high to low. */
845 return set_freq_internal(pi, pi->curr_idx + 1);
849 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
851 if (lcore_id >= RTE_MAX_LCORE) {
852 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
856 /* Frequencies in the array are from high to low. */
857 if (lcore_power_info[lcore_id].turbo_available) {
858 if (lcore_power_info[lcore_id].turbo_enable)
860 return set_freq_internal(
861 &lcore_power_info[lcore_id], 0);
863 /* Set to max non-turbo */
864 return set_freq_internal(
865 &lcore_power_info[lcore_id], 1);
867 return set_freq_internal(&lcore_power_info[lcore_id], 0);
872 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
874 struct pstate_power_info *pi;
876 if (lcore_id >= RTE_MAX_LCORE) {
877 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
881 pi = &lcore_power_info[lcore_id];
883 /* Frequencies in the array are from high to low. */
884 return set_freq_internal(pi, pi->nb_freqs - 1);
889 power_pstate_turbo_status(unsigned int lcore_id)
891 struct pstate_power_info *pi;
893 if (lcore_id >= RTE_MAX_LCORE) {
894 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
898 pi = &lcore_power_info[lcore_id];
900 return pi->turbo_enable;
904 power_pstate_enable_turbo(unsigned int lcore_id)
906 struct pstate_power_info *pi;
908 if (lcore_id >= RTE_MAX_LCORE) {
909 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
913 pi = &lcore_power_info[lcore_id];
915 if (pi->turbo_available)
916 pi->turbo_enable = 1;
918 pi->turbo_enable = 0;
920 "Failed to enable turbo on lcore %u\n",
930 power_pstate_disable_turbo(unsigned int lcore_id)
932 struct pstate_power_info *pi;
934 if (lcore_id >= RTE_MAX_LCORE) {
935 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
939 pi = &lcore_power_info[lcore_id];
941 pi->turbo_enable = 0;
943 if (pi->turbo_available && pi->curr_idx <= 1) {
944 /* Try to set freq to max by default coming out of turbo */
945 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
947 "Failed to set frequency of lcore %u to max\n",
957 int power_pstate_get_capabilities(unsigned int lcore_id,
958 struct rte_power_core_capabilities *caps)
960 struct pstate_power_info *pi;
962 if (lcore_id >= RTE_MAX_LCORE) {
963 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
967 RTE_LOG(ERR, POWER, "Invalid argument\n");
971 pi = &lcore_power_info[lcore_id];
972 caps->capabilities = 0;
973 caps->turbo = !!(pi->turbo_available);
974 caps->priority = pi->priority_core;