blob: a5acc56795ce50e12a9a5fffecc9defc2167c806 [file] [log] [blame]
J-Alves9f6f0142020-06-17 15:37:59 +01001/*
Max Shvetsov103e0562021-02-04 16:58:31 +00002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
J-Alves9f6f0142020-06-17 15:37:59 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <assert.h>
J-Alves9f6f0142020-06-17 15:37:59 +01007#include <debug.h>
Max Shvetsov40eb6a22020-06-08 11:15:30 +01008#include <errno.h>
Max Shvetsov103e0562021-02-04 16:58:31 +00009
Max Shvetsovc32f4782020-06-23 09:41:15 +010010#include <cactus_def.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000011#include <cactus_platform_def.h>
J-Alves8f4a56f2020-10-28 10:29:05 +000012#include <ffa_endpoints.h>
J-Alves9f6f0142020-06-17 15:37:59 +010013#include <sp_helpers.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000014#include <spm_common.h>
J-Alves9f6f0142020-06-17 15:37:59 +010015
J-Alves63cdaa72020-10-08 17:22:45 +010016#include <lib/libc/string.h>
17#include <lib/xlat_tables/xlat_tables_v2.h>
18
J-Alves9f6f0142020-06-17 15:37:59 +010019/* FFA version test helpers */
20#define FFA_MAJOR 1U
21#define FFA_MINOR 0U
22
Max Shvetsovc32f4782020-06-23 09:41:15 +010023static const uint32_t primary_uuid[4] = PRIMARY_UUID;
24static const uint32_t secondary_uuid[4] = SECONDARY_UUID;
Ruari Phippsd75596a2020-07-17 16:41:34 +010025static const uint32_t tertiary_uuid[4] = TERTIARY_UUID;
Max Shvetsovc32f4782020-06-23 09:41:15 +010026static const uint32_t null_uuid[4] = {0};
27
Max Shvetsov40eb6a22020-06-08 11:15:30 +010028/*
29 * Test FFA_FEATURES interface.
30 */
31static void ffa_features_test(void)
32{
33 const char *test_features = "FFA Features interface";
34 smc_ret_values ffa_ret;
Max Shvetsov103e0562021-02-04 16:58:31 +000035 const struct ffa_features_test *ffa_feature_test_target;
Max Shvetsov40eb6a22020-06-08 11:15:30 +010036 unsigned int i, test_target_size =
Max Shvetsov103e0562021-02-04 16:58:31 +000037 get_ffa_feature_test_target(&ffa_feature_test_target);
38
Max Shvetsov40eb6a22020-06-08 11:15:30 +010039
40 announce_test_section_start(test_features);
41
42 for (i = 0U; i < test_target_size; i++) {
Max Shvetsov103e0562021-02-04 16:58:31 +000043 announce_test_start(ffa_feature_test_target[i].test_name);
Max Shvetsov40eb6a22020-06-08 11:15:30 +010044
Max Shvetsov103e0562021-02-04 16:58:31 +000045 ffa_ret = ffa_features(ffa_feature_test_target[i].feature);
46 expect(ffa_ret.ret0, ffa_feature_test_target[i].expected_ret);
47 if (ffa_feature_test_target[i].expected_ret == FFA_ERROR) {
Max Shvetsov40eb6a22020-06-08 11:15:30 +010048 expect(ffa_ret.ret2, FFA_ERROR_NOT_SUPPORTED);
49 }
50
Max Shvetsov103e0562021-02-04 16:58:31 +000051 announce_test_end(ffa_feature_test_target[i].test_name);
Max Shvetsov40eb6a22020-06-08 11:15:30 +010052 }
53
54 announce_test_section_end(test_features);
55}
56
Max Shvetsovc32f4782020-06-23 09:41:15 +010057static void ffa_partition_info_helper(struct mailbox_buffers *mb, const uint32_t uuid[4],
58 const struct ffa_partition_info *expected,
59 const uint16_t expected_size)
60{
61 smc_ret_values ret = ffa_partition_info_get(uuid);
62 unsigned int i;
63 expect(ret.ret0, FFA_SUCCESS_SMC32);
64
65 struct ffa_partition_info *info = (struct ffa_partition_info *)(mb->recv);
66 for (i = 0U; i < expected_size; i++) {
67 expect(info[i].id, expected[i].id);
68 expect(info[i].exec_context, expected[i].exec_context);
69 expect(info[i].properties, expected[i].properties);
70 }
71
72 ret = ffa_rx_release();
73 expect(ret.ret0, FFA_SUCCESS_SMC32);
74}
75
76static void ffa_partition_info_wrong_test(void)
77{
78 const char *test_wrong_uuid = "Request wrong UUID";
79 uint32_t uuid[4] = {1};
80
81 announce_test_start(test_wrong_uuid);
82
83 smc_ret_values ret = ffa_partition_info_get(uuid);
84 expect(ret.ret0, FFA_ERROR);
85 expect(ret.ret2, FFA_ERROR_INVALID_PARAMETER);
86
87 announce_test_end(test_wrong_uuid);
88}
89
90static void ffa_partition_info_get_test(struct mailbox_buffers *mb)
91{
92 const char *test_partition_info = "FFA Partition info interface";
93 const char *test_primary = "Get primary partition info";
94 const char *test_secondary = "Get secondary partition info";
Ruari Phippsd75596a2020-07-17 16:41:34 +010095 const char *test_tertiary = "Get tertiary partition info";
Max Shvetsovc32f4782020-06-23 09:41:15 +010096 const char *test_all = "Get all partitions info";
97
98 const struct ffa_partition_info expected_info[] = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010099 /* Primary partition info */
100 {
101 .id = SPM_VM_ID_FIRST,
102 .exec_context = CACTUS_PRIMARY_EC_COUNT,
Olivier Deprez6d274a42020-10-23 09:12:27 +0200103 /* Supports receipt of direct message requests. */
104 .properties = 1U
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100105 },
106 /* Secondary partition info */
107 {
108 .id = SPM_VM_ID_FIRST + 1U,
109 .exec_context = CACTUS_SECONDARY_EC_COUNT,
Olivier Deprez6d274a42020-10-23 09:12:27 +0200110 .properties = 1U
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100111 },
112 /* Tertiary partition info */
113 {
114 .id = SPM_VM_ID_FIRST + 2U,
115 .exec_context = CACTUS_TERTIARY_EC_COUNT,
Olivier Deprez6d274a42020-10-23 09:12:27 +0200116 .properties = 1U
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100117 }
Max Shvetsovc32f4782020-06-23 09:41:15 +0100118 };
119
120 announce_test_section_start(test_partition_info);
121
Ruari Phippsd75596a2020-07-17 16:41:34 +0100122 announce_test_start(test_tertiary);
123 ffa_partition_info_helper(mb, tertiary_uuid, &expected_info[2], 1);
124 announce_test_end(test_tertiary);
125
Max Shvetsovc32f4782020-06-23 09:41:15 +0100126 announce_test_start(test_secondary);
127 ffa_partition_info_helper(mb, secondary_uuid, &expected_info[1], 1);
128 announce_test_end(test_secondary);
129
130 announce_test_start(test_primary);
131 ffa_partition_info_helper(mb, primary_uuid, &expected_info[0], 1);
132 announce_test_end(test_primary);
133
134 announce_test_start(test_all);
Ruari Phippsd75596a2020-07-17 16:41:34 +0100135 ffa_partition_info_helper(mb, null_uuid, expected_info, 3);
Max Shvetsovc32f4782020-06-23 09:41:15 +0100136 announce_test_end(test_all);
137
138 ffa_partition_info_wrong_test();
139
140 announce_test_section_end(test_partition_info);
141}
142
Max Shvetsov57c6ddb2020-07-01 14:09:48 +0100143void ffa_version_test(void)
J-Alves9f6f0142020-06-17 15:37:59 +0100144{
J-Alves9f6f0142020-06-17 15:37:59 +0100145 const char *test_ffa_version = "FFA Version interface";
146
J-Alves9f6f0142020-06-17 15:37:59 +0100147 announce_test_start(test_ffa_version);
148
149 smc_ret_values ret = ffa_version(MAKE_FFA_VERSION(FFA_MAJOR, FFA_MINOR));
Max Shvetsov57c6ddb2020-07-01 14:09:48 +0100150 uint32_t spm_version = (uint32_t)ret.ret0;
J-Alves9f6f0142020-06-17 15:37:59 +0100151
Max Shvetsov40eb6a22020-06-08 11:15:30 +0100152 bool ffa_version_compatible =
153 ((spm_version >> FFA_VERSION_MAJOR_SHIFT) == FFA_MAJOR &&
154 (spm_version & FFA_VERSION_MINOR_MASK) >= FFA_MINOR);
J-Alves9f6f0142020-06-17 15:37:59 +0100155
156 NOTICE("FFA_VERSION returned %u.%u; Compatible: %i\n",
157 spm_version >> FFA_VERSION_MAJOR_SHIFT,
158 spm_version & FFA_VERSION_MINOR_MASK,
159 (int)ffa_version_compatible);
160
161 expect((int)ffa_version_compatible, (int)true);
162
163 announce_test_end(test_ffa_version);
Max Shvetsov57c6ddb2020-07-01 14:09:48 +0100164}
165
J-Alves63cdaa72020-10-08 17:22:45 +0100166bool ffa_memory_retrieve_test(struct mailbox_buffers *mb,
J-Alves0435cae2020-11-06 10:49:56 +0000167 struct ffa_memory_region **retrieved,
J-Alves63cdaa72020-10-08 17:22:45 +0100168 uint64_t handle, ffa_vm_id_t sender,
169 ffa_vm_id_t receiver, uint32_t mem_func)
170{
171 smc_ret_values ret;
172 uint32_t fragment_size;
173 uint32_t total_size;
174 uint32_t descriptor_size;
175
176 if (retrieved == NULL || mb == NULL) {
177 ERROR("Invalid parameters!\n");
178 return false;
179 }
180
J-Alves63cdaa72020-10-08 17:22:45 +0100181 /*
182 * TODO: Revise shareability attribute in function call
183 * below.
184 * https://lists.trustedfirmware.org/pipermail/hafnium/2020-June/000023.html
185 */
186 descriptor_size = ffa_memory_retrieve_request_init(
J-Alves0435cae2020-11-06 10:49:56 +0000187 mb->send, handle, sender, receiver, 0, 0,
J-Alves63cdaa72020-10-08 17:22:45 +0100188 FFA_DATA_ACCESS_RW,
189 FFA_INSTRUCTION_ACCESS_NX,
190 FFA_MEMORY_NORMAL_MEM,
191 FFA_MEMORY_CACHE_WRITE_BACK,
192 FFA_MEMORY_OUTER_SHAREABLE);
193
194 ret = ffa_mem_retrieve_req(descriptor_size, descriptor_size);
195
196 if (ret.ret0 != FFA_MEM_RETRIEVE_RESP) {
J-Alves0435cae2020-11-06 10:49:56 +0000197 ERROR("Couldn't retrieve the memory page. Error: %lx\n",
198 ret.ret2);
J-Alves63cdaa72020-10-08 17:22:45 +0100199 return false;
200 }
201
202 /*
203 * Following total_size and fragment_size are useful to keep track
204 * of the state of transaction. When the sum of all fragment_size of all
205 * fragments is equal to total_size, the memory transaction has been
206 * completed.
207 * This is a simple test with only one segment. As such, upon
208 * successful ffa_mem_retrieve_req, total_size must be equal to
209 * fragment_size.
210 */
211 total_size = ret.ret1;
212 fragment_size = ret.ret2;
213
214 if (total_size != fragment_size) {
215 ERROR("Only expect one memory segment to be sent!\n");
216 return false;
217 }
218
219 if (fragment_size > PAGE_SIZE) {
220 ERROR("Fragment should be smaller than RX buffer!\n");
221 return false;
222 }
223
J-Alves0435cae2020-11-06 10:49:56 +0000224 *retrieved = (struct ffa_memory_region *)mb->recv;
J-Alves63cdaa72020-10-08 17:22:45 +0100225
J-Alves0435cae2020-11-06 10:49:56 +0000226 if ((*retrieved)->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
227 VERBOSE("SPMC memory sharing operations support max of %u "
228 "receivers!\n", MAX_MEM_SHARE_RECIPIENTS);
J-Alves63cdaa72020-10-08 17:22:45 +0100229 return false;
230 }
231
Olivier Deprezaed7f082020-11-04 15:11:59 +0100232 VERBOSE("Memory Retrieved!\n");
J-Alves63cdaa72020-10-08 17:22:45 +0100233
234 return true;
235}
236
237bool ffa_memory_relinquish_test(struct ffa_mem_relinquish *m,
238 uint64_t handle,
239 ffa_vm_id_t id)
240{
241 ffa_mem_relinquish_init(m, handle, 0, id);
242
243 if (ffa_mem_relinquish().ret0 != FFA_SUCCESS_SMC32) {
244 ERROR("%s failed to relinquish memory!\n", __func__);
245 return false;
246 }
247
Olivier Deprezaed7f082020-11-04 15:11:59 +0100248 VERBOSE("Memory Relinquished!\n");
J-Alves63cdaa72020-10-08 17:22:45 +0100249 return true;
250}
251
252void ffa_memory_management_test(struct mailbox_buffers *mb, ffa_vm_id_t vm_id,
253 ffa_vm_id_t sender, uint32_t mem_func,
254 uint64_t handle)
255{
256 const char *test_ffa = "Memory Management";
J-Alves0435cae2020-11-06 10:49:56 +0000257 struct ffa_memory_region *m;
J-Alves63cdaa72020-10-08 17:22:45 +0100258 struct ffa_composite_memory_region *composite;
259 int ret;
260 unsigned int mem_attrs;
261 uint32_t *ptr;
262
263 announce_test_section_start(test_ffa);
264
265 expect(ffa_memory_retrieve_test(
266 mb, &m, handle, sender, vm_id, mem_func),
267 true);
268
J-Alves0435cae2020-11-06 10:49:56 +0000269 composite = ffa_memory_region_get_composite(m, 0);
J-Alves63cdaa72020-10-08 17:22:45 +0100270
Olivier Deprezaed7f082020-11-04 15:11:59 +0100271 VERBOSE("Address: %p; page_count: %x %x\n",
J-Alves63cdaa72020-10-08 17:22:45 +0100272 composite->constituents[0].address,
273 composite->constituents[0].page_count, PAGE_SIZE);
274
275 /* This test is only concerned with RW permissions. */
276 expect(ffa_get_data_access_attr(
J-Alves0435cae2020-11-06 10:49:56 +0000277 m->receivers[0].receiver_permissions.permissions),
J-Alves63cdaa72020-10-08 17:22:45 +0100278 FFA_DATA_ACCESS_RW);
279
J-Alvesd8edeed2020-11-18 10:48:12 +0000280 mem_attrs = MT_RW_DATA | MT_EXECUTE_NEVER;
281
282 if (!IS_SP_ID(sender)) {
283 mem_attrs |= MT_NS;
284 }
J-Alves63cdaa72020-10-08 17:22:45 +0100285
286 ret = mmap_add_dynamic_region(
287 (uint64_t)composite->constituents[0].address,
288 (uint64_t)composite->constituents[0].address,
289 composite->constituents[0].page_count * PAGE_SIZE,
290 mem_attrs);
291 expect(ret, 0);
292
293 VERBOSE("Memory has been mapped\n");
294
295 ptr = (uint32_t *) composite->constituents[0].address;
296
297 /* Write mem_func to retrieved memory region for validation purposes. */
298 VERBOSE("Writing: %x\n", mem_func);
299 for (unsigned int i = 0U; i < 5U; i++)
300 ptr[i] = mem_func;
301
302 /*
303 * A FFA_MEM_DONATE changes the ownership of the page, as such no
304 * relinquish is needed.
305 */
306 if (mem_func != FFA_MEM_DONATE_SMC32) {
307 ret = mmap_remove_dynamic_region(
308 (uint64_t)composite->constituents[0].address,
309 composite->constituents[0].page_count * PAGE_SIZE);
310 expect(ret, 0);
311
312 expect(ffa_memory_relinquish_test(
313 (struct ffa_mem_relinquish *)mb->send,
J-Alves0435cae2020-11-06 10:49:56 +0000314 m->handle, vm_id),
J-Alves63cdaa72020-10-08 17:22:45 +0100315 true);
316 }
317
J-Alves0435cae2020-11-06 10:49:56 +0000318 expect(ffa_rx_release().ret0, FFA_SUCCESS_SMC32);
319
J-Alves63cdaa72020-10-08 17:22:45 +0100320 announce_test_section_end(test_ffa);
321}
322
Max Shvetsov57c6ddb2020-07-01 14:09:48 +0100323void ffa_tests(struct mailbox_buffers *mb)
324{
325 const char *test_ffa = "FFA Interfaces";
326
327 announce_test_section_start(test_ffa);
J-Alves9f6f0142020-06-17 15:37:59 +0100328
Max Shvetsov40eb6a22020-06-08 11:15:30 +0100329 ffa_features_test();
Max Shvetsov57c6ddb2020-07-01 14:09:48 +0100330 ffa_version_test();
Max Shvetsovc32f4782020-06-23 09:41:15 +0100331 ffa_partition_info_get_test(mb);
Max Shvetsov40eb6a22020-06-08 11:15:30 +0100332
J-Alves9f6f0142020-06-17 15:37:59 +0100333 announce_test_section_end(test_ffa);
334}