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