1085e80ecSAntonio Nino Diaz /*
2*be5b1e22SGovindraj Raja * Copyright (c) 2016-2024, 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)
23*be5b1e22SGovindraj Raja #define SMCCC_MINOR_VERSION U(5)
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
40f8a35797SJayanth Dodderi Chidanand #define FUNCID_FC_RESERVED_SHIFT U(17)
41f8a35797SJayanth Dodderi Chidanand #define FUNCID_FC_RESERVED_MASK U(0x7f)
42f8a35797SJayanth Dodderi Chidanand #define FUNCID_FC_RESERVED_WIDTH U(7)
43f8a35797SJayanth Dodderi Chidanand
440fe7b9f2SOlivier Deprez #define FUNCID_SVE_HINT_SHIFT U(16)
450fe7b9f2SOlivier Deprez #define FUNCID_SVE_HINT_MASK U(1)
460fe7b9f2SOlivier Deprez #define FUNCID_SVE_HINT_WIDTH U(1)
470fe7b9f2SOlivier Deprez
480709055eSAntonio Nino Diaz #define FUNCID_NUM_SHIFT U(0)
490709055eSAntonio Nino Diaz #define FUNCID_NUM_MASK U(0xffff)
500709055eSAntonio Nino Diaz #define FUNCID_NUM_WIDTH U(16)
510709055eSAntonio Nino Diaz
52e62748e3SManish V Badarkhe #define FUNCID_MASK U(0xffffffff)
53e62748e3SManish V Badarkhe
54e073e070SOlivier Deprez #define GET_SMC_NUM(id) (((id) >> FUNCID_NUM_SHIFT) & \
55e073e070SOlivier Deprez FUNCID_NUM_MASK)
560709055eSAntonio Nino Diaz #define GET_SMC_TYPE(id) (((id) >> FUNCID_TYPE_SHIFT) & \
570709055eSAntonio Nino Diaz FUNCID_TYPE_MASK)
580709055eSAntonio Nino Diaz #define GET_SMC_CC(id) (((id) >> FUNCID_CC_SHIFT) & \
590709055eSAntonio Nino Diaz FUNCID_CC_MASK)
600709055eSAntonio Nino Diaz #define GET_SMC_OEN(id) (((id) >> FUNCID_OEN_SHIFT) & \
610709055eSAntonio Nino Diaz FUNCID_OEN_MASK)
620709055eSAntonio Nino Diaz
630709055eSAntonio Nino Diaz /*******************************************************************************
6496b0596eSYann Gautier * SMCCC_ARCH_SOC_ID SoC version & revision bit definition
6596b0596eSYann Gautier ******************************************************************************/
6696b0596eSYann Gautier #define SOC_ID_JEP_106_BANK_IDX_MASK GENMASK_32(30, 24)
6796b0596eSYann Gautier #define SOC_ID_JEP_106_BANK_IDX_SHIFT U(24)
6896b0596eSYann Gautier #define SOC_ID_JEP_106_ID_CODE_MASK GENMASK_32(23, 16)
6996b0596eSYann Gautier #define SOC_ID_JEP_106_ID_CODE_SHIFT U(16)
7096b0596eSYann Gautier #define SOC_ID_IMPL_DEF_MASK GENMASK_32(15, 0)
7196b0596eSYann Gautier #define SOC_ID_IMPL_DEF_SHIFT U(0)
7296b0596eSYann Gautier #define SOC_ID_SET_JEP_106(bkid, mfid) ((((bkid) << SOC_ID_JEP_106_BANK_IDX_SHIFT) & \
7396b0596eSYann Gautier SOC_ID_JEP_106_BANK_IDX_MASK) | \
7496b0596eSYann Gautier (((mfid) << SOC_ID_JEP_106_ID_CODE_SHIFT) & \
7596b0596eSYann Gautier SOC_ID_JEP_106_ID_CODE_MASK))
7696b0596eSYann Gautier
7796b0596eSYann Gautier #define SOC_ID_REV_MASK GENMASK_32(30, 0)
7896b0596eSYann Gautier #define SOC_ID_REV_SHIFT U(0)
7996b0596eSYann Gautier
8096b0596eSYann Gautier /*******************************************************************************
810709055eSAntonio Nino Diaz * Owning entity number definitions inside the function id as per the SMC
820709055eSAntonio Nino Diaz * calling convention
830709055eSAntonio Nino Diaz ******************************************************************************/
840709055eSAntonio Nino Diaz #define OEN_ARM_START U(0)
850709055eSAntonio Nino Diaz #define OEN_ARM_END U(0)
860709055eSAntonio Nino Diaz #define OEN_CPU_START U(1)
870709055eSAntonio Nino Diaz #define OEN_CPU_END U(1)
880709055eSAntonio Nino Diaz #define OEN_SIP_START U(2)
890709055eSAntonio Nino Diaz #define OEN_SIP_END U(2)
900709055eSAntonio Nino Diaz #define OEN_OEM_START U(3)
910709055eSAntonio Nino Diaz #define OEN_OEM_END U(3)
920709055eSAntonio Nino Diaz #define OEN_STD_START U(4) /* Standard Service Calls */
930709055eSAntonio Nino Diaz #define OEN_STD_END U(4)
940709055eSAntonio Nino Diaz #define OEN_STD_HYP_START U(5) /* Standard Hypervisor Service calls */
950709055eSAntonio Nino Diaz #define OEN_STD_HYP_END U(5)
960709055eSAntonio Nino Diaz #define OEN_VEN_HYP_START U(6) /* Vendor Hypervisor Service calls */
970709055eSAntonio Nino Diaz #define OEN_VEN_HYP_END U(6)
98*be5b1e22SGovindraj Raja #define OEN_VEN_EL3_START U(7) /* Vendor Specific EL3 Monitor Calls */
99*be5b1e22SGovindraj Raja #define OEN_VEN_EL3_END U(7)
1000709055eSAntonio Nino Diaz #define OEN_TAP_START U(48) /* Trusted Applications */
1010709055eSAntonio Nino Diaz #define OEN_TAP_END U(49)
1020709055eSAntonio Nino Diaz #define OEN_TOS_START U(50) /* Trusted OS */
1030709055eSAntonio Nino Diaz #define OEN_TOS_END U(63)
1040709055eSAntonio Nino Diaz #define OEN_LIMIT U(64)
1050709055eSAntonio Nino Diaz
1060709055eSAntonio Nino Diaz /* Flags and error codes */
1070709055eSAntonio Nino Diaz #define SMC_64 U(1)
1080709055eSAntonio Nino Diaz #define SMC_32 U(0)
1090709055eSAntonio Nino Diaz
110d7b5f408SJimmy Brisson #define SMC_TYPE_FAST UL(1)
111d7b5f408SJimmy Brisson #define SMC_TYPE_YIELD UL(0)
1120709055eSAntonio Nino Diaz
1130709055eSAntonio Nino Diaz #define SMC_OK ULL(0)
1140709055eSAntonio Nino Diaz #define SMC_UNK -1
1150709055eSAntonio Nino Diaz #define SMC_PREEMPTED -2 /* Not defined by the SMCCC */
1166873088cSJ-Alves #define SMC_DENIED -3 /* Not defined by the SMCCC */
1176873088cSJ-Alves #define SMC_INVALID_PARAM -4 /* Not defined by the SMCCC */
1182f370465SAntonio Nino Diaz
119e34cc0ceSMadhukar Pappireddy /* Return codes for Arm Architecture Service SMC calls */
120e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_SUCCESS 0
121e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_NOT_SUPPORTED -1
122e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_NOT_REQUIRED -2
123e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_INVAL_PARAM -3
124e34cc0ceSMadhukar Pappireddy
1254693ff72SZelalem Aweke /*
1264693ff72SZelalem Aweke * Various flags passed to SMC handlers
1274693ff72SZelalem Aweke *
1284693ff72SZelalem Aweke * Bit 5 and bit 0 of the flag are used to
1294693ff72SZelalem Aweke * determine the source security state as
1304693ff72SZelalem Aweke * follows:
1314693ff72SZelalem Aweke * ---------------------------------
1324693ff72SZelalem Aweke * Bit 5 | Bit 0 | Security state
1334693ff72SZelalem Aweke * ---------------------------------
1344693ff72SZelalem Aweke * 0 0 SMC_FROM_SECURE
1354693ff72SZelalem Aweke * 0 1 SMC_FROM_NON_SECURE
1364693ff72SZelalem Aweke * 1 1 SMC_FROM_REALM
1370fe7b9f2SOlivier Deprez *
1380fe7b9f2SOlivier Deprez * Bit 16 of flags records the caller's SMC
1390fe7b9f2SOlivier Deprez * SVE hint bit according to SMCCCv1.3.
1400fe7b9f2SOlivier Deprez * It can be consumed by dispatchers using
1410fe7b9f2SOlivier Deprez * is_sve_hint_set macro.
1420fe7b9f2SOlivier Deprez *
1434693ff72SZelalem Aweke */
1444693ff72SZelalem Aweke
1452f370465SAntonio Nino Diaz #define SMC_FROM_SECURE (U(0) << 0)
1462f370465SAntonio Nino Diaz #define SMC_FROM_NON_SECURE (U(1) << 0)
1474693ff72SZelalem Aweke #define SMC_FROM_REALM U(0x21)
1484693ff72SZelalem Aweke #define SMC_FROM_MASK U(0x21)
149085e80ecSAntonio Nino Diaz
150d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
151085e80ecSAntonio Nino Diaz
152085e80ecSAntonio Nino Diaz #include <stdint.h>
153085e80ecSAntonio Nino Diaz
15409d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
15509d40e0eSAntonio Nino Diaz
1564693ff72SZelalem Aweke #if ENABLE_RME
1574693ff72SZelalem Aweke #define is_caller_non_secure(_f) (((_f) & SMC_FROM_MASK) \
1584693ff72SZelalem Aweke == SMC_FROM_NON_SECURE)
1594693ff72SZelalem Aweke #define is_caller_secure(_f) (((_f) & SMC_FROM_MASK) \
1604693ff72SZelalem Aweke == SMC_FROM_SECURE)
1614693ff72SZelalem Aweke #define is_caller_realm(_f) (((_f) & SMC_FROM_MASK) \
1624693ff72SZelalem Aweke == SMC_FROM_REALM)
1634693ff72SZelalem Aweke #define caller_sec_state(_f) ((_f) & SMC_FROM_MASK)
1644693ff72SZelalem Aweke #else /* ENABLE_RME */
165b3323cd6SAntonio Nino Diaz #define is_caller_non_secure(_f) (((_f) & SMC_FROM_NON_SECURE) != U(0))
166b3323cd6SAntonio Nino Diaz #define is_caller_secure(_f) (!is_caller_non_secure(_f))
1674693ff72SZelalem Aweke #endif /* ENABLE_RME */
168085e80ecSAntonio Nino Diaz
1690fe7b9f2SOlivier Deprez #define is_sve_hint_set(_f) (((_f) & (FUNCID_SVE_HINT_MASK \
1700fe7b9f2SOlivier Deprez << FUNCID_SVE_HINT_SHIFT)) != U(0))
1710fe7b9f2SOlivier Deprez
172085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Standard Service SMC call */
1732f370465SAntonio Nino Diaz #define is_std_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_STD_START)
174085e80ecSAntonio Nino Diaz
175085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Arm Architectural Service SMC call */
1762f370465SAntonio Nino Diaz #define is_arm_arch_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_ARM_START)
177085e80ecSAntonio Nino Diaz
178085e80ecSAntonio Nino Diaz /* The macro below is used to identify a valid Fast SMC call */
179085e80ecSAntonio Nino Diaz #define is_valid_fast_smc(_fid) ((!(((_fid) >> 16) & U(0xff))) && \
180d7b5f408SJimmy Brisson (GET_SMC_TYPE(_fid) \
181d7b5f408SJimmy Brisson == (uint32_t)SMC_TYPE_FAST))
182085e80ecSAntonio Nino Diaz
183085e80ecSAntonio Nino Diaz /*
184085e80ecSAntonio Nino Diaz * Macro to define UUID for services. Apart from defining and initializing a
185085e80ecSAntonio Nino Diaz * uuid_t structure, this macro verifies that the first word of the defined UUID
186085e80ecSAntonio Nino Diaz * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
187085e80ecSAntonio Nino Diaz * returned UUID in x0 for an invalid SMC error return
188085e80ecSAntonio Nino Diaz */
18903364865SRoberto Vargas #define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch, \
19003364865SRoberto Vargas _n0, _n1, _n2, _n3, _n4, _n5) \
191d74c6b83SJimmy Brisson CASSERT((uint32_t)(_tl) != (uint32_t)SMC_UNK, \
192d74c6b83SJimmy Brisson invalid_svc_uuid_##_name); \
19303364865SRoberto Vargas static const uuid_t _name = { \
1943443a702SJohn Powell {((_tl) >> 24) & 0xFF, \
1953443a702SJohn Powell ((_tl) >> 16) & 0xFF, \
1963443a702SJohn Powell ((_tl) >> 8) & 0xFF, \
1973443a702SJohn Powell ((_tl) & 0xFF)}, \
1983443a702SJohn Powell {((_tm) >> 8) & 0xFF, \
1993443a702SJohn Powell ((_tm) & 0xFF)}, \
2003443a702SJohn Powell {((_th) >> 8) & 0xFF, \
2013443a702SJohn Powell ((_th) & 0xFF)}, \
2023443a702SJohn Powell (_cl), (_ch), \
2033443a702SJohn Powell { (_n0), (_n1), (_n2), (_n3), (_n4), (_n5) } \
20403364865SRoberto Vargas }
205085e80ecSAntonio Nino Diaz
20643b8fa8eSSandrine Bailleux /*
20743b8fa8eSSandrine Bailleux * Return a UUID in the SMC return registers.
20843b8fa8eSSandrine Bailleux *
20943b8fa8eSSandrine Bailleux * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single
21043b8fa8eSSandrine Bailleux * 128-bit value using the SMC32 calling convention. This value is mapped to
21143b8fa8eSSandrine Bailleux * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example
21243b8fa8eSSandrine Bailleux * shall hold bytes 0 to 3, with byte 0 in the low-order bits.
21343b8fa8eSSandrine Bailleux */
smc_uuid_word(uint8_t b0,uint8_t b1,uint8_t b2,uint8_t b3)21443b8fa8eSSandrine Bailleux static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
21543b8fa8eSSandrine Bailleux {
21643b8fa8eSSandrine Bailleux return ((uint32_t) b0) | (((uint32_t) b1) << 8) |
21743b8fa8eSSandrine Bailleux (((uint32_t) b2) << 16) | (((uint32_t) b3) << 24);
21843b8fa8eSSandrine Bailleux }
21943b8fa8eSSandrine Bailleux
22043b8fa8eSSandrine Bailleux #define SMC_UUID_RET(_h, _uuid) \
22143b8fa8eSSandrine Bailleux SMC_RET4(handle, \
22243b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1], \
22343b8fa8eSSandrine Bailleux (_uuid).time_low[2], (_uuid).time_low[3]), \
22443b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1], \
22543b8fa8eSSandrine Bailleux (_uuid).time_hi_and_version[0], \
22643b8fa8eSSandrine Bailleux (_uuid).time_hi_and_version[1]), \
22743b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).clock_seq_hi_and_reserved, \
22843b8fa8eSSandrine Bailleux (_uuid).clock_seq_low, (_uuid).node[0], \
22943b8fa8eSSandrine Bailleux (_uuid).node[1]), \
23043b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).node[2], (_uuid).node[3], \
23143b8fa8eSSandrine Bailleux (_uuid).node[4], (_uuid).node[5]))
23243b8fa8eSSandrine Bailleux
233d5dfdeb6SJulius Werner #endif /*__ASSEMBLER__*/
234c3cf06f1SAntonio Nino Diaz #endif /* SMCCC_H */
235