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