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