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
rsi_request_version(unsigned long req,unsigned long * out_lower,unsigned long * out_higher)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
rsi_get_realm_config(struct realm_config * cfg)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
rsi_ipa_state_get(phys_addr_t start,phys_addr_t end,enum ripas * state,phys_addr_t * top)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
rsi_set_addr_range_state(phys_addr_t start,phys_addr_t end,enum ripas state,unsigned long flags,phys_addr_t * top)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 /**
92 * rsi_attestation_token_init - Initialise the operation to retrieve an
93 * attestation token.
94 *
95 * @challenge: The challenge data to be used in the attestation token
96 * generation.
97 * @size: Size of the challenge data in bytes.
98 *
99 * Initialises the attestation token generation and returns an upper bound
100 * on the attestation token size that can be used to allocate an adequate
101 * buffer. The caller is expected to subsequently call
102 * rsi_attestation_token_continue() to retrieve the attestation token data on
103 * the same CPU.
104 *
105 * Returns:
106 * On success, returns the upper limit of the attestation report size.
107 * Otherwise, -EINVAL
108 */
109 static inline long
rsi_attestation_token_init(const u8 * challenge,unsigned long size)110 rsi_attestation_token_init(const u8 *challenge, unsigned long size)
111 {
112 struct arm_smccc_1_2_regs regs = { 0 };
113
114 /* The challenge must be at least 32bytes and at most 64bytes */
115 if (!challenge || size < 32 || size > 64)
116 return -EINVAL;
117
118 regs.a0 = SMC_RSI_ATTESTATION_TOKEN_INIT;
119 memcpy(®s.a1, challenge, size);
120 arm_smccc_1_2_smc(®s, ®s);
121
122 if (regs.a0 == RSI_SUCCESS)
123 return regs.a1;
124
125 return -EINVAL;
126 }
127
128 /**
129 * rsi_attestation_token_continue - Continue the operation to retrieve an
130 * attestation token.
131 *
132 * @granule: {I}PA of the Granule to which the token will be written.
133 * @offset: Offset within Granule to start of buffer in bytes.
134 * @size: The size of the buffer.
135 * @len: The number of bytes written to the buffer.
136 *
137 * Retrieves up to a RSI_GRANULE_SIZE worth of token data per call. The caller
138 * is expected to call rsi_attestation_token_init() before calling this
139 * function to retrieve the attestation token.
140 *
141 * Return:
142 * * %RSI_SUCCESS - Attestation token retrieved successfully.
143 * * %RSI_INCOMPLETE - Token generation is not complete.
144 * * %RSI_ERROR_INPUT - A parameter was not valid.
145 * * %RSI_ERROR_STATE - Attestation not in progress.
146 */
rsi_attestation_token_continue(phys_addr_t granule,unsigned long offset,unsigned long size,unsigned long * len)147 static inline unsigned long rsi_attestation_token_continue(phys_addr_t granule,
148 unsigned long offset,
149 unsigned long size,
150 unsigned long *len)
151 {
152 struct arm_smccc_res res;
153
154 arm_smccc_1_1_invoke(SMC_RSI_ATTESTATION_TOKEN_CONTINUE,
155 granule, offset, size, 0, &res);
156
157 if (len)
158 *len = res.a1;
159 return res.a0;
160 }
161
162 #endif /* __ASM_RSI_CMDS_H */
163