xref: /rk3399_ARM-atf/include/services/rmmd_svc.h (revision 118017c392aa373ead0e523d033c869c5f4df2a2)
1 /*
2  * Copyright (c) 2021-2025, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef RMMD_SVC_H
8 #define RMMD_SVC_H
9 
10 #include <common/sha_common_macros.h>
11 #include <lib/smccc.h>
12 #include <lib/utils_def.h>
13 
14 /* STD calls FNUM Min/Max ranges */
15 #define RMI_FNUM_MIN_VALUE	U(0x150)
16 #define RMI_FNUM_MAX_VALUE	U(0x18F)
17 
18 #define RMI_FNUM1_MIN_VALUE	U(0x1D0)
19 #define RMI_FNUM1_MAX_VALUE	U(0x2CF)
20 
21 /* Construct RMI fastcall std FID from offset */
22 #define SMC64_RMI_FID(_offset)					  \
23 	((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)			| \
24 	 (SMC_64 << FUNCID_CC_SHIFT)				| \
25 	 (OEN_STD_START << FUNCID_OEN_SHIFT)			| \
26 	 (((RMI_FNUM_MIN_VALUE + (_offset)) & FUNCID_NUM_MASK)	  \
27 	  << FUNCID_NUM_SHIFT))
28 
29 #define is_rmi_fid(fid) __extension__ ({		\
30 	__typeof__(fid) _fid = (fid);			\
31 	((((GET_SMC_NUM(_fid) >= RMI_FNUM_MIN_VALUE) &&	\
32 	(GET_SMC_NUM(_fid) <= RMI_FNUM_MAX_VALUE)) ||	\
33 	((GET_SMC_NUM(_fid) >= RMI_FNUM1_MIN_VALUE) &&	\
34 	(GET_SMC_NUM(_fid) <= RMI_FNUM1_MAX_VALUE))) &&	\
35 	(GET_SMC_TYPE(_fid) == SMC_TYPE_FAST) &&	\
36 	(GET_SMC_CC(_fid) == SMC_64) &&			\
37 	(GET_SMC_OEN(_fid) == OEN_STD_START) &&		\
38 	((_fid & 0x00FE0000) == 0U)); })
39 
40 /*
41  * RMI_FNUM_REQ_COMPLETE is the only function in the RMI range that originates
42  * from the Realm world and is handled by the RMMD. The RMI functions are
43  * always invoked by the Normal world, forwarded by RMMD and handled by the
44  * RMM.
45  */
46 					/* 0x18F */
47 #define RMM_RMI_REQ_COMPLETE		SMC64_RMI_FID(U(0x3F))
48 
49 /* RMM_BOOT_COMPLETE arg0 error codes */
50 #define E_RMM_BOOT_SUCCESS				(0)
51 #define E_RMM_BOOT_UNKNOWN				(-1)
52 #define E_RMM_BOOT_VERSION_MISMATCH			(-2)
53 #define E_RMM_BOOT_CPUS_OUT_OF_RANGE			(-3)
54 #define E_RMM_BOOT_CPU_ID_OUT_OF_RANGE			(-4)
55 #define E_RMM_BOOT_INVALID_SHARED_BUFFER		(-5)
56 #define E_RMM_BOOT_MANIFEST_VERSION_NOT_SUPPORTED	(-6)
57 #define E_RMM_BOOT_MANIFEST_DATA_ERROR			(-7)
58 
59 /* The SMC in the range 0x8400 0191 - 0x8400 01AF are reserved for RSIs.*/
60 
61 /*
62  * EL3 - RMM SMCs used for requesting RMMD services. These SMCs originate in Realm
63  * world and return to Realm world.
64  *
65  * These are allocated from 0x8400 01B0 - 0x8400 01CF in the RMM Service range.
66  */
67 #define RMMD_EL3_FNUM_MIN_VALUE		U(0x1B0)
68 #define RMMD_EL3_FNUM_MAX_VALUE		U(0x1CF)
69 
70 /* Construct RMM_EL3 fastcall std FID from offset */
71 #define SMC64_RMMD_EL3_FID(_offset)					  \
72 	((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)				| \
73 	 (SMC_64 << FUNCID_CC_SHIFT)					| \
74 	 (OEN_STD_START << FUNCID_OEN_SHIFT)				| \
75 	 (((RMMD_EL3_FNUM_MIN_VALUE + (_offset)) & FUNCID_NUM_MASK)	  \
76 	  << FUNCID_NUM_SHIFT))
77 
78 /* The macros below are used to identify GTSI calls from the SMC function ID */
79 #define is_rmmd_el3_fid(fid) __extension__ ({		\
80 	__typeof__(fid) _fid = (fid);			\
81 	((GET_SMC_NUM(_fid) >= RMMD_EL3_FNUM_MIN_VALUE) &&\
82 	(GET_SMC_NUM(_fid) <= RMMD_EL3_FNUM_MAX_VALUE)  &&\
83 	(GET_SMC_TYPE(_fid) == SMC_TYPE_FAST)	    &&	\
84 	(GET_SMC_CC(_fid) == SMC_64)                &&	\
85 	(GET_SMC_OEN(_fid) == OEN_STD_START)        &&	\
86 	((_fid & 0x00FE0000) == 0U)); })
87 
88 					/* 0x1B0 - 0x1B1 */
89 #define RMM_GTSI_DELEGATE		SMC64_RMMD_EL3_FID(U(0))
90 #define RMM_GTSI_UNDELEGATE		SMC64_RMMD_EL3_FID(U(1))
91 
92 /* Return error codes from RMM-EL3 SMCs */
93 #define E_RMM_OK			 0
94 #define E_RMM_UNK			-1
95 #define E_RMM_BAD_ADDR			-2
96 #define E_RMM_BAD_PAS			-3
97 #define E_RMM_NOMEM			-4
98 #define E_RMM_INVAL			-5
99 #define E_RMM_AGAIN			-6
100 #define E_RMM_FAULT			-7
101 #define E_RMM_IN_PROGRESS		-8
102 
103 /* Return error codes from RMI SMCs */
104 #define RMI_SUCCESS			0
105 #define RMI_ERROR_INPUT			1
106 
107 /*
108  * Retrieve Realm attestation key from EL3. Only P-384 ECC curve key is
109  * supported. The arguments to this SMC are :
110  *    arg0 - Function ID.
111  *    arg1 - Realm attestation key buffer Physical address.
112  *    arg2 - Realm attestation key buffer size (in bytes).
113  *    arg3 - The type of the elliptic curve to which the requested
114  *           attestation key belongs to. The value should be one of the
115  *           defined curve types.
116  * The return arguments are :
117  *    ret0 - Status / error.
118  *    ret1 - Size of the realm attestation key if successful.
119  */
120 					/* 0x1B2 */
121 #define RMM_ATTEST_GET_REALM_KEY	SMC64_RMMD_EL3_FID(U(2))
122 
123 /*
124  * Retrieve Platform token from EL3.
125  * The arguments to this SMC are :
126  *    arg0 - Function ID.
127  *    arg1 - Platform attestation token buffer Physical address. (The challenge
128  *           object is passed in this buffer.)
129  *    arg2 - Platform attestation token buffer size (in bytes).
130  *    arg3 - Challenge object size (in bytes). It has to be one of the defined
131  *           SHA hash sizes.
132  * The return arguments are :
133  *    ret0 - Status / error.
134  *    ret1 - Size of the platform token if successful.
135  */
136 					/* 0x1B3 */
137 #define RMM_ATTEST_GET_PLAT_TOKEN	SMC64_RMMD_EL3_FID(U(3))
138 
139 /* Starting RMM-EL3 interface version 0.4 */
140 #define RMM_EL3_FEATURES				SMC64_RMMD_EL3_FID(U(4))
141 #define RMM_EL3_FEAT_REG_0_IDX				U(0)
142 /* Bit 0 of FEAT_REG_0 */
143 /* 1 - the feature is present in EL3 , 0 - the feature is absent */
144 #define RMM_EL3_FEAT_REG_0_EL3_TOKEN_SIGN_MASK		U(0x1)
145 
146 /*
147  * Function codes to support attestation where EL3 is used to sign
148  * realm attestation tokens. In this model, the private key is not
149  * exposed to the RMM.
150  * The arguments to this SMC are:
151  *     arg0 - Function ID.
152  *     arg1 - Opcode, one of:
153  *               RMM_EL3_TOKEN_SIGN_PUSH_REQ_OP,
154  *               RMM_EL3_TOKEN_SIGN_PULL_RESP_OP,
155  *               RMM_EL3_TOKEN_SIGN_GET_RAK_PUB_OP
156  *     arg2 - Pointer to buffer with request/response structures,
157  *            which is in the RMM<->EL3 shared buffer.
158  *     arg3 - Buffer size of memory pointed by arg2.
159  *     arg4 - ECC Curve, when opcode is RMM_EL3_TOKEN_SIGN_GET_RAK_PUB_OP
160  * The return arguments are:
161  *     ret0 - Status/Error
162  *     ret1 - Size of public key if opcode is RMM_EL3_TOKEN_SIGN_GET_RAK_PUB_OP
163  */
164 #define RMM_EL3_TOKEN_SIGN			SMC64_RMMD_EL3_FID(U(5))
165 
166 /* Opcodes for RMM_EL3_TOKEN_SIGN  */
167 #define RMM_EL3_TOKEN_SIGN_PUSH_REQ_OP          U(1)
168 #define RMM_EL3_TOKEN_SIGN_PULL_RESP_OP         U(2)
169 #define RMM_EL3_TOKEN_SIGN_GET_RAK_PUB_OP       U(3)
170 
171 /* Starting RMM-EL3 interface version 0.5 */
172 
173 /*
174  * Function code to support update of MEC keys.
175  * The arguments of this SMC are:
176  * 	arg0 - Function ID.
177  * 	arg1 - [63:48]: Res0, [47:32]: MECID, [31:1]: Res0, [0]: MEC refresh
178  * 	       reason
179  * The return arguments are:
180  * 	ret0 - Status/Error
181  */
182 #define RMM_MEC_REFRESH				SMC64_RMMD_EL3_FID(U(6))
183 
184 /* ECC Curve types for attest key generation */
185 #define ATTEST_KEY_CURVE_ECC_SECP384R1		U(0)
186 
187 /* Identifier for the hash algorithm used for attestation signing */
188 #define EL3_TOKEN_SIGN_HASH_ALG_SHA384		U(1)
189 
190 /* Starting RMM-EL3 interface version 0.6 */
191 /*
192  * Function codes to support RMM IDE Key management Interface.
193  * The arguments to this SMC are:
194  *     arg0 - Function ID.
195  *     arg1 - Enhanced Configuration Access Mechanism address
196  *     arg2 - Root Port ID
197  *     arg3 - IDE selective stream info
198  *     arg4 - Quad word of key[63:0]
199  *     arg5 - Quad word of key[127:64]
200  *     arg6 - Quad word of key[191:128]
201  *     arg7 - Quad word of key[255:192]
202  *     arg8 - Quad word of IV [63:0]
203  *     arg9 - Quad word of IV [95:64]
204  *     arg10 - request_id
205  *     arg11 - cookie
206  * The return arguments are:
207  *     ret0 - Status/Error
208  */
209 #define RMM_IDE_KEY_PROG			SMC64_RMMD_EL3_FID(U(7))
210 
211 /*******************************************************************************
212  * Structure to hold el3_ide_key info
213  ******************************************************************************/
214 #ifndef __ASSEMBLER__
215 typedef struct rp_ide_key_info {
216 	uint64_t keyqw0;
217 	uint64_t keyqw1;
218 	uint64_t keyqw2;
219 	uint64_t keyqw3;
220 	uint64_t ifvqw0;
221 	uint64_t ifvqw1;
222 } rp_ide_key_info_t;
223 #endif /* __ASSEMBLER__ */
224 
225 /*
226  * Function codes to support RMM IDE Key management Interface.
227  * The arguments to this SMC are:
228  *     arg0 - Function ID.
229  *     arg1 - Enhanced Configuration Access Mechanism address
230  *     arg2 - Root Port ID
231  *     arg3 - IDE selective stream info
232  *     arg4 - request_id
233  *     arg5 - cookie
234  * The return arguments are:
235  *     ret0 - Status/Error
236  */
237 #define RMM_IDE_KEY_SET_GO			SMC64_RMMD_EL3_FID(U(8))
238 
239 /*
240  * Function codes to support RMM IDE Key management Interface.
241  * The arguments to this SMC are:
242  *     arg0 - Function ID.
243  *     arg1 - Enhanced Configuration Access Mechanism address
244  *     arg2 - Root Port ID
245  *     arg3 - IDE selective stream info
246  *     arg4 - request_id
247  *     arg5 - cookie
248  * The return arguments are:
249  *     ret0 - Status/Error
250  */
251 #define RMM_IDE_KEY_SET_STOP			SMC64_RMMD_EL3_FID(U(9))
252 
253 /*
254  * Function codes to support RMM IDE Key management Interface.
255  * The arguments to this SMC are:
256  *     arg0 - Function ID.
257  *     arg1 - Enhanced Configuration Access Mechanism address
258  *     arg2 - Root Port ID
259  * The return arguments are:
260  *     ret0 - Status/Error
261  *     ret1 - Retrieved response corresponding to the previous request.
262  *     ret2 - request_id
263  *     ret3 - cookie
264  */
265 #define RMM_IDE_KM_PULL_RESPONSE		SMC64_RMMD_EL3_FID(U(10))
266 
267 /* Starting RMM-EL3 interface version 0.7 */
268 /*
269  * Reserve memory for the RMM.
270  * The arguments to this SMC are:
271  *     arg0 - Function ID.
272  *     arg1 - Size of memory to be reserved (in bytes).
273  *     arg2 - Flags and alignment requirements.
274  * The return arguments are:
275  *     ret0 - Status/error.
276  *     ret1 - Physical address of the reserved memory area.
277  */
278 #define RMM_RESERVE_MEMORY			SMC64_RMMD_EL3_FID(U(11))
279 
280 /*
281  * RMM_BOOT_COMPLETE originates on RMM when the boot finishes (either cold
282  * or warm boot). This is handled by the RMM-EL3 interface SMC handler.
283  *
284  * RMM_BOOT_COMPLETE FID is located at the end of the available range.
285  */
286 					 /* 0x1CF */
287 #define RMM_BOOT_COMPLETE		SMC64_RMMD_EL3_FID(U(0x1F))
288 
289 /*
290  * The major version number of the RMM Boot Interface implementation.
291  * Increase this whenever the semantics of the boot arguments change making it
292  * backwards incompatible.
293  */
294 #define RMM_EL3_IFC_VERSION_MAJOR	(U(0))
295 
296 /*
297  * The minor version number of the RMM Boot Interface implementation.
298  * Increase this when a bug is fixed, or a feature is added without
299  * breaking compatibility.
300  */
301 #define RMM_EL3_IFC_VERSION_MINOR	(U(8))
302 
303 #define RMM_EL3_INTERFACE_VERSION				\
304 	(((RMM_EL3_IFC_VERSION_MAJOR << 16) & 0x7FFFF) |	\
305 		RMM_EL3_IFC_VERSION_MINOR)
306 
307 #define RMM_EL3_IFC_VERSION_GET_MAJOR(_version) (((_version) >> 16) \
308 								& 0x7FFF)
309 #define RMM_EL3_IFC_VERSION_GET_MAJOR_MINOR(_version) ((_version) & 0xFFFF)
310 
311 #ifndef __ASSEMBLER__
312 #include <stdint.h>
313 
314 int rmmd_setup(void);
315 int rmmd_primary_activate(void);
316 int rmmd_secondary_activate(void);
317 uint64_t rmmd_rmi_handler(uint32_t smc_fid,
318 		uint64_t x1,
319 		uint64_t x2,
320 		uint64_t x3,
321 		uint64_t x4,
322 		void *cookie,
323 		void *handle,
324 		uint64_t flags);
325 
326 uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid,
327 		uint64_t x1,
328 		uint64_t x2,
329 		uint64_t x3,
330 		uint64_t x4,
331 		void *cookie,
332 		void *handle,
333 		uint64_t flags);
334 
335 #endif /* __ASSEMBLER__ */
336 #endif /* RMMD_SVC_H */
337