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