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);
169 /* We need to flush to see if the fputs succeeds */
171 FOPS_OR_ERR_GOTO(val, out);
174 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
175 "set to user space successfully\n", pi->lcore_id);
183 * It is to get the available frequencies of the specific lcore by reading the
187 power_get_available_freqs(struct rte_power_info *pi)
190 int ret = -1, i, count;
193 char fullpath[PATH_MAX];
194 char *freqs[RTE_MAX_LCORE_FREQS];
197 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
199 f = fopen(fullpath, "r");
200 FOPEN_OR_ERR_RET(f, ret);
202 s = fgets(buf, sizeof(buf), f);
203 FOPS_OR_NULL_GOTO(s, out);
205 /* Strip the line break if there is */
206 p = strchr(buf, '\n');
210 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
211 count = rte_strsplit(buf, sizeof(buf), freqs,
212 RTE_MAX_LCORE_FREQS, ' ');
214 RTE_LOG(ERR, POWER, "No available frequency in "
215 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
218 if (count >= RTE_MAX_LCORE_FREQS) {
219 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
224 /* Store the available frequncies into power context */
225 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
226 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
228 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
229 POWER_CONVERT_TO_DECIMAL);
232 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
233 pi->turbo_available = 1;
234 pi->turbo_enable = 1;
235 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
238 pi->turbo_available = 0;
239 pi->turbo_enable = 0;
240 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
245 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
246 count, pi->lcore_id);
254 * It is to fopen the sys file for the future setting the lcore frequency.
257 power_init_for_setting_freq(struct rte_power_info *pi)
260 char fullpath[PATH_MAX];
265 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
267 f = fopen(fullpath, "rw+");
268 FOPEN_OR_ERR_RET(f, -1);
270 s = fgets(buf, sizeof(buf), f);
271 FOPS_OR_NULL_GOTO(s, out);
273 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
274 for (i = 0; i < pi->nb_freqs; i++) {
275 if (freq == pi->freqs[i]) {
289 power_acpi_cpufreq_init(unsigned int lcore_id)
291 struct rte_power_info *pi;
293 if (lcore_id >= RTE_MAX_LCORE) {
294 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
295 lcore_id, RTE_MAX_LCORE - 1U);
299 pi = &lcore_power_info[lcore_id];
300 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
302 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
303 "in use\n", lcore_id);
307 pi->lcore_id = lcore_id;
308 /* Check and set the governor */
309 if (power_set_governor_userspace(pi) < 0) {
310 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
311 "userspace\n", lcore_id);
315 /* Get the available frequencies */
316 if (power_get_available_freqs(pi) < 0) {
317 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
318 "lcore %u\n", lcore_id);
322 /* Init for setting lcore frequency */
323 if (power_init_for_setting_freq(pi) < 0) {
324 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
325 "lcore %u\n", lcore_id);
329 /* Set freq to max by default */
330 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
331 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
332 "to max\n", lcore_id);
336 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
337 "power management\n", lcore_id);
338 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
343 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
349 * It is to check the governor and then set the original governor back if
350 * needed by writing the sys file.
353 power_set_governor_original(struct rte_power_info *pi)
358 char fullpath[PATH_MAX];
362 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
364 f = fopen(fullpath, "rw+");
365 FOPEN_OR_ERR_RET(f, ret);
367 s = fgets(buf, sizeof(buf), f);
368 FOPS_OR_NULL_GOTO(s, out);
370 /* Check if the governor to be set is the same as current */
371 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
373 POWER_DEBUG_TRACE("Power management governor of lcore %u "
374 "has already been set to %s\n",
375 pi->lcore_id, pi->governor_ori);
379 /* Write back the original governor */
380 val = fseek(f, 0, SEEK_SET);
381 FOPS_OR_ERR_GOTO(val, out);
383 val = fputs(pi->governor_ori, f);
384 FOPS_OR_ERR_GOTO(val, out);
387 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
388 "has been set back to %s successfully\n",
389 pi->lcore_id, pi->governor_ori);
397 power_acpi_cpufreq_exit(unsigned int lcore_id)
399 struct rte_power_info *pi;
401 if (lcore_id >= RTE_MAX_LCORE) {
402 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
403 lcore_id, RTE_MAX_LCORE - 1U);
406 pi = &lcore_power_info[lcore_id];
407 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
409 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
410 "not used\n", lcore_id);
414 /* Close FD of setting freq */
418 /* Set the governor back to the original */
419 if (power_set_governor_original(pi) < 0) {
420 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
421 "to the original\n", lcore_id);
425 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
426 "'userspace' mode and been set back to the "
427 "original\n", lcore_id);
428 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
433 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
439 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
441 struct rte_power_info *pi;
443 if (lcore_id >= RTE_MAX_LCORE) {
444 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
449 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
453 pi = &lcore_power_info[lcore_id];
454 if (num < pi->nb_freqs) {
455 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
458 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
464 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
466 if (lcore_id >= RTE_MAX_LCORE) {
467 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
468 return RTE_POWER_INVALID_FREQ_INDEX;
471 return lcore_power_info[lcore_id].curr_idx;
475 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
477 if (lcore_id >= RTE_MAX_LCORE) {
478 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
482 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
486 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
488 struct rte_power_info *pi;
490 if (lcore_id >= RTE_MAX_LCORE) {
491 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
495 pi = &lcore_power_info[lcore_id];
496 if (pi->curr_idx + 1 == pi->nb_freqs)
499 /* Frequencies in the array are from high to low. */
500 return set_freq_internal(pi, pi->curr_idx + 1);
504 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
506 struct rte_power_info *pi;
508 if (lcore_id >= RTE_MAX_LCORE) {
509 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
513 pi = &lcore_power_info[lcore_id];
514 if (pi->curr_idx == 0)
517 /* Frequencies in the array are from high to low. */
518 return set_freq_internal(pi, pi->curr_idx - 1);
522 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
524 if (lcore_id >= RTE_MAX_LCORE) {
525 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
529 /* Frequencies in the array are from high to low. */
530 if (lcore_power_info[lcore_id].turbo_available) {
531 if (lcore_power_info[lcore_id].turbo_enable)
533 return set_freq_internal(
534 &lcore_power_info[lcore_id], 0);
536 /* Set to max non-turbo */
537 return set_freq_internal(
538 &lcore_power_info[lcore_id], 1);
540 return set_freq_internal(&lcore_power_info[lcore_id], 0);
544 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
546 struct rte_power_info *pi;
548 if (lcore_id >= RTE_MAX_LCORE) {
549 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
553 pi = &lcore_power_info[lcore_id];
555 /* Frequencies in the array are from high to low. */
556 return set_freq_internal(pi, pi->nb_freqs - 1);
561 power_acpi_turbo_status(unsigned int lcore_id)
563 struct rte_power_info *pi;
565 if (lcore_id >= RTE_MAX_LCORE) {
566 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
570 pi = &lcore_power_info[lcore_id];
572 return pi->turbo_enable;
577 power_acpi_enable_turbo(unsigned int lcore_id)
579 struct rte_power_info *pi;
581 if (lcore_id >= RTE_MAX_LCORE) {
582 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
586 pi = &lcore_power_info[lcore_id];
588 if (pi->turbo_available)
589 pi->turbo_enable = 1;
591 pi->turbo_enable = 0;
593 "Failed to enable turbo on lcore %u\n",
598 /* Max may have changed, so call to max function */
599 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
601 "Failed to set frequency of lcore %u to max\n",
610 power_acpi_disable_turbo(unsigned int lcore_id)
612 struct rte_power_info *pi;
614 if (lcore_id >= RTE_MAX_LCORE) {
615 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
619 pi = &lcore_power_info[lcore_id];
621 pi->turbo_enable = 0;
623 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
624 /* Try to set freq to max by default coming out of turbo */
625 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
627 "Failed to set frequency of lcore %u to max\n",
636 int power_acpi_get_capabilities(unsigned int lcore_id,
637 struct rte_power_core_capabilities *caps)
639 struct rte_power_info *pi;
641 if (lcore_id >= RTE_MAX_LCORE) {
642 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
646 RTE_LOG(ERR, POWER, "Invalid argument\n");
650 pi = &lcore_power_info[lcore_id];
651 caps->capabilities = 0;
652 caps->turbo = !!(pi->turbo_available);