doc: add references to flow isolated mode in NICs guide
[dpdk.git] / lib / librte_power / power_pstate_cpufreq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <limits.h>
14 #include <errno.h>
15 #include <inttypes.h>
16
17 #include <rte_memcpy.h>
18 #include <rte_atomic.h>
19
20 #include "power_pstate_cpufreq.h"
21 #include "power_common.h"
22
23
24 #ifdef RTE_LIBRTE_POWER_DEBUG
25 #define POWER_DEBUG_TRACE(fmt, args...) do { \
26                 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
27 } while (0)
28 #else
29 #define POWER_DEBUG_TRACE(fmt, args...)
30 #endif
31
32 #define FOPEN_OR_ERR_RET(f, retval) do { \
33                 if ((f) == NULL) { \
34                         RTE_LOG(ERR, POWER, "File not openned\n"); \
35                         return retval; \
36                 } \
37 } while (0)
38
39 #define FOPS_OR_NULL_GOTO(ret, label) do { \
40                 if ((ret) == NULL) { \
41                         RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
42                         goto label; \
43                 } \
44 } while (0)
45
46 #define FOPS_OR_ERR_GOTO(ret, label) do { \
47                 if ((ret) < 0) { \
48                         RTE_LOG(ERR, POWER, "File operations failed\n"); \
49                         goto label; \
50                 } \
51 } while (0)
52
53
54 #define POWER_CONVERT_TO_DECIMAL 10
55 #define BUS_FREQ     100000
56
57 #define POWER_GOVERNOR_PERF "performance"
58 #define POWER_SYSFILE_GOVERNOR  \
59                 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
60 #define POWER_SYSFILE_MAX_FREQ \
61                 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
62 #define POWER_SYSFILE_MIN_FREQ  \
63                 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_min_freq"
64 #define POWER_SYSFILE_CUR_FREQ  \
65                 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
66 #define POWER_SYSFILE_BASE_MAX_FREQ \
67                 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq"
68 #define POWER_SYSFILE_BASE_MIN_FREQ  \
69                 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_min_freq"
70 #define POWER_MSR_PATH  "/dev/cpu/%u/msr"
71
72 /*
73  * MSR related
74  */
75 #define PLATFORM_INFO     0x0CE
76 #define NON_TURBO_MASK    0xFF00
77 #define NON_TURBO_OFFSET  0x8
78
79
80 enum power_state {
81         POWER_IDLE = 0,
82         POWER_ONGOING,
83         POWER_USED,
84         POWER_UNKNOWN
85 };
86
87 struct pstate_power_info {
88         unsigned int lcore_id;               /**< Logical core id */
89         uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
90         uint32_t nb_freqs;                   /**< number of available freqs */
91         FILE *f_cur_min;                     /**< FD of scaling_min */
92         FILE *f_cur_max;                     /**< FD of scaling_max */
93         char governor_ori[32];               /**< Original governor name */
94         uint32_t curr_idx;                   /**< Freq index in freqs array */
95         uint32_t non_turbo_max_ratio;        /**< Non Turbo Max ratio  */
96         uint32_t sys_max_freq;               /**< system wide max freq  */
97         volatile uint32_t state;             /**< Power in use state */
98         uint16_t turbo_available;            /**< Turbo Boost available */
99         uint16_t turbo_enable;               /**< Turbo Boost enable/disable */
100 } __rte_cache_aligned;
101
102
103 static struct pstate_power_info lcore_power_info[RTE_MAX_LCORE];
104
105 /**
106  * It is to read the specific MSR.
107  */
108
109 static int32_t
110 power_rdmsr(int msr, uint64_t *val, unsigned int lcore_id)
111 {
112         int fd, ret;
113         char fullpath[PATH_MAX];
114
115         snprintf(fullpath, sizeof(fullpath), POWER_MSR_PATH, lcore_id);
116
117         fd = open(fullpath, O_RDONLY);
118
119         if (fd < 0) {
120                 RTE_LOG(ERR, POWER, "Error opening '%s': %s\n", fullpath,
121                                  strerror(errno));
122                 return fd;
123         }
124
125         ret = pread(fd, val, sizeof(uint64_t), msr);
126
127         if (ret < 0) {
128                 RTE_LOG(ERR, POWER, "Error reading '%s': %s\n", fullpath,
129                                  strerror(errno));
130                 goto out;
131         }
132
133         POWER_DEBUG_TRACE("MSR Path %s, offset 0x%X for lcore %u\n",
134                         fullpath, msr, lcore_id);
135
136         POWER_DEBUG_TRACE("Ret value %d, content is 0x%"PRIx64"\n", ret, *val);
137
138 out:    close(fd);
139         return ret;
140 }
141
142 /**
143  * It is to fopen the sys file for the future setting the lcore frequency.
144  */
145 static int
146 power_init_for_setting_freq(struct pstate_power_info *pi)
147 {
148         FILE *f_min, *f_max;
149         char fullpath_min[PATH_MAX];
150         char fullpath_max[PATH_MAX];
151         uint64_t max_non_turbo = 0;
152
153         snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ,
154                         pi->lcore_id);
155
156         f_min = fopen(fullpath_min, "rw+");
157         FOPEN_OR_ERR_RET(f_min, -1);
158
159         snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ,
160                         pi->lcore_id);
161
162         f_max = fopen(fullpath_max, "rw+");
163         if (f_max == NULL)
164                 fclose(f_min);
165
166         FOPEN_OR_ERR_RET(f_max, -1);
167
168         pi->f_cur_min = f_min;
169         pi->f_cur_max = f_max;
170
171         /* Add MSR read to detect turbo status */
172
173         if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) < 0)
174                 return -1;
175
176         max_non_turbo = (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
177
178         POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n", max_non_turbo);
179
180         pi->non_turbo_max_ratio = max_non_turbo;
181
182         return 0;
183 }
184
185 static int
186 set_freq_internal(struct pstate_power_info *pi, uint32_t idx)
187 {
188         uint32_t target_freq = 0;
189
190         if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
191                 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
192                                 "should be less than %u\n", idx, pi->nb_freqs);
193                 return -1;
194         }
195
196         /* Check if it is the same as current */
197         if (idx == pi->curr_idx)
198                 return 0;
199
200         /* Because Intel Pstate Driver only allow user change min/max hint
201          * User need change the min/max as same value.
202          */
203         if (fseek(pi->f_cur_min, 0, SEEK_SET) < 0) {
204                 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
205                                 "for setting frequency for lcore %u\n",
206                                 pi->lcore_id);
207                 return -1;
208         }
209
210         if (fseek(pi->f_cur_max, 0, SEEK_SET) < 0) {
211                 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
212                                 "for setting frequency for lcore %u\n",
213                                 pi->lcore_id);
214                 return -1;
215         }
216
217         /* Turbo is available and enabled, first freq bucket is sys max freq */
218         if (pi->turbo_available && pi->turbo_enable && (idx == 0))
219                 target_freq = pi->sys_max_freq;
220         else
221                 target_freq = pi->freqs[idx];
222
223         /* Decrease freq, the min freq should be updated first */
224         if (idx  >  pi->curr_idx) {
225
226                 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
227                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
228                                         "lcore %u\n", pi->lcore_id);
229                         return -1;
230                 }
231
232                 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
233                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
234                                         "lcore %u\n", pi->lcore_id);
235                         return -1;
236                 }
237
238                 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
239                                   target_freq, pi->lcore_id);
240
241                 fflush(pi->f_cur_min);
242                 fflush(pi->f_cur_max);
243
244         }
245
246         /* Increase freq, the max freq should be updated first */
247         if (idx  <  pi->curr_idx) {
248
249                 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
250                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
251                                         "lcore %u\n", pi->lcore_id);
252                         return -1;
253                 }
254
255                 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
256                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
257                                         "lcore %u\n", pi->lcore_id);
258                         return -1;
259                 }
260
261                 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
262                                   target_freq, pi->lcore_id);
263
264                 fflush(pi->f_cur_max);
265                 fflush(pi->f_cur_min);
266         }
267
268         pi->curr_idx = idx;
269
270         return 1;
271 }
272
273 /**
274  * It is to check the current scaling governor by reading sys file, and then
275  * set it into 'performance' if it is not by writing the sys file. The original
276  * governor will be saved for rolling back.
277  */
278 static int
279 power_set_governor_performance(struct pstate_power_info *pi)
280 {
281         FILE *f;
282         int ret = -1;
283         char buf[BUFSIZ];
284         char fullpath[PATH_MAX];
285         char *s;
286         int val;
287
288         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
289                         pi->lcore_id);
290         f = fopen(fullpath, "rw+");
291         FOPEN_OR_ERR_RET(f, ret);
292
293         s = fgets(buf, sizeof(buf), f);
294         FOPS_OR_NULL_GOTO(s, out);
295
296         /* Check if current governor is performance */
297         if (strncmp(buf, POWER_GOVERNOR_PERF,
298                         sizeof(POWER_GOVERNOR_PERF)) == 0) {
299                 ret = 0;
300                 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
301                                 "already performance\n", pi->lcore_id);
302                 goto out;
303         }
304         /* Save the original governor */
305         snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
306
307         /* Write 'performance' to the governor */
308         val = fseek(f, 0, SEEK_SET);
309         FOPS_OR_ERR_GOTO(val, out);
310
311         val = fputs(POWER_GOVERNOR_PERF, f);
312         FOPS_OR_ERR_GOTO(val, out);
313
314         /* We need to flush to see if the fputs succeeds */
315         val = fflush(f);
316         FOPS_OR_ERR_GOTO(val, out);
317
318         ret = 0;
319         RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
320                         "set to performance successfully\n", pi->lcore_id);
321 out:
322         fclose(f);
323
324         return ret;
325 }
326
327 /**
328  * It is to check the governor and then set the original governor back if
329  * needed by writing the sys file.
330  */
331 static int
332 power_set_governor_original(struct pstate_power_info *pi)
333 {
334         FILE *f;
335         int ret = -1;
336         char buf[BUFSIZ];
337         char fullpath[PATH_MAX];
338         char *s;
339         int val;
340
341         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
342                         pi->lcore_id);
343         f = fopen(fullpath, "rw+");
344         FOPEN_OR_ERR_RET(f, ret);
345
346         s = fgets(buf, sizeof(buf), f);
347         FOPS_OR_NULL_GOTO(s, out);
348
349         /* Check if the governor to be set is the same as current */
350         if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
351                 ret = 0;
352                 POWER_DEBUG_TRACE("Power management governor of lcore %u "
353                                 "has already been set to %s\n",
354                                 pi->lcore_id, pi->governor_ori);
355                 goto out;
356         }
357
358         /* Write back the original governor */
359         val = fseek(f, 0, SEEK_SET);
360         FOPS_OR_ERR_GOTO(val, out);
361
362         val = fputs(pi->governor_ori, f);
363         FOPS_OR_ERR_GOTO(val, out);
364
365         ret = 0;
366         RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
367                         "has been set back to %s successfully\n",
368                         pi->lcore_id, pi->governor_ori);
369 out:
370         fclose(f);
371
372         return ret;
373 }
374
375 /**
376  * It is to get the available frequencies of the specific lcore by reading the
377  * sys file.
378  */
379 static int
380 power_get_available_freqs(struct pstate_power_info *pi)
381 {
382         FILE *f_min, *f_max;
383         int ret = -1;
384         char *p_min, *p_max;
385         char buf_min[BUFSIZ];
386         char buf_max[BUFSIZ];
387         char fullpath_min[PATH_MAX];
388         char fullpath_max[PATH_MAX];
389         char *s_min, *s_max;
390         uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
391         uint32_t i, num_freqs = 0;
392
393         snprintf(fullpath_max, sizeof(fullpath_max),
394                         POWER_SYSFILE_BASE_MAX_FREQ,
395                         pi->lcore_id);
396         snprintf(fullpath_min, sizeof(fullpath_min),
397                         POWER_SYSFILE_BASE_MIN_FREQ,
398                         pi->lcore_id);
399
400         f_min = fopen(fullpath_min, "r");
401         FOPEN_OR_ERR_RET(f_min, ret);
402
403         f_max = fopen(fullpath_max, "r");
404         if (f_max == NULL)
405                 fclose(f_min);
406
407         FOPEN_OR_ERR_RET(f_max, ret);
408
409         s_min = fgets(buf_min, sizeof(buf_min), f_min);
410         FOPS_OR_NULL_GOTO(s_min, out);
411
412         s_max = fgets(buf_max, sizeof(buf_max), f_max);
413         FOPS_OR_NULL_GOTO(s_max, out);
414
415
416         /* Strip the line break if there is */
417         p_min = strchr(buf_min, '\n');
418         if (p_min != NULL)
419                 *p_min = 0;
420
421         p_max = strchr(buf_max, '\n');
422         if (p_max != NULL)
423                 *p_max = 0;
424
425         sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
426         sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
427
428         if (sys_max_freq < sys_min_freq)
429                 goto out;
430
431         pi->sys_max_freq = sys_max_freq;
432
433         base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
434
435         POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
436                         sys_min_freq,
437                         sys_max_freq,
438                         base_max_freq);
439
440         if (base_max_freq < sys_max_freq)
441                 pi->turbo_available = 1;
442         else
443                 pi->turbo_available = 0;
444
445         /* If turbo is available then there is one extra freq bucket
446          * to store the sys max freq which value is base_max +1
447          */
448         num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
449                 pi->turbo_available;
450
451         /* Generate the freq bucket array.
452          * If turbo is available the freq bucket[0] value is base_max +1
453          * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
454          * and so on.
455          * If turbo is not available bucket[0] is base_max and so on
456          */
457         for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
458                 if ((i == 0) && pi->turbo_available)
459                         pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
460                 else
461                         pi->freqs[pi->nb_freqs++] =
462                         base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
463         }
464
465         ret = 0;
466
467         POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
468                         num_freqs, pi->lcore_id);
469
470 out:
471         fclose(f_min);
472         fclose(f_max);
473
474         return ret;
475 }
476
477 int
478 power_pstate_cpufreq_init(unsigned int lcore_id)
479 {
480         struct pstate_power_info *pi;
481
482         if (lcore_id >= RTE_MAX_LCORE) {
483                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
484                                 lcore_id, RTE_MAX_LCORE - 1U);
485                 return -1;
486         }
487
488         pi = &lcore_power_info[lcore_id];
489         if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
490                         == 0) {
491                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
492                                 "in use\n", lcore_id);
493                 return -1;
494         }
495
496         pi->lcore_id = lcore_id;
497         /* Check and set the governor */
498         if (power_set_governor_performance(pi) < 0) {
499                 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
500                                 "performance\n", lcore_id);
501                 goto fail;
502         }
503         /* Init for setting lcore frequency */
504         if (power_init_for_setting_freq(pi) < 0) {
505                 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
506                                 "lcore %u\n", lcore_id);
507                 goto fail;
508         }
509
510         /* Get the available frequencies */
511         if (power_get_available_freqs(pi) < 0) {
512                 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
513                                 "lcore %u\n", lcore_id);
514                 goto fail;
515         }
516
517
518         /* Set freq to max by default */
519         if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
520                 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
521                                 "to max\n", lcore_id);
522                 goto fail;
523         }
524
525         RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
526                         "power management\n", lcore_id);
527         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
528
529         return 0;
530
531 fail:
532         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
533
534         return -1;
535 }
536
537 int
538 power_pstate_cpufreq_exit(unsigned int lcore_id)
539 {
540         struct pstate_power_info *pi;
541
542         if (lcore_id >= RTE_MAX_LCORE) {
543                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
544                                 lcore_id, RTE_MAX_LCORE - 1U);
545                 return -1;
546         }
547         pi = &lcore_power_info[lcore_id];
548
549         if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
550                         == 0) {
551                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
552                                 "not used\n", lcore_id);
553                 return -1;
554         }
555
556         /* Close FD of setting freq */
557         fclose(pi->f_cur_min);
558         fclose(pi->f_cur_max);
559         pi->f_cur_min = NULL;
560         pi->f_cur_max = NULL;
561
562         /* Set the governor back to the original */
563         if (power_set_governor_original(pi) < 0) {
564                 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
565                                 "to the original\n", lcore_id);
566                 goto fail;
567         }
568
569         RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
570                         "'performance' mode and been set back to the "
571                         "original\n", lcore_id);
572         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
573
574         return 0;
575
576 fail:
577         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
578
579         return -1;
580 }
581
582
583 uint32_t
584 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
585 {
586         struct pstate_power_info *pi;
587
588         if (lcore_id >= RTE_MAX_LCORE) {
589                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
590                 return 0;
591         }
592
593         if (freqs == NULL) {
594                 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
595                 return 0;
596         }
597
598         pi = &lcore_power_info[lcore_id];
599         if (num < pi->nb_freqs) {
600                 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
601                 return 0;
602         }
603         rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
604
605         return pi->nb_freqs;
606 }
607
608 uint32_t
609 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
610 {
611         if (lcore_id >= RTE_MAX_LCORE) {
612                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
613                 return RTE_POWER_INVALID_FREQ_INDEX;
614         }
615
616         return lcore_power_info[lcore_id].curr_idx;
617 }
618
619
620 int
621 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
622 {
623         if (lcore_id >= RTE_MAX_LCORE) {
624                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
625                 return -1;
626         }
627
628         return set_freq_internal(&(lcore_power_info[lcore_id]), index);
629 }
630
631 int
632 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
633 {
634         struct pstate_power_info *pi;
635
636         if (lcore_id >= RTE_MAX_LCORE) {
637                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
638                 return -1;
639         }
640
641         pi = &lcore_power_info[lcore_id];
642         if (pi->curr_idx == 0)
643                 return 0;
644
645         /* Frequencies in the array are from high to low. */
646         return set_freq_internal(pi, pi->curr_idx - 1);
647 }
648
649 int
650 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
651 {
652         struct pstate_power_info *pi;
653
654         if (lcore_id >= RTE_MAX_LCORE) {
655                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
656                 return -1;
657         }
658
659         pi = &lcore_power_info[lcore_id];
660         if (pi->curr_idx + 1 == pi->nb_freqs)
661                 return 0;
662
663         /* Frequencies in the array are from high to low. */
664         return set_freq_internal(pi, pi->curr_idx + 1);
665 }
666
667 int
668 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
669 {
670         if (lcore_id >= RTE_MAX_LCORE) {
671                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
672                 return -1;
673         }
674
675         /* Frequencies in the array are from high to low. */
676         if (lcore_power_info[lcore_id].turbo_available) {
677                 if (lcore_power_info[lcore_id].turbo_enable)
678                         /* Set to Turbo */
679                         return set_freq_internal(
680                                         &lcore_power_info[lcore_id], 0);
681                 else
682                         /* Set to max non-turbo */
683                         return set_freq_internal(
684                                         &lcore_power_info[lcore_id], 1);
685         } else
686                 return set_freq_internal(&lcore_power_info[lcore_id], 0);
687 }
688
689
690 int
691 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
692 {
693         struct pstate_power_info *pi;
694
695         if (lcore_id >= RTE_MAX_LCORE) {
696                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
697                 return -1;
698         }
699
700         pi = &lcore_power_info[lcore_id];
701
702         /* Frequencies in the array are from high to low. */
703         return set_freq_internal(pi, pi->nb_freqs - 1);
704 }
705
706
707 int
708 power_pstate_turbo_status(unsigned int lcore_id)
709 {
710         struct pstate_power_info *pi;
711
712         if (lcore_id >= RTE_MAX_LCORE) {
713                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
714                 return -1;
715         }
716
717         pi = &lcore_power_info[lcore_id];
718
719         return pi->turbo_enable;
720 }
721
722 int
723 power_pstate_enable_turbo(unsigned int lcore_id)
724 {
725         struct pstate_power_info *pi;
726
727         if (lcore_id >= RTE_MAX_LCORE) {
728                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
729                 return -1;
730         }
731
732         pi = &lcore_power_info[lcore_id];
733
734         if (pi->turbo_available)
735                 pi->turbo_enable = 1;
736         else {
737                 pi->turbo_enable = 0;
738                 RTE_LOG(ERR, POWER,
739                         "Failed to enable turbo on lcore %u\n",
740                         lcore_id);
741                         return -1;
742         }
743
744         return 0;
745 }
746
747
748 int
749 power_pstate_disable_turbo(unsigned int lcore_id)
750 {
751         struct pstate_power_info *pi;
752
753         if (lcore_id >= RTE_MAX_LCORE) {
754                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
755                 return -1;
756         }
757
758         pi = &lcore_power_info[lcore_id];
759
760         pi->turbo_enable = 0;
761
762
763         return 0;
764 }
765
766
767 int power_pstate_get_capabilities(unsigned int lcore_id,
768                 struct rte_power_core_capabilities *caps)
769 {
770         struct pstate_power_info *pi;
771
772         if (lcore_id >= RTE_MAX_LCORE) {
773                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
774                 return -1;
775         }
776         if (caps == NULL) {
777                 RTE_LOG(ERR, POWER, "Invalid argument\n");
778                 return -1;
779         }
780
781         pi = &lcore_power_info[lcore_id];
782         caps->capabilities = 0;
783         caps->turbo = !!(pi->turbo_available);
784
785         return 0;
786 }