1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_memcpy.h>
16 #include <rte_atomic.h>
18 #include "power_acpi_cpufreq.h"
19 #include "power_common.h"
21 #ifdef RTE_LIBRTE_POWER_DEBUG
22 #define POWER_DEBUG_TRACE(fmt, args...) do { \
23 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
26 #define POWER_DEBUG_TRACE(fmt, args...)
29 #define FOPEN_OR_ERR_RET(f, retval) do { \
31 RTE_LOG(ERR, POWER, "File not openned\n"); \
36 #define FOPS_OR_NULL_GOTO(ret, label) do { \
37 if ((ret) == NULL) { \
38 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
43 #define FOPS_OR_ERR_GOTO(ret, label) do { \
45 RTE_LOG(ERR, POWER, "File operations failed\n"); \
51 #define POWER_CONVERT_TO_DECIMAL 10
53 #define POWER_GOVERNOR_USERSPACE "userspace"
54 #define POWER_SYSFILE_GOVERNOR \
55 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
56 #define POWER_SYSFILE_AVAIL_FREQ \
57 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies"
58 #define POWER_SYSFILE_SETSPEED \
59 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
64 #define PLATFORM_INFO 0x0CE
65 #define TURBO_RATIO_LIMIT 0x1AD
66 #define IA32_PERF_CTL 0x199
67 #define CORE_TURBO_DISABLE_BIT ((uint64_t)1<<32)
77 * Power info per lcore.
79 struct rte_power_info {
80 unsigned int lcore_id; /**< Logical core id */
81 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
82 uint32_t nb_freqs; /**< number of available freqs */
83 FILE *f; /**< FD of scaling_setspeed */
84 char governor_ori[32]; /**< Original governor name */
85 uint32_t curr_idx; /**< Freq index in freqs array */
86 volatile uint32_t state; /**< Power in use state */
87 uint16_t turbo_available; /**< Turbo Boost available */
88 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
89 } __rte_cache_aligned;
91 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
94 * It is to set specific freq for specific logical core, according to the index
95 * of supported frequencies.
98 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
100 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
101 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
102 "should be less than %u\n", idx, pi->nb_freqs);
106 /* Check if it is the same as current */
107 if (idx == pi->curr_idx)
110 POWER_DEBUG_TRACE("Freqency[%u] %u to be set for lcore %u\n",
111 idx, pi->freqs[idx], pi->lcore_id);
112 if (fseek(pi->f, 0, SEEK_SET) < 0) {
113 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
114 "for setting frequency for lcore %u\n", pi->lcore_id);
117 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
118 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
119 "lcore %u\n", pi->lcore_id);
129 * It is to check the current scaling governor by reading sys file, and then
130 * set it into 'userspace' if it is not by writing the sys file. The original
131 * governor will be saved for rolling back.
134 power_set_governor_userspace(struct rte_power_info *pi)
139 char fullpath[PATH_MAX];
143 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
145 f = fopen(fullpath, "rw+");
146 FOPEN_OR_ERR_RET(f, ret);
148 s = fgets(buf, sizeof(buf), f);
149 FOPS_OR_NULL_GOTO(s, out);
150 /* Strip off terminating '\n' */
153 /* Check if current governor is userspace */
154 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
155 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
157 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
158 "already userspace\n", pi->lcore_id);
161 /* Save the original governor */
162 snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
164 /* Write 'userspace' to the governor */
165 val = fseek(f, 0, SEEK_SET);
166 FOPS_OR_ERR_GOTO(val, out);
168 val = fputs(POWER_GOVERNOR_USERSPACE, f);
169 FOPS_OR_ERR_GOTO(val, out);
171 /* We need to flush to see if the fputs succeeds */
173 FOPS_OR_ERR_GOTO(val, out);
176 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
177 "set to user space successfully\n", pi->lcore_id);
185 * It is to get the available frequencies of the specific lcore by reading the
189 power_get_available_freqs(struct rte_power_info *pi)
192 int ret = -1, i, count;
195 char fullpath[PATH_MAX];
196 char *freqs[RTE_MAX_LCORE_FREQS];
199 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
201 f = fopen(fullpath, "r");
202 FOPEN_OR_ERR_RET(f, ret);
204 s = fgets(buf, sizeof(buf), f);
205 FOPS_OR_NULL_GOTO(s, out);
207 /* Strip the line break if there is */
208 p = strchr(buf, '\n');
212 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
213 count = rte_strsplit(buf, sizeof(buf), freqs,
214 RTE_MAX_LCORE_FREQS, ' ');
216 RTE_LOG(ERR, POWER, "No available frequency in "
217 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
220 if (count >= RTE_MAX_LCORE_FREQS) {
221 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
226 /* Store the available frequncies into power context */
227 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
228 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
230 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
231 POWER_CONVERT_TO_DECIMAL);
234 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
235 pi->turbo_available = 1;
236 pi->turbo_enable = 1;
237 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
240 pi->turbo_available = 0;
241 pi->turbo_enable = 0;
242 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
247 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
248 count, pi->lcore_id);
256 * It is to fopen the sys file for the future setting the lcore frequency.
259 power_init_for_setting_freq(struct rte_power_info *pi)
262 char fullpath[PATH_MAX];
267 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
269 f = fopen(fullpath, "rw+");
270 FOPEN_OR_ERR_RET(f, -1);
272 s = fgets(buf, sizeof(buf), f);
273 FOPS_OR_NULL_GOTO(s, out);
275 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
276 for (i = 0; i < pi->nb_freqs; i++) {
277 if (freq == pi->freqs[i]) {
291 power_acpi_cpufreq_init(unsigned int lcore_id)
293 struct rte_power_info *pi;
295 if (lcore_id >= RTE_MAX_LCORE) {
296 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
297 lcore_id, RTE_MAX_LCORE - 1U);
301 pi = &lcore_power_info[lcore_id];
302 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
304 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
305 "in use\n", lcore_id);
309 pi->lcore_id = lcore_id;
310 /* Check and set the governor */
311 if (power_set_governor_userspace(pi) < 0) {
312 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
313 "userspace\n", lcore_id);
317 /* Get the available frequencies */
318 if (power_get_available_freqs(pi) < 0) {
319 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
320 "lcore %u\n", lcore_id);
324 /* Init for setting lcore frequency */
325 if (power_init_for_setting_freq(pi) < 0) {
326 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
327 "lcore %u\n", lcore_id);
331 /* Set freq to max by default */
332 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
333 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
334 "to max\n", lcore_id);
338 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
339 "power management\n", lcore_id);
340 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
345 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
351 * It is to check the governor and then set the original governor back if
352 * needed by writing the sys file.
355 power_set_governor_original(struct rte_power_info *pi)
360 char fullpath[PATH_MAX];
364 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
366 f = fopen(fullpath, "rw+");
367 FOPEN_OR_ERR_RET(f, ret);
369 s = fgets(buf, sizeof(buf), f);
370 FOPS_OR_NULL_GOTO(s, out);
372 /* Check if the governor to be set is the same as current */
373 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
375 POWER_DEBUG_TRACE("Power management governor of lcore %u "
376 "has already been set to %s\n",
377 pi->lcore_id, pi->governor_ori);
381 /* Write back the original governor */
382 val = fseek(f, 0, SEEK_SET);
383 FOPS_OR_ERR_GOTO(val, out);
385 val = fputs(pi->governor_ori, f);
386 FOPS_OR_ERR_GOTO(val, out);
389 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
390 "has been set back to %s successfully\n",
391 pi->lcore_id, pi->governor_ori);
399 power_acpi_cpufreq_exit(unsigned int lcore_id)
401 struct rte_power_info *pi;
403 if (lcore_id >= RTE_MAX_LCORE) {
404 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
405 lcore_id, RTE_MAX_LCORE - 1U);
408 pi = &lcore_power_info[lcore_id];
409 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
411 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
412 "not used\n", lcore_id);
416 /* Close FD of setting freq */
420 /* Set the governor back to the original */
421 if (power_set_governor_original(pi) < 0) {
422 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
423 "to the original\n", lcore_id);
427 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
428 "'userspace' mode and been set back to the "
429 "original\n", lcore_id);
430 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
435 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
441 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
443 struct rte_power_info *pi;
445 if (lcore_id >= RTE_MAX_LCORE) {
446 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
451 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
455 pi = &lcore_power_info[lcore_id];
456 if (num < pi->nb_freqs) {
457 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
460 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
466 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
468 if (lcore_id >= RTE_MAX_LCORE) {
469 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
470 return RTE_POWER_INVALID_FREQ_INDEX;
473 return lcore_power_info[lcore_id].curr_idx;
477 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
479 if (lcore_id >= RTE_MAX_LCORE) {
480 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
484 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
488 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
490 struct rte_power_info *pi;
492 if (lcore_id >= RTE_MAX_LCORE) {
493 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
497 pi = &lcore_power_info[lcore_id];
498 if (pi->curr_idx + 1 == pi->nb_freqs)
501 /* Frequencies in the array are from high to low. */
502 return set_freq_internal(pi, pi->curr_idx + 1);
506 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
508 struct rte_power_info *pi;
510 if (lcore_id >= RTE_MAX_LCORE) {
511 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
515 pi = &lcore_power_info[lcore_id];
516 if (pi->curr_idx == 0)
519 /* Frequencies in the array are from high to low. */
520 return set_freq_internal(pi, pi->curr_idx - 1);
524 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
526 if (lcore_id >= RTE_MAX_LCORE) {
527 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
531 /* Frequencies in the array are from high to low. */
532 if (lcore_power_info[lcore_id].turbo_available) {
533 if (lcore_power_info[lcore_id].turbo_enable)
535 return set_freq_internal(
536 &lcore_power_info[lcore_id], 0);
538 /* Set to max non-turbo */
539 return set_freq_internal(
540 &lcore_power_info[lcore_id], 1);
542 return set_freq_internal(&lcore_power_info[lcore_id], 0);
546 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
548 struct rte_power_info *pi;
550 if (lcore_id >= RTE_MAX_LCORE) {
551 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
555 pi = &lcore_power_info[lcore_id];
557 /* Frequencies in the array are from high to low. */
558 return set_freq_internal(pi, pi->nb_freqs - 1);
563 power_acpi_turbo_status(unsigned int lcore_id)
565 struct rte_power_info *pi;
567 if (lcore_id >= RTE_MAX_LCORE) {
568 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
572 pi = &lcore_power_info[lcore_id];
574 return pi->turbo_enable;
579 power_acpi_enable_turbo(unsigned int lcore_id)
581 struct rte_power_info *pi;
583 if (lcore_id >= RTE_MAX_LCORE) {
584 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
588 pi = &lcore_power_info[lcore_id];
590 if (pi->turbo_available)
591 pi->turbo_enable = 1;
593 pi->turbo_enable = 0;
595 "Failed to enable turbo on lcore %u\n",
600 /* Max may have changed, so call to max function */
601 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
603 "Failed to set frequency of lcore %u to max\n",
612 power_acpi_disable_turbo(unsigned int lcore_id)
614 struct rte_power_info *pi;
616 if (lcore_id >= RTE_MAX_LCORE) {
617 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
621 pi = &lcore_power_info[lcore_id];
623 pi->turbo_enable = 0;
625 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
626 /* Try to set freq to max by default coming out of turbo */
627 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
629 "Failed to set frequency of lcore %u to max\n",
638 int power_acpi_get_capabilities(unsigned int lcore_id,
639 struct rte_power_core_capabilities *caps)
641 struct rte_power_info *pi;
643 if (lcore_id >= RTE_MAX_LCORE) {
644 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
648 RTE_LOG(ERR, POWER, "Invalid argument\n");
652 pi = &lcore_power_info[lcore_id];
653 caps->capabilities = 0;
654 caps->turbo = !!(pi->turbo_available);