xref: /linux/arch/arm64/include/asm/rsi_cmds.h (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2023 ARM Ltd.
4  */
5 
6 #ifndef __ASM_RSI_CMDS_H
7 #define __ASM_RSI_CMDS_H
8 
9 #include <linux/arm-smccc.h>
10 #include <linux/string.h>
11 #include <asm/memory.h>
12 
13 #include <asm/rsi_smc.h>
14 
15 #define RSI_GRANULE_SHIFT		12
16 #define RSI_GRANULE_SIZE		(_AC(1, UL) << RSI_GRANULE_SHIFT)
17 
18 enum ripas {
19 	RSI_RIPAS_EMPTY = 0,
20 	RSI_RIPAS_RAM = 1,
21 	RSI_RIPAS_DESTROYED = 2,
22 	RSI_RIPAS_DEV = 3,
23 };
24 
25 static inline unsigned long rsi_request_version(unsigned long req,
26 						unsigned long *out_lower,
27 						unsigned long *out_higher)
28 {
29 	struct arm_smccc_res res;
30 
31 	arm_smccc_smc(SMC_RSI_ABI_VERSION, req, 0, 0, 0, 0, 0, 0, &res);
32 
33 	if (out_lower)
34 		*out_lower = res.a1;
35 	if (out_higher)
36 		*out_higher = res.a2;
37 
38 	return res.a0;
39 }
40 
41 static inline unsigned long rsi_get_realm_config(struct realm_config *cfg)
42 {
43 	struct arm_smccc_res res;
44 
45 	arm_smccc_smc(SMC_RSI_REALM_CONFIG, virt_to_phys(cfg),
46 		      0, 0, 0, 0, 0, 0, &res);
47 	return res.a0;
48 }
49 
50 static inline unsigned long rsi_ipa_state_get(phys_addr_t start,
51 					      phys_addr_t end,
52 					      enum ripas *state,
53 					      phys_addr_t *top)
54 {
55 	struct arm_smccc_res res;
56 
57 	arm_smccc_smc(SMC_RSI_IPA_STATE_GET,
58 		      start, end, 0, 0, 0, 0, 0,
59 		      &res);
60 
61 	if (res.a0 == RSI_SUCCESS) {
62 		if (top)
63 			*top = res.a1;
64 		if (state)
65 			*state = res.a2;
66 	}
67 
68 	return res.a0;
69 }
70 
71 static inline long rsi_set_addr_range_state(phys_addr_t start,
72 					    phys_addr_t end,
73 					    enum ripas state,
74 					    unsigned long flags,
75 					    phys_addr_t *top)
76 {
77 	struct arm_smccc_res res;
78 
79 	arm_smccc_smc(SMC_RSI_IPA_STATE_SET, start, end, state,
80 		      flags, 0, 0, 0, &res);
81 
82 	if (top)
83 		*top = res.a1;
84 
85 	if (res.a2 != RSI_ACCEPT)
86 		return -EPERM;
87 
88 	return res.a0;
89 }
90 
91 #define RSI_ATTEST_CHALLENGE_MIN_SIZE	32
92 #define RSI_ATTEST_CHALLENGE_MAX_SIZE	64
93 
94 struct rsi_attestation_token_init_args {
95 	unsigned long fid;
96 	u8 challenge[RSI_ATTEST_CHALLENGE_MAX_SIZE];
97 };
98 
99 /**
100  * rsi_attestation_token_init - Initialise the operation to retrieve an
101  * attestation token.
102  *
103  * @challenge:	The challenge data to be used in the attestation token
104  *		generation.
105  * @size:	Size of the challenge data in bytes.
106  *
107  * Initialises the attestation token generation and returns an upper bound
108  * on the attestation token size that can be used to allocate an adequate
109  * buffer. The caller is expected to subsequently call
110  * rsi_attestation_token_continue() to retrieve the attestation token data on
111  * the same CPU.
112  *
113  * Returns:
114  *  On success, returns the upper limit of the attestation report size.
115  *  Otherwise, -EINVAL
116  */
117 static inline long
118 rsi_attestation_token_init(const u8 *challenge, unsigned long size)
119 {
120 	union {
121 		struct arm_smccc_1_2_regs regs;
122 		struct rsi_attestation_token_init_args init;
123 	} args = { 0 };
124 
125 	if (!challenge || size < RSI_ATTEST_CHALLENGE_MIN_SIZE ||
126 	    size > RSI_ATTEST_CHALLENGE_MAX_SIZE)
127 		return -EINVAL;
128 
129 	args.init.fid = SMC_RSI_ATTESTATION_TOKEN_INIT;
130 	memcpy(args.init.challenge, challenge, size);
131 	arm_smccc_1_2_smc(&args.regs, &args.regs);
132 
133 	if (args.regs.a0 == RSI_SUCCESS)
134 		return args.regs.a1;
135 
136 	return -EINVAL;
137 }
138 
139 /**
140  * rsi_attestation_token_continue - Continue the operation to retrieve an
141  * attestation token.
142  *
143  * @granule: {I}PA of the Granule to which the token will be written.
144  * @offset:  Offset within Granule to start of buffer in bytes.
145  * @size:    The size of the buffer.
146  * @len:     The number of bytes written to the buffer.
147  *
148  * Retrieves up to a RSI_GRANULE_SIZE worth of token data per call. The caller
149  * is expected to call rsi_attestation_token_init() before calling this
150  * function to retrieve the attestation token.
151  *
152  * Return:
153  * * %RSI_SUCCESS     - Attestation token retrieved successfully.
154  * * %RSI_INCOMPLETE  - Token generation is not complete.
155  * * %RSI_ERROR_INPUT - A parameter was not valid.
156  * * %RSI_ERROR_STATE - Attestation not in progress.
157  */
158 static inline unsigned long rsi_attestation_token_continue(phys_addr_t granule,
159 							   unsigned long offset,
160 							   unsigned long size,
161 							   unsigned long *len)
162 {
163 	struct arm_smccc_res res;
164 
165 	arm_smccc_1_1_invoke(SMC_RSI_ATTESTATION_TOKEN_CONTINUE,
166 			     granule, offset, size, 0, &res);
167 
168 	if (len)
169 		*len = res.a1;
170 	return res.a0;
171 }
172 
173 #endif /* __ASM_RSI_CMDS_H */
174