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