4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
44 #include <rte_memcpy.h>
45 #include <rte_atomic.h>
47 #include "rte_power_acpi_cpufreq.h"
48 #include "rte_power_common.h"
50 #ifdef RTE_LIBRTE_POWER_DEBUG
51 #define POWER_DEBUG_TRACE(fmt, args...) do { \
52 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
55 #define POWER_DEBUG_TRACE(fmt, args...)
58 #define FOPEN_OR_ERR_RET(f, retval) do { \
60 RTE_LOG(ERR, POWER, "File not openned\n"); \
65 #define FOPS_OR_NULL_GOTO(ret, label) do { \
66 if ((ret) == NULL) { \
67 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
72 #define FOPS_OR_ERR_GOTO(ret, label) do { \
74 RTE_LOG(ERR, POWER, "File operations failed\n"); \
80 #define POWER_CONVERT_TO_DECIMAL 10
82 #define POWER_GOVERNOR_USERSPACE "userspace"
83 #define POWER_SYSFILE_GOVERNOR \
84 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
85 #define POWER_SYSFILE_AVAIL_FREQ \
86 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies"
87 #define POWER_SYSFILE_SETSPEED \
88 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
93 #define PLATFORM_INFO 0x0CE
94 #define TURBO_RATIO_LIMIT 0x1AD
95 #define IA32_PERF_CTL 0x199
96 #define CORE_TURBO_DISABLE_BIT ((uint64_t)1<<32)
106 * Power info per lcore.
108 struct rte_power_info {
109 unsigned lcore_id; /**< Logical core id */
110 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
111 uint32_t nb_freqs; /**< number of available freqs */
112 FILE *f; /**< FD of scaling_setspeed */
113 char governor_ori[32]; /**< Original governor name */
114 uint32_t curr_idx; /**< Freq index in freqs array */
115 volatile uint32_t state; /**< Power in use state */
116 uint16_t turbo_available; /**< Turbo Boost available */
117 uint16_t turbo_enable; /**< Turbo Boost enable/disable */
118 } __rte_cache_aligned;
120 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
123 * It is to set specific freq for specific logical core, according to the index
124 * of supported frequencies.
127 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
129 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
130 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
131 "should be less than %u\n", idx, pi->nb_freqs);
135 /* Check if it is the same as current */
136 if (idx == pi->curr_idx)
139 POWER_DEBUG_TRACE("Freqency[%u] %u to be set for lcore %u\n",
140 idx, pi->freqs[idx], pi->lcore_id);
141 if (fseek(pi->f, 0, SEEK_SET) < 0) {
142 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
143 "for setting frequency for lcore %u\n", pi->lcore_id);
146 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
147 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
148 "lcore %u\n", pi->lcore_id);
158 * It is to check the current scaling governor by reading sys file, and then
159 * set it into 'userspace' if it is not by writing the sys file. The original
160 * governor will be saved for rolling back.
163 power_set_governor_userspace(struct rte_power_info *pi)
168 char fullpath[PATH_MAX];
172 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
174 f = fopen(fullpath, "rw+");
175 FOPEN_OR_ERR_RET(f, ret);
177 s = fgets(buf, sizeof(buf), f);
178 FOPS_OR_NULL_GOTO(s, out);
180 /* Check if current governor is userspace */
181 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
182 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
184 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
185 "already userspace\n", pi->lcore_id);
188 /* Save the original governor */
189 snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
191 /* Write 'userspace' to the governor */
192 val = fseek(f, 0, SEEK_SET);
193 FOPS_OR_ERR_GOTO(val, out);
195 val = fputs(POWER_GOVERNOR_USERSPACE, f);
196 FOPS_OR_ERR_GOTO(val, out);
199 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
200 "set to user space successfully\n", pi->lcore_id);
208 * It is to get the available frequencies of the specific lcore by reading the
212 power_get_available_freqs(struct rte_power_info *pi)
215 int ret = -1, i, count;
218 char fullpath[PATH_MAX];
219 char *freqs[RTE_MAX_LCORE_FREQS];
222 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
224 f = fopen(fullpath, "r");
225 FOPEN_OR_ERR_RET(f, ret);
227 s = fgets(buf, sizeof(buf), f);
228 FOPS_OR_NULL_GOTO(s, out);
230 /* Strip the line break if there is */
231 p = strchr(buf, '\n');
235 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
236 count = rte_strsplit(buf, sizeof(buf), freqs,
237 RTE_MAX_LCORE_FREQS, ' ');
239 RTE_LOG(ERR, POWER, "No available frequency in "
240 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
243 if (count >= RTE_MAX_LCORE_FREQS) {
244 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
249 /* Store the available frequncies into power context */
250 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
251 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
253 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
254 POWER_CONVERT_TO_DECIMAL);
257 if ((pi->freqs[0]-1000) == pi->freqs[1]) {
258 pi->turbo_available = 1;
259 pi->turbo_enable = 1;
260 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
263 pi->turbo_available = 0;
264 pi->turbo_enable = 0;
265 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
270 POWER_DEBUG_TRACE("%d frequencie(s) of lcore %u are available\n",
271 count, pi->lcore_id);
279 * It is to fopen the sys file for the future setting the lcore frequency.
282 power_init_for_setting_freq(struct rte_power_info *pi)
285 char fullpath[PATH_MAX];
290 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
292 f = fopen(fullpath, "rw+");
293 FOPEN_OR_ERR_RET(f, -1);
295 s = fgets(buf, sizeof(buf), f);
296 FOPS_OR_NULL_GOTO(s, out);
298 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
299 for (i = 0; i < pi->nb_freqs; i++) {
300 if (freq == pi->freqs[i]) {
314 rte_power_acpi_cpufreq_init(unsigned lcore_id)
316 struct rte_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);
324 pi = &lcore_power_info[lcore_id];
325 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
327 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
328 "in use\n", lcore_id);
332 pi->lcore_id = lcore_id;
333 /* Check and set the governor */
334 if (power_set_governor_userspace(pi) < 0) {
335 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
336 "userspace\n", lcore_id);
340 /* Get the available frequencies */
341 if (power_get_available_freqs(pi) < 0) {
342 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
343 "lcore %u\n", lcore_id);
347 /* Init for setting lcore frequency */
348 if (power_init_for_setting_freq(pi) < 0) {
349 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
350 "lcore %u\n", lcore_id);
354 /* Set freq to max by default */
355 if (rte_power_acpi_cpufreq_freq_max(lcore_id) < 0) {
356 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
357 "to max\n", lcore_id);
361 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
362 "power manamgement\n", lcore_id);
363 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
368 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
374 * It is to check the governor and then set the original governor back if
375 * needed by writing the the sys file.
378 power_set_governor_original(struct rte_power_info *pi)
383 char fullpath[PATH_MAX];
387 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
389 f = fopen(fullpath, "rw+");
390 FOPEN_OR_ERR_RET(f, ret);
392 s = fgets(buf, sizeof(buf), f);
393 FOPS_OR_NULL_GOTO(s, out);
395 /* Check if the governor to be set is the same as current */
396 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
398 POWER_DEBUG_TRACE("Power management governor of lcore %u "
399 "has already been set to %s\n",
400 pi->lcore_id, pi->governor_ori);
404 /* Write back the original governor */
405 val = fseek(f, 0, SEEK_SET);
406 FOPS_OR_ERR_GOTO(val, out);
408 val = fputs(pi->governor_ori, f);
409 FOPS_OR_ERR_GOTO(val, out);
412 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
413 "has been set back to %s successfully\n",
414 pi->lcore_id, pi->governor_ori);
422 rte_power_acpi_cpufreq_exit(unsigned lcore_id)
424 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 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
434 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
435 "not used\n", lcore_id);
439 /* Close FD of setting freq */
443 /* Set the governor back to the original */
444 if (power_set_governor_original(pi) < 0) {
445 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
446 "to the original\n", lcore_id);
450 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
451 "'userspace' mode and been set back to the "
452 "original\n", lcore_id);
453 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
458 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
464 rte_power_acpi_cpufreq_freqs(unsigned lcore_id, uint32_t *freqs, uint32_t num)
466 struct rte_power_info *pi;
468 if (lcore_id >= RTE_MAX_LCORE || !freqs) {
469 RTE_LOG(ERR, POWER, "Invalid input parameter\n");
473 pi = &lcore_power_info[lcore_id];
474 if (num < pi->nb_freqs) {
475 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
478 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
484 rte_power_acpi_cpufreq_get_freq(unsigned lcore_id)
486 if (lcore_id >= RTE_MAX_LCORE) {
487 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
488 return RTE_POWER_INVALID_FREQ_INDEX;
491 return lcore_power_info[lcore_id].curr_idx;
495 rte_power_acpi_cpufreq_set_freq(unsigned lcore_id, uint32_t index)
497 if (lcore_id >= RTE_MAX_LCORE) {
498 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
502 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
506 rte_power_acpi_cpufreq_freq_down(unsigned lcore_id)
508 struct rte_power_info *pi;
510 if (lcore_id >= RTE_MAX_LCORE) {
511 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
515 pi = &lcore_power_info[lcore_id];
516 if (pi->curr_idx + 1 == pi->nb_freqs)
519 /* Frequencies in the array are from high to low. */
520 return set_freq_internal(pi, pi->curr_idx + 1);
524 rte_power_acpi_cpufreq_freq_up(unsigned lcore_id)
526 struct rte_power_info *pi;
528 if (lcore_id >= RTE_MAX_LCORE) {
529 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
533 pi = &lcore_power_info[lcore_id];
534 if (pi->curr_idx == 0)
537 /* Frequencies in the array are from high to low. */
538 return set_freq_internal(pi, pi->curr_idx - 1);
542 rte_power_acpi_cpufreq_freq_max(unsigned lcore_id)
544 if (lcore_id >= RTE_MAX_LCORE) {
545 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
549 /* Frequencies in the array are from high to low. */
550 if (lcore_power_info[lcore_id].turbo_available) {
551 if (lcore_power_info[lcore_id].turbo_enable)
553 return set_freq_internal(
554 &lcore_power_info[lcore_id], 0);
556 /* Set to max non-turbo */
557 return set_freq_internal(
558 &lcore_power_info[lcore_id], 1);
560 return set_freq_internal(&lcore_power_info[lcore_id], 0);
564 rte_power_acpi_cpufreq_freq_min(unsigned lcore_id)
566 struct rte_power_info *pi;
568 if (lcore_id >= RTE_MAX_LCORE) {
569 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
573 pi = &lcore_power_info[lcore_id];
575 /* Frequencies in the array are from high to low. */
576 return set_freq_internal(pi, pi->nb_freqs - 1);
581 rte_power_acpi_turbo_status(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 return pi->turbo_enable;
597 rte_power_acpi_enable_turbo(unsigned int lcore_id)
599 struct rte_power_info *pi;
601 if (lcore_id >= RTE_MAX_LCORE) {
602 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
606 pi = &lcore_power_info[lcore_id];
608 if (pi->turbo_available)
609 pi->turbo_enable = 1;
611 pi->turbo_enable = 0;
613 "Failed to enable turbo on lcore %u\n",
618 /* Max may have changed, so call to max function */
619 if (rte_power_acpi_cpufreq_freq_max(lcore_id) < 0) {
621 "Failed to set frequency of lcore %u to max\n",
630 rte_power_acpi_disable_turbo(unsigned int lcore_id)
632 struct rte_power_info *pi;
634 if (lcore_id >= RTE_MAX_LCORE) {
635 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
639 pi = &lcore_power_info[lcore_id];
641 pi->turbo_enable = 0;
643 if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
644 /* Try to set freq to max by default coming out of turbo */
645 if (rte_power_acpi_cpufreq_freq_max(lcore_id) < 0) {
647 "Failed to set frequency of lcore %u to max\n",