1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  */
5 #ifndef __LINUX_ARM_SMCCC_H
6 #define __LINUX_ARM_SMCCC_H
7 
8 #include <linux/args.h>
9 #include <linux/init.h>
10 #include <uapi/linux/const.h>
11 
12 /*
13  * This file provides common defines for ARM SMC Calling Convention as
14  * specified in
15  * https://developer.arm.com/docs/den0028/latest
16  *
17  * This code is up-to-date with version DEN 0028 C
18  */
19 
20 #define ARM_SMCCC_STD_CALL	        _AC(0,U)
21 #define ARM_SMCCC_FAST_CALL	        _AC(1,U)
22 #define ARM_SMCCC_TYPE_SHIFT		31
23 
24 #define ARM_SMCCC_SMC_32		0
25 #define ARM_SMCCC_SMC_64		1
26 #define ARM_SMCCC_CALL_CONV_SHIFT	30
27 
28 #define ARM_SMCCC_OWNER_MASK		0x3F
29 #define ARM_SMCCC_OWNER_SHIFT		24
30 
31 #define ARM_SMCCC_FUNC_MASK		0xFFFF
32 
33 #define ARM_SMCCC_IS_FAST_CALL(smc_val)	\
34 	((smc_val) & (ARM_SMCCC_FAST_CALL << ARM_SMCCC_TYPE_SHIFT))
35 #define ARM_SMCCC_IS_64(smc_val) \
36 	((smc_val) & (ARM_SMCCC_SMC_64 << ARM_SMCCC_CALL_CONV_SHIFT))
37 #define ARM_SMCCC_FUNC_NUM(smc_val)	((smc_val) & ARM_SMCCC_FUNC_MASK)
38 #define ARM_SMCCC_OWNER_NUM(smc_val) \
39 	(((smc_val) >> ARM_SMCCC_OWNER_SHIFT) & ARM_SMCCC_OWNER_MASK)
40 
41 #define ARM_SMCCC_CALL_VAL(type, calling_convention, owner, func_num) \
42 	(((type) << ARM_SMCCC_TYPE_SHIFT) | \
43 	((calling_convention) << ARM_SMCCC_CALL_CONV_SHIFT) | \
44 	(((owner) & ARM_SMCCC_OWNER_MASK) << ARM_SMCCC_OWNER_SHIFT) | \
45 	((func_num) & ARM_SMCCC_FUNC_MASK))
46 
47 #define ARM_SMCCC_OWNER_ARCH		0
48 #define ARM_SMCCC_OWNER_CPU		1
49 #define ARM_SMCCC_OWNER_SIP		2
50 #define ARM_SMCCC_OWNER_OEM		3
51 #define ARM_SMCCC_OWNER_STANDARD	4
52 #define ARM_SMCCC_OWNER_STANDARD_HYP	5
53 #define ARM_SMCCC_OWNER_VENDOR_HYP	6
54 #define ARM_SMCCC_OWNER_TRUSTED_APP	48
55 #define ARM_SMCCC_OWNER_TRUSTED_APP_END	49
56 #define ARM_SMCCC_OWNER_TRUSTED_OS	50
57 #define ARM_SMCCC_OWNER_TRUSTED_OS_END	63
58 
59 #define ARM_SMCCC_FUNC_QUERY_CALL_UID  0xff01
60 
61 #define ARM_SMCCC_QUIRK_NONE		0
62 #define ARM_SMCCC_QUIRK_QCOM_A6		1 /* Save/restore register a6 */
63 
64 #define ARM_SMCCC_VERSION_1_0		0x10000
65 #define ARM_SMCCC_VERSION_1_1		0x10001
66 #define ARM_SMCCC_VERSION_1_2		0x10002
67 #define ARM_SMCCC_VERSION_1_3		0x10003
68 
69 #define ARM_SMCCC_1_3_SVE_HINT		0x10000
70 #define ARM_SMCCC_CALL_HINTS		ARM_SMCCC_1_3_SVE_HINT
71 
72 
73 #define ARM_SMCCC_VERSION_FUNC_ID					\
74 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
75 			   ARM_SMCCC_SMC_32,				\
76 			   0, 0)
77 
78 #define ARM_SMCCC_ARCH_FEATURES_FUNC_ID					\
79 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
80 			   ARM_SMCCC_SMC_32,				\
81 			   0, 1)
82 
83 #define ARM_SMCCC_ARCH_SOC_ID						\
84 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
85 			   ARM_SMCCC_SMC_32,				\
86 			   0, 2)
87 
88 #define ARM_SMCCC_ARCH_WORKAROUND_1					\
89 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
90 			   ARM_SMCCC_SMC_32,				\
91 			   0, 0x8000)
92 
93 #define ARM_SMCCC_ARCH_WORKAROUND_2					\
94 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
95 			   ARM_SMCCC_SMC_32,				\
96 			   0, 0x7fff)
97 
98 #define ARM_SMCCC_ARCH_WORKAROUND_3					\
99 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
100 			   ARM_SMCCC_SMC_32,				\
101 			   0, 0x3fff)
102 
103 #define ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID				\
104 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
105 			   ARM_SMCCC_SMC_32,				\
106 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
107 			   ARM_SMCCC_FUNC_QUERY_CALL_UID)
108 
109 /* KVM UID value: 28b46fb6-2ec5-11e9-a9ca-4b564d003a74 */
110 #define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0	0xb66fb428U
111 #define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1	0xe911c52eU
112 #define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2	0x564bcaa9U
113 #define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3	0x743a004dU
114 
115 #define ARM_SMCCC_KVM_PVIOMMU_READ		(1 << 0)
116 #define ARM_SMCCC_KVM_PVIOMMU_WRITE		(1 << 1)
117 #define ARM_SMCCC_KVM_PVIOMMU_CACHE		(1 << 2)
118 #define ARM_SMCCC_KVM_PVIOMMU_NOEXEC		(1 << 3)
119 #define ARM_SMCCC_KVM_PVIOMMU_MMIO		(1 << 4)
120 #define ARM_SMCCC_KVM_PVIOMMU_PRIV		(1 << 5)
121 
122 /* KVM "vendor specific" services */
123 #define ARM_SMCCC_KVM_FUNC_FEATURES		0
124 #define ARM_SMCCC_KVM_FUNC_PTP			1
125 /* Start of pKVM hypercall range */
126 #define ARM_SMCCC_KVM_FUNC_HYP_MEMINFO		2
127 #define ARM_SMCCC_KVM_FUNC_MEM_SHARE		3
128 #define ARM_SMCCC_KVM_FUNC_MEM_UNSHARE		4
129 #define ARM_SMCCC_KVM_FUNC_MMIO_GUARD_INFO	5
130 #define ARM_SMCCC_KVM_FUNC_MMIO_GUARD_ENROLL	6
131 #define ARM_SMCCC_KVM_FUNC_MMIO_GUARD_MAP	7
132 #define ARM_SMCCC_KVM_FUNC_MMIO_GUARD_UNMAP	8
133 #define ARM_SMCCC_KVM_FUNC_MEM_RELINQUISH	9
134 #define ARM_SMCCC_KVM_FUNC_MMIO_RGUARD_MAP	10
135 #define ARM_SMCCC_KVM_FUNC_MMIO_RGUARD_UNMAP	11
136 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_12		12
137 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_13		13
138 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_14		14
139 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_15		15
140 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_16		16
141 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_17		17
142 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_18		18
143 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_19		19
144 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_20		20
145 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_21		21
146 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_22		22
147 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_23		23
148 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_24		24
149 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_25		25
150 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_26		26
151 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_27		27
152 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_28		28
153 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_29		29
154 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_30		30
155 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_31		31
156 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_32		32
157 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_33		33
158 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_34		34
159 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_35		35
160 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_36		36
161 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_37		37
162 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_38		38
163 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_39		39
164 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_40		40
165 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_41		41
166 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_42		42
167 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_43		43
168 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_44		44
169 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_45		45
170 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_46		46
171 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_47		47
172 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_48		48
173 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_49		49
174 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_50		50
175 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_51		51
176 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_52		52
177 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_53		53
178 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_54		54
179 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_55		55
180 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_56		56
181 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_57		57
182 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_58		58
183 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_59		59
184 #define ARM_SMCCC_KVM_FUNC_PKVM_RESV_60		60
185 #define ARM_SMCCC_KVM_FUNC_DEV_REQ_DMA		61
186 #define ARM_SMCCC_KVM_FUNC_PVIOMMU_OP		62
187 #define ARM_SMCCC_KVM_FUNC_DEV_REQ_MMIO		63
188 /* End of pKVM hypercall range */
189 #define ARM_SMCCC_KVM_FUNC_FEATURES_2		127
190 #define ARM_SMCCC_KVM_NUM_FUNCS			128
191 
192 #define KVM_FUNC_HAS_RANGE                  BIT(0)
193 
194 #define ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID			\
195 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
196 			   ARM_SMCCC_SMC_32,				\
197 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
198 			   ARM_SMCCC_KVM_FUNC_FEATURES)
199 
200 #define SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED	1
201 
202 /*
203  * ptp_kvm is a feature used for time sync between vm and host.
204  * ptp_kvm module in guest kernel will get service from host using
205  * this hypercall ID.
206  */
207 #define ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID				\
208 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
209 			   ARM_SMCCC_SMC_32,				\
210 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
211 			   ARM_SMCCC_KVM_FUNC_PTP)
212 
213 #define ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID			\
214 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
215 			   ARM_SMCCC_SMC_64,				\
216 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
217 			   ARM_SMCCC_KVM_FUNC_HYP_MEMINFO)
218 
219 #define ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID			\
220 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
221 			   ARM_SMCCC_SMC_64,				\
222 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
223 			   ARM_SMCCC_KVM_FUNC_MEM_SHARE)
224 
225 #define ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID			\
226 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
227 			   ARM_SMCCC_SMC_64,				\
228 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
229 			   ARM_SMCCC_KVM_FUNC_MEM_UNSHARE)
230 
231 #define ARM_SMCCC_VENDOR_HYP_KVM_MEM_RELINQUISH_FUNC_ID			\
232 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
233 			   ARM_SMCCC_SMC_64,				\
234 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
235 			   ARM_SMCCC_KVM_FUNC_MEM_RELINQUISH)
236 
237 #define KVM_PVIOMMU_OP_ATTACH_DEV			0
238 #define KVM_PVIOMMU_OP_DETACH_DEV			1
239 #define KVM_PVIOMMU_OP_ALLOC_DOMAIN			2
240 #define KVM_PVIOMMU_OP_FREE_DOMAIN			3
241 #define KVM_PVIOMMU_OP_MAP_PAGES			4
242 #define KVM_PVIOMMU_OP_UNMAP_PAGES			5
243 
244 #define ARM_SMCCC_VENDOR_HYP_KVM_PVIOMMU_OP_FUNC_ID			\
245 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
246 			   ARM_SMCCC_SMC_64,				\
247 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
248 			   ARM_SMCCC_KVM_FUNC_PVIOMMU_OP)
249 
250 #define ARM_SMCCC_VENDOR_HYP_KVM_DEV_REQ_MMIO_FUNC_ID			\
251 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
252 			   ARM_SMCCC_SMC_64,				\
253 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
254 			   ARM_SMCCC_KVM_FUNC_DEV_REQ_MMIO)
255 
256 #define ARM_SMCCC_VENDOR_HYP_KVM_DEV_REQ_DMA_FUNC_ID			\
257 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
258 			   ARM_SMCCC_SMC_64,				\
259 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
260 			   ARM_SMCCC_KVM_FUNC_DEV_REQ_DMA)
261 
262 /* ptp_kvm counter type ID */
263 #define KVM_PTP_VIRT_COUNTER			0
264 #define KVM_PTP_PHYS_COUNTER			1
265 
266 #define ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID		\
267 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
268 			   ARM_SMCCC_SMC_64,				\
269 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
270 			   ARM_SMCCC_KVM_FUNC_MMIO_GUARD_INFO)
271 
272 #define ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID		\
273 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
274 			   ARM_SMCCC_SMC_64,				\
275 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
276 			   ARM_SMCCC_KVM_FUNC_MMIO_GUARD_ENROLL)
277 
278 #define ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID			\
279 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
280 			   ARM_SMCCC_SMC_64,				\
281 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
282 			   ARM_SMCCC_KVM_FUNC_MMIO_GUARD_MAP)
283 
284 #define ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID		\
285 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
286 			   ARM_SMCCC_SMC_64,				\
287 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
288 			   ARM_SMCCC_KVM_FUNC_MMIO_GUARD_UNMAP)
289 
290 #define ARM_SMCCC_VENDOR_HYP_KVM_MMIO_RGUARD_MAP_FUNC_ID		\
291 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
292 			   ARM_SMCCC_SMC_64,				\
293 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
294 			   ARM_SMCCC_KVM_FUNC_MMIO_RGUARD_MAP)
295 
296 #define ARM_SMCCC_VENDOR_HYP_KVM_MMIO_RGUARD_UNMAP_FUNC_ID		\
297 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
298 			   ARM_SMCCC_SMC_64,				\
299 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
300 			   ARM_SMCCC_KVM_FUNC_MMIO_RGUARD_UNMAP)
301 
302 /* Paravirtualised time calls (defined by ARM DEN0057A) */
303 #define ARM_SMCCC_HV_PV_TIME_FEATURES				\
304 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
305 			   ARM_SMCCC_SMC_64,			\
306 			   ARM_SMCCC_OWNER_STANDARD_HYP,	\
307 			   0x20)
308 
309 #define ARM_SMCCC_HV_PV_TIME_ST					\
310 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
311 			   ARM_SMCCC_SMC_64,			\
312 			   ARM_SMCCC_OWNER_STANDARD_HYP,	\
313 			   0x21)
314 
315 /* TRNG entropy source calls (defined by ARM DEN0098) */
316 #define ARM_SMCCC_TRNG_VERSION					\
317 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
318 			   ARM_SMCCC_SMC_32,			\
319 			   ARM_SMCCC_OWNER_STANDARD,		\
320 			   0x50)
321 
322 #define ARM_SMCCC_TRNG_FEATURES					\
323 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
324 			   ARM_SMCCC_SMC_32,			\
325 			   ARM_SMCCC_OWNER_STANDARD,		\
326 			   0x51)
327 
328 #define ARM_SMCCC_TRNG_GET_UUID					\
329 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
330 			   ARM_SMCCC_SMC_32,			\
331 			   ARM_SMCCC_OWNER_STANDARD,		\
332 			   0x52)
333 
334 #define ARM_SMCCC_TRNG_RND32					\
335 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
336 			   ARM_SMCCC_SMC_32,			\
337 			   ARM_SMCCC_OWNER_STANDARD,		\
338 			   0x53)
339 
340 #define ARM_SMCCC_TRNG_RND64					\
341 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
342 			   ARM_SMCCC_SMC_64,			\
343 			   ARM_SMCCC_OWNER_STANDARD,		\
344 			   0x53)
345 
346 #define ARM_CCA_FUNC_END       0x840001CF
347 #define ARM_CCA_64BIT_FUNC_END 0xC40001CF
348 
349 /*
350  * Return codes defined in ARM DEN 0070A
351  * ARM DEN 0070A is now merged/consolidated into ARM DEN 0028 C
352  */
353 #define SMCCC_RET_SUCCESS			0
354 #define SMCCC_RET_NOT_SUPPORTED			-1
355 #define SMCCC_RET_NOT_REQUIRED			-2
356 #define SMCCC_RET_INVALID_PARAMETER		-3
357 
358 #ifndef __ASSEMBLY__
359 
360 #include <linux/linkage.h>
361 #include <linux/types.h>
362 
363 enum arm_smccc_conduit {
364 	SMCCC_CONDUIT_NONE,
365 	SMCCC_CONDUIT_SMC,
366 	SMCCC_CONDUIT_HVC,
367 };
368 
369 /**
370  * arm_smccc_1_1_get_conduit()
371  *
372  * Returns the conduit to be used for SMCCCv1.1 or later.
373  *
374  * When SMCCCv1.1 is not present, returns SMCCC_CONDUIT_NONE.
375  */
376 enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void);
377 
378 /**
379  * arm_smccc_get_version()
380  *
381  * Returns the version to be used for SMCCCv1.1 or later.
382  *
383  * When SMCCCv1.1 or above is not present, returns SMCCCv1.0, but this
384  * does not imply the presence of firmware or a valid conduit. Caller
385  * handling SMCCCv1.0 must determine the conduit by other means.
386  */
387 u32 arm_smccc_get_version(void);
388 
389 void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit);
390 
391 /**
392  * arm_smccc_get_soc_id_version()
393  *
394  * Returns the SOC ID version.
395  *
396  * When ARM_SMCCC_ARCH_SOC_ID is not present, returns SMCCC_RET_NOT_SUPPORTED.
397  */
398 s32 arm_smccc_get_soc_id_version(void);
399 
400 /**
401  * arm_smccc_get_soc_id_revision()
402  *
403  * Returns the SOC ID revision.
404  *
405  * When ARM_SMCCC_ARCH_SOC_ID is not present, returns SMCCC_RET_NOT_SUPPORTED.
406  */
407 s32 arm_smccc_get_soc_id_revision(void);
408 
409 /**
410  * struct arm_smccc_res - Result from SMC/HVC call
411  * @a0-a3 result values from registers 0 to 3
412  */
413 struct arm_smccc_res {
414 	unsigned long a0;
415 	unsigned long a1;
416 	unsigned long a2;
417 	unsigned long a3;
418 };
419 
420 #ifdef CONFIG_ARM64
421 /**
422  * struct arm_smccc_1_2_regs - Arguments for or Results from SMC/HVC call
423  * @a0-a17 argument values from registers 0 to 17
424  */
425 struct arm_smccc_1_2_regs {
426 	unsigned long a0;
427 	unsigned long a1;
428 	unsigned long a2;
429 	unsigned long a3;
430 	unsigned long a4;
431 	unsigned long a5;
432 	unsigned long a6;
433 	unsigned long a7;
434 	unsigned long a8;
435 	unsigned long a9;
436 	unsigned long a10;
437 	unsigned long a11;
438 	unsigned long a12;
439 	unsigned long a13;
440 	unsigned long a14;
441 	unsigned long a15;
442 	unsigned long a16;
443 	unsigned long a17;
444 };
445 
446 /**
447  * arm_smccc_1_2_hvc() - make HVC calls
448  * @args: arguments passed via struct arm_smccc_1_2_regs
449  * @res: result values via struct arm_smccc_1_2_regs
450  *
451  * This function is used to make HVC calls following SMC Calling Convention
452  * v1.2 or above. The content of the supplied param are copied from the
453  * structure to registers prior to the HVC instruction. The return values
454  * are updated with the content from registers on return from the HVC
455  * instruction.
456  */
457 asmlinkage void arm_smccc_1_2_hvc(const struct arm_smccc_1_2_regs *args,
458 				  struct arm_smccc_1_2_regs *res);
459 
460 /**
461  * arm_smccc_1_2_smc() - make SMC calls
462  * @args: arguments passed via struct arm_smccc_1_2_regs
463  * @res: result values via struct arm_smccc_1_2_regs
464  *
465  * This function is used to make SMC calls following SMC Calling Convention
466  * v1.2 or above. The content of the supplied param are copied from the
467  * structure to registers prior to the SMC instruction. The return values
468  * are updated with the content from registers on return from the SMC
469  * instruction.
470  */
471 asmlinkage void arm_smccc_1_2_smc(const struct arm_smccc_1_2_regs *args,
472 				  struct arm_smccc_1_2_regs *res);
473 #endif
474 
475 /**
476  * struct arm_smccc_quirk - Contains quirk information
477  * @id: quirk identification
478  * @state: quirk specific information
479  * @a6: Qualcomm quirk entry for returning post-smc call contents of a6
480  */
481 struct arm_smccc_quirk {
482 	int	id;
483 	union {
484 		unsigned long a6;
485 	} state;
486 };
487 
488 /**
489  * __arm_smccc_smc() - make SMC calls
490  * @a0-a7: arguments passed in registers 0 to 7
491  * @res: result values from registers 0 to 3
492  * @quirk: points to an arm_smccc_quirk, or NULL when no quirks are required.
493  *
494  * This function is used to make SMC calls following SMC Calling Convention.
495  * The content of the supplied param are copied to registers 0 to 7 prior
496  * to the SMC instruction. The return values are updated with the content
497  * from register 0 to 3 on return from the SMC instruction.  An optional
498  * quirk structure provides vendor specific behavior.
499  */
500 #ifdef CONFIG_HAVE_ARM_SMCCC
501 asmlinkage void __arm_smccc_smc(unsigned long a0, unsigned long a1,
502 			unsigned long a2, unsigned long a3, unsigned long a4,
503 			unsigned long a5, unsigned long a6, unsigned long a7,
504 			struct arm_smccc_res *res, struct arm_smccc_quirk *quirk);
505 #else
__arm_smccc_smc(unsigned long a0,unsigned long a1,unsigned long a2,unsigned long a3,unsigned long a4,unsigned long a5,unsigned long a6,unsigned long a7,struct arm_smccc_res * res,struct arm_smccc_quirk * quirk)506 static inline void __arm_smccc_smc(unsigned long a0, unsigned long a1,
507 			unsigned long a2, unsigned long a3, unsigned long a4,
508 			unsigned long a5, unsigned long a6, unsigned long a7,
509 			struct arm_smccc_res *res, struct arm_smccc_quirk *quirk)
510 {
511 	*res = (struct arm_smccc_res){};
512 }
513 #endif
514 
515 /**
516  * __arm_smccc_hvc() - make HVC calls
517  * @a0-a7: arguments passed in registers 0 to 7
518  * @res: result values from registers 0 to 3
519  * @quirk: points to an arm_smccc_quirk, or NULL when no quirks are required.
520  *
521  * This function is used to make HVC calls following SMC Calling
522  * Convention.  The content of the supplied param are copied to registers 0
523  * to 7 prior to the HVC instruction. The return values are updated with
524  * the content from register 0 to 3 on return from the HVC instruction.  An
525  * optional quirk structure provides vendor specific behavior.
526  */
527 asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,
528 			unsigned long a2, unsigned long a3, unsigned long a4,
529 			unsigned long a5, unsigned long a6, unsigned long a7,
530 			struct arm_smccc_res *res, struct arm_smccc_quirk *quirk);
531 
532 #define arm_smccc_smc(...) __arm_smccc_smc(__VA_ARGS__, NULL)
533 
534 #define arm_smccc_smc_quirk(...) __arm_smccc_smc(__VA_ARGS__)
535 
536 #define arm_smccc_hvc(...) __arm_smccc_hvc(__VA_ARGS__, NULL)
537 
538 #define arm_smccc_hvc_quirk(...) __arm_smccc_hvc(__VA_ARGS__)
539 
540 /* SMCCC v1.1 implementation madness follows */
541 #ifdef CONFIG_ARM64
542 
543 #define SMCCC_SMC_INST	"smc	#0"
544 #define SMCCC_HVC_INST	"hvc	#0"
545 
546 #elif defined(CONFIG_ARM)
547 #include <asm/opcodes-sec.h>
548 #include <asm/opcodes-virt.h>
549 
550 #define SMCCC_SMC_INST	__SMC(0)
551 #define SMCCC_HVC_INST	__HVC(0)
552 
553 #endif
554 
555 #define __constraint_read_2	"r" (arg0)
556 #define __constraint_read_3	__constraint_read_2, "r" (arg1)
557 #define __constraint_read_4	__constraint_read_3, "r" (arg2)
558 #define __constraint_read_5	__constraint_read_4, "r" (arg3)
559 #define __constraint_read_6	__constraint_read_5, "r" (arg4)
560 #define __constraint_read_7	__constraint_read_6, "r" (arg5)
561 #define __constraint_read_8	__constraint_read_7, "r" (arg6)
562 #define __constraint_read_9	__constraint_read_8, "r" (arg7)
563 
564 #define __declare_arg_2(a0, res)					\
565 	struct arm_smccc_res   *___res = res;				\
566 	register unsigned long arg0 asm("r0") = (u32)a0
567 
568 #define __declare_arg_3(a0, a1, res)					\
569 	typeof(a1) __a1 = a1;						\
570 	struct arm_smccc_res   *___res = res;				\
571 	register unsigned long arg0 asm("r0") = (u32)a0;			\
572 	register typeof(a1) arg1 asm("r1") = __a1
573 
574 #define __declare_arg_4(a0, a1, a2, res)				\
575 	typeof(a1) __a1 = a1;						\
576 	typeof(a2) __a2 = a2;						\
577 	struct arm_smccc_res   *___res = res;				\
578 	register unsigned long arg0 asm("r0") = (u32)a0;			\
579 	register typeof(a1) arg1 asm("r1") = __a1;			\
580 	register typeof(a2) arg2 asm("r2") = __a2
581 
582 #define __declare_arg_5(a0, a1, a2, a3, res)				\
583 	typeof(a1) __a1 = a1;						\
584 	typeof(a2) __a2 = a2;						\
585 	typeof(a3) __a3 = a3;						\
586 	struct arm_smccc_res   *___res = res;				\
587 	register unsigned long arg0 asm("r0") = (u32)a0;			\
588 	register typeof(a1) arg1 asm("r1") = __a1;			\
589 	register typeof(a2) arg2 asm("r2") = __a2;			\
590 	register typeof(a3) arg3 asm("r3") = __a3
591 
592 #define __declare_arg_6(a0, a1, a2, a3, a4, res)			\
593 	typeof(a4) __a4 = a4;						\
594 	__declare_arg_5(a0, a1, a2, a3, res);				\
595 	register typeof(a4) arg4 asm("r4") = __a4
596 
597 #define __declare_arg_7(a0, a1, a2, a3, a4, a5, res)			\
598 	typeof(a5) __a5 = a5;						\
599 	__declare_arg_6(a0, a1, a2, a3, a4, res);			\
600 	register typeof(a5) arg5 asm("r5") = __a5
601 
602 #define __declare_arg_8(a0, a1, a2, a3, a4, a5, a6, res)		\
603 	typeof(a6) __a6 = a6;						\
604 	__declare_arg_7(a0, a1, a2, a3, a4, a5, res);			\
605 	register typeof(a6) arg6 asm("r6") = __a6
606 
607 #define __declare_arg_9(a0, a1, a2, a3, a4, a5, a6, a7, res)		\
608 	typeof(a7) __a7 = a7;						\
609 	__declare_arg_8(a0, a1, a2, a3, a4, a5, a6, res);		\
610 	register typeof(a7) arg7 asm("r7") = __a7
611 
612 /*
613  * We have an output list that is not necessarily used, and GCC feels
614  * entitled to optimise the whole sequence away. "volatile" is what
615  * makes it stick.
616  */
617 #define __arm_smccc_1_1(inst, ...)					\
618 	do {								\
619 		register unsigned long r0 asm("r0");			\
620 		register unsigned long r1 asm("r1");			\
621 		register unsigned long r2 asm("r2");			\
622 		register unsigned long r3 asm("r3"); 			\
623 		CONCATENATE(__declare_arg_,				\
624 			    COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__);	\
625 		asm volatile(inst "\n" :				\
626 			     "=r" (r0), "=r" (r1), "=r" (r2), "=r" (r3)	\
627 			     : CONCATENATE(__constraint_read_,		\
628 					   COUNT_ARGS(__VA_ARGS__))	\
629 			     : "memory");				\
630 		if (___res)						\
631 			*___res = (typeof(*___res)){r0, r1, r2, r3};	\
632 	} while (0)
633 
634 /*
635  * arm_smccc_1_1_smc() - make an SMCCC v1.1 compliant SMC call
636  *
637  * This is a variadic macro taking one to eight source arguments, and
638  * an optional return structure.
639  *
640  * @a0-a7: arguments passed in registers 0 to 7
641  * @res: result values from registers 0 to 3
642  *
643  * This macro is used to make SMC calls following SMC Calling Convention v1.1.
644  * The content of the supplied param are copied to registers 0 to 7 prior
645  * to the SMC instruction. The return values are updated with the content
646  * from register 0 to 3 on return from the SMC instruction if not NULL.
647  */
648 #define arm_smccc_1_1_smc(...)	__arm_smccc_1_1(SMCCC_SMC_INST, __VA_ARGS__)
649 
650 /*
651  * arm_smccc_1_1_hvc() - make an SMCCC v1.1 compliant HVC call
652  *
653  * This is a variadic macro taking one to eight source arguments, and
654  * an optional return structure.
655  *
656  * @a0-a7: arguments passed in registers 0 to 7
657  * @res: result values from registers 0 to 3
658  *
659  * This macro is used to make HVC calls following SMC Calling Convention v1.1.
660  * The content of the supplied param are copied to registers 0 to 7 prior
661  * to the HVC instruction. The return values are updated with the content
662  * from register 0 to 3 on return from the HVC instruction if not NULL.
663  */
664 #define arm_smccc_1_1_hvc(...)	__arm_smccc_1_1(SMCCC_HVC_INST, __VA_ARGS__)
665 
666 /*
667  * Like arm_smccc_1_1* but always returns SMCCC_RET_NOT_SUPPORTED.
668  * Used when the SMCCC conduit is not defined. The empty asm statement
669  * avoids compiler warnings about unused variables.
670  */
671 #define __fail_smccc_1_1(...)						\
672 	do {								\
673 		CONCATENATE(__declare_arg_,				\
674 			    COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__);	\
675 		asm ("" :						\
676 		     : CONCATENATE(__constraint_read_,			\
677 				   COUNT_ARGS(__VA_ARGS__))		\
678 		     : "memory");					\
679 		if (___res)						\
680 			___res->a0 = SMCCC_RET_NOT_SUPPORTED;		\
681 	} while (0)
682 
683 /*
684  * arm_smccc_1_1_invoke() - make an SMCCC v1.1 compliant call
685  *
686  * This is a variadic macro taking one to eight source arguments, and
687  * an optional return structure.
688  *
689  * @a0-a7: arguments passed in registers 0 to 7
690  * @res: result values from registers 0 to 3
691  *
692  * This macro will make either an HVC call or an SMC call depending on the
693  * current SMCCC conduit. If no valid conduit is available then -1
694  * (SMCCC_RET_NOT_SUPPORTED) is returned in @res.a0 (if supplied).
695  *
696  * The return value also provides the conduit that was used.
697  */
698 #define arm_smccc_1_1_invoke(...) ({					\
699 		int method = arm_smccc_1_1_get_conduit();		\
700 		switch (method) {					\
701 		case SMCCC_CONDUIT_HVC:					\
702 			arm_smccc_1_1_hvc(__VA_ARGS__);			\
703 			break;						\
704 		case SMCCC_CONDUIT_SMC:					\
705 			arm_smccc_1_1_smc(__VA_ARGS__);			\
706 			break;						\
707 		default:						\
708 			__fail_smccc_1_1(__VA_ARGS__);			\
709 			method = SMCCC_CONDUIT_NONE;			\
710 			break;						\
711 		}							\
712 		method;							\
713 	})
714 
715 #endif /*__ASSEMBLY__*/
716 #endif /*__LINUX_ARM_SMCCC_H*/
717