target/arm/cpu64: max cpu: Introduce sve<N> properties
[qemu/ar7.git] / tests / arm-cpu-features.c
blob45fa3cb88d181deba41c2d142a69d1e73cfa4c22
1 /*
2 * Arm CPU feature test cases
4 * Copyright (c) 2019 Red Hat Inc.
5 * Authors:
6 * Andrew Jones <drjones@redhat.com>
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.
11 #include "qemu/osdep.h"
12 #include "qemu/bitops.h"
13 #include "libqtest.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qjson.h"
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.
21 #define SVE_MAX_VQ 16
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 "}}"
29 static bool kvm_enabled(QTestState *qts)
31 QDict *resp, *qdict;
32 bool enabled;
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);
41 return enabled;
44 static QDict *do_query_no_props(QTestState *qts, const char *cpu_type)
46 return qtest_qmp(qts, QUERY_HEAD "'model': { 'name': %s }"
47 QUERY_TAIL, cpu_type);
50 static QDict *do_query(QTestState *qts, const char *cpu_type,
51 const char *fmt, ...)
53 QDict *resp;
55 if (fmt) {
56 QDict *args;
57 va_list ap;
59 va_start(ap, fmt);
60 args = qdict_from_vjsonf_nofail(fmt, ap);
61 va_end(ap);
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);
70 return resp;
73 static const char *resp_get_error(QDict *resp)
75 QDict *qdict;
77 g_assert(resp);
79 qdict = qdict_get_qdict(resp, "error");
80 if (qdict) {
81 return qdict_get_str(qdict, "desc");
84 return NULL;
87 #define assert_error(qts, cpu_type, expected_error, fmt, ...) \
88 ({ \
89 QDict *_resp; \
90 const char *_error; \
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); \
100 static bool resp_has_props(QDict *resp)
102 QDict *qdict;
104 g_assert(resp);
106 if (!qdict_haskey(resp, "return")) {
107 return false;
109 qdict = qdict_get_qdict(resp, "return");
111 if (!qdict_haskey(qdict, "model")) {
112 return false;
114 qdict = qdict_get_qdict(qdict, "model");
116 return qdict_haskey(qdict, "props");
119 static QDict *resp_get_props(QDict *resp)
121 QDict *qdict;
123 g_assert(resp);
124 g_assert(resp_has_props(resp));
126 qdict = qdict_get_qdict(resp, "return");
127 qdict = qdict_get_qdict(qdict, "model");
128 qdict = qdict_get_qdict(qdict, "props");
130 return qdict;
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); \
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); \
151 static void assert_type_full(QTestState *qts)
153 const char *error;
154 QDict *resp;
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);
167 static void assert_bad_props(QTestState *qts, const char *cpu_type)
169 const char *error;
170 QDict *resp;
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);
185 static uint64_t resp_get_sve_vls(QDict *resp)
187 QDict *props;
188 const QDictEntry *e;
189 uint64_t vls = 0;
190 int n = 0;
192 g_assert(resp);
193 g_assert(resp_has_props(resp));
195 props = resp_get_props(resp);
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;
203 bits = g_ascii_strtoll(&e->key[3], &endptr, 10);
204 if (!bits || *endptr != '\0') {
205 continue;
208 if (qdict_get_bool(props, e->key)) {
209 vls |= BIT_ULL((bits / 128) - 1);
211 ++n;
215 g_assert(n == SVE_MAX_VQ);
217 return vls;
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); \
229 static void sve_tests_default(QTestState *qts, const char *cpu_type)
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.
236 assert_sve_vls(qts, cpu_type, BIT_ULL(SVE_MAX_VQ) - 1, NULL);
238 /* With SVE off, all vector lengths should also be off. */
239 assert_sve_vls(qts, cpu_type, 0, "{ 'sve': false }");
241 /* With SVE on, we must have at least one vector length enabled. */
242 assert_error(qts, cpu_type, "cannot disable sve128", "{ 'sve128': false }");
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 }");
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 * ---------------------------------------------------------------------
263 * [*] "MUST" means this requirement must already be satisfied,
264 * otherwise 'max_vq' couldn't itself be enabled.
266 * [**] Not testable with the QMP interface, only with the command line.
269 /* max_vq := 8 */
270 assert_sve_vls(qts, cpu_type, 0x8b, "{ 'sve1024': true }");
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 }");
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 }");
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 }");
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.
293 assert_sve_vls(qts, cpu_type, 0x7, "{ 'sve512': false }");
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 }");
302 static void sve_tests_sve_max_vq_8(const void *data)
304 QTestState *qts;
306 qts = qtest_init(MACHINE "-cpu max,sve-max-vq=8");
308 assert_sve_vls(qts, "max", BIT_ULL(8) - 1, NULL);
311 * Disabling the max-vq set by sve-max-vq is not allowed, but
312 * of course enabling it is OK.
314 assert_error(qts, "max", "cannot disable sve1024", "{ 'sve1024': false }");
315 assert_sve_vls(qts, "max", 0xff, "{ 'sve1024': true }");
318 * Enabling anything larger than max-vq set by sve-max-vq is not
319 * allowed, but of course disabling everything larger is OK.
321 assert_error(qts, "max", "cannot enable sve1152", "{ 'sve1152': true }");
322 assert_sve_vls(qts, "max", 0xff, "{ 'sve1152': false }");
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.
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 }");
334 qtest_quit(qts);
337 static void sve_tests_sve_off(const void *data)
339 QTestState *qts;
341 qts = qtest_init(MACHINE "-cpu max,sve=off");
343 /* SVE is off, so the map should be empty. */
344 assert_sve_vls(qts, "max", 0, NULL);
346 /* The map stays empty even if we turn lengths off. */
347 assert_sve_vls(qts, "max", 0, "{ 'sve128': false }");
349 /* It's an error to enable lengths when SVE is off. */
350 assert_error(qts, "max", "cannot enable sve128", "{ 'sve128': true }");
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 }");
355 /* Or enable SVE with just specific vector lengths. */
356 assert_sve_vls(qts, "max", 0x3,
357 "{ 'sve': true, 'sve128': true, 'sve256': true }");
359 qtest_quit(qts);
362 static void test_query_cpu_model_expansion(const void *data)
364 QTestState *qts;
366 qts = qtest_init(MACHINE "-cpu max");
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);
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");
382 if (g_str_equal(qtest_get_arch(), "aarch64")) {
383 assert_has_feature(qts, "max", "aarch64");
384 assert_has_feature(qts, "max", "sve");
385 assert_has_feature(qts, "max", "sve128");
386 assert_has_feature(qts, "cortex-a57", "pmu");
387 assert_has_feature(qts, "cortex-a57", "aarch64");
389 sve_tests_default(qts, "max");
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 }");
399 qtest_quit(qts);
402 static void test_query_cpu_model_expansion_kvm(const void *data)
404 QTestState *qts;
406 qts = qtest_init(MACHINE_KVM "-cpu max");
409 * These tests target the 'host' CPU type, so KVM must be enabled.
411 if (!kvm_enabled(qts)) {
412 qtest_quit(qts);
413 return;
416 if (g_str_equal(qtest_get_arch(), "aarch64")) {
417 assert_has_feature(qts, "host", "aarch64");
418 assert_has_feature(qts, "host", "pmu");
420 assert_error(qts, "cortex-a15",
421 "We cannot guarantee the CPU type 'cortex-a15' works "
422 "with KVM on this host", NULL);
423 } else {
424 assert_has_not_feature(qts, "host", "aarch64");
425 assert_has_not_feature(qts, "host", "pmu");
428 qtest_quit(qts);
431 int main(int argc, char **argv)
433 g_test_init(&argc, &argv, NULL);
435 qtest_add_data_func("/arm/query-cpu-model-expansion",
436 NULL, test_query_cpu_model_expansion);
439 * For now we only run KVM specific tests with AArch64 QEMU in
440 * order avoid attempting to run an AArch32 QEMU with KVM on
441 * AArch64 hosts. That won't work and isn't easy to detect.
443 if (g_str_equal(qtest_get_arch(), "aarch64")) {
444 qtest_add_data_func("/arm/kvm/query-cpu-model-expansion",
445 NULL, test_query_cpu_model_expansion_kvm);
448 if (g_str_equal(qtest_get_arch(), "aarch64")) {
449 qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-max-vq-8",
450 NULL, sve_tests_sve_max_vq_8);
451 qtest_add_data_func("/arm/max/query-cpu-model-expansion/sve-off",
452 NULL, sve_tests_sve_off);
455 return g_test_run();