LCOV - code coverage report
Current view: top level - lib/power - power_cppc_cpufreq.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 13 256 5.1 %
Date: 2024-01-22 15:55:54 Functions: 1 18 5.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 3 140 2.1 %

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

Generated by: LCOV version 1.14