Abdellatif El Khlifi | a2f5c91 | 2023-08-04 14:33:42 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Functional tests for UCLASS_FFA class |
| 4 | * |
| 5 | * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 6 | * |
| 7 | * Authors: |
| 8 | * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <console.h> |
| 13 | #include <dm.h> |
| 14 | #include <asm/sandbox_arm_ffa.h> |
| 15 | #include <asm/sandbox_arm_ffa_priv.h> |
| 16 | #include <dm/test.h> |
| 17 | #include <test/test.h> |
| 18 | #include <test/ut.h> |
| 19 | |
| 20 | /* Functional tests for the UCLASS_FFA */ |
| 21 | |
| 22 | static int check_fwk_version(struct ffa_priv *uc_priv, struct unit_test_state *uts) |
| 23 | { |
| 24 | struct ffa_sandbox_data func_data; |
| 25 | u32 fwk_version = 0; |
| 26 | |
| 27 | func_data.data0 = &fwk_version; |
| 28 | func_data.data0_size = sizeof(fwk_version); |
| 29 | ut_assertok(sandbox_query_ffa_emul_state(FFA_VERSION, &func_data)); |
| 30 | ut_asserteq(uc_priv->fwk_version, fwk_version); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int check_endpoint_id(struct ffa_priv *uc_priv, struct unit_test_state *uts) |
| 36 | { |
| 37 | ut_asserteq(0, uc_priv->id); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static int check_rxtxbuf(struct ffa_priv *uc_priv, struct unit_test_state *uts) |
| 43 | { |
| 44 | ut_assertnonnull(uc_priv->pair.rxbuf); |
| 45 | ut_assertnonnull(uc_priv->pair.txbuf); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int check_features(struct ffa_priv *uc_priv, struct unit_test_state *uts) |
| 51 | { |
| 52 | ut_assert(uc_priv->pair.rxtx_min_pages == RXTX_4K || |
| 53 | uc_priv->pair.rxtx_min_pages == RXTX_16K || |
| 54 | uc_priv->pair.rxtx_min_pages == RXTX_64K); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int check_rxbuf_mapped_flag(u32 queried_func_id, |
| 60 | u8 rxbuf_mapped, |
| 61 | struct unit_test_state *uts) |
| 62 | { |
| 63 | switch (queried_func_id) { |
| 64 | case FFA_RXTX_MAP: |
| 65 | ut_asserteq(1, rxbuf_mapped); |
| 66 | break; |
| 67 | case FFA_RXTX_UNMAP: |
| 68 | ut_asserteq(0, rxbuf_mapped); |
| 69 | break; |
| 70 | default: |
| 71 | ut_assert(false); |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int check_rxbuf_release_flag(u8 rxbuf_owned, struct unit_test_state *uts) |
| 78 | { |
| 79 | ut_asserteq(0, rxbuf_owned); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int test_ffa_msg_send_direct_req(u16 part_id, struct unit_test_state *uts) |
| 85 | { |
| 86 | struct ffa_send_direct_data msg; |
| 87 | u8 cnt; |
| 88 | struct udevice *dev; |
| 89 | |
| 90 | ut_assertok(uclass_first_device_err(UCLASS_FFA, &dev)); |
| 91 | |
| 92 | ut_assertok(ffa_sync_send_receive(dev, part_id, &msg, 1)); |
| 93 | |
| 94 | for (cnt = 0; cnt < sizeof(struct ffa_send_direct_data) / sizeof(u64); cnt++) |
| 95 | ut_asserteq_64(-1UL, ((u64 *)&msg)[cnt]); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int test_partitions_and_comms(const char *service_uuid, |
| 101 | struct unit_test_state *uts) |
| 102 | { |
| 103 | struct ffa_partition_desc *descs; |
| 104 | u32 count, i, j, valid_sps = 0; |
| 105 | struct udevice *dev; |
| 106 | struct ffa_sandbox_data func_data; |
| 107 | struct ffa_partitions *partitions; |
| 108 | |
| 109 | ut_assertok(uclass_first_device_err(UCLASS_FFA, &dev)); |
| 110 | |
| 111 | /* Get from the driver the count and information of the SPs matching the UUID */ |
| 112 | ut_assertok(ffa_partition_info_get(dev, service_uuid, &count, &descs)); |
| 113 | |
| 114 | /* Make sure the count is correct */ |
| 115 | ut_asserteq(SANDBOX_SP_COUNT_PER_VALID_SERVICE, count); |
| 116 | |
| 117 | /* SPs found , verify the partitions information */ |
| 118 | |
| 119 | func_data.data0 = &partitions; |
| 120 | func_data.data0_size = sizeof(struct ffa_partitions *); |
| 121 | ut_assertok(sandbox_query_ffa_emul_state(FFA_PARTITION_INFO_GET, &func_data)); |
| 122 | |
| 123 | for (i = 0; i < count ; i++) { |
| 124 | for (j = 0; |
| 125 | j < partitions->count; |
| 126 | j++) { |
| 127 | if (descs[i].info.id == |
| 128 | partitions->descs[j].info.id) { |
| 129 | valid_sps++; |
| 130 | ut_asserteq_mem(&descs[i], |
| 131 | &partitions->descs[j], |
| 132 | sizeof(struct ffa_partition_desc)); |
| 133 | /* Send and receive data from the current partition */ |
| 134 | test_ffa_msg_send_direct_req(descs[i].info.id, uts); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* Verify expected partitions found in the emulated secure world */ |
| 140 | ut_asserteq(SANDBOX_SP_COUNT_PER_VALID_SERVICE, valid_sps); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int dm_test_ffa_ack(struct unit_test_state *uts) |
| 146 | { |
| 147 | struct ffa_priv *uc_priv; |
| 148 | struct ffa_sandbox_data func_data; |
| 149 | u8 rxbuf_flag = 0; |
| 150 | const char *svc1_uuid = SANDBOX_SERVICE1_UUID; |
| 151 | const char *svc2_uuid = SANDBOX_SERVICE2_UUID; |
| 152 | struct udevice *dev; |
| 153 | |
| 154 | /* Test probing the sandbox FF-A bus */ |
| 155 | ut_assertok(uclass_first_device_err(UCLASS_FFA, &dev)); |
| 156 | |
| 157 | /* Get a pointer to the sandbox FF-A bus private data */ |
| 158 | uc_priv = dev_get_uclass_priv(dev); |
| 159 | |
| 160 | /* Make sure the private data pointer is retrieved */ |
| 161 | ut_assertnonnull(uc_priv); |
| 162 | |
| 163 | /* Test FFA_VERSION */ |
| 164 | check_fwk_version(uc_priv, uts); |
| 165 | |
| 166 | /* Test FFA_ID_GET */ |
| 167 | check_endpoint_id(uc_priv, uts); |
| 168 | |
| 169 | /* Test FFA_FEATURES */ |
| 170 | check_features(uc_priv, uts); |
| 171 | |
| 172 | /* Test RX/TX buffers */ |
| 173 | check_rxtxbuf(uc_priv, uts); |
| 174 | |
| 175 | /* Test FFA_RXTX_MAP */ |
| 176 | func_data.data0 = &rxbuf_flag; |
| 177 | func_data.data0_size = sizeof(rxbuf_flag); |
| 178 | |
| 179 | rxbuf_flag = 0; |
| 180 | sandbox_query_ffa_emul_state(FFA_RXTX_MAP, &func_data); |
| 181 | check_rxbuf_mapped_flag(FFA_RXTX_MAP, rxbuf_flag, uts); |
| 182 | |
| 183 | /* FFA_PARTITION_INFO_GET / FFA_MSG_SEND_DIRECT_REQ */ |
| 184 | test_partitions_and_comms(svc1_uuid, uts); |
| 185 | |
| 186 | /* Test FFA_RX_RELEASE */ |
| 187 | rxbuf_flag = 1; |
| 188 | sandbox_query_ffa_emul_state(FFA_RX_RELEASE, &func_data); |
| 189 | check_rxbuf_release_flag(rxbuf_flag, uts); |
| 190 | |
| 191 | /* FFA_PARTITION_INFO_GET / FFA_MSG_SEND_DIRECT_REQ */ |
| 192 | test_partitions_and_comms(svc2_uuid, uts); |
| 193 | |
| 194 | /* Test FFA_RX_RELEASE */ |
| 195 | rxbuf_flag = 1; |
| 196 | ut_assertok(sandbox_query_ffa_emul_state(FFA_RX_RELEASE, &func_data)); |
| 197 | check_rxbuf_release_flag(rxbuf_flag, uts); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | DM_TEST(dm_test_ffa_ack, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC); |
| 203 | |
| 204 | static int dm_test_ffa_nack(struct unit_test_state *uts) |
| 205 | { |
| 206 | struct ffa_priv *uc_priv; |
| 207 | const char *valid_svc_uuid = SANDBOX_SERVICE1_UUID; |
| 208 | const char *unvalid_svc_uuid = SANDBOX_SERVICE3_UUID; |
| 209 | const char *unvalid_svc_uuid_str = SANDBOX_SERVICE4_UUID; |
| 210 | struct ffa_send_direct_data msg; |
| 211 | int ret; |
| 212 | u32 count; |
| 213 | u16 part_id = 0; |
| 214 | struct udevice *dev; |
| 215 | struct ffa_partition_desc *descs = NULL; |
| 216 | |
| 217 | /* Test probing the sandbox FF-A bus */ |
| 218 | ut_assertok(uclass_first_device_err(UCLASS_FFA, &dev)); |
| 219 | |
| 220 | /* Get a pointer to the sandbox FF-A bus private data */ |
| 221 | uc_priv = dev_get_uclass_priv(dev); |
| 222 | |
| 223 | /* Make sure the private data pointer is retrieved */ |
| 224 | ut_assertnonnull(uc_priv); |
| 225 | |
| 226 | /* Query partitions count using invalid arguments */ |
| 227 | ret = ffa_partition_info_get(dev, NULL, NULL, NULL); |
| 228 | ut_asserteq(-EINVAL, ret); |
| 229 | ret = ffa_partition_info_get(dev, unvalid_svc_uuid, NULL, NULL); |
| 230 | ut_asserteq(-EINVAL, ret); |
| 231 | ret = ffa_partition_info_get(dev, unvalid_svc_uuid, &count, NULL); |
| 232 | ut_asserteq(-EINVAL, ret); |
| 233 | |
| 234 | /* Query partitions count using an invalid UUID string */ |
| 235 | ret = ffa_partition_info_get(dev, unvalid_svc_uuid_str, &count, &descs); |
| 236 | ut_asserteq(-EINVAL, ret); |
| 237 | |
| 238 | /* Query partitions count using an invalid UUID (no matching SP) */ |
| 239 | count = 0; |
| 240 | ret = ffa_partition_info_get(dev, unvalid_svc_uuid, &count, &descs); |
| 241 | ut_asserteq(0, count); |
| 242 | |
| 243 | /* Query partitions data using a valid UUID */ |
| 244 | count = 0; |
| 245 | ut_assertok(ffa_partition_info_get(dev, valid_svc_uuid, &count, &descs)); |
| 246 | /* Make sure partitions are detected */ |
| 247 | ut_asserteq(SANDBOX_SP_COUNT_PER_VALID_SERVICE, count); |
| 248 | ut_assertnonnull(descs); |
| 249 | |
| 250 | /* Send data to an invalid partition */ |
| 251 | ret = ffa_sync_send_receive(dev, part_id, &msg, 1); |
| 252 | ut_asserteq(-EINVAL, ret); |
| 253 | |
| 254 | /* Send data to a valid partition */ |
| 255 | part_id = uc_priv->partitions.descs[0].info.id; |
| 256 | ut_assertok(ffa_sync_send_receive(dev, part_id, &msg, 1)); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | DM_TEST(dm_test_ffa_nack, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC); |