1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <rte_spinlock.h>
8 #include "power_acpi_cpufreq.h"
9 #include "power_kvm_vm.h"
10 #include "power_pstate_cpufreq.h"
11 #include "power_common.h"
13 enum power_management_env global_default_env = PM_ENV_NOT_SET;
15 static rte_spinlock_t global_env_cfg_lock = RTE_SPINLOCK_INITIALIZER;
17 /* function pointers */
18 rte_power_freqs_t rte_power_freqs = NULL;
19 rte_power_get_freq_t rte_power_get_freq = NULL;
20 rte_power_set_freq_t rte_power_set_freq = NULL;
21 rte_power_freq_change_t rte_power_freq_up = NULL;
22 rte_power_freq_change_t rte_power_freq_down = NULL;
23 rte_power_freq_change_t rte_power_freq_max = NULL;
24 rte_power_freq_change_t rte_power_freq_min = NULL;
25 rte_power_freq_change_t rte_power_turbo_status;
26 rte_power_freq_change_t rte_power_freq_enable_turbo;
27 rte_power_freq_change_t rte_power_freq_disable_turbo;
28 rte_power_get_capabilities_t rte_power_get_capabilities;
31 reset_power_function_ptrs(void)
33 rte_power_freqs = NULL;
34 rte_power_get_freq = NULL;
35 rte_power_set_freq = NULL;
36 rte_power_freq_up = NULL;
37 rte_power_freq_down = NULL;
38 rte_power_freq_max = NULL;
39 rte_power_freq_min = NULL;
40 rte_power_turbo_status = NULL;
41 rte_power_freq_enable_turbo = NULL;
42 rte_power_freq_disable_turbo = NULL;
43 rte_power_get_capabilities = NULL;
47 rte_power_set_env(enum power_management_env env)
49 rte_spinlock_lock(&global_env_cfg_lock);
51 if (global_default_env != PM_ENV_NOT_SET) {
52 RTE_LOG(ERR, POWER, "Power Management Environment already set.\n");
53 rte_spinlock_unlock(&global_env_cfg_lock);
59 if (env == PM_ENV_ACPI_CPUFREQ) {
60 rte_power_freqs = power_acpi_cpufreq_freqs;
61 rte_power_get_freq = power_acpi_cpufreq_get_freq;
62 rte_power_set_freq = power_acpi_cpufreq_set_freq;
63 rte_power_freq_up = power_acpi_cpufreq_freq_up;
64 rte_power_freq_down = power_acpi_cpufreq_freq_down;
65 rte_power_freq_min = power_acpi_cpufreq_freq_min;
66 rte_power_freq_max = power_acpi_cpufreq_freq_max;
67 rte_power_turbo_status = power_acpi_turbo_status;
68 rte_power_freq_enable_turbo = power_acpi_enable_turbo;
69 rte_power_freq_disable_turbo = power_acpi_disable_turbo;
70 rte_power_get_capabilities = power_acpi_get_capabilities;
71 } else if (env == PM_ENV_KVM_VM) {
72 rte_power_freqs = power_kvm_vm_freqs;
73 rte_power_get_freq = power_kvm_vm_get_freq;
74 rte_power_set_freq = power_kvm_vm_set_freq;
75 rte_power_freq_up = power_kvm_vm_freq_up;
76 rte_power_freq_down = power_kvm_vm_freq_down;
77 rte_power_freq_min = power_kvm_vm_freq_min;
78 rte_power_freq_max = power_kvm_vm_freq_max;
79 rte_power_turbo_status = power_kvm_vm_turbo_status;
80 rte_power_freq_enable_turbo = power_kvm_vm_enable_turbo;
81 rte_power_freq_disable_turbo = power_kvm_vm_disable_turbo;
82 rte_power_get_capabilities = power_kvm_vm_get_capabilities;
83 } else if (env == PM_ENV_PSTATE_CPUFREQ) {
84 rte_power_freqs = power_pstate_cpufreq_freqs;
85 rte_power_get_freq = power_pstate_cpufreq_get_freq;
86 rte_power_set_freq = power_pstate_cpufreq_set_freq;
87 rte_power_freq_up = power_pstate_cpufreq_freq_up;
88 rte_power_freq_down = power_pstate_cpufreq_freq_down;
89 rte_power_freq_min = power_pstate_cpufreq_freq_min;
90 rte_power_freq_max = power_pstate_cpufreq_freq_max;
91 rte_power_turbo_status = power_pstate_turbo_status;
92 rte_power_freq_enable_turbo = power_pstate_enable_turbo;
93 rte_power_freq_disable_turbo = power_pstate_disable_turbo;
94 rte_power_get_capabilities = power_pstate_get_capabilities;
97 RTE_LOG(ERR, POWER, "Invalid Power Management Environment(%d) set\n",
103 global_default_env = env;
105 global_default_env = PM_ENV_NOT_SET;
106 reset_power_function_ptrs();
109 rte_spinlock_unlock(&global_env_cfg_lock);
114 rte_power_unset_env(void)
116 rte_spinlock_lock(&global_env_cfg_lock);
117 global_default_env = PM_ENV_NOT_SET;
118 reset_power_function_ptrs();
119 rte_spinlock_unlock(&global_env_cfg_lock);
122 enum power_management_env
123 rte_power_get_env(void) {
124 return global_default_env;
128 rte_power_init(unsigned int lcore_id)
132 switch (global_default_env) {
133 case PM_ENV_ACPI_CPUFREQ:
134 return power_acpi_cpufreq_init(lcore_id);
136 return power_kvm_vm_init(lcore_id);
137 case PM_ENV_PSTATE_CPUFREQ:
138 return power_pstate_cpufreq_init(lcore_id);
140 RTE_LOG(INFO, POWER, "Env isn't set yet!\n");
143 /* Auto detect Environment */
144 RTE_LOG(INFO, POWER, "Attempting to initialise ACPI cpufreq power management...\n");
145 ret = power_acpi_cpufreq_init(lcore_id);
147 rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
151 RTE_LOG(INFO, POWER, "Attempting to initialise PSTAT power management...\n");
152 ret = power_pstate_cpufreq_init(lcore_id);
154 rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
158 RTE_LOG(INFO, POWER, "Attempting to initialise VM power management...\n");
159 ret = power_kvm_vm_init(lcore_id);
161 rte_power_set_env(PM_ENV_KVM_VM);
164 RTE_LOG(ERR, POWER, "Unable to set Power Management Environment for lcore "
171 rte_power_exit(unsigned int lcore_id)
173 switch (global_default_env) {
174 case PM_ENV_ACPI_CPUFREQ:
175 return power_acpi_cpufreq_exit(lcore_id);
177 return power_kvm_vm_exit(lcore_id);
178 case PM_ENV_PSTATE_CPUFREQ:
179 return power_pstate_cpufreq_exit(lcore_id);
181 RTE_LOG(ERR, POWER, "Environment has not been set, unable to exit gracefully\n");