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