1 /*
2  * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 #include <common/debug.h>
9 #include <plat/common/platform.h>
10 #include <services/gtsi_svc.h>
11 #include <services/rmi_svc.h>
12 #include <services/trp/platform_trp.h>
13 
14 #include <platform_def.h>
15 #include "trp_private.h"
16 
17 /*******************************************************************************
18  * Per cpu data structure to populate parameters for an SMC in C code and use
19  * a pointer to this structure in assembler code to populate x0-x7
20  ******************************************************************************/
21 static trp_args_t trp_smc_args[PLATFORM_CORE_COUNT];
22 
23 /*******************************************************************************
24  * Set the arguments for SMC call
25  ******************************************************************************/
set_smc_args(uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5,uint64_t arg6,uint64_t arg7)26 static trp_args_t *set_smc_args(uint64_t arg0,
27 				uint64_t arg1,
28 				uint64_t arg2,
29 				uint64_t arg3,
30 				uint64_t arg4,
31 				uint64_t arg5,
32 				uint64_t arg6,
33 				uint64_t arg7)
34 {
35 	uint32_t linear_id;
36 	trp_args_t *pcpu_smc_args;
37 
38 	/*
39 	 * Return to Secure Monitor by raising an SMC. The results of the
40 	 * service are passed as an arguments to the SMC
41 	 */
42 	linear_id = plat_my_core_pos();
43 	pcpu_smc_args = &trp_smc_args[linear_id];
44 	write_trp_arg(pcpu_smc_args, TRP_ARG0, arg0);
45 	write_trp_arg(pcpu_smc_args, TRP_ARG1, arg1);
46 	write_trp_arg(pcpu_smc_args, TRP_ARG2, arg2);
47 	write_trp_arg(pcpu_smc_args, TRP_ARG3, arg3);
48 	write_trp_arg(pcpu_smc_args, TRP_ARG4, arg4);
49 	write_trp_arg(pcpu_smc_args, TRP_ARG5, arg5);
50 	write_trp_arg(pcpu_smc_args, TRP_ARG6, arg6);
51 	write_trp_arg(pcpu_smc_args, TRP_ARG7, arg7);
52 
53 	return pcpu_smc_args;
54 }
55 
56 /*******************************************************************************
57  * Setup function for TRP.
58  ******************************************************************************/
trp_setup(void)59 void trp_setup(void)
60 {
61 	/* Perform early platform-specific setup */
62 	trp_early_platform_setup();
63 }
64 
65 /* Main function for TRP */
trp_main(void)66 void trp_main(void)
67 {
68 	NOTICE("TRP: %s\n", version_string);
69 	NOTICE("TRP: %s\n", build_message);
70 	INFO("TRP: Memory base : 0x%lx\n", (unsigned long)RMM_BASE);
71 	INFO("TRP: Total size : 0x%lx bytes\n", (unsigned long)(RMM_END
72 								- RMM_BASE));
73 }
74 
75 /*******************************************************************************
76  * Returning RMI version back to Normal World
77  ******************************************************************************/
trp_ret_rmi_version(void)78 static trp_args_t *trp_ret_rmi_version(void)
79 {
80 	VERBOSE("RMM version is %u.%u\n", RMI_ABI_VERSION_MAJOR,
81 					  RMI_ABI_VERSION_MINOR);
82 	return set_smc_args(RMI_RMM_REQ_COMPLETE, RMI_ABI_VERSION,
83 			    0, 0, 0, 0, 0, 0);
84 }
85 
86 /*******************************************************************************
87  * Transitioning granule of NON-SECURE type to REALM type
88  ******************************************************************************/
trp_asc_mark_realm(unsigned long long x1)89 static trp_args_t *trp_asc_mark_realm(unsigned long long x1)
90 {
91 	unsigned long long ret;
92 
93 	VERBOSE("Delegating granule 0x%llx\n", x1);
94 	ret = trp_smc(set_smc_args(SMC_ASC_MARK_REALM, x1, 0, 0, 0, 0, 0, 0));
95 
96 	if (ret != 0ULL) {
97 		ERROR("Granule transition from NON-SECURE type to REALM type "
98 			"failed 0x%llx\n", ret);
99 	}
100 	return set_smc_args(RMI_RMM_REQ_COMPLETE, ret, 0, 0, 0, 0, 0, 0);
101 }
102 
103 /*******************************************************************************
104  * Transitioning granule of REALM type to NON-SECURE type
105  ******************************************************************************/
trp_asc_mark_nonsecure(unsigned long long x1)106 static trp_args_t *trp_asc_mark_nonsecure(unsigned long long x1)
107 {
108 	unsigned long long ret;
109 
110 	VERBOSE("Undelegating granule 0x%llx\n", x1);
111 	ret = trp_smc(set_smc_args(SMC_ASC_MARK_NONSECURE, x1, 0, 0, 0, 0, 0, 0));
112 
113 	if (ret != 0ULL) {
114 		ERROR("Granule transition from REALM type to NON-SECURE type "
115 			"failed 0x%llx\n", ret);
116 	}
117 	return set_smc_args(RMI_RMM_REQ_COMPLETE, ret, 0, 0, 0, 0, 0, 0);
118 }
119 
120 /*******************************************************************************
121  * Main RMI SMC handler function
122  ******************************************************************************/
trp_rmi_handler(unsigned long fid,unsigned long long x1)123 trp_args_t *trp_rmi_handler(unsigned long fid, unsigned long long x1)
124 {
125 	switch (fid) {
126 	case RMI_RMM_REQ_VERSION:
127 		return trp_ret_rmi_version();
128 	case RMI_RMM_GRANULE_DELEGATE:
129 		return trp_asc_mark_realm(x1);
130 	case RMI_RMM_GRANULE_UNDELEGATE:
131 		return trp_asc_mark_nonsecure(x1);
132 	default:
133 		ERROR("Invalid SMC code to %s, FID %lu\n", __func__, fid);
134 	}
135 	return set_smc_args(SMC_UNK, 0, 0, 0, 0, 0, 0, 0);
136 }
137