xref: /rk3399_ARM-atf/include/lib/smccc.h (revision 0fe7b9f2bcdf754c483399c841e5f0ec71e53ef3)
1085e80ecSAntonio Nino Diaz /*
2e62748e3SManish V Badarkhe  * Copyright (c) 2016-2022, 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 
220709055eSAntonio Nino Diaz #define SMCCC_MAJOR_VERSION U(1)
23e34cc0ceSMadhukar Pappireddy #define SMCCC_MINOR_VERSION U(2)
240709055eSAntonio Nino Diaz 
250709055eSAntonio Nino Diaz /*******************************************************************************
260709055eSAntonio Nino Diaz  * Bit definitions inside the function id as per the SMC calling convention
270709055eSAntonio Nino Diaz  ******************************************************************************/
280709055eSAntonio Nino Diaz #define FUNCID_TYPE_SHIFT		U(31)
290709055eSAntonio Nino Diaz #define FUNCID_TYPE_MASK		U(0x1)
300709055eSAntonio Nino Diaz #define FUNCID_TYPE_WIDTH		U(1)
310709055eSAntonio Nino Diaz 
320709055eSAntonio Nino Diaz #define FUNCID_CC_SHIFT			U(30)
330709055eSAntonio Nino Diaz #define FUNCID_CC_MASK			U(0x1)
340709055eSAntonio Nino Diaz #define FUNCID_CC_WIDTH			U(1)
350709055eSAntonio Nino Diaz 
360709055eSAntonio Nino Diaz #define FUNCID_OEN_SHIFT		U(24)
370709055eSAntonio Nino Diaz #define FUNCID_OEN_MASK			U(0x3f)
380709055eSAntonio Nino Diaz #define FUNCID_OEN_WIDTH		U(6)
390709055eSAntonio Nino Diaz 
40*0fe7b9f2SOlivier Deprez #define FUNCID_SVE_HINT_SHIFT          U(16)
41*0fe7b9f2SOlivier Deprez #define FUNCID_SVE_HINT_MASK           U(1)
42*0fe7b9f2SOlivier Deprez #define FUNCID_SVE_HINT_WIDTH          U(1)
43*0fe7b9f2SOlivier Deprez 
440709055eSAntonio Nino Diaz #define FUNCID_NUM_SHIFT		U(0)
450709055eSAntonio Nino Diaz #define FUNCID_NUM_MASK			U(0xffff)
460709055eSAntonio Nino Diaz #define FUNCID_NUM_WIDTH		U(16)
470709055eSAntonio Nino Diaz 
48e62748e3SManish V Badarkhe #define FUNCID_MASK			U(0xffffffff)
49e62748e3SManish V Badarkhe 
50e073e070SOlivier Deprez #define GET_SMC_NUM(id)			(((id) >> FUNCID_NUM_SHIFT) & \
51e073e070SOlivier Deprez 					 FUNCID_NUM_MASK)
520709055eSAntonio Nino Diaz #define GET_SMC_TYPE(id)		(((id) >> FUNCID_TYPE_SHIFT) & \
530709055eSAntonio Nino Diaz 					 FUNCID_TYPE_MASK)
540709055eSAntonio Nino Diaz #define GET_SMC_CC(id)			(((id) >> FUNCID_CC_SHIFT) & \
550709055eSAntonio Nino Diaz 					 FUNCID_CC_MASK)
560709055eSAntonio Nino Diaz #define GET_SMC_OEN(id)			(((id) >> FUNCID_OEN_SHIFT) & \
570709055eSAntonio Nino Diaz 					 FUNCID_OEN_MASK)
580709055eSAntonio Nino Diaz 
590709055eSAntonio Nino Diaz /*******************************************************************************
6096b0596eSYann Gautier  * SMCCC_ARCH_SOC_ID SoC version & revision bit definition
6196b0596eSYann Gautier  ******************************************************************************/
6296b0596eSYann Gautier #define SOC_ID_JEP_106_BANK_IDX_MASK	GENMASK_32(30, 24)
6396b0596eSYann Gautier #define SOC_ID_JEP_106_BANK_IDX_SHIFT	U(24)
6496b0596eSYann Gautier #define SOC_ID_JEP_106_ID_CODE_MASK	GENMASK_32(23, 16)
6596b0596eSYann Gautier #define SOC_ID_JEP_106_ID_CODE_SHIFT	U(16)
6696b0596eSYann Gautier #define SOC_ID_IMPL_DEF_MASK		GENMASK_32(15, 0)
6796b0596eSYann Gautier #define SOC_ID_IMPL_DEF_SHIFT		U(0)
6896b0596eSYann Gautier #define SOC_ID_SET_JEP_106(bkid, mfid)	((((bkid) << SOC_ID_JEP_106_BANK_IDX_SHIFT) & \
6996b0596eSYann Gautier 					  SOC_ID_JEP_106_BANK_IDX_MASK) | \
7096b0596eSYann Gautier 					 (((mfid) << SOC_ID_JEP_106_ID_CODE_SHIFT) & \
7196b0596eSYann Gautier 					  SOC_ID_JEP_106_ID_CODE_MASK))
7296b0596eSYann Gautier 
7396b0596eSYann Gautier #define SOC_ID_REV_MASK			GENMASK_32(30, 0)
7496b0596eSYann Gautier #define SOC_ID_REV_SHIFT		U(0)
7596b0596eSYann Gautier 
7696b0596eSYann Gautier /*******************************************************************************
770709055eSAntonio Nino Diaz  * Owning entity number definitions inside the function id as per the SMC
780709055eSAntonio Nino Diaz  * calling convention
790709055eSAntonio Nino Diaz  ******************************************************************************/
800709055eSAntonio Nino Diaz #define OEN_ARM_START			U(0)
810709055eSAntonio Nino Diaz #define OEN_ARM_END			U(0)
820709055eSAntonio Nino Diaz #define OEN_CPU_START			U(1)
830709055eSAntonio Nino Diaz #define OEN_CPU_END			U(1)
840709055eSAntonio Nino Diaz #define OEN_SIP_START			U(2)
850709055eSAntonio Nino Diaz #define OEN_SIP_END			U(2)
860709055eSAntonio Nino Diaz #define OEN_OEM_START			U(3)
870709055eSAntonio Nino Diaz #define OEN_OEM_END			U(3)
880709055eSAntonio Nino Diaz #define OEN_STD_START			U(4)	/* Standard Service Calls */
890709055eSAntonio Nino Diaz #define OEN_STD_END			U(4)
900709055eSAntonio Nino Diaz #define OEN_STD_HYP_START		U(5)	/* Standard Hypervisor Service calls */
910709055eSAntonio Nino Diaz #define OEN_STD_HYP_END			U(5)
920709055eSAntonio Nino Diaz #define OEN_VEN_HYP_START		U(6)	/* Vendor Hypervisor Service calls */
930709055eSAntonio Nino Diaz #define OEN_VEN_HYP_END			U(6)
940709055eSAntonio Nino Diaz #define OEN_TAP_START			U(48)	/* Trusted Applications */
950709055eSAntonio Nino Diaz #define OEN_TAP_END			U(49)
960709055eSAntonio Nino Diaz #define OEN_TOS_START			U(50)	/* Trusted OS */
970709055eSAntonio Nino Diaz #define OEN_TOS_END			U(63)
980709055eSAntonio Nino Diaz #define OEN_LIMIT			U(64)
990709055eSAntonio Nino Diaz 
1000709055eSAntonio Nino Diaz /* Flags and error codes */
1010709055eSAntonio Nino Diaz #define SMC_64				U(1)
1020709055eSAntonio Nino Diaz #define SMC_32				U(0)
1030709055eSAntonio Nino Diaz 
104d7b5f408SJimmy Brisson #define SMC_TYPE_FAST			UL(1)
105d7b5f408SJimmy Brisson #define SMC_TYPE_YIELD			UL(0)
1060709055eSAntonio Nino Diaz 
1070709055eSAntonio Nino Diaz #define SMC_OK				ULL(0)
1080709055eSAntonio Nino Diaz #define SMC_UNK				-1
1090709055eSAntonio Nino Diaz #define SMC_PREEMPTED			-2	/* Not defined by the SMCCC */
1102f370465SAntonio Nino Diaz 
111e34cc0ceSMadhukar Pappireddy /* Return codes for Arm Architecture Service SMC calls */
112e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_SUCCESS		0
113e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_NOT_SUPPORTED	-1
114e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_NOT_REQUIRED	-2
115e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_INVAL_PARAM	-3
116e34cc0ceSMadhukar Pappireddy 
1174693ff72SZelalem Aweke /*
1184693ff72SZelalem Aweke  * Various flags passed to SMC handlers
1194693ff72SZelalem Aweke  *
1204693ff72SZelalem Aweke  * Bit 5 and bit 0 of the flag are used to
1214693ff72SZelalem Aweke  * determine the source security state as
1224693ff72SZelalem Aweke  * follows:
1234693ff72SZelalem Aweke  * ---------------------------------
1244693ff72SZelalem Aweke  *  Bit 5 | Bit 0 | Security state
1254693ff72SZelalem Aweke  * ---------------------------------
1264693ff72SZelalem Aweke  *   0        0      SMC_FROM_SECURE
1274693ff72SZelalem Aweke  *   0        1      SMC_FROM_NON_SECURE
1284693ff72SZelalem Aweke  *   1        1      SMC_FROM_REALM
129*0fe7b9f2SOlivier Deprez  *
130*0fe7b9f2SOlivier Deprez  * Bit 16 of flags records the caller's SMC
131*0fe7b9f2SOlivier Deprez  * SVE hint bit according to SMCCCv1.3.
132*0fe7b9f2SOlivier Deprez  * It can be consumed by dispatchers using
133*0fe7b9f2SOlivier Deprez  * is_sve_hint_set macro.
134*0fe7b9f2SOlivier Deprez  *
1354693ff72SZelalem Aweke  */
1364693ff72SZelalem Aweke 
1372f370465SAntonio Nino Diaz #define SMC_FROM_SECURE		(U(0) << 0)
1382f370465SAntonio Nino Diaz #define SMC_FROM_NON_SECURE	(U(1) << 0)
1394693ff72SZelalem Aweke #define SMC_FROM_REALM		U(0x21)
1404693ff72SZelalem Aweke #define SMC_FROM_MASK		U(0x21)
141085e80ecSAntonio Nino Diaz 
142d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
143085e80ecSAntonio Nino Diaz 
144085e80ecSAntonio Nino Diaz #include <stdint.h>
145085e80ecSAntonio Nino Diaz 
14609d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
14709d40e0eSAntonio Nino Diaz 
1484693ff72SZelalem Aweke #if ENABLE_RME
1494693ff72SZelalem Aweke #define is_caller_non_secure(_f)	(((_f) & SMC_FROM_MASK) \
1504693ff72SZelalem Aweke 					  == SMC_FROM_NON_SECURE)
1514693ff72SZelalem Aweke #define is_caller_secure(_f)		(((_f) & SMC_FROM_MASK) \
1524693ff72SZelalem Aweke 					  == SMC_FROM_SECURE)
1534693ff72SZelalem Aweke #define is_caller_realm(_f)		(((_f) & SMC_FROM_MASK) \
1544693ff72SZelalem Aweke 					  == SMC_FROM_REALM)
1554693ff72SZelalem Aweke #define caller_sec_state(_f)		((_f) & SMC_FROM_MASK)
1564693ff72SZelalem Aweke #else /* ENABLE_RME */
157b3323cd6SAntonio Nino Diaz #define is_caller_non_secure(_f)	(((_f) & SMC_FROM_NON_SECURE) != U(0))
158b3323cd6SAntonio Nino Diaz #define is_caller_secure(_f)		(!is_caller_non_secure(_f))
1594693ff72SZelalem Aweke #endif /* ENABLE_RME */
160085e80ecSAntonio Nino Diaz 
161*0fe7b9f2SOlivier Deprez #define is_sve_hint_set(_f)		(((_f) & (FUNCID_SVE_HINT_MASK \
162*0fe7b9f2SOlivier Deprez 						<< FUNCID_SVE_HINT_SHIFT)) != U(0))
163*0fe7b9f2SOlivier Deprez 
164085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Standard Service SMC call */
1652f370465SAntonio Nino Diaz #define is_std_svc_call(_fid)		(GET_SMC_OEN(_fid) == OEN_STD_START)
166085e80ecSAntonio Nino Diaz 
167085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Arm Architectural Service SMC call */
1682f370465SAntonio Nino Diaz #define is_arm_arch_svc_call(_fid)	(GET_SMC_OEN(_fid) == OEN_ARM_START)
169085e80ecSAntonio Nino Diaz 
170085e80ecSAntonio Nino Diaz /* The macro below is used to identify a valid Fast SMC call */
171085e80ecSAntonio Nino Diaz #define is_valid_fast_smc(_fid)		((!(((_fid) >> 16) & U(0xff))) && \
172d7b5f408SJimmy Brisson 					   (GET_SMC_TYPE(_fid)		\
173d7b5f408SJimmy Brisson 					    == (uint32_t)SMC_TYPE_FAST))
174085e80ecSAntonio Nino Diaz 
175085e80ecSAntonio Nino Diaz /*
176085e80ecSAntonio Nino Diaz  * Macro to define UUID for services. Apart from defining and initializing a
177085e80ecSAntonio Nino Diaz  * uuid_t structure, this macro verifies that the first word of the defined UUID
178085e80ecSAntonio Nino Diaz  * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
179085e80ecSAntonio Nino Diaz  * returned UUID in x0 for an invalid SMC error return
180085e80ecSAntonio Nino Diaz  */
18103364865SRoberto Vargas #define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch,		\
18203364865SRoberto Vargas 		_n0, _n1, _n2, _n3, _n4, _n5)				\
183d74c6b83SJimmy Brisson 	CASSERT((uint32_t)(_tl) != (uint32_t)SMC_UNK,			\
184d74c6b83SJimmy Brisson 		invalid_svc_uuid_##_name);				\
18503364865SRoberto Vargas 	static const uuid_t _name = {					\
1863443a702SJohn Powell 		{((_tl) >> 24) & 0xFF,					\
1873443a702SJohn Powell 		 ((_tl) >> 16) & 0xFF,					\
1883443a702SJohn Powell 		 ((_tl) >> 8)  & 0xFF,					\
1893443a702SJohn Powell 		 ((_tl) & 0xFF)},					\
1903443a702SJohn Powell 		{((_tm) >> 8) & 0xFF,					\
1913443a702SJohn Powell 		 ((_tm)  & 0xFF)},					\
1923443a702SJohn Powell 		{((_th) >> 8) & 0xFF,					\
1933443a702SJohn Powell 		 ((_th) & 0xFF)},					\
1943443a702SJohn Powell 		(_cl), (_ch),						\
1953443a702SJohn Powell 		{ (_n0), (_n1), (_n2), (_n3), (_n4), (_n5) }		\
19603364865SRoberto Vargas 	}
197085e80ecSAntonio Nino Diaz 
19843b8fa8eSSandrine Bailleux /*
19943b8fa8eSSandrine Bailleux  * Return a UUID in the SMC return registers.
20043b8fa8eSSandrine Bailleux  *
20143b8fa8eSSandrine Bailleux  * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single
20243b8fa8eSSandrine Bailleux  * 128-bit value using the SMC32 calling convention. This value is mapped to
20343b8fa8eSSandrine Bailleux  * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example
20443b8fa8eSSandrine Bailleux  * shall hold bytes 0 to 3, with byte 0 in the low-order bits.
20543b8fa8eSSandrine Bailleux  */
20643b8fa8eSSandrine Bailleux static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
20743b8fa8eSSandrine Bailleux {
20843b8fa8eSSandrine Bailleux 	return ((uint32_t) b0) | (((uint32_t) b1) << 8) |
20943b8fa8eSSandrine Bailleux 		(((uint32_t) b2) << 16) | (((uint32_t) b3) << 24);
21043b8fa8eSSandrine Bailleux }
21143b8fa8eSSandrine Bailleux 
21243b8fa8eSSandrine Bailleux #define SMC_UUID_RET(_h, _uuid)							\
21343b8fa8eSSandrine Bailleux 	SMC_RET4(handle,							\
21443b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1],		\
21543b8fa8eSSandrine Bailleux 			      (_uuid).time_low[2], (_uuid).time_low[3]),	\
21643b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1],		\
21743b8fa8eSSandrine Bailleux 			      (_uuid).time_hi_and_version[0],			\
21843b8fa8eSSandrine Bailleux 			      (_uuid).time_hi_and_version[1]),			\
21943b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).clock_seq_hi_and_reserved,		\
22043b8fa8eSSandrine Bailleux 			      (_uuid).clock_seq_low, (_uuid).node[0],		\
22143b8fa8eSSandrine Bailleux 			      (_uuid).node[1]),					\
22243b8fa8eSSandrine Bailleux 		smc_uuid_word((_uuid).node[2], (_uuid).node[3],			\
22343b8fa8eSSandrine Bailleux 			      (_uuid).node[4], (_uuid).node[5]))
22443b8fa8eSSandrine Bailleux 
225d5dfdeb6SJulius Werner #endif /*__ASSEMBLER__*/
226c3cf06f1SAntonio Nino Diaz #endif /* SMCCC_H */
227