xref: /arm-trusted-firmware/include/arch/aarch64/smccc_helpers.h (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #ifndef SMCCC_HELPERS_H
8*91f16700Schasinglulu #define SMCCC_HELPERS_H
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <lib/smccc.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu /* Definitions to help the assembler access the SMC/ERET args structure */
13*91f16700Schasinglulu #define SMC_ARGS_SIZE		0x40
14*91f16700Schasinglulu #define SMC_ARG0		0x0
15*91f16700Schasinglulu #define SMC_ARG1		0x8
16*91f16700Schasinglulu #define SMC_ARG2		0x10
17*91f16700Schasinglulu #define SMC_ARG3		0x18
18*91f16700Schasinglulu #define SMC_ARG4		0x20
19*91f16700Schasinglulu #define SMC_ARG5		0x28
20*91f16700Schasinglulu #define SMC_ARG6		0x30
21*91f16700Schasinglulu #define SMC_ARG7		0x38
22*91f16700Schasinglulu #define SMC_ARGS_END		0x40
23*91f16700Schasinglulu 
24*91f16700Schasinglulu #ifndef __ASSEMBLER__
25*91f16700Schasinglulu 
26*91f16700Schasinglulu #include <stdbool.h>
27*91f16700Schasinglulu 
28*91f16700Schasinglulu #include <context.h>
29*91f16700Schasinglulu 
30*91f16700Schasinglulu #include <platform_def.h> /* For CACHE_WRITEBACK_GRANULE */
31*91f16700Schasinglulu 
32*91f16700Schasinglulu /* Convenience macros to return from SMC handler */
33*91f16700Schasinglulu #define SMC_RET0(_h)	{					\
34*91f16700Schasinglulu 	return (uint64_t) (_h);					\
35*91f16700Schasinglulu }
36*91f16700Schasinglulu #define SMC_RET1(_h, _x0)	{				\
37*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X0), (_x0));	\
38*91f16700Schasinglulu 	SMC_RET0(_h);						\
39*91f16700Schasinglulu }
40*91f16700Schasinglulu #define SMC_RET2(_h, _x0, _x1)	{				\
41*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X1), (_x1));	\
42*91f16700Schasinglulu 	SMC_RET1(_h, (_x0));					\
43*91f16700Schasinglulu }
44*91f16700Schasinglulu #define SMC_RET3(_h, _x0, _x1, _x2)	{			\
45*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X2), (_x2));	\
46*91f16700Schasinglulu 	SMC_RET2(_h, (_x0), (_x1));				\
47*91f16700Schasinglulu }
48*91f16700Schasinglulu #define SMC_RET4(_h, _x0, _x1, _x2, _x3)	{		\
49*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X3), (_x3));	\
50*91f16700Schasinglulu 	SMC_RET3(_h, (_x0), (_x1), (_x2));			\
51*91f16700Schasinglulu }
52*91f16700Schasinglulu #define SMC_RET5(_h, _x0, _x1, _x2, _x3, _x4)	{		\
53*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X4), (_x4));	\
54*91f16700Schasinglulu 	SMC_RET4(_h, (_x0), (_x1), (_x2), (_x3));		\
55*91f16700Schasinglulu }
56*91f16700Schasinglulu #define SMC_RET6(_h, _x0, _x1, _x2, _x3, _x4, _x5)	{	\
57*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X5), (_x5));	\
58*91f16700Schasinglulu 	SMC_RET5(_h, (_x0), (_x1), (_x2), (_x3), (_x4));	\
59*91f16700Schasinglulu }
60*91f16700Schasinglulu #define SMC_RET7(_h, _x0, _x1, _x2, _x3, _x4, _x5, _x6)	{	\
61*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X6), (_x6));	\
62*91f16700Schasinglulu 	SMC_RET6(_h, (_x0), (_x1), (_x2), (_x3), (_x4), (_x5));	\
63*91f16700Schasinglulu }
64*91f16700Schasinglulu #define SMC_RET8(_h, _x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7) {	\
65*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (CTX_GPREG_X7), (_x7));	\
66*91f16700Schasinglulu 	SMC_RET7(_h, (_x0), (_x1), (_x2), (_x3), (_x4), (_x5), (_x6));	\
67*91f16700Schasinglulu }
68*91f16700Schasinglulu 
69*91f16700Schasinglulu /*
70*91f16700Schasinglulu  * Convenience macros to access general purpose registers using handle provided
71*91f16700Schasinglulu  * to SMC handler. These take the offset values defined in context.h
72*91f16700Schasinglulu  */
73*91f16700Schasinglulu #define SMC_GET_GP(_h, _g)					\
74*91f16700Schasinglulu 	read_ctx_reg((get_gpregs_ctx(_h)), (_g))
75*91f16700Schasinglulu #define SMC_SET_GP(_h, _g, _v)					\
76*91f16700Schasinglulu 	write_ctx_reg((get_gpregs_ctx(_h)), (_g), (_v))
77*91f16700Schasinglulu 
78*91f16700Schasinglulu 
79*91f16700Schasinglulu /* Useful for SMCCCv1.2 */
80*91f16700Schasinglulu #define SMC_RET18(_h, _x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7, _x8, _x9, \
81*91f16700Schasinglulu 		_x10, _x11, _x12, _x13, _x14, _x15, _x16, _x17) {	\
82*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X8, _x8);				\
83*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X9, _x9);				\
84*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X10, _x10);				\
85*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X11, _x11);				\
86*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X12, _x12);				\
87*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X13, _x13);				\
88*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X14, _x14);				\
89*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X15, _x15);				\
90*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X16, _x16);				\
91*91f16700Schasinglulu 	SMC_SET_GP(_h, CTX_GPREG_X17, _x17);				\
92*91f16700Schasinglulu 	SMC_RET8(_h, (_x0), (_x1), (_x2), (_x3), (_x4), (_x5), (_x6),	\
93*91f16700Schasinglulu 		(_x7));							\
94*91f16700Schasinglulu }
95*91f16700Schasinglulu 
96*91f16700Schasinglulu /*
97*91f16700Schasinglulu  * Convenience macros to access EL3 context registers using handle provided to
98*91f16700Schasinglulu  * SMC handler. These take the offset values defined in context.h
99*91f16700Schasinglulu  */
100*91f16700Schasinglulu #define SMC_GET_EL3(_h, _e)					\
101*91f16700Schasinglulu 	read_ctx_reg((get_el3state_ctx(_h)), (_e))
102*91f16700Schasinglulu #define SMC_SET_EL3(_h, _e, _v)					\
103*91f16700Schasinglulu 	write_ctx_reg((get_el3state_ctx(_h)), (_e), (_v))
104*91f16700Schasinglulu 
105*91f16700Schasinglulu /*
106*91f16700Schasinglulu  * Helper macro to retrieve the SMC parameters from cpu_context_t.
107*91f16700Schasinglulu  */
108*91f16700Schasinglulu #define get_smc_params_from_ctx(_hdl, _x1, _x2, _x3, _x4)	\
109*91f16700Schasinglulu 	do {							\
110*91f16700Schasinglulu 		const gp_regs_t *regs = get_gpregs_ctx(_hdl);	\
111*91f16700Schasinglulu 		_x1 = read_ctx_reg(regs, CTX_GPREG_X1);		\
112*91f16700Schasinglulu 		_x2 = read_ctx_reg(regs, CTX_GPREG_X2);		\
113*91f16700Schasinglulu 		_x3 = read_ctx_reg(regs, CTX_GPREG_X3);		\
114*91f16700Schasinglulu 		_x4 = read_ctx_reg(regs, CTX_GPREG_X4);		\
115*91f16700Schasinglulu 	} while (false)
116*91f16700Schasinglulu 
117*91f16700Schasinglulu typedef struct {
118*91f16700Schasinglulu 	uint64_t _regs[SMC_ARGS_END >> 3];
119*91f16700Schasinglulu } __aligned(CACHE_WRITEBACK_GRANULE) smc_args_t;
120*91f16700Schasinglulu 
121*91f16700Schasinglulu /*
122*91f16700Schasinglulu  * Ensure that the assembler's view of the size of the tsp_args is the
123*91f16700Schasinglulu  * same as the compilers.
124*91f16700Schasinglulu  */
125*91f16700Schasinglulu CASSERT(sizeof(smc_args_t) == SMC_ARGS_SIZE, assert_sp_args_size_mismatch);
126*91f16700Schasinglulu 
127*91f16700Schasinglulu static inline smc_args_t smc_helper(uint32_t func, uint64_t arg0,
128*91f16700Schasinglulu 	       uint64_t arg1, uint64_t arg2,
129*91f16700Schasinglulu 	       uint64_t arg3, uint64_t arg4,
130*91f16700Schasinglulu 	       uint64_t arg5, uint64_t arg6)
131*91f16700Schasinglulu {
132*91f16700Schasinglulu 	smc_args_t ret_args = {0};
133*91f16700Schasinglulu 
134*91f16700Schasinglulu 	register uint64_t r0 __asm__("x0") = func;
135*91f16700Schasinglulu 	register uint64_t r1 __asm__("x1") = arg0;
136*91f16700Schasinglulu 	register uint64_t r2 __asm__("x2") = arg1;
137*91f16700Schasinglulu 	register uint64_t r3 __asm__("x3") = arg2;
138*91f16700Schasinglulu 	register uint64_t r4 __asm__("x4") = arg3;
139*91f16700Schasinglulu 	register uint64_t r5 __asm__("x5") = arg4;
140*91f16700Schasinglulu 	register uint64_t r6 __asm__("x6") = arg5;
141*91f16700Schasinglulu 	register uint64_t r7 __asm__("x7") = arg6;
142*91f16700Schasinglulu 
143*91f16700Schasinglulu 	/* Output registers, also used as inputs ('+' constraint). */
144*91f16700Schasinglulu 	__asm__ volatile("smc #0"
145*91f16700Schasinglulu 			: "+r"(r0), "+r"(r1), "+r"(r2), "+r"(r3), "+r"(r4),
146*91f16700Schasinglulu 			  "+r"(r5), "+r"(r6), "+r"(r7));
147*91f16700Schasinglulu 
148*91f16700Schasinglulu 	ret_args._regs[0] = r0;
149*91f16700Schasinglulu 	ret_args._regs[1] = r1;
150*91f16700Schasinglulu 	ret_args._regs[2] = r2;
151*91f16700Schasinglulu 	ret_args._regs[3] = r3;
152*91f16700Schasinglulu 	ret_args._regs[4] = r4;
153*91f16700Schasinglulu 	ret_args._regs[5] = r5;
154*91f16700Schasinglulu 	ret_args._regs[6] = r6;
155*91f16700Schasinglulu 	ret_args._regs[7] = r7;
156*91f16700Schasinglulu 
157*91f16700Schasinglulu 	return ret_args;
158*91f16700Schasinglulu }
159*91f16700Schasinglulu 
160*91f16700Schasinglulu #endif /*__ASSEMBLER__*/
161*91f16700Schasinglulu 
162*91f16700Schasinglulu #endif /* SMCCC_HELPERS_H */
163