4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
44 #include <sys/types.h>
47 #include <rte_power.h>
48 #include <rte_spinlock.h>
50 #include "power_manager.h"
52 #define RTE_LOGTYPE_POWER_MANAGER RTE_LOGTYPE_USER1
54 #define POWER_SCALE_CORE(DIRECTION, core_num , ret) do { \
55 if (core_num >= POWER_MGR_MAX_CPUS) \
57 if (!(global_enabled_cpus & (1ULL << core_num))) \
59 rte_spinlock_lock(&global_core_freq_info[core_num].power_sl); \
60 ret = rte_power_freq_##DIRECTION(core_num); \
61 rte_spinlock_unlock(&global_core_freq_info[core_num].power_sl); \
64 #define POWER_SCALE_MASK(DIRECTION, core_mask, ret) do { \
66 for (i = 0; core_mask; core_mask &= ~(1 << i++)) { \
67 if ((core_mask >> i) & 1) { \
68 if (!(global_enabled_cpus & (1ULL << i))) \
70 rte_spinlock_lock(&global_core_freq_info[i].power_sl); \
71 if (rte_power_freq_##DIRECTION(i) != 1) \
73 rte_spinlock_unlock(&global_core_freq_info[i].power_sl); \
79 rte_spinlock_t power_sl;
80 uint32_t freqs[RTE_MAX_LCORE_FREQS];
82 } __rte_cache_aligned;
84 static struct freq_info global_core_freq_info[POWER_MGR_MAX_CPUS];
86 static uint64_t global_enabled_cpus;
88 #define SYSFS_CPU_PATH "/sys/devices/system/cpu/cpu%u/topology/core_id"
91 set_host_cpus_mask(void)
95 unsigned num_cpus = 0;
97 for (i = 0; i < POWER_MGR_MAX_CPUS; i++) {
98 snprintf(path, sizeof(path), SYSFS_CPU_PATH, i);
99 if (access(path, F_OK) == 0) {
100 global_enabled_cpus |= 1ULL << i;
109 power_manager_init(void)
111 unsigned i, num_cpus;
115 num_cpus = set_host_cpus_mask();
117 RTE_LOG(ERR, POWER_MANAGER, "Unable to detected host CPUs, please "
118 "ensure that sufficient privileges exist to inspect sysfs\n");
121 rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
122 cpu_mask = global_enabled_cpus;
123 for (i = 0; cpu_mask; cpu_mask &= ~(1 << i++)) {
124 if (rte_power_init(i) < 0 || rte_power_freqs(i,
125 global_core_freq_info[i].freqs,
126 RTE_MAX_LCORE_FREQS) == 0) {
127 RTE_LOG(ERR, POWER_MANAGER, "Unable to initialize power manager "
129 global_enabled_cpus &= ~(1 << i);
133 rte_spinlock_init(&global_core_freq_info[i].power_sl);
135 RTE_LOG(INFO, POWER_MANAGER, "Detected %u host CPUs , enabled core mask:"
136 " 0x%"PRIx64"\n", num_cpus, global_enabled_cpus);
142 power_manager_get_current_frequency(unsigned core_num)
144 uint32_t freq, index;
146 if (core_num >= POWER_MGR_MAX_CPUS) {
147 RTE_LOG(ERR, POWER_MANAGER, "Core(%u) is out of range 0...%d\n",
148 core_num, POWER_MGR_MAX_CPUS-1);
151 if (!(global_enabled_cpus & (1ULL << core_num)))
154 rte_spinlock_lock(&global_core_freq_info[core_num].power_sl);
155 index = rte_power_get_freq(core_num);
156 rte_spinlock_unlock(&global_core_freq_info[core_num].power_sl);
157 if (index >= POWER_MGR_MAX_CPUS)
160 freq = global_core_freq_info[core_num].freqs[index];
166 power_manager_exit(void)
171 for (i = 0; global_enabled_cpus; global_enabled_cpus &= ~(1 << i++)) {
172 if (rte_power_exit(i) < 0) {
173 RTE_LOG(ERR, POWER_MANAGER, "Unable to shutdown power manager "
178 global_enabled_cpus = 0;
183 power_manager_scale_mask_up(uint64_t core_mask)
187 POWER_SCALE_MASK(up, core_mask, ret);
192 power_manager_scale_mask_down(uint64_t core_mask)
196 POWER_SCALE_MASK(down, core_mask, ret);
201 power_manager_scale_mask_min(uint64_t core_mask)
205 POWER_SCALE_MASK(min, core_mask, ret);
210 power_manager_scale_mask_max(uint64_t core_mask)
214 POWER_SCALE_MASK(max, core_mask, ret);
219 power_manager_scale_core_up(unsigned core_num)
223 POWER_SCALE_CORE(up, core_num, ret);
228 power_manager_scale_core_down(unsigned core_num)
232 POWER_SCALE_CORE(down, core_num, ret);
237 power_manager_scale_core_min(unsigned core_num)
241 POWER_SCALE_CORE(min, core_num, ret);
246 power_manager_scale_core_max(unsigned core_num)
250 POWER_SCALE_CORE(max, core_num, ret);