1 /*
2  * Copyright 2019 The Hafnium Authors.
3  *
4  * Use of this source code is governed by a BSD-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/BSD-3-Clause.
7  */
8 
9 #include "smc.h"
10 
11 #include <stdint.h>
12 
13 #include "vmapi/hf/ffa.h"
14 
smc_internal(uint32_t func,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5,uint64_t arg6)15 static struct ffa_value smc_internal(uint32_t func, uint64_t arg0,
16 				     uint64_t arg1, uint64_t arg2,
17 				     uint64_t arg3, uint64_t arg4,
18 				     uint64_t arg5, uint64_t arg6)
19 {
20 	register uint64_t r0 __asm__("x0") = func;
21 	register uint64_t r1 __asm__("x1") = arg0;
22 	register uint64_t r2 __asm__("x2") = arg1;
23 	register uint64_t r3 __asm__("x3") = arg2;
24 	register uint64_t r4 __asm__("x4") = arg3;
25 	register uint64_t r5 __asm__("x5") = arg4;
26 	register uint64_t r6 __asm__("x6") = arg5;
27 	register uint64_t r7 __asm__("x7") = arg6;
28 
29 	__asm__ volatile(
30 		"smc #0"
31 		: /* Output registers, also used as inputs ('+' constraint). */
32 		"+r"(r0), "+r"(r1), "+r"(r2), "+r"(r3), "+r"(r4), "+r"(r5),
33 		"+r"(r6), "+r"(r7));
34 
35 	return (struct ffa_value){.func = r0,
36 				  .arg1 = r1,
37 				  .arg2 = r2,
38 				  .arg3 = r3,
39 				  .arg4 = r4,
40 				  .arg5 = r5,
41 				  .arg6 = r6,
42 				  .arg7 = r7};
43 }
44 
45 /** Make an SMC call following the 32-bit SMC calling convention. */
smc32(uint32_t func,uint32_t arg0,uint32_t arg1,uint32_t arg2,uint32_t arg3,uint32_t arg4,uint32_t arg5,uint32_t caller_id)46 struct ffa_value smc32(uint32_t func, uint32_t arg0, uint32_t arg1,
47 		       uint32_t arg2, uint32_t arg3, uint32_t arg4,
48 		       uint32_t arg5, uint32_t caller_id)
49 {
50 	return smc_internal(func | SMCCC_32_BIT, arg0, arg1, arg2, arg3, arg4,
51 			    arg5, caller_id);
52 }
53 
54 /** Make an SMC call following the 64-bit SMC calling convention. */
smc64(uint32_t func,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5,uint32_t caller_id)55 struct ffa_value smc64(uint32_t func, uint64_t arg0, uint64_t arg1,
56 		       uint64_t arg2, uint64_t arg3, uint64_t arg4,
57 		       uint64_t arg5, uint32_t caller_id)
58 {
59 	return smc_internal(func | SMCCC_64_BIT, arg0, arg1, arg2, arg3, arg4,
60 			    arg5, caller_id);
61 }
62 
63 /** Forward a raw SMC on to EL3. */
smc_forward(uint32_t func,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5,uint32_t caller_id)64 struct ffa_value smc_forward(uint32_t func, uint64_t arg0, uint64_t arg1,
65 			     uint64_t arg2, uint64_t arg3, uint64_t arg4,
66 			     uint64_t arg5, uint32_t caller_id)
67 {
68 	return smc_internal(func, arg0, arg1, arg2, arg3, arg4, arg5,
69 			    caller_id);
70 }
71 
72 /**
73  * Make an FF-A call up to EL3. Assumes the function ID is already masked
74  * appropriately for the 32-bit or 64-bit SMCCC.
75  */
smc_ffa_call(struct ffa_value args)76 struct ffa_value smc_ffa_call(struct ffa_value args)
77 {
78 	return smc_internal(args.func, args.arg1, args.arg2, args.arg3,
79 			    args.arg4, args.arg5, args.arg6, args.arg7);
80 }
81