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