xref: /rk3399_ARM-atf/include/lib/smccc.h (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1085e80ecSAntonio Nino Diaz /*
2085e80ecSAntonio Nino Diaz  * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
3085e80ecSAntonio Nino Diaz  *
4085e80ecSAntonio Nino Diaz  * SPDX-License-Identifier: BSD-3-Clause
5085e80ecSAntonio Nino Diaz  */
6085e80ecSAntonio Nino Diaz 
7c3cf06f1SAntonio Nino Diaz #ifndef SMCCC_H
8c3cf06f1SAntonio Nino Diaz #define SMCCC_H
9085e80ecSAntonio Nino Diaz 
10*09d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
11085e80ecSAntonio Nino Diaz 
122f370465SAntonio Nino Diaz #define SMCCC_VERSION_MAJOR_SHIFT	U(16)
132f370465SAntonio Nino Diaz #define SMCCC_VERSION_MAJOR_MASK	U(0x7FFF)
142f370465SAntonio Nino Diaz #define SMCCC_VERSION_MINOR_SHIFT	U(0)
152f370465SAntonio Nino Diaz #define SMCCC_VERSION_MINOR_MASK	U(0xFFFF)
162f370465SAntonio Nino Diaz #define MAKE_SMCCC_VERSION(_major, _minor) \
170c487ea4SAntonio Nino Diaz 	((((uint32_t)(_major) & SMCCC_VERSION_MAJOR_MASK) << \
180c487ea4SAntonio Nino Diaz 						SMCCC_VERSION_MAJOR_SHIFT) \
190c487ea4SAntonio Nino Diaz 	| (((uint32_t)(_minor) & SMCCC_VERSION_MINOR_MASK) << \
200c487ea4SAntonio Nino Diaz 						SMCCC_VERSION_MINOR_SHIFT))
21085e80ecSAntonio Nino Diaz 
222f370465SAntonio Nino Diaz #if SMCCC_MAJOR_VERSION == 1
232f370465SAntonio Nino Diaz # define SMCCC_MINOR_VERSION U(1)
24*09d40e0eSAntonio Nino Diaz # include <lib/smccc_v1.h>
252f370465SAntonio Nino Diaz #elif SMCCC_MAJOR_VERSION == 2
262f370465SAntonio Nino Diaz # define SMCCC_MINOR_VERSION U(0)
27*09d40e0eSAntonio Nino Diaz # include <lib/smccc_v2.h>
282f370465SAntonio Nino Diaz #else
292f370465SAntonio Nino Diaz # error "Unsupported version of SMCCC."
30085e80ecSAntonio Nino Diaz #endif
312f370465SAntonio Nino Diaz 
322f370465SAntonio Nino Diaz /* Various flags passed to SMC handlers */
332f370465SAntonio Nino Diaz #define SMC_FROM_SECURE		(U(0) << 0)
342f370465SAntonio Nino Diaz #define SMC_FROM_NON_SECURE	(U(1) << 0)
35085e80ecSAntonio Nino Diaz 
36085e80ecSAntonio Nino Diaz #ifndef __ASSEMBLY__
37085e80ecSAntonio Nino Diaz 
38085e80ecSAntonio Nino Diaz #include <stdint.h>
39085e80ecSAntonio Nino Diaz 
40*09d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
41*09d40e0eSAntonio Nino Diaz 
42b3323cd6SAntonio Nino Diaz #define is_caller_non_secure(_f)	(((_f) & SMC_FROM_NON_SECURE) != U(0))
43b3323cd6SAntonio Nino Diaz #define is_caller_secure(_f)		(!is_caller_non_secure(_f))
44085e80ecSAntonio Nino Diaz 
45085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Standard Service SMC call */
462f370465SAntonio Nino Diaz #define is_std_svc_call(_fid)		(GET_SMC_OEN(_fid) == OEN_STD_START)
47085e80ecSAntonio Nino Diaz 
48085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Arm Architectural Service SMC call */
492f370465SAntonio Nino Diaz #define is_arm_arch_svc_call(_fid)	(GET_SMC_OEN(_fid) == OEN_ARM_START)
50085e80ecSAntonio Nino Diaz 
51085e80ecSAntonio Nino Diaz /* The macro below is used to identify a valid Fast SMC call */
52085e80ecSAntonio Nino Diaz #define is_valid_fast_smc(_fid)		((!(((_fid) >> 16) & U(0xff))) && \
53085e80ecSAntonio Nino Diaz 					   (GET_SMC_TYPE(_fid) == SMC_TYPE_FAST))
54085e80ecSAntonio Nino Diaz 
55085e80ecSAntonio Nino Diaz /*
56085e80ecSAntonio Nino Diaz  * Macro to define UUID for services. Apart from defining and initializing a
57085e80ecSAntonio Nino Diaz  * uuid_t structure, this macro verifies that the first word of the defined UUID
58085e80ecSAntonio Nino Diaz  * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
59085e80ecSAntonio Nino Diaz  * returned UUID in x0 for an invalid SMC error return
60085e80ecSAntonio Nino Diaz  */
6103364865SRoberto Vargas #define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch,		\
6203364865SRoberto Vargas 		_n0, _n1, _n2, _n3, _n4, _n5)				\
6303364865SRoberto Vargas 	CASSERT((uint32_t)(_tl) != (uint32_t) SMC_UNK, invalid_svc_uuid);\
6403364865SRoberto Vargas 	static const uuid_t _name = {					\
6503364865SRoberto Vargas 		{(_tl >> 24) & 0xFF,					\
6603364865SRoberto Vargas 		 (_tl >> 16) & 0xFF,					\
6703364865SRoberto Vargas 		 (_tl >> 8)  & 0xFF,					\
6803364865SRoberto Vargas 		 (_tl & 0xFF)},						\
6903364865SRoberto Vargas 		{(_tm >> 8) & 0xFF,					\
7003364865SRoberto Vargas 		 (_tm  & 0xFF)},					\
7103364865SRoberto Vargas 		{(_th >> 8) & 0xFF,					\
7203364865SRoberto Vargas 		 (_th & 0xFF)},						\
7303364865SRoberto Vargas 		_cl, _ch,						\
7403364865SRoberto Vargas 		{ _n0, _n1, _n2, _n3, _n4, _n5 }			\
7503364865SRoberto Vargas 	}
76085e80ecSAntonio Nino Diaz 
7743b8fa8eSSandrine Bailleux /*
7843b8fa8eSSandrine Bailleux  * Return a UUID in the SMC return registers.
7943b8fa8eSSandrine Bailleux  *
8043b8fa8eSSandrine Bailleux  * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single
8143b8fa8eSSandrine Bailleux  * 128-bit value using the SMC32 calling convention. This value is mapped to
8243b8fa8eSSandrine Bailleux  * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example
8343b8fa8eSSandrine Bailleux  * shall hold bytes 0 to 3, with byte 0 in the low-order bits.
8443b8fa8eSSandrine Bailleux  */
8543b8fa8eSSandrine Bailleux static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
8643b8fa8eSSandrine Bailleux {
8743b8fa8eSSandrine Bailleux 	return ((uint32_t) b0) | (((uint32_t) b1) << 8) |
8843b8fa8eSSandrine Bailleux 		(((uint32_t) b2) << 16) | (((uint32_t) b3) << 24);
8943b8fa8eSSandrine Bailleux }
9043b8fa8eSSandrine Bailleux 
9143b8fa8eSSandrine Bailleux #define SMC_UUID_RET(_h, _uuid)							\
9243b8fa8eSSandrine Bailleux 	SMC_RET4(handle,							\
9343b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1],		\
9443b8fa8eSSandrine Bailleux 			      (_uuid).time_low[2], (_uuid).time_low[3]),	\
9543b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1],		\
9643b8fa8eSSandrine Bailleux 			      (_uuid).time_hi_and_version[0],			\
9743b8fa8eSSandrine Bailleux 			      (_uuid).time_hi_and_version[1]),			\
9843b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).clock_seq_hi_and_reserved,		\
9943b8fa8eSSandrine Bailleux 			      (_uuid).clock_seq_low, (_uuid).node[0],		\
10043b8fa8eSSandrine Bailleux 			      (_uuid).node[1]),					\
10143b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).node[2], (_uuid).node[3],			\
10243b8fa8eSSandrine Bailleux 			      (_uuid).node[4], (_uuid).node[5]))
10343b8fa8eSSandrine Bailleux 
104085e80ecSAntonio Nino Diaz #endif /*__ASSEMBLY__*/
105c3cf06f1SAntonio Nino Diaz #endif /* SMCCC_H */
106