power: fix frequency list to handle null buffer
[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         FOPEN_OR_ERR_RET(f_max, -1);
164
165         pi->f_cur_min = f_min;
166         pi->f_cur_max = f_max;
167
168         /* Add MSR read to detect turbo status */
169
170         if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) < 0)
171                 return -1;
172
173         max_non_turbo = (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
174
175         POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n", max_non_turbo);
176
177         pi->non_turbo_max_ratio = max_non_turbo;
178
179         return 0;
180 }
181
182 static int
183 set_freq_internal(struct pstate_power_info *pi, uint32_t idx)
184 {
185         uint32_t target_freq = 0;
186
187         if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
188                 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
189                                 "should be less than %u\n", idx, pi->nb_freqs);
190                 return -1;
191         }
192
193         /* Check if it is the same as current */
194         if (idx == pi->curr_idx)
195                 return 0;
196
197         /* Because Intel Pstate Driver only allow user change min/max hint
198          * User need change the min/max as same value.
199          */
200         if (fseek(pi->f_cur_min, 0, SEEK_SET) < 0) {
201                 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
202                                 "for setting frequency for lcore %u\n",
203                                 pi->lcore_id);
204                 return -1;
205         }
206
207         if (fseek(pi->f_cur_max, 0, SEEK_SET) < 0) {
208                 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
209                                 "for setting frequency for lcore %u\n",
210                                 pi->lcore_id);
211                 return -1;
212         }
213
214         /* Turbo is available and enabled, first freq bucket is sys max freq */
215         if (pi->turbo_available && pi->turbo_enable && (idx == 0))
216                 target_freq = pi->sys_max_freq;
217         else
218                 target_freq = pi->freqs[idx];
219
220         /* Decrease freq, the min freq should be updated first */
221         if (idx  >  pi->curr_idx) {
222
223                 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
224                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
225                                         "lcore %u\n", pi->lcore_id);
226                         return -1;
227                 }
228
229                 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
230                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
231                                         "lcore %u\n", pi->lcore_id);
232                         return -1;
233                 }
234
235                 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
236                                   target_freq, pi->lcore_id);
237
238                 fflush(pi->f_cur_min);
239                 fflush(pi->f_cur_max);
240
241         }
242
243         /* Increase freq, the max freq should be updated first */
244         if (idx  <  pi->curr_idx) {
245
246                 if (fprintf(pi->f_cur_max, "%u", target_freq) < 0) {
247                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
248                                         "lcore %u\n", pi->lcore_id);
249                         return -1;
250                 }
251
252                 if (fprintf(pi->f_cur_min, "%u", target_freq) < 0) {
253                         RTE_LOG(ERR, POWER, "Fail to write new frequency for "
254                                         "lcore %u\n", pi->lcore_id);
255                         return -1;
256                 }
257
258                 POWER_DEBUG_TRACE("Freqency '%u' to be set for lcore %u\n",
259                                   target_freq, pi->lcore_id);
260
261                 fflush(pi->f_cur_max);
262                 fflush(pi->f_cur_min);
263         }
264
265         pi->curr_idx = idx;
266
267         return 1;
268 }
269
270 /**
271  * It is to check the current scaling governor by reading sys file, and then
272  * set it into 'performance' if it is not by writing the sys file. The original
273  * governor will be saved for rolling back.
274  */
275 static int
276 power_set_governor_performance(struct pstate_power_info *pi)
277 {
278         FILE *f;
279         int ret = -1;
280         char buf[BUFSIZ];
281         char fullpath[PATH_MAX];
282         char *s;
283         int val;
284
285         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
286                         pi->lcore_id);
287         f = fopen(fullpath, "rw+");
288         FOPEN_OR_ERR_RET(f, ret);
289
290         s = fgets(buf, sizeof(buf), f);
291         FOPS_OR_NULL_GOTO(s, out);
292
293         /* Check if current governor is performance */
294         if (strncmp(buf, POWER_GOVERNOR_PERF,
295                         sizeof(POWER_GOVERNOR_PERF)) == 0) {
296                 ret = 0;
297                 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
298                                 "already performance\n", pi->lcore_id);
299                 goto out;
300         }
301         /* Save the original governor */
302         snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
303
304         /* Write 'performance' to the governor */
305         val = fseek(f, 0, SEEK_SET);
306         FOPS_OR_ERR_GOTO(val, out);
307
308         val = fputs(POWER_GOVERNOR_PERF, f);
309         FOPS_OR_ERR_GOTO(val, out);
310
311         /* We need to flush to see if the fputs succeeds */
312         val = fflush(f);
313         FOPS_OR_ERR_GOTO(val, out);
314
315         ret = 0;
316         RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
317                         "set to performance successfully\n", pi->lcore_id);
318 out:
319         fclose(f);
320
321         return ret;
322 }
323
324 /**
325  * It is to check the governor and then set the original governor back if
326  * needed by writing the sys file.
327  */
328 static int
329 power_set_governor_original(struct pstate_power_info *pi)
330 {
331         FILE *f;
332         int ret = -1;
333         char buf[BUFSIZ];
334         char fullpath[PATH_MAX];
335         char *s;
336         int val;
337
338         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
339                         pi->lcore_id);
340         f = fopen(fullpath, "rw+");
341         FOPEN_OR_ERR_RET(f, ret);
342
343         s = fgets(buf, sizeof(buf), f);
344         FOPS_OR_NULL_GOTO(s, out);
345
346         /* Check if the governor to be set is the same as current */
347         if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
348                 ret = 0;
349                 POWER_DEBUG_TRACE("Power management governor of lcore %u "
350                                 "has already been set to %s\n",
351                                 pi->lcore_id, pi->governor_ori);
352                 goto out;
353         }
354
355         /* Write back the original governor */
356         val = fseek(f, 0, SEEK_SET);
357         FOPS_OR_ERR_GOTO(val, out);
358
359         val = fputs(pi->governor_ori, f);
360         FOPS_OR_ERR_GOTO(val, out);
361
362         ret = 0;
363         RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
364                         "has been set back to %s successfully\n",
365                         pi->lcore_id, pi->governor_ori);
366 out:
367         fclose(f);
368
369         return ret;
370 }
371
372 /**
373  * It is to get the available frequencies of the specific lcore by reading the
374  * sys file.
375  */
376 static int
377 power_get_available_freqs(struct pstate_power_info *pi)
378 {
379         FILE *f_min, *f_max;
380         int ret = -1;
381         char *p_min, *p_max;
382         char buf_min[BUFSIZ];
383         char buf_max[BUFSIZ];
384         char fullpath_min[PATH_MAX];
385         char fullpath_max[PATH_MAX];
386         char *s_min, *s_max;
387         uint32_t sys_min_freq = 0, sys_max_freq = 0, base_max_freq = 0;
388         uint32_t i, num_freqs = 0;
389
390         snprintf(fullpath_max, sizeof(fullpath_max),
391                         POWER_SYSFILE_BASE_MAX_FREQ,
392                         pi->lcore_id);
393         snprintf(fullpath_min, sizeof(fullpath_min),
394                         POWER_SYSFILE_BASE_MIN_FREQ,
395                         pi->lcore_id);
396
397         f_min = fopen(fullpath_min, "r");
398         FOPEN_OR_ERR_RET(f_min, ret);
399
400         f_max = fopen(fullpath_max, "r");
401         FOPEN_OR_ERR_RET(f_max, ret);
402
403         s_min = fgets(buf_min, sizeof(buf_min), f_min);
404         FOPS_OR_NULL_GOTO(s_min, out);
405
406         s_max = fgets(buf_max, sizeof(buf_max), f_max);
407         FOPS_OR_NULL_GOTO(s_max, out);
408
409
410         /* Strip the line break if there is */
411         p_min = strchr(buf_min, '\n');
412         if (p_min != NULL)
413                 *p_min = 0;
414
415         p_max = strchr(buf_max, '\n');
416         if (p_max != NULL)
417                 *p_max = 0;
418
419         sys_min_freq = strtoul(buf_min, &p_min, POWER_CONVERT_TO_DECIMAL);
420         sys_max_freq = strtoul(buf_max, &p_max, POWER_CONVERT_TO_DECIMAL);
421
422         if (sys_max_freq < sys_min_freq)
423                 goto out;
424
425         pi->sys_max_freq = sys_max_freq;
426
427         base_max_freq = pi->non_turbo_max_ratio * BUS_FREQ;
428
429         POWER_DEBUG_TRACE("sys min %u, sys max %u, base_max %u\n",
430                         sys_min_freq,
431                         sys_max_freq,
432                         base_max_freq);
433
434         if (base_max_freq < sys_max_freq)
435                 pi->turbo_available = 1;
436         else
437                 pi->turbo_available = 0;
438
439         /* If turbo is available then there is one extra freq bucket
440          * to store the sys max freq which value is base_max +1
441          */
442         num_freqs = (base_max_freq - sys_min_freq) / BUS_FREQ + 1 +
443                 pi->turbo_available;
444
445         /* Generate the freq bucket array.
446          * If turbo is available the freq bucket[0] value is base_max +1
447          * the bucket[1] is base_max, bucket[2] is base_max - BUS_FREQ
448          * and so on.
449          * If turbo is not available bucket[0] is base_max and so on
450          */
451         for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
452                 if ((i == 0) && pi->turbo_available)
453                         pi->freqs[pi->nb_freqs++] = base_max_freq + 1;
454                 else
455                         pi->freqs[pi->nb_freqs++] =
456                         base_max_freq - (i - pi->turbo_available) * BUS_FREQ;
457         }
458
459         ret = 0;
460
461         POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
462                         num_freqs, pi->lcore_id);
463
464 out:
465         fclose(f_min);
466         fclose(f_max);
467
468         return ret;
469 }
470
471 int
472 power_pstate_cpufreq_init(unsigned int lcore_id)
473 {
474         struct pstate_power_info *pi;
475
476         if (lcore_id >= RTE_MAX_LCORE) {
477                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceed %u\n",
478                                 lcore_id, RTE_MAX_LCORE - 1U);
479                 return -1;
480         }
481
482         pi = &lcore_power_info[lcore_id];
483         if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
484                         == 0) {
485                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
486                                 "in use\n", lcore_id);
487                 return -1;
488         }
489
490         pi->lcore_id = lcore_id;
491         /* Check and set the governor */
492         if (power_set_governor_performance(pi) < 0) {
493                 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
494                                 "performance\n", lcore_id);
495                 goto fail;
496         }
497         /* Init for setting lcore frequency */
498         if (power_init_for_setting_freq(pi) < 0) {
499                 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
500                                 "lcore %u\n", lcore_id);
501                 goto fail;
502         }
503
504         /* Get the available frequencies */
505         if (power_get_available_freqs(pi) < 0) {
506                 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
507                                 "lcore %u\n", lcore_id);
508                 goto fail;
509         }
510
511
512         /* Set freq to max by default */
513         if (power_pstate_cpufreq_freq_max(lcore_id) < 0) {
514                 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
515                                 "to max\n", lcore_id);
516                 goto fail;
517         }
518
519         RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
520                         "power management\n", lcore_id);
521         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
522
523         return 0;
524
525 fail:
526         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
527
528         return -1;
529 }
530
531 int
532 power_pstate_cpufreq_exit(unsigned int lcore_id)
533 {
534         struct pstate_power_info *pi;
535
536         if (lcore_id >= RTE_MAX_LCORE) {
537                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
538                                 lcore_id, RTE_MAX_LCORE - 1U);
539                 return -1;
540         }
541         pi = &lcore_power_info[lcore_id];
542
543         if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
544                         == 0) {
545                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
546                                 "not used\n", lcore_id);
547                 return -1;
548         }
549
550         /* Close FD of setting freq */
551         fclose(pi->f_cur_min);
552         fclose(pi->f_cur_max);
553         pi->f_cur_min = NULL;
554         pi->f_cur_max = NULL;
555
556         /* Set the governor back to the original */
557         if (power_set_governor_original(pi) < 0) {
558                 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
559                                 "to the original\n", lcore_id);
560                 goto fail;
561         }
562
563         RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
564                         "'performance' mode and been set back to the "
565                         "original\n", lcore_id);
566         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
567
568         return 0;
569
570 fail:
571         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
572
573         return -1;
574 }
575
576
577 uint32_t
578 power_pstate_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
579 {
580         struct pstate_power_info *pi;
581
582         if (lcore_id >= RTE_MAX_LCORE) {
583                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
584                 return -1;
585         }
586
587         if (freqs == NULL) {
588                 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
589                 return 0;
590         }
591
592         pi = &lcore_power_info[lcore_id];
593         if (num < pi->nb_freqs) {
594                 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
595                 return 0;
596         }
597         rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
598
599         return pi->nb_freqs;
600 }
601
602 uint32_t
603 power_pstate_cpufreq_get_freq(unsigned int lcore_id)
604 {
605         if (lcore_id >= RTE_MAX_LCORE) {
606                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
607                 return RTE_POWER_INVALID_FREQ_INDEX;
608         }
609
610         return lcore_power_info[lcore_id].curr_idx;
611 }
612
613
614 int
615 power_pstate_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
616 {
617         if (lcore_id >= RTE_MAX_LCORE) {
618                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
619                 return -1;
620         }
621
622         return set_freq_internal(&(lcore_power_info[lcore_id]), index);
623 }
624
625 int
626 power_pstate_cpufreq_freq_up(unsigned int lcore_id)
627 {
628         struct pstate_power_info *pi;
629
630         if (lcore_id >= RTE_MAX_LCORE) {
631                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
632                 return -1;
633         }
634
635         pi = &lcore_power_info[lcore_id];
636         if (pi->curr_idx == 0)
637                 return 0;
638
639         /* Frequencies in the array are from high to low. */
640         return set_freq_internal(pi, pi->curr_idx - 1);
641 }
642
643 int
644 power_pstate_cpufreq_freq_down(unsigned int lcore_id)
645 {
646         struct pstate_power_info *pi;
647
648         if (lcore_id >= RTE_MAX_LCORE) {
649                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
650                 return -1;
651         }
652
653         pi = &lcore_power_info[lcore_id];
654         if (pi->curr_idx + 1 == pi->nb_freqs)
655                 return 0;
656
657         /* Frequencies in the array are from high to low. */
658         return set_freq_internal(pi, pi->curr_idx + 1);
659 }
660
661 int
662 power_pstate_cpufreq_freq_max(unsigned int lcore_id)
663 {
664         if (lcore_id >= RTE_MAX_LCORE) {
665                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
666                 return -1;
667         }
668
669         /* Frequencies in the array are from high to low. */
670         if (lcore_power_info[lcore_id].turbo_available) {
671                 if (lcore_power_info[lcore_id].turbo_enable)
672                         /* Set to Turbo */
673                         return set_freq_internal(
674                                         &lcore_power_info[lcore_id], 0);
675                 else
676                         /* Set to max non-turbo */
677                         return set_freq_internal(
678                                         &lcore_power_info[lcore_id], 1);
679         } else
680                 return set_freq_internal(&lcore_power_info[lcore_id], 0);
681 }
682
683
684 int
685 power_pstate_cpufreq_freq_min(unsigned int lcore_id)
686 {
687         struct pstate_power_info *pi;
688
689         if (lcore_id >= RTE_MAX_LCORE) {
690                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
691                 return -1;
692         }
693
694         pi = &lcore_power_info[lcore_id];
695
696         /* Frequencies in the array are from high to low. */
697         return set_freq_internal(pi, pi->nb_freqs - 1);
698 }
699
700
701 int
702 power_pstate_turbo_status(unsigned int lcore_id)
703 {
704         struct pstate_power_info *pi;
705
706         if (lcore_id >= RTE_MAX_LCORE) {
707                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
708                 return -1;
709         }
710
711         pi = &lcore_power_info[lcore_id];
712
713         return pi->turbo_enable;
714 }
715
716 int
717 power_pstate_enable_turbo(unsigned int lcore_id)
718 {
719         struct pstate_power_info *pi;
720
721         if (lcore_id >= RTE_MAX_LCORE) {
722                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
723                 return -1;
724         }
725
726         pi = &lcore_power_info[lcore_id];
727
728         if (pi->turbo_available)
729                 pi->turbo_enable = 1;
730         else {
731                 pi->turbo_enable = 0;
732                 RTE_LOG(ERR, POWER,
733                         "Failed to enable turbo on lcore %u\n",
734                         lcore_id);
735                         return -1;
736         }
737
738         return 0;
739 }
740
741
742 int
743 power_pstate_disable_turbo(unsigned int lcore_id)
744 {
745         struct pstate_power_info *pi;
746
747         if (lcore_id >= RTE_MAX_LCORE) {
748                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
749                 return -1;
750         }
751
752         pi = &lcore_power_info[lcore_id];
753
754         pi->turbo_enable = 0;
755
756
757         return 0;
758 }
759
760
761 int power_pstate_get_capabilities(unsigned int lcore_id,
762                 struct rte_power_core_capabilities *caps)
763 {
764         struct pstate_power_info *pi;
765
766         if (lcore_id >= RTE_MAX_LCORE) {
767                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
768                 return -1;
769         }
770         if (caps == NULL) {
771                 RTE_LOG(ERR, POWER, "Invalid argument\n");
772                 return -1;
773         }
774
775         pi = &lcore_power_info[lcore_id];
776         caps->capabilities = 0;
777         caps->turbo = !!(pi->turbo_available);
778
779         return 0;
780 }