New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_power / rte_power.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_atomic.h>
35
36 #include "rte_power.h"
37 #include "rte_power_acpi_cpufreq.h"
38 #include "rte_power_kvm_vm.h"
39 #include "rte_power_common.h"
40
41 enum power_management_env global_default_env = PM_ENV_NOT_SET;
42
43 volatile uint32_t global_env_cfg_status = 0;
44
45 /* function pointers */
46 rte_power_freqs_t rte_power_freqs  = NULL;
47 rte_power_get_freq_t rte_power_get_freq = NULL;
48 rte_power_set_freq_t rte_power_set_freq = NULL;
49 rte_power_freq_change_t rte_power_freq_up = NULL;
50 rte_power_freq_change_t rte_power_freq_down = NULL;
51 rte_power_freq_change_t rte_power_freq_max = NULL;
52 rte_power_freq_change_t rte_power_freq_min = NULL;
53 rte_power_freq_change_t rte_power_turbo_status;
54 rte_power_freq_change_t rte_power_freq_enable_turbo;
55 rte_power_freq_change_t rte_power_freq_disable_turbo;
56
57 int
58 rte_power_set_env(enum power_management_env env)
59 {
60         if (rte_atomic32_cmpset(&global_env_cfg_status, 0, 1) == 0) {
61                 return 0;
62         }
63         if (env == PM_ENV_ACPI_CPUFREQ) {
64                 rte_power_freqs = rte_power_acpi_cpufreq_freqs;
65                 rte_power_get_freq = rte_power_acpi_cpufreq_get_freq;
66                 rte_power_set_freq = rte_power_acpi_cpufreq_set_freq;
67                 rte_power_freq_up = rte_power_acpi_cpufreq_freq_up;
68                 rte_power_freq_down = rte_power_acpi_cpufreq_freq_down;
69                 rte_power_freq_min = rte_power_acpi_cpufreq_freq_min;
70                 rte_power_freq_max = rte_power_acpi_cpufreq_freq_max;
71                 rte_power_turbo_status = rte_power_acpi_turbo_status;
72                 rte_power_freq_enable_turbo = rte_power_acpi_enable_turbo;
73                 rte_power_freq_disable_turbo = rte_power_acpi_disable_turbo;
74         } else if (env == PM_ENV_KVM_VM) {
75                 rte_power_freqs = rte_power_kvm_vm_freqs;
76                 rte_power_get_freq = rte_power_kvm_vm_get_freq;
77                 rte_power_set_freq = rte_power_kvm_vm_set_freq;
78                 rte_power_freq_up = rte_power_kvm_vm_freq_up;
79                 rte_power_freq_down = rte_power_kvm_vm_freq_down;
80                 rte_power_freq_min = rte_power_kvm_vm_freq_min;
81                 rte_power_freq_max = rte_power_kvm_vm_freq_max;
82                 rte_power_turbo_status = rte_power_kvm_vm_turbo_status;
83                 rte_power_freq_enable_turbo = rte_power_kvm_vm_enable_turbo;
84                 rte_power_freq_disable_turbo = rte_power_kvm_vm_disable_turbo;
85         } else {
86                 RTE_LOG(ERR, POWER, "Invalid Power Management Environment(%d) set\n",
87                                 env);
88                 rte_power_unset_env();
89                 return -1;
90         }
91         global_default_env = env;
92         return 0;
93
94 }
95
96 void
97 rte_power_unset_env(void)
98 {
99         if (rte_atomic32_cmpset(&global_env_cfg_status, 1, 0) != 0)
100                 global_default_env = PM_ENV_NOT_SET;
101 }
102
103 enum power_management_env
104 rte_power_get_env(void) {
105         return global_default_env;
106 }
107
108 int
109 rte_power_init(unsigned lcore_id)
110 {
111         int ret = -1;
112
113         if (global_default_env == PM_ENV_ACPI_CPUFREQ) {
114                 return rte_power_acpi_cpufreq_init(lcore_id);
115         }
116         if (global_default_env == PM_ENV_KVM_VM) {
117                 return rte_power_kvm_vm_init(lcore_id);
118         }
119         /* Auto detect Environment */
120         RTE_LOG(INFO, POWER, "Attempting to initialise ACPI cpufreq power "
121                         "management...\n");
122         ret = rte_power_acpi_cpufreq_init(lcore_id);
123         if (ret == 0) {
124                 rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
125                 goto out;
126         }
127
128         RTE_LOG(INFO, POWER, "Attempting to initialise VM power management...\n");
129         ret = rte_power_kvm_vm_init(lcore_id);
130         if (ret == 0) {
131                 rte_power_set_env(PM_ENV_KVM_VM);
132                 goto out;
133         }
134         RTE_LOG(ERR, POWER, "Unable to set Power Management Environment for lcore "
135                         "%u\n", lcore_id);
136 out:
137         return ret;
138 }
139
140 int
141 rte_power_exit(unsigned lcore_id)
142 {
143         if (global_default_env == PM_ENV_ACPI_CPUFREQ)
144                 return rte_power_acpi_cpufreq_exit(lcore_id);
145         if (global_default_env == PM_ENV_KVM_VM)
146                 return rte_power_kvm_vm_exit(lcore_id);
147
148         RTE_LOG(ERR, POWER, "Environment has not been set, unable to exit "
149                                 "gracefully\n");
150         return -1;
151
152 }