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