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 /* Save the original governor */
156 rte_strscpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
158 /* Check if current governor is userspace */
159 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
160 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
162 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
163 "already userspace\n", pi->lcore_id);
167 /* Write 'userspace' to the governor */
168 val = fseek(f, 0, SEEK_SET);
169 FOPS_OR_ERR_GOTO(val, out);
171 val = fputs(POWER_GOVERNOR_USERSPACE, f);
172 FOPS_OR_ERR_GOTO(val, out);
174 /* We need to flush to see if the fputs succeeds */
176 FOPS_OR_ERR_GOTO(val, out);
179 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
180 "set to user space successfully\n", pi->lcore_id);
188 * It is to get the available frequencies of the specific lcore by reading the
192 power_get_available_freqs(struct rte_power_info *pi)
195 int ret = -1, i, count;
198 char fullpath[PATH_MAX];
199 char *freqs[RTE_MAX_LCORE_FREQS];
202 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
204 f = fopen(fullpath, "r");
205 FOPEN_OR_ERR_RET(f, ret);
207 s = fgets(buf, sizeof(buf), f);
208 FOPS_OR_NULL_GOTO(s, out);
210 /* Strip the line break if there is */
211 p = strchr(buf, '\n');
215 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
216 count = rte_strsplit(buf, sizeof(buf), freqs,
217 RTE_MAX_LCORE_FREQS, ' ');
219 RTE_LOG(ERR, POWER, "No available frequency in "
220 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
223 if (count >= RTE_MAX_LCORE_FREQS) {
224 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
229 /* Store the available frequncies into power context */
230 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
231 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
233 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
234 POWER_CONVERT_TO_DECIMAL);
237 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
238 pi->turbo_available = 1;
239 pi->turbo_enable = 1;
240 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
243 pi->turbo_available = 0;
244 pi->turbo_enable = 0;
245 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
250 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
251 count, pi->lcore_id);
259 * It is to fopen the sys file for the future setting the lcore frequency.
262 power_init_for_setting_freq(struct rte_power_info *pi)
265 char fullpath[PATH_MAX];
270 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
272 f = fopen(fullpath, "rw+");
273 FOPEN_OR_ERR_RET(f, -1);
275 s = fgets(buf, sizeof(buf), f);
276 FOPS_OR_NULL_GOTO(s, out);
278 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
279 for (i = 0; i < pi->nb_freqs; i++) {
280 if (freq == pi->freqs[i]) {
294 power_acpi_cpufreq_check_supported(void)
296 return cpufreq_check_scaling_driver(POWER_ACPI_DRIVER);
300 power_acpi_cpufreq_init(unsigned int lcore_id)
302 struct rte_power_info *pi;
305 if (lcore_id >= RTE_MAX_LCORE) {
306 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
307 lcore_id, RTE_MAX_LCORE - 1U);
311 pi = &lcore_power_info[lcore_id];
312 exp_state = POWER_IDLE;
313 /* The power in use state works as a guard variable between
314 * the CPU frequency control initialization and exit process.
315 * The ACQUIRE memory ordering here pairs with the RELEASE
316 * ordering below as lock to make sure the frequency operations
317 * in the critical section are done under the correct state.
319 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
321 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
322 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
323 "in use\n", lcore_id);
327 pi->lcore_id = lcore_id;
328 /* Check and set the governor */
329 if (power_set_governor_userspace(pi) < 0) {
330 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
331 "userspace\n", lcore_id);
335 /* Get the available frequencies */
336 if (power_get_available_freqs(pi) < 0) {
337 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
338 "lcore %u\n", lcore_id);
342 /* Init for setting lcore frequency */
343 if (power_init_for_setting_freq(pi) < 0) {
344 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
345 "lcore %u\n", lcore_id);
349 /* Set freq to max by default */
350 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
351 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
352 "to max\n", lcore_id);
356 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
357 "power management\n", lcore_id);
358 exp_state = POWER_ONGOING;
359 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
360 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
365 exp_state = POWER_ONGOING;
366 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
367 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
373 * It is to check the governor and then set the original governor back if
374 * needed by writing the sys file.
377 power_set_governor_original(struct rte_power_info *pi)
382 char fullpath[PATH_MAX];
386 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
388 f = fopen(fullpath, "rw+");
389 FOPEN_OR_ERR_RET(f, ret);
391 s = fgets(buf, sizeof(buf), f);
392 FOPS_OR_NULL_GOTO(s, out);
394 /* Check if the governor to be set is the same as current */
395 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
397 POWER_DEBUG_TRACE("Power management governor of lcore %u "
398 "has already been set to %s\n",
399 pi->lcore_id, pi->governor_ori);
403 /* Write back the original governor */
404 val = fseek(f, 0, SEEK_SET);
405 FOPS_OR_ERR_GOTO(val, out);
407 val = fputs(pi->governor_ori, f);
408 FOPS_OR_ERR_GOTO(val, out);
411 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
412 "has been set back to %s successfully\n",
413 pi->lcore_id, pi->governor_ori);
421 power_acpi_cpufreq_exit(unsigned int lcore_id)
423 struct rte_power_info *pi;
426 if (lcore_id >= RTE_MAX_LCORE) {
427 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
428 lcore_id, RTE_MAX_LCORE - 1U);
431 pi = &lcore_power_info[lcore_id];
432 exp_state = POWER_USED;
433 /* The power in use state works as a guard variable between
434 * the CPU frequency control initialization and exit process.
435 * The ACQUIRE memory ordering here pairs with the RELEASE
436 * ordering below as lock to make sure the frequency operations
437 * in the critical section are done under the correct state.
439 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
441 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
442 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
443 "not used\n", lcore_id);
447 /* Close FD of setting freq */
451 /* Set the governor back to the original */
452 if (power_set_governor_original(pi) < 0) {
453 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
454 "to the original\n", lcore_id);
458 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
459 "'userspace' mode and been set back to the "
460 "original\n", lcore_id);
461 exp_state = POWER_ONGOING;
462 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
463 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
468 exp_state = POWER_ONGOING;
469 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
470 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
476 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
478 struct rte_power_info *pi;
480 if (lcore_id >= RTE_MAX_LCORE) {
481 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
486 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
490 pi = &lcore_power_info[lcore_id];
491 if (num < pi->nb_freqs) {
492 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
495 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
501 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
503 if (lcore_id >= RTE_MAX_LCORE) {
504 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
505 return RTE_POWER_INVALID_FREQ_INDEX;
508 return lcore_power_info[lcore_id].curr_idx;
512 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
514 if (lcore_id >= RTE_MAX_LCORE) {
515 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
519 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
523 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
525 struct rte_power_info *pi;
527 if (lcore_id >= RTE_MAX_LCORE) {
528 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
532 pi = &lcore_power_info[lcore_id];
533 if (pi->curr_idx + 1 == pi->nb_freqs)
536 /* Frequencies in the array are from high to low. */
537 return set_freq_internal(pi, pi->curr_idx + 1);
541 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
543 struct rte_power_info *pi;
545 if (lcore_id >= RTE_MAX_LCORE) {
546 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
550 pi = &lcore_power_info[lcore_id];
551 if (pi->curr_idx == 0 ||
552 (pi->curr_idx == 1 && pi->turbo_available && !pi->turbo_enable))
555 /* Frequencies in the array are from high to low. */
556 return set_freq_internal(pi, pi->curr_idx - 1);
560 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
562 if (lcore_id >= RTE_MAX_LCORE) {
563 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
567 /* Frequencies in the array are from high to low. */
568 if (lcore_power_info[lcore_id].turbo_available) {
569 if (lcore_power_info[lcore_id].turbo_enable)
571 return set_freq_internal(
572 &lcore_power_info[lcore_id], 0);
574 /* Set to max non-turbo */
575 return set_freq_internal(
576 &lcore_power_info[lcore_id], 1);
578 return set_freq_internal(&lcore_power_info[lcore_id], 0);
582 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
584 struct rte_power_info *pi;
586 if (lcore_id >= RTE_MAX_LCORE) {
587 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
591 pi = &lcore_power_info[lcore_id];
593 /* Frequencies in the array are from high to low. */
594 return set_freq_internal(pi, pi->nb_freqs - 1);
599 power_acpi_turbo_status(unsigned int lcore_id)
601 struct rte_power_info *pi;
603 if (lcore_id >= RTE_MAX_LCORE) {
604 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
608 pi = &lcore_power_info[lcore_id];
610 return pi->turbo_enable;
615 power_acpi_enable_turbo(unsigned int lcore_id)
617 struct rte_power_info *pi;
619 if (lcore_id >= RTE_MAX_LCORE) {
620 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
624 pi = &lcore_power_info[lcore_id];
626 if (pi->turbo_available)
627 pi->turbo_enable = 1;
629 pi->turbo_enable = 0;
631 "Failed to enable turbo on lcore %u\n",
636 /* Max may have changed, so call to max function */
637 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
639 "Failed to set frequency of lcore %u to max\n",
648 power_acpi_disable_turbo(unsigned int lcore_id)
650 struct rte_power_info *pi;
652 if (lcore_id >= RTE_MAX_LCORE) {
653 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
657 pi = &lcore_power_info[lcore_id];
659 pi->turbo_enable = 0;
661 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
662 /* Try to set freq to max by default coming out of turbo */
663 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
665 "Failed to set frequency of lcore %u to max\n",
674 int power_acpi_get_capabilities(unsigned int lcore_id,
675 struct rte_power_core_capabilities *caps)
677 struct rte_power_info *pi;
679 if (lcore_id >= RTE_MAX_LCORE) {
680 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
684 RTE_LOG(ERR, POWER, "Invalid argument\n");
688 pi = &lcore_power_info[lcore_id];
689 caps->capabilities = 0;
690 caps->turbo = !!(pi->turbo_available);