]>
Commit | Line | Data |
---|---|---|
bd31b751 AJ |
1 | /* |
2 | * Arm CPU feature test cases | |
3 | * | |
4 | * Copyright (c) 2019 Red Hat Inc. | |
5 | * Authors: | |
6 | * Andrew Jones <[email protected]> | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
9 | * See the COPYING file in the top-level directory. | |
10 | */ | |
11 | #include "qemu/osdep.h" | |
0df9142d | 12 | #include "qemu/bitops.h" |
bd31b751 AJ |
13 | #include "libqtest.h" |
14 | #include "qapi/qmp/qdict.h" | |
15 | #include "qapi/qmp/qjson.h" | |
16 | ||
0df9142d AJ |
17 | /* |
18 | * We expect the SVE max-vq to be 16. Also it must be <= 64 | |
19 | * for our test code, otherwise 'vls' can't just be a uint64_t. | |
20 | */ | |
21 | #define SVE_MAX_VQ 16 | |
22 | ||
bd31b751 AJ |
23 | #define MACHINE "-machine virt,gic-version=max,accel=tcg " |
24 | #define MACHINE_KVM "-machine virt,gic-version=max,accel=kvm:tcg " | |
25 | #define QUERY_HEAD "{ 'execute': 'query-cpu-model-expansion', " \ | |
26 | " 'arguments': { 'type': 'full', " | |
27 | #define QUERY_TAIL "}}" | |
28 | ||
29 | static bool kvm_enabled(QTestState *qts) | |
30 | { | |
31 | QDict *resp, *qdict; | |
32 | bool enabled; | |
33 | ||
34 | resp = qtest_qmp(qts, "{ 'execute': 'query-kvm' }"); | |
35 | g_assert(qdict_haskey(resp, "return")); | |
36 | qdict = qdict_get_qdict(resp, "return"); | |
37 | g_assert(qdict_haskey(qdict, "enabled")); | |
38 | enabled = qdict_get_bool(qdict, "enabled"); | |
39 | qobject_unref(resp); | |
40 | ||
41 | return enabled; | |
42 | } | |
43 | ||
44 | static QDict *do_query_no_props(QTestState *qts, const char *cpu_type) | |
45 | { | |
46 | return qtest_qmp(qts, QUERY_HEAD "'model': { 'name': %s }" | |
47 | QUERY_TAIL, cpu_type); | |
48 | } | |
49 | ||
50 | static QDict *do_query(QTestState *qts, const char *cpu_type, | |
51 | const char *fmt, ...) | |
52 | { | |
53 | QDict *resp; | |
54 | ||
55 | if (fmt) { | |
56 | QDict *args; | |
57 | va_list ap; | |
58 | ||
59 | va_start(ap, fmt); | |
60 | args = qdict_from_vjsonf_nofail(fmt, ap); | |
61 | va_end(ap); | |
62 | ||
63 | resp = qtest_qmp(qts, QUERY_HEAD "'model': { 'name': %s, " | |
64 | "'props': %p }" | |
65 | QUERY_TAIL, cpu_type, args); | |
66 | } else { | |
67 | resp = do_query_no_props(qts, cpu_type); | |
68 | } | |
69 | ||
70 | return resp; | |
71 | } | |
72 | ||
73 | static const char *resp_get_error(QDict *resp) | |
74 | { | |
75 | QDict *qdict; | |
76 | ||
77 | g_assert(resp); | |
78 | ||
79 | qdict = qdict_get_qdict(resp, "error"); | |
80 | if (qdict) { | |
81 | return qdict_get_str(qdict, "desc"); | |
82 | } | |
83 | ||
84 | return NULL; | |
85 | } | |
86 | ||
87 | #define assert_error(qts, cpu_type, expected_error, fmt, ...) \ | |
88 | ({ \ | |
89 | QDict *_resp; \ | |
90 | const char *_error; \ | |
91 | \ | |
92 | _resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \ | |
93 | g_assert(_resp); \ | |
94 | _error = resp_get_error(_resp); \ | |
95 | g_assert(_error); \ | |
96 | g_assert(g_str_equal(_error, expected_error)); \ | |
97 | qobject_unref(_resp); \ | |
98 | }) | |
99 | ||
100 | static bool resp_has_props(QDict *resp) | |
101 | { | |
102 | QDict *qdict; | |
103 | ||
104 | g_assert(resp); | |
105 | ||
106 | if (!qdict_haskey(resp, "return")) { | |
107 | return false; | |
108 | } | |
109 | qdict = qdict_get_qdict(resp, "return"); | |
110 | ||
111 | if (!qdict_haskey(qdict, "model")) { | |
112 | return false; | |
113 | } | |
114 | qdict = qdict_get_qdict(qdict, "model"); | |
115 | ||
116 | return qdict_haskey(qdict, "props"); | |
117 | } | |
118 | ||
119 | static QDict *resp_get_props(QDict *resp) | |
120 | { | |
121 | QDict *qdict; | |
122 | ||
123 | g_assert(resp); | |
124 | g_assert(resp_has_props(resp)); | |
125 | ||
126 | qdict = qdict_get_qdict(resp, "return"); | |
127 | qdict = qdict_get_qdict(qdict, "model"); | |
128 | qdict = qdict_get_qdict(qdict, "props"); | |
129 | ||
130 | return qdict; | |
131 | } | |
132 | ||
133 | #define assert_has_feature(qts, cpu_type, feature) \ | |
134 | ({ \ | |
135 | QDict *_resp = do_query_no_props(qts, cpu_type); \ | |
136 | g_assert(_resp); \ | |
137 | g_assert(resp_has_props(_resp)); \ | |
138 | g_assert(qdict_get(resp_get_props(_resp), feature)); \ | |
139 | qobject_unref(_resp); \ | |
140 | }) | |
141 | ||
142 | #define assert_has_not_feature(qts, cpu_type, feature) \ | |
143 | ({ \ | |
144 | QDict *_resp = do_query_no_props(qts, cpu_type); \ | |
145 | g_assert(_resp); \ | |
146 | g_assert(!resp_has_props(_resp) || \ | |
147 | !qdict_get(resp_get_props(_resp), feature)); \ | |
148 | qobject_unref(_resp); \ | |
149 | }) | |
150 | ||
151 | static void assert_type_full(QTestState *qts) | |
152 | { | |
153 | const char *error; | |
154 | QDict *resp; | |
155 | ||
156 | resp = qtest_qmp(qts, "{ 'execute': 'query-cpu-model-expansion', " | |
157 | "'arguments': { 'type': 'static', " | |
158 | "'model': { 'name': 'foo' }}}"); | |
159 | g_assert(resp); | |
160 | error = resp_get_error(resp); | |
161 | g_assert(error); | |
162 | g_assert(g_str_equal(error, | |
163 | "The requested expansion type is not supported")); | |
164 | qobject_unref(resp); | |
165 | } | |
166 | ||
167 | static void assert_bad_props(QTestState *qts, const char *cpu_type) | |
168 | { | |
169 | const char *error; | |
170 | QDict *resp; | |
171 | ||
172 | resp = qtest_qmp(qts, "{ 'execute': 'query-cpu-model-expansion', " | |
173 | "'arguments': { 'type': 'full', " | |
174 | "'model': { 'name': %s, " | |
175 | "'props': false }}}", | |
176 | cpu_type); | |
177 | g_assert(resp); | |
178 | error = resp_get_error(resp); | |
179 | g_assert(error); | |
180 | g_assert(g_str_equal(error, | |
181 | "Invalid parameter type for 'props', expected: dict")); | |
182 | qobject_unref(resp); | |
183 | } | |
184 | ||
0df9142d AJ |
185 | static uint64_t resp_get_sve_vls(QDict *resp) |
186 | { | |
187 | QDict *props; | |
188 | const QDictEntry *e; | |
189 | uint64_t vls = 0; | |
190 | int n = 0; | |
191 | ||
192 | g_assert(resp); | |
193 | g_assert(resp_has_props(resp)); | |
194 | ||
195 | props = resp_get_props(resp); | |
196 | ||
197 | for (e = qdict_first(props); e; e = qdict_next(props, e)) { | |
198 | if (strlen(e->key) > 3 && !strncmp(e->key, "sve", 3) && | |
199 | g_ascii_isdigit(e->key[3])) { | |
200 | char *endptr; | |
201 | int bits; | |
202 | ||
203 | bits = g_ascii_strtoll(&e->key[3], &endptr, 10); | |
204 | if (!bits || *endptr != '\0') { | |
205 | continue; | |
206 | } | |
207 | ||
208 | if (qdict_get_bool(props, e->key)) { | |
209 | vls |= BIT_ULL((bits / 128) - 1); | |
210 | } | |
211 | ++n; | |
212 | } | |
213 | } | |
214 | ||
215 | g_assert(n == SVE_MAX_VQ); | |
216 | ||
217 | return vls; | |
218 | } | |
219 | ||
220 | #define assert_sve_vls(qts, cpu_type, expected_vls, fmt, ...) \ | |
221 | ({ \ | |
222 | QDict *_resp = do_query(qts, cpu_type, fmt, ##__VA_ARGS__); \ | |
223 | g_assert(_resp); \ | |
224 | g_assert(resp_has_props(_resp)); \ | |
225 | g_assert(resp_get_sve_vls(_resp) == expected_vls); \ | |
226 | qobject_unref(_resp); \ | |
227 | }) | |
228 | ||
229 | static void sve_tests_default(QTestState *qts, const char *cpu_type) | |
230 | { | |
231 | /* | |
232 | * With no sve-max-vq or sve<N> properties on the command line | |
233 | * the default is to have all vector lengths enabled. This also | |
234 | * tests that 'sve' is 'on' by default. | |
235 | */ | |
236 | assert_sve_vls(qts, cpu_type, BIT_ULL(SVE_MAX_VQ) - 1, NULL); | |
237 | ||
238 | /* With SVE off, all vector lengths should also be off. */ | |
239 | assert_sve_vls(qts, cpu_type, 0, "{ 'sve': false }"); | |
240 | ||
241 | /* With SVE on, we must have at least one vector length enabled. */ | |
242 | assert_error(qts, cpu_type, "cannot disable sve128", "{ 'sve128': false }"); | |
243 | ||
244 | /* Basic enable/disable tests. */ | |
245 | assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve384': true }"); | |
246 | assert_sve_vls(qts, cpu_type, ((BIT_ULL(SVE_MAX_VQ) - 1) & ~BIT_ULL(2)), | |
247 | "{ 'sve384': false }"); | |
248 | ||
249 | /* | |
250 | * --------------------------------------------------------------------- | |
251 | * power-of-two(vq) all-power- can can | |
252 | * of-two(< vq) enable disable | |
253 | * --------------------------------------------------------------------- | |
254 | * vq < max_vq no MUST* yes yes | |
255 | * vq < max_vq yes MUST* yes no | |
256 | * --------------------------------------------------------------------- | |
257 | * vq == max_vq n/a MUST* yes** yes** | |
258 | * --------------------------------------------------------------------- | |
259 | * vq > max_vq n/a no no yes | |
260 | * vq > max_vq n/a yes yes yes | |
261 | * --------------------------------------------------------------------- | |
262 | * | |
263 | * [*] "MUST" means this requirement must already be satisfied, | |
264 | * otherwise 'max_vq' couldn't itself be enabled. | |
265 | * | |
266 | * [**] Not testable with the QMP interface, only with the command line. | |
267 | */ | |
268 | ||
269 | /* max_vq := 8 */ | |
270 | assert_sve_vls(qts, cpu_type, 0x8b, "{ 'sve1024': true }"); | |
271 | ||
272 | /* max_vq := 8, vq < max_vq, !power-of-two(vq) */ | |
273 | assert_sve_vls(qts, cpu_type, 0x8f, | |
274 | "{ 'sve1024': true, 'sve384': true }"); | |
275 | assert_sve_vls(qts, cpu_type, 0x8b, | |
276 | "{ 'sve1024': true, 'sve384': false }"); | |
277 | ||
278 | /* max_vq := 8, vq < max_vq, power-of-two(vq) */ | |
279 | assert_sve_vls(qts, cpu_type, 0x8b, | |
280 | "{ 'sve1024': true, 'sve256': true }"); | |
281 | assert_error(qts, cpu_type, "cannot disable sve256", | |
282 | "{ 'sve1024': true, 'sve256': false }"); | |
283 | ||
284 | /* max_vq := 3, vq > max_vq, !all-power-of-two(< vq) */ | |
285 | assert_error(qts, cpu_type, "cannot disable sve512", | |
286 | "{ 'sve384': true, 'sve512': false, 'sve640': true }"); | |
287 | ||
288 | /* | |
289 | * We can disable power-of-two vector lengths when all larger lengths | |
290 | * are also disabled. We only need to disable the power-of-two length, | |
291 | * as all non-enabled larger lengths will then be auto-disabled. | |
292 | */ | |
293 | assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve512': false }"); | |
294 | ||
295 | /* max_vq := 3, vq > max_vq, all-power-of-two(< vq) */ | |
296 | assert_sve_vls(qts, cpu_type, 0x1f, | |
297 | "{ 'sve384': true, 'sve512': true, 'sve640': true }"); | |
298 | assert_sve_vls(qts, cpu_type, 0xf, | |
299 | "{ 'sve384': true, 'sve512': true, 'sve640': false }"); | |
300 | } | |
301 | ||
302 | static void sve_tests_sve_max_vq_8(const void *data) | |
303 | { | |
304 | QTestState *qts; | |
305 | ||
306 | qts = qtest_init(MACHINE "-cpu max,sve-max-vq=8"); | |
307 | ||
308 | assert_sve_vls(qts, "max", BIT_ULL(8) - 1, NULL); | |
309 | ||
310 | /* | |
311 | * Disabling the max-vq set by sve-max-vq is not allowed, but | |
312 | * of course enabling it is OK. | |
313 | */ | |
314 | assert_error(qts, "max", "cannot disable sve1024", "{ 'sve1024': false }"); | |
315 | assert_sve_vls(qts, "max", 0xff, "{ 'sve1024': true }"); | |
316 | ||
317 | /* | |
318 | * Enabling anything larger than max-vq set by sve-max-vq is not | |
319 | * allowed, but of course disabling everything larger is OK. | |
320 | */ | |
321 | assert_error(qts, "max", "cannot enable sve1152", "{ 'sve1152': true }"); | |
322 | assert_sve_vls(qts, "max", 0xff, "{ 'sve1152': false }"); | |
323 | ||
324 | /* | |
325 | * We can enable/disable non power-of-two lengths smaller than the | |
326 | * max-vq set by sve-max-vq, but, while we can enable power-of-two | |
327 | * lengths, we can't disable them. | |
328 | */ | |
329 | assert_sve_vls(qts, "max", 0xff, "{ 'sve384': true }"); | |
330 | assert_sve_vls(qts, "max", 0xfb, "{ 'sve384': false }"); | |
331 | assert_sve_vls(qts, "max", 0xff, "{ 'sve256': true }"); | |
332 | assert_error(qts, "max", "cannot disable sve256", "{ 'sve256': false }"); | |
333 | ||
334 | qtest_quit(qts); | |
335 | } | |
336 | ||
337 | static void sve_tests_sve_off(const void *data) | |
338 | { | |
339 | QTestState *qts; | |
340 | ||
341 | qts = qtest_init(MACHINE "-cpu max,sve=off"); | |
342 | ||
343 | /* SVE is off, so the map should be empty. */ | |
344 | assert_sve_vls(qts, "max", 0, NULL); | |
345 | ||
346 | /* The map stays empty even if we turn lengths off. */ | |
347 | assert_sve_vls(qts, "max", 0, "{ 'sve128': false }"); | |
348 | ||
349 | /* It's an error to enable lengths when SVE is off. */ | |
350 | assert_error(qts, "max", "cannot enable sve128", "{ 'sve128': true }"); | |
351 | ||
352 | /* With SVE re-enabled we should get all vector lengths enabled. */ | |
353 | assert_sve_vls(qts, "max", BIT_ULL(SVE_MAX_VQ) - 1, "{ 'sve': true }"); | |
354 | ||
355 | /* Or enable SVE with just specific vector lengths. */ | |
356 | assert_sve_vls(qts, "max", 0x3, | |
357 | "{ 'sve': true, 'sve128': true, 'sve256': true }"); | |
358 | ||
359 | qtest_quit(qts); | |
360 | } | |
361 | ||
bd31b751 AJ |
362 | static void test_query_cpu_model_expansion(const void *data) |
363 | { | |
364 | QTestState *qts; | |
365 | ||
366 | qts = qtest_init(MACHINE "-cpu max"); | |
367 | ||
368 | /* Test common query-cpu-model-expansion input validation */ | |
369 | assert_type_full(qts); | |
370 | assert_bad_props(qts, "max"); | |
371 | assert_error(qts, "foo", "The CPU type 'foo' is not a recognized " | |
372 | "ARM CPU type", NULL); | |
373 | assert_error(qts, "max", "Parameter 'not-a-prop' is unexpected", | |
374 | "{ 'not-a-prop': false }"); | |
375 | assert_error(qts, "host", "The CPU type 'host' requires KVM", NULL); | |
376 | ||
377 | /* Test expected feature presence/absence for some cpu types */ | |
378 | assert_has_feature(qts, "max", "pmu"); | |
379 | assert_has_feature(qts, "cortex-a15", "pmu"); | |
380 | assert_has_not_feature(qts, "cortex-a15", "aarch64"); | |
381 | ||
382 | if (g_str_equal(qtest_get_arch(), "aarch64")) { | |
383 | assert_has_feature(qts, "max", "aarch64"); | |
73234775 | 384 | assert_has_feature(qts, "max", "sve"); |
0df9142d | 385 | assert_has_feature(qts, "max", "sve128"); |
bd31b751 AJ |
386 | assert_has_feature(qts, "cortex-a57", "pmu"); |
387 | assert_has_feature(qts, "cortex-a57", "aarch64"); | |
388 | ||
0df9142d AJ |
389 | sve_tests_default(qts, "max"); |
390 | ||
bd31b751 AJ |
391 | /* Test that features that depend on KVM generate errors without. */ |
392 | assert_error(qts, "max", | |
393 | "'aarch64' feature cannot be disabled " | |
394 | "unless KVM is enabled and 32-bit EL1 " | |
395 | "is supported", | |
396 | "{ 'aarch64': false }"); | |
397 | } | |
398 | ||
399 | qtest_quit(qts); | |
400 | } | |
401 | ||
402 | static void test_query_cpu_model_expansion_kvm(const void *data) | |
403 | { | |
404 | QTestState *qts; | |
405 | ||
406 | qts = qtest_init(MACHINE_KVM "-cpu max"); | |
407 | ||
408 | /* | |
409 | * These tests target the 'host' CPU type, so KVM must be enabled. | |
410 | */ | |
411 | if (!kvm_enabled(qts)) { | |
412 | qtest_quit(qts); | |
413 | return; | |
414 | } | |
415 | ||
416 | if (g_str_equal(qtest_get_arch(), "aarch64")) { | |
417 | assert_has_feature(qts, "host", "aarch64"); | |
418 | assert_has_feature(qts, "host", "pmu"); | |
419 | ||
14e99e0f AJ |
420 | assert_has_feature(qts, "max", "sve"); |
421 | ||
bd31b751 AJ |
422 | assert_error(qts, "cortex-a15", |
423 | "We cannot guarantee the CPU type 'cortex-a15' works " | |
424 | "with KVM on this host", NULL); | |
425 | } else { | |
426 | assert_has_not_feature(qts, "host", "aarch64"); | |
427 | assert_has_not_feature(qts, "host", "pmu"); | |
14e99e0f AJ |
428 | |
429 | assert_has_not_feature(qts, "max", "sve"); | |
bd31b751 AJ |
430 | } |
431 | ||
432 | qtest_quit(qts); | |
433 | } | |
434 | ||
435 | int main(int argc, char **argv) | |
436 | { | |
437 | g_test_init(&argc, &argv, NULL); | |
438 | ||
439 | qtest_add_data_func("/arm/query-cpu-model-expansion", | |
440 | NULL, test_query_cpu_model_expansion); | |
441 | ||
442 | /* | |
443 | * For now we only run KVM specific tests with AArch64 QEMU in | |
444 | * order avoid attempting to run an AArch32 QEMU with KVM on | |
445 | * AArch64 hosts. That won't work and isn't easy to detect. | |
446 | */ | |
447 | if (g_str_equal(qtest_get_arch(), "aarch64")) { | |
448 | qtest_add_data_func("/arm/kvm/query-cpu-model-expansion", | |
449 | NULL, test_query_cpu_model_expansion_kvm); | |
450 | } | |
451 | ||
0df9142d AJ |
452 | if (g_str_equal(qtest_get_arch(), "aarch64")) { |
453 | qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-max-vq-8", | |
454 | NULL, sve_tests_sve_max_vq_8); | |
455 | qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-off", | |
456 | NULL, sve_tests_sve_off); | |
457 | } | |
458 | ||
bd31b751 AJ |
459 | return g_test_run(); |
460 | } |