LCOV - code coverage report
Current view: top level - drivers/power/cppc - cppc_cpufreq.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 7 262 2.7 %
Date: 2026-07-01 18:02:41 Functions: 3 19 15.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 144 0.7 %

           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                 :          2 : power_cppc_cpufreq_check_supported(void)
     324                 :            : {
     325                 :          2 :         return cpufreq_check_scaling_driver(POWER_CPPC_DRIVER);
     326                 :            : }
     327                 :            : 
     328                 :            : int
     329                 :          2 : power_cppc_cpufreq_init(unsigned int lcore_id)
     330                 :            : {
     331                 :            :         struct cppc_power_info *pi;
     332                 :            :         uint32_t exp_state;
     333                 :            : 
     334         [ +  - ]:          2 :         if (!power_cppc_cpufreq_check_supported()) {
     335                 :          2 :                 POWER_LOG(ERR, "%s driver is not supported",
     336                 :            :                                 POWER_CPPC_DRIVER);
     337                 :          2 :                 return -1;
     338                 :            :         }
     339                 :            : 
     340         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     341                 :          0 :                 POWER_LOG(ERR, "Lcore id %u can not exceeds %u",
     342                 :            :                                 lcore_id, RTE_MAX_LCORE - 1U);
     343                 :          0 :                 return -1;
     344                 :            :         }
     345                 :            : 
     346                 :          0 :         pi = &lcore_power_info[lcore_id];
     347                 :            :         exp_state = POWER_IDLE;
     348                 :            :         /* The power in use state works as a guard variable between
     349                 :            :          * the CPU frequency control initialization and exit process.
     350                 :            :          * The ACQUIRE memory ordering here pairs with the RELEASE
     351                 :            :          * ordering below as lock to make sure the frequency operations
     352                 :            :          * in the critical section are done under the correct state.
     353                 :            :          */
     354         [ #  # ]:          0 :         if (!rte_atomic_compare_exchange_strong_explicit(&(pi->state), &exp_state,
     355                 :            :                                         POWER_ONGOING,
     356                 :            :                                         rte_memory_order_acquire, rte_memory_order_relaxed)) {
     357                 :          0 :                 POWER_LOG(INFO, "Power management of lcore %u is "
     358                 :            :                                 "in use", lcore_id);
     359                 :          0 :                 return -1;
     360                 :            :         }
     361                 :            : 
     362         [ #  # ]:          0 :         if (power_get_lcore_mapped_cpu_id(lcore_id, &pi->lcore_id) < 0) {
     363                 :          0 :                 POWER_LOG(ERR, "Cannot get CPU ID mapped for lcore %u", lcore_id);
     364                 :          0 :                 return -1;
     365                 :            :         }
     366                 :            : 
     367                 :            :         /* Check and set the governor */
     368         [ #  # ]:          0 :         if (power_set_governor_userspace(pi) < 0) {
     369                 :          0 :                 POWER_LOG(ERR, "Cannot set governor of lcore %u to "
     370                 :            :                                 "userspace", lcore_id);
     371                 :          0 :                 goto fail;
     372                 :            :         }
     373                 :            : 
     374                 :            :         /* Get the available frequencies */
     375         [ #  # ]:          0 :         if (power_get_available_freqs(pi) < 0) {
     376                 :          0 :                 POWER_LOG(ERR, "Cannot get available frequencies of "
     377                 :            :                                 "lcore %u", lcore_id);
     378                 :          0 :                 goto fail;
     379                 :            :         }
     380                 :            : 
     381                 :            :         /* Init for setting lcore frequency */
     382         [ #  # ]:          0 :         if (power_init_for_setting_freq(pi) < 0) {
     383                 :          0 :                 POWER_LOG(ERR, "Cannot init for setting frequency for "
     384                 :            :                                 "lcore %u", lcore_id);
     385                 :          0 :                 goto fail;
     386                 :            :         }
     387                 :            : 
     388                 :            :         /* Set freq to max by default */
     389         [ #  # ]:          0 :         if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
     390                 :          0 :                 POWER_LOG(ERR, "Cannot set frequency of lcore %u "
     391                 :            :                                 "to max", lcore_id);
     392                 :          0 :                 goto fail;
     393                 :            :         }
     394                 :            : 
     395                 :          0 :         POWER_LOG(INFO, "Initialized successfully for lcore %u "
     396                 :            :                         "power management", lcore_id);
     397                 :            : 
     398                 :          0 :         rte_atomic_store_explicit(&(pi->state), POWER_USED, rte_memory_order_release);
     399                 :            : 
     400                 :          0 :         return 0;
     401                 :            : 
     402                 :          0 : fail:
     403                 :          0 :         rte_atomic_store_explicit(&(pi->state), POWER_UNKNOWN, rte_memory_order_release);
     404                 :          0 :         return -1;
     405                 :            : }
     406                 :            : 
     407                 :            : /**
     408                 :            :  * It is to check the governor and then set the original governor back if
     409                 :            :  * needed by writing the sys file.
     410                 :            :  */
     411                 :            : static int
     412                 :            : power_set_governor_original(struct cppc_power_info *pi)
     413                 :            : {
     414                 :          0 :         return power_set_governor(pi->lcore_id, pi->governor_ori, NULL, 0);
     415                 :            : }
     416                 :            : 
     417                 :            : int
     418                 :          0 : power_cppc_cpufreq_exit(unsigned int lcore_id)
     419                 :            : {
     420                 :            :         struct cppc_power_info *pi;
     421                 :            :         uint32_t exp_state;
     422                 :            : 
     423         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     424                 :          0 :                 POWER_LOG(ERR, "Lcore id %u can not exceeds %u",
     425                 :            :                                 lcore_id, RTE_MAX_LCORE - 1U);
     426                 :          0 :                 return -1;
     427                 :            :         }
     428                 :            :         pi = &lcore_power_info[lcore_id];
     429                 :            :         exp_state = POWER_USED;
     430                 :            :         /* The power in use state works as a guard variable between
     431                 :            :          * the CPU frequency control initialization and exit process.
     432                 :            :          * The ACQUIRE memory ordering here pairs with the RELEASE
     433                 :            :          * ordering below as lock to make sure the frequency operations
     434                 :            :          * in the critical section are done under the correct state.
     435                 :            :          */
     436         [ #  # ]:          0 :         if (!rte_atomic_compare_exchange_strong_explicit(&(pi->state), &exp_state,
     437                 :            :                                         POWER_ONGOING,
     438                 :            :                                         rte_memory_order_acquire, rte_memory_order_relaxed)) {
     439                 :          0 :                 POWER_LOG(INFO, "Power management of lcore %u is "
     440                 :            :                                 "not used", lcore_id);
     441                 :          0 :                 return -1;
     442                 :            :         }
     443                 :            : 
     444                 :            :         /* Close FD of setting freq */
     445                 :          0 :         fclose(pi->f);
     446                 :          0 :         pi->f = NULL;
     447                 :            : 
     448                 :            :         /* Set the governor back to the original */
     449         [ #  # ]:          0 :         if (power_set_governor_original(pi) < 0) {
     450                 :          0 :                 POWER_LOG(ERR, "Cannot set the governor of %u back "
     451                 :            :                                 "to the original", lcore_id);
     452                 :          0 :                 goto fail;
     453                 :            :         }
     454                 :            : 
     455                 :          0 :         POWER_LOG(INFO, "Power management of lcore %u has exited from "
     456                 :            :                         "'userspace' mode and been set back to the "
     457                 :            :                         "original", lcore_id);
     458                 :          0 :         rte_atomic_store_explicit(&(pi->state), POWER_IDLE, rte_memory_order_release);
     459                 :            : 
     460                 :          0 :         return 0;
     461                 :            : 
     462                 :            : fail:
     463                 :          0 :         rte_atomic_store_explicit(&(pi->state), POWER_UNKNOWN, rte_memory_order_release);
     464                 :            : 
     465                 :          0 :         return -1;
     466                 :            : }
     467                 :            : 
     468                 :            : uint32_t
     469                 :          0 : power_cppc_cpufreq_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t num)
     470                 :            : {
     471                 :            :         struct cppc_power_info *pi;
     472                 :            : 
     473         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     474                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     475                 :          0 :                 return 0;
     476                 :            :         }
     477                 :            : 
     478         [ #  # ]:          0 :         if (freqs == NULL) {
     479                 :          0 :                 POWER_LOG(ERR, "NULL buffer supplied");
     480                 :          0 :                 return 0;
     481                 :            :         }
     482                 :            : 
     483                 :            :         pi = &lcore_power_info[lcore_id];
     484         [ #  # ]:          0 :         if (num < pi->nb_freqs) {
     485                 :          0 :                 POWER_LOG(ERR, "Buffer size is not enough");
     486                 :          0 :                 return 0;
     487                 :            :         }
     488         [ #  # ]:          0 :         rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
     489                 :            : 
     490                 :          0 :         return pi->nb_freqs;
     491                 :            : }
     492                 :            : 
     493                 :            : uint32_t
     494                 :          0 : power_cppc_cpufreq_get_freq(unsigned int lcore_id)
     495                 :            : {
     496         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     497                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     498                 :          0 :                 return RTE_POWER_INVALID_FREQ_INDEX;
     499                 :            :         }
     500                 :            : 
     501                 :          0 :         return lcore_power_info[lcore_id].curr_idx;
     502                 :            : }
     503                 :            : 
     504                 :            : int
     505                 :          0 : power_cppc_cpufreq_set_freq(unsigned int lcore_id, uint32_t index)
     506                 :            : {
     507         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     508                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     509                 :          0 :                 return -1;
     510                 :            :         }
     511                 :            : 
     512                 :          0 :         return set_freq_internal(&(lcore_power_info[lcore_id]), index);
     513                 :            : }
     514                 :            : 
     515                 :            : int
     516                 :          0 : power_cppc_cpufreq_freq_down(unsigned int lcore_id)
     517                 :            : {
     518                 :            :         struct cppc_power_info *pi;
     519                 :            : 
     520         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     521                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     522                 :          0 :                 return -1;
     523                 :            :         }
     524                 :            : 
     525                 :          0 :         pi = &lcore_power_info[lcore_id];
     526         [ #  # ]:          0 :         if (pi->curr_idx + 1 == pi->nb_freqs)
     527                 :            :                 return 0;
     528                 :            : 
     529                 :            :         /* Frequencies in the array are from high to low. */
     530                 :          0 :         return set_freq_internal(pi, pi->curr_idx + 1);
     531                 :            : }
     532                 :            : 
     533                 :            : int
     534                 :          0 : power_cppc_cpufreq_freq_up(unsigned int lcore_id)
     535                 :            : {
     536                 :            :         struct cppc_power_info *pi;
     537                 :            : 
     538         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     539                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     540                 :          0 :                 return -1;
     541                 :            :         }
     542                 :            : 
     543                 :          0 :         pi = &lcore_power_info[lcore_id];
     544   [ #  #  #  # ]:          0 :         if (pi->curr_idx == 0 || (pi->curr_idx == 1 &&
     545   [ #  #  #  # ]:          0 :                 pi->turbo_available && !pi->turbo_enable))
     546                 :            :                 return 0;
     547                 :            : 
     548                 :            :         /* Frequencies in the array are from high to low. */
     549                 :          0 :         return set_freq_internal(pi, pi->curr_idx - 1);
     550                 :            : }
     551                 :            : 
     552                 :            : int
     553                 :          0 : power_cppc_cpufreq_freq_max(unsigned int lcore_id)
     554                 :            : {
     555         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     556                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     557                 :          0 :                 return -1;
     558                 :            :         }
     559                 :            : 
     560                 :            :         /* Frequencies in the array are from high to low. */
     561         [ #  # ]:          0 :         if (lcore_power_info[lcore_id].turbo_available) {
     562         [ #  # ]:          0 :                 if (lcore_power_info[lcore_id].turbo_enable)
     563                 :            :                         /* Set to Turbo */
     564                 :          0 :                         return set_freq_internal(
     565                 :            :                                 &lcore_power_info[lcore_id], 0);
     566                 :            :                 else
     567                 :            :                         /* Set to max non-turbo */
     568                 :          0 :                         return set_freq_internal(
     569                 :            :                                 &lcore_power_info[lcore_id], 1);
     570                 :            :         } else
     571                 :          0 :                 return set_freq_internal(&lcore_power_info[lcore_id], 0);
     572                 :            : }
     573                 :            : 
     574                 :            : int
     575                 :          0 : power_cppc_cpufreq_freq_min(unsigned int lcore_id)
     576                 :            : {
     577                 :            :         struct cppc_power_info *pi;
     578                 :            : 
     579         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     580                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     581                 :          0 :                 return -1;
     582                 :            :         }
     583                 :            : 
     584                 :          0 :         pi = &lcore_power_info[lcore_id];
     585                 :            : 
     586                 :            :         /* Frequencies in the array are from high to low. */
     587                 :          0 :         return set_freq_internal(pi, pi->nb_freqs - 1);
     588                 :            : }
     589                 :            : 
     590                 :            : int
     591                 :          0 : power_cppc_turbo_status(unsigned int lcore_id)
     592                 :            : {
     593                 :            :         struct cppc_power_info *pi;
     594                 :            : 
     595         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     596                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     597                 :          0 :                 return -1;
     598                 :            :         }
     599                 :            : 
     600                 :            :         pi = &lcore_power_info[lcore_id];
     601                 :            : 
     602                 :          0 :         return pi->turbo_enable;
     603                 :            : }
     604                 :            : 
     605                 :            : int
     606                 :          0 : power_cppc_enable_turbo(unsigned int lcore_id)
     607                 :            : {
     608                 :            :         struct cppc_power_info *pi;
     609                 :            : 
     610         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     611                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     612                 :          0 :                 return -1;
     613                 :            :         }
     614                 :            : 
     615                 :            :         pi = &lcore_power_info[lcore_id];
     616                 :            : 
     617         [ #  # ]:          0 :         if (pi->turbo_available)
     618                 :          0 :                 pi->turbo_enable = 1;
     619                 :            :         else {
     620                 :          0 :                 pi->turbo_enable = 0;
     621                 :          0 :                 POWER_LOG(ERR,
     622                 :            :                         "Failed to enable turbo on lcore %u",
     623                 :            :                         lcore_id);
     624                 :          0 :                 return -1;
     625                 :            :         }
     626                 :            : 
     627                 :            :         /* TODO: must set to max once enabling Turbo? Considering add condition:
     628                 :            :          * if ((pi->turbo_available) && (pi->curr_idx <= 1))
     629                 :            :          */
     630                 :            :         /* Max may have changed, so call to max function */
     631         [ #  # ]:          0 :         if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
     632                 :          0 :                 POWER_LOG(ERR,
     633                 :            :                         "Failed to set frequency of lcore %u to max",
     634                 :            :                         lcore_id);
     635                 :          0 :                 return -1;
     636                 :            :         }
     637                 :            : 
     638                 :            :         return 0;
     639                 :            : }
     640                 :            : 
     641                 :            : int
     642                 :          0 : power_cppc_disable_turbo(unsigned int lcore_id)
     643                 :            : {
     644                 :            :         struct cppc_power_info *pi;
     645                 :            : 
     646         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     647                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     648                 :          0 :                 return -1;
     649                 :            :         }
     650                 :            : 
     651                 :            :         pi = &lcore_power_info[lcore_id];
     652                 :            : 
     653                 :          0 :         pi->turbo_enable = 0;
     654                 :            : 
     655   [ #  #  #  # ]:          0 :         if ((pi->turbo_available) && (pi->curr_idx <= 1)) {
     656                 :            :                 /* Try to set freq to max by default coming out of turbo */
     657         [ #  # ]:          0 :                 if (power_cppc_cpufreq_freq_max(lcore_id) < 0) {
     658                 :          0 :                         POWER_LOG(ERR,
     659                 :            :                                 "Failed to set frequency of lcore %u to max",
     660                 :            :                                 lcore_id);
     661                 :          0 :                         return -1;
     662                 :            :                 }
     663                 :            :         }
     664                 :            : 
     665                 :            :         return 0;
     666                 :            : }
     667                 :            : 
     668                 :            : int
     669                 :          0 : power_cppc_get_capabilities(unsigned int lcore_id,
     670                 :            :                 struct rte_power_core_capabilities *caps)
     671                 :            : {
     672                 :            :         struct cppc_power_info *pi;
     673                 :            : 
     674         [ #  # ]:          0 :         if (lcore_id >= RTE_MAX_LCORE) {
     675                 :          0 :                 POWER_LOG(ERR, "Invalid lcore ID");
     676                 :          0 :                 return -1;
     677                 :            :         }
     678         [ #  # ]:          0 :         if (caps == NULL) {
     679                 :          0 :                 POWER_LOG(ERR, "Invalid argument");
     680                 :          0 :                 return -1;
     681                 :            :         }
     682                 :            : 
     683                 :            :         pi = &lcore_power_info[lcore_id];
     684                 :          0 :         caps->capabilities = 0;
     685                 :          0 :         caps->turbo = !!(pi->turbo_available);
     686                 :            : 
     687                 :          0 :         return 0;
     688                 :            : }
     689                 :            : 
     690                 :            : static struct rte_power_cpufreq_ops cppc_ops = {
     691                 :            :         .name = "cppc",
     692                 :            :         .init = power_cppc_cpufreq_init,
     693                 :            :         .exit = power_cppc_cpufreq_exit,
     694                 :            :         .check_env_support = power_cppc_cpufreq_check_supported,
     695                 :            :         .get_avail_freqs = power_cppc_cpufreq_freqs,
     696                 :            :         .get_freq = power_cppc_cpufreq_get_freq,
     697                 :            :         .set_freq = power_cppc_cpufreq_set_freq,
     698                 :            :         .freq_down = power_cppc_cpufreq_freq_down,
     699                 :            :         .freq_up = power_cppc_cpufreq_freq_up,
     700                 :            :         .freq_max = power_cppc_cpufreq_freq_max,
     701                 :            :         .freq_min = power_cppc_cpufreq_freq_min,
     702                 :            :         .turbo_status = power_cppc_turbo_status,
     703                 :            :         .enable_turbo = power_cppc_enable_turbo,
     704                 :            :         .disable_turbo = power_cppc_disable_turbo,
     705                 :            :         .get_caps = power_cppc_get_capabilities
     706                 :            : };
     707                 :            : 
     708                 :        301 : RTE_POWER_REGISTER_CPUFREQ_OPS(cppc_ops);

Generated by: LCOV version 1.14