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"
98 * Power info per lcore.
100 struct rte_power_info {
101 unsigned lcore_id; /**< Logical core id */
102 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
103 uint32_t nb_freqs; /**< number of available freqs */
104 FILE *f; /**< FD of scaling_setspeed */
105 char governor_ori[32]; /**< Original governor name */
106 uint32_t curr_idx; /**< Freq index in freqs array */
107 volatile uint32_t state; /**< Power in use state */
108 } __rte_cache_aligned;
110 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
113 * It is to set specific freq for specific logical core, according to the index
114 * of supported frequencies.
117 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
119 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
120 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
121 "should be less than %u\n", idx, pi->nb_freqs);
125 /* Check if it is the same as current */
126 if (idx == pi->curr_idx)
129 POWER_DEBUG_TRACE("Freqency[%u] %u to be set for lcore %u\n",
130 idx, pi->freqs[idx], pi->lcore_id);
131 if (fseek(pi->f, 0, SEEK_SET) < 0) {
132 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
133 "for setting frequency for lcore %u\n", pi->lcore_id);
136 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
137 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
138 "lcore %u\n", pi->lcore_id);
148 * It is to check the current scaling governor by reading sys file, and then
149 * set it into 'userspace' if it is not by writing the sys file. The original
150 * governor will be saved for rolling back.
153 power_set_governor_userspace(struct rte_power_info *pi)
158 char fullpath[PATH_MAX];
162 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
164 f = fopen(fullpath, "rw+");
165 FOPEN_OR_ERR_RET(f, ret);
167 s = fgets(buf, sizeof(buf), f);
168 FOPS_OR_NULL_GOTO(s, out);
170 /* Check if current governor is userspace */
171 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
172 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
174 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
175 "already userspace\n", pi->lcore_id);
178 /* Save the original governor */
179 snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
181 /* Write 'userspace' to the governor */
182 val = fseek(f, 0, SEEK_SET);
183 FOPS_OR_ERR_GOTO(val, out);
185 val = fputs(POWER_GOVERNOR_USERSPACE, f);
186 FOPS_OR_ERR_GOTO(val, out);
189 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
190 "set to user space successfully\n", pi->lcore_id);
198 * It is to get the available frequencies of the specific lcore by reading the
202 power_get_available_freqs(struct rte_power_info *pi)
205 int ret = -1, i, count;
208 char fullpath[PATH_MAX];
209 char *freqs[RTE_MAX_LCORE_FREQS];
212 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
214 f = fopen(fullpath, "r");
215 FOPEN_OR_ERR_RET(f, ret);
217 s = fgets(buf, sizeof(buf), f);
218 FOPS_OR_NULL_GOTO(s, out);
220 /* Strip the line break if there is */
221 p = strchr(buf, '\n');
225 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
226 count = rte_strsplit(buf, sizeof(buf), freqs,
227 RTE_MAX_LCORE_FREQS, ' ');
229 RTE_LOG(ERR, POWER, "No available frequency in "
230 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
233 if (count >= RTE_MAX_LCORE_FREQS) {
234 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
239 /* Store the available frequncies into power context */
240 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
241 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
243 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
244 POWER_CONVERT_TO_DECIMAL);
248 POWER_DEBUG_TRACE("%d frequencie(s) of lcore %u are available\n",
249 count, pi->lcore_id);
257 * It is to fopen the sys file for the future setting the lcore frequency.
260 power_init_for_setting_freq(struct rte_power_info *pi)
263 char fullpath[PATH_MAX];
268 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
270 f = fopen(fullpath, "rw+");
271 FOPEN_OR_ERR_RET(f, -1);
273 s = fgets(buf, sizeof(buf), f);
274 FOPS_OR_NULL_GOTO(s, out);
276 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
277 for (i = 0; i < pi->nb_freqs; i++) {
278 if (freq == pi->freqs[i]) {
292 rte_power_acpi_cpufreq_init(unsigned lcore_id)
294 struct rte_power_info *pi;
296 if (lcore_id >= RTE_MAX_LCORE) {
297 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
298 lcore_id, RTE_MAX_LCORE - 1U);
302 pi = &lcore_power_info[lcore_id];
303 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
305 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
306 "in use\n", lcore_id);
310 pi->lcore_id = lcore_id;
311 /* Check and set the governor */
312 if (power_set_governor_userspace(pi) < 0) {
313 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
314 "userspace\n", lcore_id);
318 /* Get the available frequencies */
319 if (power_get_available_freqs(pi) < 0) {
320 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
321 "lcore %u\n", lcore_id);
325 /* Init for setting lcore frequency */
326 if (power_init_for_setting_freq(pi) < 0) {
327 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
328 "lcore %u\n", lcore_id);
332 /* Set freq to max by default */
333 if (rte_power_acpi_cpufreq_freq_max(lcore_id) < 0) {
334 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
335 "to max\n", lcore_id);
339 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
340 "power manamgement\n", lcore_id);
341 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
346 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
352 * It is to check the governor and then set the original governor back if
353 * needed by writing the the sys file.
356 power_set_governor_original(struct rte_power_info *pi)
361 char fullpath[PATH_MAX];
365 snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
367 f = fopen(fullpath, "rw+");
368 FOPEN_OR_ERR_RET(f, ret);
370 s = fgets(buf, sizeof(buf), f);
371 FOPS_OR_NULL_GOTO(s, out);
373 /* Check if the governor to be set is the same as current */
374 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
376 POWER_DEBUG_TRACE("Power management governor of lcore %u "
377 "has already been set to %s\n",
378 pi->lcore_id, pi->governor_ori);
382 /* Write back the original governor */
383 val = fseek(f, 0, SEEK_SET);
384 FOPS_OR_ERR_GOTO(val, out);
386 val = fputs(pi->governor_ori, f);
387 FOPS_OR_ERR_GOTO(val, out);
390 RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
391 "has been set back to %s successfully\n",
392 pi->lcore_id, pi->governor_ori);
400 rte_power_acpi_cpufreq_exit(unsigned lcore_id)
402 struct rte_power_info *pi;
404 if (lcore_id >= RTE_MAX_LCORE) {
405 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
406 lcore_id, RTE_MAX_LCORE - 1U);
409 pi = &lcore_power_info[lcore_id];
410 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
412 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
413 "not used\n", lcore_id);
417 /* Close FD of setting freq */
421 /* Set the governor back to the original */
422 if (power_set_governor_original(pi) < 0) {
423 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
424 "to the original\n", lcore_id);
428 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
429 "'userspace' mode and been set back to the "
430 "original\n", lcore_id);
431 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
436 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
442 rte_power_acpi_cpufreq_freqs(unsigned lcore_id, uint32_t *freqs, uint32_t num)
444 struct rte_power_info *pi;
446 if (lcore_id >= RTE_MAX_LCORE || !freqs) {
447 RTE_LOG(ERR, POWER, "Invalid input parameter\n");
451 pi = &lcore_power_info[lcore_id];
452 if (num < pi->nb_freqs) {
453 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
456 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
462 rte_power_acpi_cpufreq_get_freq(unsigned lcore_id)
464 if (lcore_id >= RTE_MAX_LCORE) {
465 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
466 return RTE_POWER_INVALID_FREQ_INDEX;
469 return lcore_power_info[lcore_id].curr_idx;
473 rte_power_acpi_cpufreq_set_freq(unsigned lcore_id, uint32_t index)
475 if (lcore_id >= RTE_MAX_LCORE) {
476 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
480 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
484 rte_power_acpi_cpufreq_freq_down(unsigned lcore_id)
486 struct rte_power_info *pi;
488 if (lcore_id >= RTE_MAX_LCORE) {
489 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
493 pi = &lcore_power_info[lcore_id];
494 if (pi->curr_idx + 1 == pi->nb_freqs)
497 /* Frequencies in the array are from high to low. */
498 return set_freq_internal(pi, pi->curr_idx + 1);
502 rte_power_acpi_cpufreq_freq_up(unsigned lcore_id)
504 struct rte_power_info *pi;
506 if (lcore_id >= RTE_MAX_LCORE) {
507 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
511 pi = &lcore_power_info[lcore_id];
512 if (pi->curr_idx == 0)
515 /* Frequencies in the array are from high to low. */
516 return set_freq_internal(pi, pi->curr_idx - 1);
520 rte_power_acpi_cpufreq_freq_max(unsigned lcore_id)
522 if (lcore_id >= RTE_MAX_LCORE) {
523 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
527 /* Frequencies in the array are from high to low. */
528 return set_freq_internal(&lcore_power_info[lcore_id], 0);
532 rte_power_acpi_cpufreq_freq_min(unsigned lcore_id)
534 struct rte_power_info *pi;
536 if (lcore_id >= RTE_MAX_LCORE) {
537 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
541 pi = &lcore_power_info[lcore_id];
543 /* Frequencies in the array are from high to low. */
544 return set_freq_internal(pi, pi->nb_freqs - 1);