1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_memcpy.h>
16 #include <rte_memory.h>
17 #include <rte_string_fns.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 opened\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"
61 #define POWER_ACPI_DRIVER "acpi-cpufreq"
66 #define PLATFORM_INFO 0x0CE
67 #define TURBO_RATIO_LIMIT 0x1AD
68 #define IA32_PERF_CTL 0x199
69 #define CORE_TURBO_DISABLE_BIT ((uint64_t)1<<32)
79 * Power info per lcore.
81 struct rte_power_info {
82 unsigned int lcore_id; /**< Logical core id */
83 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
84 uint32_t nb_freqs; /**< number of available freqs */
85 FILE *f; /**< FD of scaling_setspeed */
86 char governor_ori[32]; /**< Original governor name */
87 uint32_t curr_idx; /**< Freq index in freqs array */
88 uint32_t state; /**< Power in use state */
89 uint16_t turbo_available; /**< Turbo Boost available */
90 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
91 } __rte_cache_aligned;
93 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
96 * It is to set specific freq for specific logical core, according to the index
97 * of supported frequencies.
100 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
102 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
103 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
104 "should be less than %u\n", idx, pi->nb_freqs);
108 /* Check if it is the same as current */
109 if (idx == pi->curr_idx)
112 POWER_DEBUG_TRACE("Frequency[%u] %u to be set for lcore %u\n",
113 idx, pi->freqs[idx], pi->lcore_id);
114 if (fseek(pi->f, 0, SEEK_SET) < 0) {
115 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
116 "for setting frequency for lcore %u\n", pi->lcore_id);
119 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
120 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
121 "lcore %u\n", pi->lcore_id);
131 * It is to check the current scaling governor by reading sys file, and then
132 * set it into 'userspace' if it is not by writing the sys file. The original
133 * governor will be saved for rolling back.
136 power_set_governor_userspace(struct rte_power_info *pi)
141 char fullpath[PATH_MAX];
145 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
147 f = fopen(fullpath, "rw+");
148 FOPEN_OR_ERR_RET(f, ret);
150 s = fgets(buf, sizeof(buf), f);
151 FOPS_OR_NULL_GOTO(s, out);
152 /* Strip off terminating '\n' */
155 /* Check if current governor is userspace */
156 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
157 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
159 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
160 "already userspace\n", pi->lcore_id);
163 /* Save the original governor */
164 strlcpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
166 /* Write 'userspace' to the governor */
167 val = fseek(f, 0, SEEK_SET);
168 FOPS_OR_ERR_GOTO(val, out);
170 val = fputs(POWER_GOVERNOR_USERSPACE, f);
171 FOPS_OR_ERR_GOTO(val, out);
173 /* We need to flush to see if the fputs succeeds */
175 FOPS_OR_ERR_GOTO(val, out);
178 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
179 "set to user space successfully\n", pi->lcore_id);
187 * It is to get the available frequencies of the specific lcore by reading the
191 power_get_available_freqs(struct rte_power_info *pi)
194 int ret = -1, i, count;
197 char fullpath[PATH_MAX];
198 char *freqs[RTE_MAX_LCORE_FREQS];
201 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
203 f = fopen(fullpath, "r");
204 FOPEN_OR_ERR_RET(f, ret);
206 s = fgets(buf, sizeof(buf), f);
207 FOPS_OR_NULL_GOTO(s, out);
209 /* Strip the line break if there is */
210 p = strchr(buf, '\n');
214 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
215 count = rte_strsplit(buf, sizeof(buf), freqs,
216 RTE_MAX_LCORE_FREQS, ' ');
218 RTE_LOG(ERR, POWER, "No available frequency in "
219 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
222 if (count >= RTE_MAX_LCORE_FREQS) {
223 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
228 /* Store the available frequncies into power context */
229 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
230 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
232 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
233 POWER_CONVERT_TO_DECIMAL);
236 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
237 pi->turbo_available = 1;
238 pi->turbo_enable = 1;
239 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
242 pi->turbo_available = 0;
243 pi->turbo_enable = 0;
244 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
249 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
250 count, pi->lcore_id);
258 * It is to fopen the sys file for the future setting the lcore frequency.
261 power_init_for_setting_freq(struct rte_power_info *pi)
264 char fullpath[PATH_MAX];
269 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
271 f = fopen(fullpath, "rw+");
272 FOPEN_OR_ERR_RET(f, -1);
274 s = fgets(buf, sizeof(buf), f);
275 FOPS_OR_NULL_GOTO(s, out);
277 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
278 for (i = 0; i < pi->nb_freqs; i++) {
279 if (freq == pi->freqs[i]) {
293 power_acpi_cpufreq_check_supported(void)
295 return cpufreq_check_scaling_driver(POWER_ACPI_DRIVER);
299 power_acpi_cpufreq_init(unsigned int lcore_id)
301 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 exp_state = POWER_IDLE;
312 /* The power in use state works as a guard variable between
313 * the CPU frequency control initialization and exit process.
314 * The ACQUIRE memory ordering here pairs with the RELEASE
315 * ordering below as lock to make sure the frequency operations
316 * in the critical section are done under the correct state.
318 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
320 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
321 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
322 "in use\n", lcore_id);
326 pi->lcore_id = lcore_id;
327 /* Check and set the governor */
328 if (power_set_governor_userspace(pi) < 0) {
329 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
330 "userspace\n", lcore_id);
334 /* Get the available frequencies */
335 if (power_get_available_freqs(pi) < 0) {
336 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
337 "lcore %u\n", lcore_id);
341 /* Init for setting lcore frequency */
342 if (power_init_for_setting_freq(pi) < 0) {
343 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
344 "lcore %u\n", lcore_id);
348 /* Set freq to max by default */
349 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
350 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
351 "to max\n", lcore_id);
355 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
356 "power management\n", lcore_id);
357 exp_state = POWER_ONGOING;
358 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
359 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
364 exp_state = POWER_ONGOING;
365 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
366 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
372 * It is to check the governor and then set the original governor back if
373 * needed by writing the sys file.
376 power_set_governor_original(struct rte_power_info *pi)
381 char fullpath[PATH_MAX];
385 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
387 f = fopen(fullpath, "rw+");
388 FOPEN_OR_ERR_RET(f, ret);
390 s = fgets(buf, sizeof(buf), f);
391 FOPS_OR_NULL_GOTO(s, out);
393 /* Check if the governor to be set is the same as current */
394 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
396 POWER_DEBUG_TRACE("Power management governor of lcore %u "
397 "has already been set to %s\n",
398 pi->lcore_id, pi->governor_ori);
402 /* Write back the original governor */
403 val = fseek(f, 0, SEEK_SET);
404 FOPS_OR_ERR_GOTO(val, out);
406 val = fputs(pi->governor_ori, f);
407 FOPS_OR_ERR_GOTO(val, out);
410 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
411 "has been set back to %s successfully\n",
412 pi->lcore_id, pi->governor_ori);
420 power_acpi_cpufreq_exit(unsigned int lcore_id)
422 struct rte_power_info *pi;
425 if (lcore_id >= RTE_MAX_LCORE) {
426 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
427 lcore_id, RTE_MAX_LCORE - 1U);
430 pi = &lcore_power_info[lcore_id];
431 exp_state = POWER_USED;
432 /* The power in use state works as a guard variable between
433 * the CPU frequency control initialization and exit process.
434 * The ACQUIRE memory ordering here pairs with the RELEASE
435 * ordering below as lock to make sure the frequency operations
436 * in the critical section are done under the correct state.
438 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
440 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
441 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
442 "not used\n", lcore_id);
446 /* Close FD of setting freq */
450 /* Set the governor back to the original */
451 if (power_set_governor_original(pi) < 0) {
452 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
453 "to the original\n", lcore_id);
457 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
458 "'userspace' mode and been set back to the "
459 "original\n", lcore_id);
460 exp_state = POWER_ONGOING;
461 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
462 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
467 exp_state = POWER_ONGOING;
468 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
469 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
475 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
477 struct rte_power_info *pi;
479 if (lcore_id >= RTE_MAX_LCORE) {
480 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
485 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
489 pi = &lcore_power_info[lcore_id];
490 if (num < pi->nb_freqs) {
491 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
494 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
500 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
502 if (lcore_id >= RTE_MAX_LCORE) {
503 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
504 return RTE_POWER_INVALID_FREQ_INDEX;
507 return lcore_power_info[lcore_id].curr_idx;
511 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
513 if (lcore_id >= RTE_MAX_LCORE) {
514 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
518 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
522 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
524 struct rte_power_info *pi;
526 if (lcore_id >= RTE_MAX_LCORE) {
527 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
531 pi = &lcore_power_info[lcore_id];
532 if (pi->curr_idx + 1 == pi->nb_freqs)
535 /* Frequencies in the array are from high to low. */
536 return set_freq_internal(pi, pi->curr_idx + 1);
540 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
542 struct rte_power_info *pi;
544 if (lcore_id >= RTE_MAX_LCORE) {
545 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
549 pi = &lcore_power_info[lcore_id];
550 if (pi->curr_idx == 0 ||
551 (pi->curr_idx == 1 && pi->turbo_available && !pi->turbo_enable))
554 /* Frequencies in the array are from high to low. */
555 return set_freq_internal(pi, pi->curr_idx - 1);
559 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
561 if (lcore_id >= RTE_MAX_LCORE) {
562 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
566 /* Frequencies in the array are from high to low. */
567 if (lcore_power_info[lcore_id].turbo_available) {
568 if (lcore_power_info[lcore_id].turbo_enable)
570 return set_freq_internal(
571 &lcore_power_info[lcore_id], 0);
573 /* Set to max non-turbo */
574 return set_freq_internal(
575 &lcore_power_info[lcore_id], 1);
577 return set_freq_internal(&lcore_power_info[lcore_id], 0);
581 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
583 struct rte_power_info *pi;
585 if (lcore_id >= RTE_MAX_LCORE) {
586 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
590 pi = &lcore_power_info[lcore_id];
592 /* Frequencies in the array are from high to low. */
593 return set_freq_internal(pi, pi->nb_freqs - 1);
598 power_acpi_turbo_status(unsigned int lcore_id)
600 struct rte_power_info *pi;
602 if (lcore_id >= RTE_MAX_LCORE) {
603 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
607 pi = &lcore_power_info[lcore_id];
609 return pi->turbo_enable;
614 power_acpi_enable_turbo(unsigned int lcore_id)
616 struct rte_power_info *pi;
618 if (lcore_id >= RTE_MAX_LCORE) {
619 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
623 pi = &lcore_power_info[lcore_id];
625 if (pi->turbo_available)
626 pi->turbo_enable = 1;
628 pi->turbo_enable = 0;
630 "Failed to enable turbo on lcore %u\n",
635 /* Max may have changed, so call to max function */
636 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
638 "Failed to set frequency of lcore %u to max\n",
647 power_acpi_disable_turbo(unsigned int lcore_id)
649 struct rte_power_info *pi;
651 if (lcore_id >= RTE_MAX_LCORE) {
652 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
656 pi = &lcore_power_info[lcore_id];
658 pi->turbo_enable = 0;
660 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
661 /* Try to set freq to max by default coming out of turbo */
662 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
664 "Failed to set frequency of lcore %u to max\n",
673 int power_acpi_get_capabilities(unsigned int lcore_id,
674 struct rte_power_core_capabilities *caps)
676 struct rte_power_info *pi;
678 if (lcore_id >= RTE_MAX_LCORE) {
679 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
683 RTE_LOG(ERR, POWER, "Invalid argument\n");
687 pi = &lcore_power_info[lcore_id];
688 caps->capabilities = 0;
689 caps->turbo = !!(pi->turbo_available);