blob: 90773ae6830a57d51f8561b24aa98fd4ea2d0ac8 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
AlexeiFedorov2f30f102023-03-13 19:37:46 +00002 * Copyright (c) 2020-2023, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
AlexeiFedorov2f30f102023-03-13 19:37:46 +00007#include <stdlib.h>
8
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02009#include <arch_helpers.h>
J-Alves952e1f72021-07-30 17:19:09 +010010#include <cactus_test_cmds.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020011#include <plat_topology.h>
12#include <platform.h>
13#include <power_management.h>
14#include <test_helpers.h>
15#include <tftf_lib.h>
16
J-Alvesf1126f22020-11-02 17:28:20 +000017static struct mailbox_buffers test_mb = {.send = NULL, .recv = NULL};
18
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020019int is_sys_suspend_state_ready(void)
20{
21 int aff_info;
22 unsigned int target_node;
23 u_register_t target_mpid;
24 u_register_t current_mpid = read_mpidr_el1() & MPID_MASK;
25
26 for_each_cpu(target_node) {
27 target_mpid = tftf_get_mpidr_from_node(target_node);
28
29 /* Skip current CPU, as it is powered on */
30 if (target_mpid == current_mpid)
31 continue;
32
33 aff_info = tftf_psci_affinity_info(target_mpid, MPIDR_AFFLVL0);
34 if (aff_info != PSCI_STATE_OFF)
35 return 0;
36 }
37
38 return 1;
39}
40
41void psci_system_reset(void)
42{
43 smc_args args = { SMC_PSCI_SYSTEM_RESET };
44 smc_ret_values ret;
45
46 ret = tftf_smc(&args);
47
48 /* The PSCI SYSTEM_RESET call is not supposed to return */
49 tftf_testcase_printf("System didn't reboot properly (%d)\n",
50 (unsigned int)ret.ret0);
51}
52
53int psci_mem_protect(int val)
54{
55 smc_args args = { SMC_PSCI_MEM_PROTECT};
56 smc_ret_values ret;
57
58 args.arg1 = val;
59 ret = tftf_smc(&args);
60
61 return ret.ret0;
62}
63
64int psci_mem_protect_check(uintptr_t addr, size_t size)
65{
66 smc_args args = { SMC_PSCI_MEM_PROTECT_CHECK };
67 smc_ret_values ret;
68
69 args.arg1 = addr;
70 args.arg2 = size;
71 ret = tftf_smc(&args);
72 return ret.ret0;
73}
74
75/*
76 * This function returns an address that can be used as
77 * sentinel for mem_protect functions. The logic behind
78 * it is that it has to search one region that doesn't intersect
79 * with the memory used by TFTF.
80 */
81unsigned char *psci_mem_prot_get_sentinel(void)
82{
83 const mem_region_t *ranges, *rp, *lim;
84 int nranges;
85 IMPORT_SYM(uintptr_t, __TFTF_BASE__, tftf_base);
86 IMPORT_SYM(uintptr_t, __TFTF_END__, tftf_end);
87 uintptr_t p = 0;
88
89 ranges = plat_get_prot_regions(&nranges);
90 if (!ranges)
91 return NULL;
92
93 lim = &ranges[nranges];
94 for (rp = ranges ; rp < lim; rp++) {
95 p = rp->addr;
96 if (p < tftf_base || p > tftf_end)
97 break;
98 p = p + (rp->size - 1);
99 if (p < tftf_base || p > tftf_end)
100 break;
101 }
102
103 return (rp == lim) ? NULL : (unsigned char *) p;
104}
105
106/*
107 * This function maps the memory region before the
108 * test and unmap it after the test is run
109 */
110test_result_t map_test_unmap(const map_args_unmap_t *args,
111 test_function_arg_t test)
112{
113 int mmap_ret;
114 test_result_t test_ret;
115
116 mmap_ret = mmap_add_dynamic_region(args->addr, args->addr,
117 args->size, args->attr);
118
119 if (mmap_ret != 0) {
120 tftf_testcase_printf("Couldn't map memory (ret = %d)\n",
121 mmap_ret);
122 return TEST_RESULT_FAIL;
123 }
124
125 test_ret = (*test)(args->arg);
126
127 mmap_ret = mmap_remove_dynamic_region(args->addr, args->size);
128 if (mmap_ret != 0) {
129 tftf_testcase_printf("Couldn't unmap memory (ret = %d)\n",
130 mmap_ret);
131 return TEST_RESULT_FAIL;
132 }
133
134 return test_ret;
135}
J-Alvesf1126f22020-11-02 17:28:20 +0000136
J-Alves08b78602023-01-24 15:54:50 +0000137bool reset_tftf_mailbox(void)
J-Alvesf1126f22020-11-02 17:28:20 +0000138{
J-Alves08b78602023-01-24 15:54:50 +0000139 if (is_ffa_call_error(ffa_rxtx_unmap())) {
140 return false;
J-Alvesf1126f22020-11-02 17:28:20 +0000141 }
J-Alves08b78602023-01-24 15:54:50 +0000142
143 test_mb.send = NULL;
144 test_mb.recv = NULL;
145
146 return true;
J-Alvesf1126f22020-11-02 17:28:20 +0000147}
148
149bool get_tftf_mailbox(struct mailbox_buffers *mb)
150{
J-Alves08b78602023-01-24 15:54:50 +0000151 struct ffa_value ret;
152
153 if (test_mb.recv == NULL || test_mb.send == NULL) {
154 CONFIGURE_AND_MAP_MAILBOX(test_mb, PAGE_SIZE, ret);
155 if (is_ffa_call_error(ret)) {
156 return false;
157 }
J-Alvesf1126f22020-11-02 17:28:20 +0000158 }
J-Alves08b78602023-01-24 15:54:50 +0000159
160 *mb = test_mb;
161
162 return true;
J-Alvesf1126f22020-11-02 17:28:20 +0000163}
J-Alvesd708c032020-11-19 12:14:21 +0000164
J-Alves04469302021-01-21 14:48:13 +0000165test_result_t check_spmc_testing_set_up(
J-Alvesd708c032020-11-19 12:14:21 +0000166 uint32_t ffa_version_major, uint32_t ffa_version_minor,
167 const struct ffa_uuid *ffa_uuids, size_t ffa_uuids_size)
168{
169 struct mailbox_buffers mb;
170
171 if (ffa_uuids == NULL) {
172 ERROR("Invalid parameter ffa_uuids!\n");
173 return TEST_RESULT_FAIL;
174 }
175
176 SKIP_TEST_IF_FFA_VERSION_LESS_THAN(ffa_version_major,
177 ffa_version_minor);
178
179 /**********************************************************************
180 * If OP-TEE is SPMC skip the current test.
181 **********************************************************************/
182 if (check_spmc_execution_level()) {
183 VERBOSE("OPTEE as SPMC at S-EL1. Skipping test!\n");
184 return TEST_RESULT_SKIPPED;
185 }
186
187 GET_TFTF_MAILBOX(mb);
188
189 for (unsigned int i = 0U; i < ffa_uuids_size; i++)
Max Shvetsov0b7d25f2021-03-05 13:46:42 +0000190 SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(*mb, ffa_uuids[i]);
J-Alvesd708c032020-11-19 12:14:21 +0000191
192 return TEST_RESULT_SUCCESS;
193}
J-Alvesd56c53c2021-07-01 16:32:16 +0100194
195test_result_t spm_run_multi_core_test(uintptr_t cpu_on_handler,
196 event_t *cpu_done)
197{
198 unsigned int lead_mpid = read_mpidr_el1() & MPID_MASK;
199 unsigned int core_pos, cpu_node, mpidr;
200 int32_t ret;
201
202 VERBOSE("Powering on all cpus.\n");
203
204 for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) {
205 tftf_init_event(&cpu_done[i]);
206 }
207
Madhukar Pappireddy1632b4a2022-09-29 09:54:09 -0500208 /* Power on each secondary CPU one after the other. */
J-Alvesd56c53c2021-07-01 16:32:16 +0100209 for_each_cpu(cpu_node) {
210 mpidr = tftf_get_mpidr_from_node(cpu_node);
211 if (mpidr == lead_mpid) {
212 continue;
213 }
214
215 ret = tftf_cpu_on(mpidr, cpu_on_handler, 0U);
216 if (ret != 0) {
217 ERROR("tftf_cpu_on mpidr 0x%x returns %d\n",
218 mpidr, ret);
219 }
J-Alvesd56c53c2021-07-01 16:32:16 +0100220
Madhukar Pappireddy1632b4a2022-09-29 09:54:09 -0500221 /* Wait for the secondary CPU to be ready. */
J-Alvesd56c53c2021-07-01 16:32:16 +0100222 core_pos = platform_get_core_pos(mpidr);
223 tftf_wait_for_event(&cpu_done[core_pos]);
224 }
225
226 VERBOSE("Done exiting.\n");
227
228 return TEST_RESULT_SUCCESS;
229}
J-Alves952e1f72021-07-30 17:19:09 +0100230
J-Alves79c08f12021-10-27 15:15:16 +0100231bool spm_core_sp_init(ffa_id_t sp_id)
232{
233 /*
234 * Secure Partitions secondary ECs need one round of ffa_run to reach
235 * the message loop.
236 */
237 if (sp_id != SP_ID(1)) {
238 uint32_t core_pos = get_current_core_id();
Daniel Boulbyce386b12022-03-29 18:36:36 +0100239 struct ffa_value ret = ffa_run(sp_id, core_pos);
J-Alves79c08f12021-10-27 15:15:16 +0100240
241 if (ffa_func_id(ret) != FFA_MSG_WAIT) {
242 ERROR("Failed to run SP%x on core %u\n",
243 sp_id, core_pos);
244 return false;
245 }
246 }
247
248 return true;
249}
250
nabkah01d5c67b82022-03-22 22:54:23 +0000251/*
nabkah019ea16642022-03-01 19:39:59 +0000252 * Utility function to wait for all CPUs other than the caller to be
253 * OFF.
254 */
255void wait_for_non_lead_cpus(void)
256{
257 unsigned int target_mpid, target_node;
258
259 for_each_cpu(target_node) {
260 target_mpid = tftf_get_mpidr_from_node(target_node);
261 wait_for_core_to_turn_off(target_mpid);
262 }
263}
264
265void wait_for_core_to_turn_off(unsigned int mpidr)
266{
267 /* Skip lead CPU, as it is powered on */
268 if (mpidr == (read_mpidr_el1() & MPID_MASK))
269 return;
270
271 while (tftf_psci_affinity_info(mpidr, MPIDR_AFFLVL0) != PSCI_STATE_OFF) {
272 continue;
273 }
274}
AlexeiFedorov2f30f102023-03-13 19:37:46 +0000275
276/* Generate 64-bit random number */
277unsigned long long rand64(void)
278{
279 return ((unsigned long long)rand() << 32) | rand();
280}