net/bnxt: use first completion ring for fwd and async event
[dpdk.git] / lib / librte_power / rte_power.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <rte_atomic.h>
6
7 #include "rte_power.h"
8 #include "power_acpi_cpufreq.h"
9 #include "power_kvm_vm.h"
10 #include "power_common.h"
11
12 enum power_management_env global_default_env = PM_ENV_NOT_SET;
13
14 volatile uint32_t global_env_cfg_status = 0;
15
16 /* function pointers */
17 rte_power_freqs_t rte_power_freqs  = NULL;
18 rte_power_get_freq_t rte_power_get_freq = NULL;
19 rte_power_set_freq_t rte_power_set_freq = NULL;
20 rte_power_freq_change_t rte_power_freq_up = NULL;
21 rte_power_freq_change_t rte_power_freq_down = NULL;
22 rte_power_freq_change_t rte_power_freq_max = NULL;
23 rte_power_freq_change_t rte_power_freq_min = NULL;
24 rte_power_freq_change_t rte_power_turbo_status;
25 rte_power_freq_change_t rte_power_freq_enable_turbo;
26 rte_power_freq_change_t rte_power_freq_disable_turbo;
27
28 int
29 rte_power_set_env(enum power_management_env env)
30 {
31         if (rte_atomic32_cmpset(&global_env_cfg_status, 0, 1) == 0) {
32                 return 0;
33         }
34         if (env == PM_ENV_ACPI_CPUFREQ) {
35                 rte_power_freqs = power_acpi_cpufreq_freqs;
36                 rte_power_get_freq = power_acpi_cpufreq_get_freq;
37                 rte_power_set_freq = power_acpi_cpufreq_set_freq;
38                 rte_power_freq_up = power_acpi_cpufreq_freq_up;
39                 rte_power_freq_down = power_acpi_cpufreq_freq_down;
40                 rte_power_freq_min = power_acpi_cpufreq_freq_min;
41                 rte_power_freq_max = power_acpi_cpufreq_freq_max;
42                 rte_power_turbo_status = power_acpi_turbo_status;
43                 rte_power_freq_enable_turbo = power_acpi_enable_turbo;
44                 rte_power_freq_disable_turbo = power_acpi_disable_turbo;
45         } else if (env == PM_ENV_KVM_VM) {
46                 rte_power_freqs = power_kvm_vm_freqs;
47                 rte_power_get_freq = power_kvm_vm_get_freq;
48                 rte_power_set_freq = power_kvm_vm_set_freq;
49                 rte_power_freq_up = power_kvm_vm_freq_up;
50                 rte_power_freq_down = power_kvm_vm_freq_down;
51                 rte_power_freq_min = power_kvm_vm_freq_min;
52                 rte_power_freq_max = power_kvm_vm_freq_max;
53                 rte_power_turbo_status = power_kvm_vm_turbo_status;
54                 rte_power_freq_enable_turbo = power_kvm_vm_enable_turbo;
55                 rte_power_freq_disable_turbo = power_kvm_vm_disable_turbo;
56         } else {
57                 RTE_LOG(ERR, POWER, "Invalid Power Management Environment(%d) set\n",
58                                 env);
59                 rte_power_unset_env();
60                 return -1;
61         }
62         global_default_env = env;
63         return 0;
64
65 }
66
67 void
68 rte_power_unset_env(void)
69 {
70         if (rte_atomic32_cmpset(&global_env_cfg_status, 1, 0) != 0)
71                 global_default_env = PM_ENV_NOT_SET;
72 }
73
74 enum power_management_env
75 rte_power_get_env(void) {
76         return global_default_env;
77 }
78
79 int
80 rte_power_init(unsigned int lcore_id)
81 {
82         int ret = -1;
83
84         if (global_default_env == PM_ENV_ACPI_CPUFREQ) {
85                 return power_acpi_cpufreq_init(lcore_id);
86         }
87         if (global_default_env == PM_ENV_KVM_VM) {
88                 return power_kvm_vm_init(lcore_id);
89         }
90         /* Auto detect Environment */
91         RTE_LOG(INFO, POWER, "Attempting to initialise ACPI cpufreq power "
92                         "management...\n");
93         ret = power_acpi_cpufreq_init(lcore_id);
94         if (ret == 0) {
95                 rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
96                 goto out;
97         }
98
99         RTE_LOG(INFO, POWER, "Attempting to initialise VM power management...\n");
100         ret = power_kvm_vm_init(lcore_id);
101         if (ret == 0) {
102                 rte_power_set_env(PM_ENV_KVM_VM);
103                 goto out;
104         }
105         RTE_LOG(ERR, POWER, "Unable to set Power Management Environment for lcore "
106                         "%u\n", lcore_id);
107 out:
108         return ret;
109 }
110
111 int
112 rte_power_exit(unsigned int lcore_id)
113 {
114         if (global_default_env == PM_ENV_ACPI_CPUFREQ)
115                 return power_acpi_cpufreq_exit(lcore_id);
116         if (global_default_env == PM_ENV_KVM_VM)
117                 return power_kvm_vm_exit(lcore_id);
118
119         RTE_LOG(ERR, POWER, "Environment has not been set, unable to exit "
120                                 "gracefully\n");
121         return -1;
122
123 }