1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_string_fns.h>
16 #include <rte_memcpy.h>
17 #include <rte_atomic.h>
19 #include "power_acpi_cpufreq.h"
20 #include "power_common.h"
22 #ifdef RTE_LIBRTE_POWER_DEBUG
23 #define POWER_DEBUG_TRACE(fmt, args...) do { \
24 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
27 #define POWER_DEBUG_TRACE(fmt, args...)
30 #define FOPEN_OR_ERR_RET(f, retval) do { \
32 RTE_LOG(ERR, POWER, "File not openned\n"); \
37 #define FOPS_OR_NULL_GOTO(ret, label) do { \
38 if ((ret) == NULL) { \
39 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
44 #define FOPS_OR_ERR_GOTO(ret, label) do { \
46 RTE_LOG(ERR, POWER, "File operations failed\n"); \
52 #define POWER_CONVERT_TO_DECIMAL 10
54 #define POWER_GOVERNOR_USERSPACE "userspace"
55 #define POWER_SYSFILE_GOVERNOR \
56 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
57 #define POWER_SYSFILE_AVAIL_FREQ \
58 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies"
59 #define POWER_SYSFILE_SETSPEED \
60 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
65 #define PLATFORM_INFO 0x0CE
66 #define TURBO_RATIO_LIMIT 0x1AD
67 #define IA32_PERF_CTL 0x199
68 #define CORE_TURBO_DISABLE_BIT ((uint64_t)1<<32)
78 * Power info per lcore.
80 struct rte_power_info {
81 unsigned int lcore_id; /**< Logical core id */
82 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
83 uint32_t nb_freqs; /**< number of available freqs */
84 FILE *f; /**< FD of scaling_setspeed */
85 char governor_ori[32]; /**< Original governor name */
86 uint32_t curr_idx; /**< Freq index in freqs array */
87 volatile uint32_t state; /**< Power in use state */
88 uint16_t turbo_available; /**< Turbo Boost available */
89 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
90 } __rte_cache_aligned;
92 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
95 * It is to set specific freq for specific logical core, according to the index
96 * of supported frequencies.
99 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
101 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
102 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
103 "should be less than %u\n", idx, pi->nb_freqs);
107 /* Check if it is the same as current */
108 if (idx == pi->curr_idx)
111 POWER_DEBUG_TRACE("Freqency[%u] %u to be set for lcore %u\n",
112 idx, pi->freqs[idx], pi->lcore_id);
113 if (fseek(pi->f, 0, SEEK_SET) < 0) {
114 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
115 "for setting frequency for lcore %u\n", pi->lcore_id);
118 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
119 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
120 "lcore %u\n", pi->lcore_id);
130 * It is to check the current scaling governor by reading sys file, and then
131 * set it into 'userspace' if it is not by writing the sys file. The original
132 * governor will be saved for rolling back.
135 power_set_governor_userspace(struct rte_power_info *pi)
140 char fullpath[PATH_MAX];
144 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
146 f = fopen(fullpath, "rw+");
147 FOPEN_OR_ERR_RET(f, ret);
149 s = fgets(buf, sizeof(buf), f);
150 FOPS_OR_NULL_GOTO(s, out);
151 /* Strip off terminating '\n' */
154 /* Check if current governor is userspace */
155 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
156 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
158 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
159 "already userspace\n", pi->lcore_id);
162 /* Save the original governor */
163 strlcpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
165 /* Write 'userspace' to the governor */
166 val = fseek(f, 0, SEEK_SET);
167 FOPS_OR_ERR_GOTO(val, out);
169 val = fputs(POWER_GOVERNOR_USERSPACE, f);
170 FOPS_OR_ERR_GOTO(val, out);
172 /* We need to flush to see if the fputs succeeds */
174 FOPS_OR_ERR_GOTO(val, out);
177 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
178 "set to user space successfully\n", pi->lcore_id);
186 * It is to get the available frequencies of the specific lcore by reading the
190 power_get_available_freqs(struct rte_power_info *pi)
193 int ret = -1, i, count;
196 char fullpath[PATH_MAX];
197 char *freqs[RTE_MAX_LCORE_FREQS];
200 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
202 f = fopen(fullpath, "r");
203 FOPEN_OR_ERR_RET(f, ret);
205 s = fgets(buf, sizeof(buf), f);
206 FOPS_OR_NULL_GOTO(s, out);
208 /* Strip the line break if there is */
209 p = strchr(buf, '\n');
213 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
214 count = rte_strsplit(buf, sizeof(buf), freqs,
215 RTE_MAX_LCORE_FREQS, ' ');
217 RTE_LOG(ERR, POWER, "No available frequency in "
218 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
221 if (count >= RTE_MAX_LCORE_FREQS) {
222 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
227 /* Store the available frequncies into power context */
228 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
229 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
231 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
232 POWER_CONVERT_TO_DECIMAL);
235 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
236 pi->turbo_available = 1;
237 pi->turbo_enable = 1;
238 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
241 pi->turbo_available = 0;
242 pi->turbo_enable = 0;
243 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
248 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
249 count, pi->lcore_id);
257 * It is to fopen the sys file for the future setting the lcore frequency.
260 power_init_for_setting_freq(struct rte_power_info *pi)
263 char fullpath[PATH_MAX];
268 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
270 f = fopen(fullpath, "rw+");
271 FOPEN_OR_ERR_RET(f, -1);
273 s = fgets(buf, sizeof(buf), f);
274 FOPS_OR_NULL_GOTO(s, out);
276 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
277 for (i = 0; i < pi->nb_freqs; i++) {
278 if (freq == pi->freqs[i]) {
292 power_acpi_cpufreq_init(unsigned int lcore_id)
294 struct rte_power_info *pi;
296 if (lcore_id >= RTE_MAX_LCORE) {
297 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
298 lcore_id, RTE_MAX_LCORE - 1U);
302 pi = &lcore_power_info[lcore_id];
303 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
305 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
306 "in use\n", lcore_id);
310 pi->lcore_id = lcore_id;
311 /* Check and set the governor */
312 if (power_set_governor_userspace(pi) < 0) {
313 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
314 "userspace\n", lcore_id);
318 /* Get the available frequencies */
319 if (power_get_available_freqs(pi) < 0) {
320 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
321 "lcore %u\n", lcore_id);
325 /* Init for setting lcore frequency */
326 if (power_init_for_setting_freq(pi) < 0) {
327 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
328 "lcore %u\n", lcore_id);
332 /* Set freq to max by default */
333 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
334 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
335 "to max\n", lcore_id);
339 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
340 "power management\n", lcore_id);
341 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
346 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
352 * It is to check the governor and then set the original governor back if
353 * needed by writing the sys file.
356 power_set_governor_original(struct rte_power_info *pi)
361 char fullpath[PATH_MAX];
365 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
367 f = fopen(fullpath, "rw+");
368 FOPEN_OR_ERR_RET(f, ret);
370 s = fgets(buf, sizeof(buf), f);
371 FOPS_OR_NULL_GOTO(s, out);
373 /* Check if the governor to be set is the same as current */
374 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
376 POWER_DEBUG_TRACE("Power management governor of lcore %u "
377 "has already been set to %s\n",
378 pi->lcore_id, pi->governor_ori);
382 /* Write back the original governor */
383 val = fseek(f, 0, SEEK_SET);
384 FOPS_OR_ERR_GOTO(val, out);
386 val = fputs(pi->governor_ori, f);
387 FOPS_OR_ERR_GOTO(val, out);
390 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
391 "has been set back to %s successfully\n",
392 pi->lcore_id, pi->governor_ori);
400 power_acpi_cpufreq_exit(unsigned int lcore_id)
402 struct rte_power_info *pi;
404 if (lcore_id >= RTE_MAX_LCORE) {
405 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
406 lcore_id, RTE_MAX_LCORE - 1U);
409 pi = &lcore_power_info[lcore_id];
410 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
412 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
413 "not used\n", lcore_id);
417 /* Close FD of setting freq */
421 /* Set the governor back to the original */
422 if (power_set_governor_original(pi) < 0) {
423 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
424 "to the original\n", lcore_id);
428 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
429 "'userspace' mode and been set back to the "
430 "original\n", lcore_id);
431 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
436 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
442 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
444 struct rte_power_info *pi;
446 if (lcore_id >= RTE_MAX_LCORE) {
447 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
452 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
456 pi = &lcore_power_info[lcore_id];
457 if (num < pi->nb_freqs) {
458 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
461 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
467 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
469 if (lcore_id >= RTE_MAX_LCORE) {
470 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
471 return RTE_POWER_INVALID_FREQ_INDEX;
474 return lcore_power_info[lcore_id].curr_idx;
478 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
480 if (lcore_id >= RTE_MAX_LCORE) {
481 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
485 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
489 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
491 struct rte_power_info *pi;
493 if (lcore_id >= RTE_MAX_LCORE) {
494 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
498 pi = &lcore_power_info[lcore_id];
499 if (pi->curr_idx + 1 == pi->nb_freqs)
502 /* Frequencies in the array are from high to low. */
503 return set_freq_internal(pi, pi->curr_idx + 1);
507 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
509 struct rte_power_info *pi;
511 if (lcore_id >= RTE_MAX_LCORE) {
512 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
516 pi = &lcore_power_info[lcore_id];
517 if (pi->curr_idx == 0)
520 /* Frequencies in the array are from high to low. */
521 return set_freq_internal(pi, pi->curr_idx - 1);
525 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
527 if (lcore_id >= RTE_MAX_LCORE) {
528 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
532 /* Frequencies in the array are from high to low. */
533 if (lcore_power_info[lcore_id].turbo_available) {
534 if (lcore_power_info[lcore_id].turbo_enable)
536 return set_freq_internal(
537 &lcore_power_info[lcore_id], 0);
539 /* Set to max non-turbo */
540 return set_freq_internal(
541 &lcore_power_info[lcore_id], 1);
543 return set_freq_internal(&lcore_power_info[lcore_id], 0);
547 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
549 struct rte_power_info *pi;
551 if (lcore_id >= RTE_MAX_LCORE) {
552 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
556 pi = &lcore_power_info[lcore_id];
558 /* Frequencies in the array are from high to low. */
559 return set_freq_internal(pi, pi->nb_freqs - 1);
564 power_acpi_turbo_status(unsigned int lcore_id)
566 struct rte_power_info *pi;
568 if (lcore_id >= RTE_MAX_LCORE) {
569 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
573 pi = &lcore_power_info[lcore_id];
575 return pi->turbo_enable;
580 power_acpi_enable_turbo(unsigned int lcore_id)
582 struct rte_power_info *pi;
584 if (lcore_id >= RTE_MAX_LCORE) {
585 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
589 pi = &lcore_power_info[lcore_id];
591 if (pi->turbo_available)
592 pi->turbo_enable = 1;
594 pi->turbo_enable = 0;
596 "Failed to enable turbo on lcore %u\n",
601 /* Max may have changed, so call to max function */
602 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
604 "Failed to set frequency of lcore %u to max\n",
613 power_acpi_disable_turbo(unsigned int lcore_id)
615 struct rte_power_info *pi;
617 if (lcore_id >= RTE_MAX_LCORE) {
618 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
622 pi = &lcore_power_info[lcore_id];
624 pi->turbo_enable = 0;
626 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
627 /* Try to set freq to max by default coming out of turbo */
628 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
630 "Failed to set frequency of lcore %u to max\n",
639 int power_acpi_get_capabilities(unsigned int lcore_id,
640 struct rte_power_core_capabilities *caps)
642 struct rte_power_info *pi;
644 if (lcore_id >= RTE_MAX_LCORE) {
645 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
649 RTE_LOG(ERR, POWER, "Invalid argument\n");
653 pi = &lcore_power_info[lcore_id];
654 caps->capabilities = 0;
655 caps->turbo = !!(pi->turbo_available);