kvargs: use SPDX tags
[dpdk.git] / test / test / test_power_kvm_vm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <string.h>
10
11 #include "test.h"
12
13 #include <rte_power.h>
14
15 #define TEST_POWER_VM_LCORE_ID            0U
16 #define TEST_POWER_VM_LCORE_OUT_OF_BOUNDS (RTE_MAX_LCORE+1)
17 #define TEST_POWER_VM_LCORE_INVALID       1U
18
19 static int
20 test_power_kvm_vm(void)
21 {
22         int ret;
23         enum power_management_env env;
24
25         ret = rte_power_set_env(PM_ENV_KVM_VM);
26         if (ret != 0) {
27                 printf("Failed on setting environment to PM_ENV_KVM_VM\n");
28                 return -1;
29         }
30
31         /* Test environment configuration */
32         env = rte_power_get_env();
33         if (env != PM_ENV_KVM_VM) {
34                 printf("Unexpectedly got a Power Management environment other than "
35                                 "KVM VM\n");
36                 rte_power_unset_env();
37                 return -1;
38         }
39
40         /* verify that function pointers are not NULL */
41         if (rte_power_freqs == NULL) {
42                 printf("rte_power_freqs should not be NULL, environment has not been "
43                                 "initialised\n");
44                 return -1;
45         }
46         if (rte_power_get_freq == NULL) {
47                 printf("rte_power_get_freq should not be NULL, environment has not "
48                                 "been initialised\n");
49                 return -1;
50         }
51         if (rte_power_set_freq == NULL) {
52                 printf("rte_power_set_freq should not be NULL, environment has not "
53                                 "been initialised\n");
54                 return -1;
55         }
56         if (rte_power_freq_up == NULL) {
57                 printf("rte_power_freq_up should not be NULL, environment has not "
58                                 "been initialised\n");
59                 return -1;
60         }
61         if (rte_power_freq_down == NULL) {
62                 printf("rte_power_freq_down should not be NULL, environment has not "
63                                 "been initialised\n");
64                 return -1;
65         }
66         if (rte_power_freq_max == NULL) {
67                 printf("rte_power_freq_max should not be NULL, environment has not "
68                                 "been initialised\n");
69                 return -1;
70         }
71         if (rte_power_freq_min == NULL) {
72                 printf("rte_power_freq_min should not be NULL, environment has not "
73                                 "been initialised\n");
74                 return -1;
75         }
76         /* Test initialisation of an out of bounds lcore */
77         ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
78         if (ret != -1) {
79                 printf("rte_power_init unexpectedly succeeded on an invalid lcore %u\n",
80                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
81                 rte_power_unset_env();
82                 return -1;
83         }
84
85         /* Test initialisation of a valid lcore */
86         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
87         if (ret < 0) {
88                 printf("Cannot initialise power management for lcore %u, this "
89                                 "may occur if environment is not configured "
90                                 "correctly(KVM VM) or operating in another valid "
91                                 "Power management environment\n", TEST_POWER_VM_LCORE_ID);
92                 rte_power_unset_env();
93                 return -1;
94         }
95
96         /* Test initialisation of previously initialised lcore */
97         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
98         if (ret == 0) {
99                 printf("rte_power_init unexpectedly succeeded on calling init twice on"
100                                 " lcore %u\n", TEST_POWER_VM_LCORE_ID);
101                 goto fail_all;
102         }
103
104         /* Test frequency up of invalid lcore */
105         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
106         if (ret == 1) {
107                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
108                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
109                 goto fail_all;
110         }
111
112         /* Test frequency down of invalid lcore */
113         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
114         if (ret == 1) {
115                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
116                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
117                 goto fail_all;
118         }
119
120         /* Test frequency min of invalid lcore */
121         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
122         if (ret == 1) {
123                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
124                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
125                 goto fail_all;
126         }
127
128         /* Test frequency max of invalid lcore */
129         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
130         if (ret == 1) {
131                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
132                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
133                 goto fail_all;
134         }
135
136         /* Test frequency up of valid but uninitialised lcore */
137         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_INVALID);
138         if (ret == 1) {
139                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
140                                 TEST_POWER_VM_LCORE_INVALID);
141                 goto fail_all;
142         }
143
144         /* Test frequency down of valid but uninitialised lcore */
145         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_INVALID);
146         if (ret == 1) {
147                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
148                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
149                 goto fail_all;
150         }
151
152         /* Test frequency min of valid but uninitialised lcore */
153         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_INVALID);
154         if (ret == 1) {
155                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
156                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
157                 goto fail_all;
158         }
159
160         /* Test frequency max of valid but uninitialised lcore */
161         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_INVALID);
162         if (ret == 1) {
163                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
164                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
165                 goto fail_all;
166         }
167
168         /* Test frequency up of valid lcore */
169         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
170         if (ret != 1) {
171                 printf("rte_power_freq_up unexpectedly failed on valid lcore %u\n",
172                                 TEST_POWER_VM_LCORE_ID);
173                 goto fail_all;
174         }
175
176         /* Test frequency down of valid lcore */
177         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
178         if (ret != 1) {
179                 printf("rte_power_freq_down unexpectedly failed on valid lcore "
180                                 "%u\n", TEST_POWER_VM_LCORE_ID);
181                 goto fail_all;
182         }
183
184         /* Test frequency min of valid lcore */
185         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
186         if (ret != 1) {
187                 printf("rte_power_freq_min unexpectedly failed on valid lcore "
188                                 "%u\n", TEST_POWER_VM_LCORE_ID);
189                 goto fail_all;
190         }
191
192         /* Test frequency max of valid lcore */
193         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
194         if (ret != 1) {
195                 printf("rte_power_freq_max unexpectedly failed on valid lcore "
196                                 "%u\n", TEST_POWER_VM_LCORE_ID);
197                 goto fail_all;
198         }
199
200         /* Test unsupported rte_power_freqs */
201         ret = rte_power_freqs(TEST_POWER_VM_LCORE_ID, NULL, 0);
202         if (ret != -ENOTSUP) {
203                 printf("rte_power_freqs did not return the expected -ENOTSUP(%d) but "
204                                 "returned %d\n", -ENOTSUP, ret);
205                 goto fail_all;
206         }
207
208         /* Test unsupported rte_power_get_freq */
209         ret = rte_power_get_freq(TEST_POWER_VM_LCORE_ID);
210         if (ret != -ENOTSUP) {
211                 printf("rte_power_get_freq did not return the expected -ENOTSUP(%d) but"
212                                 " returned %d for lcore %u\n",
213                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
214                 goto fail_all;
215         }
216
217         /* Test unsupported rte_power_set_freq */
218         ret = rte_power_set_freq(TEST_POWER_VM_LCORE_ID, 0);
219         if (ret != -ENOTSUP) {
220                 printf("rte_power_set_freq did not return the expected -ENOTSUP(%d) but"
221                                 " returned %d for lcore %u\n",
222                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
223                 goto fail_all;
224         }
225
226         /* Test removing of an lcore */
227         ret = rte_power_exit(TEST_POWER_VM_LCORE_ID);
228         if (ret != 0) {
229                 printf("rte_power_exit unexpectedly failed on valid lcore %u,"
230                                 "please ensure that the environment has been configured "
231                                 "correctly\n", TEST_POWER_VM_LCORE_ID);
232                 goto fail_all;
233         }
234
235         /* Test frequency up of previously removed lcore */
236         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
237         if (ret == 0) {
238                 printf("rte_power_freq_up unexpectedly succeeded on a removed "
239                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
240                 return -1;
241         }
242
243         /* Test frequency down of previously removed lcore */
244         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
245         if (ret == 0) {
246                 printf("rte_power_freq_down unexpectedly succeeded on a removed "
247                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
248                 return -1;
249         }
250
251         /* Test frequency min of previously removed lcore */
252         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
253         if (ret == 0) {
254                 printf("rte_power_freq_min unexpectedly succeeded on a removed "
255                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
256                 return -1;
257         }
258
259         /* Test frequency max of previously removed lcore */
260         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
261         if (ret == 0) {
262                 printf("rte_power_freq_max unexpectedly succeeded on a removed "
263                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
264                 return -1;
265         }
266         rte_power_unset_env();
267         return 0;
268 fail_all:
269         rte_power_exit(TEST_POWER_VM_LCORE_ID);
270         rte_power_unset_env();
271         return -1;
272 }
273
274 REGISTER_TEST_COMMAND(power_kvm_vm_autotest, test_power_kvm_vm);