Imported Upstream version 16.04
[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
54 int
55 rte_power_set_env(enum power_management_env env)
56 {
57         if (rte_atomic32_cmpset(&global_env_cfg_status, 0, 1) == 0) {
58                 return 0;
59         }
60         if (env == PM_ENV_ACPI_CPUFREQ) {
61                 rte_power_freqs = rte_power_acpi_cpufreq_freqs;
62                 rte_power_get_freq = rte_power_acpi_cpufreq_get_freq;
63                 rte_power_set_freq = rte_power_acpi_cpufreq_set_freq;
64                 rte_power_freq_up = rte_power_acpi_cpufreq_freq_up;
65                 rte_power_freq_down = rte_power_acpi_cpufreq_freq_down;
66                 rte_power_freq_min = rte_power_acpi_cpufreq_freq_min;
67                 rte_power_freq_max = rte_power_acpi_cpufreq_freq_max;
68         } else if (env == PM_ENV_KVM_VM) {
69                 rte_power_freqs = rte_power_kvm_vm_freqs;
70                 rte_power_get_freq = rte_power_kvm_vm_get_freq;
71                 rte_power_set_freq = rte_power_kvm_vm_set_freq;
72                 rte_power_freq_up = rte_power_kvm_vm_freq_up;
73                 rte_power_freq_down = rte_power_kvm_vm_freq_down;
74                 rte_power_freq_min = rte_power_kvm_vm_freq_min;
75                 rte_power_freq_max = rte_power_kvm_vm_freq_max;
76         } else {
77                 RTE_LOG(ERR, POWER, "Invalid Power Management Environment(%d) set\n",
78                                 env);
79                 rte_power_unset_env();
80                 return -1;
81         }
82         global_default_env = env;
83         return 0;
84
85 }
86
87 void
88 rte_power_unset_env(void)
89 {
90         if (rte_atomic32_cmpset(&global_env_cfg_status, 1, 0) != 0)
91                 global_default_env = PM_ENV_NOT_SET;
92 }
93
94 enum power_management_env
95 rte_power_get_env(void) {
96         return global_default_env;
97 }
98
99 int
100 rte_power_init(unsigned lcore_id)
101 {
102         int ret = -1;
103
104         if (global_default_env == PM_ENV_ACPI_CPUFREQ) {
105                 return rte_power_acpi_cpufreq_init(lcore_id);
106         }
107         if (global_default_env == PM_ENV_KVM_VM) {
108                 return rte_power_kvm_vm_init(lcore_id);
109         }
110         /* Auto detect Environment */
111         RTE_LOG(INFO, POWER, "Attempting to initialise ACPI cpufreq power "
112                         "management...\n");
113         ret = rte_power_acpi_cpufreq_init(lcore_id);
114         if (ret == 0) {
115                 rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
116                 goto out;
117         }
118
119         RTE_LOG(INFO, POWER, "Attempting to initialise VM power management...\n");
120         ret = rte_power_kvm_vm_init(lcore_id);
121         if (ret == 0) {
122                 rte_power_set_env(PM_ENV_KVM_VM);
123                 goto out;
124         }
125         RTE_LOG(ERR, POWER, "Unable to set Power Management Environment for lcore "
126                         "%u\n", lcore_id);
127 out:
128         return ret;
129 }
130
131 int
132 rte_power_exit(unsigned lcore_id)
133 {
134         if (global_default_env == PM_ENV_ACPI_CPUFREQ)
135                 return rte_power_acpi_cpufreq_exit(lcore_id);
136         if (global_default_env == PM_ENV_KVM_VM)
137                 return rte_power_kvm_vm_exit(lcore_id);
138
139         RTE_LOG(ERR, POWER, "Environment has not been set, unable to exit "
140                                 "gracefully\n");
141         return -1;
142
143 }