blob: cce91afe25700f133645220de8034301503a94d5 [file] [log] [blame]
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +00001/*
Manish V Badarkhe8a766032022-02-23 11:26:53 +00002 * Copyright (c) 2016-2022, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef SMCCC_H
8#define SMCCC_H
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +00009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <lib/utils_def.h>
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +000011
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +010012#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 Diaz39ed74d2018-05-02 09:52:35 +010017 ((((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 Diaz3c817f42018-03-21 10:49:27 +000021
Antonio Nino Diaz0e402d32019-01-30 16:01:49 +000022#define SMCCC_MAJOR_VERSION U(1)
Madhukar Pappireddy20be0772019-11-09 23:28:08 -060023#define SMCCC_MINOR_VERSION U(2)
Antonio Nino Diaz0e402d32019-01-30 16:01:49 +000024
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
Olivier Deprez33dd8452022-10-11 15:38:27 +020040#define FUNCID_SVE_HINT_SHIFT U(16)
41#define FUNCID_SVE_HINT_MASK U(1)
42#define FUNCID_SVE_HINT_WIDTH U(1)
43
Antonio Nino Diaz0e402d32019-01-30 16:01:49 +000044#define FUNCID_NUM_SHIFT U(0)
45#define FUNCID_NUM_MASK U(0xffff)
46#define FUNCID_NUM_WIDTH U(16)
47
Manish V Badarkhe8a766032022-02-23 11:26:53 +000048#define FUNCID_MASK U(0xffffffff)
49
Olivier Deprez304b7f92019-12-23 12:04:45 +010050#define GET_SMC_NUM(id) (((id) >> FUNCID_NUM_SHIFT) & \
51 FUNCID_NUM_MASK)
Antonio Nino Diaz0e402d32019-01-30 16:01:49 +000052#define GET_SMC_TYPE(id) (((id) >> FUNCID_TYPE_SHIFT) & \
53 FUNCID_TYPE_MASK)
54#define GET_SMC_CC(id) (((id) >> FUNCID_CC_SHIFT) & \
55 FUNCID_CC_MASK)
56#define GET_SMC_OEN(id) (((id) >> FUNCID_OEN_SHIFT) & \
57 FUNCID_OEN_MASK)
58
59/*******************************************************************************
Yann Gautierf008d402021-05-20 10:32:22 +020060 * SMCCC_ARCH_SOC_ID SoC version & revision bit definition
61 ******************************************************************************/
62#define SOC_ID_JEP_106_BANK_IDX_MASK GENMASK_32(30, 24)
63#define SOC_ID_JEP_106_BANK_IDX_SHIFT U(24)
64#define SOC_ID_JEP_106_ID_CODE_MASK GENMASK_32(23, 16)
65#define SOC_ID_JEP_106_ID_CODE_SHIFT U(16)
66#define SOC_ID_IMPL_DEF_MASK GENMASK_32(15, 0)
67#define SOC_ID_IMPL_DEF_SHIFT U(0)
68#define SOC_ID_SET_JEP_106(bkid, mfid) ((((bkid) << SOC_ID_JEP_106_BANK_IDX_SHIFT) & \
69 SOC_ID_JEP_106_BANK_IDX_MASK) | \
70 (((mfid) << SOC_ID_JEP_106_ID_CODE_SHIFT) & \
71 SOC_ID_JEP_106_ID_CODE_MASK))
72
73#define SOC_ID_REV_MASK GENMASK_32(30, 0)
74#define SOC_ID_REV_SHIFT U(0)
75
76/*******************************************************************************
Antonio Nino Diaz0e402d32019-01-30 16:01:49 +000077 * Owning entity number definitions inside the function id as per the SMC
78 * calling convention
79 ******************************************************************************/
80#define OEN_ARM_START U(0)
81#define OEN_ARM_END U(0)
82#define OEN_CPU_START U(1)
83#define OEN_CPU_END U(1)
84#define OEN_SIP_START U(2)
85#define OEN_SIP_END U(2)
86#define OEN_OEM_START U(3)
87#define OEN_OEM_END U(3)
88#define OEN_STD_START U(4) /* Standard Service Calls */
89#define OEN_STD_END U(4)
90#define OEN_STD_HYP_START U(5) /* Standard Hypervisor Service calls */
91#define OEN_STD_HYP_END U(5)
92#define OEN_VEN_HYP_START U(6) /* Vendor Hypervisor Service calls */
93#define OEN_VEN_HYP_END U(6)
94#define OEN_TAP_START U(48) /* Trusted Applications */
95#define OEN_TAP_END U(49)
96#define OEN_TOS_START U(50) /* Trusted OS */
97#define OEN_TOS_END U(63)
98#define OEN_LIMIT U(64)
99
100/* Flags and error codes */
101#define SMC_64 U(1)
102#define SMC_32 U(0)
103
Jimmy Brissoned202072020-08-04 16:18:52 -0500104#define SMC_TYPE_FAST UL(1)
105#define SMC_TYPE_YIELD UL(0)
Antonio Nino Diaz0e402d32019-01-30 16:01:49 +0000106
107#define SMC_OK ULL(0)
108#define SMC_UNK -1
109#define SMC_PREEMPTED -2 /* Not defined by the SMCCC */
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +0100110
Madhukar Pappireddy20be0772019-11-09 23:28:08 -0600111/* Return codes for Arm Architecture Service SMC calls */
112#define SMC_ARCH_CALL_SUCCESS 0
113#define SMC_ARCH_CALL_NOT_SUPPORTED -1
114#define SMC_ARCH_CALL_NOT_REQUIRED -2
115#define SMC_ARCH_CALL_INVAL_PARAM -3
116
Zelalem Aweke4d666ac2021-07-08 17:13:09 -0500117/*
118 * Various flags passed to SMC handlers
119 *
120 * Bit 5 and bit 0 of the flag are used to
121 * determine the source security state as
122 * follows:
123 * ---------------------------------
124 * Bit 5 | Bit 0 | Security state
125 * ---------------------------------
126 * 0 0 SMC_FROM_SECURE
127 * 0 1 SMC_FROM_NON_SECURE
128 * 1 1 SMC_FROM_REALM
Olivier Deprez33dd8452022-10-11 15:38:27 +0200129 *
130 * Bit 16 of flags records the caller's SMC
131 * SVE hint bit according to SMCCCv1.3.
132 * It can be consumed by dispatchers using
133 * is_sve_hint_set macro.
134 *
Zelalem Aweke4d666ac2021-07-08 17:13:09 -0500135 */
136
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +0100137#define SMC_FROM_SECURE (U(0) << 0)
138#define SMC_FROM_NON_SECURE (U(1) << 0)
Zelalem Aweke4d666ac2021-07-08 17:13:09 -0500139#define SMC_FROM_REALM U(0x21)
140#define SMC_FROM_MASK U(0x21)
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000141
Julius Werner53456fc2019-07-09 13:49:11 -0700142#ifndef __ASSEMBLER__
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000143
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000144#include <stdint.h>
145
Antonio Nino Diaze0f90632018-12-14 00:18:21 +0000146#include <lib/cassert.h>
147
Zelalem Aweke4d666ac2021-07-08 17:13:09 -0500148#if ENABLE_RME
149#define is_caller_non_secure(_f) (((_f) & SMC_FROM_MASK) \
150 == SMC_FROM_NON_SECURE)
151#define is_caller_secure(_f) (((_f) & SMC_FROM_MASK) \
152 == SMC_FROM_SECURE)
153#define is_caller_realm(_f) (((_f) & SMC_FROM_MASK) \
154 == SMC_FROM_REALM)
155#define caller_sec_state(_f) ((_f) & SMC_FROM_MASK)
156#else /* ENABLE_RME */
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100157#define is_caller_non_secure(_f) (((_f) & SMC_FROM_NON_SECURE) != U(0))
158#define is_caller_secure(_f) (!is_caller_non_secure(_f))
Zelalem Aweke4d666ac2021-07-08 17:13:09 -0500159#endif /* ENABLE_RME */
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000160
Olivier Deprez33dd8452022-10-11 15:38:27 +0200161#define is_sve_hint_set(_f) (((_f) & (FUNCID_SVE_HINT_MASK \
162 << FUNCID_SVE_HINT_SHIFT)) != U(0))
163
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000164/* The macro below is used to identify a Standard Service SMC call */
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +0100165#define is_std_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_STD_START)
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000166
167/* The macro below is used to identify a Arm Architectural Service SMC call */
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +0100168#define is_arm_arch_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_ARM_START)
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000169
170/* The macro below is used to identify a valid Fast SMC call */
171#define is_valid_fast_smc(_fid) ((!(((_fid) >> 16) & U(0xff))) && \
Jimmy Brissoned202072020-08-04 16:18:52 -0500172 (GET_SMC_TYPE(_fid) \
173 == (uint32_t)SMC_TYPE_FAST))
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000174
175/*
176 * Macro to define UUID for services. Apart from defining and initializing a
177 * uuid_t structure, this macro verifies that the first word of the defined UUID
178 * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
179 * returned UUID in x0 for an invalid SMC error return
180 */
Roberto Vargaseace8f12018-04-26 13:36:53 +0100181#define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch, \
182 _n0, _n1, _n2, _n3, _n4, _n5) \
Jimmy Brissond7297c72020-08-05 14:05:53 -0500183 CASSERT((uint32_t)(_tl) != (uint32_t)SMC_UNK, \
184 invalid_svc_uuid_##_name); \
Roberto Vargaseace8f12018-04-26 13:36:53 +0100185 static const uuid_t _name = { \
John Powella5c66362020-03-20 14:21:05 -0500186 {((_tl) >> 24) & 0xFF, \
187 ((_tl) >> 16) & 0xFF, \
188 ((_tl) >> 8) & 0xFF, \
189 ((_tl) & 0xFF)}, \
190 {((_tm) >> 8) & 0xFF, \
191 ((_tm) & 0xFF)}, \
192 {((_th) >> 8) & 0xFF, \
193 ((_th) & 0xFF)}, \
194 (_cl), (_ch), \
195 { (_n0), (_n1), (_n2), (_n3), (_n4), (_n5) } \
Roberto Vargaseace8f12018-04-26 13:36:53 +0100196 }
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +0000197
Sandrine Bailleuxc5e89782018-07-02 13:01:16 +0200198/*
199 * Return a UUID in the SMC return registers.
200 *
201 * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single
202 * 128-bit value using the SMC32 calling convention. This value is mapped to
203 * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example
204 * shall hold bytes 0 to 3, with byte 0 in the low-order bits.
205 */
206static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
207{
208 return ((uint32_t) b0) | (((uint32_t) b1) << 8) |
209 (((uint32_t) b2) << 16) | (((uint32_t) b3) << 24);
210}
211
212#define SMC_UUID_RET(_h, _uuid) \
213 SMC_RET4(handle, \
214 smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1], \
215 (_uuid).time_low[2], (_uuid).time_low[3]), \
216 smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1], \
217 (_uuid).time_hi_and_version[0], \
218 (_uuid).time_hi_and_version[1]), \
219 smc_uuid_word((_uuid).clock_seq_hi_and_reserved, \
220 (_uuid).clock_seq_low, (_uuid).node[0], \
221 (_uuid).node[1]), \
222 smc_uuid_word((_uuid).node[2], (_uuid).node[3], \
223 (_uuid).node[4], (_uuid).node[5]))
224
Julius Werner53456fc2019-07-09 13:49:11 -0700225#endif /*__ASSEMBLER__*/
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000226#endif /* SMCCC_H */