1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
17 #include <rte_atomic.h>
18 #include <rte_memcpy.h>
19 #include <rte_memory.h>
20 #include <rte_string_fns.h>
22 #include "power_pstate_cpufreq.h"
23 #include "power_common.h"
26 #ifdef RTE_LIBRTE_POWER_DEBUG
27 #define POWER_DEBUG_TRACE(fmt, args...) do { \
28 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
31 #define POWER_DEBUG_TRACE(fmt, args...)
34 #define FOPEN_OR_ERR_RET(f, retval) do { \
36 RTE_LOG(ERR, POWER, "File not opened\n"); \
41 #define FOPS_OR_NULL_GOTO(ret, label) do { \
42 if ((ret) == NULL) { \
43 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
48 #define FOPS_OR_ERR_GOTO(ret, label) do { \
50 RTE_LOG(ERR, POWER, "File operations failed\n"); \
56 #define POWER_CONVERT_TO_DECIMAL 10
57 #define BUS_FREQ 100000
59 #define POWER_GOVERNOR_PERF "performance"
60 #define POWER_SYSFILE_GOVERNOR \
61 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
62 #define POWER_SYSFILE_MAX_FREQ \
63 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
64 #define POWER_SYSFILE_MIN_FREQ \
65 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_min_freq"
66 #define POWER_SYSFILE_CUR_FREQ \
67 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
68 #define POWER_SYSFILE_BASE_MAX_FREQ \
69 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq"
70 #define POWER_SYSFILE_BASE_MIN_FREQ \
71 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_min_freq"
72 #define POWER_SYSFILE_BASE_FREQ \
73 "/sys/devices/system/cpu/cpu%u/cpufreq/base_frequency"
74 #define POWER_MSR_PATH "/dev/cpu/%u/msr"
79 #define PLATFORM_INFO 0x0CE
80 #define NON_TURBO_MASK 0xFF00
81 #define NON_TURBO_OFFSET 0x8
91 struct pstate_power_info {
92 unsigned int lcore_id; /**< Logical core id */
93 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
94 uint32_t nb_freqs; /**< number of available freqs */
95 FILE *f_cur_min; /**< FD of scaling_min */
96 FILE *f_cur_max; /**< FD of scaling_max */
97 char governor_ori[32]; /**< Original governor name */
98 uint32_t curr_idx; /**< Freq index in freqs array */
99 uint32_t non_turbo_max_ratio; /**< Non Turbo Max ratio */
100 uint32_t sys_max_freq; /**< system wide max freq */
101 uint32_t core_base_freq; /**< core base freq */
102 volatile uint32_t state; /**< Power in use state */
103 uint16_t turbo_available; /**< Turbo Boost available */
104 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
105 uint16_t priority_core; /**< High Performance core */
106 } __rte_cache_aligned;
109 static struct pstate_power_info lcore_power_info[RTE_MAX_LCORE];
112 * It is to read the specific MSR.
116 power_rdmsr(int msr, uint64_t *val, unsigned int lcore_id)
119 char fullpath[PATH_MAX];
121 snprintf(fullpath, sizeof(fullpath), POWER_MSR_PATH, lcore_id);
123 fd = open(fullpath, O_RDONLY);
126 RTE_LOG(ERR, POWER, "Error opening '%s': %s\n", fullpath,
131 ret = pread(fd, val, sizeof(uint64_t), msr);
134 RTE_LOG(ERR, POWER, "Error reading '%s': %s\n", fullpath,
139 POWER_DEBUG_TRACE("MSR Path %s, offset 0x%X for lcore %u\n",
140 fullpath, msr, lcore_id);
142 POWER_DEBUG_TRACE("Ret value %d, content is 0x%"PRIx64"\n", ret, *val);
149 * It is to fopen the sys file for the future setting the lcore frequency.
152 power_init_for_setting_freq(struct pstate_power_info *pi)
154 FILE *f_min, *f_max, *f_base;
155 char fullpath_min[PATH_MAX];
156 char fullpath_max[PATH_MAX];
157 char fullpath_base[PATH_MAX];
158 char buf_base[BUFSIZ];
160 uint32_t base_ratio = 0;
161 uint64_t max_non_turbo = 0;
164 snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ,
167 f_min = fopen(fullpath_min, "rw+");
168 FOPEN_OR_ERR_RET(f_min, -1);
170 snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ,
173 f_max = fopen(fullpath_max, "rw+");
177 FOPEN_OR_ERR_RET(f_max, -1);
179 pi->f_cur_min = f_min;
180 pi->f_cur_max = f_max;
182 snprintf(fullpath_base, sizeof(fullpath_base), POWER_SYSFILE_BASE_FREQ,
185 f_base = fopen(fullpath_base, "r");
186 if (f_base == NULL) {
187 /* No sysfs base_frequency, that's OK, continue without */
190 s_base = fgets(buf_base, sizeof(buf_base), f_base);
191 FOPS_OR_NULL_GOTO(s_base, out);
193 buf_base[BUFSIZ-1] = '\0';
194 if (strlen(buf_base))
195 /* Strip off terminating '\n' */
196 strtok(buf_base, "\n");
198 base_ratio = strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
202 /* Add MSR read to detect turbo status */
204 if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) < 0) {
209 max_non_turbo = (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
211 POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n", max_non_turbo);
213 pi->non_turbo_max_ratio = max_non_turbo;
216 * If base_frequency is reported as greater than the maximum
217 * non-turbo frequency, then mark it as a high priority core.
219 if (base_ratio > max_non_turbo)
220 pi->priority_core = 1;
222 pi->priority_core = 0;
223 pi->core_base_freq = base_ratio * BUS_FREQ;
232 set_freq_internal(struct pstate_power_info *pi, uint32_t idx)
234 uint32_t target_freq = 0;
236 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
237 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
238 "should be less than %u\n", idx, pi->nb_freqs);
242 /* Check if it is the same as current */
243 if (idx == pi->curr_idx)
246 /* Because Intel Pstate Driver only allow user change min/max hint
247 * User need change the min/max as same value.
249 if (fseek(pi->f_cur_min, 0, SEEK_SET) < 0) {
250 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
251 "for setting frequency for lcore %u\n",
256 if (fseek(pi->f_cur_max, 0, SEEK_SET) < 0) {
257 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
258 "for setting frequency for lcore %u\n",
263 /* Turbo is available and enabled, first freq bucket is sys max freq */
264 if (pi->turbo_available && idx == 0) {
265 if (pi->turbo_enable)
266 target_freq = pi->sys_max_freq;
268 RTE_LOG(ERR, POWER, "Turbo is off, frequency can't be scaled up more %u\n",
273 target_freq = pi->freqs[idx];
275 /* Decrease freq, the min freq should be updated first */
276 if (idx > pi->curr_idx) {
278 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
279 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
280 "lcore %u\n", pi->lcore_id);
284 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
285 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
286 "lcore %u\n", pi->lcore_id);
290 POWER_DEBUG_TRACE("Frequency '%u' to be set for lcore %u\n",
291 target_freq, pi->lcore_id);
293 fflush(pi->f_cur_min);
294 fflush(pi->f_cur_max);
298 /* Increase freq, the max freq should be updated first */
299 if (idx < pi->curr_idx) {
301 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
302 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
303 "lcore %u\n", pi->lcore_id);
307 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
308 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
309 "lcore %u\n", pi->lcore_id);
313 POWER_DEBUG_TRACE("Frequency '%u' to be set for lcore %u\n",
314 target_freq, pi->lcore_id);
316 fflush(pi->f_cur_max);
317 fflush(pi->f_cur_min);
326 * It is to check the current scaling governor by reading sys file, and then
327 * set it into 'performance' if it is not by writing the sys file. The original
328 * governor will be saved for rolling back.
331 power_set_governor_performance(struct pstate_power_info *pi)
336 char fullpath[PATH_MAX];
340 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
342 f = fopen(fullpath, "rw+");
343 FOPEN_OR_ERR_RET(f, ret);
345 s = fgets(buf, sizeof(buf), f);
346 FOPS_OR_NULL_GOTO(s, out);
347 /* Strip off terminating '\n' */
350 /* Check if current governor is performance */
351 if (strncmp(buf, POWER_GOVERNOR_PERF,
352 sizeof(POWER_GOVERNOR_PERF)) == 0) {
354 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
355 "already performance\n", pi->lcore_id);
358 /* Save the original governor */
359 strlcpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
361 /* Write 'performance' to the governor */
362 val = fseek(f, 0, SEEK_SET);
363 FOPS_OR_ERR_GOTO(val, out);
365 val = fputs(POWER_GOVERNOR_PERF, f);
366 FOPS_OR_ERR_GOTO(val, out);
368 /* We need to flush to see if the fputs succeeds */
370 FOPS_OR_ERR_GOTO(val, out);
373 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
374 "set to performance successfully\n", pi->lcore_id);
382 * It is to check the governor and then set the original governor back if
383 * needed by writing the sys file.
386 power_set_governor_original(struct pstate_power_info *pi)
391 char fullpath[PATH_MAX];
395 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
397 f = fopen(fullpath, "rw+");
398 FOPEN_OR_ERR_RET(f, ret);
400 s = fgets(buf, sizeof(buf), f);
401 FOPS_OR_NULL_GOTO(s, out);
403 /* Check if the governor to be set is the same as current */
404 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
406 POWER_DEBUG_TRACE("Power management governor of lcore %u "
407 "has already been set to %s\n",
408 pi->lcore_id, pi->governor_ori);
412 /* Write back the original governor */
413 val = fseek(f, 0, SEEK_SET);
414 FOPS_OR_ERR_GOTO(val, out);
416 val = fputs(pi->governor_ori, f);
417 FOPS_OR_ERR_GOTO(val, out);
420 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
421 "has been set back to %s successfully\n",
422 pi->lcore_id, pi->governor_ori);
430 * It is to get the available frequencies of the specific lcore by reading the
434 power_get_available_freqs(struct pstate_power_info *pi)
439 char buf_min[BUFSIZ];
440 char buf_max[BUFSIZ];
441 char fullpath_min[PATH_MAX];
442 char fullpath_max[PATH_MAX];
444 uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
445 uint32_t i, num_freqs = 0;
447 snprintf(fullpath_max, sizeof(fullpath_max),
448 POWER_SYSFILE_BASE_MAX_FREQ,
450 snprintf(fullpath_min, sizeof(fullpath_min),
451 POWER_SYSFILE_BASE_MIN_FREQ,
454 f_min = fopen(fullpath_min, "r");
455 FOPEN_OR_ERR_RET(f_min, ret);
457 f_max = fopen(fullpath_max, "r");
461 FOPEN_OR_ERR_RET(f_max, ret);
463 s_min = fgets(buf_min, sizeof(buf_min), f_min);
464 FOPS_OR_NULL_GOTO(s_min, out);
466 s_max = fgets(buf_max, sizeof(buf_max), f_max);
467 FOPS_OR_NULL_GOTO(s_max, out);
470 /* Strip the line break if there is */
471 p_min = strchr(buf_min, '\n');
475 p_max = strchr(buf_max, '\n');
479 sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
480 sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
482 if (sys_max_freq < sys_min_freq)
485 pi->sys_max_freq = sys_max_freq;
487 if (pi->priority_core == 1)
488 base_max_freq = pi->core_base_freq;
490 base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
492 POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
497 if (base_max_freq < sys_max_freq)
498 pi->turbo_available = 1;
500 pi->turbo_available = 0;
502 /* If turbo is available then there is one extra freq bucket
503 * to store the sys max freq which value is base_max +1
505 num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
508 /* Generate the freq bucket array.
509 * If turbo is available the freq bucket[0] value is base_max +1
510 * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
512 * If turbo is not available bucket[0] is base_max and so on
514 for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
515 if ((i == 0) && pi->turbo_available)
516 pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
518 pi->freqs[pi->nb_freqs++] =
519 base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
524 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
525 num_freqs, pi->lcore_id);
535 power_pstate_cpufreq_init(unsigned int lcore_id)
537 struct pstate_power_info *pi;
539 if (lcore_id >= RTE_MAX_LCORE) {
540 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
541 lcore_id, RTE_MAX_LCORE - 1U);
545 pi = &lcore_power_info[lcore_id];
546 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
548 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
549 "in use\n", lcore_id);
553 pi->lcore_id = lcore_id;
554 /* Check and set the governor */
555 if (power_set_governor_performance(pi) < 0) {
556 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
557 "performance\n", lcore_id);
560 /* Init for setting lcore frequency */
561 if (power_init_for_setting_freq(pi) < 0) {
562 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
563 "lcore %u\n", lcore_id);
567 /* Get the available frequencies */
568 if (power_get_available_freqs(pi) < 0) {
569 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
570 "lcore %u\n", lcore_id);
575 /* Set freq to max by default */
576 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
577 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
578 "to max\n", lcore_id);
582 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
583 "power management\n", lcore_id);
584 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
589 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
595 power_pstate_cpufreq_exit(unsigned int lcore_id)
597 struct pstate_power_info *pi;
599 if (lcore_id >= RTE_MAX_LCORE) {
600 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
601 lcore_id, RTE_MAX_LCORE - 1U);
604 pi = &lcore_power_info[lcore_id];
606 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
608 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
609 "not used\n", lcore_id);
613 /* Close FD of setting freq */
614 fclose(pi->f_cur_min);
615 fclose(pi->f_cur_max);
616 pi->f_cur_min = NULL;
617 pi->f_cur_max = NULL;
619 /* Set the governor back to the original */
620 if (power_set_governor_original(pi) < 0) {
621 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
622 "to the original\n", lcore_id);
626 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
627 "'performance' mode and been set back to the "
628 "original\n", lcore_id);
629 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
634 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
641 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
643 struct pstate_power_info *pi;
645 if (lcore_id >= RTE_MAX_LCORE) {
646 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
651 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
655 pi = &lcore_power_info[lcore_id];
656 if (num < pi->nb_freqs) {
657 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
660 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
666 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
668 if (lcore_id >= RTE_MAX_LCORE) {
669 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
670 return RTE_POWER_INVALID_FREQ_INDEX;
673 return lcore_power_info[lcore_id].curr_idx;
678 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
680 if (lcore_id >= RTE_MAX_LCORE) {
681 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
685 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
689 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
691 struct pstate_power_info *pi;
693 if (lcore_id >= RTE_MAX_LCORE) {
694 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
698 pi = &lcore_power_info[lcore_id];
699 if (pi->curr_idx == 0 ||
700 (pi->curr_idx == 1 && pi->turbo_available && !pi->turbo_enable))
703 /* Frequencies in the array are from high to low. */
704 return set_freq_internal(pi, pi->curr_idx - 1);
708 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
710 struct pstate_power_info *pi;
712 if (lcore_id >= RTE_MAX_LCORE) {
713 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
717 pi = &lcore_power_info[lcore_id];
718 if (pi->curr_idx + 1 == pi->nb_freqs)
721 /* Frequencies in the array are from high to low. */
722 return set_freq_internal(pi, pi->curr_idx + 1);
726 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
728 if (lcore_id >= RTE_MAX_LCORE) {
729 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
733 /* Frequencies in the array are from high to low. */
734 if (lcore_power_info[lcore_id].turbo_available) {
735 if (lcore_power_info[lcore_id].turbo_enable)
737 return set_freq_internal(
738 &lcore_power_info[lcore_id], 0);
740 /* Set to max non-turbo */
741 return set_freq_internal(
742 &lcore_power_info[lcore_id], 1);
744 return set_freq_internal(&lcore_power_info[lcore_id], 0);
749 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
751 struct pstate_power_info *pi;
753 if (lcore_id >= RTE_MAX_LCORE) {
754 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
758 pi = &lcore_power_info[lcore_id];
760 /* Frequencies in the array are from high to low. */
761 return set_freq_internal(pi, pi->nb_freqs - 1);
766 power_pstate_turbo_status(unsigned int lcore_id)
768 struct pstate_power_info *pi;
770 if (lcore_id >= RTE_MAX_LCORE) {
771 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
775 pi = &lcore_power_info[lcore_id];
777 return pi->turbo_enable;
781 power_pstate_enable_turbo(unsigned int lcore_id)
783 struct pstate_power_info *pi;
785 if (lcore_id >= RTE_MAX_LCORE) {
786 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
790 pi = &lcore_power_info[lcore_id];
792 if (pi->turbo_available)
793 pi->turbo_enable = 1;
795 pi->turbo_enable = 0;
797 "Failed to enable turbo on lcore %u\n",
807 power_pstate_disable_turbo(unsigned int lcore_id)
809 struct pstate_power_info *pi;
811 if (lcore_id >= RTE_MAX_LCORE) {
812 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
816 pi = &lcore_power_info[lcore_id];
818 pi->turbo_enable = 0;
820 if (pi->turbo_available && pi->curr_idx <= 1) {
821 /* Try to set freq to max by default coming out of turbo */
822 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
824 "Failed to set frequency of lcore %u to max\n",
834 int power_pstate_get_capabilities(unsigned int lcore_id,
835 struct rte_power_core_capabilities *caps)
837 struct pstate_power_info *pi;
839 if (lcore_id >= RTE_MAX_LCORE) {
840 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
844 RTE_LOG(ERR, POWER, "Invalid argument\n");
848 pi = &lcore_power_info[lcore_id];
849 caps->capabilities = 0;
850 caps->turbo = !!(pi->turbo_available);
851 caps->priority = pi->priority_core;