Skip to content

Commit a4a4914

Browse files
committed
Merge branch 'for-next/smccc' into for-next/core
Add support for versions v1.2 and 1.3 of the SMC calling convention. * for-next/smccc: arm64: smccc: Support SMCCC v1.3 SVE register saving hint arm64: smccc: Add support for SMCCCv1.2 extended input/output registers
2 parents 26a0f50 + cfa7ff9 commit a4a4914

File tree

4 files changed

+182
-2
lines changed

4 files changed

+182
-2
lines changed

arch/arm64/kernel/asm-offsets.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ int main(void)
140140
DEFINE(ARM_SMCCC_RES_X2_OFFS, offsetof(struct arm_smccc_res, a2));
141141
DEFINE(ARM_SMCCC_QUIRK_ID_OFFS, offsetof(struct arm_smccc_quirk, id));
142142
DEFINE(ARM_SMCCC_QUIRK_STATE_OFFS, offsetof(struct arm_smccc_quirk, state));
143+
DEFINE(ARM_SMCCC_1_2_REGS_X0_OFFS, offsetof(struct arm_smccc_1_2_regs, a0));
144+
DEFINE(ARM_SMCCC_1_2_REGS_X2_OFFS, offsetof(struct arm_smccc_1_2_regs, a2));
145+
DEFINE(ARM_SMCCC_1_2_REGS_X4_OFFS, offsetof(struct arm_smccc_1_2_regs, a4));
146+
DEFINE(ARM_SMCCC_1_2_REGS_X6_OFFS, offsetof(struct arm_smccc_1_2_regs, a6));
147+
DEFINE(ARM_SMCCC_1_2_REGS_X8_OFFS, offsetof(struct arm_smccc_1_2_regs, a8));
148+
DEFINE(ARM_SMCCC_1_2_REGS_X10_OFFS, offsetof(struct arm_smccc_1_2_regs, a10));
149+
DEFINE(ARM_SMCCC_1_2_REGS_X12_OFFS, offsetof(struct arm_smccc_1_2_regs, a12));
150+
DEFINE(ARM_SMCCC_1_2_REGS_X14_OFFS, offsetof(struct arm_smccc_1_2_regs, a14));
151+
DEFINE(ARM_SMCCC_1_2_REGS_X16_OFFS, offsetof(struct arm_smccc_1_2_regs, a16));
143152
BLANK();
144153
DEFINE(HIBERN_PBE_ORIG, offsetof(struct pbe, orig_address));
145154
DEFINE(HIBERN_PBE_ADDR, offsetof(struct pbe, address));

arch/arm64/kernel/smccc-call.S

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,34 @@
77

88
#include <asm/asm-offsets.h>
99
#include <asm/assembler.h>
10+
#include <asm/thread_info.h>
11+
12+
/*
13+
* If we have SMCCC v1.3 and (as is likely) no SVE state in
14+
* the registers then set the SMCCC hint bit to say there's no
15+
* need to preserve it. Do this by directly adjusting the SMCCC
16+
* function value which is already stored in x0 ready to be called.
17+
*/
18+
SYM_FUNC_START(__arm_smccc_sve_check)
19+
20+
ldr_l x16, smccc_has_sve_hint
21+
cbz x16, 2f
22+
23+
get_current_task x16
24+
ldr x16, [x16, #TSK_TI_FLAGS]
25+
tbnz x16, #TIF_FOREIGN_FPSTATE, 1f // Any live FP state?
26+
tbnz x16, #TIF_SVE, 2f // Does that state include SVE?
27+
28+
1: orr x0, x0, ARM_SMCCC_1_3_SVE_HINT
29+
30+
2: ret
31+
SYM_FUNC_END(__arm_smccc_sve_check)
32+
EXPORT_SYMBOL(__arm_smccc_sve_check)
1033

1134
.macro SMCCC instr
35+
alternative_if ARM64_SVE
36+
bl __arm_smccc_sve_check
37+
alternative_else_nop_endif
1238
\instr #0
1339
ldr x4, [sp]
1440
stp x0, x1, [x4, #ARM_SMCCC_RES_X0_OFFS]
@@ -43,3 +69,60 @@ SYM_FUNC_START(__arm_smccc_hvc)
4369
SMCCC hvc
4470
SYM_FUNC_END(__arm_smccc_hvc)
4571
EXPORT_SYMBOL(__arm_smccc_hvc)
72+
73+
.macro SMCCC_1_2 instr
74+
/* Save `res` and free a GPR that won't be clobbered */
75+
stp x1, x19, [sp, #-16]!
76+
77+
/* Ensure `args` won't be clobbered while loading regs in next step */
78+
mov x19, x0
79+
80+
/* Load the registers x0 - x17 from the struct arm_smccc_1_2_regs */
81+
ldp x0, x1, [x19, #ARM_SMCCC_1_2_REGS_X0_OFFS]
82+
ldp x2, x3, [x19, #ARM_SMCCC_1_2_REGS_X2_OFFS]
83+
ldp x4, x5, [x19, #ARM_SMCCC_1_2_REGS_X4_OFFS]
84+
ldp x6, x7, [x19, #ARM_SMCCC_1_2_REGS_X6_OFFS]
85+
ldp x8, x9, [x19, #ARM_SMCCC_1_2_REGS_X8_OFFS]
86+
ldp x10, x11, [x19, #ARM_SMCCC_1_2_REGS_X10_OFFS]
87+
ldp x12, x13, [x19, #ARM_SMCCC_1_2_REGS_X12_OFFS]
88+
ldp x14, x15, [x19, #ARM_SMCCC_1_2_REGS_X14_OFFS]
89+
ldp x16, x17, [x19, #ARM_SMCCC_1_2_REGS_X16_OFFS]
90+
91+
\instr #0
92+
93+
/* Load the `res` from the stack */
94+
ldr x19, [sp]
95+
96+
/* Store the registers x0 - x17 into the result structure */
97+
stp x0, x1, [x19, #ARM_SMCCC_1_2_REGS_X0_OFFS]
98+
stp x2, x3, [x19, #ARM_SMCCC_1_2_REGS_X2_OFFS]
99+
stp x4, x5, [x19, #ARM_SMCCC_1_2_REGS_X4_OFFS]
100+
stp x6, x7, [x19, #ARM_SMCCC_1_2_REGS_X6_OFFS]
101+
stp x8, x9, [x19, #ARM_SMCCC_1_2_REGS_X8_OFFS]
102+
stp x10, x11, [x19, #ARM_SMCCC_1_2_REGS_X10_OFFS]
103+
stp x12, x13, [x19, #ARM_SMCCC_1_2_REGS_X12_OFFS]
104+
stp x14, x15, [x19, #ARM_SMCCC_1_2_REGS_X14_OFFS]
105+
stp x16, x17, [x19, #ARM_SMCCC_1_2_REGS_X16_OFFS]
106+
107+
/* Restore original x19 */
108+
ldp xzr, x19, [sp], #16
109+
ret
110+
.endm
111+
112+
/*
113+
* void arm_smccc_1_2_hvc(const struct arm_smccc_1_2_regs *args,
114+
* struct arm_smccc_1_2_regs *res);
115+
*/
116+
SYM_FUNC_START(arm_smccc_1_2_hvc)
117+
SMCCC_1_2 hvc
118+
SYM_FUNC_END(arm_smccc_1_2_hvc)
119+
EXPORT_SYMBOL(arm_smccc_1_2_hvc)
120+
121+
/*
122+
* void arm_smccc_1_2_smc(const struct arm_smccc_1_2_regs *args,
123+
* struct arm_smccc_1_2_regs *res);
124+
*/
125+
SYM_FUNC_START(arm_smccc_1_2_smc)
126+
SMCCC_1_2 smc
127+
SYM_FUNC_END(arm_smccc_1_2_smc)
128+
EXPORT_SYMBOL(arm_smccc_1_2_smc)

drivers/firmware/smccc/smccc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ static u32 smccc_version = ARM_SMCCC_VERSION_1_0;
1515
static enum arm_smccc_conduit smccc_conduit = SMCCC_CONDUIT_NONE;
1616

1717
bool __ro_after_init smccc_trng_available = false;
18+
u64 __ro_after_init smccc_has_sve_hint = false;
1819

1920
void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit)
2021
{
2122
smccc_version = version;
2223
smccc_conduit = conduit;
2324

2425
smccc_trng_available = smccc_probe_trng();
26+
if (IS_ENABLED(CONFIG_ARM64_SVE) &&
27+
smccc_version >= ARM_SMCCC_VERSION_1_3)
28+
smccc_has_sve_hint = true;
2529
}
2630

2731
enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void)

include/linux/arm-smccc.h

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
#define ARM_SMCCC_VERSION_1_0 0x10000
6464
#define ARM_SMCCC_VERSION_1_1 0x10001
6565
#define ARM_SMCCC_VERSION_1_2 0x10002
66+
#define ARM_SMCCC_VERSION_1_3 0x10003
67+
68+
#define ARM_SMCCC_1_3_SVE_HINT 0x10000
6669

6770
#define ARM_SMCCC_VERSION_FUNC_ID \
6871
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
@@ -216,6 +219,8 @@ u32 arm_smccc_get_version(void);
216219

217220
void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit);
218221

222+
extern u64 smccc_has_sve_hint;
223+
219224
/**
220225
* struct arm_smccc_res - Result from SMC/HVC call
221226
* @a0-a3 result values from registers 0 to 3
@@ -227,6 +232,61 @@ struct arm_smccc_res {
227232
unsigned long a3;
228233
};
229234

235+
#ifdef CONFIG_ARM64
236+
/**
237+
* struct arm_smccc_1_2_regs - Arguments for or Results from SMC/HVC call
238+
* @a0-a17 argument values from registers 0 to 17
239+
*/
240+
struct arm_smccc_1_2_regs {
241+
unsigned long a0;
242+
unsigned long a1;
243+
unsigned long a2;
244+
unsigned long a3;
245+
unsigned long a4;
246+
unsigned long a5;
247+
unsigned long a6;
248+
unsigned long a7;
249+
unsigned long a8;
250+
unsigned long a9;
251+
unsigned long a10;
252+
unsigned long a11;
253+
unsigned long a12;
254+
unsigned long a13;
255+
unsigned long a14;
256+
unsigned long a15;
257+
unsigned long a16;
258+
unsigned long a17;
259+
};
260+
261+
/**
262+
* arm_smccc_1_2_hvc() - make HVC calls
263+
* @args: arguments passed via struct arm_smccc_1_2_regs
264+
* @res: result values via struct arm_smccc_1_2_regs
265+
*
266+
* This function is used to make HVC calls following SMC Calling Convention
267+
* v1.2 or above. The content of the supplied param are copied from the
268+
* structure to registers prior to the HVC instruction. The return values
269+
* are updated with the content from registers on return from the HVC
270+
* instruction.
271+
*/
272+
asmlinkage void arm_smccc_1_2_hvc(const struct arm_smccc_1_2_regs *args,
273+
struct arm_smccc_1_2_regs *res);
274+
275+
/**
276+
* arm_smccc_1_2_smc() - make SMC calls
277+
* @args: arguments passed via struct arm_smccc_1_2_regs
278+
* @res: result values via struct arm_smccc_1_2_regs
279+
*
280+
* This function is used to make SMC calls following SMC Calling Convention
281+
* v1.2 or above. The content of the supplied param are copied from the
282+
* structure to registers prior to the SMC instruction. The return values
283+
* are updated with the content from registers on return from the SMC
284+
* instruction.
285+
*/
286+
asmlinkage void arm_smccc_1_2_smc(const struct arm_smccc_1_2_regs *args,
287+
struct arm_smccc_1_2_regs *res);
288+
#endif
289+
230290
/**
231291
* struct arm_smccc_quirk - Contains quirk information
232292
* @id: quirk identification
@@ -240,6 +300,15 @@ struct arm_smccc_quirk {
240300
} state;
241301
};
242302

303+
/**
304+
* __arm_smccc_sve_check() - Set the SVE hint bit when doing SMC calls
305+
*
306+
* Sets the SMCCC hint bit to indicate if there is live state in the SVE
307+
* registers, this modifies x0 in place and should never be called from C
308+
* code.
309+
*/
310+
asmlinkage unsigned long __arm_smccc_sve_check(unsigned long x0);
311+
243312
/**
244313
* __arm_smccc_smc() - make SMC calls
245314
* @a0-a7: arguments passed in registers 0 to 7
@@ -297,6 +366,20 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,
297366

298367
#endif
299368

369+
/* nVHE hypervisor doesn't have a current thread so needs separate checks */
370+
#if defined(CONFIG_ARM64_SVE) && !defined(__KVM_NVHE_HYPERVISOR__)
371+
372+
#define SMCCC_SVE_CHECK ALTERNATIVE("nop \n", "bl __arm_smccc_sve_check \n", \
373+
ARM64_SVE)
374+
#define smccc_sve_clobbers "x16", "x30", "cc",
375+
376+
#else
377+
378+
#define SMCCC_SVE_CHECK
379+
#define smccc_sve_clobbers
380+
381+
#endif
382+
300383
#define ___count_args(_0, _1, _2, _3, _4, _5, _6, _7, _8, x, ...) x
301384

302385
#define __count_args(...) \
@@ -364,7 +447,7 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,
364447

365448
#define ___constraints(count) \
366449
: __constraint_read_ ## count \
367-
: "memory"
450+
: smccc_sve_clobbers "memory"
368451
#define __constraints(count) ___constraints(count)
369452

370453
/*
@@ -379,7 +462,8 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,
379462
register unsigned long r2 asm("r2"); \
380463
register unsigned long r3 asm("r3"); \
381464
__declare_args(__count_args(__VA_ARGS__), __VA_ARGS__); \
382-
asm volatile(inst "\n" : \
465+
asm volatile(SMCCC_SVE_CHECK \
466+
inst "\n" : \
383467
"=r" (r0), "=r" (r1), "=r" (r2), "=r" (r3) \
384468
__constraints(__count_args(__VA_ARGS__))); \
385469
if (___res) \

0 commit comments

Comments
 (0)