xref: /rk3399_ARM-atf/services/spd/pncd/pncd_helpers.S (revision b86cbe10d251284571595fd95a791cfcc3f79cd8)
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 <asm_macros.S>
8*b0980e58SFlorian Lugou#include "pncd_private.h"
9*b0980e58SFlorian Lugou
10*b0980e58SFlorian Lugou	.global	pncd_enter_sp
11*b0980e58SFlorian Lugou	/* ---------------------------------------------
12*b0980e58SFlorian Lugou	 * This function is called with SP_EL0 as stack.
13*b0980e58SFlorian Lugou	 * Here we stash our EL3 callee-saved registers
14*b0980e58SFlorian Lugou	 * on to the stack as a part of saving the C
15*b0980e58SFlorian Lugou	 * runtime and enter the secure payload.
16*b0980e58SFlorian Lugou	 * 'x0' contains a pointer to the memory where
17*b0980e58SFlorian Lugou	 * the address of the C runtime context is to be
18*b0980e58SFlorian Lugou	 * saved.
19*b0980e58SFlorian Lugou	 * ---------------------------------------------
20*b0980e58SFlorian Lugou	 */
21*b0980e58SFlorian Lugoufunc pncd_enter_sp
22*b0980e58SFlorian Lugou	/* Make space for the registers that we're going to save */
23*b0980e58SFlorian Lugou	mov	x3, sp
24*b0980e58SFlorian Lugou	str	x3, [x0, #0]
25*b0980e58SFlorian Lugou	sub	sp, sp, #PNCD_C_RT_CTX_SIZE
26*b0980e58SFlorian Lugou
27*b0980e58SFlorian Lugou	/* Save callee-saved registers on to the stack */
28*b0980e58SFlorian Lugou	stp	x19, x20, [sp, #PNCD_C_RT_CTX_X19]
29*b0980e58SFlorian Lugou	stp	x21, x22, [sp, #PNCD_C_RT_CTX_X21]
30*b0980e58SFlorian Lugou	stp	x23, x24, [sp, #PNCD_C_RT_CTX_X23]
31*b0980e58SFlorian Lugou	stp	x25, x26, [sp, #PNCD_C_RT_CTX_X25]
32*b0980e58SFlorian Lugou	stp	x27, x28, [sp, #PNCD_C_RT_CTX_X27]
33*b0980e58SFlorian Lugou	stp	x29, x30, [sp, #PNCD_C_RT_CTX_X29]
34*b0980e58SFlorian Lugou
35*b0980e58SFlorian Lugou	/* ---------------------------------------------
36*b0980e58SFlorian Lugou	 * Everything is setup now. el3_exit() will
37*b0980e58SFlorian Lugou	 * use the secure context to restore to the
38*b0980e58SFlorian Lugou	 * general purpose and EL3 system registers to
39*b0980e58SFlorian Lugou	 * ERET into the secure payload.
40*b0980e58SFlorian Lugou	 * ---------------------------------------------
41*b0980e58SFlorian Lugou	 */
42*b0980e58SFlorian Lugou	b	el3_exit
43*b0980e58SFlorian Lugouendfunc pncd_enter_sp
44*b0980e58SFlorian Lugou
45*b0980e58SFlorian Lugou	/* ---------------------------------------------
46*b0980e58SFlorian Lugou	 * This function is called 'x0' pointing to a C
47*b0980e58SFlorian Lugou	 * runtime context saved in pncd_enter_sp(). It
48*b0980e58SFlorian Lugou	 * restores the saved registers and jumps to
49*b0980e58SFlorian Lugou	 * that runtime with 'x0' as the new sp. This
50*b0980e58SFlorian Lugou	 * destroys the C runtime context that had been
51*b0980e58SFlorian Lugou	 * built on the stack below the saved context by
52*b0980e58SFlorian Lugou	 * the caller. Later the second parameter 'x1'
53*b0980e58SFlorian Lugou	 * is passed as return value to the caller
54*b0980e58SFlorian Lugou	 * ---------------------------------------------
55*b0980e58SFlorian Lugou	 */
56*b0980e58SFlorian Lugou	.global pncd_exit_sp
57*b0980e58SFlorian Lugoufunc pncd_exit_sp
58*b0980e58SFlorian Lugou	/* Restore the previous stack */
59*b0980e58SFlorian Lugou	mov	sp, x0
60*b0980e58SFlorian Lugou
61*b0980e58SFlorian Lugou	/* Restore callee-saved registers on to the stack */
62*b0980e58SFlorian Lugou	ldp	x19, x20, [x0, #(PNCD_C_RT_CTX_X19 - PNCD_C_RT_CTX_SIZE)]
63*b0980e58SFlorian Lugou	ldp	x21, x22, [x0, #(PNCD_C_RT_CTX_X21 - PNCD_C_RT_CTX_SIZE)]
64*b0980e58SFlorian Lugou	ldp	x23, x24, [x0, #(PNCD_C_RT_CTX_X23 - PNCD_C_RT_CTX_SIZE)]
65*b0980e58SFlorian Lugou	ldp	x25, x26, [x0, #(PNCD_C_RT_CTX_X25 - PNCD_C_RT_CTX_SIZE)]
66*b0980e58SFlorian Lugou	ldp	x27, x28, [x0, #(PNCD_C_RT_CTX_X27 - PNCD_C_RT_CTX_SIZE)]
67*b0980e58SFlorian Lugou	ldp	x29, x30, [x0, #(PNCD_C_RT_CTX_X29 - PNCD_C_RT_CTX_SIZE)]
68*b0980e58SFlorian Lugou
69*b0980e58SFlorian Lugou	/* ---------------------------------------------
70*b0980e58SFlorian Lugou	 * This should take us back to the instruction
71*b0980e58SFlorian Lugou	 * after the call to the last pncd_enter_sp().
72*b0980e58SFlorian Lugou	 * Place the second parameter to x0 so that the
73*b0980e58SFlorian Lugou	 * caller will see it as a return value from the
74*b0980e58SFlorian Lugou	 * original entry call
75*b0980e58SFlorian Lugou	 * ---------------------------------------------
76*b0980e58SFlorian Lugou	 */
77*b0980e58SFlorian Lugou	mov	x0, x1
78*b0980e58SFlorian Lugou	ret
79*b0980e58SFlorian Lugouendfunc pncd_exit_sp
80