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