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