refactor(spm): test_sve_vectors_preserved

Refactor test_sve_vectors_preserved to probe the possible vector length
that the platform implements.
Perform normal/secure world switches and check that SVE state is
preserved for all possible vector lengths.

Signed-off-by: Olivier Deprez <olivier.deprez@arm.com>
Change-Id: I90f56d9ae97624ceaaae5a4818045922961e5066
diff --git a/tftf/tests/runtime_services/secure_service/test_spm_simd.c b/tftf/tests/runtime_services/secure_service/test_spm_simd.c
index cfc931f..6013744 100644
--- a/tftf/tests/runtime_services/secure_service/test_spm_simd.c
+++ b/tftf/tests/runtime_services/secure_service/test_spm_simd.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2024, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -38,7 +38,7 @@
 	/**********************************************************************
 	 * Verify that FF-A is there and that it has the correct version.
 	 **********************************************************************/
-	CHECK_SPMC_TESTING_SETUP(1, 1, expected_sp_uuids);
+	CHECK_SPMC_TESTING_SETUP(1, 2, expected_sp_uuids);
 
 	fpu_state_write_rand(&g_fpu_state_write);
 	struct ffa_value ret = cactus_req_simd_fill_send_cmd(SENDER, RECEIVER);
@@ -70,23 +70,13 @@
 	return TEST_RESULT_SUCCESS;
 }
 
-/*
- * Tests that SVE vectors are preserved during the context switches between
- * normal world and the secure world.
- * Fills the SVE vectors with known values, requests SP to fill the vectors
- * with a different values, checks that the context is restored on return.
- */
-test_result_t test_sve_vectors_preserved(void)
+static test_result_t test_sve_vectors_preserved_impl(uint8_t vq)
 {
-	uint64_t vl;
 	uint8_t *sve_vector;
+	uint64_t vl;
 
-	SKIP_TEST_IF_SVE_NOT_SUPPORTED();
-
-	/**********************************************************************
-	 * Verify that FF-A is there and that it has the correct version.
-	 **********************************************************************/
-	CHECK_SPMC_TESTING_SETUP(1, 1, expected_sp_uuids);
+	/* Configure requested VL. */
+	sve_config_vq(vq);
 
 	/*
 	 * Clear SVE vectors buffers used to compare the SVE state before calling
@@ -95,15 +85,12 @@
 	memset(sve_vectors_input, 0, sizeof(sve_vectors_input));
 	memset(sve_vectors_output, 0, sizeof(sve_vectors_output));
 
-	/* Set ZCR_EL2.LEN to implemented VL (constrained by EL3). */
-	write_zcr_el2(0xf);
-	isb();
-
-	/* Get the implemented VL. */
-	vl = sve_rdvl_1();
+	/* Vector length in bytes from vq. */
+	vl = SVE_VQ_TO_BYTES(vq);
 
 	/* Fill each vector for the VL size with a fixed pattern. */
 	sve_vector = (uint8_t *) sve_vectors_input;
+
 	for (uint32_t vector_num = 0U; vector_num < SVE_NUM_VECTORS; vector_num++) {
 		memset(sve_vector, 0x11 * (vector_num + 1), vl);
 		sve_vector += vl;
@@ -126,6 +113,11 @@
 		return TEST_RESULT_FAIL;
 	}
 
+	/* Check ZCR_EL2 was preserved. */
+	if (sve_read_zcr_elx() != vq) {
+		return TEST_RESULT_FAIL;
+	}
+
 	/* Get the SVE vectors state after returning to normal world. */
 	sve_z_regs_read(&sve_vectors_output);
 
@@ -137,6 +129,53 @@
 	return TEST_RESULT_SUCCESS;
 }
 
+static test_result_t helper_test_sve(test_result_t (*func)(uint8_t vq))
+{
+	uint32_t bitmap, vl_bitmap;
+	uint32_t vq = 0;
+	test_result_t ret;
+
+	SKIP_TEST_IF_SVE_NOT_SUPPORTED();
+
+	/**********************************************************************
+	 * Verify that FF-A is there and that it has the correct version.
+	 **********************************************************************/
+	CHECK_SPMC_TESTING_SETUP(1, 2, expected_sp_uuids);
+
+	/*
+	 * Check SVE state is preserved normal/secure accross world switches for
+	 * the discovered vector lengths.
+	 */
+	vl_bitmap = sve_probe_vl(SVE_VQ_ARCH_MAX);
+	for (bitmap = vl_bitmap; bitmap != 0U; bitmap >>= 1) {
+		if ((bitmap & 1) != 0) {
+			VERBOSE("Test VL %u bits.\n", SVE_VQ_TO_BITS(vq));
+
+			ret = func(vq);
+			if (ret != TEST_RESULT_SUCCESS) {
+				return ret;
+			}
+		}
+		vq++;
+	}
+
+	return TEST_RESULT_SUCCESS;
+}
+
+/*
+ * Tests that SVE vectors are preserved during the context switches between
+ * normal world and the secure world.
+ * Probe all possible vector length that the platform implements.
+ * For each vector length value:
+ * -Fill SVE vectors with known values.
+ * -Request SP to fill NEON vectors with different values.
+ * -Check the SVE context is restored on return.
+ */
+test_result_t test_sve_vectors_preserved(void)
+{
+	return helper_test_sve(test_sve_vectors_preserved_impl);
+}
+
 /*
  * Sends SIMD fill command to Cactus SP
  * Returns:
@@ -174,7 +213,7 @@
 	/**********************************************************************
 	 * Verify that FF-A is there and that it has the correct version.
 	 **********************************************************************/
-	CHECK_SPMC_TESTING_SETUP(1, 1, expected_sp_uuids);
+	CHECK_SPMC_TESTING_SETUP(1, 2, expected_sp_uuids);
 
 	val = 2 * SVE_TEST_ITERATIONS;
 
@@ -184,7 +223,7 @@
 	}
 
 	/* Set ZCR_EL2.LEN to implemented VL (constrained by EL3). */
-	write_zcr_el2(0xf);
+	write_zcr_el2(SVE_VQ_ARCH_MAX);
 	isb();
 
 	for (unsigned int i = 0; i < SVE_TEST_ITERATIONS; i++) {