power: fix governor storage to trim newlines
[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         /* Strip off terminating '\n' */
151         strtok(buf, "\n");
152
153         /* Check if current governor is userspace */
154         if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
155                         sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
156                 ret = 0;
157                 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
158                                 "already userspace\n", pi->lcore_id);
159                 goto out;
160         }
161         /* Save the original governor */
162         snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
163
164         /* Write 'userspace' to the governor */
165         val = fseek(f, 0, SEEK_SET);
166         FOPS_OR_ERR_GOTO(val, out);
167
168         val = fputs(POWER_GOVERNOR_USERSPACE, f);
169         FOPS_OR_ERR_GOTO(val, out);
170
171         /* We need to flush to see if the fputs succeeds */
172         val = fflush(f);
173         FOPS_OR_ERR_GOTO(val, out);
174
175         ret = 0;
176         RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
177                         "set to user space successfully\n", pi->lcore_id);
178 out:
179         fclose(f);
180
181         return ret;
182 }
183
184 /**
185  * It is to get the available frequencies of the specific lcore by reading the
186  * sys file.
187  */
188 static int
189 power_get_available_freqs(struct rte_power_info *pi)
190 {
191         FILE *f;
192         int ret = -1, i, count;
193         char *p;
194         char buf[BUFSIZ];
195         char fullpath[PATH_MAX];
196         char *freqs[RTE_MAX_LCORE_FREQS];
197         char *s;
198
199         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
200                         pi->lcore_id);
201         f = fopen(fullpath, "r");
202         FOPEN_OR_ERR_RET(f, ret);
203
204         s = fgets(buf, sizeof(buf), f);
205         FOPS_OR_NULL_GOTO(s, out);
206
207         /* Strip the line break if there is */
208         p = strchr(buf, '\n');
209         if (p != NULL)
210                 *p = 0;
211
212         /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
213         count = rte_strsplit(buf, sizeof(buf), freqs,
214                         RTE_MAX_LCORE_FREQS, ' ');
215         if (count <= 0) {
216                 RTE_LOG(ERR, POWER, "No available frequency in "
217                                 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
218                 goto out;
219         }
220         if (count >= RTE_MAX_LCORE_FREQS) {
221                 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
222                                 count);
223                 goto out;
224         }
225
226         /* Store the available frequncies into power context */
227         for (i = 0, pi->nb_freqs = 0; i < count; i++) {
228                 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
229                                 i, freqs[i]);
230                 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
231                                 POWER_CONVERT_TO_DECIMAL);
232         }
233
234         if ((pi->freqs[0]-1000) == pi->freqs[1]) {
235                 pi->turbo_available = 1;
236                 pi->turbo_enable = 1;
237                 POWER_DEBUG_TRACE("Lcore %u Can do Turbo Boost\n",
238                                 pi->lcore_id);
239         } else {
240                 pi->turbo_available = 0;
241                 pi->turbo_enable = 0;
242                 POWER_DEBUG_TRACE("Turbo Boost not available on Lcore %u\n",
243                                 pi->lcore_id);
244         }
245
246         ret = 0;
247         POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are available\n",
248                         count, pi->lcore_id);
249 out:
250         fclose(f);
251
252         return ret;
253 }
254
255 /**
256  * It is to fopen the sys file for the future setting the lcore frequency.
257  */
258 static int
259 power_init_for_setting_freq(struct rte_power_info *pi)
260 {
261         FILE *f;
262         char fullpath[PATH_MAX];
263         char buf[BUFSIZ];
264         uint32_t i, freq;
265         char *s;
266
267         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
268                         pi->lcore_id);
269         f = fopen(fullpath, "rw+");
270         FOPEN_OR_ERR_RET(f, -1);
271
272         s = fgets(buf, sizeof(buf), f);
273         FOPS_OR_NULL_GOTO(s, out);
274
275         freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
276         for (i = 0; i < pi->nb_freqs; i++) {
277                 if (freq == pi->freqs[i]) {
278                         pi->curr_idx = i;
279                         pi->f = f;
280                         return 0;
281                 }
282         }
283
284 out:
285         fclose(f);
286
287         return -1;
288 }
289
290 int
291 power_acpi_cpufreq_init(unsigned int lcore_id)
292 {
293         struct rte_power_info *pi;
294
295         if (lcore_id >= RTE_MAX_LCORE) {
296                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
297                                 lcore_id, RTE_MAX_LCORE - 1U);
298                 return -1;
299         }
300
301         pi = &lcore_power_info[lcore_id];
302         if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
303                         == 0) {
304                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
305                                 "in use\n", lcore_id);
306                 return -1;
307         }
308
309         pi->lcore_id = lcore_id;
310         /* Check and set the governor */
311         if (power_set_governor_userspace(pi) < 0) {
312                 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
313                                 "userspace\n", lcore_id);
314                 goto fail;
315         }
316
317         /* Get the available frequencies */
318         if (power_get_available_freqs(pi) < 0) {
319                 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
320                                 "lcore %u\n", lcore_id);
321                 goto fail;
322         }
323
324         /* Init for setting lcore frequency */
325         if (power_init_for_setting_freq(pi) < 0) {
326                 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
327                                 "lcore %u\n", lcore_id);
328                 goto fail;
329         }
330
331         /* Set freq to max by default */
332         if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
333                 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
334                                 "to max\n", lcore_id);
335                 goto fail;
336         }
337
338         RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
339                         "power management\n", lcore_id);
340         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
341
342         return 0;
343
344 fail:
345         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
346
347         return -1;
348 }
349
350 /**
351  * It is to check the governor and then set the original governor back if
352  * needed by writing the sys file.
353  */
354 static int
355 power_set_governor_original(struct rte_power_info *pi)
356 {
357         FILE *f;
358         int ret = -1;
359         char buf[BUFSIZ];
360         char fullpath[PATH_MAX];
361         char *s;
362         int val;
363
364         snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
365                         pi->lcore_id);
366         f = fopen(fullpath, "rw+");
367         FOPEN_OR_ERR_RET(f, ret);
368
369         s = fgets(buf, sizeof(buf), f);
370         FOPS_OR_NULL_GOTO(s, out);
371
372         /* Check if the governor to be set is the same as current */
373         if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
374                 ret = 0;
375                 POWER_DEBUG_TRACE("Power management governor of lcore %u "
376                                 "has already been set to %s\n",
377                                 pi->lcore_id, pi->governor_ori);
378                 goto out;
379         }
380
381         /* Write back the original governor */
382         val = fseek(f, 0, SEEK_SET);
383         FOPS_OR_ERR_GOTO(val, out);
384
385         val = fputs(pi->governor_ori, f);
386         FOPS_OR_ERR_GOTO(val, out);
387
388         ret = 0;
389         RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
390                         "has been set back to %s successfully\n",
391                         pi->lcore_id, pi->governor_ori);
392 out:
393         fclose(f);
394
395         return ret;
396 }
397
398 int
399 power_acpi_cpufreq_exit(unsigned int lcore_id)
400 {
401         struct rte_power_info *pi;
402
403         if (lcore_id >= RTE_MAX_LCORE) {
404                 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
405                                 lcore_id, RTE_MAX_LCORE - 1U);
406                 return -1;
407         }
408         pi = &lcore_power_info[lcore_id];
409         if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
410                         == 0) {
411                 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
412                                 "not used\n", lcore_id);
413                 return -1;
414         }
415
416         /* Close FD of setting freq */
417         fclose(pi->f);
418         pi->f = NULL;
419
420         /* Set the governor back to the original */
421         if (power_set_governor_original(pi) < 0) {
422                 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
423                                 "to the original\n", lcore_id);
424                 goto fail;
425         }
426
427         RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
428                         "'userspace' mode and been set back to the "
429                         "original\n", lcore_id);
430         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
431
432         return 0;
433
434 fail:
435         rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
436
437         return -1;
438 }
439
440 uint32_t
441 power_acpi_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
442 {
443         struct rte_power_info *pi;
444
445         if (lcore_id >= RTE_MAX_LCORE) {
446                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
447                 return 0;
448         }
449
450         if (freqs == NULL) {
451                 RTE_LOG(ERR, POWER, "NULL buffer supplied\n");
452                 return 0;
453         }
454
455         pi = &lcore_power_info[lcore_id];
456         if (num < pi->nb_freqs) {
457                 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
458                 return 0;
459         }
460         rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
461
462         return pi->nb_freqs;
463 }
464
465 uint32_t
466 power_acpi_cpufreq_get_freq(unsigned int lcore_id)
467 {
468         if (lcore_id >= RTE_MAX_LCORE) {
469                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
470                 return RTE_POWER_INVALID_FREQ_INDEX;
471         }
472
473         return lcore_power_info[lcore_id].curr_idx;
474 }
475
476 int
477 power_acpi_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
478 {
479         if (lcore_id >= RTE_MAX_LCORE) {
480                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
481                 return -1;
482         }
483
484         return set_freq_internal(&(lcore_power_info[lcore_id]), index);
485 }
486
487 int
488 power_acpi_cpufreq_freq_down(unsigned int lcore_id)
489 {
490         struct rte_power_info *pi;
491
492         if (lcore_id >= RTE_MAX_LCORE) {
493                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
494                 return -1;
495         }
496
497         pi = &lcore_power_info[lcore_id];
498         if (pi->curr_idx + 1 == pi->nb_freqs)
499                 return 0;
500
501         /* Frequencies in the array are from high to low. */
502         return set_freq_internal(pi, pi->curr_idx + 1);
503 }
504
505 int
506 power_acpi_cpufreq_freq_up(unsigned int lcore_id)
507 {
508         struct rte_power_info *pi;
509
510         if (lcore_id >= RTE_MAX_LCORE) {
511                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
512                 return -1;
513         }
514
515         pi = &lcore_power_info[lcore_id];
516         if (pi->curr_idx == 0)
517                 return 0;
518
519         /* Frequencies in the array are from high to low. */
520         return set_freq_internal(pi, pi->curr_idx - 1);
521 }
522
523 int
524 power_acpi_cpufreq_freq_max(unsigned int lcore_id)
525 {
526         if (lcore_id >= RTE_MAX_LCORE) {
527                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
528                 return -1;
529         }
530
531         /* Frequencies in the array are from high to low. */
532         if (lcore_power_info[lcore_id].turbo_available) {
533                 if (lcore_power_info[lcore_id].turbo_enable)
534                         /* Set to Turbo */
535                         return set_freq_internal(
536                                         &lcore_power_info[lcore_id], 0);
537                 else
538                         /* Set to max non-turbo */
539                         return set_freq_internal(
540                                         &lcore_power_info[lcore_id], 1);
541         } else
542                 return set_freq_internal(&lcore_power_info[lcore_id], 0);
543 }
544
545 int
546 power_acpi_cpufreq_freq_min(unsigned int lcore_id)
547 {
548         struct rte_power_info *pi;
549
550         if (lcore_id >= RTE_MAX_LCORE) {
551                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
552                 return -1;
553         }
554
555         pi = &lcore_power_info[lcore_id];
556
557         /* Frequencies in the array are from high to low. */
558         return set_freq_internal(pi, pi->nb_freqs - 1);
559 }
560
561
562 int
563 power_acpi_turbo_status(unsigned int lcore_id)
564 {
565         struct rte_power_info *pi;
566
567         if (lcore_id >= RTE_MAX_LCORE) {
568                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
569                 return -1;
570         }
571
572         pi = &lcore_power_info[lcore_id];
573
574         return pi->turbo_enable;
575 }
576
577
578 int
579 power_acpi_enable_turbo(unsigned int lcore_id)
580 {
581         struct rte_power_info *pi;
582
583         if (lcore_id >= RTE_MAX_LCORE) {
584                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
585                 return -1;
586         }
587
588         pi = &lcore_power_info[lcore_id];
589
590         if (pi->turbo_available)
591                 pi->turbo_enable = 1;
592         else {
593                 pi->turbo_enable = 0;
594                 RTE_LOG(ERR, POWER,
595                         "Failed to enable turbo on lcore %u\n",
596                         lcore_id);
597                         return -1;
598         }
599
600         /* Max may have changed, so call to max function */
601         if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
602                 RTE_LOG(ERR, POWER,
603                         "Failed to set frequency of lcore %u to max\n",
604                         lcore_id);
605                         return -1;
606         }
607
608         return 0;
609 }
610
611 int
612 power_acpi_disable_turbo(unsigned int lcore_id)
613 {
614         struct rte_power_info *pi;
615
616         if (lcore_id >= RTE_MAX_LCORE) {
617                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
618                 return -1;
619         }
620
621         pi = &lcore_power_info[lcore_id];
622
623          pi->turbo_enable = 0;
624
625         if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
626                 /* Try to set freq to max by default coming out of turbo */
627                 if (power_acpi_cpufreq_freq_max(lcore_id) < 0) {
628                         RTE_LOG(ERR, POWER,
629                                 "Failed to set frequency of lcore %u to max\n",
630                                 lcore_id);
631                         return -1;
632                 }
633         }
634
635         return 0;
636 }
637
638 int power_acpi_get_capabilities(unsigned int lcore_id,
639                 struct rte_power_core_capabilities *caps)
640 {
641         struct rte_power_info *pi;
642
643         if (lcore_id >= RTE_MAX_LCORE) {
644                 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
645                 return -1;
646         }
647         if (caps == NULL) {
648                 RTE_LOG(ERR, POWER, "Invalid argument\n");
649                 return -1;
650         }
651
652         pi = &lcore_power_info[lcore_id];
653         caps->capabilities = 0;
654         caps->turbo = !!(pi->turbo_available);
655
656         return 0;
657 }