Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 1 | /* |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 2 | * Copyright (c) 2023-2025, Arm Limited. All rights reserved. |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 3 | * SPDX-License-Identifier: BSD-3-Clause |
| 4 | */ |
| 5 | |
| 6 | #include <arch.h> |
| 7 | #include <arch_features.h> |
| 8 | #include <arch_helpers.h> |
| 9 | #include <assert.h> |
| 10 | #include <debug.h> |
Arunachalam Ganapathy | c1136a8 | 2023-04-12 15:24:44 +0100 | [diff] [blame] | 11 | #include <stdlib.h> |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 12 | |
Arunachalam Ganapathy | 73949a2 | 2023-06-05 12:01:05 +0100 | [diff] [blame] | 13 | #include <sync.h> |
Arunachalam Ganapathy | f369717 | 2023-09-04 15:04:46 +0100 | [diff] [blame] | 14 | #include <lib/extensions/fpu.h> |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 15 | #include <lib/extensions/sve.h> |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 16 | #include <realm_helpers.h> |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 17 | |
Arunachalam Ganapathy | 1768e59 | 2023-05-23 13:28:38 +0100 | [diff] [blame] | 18 | #include <host_realm_simd.h> |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 19 | #include <host_shared_data.h> |
| 20 | |
Arunachalam Ganapathy | c1136a8 | 2023-04-12 15:24:44 +0100 | [diff] [blame] | 21 | #define RL_SVE_OP_ARRAYSIZE 512U |
| 22 | #define SVE_TEST_ITERATIONS 4U |
| 23 | |
| 24 | static int rl_sve_op_1[RL_SVE_OP_ARRAYSIZE]; |
| 25 | static int rl_sve_op_2[RL_SVE_OP_ARRAYSIZE]; |
| 26 | |
Arunachalam Ganapathy | 0358997 | 2023-08-30 11:04:51 +0100 | [diff] [blame] | 27 | static sve_z_regs_t rl_sve_z_regs_write; |
Arunachalam Ganapathy | f369717 | 2023-09-04 15:04:46 +0100 | [diff] [blame] | 28 | static sve_z_regs_t rl_sve_z_regs_read; |
| 29 | |
| 30 | static sve_p_regs_t rl_sve_p_regs_write; |
| 31 | static sve_p_regs_t rl_sve_p_regs_read; |
| 32 | |
| 33 | static sve_ffr_regs_t rl_sve_ffr_regs_write; |
| 34 | static sve_ffr_regs_t rl_sve_ffr_regs_read; |
| 35 | |
| 36 | static fpu_cs_regs_t rl_fpu_cs_regs_write; |
| 37 | static fpu_cs_regs_t rl_fpu_cs_regs_read; |
Arunachalam Ganapathy | 5270d01 | 2023-04-19 14:53:42 +0100 | [diff] [blame] | 38 | |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 39 | /* Returns the maximum supported VL. This test is called only by sve Realm */ |
| 40 | bool test_realm_sve_rdvl(void) |
| 41 | { |
Shruti Gupta | 550e3e8 | 2023-08-16 13:20:11 +0100 | [diff] [blame] | 42 | host_shared_data_t *sd = realm_get_my_shared_structure(); |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 43 | struct sve_cmd_rdvl *output; |
| 44 | |
| 45 | assert(is_armv8_2_sve_present()); |
| 46 | |
| 47 | output = (struct sve_cmd_rdvl *)sd->realm_cmd_output_buffer; |
| 48 | memset((void *)output, 0, sizeof(struct sve_cmd_rdvl)); |
| 49 | |
| 50 | sve_config_vq(SVE_VQ_ARCH_MAX); |
Arunachalam Ganapathy | 0358997 | 2023-08-30 11:04:51 +0100 | [diff] [blame] | 51 | output->rdvl = sve_rdvl_1(); |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Reads and returns the ID_AA64PFR0_EL1 and ID_AA64ZFR0_EL1 registers |
| 58 | * This test could be called from sve or non-sve Realm |
| 59 | */ |
| 60 | bool test_realm_sve_read_id_registers(void) |
| 61 | { |
Shruti Gupta | 550e3e8 | 2023-08-16 13:20:11 +0100 | [diff] [blame] | 62 | host_shared_data_t *sd = realm_get_my_shared_structure(); |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 63 | struct sve_cmd_id_regs *output; |
| 64 | |
| 65 | output = (struct sve_cmd_id_regs *)sd->realm_cmd_output_buffer; |
| 66 | memset((void *)output, 0, sizeof(struct sve_cmd_id_regs)); |
| 67 | |
Shruti Gupta | a276b20 | 2023-12-18 10:07:43 +0000 | [diff] [blame] | 68 | realm_printf("reading ID registers: ID_AA64PFR0_EL1, " |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 69 | " ID_AA64ZFR0_EL1\n"); |
| 70 | output->id_aa64pfr0_el1 = read_id_aa64pfr0_el1(); |
| 71 | output->id_aa64zfr0_el1 = read_id_aa64zfr0_el1(); |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Probes all VLs and return the bitmap with the bit set for each corresponding |
| 78 | * valid VQ. This test is called only by sve Realm |
| 79 | */ |
| 80 | bool test_realm_sve_probe_vl(void) |
| 81 | { |
Shruti Gupta | 550e3e8 | 2023-08-16 13:20:11 +0100 | [diff] [blame] | 82 | host_shared_data_t *sd = realm_get_my_shared_structure(); |
Arunachalam Ganapathy | 0bbdc2d | 2023-04-05 15:30:18 +0100 | [diff] [blame] | 83 | struct sve_cmd_probe_vl *output; |
| 84 | |
| 85 | assert(is_armv8_2_sve_present()); |
| 86 | |
| 87 | output = (struct sve_cmd_probe_vl *)&sd->realm_cmd_output_buffer; |
| 88 | memset((void *)output, 0, sizeof(struct sve_cmd_probe_vl)); |
| 89 | |
| 90 | /* Probe all VLs */ |
| 91 | output->vl_bitmap = sve_probe_vl(SVE_VQ_ARCH_MAX); |
| 92 | |
| 93 | return true; |
| 94 | } |
Arunachalam Ganapathy | c1136a8 | 2023-04-12 15:24:44 +0100 | [diff] [blame] | 95 | |
| 96 | bool test_realm_sve_ops(void) |
| 97 | { |
| 98 | int val, i; |
| 99 | |
| 100 | assert(is_armv8_2_sve_present()); |
| 101 | |
| 102 | /* get at random value to do sve_subtract */ |
| 103 | val = rand(); |
| 104 | for (i = 0; i < RL_SVE_OP_ARRAYSIZE; i++) { |
| 105 | rl_sve_op_1[i] = val - i; |
| 106 | rl_sve_op_2[i] = 1; |
| 107 | } |
| 108 | |
| 109 | for (i = 0; i < SVE_TEST_ITERATIONS; i++) { |
| 110 | /* Config Realm with random SVE length */ |
| 111 | sve_config_vq(SVE_GET_RANDOM_VQ); |
| 112 | |
| 113 | /* Perform SVE operations, without world switch */ |
| 114 | sve_subtract_arrays(rl_sve_op_1, rl_sve_op_1, rl_sve_op_2, |
| 115 | RL_SVE_OP_ARRAYSIZE); |
| 116 | } |
| 117 | |
| 118 | /* Check result of SVE operations. */ |
| 119 | for (i = 0; i < RL_SVE_OP_ARRAYSIZE; i++) { |
| 120 | if (rl_sve_op_1[i] != (val - i - SVE_TEST_ITERATIONS)) { |
Shruti Gupta | a276b20 | 2023-12-18 10:07:43 +0000 | [diff] [blame] | 121 | realm_printf("SVE ops failed\n"); |
Arunachalam Ganapathy | c1136a8 | 2023-04-12 15:24:44 +0100 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
Arunachalam Ganapathy | 5270d01 | 2023-04-19 14:53:42 +0100 | [diff] [blame] | 128 | |
| 129 | /* Fill SVE Z registers with known pattern */ |
| 130 | bool test_realm_sve_fill_regs(void) |
| 131 | { |
Arunachalam Ganapathy | 5270d01 | 2023-04-19 14:53:42 +0100 | [diff] [blame] | 132 | assert(is_armv8_2_sve_present()); |
| 133 | |
| 134 | /* Config Realm with max SVE length */ |
| 135 | sve_config_vq(SVE_VQ_ARCH_MAX); |
Arunachalam Ganapathy | 5270d01 | 2023-04-19 14:53:42 +0100 | [diff] [blame] | 136 | |
Arunachalam Ganapathy | f369717 | 2023-09-04 15:04:46 +0100 | [diff] [blame] | 137 | sve_z_regs_write_rand(&rl_sve_z_regs_write); |
| 138 | sve_p_regs_write_rand(&rl_sve_p_regs_write); |
| 139 | sve_ffr_regs_write_rand(&rl_sve_ffr_regs_write); |
| 140 | |
| 141 | /* fpcr, fpsr common registers */ |
| 142 | fpu_cs_regs_write_rand(&rl_fpu_cs_regs_write); |
Arunachalam Ganapathy | 5270d01 | 2023-04-19 14:53:42 +0100 | [diff] [blame] | 143 | |
| 144 | return true; |
| 145 | } |
Arunachalam Ganapathy | 73949a2 | 2023-06-05 12:01:05 +0100 | [diff] [blame] | 146 | |
Arunachalam Ganapathy | f369717 | 2023-09-04 15:04:46 +0100 | [diff] [blame] | 147 | /* Compare SVE Z registers with last filled in values */ |
| 148 | bool test_realm_sve_cmp_regs(void) |
| 149 | { |
| 150 | bool rc = true; |
| 151 | uint64_t bit_map; |
| 152 | |
| 153 | assert(is_armv8_2_sve_present()); |
| 154 | |
| 155 | memset(&rl_sve_z_regs_read, 0, sizeof(rl_sve_z_regs_read)); |
| 156 | memset(&rl_sve_p_regs_read, 0, sizeof(rl_sve_p_regs_read)); |
| 157 | memset(&rl_sve_ffr_regs_read, 0, sizeof(rl_sve_ffr_regs_read)); |
| 158 | |
| 159 | /* Read all SVE registers */ |
| 160 | sve_z_regs_read(&rl_sve_z_regs_read); |
| 161 | sve_p_regs_read(&rl_sve_p_regs_read); |
| 162 | sve_ffr_regs_read(&rl_sve_ffr_regs_read); |
| 163 | |
| 164 | /* Compare the read values with last written values */ |
| 165 | bit_map = sve_z_regs_compare(&rl_sve_z_regs_write, &rl_sve_z_regs_read); |
| 166 | if (bit_map) { |
| 167 | rc = false; |
| 168 | } |
| 169 | |
| 170 | bit_map = sve_p_regs_compare(&rl_sve_p_regs_write, &rl_sve_p_regs_read); |
| 171 | if (bit_map) { |
| 172 | rc = false; |
| 173 | } |
| 174 | |
| 175 | bit_map = sve_ffr_regs_compare(&rl_sve_ffr_regs_write, |
| 176 | &rl_sve_ffr_regs_read); |
| 177 | if (bit_map) { |
| 178 | rc = false; |
| 179 | } |
| 180 | |
| 181 | /* fpcr, fpsr common registers */ |
| 182 | fpu_cs_regs_read(&rl_fpu_cs_regs_read); |
| 183 | if (fpu_cs_regs_compare(&rl_fpu_cs_regs_write, &rl_fpu_cs_regs_read)) { |
Shruti Gupta | a276b20 | 2023-12-18 10:07:43 +0000 | [diff] [blame] | 184 | ERROR("FPCR/FPSR mismatch\n"); |
Arunachalam Ganapathy | f369717 | 2023-09-04 15:04:46 +0100 | [diff] [blame] | 185 | rc = false; |
| 186 | } |
| 187 | |
| 188 | return rc; |
| 189 | } |
| 190 | |
Arunachalam Ganapathy | 73949a2 | 2023-06-05 12:01:05 +0100 | [diff] [blame] | 191 | /* Check if Realm gets undefined abort when it accesses SVE functionality */ |
| 192 | bool test_realm_sve_undef_abort(void) |
| 193 | { |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 194 | realm_reset_undef_abort_count(); |
Arunachalam Ganapathy | 73949a2 | 2023-06-05 12:01:05 +0100 | [diff] [blame] | 195 | |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 196 | /* Install exception handler to catch undefined abort */ |
Arunachalam Ganapathy | 73949a2 | 2023-06-05 12:01:05 +0100 | [diff] [blame] | 197 | register_custom_sync_exception_handler(&realm_sync_exception_handler); |
| 198 | (void)sve_rdvl_1(); |
| 199 | unregister_custom_sync_exception_handler(); |
| 200 | |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 201 | return (realm_get_undef_abort_count() != 0U); |
Arunachalam Ganapathy | 73949a2 | 2023-06-05 12:01:05 +0100 | [diff] [blame] | 202 | } |
Arunachalam Ganapathy | 1768e59 | 2023-05-23 13:28:38 +0100 | [diff] [blame] | 203 | |
| 204 | /* Reads and returns the ID_AA64PFR1_EL1 and ID_AA64SMFR0_EL1 registers */ |
| 205 | bool test_realm_sme_read_id_registers(void) |
| 206 | { |
| 207 | host_shared_data_t *sd = realm_get_my_shared_structure(); |
| 208 | struct sme_cmd_id_regs *output; |
| 209 | |
| 210 | output = (struct sme_cmd_id_regs *)sd->realm_cmd_output_buffer; |
| 211 | memset((void *)output, 0, sizeof(struct sme_cmd_id_regs)); |
| 212 | |
Shruti Gupta | a276b20 | 2023-12-18 10:07:43 +0000 | [diff] [blame] | 213 | realm_printf("reading ID registers: ID_AA64PFR1_EL1, " |
Arunachalam Ganapathy | 1768e59 | 2023-05-23 13:28:38 +0100 | [diff] [blame] | 214 | " ID_AA64SMFR0_EL1\n"); |
| 215 | |
| 216 | output->id_aa64pfr1_el1 = read_id_aa64pfr1_el1(); |
| 217 | output->id_aa64smfr0_el1 = read_id_aa64smfr0_el1(); |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /* Check if Realm gets undefined abort when it access SME functionality */ |
| 223 | bool test_realm_sme_undef_abort(void) |
| 224 | { |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 225 | realm_reset_undef_abort_count(); |
Arunachalam Ganapathy | 1768e59 | 2023-05-23 13:28:38 +0100 | [diff] [blame] | 226 | |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 227 | /* Install exception handler to catch undefined abort */ |
Arunachalam Ganapathy | 1768e59 | 2023-05-23 13:28:38 +0100 | [diff] [blame] | 228 | register_custom_sync_exception_handler(&realm_sync_exception_handler); |
| 229 | (void)read_svcr(); |
| 230 | unregister_custom_sync_exception_handler(); |
| 231 | |
Javier Almansa Sobrino | 82cd82e | 2025-01-17 17:37:42 +0000 | [diff] [blame] | 232 | return (realm_get_undef_abort_count() != 0U); |
Arunachalam Ganapathy | 1768e59 | 2023-05-23 13:28:38 +0100 | [diff] [blame] | 233 | } |