f7d3f9ca93caf57995b063641b6b94b67dd68a00
[deb_dpdk.git] / lib / librte_power / power_acpi_cpufreq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 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
15 #include <rte_atomic.h>
16 #include <rte_memcpy.h>
17 #include <rte_memory.h>
18
19 #include "power_acpi_cpufreq.h"
20 #include "power_common.h"
21
22 #ifdef RTE_LIBRTE_POWER_DEBUG
23 #define POWER_DEBUG_TRACE(fmt, args...) do { \
24                 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
25 } while (0)
26 #else
27 #define POWER_DEBUG_TRACE(fmt, args...)
28 #endif
29
30 #define FOPEN_OR_ERR_RET(f, retval) do { \
31                 if ((f) == NULL) { \
32                         RTE_LOG(ERR, POWER, "File not openned\n"); \
33                         return retval; \
34                 } \
35 } while (0)
36
37 #define FOPS_OR_NULL_GOTO(ret, label) do { \
38                 if ((ret) == NULL) { \
39                         RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
40                         goto label; \
41                 } \
42 } while (0)
43
44 #define FOPS_OR_ERR_GOTO(ret, label) do { \
45                 if ((ret) < 0) { \
46                         RTE_LOG(ERR, POWER, "File operations failed\n"); \
47                         goto label; \
48                 } \
49 } while (0)
50
51 #define STR_SIZE     1024
52 #define POWER_CONVERT_TO_DECIMAL 10
53
54 #define POWER_GOVERNOR_USERSPACE "userspace"
55 #define POWER_SYSFILE_GOVERNOR   \
56                 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
57 #define POWER_SYSFILE_AVAIL_FREQ \
58                 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies"
59 #define POWER_SYSFILE_SETSPEED   \
60                 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
61
62 /*
63  * MSR related
64  */
65 #define PLATFORM_INFO     0x0CE
66 #define TURBO_RATIO_LIMIT 0x1AD
67 #define IA32_PERF_CTL     0x199
68 #define CORE_TURBO_DISABLE_BIT ((uint64_t)1<<32)
69
70 enum power_state {
71         POWER_IDLE = 0,
72         POWER_ONGOING,
73         POWER_USED,
74         POWER_UNKNOWN
75 };
76
77 /**
78  * Power info per lcore.
79  */
80 struct rte_power_info {
81         unsigned int lcore_id;                   /**< Logical core id */
82         uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
83         uint32_t nb_freqs;                   /**< number of available freqs */
84         FILE *f;                             /**< FD of scaling_setspeed */
85         char governor_ori[32];               /**< Original governor name */
86         uint32_t curr_idx;                   /**< Freq index in freqs array */
87         volatile uint32_t state;             /**< Power in use state */
88         uint16_t turbo_available;            /**< Turbo Boost available */
89         uint16_t turbo_enable;               /**< Turbo Boost enable/disable */
90 } __rte_cache_aligned;
91
92 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
93
94 /**
95  * It is to set specific freq for specific logical core, according to the index
96  * of supported frequencies.
97  */
98 static int
99 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
100 {
101         if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
102                 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
103                                 "should be less than %u\n", idx, pi->nb_freqs);
104                 return -1;
105         }
106
107         /* Check if it is the same as current */
108         if (idx == pi->curr_idx)
109                 return 0;
110
111         POWER_DEBUG_TRACE("Freqency[%u] %u to be set for lcore %u\n",
112                         idx, pi->freqs[idx], pi->lcore_id);
113         if (fseek(pi->f, 0, SEEK_SET) < 0) {
114                 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
115                                 "for setting frequency for lcore %u\n", pi->lcore_id);
116                 return -1;
117         }
118         if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
119                 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
120                                 "lcore %u\n", pi->lcore_id);
121                 return -1;
122         }
123         fflush(pi->f);
124         pi->curr_idx = idx;
125
126         return 1;
127 }
128
129 /**
130  * It is to check the current scaling governor by reading sys file, and then
131  * set it into 'userspace' if it is not by writing the sys file. The original
132  * governor will be saved for rolling back.
133  */
134 static int
135 power_set_governor_userspace(struct rte_power_info *pi)
136 {
137         FILE *f;
138         int ret = -1;
139         char buf[BUFSIZ];
140         char fullpath[PATH_MAX];
141         char *s;
142         int val;
143
144         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
145                         pi->lcore_id);
146         f = fopen(fullpath, "rw+");
147         FOPEN_OR_ERR_RET(f, ret);
148
149         s = fgets(buf, sizeof(buf), f);
150         FOPS_OR_NULL_GOTO(s, out);
151         /* Strip off terminating '\n' */
152         strtok(buf, "\n");
153
154         /* Check if current governor is userspace */
155         if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
156                         sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
157                 ret = 0;
158                 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
159                                 "already userspace\n", pi->lcore_id);
160                 goto out;
161         }
162         /* Save the original governor */
163         snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
164
165         /* Write 'userspace' to the governor */
166         val = fseek(f, 0, SEEK_SET);
167         FOPS_OR_ERR_GOTO(val, out);
168
169         val = fputs(POWER_GOVERNOR_USERSPACE, f);
170         FOPS_OR_ERR_GOTO(val, out);
171
172         ret = 0;
173         RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
174                         "set to user space successfully\n", pi->lcore_id);
175 out:
176         fclose(f);
177
178         return ret;
179 }
180
181 /**
182  * It is to get the available frequencies of the specific lcore by reading the
183  * sys file.
184  */
185 static int
186 power_get_available_freqs(struct rte_power_info *pi)
187 {
188         FILE *f;
189         int ret = -1, i, count;
190         char *p;
191         char buf[BUFSIZ];
192         char fullpath[PATH_MAX];
193         char *freqs[RTE_MAX_LCORE_FREQS];
194         char *s;
195
196         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
197                         pi->lcore_id);
198         f = fopen(fullpath, "r");
199         FOPEN_OR_ERR_RET(f, ret);
200
201         s = fgets(buf, sizeof(buf), f);
202         FOPS_OR_NULL_GOTO(s, out);
203
204         /* Strip the line break if there is */
205         p = strchr(buf, '\n');
206         if (p != NULL)
207                 *p = 0;
208
209         /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
210         count = rte_strsplit(buf, sizeof(buf), freqs,
211                         RTE_MAX_LCORE_FREQS, ' ');
212         if (count <= 0) {
213                 RTE_LOG(ERR, POWER, "No available frequency in "
214                                 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
215                 goto out;
216         }
217         if (count >= RTE_MAX_LCORE_FREQS) {
218                 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
219                                 count);
220                 goto out;
221         }
222
223         /* Store the available frequncies into power context */
224         for (i = 0, pi->nb_freqs = 0; i < count; i++) {
225                 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
226                                 i, freqs[i]);
227                 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
228                                 POWER_CONVERT_TO_DECIMAL);
229         }
230
231         if ((pi->freqs[0]-1000) == pi->freqs[1]) {
232                 pi->turbo_available = 1;
233                 pi->turbo_enable = 1;
234                 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
235                                 pi->lcore_id);
236         } else {
237                 pi->turbo_available = 0;
238                 pi->turbo_enable = 0;
239                 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
240                                 pi->lcore_id);
241         }
242
243         ret = 0;
244         POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
245                         count, pi->lcore_id);
246 out:
247         fclose(f);
248
249         return ret;
250 }
251
252 /**
253  * It is to fopen the sys file for the future setting the lcore frequency.
254  */
255 static int
256 power_init_for_setting_freq(struct rte_power_info *pi)
257 {
258         FILE *f;
259         char fullpath[PATH_MAX];
260         char buf[BUFSIZ];
261         uint32_t i, freq;
262         char *s;
263
264         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
265                         pi->lcore_id);
266         f = fopen(fullpath, "rw+");
267         FOPEN_OR_ERR_RET(f, -1);
268
269         s = fgets(buf, sizeof(buf), f);
270         FOPS_OR_NULL_GOTO(s, out);
271
272         freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
273         for (i = 0; i < pi->nb_freqs; i++) {
274                 if (freq == pi->freqs[i]) {
275                         pi->curr_idx = i;
276                         pi->f = f;
277                         return 0;
278                 }
279         }
280
281 out:
282         fclose(f);
283
284         return -1;
285 }
286
287 int
288 power_acpi_cpufreq_init(unsigned int lcore_id)
289 {
290         struct rte_power_info *pi;
291
292         if (lcore_id >= RTE_MAX_LCORE) {
293                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
294                                 lcore_id, RTE_MAX_LCORE - 1U);
295                 return -1;
296         }
297
298         pi = &lcore_power_info[lcore_id];
299         if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
300                         == 0) {
301                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
302                                 "in use\n", lcore_id);
303                 return -1;
304         }
305
306         pi->lcore_id = lcore_id;
307         /* Check and set the governor */
308         if (power_set_governor_userspace(pi) < 0) {
309                 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
310                                 "userspace\n", lcore_id);
311                 goto fail;
312         }
313
314         /* Get the available frequencies */
315         if (power_get_available_freqs(pi) < 0) {
316                 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
317                                 "lcore %u\n", lcore_id);
318                 goto fail;
319         }
320
321         /* Init for setting lcore frequency */
322         if (power_init_for_setting_freq(pi) < 0) {
323                 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
324                                 "lcore %u\n", lcore_id);
325                 goto fail;
326         }
327
328         /* Set freq to max by default */
329         if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
330                 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
331                                 "to max\n", lcore_id);
332                 goto fail;
333         }
334
335         RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
336                         "power management\n", lcore_id);
337         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
338
339         return 0;
340
341 fail:
342         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
343
344         return -1;
345 }
346
347 /**
348  * It is to check the governor and then set the original governor back if
349  * needed by writing the sys file.
350  */
351 static int
352 power_set_governor_original(struct rte_power_info *pi)
353 {
354         FILE *f;
355         int ret = -1;
356         char buf[BUFSIZ];
357         char fullpath[PATH_MAX];
358         char *s;
359         int val;
360
361         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
362                         pi->lcore_id);
363         f = fopen(fullpath, "rw+");
364         FOPEN_OR_ERR_RET(f, ret);
365
366         s = fgets(buf, sizeof(buf), f);
367         FOPS_OR_NULL_GOTO(s, out);
368
369         /* Check if the governor to be set is the same as current */
370         if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
371                 ret = 0;
372                 POWER_DEBUG_TRACE("Power management governor of lcore %u "
373                                 "has already been set to %s\n",
374                                 pi->lcore_id, pi->governor_ori);
375                 goto out;
376         }
377
378         /* Write back the original governor */
379         val = fseek(f, 0, SEEK_SET);
380         FOPS_OR_ERR_GOTO(val, out);
381
382         val = fputs(pi->governor_ori, f);
383         FOPS_OR_ERR_GOTO(val, out);
384
385         ret = 0;
386         RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
387                         "has been set back to %s successfully\n",
388                         pi->lcore_id, pi->governor_ori);
389 out:
390         fclose(f);
391
392         return ret;
393 }
394
395 int
396 power_acpi_cpufreq_exit(unsigned int lcore_id)
397 {
398         struct rte_power_info *pi;
399
400         if (lcore_id >= RTE_MAX_LCORE) {
401                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
402                                 lcore_id, RTE_MAX_LCORE - 1U);
403                 return -1;
404         }
405         pi = &lcore_power_info[lcore_id];
406         if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
407                         == 0) {
408                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
409                                 "not used\n", lcore_id);
410                 return -1;
411         }
412
413         /* Close FD of setting freq */
414         fclose(pi->f);
415         pi->f = NULL;
416
417         /* Set the governor back to the original */
418         if (power_set_governor_original(pi) < 0) {
419                 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
420                                 "to the original\n", lcore_id);
421                 goto fail;
422         }
423
424         RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
425                         "'userspace' mode and been set back to the "
426                         "original\n", lcore_id);
427         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
428
429         return 0;
430
431 fail:
432         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
433
434         return -1;
435 }
436
437 uint32_t
438 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
439 {
440         struct rte_power_info *pi;
441
442         if (lcore_id >= RTE_MAX_LCORE || !freqs) {
443                 RTE_LOG(ERR, POWER, "Invalid input parameter\n");
444                 return 0;
445         }
446
447         pi = &lcore_power_info[lcore_id];
448         if (num < pi->nb_freqs) {
449                 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
450                 return 0;
451         }
452         rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
453
454         return pi->nb_freqs;
455 }
456
457 uint32_t
458 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
459 {
460         if (lcore_id >= RTE_MAX_LCORE) {
461                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
462                 return RTE_POWER_INVALID_FREQ_INDEX;
463         }
464
465         return lcore_power_info[lcore_id].curr_idx;
466 }
467
468 int
469 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
470 {
471         if (lcore_id >= RTE_MAX_LCORE) {
472                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
473                 return -1;
474         }
475
476         return set_freq_internal(&(lcore_power_info[lcore_id]), index);
477 }
478
479 int
480 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
481 {
482         struct rte_power_info *pi;
483
484         if (lcore_id >= RTE_MAX_LCORE) {
485                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
486                 return -1;
487         }
488
489         pi = &lcore_power_info[lcore_id];
490         if (pi->curr_idx + 1 == pi->nb_freqs)
491                 return 0;
492
493         /* Frequencies in the array are from high to low. */
494         return set_freq_internal(pi, pi->curr_idx + 1);
495 }
496
497 int
498 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
499 {
500         struct rte_power_info *pi;
501
502         if (lcore_id >= RTE_MAX_LCORE) {
503                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
504                 return -1;
505         }
506
507         pi = &lcore_power_info[lcore_id];
508         if (pi->curr_idx == 0)
509                 return 0;
510
511         /* Frequencies in the array are from high to low. */
512         return set_freq_internal(pi, pi->curr_idx - 1);
513 }
514
515 int
516 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
517 {
518         if (lcore_id >= RTE_MAX_LCORE) {
519                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
520                 return -1;
521         }
522
523         /* Frequencies in the array are from high to low. */
524         if (lcore_power_info[lcore_id].turbo_available) {
525                 if (lcore_power_info[lcore_id].turbo_enable)
526                         /* Set to Turbo */
527                         return set_freq_internal(
528                                         &lcore_power_info[lcore_id], 0);
529                 else
530                         /* Set to max non-turbo */
531                         return set_freq_internal(
532                                         &lcore_power_info[lcore_id], 1);
533         } else
534                 return set_freq_internal(&lcore_power_info[lcore_id], 0);
535 }
536
537 int
538 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
539 {
540         struct rte_power_info *pi;
541
542         if (lcore_id >= RTE_MAX_LCORE) {
543                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
544                 return -1;
545         }
546
547         pi = &lcore_power_info[lcore_id];
548
549         /* Frequencies in the array are from high to low. */
550         return set_freq_internal(pi, pi->nb_freqs - 1);
551 }
552
553
554 int
555 power_acpi_turbo_status(unsigned int lcore_id)
556 {
557         struct rte_power_info *pi;
558
559         if (lcore_id >= RTE_MAX_LCORE) {
560                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
561                 return -1;
562         }
563
564         pi = &lcore_power_info[lcore_id];
565
566         return pi->turbo_enable;
567 }
568
569
570 int
571 power_acpi_enable_turbo(unsigned int lcore_id)
572 {
573         struct rte_power_info *pi;
574
575         if (lcore_id >= RTE_MAX_LCORE) {
576                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
577                 return -1;
578         }
579
580         pi = &lcore_power_info[lcore_id];
581
582         if (pi->turbo_available)
583                 pi->turbo_enable = 1;
584         else {
585                 pi->turbo_enable = 0;
586                 RTE_LOG(ERR, POWER,
587                         "Failed to enable turbo on lcore %u\n",
588                         lcore_id);
589                         return -1;
590         }
591
592         /* Max may have changed, so call to max function */
593         if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
594                 RTE_LOG(ERR, POWER,
595                         "Failed to set frequency of lcore %u to max\n",
596                         lcore_id);
597                         return -1;
598         }
599
600         return 0;
601 }
602
603 int
604 power_acpi_disable_turbo(unsigned int lcore_id)
605 {
606         struct rte_power_info *pi;
607
608         if (lcore_id >= RTE_MAX_LCORE) {
609                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
610                 return -1;
611         }
612
613         pi = &lcore_power_info[lcore_id];
614
615          pi->turbo_enable = 0;
616
617         if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
618                 /* Try to set freq to max by default coming out of turbo */
619                 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
620                         RTE_LOG(ERR, POWER,
621                                 "Failed to set frequency of lcore %u to max\n",
622                                 lcore_id);
623                         return -1;
624                 }
625         }
626
627         return 0;
628 }
629
630 int power_acpi_get_capabilities(unsigned int lcore_id,
631                 struct rte_power_core_capabilities *caps)
632 {
633         struct rte_power_info *pi;
634
635         if (lcore_id >= RTE_MAX_LCORE) {
636                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
637                 return -1;
638         }
639         if (caps == NULL) {
640                 RTE_LOG(ERR, POWER, "Invalid argument\n");
641                 return -1;
642         }
643
644         pi = &lcore_power_info[lcore_id];
645         caps->capabilities = 0;
646         caps->turbo = !!(pi->turbo_available);
647
648         return 0;
649 }