1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
17 #include <rte_memcpy.h>
18 #include <rte_atomic.h>
20 #include "power_pstate_cpufreq.h"
21 #include "power_common.h"
24 #ifdef RTE_LIBRTE_POWER_DEBUG
25 #define POWER_DEBUG_TRACE(fmt, args...) do { \
26 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
29 #define POWER_DEBUG_TRACE(fmt, args...)
32 #define FOPEN_OR_ERR_RET(f, retval) do { \
34 RTE_LOG(ERR, POWER, "File not openned\n"); \
39 #define FOPS_OR_NULL_GOTO(ret, label) do { \
40 if ((ret) == NULL) { \
41 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
46 #define FOPS_OR_ERR_GOTO(ret, label) do { \
48 RTE_LOG(ERR, POWER, "File operations failed\n"); \
54 #define POWER_CONVERT_TO_DECIMAL 10
55 #define BUS_FREQ 100000
57 #define POWER_GOVERNOR_PERF "performance"
58 #define POWER_SYSFILE_GOVERNOR \
59 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
60 #define POWER_SYSFILE_MAX_FREQ \
61 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
62 #define POWER_SYSFILE_MIN_FREQ \
63 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_min_freq"
64 #define POWER_SYSFILE_CUR_FREQ \
65 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
66 #define POWER_SYSFILE_BASE_MAX_FREQ \
67 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq"
68 #define POWER_SYSFILE_BASE_MIN_FREQ \
69 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_min_freq"
70 #define POWER_MSR_PATH "/dev/cpu/%u/msr"
75 #define PLATFORM_INFO 0x0CE
76 #define NON_TURBO_MASK 0xFF00
77 #define NON_TURBO_OFFSET 0x8
87 struct pstate_power_info {
88 unsigned int lcore_id; /**< Logical core id */
89 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
90 uint32_t nb_freqs; /**< number of available freqs */
91 FILE *f_cur_min; /**< FD of scaling_min */
92 FILE *f_cur_max; /**< FD of scaling_max */
93 char governor_ori[32]; /**< Original governor name */
94 uint32_t curr_idx; /**< Freq index in freqs array */
95 uint32_t non_turbo_max_ratio; /**< Non Turbo Max ratio */
96 uint32_t sys_max_freq; /**< system wide max freq */
97 volatile uint32_t state; /**< Power in use state */
98 uint16_t turbo_available; /**< Turbo Boost available */
99 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
100 } __rte_cache_aligned;
103 static struct pstate_power_info lcore_power_info[RTE_MAX_LCORE];
106 * It is to read the specific MSR.
110 power_rdmsr(int msr, uint64_t *val, unsigned int lcore_id)
113 char fullpath[PATH_MAX];
115 snprintf(fullpath, sizeof(fullpath), POWER_MSR_PATH, lcore_id);
117 fd = open(fullpath, O_RDONLY);
120 RTE_LOG(ERR, POWER, "Error opening '%s': %s\n", fullpath,
125 ret = pread(fd, val, sizeof(uint64_t), msr);
128 RTE_LOG(ERR, POWER, "Error reading '%s': %s\n", fullpath,
133 POWER_DEBUG_TRACE("MSR Path %s, offset 0x%X for lcore %u\n",
134 fullpath, msr, lcore_id);
136 POWER_DEBUG_TRACE("Ret value %d, content is 0x%"PRIx64"\n", ret, *val);
143 * It is to fopen the sys file for the future setting the lcore frequency.
146 power_init_for_setting_freq(struct pstate_power_info *pi)
149 char fullpath_min[PATH_MAX];
150 char fullpath_max[PATH_MAX];
151 uint64_t max_non_turbo = 0;
153 snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ,
156 f_min = fopen(fullpath_min, "rw+");
157 FOPEN_OR_ERR_RET(f_min, -1);
159 snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ,
162 f_max = fopen(fullpath_max, "rw+");
163 FOPEN_OR_ERR_RET(f_max, -1);
165 pi->f_cur_min = f_min;
166 pi->f_cur_max = f_max;
168 /* Add MSR read to detect turbo status */
170 if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) < 0)
173 max_non_turbo = (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
175 POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n", max_non_turbo);
177 pi->non_turbo_max_ratio = max_non_turbo;
183 set_freq_internal(struct pstate_power_info *pi, uint32_t idx)
185 uint32_t target_freq = 0;
187 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
188 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
189 "should be less than %u\n", idx, pi->nb_freqs);
193 /* Check if it is the same as current */
194 if (idx == pi->curr_idx)
197 /* Because Intel Pstate Driver only allow user change min/max hint
198 * User need change the min/max as same value.
200 if (fseek(pi->f_cur_min, 0, SEEK_SET) < 0) {
201 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
202 "for setting frequency for lcore %u\n",
207 if (fseek(pi->f_cur_max, 0, SEEK_SET) < 0) {
208 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
209 "for setting frequency for lcore %u\n",
214 /* Turbo is available and enabled, first freq bucket is sys max freq */
215 if (pi->turbo_available && pi->turbo_enable && (idx == 0))
216 target_freq = pi->sys_max_freq;
218 target_freq = pi->freqs[idx];
220 /* Decrease freq, the min freq should be updated first */
221 if (idx > pi->curr_idx) {
223 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
224 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
225 "lcore %u\n", pi->lcore_id);
229 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
230 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
231 "lcore %u\n", pi->lcore_id);
235 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
236 target_freq, pi->lcore_id);
238 fflush(pi->f_cur_min);
239 fflush(pi->f_cur_max);
243 /* Increase freq, the max freq should be updated first */
244 if (idx < pi->curr_idx) {
246 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
247 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
248 "lcore %u\n", pi->lcore_id);
252 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
253 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
254 "lcore %u\n", pi->lcore_id);
258 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
259 target_freq, pi->lcore_id);
261 fflush(pi->f_cur_max);
262 fflush(pi->f_cur_min);
271 * It is to check the current scaling governor by reading sys file, and then
272 * set it into 'performance' if it is not by writing the sys file. The original
273 * governor will be saved for rolling back.
276 power_set_governor_performance(struct pstate_power_info *pi)
281 char fullpath[PATH_MAX];
285 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
287 f = fopen(fullpath, "rw+");
288 FOPEN_OR_ERR_RET(f, ret);
290 s = fgets(buf, sizeof(buf), f);
291 FOPS_OR_NULL_GOTO(s, out);
293 /* Check if current governor is performance */
294 if (strncmp(buf, POWER_GOVERNOR_PERF,
295 sizeof(POWER_GOVERNOR_PERF)) == 0) {
297 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
298 "already performance\n", pi->lcore_id);
301 /* Save the original governor */
302 snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
304 /* Write 'performance' to the governor */
305 val = fseek(f, 0, SEEK_SET);
306 FOPS_OR_ERR_GOTO(val, out);
308 val = fputs(POWER_GOVERNOR_PERF, f);
309 FOPS_OR_ERR_GOTO(val, out);
312 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
313 "set to performance successfully\n", pi->lcore_id);
321 * It is to check the governor and then set the original governor back if
322 * needed by writing the sys file.
325 power_set_governor_original(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);
342 /* Check if the governor to be set is the same as current */
343 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
345 POWER_DEBUG_TRACE("Power management governor of lcore %u "
346 "has already been set to %s\n",
347 pi->lcore_id, pi->governor_ori);
351 /* Write back the original governor */
352 val = fseek(f, 0, SEEK_SET);
353 FOPS_OR_ERR_GOTO(val, out);
355 val = fputs(pi->governor_ori, f);
356 FOPS_OR_ERR_GOTO(val, out);
359 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
360 "has been set back to %s successfully\n",
361 pi->lcore_id, pi->governor_ori);
369 * It is to get the available frequencies of the specific lcore by reading the
373 power_get_available_freqs(struct pstate_power_info *pi)
378 char buf_min[BUFSIZ];
379 char buf_max[BUFSIZ];
380 char fullpath_min[PATH_MAX];
381 char fullpath_max[PATH_MAX];
383 uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
384 uint32_t i, num_freqs = 0;
386 snprintf(fullpath_max, sizeof(fullpath_max),
387 POWER_SYSFILE_BASE_MAX_FREQ,
389 snprintf(fullpath_min, sizeof(fullpath_min),
390 POWER_SYSFILE_BASE_MIN_FREQ,
393 f_min = fopen(fullpath_min, "r");
394 FOPEN_OR_ERR_RET(f_min, ret);
396 f_max = fopen(fullpath_max, "r");
397 FOPEN_OR_ERR_RET(f_max, ret);
399 s_min = fgets(buf_min, sizeof(buf_min), f_min);
400 FOPS_OR_NULL_GOTO(s_min, out);
402 s_max = fgets(buf_max, sizeof(buf_max), f_max);
403 FOPS_OR_NULL_GOTO(s_max, out);
406 /* Strip the line break if there is */
407 p_min = strchr(buf_min, '\n');
411 p_max = strchr(buf_max, '\n');
415 sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
416 sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
418 if (sys_max_freq < sys_min_freq)
421 pi->sys_max_freq = sys_max_freq;
423 base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
425 POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
430 if (base_max_freq < sys_max_freq)
431 pi->turbo_available = 1;
433 pi->turbo_available = 0;
435 /* If turbo is available then there is one extra freq bucket
436 * to store the sys max freq which value is base_max +1
438 num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
441 /* Generate the freq bucket array.
442 * If turbo is available the freq bucket[0] value is base_max +1
443 * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
445 * If turbo is not available bucket[0] is base_max and so on
447 for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
448 if ((i == 0) && pi->turbo_available)
449 pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
451 pi->freqs[pi->nb_freqs++] =
452 base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
457 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
458 num_freqs, pi->lcore_id);
468 power_pstate_cpufreq_init(unsigned int lcore_id)
470 struct pstate_power_info *pi;
472 if (lcore_id >= RTE_MAX_LCORE) {
473 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
474 lcore_id, RTE_MAX_LCORE - 1U);
478 pi = &lcore_power_info[lcore_id];
479 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
481 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
482 "in use\n", lcore_id);
486 pi->lcore_id = lcore_id;
487 /* Check and set the governor */
488 if (power_set_governor_performance(pi) < 0) {
489 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
490 "performance\n", lcore_id);
493 /* Init for setting lcore frequency */
494 if (power_init_for_setting_freq(pi) < 0) {
495 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
496 "lcore %u\n", lcore_id);
500 /* Get the available frequencies */
501 if (power_get_available_freqs(pi) < 0) {
502 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
503 "lcore %u\n", lcore_id);
508 /* Set freq to max by default */
509 if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
510 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
511 "to max\n", lcore_id);
515 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
516 "power management\n", lcore_id);
517 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
522 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
528 power_pstate_cpufreq_exit(unsigned int lcore_id)
530 struct pstate_power_info *pi;
532 if (lcore_id >= RTE_MAX_LCORE) {
533 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
534 lcore_id, RTE_MAX_LCORE - 1U);
537 pi = &lcore_power_info[lcore_id];
539 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
541 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
542 "not used\n", lcore_id);
546 /* Close FD of setting freq */
547 fclose(pi->f_cur_min);
548 fclose(pi->f_cur_max);
549 pi->f_cur_min = NULL;
550 pi->f_cur_max = NULL;
552 /* Set the governor back to the original */
553 if (power_set_governor_original(pi) < 0) {
554 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
555 "to the original\n", lcore_id);
559 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
560 "'performance' mode and been set back to the "
561 "original\n", lcore_id);
562 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
567 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
574 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
576 struct pstate_power_info *pi;
578 if (lcore_id >= RTE_MAX_LCORE) {
579 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
583 pi = &lcore_power_info[lcore_id];
584 if (num < pi->nb_freqs) {
585 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
588 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
594 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
596 if (lcore_id >= RTE_MAX_LCORE) {
597 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
598 return RTE_POWER_INVALID_FREQ_INDEX;
601 return lcore_power_info[lcore_id].curr_idx;
606 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
608 if (lcore_id >= RTE_MAX_LCORE) {
609 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
613 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
617 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
619 struct pstate_power_info *pi;
621 if (lcore_id >= RTE_MAX_LCORE) {
622 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
626 pi = &lcore_power_info[lcore_id];
627 if (pi->curr_idx == 0)
630 /* Frequencies in the array are from high to low. */
631 return set_freq_internal(pi, pi->curr_idx - 1);
635 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
637 struct pstate_power_info *pi;
639 if (lcore_id >= RTE_MAX_LCORE) {
640 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
644 pi = &lcore_power_info[lcore_id];
645 if (pi->curr_idx + 1 == pi->nb_freqs)
648 /* Frequencies in the array are from high to low. */
649 return set_freq_internal(pi, pi->curr_idx + 1);
653 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
655 if (lcore_id >= RTE_MAX_LCORE) {
656 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
660 /* Frequencies in the array are from high to low. */
661 if (lcore_power_info[lcore_id].turbo_available) {
662 if (lcore_power_info[lcore_id].turbo_enable)
664 return set_freq_internal(
665 &lcore_power_info[lcore_id], 0);
667 /* Set to max non-turbo */
668 return set_freq_internal(
669 &lcore_power_info[lcore_id], 1);
671 return set_freq_internal(&lcore_power_info[lcore_id], 0);
676 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
678 struct pstate_power_info *pi;
680 if (lcore_id >= RTE_MAX_LCORE) {
681 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
685 pi = &lcore_power_info[lcore_id];
687 /* Frequencies in the array are from high to low. */
688 return set_freq_internal(pi, pi->nb_freqs - 1);
693 power_pstate_turbo_status(unsigned int lcore_id)
695 struct pstate_power_info *pi;
697 if (lcore_id >= RTE_MAX_LCORE) {
698 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
702 pi = &lcore_power_info[lcore_id];
704 return pi->turbo_enable;
708 power_pstate_enable_turbo(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];
719 if (pi->turbo_available)
720 pi->turbo_enable = 1;
722 pi->turbo_enable = 0;
724 "Failed to enable turbo on lcore %u\n",
734 power_pstate_disable_turbo(unsigned int lcore_id)
736 struct pstate_power_info *pi;
738 if (lcore_id >= RTE_MAX_LCORE) {
739 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
743 pi = &lcore_power_info[lcore_id];
745 pi->turbo_enable = 0;
752 int power_pstate_get_capabilities(unsigned int lcore_id,
753 struct rte_power_core_capabilities *caps)
755 struct pstate_power_info *pi;
757 if (lcore_id >= RTE_MAX_LCORE) {
758 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
762 RTE_LOG(ERR, POWER, "Invalid argument\n");
766 pi = &lcore_power_info[lcore_id];
767 caps->capabilities = 0;
768 caps->turbo = !!(pi->turbo_available);