3e04e804072a4f3bc12e52a9a066ecda879ce0d8
[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         /* Strip off terminating '\n' */
296         strtok(buf, "\n");
297
298         /* Check if current governor is performance */
299         if (strncmp(buf, POWER_GOVERNOR_PERF,
300                         sizeof(POWER_GOVERNOR_PERF)) == 0) {
301                 ret = 0;
302                 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
303                                 "already performance\n", pi->lcore_id);
304                 goto out;
305         }
306         /* Save the original governor */
307         snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
308
309         /* Write 'performance' to the governor */
310         val = fseek(f, 0, SEEK_SET);
311         FOPS_OR_ERR_GOTO(val, out);
312
313         val = fputs(POWER_GOVERNOR_PERF, f);
314         FOPS_OR_ERR_GOTO(val, out);
315
316         /* We need to flush to see if the fputs succeeds */
317         val = fflush(f);
318         FOPS_OR_ERR_GOTO(val, out);
319
320         ret = 0;
321         RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
322                         "set to performance successfully\n", pi->lcore_id);
323 out:
324         fclose(f);
325
326         return ret;
327 }
328
329 /**
330  * It is to check the governor and then set the original governor back if
331  * needed by writing the sys file.
332  */
333 static int
334 power_set_governor_original(struct pstate_power_info *pi)
335 {
336         FILE *f;
337         int ret = -1;
338         char buf[BUFSIZ];
339         char fullpath[PATH_MAX];
340         char *s;
341         int val;
342
343         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
344                         pi->lcore_id);
345         f = fopen(fullpath, "rw+");
346         FOPEN_OR_ERR_RET(f, ret);
347
348         s = fgets(buf, sizeof(buf), f);
349         FOPS_OR_NULL_GOTO(s, out);
350
351         /* Check if the governor to be set is the same as current */
352         if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
353                 ret = 0;
354                 POWER_DEBUG_TRACE("Power management governor of lcore %u "
355                                 "has already been set to %s\n",
356                                 pi->lcore_id, pi->governor_ori);
357                 goto out;
358         }
359
360         /* Write back the original governor */
361         val = fseek(f, 0, SEEK_SET);
362         FOPS_OR_ERR_GOTO(val, out);
363
364         val = fputs(pi->governor_ori, f);
365         FOPS_OR_ERR_GOTO(val, out);
366
367         ret = 0;
368         RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
369                         "has been set back to %s successfully\n",
370                         pi->lcore_id, pi->governor_ori);
371 out:
372         fclose(f);
373
374         return ret;
375 }
376
377 /**
378  * It is to get the available frequencies of the specific lcore by reading the
379  * sys file.
380  */
381 static int
382 power_get_available_freqs(struct pstate_power_info *pi)
383 {
384         FILE *f_min, *f_max;
385         int ret = -1;
386         char *p_min, *p_max;
387         char buf_min[BUFSIZ];
388         char buf_max[BUFSIZ];
389         char fullpath_min[PATH_MAX];
390         char fullpath_max[PATH_MAX];
391         char *s_min, *s_max;
392         uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
393         uint32_t i, num_freqs = 0;
394
395         snprintf(fullpath_max, sizeof(fullpath_max),
396                         POWER_SYSFILE_BASE_MAX_FREQ,
397                         pi->lcore_id);
398         snprintf(fullpath_min, sizeof(fullpath_min),
399                         POWER_SYSFILE_BASE_MIN_FREQ,
400                         pi->lcore_id);
401
402         f_min = fopen(fullpath_min, "r");
403         FOPEN_OR_ERR_RET(f_min, ret);
404
405         f_max = fopen(fullpath_max, "r");
406         if (f_max == NULL)
407                 fclose(f_min);
408
409         FOPEN_OR_ERR_RET(f_max, ret);
410
411         s_min = fgets(buf_min, sizeof(buf_min), f_min);
412         FOPS_OR_NULL_GOTO(s_min, out);
413
414         s_max = fgets(buf_max, sizeof(buf_max), f_max);
415         FOPS_OR_NULL_GOTO(s_max, out);
416
417
418         /* Strip the line break if there is */
419         p_min = strchr(buf_min, '\n');
420         if (p_min != NULL)
421                 *p_min = 0;
422
423         p_max = strchr(buf_max, '\n');
424         if (p_max != NULL)
425                 *p_max = 0;
426
427         sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
428         sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
429
430         if (sys_max_freq < sys_min_freq)
431                 goto out;
432
433         pi->sys_max_freq = sys_max_freq;
434
435         base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
436
437         POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
438                         sys_min_freq,
439                         sys_max_freq,
440                         base_max_freq);
441
442         if (base_max_freq < sys_max_freq)
443                 pi->turbo_available = 1;
444         else
445                 pi->turbo_available = 0;
446
447         /* If turbo is available then there is one extra freq bucket
448          * to store the sys max freq which value is base_max +1
449          */
450         num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
451                 pi->turbo_available;
452
453         /* Generate the freq bucket array.
454          * If turbo is available the freq bucket[0] value is base_max +1
455          * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
456          * and so on.
457          * If turbo is not available bucket[0] is base_max and so on
458          */
459         for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
460                 if ((i == 0) && pi->turbo_available)
461                         pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
462                 else
463                         pi->freqs[pi->nb_freqs++] =
464                         base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
465         }
466
467         ret = 0;
468
469         POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
470                         num_freqs, pi->lcore_id);
471
472 out:
473         fclose(f_min);
474         fclose(f_max);
475
476         return ret;
477 }
478
479 int
480 power_pstate_cpufreq_init(unsigned int lcore_id)
481 {
482         struct pstate_power_info *pi;
483
484         if (lcore_id >= RTE_MAX_LCORE) {
485                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
486                                 lcore_id, RTE_MAX_LCORE - 1U);
487                 return -1;
488         }
489
490         pi = &lcore_power_info[lcore_id];
491         if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
492                         == 0) {
493                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
494                                 "in use\n", lcore_id);
495                 return -1;
496         }
497
498         pi->lcore_id = lcore_id;
499         /* Check and set the governor */
500         if (power_set_governor_performance(pi) < 0) {
501                 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
502                                 "performance\n", lcore_id);
503                 goto fail;
504         }
505         /* Init for setting lcore frequency */
506         if (power_init_for_setting_freq(pi) < 0) {
507                 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
508                                 "lcore %u\n", lcore_id);
509                 goto fail;
510         }
511
512         /* Get the available frequencies */
513         if (power_get_available_freqs(pi) < 0) {
514                 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
515                                 "lcore %u\n", lcore_id);
516                 goto fail;
517         }
518
519
520         /* Set freq to max by default */
521         if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
522                 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
523                                 "to max\n", lcore_id);
524                 goto fail;
525         }
526
527         RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
528                         "power management\n", lcore_id);
529         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
530
531         return 0;
532
533 fail:
534         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
535
536         return -1;
537 }
538
539 int
540 power_pstate_cpufreq_exit(unsigned int lcore_id)
541 {
542         struct pstate_power_info *pi;
543
544         if (lcore_id >= RTE_MAX_LCORE) {
545                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
546                                 lcore_id, RTE_MAX_LCORE - 1U);
547                 return -1;
548         }
549         pi = &lcore_power_info[lcore_id];
550
551         if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
552                         == 0) {
553                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
554                                 "not used\n", lcore_id);
555                 return -1;
556         }
557
558         /* Close FD of setting freq */
559         fclose(pi->f_cur_min);
560         fclose(pi->f_cur_max);
561         pi->f_cur_min = NULL;
562         pi->f_cur_max = NULL;
563
564         /* Set the governor back to the original */
565         if (power_set_governor_original(pi) < 0) {
566                 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
567                                 "to the original\n", lcore_id);
568                 goto fail;
569         }
570
571         RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
572                         "'performance' mode and been set back to the "
573                         "original\n", lcore_id);
574         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
575
576         return 0;
577
578 fail:
579         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
580
581         return -1;
582 }
583
584
585 uint32_t
586 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
587 {
588         struct pstate_power_info *pi;
589
590         if (lcore_id >= RTE_MAX_LCORE) {
591                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
592                 return 0;
593         }
594
595         if (freqs == NULL) {
596                 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
597                 return 0;
598         }
599
600         pi = &lcore_power_info[lcore_id];
601         if (num < pi->nb_freqs) {
602                 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
603                 return 0;
604         }
605         rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
606
607         return pi->nb_freqs;
608 }
609
610 uint32_t
611 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
612 {
613         if (lcore_id >= RTE_MAX_LCORE) {
614                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
615                 return RTE_POWER_INVALID_FREQ_INDEX;
616         }
617
618         return lcore_power_info[lcore_id].curr_idx;
619 }
620
621
622 int
623 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
624 {
625         if (lcore_id >= RTE_MAX_LCORE) {
626                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
627                 return -1;
628         }
629
630         return set_freq_internal(&(lcore_power_info[lcore_id]), index);
631 }
632
633 int
634 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
635 {
636         struct pstate_power_info *pi;
637
638         if (lcore_id >= RTE_MAX_LCORE) {
639                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
640                 return -1;
641         }
642
643         pi = &lcore_power_info[lcore_id];
644         if (pi->curr_idx == 0)
645                 return 0;
646
647         /* Frequencies in the array are from high to low. */
648         return set_freq_internal(pi, pi->curr_idx - 1);
649 }
650
651 int
652 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
653 {
654         struct pstate_power_info *pi;
655
656         if (lcore_id >= RTE_MAX_LCORE) {
657                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
658                 return -1;
659         }
660
661         pi = &lcore_power_info[lcore_id];
662         if (pi->curr_idx + 1 == pi->nb_freqs)
663                 return 0;
664
665         /* Frequencies in the array are from high to low. */
666         return set_freq_internal(pi, pi->curr_idx + 1);
667 }
668
669 int
670 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
671 {
672         if (lcore_id >= RTE_MAX_LCORE) {
673                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
674                 return -1;
675         }
676
677         /* Frequencies in the array are from high to low. */
678         if (lcore_power_info[lcore_id].turbo_available) {
679                 if (lcore_power_info[lcore_id].turbo_enable)
680                         /* Set to Turbo */
681                         return set_freq_internal(
682                                         &lcore_power_info[lcore_id], 0);
683                 else
684                         /* Set to max non-turbo */
685                         return set_freq_internal(
686                                         &lcore_power_info[lcore_id], 1);
687         } else
688                 return set_freq_internal(&lcore_power_info[lcore_id], 0);
689 }
690
691
692 int
693 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
694 {
695         struct pstate_power_info *pi;
696
697         if (lcore_id >= RTE_MAX_LCORE) {
698                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
699                 return -1;
700         }
701
702         pi = &lcore_power_info[lcore_id];
703
704         /* Frequencies in the array are from high to low. */
705         return set_freq_internal(pi, pi->nb_freqs - 1);
706 }
707
708
709 int
710 power_pstate_turbo_status(unsigned int lcore_id)
711 {
712         struct pstate_power_info *pi;
713
714         if (lcore_id >= RTE_MAX_LCORE) {
715                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
716                 return -1;
717         }
718
719         pi = &lcore_power_info[lcore_id];
720
721         return pi->turbo_enable;
722 }
723
724 int
725 power_pstate_enable_turbo(unsigned int lcore_id)
726 {
727         struct pstate_power_info *pi;
728
729         if (lcore_id >= RTE_MAX_LCORE) {
730                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
731                 return -1;
732         }
733
734         pi = &lcore_power_info[lcore_id];
735
736         if (pi->turbo_available)
737                 pi->turbo_enable = 1;
738         else {
739                 pi->turbo_enable = 0;
740                 RTE_LOG(ERR, POWER,
741                         "Failed to enable turbo on lcore %u\n",
742                         lcore_id);
743                         return -1;
744         }
745
746         return 0;
747 }
748
749
750 int
751 power_pstate_disable_turbo(unsigned int lcore_id)
752 {
753         struct pstate_power_info *pi;
754
755         if (lcore_id >= RTE_MAX_LCORE) {
756                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
757                 return -1;
758         }
759
760         pi = &lcore_power_info[lcore_id];
761
762         pi->turbo_enable = 0;
763
764
765         return 0;
766 }
767
768
769 int power_pstate_get_capabilities(unsigned int lcore_id,
770                 struct rte_power_core_capabilities *caps)
771 {
772         struct pstate_power_info *pi;
773
774         if (lcore_id >= RTE_MAX_LCORE) {
775                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
776                 return -1;
777         }
778         if (caps == NULL) {
779                 RTE_LOG(ERR, POWER, "Invalid argument\n");
780                 return -1;
781         }
782
783         pi = &lcore_power_info[lcore_id];
784         caps->capabilities = 0;
785         caps->turbo = !!(pi->turbo_available);
786
787         return 0;
788 }