xref: /rk3399_ARM-atf/services/spd/pncd/pncd_common.c (revision b0980e584398fc5adc908cd68f1a6deefa943d29)
1*b0980e58SFlorian Lugou /*
2*b0980e58SFlorian Lugou  * Copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved.
3*b0980e58SFlorian Lugou  *
4*b0980e58SFlorian Lugou  * SPDX-License-Identifier: BSD-3-Clause
5*b0980e58SFlorian Lugou  */
6*b0980e58SFlorian Lugou 
7*b0980e58SFlorian Lugou #include <assert.h>
8*b0980e58SFlorian Lugou #include <string.h>
9*b0980e58SFlorian Lugou 
10*b0980e58SFlorian Lugou #include <arch_helpers.h>
11*b0980e58SFlorian Lugou #include <common/bl_common.h>
12*b0980e58SFlorian Lugou #include <common/debug.h>
13*b0980e58SFlorian Lugou #include <lib/el3_runtime/context_mgmt.h>
14*b0980e58SFlorian Lugou #include <lib/utils.h>
15*b0980e58SFlorian Lugou #include <plat/common/platform.h>
16*b0980e58SFlorian Lugou 
17*b0980e58SFlorian Lugou #include "pncd_private.h"
18*b0980e58SFlorian Lugou 
19*b0980e58SFlorian Lugou /*******************************************************************************
20*b0980e58SFlorian Lugou  * Given a secure payload entrypoint info pointer, entry point PC & pointer to a
21*b0980e58SFlorian Lugou  * context data structure, this function will initialize pnc context and entry
22*b0980e58SFlorian Lugou  * point info for the secure payload
23*b0980e58SFlorian Lugou  ******************************************************************************/
24*b0980e58SFlorian Lugou void pncd_init_pnc_ep_state(struct entry_point_info *pnc_entry_point,
25*b0980e58SFlorian Lugou 				uint64_t pc,
26*b0980e58SFlorian Lugou 				pnc_context_t *pnc_ctx)
27*b0980e58SFlorian Lugou {
28*b0980e58SFlorian Lugou 	uint32_t ep_attr;
29*b0980e58SFlorian Lugou 
30*b0980e58SFlorian Lugou 	/* Passing a NULL context is a critical programming error */
31*b0980e58SFlorian Lugou 	assert(pnc_ctx);
32*b0980e58SFlorian Lugou 	assert(pnc_entry_point);
33*b0980e58SFlorian Lugou 	assert(pc);
34*b0980e58SFlorian Lugou 
35*b0980e58SFlorian Lugou 	/* Associate this context with the current cpu */
36*b0980e58SFlorian Lugou 	pnc_ctx->mpidr = read_mpidr();
37*b0980e58SFlorian Lugou 
38*b0980e58SFlorian Lugou 	cm_set_context(&pnc_ctx->cpu_ctx, SECURE);
39*b0980e58SFlorian Lugou 
40*b0980e58SFlorian Lugou 	/* initialise an entrypoint to set up the CPU context */
41*b0980e58SFlorian Lugou 	ep_attr = SECURE | EP_ST_ENABLE;
42*b0980e58SFlorian Lugou 	if (read_sctlr_el3() & SCTLR_EE_BIT) {
43*b0980e58SFlorian Lugou 		ep_attr |= EP_EE_BIG;
44*b0980e58SFlorian Lugou 	}
45*b0980e58SFlorian Lugou 	SET_PARAM_HEAD(pnc_entry_point, PARAM_EP, VERSION_1, ep_attr);
46*b0980e58SFlorian Lugou 
47*b0980e58SFlorian Lugou 	pnc_entry_point->pc = pc;
48*b0980e58SFlorian Lugou 	pnc_entry_point->spsr = SPSR_64(MODE_EL1,
49*b0980e58SFlorian Lugou 					MODE_SP_ELX,
50*b0980e58SFlorian Lugou 					DISABLE_ALL_EXCEPTIONS);
51*b0980e58SFlorian Lugou 	memset(&pnc_entry_point->args, 0, sizeof(pnc_entry_point->args));
52*b0980e58SFlorian Lugou }
53*b0980e58SFlorian Lugou 
54*b0980e58SFlorian Lugou /*******************************************************************************
55*b0980e58SFlorian Lugou  * This function takes an SP context pointer and:
56*b0980e58SFlorian Lugou  * 1. Applies the S-EL1 system register context from pnc_ctx->cpu_ctx.
57*b0980e58SFlorian Lugou  * 2. Saves the current C runtime state (callee saved registers) on the stack
58*b0980e58SFlorian Lugou  *    frame and saves a reference to this state.
59*b0980e58SFlorian Lugou  * 3. Calls el3_exit() so that the EL3 system and general purpose registers
60*b0980e58SFlorian Lugou  *    from the pnc_ctx->cpu_ctx are used to enter the secure payload image.
61*b0980e58SFlorian Lugou  ******************************************************************************/
62*b0980e58SFlorian Lugou uint64_t pncd_synchronous_sp_entry(pnc_context_t *pnc_ctx)
63*b0980e58SFlorian Lugou {
64*b0980e58SFlorian Lugou 	assert(pnc_ctx != NULL);
65*b0980e58SFlorian Lugou 	assert(pnc_ctx->c_rt_ctx == 0U);
66*b0980e58SFlorian Lugou 
67*b0980e58SFlorian Lugou 	/* Apply the Secure EL1 system register context and switch to it */
68*b0980e58SFlorian Lugou 	assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx);
69*b0980e58SFlorian Lugou 	cm_el1_sysregs_context_restore(SECURE);
70*b0980e58SFlorian Lugou #if CTX_INCLUDE_FPREGS
71*b0980e58SFlorian Lugou 	fpregs_context_restore(get_fpregs_ctx(cm_get_context(SECURE)));
72*b0980e58SFlorian Lugou #endif
73*b0980e58SFlorian Lugou 	cm_set_next_eret_context(SECURE);
74*b0980e58SFlorian Lugou 
75*b0980e58SFlorian Lugou 	return pncd_enter_sp(&pnc_ctx->c_rt_ctx);
76*b0980e58SFlorian Lugou }
77*b0980e58SFlorian Lugou 
78*b0980e58SFlorian Lugou 
79*b0980e58SFlorian Lugou /*******************************************************************************
80*b0980e58SFlorian Lugou  * This function takes an SP context pointer and:
81*b0980e58SFlorian Lugou  * 1. Saves the S-EL1 system register context tp pnc_ctx->cpu_ctx.
82*b0980e58SFlorian Lugou  * 2. Restores the current C runtime state (callee saved registers) from the
83*b0980e58SFlorian Lugou  *    stack frame using the reference to this state saved in pncd_enter_sp().
84*b0980e58SFlorian Lugou  * 3. It does not need to save any general purpose or EL3 system register state
85*b0980e58SFlorian Lugou  *    as the generic smc entry routine should have saved those.
86*b0980e58SFlorian Lugou  ******************************************************************************/
87*b0980e58SFlorian Lugou void pncd_synchronous_sp_exit(pnc_context_t *pnc_ctx, uint64_t ret)
88*b0980e58SFlorian Lugou {
89*b0980e58SFlorian Lugou 	assert(pnc_ctx != NULL);
90*b0980e58SFlorian Lugou 	/* Save the Secure EL1 system register context */
91*b0980e58SFlorian Lugou 	assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx);
92*b0980e58SFlorian Lugou 	cm_el1_sysregs_context_save(SECURE);
93*b0980e58SFlorian Lugou #if CTX_INCLUDE_FPREGS
94*b0980e58SFlorian Lugou 	fpregs_context_save(get_fpregs_ctx(cm_get_context(SECURE)));
95*b0980e58SFlorian Lugou #endif
96*b0980e58SFlorian Lugou 
97*b0980e58SFlorian Lugou 	assert(pnc_ctx->c_rt_ctx != 0);
98*b0980e58SFlorian Lugou 	pncd_exit_sp(pnc_ctx->c_rt_ctx, ret);
99*b0980e58SFlorian Lugou 
100*b0980e58SFlorian Lugou 	/* Should never reach here */
101*b0980e58SFlorian Lugou 	panic();
102*b0980e58SFlorian Lugou }
103