summaryrefslogtreecommitdiff
path: root/tests/runtime_services/trusted_os/tsp/test_tsp_fast_smc.c
blob: aa27fb13a76de1a5d6998c1cef1196f6a6bf89bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <arch.h>
#include <arch_helpers.h>
#include <debug.h>
#include <events.h>
#include <plat_topology.h>
#include <platform.h>
#include <power_management.h>
#include <psci.h>
#include <tftf_lib.h>
#include <trusted_os.h>
#include <tsp.h>
#include <uuid_utils.h>
#include <uuid.h>

#define TEST_ITERATIONS_COUNT 1000

static event_t cpu_has_entered_test[PLATFORM_CORE_COUNT];

/*
 * This function calls and validates the results of TSP operation.
 * It expects:
 *           - fn_identifier: SMC function identifier
 *           - arg1, arg2: data on which TSP performs operation
 *           - ret1, ret2: results expected after performing operation
 *                         on arg1 and arg2
 * Returns Success if return values of SMC operation are same as
 * expected else failure.
 */
static test_result_t validate_tsp_operations(uint64_t fn_identifier,
					   uint64_t arg1,
					   uint64_t arg2,
					   uint64_t ret1,
					   uint64_t ret2)
{
	smc_args tsp_svc_params = {fn_identifier, arg1, arg2};
	smc_ret_values tsp_result;

	tsp_result = tftf_smc(&tsp_svc_params);

	if (tsp_result.ret0) {
		tftf_testcase_printf("TSP operation 0x%x failed, error:0x%x\n",
					(unsigned int) fn_identifier,
					(unsigned int) tsp_result.ret0);
		return TEST_RESULT_FAIL;
	}

	if (tsp_result.ret1 != ret1 || tsp_result.ret2 != ret2) {
		tftf_testcase_printf("TSP function:0x%x returned wrong result:"
				     "got 0x%x 0x%x expected: 0x%x 0x%x\n",
						(unsigned int)fn_identifier,
						(unsigned int)tsp_result.ret1,
						(unsigned int)tsp_result.ret2,
						(unsigned int)ret1,
						(unsigned int)ret2);
		return TEST_RESULT_FAIL;
	}
	return TEST_RESULT_SUCCESS;
}

/*
 * This function issues SMC calls to trusted OS(TSP) to perform basic mathematical
 * operations supported by it and validates the result.
 */
static test_result_t issue_trustedos_service_calls(void)
{
	unsigned int mpid = read_mpidr_el1() & MPID_MASK;
	unsigned int core_pos = platform_get_core_pos(mpid);
	test_result_t ret;
	int i;

	/* Signal to the lead CPU that the calling CPU has entered the test */
	tftf_send_event(&cpu_has_entered_test[core_pos]);

	for (i = 0; i < TEST_ITERATIONS_COUNT; i++) {
		/*
		 * TSP add function performs addition of argx to itself and
		 * returns the result in argx where x is 1, 2
		 */
		ret = validate_tsp_operations(TSP_FAST_FID(TSP_ADD), 4, 6, 8, 12);
		if (ret != TEST_RESULT_SUCCESS)
			return ret;

		/*
		 * TSP sub function performs substraction of argx to itself and
		 * returns the result in argx where x is 1, 2
		 */
		ret = validate_tsp_operations(TSP_FAST_FID(TSP_SUB), 4, 6, 0, 0);
		if (ret != TEST_RESULT_SUCCESS)
			return ret;

		/*
		 * TSP mul function performs multiplication of argx to itself and
		 * returns the result in argx where x is 1, 2
		 */
		ret = validate_tsp_operations(TSP_FAST_FID(TSP_MUL), 4, 6, 16, 36);
		if (ret != TEST_RESULT_SUCCESS)
			return ret;

		/*
		 * TSP div function performs division of argx to itself and
		 * returns the result in argx where x is 1, 2
		 */
		ret = validate_tsp_operations(TSP_FAST_FID(TSP_DIV), 4, 6, 1, 1);
		if (ret != TEST_RESULT_SUCCESS)
			return ret;
	}
	return ret;
}

/*
 * @Test_Aim@ Stress test the tsp functionality by issuing fast smc calls
 * to perform trusted OS operations on multiple CPUs
 * Returns Success/Failure/Skipped (if Trusted OS is absent or is not TSP)
 */
test_result_t test_tsp_fast_smc_operations(void)
{
	unsigned int lead_cpu = read_mpidr_el1() & MPID_MASK;
	unsigned int cpu_mpid, cpu_node;
	unsigned int core_pos;
	uint32_t psci_ret;
	test_result_t ret;
	uuid_t tos_uuid;
	char tos_uuid_str[UUID_STR_SIZE];

	if (!is_trusted_os_present(&tos_uuid)) {
		tftf_testcase_printf("No Trusted OS detected\n");
		return TEST_RESULT_SKIPPED;
	}

	if (!uuid_equal(&tos_uuid, &tsp_uuid)) {
		tftf_testcase_printf("Trusted OS is not the TSP, its UUID"
				     " is: %s\n", uuid_to_str(&tos_uuid,
						  tos_uuid_str));
		return TEST_RESULT_SKIPPED;
	}

	for_each_cpu(cpu_node) {
		cpu_mpid = tftf_get_mpidr_from_node(cpu_node);
		if (cpu_mpid != lead_cpu)
			psci_ret = tftf_cpu_on(cpu_mpid,
				    (uintptr_t)issue_trustedos_service_calls,
				    0);
		if (psci_ret != PSCI_E_SUCCESS)
			return TEST_RESULT_FAIL;
	}

	/* Wait for non-lead CPUs to enter the test */
	for_each_cpu(cpu_node) {
		cpu_mpid = tftf_get_mpidr_from_node(cpu_node);
		/* Skip lead CPU */
		if (cpu_mpid == lead_cpu)
			continue;

		core_pos = platform_get_core_pos(cpu_mpid);
		tftf_wait_for_event(&cpu_has_entered_test[core_pos]);
	}

	ret = issue_trustedos_service_calls();
	return ret;
}