xref: /rk3399_ARM-atf/include/lib/smccc.h (revision 0709055ed66330fe7709c77d2dfbcb87f0c94272)
1085e80ecSAntonio Nino Diaz /*
2*0709055eSAntonio Nino Diaz  * Copyright (c) 2016-2019, 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 
1009d40e0eSAntonio 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 
22*0709055eSAntonio Nino Diaz #define SMCCC_MAJOR_VERSION U(1)
232f370465SAntonio Nino Diaz #define SMCCC_MINOR_VERSION U(1)
24*0709055eSAntonio Nino Diaz 
25*0709055eSAntonio Nino Diaz /*******************************************************************************
26*0709055eSAntonio Nino Diaz  * Bit definitions inside the function id as per the SMC calling convention
27*0709055eSAntonio Nino Diaz  ******************************************************************************/
28*0709055eSAntonio Nino Diaz #define FUNCID_TYPE_SHIFT		U(31)
29*0709055eSAntonio Nino Diaz #define FUNCID_TYPE_MASK		U(0x1)
30*0709055eSAntonio Nino Diaz #define FUNCID_TYPE_WIDTH		U(1)
31*0709055eSAntonio Nino Diaz 
32*0709055eSAntonio Nino Diaz #define FUNCID_CC_SHIFT			U(30)
33*0709055eSAntonio Nino Diaz #define FUNCID_CC_MASK			U(0x1)
34*0709055eSAntonio Nino Diaz #define FUNCID_CC_WIDTH			U(1)
35*0709055eSAntonio Nino Diaz 
36*0709055eSAntonio Nino Diaz #define FUNCID_OEN_SHIFT		U(24)
37*0709055eSAntonio Nino Diaz #define FUNCID_OEN_MASK			U(0x3f)
38*0709055eSAntonio Nino Diaz #define FUNCID_OEN_WIDTH		U(6)
39*0709055eSAntonio Nino Diaz 
40*0709055eSAntonio Nino Diaz #define FUNCID_NUM_SHIFT		U(0)
41*0709055eSAntonio Nino Diaz #define FUNCID_NUM_MASK			U(0xffff)
42*0709055eSAntonio Nino Diaz #define FUNCID_NUM_WIDTH		U(16)
43*0709055eSAntonio Nino Diaz 
44*0709055eSAntonio Nino Diaz #define GET_SMC_TYPE(id)		(((id) >> FUNCID_TYPE_SHIFT) & \
45*0709055eSAntonio Nino Diaz 					 FUNCID_TYPE_MASK)
46*0709055eSAntonio Nino Diaz #define GET_SMC_CC(id)			(((id) >> FUNCID_CC_SHIFT) & \
47*0709055eSAntonio Nino Diaz 					 FUNCID_CC_MASK)
48*0709055eSAntonio Nino Diaz #define GET_SMC_OEN(id)			(((id) >> FUNCID_OEN_SHIFT) & \
49*0709055eSAntonio Nino Diaz 					 FUNCID_OEN_MASK)
50*0709055eSAntonio Nino Diaz 
51*0709055eSAntonio Nino Diaz /*******************************************************************************
52*0709055eSAntonio Nino Diaz  * Owning entity number definitions inside the function id as per the SMC
53*0709055eSAntonio Nino Diaz  * calling convention
54*0709055eSAntonio Nino Diaz  ******************************************************************************/
55*0709055eSAntonio Nino Diaz #define OEN_ARM_START			U(0)
56*0709055eSAntonio Nino Diaz #define OEN_ARM_END			U(0)
57*0709055eSAntonio Nino Diaz #define OEN_CPU_START			U(1)
58*0709055eSAntonio Nino Diaz #define OEN_CPU_END			U(1)
59*0709055eSAntonio Nino Diaz #define OEN_SIP_START			U(2)
60*0709055eSAntonio Nino Diaz #define OEN_SIP_END			U(2)
61*0709055eSAntonio Nino Diaz #define OEN_OEM_START			U(3)
62*0709055eSAntonio Nino Diaz #define OEN_OEM_END			U(3)
63*0709055eSAntonio Nino Diaz #define OEN_STD_START			U(4)	/* Standard Service Calls */
64*0709055eSAntonio Nino Diaz #define OEN_STD_END			U(4)
65*0709055eSAntonio Nino Diaz #define OEN_STD_HYP_START		U(5)	/* Standard Hypervisor Service calls */
66*0709055eSAntonio Nino Diaz #define OEN_STD_HYP_END			U(5)
67*0709055eSAntonio Nino Diaz #define OEN_VEN_HYP_START		U(6)	/* Vendor Hypervisor Service calls */
68*0709055eSAntonio Nino Diaz #define OEN_VEN_HYP_END			U(6)
69*0709055eSAntonio Nino Diaz #define OEN_TAP_START			U(48)	/* Trusted Applications */
70*0709055eSAntonio Nino Diaz #define OEN_TAP_END			U(49)
71*0709055eSAntonio Nino Diaz #define OEN_TOS_START			U(50)	/* Trusted OS */
72*0709055eSAntonio Nino Diaz #define OEN_TOS_END			U(63)
73*0709055eSAntonio Nino Diaz #define OEN_LIMIT			U(64)
74*0709055eSAntonio Nino Diaz 
75*0709055eSAntonio Nino Diaz /* Flags and error codes */
76*0709055eSAntonio Nino Diaz #define SMC_64				U(1)
77*0709055eSAntonio Nino Diaz #define SMC_32				U(0)
78*0709055eSAntonio Nino Diaz 
79*0709055eSAntonio Nino Diaz #define SMC_TYPE_FAST			ULL(1)
80*0709055eSAntonio Nino Diaz #define SMC_TYPE_YIELD			ULL(0)
81*0709055eSAntonio Nino Diaz 
82*0709055eSAntonio Nino Diaz #define SMC_OK				ULL(0)
83*0709055eSAntonio Nino Diaz #define SMC_UNK				-1
84*0709055eSAntonio Nino Diaz #define SMC_PREEMPTED			-2	/* Not defined by the SMCCC */
852f370465SAntonio Nino Diaz 
862f370465SAntonio Nino Diaz /* Various flags passed to SMC handlers */
872f370465SAntonio Nino Diaz #define SMC_FROM_SECURE		(U(0) << 0)
882f370465SAntonio Nino Diaz #define SMC_FROM_NON_SECURE	(U(1) << 0)
89085e80ecSAntonio Nino Diaz 
90085e80ecSAntonio Nino Diaz #ifndef __ASSEMBLY__
91085e80ecSAntonio Nino Diaz 
92085e80ecSAntonio Nino Diaz #include <stdint.h>
93085e80ecSAntonio Nino Diaz 
9409d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
9509d40e0eSAntonio Nino Diaz 
96b3323cd6SAntonio Nino Diaz #define is_caller_non_secure(_f)	(((_f) & SMC_FROM_NON_SECURE) != U(0))
97b3323cd6SAntonio Nino Diaz #define is_caller_secure(_f)		(!is_caller_non_secure(_f))
98085e80ecSAntonio Nino Diaz 
99085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Standard Service SMC call */
1002f370465SAntonio Nino Diaz #define is_std_svc_call(_fid)		(GET_SMC_OEN(_fid) == OEN_STD_START)
101085e80ecSAntonio Nino Diaz 
102085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Arm Architectural Service SMC call */
1032f370465SAntonio Nino Diaz #define is_arm_arch_svc_call(_fid)	(GET_SMC_OEN(_fid) == OEN_ARM_START)
104085e80ecSAntonio Nino Diaz 
105085e80ecSAntonio Nino Diaz /* The macro below is used to identify a valid Fast SMC call */
106085e80ecSAntonio Nino Diaz #define is_valid_fast_smc(_fid)		((!(((_fid) >> 16) & U(0xff))) && \
107085e80ecSAntonio Nino Diaz 					   (GET_SMC_TYPE(_fid) == SMC_TYPE_FAST))
108085e80ecSAntonio Nino Diaz 
109085e80ecSAntonio Nino Diaz /*
110085e80ecSAntonio Nino Diaz  * Macro to define UUID for services. Apart from defining and initializing a
111085e80ecSAntonio Nino Diaz  * uuid_t structure, this macro verifies that the first word of the defined UUID
112085e80ecSAntonio Nino Diaz  * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
113085e80ecSAntonio Nino Diaz  * returned UUID in x0 for an invalid SMC error return
114085e80ecSAntonio Nino Diaz  */
11503364865SRoberto Vargas #define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch,		\
11603364865SRoberto Vargas 		_n0, _n1, _n2, _n3, _n4, _n5)				\
11703364865SRoberto Vargas 	CASSERT((uint32_t)(_tl) != (uint32_t) SMC_UNK, invalid_svc_uuid);\
11803364865SRoberto Vargas 	static const uuid_t _name = {					\
11903364865SRoberto Vargas 		{(_tl >> 24) & 0xFF,					\
12003364865SRoberto Vargas 		 (_tl >> 16) & 0xFF,					\
12103364865SRoberto Vargas 		 (_tl >> 8)  & 0xFF,					\
12203364865SRoberto Vargas 		 (_tl & 0xFF)},						\
12303364865SRoberto Vargas 		{(_tm >> 8) & 0xFF,					\
12403364865SRoberto Vargas 		 (_tm  & 0xFF)},					\
12503364865SRoberto Vargas 		{(_th >> 8) & 0xFF,					\
12603364865SRoberto Vargas 		 (_th & 0xFF)},						\
12703364865SRoberto Vargas 		_cl, _ch,						\
12803364865SRoberto Vargas 		{ _n0, _n1, _n2, _n3, _n4, _n5 }			\
12903364865SRoberto Vargas 	}
130085e80ecSAntonio Nino Diaz 
13143b8fa8eSSandrine Bailleux /*
13243b8fa8eSSandrine Bailleux  * Return a UUID in the SMC return registers.
13343b8fa8eSSandrine Bailleux  *
13443b8fa8eSSandrine Bailleux  * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single
13543b8fa8eSSandrine Bailleux  * 128-bit value using the SMC32 calling convention. This value is mapped to
13643b8fa8eSSandrine Bailleux  * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example
13743b8fa8eSSandrine Bailleux  * shall hold bytes 0 to 3, with byte 0 in the low-order bits.
13843b8fa8eSSandrine Bailleux  */
13943b8fa8eSSandrine Bailleux static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
14043b8fa8eSSandrine Bailleux {
14143b8fa8eSSandrine Bailleux 	return ((uint32_t) b0) | (((uint32_t) b1) << 8) |
14243b8fa8eSSandrine Bailleux 		(((uint32_t) b2) << 16) | (((uint32_t) b3) << 24);
14343b8fa8eSSandrine Bailleux }
14443b8fa8eSSandrine Bailleux 
14543b8fa8eSSandrine Bailleux #define SMC_UUID_RET(_h, _uuid)							\
14643b8fa8eSSandrine Bailleux 	SMC_RET4(handle,							\
14743b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1],		\
14843b8fa8eSSandrine Bailleux 			      (_uuid).time_low[2], (_uuid).time_low[3]),	\
14943b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1],		\
15043b8fa8eSSandrine Bailleux 			      (_uuid).time_hi_and_version[0],			\
15143b8fa8eSSandrine Bailleux 			      (_uuid).time_hi_and_version[1]),			\
15243b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).clock_seq_hi_and_reserved,		\
15343b8fa8eSSandrine Bailleux 			      (_uuid).clock_seq_low, (_uuid).node[0],		\
15443b8fa8eSSandrine Bailleux 			      (_uuid).node[1]),					\
15543b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).node[2], (_uuid).node[3],			\
15643b8fa8eSSandrine Bailleux 			      (_uuid).node[4], (_uuid).node[5]))
15743b8fa8eSSandrine Bailleux 
158085e80ecSAntonio Nino Diaz #endif /*__ASSEMBLY__*/
159c3cf06f1SAntonio Nino Diaz #endif /* SMCCC_H */
160