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"
23 #define POWER_CONVERT_TO_DECIMAL 10
25 #define POWER_GOVERNOR_USERSPACE "userspace"
26 #define POWER_SYSFILE_AVAIL_FREQ \
27 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies"
28 #define POWER_SYSFILE_SETSPEED \
29 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
30 #define POWER_ACPI_DRIVER "acpi-cpufreq"
35 #define PLATFORM_INFO 0x0CE
36 #define TURBO_RATIO_LIMIT 0x1AD
37 #define IA32_PERF_CTL 0x199
38 #define CORE_TURBO_DISABLE_BIT ((uint64_t)1<<32)
48 * Power info per lcore.
50 struct acpi_power_info {
51 unsigned int lcore_id; /**< Logical core id */
52 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
53 uint32_t nb_freqs; /**< number of available freqs */
54 FILE *f; /**< FD of scaling_setspeed */
55 char governor_ori[32]; /**< Original governor name */
56 uint32_t curr_idx; /**< Freq index in freqs array */
57 uint32_t state; /**< Power in use state */
58 uint16_t turbo_available; /**< Turbo Boost available */
59 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
60 } __rte_cache_aligned;
62 static struct acpi_power_info lcore_power_info[RTE_MAX_LCORE];
65 * It is to set specific freq for specific logical core, according to the index
66 * of supported frequencies.
69 set_freq_internal(struct acpi_power_info *pi, uint32_t idx)
71 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
72 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
73 "should be less than %u\n", idx, pi->nb_freqs);
77 /* Check if it is the same as current */
78 if (idx == pi->curr_idx)
81 POWER_DEBUG_TRACE("Frequency[%u] %u to be set for lcore %u\n",
82 idx, pi->freqs[idx], pi->lcore_id);
83 if (fseek(pi->f, 0, SEEK_SET) < 0) {
84 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
85 "for setting frequency for lcore %u\n", pi->lcore_id);
88 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
89 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
90 "lcore %u\n", pi->lcore_id);
100 * It is to check the current scaling governor by reading sys file, and then
101 * set it into 'userspace' if it is not by writing the sys file. The original
102 * governor will be saved for rolling back.
105 power_set_governor_userspace(struct acpi_power_info *pi)
107 return power_set_governor(pi->lcore_id, POWER_GOVERNOR_USERSPACE,
108 pi->governor_ori, sizeof(pi->governor_ori));
112 * It is to check the governor and then set the original governor back if
113 * needed by writing the sys file.
116 power_set_governor_original(struct acpi_power_info *pi)
118 return power_set_governor(pi->lcore_id, pi->governor_ori, NULL, 0);
122 * It is to get the available frequencies of the specific lcore by reading the
126 power_get_available_freqs(struct acpi_power_info *pi)
129 int ret = -1, i, count;
132 char *freqs[RTE_MAX_LCORE_FREQS];
134 open_core_sysfs_file(&f, "r", POWER_SYSFILE_AVAIL_FREQ, pi->lcore_id);
136 RTE_LOG(ERR, POWER, "failed to open %s\n",
137 POWER_SYSFILE_AVAIL_FREQ);
141 ret = read_core_sysfs_s(f, buf, sizeof(buf));
143 RTE_LOG(ERR, POWER, "Failed to read %s\n",
144 POWER_SYSFILE_AVAIL_FREQ);
148 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
149 count = rte_strsplit(buf, sizeof(buf), freqs,
150 RTE_MAX_LCORE_FREQS, ' ');
152 RTE_LOG(ERR, POWER, "No available frequency in "
153 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
156 if (count >= RTE_MAX_LCORE_FREQS) {
157 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
162 /* Store the available frequncies into power context */
163 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
164 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
166 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
167 POWER_CONVERT_TO_DECIMAL);
170 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
171 pi->turbo_available = 1;
172 pi->turbo_enable = 1;
173 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
176 pi->turbo_available = 0;
177 pi->turbo_enable = 0;
178 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
183 POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
184 count, pi->lcore_id);
193 * It is to fopen the sys file for the future setting the lcore frequency.
196 power_init_for_setting_freq(struct acpi_power_info *pi)
203 open_core_sysfs_file(&f, "rw+", POWER_SYSFILE_SETSPEED, pi->lcore_id);
205 RTE_LOG(ERR, POWER, "Failed to open %s\n",
206 POWER_SYSFILE_SETSPEED);
210 ret = read_core_sysfs_s(f, buf, sizeof(buf));
212 RTE_LOG(ERR, POWER, "Failed to read %s\n",
213 POWER_SYSFILE_SETSPEED);
217 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
218 for (i = 0; i < pi->nb_freqs; i++) {
219 if (freq == pi->freqs[i]) {
234 power_acpi_cpufreq_check_supported(void)
236 return cpufreq_check_scaling_driver(POWER_ACPI_DRIVER);
240 power_acpi_cpufreq_init(unsigned int lcore_id)
242 struct acpi_power_info *pi;
245 if (lcore_id >= RTE_MAX_LCORE) {
246 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
247 lcore_id, RTE_MAX_LCORE - 1U);
251 pi = &lcore_power_info[lcore_id];
252 exp_state = POWER_IDLE;
253 /* The power in use state works as a guard variable between
254 * the CPU frequency control initialization and exit process.
255 * The ACQUIRE memory ordering here pairs with the RELEASE
256 * ordering below as lock to make sure the frequency operations
257 * in the critical section are done under the correct state.
259 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
261 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
262 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
263 "in use\n", lcore_id);
267 pi->lcore_id = lcore_id;
268 /* Check and set the governor */
269 if (power_set_governor_userspace(pi) < 0) {
270 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
271 "userspace\n", lcore_id);
275 /* Get the available frequencies */
276 if (power_get_available_freqs(pi) < 0) {
277 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
278 "lcore %u\n", lcore_id);
282 /* Init for setting lcore frequency */
283 if (power_init_for_setting_freq(pi) < 0) {
284 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
285 "lcore %u\n", lcore_id);
289 /* Set freq to max by default */
290 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
291 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
292 "to max\n", lcore_id);
296 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
297 "power management\n", lcore_id);
298 exp_state = POWER_ONGOING;
299 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
300 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
305 exp_state = POWER_ONGOING;
306 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
307 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
313 power_acpi_cpufreq_exit(unsigned int lcore_id)
315 struct acpi_power_info *pi;
318 if (lcore_id >= RTE_MAX_LCORE) {
319 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
320 lcore_id, RTE_MAX_LCORE - 1U);
323 pi = &lcore_power_info[lcore_id];
324 exp_state = POWER_USED;
325 /* The power in use state works as a guard variable between
326 * the CPU frequency control initialization and exit process.
327 * The ACQUIRE memory ordering here pairs with the RELEASE
328 * ordering below as lock to make sure the frequency operations
329 * in the critical section are done under the correct state.
331 if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
333 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
334 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
335 "not used\n", lcore_id);
339 /* Close FD of setting freq */
343 /* Set the governor back to the original */
344 if (power_set_governor_original(pi) < 0) {
345 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
346 "to the original\n", lcore_id);
350 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
351 "'userspace' mode and been set back to the "
352 "original\n", lcore_id);
353 exp_state = POWER_ONGOING;
354 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
355 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
360 exp_state = POWER_ONGOING;
361 __atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
362 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
368 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
370 struct acpi_power_info *pi;
372 if (lcore_id >= RTE_MAX_LCORE) {
373 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
378 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
382 pi = &lcore_power_info[lcore_id];
383 if (num < pi->nb_freqs) {
384 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
387 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
393 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
395 if (lcore_id >= RTE_MAX_LCORE) {
396 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
397 return RTE_POWER_INVALID_FREQ_INDEX;
400 return lcore_power_info[lcore_id].curr_idx;
404 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
406 if (lcore_id >= RTE_MAX_LCORE) {
407 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
411 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
415 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
417 struct acpi_power_info *pi;
419 if (lcore_id >= RTE_MAX_LCORE) {
420 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
424 pi = &lcore_power_info[lcore_id];
425 if (pi->curr_idx + 1 == pi->nb_freqs)
428 /* Frequencies in the array are from high to low. */
429 return set_freq_internal(pi, pi->curr_idx + 1);
433 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
435 struct acpi_power_info *pi;
437 if (lcore_id >= RTE_MAX_LCORE) {
438 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
442 pi = &lcore_power_info[lcore_id];
443 if (pi->curr_idx == 0 ||
444 (pi->curr_idx == 1 && pi->turbo_available && !pi->turbo_enable))
447 /* Frequencies in the array are from high to low. */
448 return set_freq_internal(pi, pi->curr_idx - 1);
452 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
454 if (lcore_id >= RTE_MAX_LCORE) {
455 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
459 /* Frequencies in the array are from high to low. */
460 if (lcore_power_info[lcore_id].turbo_available) {
461 if (lcore_power_info[lcore_id].turbo_enable)
463 return set_freq_internal(
464 &lcore_power_info[lcore_id], 0);
466 /* Set to max non-turbo */
467 return set_freq_internal(
468 &lcore_power_info[lcore_id], 1);
470 return set_freq_internal(&lcore_power_info[lcore_id], 0);
474 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
476 struct acpi_power_info *pi;
478 if (lcore_id >= RTE_MAX_LCORE) {
479 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
483 pi = &lcore_power_info[lcore_id];
485 /* Frequencies in the array are from high to low. */
486 return set_freq_internal(pi, pi->nb_freqs - 1);
491 power_acpi_turbo_status(unsigned int lcore_id)
493 struct acpi_power_info *pi;
495 if (lcore_id >= RTE_MAX_LCORE) {
496 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
500 pi = &lcore_power_info[lcore_id];
502 return pi->turbo_enable;
507 power_acpi_enable_turbo(unsigned int lcore_id)
509 struct acpi_power_info *pi;
511 if (lcore_id >= RTE_MAX_LCORE) {
512 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
516 pi = &lcore_power_info[lcore_id];
518 if (pi->turbo_available)
519 pi->turbo_enable = 1;
521 pi->turbo_enable = 0;
523 "Failed to enable turbo on lcore %u\n",
528 /* Max may have changed, so call to max function */
529 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
531 "Failed to set frequency of lcore %u to max\n",
540 power_acpi_disable_turbo(unsigned int lcore_id)
542 struct acpi_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];
551 pi->turbo_enable = 0;
553 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
554 /* Try to set freq to max by default coming out of turbo */
555 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
557 "Failed to set frequency of lcore %u to max\n",
566 int power_acpi_get_capabilities(unsigned int lcore_id,
567 struct rte_power_core_capabilities *caps)
569 struct acpi_power_info *pi;
571 if (lcore_id >= RTE_MAX_LCORE) {
572 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
576 RTE_LOG(ERR, POWER, "Invalid argument\n");
580 pi = &lcore_power_info[lcore_id];
581 caps->capabilities = 0;
582 caps->turbo = !!(pi->turbo_available);