blob: 64391ab8a729a4031f816828bc1d459eec03463f [file] [log] [blame]
johpow0150ccb552020-11-10 19:22:13 -06001/*
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +00002 * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
johpow0150ccb552020-11-10 19:22:13 -06003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11#include <arch_features.h>
12#include <arch_helpers.h>
13#include <lib/extensions/sme.h>
14#include <test_helpers.h>
15#include <tftf_lib.h>
16
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000017#ifdef __aarch64__
18
19/* Global buffers*/
Olivier Depreza61927e2023-06-01 17:16:00 +020020static __aligned(16) uint64_t ZA_In_vector[8] = {0xaaff, 0xbbff, 0xccff, 0xddff, 0xeeff,
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000021 0xffff, 0xff00, 0xff00};
Olivier Depreza61927e2023-06-01 17:16:00 +020022static __aligned(16) uint64_t ZA_Out_vector[8] = {0};
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000023
24/**
25 * sme_zero_ZA
26 * ZER0 : Zero a list of upto eight 64bit element ZA tiles.
27 * ZERO {<mask>} , where mask=ff, to clear all the 8, 64 bit elements.
28 */
29static void sme_zero_ZA(void)
30{
31 /**
32 * Due to the lack of support from the toolchain, instruction
33 * opcodes are used here.
34 * Manual Encoding Instruction, to Zero all the tiles of ZA array.
35 *
36 * TODO: Further, once the toolchain adds support for SME features
37 * this could be replaced with the actual instruction ZERO { <mask>}.
38 */
39 asm volatile(".inst 0xc008000f" : : : );
40}
41
42/**
43 * This function compares two buffers/vector elements
44 * Inputs: uint64_t *ZA_In_vector, ZA_Out_vector
45 * @return true : If both are equal
46 * @return false : If both are not equal
47 */
48static bool sme_cmp_vector(const uint64_t *ZA_In_vector, const uint64_t *ZA_Out_vector)
49{
50 bool ret = true;
51
52 for (int i = 0; i < (MAX_VL_B/8); i++) {
53 if (ZA_In_vector[i] != ZA_Out_vector[i]) {
54 ret = false;
55 }
56 }
57
58 return ret;
59}
60
61#endif /* __aarch64__ */
62
johpow0150ccb552020-11-10 19:22:13 -060063test_result_t test_sme_support(void)
64{
65 /* SME is an AArch64-only feature.*/
66 SKIP_TEST_IF_AARCH32();
67
68#ifdef __aarch64__
69 u_register_t reg;
70 unsigned int current_vector_len;
71 unsigned int requested_vector_len;
72 unsigned int len_max;
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000073 unsigned int __unused svl_max = 0U;
johpow0150ccb552020-11-10 19:22:13 -060074
75 /* Skip the test if SME is not supported. */
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000076 SKIP_TEST_IF_SME_NOT_SUPPORTED();
johpow0150ccb552020-11-10 19:22:13 -060077
78 /* Enable SME for use at NS EL2. */
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000079 sme_enable();
johpow0150ccb552020-11-10 19:22:13 -060080
81 /* Make sure TPIDR2_EL0 is accessible. */
82 write_tpidr2_el0(0);
83 if (read_tpidr2_el0() != 0) {
84 ERROR("Could not read TPIDR2_EL0.\n");
85 return TEST_RESULT_FAIL;
86 }
87 write_tpidr2_el0(0xb0bafe77);
88 if (read_tpidr2_el0() != 0xb0bafe77) {
89 ERROR("Could not write TPIDR2_EL0.\n");
90 return TEST_RESULT_FAIL;
91 }
92
johpow0150ccb552020-11-10 19:22:13 -060093 /*
94 * Iterate through values for LEN to detect supported vector lengths.
johpow0150ccb552020-11-10 19:22:13 -060095 */
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000096
97 /* Entering Streaming SVE mode */
98 sme_smstart(SMSTART_SM);
johpow0150ccb552020-11-10 19:22:13 -060099
100 /* Write SMCR_EL2 with the LEN max to find implemented width. */
101 write_smcr_el2(SME_SMCR_LEN_MAX);
102 len_max = (unsigned int)read_smcr_el2();
103 VERBOSE("Maximum SMCR_EL2.LEN value: 0x%x\n", len_max);
104 VERBOSE("Enumerating supported vector lengths...\n");
105 for (unsigned int i = 0; i <= len_max; i++) {
106 /* Load new value into SMCR_EL2.LEN */
107 reg = read_smcr_el2();
108 reg &= ~(SMCR_ELX_LEN_MASK << SMCR_ELX_LEN_SHIFT);
109 reg |= (i << SMCR_ELX_LEN_SHIFT);
110 write_smcr_el2(reg);
111
112 /* Compute current and requested vector lengths in bits. */
113 current_vector_len = ((unsigned int)sme_rdvl_1() * 8U);
114 requested_vector_len = (i+1U)*128U;
115
116 /*
117 * We count down from the maximum SMLEN value, so if the values
118 * match, we've found the largest supported value for SMLEN.
119 */
120 if (current_vector_len == requested_vector_len) {
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +0000121 svl_max = current_vector_len;
johpow0150ccb552020-11-10 19:22:13 -0600122 VERBOSE("SUPPORTED: %u bits (LEN=%u)\n", requested_vector_len, i);
123 } else {
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +0000124 VERBOSE("NOT SUPPORTED: %u bits (LEN=%u)\n", requested_vector_len, i);
johpow0150ccb552020-11-10 19:22:13 -0600125 }
126 }
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +0000127
128 INFO("Largest Supported Streaming Vector Length(SVL): %u bits \n", svl_max);
129
130 /* Exiting Streaming SVE mode */
131 sme_smstop(SMSTOP_SM);
132
133 /**
134 * Perform/Execute SME Instructions.
135 * SME Data processing instructions LDR, STR, and ZERO instructions that
136 * access the SME ZA storage are legal only if ZA is enabled.
137 */
138
139 /* Enable SME ZA Array Storage */
140 sme_smstart(SMSTART_ZA);
141
142 /* LDR : Load vector to ZA Array */
143 sme_vector_to_ZA(ZA_In_vector);
144
145 /* STR : Store Vector from ZA Array. */
146 sme_ZA_to_vector(ZA_Out_vector);
147
148 /* Compare both vectors to ensure load and store instructions have
149 * executed precisely.
150 */
151 if (!sme_cmp_vector(ZA_In_vector, ZA_Out_vector)) {
152 return TEST_RESULT_FAIL;
153 }
154
155 /* Zero or clear the entire ZA Array Storage/Tile */
156 sme_zero_ZA();
157
158 /* Disable the SME ZA array storage. */
159 sme_smstop(SMSTOP_ZA);
johpow0150ccb552020-11-10 19:22:13 -0600160
161 /* If FEAT_SME_FA64 then attempt to execute an illegal instruction. */
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +0000162 if (is_feat_sme_fa64_supported()) {
johpow0150ccb552020-11-10 19:22:13 -0600163 VERBOSE("FA64 supported, trying illegal instruction.\n");
164 sme_try_illegal_instruction();
165 }
166
167 return TEST_RESULT_SUCCESS;
168#endif /* __aarch64__ */
169}