xref: /rk3399_ARM-atf/include/lib/smccc.h (revision 6c8f523138cd94bc0608708e821a09b02c8c2f5a)
1 /*
2  * Copyright (c) 2016-2022, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef SMCCC_H
8 #define SMCCC_H
9 
10 #include <lib/utils_def.h>
11 
12 #define SMCCC_VERSION_MAJOR_SHIFT	U(16)
13 #define SMCCC_VERSION_MAJOR_MASK	U(0x7FFF)
14 #define SMCCC_VERSION_MINOR_SHIFT	U(0)
15 #define SMCCC_VERSION_MINOR_MASK	U(0xFFFF)
16 #define MAKE_SMCCC_VERSION(_major, _minor) \
17 	((((uint32_t)(_major) & SMCCC_VERSION_MAJOR_MASK) << \
18 						SMCCC_VERSION_MAJOR_SHIFT) \
19 	| (((uint32_t)(_minor) & SMCCC_VERSION_MINOR_MASK) << \
20 						SMCCC_VERSION_MINOR_SHIFT))
21 
22 #define SMCCC_MAJOR_VERSION U(1)
23 #define SMCCC_MINOR_VERSION U(2)
24 
25 /*******************************************************************************
26  * Bit definitions inside the function id as per the SMC calling convention
27  ******************************************************************************/
28 #define FUNCID_TYPE_SHIFT		U(31)
29 #define FUNCID_TYPE_MASK		U(0x1)
30 #define FUNCID_TYPE_WIDTH		U(1)
31 
32 #define FUNCID_CC_SHIFT			U(30)
33 #define FUNCID_CC_MASK			U(0x1)
34 #define FUNCID_CC_WIDTH			U(1)
35 
36 #define FUNCID_OEN_SHIFT		U(24)
37 #define FUNCID_OEN_MASK			U(0x3f)
38 #define FUNCID_OEN_WIDTH		U(6)
39 
40 #define FUNCID_NUM_SHIFT		U(0)
41 #define FUNCID_NUM_MASK			U(0xffff)
42 #define FUNCID_NUM_WIDTH		U(16)
43 
44 #define FUNCID_MASK			U(0xffffffff)
45 
46 #define GET_SMC_NUM(id)			(((id) >> FUNCID_NUM_SHIFT) & \
47 					 FUNCID_NUM_MASK)
48 #define GET_SMC_TYPE(id)		(((id) >> FUNCID_TYPE_SHIFT) & \
49 					 FUNCID_TYPE_MASK)
50 #define GET_SMC_CC(id)			(((id) >> FUNCID_CC_SHIFT) & \
51 					 FUNCID_CC_MASK)
52 #define GET_SMC_OEN(id)			(((id) >> FUNCID_OEN_SHIFT) & \
53 					 FUNCID_OEN_MASK)
54 
55 /*******************************************************************************
56  * SMCCC_ARCH_SOC_ID SoC version & revision bit definition
57  ******************************************************************************/
58 #define SOC_ID_JEP_106_BANK_IDX_MASK	GENMASK_32(30, 24)
59 #define SOC_ID_JEP_106_BANK_IDX_SHIFT	U(24)
60 #define SOC_ID_JEP_106_ID_CODE_MASK	GENMASK_32(23, 16)
61 #define SOC_ID_JEP_106_ID_CODE_SHIFT	U(16)
62 #define SOC_ID_IMPL_DEF_MASK		GENMASK_32(15, 0)
63 #define SOC_ID_IMPL_DEF_SHIFT		U(0)
64 #define SOC_ID_SET_JEP_106(bkid, mfid)	((((bkid) << SOC_ID_JEP_106_BANK_IDX_SHIFT) & \
65 					  SOC_ID_JEP_106_BANK_IDX_MASK) | \
66 					 (((mfid) << SOC_ID_JEP_106_ID_CODE_SHIFT) & \
67 					  SOC_ID_JEP_106_ID_CODE_MASK))
68 
69 #define SOC_ID_REV_MASK			GENMASK_32(30, 0)
70 #define SOC_ID_REV_SHIFT		U(0)
71 
72 /*******************************************************************************
73  * Owning entity number definitions inside the function id as per the SMC
74  * calling convention
75  ******************************************************************************/
76 #define OEN_ARM_START			U(0)
77 #define OEN_ARM_END			U(0)
78 #define OEN_CPU_START			U(1)
79 #define OEN_CPU_END			U(1)
80 #define OEN_SIP_START			U(2)
81 #define OEN_SIP_END			U(2)
82 #define OEN_OEM_START			U(3)
83 #define OEN_OEM_END			U(3)
84 #define OEN_STD_START			U(4)	/* Standard Service Calls */
85 #define OEN_STD_END			U(4)
86 #define OEN_STD_HYP_START		U(5)	/* Standard Hypervisor Service calls */
87 #define OEN_STD_HYP_END			U(5)
88 #define OEN_VEN_HYP_START		U(6)	/* Vendor Hypervisor Service calls */
89 #define OEN_VEN_HYP_END			U(6)
90 #define OEN_TAP_START			U(48)	/* Trusted Applications */
91 #define OEN_TAP_END			U(49)
92 #define OEN_TOS_START			U(50)	/* Trusted OS */
93 #define OEN_TOS_END			U(63)
94 #define OEN_LIMIT			U(64)
95 
96 /* Flags and error codes */
97 #define SMC_64				U(1)
98 #define SMC_32				U(0)
99 
100 #define SMC_TYPE_FAST			UL(1)
101 #define SMC_TYPE_YIELD			UL(0)
102 
103 #define SMC_OK				ULL(0)
104 #define SMC_UNK				-1
105 #define SMC_PREEMPTED			-2	/* Not defined by the SMCCC */
106 
107 /* Return codes for Arm Architecture Service SMC calls */
108 #define SMC_ARCH_CALL_SUCCESS		0
109 #define SMC_ARCH_CALL_NOT_SUPPORTED	-1
110 #define SMC_ARCH_CALL_NOT_REQUIRED	-2
111 #define SMC_ARCH_CALL_INVAL_PARAM	-3
112 
113 /*
114  * Various flags passed to SMC handlers
115  *
116  * Bit 5 and bit 0 of the flag are used to
117  * determine the source security state as
118  * follows:
119  * ---------------------------------
120  *  Bit 5 | Bit 0 | Security state
121  * ---------------------------------
122  *   0        0      SMC_FROM_SECURE
123  *   0        1      SMC_FROM_NON_SECURE
124  *   1        1      SMC_FROM_REALM
125  */
126 
127 #define SMC_FROM_SECURE		(U(0) << 0)
128 #define SMC_FROM_NON_SECURE	(U(1) << 0)
129 #define SMC_FROM_REALM		U(0x21)
130 #define SMC_FROM_MASK		U(0x21)
131 
132 #ifndef __ASSEMBLER__
133 
134 #include <stdint.h>
135 
136 #include <lib/cassert.h>
137 
138 #if ENABLE_RME
139 #define is_caller_non_secure(_f)	(((_f) & SMC_FROM_MASK) \
140 					  == SMC_FROM_NON_SECURE)
141 #define is_caller_secure(_f)		(((_f) & SMC_FROM_MASK) \
142 					  == SMC_FROM_SECURE)
143 #define is_caller_realm(_f)		(((_f) & SMC_FROM_MASK) \
144 					  == SMC_FROM_REALM)
145 #define caller_sec_state(_f)		((_f) & SMC_FROM_MASK)
146 #else /* ENABLE_RME */
147 #define is_caller_non_secure(_f)	(((_f) & SMC_FROM_NON_SECURE) != U(0))
148 #define is_caller_secure(_f)		(!is_caller_non_secure(_f))
149 #endif /* ENABLE_RME */
150 
151 /* The macro below is used to identify a Standard Service SMC call */
152 #define is_std_svc_call(_fid)		(GET_SMC_OEN(_fid) == OEN_STD_START)
153 
154 /* The macro below is used to identify a Arm Architectural Service SMC call */
155 #define is_arm_arch_svc_call(_fid)	(GET_SMC_OEN(_fid) == OEN_ARM_START)
156 
157 /* The macro below is used to identify a valid Fast SMC call */
158 #define is_valid_fast_smc(_fid)		((!(((_fid) >> 16) & U(0xff))) && \
159 					   (GET_SMC_TYPE(_fid)		\
160 					    == (uint32_t)SMC_TYPE_FAST))
161 
162 /*
163  * Macro to define UUID for services. Apart from defining and initializing a
164  * uuid_t structure, this macro verifies that the first word of the defined UUID
165  * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
166  * returned UUID in x0 for an invalid SMC error return
167  */
168 #define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch,		\
169 		_n0, _n1, _n2, _n3, _n4, _n5)				\
170 	CASSERT((uint32_t)(_tl) != (uint32_t)SMC_UNK,			\
171 		invalid_svc_uuid_##_name);				\
172 	static const uuid_t _name = {					\
173 		{((_tl) >> 24) & 0xFF,					\
174 		 ((_tl) >> 16) & 0xFF,					\
175 		 ((_tl) >> 8)  & 0xFF,					\
176 		 ((_tl) & 0xFF)},					\
177 		{((_tm) >> 8) & 0xFF,					\
178 		 ((_tm)  & 0xFF)},					\
179 		{((_th) >> 8) & 0xFF,					\
180 		 ((_th) & 0xFF)},					\
181 		(_cl), (_ch),						\
182 		{ (_n0), (_n1), (_n2), (_n3), (_n4), (_n5) }		\
183 	}
184 
185 /*
186  * Return a UUID in the SMC return registers.
187  *
188  * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single
189  * 128-bit value using the SMC32 calling convention. This value is mapped to
190  * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example
191  * shall hold bytes 0 to 3, with byte 0 in the low-order bits.
192  */
193 static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
194 {
195 	return ((uint32_t) b0) | (((uint32_t) b1) << 8) |
196 		(((uint32_t) b2) << 16) | (((uint32_t) b3) << 24);
197 }
198 
199 #define SMC_UUID_RET(_h, _uuid)							\
200 	SMC_RET4(handle,							\
201 		smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1],		\
202 			      (_uuid).time_low[2], (_uuid).time_low[3]),	\
203 		smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1],		\
204 			      (_uuid).time_hi_and_version[0],			\
205 			      (_uuid).time_hi_and_version[1]),			\
206 		smc_uuid_word((_uuid).clock_seq_hi_and_reserved,		\
207 			      (_uuid).clock_seq_low, (_uuid).node[0],		\
208 			      (_uuid).node[1]),					\
209 		smc_uuid_word((_uuid).node[2], (_uuid).node[3],			\
210 			      (_uuid).node[4], (_uuid).node[5]))
211 
212 #endif /*__ASSEMBLER__*/
213 #endif /* SMCCC_H */
214