xref: /rk3399_ARM-atf/services/spd/tspd/tspd_common.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1375f538aSAchin Gupta /*
232f0d3c6SDouglas Raillard  * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
3375f538aSAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5375f538aSAchin Gupta  */
6375f538aSAchin Gupta 
797043ac9SDan Handley #include <assert.h>
897043ac9SDan Handley #include <string.h>
9*09d40e0eSAntonio Nino Diaz 
10*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
11*09d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h>
12*09d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
13*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
14*09d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
15*09d40e0eSAntonio Nino Diaz #include <lib/utils.h>
16*09d40e0eSAntonio Nino Diaz 
1735e98e55SDan Handley #include "tspd_private.h"
18375f538aSAchin Gupta 
19375f538aSAchin Gupta /*******************************************************************************
2050e27dadSVikram Kanigiri  * Given a secure payload entrypoint info pointer, entry point PC, register
2150e27dadSVikram Kanigiri  * width, cpu id & pointer to a context data structure, this function will
2250e27dadSVikram Kanigiri  * initialize tsp context and entry point info for the secure payload
23375f538aSAchin Gupta  ******************************************************************************/
2450e27dadSVikram Kanigiri void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point,
25375f538aSAchin Gupta 				uint32_t rw,
2650e27dadSVikram Kanigiri 				uint64_t pc,
27fb037bfbSDan Handley 				tsp_context_t *tsp_ctx)
28375f538aSAchin Gupta {
29167a9357SAndrew Thoelke 	uint32_t ep_attr;
30375f538aSAchin Gupta 
31375f538aSAchin Gupta 	/* Passing a NULL context is a critical programming error */
32375f538aSAchin Gupta 	assert(tsp_ctx);
3350e27dadSVikram Kanigiri 	assert(tsp_entry_point);
3450e27dadSVikram Kanigiri 	assert(pc);
35375f538aSAchin Gupta 
36375f538aSAchin Gupta 	/*
37375f538aSAchin Gupta 	 * We support AArch64 TSP for now.
38375f538aSAchin Gupta 	 * TODO: Add support for AArch32 TSP
39375f538aSAchin Gupta 	 */
40375f538aSAchin Gupta 	assert(rw == TSP_AARCH64);
41375f538aSAchin Gupta 
42375f538aSAchin Gupta 	/* Associate this context with the cpu specified */
4350e27dadSVikram Kanigiri 	tsp_ctx->mpidr = read_mpidr_el1();
44167a9357SAndrew Thoelke 	tsp_ctx->state = 0;
45167a9357SAndrew Thoelke 	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
4616292f54SDavid Cunado 	clr_yield_smc_active_flag(tsp_ctx->state);
47375f538aSAchin Gupta 
4850e27dadSVikram Kanigiri 	cm_set_context(&tsp_ctx->cpu_ctx, SECURE);
49167a9357SAndrew Thoelke 
50167a9357SAndrew Thoelke 	/* initialise an entrypoint to set up the CPU context */
51167a9357SAndrew Thoelke 	ep_attr = SECURE | EP_ST_ENABLE;
52167a9357SAndrew Thoelke 	if (read_sctlr_el3() & SCTLR_EE_BIT)
53167a9357SAndrew Thoelke 		ep_attr |= EP_EE_BIG;
5450e27dadSVikram Kanigiri 	SET_PARAM_HEAD(tsp_entry_point, PARAM_EP, VERSION_1, ep_attr);
55167a9357SAndrew Thoelke 
5650e27dadSVikram Kanigiri 	tsp_entry_point->pc = pc;
5750e27dadSVikram Kanigiri 	tsp_entry_point->spsr = SPSR_64(MODE_EL1,
5850e27dadSVikram Kanigiri 					MODE_SP_ELX,
5950e27dadSVikram Kanigiri 					DISABLE_ALL_EXCEPTIONS);
6032f0d3c6SDouglas Raillard 	zeromem(&tsp_entry_point->args, sizeof(tsp_entry_point->args));
61375f538aSAchin Gupta }
62375f538aSAchin Gupta 
63375f538aSAchin Gupta /*******************************************************************************
64375f538aSAchin Gupta  * This function takes an SP context pointer and:
65375f538aSAchin Gupta  * 1. Applies the S-EL1 system register context from tsp_ctx->cpu_ctx.
66375f538aSAchin Gupta  * 2. Saves the current C runtime state (callee saved registers) on the stack
67375f538aSAchin Gupta  *    frame and saves a reference to this state.
68375f538aSAchin Gupta  * 3. Calls el3_exit() so that the EL3 system and general purpose registers
69375f538aSAchin Gupta  *    from the tsp_ctx->cpu_ctx are used to enter the secure payload image.
70375f538aSAchin Gupta  ******************************************************************************/
71fb037bfbSDan Handley uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx)
72375f538aSAchin Gupta {
73375f538aSAchin Gupta 	uint64_t rc;
74375f538aSAchin Gupta 
75d3280bebSJuan Castillo 	assert(tsp_ctx != NULL);
76375f538aSAchin Gupta 	assert(tsp_ctx->c_rt_ctx == 0);
77375f538aSAchin Gupta 
78375f538aSAchin Gupta 	/* Apply the Secure EL1 system register context and switch to it */
7908ab89d3SAndrew Thoelke 	assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
80375f538aSAchin Gupta 	cm_el1_sysregs_context_restore(SECURE);
81375f538aSAchin Gupta 	cm_set_next_eret_context(SECURE);
82375f538aSAchin Gupta 
83375f538aSAchin Gupta 	rc = tspd_enter_sp(&tsp_ctx->c_rt_ctx);
8492cad5faSAntonio Nino Diaz #if ENABLE_ASSERTIONS
85375f538aSAchin Gupta 	tsp_ctx->c_rt_ctx = 0;
86375f538aSAchin Gupta #endif
87375f538aSAchin Gupta 
88375f538aSAchin Gupta 	return rc;
89375f538aSAchin Gupta }
90375f538aSAchin Gupta 
91375f538aSAchin Gupta 
92375f538aSAchin Gupta /*******************************************************************************
93375f538aSAchin Gupta  * This function takes an SP context pointer and:
94375f538aSAchin Gupta  * 1. Saves the S-EL1 system register context tp tsp_ctx->cpu_ctx.
95375f538aSAchin Gupta  * 2. Restores the current C runtime state (callee saved registers) from the
96375f538aSAchin Gupta  *    stack frame using the reference to this state saved in tspd_enter_sp().
97375f538aSAchin Gupta  * 3. It does not need to save any general purpose or EL3 system register state
98375f538aSAchin Gupta  *    as the generic smc entry routine should have saved those.
99375f538aSAchin Gupta  ******************************************************************************/
100fb037bfbSDan Handley void tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret)
101375f538aSAchin Gupta {
102d3280bebSJuan Castillo 	assert(tsp_ctx != NULL);
103375f538aSAchin Gupta 	/* Save the Secure EL1 system register context */
10408ab89d3SAndrew Thoelke 	assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
105375f538aSAchin Gupta 	cm_el1_sysregs_context_save(SECURE);
106375f538aSAchin Gupta 
107375f538aSAchin Gupta 	assert(tsp_ctx->c_rt_ctx != 0);
108375f538aSAchin Gupta 	tspd_exit_sp(tsp_ctx->c_rt_ctx, ret);
109375f538aSAchin Gupta 
110375f538aSAchin Gupta 	/* Should never reach here */
111375f538aSAchin Gupta 	assert(0);
112375f538aSAchin Gupta }
1133df6012aSDouglas Raillard 
1143df6012aSDouglas Raillard /*******************************************************************************
1153df6012aSDouglas Raillard  * This function takes an SP context pointer and abort any preempted SMC
1163df6012aSDouglas Raillard  * request.
1173df6012aSDouglas Raillard  * Return 1 if there was a preempted SMC request, 0 otherwise.
1183df6012aSDouglas Raillard  ******************************************************************************/
1193df6012aSDouglas Raillard int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx)
1203df6012aSDouglas Raillard {
12116292f54SDavid Cunado 	if (!get_yield_smc_active_flag(tsp_ctx->state))
1223df6012aSDouglas Raillard 		return 0;
1233df6012aSDouglas Raillard 
1243df6012aSDouglas Raillard 	/* Abort any preempted SMC request */
12516292f54SDavid Cunado 	clr_yield_smc_active_flag(tsp_ctx->state);
1263df6012aSDouglas Raillard 
1273df6012aSDouglas Raillard 	/*
1283df6012aSDouglas Raillard 	 * Arrange for an entry into the test secure payload. It will
1293df6012aSDouglas Raillard 	 * be returned via TSP_ABORT_DONE case in tspd_smc_handler.
1303df6012aSDouglas Raillard 	 */
1313df6012aSDouglas Raillard 	cm_set_elr_el3(SECURE,
13216292f54SDavid Cunado 		       (uint64_t) &tsp_vectors->abort_yield_smc_entry);
1333df6012aSDouglas Raillard 	uint64_t rc = tspd_synchronous_sp_entry(tsp_ctx);
1343df6012aSDouglas Raillard 
1353df6012aSDouglas Raillard 	if (rc != 0)
1363df6012aSDouglas Raillard 		panic();
1373df6012aSDouglas Raillard 
1383df6012aSDouglas Raillard 	return 1;
1393df6012aSDouglas Raillard }
1403df6012aSDouglas Raillard 
141