Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2021 Intel Corporation
3 : : * Copyright(c) 2021 Arm Limited
4 : : */
5 : :
6 : : #include <stdlib.h>
7 : :
8 : : #include <rte_memcpy.h>
9 : : #include <rte_stdatomic.h>
10 : :
11 : : #include "cppc_cpufreq.h"
12 : : #include "power_common.h"
13 : :
14 : : /* macros used for rounding frequency to nearest 100000 */
15 : : #define FREQ_ROUNDING_DELTA 50000
16 : : #define ROUND_FREQ_TO_N_100000 100000
17 : :
18 : : /* the unit of highest_perf and nominal_perf differs on different arm platforms.
19 : : * For highest_perf, it maybe 300 or 3000000, both means 3.0GHz.
20 : : */
21 : : #define UNIT_DIFF 10000
22 : :
23 : : #define POWER_GOVERNOR_USERSPACE "userspace"
24 : : #define POWER_SYSFILE_SETSPEED \
25 : : "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
26 : : #define POWER_SYSFILE_SCALING_MAX_FREQ \
27 : : "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
28 : : #define POWER_SYSFILE_SCALING_MIN_FREQ \
29 : : "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_min_freq"
30 : : #define POWER_SYSFILE_HIGHEST_PERF \
31 : : "/sys/devices/system/cpu/cpu%u/acpi_cppc/highest_perf"
32 : : #define POWER_SYSFILE_NOMINAL_PERF \
33 : : "/sys/devices/system/cpu/cpu%u/acpi_cppc/nominal_perf"
34 : : #define POWER_SYSFILE_SYS_MAX \
35 : : "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq"
36 : :
37 : : #define POWER_CPPC_DRIVER "cppc_cpufreq"
38 : : #define BUS_FREQ 100000
39 : :
40 : : /**
41 : : * Power info per lcore.
42 : : */
43 : : struct __rte_cache_aligned cppc_power_info {
44 : : unsigned int lcore_id; /**< Logical core id */
45 : : RTE_ATOMIC(uint32_t) state; /**< Power in use state */
46 : : FILE *f; /**< FD of scaling_setspeed */
47 : : char governor_ori[32]; /**< Original governor name */
48 : : uint32_t curr_idx; /**< Freq index in freqs array */
49 : : uint32_t highest_perf; /**< system wide max freq */
50 : : uint32_t nominal_perf; /**< system wide nominal freq */
51 : : uint16_t turbo_available; /**< Turbo Boost available */
52 : : uint16_t turbo_enable; /**< Turbo Boost enable/disable */
53 : : uint32_t nb_freqs; /**< number of available freqs */
54 : : uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
55 : : };
56 : :
57 : : static struct cppc_power_info lcore_power_info[RTE_MAX_LCORE];
58 : :
59 : : /**
60 : : * It is to set specific freq for specific logical core, according to the index
61 : : * of supported frequencies.
62 : : */
63 : : static int
64 : 0 : set_freq_internal(struct cppc_power_info *pi, uint32_t idx)
65 : : {
66 [ # # # # ]: 0 : if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
67 : 0 : POWER_LOG(ERR, "Invalid frequency index %u, which "
68 : : "should be less than %u", idx, pi->nb_freqs);
69 : 0 : return -1;
70 : : }
71 : :
72 : : /* Check if it is the same as current */
73 [ # # ]: 0 : if (idx == pi->curr_idx)
74 : : return 0;
75 : :
76 : : POWER_DEBUG_LOG("Frequency[%u] %u to be set for lcore %u",
77 : : idx, pi->freqs[idx], pi->lcore_id);
78 [ # # ]: 0 : if (fseek(pi->f, 0, SEEK_SET) < 0) {
79 : 0 : POWER_LOG(ERR, "Fail to set file position indicator to 0 "
80 : : "for setting frequency for lcore %u", pi->lcore_id);
81 : 0 : return -1;
82 : : }
83 [ # # ]: 0 : if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
84 : 0 : POWER_LOG(ERR, "Fail to write new frequency for "
85 : : "lcore %u", pi->lcore_id);
86 : 0 : return -1;
87 : : }
88 : 0 : fflush(pi->f);
89 : 0 : pi->curr_idx = idx;
90 : :
91 : 0 : return 1;
92 : : }
93 : :
94 : : /**
95 : : * It is to check the current scaling governor by reading sys file, and then
96 : : * set it into 'userspace' if it is not by writing the sys file. The original
97 : : * governor will be saved for rolling back.
98 : : */
99 : : static int
100 : : power_set_governor_userspace(struct cppc_power_info *pi)
101 : : {
102 : 0 : return power_set_governor(pi->lcore_id, POWER_GOVERNOR_USERSPACE,
103 : 0 : pi->governor_ori, sizeof(pi->governor_ori));
104 : : }
105 : :
106 : : static int
107 : 0 : power_check_turbo(struct cppc_power_info *pi)
108 : : {
109 : 0 : FILE *f_nom = NULL, *f_max = NULL, *f_cmax = NULL;
110 : : int ret = -1;
111 : 0 : uint32_t nominal_perf = 0, highest_perf = 0, cpuinfo_max_freq = 0;
112 : :
113 : 0 : open_core_sysfs_file(&f_max, "r", POWER_SYSFILE_HIGHEST_PERF,
114 : : pi->lcore_id);
115 [ # # ]: 0 : if (f_max == NULL) {
116 : 0 : POWER_LOG(ERR, "failed to open %s",
117 : : POWER_SYSFILE_HIGHEST_PERF);
118 : 0 : goto err;
119 : : }
120 : :
121 : 0 : open_core_sysfs_file(&f_nom, "r", POWER_SYSFILE_NOMINAL_PERF,
122 : : pi->lcore_id);
123 [ # # ]: 0 : if (f_nom == NULL) {
124 : 0 : POWER_LOG(ERR, "failed to open %s",
125 : : POWER_SYSFILE_NOMINAL_PERF);
126 : 0 : goto err;
127 : : }
128 : :
129 : 0 : open_core_sysfs_file(&f_cmax, "r", POWER_SYSFILE_SYS_MAX,
130 : : pi->lcore_id);
131 [ # # ]: 0 : if (f_cmax == NULL) {
132 : 0 : POWER_LOG(ERR, "failed to open %s",
133 : : POWER_SYSFILE_SYS_MAX);
134 : 0 : goto err;
135 : : }
136 : :
137 : 0 : ret = read_core_sysfs_u32(f_max, &highest_perf);
138 [ # # ]: 0 : if (ret < 0) {
139 : 0 : POWER_LOG(ERR, "Failed to read %s",
140 : : POWER_SYSFILE_HIGHEST_PERF);
141 : 0 : goto err;
142 : : }
143 : :
144 : 0 : ret = read_core_sysfs_u32(f_nom, &nominal_perf);
145 [ # # ]: 0 : if (ret < 0) {
146 : 0 : POWER_LOG(ERR, "Failed to read %s",
147 : : POWER_SYSFILE_NOMINAL_PERF);
148 : 0 : goto err;
149 : : }
150 : :
151 : 0 : ret = read_core_sysfs_u32(f_cmax, &cpuinfo_max_freq);
152 [ # # ]: 0 : if (ret < 0) {
153 : 0 : POWER_LOG(ERR, "Failed to read %s",
154 : : POWER_SYSFILE_SYS_MAX);
155 : 0 : goto err;
156 : : }
157 : :
158 : 0 : pi->highest_perf = highest_perf;
159 : 0 : pi->nominal_perf = nominal_perf;
160 : :
161 [ # # # # ]: 0 : if ((highest_perf > nominal_perf) && ((cpuinfo_max_freq == highest_perf)
162 [ # # ]: 0 : || cpuinfo_max_freq == highest_perf * UNIT_DIFF)) {
163 : 0 : pi->turbo_available = 1;
164 : 0 : pi->turbo_enable = 1;
165 : 0 : ret = 0;
166 : : POWER_DEBUG_LOG("Lcore %u can do Turbo Boost! highest perf %u, "
167 : : "nominal perf %u",
168 : : pi->lcore_id, highest_perf, nominal_perf);
169 : : } else {
170 : 0 : pi->turbo_available = 0;
171 : 0 : pi->turbo_enable = 0;
172 : : POWER_DEBUG_LOG("Lcore %u Turbo not available! highest perf %u, "
173 : : "nominal perf %u",
174 : : pi->lcore_id, highest_perf, nominal_perf);
175 : : }
176 : :
177 : 0 : err:
178 [ # # ]: 0 : if (f_max != NULL)
179 : 0 : fclose(f_max);
180 [ # # ]: 0 : if (f_nom != NULL)
181 : 0 : fclose(f_nom);
182 [ # # ]: 0 : if (f_cmax != NULL)
183 : 0 : fclose(f_cmax);
184 : :
185 : 0 : return ret;
186 : : }
187 : :
188 : : /**
189 : : * It is to get the available frequencies of the specific lcore by reading the
190 : : * sys file.
191 : : */
192 : : static int
193 : 0 : power_get_available_freqs(struct cppc_power_info *pi)
194 : : {
195 : 0 : FILE *f_min = NULL, *f_max = NULL;
196 : : int ret = -1;
197 : 0 : uint32_t scaling_min_freq = 0, scaling_max_freq = 0, nominal_perf = 0;
198 : : uint32_t i, num_freqs = 0;
199 : :
200 : 0 : open_core_sysfs_file(&f_max, "r", POWER_SYSFILE_SCALING_MAX_FREQ,
201 : : pi->lcore_id);
202 [ # # ]: 0 : if (f_max == NULL) {
203 : 0 : POWER_LOG(ERR, "failed to open %s",
204 : : POWER_SYSFILE_SCALING_MAX_FREQ);
205 : 0 : goto out;
206 : : }
207 : :
208 : 0 : open_core_sysfs_file(&f_min, "r", POWER_SYSFILE_SCALING_MIN_FREQ,
209 : : pi->lcore_id);
210 [ # # ]: 0 : if (f_min == NULL) {
211 : 0 : POWER_LOG(ERR, "failed to open %s",
212 : : POWER_SYSFILE_SCALING_MIN_FREQ);
213 : 0 : goto out;
214 : : }
215 : :
216 : 0 : ret = read_core_sysfs_u32(f_max, &scaling_max_freq);
217 [ # # ]: 0 : if (ret < 0) {
218 : 0 : POWER_LOG(ERR, "Failed to read %s",
219 : : POWER_SYSFILE_SCALING_MAX_FREQ);
220 : 0 : goto out;
221 : : }
222 : :
223 : 0 : ret = read_core_sysfs_u32(f_min, &scaling_min_freq);
224 [ # # ]: 0 : if (ret < 0) {
225 : 0 : POWER_LOG(ERR, "Failed to read %s",
226 : : POWER_SYSFILE_SCALING_MIN_FREQ);
227 : 0 : goto out;
228 : : }
229 : :
230 : 0 : power_check_turbo(pi);
231 : :
232 [ # # ]: 0 : if (scaling_max_freq < scaling_min_freq)
233 : 0 : goto out;
234 : :
235 : : /* If turbo is available then there is one extra freq bucket
236 : : * to store the sys max freq which value is scaling_max_freq
237 : : */
238 : 0 : nominal_perf = (pi->nominal_perf < UNIT_DIFF) ?
239 [ # # ]: 0 : pi->nominal_perf * UNIT_DIFF : pi->nominal_perf;
240 : 0 : num_freqs = (nominal_perf - scaling_min_freq) / BUS_FREQ + 1 +
241 : 0 : pi->turbo_available;
242 [ # # ]: 0 : if (num_freqs >= RTE_MAX_LCORE_FREQS) {
243 : 0 : POWER_LOG(ERR, "Too many available frequencies: %d",
244 : : num_freqs);
245 : 0 : goto out;
246 : : }
247 : :
248 : : /* Generate the freq bucket array. */
249 [ # # ]: 0 : for (i = 0, pi->nb_freqs = 0; i < num_freqs; i++) {
250 [ # # # # ]: 0 : if ((i == 0) && pi->turbo_available)
251 : 0 : pi->freqs[pi->nb_freqs++] = scaling_max_freq;
252 : : else
253 : 0 : pi->freqs[pi->nb_freqs++] =
254 : 0 : nominal_perf - (i - pi->turbo_available) * BUS_FREQ;
255 : : }
256 : :
257 : : ret = 0;
258 : :
259 : : POWER_DEBUG_LOG("%d frequency(s) of lcore %u are available",
260 : : num_freqs, pi->lcore_id);
261 : :
262 : 0 : out:
263 [ # # ]: 0 : if (f_min != NULL)
264 : 0 : fclose(f_min);
265 [ # # ]: 0 : if (f_max != NULL)
266 : 0 : fclose(f_max);
267 : :
268 : 0 : return ret;
269 : : }
270 : :
271 : : /**
272 : : * It is to fopen the sys file for the future setting the lcore frequency.
273 : : */
274 : : static int
275 : 0 : power_init_for_setting_freq(struct cppc_power_info *pi)
276 : : {
277 : 0 : FILE *f = NULL;
278 : : char buf[BUFSIZ];
279 : : uint32_t i, freq;
280 : : int ret;
281 : :
282 : 0 : open_core_sysfs_file(&f, "rw+", POWER_SYSFILE_SETSPEED, pi->lcore_id);
283 [ # # ]: 0 : if (f == NULL) {
284 : 0 : POWER_LOG(ERR, "failed to open %s",
285 : : POWER_SYSFILE_SETSPEED);
286 : 0 : goto err;
287 : : }
288 : :
289 : 0 : ret = read_core_sysfs_s(f, buf, sizeof(buf));
290 [ # # ]: 0 : if (ret < 0) {
291 : 0 : POWER_LOG(ERR, "Failed to read %s",
292 : : POWER_SYSFILE_SETSPEED);
293 : 0 : goto err;
294 : : }
295 : :
296 : 0 : freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
297 : :
298 : : /* convert the frequency to nearest 100000 value
299 : : * Ex: if freq=1396789 then freq_conv=1400000
300 : : * Ex: if freq=800030 then freq_conv=800000
301 : : */
302 : : unsigned int freq_conv = 0;
303 : 0 : freq_conv = (freq + FREQ_ROUNDING_DELTA)
304 : : / ROUND_FREQ_TO_N_100000;
305 : 0 : freq_conv = freq_conv * ROUND_FREQ_TO_N_100000;
306 : :
307 [ # # ]: 0 : for (i = 0; i < pi->nb_freqs; i++) {
308 [ # # ]: 0 : if (freq_conv == pi->freqs[i]) {
309 : 0 : pi->curr_idx = i;
310 : 0 : pi->f = f;
311 : 0 : return 0;
312 : : }
313 : : }
314 : :
315 : 0 : err:
316 [ # # ]: 0 : if (f != NULL)
317 : 0 : fclose(f);
318 : :
319 : : return -1;
320 : : }
321 : :
322 : : int
323 : 0 : power_cppc_cpufreq_check_supported(void)
324 : : {
325 : 0 : return cpufreq_check_scaling_driver(POWER_CPPC_DRIVER);
326 : : }
327 : :
328 : : int
329 : 0 : power_cppc_cpufreq_init(unsigned int lcore_id)
330 : : {
331 : : struct cppc_power_info *pi;
332 : : uint32_t exp_state;
333 : :
334 [ # # ]: 0 : if (!power_cppc_cpufreq_check_supported()) {
335 : 0 : POWER_LOG(ERR, "%s driver is not supported",
336 : : POWER_CPPC_DRIVER);
337 : 0 : return -1;
338 : : }
339 : :
340 : 0 : pi = &lcore_power_info[lcore_id];
341 : : exp_state = POWER_IDLE;
342 : : /* The power in use state works as a guard variable between
343 : : * the CPU frequency control initialization and exit process.
344 : : * The ACQUIRE memory ordering here pairs with the RELEASE
345 : : * ordering below as lock to make sure the frequency operations
346 : : * in the critical section are done under the correct state.
347 : : */
348 [ # # ]: 0 : if (!rte_atomic_compare_exchange_strong_explicit(&(pi->state), &exp_state,
349 : : POWER_ONGOING,
350 : : rte_memory_order_acquire, rte_memory_order_relaxed)) {
351 : 0 : POWER_LOG(INFO, "Power management of lcore %u is "
352 : : "in use", lcore_id);
353 : 0 : return -1;
354 : : }
355 : :
356 [ # # ]: 0 : if (power_get_lcore_mapped_cpu_id(lcore_id, &pi->lcore_id) < 0) {
357 : 0 : POWER_LOG(ERR, "Cannot get CPU ID mapped for lcore %u", lcore_id);
358 : 0 : return -1;
359 : : }
360 : :
361 : : /* Check and set the governor */
362 [ # # ]: 0 : if (power_set_governor_userspace(pi) < 0) {
363 : 0 : POWER_LOG(ERR, "Cannot set governor of lcore %u to "
364 : : "userspace", lcore_id);
365 : 0 : goto fail;
366 : : }
367 : :
368 : : /* Get the available frequencies */
369 [ # # ]: 0 : if (power_get_available_freqs(pi) < 0) {
370 : 0 : POWER_LOG(ERR, "Cannot get available frequencies of "
371 : : "lcore %u", lcore_id);
372 : 0 : goto fail;
373 : : }
374 : :
375 : : /* Init for setting lcore frequency */
376 [ # # ]: 0 : if (power_init_for_setting_freq(pi) < 0) {
377 : 0 : POWER_LOG(ERR, "Cannot init for setting frequency for "
378 : : "lcore %u", lcore_id);
379 : 0 : goto fail;
380 : : }
381 : :
382 : : /* Set freq to max by default */
383 [ # # ]: 0 : if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
384 : 0 : POWER_LOG(ERR, "Cannot set frequency of lcore %u "
385 : : "to max", lcore_id);
386 : 0 : goto fail;
387 : : }
388 : :
389 : 0 : POWER_LOG(INFO, "Initialized successfully for lcore %u "
390 : : "power management", lcore_id);
391 : :
392 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_USED, rte_memory_order_release);
393 : :
394 : 0 : return 0;
395 : :
396 : 0 : fail:
397 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_UNKNOWN, rte_memory_order_release);
398 : 0 : return -1;
399 : : }
400 : :
401 : : /**
402 : : * It is to check the governor and then set the original governor back if
403 : : * needed by writing the sys file.
404 : : */
405 : : static int
406 : : power_set_governor_original(struct cppc_power_info *pi)
407 : : {
408 : 0 : return power_set_governor(pi->lcore_id, pi->governor_ori, NULL, 0);
409 : : }
410 : :
411 : : int
412 : 0 : power_cppc_cpufreq_exit(unsigned int lcore_id)
413 : : {
414 : : struct cppc_power_info *pi;
415 : : uint32_t exp_state;
416 : :
417 : : pi = &lcore_power_info[lcore_id];
418 : : exp_state = POWER_USED;
419 : : /* The power in use state works as a guard variable between
420 : : * the CPU frequency control initialization and exit process.
421 : : * The ACQUIRE memory ordering here pairs with the RELEASE
422 : : * ordering below as lock to make sure the frequency operations
423 : : * in the critical section are done under the correct state.
424 : : */
425 [ # # ]: 0 : if (!rte_atomic_compare_exchange_strong_explicit(&(pi->state), &exp_state,
426 : : POWER_ONGOING,
427 : : rte_memory_order_acquire, rte_memory_order_relaxed)) {
428 : 0 : POWER_LOG(INFO, "Power management of lcore %u is "
429 : : "not used", lcore_id);
430 : 0 : return -1;
431 : : }
432 : :
433 : : /* Close FD of setting freq */
434 : 0 : fclose(pi->f);
435 : 0 : pi->f = NULL;
436 : :
437 : : /* Set the governor back to the original */
438 [ # # ]: 0 : if (power_set_governor_original(pi) < 0) {
439 : 0 : POWER_LOG(ERR, "Cannot set the governor of %u back "
440 : : "to the original", lcore_id);
441 : 0 : goto fail;
442 : : }
443 : :
444 : 0 : POWER_LOG(INFO, "Power management of lcore %u has exited from "
445 : : "'userspace' mode and been set back to the "
446 : : "original", lcore_id);
447 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_IDLE, rte_memory_order_release);
448 : :
449 : 0 : return 0;
450 : :
451 : : fail:
452 : 0 : rte_atomic_store_explicit(&(pi->state), POWER_UNKNOWN, rte_memory_order_release);
453 : :
454 : 0 : return -1;
455 : : }
456 : :
457 : : uint32_t
458 : 0 : power_cppc_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
459 : : {
460 : : struct cppc_power_info *pi;
461 : :
462 [ # # ]: 0 : if (freqs == NULL) {
463 : 0 : POWER_LOG(ERR, "NULL buffer supplied");
464 : 0 : return 0;
465 : : }
466 : :
467 : : pi = &lcore_power_info[lcore_id];
468 [ # # ]: 0 : if (num < pi->nb_freqs) {
469 : 0 : POWER_LOG(ERR, "Buffer size is not enough");
470 : 0 : return 0;
471 : : }
472 [ # # ]: 0 : rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
473 : :
474 : 0 : return pi->nb_freqs;
475 : : }
476 : :
477 : : uint32_t
478 : 0 : power_cppc_cpufreq_get_freq(unsigned int lcore_id)
479 : : {
480 : 0 : return lcore_power_info[lcore_id].curr_idx;
481 : : }
482 : :
483 : : int
484 : 0 : power_cppc_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
485 : : {
486 : 0 : return set_freq_internal(&(lcore_power_info[lcore_id]), index);
487 : : }
488 : :
489 : : int
490 : 0 : power_cppc_cpufreq_freq_down(unsigned int lcore_id)
491 : : {
492 : : struct cppc_power_info *pi;
493 : :
494 : 0 : pi = &lcore_power_info[lcore_id];
495 [ # # ]: 0 : if (pi->curr_idx + 1 == pi->nb_freqs)
496 : : return 0;
497 : :
498 : : /* Frequencies in the array are from high to low. */
499 : 0 : return set_freq_internal(pi, pi->curr_idx + 1);
500 : : }
501 : :
502 : : int
503 : 0 : power_cppc_cpufreq_freq_up(unsigned int lcore_id)
504 : : {
505 : : struct cppc_power_info *pi;
506 : :
507 : 0 : pi = &lcore_power_info[lcore_id];
508 [ # # # # ]: 0 : if (pi->curr_idx == 0 || (pi->curr_idx == 1 &&
509 [ # # # # ]: 0 : pi->turbo_available && !pi->turbo_enable))
510 : : return 0;
511 : :
512 : : /* Frequencies in the array are from high to low. */
513 : 0 : return set_freq_internal(pi, pi->curr_idx - 1);
514 : : }
515 : :
516 : : int
517 : 0 : power_cppc_cpufreq_freq_max(unsigned int lcore_id)
518 : : {
519 : : /* Frequencies in the array are from high to low. */
520 [ # # ]: 0 : if (lcore_power_info[lcore_id].turbo_available) {
521 [ # # ]: 0 : if (lcore_power_info[lcore_id].turbo_enable)
522 : : /* Set to Turbo */
523 : 0 : return set_freq_internal(
524 : : &lcore_power_info[lcore_id], 0);
525 : : else
526 : : /* Set to max non-turbo */
527 : 0 : return set_freq_internal(
528 : : &lcore_power_info[lcore_id], 1);
529 : : } else
530 : 0 : return set_freq_internal(&lcore_power_info[lcore_id], 0);
531 : : }
532 : :
533 : : int
534 : 0 : power_cppc_cpufreq_freq_min(unsigned int lcore_id)
535 : : {
536 : : struct cppc_power_info *pi;
537 : :
538 : 0 : pi = &lcore_power_info[lcore_id];
539 : :
540 : : /* Frequencies in the array are from high to low. */
541 : 0 : return set_freq_internal(pi, pi->nb_freqs - 1);
542 : : }
543 : :
544 : : int
545 : 0 : power_cppc_turbo_status(unsigned int lcore_id)
546 : : {
547 : : struct cppc_power_info *pi;
548 : :
549 : : pi = &lcore_power_info[lcore_id];
550 : :
551 : 0 : return pi->turbo_enable;
552 : : }
553 : :
554 : : int
555 : 0 : power_cppc_enable_turbo(unsigned int lcore_id)
556 : : {
557 : : struct cppc_power_info *pi;
558 : :
559 : : pi = &lcore_power_info[lcore_id];
560 : :
561 [ # # ]: 0 : if (pi->turbo_available)
562 : 0 : pi->turbo_enable = 1;
563 : : else {
564 : 0 : pi->turbo_enable = 0;
565 : 0 : POWER_LOG(ERR,
566 : : "Failed to enable turbo on lcore %u",
567 : : lcore_id);
568 : 0 : return -1;
569 : : }
570 : :
571 : : /* TODO: must set to max once enabling Turbo? Considering add condition:
572 : : * if ((pi->turbo_available) && (pi->curr_idx <= 1))
573 : : */
574 : : /* Max may have changed, so call to max function */
575 [ # # ]: 0 : if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
576 : 0 : POWER_LOG(ERR,
577 : : "Failed to set frequency of lcore %u to max",
578 : : lcore_id);
579 : 0 : return -1;
580 : : }
581 : :
582 : : return 0;
583 : : }
584 : :
585 : : int
586 : 0 : power_cppc_disable_turbo(unsigned int lcore_id)
587 : : {
588 : : struct cppc_power_info *pi;
589 : :
590 : : pi = &lcore_power_info[lcore_id];
591 : :
592 : 0 : pi->turbo_enable = 0;
593 : :
594 [ # # # # ]: 0 : if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
595 : : /* Try to set freq to max by default coming out of turbo */
596 [ # # ]: 0 : if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
597 : 0 : POWER_LOG(ERR,
598 : : "Failed to set frequency of lcore %u to max",
599 : : lcore_id);
600 : 0 : return -1;
601 : : }
602 : : }
603 : :
604 : : return 0;
605 : : }
606 : :
607 : : int
608 : 0 : power_cppc_get_capabilities(unsigned int lcore_id,
609 : : struct rte_power_core_capabilities *caps)
610 : : {
611 : : struct cppc_power_info *pi;
612 : :
613 [ # # ]: 0 : if (caps == NULL) {
614 : 0 : POWER_LOG(ERR, "Invalid argument");
615 : 0 : return -1;
616 : : }
617 : :
618 : : pi = &lcore_power_info[lcore_id];
619 : 0 : caps->capabilities = 0;
620 : 0 : caps->turbo = !!(pi->turbo_available);
621 : :
622 : 0 : return 0;
623 : : }
624 : :
625 : : static struct rte_power_cpufreq_ops cppc_ops = {
626 : : .name = "cppc",
627 : : .init = power_cppc_cpufreq_init,
628 : : .exit = power_cppc_cpufreq_exit,
629 : : .check_env_support = power_cppc_cpufreq_check_supported,
630 : : .get_avail_freqs = power_cppc_cpufreq_freqs,
631 : : .get_freq = power_cppc_cpufreq_get_freq,
632 : : .set_freq = power_cppc_cpufreq_set_freq,
633 : : .freq_down = power_cppc_cpufreq_freq_down,
634 : : .freq_up = power_cppc_cpufreq_freq_up,
635 : : .freq_max = power_cppc_cpufreq_freq_max,
636 : : .freq_min = power_cppc_cpufreq_freq_min,
637 : : .turbo_status = power_cppc_turbo_status,
638 : : .enable_turbo = power_cppc_enable_turbo,
639 : : .disable_turbo = power_cppc_disable_turbo,
640 : : .get_caps = power_cppc_get_capabilities
641 : : };
642 : :
643 : 303 : RTE_POWER_REGISTER_CPUFREQ_OPS(cppc_ops);
|