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 rte_power_set_env(enum power_management_env env)
33 rte_spinlock_lock(&global_env_cfg_lock);
35 if (global_default_env != PM_ENV_NOT_SET) {
36 RTE_LOG(ERR, POWER, "Power Management Environment already set.\n");
37 rte_spinlock_unlock(&global_env_cfg_lock);
43 if (env == PM_ENV_ACPI_CPUFREQ) {
44 rte_power_freqs = power_acpi_cpufreq_freqs;
45 rte_power_get_freq = power_acpi_cpufreq_get_freq;
46 rte_power_set_freq = power_acpi_cpufreq_set_freq;
47 rte_power_freq_up = power_acpi_cpufreq_freq_up;
48 rte_power_freq_down = power_acpi_cpufreq_freq_down;
49 rte_power_freq_min = power_acpi_cpufreq_freq_min;
50 rte_power_freq_max = power_acpi_cpufreq_freq_max;
51 rte_power_turbo_status = power_acpi_turbo_status;
52 rte_power_freq_enable_turbo = power_acpi_enable_turbo;
53 rte_power_freq_disable_turbo = power_acpi_disable_turbo;
54 rte_power_get_capabilities = power_acpi_get_capabilities;
55 } else if (env == PM_ENV_KVM_VM) {
56 rte_power_freqs = power_kvm_vm_freqs;
57 rte_power_get_freq = power_kvm_vm_get_freq;
58 rte_power_set_freq = power_kvm_vm_set_freq;
59 rte_power_freq_up = power_kvm_vm_freq_up;
60 rte_power_freq_down = power_kvm_vm_freq_down;
61 rte_power_freq_min = power_kvm_vm_freq_min;
62 rte_power_freq_max = power_kvm_vm_freq_max;
63 rte_power_turbo_status = power_kvm_vm_turbo_status;
64 rte_power_freq_enable_turbo = power_kvm_vm_enable_turbo;
65 rte_power_freq_disable_turbo = power_kvm_vm_disable_turbo;
66 rte_power_get_capabilities = power_kvm_vm_get_capabilities;
67 } else if (env == PM_ENV_PSTATE_CPUFREQ) {
68 rte_power_freqs = power_pstate_cpufreq_freqs;
69 rte_power_get_freq = power_pstate_cpufreq_get_freq;
70 rte_power_set_freq = power_pstate_cpufreq_set_freq;
71 rte_power_freq_up = power_pstate_cpufreq_freq_up;
72 rte_power_freq_down = power_pstate_cpufreq_freq_down;
73 rte_power_freq_min = power_pstate_cpufreq_freq_min;
74 rte_power_freq_max = power_pstate_cpufreq_freq_max;
75 rte_power_turbo_status = power_pstate_turbo_status;
76 rte_power_freq_enable_turbo = power_pstate_enable_turbo;
77 rte_power_freq_disable_turbo = power_pstate_disable_turbo;
78 rte_power_get_capabilities = power_pstate_get_capabilities;
81 RTE_LOG(ERR, POWER, "Invalid Power Management Environment(%d) set\n",
87 global_default_env = env;
89 global_default_env = PM_ENV_NOT_SET;
91 rte_spinlock_unlock(&global_env_cfg_lock);
96 rte_power_unset_env(void)
98 rte_spinlock_lock(&global_env_cfg_lock);
99 global_default_env = PM_ENV_NOT_SET;
100 rte_spinlock_unlock(&global_env_cfg_lock);
103 enum power_management_env
104 rte_power_get_env(void) {
105 return global_default_env;
109 rte_power_init(unsigned int lcore_id)
113 switch (global_default_env) {
114 case PM_ENV_ACPI_CPUFREQ:
115 return power_acpi_cpufreq_init(lcore_id);
117 return power_kvm_vm_init(lcore_id);
118 case PM_ENV_PSTATE_CPUFREQ:
119 return power_pstate_cpufreq_init(lcore_id);
121 RTE_LOG(INFO, POWER, "Env isn't set yet!\n");
124 /* Auto detect Environment */
125 RTE_LOG(INFO, POWER, "Attempting to initialise ACPI cpufreq power management...\n");
126 ret = power_acpi_cpufreq_init(lcore_id);
128 rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
132 RTE_LOG(INFO, POWER, "Attempting to initialise PSTAT power management...\n");
133 ret = power_pstate_cpufreq_init(lcore_id);
135 rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
139 RTE_LOG(INFO, POWER, "Attempting to initialise VM power management...\n");
140 ret = power_kvm_vm_init(lcore_id);
142 rte_power_set_env(PM_ENV_KVM_VM);
145 RTE_LOG(ERR, POWER, "Unable to set Power Management Environment for lcore "
152 rte_power_exit(unsigned int lcore_id)
154 switch (global_default_env) {
155 case PM_ENV_ACPI_CPUFREQ:
156 return power_acpi_cpufreq_exit(lcore_id);
158 return power_kvm_vm_exit(lcore_id);
159 case PM_ENV_PSTATE_CPUFREQ:
160 return power_pstate_cpufreq_exit(lcore_id);
162 RTE_LOG(ERR, POWER, "Environment has not been set, unable to exit gracefully\n");