1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
17 #include <rte_string_fns.h>
18 #include <rte_memcpy.h>
19 #include <rte_atomic.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 openned\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"); \
55 #define POWER_CONVERT_TO_DECIMAL 10
56 #define BUS_FREQ 100000
58 #define POWER_GOVERNOR_PERF "performance"
59 #define POWER_SYSFILE_GOVERNOR \
60 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
61 #define POWER_SYSFILE_MAX_FREQ \
62 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
63 #define POWER_SYSFILE_MIN_FREQ \
64 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_min_freq"
65 #define POWER_SYSFILE_CUR_FREQ \
66 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
67 #define POWER_SYSFILE_BASE_MAX_FREQ \
68 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq"
69 #define POWER_SYSFILE_BASE_MIN_FREQ \
70 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_min_freq"
71 #define POWER_SYSFILE_BASE_FREQ \
72 "/sys/devices/system/cpu/cpu%u/cpufreq/base_frequency"
73 #define POWER_MSR_PATH "/dev/cpu/%u/msr"
78 #define PLATFORM_INFO 0x0CE
79 #define NON_TURBO_MASK 0xFF00
80 #define NON_TURBO_OFFSET 0x8
90 struct pstate_power_info {
91 unsigned int lcore_id; /**< Logical core id */
92 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
93 uint32_t nb_freqs; /**< number of available freqs */
94 FILE *f_cur_min; /**< FD of scaling_min */
95 FILE *f_cur_max; /**< FD of scaling_max */
96 char governor_ori[32]; /**< Original governor name */
97 uint32_t curr_idx; /**< Freq index in freqs array */
98 uint32_t non_turbo_max_ratio; /**< Non Turbo Max ratio */
99 uint32_t sys_max_freq; /**< system wide max freq */
100 uint32_t core_base_freq; /**< core base freq */
101 volatile uint32_t state; /**< Power in use state */
102 uint16_t turbo_available; /**< Turbo Boost available */
103 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
104 uint16_t priority_core; /**< High Performance core */
105 } __rte_cache_aligned;
108 static struct pstate_power_info lcore_power_info[RTE_MAX_LCORE];
111 * It is to read the specific MSR.
115 power_rdmsr(int msr, uint64_t *val, unsigned int lcore_id)
118 char fullpath[PATH_MAX];
120 snprintf(fullpath, sizeof(fullpath), POWER_MSR_PATH, lcore_id);
122 fd = open(fullpath, O_RDONLY);
125 RTE_LOG(ERR, POWER, "Error opening '%s': %s\n", fullpath,
130 ret = pread(fd, val, sizeof(uint64_t), msr);
133 RTE_LOG(ERR, POWER, "Error reading '%s': %s\n", fullpath,
138 POWER_DEBUG_TRACE("MSR Path %s, offset 0x%X for lcore %u\n",
139 fullpath, msr, lcore_id);
141 POWER_DEBUG_TRACE("Ret value %d, content is 0x%"PRIx64"\n", ret, *val);
148 * It is to fopen the sys file for the future setting the lcore frequency.
151 power_init_for_setting_freq(struct pstate_power_info *pi)
153 FILE *f_min, *f_max, *f_base;
154 char fullpath_min[PATH_MAX];
155 char fullpath_max[PATH_MAX];
156 char fullpath_base[PATH_MAX];
157 char buf_base[BUFSIZ];
159 uint32_t base_ratio = 0;
160 uint64_t max_non_turbo = 0;
162 snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ,
165 f_min = fopen(fullpath_min, "rw+");
166 FOPEN_OR_ERR_RET(f_min, -1);
168 snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ,
171 f_max = fopen(fullpath_max, "rw+");
175 FOPEN_OR_ERR_RET(f_max, -1);
177 pi->f_cur_min = f_min;
178 pi->f_cur_max = f_max;
180 snprintf(fullpath_base, sizeof(fullpath_base), POWER_SYSFILE_BASE_FREQ,
183 f_base = fopen(fullpath_base, "r");
184 if (f_base == NULL) {
185 /* No sysfs base_frequency, that's OK, continue without */
188 s_base = fgets(buf_base, sizeof(buf_base), f_base);
189 FOPS_OR_NULL_GOTO(s_base, out);
191 buf_base[BUFSIZ-1] = '\0';
192 if (strlen(buf_base))
193 /* Strip off terminating '\n' */
194 strtok(buf_base, "\n");
196 base_ratio = strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
200 /* Add MSR read to detect turbo status */
202 if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) < 0)
205 max_non_turbo = (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
207 POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n", max_non_turbo);
209 pi->non_turbo_max_ratio = max_non_turbo;
212 * If base_frequency is reported as greater than the maximum
213 * non-turbo frequency, then mark it as a high priority core.
215 if (base_ratio > max_non_turbo)
216 pi->priority_core = 1;
218 pi->priority_core = 0;
219 pi->core_base_freq = base_ratio * BUS_FREQ;
226 set_freq_internal(struct pstate_power_info *pi, uint32_t idx)
228 uint32_t target_freq = 0;
230 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
231 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
232 "should be less than %u\n", idx, pi->nb_freqs);
236 /* Check if it is the same as current */
237 if (idx == pi->curr_idx)
240 /* Because Intel Pstate Driver only allow user change min/max hint
241 * User need change the min/max as same value.
243 if (fseek(pi->f_cur_min, 0, SEEK_SET) < 0) {
244 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
245 "for setting frequency for lcore %u\n",
250 if (fseek(pi->f_cur_max, 0, SEEK_SET) < 0) {
251 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
252 "for setting frequency for lcore %u\n",
257 /* Turbo is available and enabled, first freq bucket is sys max freq */
258 if (pi->turbo_available && idx == 0) {
259 if (pi->turbo_enable)
260 target_freq = pi->sys_max_freq;
262 RTE_LOG(ERR, POWER, "Turbo is off, frequency can't be scaled up more %u\n",
267 target_freq = pi->freqs[idx];
269 /* Decrease freq, the min freq should be updated first */
270 if (idx > pi->curr_idx) {
272 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
273 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
274 "lcore %u\n", pi->lcore_id);
278 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
279 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
280 "lcore %u\n", pi->lcore_id);
284 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
285 target_freq, pi->lcore_id);
287 fflush(pi->f_cur_min);
288 fflush(pi->f_cur_max);
292 /* Increase freq, the max freq should be updated first */
293 if (idx < pi->curr_idx) {
295 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
296 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
297 "lcore %u\n", pi->lcore_id);
301 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
302 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
303 "lcore %u\n", pi->lcore_id);
307 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
308 target_freq, pi->lcore_id);
310 fflush(pi->f_cur_max);
311 fflush(pi->f_cur_min);
320 * It is to check the current scaling governor by reading sys file, and then
321 * set it into 'performance' if it is not by writing the sys file. The original
322 * governor will be saved for rolling back.
325 power_set_governor_performance(struct pstate_power_info *pi)
330 char fullpath[PATH_MAX];
334 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
336 f = fopen(fullpath, "rw+");
337 FOPEN_OR_ERR_RET(f, ret);
339 s = fgets(buf, sizeof(buf), f);
340 FOPS_OR_NULL_GOTO(s, out);
341 /* Strip off terminating '\n' */
344 /* Check if current governor is performance */
345 if (strncmp(buf, POWER_GOVERNOR_PERF,
346 sizeof(POWER_GOVERNOR_PERF)) == 0) {
348 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
349 "already performance\n", pi->lcore_id);
352 /* Save the original governor */
353 strlcpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
355 /* Write 'performance' to the governor */
356 val = fseek(f, 0, SEEK_SET);
357 FOPS_OR_ERR_GOTO(val, out);
359 val = fputs(POWER_GOVERNOR_PERF, f);
360 FOPS_OR_ERR_GOTO(val, out);
362 /* We need to flush to see if the fputs succeeds */
364 FOPS_OR_ERR_GOTO(val, out);
367 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
368 "set to performance successfully\n", pi->lcore_id);
376 * It is to check the governor and then set the original governor back if
377 * needed by writing the sys file.
380 power_set_governor_original(struct pstate_power_info *pi)
385 char fullpath[PATH_MAX];
389 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
391 f = fopen(fullpath, "rw+");
392 FOPEN_OR_ERR_RET(f, ret);
394 s = fgets(buf, sizeof(buf), f);
395 FOPS_OR_NULL_GOTO(s, out);
397 /* Check if the governor to be set is the same as current */
398 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
400 POWER_DEBUG_TRACE("Power management governor of lcore %u "
401 "has already been set to %s\n",
402 pi->lcore_id, pi->governor_ori);
406 /* Write back the original governor */
407 val = fseek(f, 0, SEEK_SET);
408 FOPS_OR_ERR_GOTO(val, out);
410 val = fputs(pi->governor_ori, f);
411 FOPS_OR_ERR_GOTO(val, out);
414 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
415 "has been set back to %s successfully\n",
416 pi->lcore_id, pi->governor_ori);
424 * It is to get the available frequencies of the specific lcore by reading the
428 power_get_available_freqs(struct pstate_power_info *pi)
433 char buf_min[BUFSIZ];
434 char buf_max[BUFSIZ];
435 char fullpath_min[PATH_MAX];
436 char fullpath_max[PATH_MAX];
438 uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
439 uint32_t i, num_freqs = 0;
441 snprintf(fullpath_max, sizeof(fullpath_max),
442 POWER_SYSFILE_BASE_MAX_FREQ,
444 snprintf(fullpath_min, sizeof(fullpath_min),
445 POWER_SYSFILE_BASE_MIN_FREQ,
448 f_min = fopen(fullpath_min, "r");
449 FOPEN_OR_ERR_RET(f_min, ret);
451 f_max = fopen(fullpath_max, "r");
455 FOPEN_OR_ERR_RET(f_max, ret);
457 s_min = fgets(buf_min, sizeof(buf_min), f_min);
458 FOPS_OR_NULL_GOTO(s_min, out);
460 s_max = fgets(buf_max, sizeof(buf_max), f_max);
461 FOPS_OR_NULL_GOTO(s_max, out);
464 /* Strip the line break if there is */
465 p_min = strchr(buf_min, '\n');
469 p_max = strchr(buf_max, '\n');
473 sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
474 sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
476 if (sys_max_freq < sys_min_freq)
479 pi->sys_max_freq = sys_max_freq;
481 if (pi->priority_core == 1)
482 base_max_freq = pi->core_base_freq;
484 base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
486 POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
491 if (base_max_freq < sys_max_freq)
492 pi->turbo_available = 1;
494 pi->turbo_available = 0;
496 /* If turbo is available then there is one extra freq bucket
497 * to store the sys max freq which value is base_max +1
499 num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
502 /* Generate the freq bucket array.
503 * If turbo is available the freq bucket[0] value is base_max +1
504 * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
506 * If turbo is not available bucket[0] is base_max and so on
508 for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
509 if ((i == 0) && pi->turbo_available)
510 pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
512 pi->freqs[pi->nb_freqs++] =
513 base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
518 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
519 num_freqs, pi->lcore_id);
529 power_pstate_cpufreq_init(unsigned int lcore_id)
531 struct pstate_power_info *pi;
533 if (lcore_id >= RTE_MAX_LCORE) {
534 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
535 lcore_id, RTE_MAX_LCORE - 1U);
539 pi = &lcore_power_info[lcore_id];
540 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
542 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
543 "in use\n", lcore_id);
547 pi->lcore_id = lcore_id;
548 /* Check and set the governor */
549 if (power_set_governor_performance(pi) < 0) {
550 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
551 "performance\n", lcore_id);
554 /* Init for setting lcore frequency */
555 if (power_init_for_setting_freq(pi) < 0) {
556 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
557 "lcore %u\n", lcore_id);
561 /* Get the available frequencies */
562 if (power_get_available_freqs(pi) < 0) {
563 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
564 "lcore %u\n", lcore_id);
569 /* Set freq to max by default */
570 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
571 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
572 "to max\n", lcore_id);
576 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
577 "power management\n", lcore_id);
578 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
583 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
589 power_pstate_cpufreq_exit(unsigned int lcore_id)
591 struct pstate_power_info *pi;
593 if (lcore_id >= RTE_MAX_LCORE) {
594 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
595 lcore_id, RTE_MAX_LCORE - 1U);
598 pi = &lcore_power_info[lcore_id];
600 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
602 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
603 "not used\n", lcore_id);
607 /* Close FD of setting freq */
608 fclose(pi->f_cur_min);
609 fclose(pi->f_cur_max);
610 pi->f_cur_min = NULL;
611 pi->f_cur_max = NULL;
613 /* Set the governor back to the original */
614 if (power_set_governor_original(pi) < 0) {
615 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
616 "to the original\n", lcore_id);
620 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
621 "'performance' mode and been set back to the "
622 "original\n", lcore_id);
623 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
628 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
635 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
637 struct pstate_power_info *pi;
639 if (lcore_id >= RTE_MAX_LCORE) {
640 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
645 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
649 pi = &lcore_power_info[lcore_id];
650 if (num < pi->nb_freqs) {
651 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
654 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
660 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
662 if (lcore_id >= RTE_MAX_LCORE) {
663 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
664 return RTE_POWER_INVALID_FREQ_INDEX;
667 return lcore_power_info[lcore_id].curr_idx;
672 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
674 if (lcore_id >= RTE_MAX_LCORE) {
675 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
679 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
683 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
685 struct pstate_power_info *pi;
687 if (lcore_id >= RTE_MAX_LCORE) {
688 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
692 pi = &lcore_power_info[lcore_id];
693 if (pi->curr_idx == 0)
696 /* Frequencies in the array are from high to low. */
697 return set_freq_internal(pi, pi->curr_idx - 1);
701 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
703 struct pstate_power_info *pi;
705 if (lcore_id >= RTE_MAX_LCORE) {
706 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
710 pi = &lcore_power_info[lcore_id];
711 if (pi->curr_idx + 1 == pi->nb_freqs)
714 /* Frequencies in the array are from high to low. */
715 return set_freq_internal(pi, pi->curr_idx + 1);
719 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
721 if (lcore_id >= RTE_MAX_LCORE) {
722 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
726 /* Frequencies in the array are from high to low. */
727 if (lcore_power_info[lcore_id].turbo_available) {
728 if (lcore_power_info[lcore_id].turbo_enable)
730 return set_freq_internal(
731 &lcore_power_info[lcore_id], 0);
733 /* Set to max non-turbo */
734 return set_freq_internal(
735 &lcore_power_info[lcore_id], 1);
737 return set_freq_internal(&lcore_power_info[lcore_id], 0);
742 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
744 struct pstate_power_info *pi;
746 if (lcore_id >= RTE_MAX_LCORE) {
747 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
751 pi = &lcore_power_info[lcore_id];
753 /* Frequencies in the array are from high to low. */
754 return set_freq_internal(pi, pi->nb_freqs - 1);
759 power_pstate_turbo_status(unsigned int lcore_id)
761 struct pstate_power_info *pi;
763 if (lcore_id >= RTE_MAX_LCORE) {
764 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
768 pi = &lcore_power_info[lcore_id];
770 return pi->turbo_enable;
774 power_pstate_enable_turbo(unsigned int lcore_id)
776 struct pstate_power_info *pi;
778 if (lcore_id >= RTE_MAX_LCORE) {
779 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
783 pi = &lcore_power_info[lcore_id];
785 if (pi->turbo_available)
786 pi->turbo_enable = 1;
788 pi->turbo_enable = 0;
790 "Failed to enable turbo on lcore %u\n",
800 power_pstate_disable_turbo(unsigned int lcore_id)
802 struct pstate_power_info *pi;
804 if (lcore_id >= RTE_MAX_LCORE) {
805 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
809 pi = &lcore_power_info[lcore_id];
811 pi->turbo_enable = 0;
818 int power_pstate_get_capabilities(unsigned int lcore_id,
819 struct rte_power_core_capabilities *caps)
821 struct pstate_power_info *pi;
823 if (lcore_id >= RTE_MAX_LCORE) {
824 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
828 RTE_LOG(ERR, POWER, "Invalid argument\n");
832 pi = &lcore_power_info[lcore_id];
833 caps->capabilities = 0;
834 caps->turbo = !!(pi->turbo_available);
835 caps->priority = pi->priority_core;