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