xref: /dpdk/app/test/test_power_intel_uncore.c (revision 93db1cfcc36ac1808c4a09ec50a470c6127b859e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2022 Intel Corporation
3  */
4 
5 #include "test.h"
6 
7 #ifndef RTE_LIB_POWER
8 
9 static int
10 test_power_intel_uncore(void)
11 {
12 	printf("Power management library not supported, skipping test\n");
13 	return TEST_SKIPPED;
14 }
15 
16 #else
17 #include <rte_power_uncore.h>
18 #include <power_common.h>
19 
20 #define VALID_PKG 0
21 #define VALID_DIE 0
22 #define INVALID_PKG (rte_power_uncore_get_num_pkgs() + 1)
23 #define INVALID_DIE (rte_power_uncore_get_num_dies(VALID_PKG) + 1)
24 #define VALID_INDEX 1
25 #define INVALID_INDEX (RTE_MAX_UNCORE_FREQS + 1)
26 
27 static int check_power_uncore_init(void)
28 {
29 	int ret;
30 
31 	/* Test initialisation of uncore configuration*/
32 	ret = rte_power_uncore_init(VALID_PKG, VALID_DIE);
33 	if (ret < 0) {
34 		printf("Cannot initialise uncore power management for pkg %u die %u, this "
35 			"may occur if environment is not configured "
36 			"correctly(APCI cpufreq) or operating in another valid "
37 			"Power management environment\n", VALID_PKG, VALID_DIE);
38 		return -1;
39 	}
40 
41 	/* Unsuccessful Test */
42 	ret = rte_power_uncore_init(INVALID_PKG, INVALID_DIE);
43 	if (ret == 0) {
44 		printf("Unexpectedly was able to initialise uncore power management "
45 			"for pkg %u die %u\n", INVALID_PKG, INVALID_DIE);
46 		return -1;
47 	}
48 
49 	return 0;
50 }
51 
52 static int
53 check_power_get_uncore_freq(void)
54 {
55 	int ret;
56 
57 	/* Successfully get uncore freq */
58 	ret = rte_power_get_uncore_freq(VALID_PKG, VALID_DIE);
59 	if (ret < 0) {
60 		printf("Failed to get uncore frequency for pkg %u die %u\n",
61 							VALID_PKG, VALID_DIE);
62 		return -1;
63 	}
64 
65 	/* Unsuccessful Test */
66 	ret = rte_power_get_uncore_freq(INVALID_PKG, INVALID_DIE);
67 	if (ret >= 0) {
68 		printf("Unexpectedly got invalid uncore frequency for pkg %u die %u\n",
69 							INVALID_PKG, INVALID_DIE);
70 		return -1;
71 	}
72 
73 	return 0;
74 }
75 
76 static int
77 check_power_set_uncore_freq(void)
78 {
79 	int ret;
80 
81 	/* Successfully set uncore freq */
82 	ret = rte_power_set_uncore_freq(VALID_PKG, VALID_DIE, VALID_INDEX);
83 	if (ret < 0) {
84 		printf("Failed to set uncore frequency for pkg %u die %u index %u\n",
85 							VALID_PKG, VALID_DIE, VALID_INDEX);
86 		return -1;
87 	}
88 
89 	/* Try to unsuccessfully set invalid uncore freq index */
90 	ret = rte_power_set_uncore_freq(VALID_PKG, VALID_DIE, INVALID_INDEX);
91 	if (ret == 0) {
92 		printf("Unexpectedly set invalid uncore index for pkg %u die %u index %u\n",
93 							VALID_PKG, VALID_DIE, INVALID_INDEX);
94 		return -1;
95 	}
96 
97 	/* Unsuccessful Test */
98 	ret = rte_power_set_uncore_freq(INVALID_PKG, INVALID_DIE, VALID_INDEX);
99 	if (ret == 0) {
100 		printf("Unexpectedly set invalid uncore frequency for pkg %u die %u index %u\n",
101 							INVALID_PKG, INVALID_DIE, VALID_INDEX);
102 		return -1;
103 	}
104 
105 	return 0;
106 }
107 
108 static int
109 check_power_uncore_freq_max(void)
110 {
111 	int ret;
112 
113 	/* Successfully get max uncore freq */
114 	ret = rte_power_uncore_freq_max(VALID_PKG, VALID_DIE);
115 	if (ret < 0) {
116 		printf("Failed to set max uncore frequency for pkg %u die %u\n",
117 							VALID_PKG, VALID_DIE);
118 		return -1;
119 	}
120 
121 	/* Unsuccessful Test */
122 	ret = rte_power_uncore_freq_max(INVALID_PKG, INVALID_DIE);
123 	if (ret == 0) {
124 		printf("Unexpectedly set invalid max uncore frequency for pkg %u die %u\n",
125 							INVALID_PKG, INVALID_DIE);
126 		return -1;
127 	}
128 
129 	return 0;
130 }
131 
132 static int
133 check_power_uncore_freq_min(void)
134 {
135 	int ret;
136 
137 	/* Successfully get min uncore freq */
138 	ret = rte_power_uncore_freq_min(VALID_PKG, VALID_DIE);
139 	if (ret < 0) {
140 		printf("Failed to set min uncore frequency for pkg %u die %u\n",
141 							VALID_PKG, VALID_DIE);
142 		return -1;
143 	}
144 
145 	/* Unsuccessful Test */
146 	ret = rte_power_uncore_freq_min(INVALID_PKG, INVALID_DIE);
147 	if (ret == 0) {
148 		printf("Unexpectedly set invalid min uncore frequency for pkg %u die %u\n",
149 							INVALID_PKG, INVALID_DIE);
150 		return -1;
151 	}
152 
153 	return 0;
154 }
155 
156 static int
157 check_power_uncore_get_num_freqs(void)
158 {
159 	int ret;
160 
161 	/* Successfully get number of uncore freq */
162 	ret = rte_power_uncore_get_num_freqs(VALID_PKG, VALID_DIE);
163 	if (ret < 0) {
164 		printf("Failed to get number of uncore frequencies for pkg %u die %u\n",
165 							VALID_PKG, VALID_DIE);
166 		return -1;
167 	}
168 
169 	/* Unsuccessful Test */
170 	ret = rte_power_uncore_get_num_freqs(INVALID_PKG, INVALID_DIE);
171 	if (ret >= 0) {
172 		printf("Unexpectedly got number of invalid frequencies for pkg %u die %u\n",
173 							INVALID_PKG, INVALID_DIE);
174 		return -1;
175 	}
176 
177 	return 0;
178 }
179 
180 static int
181 check_power_uncore_get_num_pkgs(void)
182 {
183 	int ret;
184 
185 	/* Successfully get number of uncore pkgs */
186 	ret = rte_power_uncore_get_num_pkgs();
187 	if (ret == 0) {
188 		printf("Failed to get number of uncore pkgs\n");
189 		return -1;
190 	}
191 
192 	return 0;
193 }
194 
195 static int
196 check_power_uncore_get_num_dies(void)
197 {
198 	int ret;
199 
200 	/* Successfully get number of uncore dies */
201 	ret = rte_power_uncore_get_num_dies(VALID_PKG);
202 	if (ret == 0) {
203 		printf("Failed to get number of uncore dies for pkg %u\n",
204 							VALID_PKG);
205 		return -1;
206 	}
207 
208 	/* Unsuccessful test */
209 	ret = rte_power_uncore_get_num_dies(INVALID_PKG);
210 	if (ret > 0) {
211 		printf("Unexpectedly got number of invalid dies for pkg %u\n",
212 							INVALID_PKG);
213 		return -1;
214 	}
215 
216 	return 0;
217 }
218 
219 static int
220 check_power_uncore_exit(void)
221 {
222 	int ret;
223 
224 	/* Successfully exit uncore power management */
225 	ret = rte_power_uncore_exit(VALID_PKG, VALID_DIE);
226 	if (ret < 0) {
227 		printf("Failed to exit uncore power management for pkg %u die %u\n",
228 							VALID_PKG, VALID_DIE);
229 	}
230 
231 	/* Unsuccessful Test */
232 	ret = rte_power_uncore_exit(INVALID_PKG, INVALID_DIE);
233 	if (ret == 0) {
234 		printf("Unexpectedly was able to exit uncore power management for pkg %u die %u\n",
235 							INVALID_PKG, INVALID_DIE);
236 		return -1;
237 	}
238 
239 	return 0;
240 }
241 
242 static int
243 test_power_intel_uncore(void)
244 {
245 	int ret;
246 
247 	ret = rte_power_set_uncore_env(RTE_UNCORE_PM_ENV_INTEL_UNCORE);
248 	if (ret < 0)
249 		goto fail_all;
250 
251 	ret = rte_power_uncore_get_num_pkgs();
252 	if (ret == 0) {
253 		printf("Uncore frequency management not supported/enabled on this kernel. "
254 		"Please enable CONFIG_INTEL_UNCORE_FREQ_CONTROL if on Intel x86 with linux kernel"
255 		" >= 5.6\n");
256 		return TEST_SKIPPED;
257 	}
258 
259 	ret = check_power_uncore_init();
260 	if (ret < 0)
261 		goto fail_all;
262 
263 	ret = check_power_get_uncore_freq();
264 	if (ret < 0)
265 		goto fail_all;
266 
267 	ret = check_power_set_uncore_freq();
268 	if (ret < 0)
269 		goto fail_all;
270 
271 	ret = check_power_uncore_freq_max();
272 	if (ret < 0)
273 		goto fail_all;
274 
275 	ret = check_power_uncore_freq_min();
276 	if (ret < 0)
277 		goto fail_all;
278 
279 	ret = check_power_uncore_get_num_freqs();
280 	if (ret < 0)
281 		goto fail_all;
282 
283 	ret = check_power_uncore_get_num_pkgs();
284 	if (ret < 0)
285 		goto fail_all;
286 
287 	ret = check_power_uncore_get_num_dies();
288 	if (ret < 0)
289 		goto fail_all;
290 
291 	ret = check_power_uncore_exit();
292 	if (ret < 0)
293 		return -1;
294 
295 	return 0;
296 
297 fail_all:
298 	rte_power_uncore_exit(VALID_PKG, VALID_DIE);
299 	return -1;
300 }
301 #endif
302 
303 REGISTER_FAST_TEST(power_intel_uncore_autotest, true, true, test_power_intel_uncore);
304