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>
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 snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
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);
173 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
174 "set to user space successfully\n", pi->lcore_id);
182 * It is to get the available frequencies of the specific lcore by reading the
186 power_get_available_freqs(struct rte_power_info *pi)
189 int ret = -1, i, count;
192 char fullpath[PATH_MAX];
193 char *freqs[RTE_MAX_LCORE_FREQS];
196 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
198 f = fopen(fullpath, "r");
199 FOPEN_OR_ERR_RET(f, ret);
201 s = fgets(buf, sizeof(buf), f);
202 FOPS_OR_NULL_GOTO(s, out);
204 /* Strip the line break if there is */
205 p = strchr(buf, '\n');
209 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
210 count = rte_strsplit(buf, sizeof(buf), freqs,
211 RTE_MAX_LCORE_FREQS, ' ');
213 RTE_LOG(ERR, POWER, "No available frequency in "
214 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
217 if (count >= RTE_MAX_LCORE_FREQS) {
218 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
223 /* Store the available frequncies into power context */
224 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
225 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
227 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
228 POWER_CONVERT_TO_DECIMAL);
231 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
232 pi->turbo_available = 1;
233 pi->turbo_enable = 1;
234 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
237 pi->turbo_available = 0;
238 pi->turbo_enable = 0;
239 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
244 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
245 count, pi->lcore_id);
253 * It is to fopen the sys file for the future setting the lcore frequency.
256 power_init_for_setting_freq(struct rte_power_info *pi)
259 char fullpath[PATH_MAX];
264 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
266 f = fopen(fullpath, "rw+");
267 FOPEN_OR_ERR_RET(f, -1);
269 s = fgets(buf, sizeof(buf), f);
270 FOPS_OR_NULL_GOTO(s, out);
272 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
273 for (i = 0; i < pi->nb_freqs; i++) {
274 if (freq == pi->freqs[i]) {
288 power_acpi_cpufreq_init(unsigned int lcore_id)
290 struct rte_power_info *pi;
292 if (lcore_id >= RTE_MAX_LCORE) {
293 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
294 lcore_id, RTE_MAX_LCORE - 1U);
298 pi = &lcore_power_info[lcore_id];
299 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
301 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
302 "in use\n", lcore_id);
306 pi->lcore_id = lcore_id;
307 /* Check and set the governor */
308 if (power_set_governor_userspace(pi) < 0) {
309 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
310 "userspace\n", lcore_id);
314 /* Get the available frequencies */
315 if (power_get_available_freqs(pi) < 0) {
316 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
317 "lcore %u\n", lcore_id);
321 /* Init for setting lcore frequency */
322 if (power_init_for_setting_freq(pi) < 0) {
323 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
324 "lcore %u\n", lcore_id);
328 /* Set freq to max by default */
329 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
330 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
331 "to max\n", lcore_id);
335 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
336 "power management\n", lcore_id);
337 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
342 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
348 * It is to check the governor and then set the original governor back if
349 * needed by writing the sys file.
352 power_set_governor_original(struct rte_power_info *pi)
357 char fullpath[PATH_MAX];
361 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
363 f = fopen(fullpath, "rw+");
364 FOPEN_OR_ERR_RET(f, ret);
366 s = fgets(buf, sizeof(buf), f);
367 FOPS_OR_NULL_GOTO(s, out);
369 /* Check if the governor to be set is the same as current */
370 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
372 POWER_DEBUG_TRACE("Power management governor of lcore %u "
373 "has already been set to %s\n",
374 pi->lcore_id, pi->governor_ori);
378 /* Write back the original governor */
379 val = fseek(f, 0, SEEK_SET);
380 FOPS_OR_ERR_GOTO(val, out);
382 val = fputs(pi->governor_ori, f);
383 FOPS_OR_ERR_GOTO(val, out);
386 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
387 "has been set back to %s successfully\n",
388 pi->lcore_id, pi->governor_ori);
396 power_acpi_cpufreq_exit(unsigned int lcore_id)
398 struct rte_power_info *pi;
400 if (lcore_id >= RTE_MAX_LCORE) {
401 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
402 lcore_id, RTE_MAX_LCORE - 1U);
405 pi = &lcore_power_info[lcore_id];
406 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
408 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
409 "not used\n", lcore_id);
413 /* Close FD of setting freq */
417 /* Set the governor back to the original */
418 if (power_set_governor_original(pi) < 0) {
419 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
420 "to the original\n", lcore_id);
424 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
425 "'userspace' mode and been set back to the "
426 "original\n", lcore_id);
427 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
432 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
438 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
440 struct rte_power_info *pi;
442 if (lcore_id >= RTE_MAX_LCORE || !freqs) {
443 RTE_LOG(ERR, POWER, "Invalid input parameter\n");
447 pi = &lcore_power_info[lcore_id];
448 if (num < pi->nb_freqs) {
449 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
452 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
458 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
460 if (lcore_id >= RTE_MAX_LCORE) {
461 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
462 return RTE_POWER_INVALID_FREQ_INDEX;
465 return lcore_power_info[lcore_id].curr_idx;
469 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
471 if (lcore_id >= RTE_MAX_LCORE) {
472 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
476 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
480 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
482 struct rte_power_info *pi;
484 if (lcore_id >= RTE_MAX_LCORE) {
485 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
489 pi = &lcore_power_info[lcore_id];
490 if (pi->curr_idx + 1 == pi->nb_freqs)
493 /* Frequencies in the array are from high to low. */
494 return set_freq_internal(pi, pi->curr_idx + 1);
498 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
500 struct rte_power_info *pi;
502 if (lcore_id >= RTE_MAX_LCORE) {
503 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
507 pi = &lcore_power_info[lcore_id];
508 if (pi->curr_idx == 0)
511 /* Frequencies in the array are from high to low. */
512 return set_freq_internal(pi, pi->curr_idx - 1);
516 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
518 if (lcore_id >= RTE_MAX_LCORE) {
519 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
523 /* Frequencies in the array are from high to low. */
524 if (lcore_power_info[lcore_id].turbo_available) {
525 if (lcore_power_info[lcore_id].turbo_enable)
527 return set_freq_internal(
528 &lcore_power_info[lcore_id], 0);
530 /* Set to max non-turbo */
531 return set_freq_internal(
532 &lcore_power_info[lcore_id], 1);
534 return set_freq_internal(&lcore_power_info[lcore_id], 0);
538 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
540 struct rte_power_info *pi;
542 if (lcore_id >= RTE_MAX_LCORE) {
543 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
547 pi = &lcore_power_info[lcore_id];
549 /* Frequencies in the array are from high to low. */
550 return set_freq_internal(pi, pi->nb_freqs - 1);
555 power_acpi_turbo_status(unsigned int lcore_id)
557 struct rte_power_info *pi;
559 if (lcore_id >= RTE_MAX_LCORE) {
560 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
564 pi = &lcore_power_info[lcore_id];
566 return pi->turbo_enable;
571 power_acpi_enable_turbo(unsigned int lcore_id)
573 struct rte_power_info *pi;
575 if (lcore_id >= RTE_MAX_LCORE) {
576 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
580 pi = &lcore_power_info[lcore_id];
582 if (pi->turbo_available)
583 pi->turbo_enable = 1;
585 pi->turbo_enable = 0;
587 "Failed to enable turbo on lcore %u\n",
592 /* Max may have changed, so call to max function */
593 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
595 "Failed to set frequency of lcore %u to max\n",
604 power_acpi_disable_turbo(unsigned int lcore_id)
606 struct rte_power_info *pi;
608 if (lcore_id >= RTE_MAX_LCORE) {
609 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
613 pi = &lcore_power_info[lcore_id];
615 pi->turbo_enable = 0;
617 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
618 /* Try to set freq to max by default coming out of turbo */
619 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
621 "Failed to set frequency of lcore %u to max\n",
630 int power_acpi_get_capabilities(unsigned int lcore_id,
631 struct rte_power_core_capabilities *caps)
633 struct rte_power_info *pi;
635 if (lcore_id >= RTE_MAX_LCORE) {
636 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
640 RTE_LOG(ERR, POWER, "Invalid argument\n");
644 pi = &lcore_power_info[lcore_id];
645 caps->capabilities = 0;
646 caps->turbo = !!(pi->turbo_available);