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);
179 /* close the file unconditionally */
183 FOPS_OR_NULL_GOTO(s_base_max, out);
185 buf_base[BUFSIZ-1] = '\0';
186 if (strlen(buf_base))
187 /* Strip off terminating '\n' */
188 strtok(buf_base, "\n");
191 strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
195 snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ,
197 f_min = fopen(fullpath_min, "rw+");
198 FOPEN_OR_ERR_RET(f_min, -1);
200 snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ,
202 f_max = fopen(fullpath_max, "rw+");
205 FOPEN_OR_ERR_RET(f_max, -1);
207 pi->f_cur_min = f_min;
208 pi->f_cur_max = f_max;
210 snprintf(fullpath_base, sizeof(fullpath_base), POWER_SYSFILE_BASE_FREQ,
213 f_base = fopen(fullpath_base, "r");
214 if (f_base == NULL) {
215 /* No sysfs base_frequency, that's OK, continue without */
218 s_base = fgets(buf_base, sizeof(buf_base), f_base);
219 FOPS_OR_NULL_GOTO(s_base, out);
221 buf_base[BUFSIZ-1] = '\0';
222 if (strlen(buf_base))
223 /* Strip off terminating '\n' */
224 strtok(buf_base, "\n");
226 base_ratio = strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
230 /* Add MSR read to detect turbo status */
232 if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) < 0) {
237 max_non_turbo = (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
239 POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n", max_non_turbo);
241 pi->non_turbo_max_ratio = max_non_turbo;
244 * If base_frequency is reported as greater than the maximum
245 * turbo frequency, that's a known issue with some kernels.
246 * Set base_frequency to max_non_turbo as a workaround.
248 if (base_ratio > base_max_ratio) {
249 /* base_ratio is greater than max turbo. Kernel bug. */
250 pi->priority_core = 0;
255 * If base_frequency is reported as greater than the maximum
256 * non-turbo frequency, then mark it as a high priority core.
258 if (base_ratio > max_non_turbo)
259 pi->priority_core = 1;
261 pi->priority_core = 0;
262 pi->core_base_freq = base_ratio * BUS_FREQ;
271 set_freq_internal(struct pstate_power_info *pi, uint32_t idx)
273 uint32_t target_freq = 0;
275 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
276 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
277 "should be less than %u\n", idx, pi->nb_freqs);
281 /* Check if it is the same as current */
282 if (idx == pi->curr_idx)
285 /* Because Intel Pstate Driver only allow user change min/max hint
286 * User need change the min/max as same value.
288 if (fseek(pi->f_cur_min, 0, SEEK_SET) < 0) {
289 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
290 "for setting frequency for lcore %u\n",
295 if (fseek(pi->f_cur_max, 0, SEEK_SET) < 0) {
296 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
297 "for setting frequency for lcore %u\n",
302 /* Turbo is available and enabled, first freq bucket is sys max freq */
303 if (pi->turbo_available && idx == 0) {
304 if (pi->turbo_enable)
305 target_freq = pi->sys_max_freq;
307 RTE_LOG(ERR, POWER, "Turbo is off, frequency can't be scaled up more %u\n",
312 target_freq = pi->freqs[idx];
314 /* Decrease freq, the min freq should be updated first */
315 if (idx > pi->curr_idx) {
317 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
318 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
319 "lcore %u\n", pi->lcore_id);
323 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
324 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
325 "lcore %u\n", pi->lcore_id);
329 POWER_DEBUG_TRACE("Frequency '%u' to be set for lcore %u\n",
330 target_freq, pi->lcore_id);
332 fflush(pi->f_cur_min);
333 fflush(pi->f_cur_max);
337 /* Increase freq, the max freq should be updated first */
338 if (idx < pi->curr_idx) {
340 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
341 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
342 "lcore %u\n", pi->lcore_id);
346 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
347 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
348 "lcore %u\n", pi->lcore_id);
352 POWER_DEBUG_TRACE("Frequency '%u' to be set for lcore %u\n",
353 target_freq, pi->lcore_id);
355 fflush(pi->f_cur_max);
356 fflush(pi->f_cur_min);
365 * It is to check the current scaling governor by reading sys file, and then
366 * set it into 'performance' if it is not by writing the sys file. The original
367 * governor will be saved for rolling back.
370 power_set_governor_performance(struct pstate_power_info *pi)
375 char fullpath[PATH_MAX];
379 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
381 f = fopen(fullpath, "rw+");
382 FOPEN_OR_ERR_RET(f, ret);
384 s = fgets(buf, sizeof(buf), f);
385 FOPS_OR_NULL_GOTO(s, out);
386 /* Strip off terminating '\n' */
389 /* Save the original governor */
390 rte_strscpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
392 /* Check if current governor is performance */
393 if (strncmp(buf, POWER_GOVERNOR_PERF,
394 sizeof(POWER_GOVERNOR_PERF)) == 0) {
396 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
397 "already performance\n", pi->lcore_id);
401 /* Write 'performance' to the governor */
402 val = fseek(f, 0, SEEK_SET);
403 FOPS_OR_ERR_GOTO(val, out);
405 val = fputs(POWER_GOVERNOR_PERF, f);
406 FOPS_OR_ERR_GOTO(val, out);
408 /* We need to flush to see if the fputs succeeds */
410 FOPS_OR_ERR_GOTO(val, out);
413 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
414 "set to performance successfully\n", pi->lcore_id);
422 * It is to check the governor and then set the original governor back if
423 * needed by writing the sys file.
426 power_set_governor_original(struct pstate_power_info *pi)
431 char fullpath[PATH_MAX];
435 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
437 f = fopen(fullpath, "rw+");
438 FOPEN_OR_ERR_RET(f, ret);
440 s = fgets(buf, sizeof(buf), f);
441 FOPS_OR_NULL_GOTO(s, out);
443 /* Check if the governor to be set is the same as current */
444 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
446 POWER_DEBUG_TRACE("Power management governor of lcore %u "
447 "has already been set to %s\n",
448 pi->lcore_id, pi->governor_ori);
452 /* Write back the original governor */
453 val = fseek(f, 0, SEEK_SET);
454 FOPS_OR_ERR_GOTO(val, out);
456 val = fputs(pi->governor_ori, f);
457 FOPS_OR_ERR_GOTO(val, out);
460 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
461 "has been set back to %s successfully\n",
462 pi->lcore_id, pi->governor_ori);
470 * It is to get the available frequencies of the specific lcore by reading the
474 power_get_available_freqs(struct pstate_power_info *pi)
479 char buf_min[BUFSIZ];
480 char buf_max[BUFSIZ];
481 char fullpath_min[PATH_MAX];
482 char fullpath_max[PATH_MAX];
484 uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
485 uint32_t i, num_freqs = 0;
487 snprintf(fullpath_max, sizeof(fullpath_max),
488 POWER_SYSFILE_BASE_MAX_FREQ,
490 snprintf(fullpath_min, sizeof(fullpath_min),
491 POWER_SYSFILE_BASE_MIN_FREQ,
494 f_min = fopen(fullpath_min, "r");
495 FOPEN_OR_ERR_RET(f_min, ret);
497 f_max = fopen(fullpath_max, "r");
501 FOPEN_OR_ERR_RET(f_max, ret);
503 s_min = fgets(buf_min, sizeof(buf_min), f_min);
504 FOPS_OR_NULL_GOTO(s_min, out);
506 s_max = fgets(buf_max, sizeof(buf_max), f_max);
507 FOPS_OR_NULL_GOTO(s_max, out);
510 /* Strip the line break if there is */
511 p_min = strchr(buf_min, '\n');
515 p_max = strchr(buf_max, '\n');
519 sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
520 sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
522 if (sys_max_freq < sys_min_freq)
525 pi->sys_max_freq = sys_max_freq;
527 if (pi->priority_core == 1)
528 base_max_freq = pi->core_base_freq;
530 base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
532 POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
537 if (base_max_freq < sys_max_freq)
538 pi->turbo_available = 1;
540 pi->turbo_available = 0;
542 /* If turbo is available then there is one extra freq bucket
543 * to store the sys max freq which value is base_max +1
545 num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
548 /* Generate the freq bucket array.
549 * If turbo is available the freq bucket[0] value is base_max +1
550 * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
552 * If turbo is not available bucket[0] is base_max and so on
554 for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
555 if ((i == 0) && pi->turbo_available)
556 pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
558 pi->freqs[pi->nb_freqs++] =
559 base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
564 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
565 num_freqs, pi->lcore_id);
575 power_get_cur_idx(struct pstate_power_info *pi)
580 char buf_cur[BUFSIZ];
581 char fullpath_cur[PATH_MAX];
583 uint32_t sys_cur_freq = 0;
586 snprintf(fullpath_cur, sizeof(fullpath_cur),
587 POWER_SYSFILE_CUR_FREQ,
589 f_cur = fopen(fullpath_cur, "r");
590 FOPEN_OR_ERR_RET(f_cur, ret);
592 /* initialize the cur_idx to matching current frequency freq index */
593 s_cur = fgets(buf_cur, sizeof(buf_cur), f_cur);
594 FOPS_OR_NULL_GOTO(s_cur, fail);
596 p_cur = strchr(buf_cur, '\n');
599 sys_cur_freq = strtoul(buf_cur, &p_cur, POWER_CONVERT_TO_DECIMAL);
601 /* convert the frequency to nearest 100000 value
602 * Ex: if sys_cur_freq=1396789 then freq_conv=1400000
603 * Ex: if sys_cur_freq=800030 then freq_conv=800000
604 * Ex: if sys_cur_freq=800030 then freq_conv=800000
606 unsigned int freq_conv = 0;
607 freq_conv = (sys_cur_freq + FREQ_ROUNDING_DELTA)
608 / ROUND_FREQ_TO_N_100000;
609 freq_conv = freq_conv * ROUND_FREQ_TO_N_100000;
611 for (i = 0; i < pi->nb_freqs; i++) {
612 if (freq_conv == pi->freqs[i]) {
626 power_pstate_cpufreq_check_supported(void)
628 return cpufreq_check_scaling_driver(POWER_PSTATE_DRIVER);
632 power_pstate_cpufreq_init(unsigned int lcore_id)
634 struct pstate_power_info *pi;
637 if (lcore_id >= RTE_MAX_LCORE) {
638 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
639 lcore_id, RTE_MAX_LCORE - 1U);
643 pi = &lcore_power_info[lcore_id];
644 exp_state = POWER_IDLE;
645 /* The power in use state works as a guard variable between
646 * the CPU frequency control initialization and exit process.
647 * The ACQUIRE memory ordering here pairs with the RELEASE
648 * ordering below as lock to make sure the frequency operations
649 * in the critical section are done under the correct state.
651 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
653 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
654 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
655 "in use\n", lcore_id);
659 pi->lcore_id = lcore_id;
660 /* Check and set the governor */
661 if (power_set_governor_performance(pi) < 0) {
662 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
663 "performance\n", lcore_id);
666 /* Init for setting lcore frequency */
667 if (power_init_for_setting_freq(pi) < 0) {
668 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
669 "lcore %u\n", lcore_id);
673 /* Get the available frequencies */
674 if (power_get_available_freqs(pi) < 0) {
675 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
676 "lcore %u\n", lcore_id);
680 if (power_get_cur_idx(pi) < 0) {
681 RTE_LOG(ERR, POWER, "Cannot get current frequency "
682 "index of lcore %u\n", lcore_id);
686 /* Set freq to max by default */
687 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
688 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
689 "to max\n", lcore_id);
693 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
694 "power management\n", lcore_id);
695 exp_state = POWER_ONGOING;
696 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
697 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
702 exp_state = POWER_ONGOING;
703 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
704 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
710 power_pstate_cpufreq_exit(unsigned int lcore_id)
712 struct pstate_power_info *pi;
715 if (lcore_id >= RTE_MAX_LCORE) {
716 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
717 lcore_id, RTE_MAX_LCORE - 1U);
720 pi = &lcore_power_info[lcore_id];
722 exp_state = POWER_USED;
723 /* The power in use state works as a guard variable between
724 * the CPU frequency control initialization and exit process.
725 * The ACQUIRE memory ordering here pairs with the RELEASE
726 * ordering below as lock to make sure the frequency operations
727 * in the critical section are under done the correct state.
729 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
731 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
732 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
733 "not used\n", lcore_id);
737 /* Close FD of setting freq */
738 fclose(pi->f_cur_min);
739 fclose(pi->f_cur_max);
740 pi->f_cur_min = NULL;
741 pi->f_cur_max = NULL;
743 /* Set the governor back to the original */
744 if (power_set_governor_original(pi) < 0) {
745 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
746 "to the original\n", lcore_id);
750 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
751 "'performance' mode and been set back to the "
752 "original\n", lcore_id);
753 exp_state = POWER_ONGOING;
754 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
755 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
760 exp_state = POWER_ONGOING;
761 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
762 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
769 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
771 struct pstate_power_info *pi;
773 if (lcore_id >= RTE_MAX_LCORE) {
774 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
779 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
783 pi = &lcore_power_info[lcore_id];
784 if (num < pi->nb_freqs) {
785 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
788 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
794 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
796 if (lcore_id >= RTE_MAX_LCORE) {
797 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
798 return RTE_POWER_INVALID_FREQ_INDEX;
801 return lcore_power_info[lcore_id].curr_idx;
806 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
808 if (lcore_id >= RTE_MAX_LCORE) {
809 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
813 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
817 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
819 struct pstate_power_info *pi;
821 if (lcore_id >= RTE_MAX_LCORE) {
822 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
826 pi = &lcore_power_info[lcore_id];
827 if (pi->curr_idx == 0 ||
828 (pi->curr_idx == 1 && pi->turbo_available && !pi->turbo_enable))
831 /* Frequencies in the array are from high to low. */
832 return set_freq_internal(pi, pi->curr_idx - 1);
836 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
838 struct pstate_power_info *pi;
840 if (lcore_id >= RTE_MAX_LCORE) {
841 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
845 pi = &lcore_power_info[lcore_id];
846 if (pi->curr_idx + 1 == pi->nb_freqs)
849 /* Frequencies in the array are from high to low. */
850 return set_freq_internal(pi, pi->curr_idx + 1);
854 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
856 if (lcore_id >= RTE_MAX_LCORE) {
857 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
861 /* Frequencies in the array are from high to low. */
862 if (lcore_power_info[lcore_id].turbo_available) {
863 if (lcore_power_info[lcore_id].turbo_enable)
865 return set_freq_internal(
866 &lcore_power_info[lcore_id], 0);
868 /* Set to max non-turbo */
869 return set_freq_internal(
870 &lcore_power_info[lcore_id], 1);
872 return set_freq_internal(&lcore_power_info[lcore_id], 0);
877 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
879 struct pstate_power_info *pi;
881 if (lcore_id >= RTE_MAX_LCORE) {
882 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
886 pi = &lcore_power_info[lcore_id];
888 /* Frequencies in the array are from high to low. */
889 return set_freq_internal(pi, pi->nb_freqs - 1);
894 power_pstate_turbo_status(unsigned int lcore_id)
896 struct pstate_power_info *pi;
898 if (lcore_id >= RTE_MAX_LCORE) {
899 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
903 pi = &lcore_power_info[lcore_id];
905 return pi->turbo_enable;
909 power_pstate_enable_turbo(unsigned int lcore_id)
911 struct pstate_power_info *pi;
913 if (lcore_id >= RTE_MAX_LCORE) {
914 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
918 pi = &lcore_power_info[lcore_id];
920 if (pi->turbo_available)
921 pi->turbo_enable = 1;
923 pi->turbo_enable = 0;
925 "Failed to enable turbo on lcore %u\n",
935 power_pstate_disable_turbo(unsigned int lcore_id)
937 struct pstate_power_info *pi;
939 if (lcore_id >= RTE_MAX_LCORE) {
940 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
944 pi = &lcore_power_info[lcore_id];
946 pi->turbo_enable = 0;
948 if (pi->turbo_available && pi->curr_idx <= 1) {
949 /* Try to set freq to max by default coming out of turbo */
950 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
952 "Failed to set frequency of lcore %u to max\n",
962 int power_pstate_get_capabilities(unsigned int lcore_id,
963 struct rte_power_core_capabilities *caps)
965 struct pstate_power_info *pi;
967 if (lcore_id >= RTE_MAX_LCORE) {
968 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
972 RTE_LOG(ERR, POWER, "Invalid argument\n");
976 pi = &lcore_power_info[lcore_id];
977 caps->capabilities = 0;
978 caps->turbo = !!(pi->turbo_available);
979 caps->priority = pi->priority_core;