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