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