lib: fix log typos
[dpdk.git] / lib / librte_power / rte_power.c
index a05fbef..6b77227 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
-#include <rte_atomic.h>
+#include <rte_spinlock.h>
 
 #include "rte_power.h"
 #include "power_acpi_cpufreq.h"
@@ -12,7 +12,7 @@
 
 enum power_management_env global_default_env = PM_ENV_NOT_SET;
 
-volatile uint32_t global_env_cfg_status = 0;
+static rte_spinlock_t global_env_cfg_lock = RTE_SPINLOCK_INITIALIZER;
 
 /* function pointers */
 rte_power_freqs_t rte_power_freqs  = NULL;
@@ -27,12 +27,35 @@ rte_power_freq_change_t rte_power_freq_enable_turbo;
 rte_power_freq_change_t rte_power_freq_disable_turbo;
 rte_power_get_capabilities_t rte_power_get_capabilities;
 
+static void
+reset_power_function_ptrs(void)
+{
+       rte_power_freqs  = NULL;
+       rte_power_get_freq = NULL;
+       rte_power_set_freq = NULL;
+       rte_power_freq_up = NULL;
+       rte_power_freq_down = NULL;
+       rte_power_freq_max = NULL;
+       rte_power_freq_min = NULL;
+       rte_power_turbo_status = NULL;
+       rte_power_freq_enable_turbo = NULL;
+       rte_power_freq_disable_turbo = NULL;
+       rte_power_get_capabilities = NULL;
+}
+
 int
 rte_power_set_env(enum power_management_env env)
 {
-       if (rte_atomic32_cmpset(&global_env_cfg_status, 0, 1) == 0) {
-               return 0;
+       rte_spinlock_lock(&global_env_cfg_lock);
+
+       if (global_default_env != PM_ENV_NOT_SET) {
+               RTE_LOG(ERR, POWER, "Power Management Environment already set.\n");
+               rte_spinlock_unlock(&global_env_cfg_lock);
+               return -1;
        }
+
+       int ret = 0;
+
        if (env == PM_ENV_ACPI_CPUFREQ) {
                rte_power_freqs = power_acpi_cpufreq_freqs;
                rte_power_get_freq = power_acpi_cpufreq_get_freq;
@@ -73,18 +96,27 @@ rte_power_set_env(enum power_management_env env)
        } else {
                RTE_LOG(ERR, POWER, "Invalid Power Management Environment(%d) set\n",
                                env);
-               rte_power_unset_env();
-               return -1;
+               ret = -1;
+       }
+
+       if (ret == 0)
+               global_default_env = env;
+       else {
+               global_default_env = PM_ENV_NOT_SET;
+               reset_power_function_ptrs();
        }
-       global_default_env = env;
-       return 0;
+
+       rte_spinlock_unlock(&global_env_cfg_lock);
+       return ret;
 }
 
 void
 rte_power_unset_env(void)
 {
-       if (rte_atomic32_cmpset(&global_env_cfg_status, 1, 0) != 0)
-               global_default_env = PM_ENV_NOT_SET;
+       rte_spinlock_lock(&global_env_cfg_lock);
+       global_default_env = PM_ENV_NOT_SET;
+       reset_power_function_ptrs();
+       rte_spinlock_unlock(&global_env_cfg_lock);
 }
 
 enum power_management_env