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