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