1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_atomic.h>
16 #include <rte_memcpy.h>
17 #include <rte_memory.h>
18 #include <rte_string_fns.h>
20 #include "power_acpi_cpufreq.h"
21 #include "power_common.h"
23 #ifdef RTE_LIBRTE_POWER_DEBUG
24 #define POWER_DEBUG_TRACE(fmt, args...) do { \
25 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
28 #define POWER_DEBUG_TRACE(fmt, args...)
31 #define FOPEN_OR_ERR_RET(f, retval) do { \
33 RTE_LOG(ERR, POWER, "File not opened\n"); \
38 #define FOPS_OR_NULL_GOTO(ret, label) do { \
39 if ((ret) == NULL) { \
40 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
45 #define FOPS_OR_ERR_GOTO(ret, label) do { \
47 RTE_LOG(ERR, POWER, "File operations failed\n"); \
53 #define POWER_CONVERT_TO_DECIMAL 10
55 #define POWER_GOVERNOR_USERSPACE "userspace"
56 #define POWER_SYSFILE_GOVERNOR \
57 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
58 #define POWER_SYSFILE_AVAIL_FREQ \
59 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies"
60 #define POWER_SYSFILE_SETSPEED \
61 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
62 #define POWER_ACPI_DRIVER "acpi-cpufreq"
67 #define PLATFORM_INFO 0x0CE
68 #define TURBO_RATIO_LIMIT 0x1AD
69 #define IA32_PERF_CTL 0x199
70 #define CORE_TURBO_DISABLE_BIT ((uint64_t)1<<32)
80 * Power info per lcore.
82 struct rte_power_info {
83 unsigned int lcore_id; /**< Logical core id */
84 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
85 uint32_t nb_freqs; /**< number of available freqs */
86 FILE *f; /**< FD of scaling_setspeed */
87 char governor_ori[32]; /**< Original governor name */
88 uint32_t curr_idx; /**< Freq index in freqs array */
89 volatile uint32_t state; /**< Power in use state */
90 uint16_t turbo_available; /**< Turbo Boost available */
91 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
92 } __rte_cache_aligned;
94 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
97 * It is to set specific freq for specific logical core, according to the index
98 * of supported frequencies.
101 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
103 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
104 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
105 "should be less than %u\n", idx, pi->nb_freqs);
109 /* Check if it is the same as current */
110 if (idx == pi->curr_idx)
113 POWER_DEBUG_TRACE("Frequency[%u] %u to be set for lcore %u\n",
114 idx, pi->freqs[idx], pi->lcore_id);
115 if (fseek(pi->f, 0, SEEK_SET) < 0) {
116 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
117 "for setting frequency for lcore %u\n", pi->lcore_id);
120 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
121 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
122 "lcore %u\n", pi->lcore_id);
132 * It is to check the current scaling governor by reading sys file, and then
133 * set it into 'userspace' if it is not by writing the sys file. The original
134 * governor will be saved for rolling back.
137 power_set_governor_userspace(struct rte_power_info *pi)
142 char fullpath[PATH_MAX];
146 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
148 f = fopen(fullpath, "rw+");
149 FOPEN_OR_ERR_RET(f, ret);
151 s = fgets(buf, sizeof(buf), f);
152 FOPS_OR_NULL_GOTO(s, out);
153 /* Strip off terminating '\n' */
156 /* Check if current governor is userspace */
157 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
158 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
160 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
161 "already userspace\n", pi->lcore_id);
164 /* Save the original governor */
165 strlcpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
167 /* Write 'userspace' to the governor */
168 val = fseek(f, 0, SEEK_SET);
169 FOPS_OR_ERR_GOTO(val, out);
171 val = fputs(POWER_GOVERNOR_USERSPACE, f);
172 FOPS_OR_ERR_GOTO(val, out);
174 /* We need to flush to see if the fputs succeeds */
176 FOPS_OR_ERR_GOTO(val, out);
179 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
180 "set to user space successfully\n", pi->lcore_id);
188 * It is to get the available frequencies of the specific lcore by reading the
192 power_get_available_freqs(struct rte_power_info *pi)
195 int ret = -1, i, count;
198 char fullpath[PATH_MAX];
199 char *freqs[RTE_MAX_LCORE_FREQS];
202 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
204 f = fopen(fullpath, "r");
205 FOPEN_OR_ERR_RET(f, ret);
207 s = fgets(buf, sizeof(buf), f);
208 FOPS_OR_NULL_GOTO(s, out);
210 /* Strip the line break if there is */
211 p = strchr(buf, '\n');
215 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
216 count = rte_strsplit(buf, sizeof(buf), freqs,
217 RTE_MAX_LCORE_FREQS, ' ');
219 RTE_LOG(ERR, POWER, "No available frequency in "
220 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
223 if (count >= RTE_MAX_LCORE_FREQS) {
224 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
229 /* Store the available frequncies into power context */
230 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
231 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
233 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
234 POWER_CONVERT_TO_DECIMAL);
237 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
238 pi->turbo_available = 1;
239 pi->turbo_enable = 1;
240 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
243 pi->turbo_available = 0;
244 pi->turbo_enable = 0;
245 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
250 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
251 count, pi->lcore_id);
259 * It is to fopen the sys file for the future setting the lcore frequency.
262 power_init_for_setting_freq(struct rte_power_info *pi)
265 char fullpath[PATH_MAX];
270 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
272 f = fopen(fullpath, "rw+");
273 FOPEN_OR_ERR_RET(f, -1);
275 s = fgets(buf, sizeof(buf), f);
276 FOPS_OR_NULL_GOTO(s, out);
278 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
279 for (i = 0; i < pi->nb_freqs; i++) {
280 if (freq == pi->freqs[i]) {
294 power_acpi_cpufreq_check_supported(void)
296 return cpufreq_check_scaling_driver(POWER_ACPI_DRIVER);
300 power_acpi_cpufreq_init(unsigned int lcore_id)
302 struct rte_power_info *pi;
304 if (lcore_id >= RTE_MAX_LCORE) {
305 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
306 lcore_id, RTE_MAX_LCORE - 1U);
310 pi = &lcore_power_info[lcore_id];
311 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
313 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
314 "in use\n", lcore_id);
318 pi->lcore_id = lcore_id;
319 /* Check and set the governor */
320 if (power_set_governor_userspace(pi) < 0) {
321 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
322 "userspace\n", lcore_id);
326 /* Get the available frequencies */
327 if (power_get_available_freqs(pi) < 0) {
328 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
329 "lcore %u\n", lcore_id);
333 /* Init for setting lcore frequency */
334 if (power_init_for_setting_freq(pi) < 0) {
335 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
336 "lcore %u\n", lcore_id);
340 /* Set freq to max by default */
341 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
342 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
343 "to max\n", lcore_id);
347 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
348 "power management\n", lcore_id);
349 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
354 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
360 * It is to check the governor and then set the original governor back if
361 * needed by writing the sys file.
364 power_set_governor_original(struct rte_power_info *pi)
369 char fullpath[PATH_MAX];
373 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
375 f = fopen(fullpath, "rw+");
376 FOPEN_OR_ERR_RET(f, ret);
378 s = fgets(buf, sizeof(buf), f);
379 FOPS_OR_NULL_GOTO(s, out);
381 /* Check if the governor to be set is the same as current */
382 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
384 POWER_DEBUG_TRACE("Power management governor of lcore %u "
385 "has already been set to %s\n",
386 pi->lcore_id, pi->governor_ori);
390 /* Write back the original governor */
391 val = fseek(f, 0, SEEK_SET);
392 FOPS_OR_ERR_GOTO(val, out);
394 val = fputs(pi->governor_ori, f);
395 FOPS_OR_ERR_GOTO(val, out);
398 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
399 "has been set back to %s successfully\n",
400 pi->lcore_id, pi->governor_ori);
408 power_acpi_cpufreq_exit(unsigned int lcore_id)
410 struct rte_power_info *pi;
412 if (lcore_id >= RTE_MAX_LCORE) {
413 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
414 lcore_id, RTE_MAX_LCORE - 1U);
417 pi = &lcore_power_info[lcore_id];
418 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
420 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
421 "not used\n", lcore_id);
425 /* Close FD of setting freq */
429 /* Set the governor back to the original */
430 if (power_set_governor_original(pi) < 0) {
431 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
432 "to the original\n", lcore_id);
436 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
437 "'userspace' mode and been set back to the "
438 "original\n", lcore_id);
439 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
444 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
450 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
452 struct rte_power_info *pi;
454 if (lcore_id >= RTE_MAX_LCORE) {
455 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
460 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
464 pi = &lcore_power_info[lcore_id];
465 if (num < pi->nb_freqs) {
466 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
469 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
475 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
477 if (lcore_id >= RTE_MAX_LCORE) {
478 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
479 return RTE_POWER_INVALID_FREQ_INDEX;
482 return lcore_power_info[lcore_id].curr_idx;
486 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
488 if (lcore_id >= RTE_MAX_LCORE) {
489 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
493 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
497 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
499 struct rte_power_info *pi;
501 if (lcore_id >= RTE_MAX_LCORE) {
502 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
506 pi = &lcore_power_info[lcore_id];
507 if (pi->curr_idx + 1 == pi->nb_freqs)
510 /* Frequencies in the array are from high to low. */
511 return set_freq_internal(pi, pi->curr_idx + 1);
515 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
517 struct rte_power_info *pi;
519 if (lcore_id >= RTE_MAX_LCORE) {
520 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
524 pi = &lcore_power_info[lcore_id];
525 if (pi->curr_idx == 0 ||
526 (pi->curr_idx == 1 && pi->turbo_available && !pi->turbo_enable))
529 /* Frequencies in the array are from high to low. */
530 return set_freq_internal(pi, pi->curr_idx - 1);
534 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
536 if (lcore_id >= RTE_MAX_LCORE) {
537 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
541 /* Frequencies in the array are from high to low. */
542 if (lcore_power_info[lcore_id].turbo_available) {
543 if (lcore_power_info[lcore_id].turbo_enable)
545 return set_freq_internal(
546 &lcore_power_info[lcore_id], 0);
548 /* Set to max non-turbo */
549 return set_freq_internal(
550 &lcore_power_info[lcore_id], 1);
552 return set_freq_internal(&lcore_power_info[lcore_id], 0);
556 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
558 struct rte_power_info *pi;
560 if (lcore_id >= RTE_MAX_LCORE) {
561 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
565 pi = &lcore_power_info[lcore_id];
567 /* Frequencies in the array are from high to low. */
568 return set_freq_internal(pi, pi->nb_freqs - 1);
573 power_acpi_turbo_status(unsigned int lcore_id)
575 struct rte_power_info *pi;
577 if (lcore_id >= RTE_MAX_LCORE) {
578 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
582 pi = &lcore_power_info[lcore_id];
584 return pi->turbo_enable;
589 power_acpi_enable_turbo(unsigned int lcore_id)
591 struct rte_power_info *pi;
593 if (lcore_id >= RTE_MAX_LCORE) {
594 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
598 pi = &lcore_power_info[lcore_id];
600 if (pi->turbo_available)
601 pi->turbo_enable = 1;
603 pi->turbo_enable = 0;
605 "Failed to enable turbo on lcore %u\n",
610 /* Max may have changed, so call to max function */
611 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
613 "Failed to set frequency of lcore %u to max\n",
622 power_acpi_disable_turbo(unsigned int lcore_id)
624 struct rte_power_info *pi;
626 if (lcore_id >= RTE_MAX_LCORE) {
627 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
631 pi = &lcore_power_info[lcore_id];
633 pi->turbo_enable = 0;
635 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
636 /* Try to set freq to max by default coming out of turbo */
637 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
639 "Failed to set frequency of lcore %u to max\n",
648 int power_acpi_get_capabilities(unsigned int lcore_id,
649 struct rte_power_core_capabilities *caps)
651 struct rte_power_info *pi;
653 if (lcore_id >= RTE_MAX_LCORE) {
654 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
658 RTE_LOG(ERR, POWER, "Invalid argument\n");
662 pi = &lcore_power_info[lcore_id];
663 caps->capabilities = 0;
664 caps->turbo = !!(pi->turbo_available);