xref: /arm-trusted-firmware/bl32/tsp/tsp_main.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2013-2022, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <inttypes.h>
9*91f16700Schasinglulu #include <stdint.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <arch_features.h>
12*91f16700Schasinglulu #include <arch_helpers.h>
13*91f16700Schasinglulu #include <bl32/tsp/tsp.h>
14*91f16700Schasinglulu #include <common/bl_common.h>
15*91f16700Schasinglulu #include <common/debug.h>
16*91f16700Schasinglulu #include <lib/spinlock.h>
17*91f16700Schasinglulu #include <plat/common/platform.h>
18*91f16700Schasinglulu #include <platform_tsp.h>
19*91f16700Schasinglulu #include "tsp_private.h"
20*91f16700Schasinglulu 
21*91f16700Schasinglulu #include <platform_def.h>
22*91f16700Schasinglulu 
23*91f16700Schasinglulu /*******************************************************************************
24*91f16700Schasinglulu  * TSP main entry point where it gets the opportunity to initialize its secure
25*91f16700Schasinglulu  * state/applications. Once the state is initialized, it must return to the
26*91f16700Schasinglulu  * SPD with a pointer to the 'tsp_vector_table' jump table.
27*91f16700Schasinglulu  ******************************************************************************/
28*91f16700Schasinglulu uint64_t tsp_main(void)
29*91f16700Schasinglulu {
30*91f16700Schasinglulu 	NOTICE("TSP: %s\n", version_string);
31*91f16700Schasinglulu 	NOTICE("TSP: %s\n", build_message);
32*91f16700Schasinglulu 	INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
33*91f16700Schasinglulu 	INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
34*91f16700Schasinglulu 
35*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
36*91f16700Schasinglulu 
37*91f16700Schasinglulu 	/* Initialize the platform */
38*91f16700Schasinglulu 	tsp_platform_setup();
39*91f16700Schasinglulu 
40*91f16700Schasinglulu 	/* Initialize secure/applications state here */
41*91f16700Schasinglulu 	tsp_generic_timer_start();
42*91f16700Schasinglulu 
43*91f16700Schasinglulu 	/* Update this cpu's statistics */
44*91f16700Schasinglulu 	tsp_stats[linear_id].smc_count++;
45*91f16700Schasinglulu 	tsp_stats[linear_id].eret_count++;
46*91f16700Schasinglulu 	tsp_stats[linear_id].cpu_on_count++;
47*91f16700Schasinglulu 
48*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
49*91f16700Schasinglulu 	     read_mpidr(),
50*91f16700Schasinglulu 	     tsp_stats[linear_id].smc_count,
51*91f16700Schasinglulu 	     tsp_stats[linear_id].eret_count,
52*91f16700Schasinglulu 	     tsp_stats[linear_id].cpu_on_count);
53*91f16700Schasinglulu 
54*91f16700Schasinglulu 	console_flush();
55*91f16700Schasinglulu 	return (uint64_t) &tsp_vector_table;
56*91f16700Schasinglulu }
57*91f16700Schasinglulu 
58*91f16700Schasinglulu /*******************************************************************************
59*91f16700Schasinglulu  * This function performs any remaining book keeping in the test secure payload
60*91f16700Schasinglulu  * after this cpu's architectural state has been setup in response to an earlier
61*91f16700Schasinglulu  * psci cpu_on request.
62*91f16700Schasinglulu  ******************************************************************************/
63*91f16700Schasinglulu smc_args_t *tsp_cpu_on_main(void)
64*91f16700Schasinglulu {
65*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
66*91f16700Schasinglulu 
67*91f16700Schasinglulu 	/* Initialize secure/applications state here */
68*91f16700Schasinglulu 	tsp_generic_timer_start();
69*91f16700Schasinglulu 
70*91f16700Schasinglulu 	/* Update this cpu's statistics */
71*91f16700Schasinglulu 	tsp_stats[linear_id].smc_count++;
72*91f16700Schasinglulu 	tsp_stats[linear_id].eret_count++;
73*91f16700Schasinglulu 	tsp_stats[linear_id].cpu_on_count++;
74*91f16700Schasinglulu 
75*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx turned on\n", read_mpidr());
76*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
77*91f16700Schasinglulu 		read_mpidr(),
78*91f16700Schasinglulu 		tsp_stats[linear_id].smc_count,
79*91f16700Schasinglulu 		tsp_stats[linear_id].eret_count,
80*91f16700Schasinglulu 		tsp_stats[linear_id].cpu_on_count);
81*91f16700Schasinglulu 	/* Indicate to the SPD that we have completed turned ourselves on */
82*91f16700Schasinglulu 	return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
83*91f16700Schasinglulu }
84*91f16700Schasinglulu 
85*91f16700Schasinglulu /*******************************************************************************
86*91f16700Schasinglulu  * This function performs any remaining book keeping in the test secure payload
87*91f16700Schasinglulu  * before this cpu is turned off in response to a psci cpu_off request.
88*91f16700Schasinglulu  ******************************************************************************/
89*91f16700Schasinglulu smc_args_t *tsp_cpu_off_main(uint64_t arg0,
90*91f16700Schasinglulu 			   uint64_t arg1,
91*91f16700Schasinglulu 			   uint64_t arg2,
92*91f16700Schasinglulu 			   uint64_t arg3,
93*91f16700Schasinglulu 			   uint64_t arg4,
94*91f16700Schasinglulu 			   uint64_t arg5,
95*91f16700Schasinglulu 			   uint64_t arg6,
96*91f16700Schasinglulu 			   uint64_t arg7)
97*91f16700Schasinglulu {
98*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
99*91f16700Schasinglulu 
100*91f16700Schasinglulu 	/*
101*91f16700Schasinglulu 	 * This cpu is being turned off, so disable the timer to prevent the
102*91f16700Schasinglulu 	 * secure timer interrupt from interfering with power down. A pending
103*91f16700Schasinglulu 	 * interrupt will be lost but we do not care as we are turning off.
104*91f16700Schasinglulu 	 */
105*91f16700Schasinglulu 	tsp_generic_timer_stop();
106*91f16700Schasinglulu 
107*91f16700Schasinglulu 	/* Update this cpu's statistics */
108*91f16700Schasinglulu 	tsp_stats[linear_id].smc_count++;
109*91f16700Schasinglulu 	tsp_stats[linear_id].eret_count++;
110*91f16700Schasinglulu 	tsp_stats[linear_id].cpu_off_count++;
111*91f16700Schasinglulu 
112*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx off request\n", read_mpidr());
113*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
114*91f16700Schasinglulu 		read_mpidr(),
115*91f16700Schasinglulu 		tsp_stats[linear_id].smc_count,
116*91f16700Schasinglulu 		tsp_stats[linear_id].eret_count,
117*91f16700Schasinglulu 		tsp_stats[linear_id].cpu_off_count);
118*91f16700Schasinglulu 
119*91f16700Schasinglulu 	/* Indicate to the SPD that we have completed this request */
120*91f16700Schasinglulu 	return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
121*91f16700Schasinglulu }
122*91f16700Schasinglulu 
123*91f16700Schasinglulu /*******************************************************************************
124*91f16700Schasinglulu  * This function performs any book keeping in the test secure payload before
125*91f16700Schasinglulu  * this cpu's architectural state is saved in response to an earlier psci
126*91f16700Schasinglulu  * cpu_suspend request.
127*91f16700Schasinglulu  ******************************************************************************/
128*91f16700Schasinglulu smc_args_t *tsp_cpu_suspend_main(uint64_t arg0,
129*91f16700Schasinglulu 			       uint64_t arg1,
130*91f16700Schasinglulu 			       uint64_t arg2,
131*91f16700Schasinglulu 			       uint64_t arg3,
132*91f16700Schasinglulu 			       uint64_t arg4,
133*91f16700Schasinglulu 			       uint64_t arg5,
134*91f16700Schasinglulu 			       uint64_t arg6,
135*91f16700Schasinglulu 			       uint64_t arg7)
136*91f16700Schasinglulu {
137*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
138*91f16700Schasinglulu 
139*91f16700Schasinglulu 	/*
140*91f16700Schasinglulu 	 * Save the time context and disable it to prevent the secure timer
141*91f16700Schasinglulu 	 * interrupt from interfering with wakeup from the suspend state.
142*91f16700Schasinglulu 	 */
143*91f16700Schasinglulu 	tsp_generic_timer_save();
144*91f16700Schasinglulu 	tsp_generic_timer_stop();
145*91f16700Schasinglulu 
146*91f16700Schasinglulu 	/* Update this cpu's statistics */
147*91f16700Schasinglulu 	tsp_stats[linear_id].smc_count++;
148*91f16700Schasinglulu 	tsp_stats[linear_id].eret_count++;
149*91f16700Schasinglulu 	tsp_stats[linear_id].cpu_suspend_count++;
150*91f16700Schasinglulu 
151*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
152*91f16700Schasinglulu 		read_mpidr(),
153*91f16700Schasinglulu 		tsp_stats[linear_id].smc_count,
154*91f16700Schasinglulu 		tsp_stats[linear_id].eret_count,
155*91f16700Schasinglulu 		tsp_stats[linear_id].cpu_suspend_count);
156*91f16700Schasinglulu 
157*91f16700Schasinglulu 	/* Indicate to the SPD that we have completed this request */
158*91f16700Schasinglulu 	return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
159*91f16700Schasinglulu }
160*91f16700Schasinglulu 
161*91f16700Schasinglulu /*******************************************************************************
162*91f16700Schasinglulu  * This function performs any book keeping in the test secure payload after this
163*91f16700Schasinglulu  * cpu's architectural state has been restored after wakeup from an earlier psci
164*91f16700Schasinglulu  * cpu_suspend request.
165*91f16700Schasinglulu  ******************************************************************************/
166*91f16700Schasinglulu smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
167*91f16700Schasinglulu 			      uint64_t arg1,
168*91f16700Schasinglulu 			      uint64_t arg2,
169*91f16700Schasinglulu 			      uint64_t arg3,
170*91f16700Schasinglulu 			      uint64_t arg4,
171*91f16700Schasinglulu 			      uint64_t arg5,
172*91f16700Schasinglulu 			      uint64_t arg6,
173*91f16700Schasinglulu 			      uint64_t arg7)
174*91f16700Schasinglulu {
175*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
176*91f16700Schasinglulu 
177*91f16700Schasinglulu 	/* Restore the generic timer context */
178*91f16700Schasinglulu 	tsp_generic_timer_restore();
179*91f16700Schasinglulu 
180*91f16700Schasinglulu 	/* Update this cpu's statistics */
181*91f16700Schasinglulu 	tsp_stats[linear_id].smc_count++;
182*91f16700Schasinglulu 	tsp_stats[linear_id].eret_count++;
183*91f16700Schasinglulu 	tsp_stats[linear_id].cpu_resume_count++;
184*91f16700Schasinglulu 
185*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n",
186*91f16700Schasinglulu 	     read_mpidr(), max_off_pwrlvl);
187*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n",
188*91f16700Schasinglulu 		read_mpidr(),
189*91f16700Schasinglulu 		tsp_stats[linear_id].smc_count,
190*91f16700Schasinglulu 		tsp_stats[linear_id].eret_count,
191*91f16700Schasinglulu 		tsp_stats[linear_id].cpu_resume_count);
192*91f16700Schasinglulu 	/* Indicate to the SPD that we have completed this request */
193*91f16700Schasinglulu 	return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
194*91f16700Schasinglulu }
195*91f16700Schasinglulu 
196*91f16700Schasinglulu /*******************************************************************************
197*91f16700Schasinglulu  * TSP fast smc handler. The secure monitor jumps to this function by
198*91f16700Schasinglulu  * doing the ERET after populating X0-X7 registers. The arguments are received
199*91f16700Schasinglulu  * in the function arguments in order. Once the service is rendered, this
200*91f16700Schasinglulu  * function returns to Secure Monitor by raising SMC.
201*91f16700Schasinglulu  ******************************************************************************/
202*91f16700Schasinglulu smc_args_t *tsp_smc_handler(uint64_t func,
203*91f16700Schasinglulu 			       uint64_t arg1,
204*91f16700Schasinglulu 			       uint64_t arg2,
205*91f16700Schasinglulu 			       uint64_t arg3,
206*91f16700Schasinglulu 			       uint64_t arg4,
207*91f16700Schasinglulu 			       uint64_t arg5,
208*91f16700Schasinglulu 			       uint64_t arg6,
209*91f16700Schasinglulu 			       uint64_t arg7)
210*91f16700Schasinglulu {
211*91f16700Schasinglulu 	uint128_t service_args;
212*91f16700Schasinglulu 	uint64_t service_arg0;
213*91f16700Schasinglulu 	uint64_t service_arg1;
214*91f16700Schasinglulu 	uint64_t results[2];
215*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
216*91f16700Schasinglulu 	u_register_t dit;
217*91f16700Schasinglulu 
218*91f16700Schasinglulu 	/* Update this cpu's statistics */
219*91f16700Schasinglulu 	tsp_stats[linear_id].smc_count++;
220*91f16700Schasinglulu 	tsp_stats[linear_id].eret_count++;
221*91f16700Schasinglulu 
222*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(),
223*91f16700Schasinglulu 		((func >> 31) & 1) == 1 ? "fast" : "yielding",
224*91f16700Schasinglulu 		func);
225*91f16700Schasinglulu 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
226*91f16700Schasinglulu 		tsp_stats[linear_id].smc_count,
227*91f16700Schasinglulu 		tsp_stats[linear_id].eret_count);
228*91f16700Schasinglulu 
229*91f16700Schasinglulu 	/* Render secure services and obtain results here */
230*91f16700Schasinglulu 	results[0] = arg1;
231*91f16700Schasinglulu 	results[1] = arg2;
232*91f16700Schasinglulu 
233*91f16700Schasinglulu 	/*
234*91f16700Schasinglulu 	 * Request a service back from dispatcher/secure monitor.
235*91f16700Schasinglulu 	 * This call returns and thereafter resumes execution.
236*91f16700Schasinglulu 	 */
237*91f16700Schasinglulu 	service_args = tsp_get_magic();
238*91f16700Schasinglulu 	service_arg0 = (uint64_t)service_args;
239*91f16700Schasinglulu 	service_arg1 = (uint64_t)(service_args >> 64U);
240*91f16700Schasinglulu 
241*91f16700Schasinglulu #if CTX_INCLUDE_MTE_REGS
242*91f16700Schasinglulu 	/*
243*91f16700Schasinglulu 	 * Write a dummy value to an MTE register, to simulate usage in the
244*91f16700Schasinglulu 	 * secure world
245*91f16700Schasinglulu 	 */
246*91f16700Schasinglulu 	write_gcr_el1(0x99);
247*91f16700Schasinglulu #endif
248*91f16700Schasinglulu 
249*91f16700Schasinglulu 	/* Determine the function to perform based on the function ID */
250*91f16700Schasinglulu 	switch (TSP_BARE_FID(func)) {
251*91f16700Schasinglulu 	case TSP_ADD:
252*91f16700Schasinglulu 		results[0] += service_arg0;
253*91f16700Schasinglulu 		results[1] += service_arg1;
254*91f16700Schasinglulu 		break;
255*91f16700Schasinglulu 	case TSP_SUB:
256*91f16700Schasinglulu 		results[0] -= service_arg0;
257*91f16700Schasinglulu 		results[1] -= service_arg1;
258*91f16700Schasinglulu 		break;
259*91f16700Schasinglulu 	case TSP_MUL:
260*91f16700Schasinglulu 		results[0] *= service_arg0;
261*91f16700Schasinglulu 		results[1] *= service_arg1;
262*91f16700Schasinglulu 		break;
263*91f16700Schasinglulu 	case TSP_DIV:
264*91f16700Schasinglulu 		results[0] /= service_arg0 ? service_arg0 : 1;
265*91f16700Schasinglulu 		results[1] /= service_arg1 ? service_arg1 : 1;
266*91f16700Schasinglulu 		break;
267*91f16700Schasinglulu 	case TSP_CHECK_DIT:
268*91f16700Schasinglulu 		if (!is_feat_dit_supported()) {
269*91f16700Schasinglulu 			ERROR("DIT not supported\n");
270*91f16700Schasinglulu 			results[0] = 0;
271*91f16700Schasinglulu 			results[1] = 0xffff;
272*91f16700Schasinglulu 			break;
273*91f16700Schasinglulu 		}
274*91f16700Schasinglulu 		dit = read_dit();
275*91f16700Schasinglulu 		results[0] = dit == service_arg0;
276*91f16700Schasinglulu 		results[1] = dit;
277*91f16700Schasinglulu 		/* Toggle the dit bit */
278*91f16700Schasinglulu 		write_dit(service_arg0 != 0U ? 0 : DIT_BIT);
279*91f16700Schasinglulu 		break;
280*91f16700Schasinglulu 	default:
281*91f16700Schasinglulu 		break;
282*91f16700Schasinglulu 	}
283*91f16700Schasinglulu 
284*91f16700Schasinglulu 	return set_smc_args(func, 0,
285*91f16700Schasinglulu 			    results[0],
286*91f16700Schasinglulu 			    results[1],
287*91f16700Schasinglulu 			    0, 0, 0, 0);
288*91f16700Schasinglulu }
289