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);
151 /* Check if current governor is userspace */
152 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
153 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
155 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
156 "already userspace\n", pi->lcore_id);
159 /* Save the original governor */
160 snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
162 /* Write 'userspace' to the governor */
163 val = fseek(f, 0, SEEK_SET);
164 FOPS_OR_ERR_GOTO(val, out);
166 val = fputs(POWER_GOVERNOR_USERSPACE, f);
167 FOPS_OR_ERR_GOTO(val, out);
170 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
171 "set to user space successfully\n", pi->lcore_id);
179 * It is to get the available frequencies of the specific lcore by reading the
183 power_get_available_freqs(struct rte_power_info *pi)
186 int ret = -1, i, count;
189 char fullpath[PATH_MAX];
190 char *freqs[RTE_MAX_LCORE_FREQS];
193 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
195 f = fopen(fullpath, "r");
196 FOPEN_OR_ERR_RET(f, ret);
198 s = fgets(buf, sizeof(buf), f);
199 FOPS_OR_NULL_GOTO(s, out);
201 /* Strip the line break if there is */
202 p = strchr(buf, '\n');
206 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
207 count = rte_strsplit(buf, sizeof(buf), freqs,
208 RTE_MAX_LCORE_FREQS, ' ');
210 RTE_LOG(ERR, POWER, "No available frequency in "
211 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
214 if (count >= RTE_MAX_LCORE_FREQS) {
215 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
220 /* Store the available frequncies into power context */
221 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
222 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
224 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
225 POWER_CONVERT_TO_DECIMAL);
228 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
229 pi->turbo_available = 1;
230 pi->turbo_enable = 1;
231 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
234 pi->turbo_available = 0;
235 pi->turbo_enable = 0;
236 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
241 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
242 count, pi->lcore_id);
250 * It is to fopen the sys file for the future setting the lcore frequency.
253 power_init_for_setting_freq(struct rte_power_info *pi)
256 char fullpath[PATH_MAX];
261 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
263 f = fopen(fullpath, "rw+");
264 FOPEN_OR_ERR_RET(f, -1);
266 s = fgets(buf, sizeof(buf), f);
267 FOPS_OR_NULL_GOTO(s, out);
269 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
270 for (i = 0; i < pi->nb_freqs; i++) {
271 if (freq == pi->freqs[i]) {
285 power_acpi_cpufreq_init(unsigned int lcore_id)
287 struct rte_power_info *pi;
289 if (lcore_id >= RTE_MAX_LCORE) {
290 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
291 lcore_id, RTE_MAX_LCORE - 1U);
295 pi = &lcore_power_info[lcore_id];
296 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
298 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
299 "in use\n", lcore_id);
303 pi->lcore_id = lcore_id;
304 /* Check and set the governor */
305 if (power_set_governor_userspace(pi) < 0) {
306 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
307 "userspace\n", lcore_id);
311 /* Get the available frequencies */
312 if (power_get_available_freqs(pi) < 0) {
313 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
314 "lcore %u\n", lcore_id);
318 /* Init for setting lcore frequency */
319 if (power_init_for_setting_freq(pi) < 0) {
320 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
321 "lcore %u\n", lcore_id);
325 /* Set freq to max by default */
326 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
327 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
328 "to max\n", lcore_id);
332 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
333 "power management\n", lcore_id);
334 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
339 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
345 * It is to check the governor and then set the original governor back if
346 * needed by writing the sys file.
349 power_set_governor_original(struct rte_power_info *pi)
354 char fullpath[PATH_MAX];
358 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
360 f = fopen(fullpath, "rw+");
361 FOPEN_OR_ERR_RET(f, ret);
363 s = fgets(buf, sizeof(buf), f);
364 FOPS_OR_NULL_GOTO(s, out);
366 /* Check if the governor to be set is the same as current */
367 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
369 POWER_DEBUG_TRACE("Power management governor of lcore %u "
370 "has already been set to %s\n",
371 pi->lcore_id, pi->governor_ori);
375 /* Write back the original governor */
376 val = fseek(f, 0, SEEK_SET);
377 FOPS_OR_ERR_GOTO(val, out);
379 val = fputs(pi->governor_ori, f);
380 FOPS_OR_ERR_GOTO(val, out);
383 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
384 "has been set back to %s successfully\n",
385 pi->lcore_id, pi->governor_ori);
393 power_acpi_cpufreq_exit(unsigned int lcore_id)
395 struct rte_power_info *pi;
397 if (lcore_id >= RTE_MAX_LCORE) {
398 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
399 lcore_id, RTE_MAX_LCORE - 1U);
402 pi = &lcore_power_info[lcore_id];
403 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
405 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
406 "not used\n", lcore_id);
410 /* Close FD of setting freq */
414 /* Set the governor back to the original */
415 if (power_set_governor_original(pi) < 0) {
416 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
417 "to the original\n", lcore_id);
421 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
422 "'userspace' mode and been set back to the "
423 "original\n", lcore_id);
424 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
429 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
435 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
437 struct rte_power_info *pi;
439 if (lcore_id >= RTE_MAX_LCORE || !freqs) {
440 RTE_LOG(ERR, POWER, "Invalid input parameter\n");
444 pi = &lcore_power_info[lcore_id];
445 if (num < pi->nb_freqs) {
446 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
449 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
455 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
457 if (lcore_id >= RTE_MAX_LCORE) {
458 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
459 return RTE_POWER_INVALID_FREQ_INDEX;
462 return lcore_power_info[lcore_id].curr_idx;
466 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
468 if (lcore_id >= RTE_MAX_LCORE) {
469 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
473 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
477 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
479 struct rte_power_info *pi;
481 if (lcore_id >= RTE_MAX_LCORE) {
482 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
486 pi = &lcore_power_info[lcore_id];
487 if (pi->curr_idx + 1 == pi->nb_freqs)
490 /* Frequencies in the array are from high to low. */
491 return set_freq_internal(pi, pi->curr_idx + 1);
495 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
497 struct rte_power_info *pi;
499 if (lcore_id >= RTE_MAX_LCORE) {
500 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
504 pi = &lcore_power_info[lcore_id];
505 if (pi->curr_idx == 0)
508 /* Frequencies in the array are from high to low. */
509 return set_freq_internal(pi, pi->curr_idx - 1);
513 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
515 if (lcore_id >= RTE_MAX_LCORE) {
516 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
520 /* Frequencies in the array are from high to low. */
521 if (lcore_power_info[lcore_id].turbo_available) {
522 if (lcore_power_info[lcore_id].turbo_enable)
524 return set_freq_internal(
525 &lcore_power_info[lcore_id], 0);
527 /* Set to max non-turbo */
528 return set_freq_internal(
529 &lcore_power_info[lcore_id], 1);
531 return set_freq_internal(&lcore_power_info[lcore_id], 0);
535 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
537 struct rte_power_info *pi;
539 if (lcore_id >= RTE_MAX_LCORE) {
540 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
544 pi = &lcore_power_info[lcore_id];
546 /* Frequencies in the array are from high to low. */
547 return set_freq_internal(pi, pi->nb_freqs - 1);
552 power_acpi_turbo_status(unsigned int lcore_id)
554 struct rte_power_info *pi;
556 if (lcore_id >= RTE_MAX_LCORE) {
557 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
561 pi = &lcore_power_info[lcore_id];
563 return pi->turbo_enable;
568 power_acpi_enable_turbo(unsigned int lcore_id)
570 struct rte_power_info *pi;
572 if (lcore_id >= RTE_MAX_LCORE) {
573 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
577 pi = &lcore_power_info[lcore_id];
579 if (pi->turbo_available)
580 pi->turbo_enable = 1;
582 pi->turbo_enable = 0;
584 "Failed to enable turbo on lcore %u\n",
589 /* Max may have changed, so call to max function */
590 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
592 "Failed to set frequency of lcore %u to max\n",
601 power_acpi_disable_turbo(unsigned int lcore_id)
603 struct rte_power_info *pi;
605 if (lcore_id >= RTE_MAX_LCORE) {
606 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
610 pi = &lcore_power_info[lcore_id];
612 pi->turbo_enable = 0;
614 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
615 /* Try to set freq to max by default coming out of turbo */
616 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
618 "Failed to set frequency of lcore %u to max\n",